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

Last change on this file since 397 was 394, checked in by gweltas, 20 years ago
  • Template migration
  • Admin Control Panel migration
  • Category management
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                        functions_user.inc.php                         |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-03-26 17:08:09 +0000 (Fri, 26 Mar 2004) $
10// | last modifier : $Author: gweltas $
11// | revision      : $Revision: 394 $
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 = mysql_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                    'short_period', 'long_period', 'template',
88                    'forbidden_categories' );
89    $query = 'SELECT ';
90    for ( $i = 0; $i < sizeof( $infos ); $i++ )
91    {
92      if ( $i > 0 ) $query.= ',';
93      $query.= $infos[$i];
94    }
95    $query.= ' FROM '.USERS_TABLE;
96    $query.= " WHERE username = 'guest'";
97    $query.= ';';
98    $row = mysql_fetch_array( mysql_query( $query ) );
99    // 2. adding new user
100    $query = 'INSERT INTO '.USERS_TABLE;
101    $query.= ' (';
102    $query.= ' username,password,mail_address,status';
103    for ( $i = 0; $i < sizeof( $infos ); $i++ )
104    {
105      $query.= ','.$infos[$i];
106    }
107    $query.= ') values (';
108    $query.= " '".$login."'";
109    $query.= ",'".md5( $password )."'";
110    if ( $mail_address != '' ) $query.= ",'".$mail_address."'";
111    else                       $query.= ',NULL';
112    $query.= ",'".$status."'";
113    foreach ( $infos as $info ) {
114      $query.= ',';
115      if ( !isset( $row[$info] ) ) $query.= 'NULL';
116      else                         $query.= "'".$row[$info]."'";
117    }
118    $query.= ');';
119    mysql_query( $query );
120    // 3. retrieving the id of the newly created user
121    $query = 'SELECT id';
122    $query.= ' FROM '.USERS_TABLE;
123    $query.= " WHERE username = '".$login."';";
124    $row = mysql_fetch_array( mysql_query( $query ) );
125    $user_id = $row['id'];
126    // 4. adding access to the new user, the same as the user "guest"
127    $query = 'SELECT cat_id';
128    $query.= ' FROM '.PREFIX_TABLE.'user_access as ua';
129    $query.=      ','.PREFIX_TABLE.'users as u ';
130    $query.= ' where u.id = ua.user_id';
131    $query.= " and u.username = 'guest';";
132    $result = mysql_query( $query );
133    while( $row = mysql_fetch_array( $result ) )
134    {
135      $query = 'INSERT INTO '.PREFIX_TABLE.'user_access';
136      $query.= ' (user_id,cat_id) VALUES';
137      $query.= ' ('.$user_id.','.$row['cat_id'].');';
138      mysql_query ( $query );
139    }
140    // 5. associate new user to the same groups that the guest
141    $query = 'SELECT group_id';
142    $query.= ' FROM '.PREFIX_TABLE.'user_group AS ug';
143    $query.= ',     '.PREFIX_TABLE.'users      AS u';
144    $query.= " WHERE u.username = 'guest'";
145    $query.= ' AND ug.user_id = u.id';
146    $query.= ';';
147    $result = mysql_query( $query );
148    while( $row = mysql_fetch_array( $result ) )
149    {
150      $query = 'INSERT INTO '.PREFIX_TABLE.'user_group';
151      $query.= ' (user_id,group_id) VALUES';
152      $query.= ' ('.$user_id.','.$row['group_id'].')';
153      $query.= ';';
154      mysql_query ( $query );
155    }
156    // 6. has the same categories informations than guest
157    $query = 'SELECT category_id,date_last,nb_sub_categories';
158    $query.= ' FROM '.PREFIX_TABLE.'user_category AS uc';
159    $query.= ',     '.PREFIX_TABLE.'users         AS u';
160    $query.= " WHERE u.username = 'guest'";
161    $query.= ' AND uc.user_id = u.id';
162    $query.= ';';
163    $result = mysql_query( $query );
164    while( $row = mysql_fetch_array( $result ) )
165    {
166      $query = 'INSERT INTO '.PREFIX_TABLE.'user_category';
167      $query.= ' (user_id,category_id,date_last,nb_sub_categories) VALUES';
168      $query.= ' ('.$user_id.','.$row['category_id'];
169      $query.= ",'".$row['date_last']."',".$row['nb_sub_categories'].')';
170      $query.= ';';
171      mysql_query ( $query );
172    }
173  }
174  return $error;
175}
176
177function update_user( $user_id, $mail_address, $status,
178                      $use_new_password = false, $password = '' )
179{
180  $error = array();
181  $i = 0;
182 
183  $error_mail_address = validate_mail_address( $mail_address );
184  if ( $error_mail_address != '' )
185  {
186    $error[$i++] = $error_mail_address;
187  }
188
189  if ( sizeof( $error ) == 0 )
190  {
191    $query = 'UPDATE '.USERS_TABLE;
192    $query.= " SET status = '".$status."'";
193    if ( $use_new_password )
194    {
195      $query.= ", password = '".md5( $password )."'";
196    }
197    $query.= ', mail_address = ';
198    if ( $mail_address != '' )
199    {
200      $query.= "'".$mail_address."'";
201    }
202    else
203    {
204      $query.= 'NULL';
205    }
206    $query.= ' WHERE id = '.$user_id;
207    $query.= ';';
208    mysql_query( $query );
209  }
210  return $error;
211}
212
213function check_login_authorization()
214{
215  global $user,$lang,$conf,$page;
216
217  if ( $user['is_the_guest'])
218  {
219  if ( $conf['access'] == 'restricted' || (isset($page['cat']) && $page['cat'] == 'fav' ) )
220  {
221    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
222    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
223    exit();
224  }
225  }
226}
227
228//
229// Initialise user settings on page load
230function init_userprefs($userdata)
231{
232  global $conf, $template, $lang, $lang_info;
233  include_once(PHPWG_ROOT_PATH . 'language/infos.lang.php');
234 
235  $language = (!empty($userdata['language']) && !$userdata['is_the_guest'] )?$userdata['language']:$conf['default_lang'];
236  $style = (!empty($userdata['template'])&& !$userdata['is_the_guest'] )?$userdata['template']:$conf['default_style'];
237 
238  if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language . '/common.lang.php')) )
239  {
240    $language = 'en_EN';
241  }
242  include_once(PHPWG_ROOT_PATH . 'language/' . $language . '/common.lang.php');
243 
244 
245  if ($userdata['status'] == 'admin')
246  {
247    if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language. '/admin.lang.php')) )
248    {
249      $language = 'en_EN';
250    }
251  include_once(PHPWG_ROOT_PATH . 'language/' . $language . '/admin.lang.php');
252  }
253 
254  $lang_info['current_code']=$language;
255  $template= setup_style($style);
256  return;
257}
258
259function setup_style($style)
260{
261  $template_path = 'template/' ;
262  $template_name = $style ;
263  $template = new Template(PHPWG_ROOT_PATH . $template_path . $template_name);
264  return $template;
265}
266
267function encode_ip($dotquad_ip)
268{
269  $ip_sep = explode('.', $dotquad_ip);
270  return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
271}
272
273function decode_ip($int_ip)
274{
275  $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
276  return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
277}
278
279function getuserdata($user)
280{
281  $sql = "SELECT * FROM " . USERS_TABLE;
282  $sql.= " WHERE ";
283  $sql .= ( ( is_integer($user) ) ? "id = $user" : "username = '" .  str_replace("\'", "''", $user) . "'" ) . " AND id <> " . ANONYMOUS;
284  $result = mysql_query($sql);
285  return ( $row = mysql_fetch_array($result) ) ? $row : false;
286}
287?>
Note: See TracBrowser for help on using the repository browser.