1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | file : $Id: functions_check_integrity.inc.php 2208 2008-02-13 20:13:12Z rub $ |
---|
8 | // | last update : $Date: 2008-02-13 20:13:12 +0000 (Wed, 13 Feb 2008) $ |
---|
9 | // | last modifier : $Author: rub $ |
---|
10 | // | revision : $Revision: 2208 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | /** |
---|
28 | * Check integrity |
---|
29 | * |
---|
30 | * @param void |
---|
31 | * @return void |
---|
32 | */ |
---|
33 | function check_integrity() |
---|
34 | { |
---|
35 | global $page, $header_notes, $conf; |
---|
36 | |
---|
37 | // Ignore list |
---|
38 | $conf_c13y_ignore = unserialize($conf['c13y_ignore']); |
---|
39 | if ( |
---|
40 | is_array($conf_c13y_ignore) and |
---|
41 | isset($conf_c13y_ignore['version']) and |
---|
42 | ($conf_c13y_ignore['version'] == PHPWG_VERSION) and |
---|
43 | is_array($conf_c13y_ignore['list']) |
---|
44 | ) |
---|
45 | { |
---|
46 | $ignore_list_changed = false; |
---|
47 | $page['check_integrity']['ignore_list'] = $conf_c13y_ignore['list']; |
---|
48 | } |
---|
49 | else |
---|
50 | { |
---|
51 | $ignore_list_changed = true; |
---|
52 | $page['check_integrity']['ignore_list'] = array(); |
---|
53 | } |
---|
54 | |
---|
55 | // Retrieve list |
---|
56 | $page['check_integrity']['list'] = array(); |
---|
57 | $page['check_integrity']['build_ignore_list'] = array(); |
---|
58 | |
---|
59 | add_event_handler('list_check_integrity', 'c13y_exif'); |
---|
60 | add_event_handler('list_check_integrity', 'c13y_user'); |
---|
61 | trigger_action('list_check_integrity'); |
---|
62 | |
---|
63 | // Information |
---|
64 | if (count($page['check_integrity']['list']) > 0) |
---|
65 | { |
---|
66 | $header_notes[] = |
---|
67 | l10n_dec('c13y_anomaly_count', 'c13y_anomalies_count', |
---|
68 | count($page['check_integrity']['list'])); |
---|
69 | } |
---|
70 | |
---|
71 | // Treatments |
---|
72 | if (!is_adviser()) |
---|
73 | { |
---|
74 | if (isset($_POST['c13y_submit_correction']) and isset($_POST['c13y_selection'])) |
---|
75 | { |
---|
76 | $corrected_count = 0; |
---|
77 | $not_corrected_count = 0; |
---|
78 | |
---|
79 | foreach ($page['check_integrity']['list'] as $i => $c13y) |
---|
80 | { |
---|
81 | if (!empty($c13y['correction_fct']) and |
---|
82 | $c13y['is_callable'] and |
---|
83 | in_array($c13y['id'], $_POST['c13y_selection'])) |
---|
84 | { |
---|
85 | if (is_array($c13y['correction_fct_args'])) |
---|
86 | { |
---|
87 | $args = $c13y['correction_fct_args']; |
---|
88 | } |
---|
89 | else |
---|
90 | if (!is_null($c13y['correction_fct_args'])) |
---|
91 | { |
---|
92 | $args = array($c13y['correction_fct_args']); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | $args = array(); |
---|
97 | } |
---|
98 | $page['check_integrity']['list'][$i]['corrected'] = call_user_func_array($c13y['correction_fct'], $args); |
---|
99 | |
---|
100 | if ($page['check_integrity']['list'][$i]['corrected']) |
---|
101 | { |
---|
102 | $corrected_count += 1; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | $not_corrected_count += 1; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | if ($corrected_count > 0) |
---|
112 | { |
---|
113 | $page['infos'][] = |
---|
114 | l10n_dec('c13y_anomaly_corrected_count', 'c13y_anomalies_corrected_count', |
---|
115 | $corrected_count); |
---|
116 | } |
---|
117 | if ($not_corrected_count > 0) |
---|
118 | { |
---|
119 | $page['errors'][] = |
---|
120 | l10n_dec('c13y_anomaly_not_corrected_count', 'c13y_anomalies_not_corrected_count', |
---|
121 | $not_corrected_count); |
---|
122 | } |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | if (isset($_POST['c13y_submit_ignore']) and isset($_POST['c13y_selection'])) |
---|
127 | { |
---|
128 | $ignored_count = 0; |
---|
129 | |
---|
130 | foreach ($page['check_integrity']['list'] as $i => $c13y) |
---|
131 | { |
---|
132 | if (in_array($c13y['id'], $_POST['c13y_selection'])) |
---|
133 | { |
---|
134 | $page['check_integrity']['build_ignore_list'][] = $c13y['id']; |
---|
135 | $page['check_integrity']['list'][$i]['ignored'] = true; |
---|
136 | $ignored_count += 1; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | if ($ignored_count > 0) |
---|
141 | { |
---|
142 | $page['infos'][] = |
---|
143 | l10n_dec('c13y_anomaly_ignored_count', 'c13y_anomalies_ignored_count', |
---|
144 | $ignored_count); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | $ignore_list_changed = |
---|
151 | ( |
---|
152 | ($ignore_list_changed) or |
---|
153 | (count(array_diff($page['check_integrity']['ignore_list'], $page['check_integrity']['build_ignore_list'])) > 0) or |
---|
154 | (count(array_diff($page['check_integrity']['build_ignore_list'], $page['check_integrity']['ignore_list'])) > 0) |
---|
155 | ); |
---|
156 | |
---|
157 | if ($ignore_list_changed) |
---|
158 | { |
---|
159 | c13y_update_conf($page['check_integrity']['build_ignore_list']); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Display anomalies list |
---|
165 | * |
---|
166 | * @param void |
---|
167 | * @return void |
---|
168 | */ |
---|
169 | function display_check_integrity() |
---|
170 | { |
---|
171 | global $template, $page; |
---|
172 | |
---|
173 | $check_automatic_correction = false; |
---|
174 | $submit_automatic_correction = false; |
---|
175 | $submit_ignore = false; |
---|
176 | |
---|
177 | if (isset($page['check_integrity']['list']) and count($page['check_integrity']['list']) > 0) |
---|
178 | { |
---|
179 | $template->set_filenames(array('check_integrity' => 'admin/check_integrity.tpl')); |
---|
180 | |
---|
181 | foreach ($page['check_integrity']['list'] as $i => $c13y) |
---|
182 | { |
---|
183 | $can_select = false; |
---|
184 | |
---|
185 | $template->assign_block_vars('c13y', |
---|
186 | array( |
---|
187 | 'CLASS' => ($i % 2 == 1) ? 'row2' : 'row1', |
---|
188 | 'ID' => $c13y['id'], |
---|
189 | 'ANOMALY' => $c13y['anomaly'] |
---|
190 | )); |
---|
191 | |
---|
192 | |
---|
193 | if (isset($c13y['ignored'])) |
---|
194 | { |
---|
195 | if ($c13y['ignored']) |
---|
196 | { |
---|
197 | $template->assign_block_vars('c13y.ignore_msg', array()); |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | die('$c13y[\'ignored\'] cannot be false'); |
---|
202 | } |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | if (!empty($c13y['correction_fct'])) |
---|
207 | { |
---|
208 | if (isset($c13y['corrected'])) |
---|
209 | { |
---|
210 | if ($c13y['corrected']) |
---|
211 | { |
---|
212 | $template->assign_block_vars('c13y.correction_success_fct', array()); |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | $template->assign_block_vars('c13y.correction_error_fct', |
---|
217 | array('WIKI_FOROM_LINKS' => get_htlm_links_more_info())); |
---|
218 | } |
---|
219 | } |
---|
220 | else if ($c13y['is_callable']) |
---|
221 | { |
---|
222 | $template->assign_block_vars('c13y.correction_fct', array()); |
---|
223 | $template->assign_block_vars('c13y_link_check_automatic_correction.c13y_do_check', array('ID' => $c13y['id'])); |
---|
224 | $submit_automatic_correction = true; |
---|
225 | $can_select = true; |
---|
226 | } |
---|
227 | else |
---|
228 | { |
---|
229 | $template->assign_block_vars('c13y.correction_bad_fct', array()); |
---|
230 | $can_select = true; |
---|
231 | } |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | $can_select = true; |
---|
236 | } |
---|
237 | |
---|
238 | if (!empty($c13y['correction_fct']) and !empty($c13y['correction_msg'])) |
---|
239 | { |
---|
240 | $template->assign_block_vars('c13y.br', array()); |
---|
241 | } |
---|
242 | |
---|
243 | if (!empty($c13y['correction_msg']) and !isset($c13y['corrected'])) |
---|
244 | { |
---|
245 | $template->assign_block_vars('c13y.correction_msg', |
---|
246 | array( |
---|
247 | 'DATA' => nl2br($c13y['correction_msg']) |
---|
248 | )); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | if ($can_select) |
---|
253 | { |
---|
254 | $template->assign_block_vars('c13y.can_select', array()); |
---|
255 | $submit_ignore = true; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | if ($submit_automatic_correction) |
---|
260 | { |
---|
261 | $template->assign_block_vars('c13y_submit_automatic_correction', array()); |
---|
262 | } |
---|
263 | |
---|
264 | if ($submit_ignore) |
---|
265 | { |
---|
266 | $template->assign_block_vars('c13y_link_check_uncheck', array()); |
---|
267 | $template->assign_block_vars('c13y_submit_ignore', array()); |
---|
268 | } |
---|
269 | |
---|
270 | $template->concat_var_from_handle('ADMIN_CONTENT', 'check_integrity'); |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * Returns structured anomaly data |
---|
276 | * |
---|
277 | * @param anomaly arguments |
---|
278 | * @return c13y anomaly array |
---|
279 | */ |
---|
280 | function add_c13y($anomaly, $correction_fct = null, $correction_fct_args = null, $correction_msg = null) |
---|
281 | { |
---|
282 | global $page; |
---|
283 | |
---|
284 | $id = md5($anomaly.$correction_fct.serialize($correction_fct_args).$correction_msg); |
---|
285 | |
---|
286 | if (in_array($id, $page['check_integrity']['ignore_list'])) |
---|
287 | { |
---|
288 | $page['check_integrity']['build_ignore_list'][] = $id; |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | $page['check_integrity']['list'][] = |
---|
293 | array( |
---|
294 | 'id' => $id, |
---|
295 | 'anomaly' => $anomaly, |
---|
296 | 'correction_fct' => $correction_fct, |
---|
297 | 'correction_fct_args' => $correction_fct_args, |
---|
298 | 'correction_msg' => $correction_msg, |
---|
299 | 'is_callable' => is_callable($correction_fct)); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Update table config |
---|
305 | * |
---|
306 | * @param ignore list array |
---|
307 | * @return void |
---|
308 | */ |
---|
309 | function c13y_update_conf($ignore_list = array()) |
---|
310 | { |
---|
311 | $conf_c13y_ignore = array(); |
---|
312 | $conf_c13y_ignore['version'] = PHPWG_VERSION; |
---|
313 | $conf_c13y_ignore['list'] = $ignore_list; |
---|
314 | $query = 'update '.CONFIG_TABLE.' set value =\''.serialize($conf_c13y_ignore).'\'where param = \'c13y_ignore\';'; |
---|
315 | pwg_query($query); |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * Apply maintenance |
---|
320 | * |
---|
321 | * @param void |
---|
322 | * @return void |
---|
323 | */ |
---|
324 | function c13y_maintenance() |
---|
325 | { |
---|
326 | c13y_update_conf(); |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * Returns links more informations |
---|
331 | * |
---|
332 | * @param void |
---|
333 | * @return html links |
---|
334 | */ |
---|
335 | function get_htlm_links_more_info() |
---|
336 | { |
---|
337 | $pwg_links = pwg_URL(); |
---|
338 | $link_fmt = '<a href="%s" onclick="window.open(this.href, \'\'); return false;">%s</a>'; |
---|
339 | return |
---|
340 | sprintf |
---|
341 | ( |
---|
342 | l10n('c13y_more_info'), |
---|
343 | sprintf($link_fmt, $pwg_links['FORUM'], l10n('c13y_more_info_forum')), |
---|
344 | sprintf($link_fmt, $pwg_links['WIKI'], l10n('c13y_more_info_wiki')) |
---|
345 | ); |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * Check exif |
---|
350 | * |
---|
351 | * @param void |
---|
352 | * @return void |
---|
353 | */ |
---|
354 | function c13y_exif() |
---|
355 | { |
---|
356 | global $conf; |
---|
357 | |
---|
358 | foreach (array('show_exif', 'use_exif') as $value) |
---|
359 | { |
---|
360 | if (($conf[$value]) and (!function_exists('read_exif_data'))) |
---|
361 | { |
---|
362 | add_c13y( |
---|
363 | sprintf(l10n('c13y_exif_anomaly'), '$conf[\''.$value.'\']'), |
---|
364 | null, |
---|
365 | null, |
---|
366 | sprintf(l10n('c13y_exif_correction'), '$conf[\''.$value.'\']') |
---|
367 | .'<BR />'. |
---|
368 | get_htlm_links_more_info()); |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * Check user |
---|
375 | * |
---|
376 | * @param void |
---|
377 | * @return void |
---|
378 | */ |
---|
379 | function c13y_user() |
---|
380 | { |
---|
381 | global $conf; |
---|
382 | |
---|
383 | $c13y_users = array(); |
---|
384 | $c13y_users[$conf['guest_id']] = array( |
---|
385 | 'status' => 'guest', |
---|
386 | 'l10n_non_existent' => 'c13y_guest_non_existent', |
---|
387 | 'l10n_bad_status' => 'c13y_bad_guest_status'); |
---|
388 | |
---|
389 | if ($conf['guest_id'] != $conf['default_user_id']) |
---|
390 | { |
---|
391 | $c13y_users[$conf['default_user_id']] = array( |
---|
392 | 'password' => null, |
---|
393 | 'l10n_non_existent' => 'c13y_default_non_existent'); |
---|
394 | } |
---|
395 | |
---|
396 | $c13y_users[$conf['webmaster_id']] = array( |
---|
397 | 'status' => 'webmaster', |
---|
398 | 'l10n_non_existent' => 'c13y_webmaster_non_existent', |
---|
399 | 'l10n_bad_status' => 'c13y_bad_webmaster_status'); |
---|
400 | |
---|
401 | $query = ' |
---|
402 | select u.'.$conf['user_fields']['id'].' as id, ui.status |
---|
403 | from '.USERS_TABLE.' as u |
---|
404 | left join '.USER_INFOS_TABLE.' as ui |
---|
405 | on u.'.$conf['user_fields']['id'].' = ui.user_id |
---|
406 | where |
---|
407 | u.'.$conf['user_fields']['id'].' in ('.implode(',', array_keys($c13y_users)).') |
---|
408 | ;'; |
---|
409 | |
---|
410 | |
---|
411 | $status = array(); |
---|
412 | |
---|
413 | $result = pwg_query($query); |
---|
414 | while ($row = mysql_fetch_array($result)) |
---|
415 | { |
---|
416 | $status[$row['id']] = $row['status']; |
---|
417 | } |
---|
418 | |
---|
419 | foreach ($c13y_users as $id => $data) |
---|
420 | { |
---|
421 | if (!array_key_exists($id, $status)) |
---|
422 | { |
---|
423 | add_c13y(l10n($data['l10n_non_existent']), 'c13y_correction_user', |
---|
424 | array('id' => $id, 'action' => 'creation')); |
---|
425 | } |
---|
426 | else |
---|
427 | if (!empty($data['status']) and $status[$id] != $data['status']) |
---|
428 | { |
---|
429 | add_c13y(l10n($data['l10n_bad_status']), 'c13y_correction_user', |
---|
430 | array('id' => $id, 'action' => 'status')); |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * Do correction user |
---|
437 | * |
---|
438 | * @param user_id, action |
---|
439 | * @return boolean true if ok else false |
---|
440 | */ |
---|
441 | function c13y_correction_user($id, $action) |
---|
442 | { |
---|
443 | global $conf, $page; |
---|
444 | |
---|
445 | $result = false; |
---|
446 | |
---|
447 | if (!empty($id)) |
---|
448 | { |
---|
449 | switch ($action) |
---|
450 | { |
---|
451 | case 'creation': |
---|
452 | if ($id == $conf['guest_id']) |
---|
453 | { |
---|
454 | $name = 'guest'; |
---|
455 | $password = null; |
---|
456 | } |
---|
457 | else if ($id == $conf['default_user_id']) |
---|
458 | { |
---|
459 | $name = 'guest'; |
---|
460 | $password = null; |
---|
461 | } |
---|
462 | else if ($id == $conf['webmaster_id']) |
---|
463 | { |
---|
464 | $name = 'webmaster'; |
---|
465 | $password = generate_key(6); |
---|
466 | } |
---|
467 | |
---|
468 | if (isset($name)) |
---|
469 | { |
---|
470 | $name_ok = false; |
---|
471 | while (!$name_ok) |
---|
472 | { |
---|
473 | $name_ok = (get_userid($name) === false); |
---|
474 | if (!$name_ok) |
---|
475 | { |
---|
476 | $name .= generate_key(1); |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | $inserts = array( |
---|
481 | array( |
---|
482 | 'id' => $id, |
---|
483 | 'username' => $name, |
---|
484 | 'password' => $password |
---|
485 | ), |
---|
486 | ); |
---|
487 | mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts); |
---|
488 | |
---|
489 | create_user_infos($id); |
---|
490 | |
---|
491 | $page['infos'][] = sprintf(l10n('c13y_user_created'), $name, $password); |
---|
492 | |
---|
493 | $result = true; |
---|
494 | } |
---|
495 | break; |
---|
496 | case 'status': |
---|
497 | if ($id == $conf['guest_id']) |
---|
498 | { |
---|
499 | $status = 'guest'; |
---|
500 | } |
---|
501 | else if ($id == $conf['default_user_id']) |
---|
502 | { |
---|
503 | $status = 'guest'; |
---|
504 | } |
---|
505 | else if ($id == $conf['webmaster_id']) |
---|
506 | { |
---|
507 | $status = 'webmaster'; |
---|
508 | } |
---|
509 | |
---|
510 | if (isset($status)) |
---|
511 | { |
---|
512 | $updates = array( |
---|
513 | array( |
---|
514 | 'user_id' => $id, |
---|
515 | 'status' => $status |
---|
516 | ), |
---|
517 | ); |
---|
518 | mass_updates(USER_INFOS_TABLE, |
---|
519 | array('primary' => array('user_id'),'update' => array('status')), |
---|
520 | $updates); |
---|
521 | |
---|
522 | $page['infos'][] = sprintf(l10n('c13y_user_status_updated'), get_username($id)); |
---|
523 | |
---|
524 | $result = true; |
---|
525 | } |
---|
526 | break; |
---|
527 | } |
---|
528 | } |
---|
529 | |
---|
530 | return $result; |
---|
531 | } |
---|
532 | |
---|
533 | ?> |
---|