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

Source for file login.class.php

Documentation is available at login.class.php

  1. <?php
  2.     /**
  3.     * Email check function
  4.     *
  5.     * A typical use of the validating mode is to use:
  6.     * [code]
  7.     * if (checkEmail($email_address)) {
  8.     * } else {
  9.     *   print 'Your email address is not valid';
  10.     * }
  11.     * [/code]
  12.     *
  13.     * A typical use of the debugging mode is to use:
  14.     * [code]
  15.     * $return_msgs = checkEmail($email_address, true);
  16.     * foreach($return_msgs[1] as $debug_msg) { print htmlspecialchars($debug_msg).'<br />'; }
  17.     * print $return_msgs[0] ? 'Email address seems to be valid' : 'Email address does not seem to be valid';
  18.     * [/code]
  19.     *
  20.     * @param $email Email to be checked
  21.     * @param $blnDebug Debug mode enabled
  22.     * @return array(checked, valid, messages)
  23.     */
  24.     function checkEmail($email$blnDebug=false)
  25.     {
  26.         $msgs = Array();
  27.         $msgs['Received email address: '.$email;
  28.  
  29.         //
  30.         // Check for email pattern (adapted and improved from http://karmak.org/archive/2003/02/validemail.html)
  31.         //----------------------------------------------------------------------------------------------------
  32.         // incorrectly allows IP addresses with block numbers > 256, but those will fail to create sockets anyway
  33.         // unicode norwegian chars cannot be used: C caron, D stroke, ENG, N acute, S caron, T stroke, Z caron (PHP unicode limitation)
  34.         if (!preg_match($email))
  35.         {
  36.             $msgs['Email address was not recognised as a valid email pattern';
  37.             return $blnDebug ? Array(truefalse$msgs: false;
  38.         }
  39.         $msgs['Email address was recognised as a valid email pattern';
  40.  
  41.         // Get the mx host name
  42.         if (preg_match("/@\[[\d.]*\]$/"$email))
  43.         {
  44.             $mxHost[0preg_replace("/[\w\W]*@\[([\d.]+)\]$/""$1"$email);
  45.             $msgs['Email address contained IP address '.$mxHost[0].' - no need for MX lookup';
  46.         }
  47.         else
  48.         {
  49.             //
  50.             // Get all mx servers - if no MX records, assume domain is MX (SMTP RFC)
  51.             $domain preg_replace("/^[\w\W]*@([^@]*)$/i""$1"$email);
  52.             if (!@getmxrr($domain$mxHost$weightings))
  53.             {
  54.                 $mxHost[0$domain;
  55.                 $msgs['Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol';
  56.             }
  57.             else
  58.             {
  59.                 array_multisort($weightings$mxHost);
  60.                 $cnt '';
  61.                 $co = 0;
  62.                 foreach ($mxHost as $ch)
  63.                 {
  64.                     $cnt.= ($cnt ', ' ''$ch ' (' $weightings[$co')';
  65.                     $co++;
  66.                 }
  67.                 $msgs['Obtained the following MX records for '.$domain.': '.$cnt;
  68.             }
  69.         }
  70.  
  71.         //
  72.         // Check each server until you are given permission to connect, then check only that one server
  73.         foreach ($mxHost as $currentHost)
  74.         {
  75.             $msgs['Checking MX server: '.$currentHost;
  76.             if ($connection @fsockopen($currentHost25))
  77.             {
  78.                 $msgs['Created socket ('.$connection.') to '.$currentHost;
  79.  
  80.                 if (preg_match("/^2\d\d/"$cn @fgets($connection1024)))
  81.                 {
  82.                     $msgs[$currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn;
  83.                     while (preg_match("/^2\d\d-/"$cn))
  84.                     {
  85.                         $cn @fgets($connection1024);
  86.                         $msgs[$currentHost.' sent extra connection header: '.$cn;
  87.                     //throw away any extra rubbish
  88.  
  89.                     //
  90.                     // Attempt to send an email from the user to themselves (not <> as some misconfigured servers reject it)
  91.                     $localHostIP gethostbyname(preg_replace("/^.*@|:.*$/"''getenv('HTTP_HOST')));
  92.                     $localHostName gethostbyaddr($localHostIP);
  93.                     @fputs($connection'HELO '.($localHostName $localHostName ('['.$localHostIP.']'))."\r\n");
  94.                     $hl @fgets($connection1024);
  95.                     if ($success preg_match("/^2\d\d/"$hl))
  96.                     {
  97.                         $msgs[$currentHost.' sent HELO response: '.$hl;
  98.                         @fputs($connection"MAIL FROM: <$email>\r\n");
  99.                         $from @fgets($connection1024);
  100.                         if ($success preg_match("/^2\d\d/"$from))
  101.                         {
  102.                             $msgs[$currentHost.' sent MAIL FROM response: '.$from;
  103.                             @fputs($connection"RCPT TO: <$email>\r\n");
  104.                             $to @fgets($connection1024);
  105.                             if ($success preg_match("/^2\d\d/"$to))
  106.                                 $msgs[$currentHost.' sent RCPT TO response: '.$to;
  107.                             else
  108.                                 $msgs[$currentHost.' rejected recipient: '.$to;
  109.                         }
  110.                         else
  111.                             $msgs[$currentHost.' rejected MAIL FROM: '.$from;
  112.                     }
  113.                     else
  114.                         $msgs[$currentHost.' rejected HELO: '.$hl;
  115.  
  116.                     @fputs($connection"QUIT\r\n");
  117.                     @fgets($connection1024);
  118.                     @fclose($connection);
  119.  
  120.                     //
  121.                     // See if the transaction was permitted (i.e. does that email address exist)
  122.                     $blnReturn ($success == '1'? true : false;
  123.                     $msgs[=  $blnReturn ('Email address was accepted by '.$currentHost('Email address was rejected by '.$currentHost);
  124.  
  125.                     return $blnDebug ? Array(true$blnReturn$msgs$blnReturn;
  126.                 }
  127.                 elseif (preg_match("/^550/"$cn))
  128.                 {
  129.                     $msgs['Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn;
  130.                     return $blnDebug ? Array(falsetrue$msgs: false;
  131.                 }
  132.                 else
  133.                     $msgs[$currentHost.' did not send SMTP connection header: '.$cn;
  134.             }
  135.             else
  136.                 $msgs['Failed to create socket to '.$currentHost;
  137.         }
  138.  
  139.         $msgs['Could not establish SMTP session with any MX servers';
  140.         return $blnDebug ? Array(falsetrue$msgs: false;
  141.     }
  142.  
  143.  
  144.  
  145.     // Error messages
  146.     define('ERR_USERNAME_EMPTY''You forgot to enter a username, please try again');
  147.     define('ERR_USERNAME_INVALID''The username you entered is invalid. Please try "peter" (without quotes)');
  148.     define('ERR_PASSWORD_EMPTY''You forgot to enter a password, please try again');
  149.     define('ERR_PASSWORD_INVALID''The password you entered is invalid. Please try "gabriel" (without quotes)');
  150.     define('ERR_EMAIL_EMPTY''You forgot to enter an e-mail address');
  151.     define('ERR_EMAIL_INVALID''The e-mail address you entered is invalid. Please enter a valid e-mail address.');
  152.  
  153.  
  154.  
  155.     /**
  156.      * Login class used in the "login form" example
  157.      * Please note: Constructors and private methods marked with _ are never exported in proxies to JavaScript
  158.      * 
  159.      * @category   HTML
  160.      * @package    AJAX
  161.      * @author     Gilles van den Hoven <gilles@webunity.nl>
  162.      * @copyright  2005 Gilles van den Hoven
  163.      * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  164.      * @version    Release: @package_version@
  165.      * @link       http://pear.php.net/package/HTML_AJAX
  166.      */
  167.     class login {
  168.         function _checkEmail($strEmail{
  169.         }
  170.  
  171.         /**
  172.          * Checks if the passed values are correct.
  173.          * An array will be returned consisting of the following items:
  174.          * 1) the result of this function
  175.          * 2) the id's of the failing items
  176.          * 3) the (error) messages
  177.          *
  178.          * @param string the username to be checked
  179.          * @param string the password to be checked
  180.          * @param string the email addres from the user
  181.          */
  182.         function checklogin($strUsername$strPassword$strEmail{
  183.             // Initialize return values
  184.             $arrResult = array(falsearray()array());
  185.  
  186.             // Don't trust passed values :)
  187.             $strUsername trim($strUsername);
  188.             $strPassword trim($strPassword);
  189.             $strEmail trim($strEmail);
  190.             
  191.             // Check username
  192.             if ($strUsername == ''{
  193.                 $arrResult[1][ERR_USERNAME_EMPTY;
  194.                 $arrResult[2]['username';
  195.             else if ($strUsername != 'peter'{
  196.                 $arrResult[1][ERR_USERNAME_INVALID;
  197.                 $arrResult[2]['username';
  198.             }
  199.             
  200.             // Check password
  201.             if ($strPassword == ''{
  202.                 $arrResult[1][ERR_PASSWORD_EMPTY;
  203.                 $arrResult[2]['password';
  204.             else if ($strPassword != 'gabriel'{
  205.                 $arrResult[1][ERR_PASSWORD_INVALID;
  206.                 $arrResult[2]['password';
  207.             }
  208.             
  209.             // Check email
  210.             if ($strEmail == ''{
  211.                 $arrResult[1][ERR_EMAIL_EMPTY;
  212.                 $arrResult[2]['email';
  213.             else if (checkEmail($strEmail== false{
  214.                 $arrResult[1][ERR_EMAIL_INVALID;
  215.                 $arrResult[2]['email';
  216.             }
  217.             
  218.             // Set return values
  219.             // (to avoid we are adding thesame "error message" or "id" twice, we use the array_unique() function.)
  220.             $arrResult[0(count($arrResult[1]== 0);
  221.             $arrResult[1array_unique($arrResult[1]);
  222.             $arrResult[2array_unique($arrResult[2]);
  223.             return $arrResult;
  224.         }
  225.     }
  226. ?>

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