source: trunk/admin/configuration.php @ 11433

Last change on this file since 11433 was 11433, checked in by mistic100, 13 years ago

feature:2317 move order config to Admin->Config->Options
change regex test, now compatible with strings like "file COLLATE 'utf8_general_ci' ASC"

  • Property svn:eol-style set to LF
File size: 13.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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
24if( !defined("PHPWG_ROOT_PATH") )
25{
26  die ("Hacking attempt!");
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
31
32// +-----------------------------------------------------------------------+
33// | Check Access and exit when user status is not ok                      |
34// +-----------------------------------------------------------------------+
35check_status(ACCESS_ADMINISTRATOR);
36
37//-------------------------------------------------------- sections definitions
38if (!isset($_GET['section']))
39{
40  $page['section'] = 'main';
41}
42else
43{
44  $page['section'] = $_GET['section'];
45}
46
47$main_checkboxes = array(
48    'gallery_locked',
49    'allow_user_registration',
50    'obligatory_user_mail_address',
51    'rate',
52    'rate_anonymous',
53    'email_admin_on_new_user',
54    'allow_user_customization',
55   );
56
57$history_checkboxes = array(
58    'log',
59    'history_admin',
60    'history_guest'
61   );
62
63$comments_checkboxes = array(
64    'comments_forall',
65    'comments_validation',
66    'email_admin_on_comment',
67    'email_admin_on_comment_validation',
68    'user_can_delete_comment',
69    'user_can_edit_comment',
70    'email_admin_on_comment_edition',
71    'email_admin_on_comment_deletion'
72  );
73
74$display_checkboxes = array(
75    'menubar_filter_icon',
76    'index_sort_order_input',
77    'index_flat_icon',
78    'index_posted_date_icon',
79    'index_created_date_icon',
80    'index_slideshow_icon',
81    'index_new_icon',
82    'picture_metadata_icon',
83    'picture_slideshow_icon',
84    'picture_favorite_icon',
85    'picture_download_icon',
86    'picture_navigation_icons',
87    'picture_navigation_thumb',
88    'picture_menu',
89  );
90
91$display_info_checkboxes = array(
92    'author',
93    'created_on',
94    'posted_on',
95    'dimensions',
96    'file',
97    'filesize',
98    'tags',
99    'categories',
100    'visits',
101    'average_rate',
102    'privacy_level',
103  );
104 
105$order_options = array(
106    ' ORDER BY date_available DESC, file ASC, id ASC' => 'Post date DESC, File name ASC',
107    ' ORDER BY date_available ASC, file ASC, id ASC' => 'Post date ASC, File name ASC',
108    ' ORDER BY file DESC, date_available DESC, id ASC' => 'File name DESC, Post date DESC',
109    ' ORDER BY file ASC, date_available DESC, id ASC' => 'File name ASC, Post date DESC',
110    'custom' => l10n('Custom'),
111  );
112
113//------------------------------ verification and registration of modifications
114if (isset($_POST['submit']))
115{
116  $int_pattern = '/^\d+$/';
117
118  switch ($page['section'])
119  {
120    case 'main' :
121    {
122      $order_regex = '#^(([ \w\']{2,}) (ASC|DESC),{1}){1,}$#';
123      // process 'order_by_perso' string
124      if ($_POST['order_by'] == 'custom' AND !empty($_POST['order_by_perso']))
125      {
126        $_POST['order_by_perso'] = stripslashes(trim($_POST['order_by_perso']));
127        $_POST['order_by'] = str_ireplace(
128          array('order by ', 'asc', 'desc', '"'),
129          array(null, 'ASC', 'DESC', '\''),
130          $_POST['order_by_perso']
131          );
132       
133        if (preg_match($order_regex, $_POST['order_by'].','))
134        {
135          $_POST['order_by'] = ' ORDER BY '.addslashes($_POST['order_by']);
136        }
137        else
138        {
139          array_push($page['errors'], l10n('Invalid order string').' &laquo; '.$_POST['order_by'].' &raquo;');
140        }
141      }
142      else if ($_POST['order_by'] == 'custom')
143      {
144        array_push($page['errors'], l10n('Invalid order string'));
145      }
146      // process 'order_by_inside_category_perso' string
147      if ($_POST['order_by_inside_category'] == 'as_order_by')
148      {
149        $_POST['order_by_inside_category'] = $_POST['order_by'];
150      }
151      else if ($_POST['order_by_inside_category'] == 'custom' AND !empty($_POST['order_by_inside_category_perso']))
152      {
153        $_POST['order_by_inside_category_perso'] = stripslashes(trim($_POST['order_by_inside_category_perso']));
154        $_POST['order_by_inside_category'] = str_ireplace(
155          array('order by ', 'asc', 'desc', '"'),
156          array(null, 'ASC', 'DESC', '\''),
157          $_POST['order_by_inside_category_perso']
158          );
159       
160        if (preg_match($order_regex, $_POST['order_by_inside_category'].','))
161        {
162          $_POST['order_by_inside_category'] = ' ORDER BY '.addslashes($_POST['order_by_inside_category']);
163        }
164        else
165        {
166          array_push($page['errors'], l10n('Invalid order string').' &laquo; '.$_POST['order_by_inside_category'].' &raquo;');
167        }
168      }
169      else if ($_POST['order_by_inside_category'] == 'custom')
170      {
171        array_push($page['errors'], l10n('Invalid order string'));
172      }
173     
174      if (empty($_POST['gallery_locked']) and $conf['gallery_locked'])
175      {
176        $tpl_var = & $template->get_template_vars('header_msgs');
177        $msg_key = array_search(l10n('The gallery is locked for maintenance. Please, come back later.'), $tpl_var);
178        unset($tpl_var[$msg_key]);
179      }
180      elseif (!empty($_POST['gallery_locked']) and !$conf['gallery_locked'])
181      {
182        $template->append('header_msgs', l10n('The gallery is locked for maintenance. Please, come back later.'));
183      }
184      foreach( $main_checkboxes as $checkbox)
185      {
186        $_POST[$checkbox] = empty($_POST[$checkbox])?'false':'true';
187      }
188      break;
189    }
190    case 'history' :
191    {
192      foreach( $history_checkboxes as $checkbox)
193      {
194        $_POST[$checkbox] = empty($_POST[$checkbox])?'false':'true';
195      }
196      break;
197    }
198    case 'comments' :
199    {
200      // the number of comments per page must be an integer between 5 and 50
201      // included
202      if (!preg_match($int_pattern, $_POST['nb_comment_page'])
203           or $_POST['nb_comment_page'] < 5
204           or $_POST['nb_comment_page'] > 50)
205      {
206        array_push($page['errors'], l10n('The number of comments a page must be between 5 and 50 included.'));
207      }
208      foreach( $comments_checkboxes as $checkbox)
209      {
210        $_POST[$checkbox] = empty($_POST[$checkbox])?'false':'true';
211      }
212      break;
213    }
214    case 'default' :
215    {
216      // Never go here
217      break;
218    }
219    case 'display' :
220    {
221      foreach( $display_checkboxes as $checkbox)
222      {
223        $_POST[$checkbox] = empty($_POST[$checkbox])?'false':'true';
224      }
225      foreach( $display_info_checkboxes as $checkbox)
226      {
227        $_POST['picture_informations'][$checkbox] =
228          empty($_POST['picture_informations'][$checkbox])? false : true;
229      }
230      $_POST['picture_informations'] = addslashes(serialize($_POST['picture_informations']));
231      break;
232    }
233  }
234
235  // updating configuration if no error found
236  if (count($page['errors']) == 0)
237  {
238    //echo '<pre>'; print_r($_POST); echo '</pre>';
239    $result = pwg_query('SELECT param FROM '.CONFIG_TABLE);
240    while ($row = pwg_db_fetch_assoc($result))
241    {
242      if (isset($_POST[$row['param']]))
243      {
244        $value = $_POST[$row['param']];
245
246        if ('gallery_title' == $row['param'])
247        {
248          if (!$conf['allow_html_descriptions'])
249          {
250            $value = strip_tags($value);
251          }
252        }
253
254        $query = '
255UPDATE '.CONFIG_TABLE.'
256SET value = \''. str_replace("\'", "''", $value).'\'
257WHERE param = \''.$row['param'].'\'
258;';
259        pwg_query($query);
260      }
261    }
262    array_push($page['infos'], l10n('Information data registered in database'));
263  }
264
265  //------------------------------------------------------ $conf reinitialization
266  load_conf_from_db();
267}
268
269//----------------------------------------------------- template initialization
270$template->set_filename('config', 'configuration.tpl');
271
272// TabSheet
273$tabsheet = new tabsheet();
274// TabSheet initialization
275$tabsheet->add('main', l10n('Main'), $conf_link.'main');
276$tabsheet->add('display', l10n('Display'), $conf_link.'display');
277$tabsheet->add('history', l10n('History'), $conf_link.'history');
278$tabsheet->add('comments', l10n('Comments'), $conf_link.'comments');
279$tabsheet->add('default', l10n('Guest Settings'), $conf_link.'default');
280// TabSheet selection
281$tabsheet->select($page['section']);
282// Assign tabsheet to template
283$tabsheet->assign();
284
285$action = get_root_url().'admin.php?page=configuration';
286$action.= '&amp;section='.$page['section'];
287
288$template->assign(
289  array(
290    'U_HELP' => get_root_url().'admin/popuphelp.php?page=configuration',
291    'F_ACTION'=>$action
292    ));
293
294switch ($page['section'])
295{
296  case 'main' :
297  {
298    // process 'order_by' string
299    if (array_key_exists($conf['order_by'], $order_options))
300    {
301      $order_by_selected = $conf['order_by'];
302      $order_by_perso = null;
303    }
304    else
305    {
306      $order_by_selected = 'custom';
307      $order_by_perso = str_replace(' ORDER BY ', null, $conf['order_by']);
308    }
309    // process 'order_by_inside_category' string
310    if ($conf['order_by_inside_category'] == $conf['order_by'])
311    {
312      $order_by_inside_category_selected = 'as_order_by';
313      $order_by_inside_category_perso = null;
314    }
315    else if (array_key_exists($conf['order_by_inside_category'], $order_options))
316    {
317      $order_by_inside_category_selected = $conf['order_by_inside_category'];
318      $order_by_inside_category_perso = null;
319    }
320    else
321    {
322      $order_by_inside_category_selected = 'custom';
323      $order_by_inside_category_perso = str_replace(' ORDER BY ', null, $conf['order_by_inside_category']);
324    }
325     
326    $template->assign(
327      'main',
328      array(
329        'CONF_GALLERY_TITLE' => htmlspecialchars($conf['gallery_title']),
330        'CONF_PAGE_BANNER' => htmlspecialchars($conf['page_banner']),
331        'CONF_GALLERY_URL' => $conf['gallery_url'],
332        'week_starts_on_options' => array(
333          'sunday' => $lang['day'][0],
334          'monday' => $lang['day'][1],
335          ),
336        'week_starts_on_options_selected' => $conf['week_starts_on'],
337        'order_by_options' => $order_options,
338        'order_by_selected' => $order_by_selected,
339        'order_by_perso' => $order_by_perso,
340        'order_by_inside_category_options' => 
341          array_merge(
342            array('as_order_by'=>l10n('As default order')), 
343            $order_options
344            ),
345        'order_by_inside_category_selected' => $order_by_inside_category_selected,
346        'order_by_inside_category_perso' => $order_by_inside_category_perso,
347        ));
348
349    foreach ($main_checkboxes as $checkbox)
350    {
351      $template->append(
352          'main',
353          array(
354            $checkbox => $conf[$checkbox]
355            ),
356          true
357        );
358    }
359    break;
360  }
361  case 'history' :
362  {
363    //Necessary for merge_block_vars
364    foreach ($history_checkboxes as $checkbox)
365    {
366      $template->append(
367          'history',
368          array(
369            $checkbox => $conf[$checkbox]
370            ),
371          true
372        );
373    }
374    break;
375  }
376  case 'comments' :
377  {
378    $template->assign(
379      'comments',
380      array(
381        'NB_COMMENTS_PAGE'=>$conf['nb_comment_page'],
382        ));
383
384    foreach ($comments_checkboxes as $checkbox)
385    {
386      $template->append(
387          'comments',
388          array(
389            $checkbox => $conf[$checkbox]
390            ),
391          true
392        );
393    }
394    break;
395  }
396  case 'default' :
397  {
398    $edit_user = build_user($conf['guest_id'], false);
399    include_once(PHPWG_ROOT_PATH.'profile.php');
400
401    $errors = array();
402    if (save_profile_from_post($edit_user, $errors))
403    {
404      // Reload user
405      $edit_user = build_user($conf['guest_id'], false);
406      array_push($page['infos'], l10n('Information data registered in database'));
407    }
408    $page['errors'] = array_merge($page['errors'], $errors);
409
410    load_profile_in_template(
411      $action,
412      '',
413      $edit_user
414      );
415    $template->assign('default', array());
416    break;
417  }
418  case 'display' :
419  {
420    foreach ($display_checkboxes as $checkbox)
421    {
422      $template->append(
423          'display',
424          array(
425            $checkbox => $conf[$checkbox]
426            ),
427          true
428        );
429    }
430    $template->append(
431        'display',
432        array(
433          'picture_informations' => unserialize($conf['picture_informations'])
434          ),
435        true
436      );
437    break;
438  }
439}
440
441//----------------------------------------------------------- sending html code
442$template->assign_var_from_handle('ADMIN_CONTENT', 'config');
443?>
Note: See TracBrowser for help on using the repository browser.