source: branches/2.0/profile.php @ 4004

Last change on this file since 4004 was 4004, checked in by patdenice, 15 years ago

merge r4001-4003 from trunk to branch 2.0
Issue 1079: re-refactoring of code ;-)

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