source: branches/release-1_3/include/functions_user.inc.php @ 309

Last change on this file since 309 was 309, checked in by z0rglub, 20 years ago

code simplified

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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 309 2004-01-21 23:25:20Z 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( $login, $password, $password_conf,
41                        $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 == '' )            $error[$i++] = $lang['reg_err_login1'];
53  if ( ereg( "^.* $", $login) )  $error[$i++] = $lang['reg_err_login2'];
54  if ( ereg( "^ .*$", $login ) ) $error[$i++] = $lang['reg_err_login3'];
55
56  if ( ereg( "'", $login ) or ereg( "\"", $login ) )
57    $error[$i++] = $lang['reg_err_login4'];
58  else
59  {
60    $query = 'SELECT id';
61    $query.= ' FROM '.PREFIX_TABLE.'users';
62    $query.= " WHERE username = '".$login."'";
63    $query.= ';';
64    $result = mysql_query( $query );
65    if ( mysql_num_rows($result) > 0 ) $error[$i++] = $lang['reg_err_login5'];
66  }
67  // given password must be the same as the confirmation
68  if ( $password != $password_conf ) $error[$i++] = $lang['reg_err_pass'];
69
70  $error_mail_address = validate_mail_address( $mail_address );
71  if ( $error_mail_address != '' ) $error[$i++] = $error_mail_address;
72
73  // if no error until here, registration of the user
74  if ( sizeof( $error ) == 0 )
75  {
76    // 1. retrieving default values, the ones of the user "guest"
77    $infos = array( 'nb_image_line', 'nb_line_page', 'language',
78                    'maxwidth', 'maxheight', 'expand', 'show_nb_comments',
79                    'short_period', 'long_period', 'template',
80                    'forbidden_categories' );
81    $query = 'SELECT ';
82    for ( $i = 0; $i < sizeof( $infos ); $i++ )
83    {
84      if ( $i > 0 ) $query.= ',';
85      $query.= $infos[$i];
86    }
87    $query.= ' FROM '.PREFIX_TABLE.'users';
88    $query.= " WHERE username = 'guest'";
89    $query.= ';';
90    $row = mysql_fetch_array( mysql_query( $query ) );
91    // 2. adding new user
92    $query = 'INSERT INTO '.PREFIX_TABLE.'users';
93    $query.= ' (';
94    $query.= ' username,password,mail_address,status';
95    for ( $i = 0; $i < sizeof( $infos ); $i++ )
96    {
97      $query.= ','.$infos[$i];
98    }
99    $query.= ') values (';
100    $query.= " '".$login."'";
101    $query.= ",'".md5( $password )."'";
102    if ( $mail_address != '' ) $query.= ",'".$mail_address."'";
103    else                       $query.= ',NULL';
104    $query.= ",'".$status."'";
105    foreach ( $infos as $info ) {
106      $query.= ',';
107      if ( !isset( $row[$info] ) ) $query.= 'NULL';
108      else                         $query.= "'".$row[$info]."'";
109    }
110    $query.= ');';
111    mysql_query( $query );
112    // 3. retrieving the id of the newly created user
113    $query = 'SELECT id';
114    $query.= ' FROM '.PREFIX_TABLE.'users';
115    $query.= " WHERE username = '".$login."';";
116    $row = mysql_fetch_array( mysql_query( $query ) );
117    $user_id = $row['id'];
118    // 4. adding access to the new user, the same as the user "guest"
119    $query = 'SELECT cat_id';
120    $query.= ' FROM '.PREFIX_TABLE.'user_access as ua';
121    $query.=      ','.PREFIX_TABLE.'users as u ';
122    $query.= ' where u.id = ua.user_id';
123    $query.= " and u.username = 'guest';";
124    $result = mysql_query( $query );
125    while( $row = mysql_fetch_array( $result ) )
126    {
127      $query = 'INSERT INTO '.PREFIX_TABLE.'user_access';
128      $query.= ' (user_id,cat_id) VALUES';
129      $query.= ' ('.$user_id.','.$row['cat_id'].');';
130      mysql_query ( $query );
131    }
132    // 5. associate new user to the same groups that the guest
133    $query = 'SELECT group_id';
134    $query.= ' FROM '.PREFIX_TABLE.'user_group AS ug';
135    $query.= ',     '.PREFIX_TABLE.'users      AS u';
136    $query.= " WHERE u.username = 'guest'";
137    $query.= ' AND ug.user_id = u.id';
138    $query.= ';';
139    $result = mysql_query( $query );
140    while( $row = mysql_fetch_array( $result ) )
141    {
142      $query = 'INSERT INTO '.PREFIX_TABLE.'user_group';
143      $query.= ' (user_id,group_id) VALUES';
144      $query.= ' ('.$user_id.','.$row['group_id'].')';
145      $query.= ';';
146      mysql_query ( $query );
147    }
148    // 6. has the same categories informations than guest
149    $query = 'SELECT category_id,date_last,nb_sub_categories';
150    $query.= ' FROM '.PREFIX_TABLE.'user_category AS uc';
151    $query.= ',     '.PREFIX_TABLE.'users         AS u';
152    $query.= " WHERE u.username = 'guest'";
153    $query.= ' AND uc.user_id = u.id';
154    $query.= ';';
155    $result = mysql_query( $query );
156    while( $row = mysql_fetch_array( $result ) )
157    {
158      $query = 'INSERT INTO '.PREFIX_TABLE.'user_category';
159      $query.= ' (user_id,category_id,date_last,nb_sub_categories) VALUES';
160      $query.= ' ('.$user_id.','.$row['category_id'];
161      $query.= ",'".$row['date_last']."',".$row['nb_sub_categories'].')';
162      $query.= ';';
163      mysql_query ( $query );
164    }
165  }
166  return $error;
167}
168
169function update_user( $user_id, $mail_address, $status,
170                      $use_new_password = false, $password = '' )
171{
172  $error = array();
173  $i = 0;
174 
175  $error_mail_address = validate_mail_address( $mail_address );
176  if ( $error_mail_address != '' )
177  {
178    $error[$i++] = $error_mail_address;
179  }
180
181  if ( sizeof( $error ) == 0 )
182  {
183    $query = 'UPDATE '.PREFIX_TABLE.'users';
184    $query.= " SET status = '".$status."'";
185    if ( $use_new_password )
186    {
187      $query.= ", password = '".md5( $password )."'";
188    }
189    $query.= ', mail_address = ';
190    if ( $mail_address != '' )
191    {
192      $query.= "'".$mail_address."'";
193    }
194    else
195    {
196      $query.= 'NULL';
197    }
198    $query.= ' WHERE id = '.$user_id;
199    $query.= ';';
200    mysql_query( $query );
201  }
202  return $error;
203}
204
205function check_login_authorization()
206{
207  global $user,$lang,$conf,$page;
208
209  if ( $user['is_the_guest'])
210  {
211  if ( $conf['access'] == 'restricted' || (isset($page['cat']) && $page['cat'] == 'fav' ) )
212  {
213    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
214    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
215    exit();
216  }
217  }
218}
219?>
Note: See TracBrowser for help on using the repository browser.