source: trunk/profile.php @ 2268

Last change on this file since 2268 was 2268, checked in by rvelices, 16 years ago
  • security fix (profile)
  • les langues a la hache
  • fix some copy/paste errors
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: profile.php 2268 2008-03-08 12:38:09Z rvelices $
8// | last update   : $Date: 2008-03-08 12:38:09 +0000 (Sat, 08 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2268 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27// customize appearance of the site for a user
28// +-----------------------------------------------------------------------+
29// |                           initialization                              |
30// +-----------------------------------------------------------------------+
31
32if (!defined('PHPWG_ROOT_PATH'))
33{//direct script access
34  define('PHPWG_ROOT_PATH','./');
35  include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
36
37  // +-----------------------------------------------------------------------+
38  // | Check Access and exit when user status is not ok                      |
39  // +-----------------------------------------------------------------------+
40  check_status(ACCESS_CLASSIC);
41
42  $userdata = $user;
43
44  trigger_action('loc_begin_profile');
45
46  save_profile_from_post($userdata, $errors);
47
48  $title= l10n('customize_page_title');
49  $page['body_id'] = 'theProfilePage';
50  include(PHPWG_ROOT_PATH.'include/page_header.php');
51
52  load_profile_in_template(
53    get_root_url().'profile.php', // action
54    make_index_url(), // for redirect
55    $userdata );
56
57  // +-----------------------------------------------------------------------+
58  // |                             errors display                            |
59  // +-----------------------------------------------------------------------+
60  if (count($errors) != 0)
61  {
62    $template->assign('errors', $errors);
63  }
64  $template->set_filename('profile', 'profile.tpl');
65  trigger_action('loc_end_profile');
66  $template->parse('profile');
67  include(PHPWG_ROOT_PATH.'include/page_tail.php');
68}
69
70//------------------------------------------------------ update & customization
71function save_profile_from_post($userdata, &$errors)
72{
73  global $conf;
74  $errors = array();
75
76  if (!isset($_POST['validate']))
77  {
78    return false;
79  }
80
81  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
82  if ($special_user)
83  {
84    unset($_POST['mail_address'],
85          $_POST['password'],
86          $_POST['use_new_pwd'],
87          $_POST['passwordConf']
88          );
89  }
90
91  $int_pattern = '/^\d+$/';
92  if (empty($_POST['nb_image_line'])
93      or (!preg_match($int_pattern, $_POST['nb_image_line'])))
94  {
95    $errors[] = l10n('nb_image_line_error');
96  }
97
98  if (empty($_POST['nb_line_page'])
99      or (!preg_match($int_pattern, $_POST['nb_line_page'])))
100  {
101    $errors[] = l10n('nb_line_page_error');
102  }
103
104  if ($_POST['maxwidth'] != ''
105      and (!preg_match($int_pattern, $_POST['maxwidth'])
106           or $_POST['maxwidth'] < 50))
107  {
108    $errors[] = l10n('maxwidth_error');
109  }
110  if ($_POST['maxheight']
111       and (!preg_match($int_pattern, $_POST['maxheight'])
112             or $_POST['maxheight'] < 50))
113  {
114    $errors[] = l10n('maxheight_error');
115  }
116  // periods must be integer values, they represents number of days
117  if (!preg_match($int_pattern, $_POST['recent_period'])
118      or $_POST['recent_period'] <= 0)
119  {
120    $errors[] = l10n('periods_error') ;
121  }
122
123  if (isset($_POST['mail_address']))
124  {
125    // if $_POST and $userdata have are same email
126    // validate_mail_address allows, however, to check email
127    $mail_error = validate_mail_address($userdata['id'], $_POST['mail_address']);
128    if (!empty($mail_error))
129    {
130      $errors[] = $mail_error;
131    }
132  }
133
134  if (!empty($_POST['use_new_pwd']))
135  {
136    // password must be the same as its confirmation
137    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
138    {
139      $errors[] = l10n('New password confirmation does not correspond');
140    }
141
142    if ( !defined('IN_ADMIN') )
143    {// changing password requires old password
144      $query = '
145  SELECT '.$conf['user_fields']['password'].' AS password
146    FROM '.USERS_TABLE.'
147    WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
148  ;';
149      list($current_password) = mysql_fetch_row(pwg_query($query));
150 
151      if ($conf['pass_convert']($_POST['password']) != $current_password)
152      {
153        $errors[] = l10n('Current password is wrong');
154      }
155    }
156  }
157
158  if (count($errors) == 0)
159  {
160    // mass_updates function
161    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
162
163    if (isset($_POST['mail_address']))
164    {
165      // update common user informations
166      $fields = array($conf['user_fields']['email']);
167
168      $data = array();
169      $data{$conf['user_fields']['id']} = $userdata['id'];
170      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
171
172      // password is updated only if filled
173      if (!empty($_POST['use_new_pwd']))
174      {
175        array_push($fields, $conf['user_fields']['password']);
176        // password is encrpyted with function $conf['pass_convert']
177        $data{$conf['user_fields']['password']} =
178          $conf['pass_convert']($_POST['use_new_pwd']);
179      }
180      mass_updates(USERS_TABLE,
181                   array('primary' => array($conf['user_fields']['id']),
182                         'update' => $fields),
183                   array($data));
184    }
185
186    // update user "additional" informations (specific to PhpWebGallery)
187    $fields = array(
188      'nb_image_line', 'nb_line_page', 'language', 'maxwidth', 'maxheight',
189      'expand', 'show_nb_comments', 'show_nb_hits', 'recent_period', 'template'
190      );
191
192    $data = array();
193    $data['user_id'] = $userdata['id'];
194
195    foreach ($fields as $field)
196    {
197      if (isset($_POST[$field]))
198      {
199        $data[$field] = $_POST[$field];
200      }
201    }
202    mass_updates(USER_INFOS_TABLE,
203                 array('primary' => array('user_id'), 'update' => $fields),
204                 array($data));
205
206    trigger_action( 'save_profile_from_post', $userdata['id'] );
207   
208    if (!empty($_POST['redirect']))
209    {
210      redirect($_POST['redirect']);
211    }
212  }
213  return true;
214}
215
216
217function load_profile_in_template($url_action, $url_redirect, $userdata)
218{
219  global $template, $conf;
220
221  $template->set_filename('profile_content', 'profile_content.tpl');
222
223  $template->assign('radio_options',
224    array(
225      'true' => l10n('Yes'),
226      'false' => l10n('No')));
227
228  $template->assign(
229    array(
230      'USERNAME'=>$userdata['username'],
231      'EMAIL'=>get_email_address_as_display_text(@$userdata['email']),
232      'NB_IMAGE_LINE'=>$userdata['nb_image_line'],
233      'NB_ROW_PAGE'=>$userdata['nb_line_page'],
234      'RECENT_PERIOD'=>$userdata['recent_period'],
235      'MAXWIDTH'=>@$userdata['maxwidth'],
236      'MAXHEIGHT'=>@$userdata['maxheight'],
237      'EXPAND' =>$userdata['expand'] ? 'true' : 'false',
238      'NB_COMMENTS'=>$userdata['show_nb_comments'] ? 'true' : 'false',
239      'NB_HITS'=>$userdata['show_nb_hits'] ? 'true' : 'false',
240      'REDIRECT' => $url_redirect,
241      'F_ACTION'=>$url_action,
242      ));
243
244  foreach (get_pwg_themes() as $pwg_template)
245  {
246    if (isset($_POST['submit'])
247      or $userdata['template'].'/'.$userdata['theme'] == $pwg_template)
248    {
249      $template->assign('template_selection', $pwg_template);
250    }
251    $template_options[$pwg_template] = $pwg_template;
252  }
253  $template->assign('template_options', $template_options);
254
255  foreach (get_languages() as $language_code => $language_name)
256  {
257    if (isset($_POST['submit']) or $userdata['language'] == $language_code)
258    {
259      $template->assign('language_selection', $language_code);
260    }
261    $language_options[$language_code] = $language_name;
262  }
263  $template->assign('language_options', $language_options);
264
265  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
266  $template->assign('SPECIAL_USER', $special_user);
267  $template->assign('IN_ADMIN', defined('IN_ADMIN'));
268
269  // allow plugins to add their own form data to content
270  trigger_action( 'load_profile_in_template', $userdata );
271 
272  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
273}
274?>
Note: See TracBrowser for help on using the repository browser.