source: trunk/profile.php @ 30163

Last change on this file since 30163 was 29221, checked in by flop25, 10 years ago

bug:3122
removed admin files profile.tpl and profile_content.tpl, integrated in configuration.tpl
take into account default_user_id with a message
prefix for template var, to avoid overwriting of already existing vars especially in admin panel

  • Property svn:eol-style set to LF
File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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
24// customize appearance of the site for a user
25// +-----------------------------------------------------------------------+
26// |                           initialization                              |
27// +-----------------------------------------------------------------------+
28
29if (!defined('PHPWG_ROOT_PATH'))
30{//direct script access
31  define('PHPWG_ROOT_PATH','./');
32  include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
33
34  // +-----------------------------------------------------------------------+
35  // | Check Access and exit when user status is not ok                      |
36  // +-----------------------------------------------------------------------+
37  check_status(ACCESS_CLASSIC);
38
39  if (!empty($_POST))
40  {
41    check_pwg_token();
42  }
43
44  $userdata = $user;
45
46  trigger_notify('loc_begin_profile');
47
48// Reset to default (Guest) custom settings
49  if (isset($_POST['reset_to_default']))
50  {
51    $fields = array(
52      'nb_image_page', 'expand',
53      'show_nb_comments', 'show_nb_hits', 'recent_period', 'show_nb_hits'
54      );
55
56    // Get the Guest custom settings
57    $query = '
58SELECT '.implode(',', $fields).'
59  FROM '.USER_INFOS_TABLE.'
60  WHERE user_id = '.$conf['default_user_id'].'
61;';
62    $result = pwg_query($query);
63    $default_user = pwg_db_fetch_assoc($result);
64    $userdata = array_merge($userdata, $default_user);
65  }
66
67  save_profile_from_post($userdata, $page['errors']);
68
69  $title= l10n('Your Gallery Customization');
70  $page['body_id'] = 'theProfilePage';
71  $template->set_filename('profile', 'profile.tpl');
72  $template->set_filename('profile_content', 'profile_content.tpl');
73
74  load_profile_in_template(
75    get_root_url().'profile.php', // action
76    make_index_url(), // for redirect
77    $userdata );
78  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
79
80
81 
82  // include menubar
83  $themeconf = $template->get_template_vars('themeconf');
84  if (!isset($themeconf['hide_menu_on']) OR !in_array('theProfilePage', $themeconf['hide_menu_on']))
85  {
86    include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
87  }
88 
89  include(PHPWG_ROOT_PATH.'include/page_header.php');
90  trigger_notify('loc_end_profile');
91  flush_page_messages();
92  $template->pparse('profile');
93  include(PHPWG_ROOT_PATH.'include/page_tail.php');
94}
95
96//------------------------------------------------------ update & customization
97function save_profile_from_post($userdata, &$errors)
98{
99  global $conf, $page;
100  $errors = array();
101
102  if (!isset($_POST['validate']))
103  {
104    return false;
105  }
106
107  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
108  if ($special_user)
109  {
110    unset(
111      $_POST['username'],
112      $_POST['mail_address'],
113      $_POST['password'],
114      $_POST['use_new_pwd'],
115      $_POST['passwordConf'],
116      $_POST['theme'],
117      $_POST['language']
118      );
119    $_POST['theme'] = get_default_theme();
120    $_POST['language'] = get_default_language();
121  }
122 
123  if (!defined('IN_ADMIN'))
124  {
125    unset($_POST['username']);
126  }
127
128  if ($conf['allow_user_customization'] or defined('IN_ADMIN'))
129  {
130    $int_pattern = '/^\d+$/';
131    if (empty($_POST['nb_image_page'])
132        or (!preg_match($int_pattern, $_POST['nb_image_page'])))
133    {
134      $errors[] = l10n('The number of photos per page must be a not null scalar');
135    }
136
137    // periods must be integer values, they represents number of days
138    if (!preg_match($int_pattern, $_POST['recent_period'])
139        or $_POST['recent_period'] < 0)
140    {
141      $errors[] = l10n('Recent period must be a positive integer value') ;
142    }
143
144    if (!in_array($_POST['language'], array_keys(get_languages())))
145    {
146      die('Hacking attempt, incorrect language value');
147    }
148
149    if (!in_array($_POST['theme'], array_keys(get_pwg_themes())))
150    {
151      die('Hacking attempt, incorrect theme value');
152    }
153  }
154
155  if (isset($_POST['mail_address']))
156  {
157    // if $_POST and $userdata have are same email
158    // validate_mail_address allows, however, to check email
159    $mail_error = validate_mail_address($userdata['id'], $_POST['mail_address']);
160    if (!empty($mail_error))
161    {
162      $errors[] = $mail_error;
163    }
164  }
165
166  if (!empty($_POST['use_new_pwd']))
167  {
168    // password must be the same as its confirmation
169    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
170    {
171      $errors[] = l10n('The passwords do not match');
172    }
173
174    if ( !defined('IN_ADMIN') )
175    {// changing password requires old password
176      $query = '
177  SELECT '.$conf['user_fields']['password'].' AS password
178    FROM '.USERS_TABLE.'
179    WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
180  ;';
181      list($current_password) = pwg_db_fetch_row(pwg_query($query));
182
183      if (!$conf['password_verify']($_POST['password'], $current_password))
184      {
185        $errors[] = l10n('Current password is wrong');
186      }
187    }
188  }
189
190  if (count($errors) == 0)
191  {
192    // mass_updates function
193    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
194
195    if (isset($_POST['mail_address']))
196    {
197      // update common user informations
198      $fields = array($conf['user_fields']['email']);
199
200      $data = array();
201      $data{$conf['user_fields']['id']} = $userdata['id'];
202      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
203
204      // password is updated only if filled
205      if (!empty($_POST['use_new_pwd']))
206      {
207        $fields[] = $conf['user_fields']['password'];
208        // password is hashed with function $conf['password_hash']
209        $data{$conf['user_fields']['password']} = $conf['password_hash']($_POST['use_new_pwd']);
210      }
211     
212      // username is updated only if allowed
213      if (!empty($_POST['username']))
214      {
215        if ($_POST['username'] != $userdata['username'] and get_userid($_POST['username']))
216        {
217          $page['errors'][] = l10n('this login is already used');
218          unset($_POST['redirect']);
219        }
220        else
221        {
222          $fields[] = $conf['user_fields']['username'];
223          $data{$conf['user_fields']['username']} = $_POST['username'];
224         
225          // send email to the user
226          if ($_POST['username'] != $userdata['username'])
227          {
228            include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
229            switch_lang_to($userdata['language']);
230           
231            $keyargs_content = array(
232              get_l10n_args('Hello', ''),
233              get_l10n_args('Your username has been successfully changed to : %s', $_POST['username']),
234              );
235             
236            pwg_mail(
237              $_POST['mail_address'],
238              array(
239                'subject' => '['.$conf['gallery_title'].'] '.l10n('Username modification'),
240                'content' => l10n_args($keyargs_content),
241                'content_format' => 'text/plain',
242                )
243              );
244             
245            switch_lang_back();
246          }
247        }
248      }
249     
250      mass_updates(USERS_TABLE,
251                   array(
252                    'primary' => array($conf['user_fields']['id']),
253                    'update' => $fields
254                    ),
255                   array($data));
256    }
257
258    if ($conf['allow_user_customization'] or defined('IN_ADMIN'))
259    {
260      // update user "additional" informations (specific to Piwigo)
261      $fields = array(
262        'nb_image_page', 'language',
263        'expand', 'show_nb_hits', 'recent_period', 'theme'
264        );
265       
266      if ($conf['activate_comments'])
267      {
268        $fields[] = 'show_nb_comments';
269      }
270
271      $data = array();
272      $data['user_id'] = $userdata['id'];
273
274      foreach ($fields as $field)
275      {
276        if (isset($_POST[$field]))
277        {
278          $data[$field] = $_POST[$field];
279        }
280      }
281      mass_updates(USER_INFOS_TABLE,
282                   array('primary' => array('user_id'), 'update' => $fields),
283                   array($data));
284    }
285    trigger_notify( 'save_profile_from_post', $userdata['id'] );
286
287    if (!empty($_POST['redirect']))
288    {
289      redirect($_POST['redirect']);
290    }
291  }
292  return true;
293}
294
295/**
296 * Assign template variables, from arguments
297 * Used to build profile edition pages
298 *
299 * @param string $url_action
300 * @param string $url_redirect
301 * @param array $userdata
302 */
303function load_profile_in_template($url_action, $url_redirect, $userdata, $template_prefixe=null)
304{
305  global $template, $conf;
306
307  $template->assign('radio_options',
308    array(
309      'true' => l10n('Yes'),
310      'false' => l10n('No')));
311
312  $template->assign(
313    array(
314      $template_prefixe.'USERNAME'=>stripslashes($userdata['username']),
315      $template_prefixe.'EMAIL'=>@$userdata['email'],
316      $template_prefixe.'ALLOW_USER_CUSTOMIZATION'=>$conf['allow_user_customization'],
317      $template_prefixe.'ACTIVATE_COMMENTS'=>$conf['activate_comments'],
318      $template_prefixe.'NB_IMAGE_PAGE'=>$userdata['nb_image_page'],
319      $template_prefixe.'RECENT_PERIOD'=>$userdata['recent_period'],
320      $template_prefixe.'EXPAND' =>$userdata['expand'] ? 'true' : 'false',
321      $template_prefixe.'NB_COMMENTS'=>$userdata['show_nb_comments'] ? 'true' : 'false',
322      $template_prefixe.'NB_HITS'=>$userdata['show_nb_hits'] ? 'true' : 'false',
323      $template_prefixe.'REDIRECT' => $url_redirect,
324      $template_prefixe.'F_ACTION'=>$url_action,
325      ));
326
327  $template->assign('template_selection', $userdata['theme']);
328  $template->assign('template_options', get_pwg_themes());
329
330  foreach (get_languages() as $language_code => $language_name)
331  {
332    if (isset($_POST['submit']) or $userdata['language'] == $language_code)
333    {
334      $template->assign('language_selection', $language_code);
335    }
336    $language_options[$language_code] = $language_name;
337  }
338
339  $template->assign('language_options', $language_options);
340
341  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
342  $template->assign('SPECIAL_USER', $special_user);
343  $template->assign('IN_ADMIN', defined('IN_ADMIN'));
344
345  // allow plugins to add their own form data to content
346  trigger_notify( 'load_profile_in_template', $userdata );
347
348  $template->assign('PWG_TOKEN', get_pwg_token());
349}
350?>
Note: See TracBrowser for help on using the repository browser.