AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file class.login.php

Documentation is available at class.login.php

  1. <?php
  2.     // Includes
  3.     require_once 'HTML/AJAX/Action.php';
  4.  
  5.     // Error messages
  6.     define('ERR_USERNAME_EMPTY''You forgot to enter a username, please try again');
  7.     define('ERR_USERNAME_INVALID''The username you entered is invalid. Please try "peter" (without quotes)');
  8.     define('ERR_PASSWORD_EMPTY''You forgot to enter a password, please try again');
  9.     define('ERR_PASSWORD_INVALID''The password you entered is invalid. Please try "gabriel" (without quotes)');
  10.     define('ERR_EMAIL_EMPTY''You forgot to enter an e-mail address');
  11.     define('ERR_EMAIL_INVALID''The e-mail address you entered is invalid. Please enter a valid e-mail address.');
  12.  
  13.     /**
  14.      * Login class used in the "login form" example
  15.      * Please note: Constructors and private methods marked with _ are never exported in proxies to JavaScript
  16.      *
  17.      * @category   HTML
  18.      * @package    AJAX
  19.      * @author     Gilles van den Hoven <gilles@webunity.nl>
  20.      * @copyright  2005 Gilles van den Hoven
  21.      * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  22.      * @version    Release: 0.5.7
  23.      * @link       http://pear.php.net/package/HTML_AJAX
  24.      */
  25.     class login {
  26.         /**
  27.          * PHP5 requires a constructor
  28.          */
  29.         function login({
  30.         }
  31.  
  32.         /**
  33.         * Checks the proper syntax
  34.         */    
  35.         function _checkEmail($strEmail{
  36.             return (preg_match'/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' $strEmail));
  37.         }
  38.  
  39.         /**
  40.          * Checks if the passed values are correct.
  41.          *
  42.          * @param array the form object
  43.          */
  44.         function validate($arrayForm{
  45.             //--------------------------------------------------
  46.             // Initialize function
  47.             //--------------------------------------------------
  48.             // Array to hold the messages
  49.             $arrMessages = array();
  50.  
  51.             // Set all form values that they validated. Could be improved by analyzing
  52.             // the values passed in $objForm and setting values accordingly.
  53.             $arrValidated = array();
  54.             $arrValidated['username'= true;
  55.             $arrValidated['password'= true;
  56.             $arrValidated['email'= true;
  57.  
  58.             // Never trust the values passed by users :)
  59.             $objForm = new stdClass();
  60.             $objForm->username = trim($arrayForm['username']);
  61.             $objForm->password = trim($arrayForm['password']);
  62.             $objForm->email = trim($arrayForm['email']);
  63.  
  64.             //--------------------------------------------------
  65.             // Check values
  66.             //--------------------------------------------------
  67.             // Check username
  68.             if ($objForm->username == ''{
  69.                 $arrMessages[ERR_USERNAME_EMPTY;
  70.                 $arrValidated['username'= false;
  71.             else if ($objForm->username != 'peter'{
  72.                 $arrMessages[ERR_USERNAME_INVALID;
  73.                 $arrValidated['username'= false;
  74.             }
  75.  
  76.             // Check password
  77.             if ($objForm->password == ''{
  78.                 $arrMessages[ERR_PASSWORD_EMPTY;
  79.                 $arrValidated['password'= false;
  80.             else if ($objForm->password != 'gabriel'{
  81.                 $arrMessages[ERR_PASSWORD_INVALID;
  82.                 $arrValidated['password'= false;
  83.             }
  84.  
  85.             // Check email
  86.             if ($objForm->email == ''{
  87.                 $arrMessages[ERR_EMAIL_EMPTY;
  88.                 $arrValidated['email'= false;
  89.             else if ($this->_checkEmail($objForm->email== false{
  90.                 $arrMessages[ERR_EMAIL_INVALID;
  91.                 $arrValidated['email'= false;
  92.             }
  93.  
  94.             //--------------------------------------------------
  95.             // Finalize function
  96.             //--------------------------------------------------
  97.             // Create the message list
  98.             $strMessages '';
  99.             if (count($arrMessages> 0{
  100.                 $strMessages '<ul>';
  101.                 foreach ($arrMessages as $strTemp{
  102.                     $strMessages.= '<li>'.$strTemp.'</li>';
  103.                 }
  104.                 $strMessages.= '</ul>';
  105.             }
  106.  
  107.             // Create a response object
  108.             $objResponse = new HTML_AJAX_Action();
  109.  
  110.             // Assign the messages
  111.             $objResponse->assignAttr('messages''innerHTML'$strMessages);
  112.             $objResponse->insertScript("toggleElement('messages', ".(($strMessages != ''? 1 : 0).");");
  113.  
  114.             // Generate the scripts to update the form elements' label and input element
  115.             foreach ($arrValidated as $strKey => $blnValue{
  116.                 $objResponse->insertScript("setElement('".$strKey."', ".(($blnValue'1' '0').");");
  117.             }
  118.  
  119.             // Test for no messages
  120.             if ($strMessages == ""{
  121.                 $objResponse->insertAlert("Well done chap!");
  122.             }
  123.  
  124.             // And ready! :)
  125.             return $objResponse;
  126.         }
  127.     }
  128. ?>

Documentation generated on Mon, 11 Mar 2019 15:59:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.