Changeset 661


Ignore:
Timestamp:
Dec 28, 2004, 6:56:33 PM (19 years ago)
Author:
plg
Message:
  • register process partly rewritten
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r659 r661  
    8686}
    8787
     88/**
     89 * returns boolean string 'true' or 'false' if the given var is boolean
     90 *
     91 * @param mixed $var
     92 * @return mixed
     93 */
     94function boolean_to_string($var)
     95{
     96  if (is_bool($var))
     97  {
     98    if ($var)
     99    {
     100      return 'true';
     101    }
     102    else
     103    {
     104      return 'false';
     105    }
     106  }
     107  else
     108  {
     109    return $var;
     110  }
     111}
     112
    88113// array_remove removes a value from the given array if the value existed in
    89114// this array.
     
    513538  // $refresh, $url_link and $title are required for creating an automated
    514539  // refresh page in header.tpl
    515   $refresh = 1;
     540  $refresh = 2;
    516541  $url_link = $url;
    517542  $title = 'redirection';
  • trunk/include/functions_user.inc.php

    r653 r661  
    4646}
    4747
    48 function register_user( $login, $password, $password_conf,
    49                         $mail_address, $status = 'guest' )
    50 {
    51   global $lang;
    52 
    53   $error = array();
    54   $i = 0;
     48function register_user($login, $password, $password_conf,
     49                       $mail_address, $status = 'guest')
     50{
     51  global $lang, $conf;
     52
     53  $errors = array();
    5554  // login must not
    5655  //      1. be empty
     
    5857  //      3. include ' or " characters
    5958  //      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'];
     59  if ($login == '')
     60  {
     61    array_push($errors, $lang['reg_err_login1']);
     62  }
     63  if (ereg("^.* $", $login))
     64  {
     65    array_push($errors, $lang['reg_err_login2']);
     66  }
     67  if (ereg("^ .*$", $login))
     68  {
     69    array_push($errors, $lang['reg_err_login3']);
     70  }
     71
     72  if (ereg("'", $login) or ereg("\"", $login))
     73  {
     74    array_push($errors, $lang['reg_err_login4']);
     75  }
    6676  else
    6777  {
    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'];
     78    $query = '
     79SELECT id
     80  FROM '.USERS_TABLE.'
     81  WHERE username = \''.$login.'\'
     82;';
     83    $result = pwg_query($query);
     84    if (mysql_num_rows($result) > 0)
     85    {
     86      array_push($errors, $lang['reg_err_login5']);
     87    }
    7488  }
    7589  // 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;
     90  if ($password != $password_conf)
     91  {
     92    array_push($errors, $lang['reg_err_pass']);
     93  }
     94
     95  $error_mail_address = validate_mail_address($mail_address);
     96  if ($error_mail_address != '')
     97  {
     98    array_push($errors, $error_mail_address);
     99  }
    80100
    81101  // 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;
     102  if (count($errors) == 0)
     103  {
     104    $insert = array();
     105    $insert['username'] = $login;
     106    $insert['password'] = md5($password);
     107    $insert['status'] = $status;
     108    $insert['template'] = $conf['default_template'];
     109    $insert['nb_image_line'] = $conf['nb_image_line'];
     110    $insert['nb_line_page'] = $conf['nb_line_page'];
     111    $insert['language'] = $conf['default_language'];
     112    $insert['recent_period'] = $conf['recent_period'];
     113    $insert['expand'] = boolean_to_string($conf['auto_expand']);
     114    $insert['show_nb_comments'] = boolean_to_string($conf['show_nb_comments']);
     115    if ( $mail_address != '' )
     116    {
     117      $insert['mail_address'] = $mail_address;
     118    }
     119    if ($conf['default_maxwidth'] != '')
     120    {
     121      $insert['maxwidth'] = $conf['default_maxwidth'];
     122    }
     123    if ($conf['default_maxheight'] != '')
     124    {
     125      $insert['maxheight'] = $conf['default_maxheight'];
     126    }
     127
     128    $query = '
     129INSERT INTO '.USERS_TABLE.'
     130  ('.implode(',', array_keys($insert)).')
     131  VALUES
     132  (';
     133    $is_first = true;
     134    foreach (array_keys($insert) as $field)
     135    {
     136      if (!$is_first)
     137      {
     138        $query.= ',';
     139      }
     140      $query.= "'".$insert[$field]."'";
     141      $is_first = false;
     142    }
     143    $query.= ')
     144;';
     145    pwg_query($query);
     146  }
     147  return $errors;
    121148}
    122149
  • trunk/register.php

    r631 r661  
    3131//----------------------------------------------------------- user registration
    3232$errors = array();
    33 if ( isset( $_POST['submit'] ) )
     33if (isset($_POST['submit']))
    3434{
    35   $errors = register_user( $_POST['login'], $_POST['password'],
    36                           $_POST['password_conf'], $_POST['mail_address'] );
    37   if ( sizeof( $errors ) == 0 )
     35  $errors = register_user($_POST['login'], $_POST['password'],
     36                          $_POST['password_conf'], $_POST['mail_address']);
     37  if (count($errors) == 0)
    3838  {
     39    $query = '
     40SELECT id
     41  FROM '.USERS_TABLE.'
     42  WHERE username = \''.$_POST['login'].'\'
     43;';
     44    list($user_id) = mysql_fetch_array(pwg_query($query));
     45    $session_id = session_create($user_id, $conf['session_length']);
    3946    $url = 'category.php?id='.$session_id;
    40     redirect( $url );
     47    redirect($url);
    4148  }
    4249}
Note: See TracChangeset for help on using the changeset viewer.