source: trunk/profile.php @ 2048

Last change on this file since 2048 was 2048, checked in by rvelices, 17 years ago
  • fix help files
  • feature 695 added action loc_user_profile_updated in profile.php
  • added event allow_increment_element_hit_count in picture.php
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 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 2048 2007-06-30 01:40:19Z rvelices $
8// | last update   : $Date: 2007-06-30 01:40:19 +0000 (Sat, 30 Jun 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2048 $
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    $mail_error = validate_mail_address($_POST['mail_address']);
119    if (!empty($mail_error))
120    {
121      $errors[] = $mail_error;
122    }
123  }
124
125  if (!empty($_POST['use_new_pwd']))
126  {
127    // password must be the same as its confirmation
128    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
129    {
130      $errors[] = l10n('New password confirmation does not correspond');
131    }
132
133    if ( !defined('IN_ADMIN') )
134    {// changing password requires old password
135      $query = '
136  SELECT '.$conf['user_fields']['password'].' AS password
137    FROM '.USERS_TABLE.'
138    WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
139  ;';
140      list($current_password) = mysql_fetch_row(pwg_query($query));
141 
142      if ($conf['pass_convert']($_POST['password']) != $current_password)
143      {
144        $errors[] = l10n('Current password is wrong');
145      }
146    }
147  }
148
149  if (count($errors) == 0)
150  {
151    // mass_updates function
152    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
153
154    if (isset($_POST['mail_address']))
155    {
156      // update common user informations
157      $fields = array($conf['user_fields']['email']);
158
159      $data = array();
160      $data{$conf['user_fields']['id']} = $_POST['userid'];
161      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
162
163      // password is updated only if filled
164      if (!empty($_POST['use_new_pwd']))
165      {
166        array_push($fields, $conf['user_fields']['password']);
167        // password is encrpyted with function $conf['pass_convert']
168        $data{$conf['user_fields']['password']} =
169          $conf['pass_convert']($_POST['use_new_pwd']);
170      }
171      mass_updates(USERS_TABLE,
172                   array('primary' => array($conf['user_fields']['id']),
173                         'update' => $fields),
174                   array($data));
175    }
176
177    // update user "additional" informations (specific to PhpWebGallery)
178    $fields = array(
179      'nb_image_line', 'nb_line_page', 'language', 'maxwidth', 'maxheight',
180      'expand', 'show_nb_comments', 'show_nb_hits', 'recent_period', 'template'
181      );
182
183    $data = array();
184    $data['user_id'] = $_POST['userid'];
185
186    foreach ($fields as $field)
187    {
188      if (isset($_POST[$field]))
189      {
190        $data[$field] = $_POST[$field];
191      }
192    }
193    mass_updates(USER_INFOS_TABLE,
194                 array('primary' => array('user_id'), 'update' => $fields),
195                 array($data));
196
197    trigger_action( 'loc_user_profile_updated', $_POST['userid'] );
198   
199    if (!empty($_POST['redirect']))
200    {
201      redirect($_POST['redirect']);
202    }
203  }
204  return true;
205}
206
207
208function load_profile_in_template($url_action, $url_redirect, $userdata)
209{
210  global $template, $conf;
211
212  $template->set_filename('profile_content', 'profile_content.tpl');
213
214  $expand = ($userdata['expand'] == 'true') ? 
215            'EXPAND_TREE_YES':'EXPAND_TREE_NO';
216
217  $nb_comments =
218    ($userdata['show_nb_comments'] == 'true') ? 
219               'NB_COMMENTS_YES':'NB_COMMENTS_NO';
220
221  $nb_hits =
222    ($userdata['show_nb_hits'] == 'true') ? 
223               'NB_HITS_YES':'NB_HITS_NO';
224
225  $template->assign_vars(
226    array(
227      'USERNAME'=>$userdata['username'],
228      'USERID'=>$userdata['id'],
229      'EMAIL'=>@$userdata['email'],
230      'NB_IMAGE_LINE'=>$userdata['nb_image_line'],
231      'NB_ROW_PAGE'=>$userdata['nb_line_page'],
232      'RECENT_PERIOD'=>$userdata['recent_period'],
233      'MAXWIDTH'=>@$userdata['maxwidth'],
234      'MAXHEIGHT'=>@$userdata['maxheight'],
235 
236      $expand=>'checked="checked"',
237      $nb_comments=>'checked="checked"',
238      $nb_hits=>'checked="checked"',
239 
240      'REDIRECT' => $url_redirect,
241 
242      'F_ACTION'=>$url_action,
243      ));
244
245  $blockname = 'template_option';
246
247  foreach (get_pwg_themes() as $pwg_template)
248  {
249    if (isset($_POST['submit']))
250    {
251      $selected = $_POST['template']==$pwg_template ? 'selected="selected"' : '';
252    }
253    else if ($userdata['template'].'/'.$userdata['theme'] == $pwg_template)
254    {
255      $selected = 'selected="selected"';
256    }
257    else
258    {
259      $selected = '';
260    }
261 
262    $template->assign_block_vars(
263      $blockname,
264      array(
265        'VALUE'=> $pwg_template,
266        'CONTENT' => $pwg_template,
267        'SELECTED' => $selected
268        ));
269  }
270
271  $blockname = 'language_option';
272
273  foreach (get_languages() as $language_code => $language_name)
274  {
275    if (isset($_POST['submit']))
276    {
277      $selected = $_POST['language']==$language_code ? 'selected="selected"':'';
278    }
279    else if ($userdata['language'] == $language_code)
280    {
281      $selected = 'selected="selected"';
282    }
283    else
284    {
285      $selected = '';
286    }
287 
288    $template->assign_block_vars(
289      $blockname,
290      array(
291        'VALUE'=> $language_code,
292        'CONTENT' => $language_name,
293        'SELECTED' => $selected
294        ));
295  }
296
297  if (!(in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']))))
298  {
299    $template->assign_block_vars('not_special_user', array());
300    if ( !defined('IN_ADMIN') )
301    {
302      $template->assign_block_vars( 'not_special_user.not_admin', array() );
303    }
304  }
305
306  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
307}
308?>
Note: See TracBrowser for help on using the repository browser.