source: trunk/profile.php @ 2223

Last change on this file since 2223 was 2124, checked in by rub, 17 years ago

Resolved 0000759: email unique for each user

Fix bug of last commit 2115

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