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
RevLine 
[2]1<?php
[351]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3046]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[2]23
24// customize appearance of the site for a user
[631]25// +-----------------------------------------------------------------------+
26// |                           initialization                              |
27// +-----------------------------------------------------------------------+
[808]28
[1753]29if (!defined('PHPWG_ROOT_PATH'))
30{//direct script access
31  define('PHPWG_ROOT_PATH','./');
32  include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
[1036]33
[1753]34  // +-----------------------------------------------------------------------+
35  // | Check Access and exit when user status is not ok                      |
36  // +-----------------------------------------------------------------------+
37  check_status(ACCESS_CLASSIC);
[1072]38
[1753]39  $userdata = $user;
[808]40
[2237]41  trigger_action('loc_begin_profile');
42
[3996]43// Reset to default (Guest) custom settings
44  if (isset($_POST['reset_to_default']))
45  {
[4004]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
[3996]54    // Get the Guest custom settings
[4004]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);
[3996]63
[4004]64    mass_updates(
65      USER_INFOS_TABLE,
66      array('primary' => array('user_id'), 'update' => $fields),
67      array($userdata)
68      );
69  }
70
[1753]71  save_profile_from_post($userdata, $errors);
72
[2014]73  $title= l10n('customize_page_title');
[1753]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  {
[2246]87    $template->assign('errors', $errors);
[1753]88  }
89  $template->set_filename('profile', 'profile.tpl');
[2237]90  trigger_action('loc_end_profile');
[1753]91  $template->parse('profile');
92  include(PHPWG_ROOT_PATH.'include/page_tail.php');
93}
94
[2]95//------------------------------------------------------ update & customization
[2268]96function save_profile_from_post($userdata, &$errors)
[2]97{
[1753]98  global $conf;
99  $errors = array();
[2268]100
[1753]101  if (!isset($_POST['validate']))
102  {
[1926]103    return false;
[1753]104  }
105
[2268]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
[26]116  $int_pattern = '/^\d+$/';
[1043]117  if (empty($_POST['nb_image_line'])
118      or (!preg_match($int_pattern, $_POST['nb_image_line'])))
119  {
[1753]120    $errors[] = l10n('nb_image_line_error');
[1043]121  }
122
123  if (empty($_POST['nb_line_page'])
124      or (!preg_match($int_pattern, $_POST['nb_line_page'])))
125  {
[1753]126    $errors[] = l10n('nb_line_page_error');
[1043]127  }
[1620]128
[662]129  if ($_POST['maxwidth'] != ''
130      and (!preg_match($int_pattern, $_POST['maxwidth'])
131           or $_POST['maxwidth'] < 50))
[2]132  {
[1753]133    $errors[] = l10n('maxwidth_error');
[2]134  }
[662]135  if ($_POST['maxheight']
136       and (!preg_match($int_pattern, $_POST['maxheight'])
137             or $_POST['maxheight'] < 50))
[2]138  {
[1753]139    $errors[] = l10n('maxheight_error');
[2]140  }
[26]141  // periods must be integer values, they represents number of days
[452]142  if (!preg_match($int_pattern, $_POST['recent_period'])
143      or $_POST['recent_period'] <= 0)
[2]144  {
[1753]145    $errors[] = l10n('periods_error') ;
[2]146  }
[662]147
[1926]148  if (isset($_POST['mail_address']))
[662]149  {
[2124]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']);
[1926]153    if (!empty($mail_error))
154    {
155      $errors[] = $mail_error;
156    }
[662]157  }
[1620]158
[808]159  if (!empty($_POST['use_new_pwd']))
[630]160  {
[808]161    // password must be the same as its confirmation
162    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
[631]163    {
[1753]164      $errors[] = l10n('New password confirmation does not correspond');
[662]165    }
[1620]166
[1753]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      }
[631]180    }
181  }
[1620]182
[662]183  if (count($errors) == 0)
[2]184  {
[808]185    // mass_updates function
186    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1620]187
[1926]188    if (isset($_POST['mail_address']))
189    {
190      // update common user informations
191      $fields = array($conf['user_fields']['email']);
[2]192
[1926]193      $data = array();
[2268]194      $data{$conf['user_fields']['id']} = $userdata['id'];
[1926]195      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
[808]196
[1926]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));
[2]209    }
[1620]210
[2342]211    // update user "additional" informations (specific to Piwigo)
[808]212    $fields = array(
213      'nb_image_line', 'nb_line_page', 'language', 'maxwidth', 'maxheight',
[1763]214      'expand', 'show_nb_comments', 'show_nb_hits', 'recent_period', 'template'
[808]215      );
[1620]216
[808]217    $data = array();
[2268]218    $data['user_id'] = $userdata['id'];
[1620]219
[808]220    foreach ($fields as $field)
[631]221    {
[808]222      if (isset($_POST[$field]))
[772]223      {
[1043]224        $data[$field] = $_POST[$field];
[772]225      }
[631]226    }
[808]227    mass_updates(USER_INFOS_TABLE,
228                 array('primary' => array('user_id'), 'update' => $fields),
229                 array($data));
[1620]230
[2268]231    trigger_action( 'save_profile_from_post', $userdata['id'] );
[2048]232   
[1926]233    if (!empty($_POST['redirect']))
234    {
235      redirect($_POST['redirect']);
236    }
[2]237  }
[1926]238  return true;
[2]239}
[850]240
[808]241
[1753]242function load_profile_in_template($url_action, $url_redirect, $userdata)
243{
[2029]244  global $template, $conf;
[850]245
[1753]246  $template->set_filename('profile_content', 'profile_content.tpl');
[393]247
[2246]248  $template->assign('radio_options',
249    array(
[2268]250      'true' => l10n('Yes'),
251      'false' => l10n('No')));
[1620]252
[2246]253  $template->assign(
[1753]254    array(
255      'USERNAME'=>$userdata['username'],
[2229]256      'EMAIL'=>get_email_address_as_display_text(@$userdata['email']),
[1753]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'],
[2246]262      'EXPAND' =>$userdata['expand'] ? 'true' : 'false',
263      'NB_COMMENTS'=>$userdata['show_nb_comments'] ? 'true' : 'false',
264      'NB_HITS'=>$userdata['show_nb_hits'] ? 'true' : 'false',
[1753]265      'REDIRECT' => $url_redirect,
266      'F_ACTION'=>$url_action,
267      ));
[1620]268
[1753]269  foreach (get_pwg_themes() as $pwg_template)
[854]270  {
[2246]271    if (isset($_POST['submit'])
272      or $userdata['template'].'/'.$userdata['theme'] == $pwg_template)
[1753]273    {
[2246]274      $template->assign('template_selection', $pwg_template);
[1753]275    }
[2246]276    $template_options[$pwg_template] = $pwg_template;
[854]277  }
[2246]278  $template->assign('template_options', $template_options);
[1620]279
[1753]280  foreach (get_languages() as $language_code => $language_name)
[854]281  {
[2246]282    if (isset($_POST['submit']) or $userdata['language'] == $language_code)
[1753]283    {
[2246]284      $template->assign('language_selection', $language_code);
[1753]285    }
[2246]286    $language_options[$language_code] = $language_name;
[854]287  }
[3996]288
[2246]289  $template->assign('language_options', $language_options);
[1926]290
[2268]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'));
[1926]294
[2268]295  // allow plugins to add their own form data to content
296  trigger_action( 'load_profile_in_template', $userdata );
297 
[1753]298  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
[854]299}
[362]300?>
Note: See TracBrowser for help on using the repository browser.