| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | **************************************************************************/ |
| 21 | |
| 22 | class PlErrorReport |
| 23 | { |
| 24 | public $date; |
| 25 | public $error; |
| 26 | public $state; |
| 27 | |
| 28 | private function __construct($date, $error, $state = array()) |
| 29 | { |
| 30 | $this->date = $date; |
| 31 | $this->error = "" . $error; |
| 32 | $this->state = $state; |
| 33 | } |
| 34 | |
| 35 | private function toCSV() |
| 36 | { |
| 37 | return array($this->date, $this->error, serialize($this->state)); |
| 38 | } |
| 39 | |
| 40 | public function fromCSV(array $entry) |
| 41 | { |
| 42 | return new PlErrorReport($entry[0], $entry[1], unserialize($entry[2])); |
| 43 | } |
| 44 | |
| 45 | public static function report($error) |
| 46 | { |
| 47 | $error = new PlErrorReport(date('Y-m-d G:i:s'), $error, |
| 48 | array('Session' => $_SESSION, |
| 49 | 'Env' => $_REQUEST, |
| 50 | 'Post' => $_POST, |
| 51 | 'Get' => $_GET, |
| 52 | 'Cookie' => $_COOKIE, |
| 53 | 'Server' => $_SERVER)); |
| 54 | |
| 55 | $file = fopen(Platal::globals()->spoolroot . '/spool/tmp/site_errors', 'a'); |
| 56 | fputcsv($file, $error->toCSV()); |
| 57 | fclose($file); |
| 58 | } |
| 59 | |
| 60 | public static function iterate() |
| 61 | { |
| 62 | return new PlErrorReportIterator(); |
| 63 | } |
| 64 | |
| 65 | public static function clear() |
| 66 | { |
| 67 | @unlink(Platal::globals()->spoolroot . '/spool/tmp/site_errors'); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | class PlErrorReportIterator implements PlIterator |
| 72 | { |
| 73 | private $file; |
| 74 | |
| 75 | public function __construct() |
| 76 | { |
| 77 | $this->file = fopen(Platal::globals()->spoolroot . '/spool/tmp/site_errors', 'r'); |
| 78 | } |
| 79 | |
| 80 | public function next() |
| 81 | { |
| 82 | if (!$this->file) { |
| 83 | return null; |
| 84 | } |
| 85 | $entry = fgetcsv($this->file); |
| 86 | if ($entry === false) { |
| 87 | fclose($this->file); |
| 88 | $this->file = null; |
| 89 | return null; |
| 90 | } |
| 91 | $value = PlErrorReport::fromCSV($entry); |
| 92 | return $value; |
| 93 | } |
| 94 | |
| 95 | public function total() |
| 96 | { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | public function first() |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | public function last() |
| 106 | { |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 112 | ?> |