source: trunk/admin/include/check_integrity.class.php @ 19703

Last change on this file since 19703 was 19703, checked in by plg, 11 years ago

update Piwigo headers to 2013 (the end of the world didn't occur as expected on r12922)

  • Property svn:eol-style set to LF
File size: 9.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24class check_integrity
25{
26  var $ignore_list;
27  var $retrieve_list;
28  var $build_ignore_list;
29
30  function check_integrity()
31  {
32    $this->ignore_list = array();
33    $this->retrieve_list = array();
34    $this->build_ignore_list = array();
35  }
36
37  /**
38   * Check integrities
39   *
40   * @param void
41   * @return void
42   */
43  function check()
44  {
45    global $page, $header_notes, $conf;
46
47    // Ignore list
48    $conf_c13y_ignore = unserialize($conf['c13y_ignore']);
49    if (
50          is_array($conf_c13y_ignore) and
51          isset($conf_c13y_ignore['version']) and
52          ($conf_c13y_ignore['version'] == PHPWG_VERSION) and
53          is_array($conf_c13y_ignore['list'])
54        )
55    {
56      $ignore_list_changed = false;
57      $this->ignore_list = $conf_c13y_ignore['list'];
58    }
59    else
60    {
61      $ignore_list_changed = true;
62      $this->ignore_list = array();
63    }
64
65    // Retrieve list
66    $this->retrieve_list = array();
67    $this->build_ignore_list = array();
68
69    trigger_action('list_check_integrity', $this);
70
71    // Information
72    if (count($this->retrieve_list) > 0)
73    {
74      $header_notes[] =
75        l10n_dec('%d anomaly has been detected.', '%d anomalies have been detected.',
76          count($this->retrieve_list));
77    }
78
79    // Treatments
80    if (isset($_POST['c13y_submit_correction']) and isset($_POST['c13y_selection']))
81    {
82      $corrected_count = 0;
83      $not_corrected_count = 0;
84
85      foreach ($this->retrieve_list as $i => $c13y)
86      {
87        if (!empty($c13y['correction_fct']) and
88            $c13y['is_callable'] and
89            in_array($c13y['id'], $_POST['c13y_selection']))
90        {
91          if (is_array($c13y['correction_fct_args']))
92          {
93            $args = $c13y['correction_fct_args'];
94          }
95          else
96          if (!is_null($c13y['correction_fct_args']))
97          {
98            $args = array($c13y['correction_fct_args']);
99          }
100          else
101          {
102            $args = array();
103          }
104          $this->retrieve_list[$i]['corrected'] = call_user_func_array($c13y['correction_fct'], $args);
105
106          if ($this->retrieve_list[$i]['corrected'])
107          {
108            $corrected_count += 1;
109          }
110          else
111          {
112            $not_corrected_count += 1;
113          }
114        }
115      }
116
117      if ($corrected_count > 0)
118      {
119        $page['infos'][] =
120          l10n_dec('%d anomaly has been corrected.', '%d anomalies have been detected corrected.',
121            $corrected_count);
122      }
123      if ($not_corrected_count > 0)
124      {
125        $page['errors'][] =
126          l10n_dec('%d anomaly has not been corrected.', '%d anomalies have not been corrected.',
127            $not_corrected_count);
128      }
129    }
130    else
131    {
132      if (isset($_POST['c13y_submit_ignore']) and isset($_POST['c13y_selection']))
133      {
134        $ignored_count = 0;
135
136        foreach ($this->retrieve_list as $i => $c13y)
137        {
138          if (in_array($c13y['id'], $_POST['c13y_selection']))
139          {
140            $this->build_ignore_list[] = $c13y['id'];
141            $this->retrieve_list[$i]['ignored'] = true;
142            $ignored_count += 1;
143          }
144        }
145
146        if ($ignored_count > 0)
147        {
148          $page['infos'][] =
149            l10n_dec('%d anomaly has been ignored.', '%d anomalies have been ignored.',
150              $ignored_count);
151        }
152      }
153    }
154
155    $ignore_list_changed =
156      (
157        ($ignore_list_changed) or
158        (count(array_diff($this->ignore_list, $this->build_ignore_list)) > 0) or
159        (count(array_diff($this->build_ignore_list, $this->ignore_list)) > 0)
160        );
161
162    if ($ignore_list_changed)
163    {
164      $this->update_conf($this->build_ignore_list);
165    }
166  }
167
168  /**
169   * Display anomalies list
170   *
171   * @param void
172   * @return void
173   */
174  function display()
175  {
176    global $template;
177
178    $check_automatic_correction = false;
179    $submit_automatic_correction = false;
180    $submit_ignore = false;
181
182    if (isset($this->retrieve_list) and count($this->retrieve_list) > 0)
183    {
184      $template->set_filenames(array('check_integrity' => 'check_integrity.tpl'));
185
186      foreach ($this->retrieve_list as $i => $c13y)
187      {
188        $can_select = false;
189        $c13y_display = array(
190           'id' => $c13y['id'],
191           'anomaly' => $c13y['anomaly'],
192           'show_ignore_msg' => false,
193           'show_correction_success_fct' => false,
194           'correction_error_fct' => '',
195           'show_correction_fct' => false,
196           'correction_error_fct' => '',
197           'show_correction_bad_fct' => false,
198           'correction_msg' => ''
199          );
200
201        if (isset($c13y['ignored']))
202        {
203          if ($c13y['ignored'])
204          {
205            $c13y_display['show_ignore_msg'] = true;
206          }
207          else
208          {
209            die('$c13y[\'ignored\'] cannot be false');
210          }
211        }
212        else
213        {
214          if (!empty($c13y['correction_fct']))
215          {
216            if (isset($c13y['corrected']))
217            {
218              if ($c13y['corrected'])
219              {
220                $c13y_display['show_correction_success_fct'] = true;
221              }
222              else
223              {
224                $c13y_display['correction_error_fct'] = $this->get_htlm_links_more_info();
225              }
226            }
227            else if ($c13y['is_callable'])
228            {
229              $c13y_display['show_correction_fct'] = true;
230              $template->append('c13y_do_check', $c13y['id']);
231              $submit_automatic_correction = true;
232              $can_select = true;
233            }
234            else
235            {
236              $c13y_display['show_correction_bad_fct'] = true;
237              $can_select = true;
238            }
239          }
240          else
241          {
242            $can_select = true;
243          }
244
245          if (!empty($c13y['correction_msg']) and !isset($c13y['corrected']))
246          {
247            $c13y_display['correction_msg'] = $c13y['correction_msg'];
248          }
249        }
250
251        $c13y_display['can_select'] = $can_select;
252        if ($can_select)
253        {
254          $submit_ignore = true;
255        }
256
257        $template->append('c13y_list', $c13y_display);
258      }
259
260      $template->assign('c13y_show_submit_automatic_correction', $submit_automatic_correction);
261      $template->assign('c13y_show_submit_ignore', $submit_ignore);
262
263      $template->concat('ADMIN_CONTENT', $template->parse('check_integrity', true));
264
265    }
266  }
267
268  /**
269   * Add anomaly data
270   *
271   * @param anomaly arguments
272   * @return void
273   */
274  function add_anomaly($anomaly, $correction_fct = null, $correction_fct_args = null, $correction_msg = null)
275  {
276    $id = md5($anomaly.$correction_fct.serialize($correction_fct_args).$correction_msg);
277
278    if (in_array($id, $this->ignore_list))
279    {
280      $this->build_ignore_list[] = $id;
281    }
282    else
283    {
284      $this->retrieve_list[] =
285        array(
286          'id' => $id,
287          'anomaly' => $anomaly,
288          'correction_fct' => $correction_fct,
289          'correction_fct_args' => $correction_fct_args,
290          'correction_msg' => $correction_msg,
291          'is_callable' => is_callable($correction_fct));
292    }
293  }
294
295  /**
296   * Update table config
297   *
298   * @param ignore list array
299   * @return void
300   */
301  function update_conf($conf_ignore_list = array())
302  {
303    $conf_c13y_ignore =  array();
304    $conf_c13y_ignore['version'] = PHPWG_VERSION;
305    $conf_c13y_ignore['list'] = $conf_ignore_list;
306    $query = 'update '.CONFIG_TABLE.' set value =\''.serialize($conf_c13y_ignore).'\'where param = \'c13y_ignore\';';
307    pwg_query($query);
308  }
309
310  /**
311   * Apply maintenance
312   *
313   * @param void
314   * @return void
315   */
316  function maintenance()
317  {
318    $this->update_conf();
319  }
320
321  /**
322   * Returns links more informations
323   *
324   * @param void
325   * @return html links
326   */
327  function get_htlm_links_more_info()
328  {
329    $pwg_links = pwg_URL();
330    $link_fmt = '<a href="%s" onclick="window.open(this.href, \'\'); return false;">%s</a>';
331    return
332      sprintf
333      (
334        l10n('Go to %s or %s for more informations'),
335        sprintf($link_fmt, $pwg_links['FORUM'], l10n('the forum')),
336        sprintf($link_fmt, $pwg_links['WIKI'], l10n('the wiki'))
337      );
338  }
339
340}
341
342?>
Note: See TracBrowser for help on using the repository browser.