source: trunk/profile.php @ 13370

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

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

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