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

Last change on this file since 367 was 367, checked in by gweltas, 20 years ago

Migration of installation procedure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 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-02-20 19:07:43 +0000 (Fri, 20 Feb 2004) $
10// | last modifier : $Author: gweltas $
11// | revision      : $Revision: 367 $
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_mapping;
233  $style = $conf['default_style'];
234  if ( !$userdata['is_the_guest'] )
235  {
236    if ( !empty($userdata['language']))
237    {
238      $conf['default_lang'] = $userdata['language'];
239    }
240    if ( !empty($userdata['template']))
241    {
242      $style = $userdata['template'];
243    }
244  }
245
246  if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $conf['default_lang'] . '/common.lang.php')) )
247  {
248    $conf['default_lang'] = 'en_EN';
249  }
250  include_once(PHPWG_ROOT_PATH . 'language/' . $conf['default_lang'] . '/common.lang.php');
251 
252  if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $conf['default_lang'] . '/lang.lang.php')) )
253  {
254    $conf['default_lang'] = 'en_EN';
255  }
256  include_once(PHPWG_ROOT_PATH . 'language/' . $conf['default_lang'] . '/lang.lang.php');
257 
258  if ($userdata['status'] == 'admin')
259  {
260    $admin_lang = $userdata['language'];
261    if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $conf['default_lang'] . '/admin.lang.php')) )
262    {
263      $admin_lang = 'en_EN';
264    }
265        include_once(PHPWG_ROOT_PATH . 'language/' . $admin_lang . '/admin.lang.php');
266  }
267
268  $template= setup_style($style);
269  return;
270}
271
272function setup_style($style)
273{
274  $template_path = 'template/' ;
275  $template_name = $style ;
276  include_once( PHPWG_ROOT_PATH . $template_path . $template_name.'/htmlfunctions.inc.php' );
277  $template = new Template(PHPWG_ROOT_PATH . $template_path . $template_name);
278  return $template;
279}
280
281function encode_ip($dotquad_ip)
282{
283        $ip_sep = explode('.', $dotquad_ip);
284        return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
285}
286
287function decode_ip($int_ip)
288{
289        $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
290        return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
291}
292?>
Note: See TracBrowser for help on using the repository browser.