source: tags/release-1_3_0/include/functions_user.inc.php @ 20335

Last change on this file since 20335 was 99, checked in by z0rglub, 21 years ago

phpwebgallery_users.theme is no longuer used.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1<?php
2/***************************************************************************
3 *                           functions_user.inc.php                        *
4 *                            --------------------                         *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: functions_user.inc.php 99 2003-09-14 15:03:38Z z0rglub $
9 *                                                                         *
10 ***************************************************************************
11
12 ***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20// validate_mail_address verifies whether the given mail address has the
21// right format. ie someone@domain.com "someone" can contain ".", "-" or
22// even "_". Exactly as "domain". The extension doesn't have to be
23// "com". The mail address can also be empty.
24// If the mail address doesn't correspond, an error message is returned.
25function validate_mail_address( $mail_address )
26{
27  global $lang;
28
29  if ( $mail_address == '' )
30  {
31    return '';
32  }
33  $regex = '/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)*\.[a-z]+$/';
34  if ( !preg_match( $regex, $mail_address ) )
35  {
36    return $lang['reg_err_mail_address'];
37  }
38}
39
40function register_user(
41  $login, $password, $password_conf, $mail_address, $status = 'guest' )
42{
43  global $lang;
44
45  $error = array();
46  $i = 0;
47  // login must not
48  //      1. be empty
49  //      2. start ou end with space character
50  //      3. include ' or " characters
51  //      4. be already used
52  if ( $login == '' )
53  {
54    $error[$i++] = $lang['reg_err_login1'];
55  }
56  if ( ereg( "^.* $", $login) )
57  {
58    $error[$i++] = $lang['reg_err_login2'];
59  }
60  if ( ereg( "^ .*$", $login ) )
61  {
62    $error[$i++] = $lang['reg_err_login3'];
63  }
64  if ( ereg( "'", $login ) or ereg( "\"", $login ) )
65  {
66    $error[$i++] = $lang['reg_err_login4'];
67  }
68  else
69  {
70    $query = 'select id';
71    $query.= ' from '.PREFIX_TABLE.'users';
72    $query.= " where username = '".$login."';";
73    $result = mysql_query( $query );
74    if ( mysql_num_rows( $result ) > 0 )
75    {
76      $error[$i++] = $lang['reg_err_login5'];
77    }
78  }
79  // given password must be the same as the confirmation
80  if ( $password != $password_conf )
81  {
82    $error[$i++] = $lang['reg_err_pass'];
83  }
84
85  $error_mail_address = validate_mail_address( $mail_address );
86  if ( $error_mail_address != '' )
87  {
88    $error[$i++] = $error_mail_address;
89  }
90
91  // if no error until here, registration of the user
92  if ( sizeof( $error ) == 0 )
93  {
94    // 1. retrieving default values, the ones of the user "guest"
95    $infos = array( 'nb_image_line', 'nb_line_page', 'language',
96                    'maxwidth', 'maxheight', 'expand', 'show_nb_comments',
97                    'short_period', 'long_period', 'template' );
98    $query = 'select';
99    for ( $i = 0; $i < sizeof( $infos ); $i++ )
100    {
101      if ( $i > 0 )
102      {
103        $query.= ',';
104      }
105      else
106      {
107        $query.= ' ';
108      }
109      $query.= $infos[$i];
110    }
111    $query.= ' from '.PREFIX_TABLE.'users';
112    $query.= " where username = 'guest';";
113    $row = mysql_fetch_array( mysql_query( $query ) );
114    // 2. adding new user
115    $query = 'insert into '.PREFIX_TABLE.'users';
116    $query.= ' (';
117    $query.= ' username,password,mail_address,status';
118    for ( $i = 0; $i < sizeof( $infos ); $i++ )
119    {
120      $query.= ','.$infos[$i];
121    }
122    $query.= ') values (';
123    $query.= " '".$login."'";
124    $query.= ",'".md5( $password )."'";
125    if ( $mail_address != '' )
126    {
127      $query.= ",'".$mail_address."'";
128    }
129    else
130    {
131      $query.= ',NULL';
132    }
133    $query.= ",'".$status."'";
134    for ( $i = 0; $i < sizeof( $infos ); $i++ )
135    {
136      $query.= ',';
137      if ( $row[$infos[$i]] == '' )
138      {
139        $query.= 'NULL';
140      }
141      else
142      {
143        $query.= "'".$row[$infos[$i]]."'";
144      }
145    }
146    $query.= ');';
147    mysql_query( $query );
148    // 3. retrieving the id of the newly created user
149    $query = 'SELECT id';
150    $query.= ' FROM '.PREFIX_TABLE.'users';
151    $query.= " WHERE username = '".$login."';";
152    $row = mysql_fetch_array( mysql_query( $query ) );
153    $user_id = $row['id'];
154    // 4. adding access to the new user, the same as the user "guest"
155    $query = 'SELECT cat_id';
156    $query.= ' FROM '.PREFIX_TABLE.'user_access as ua';
157    $query.=      ','.PREFIX_TABLE.'users as u ';
158    $query.= ' where u.id = ua.user_id';
159    $query.= " and u.username = 'guest';";
160    $result = mysql_query( $query );
161    while( $row = mysql_fetch_array( $result ) )
162    {
163      $query = 'INSERT INTO '.PREFIX_TABLE.'user_access';
164      $query.= ' (user_id,cat_id) VALUES';
165      $query.= ' ('.$user_id.','.$row['cat_id'].');';
166      mysql_query ( $query );
167    }
168  }
169  return $error;
170}
171
172function update_user( $user_id, $mail_address, $status,
173                      $use_new_password = false, $password = '' )
174{
175  $error = array();
176  $i = 0;
177 
178  $error_mail_address = validate_mail_address( $mail_address );
179  if ( $error_mail_address != '' )
180  {
181    $error[$i++] = $error_mail_address;
182  }
183
184  if ( sizeof( $error ) == 0 )
185  {
186    $query = 'UPDATE '.PREFIX_TABLE.'users';
187    $query.= " SET status = '".$status."'";
188    if ( $use_new_password )
189    {
190      $query.= ", password = '".md5( $password )."'";
191    }
192    $query.= ', mail_address = ';
193    if ( $mail_address != '' )
194    {
195      $query.= "'".$mail_address."'";
196    }
197    else
198    {
199      $query.= 'NULL';
200    }
201    $query.= ' WHERE id = '.$user_id;
202    $query.= ';';
203    mysql_query( $query );
204  }
205  return $error;
206}
207
208function check_login_authorization()
209{
210  global $user,$lang,$conf,$page;
211
212  if ( $user['is_the_guest']
213       and ( $conf['access'] == 'restricted' or $page['cat'] == 'fav' ) )
214  {
215    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
216    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
217    exit();
218  }
219}
220       
221// The function get_restrictions returns an array with the ids of the
222// restricted categories for the user.
223// If the $check_invisible parameter is set to true, invisible categories
224// are added to the restricted one in the array.
225function get_restrictions( $user_id, $user_status,
226                           $check_invisible, $use_groups = true )
227{
228  // 1. retrieving ids of private categories
229  $query = 'SELECT id';
230  $query.= ' FROM '.PREFIX_TABLE.'categories';
231  $query.= " WHERE status = 'private'";
232  $query.= ';';
233  $result = mysql_query( $query );
234  $privates = array();
235  while ( $row = mysql_fetch_array( $result ) )
236  {
237    array_push( $privates, $row['id'] );
238  }
239  // 2. retrieving all authorized categories for the user
240  $authorized = array();
241  // 2.1. retrieving authorized categories thanks to personnal user
242  //      authorization
243  $query = 'SELECT cat_id';
244  $query.= ' FROM '.PREFIX_TABLE.'user_access';
245  $query.= ' WHERE user_id = '.$user_id;
246  $query.= ';';
247  $result = mysql_query( $query );
248  while ( $row = mysql_fetch_array( $result ) )
249  {
250    array_push( $authorized, $row['cat_id'] );
251  }
252  // 2.2. retrieving authorized categories thanks to group authorization to
253  //      which the user is a member
254  if ( $use_groups )
255  {
256    $query = 'SELECT ga.cat_id';
257    $query.= ' FROM '.PREFIX_TABLE.'user_group as ug';
258    $query.= ', '.PREFIX_TABLE.'group_access as ga';
259    $query.= ' WHERE ug.group_id = ga.group_id';
260    $query.= ' AND ug.user_id = '.$user_id;
261    $query.= ';';
262    $result = mysql_query( $query );
263    while ( $row = mysql_fetch_array( $result ) )
264    {
265      array_push( $authorized, $row['cat_id'] );
266    }
267    $authorized = array_unique( $authorized );
268  }
269
270  $forbidden = array();
271  foreach ( $privates as $private ) {
272    if ( !in_array( $private, $authorized ) )
273    {
274      array_push( $forbidden, $private );
275    }
276  }
277
278  if ( $check_invisible )
279  {
280    // 3. adding to the restricted categories, the invisible ones
281    if ( $user_status != 'admin' )
282    {
283      $query = 'SELECT id';
284      $query.= ' FROM '.PREFIX_TABLE.'categories';
285      $query.= " WHERE visible = 'false';";
286      $result = mysql_query( $query );
287      while ( $row = mysql_fetch_array( $result ) )
288      {
289        array_push( $forbidden, $row['id'] );
290      }
291    }
292  }
293  return array_unique( $forbidden );
294}
295
296// The get_all_restrictions function returns an array with all the
297// categories id which are restricted for the user. Including the
298// sub-categories and invisible categories
299function get_all_restrictions( $user_id, $user_status )
300{
301  $restricted_cats = get_restrictions( $user_id, $user_status, true );
302  foreach ( $restricted_cats as $restricted_cat ) {
303    $sub_restricted_cats = get_subcats_id( $restricted_cat );
304    foreach ( $sub_restricted_cats as $sub_restricted_cat ) {
305      array_push( $restricted_cats, $sub_restricted_cat );
306    }
307  }
308  return $restricted_cats;
309}
310
311// The function is_user_allowed returns :
312//      - 0 : if the category is allowed with this $restrictions array
313//      - 1 : if this category is not allowed
314//      - 2 : if an uppercat category is not allowed
315function is_user_allowed( $category_id, $restrictions )
316{
317  $lowest_category_id = $category_id;
318
319  $is_root = false;
320  while ( !$is_root and !in_array( $category_id, $restrictions ) )
321  {
322    $query = 'SELECT id_uppercat';
323    $query.= ' FROM '.PREFIX_TABLE.'categories';
324    $query.= ' WHERE id = '.$category_id;
325    $query.= ';';
326    $row = mysql_fetch_array( mysql_query( $query ) );
327    if ( $row['id_uppercat'] == '' ) $is_root = true;
328    $category_id = $row['id_uppercat'];
329  }
330
331  if ( in_array( $lowest_category_id, $restrictions ) ) return 1;
332  if ( in_array( $category_id,        $restrictions ) ) return 2;
333  // this user is allowed to go in this category
334  return 0;
335}
336?>
Note: See TracBrowser for help on using the repository browser.