Commit | Line | Data |
---|---|---|
40b79bfc FB |
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 | { | |
2db8aebd SJ |
77 | $file = Platal::globals()->spoolroot . '/spool/tmp/site_errors'; |
78 | if (file_exists($file)) { | |
79 | $this->file = fopen($file, 'r'); | |
80 | } else { | |
81 | $this->file = null; | |
82 | } | |
40b79bfc FB |
83 | } |
84 | ||
85 | public function next() | |
86 | { | |
87 | if (!$this->file) { | |
88 | return null; | |
89 | } | |
90 | $entry = fgetcsv($this->file); | |
91 | if ($entry === false) { | |
92 | fclose($this->file); | |
93 | $this->file = null; | |
94 | return null; | |
95 | } | |
96 | $value = PlErrorReport::fromCSV($entry); | |
97 | return $value; | |
98 | } | |
99 | ||
100 | public function total() | |
101 | { | |
102 | return 0; | |
103 | } | |
104 | ||
105 | public function first() | |
106 | { | |
107 | return false; | |
108 | } | |
109 | ||
110 | public function last() | |
111 | { | |
112 | return false; | |
113 | } | |
114 | } | |
115 | ||
116 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
117 | ?> |