source: trunk/profile.php @ 2237

Last change on this file since 2237 was 2237, checked in by patdenice, 16 years ago

Merge from revision 2236.
801: add trigger in register_user function.
805: add 2 triggers in profile.php.
769: correct get_email_address_as_display_text function.

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