source: trunk/profile.php @ 3995

Last change on this file since 3995 was 3995, checked in by Eric, 15 years ago

[Piwigo] Bug 1079 fixed : Return to default settings in user profile page.

(I don't really enjoy the code but it's the simplest way i've found)

  • Property svn:eol-style set to LF
File size: 11.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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  $userdata = $user;
40
41  trigger_action('loc_begin_profile');
42
43// Reset to default (Guest) custom settings
44  if (isset($_POST['reset_to_default']))
45  {
46        global $conf;
47    // Get the Guest custom settings
48                $query = 'SELECT * FROM '.USER_INFOS_TABLE.
49            ' WHERE user_id = '.$conf['default_user_id'].';';
50
51    $result = pwg_query($query);
52   
53    $cache['default_user'] = mysql_fetch_assoc($result);
54   
55                $default_user = array();
56   
57                foreach ($cache['default_user'] as $name => $value)
58    {
59                // If the field is true or false, the variable is transformed into a
60        // boolean value.
61        if ($value == 'true' or $value == 'false')
62      {
63        $default_user[$name] = get_boolean($value);
64        }
65      else
66      {
67        $default_user[$name] = $value;
68      }
69        }
70                // Changing $userdata array values with default ones   
71                foreach ($userdata as $key => $valeur)
72                {
73                        if ($key == 'nb_image_line')
74                        {
75                                $userdata[$key] = $default_user['nb_image_line'];
76                                $_POST['nb_image_line'] = $userdata['nb_image_line'];
77                        }
78                        if ($key == 'nb_line_page')
79                        {
80                                $userdata[$key] = $default_user['nb_line_page'];
81                                $_POST['nb_line_page'] = $userdata['nb_line_page'];
82                        }
83                        if ($key == 'maxwidth')
84                        {
85                                $userdata[$key] = $default_user['maxwidth'];
86                                $_POST['maxwidth'] = $userdata['maxheight'];
87                        }
88                        if ($key == 'maxheight')
89                        {
90                                $userdata[$key] = $default_user['maxheight'];
91                                $_POST['maxheight'] = $userdata['maxheight'];
92                        }
93                        if ($key == 'expand')
94                        {
95                                $userdata[$key] = $default_user['expand'];
96                                $_POST['expand'] = $userdata['expand'];
97                        }
98                        if ($key == 'show_nb_comments')
99                        {
100                                $userdata[$key] = $default_user['show_nb_comments'];
101                                $_POST['show_nb_comments'] = $userdata['show_nb_comments'];
102                        }
103                        if ($key == 'recent_period')
104                        {
105                                $userdata[$key] = $default_user['recent_period'];
106                                $_POST['recent_period'] = $userdata['recent_period'];
107                        }
108                        if ($key == 'show_nb_hits')
109                        {
110                                $userdata[$key] = $default_user['show_nb_hits'];
111                                $_POST['show_nb_hits'] = $userdata['show_nb_hits'];
112                        }
113                }
114        }
115       
116  save_profile_from_post($userdata, $errors);
117
118  $title= l10n('customize_page_title');
119  $page['body_id'] = 'theProfilePage';
120  include(PHPWG_ROOT_PATH.'include/page_header.php');
121
122  load_profile_in_template(
123    get_root_url().'profile.php', // action
124    make_index_url(), // for redirect
125    $userdata );
126
127  // +-----------------------------------------------------------------------+
128  // |                             errors display                            |
129  // +-----------------------------------------------------------------------+
130  if (count($errors) != 0)
131  {
132    $template->assign('errors', $errors);
133  }
134  $template->set_filename('profile', 'profile.tpl');
135  trigger_action('loc_end_profile');
136  $template->parse('profile');
137  include(PHPWG_ROOT_PATH.'include/page_tail.php');
138}
139
140//------------------------------------------------------ update & customization
141function save_profile_from_post($userdata, &$errors)
142{
143  global $conf;
144  $errors = array();
145
146  if (!isset($_POST['validate']))
147  {
148    return false;
149  }
150
151  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
152  if ($special_user)
153  {
154    unset($_POST['mail_address'],
155          $_POST['password'],
156          $_POST['use_new_pwd'],
157          $_POST['passwordConf']
158          );
159  }
160
161  $int_pattern = '/^\d+$/';
162  if (empty($_POST['nb_image_line'])
163      or (!preg_match($int_pattern, $_POST['nb_image_line'])))
164  {
165    $errors[] = l10n('nb_image_line_error');
166  }
167
168  if (empty($_POST['nb_line_page'])
169      or (!preg_match($int_pattern, $_POST['nb_line_page'])))
170  {
171    $errors[] = l10n('nb_line_page_error');
172  }
173
174  if ($_POST['maxwidth'] != ''
175      and (!preg_match($int_pattern, $_POST['maxwidth'])
176           or $_POST['maxwidth'] < 50))
177  {
178    $errors[] = l10n('maxwidth_error');
179  }
180  if ($_POST['maxheight']
181       and (!preg_match($int_pattern, $_POST['maxheight'])
182             or $_POST['maxheight'] < 50))
183  {
184    $errors[] = l10n('maxheight_error');
185  }
186  // periods must be integer values, they represents number of days
187  if (!preg_match($int_pattern, $_POST['recent_period'])
188      or $_POST['recent_period'] <= 0)
189  {
190    $errors[] = l10n('periods_error') ;
191  }
192
193  if (isset($_POST['mail_address']))
194  {
195    // if $_POST and $userdata have are same email
196    // validate_mail_address allows, however, to check email
197    $mail_error = validate_mail_address($userdata['id'], $_POST['mail_address']);
198    if (!empty($mail_error))
199    {
200      $errors[] = $mail_error;
201    }
202  }
203
204  if (!empty($_POST['use_new_pwd']))
205  {
206    // password must be the same as its confirmation
207    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
208    {
209      $errors[] = l10n('New password confirmation does not correspond');
210    }
211
212    if ( !defined('IN_ADMIN') )
213    {// changing password requires old password
214      $query = '
215  SELECT '.$conf['user_fields']['password'].' AS password
216    FROM '.USERS_TABLE.'
217    WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
218  ;';
219      list($current_password) = mysql_fetch_row(pwg_query($query));
220 
221      if ($conf['pass_convert']($_POST['password']) != $current_password)
222      {
223        $errors[] = l10n('Current password is wrong');
224      }
225    }
226  }
227
228  if (count($errors) == 0)
229  {
230    // mass_updates function
231    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
232
233    if (isset($_POST['mail_address']))
234    {
235      // update common user informations
236      $fields = array($conf['user_fields']['email']);
237
238      $data = array();
239      $data{$conf['user_fields']['id']} = $userdata['id'];
240      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
241
242      // password is updated only if filled
243      if (!empty($_POST['use_new_pwd']))
244      {
245        array_push($fields, $conf['user_fields']['password']);
246        // password is encrpyted with function $conf['pass_convert']
247        $data{$conf['user_fields']['password']} =
248          $conf['pass_convert']($_POST['use_new_pwd']);
249      }
250      mass_updates(USERS_TABLE,
251                   array('primary' => array($conf['user_fields']['id']),
252                         'update' => $fields),
253                   array($data));
254    }
255
256    // update user "additional" informations (specific to Piwigo)
257    $fields = array(
258      'nb_image_line', 'nb_line_page', 'language', 'maxwidth', 'maxheight',
259      'expand', 'show_nb_comments', 'show_nb_hits', 'recent_period', 'template'
260      );
261
262    $data = array();
263    $data['user_id'] = $userdata['id'];
264
265    foreach ($fields as $field)
266    {
267      if (isset($_POST[$field]))
268      {
269        $data[$field] = $_POST[$field];
270      }
271    }
272    mass_updates(USER_INFOS_TABLE,
273                 array('primary' => array('user_id'), 'update' => $fields),
274                 array($data));
275
276    trigger_action( 'save_profile_from_post', $userdata['id'] );
277   
278    if (!empty($_POST['redirect']))
279    {
280      redirect($_POST['redirect']);
281    }
282  }
283  return true;
284}
285
286
287function load_profile_in_template($url_action, $url_redirect, $userdata)
288{
289  global $template, $conf;
290
291  $template->set_filename('profile_content', 'profile_content.tpl');
292
293  $template->assign('radio_options',
294    array(
295      'true' => l10n('Yes'),
296      'false' => l10n('No')));
297
298  $template->assign(
299    array(
300      'USERNAME'=>$userdata['username'],
301      'EMAIL'=>get_email_address_as_display_text(@$userdata['email']),
302      'NB_IMAGE_LINE'=>$userdata['nb_image_line'],
303      'NB_ROW_PAGE'=>$userdata['nb_line_page'],
304      'RECENT_PERIOD'=>$userdata['recent_period'],
305      'MAXWIDTH'=>@$userdata['maxwidth'],
306      'MAXHEIGHT'=>@$userdata['maxheight'],
307      'EXPAND' =>$userdata['expand'] ? 'true' : 'false',
308      'NB_COMMENTS'=>$userdata['show_nb_comments'] ? 'true' : 'false',
309      'NB_HITS'=>$userdata['show_nb_hits'] ? 'true' : 'false',
310      'REDIRECT' => $url_redirect,
311      'F_ACTION'=>$url_action,
312      ));
313
314  foreach (get_pwg_themes() as $pwg_template)
315  {
316    if (isset($_POST['submit'])
317      or $userdata['template'].'/'.$userdata['theme'] == $pwg_template)
318    {
319      $template->assign('template_selection', $pwg_template);
320    }
321    $template_options[$pwg_template] = $pwg_template;
322  }
323  $template->assign('template_options', $template_options);
324
325  foreach (get_languages() as $language_code => $language_name)
326  {
327    if (isset($_POST['submit']) or $userdata['language'] == $language_code)
328    {
329      $template->assign('language_selection', $language_code);
330    }
331    $language_options[$language_code] = $language_name;
332  }
333
334  $template->assign('language_options', $language_options);
335
336  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
337  $template->assign('SPECIAL_USER', $special_user);
338  $template->assign('IN_ADMIN', defined('IN_ADMIN'));
339
340  // allow plugins to add their own form data to content
341  trigger_action( 'load_profile_in_template', $userdata );
342 
343  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
344}
345?>
Note: See TracBrowser for help on using the repository browser.