source: trunk/include/functions_user.inc.php @ 650

Last change on this file since 650 was 650, checked in by plg, 19 years ago
  • replacement of PREFIX_TABLE constant in delete_user function
  • deletion of $isadmin variable, replaced by constant IN_ADMIN
  • small refactoring
  • in include/common.inc.php, deletion of useless part "Obtain and encode users IP" and corresponding functions encode_ip and decode_ip
  • definition of $confdefault_language deleted from include/config.inc.php : it is already present in database table config
  • function init_userprefs deleted (useless), all its content moved to include/user.inc.php
  • admin.lang.php and faq.lang.php are loaded only if current user is in administrative section
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-12-20 19:26:43 +0000 (Mon, 20 Dec 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 650 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28// validate_mail_address verifies whether the given mail address has the
29// right format. ie someone@domain.com "someone" can contain ".", "-" or
30// even "_". Exactly as "domain". The extension doesn't have to be
31// "com". The mail address can also be empty.
32// If the mail address doesn't correspond, an error message is returned.
33function validate_mail_address( $mail_address )
34{
35  global $lang;
36
37  if ( $mail_address == '' )
38  {
39    return '';
40  }
41  $regex = '/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)*\.[a-z]+$/';
42  if ( !preg_match( $regex, $mail_address ) )
43  {
44    return $lang['reg_err_mail_address'];
45  }
46}
47
48function register_user( $login, $password, $password_conf,
49                        $mail_address, $status = 'guest' )
50{
51  global $lang;
52
53  $error = array();
54  $i = 0;
55  // login must not
56  //      1. be empty
57  //      2. start ou end with space character
58  //      3. include ' or " characters
59  //      4. be already used
60  if ( $login == '' )            $error[$i++] = $lang['reg_err_login1'];
61  if ( ereg( "^.* $", $login) )  $error[$i++] = $lang['reg_err_login2'];
62  if ( ereg( "^ .*$", $login ) ) $error[$i++] = $lang['reg_err_login3'];
63
64  if ( ereg( "'", $login ) or ereg( "\"", $login ) )
65    $error[$i++] = $lang['reg_err_login4'];
66  else
67  {
68    $query = 'SELECT id';
69    $query.= ' FROM '.USERS_TABLE;
70    $query.= " WHERE username = '".$login."'";
71    $query.= ';';
72    $result = pwg_query( $query );
73    if ( mysql_num_rows($result) > 0 ) $error[$i++] = $lang['reg_err_login5'];
74  }
75  // given password must be the same as the confirmation
76  if ( $password != $password_conf ) $error[$i++] = $lang['reg_err_pass'];
77
78  $error_mail_address = validate_mail_address( $mail_address );
79  if ( $error_mail_address != '' ) $error[$i++] = $error_mail_address;
80
81  // if no error until here, registration of the user
82  if ( sizeof( $error ) == 0 )
83  {
84    // 1. retrieving default values, the ones of the user "guest"
85    $infos = array( 'nb_image_line', 'nb_line_page', 'language',
86                    'maxwidth', 'maxheight', 'expand', 'show_nb_comments',
87                    'recent_period', 'template', 'forbidden_categories' );
88    $query = 'SELECT ';
89    for ( $i = 0; $i < sizeof( $infos ); $i++ )
90    {
91      if ( $i > 0 ) $query.= ',';
92      $query.= $infos[$i];
93    }
94    $query.= ' FROM '.USERS_TABLE;
95    $query.= " WHERE username = 'guest'";
96    $query.= ';';
97    $row = mysql_fetch_array( pwg_query( $query ) );
98    // 2. adding new user
99    $query = 'INSERT INTO '.USERS_TABLE;
100    $query.= ' (';
101    $query.= ' username,password,mail_address,status';
102    for ( $i = 0; $i < sizeof( $infos ); $i++ )
103    {
104      $query.= ','.$infos[$i];
105    }
106    $query.= ') values (';
107    $query.= " '".$login."'";
108    $query.= ",'".md5( $password )."'";
109    if ( $mail_address != '' ) $query.= ",'".$mail_address."'";
110    else                       $query.= ',NULL';
111    $query.= ",'".$status."'";
112    foreach ( $infos as $info ) {
113      $query.= ',';
114      if ( !isset( $row[$info] ) ) $query.= 'NULL';
115      else                         $query.= "'".$row[$info]."'";
116    }
117    $query.= ');';
118    pwg_query( $query );
119  }
120  return $error;
121}
122
123function update_user( $user_id, $mail_address, $status,
124                      $use_new_password = false, $password = '' )
125{
126  $error = array();
127  $i = 0;
128 
129  $error_mail_address = validate_mail_address( $mail_address );
130  if ( $error_mail_address != '' )
131  {
132    $error[$i++] = $error_mail_address;
133  }
134
135  if ( sizeof( $error ) == 0 )
136  {
137    $query = 'UPDATE '.USERS_TABLE;
138    $query.= " SET status = '".$status."'";
139    if ( $use_new_password )
140    {
141      $query.= ", password = '".md5( $password )."'";
142    }
143    $query.= ', mail_address = ';
144    if ( $mail_address != '' )
145    {
146      $query.= "'".$mail_address."'";
147    }
148    else
149    {
150      $query.= 'NULL';
151    }
152    $query.= ' WHERE id = '.$user_id;
153    $query.= ';';
154    pwg_query( $query );
155  }
156  return $error;
157}
158
159function check_login_authorization($guest_allowed = true)
160{
161  global $user,$lang,$conf,$page;
162
163  if ( $user['is_the_guest'])
164  {
165  if ( $conf['access'] == 'restricted' || !$guest_allowed )
166  {
167    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
168    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
169    exit();
170  }
171  }
172}
173
174function setup_style($style)
175{
176  $template_path = 'template/' ;
177  $template_name = $style ;
178  $template = new Template(PHPWG_ROOT_PATH . $template_path . $template_name);
179  return $template;
180}
181
182function getuserdata($user)
183{
184  $sql = "SELECT * FROM " . USERS_TABLE;
185  $sql.= " WHERE ";
186  $sql .= ( ( is_integer($user) ) ? "id = $user" : "username = '" .  str_replace("\'", "''", $user) . "'" ) . " AND id <> " . ANONYMOUS;
187  $result = pwg_query($sql);
188  return ( $row = mysql_fetch_array($result) ) ? $row : false;
189}
190
191/*
192 * deletes favorites of the current user if he's not allowed to see them
193 *
194 * @return void
195 */
196function check_user_favorites()
197{
198  global $user;
199
200  if ($user['forbidden_categories'] == '')
201  {
202    return;
203  }
204 
205  $query = '
206SELECT f.image_id
207  FROM '.FAVORITES_TABLE.' AS f INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
208    ON f.image_id = ic.image_id
209  WHERE f.user_id = '.$user['id'].'
210    AND ic.category_id IN ('.$user['forbidden_categories'].')
211;';
212  $result = pwg_query($query);
213  $elements = array();
214  while ($row = mysql_fetch_array($result))
215  {
216    array_push($elements, $row['image_id']);
217  }
218
219  if (count($elements) > 0)
220  {
221    $query = '
222DELETE FROM '.FAVORITES_TABLE.'
223  WHERE image_id IN ('.implode(',', $elements).')
224    AND user_id = '.$user['id'].'
225;';
226    pwg_query($query);
227  }
228}
229
230/**
231 * update table user_forbidden for the given user
232 *
233 * table user_forbidden contains calculated data. Calculation is based on
234 * private categories minus categories authorized to the groups the user
235 * belongs to minus the categories directly authorized to the user
236 *
237 * @param int user_id
238 * @return string forbidden_categories
239 */
240function calculate_permissions($user_id)
241{
242  $private_array = array();
243  $authorized_array = array();
244
245  $query = '
246SELECT id
247  FROM '.CATEGORIES_TABLE.'
248  WHERE status = \'private\'
249;';
250  $result = pwg_query($query);
251  while ($row = mysql_fetch_array($result))
252  {
253    array_push($private_array, $row['id']);
254  }
255 
256  // retrieve category ids directly authorized to the user
257  $query = '
258SELECT cat_id
259  FROM '.USER_ACCESS_TABLE.'
260  WHERE user_id = '.$user_id.'
261;';
262  $result = pwg_query($query);
263  while ($row = mysql_fetch_array($result))
264  {
265    array_push($authorized_array, $row['cat_id']);
266  }
267
268  // retrieve category ids authorized to the groups the user belongs to
269  $query = '
270SELECT cat_id
271  FROM '.USER_GROUP_TABLE.' AS ug INNER JOIN '.GROUP_ACCESS_TABLE.' AS ga
272    ON ug.group_id = ga.group_id
273  WHERE ug.user_id = '.$user_id.'
274;';
275  $result = pwg_query($query);
276  while ($row = mysql_fetch_array($result))
277  {
278    array_push($authorized_array, $row['cat_id']);
279  }
280
281  // uniquify ids : some private categories might be authorized for the
282  // groups and for the user
283  $authorized_array = array_unique($authorized_array);
284
285  // only unauthorized private categories are forbidden
286  $forbidden_array = array_diff($private_array, $authorized_array);
287
288  $query = '
289DELETE FROM '.USER_FORBIDDEN_TABLE.'
290  WHERE user_id = '.$user_id.'
291;';
292  pwg_query($query);
293
294  $forbidden_categories = implode(',', $forbidden_array);
295 
296  $query = '
297INSERT INTO '.USER_FORBIDDEN_TABLE.'
298  (user_id,need_update,forbidden_categories)
299  VALUES
300  ('.$user_id.',\'false\',\''.$forbidden_categories.'\')
301;';
302  pwg_query($query);
303 
304  return $forbidden_categories;
305}
306?>
Note: See TracBrowser for help on using the repository browser.