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 548 2004-10-03 11:12:56Z 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. |
---|
25 | function 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 | |
---|
40 | function 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.= ","; |
---|
162 | if (!isset($row['date_last']) or $row['date_last'] == '') |
---|
163 | { |
---|
164 | $query.= 'NULL'; |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | $query.= "'".$row['date_last']."'"; |
---|
169 | } |
---|
170 | $query.= ",".$row['nb_sub_categories'].')'; |
---|
171 | $query.= ';'; |
---|
172 | mysql_query ( $query ); |
---|
173 | } |
---|
174 | } |
---|
175 | return $error; |
---|
176 | } |
---|
177 | |
---|
178 | function update_user( $user_id, $mail_address, $status, |
---|
179 | $use_new_password = false, $password = '' ) |
---|
180 | { |
---|
181 | $error = array(); |
---|
182 | $i = 0; |
---|
183 | |
---|
184 | $error_mail_address = validate_mail_address( $mail_address ); |
---|
185 | if ( $error_mail_address != '' ) |
---|
186 | { |
---|
187 | $error[$i++] = $error_mail_address; |
---|
188 | } |
---|
189 | |
---|
190 | if ( sizeof( $error ) == 0 ) |
---|
191 | { |
---|
192 | $query = 'UPDATE '.PREFIX_TABLE.'users'; |
---|
193 | $query.= " SET status = '".$status."'"; |
---|
194 | if ( $use_new_password ) |
---|
195 | { |
---|
196 | $query.= ", password = '".md5( $password )."'"; |
---|
197 | } |
---|
198 | $query.= ', mail_address = '; |
---|
199 | if ( $mail_address != '' ) |
---|
200 | { |
---|
201 | $query.= "'".$mail_address."'"; |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | $query.= 'NULL'; |
---|
206 | } |
---|
207 | $query.= ' WHERE id = '.$user_id; |
---|
208 | $query.= ';'; |
---|
209 | mysql_query( $query ); |
---|
210 | } |
---|
211 | return $error; |
---|
212 | } |
---|
213 | |
---|
214 | function check_login_authorization() |
---|
215 | { |
---|
216 | global $user,$lang,$conf,$page; |
---|
217 | |
---|
218 | if ( $user['is_the_guest']) |
---|
219 | { |
---|
220 | if ( $conf['access'] == 'restricted' || (isset($page['cat']) && $page['cat'] == 'fav' ) ) |
---|
221 | { |
---|
222 | echo '<div style="text-align:center;">'.$lang['only_members'].'<br />'; |
---|
223 | echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>'; |
---|
224 | exit(); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | ?> |
---|