source: tags/version_1_3/include/functions_user.inc.php @ 21188

Last change on this file since 21188 was 2, checked in by z0rglub, 21 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1<?php
2/***************************************************************************
3 *                           functions_user.inc.php                        *
4 *                            --------------------                         *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17function validate_mail_address( $mail_address )
18{
19  global $lang;
20
21  $output = '';
22  // le mail doit être conforme à qqch du type : nom@serveur.com
23  if ( $mail_address != ''
24       and !ereg( "([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+)",
25                 $mail_address ) )
26  {
27    $output = $lang['reg_err_mail_address'];
28  }
29
30  return $output;
31}
32
33function register_user( $login, $password, $password_conf,
34                        $mail_address, $status = 'visiteur' )
35{
36  global $prefixeTable;
37
38  $error = array();
39  $i = 0;
40  // le login ne doit pas
41  //      1. être vide
42  //      2. commencer ou se terminer par un espace
43  //      3. comporter les caractères ' ou "
44  //      4. être déjà utilisé
45  if ( $login == '' )
46  {
47    $error[$i++] = $lang['reg_err_login1'];
48  }
49  if ( ereg( "^.* $", $login) )
50  {
51    $error[$i++] = $lang['reg_err_login2'];
52  }
53  if ( ereg( "^ .*$", $login ) )
54  {
55    $error[$i++] = $lang['reg_err_login3'];
56  }
57  if ( ereg( "'", $login ) or ereg( "\"", $login ) )
58  {
59    $error[$i++] = $lang['reg_err_login4'];
60  }
61  else
62  {
63    $query = 'select id';
64    $query.= ' from '.$prefixeTable.'users';
65    $query.= " where pseudo = '".$login."';";
66    $result = mysql_query( $query );
67    if ( mysql_num_rows( $result ) > 0 )
68    {
69      $error[$i++] = $lang['reg_err_login5'];
70    }
71  }
72  // on vérifie que le password rentré correspond bien
73  // à la confirmation faite par l'utilisateur
74  if ( $password != $password_conf )
75  {
76    $error[$i++] = $lang['reg_err_pass'];
77  }
78
79  $error_mail_address = validate_mail_address( $mail_address );
80  if ( $error_mail_address != '' )
81  {
82    $error[$i++] = $error_mail_address;
83  }
84 
85  // on enregistre le nouvel utilisateur si aucune
86  //erreur détectée dans les paramètres
87  if ( sizeof( $error ) == 0 )
88  {
89    // 1.récupération des valeurs par défaut de l'application
90    $infos = array( 'nb_image_line', 'nb_line_page', 'theme', 'language',
91                    'maxwidth', 'maxheight', 'expand', 'show_nb_comments',
92                    'short_period', 'long_period', 'template' );
93    $query = 'select';
94    for ( $i = 0; $i < sizeof( $infos ); $i++ )
95    {
96      if ( $i > 0 )
97      {
98        $query.= ',';
99      }
100      else
101      {
102        $query.= ' ';
103      }
104      $query.= $infos[$i];
105    }
106    $query.= ' from '.$prefixeTable.'users';
107    $query.= " where pseudo = 'visiteur';";
108    $row = mysql_fetch_array( mysql_query( $query ) );
109    // 2.ajout du nouvel utilisateur
110    $query = 'insert into '.$prefixeTable.'users';
111    $query.= ' (';
112    $query.= ' pseudo,password,mail_address,status';
113    for ( $i = 0; $i < sizeof( $infos ); $i++ )
114    {
115      $query.= ','.$infos[$i];
116    }
117    $query.= ' values (';
118    $query.= " '".$login."'";
119    $query.= ",'".md5( $password )."'";
120    if ( $mail_address != '' )
121    {
122      $query.= ",'".$mail_address."'";
123    }
124    else
125    {
126      $query.= ',NULL';
127    }
128    $query.= ",'".$status."'";
129    for ( $i = 0; $i < sizeof( $infos ); $i++ )
130    {
131      $query.= ','.$row[$infos[$i]];
132    }
133    $query.= ');';
134    mysql_query( $query );
135    // 3. récupérer l'identifiant de l'utilisateur nouvellement créé
136    $query = 'select id';
137    $query.= ' from '.$prefixeTable.'users';
138    $query.= " where pseudo = '".$login."';";
139    $row = mysql_fetch_array( mysql_query( $query ) );
140    $user_id = $row['id'];
141    // 4.ajouter les restrictions au nouvel utilisateur,
142    //   les mêmes que celles de l'utilisateur par défaut
143    $query = 'select cat_id';
144    $query.= ' from '.$prefixeTable.'restrictions as r';
145    $query.=      ','.$prefixeTable.'users as u ';
146    $query.= ' where u.id = r.user_id';
147    $query.= " and u.pseudo = 'visiteur';";
148    $result = mysql_query( $query );
149    while( $row = mysql_fetch_array( $result ) )
150    {
151      $query = 'insert into '.$prefixeTable.'restrictions';
152      $query.= ' (user_id,cat_id) values';
153      $query.= ' ('.$user_id.','.$row['cat_id'].');';
154      mysql_query ( $query );
155    }
156  }
157  return $error;
158}
159
160function update_user( $user_id, $mail_address, $status,
161                      $use_new_password = false, $password = '' )
162{
163  global $prefixeTable;
164
165  $error = array();
166  $i = 0;
167 
168  $error_mail_address = validate_mail_address( $mail_address );
169  if ( $error_mail_address != '' )
170  {
171    $error[$i++] = $error_mail_address;
172  }
173
174  if ( sizeof( $error ) == 0 )
175  {
176    $query = 'update '.$prefixeTable.'users';
177    $query.= " set status = '".$status."'";
178    if ( $use_new_password )
179    {
180      $query.= ", password = '".md5( $password )."'";
181    }
182    $query.= ', mail_address = ';
183    if ( $mail_address != '' )
184    {
185      $query.= "'".$mail_address."'";
186    }
187    else
188    {
189      $query.= 'NULL';
190    }
191    $query.= ' where id = '.$user_id;
192    $query.= ';';
193    mysql_query( $query );
194  }
195  return $error;
196}
197
198function check_login_authorization()
199{
200  global $user,$lang,$conf,$page;
201  if ( $user['is_the_guest']
202       and ( $conf['acces'] == 'restreint' or $page['cat'] == 'fav' ) )
203  {
204    echo '<div style="text-align:center;">'.$lang['only_members'].'<br />';
205    echo '<a href="./identification.php">'.$lang['ident_title'].'</a></div>';
206    exit();
207  }
208}
209       
210// The function get_restrictions returns an array with the ids of the
211// restricted categories for the user.
212// If the $check_invisible parameter is set to true, invisible categories
213// are added to the restricted one in the array.
214function get_restrictions( $user_id, $user_status, $check_invisible )
215{
216  global $prefixeTable;
217               
218  // 1. getting the ids of the restricted categories
219  $query = "select cat_id";
220  $query.= " from $prefixeTable"."restrictions";
221  $query.= " where user_id = $user_id;";
222  $result = mysql_query( $query );
223  $i = 0;
224  $restriction = array();
225  while ( $row = mysql_fetch_array( $result ) )
226  {
227    $restriction[$i++] = $row['cat_id'];
228  }
229  if ( $check_invisible )
230  {
231    // 2. adding to the restricted categories, the invisible ones
232    if ( $user_status != "admin" )
233    {
234      $query = 'select id';
235      $query.= ' from '.$prefixeTable.'categories';
236      $query.= " where status='invisible';";
237      $result = mysql_query( $query );
238      while ( $row = mysql_fetch_array( $result ) )
239      {
240        $restriction[$i++] = $row['id'];
241      }
242    }
243  }
244  return $restriction;
245}
246
247// The get_all_restrictions function returns an array with all the
248// categories id which are restricted for the user. Including the
249// sub-categories and invisible categories
250function get_all_restrictions( $user_id, $user_status )
251{
252  global $prefixeTable;
253               
254  $restricted_cat = get_restrictions( $user_id, $user_status, true );
255  $i = sizeof( $restricted_cat );
256  for ( $k = 0; $k < sizeof( $restricted_cat ); $k++ )
257  {
258    $sub_restricted_cat = get_subcats_id( $restricted_cat[$k] );
259    for ( $j = 0; $j < sizeof( $sub_restricted_cat ); $j++ )
260    {
261      $restricted_cat[$i++] = $sub_restricted_cat[$j];
262    }
263  }
264  return $restricted_cat;
265}
266
267// The function is_user_allowed returns :
268//      - 0 : if the category is allowed with this $restrictions array
269//      - 1 : if this category is not allowed
270//      - 2 : if an uppercat category is not allowed
271function is_user_allowed( $category_id, $restrictions )
272{
273  global $user,$prefixeTable;
274               
275  $lowest_category_id = $category_id;
276               
277  $is_root = false;
278  while ( !$is_root and !in_array( $category_id, $restrictions ) )
279  {
280    $query = "select id_uppercat";
281    $query.= " from $prefixeTable"."categories";
282    $query.= " where id = $category_id;";
283    $row = mysql_fetch_array( mysql_query( $query ) );
284    if ( $row['id_uppercat'] == "" )
285    {
286      $is_root = true;
287    }
288    $category_id = $row['id_uppercat'];
289  }
290               
291  if ( in_array( $lowest_category_id, $restrictions ) )
292  {
293    return 1;
294  }
295  if ( in_array( $category_id, $restrictions ) )
296  {
297    return 2;
298  }
299  // this user is allowed to go in this category
300  return 0;
301}
302?>
Note: See TracBrowser for help on using the repository browser.