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

Last change on this file since 631 was 631, checked in by gweltas, 20 years ago
  • User control panel update (user side)
  • User control panel update (admin side)
  • Add of registration link on the main page
  • Minor bug correction for group management
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-12-03 16:30:12 +0000 (Fri, 03 Dec 2004) $
10// | last modifier : $Author: gweltas $
11// | revision      : $Revision: 631 $
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 = pwg_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                    'recent_period', 'template', 'forbidden_categories' );
88    $query = 'SELECT ';
89    for ( $i = 0; $i < sizeof( $infos ); $i++ )
90    {
91      if ( $i > 0 ) $query.= ',';
92      $query.= $infos[$i];
93    }
94    $query.= ' FROM '.USERS_TABLE;
95    $query.= " WHERE username = 'guest'";
96    $query.= ';';
97    $row = mysql_fetch_array( pwg_query( $query ) );
98    // 2. adding new user
99    $query = 'INSERT INTO '.USERS_TABLE;
100    $query.= ' (';
101    $query.= ' username,password,mail_address,status';
102    for ( $i = 0; $i < sizeof( $infos ); $i++ )
103    {
104      $query.= ','.$infos[$i];
105    }
106    $query.= ') values (';
107    $query.= " '".$login."'";
108    $query.= ",'".md5( $password )."'";
109    if ( $mail_address != '' ) $query.= ",'".$mail_address."'";
110    else                       $query.= ',NULL';
111    $query.= ",'".$status."'";
112    foreach ( $infos as $info ) {
113      $query.= ',';
114      if ( !isset( $row[$info] ) ) $query.= 'NULL';
115      else                         $query.= "'".$row[$info]."'";
116    }
117    $query.= ');';
118    pwg_query( $query );
119  }
120  return $error;
121}
122
123function update_user( $user_id, $mail_address, $status,
124                      $use_new_password = false, $password = '' )
125{
126  $error = array();
127  $i = 0;
128 
129  $error_mail_address = validate_mail_address( $mail_address );
130  if ( $error_mail_address != '' )
131  {
132    $error[$i++] = $error_mail_address;
133  }
134
135  if ( sizeof( $error ) == 0 )
136  {
137    $query = 'UPDATE '.USERS_TABLE;
138    $query.= " SET status = '".$status."'";
139    if ( $use_new_password )
140    {
141      $query.= ", password = '".md5( $password )."'";
142    }
143    $query.= ', mail_address = ';
144    if ( $mail_address != '' )
145    {
146      $query.= "'".$mail_address."'";
147    }
148    else
149    {
150      $query.= 'NULL';
151    }
152    $query.= ' WHERE id = '.$user_id;
153    $query.= ';';
154    pwg_query( $query );
155  }
156  return $error;
157}
158
159function check_login_authorization($guest_allowed = true)
160{
161  global $user,$lang,$conf,$page;
162
163  if ( $user['is_the_guest'])
164  {
165  if ( $conf['access'] == 'restricted' || !$guest_allowed )
166  {
167    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
168    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
169    exit();
170  }
171  }
172}
173
174//
175// Initialise user settings on page load
176function init_userprefs($userdata)
177{
178  global $conf, $template, $lang, $lang_info;
179 
180  $language = (!empty($userdata['language']) && !$userdata['is_the_guest'] )?$userdata['language']:$conf['default_language'];
181
182  if (!empty($userdata['template']) and !$userdata['is_the_guest'])
183  {
184    $template = $userdata['template'];
185  }
186  else
187  {
188    $template = $conf['default_template'];
189  }
190
191  if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language . '/common.lang.php')) )
192  {
193    $language = $conf['default_language'];
194  }
195  include_once(PHPWG_ROOT_PATH . 'language/' . $language . '/common.lang.php');
196 
197 
198  if ($userdata['status'] == 'admin')
199  {
200    if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language. '/admin.lang.php')) )
201    {
202      $language = $conf['default_language'];
203    }
204  include_once(PHPWG_ROOT_PATH . 'language/' . $language . '/admin.lang.php');
205  include_once(PHPWG_ROOT_PATH . 'language/' . $language . '/faq.lang.php');
206  }
207
208  $template = setup_style($template);
209  return;
210}
211
212function setup_style($style)
213{
214  $template_path = 'template/' ;
215  $template_name = $style ;
216  $template = new Template(PHPWG_ROOT_PATH . $template_path . $template_name);
217  return $template;
218}
219
220function encode_ip($dotquad_ip)
221{
222  $ip_sep = explode('.', $dotquad_ip);
223  return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
224}
225
226function decode_ip($int_ip)
227{
228  $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
229  return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
230}
231
232function getuserdata($user)
233{
234  $sql = "SELECT * FROM " . USERS_TABLE;
235  $sql.= " WHERE ";
236  $sql .= ( ( is_integer($user) ) ? "id = $user" : "username = '" .  str_replace("\'", "''", $user) . "'" ) . " AND id <> " . ANONYMOUS;
237  $result = pwg_query($sql);
238  return ( $row = mysql_fetch_array($result) ) ? $row : false;
239}
240?>
Note: See TracBrowser for help on using the repository browser.