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: @package_version@
  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($objForm{
  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->username = trim($objForm->username);
  60.             $objForm->password = trim($objForm->password);
  61.             $objForm->email = trim($objForm->email);
  62.  
  63.             //--------------------------------------------------
  64.             // Check values
  65.             //--------------------------------------------------
  66.             // Check username
  67.             if ($objForm->username == ''{
  68.                 $arrMessages[ERR_USERNAME_EMPTY;
  69.                 $arrValidated['username'= false;
  70.             else if ($objForm->username != 'peter'{
  71.                 $arrMessages[ERR_USERNAME_INVALID;
  72.                 $arrValidated['username'= false;
  73.             }
  74.  
  75.             // Check password
  76.             if ($objForm->password == ''{
  77.                 $arrMessages[ERR_PASSWORD_EMPTY;
  78.                 $arrValidated['password'= false;
  79.             else if ($objForm->password != 'gabriel'{
  80.                 $arrMessages[ERR_PASSWORD_INVALID;
  81.                 $arrValidated['password'= false;
  82.             }
  83.  
  84.             // Check email
  85.             if ($objForm->email == ''{
  86.                 $arrMessages[ERR_EMAIL_EMPTY;
  87.                 $arrValidated['email'= false;
  88.             else if ($this->_checkEmail($objForm->email== false{
  89.                 $arrMessages[ERR_EMAIL_INVALID;
  90.                 $arrValidated['email'= false;
  91.             }
  92.  
  93.             //--------------------------------------------------
  94.             // Finalize function
  95.             //--------------------------------------------------
  96.             // Create the message list
  97.             $strMessages '';
  98.             if (count($arrMessages> 0{
  99.                 $strMessages '<ul>';
  100.                 foreach ($arrMessages as $strTemp{
  101.                     $strMessages.= '<li>'.$strTemp.'</li>';
  102.                 }
  103.                 $strMessages.= '</ul>';
  104.             }
  105.  
  106.             // Create a response object
  107.             $objResponse = new HTML_AJAX_Action();
  108.  
  109.             // Assign the messages
  110.             $objResponse->assignAttr('messages''innerHTML'$strMessages);
  111.             $objResponse->insertScript("toggleElement('messages', ".(($strMessages != ''? 1 : 0).");");
  112.  
  113.             // Generate the scripts to update the form elements' label and input element
  114.             foreach ($arrValidated as $strKey => $blnValue{
  115.                 $objResponse->insertScript("setElement('".$strKey."', ".(($blnValue'1' '0').");");
  116.             }
  117.  
  118.             // Test for no messages
  119.             if ($strMessages == ""{
  120.                 $objResponse->insertAlert("Well done chap!");
  121.             }
  122.  
  123.             // And ready! :)
  124.             return $objResponse;
  125.         }
  126.     }
  127. ?>

Documentation generated on Mon, 11 Mar 2019 14:38:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.