Commit | Line | Data |
---|---|---|
40b79bfc FB |
1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
40b79bfc FB |
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 | ||
3287ae38 | 40 | public static function fromCSV(array $entry) |
40b79bfc FB |
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 | ||
ddc2d3fa FB |
65 | public static function feed(PlPage $page, PlUser $user) |
66 | { | |
67 | $feed = new PlErrorReportFeed(); | |
68 | return $feed->run($page, $user); | |
69 | } | |
70 | ||
40b79bfc FB |
71 | public static function clear() |
72 | { | |
73 | @unlink(Platal::globals()->spoolroot . '/spool/tmp/site_errors'); | |
74 | } | |
75 | } | |
76 | ||
77 | class PlErrorReportIterator implements PlIterator | |
78 | { | |
79 | private $file; | |
80 | ||
81 | public function __construct() | |
82 | { | |
2db8aebd SJ |
83 | $file = Platal::globals()->spoolroot . '/spool/tmp/site_errors'; |
84 | if (file_exists($file)) { | |
85 | $this->file = fopen($file, 'r'); | |
86 | } else { | |
87 | $this->file = null; | |
88 | } | |
40b79bfc FB |
89 | } |
90 | ||
91 | public function next() | |
92 | { | |
93 | if (!$this->file) { | |
94 | return null; | |
95 | } | |
96 | $entry = fgetcsv($this->file); | |
97 | if ($entry === false) { | |
98 | fclose($this->file); | |
99 | $this->file = null; | |
100 | return null; | |
101 | } | |
102 | $value = PlErrorReport::fromCSV($entry); | |
103 | return $value; | |
104 | } | |
105 | ||
106 | public function total() | |
107 | { | |
108 | return 0; | |
109 | } | |
110 | ||
111 | public function first() | |
112 | { | |
113 | return false; | |
114 | } | |
115 | ||
116 | public function last() | |
117 | { | |
118 | return false; | |
119 | } | |
120 | } | |
121 | ||
ddc2d3fa FB |
122 | class PlErrorReportFeed extends PlFeed |
123 | { | |
124 | public function __construct() | |
125 | { | |
126 | global $globals; | |
127 | parent::__construct($globals->core->sitename . ' :: News', | |
128 | $globals->baseurl . '/site_errors', | |
129 | 'Erreurs d\'exécution', | |
130 | $globals->baseurl . '/images/logo.png', | |
5faeba22 | 131 | $globals->coreroot . '/templates/site_errors.feed.tpl'); |
ddc2d3fa FB |
132 | } |
133 | ||
134 | protected function fetch(PlUser $user) | |
135 | { | |
136 | global $globals; | |
137 | $it = PlErrorReport::iterate(); | |
138 | $data = array(); | |
139 | while ($row = $it->next()) { | |
140 | $title = explode("\n", $row->error); | |
141 | $title = $title[0]; | |
142 | $line = array(); | |
143 | $line['id'] = $row->date + count($data); | |
144 | $line['author'] = 'admin'; | |
145 | $line['link'] = $globals->baseurl . '/site_errors'; | |
146 | $line['data'] = $row; | |
147 | $line['title'] = $title; | |
148 | $line['last_modification'] = $row->date; | |
149 | $line['publication'] = $row->date; | |
150 | $data[] = $line; | |
151 | } | |
152 | return PlIteratorUtils::fromArray($data, 1, true); | |
153 | } | |
154 | } | |
155 | ||
fa7ffd66 | 156 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |
40b79bfc | 157 | ?> |