source: trunk/profile.php @ 4001

Last change on this file since 4001 was 4001, checked in by nikrou, 15 years ago

Issue 1079: refactoring of code

  • Property svn:eol-style set to LF
File size: 10.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23
24// customize appearance of the site for a user
25// +-----------------------------------------------------------------------+
26// |                           initialization                              |
27// +-----------------------------------------------------------------------+
28
29if (!defined('PHPWG_ROOT_PATH'))
30{//direct script access
31  define('PHPWG_ROOT_PATH','./');
32  include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
33
34  // +-----------------------------------------------------------------------+
35  // | Check Access and exit when user status is not ok                      |
36  // +-----------------------------------------------------------------------+
37  check_status(ACCESS_CLASSIC);
38
39  $userdata = $user;
40
41  trigger_action('loc_begin_profile');
42
43  // Reset to default (Guest) custom settings
44  if (isset($_POST['reset_to_default']))
45  {
46    global $conf;
47    // Get the Guest custom settings
48    $query = '
49SELECT * FROM '.USER_INFOS_TABLE.'
50WHERE user_id = '.$conf['default_user_id'].
51';';
52
53    $result = pwg_query($query);
54   
55    $cache['default_user'] = mysql_fetch_assoc($result);
56   
57    $default_user = array();
58   
59    foreach ($cache['default_user'] as $name => $value)
60    {
61      // If the field is true or false, the variable is transformed into a
62      // boolean value.
63      if ($value == 'true' or $value == 'false')
64      {
65        $default_user[$name] = get_boolean($value);
66      }
67      else
68      {
69        $default_user[$name] = $value;
70      }
71    }
72    // Changing $userdata array values with default ones       
73    $userdata_params = array('nb_image_line', 'nb_line_page', 
74                              'maxwidth', 'maxheight', 'expand', 
75                              'show_nb_comments', 'show_nb_hits');
76    foreach ($userdata_params as $key)     
77    {
78      $userdata[$key] = $default_user[$key];
79      $_POST[$key] = $userdata[$key];
80    }
81  }     
82  save_profile_from_post($userdata, $errors);
83
84  $title= l10n('customize_page_title');
85  $page['body_id'] = 'theProfilePage';
86  include(PHPWG_ROOT_PATH.'include/page_header.php');
87
88  load_profile_in_template(
89    get_root_url().'profile.php', // action
90    make_index_url(), // for redirect
91    $userdata );
92
93  // +-----------------------------------------------------------------------+
94  // |                             errors display                            |
95  // +-----------------------------------------------------------------------+
96  if (count($errors) != 0)
97  {
98    $template->assign('errors', $errors);
99  }
100  $template->set_filename('profile', 'profile.tpl');
101  trigger_action('loc_end_profile');
102  $template->parse('profile');
103  include(PHPWG_ROOT_PATH.'include/page_tail.php');
104}
105
106//------------------------------------------------------ update & customization
107function save_profile_from_post($userdata, &$errors)
108{
109  global $conf;
110  $errors = array();
111
112  if (!isset($_POST['validate']))
113  {
114    return false;
115  }
116
117  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
118  if ($special_user)
119  {
120    unset($_POST['mail_address'],
121          $_POST['password'],
122          $_POST['use_new_pwd'],
123          $_POST['passwordConf']
124          );
125  }
126
127  $int_pattern = '/^\d+$/';
128  if (empty($_POST['nb_image_line'])
129      or (!preg_match($int_pattern, $_POST['nb_image_line'])))
130  {
131    $errors[] = l10n('nb_image_line_error');
132  }
133
134  if (empty($_POST['nb_line_page'])
135      or (!preg_match($int_pattern, $_POST['nb_line_page'])))
136  {
137    $errors[] = l10n('nb_line_page_error');
138  }
139
140  if ($_POST['maxwidth'] != ''
141      and (!preg_match($int_pattern, $_POST['maxwidth'])
142           or $_POST['maxwidth'] < 50))
143  {
144    $errors[] = l10n('maxwidth_error');
145  }
146  if ($_POST['maxheight']
147       and (!preg_match($int_pattern, $_POST['maxheight'])
148             or $_POST['maxheight'] < 50))
149  {
150    $errors[] = l10n('maxheight_error');
151  }
152  // periods must be integer values, they represents number of days
153  if (!preg_match($int_pattern, $_POST['recent_period'])
154      or $_POST['recent_period'] <= 0)
155  {
156    $errors[] = l10n('periods_error') ;
157  }
158
159  if (isset($_POST['mail_address']))
160  {
161    // if $_POST and $userdata have are same email
162    // validate_mail_address allows, however, to check email
163    $mail_error = validate_mail_address($userdata['id'], $_POST['mail_address']);
164    if (!empty($mail_error))
165    {
166      $errors[] = $mail_error;
167    }
168  }
169
170  if (!empty($_POST['use_new_pwd']))
171  {
172    // password must be the same as its confirmation
173    if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
174    {
175      $errors[] = l10n('New password confirmation does not correspond');
176    }
177
178    if ( !defined('IN_ADMIN') )
179    {// changing password requires old password
180      $query = '
181  SELECT '.$conf['user_fields']['password'].' AS password
182    FROM '.USERS_TABLE.'
183    WHERE '.$conf['user_fields']['id'].' = \''.$userdata['id'].'\'
184  ;';
185      list($current_password) = mysql_fetch_row(pwg_query($query));
186 
187      if ($conf['pass_convert']($_POST['password']) != $current_password)
188      {
189        $errors[] = l10n('Current password is wrong');
190      }
191    }
192  }
193
194  if (count($errors) == 0)
195  {
196    // mass_updates function
197    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
198
199    if (isset($_POST['mail_address']))
200    {
201      // update common user informations
202      $fields = array($conf['user_fields']['email']);
203
204      $data = array();
205      $data{$conf['user_fields']['id']} = $userdata['id'];
206      $data{$conf['user_fields']['email']} = $_POST['mail_address'];
207
208      // password is updated only if filled
209      if (!empty($_POST['use_new_pwd']))
210      {
211        array_push($fields, $conf['user_fields']['password']);
212        // password is encrpyted with function $conf['pass_convert']
213        $data{$conf['user_fields']['password']} =
214          $conf['pass_convert']($_POST['use_new_pwd']);
215      }
216      mass_updates(USERS_TABLE,
217                   array('primary' => array($conf['user_fields']['id']),
218                         'update' => $fields),
219                   array($data));
220    }
221
222    // update user "additional" informations (specific to Piwigo)
223    $fields = array(
224      'nb_image_line', 'nb_line_page', 'language', 'maxwidth', 'maxheight',
225      'expand', 'show_nb_comments', 'show_nb_hits', 'recent_period', 'template'
226      );
227
228    $data = array();
229    $data['user_id'] = $userdata['id'];
230
231    foreach ($fields as $field)
232    {
233      if (isset($_POST[$field]))
234      {
235        $data[$field] = $_POST[$field];
236      }
237    }
238    mass_updates(USER_INFOS_TABLE,
239                 array('primary' => array('user_id'), 'update' => $fields),
240                 array($data));
241
242    trigger_action( 'save_profile_from_post', $userdata['id'] );
243   
244    if (!empty($_POST['redirect']))
245    {
246      redirect($_POST['redirect']);
247    }
248  }
249  return true;
250}
251
252
253function load_profile_in_template($url_action, $url_redirect, $userdata)
254{
255  global $template, $conf;
256
257  $template->set_filename('profile_content', 'profile_content.tpl');
258
259  $template->assign('radio_options',
260    array(
261      'true' => l10n('Yes'),
262      'false' => l10n('No')));
263
264  $template->assign(
265    array(
266      'USERNAME'=>$userdata['username'],
267      'EMAIL'=>get_email_address_as_display_text(@$userdata['email']),
268      'NB_IMAGE_LINE'=>$userdata['nb_image_line'],
269      'NB_ROW_PAGE'=>$userdata['nb_line_page'],
270      'RECENT_PERIOD'=>$userdata['recent_period'],
271      'MAXWIDTH'=>@$userdata['maxwidth'],
272      'MAXHEIGHT'=>@$userdata['maxheight'],
273      'EXPAND' =>$userdata['expand'] ? 'true' : 'false',
274      'NB_COMMENTS'=>$userdata['show_nb_comments'] ? 'true' : 'false',
275      'NB_HITS'=>$userdata['show_nb_hits'] ? 'true' : 'false',
276      'REDIRECT' => $url_redirect,
277      'F_ACTION'=>$url_action,
278      ));
279
280  foreach (get_pwg_themes() as $pwg_template)
281  {
282    if (isset($_POST['submit'])
283      or $userdata['template'].'/'.$userdata['theme'] == $pwg_template)
284    {
285      $template->assign('template_selection', $pwg_template);
286    }
287    $template_options[$pwg_template] = $pwg_template;
288  }
289  $template->assign('template_options', $template_options);
290
291  foreach (get_languages() as $language_code => $language_name)
292  {
293    if (isset($_POST['submit']) or $userdata['language'] == $language_code)
294    {
295      $template->assign('language_selection', $language_code);
296    }
297    $language_options[$language_code] = $language_name;
298  }
299
300  $template->assign('language_options', $language_options);
301
302  $special_user = in_array($userdata['id'], array($conf['guest_id'], $conf['default_user_id']));
303  $template->assign('SPECIAL_USER', $special_user);
304  $template->assign('IN_ADMIN', defined('IN_ADMIN'));
305
306  // allow plugins to add their own form data to content
307  trigger_action( 'load_profile_in_template', $userdata );
308 
309  $template->assign_var_from_handle('PROFILE_CONTENT', 'profile_content');
310}
311?>
Note: See TracBrowser for help on using the repository browser.