source: trunk/profile.php @ 10464

Last change on this file since 10464 was 10198, checked in by mistic100, 14 years ago

bug:2224 one parameter for change thumnails number (needs some translations)

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