cvs: pear /HTML_AJAX/examples index.php /HTML_AJAX/examples/login index.php /HTML_AJAX/examples/login/inc login.css login.js
/HTML_AJAX/examples/login/php auto_server.php login.class.php
| From: | Joshua Eichorn | Date: | Wed, 26 Oct 2005 16:45:31 +0000 |
| Subject: | cvs: pear /HTML_AJAX/examples index.php /HTML_AJAX/examples/login index.php /HTML_AJAX/examples/login/inc login.css login.js /HTML_AJAX/examples/login/php auto_server.php login.class.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-35875@lists.php.net to get a copy of this message | ||
jeichorn Wed Oct 26 12:45:31 2005 EDT
Added files:
/pear/HTML_AJAX/examples/login index.php
/pear/HTML_AJAX/examples/login/inc login.css login.js
/pear/HTML_AJAX/examples/login/php auto_server.php login.class.php
Modified files:
/pear/HTML_AJAX/examples index.php
Log:
Import new login example from Gilles van den Hoven
http://cvs.php.net/diff.php/pear/HTML_AJAX/examples/index.php?r1=1.1&r2=1.2&ty=u Index: pear/HTML_AJAX/examples/index.php diff -u pear/HTML_AJAX/examples/index.php:1.1 pear/HTML_AJAX/examples/index.php:1.2 --- pear/HTML_AJAX/examples/index.php:1.1 Mon Oct 24 23:27:41 2005 +++ pear/HTML_AJAX/examples/index.php Wed Oct 26 12:45:30 2005 @@ -26,6 +26,11 @@ <li><a href='helper_usage.php'>helper_usage.php</a> - An example showing the basics of the helper api</li> </ul> +<p>Real Life Examples</p> +<ul> +<li><a href='login/index.php'>login/index.php</a> - An example creating an AJAX driven login</a></li> +</ul> + <p> 2 server examples are provided </p> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/index.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/index.php +++ pear/HTML_AJAX/examples/login/index.php <?php /** * Example of using HTML_AJAX as a form validator * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ ?> <html> <head> <title>Form validation with HTML_AJAX</title> <script type="text/javascript" src="../server.php?client=Util,main,dispatcher,httpclient,request,json,loading"></script> <script type="text/javascript" src="./php/auto_server.php?stub=login"></script> <!-- STYLESHEET AND SCRIPT --> <link rel="stylesheet" type="text/css" href="./inc/login.css" /> <script type="text/javascript" src="./inc/login.js"></script> </head> <body> <p> <strong>Introduction:</strong><br> This example was built upon the example "queue_usage.php". I made this example to simulate a basic form validation with HTML_AJAX.<br> It became bigger than i thought, but nevertheless shows the power of HTML_AJAX i wanted it to show.<br> <br> <strong>Sample layout:</strong><br> - Of course there is an auto_server.php, which initializes the login class only when needed.<br> - Besides that there is a login.class.php, which consists of a "checkEmail" function, some error messages and a simple script.<br> - Then there is the client side code, located in "inc/login.js". The functions "setNormal" and "setError" are used by the callback function to colorize the form fields.<br> - The callback function receives an array counting 3 elements. The first being the result of the check, the second being an array of messages and the third being an array of id's which need to be colorized.<br> </p> <!-- THE ERROR MESSAGES --> <div id="messages" class="errorbox"></div> <!-- THE FORM --> <form method="post" action="javascript://" onSubmit="return false;"> <fieldset> <legend>And now, the big sample :)</legend> <table width="400" border="0" cellspacing="0" cellpadding="2"> <tr> <td><label for="username" id="lbl_username">Username:</label></td> <td><input type="text" name="username" id="txt_username" size="40" tabindex="1"></td> </tr> <tr> <td><label for="password" id="lbl_password">Password:</label></td> <td><input type="text" name="password" id="txt_password" size="40" tabindex="2"></td> </tr> <tr> <td><label for="email" id="lbl_email">E-mail:</label></td> <td><input type="text" name="email" id="txt_email" size="40" tabindex="3"></td> </tr> <tr> <td> </td> <td><input type="button" value=" login " onClick="processLogin()"> <input type="reset" value=" reset "></td> </tr> </table> </fieldset> </form> <script type="text/javascript"> // Reset the form colors setNormal('username'); setNormal('password'); setNormal('email'); // Set the focus on the first element document.getElementById('txt_username').focus(); </script> <p> This sample and all its code is <a href="http://www.opensource.org/licenses/lgpl-license.php">licensed under LGPL</a>.<br> <br> © 2005 www.webunity.nl<br> <br> <span style="font-size: 9px">(look at the source for my e-mail)</span> </p> </body> </html> <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ ?> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/inc/login.css?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/inc/login.css +++ pear/HTML_AJAX/examples/login/inc/login.css /* -------------------------------------------------------------------------------------------------- General styles ------------------------------------------------------------------------------------------------- */ body, table { color: #000000; font-size: 70%; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; } table, fieldset { font-size: 1em; } body, td, th { line-height: 140%; } td, th { vertical-align: top; } form, ul { margin: 0; padding: 0; } legend { color: #0046D5; font-weight: bold; padding-right: 8px; margin-bottom: 5px; } ol, ul { margin-left: 6px; padding-left: 15px; } /* -------------------------------------------------------------------------------------------------- Custom styles ------------------------------------------------------------------------------------------------- */ label.normal { color: #006fcc; font-weight: normal; } label.error { color: #ff0000; font-weight: bold; } input.normal, input.error { padding: 1px 3px 1px 3px; border: 1px solid #B3B3B3; color: #000000; background-color: #ffffff; } input.error { color: #000000; background-color: #ffcccc; } ..errorbox { border: 1px solid #ff0000; display: none; padding: 8px; margin-top: 1em; margin-bottom: 1em; } http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/inc/login.js?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/inc/login.js +++ pear/HTML_AJAX/examples/login/inc/login.js function setNormal(sID) { try { document.getElementById('lbl_' + sID).className = 'normal'; document.getElementById('txt_' + sID).className = 'normal'; } catch (e) { // blub } } function setError(sID) { try { document.getElementById('lbl_' + sID).className = 'error'; document.getElementById('txt_' + sID).className = 'error'; } catch (e) { // blub } } // callback hash, outputs the results of the login class callback = { checklogin: function(result) { var iMessage; var sMessages; var iID; var sID; // Reset the form colors setNormal('username'); setNormal('password'); setNormal('email'); // If the form validated, bail this function if (result[0] == true) { alert('the form was succesfully filled in!'); exit; } // Create a new list out of the error messages sMessages = '<strong>The following errors occured:</strong><br>'; sMessages+= '<ul>'; for (iMessage in result[1]) { if (iMessage != '______array') { sMessages+= '<li>' + result[1][iMessage] + '</li>'; } } sMessages+= '</ul>'; // Assign the message string document.getElementById('messages').innerHTML = sMessages; document.getElementById('messages').style.display = 'block'; // Loop the array of ID's which we need to colorize for (iID in result[2]) { setError(result[2][iID]); } } } // setup our remote object var remoteObject = new login(callback); // The function which will be called when the form is submitted. // !!! Be sure to create a version for browsers which do not support ajax. !!! function processLogin() { var sUsername = document.getElementById('txt_username').value; var sPassword = document.getElementById('txt_password').value; var sEmail = document.getElementById('txt_email').value; remoteObject.checklogin(sUsername, sPassword, sEmail); } http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/php/auto_server.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/php/auto_server.php +++ pear/HTML_AJAX/examples/login/php/auto_server.php <?php /** * Very simple form script with error handling. * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ // include the server class include 'HTML/AJAX/Server.php'; // extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on class TestServer extends HTML_AJAX_Server { // this flag must be set to on init methods var $initMethods = true; // init method for the test class, includes needed files an registers it for ajax function initLogin() { include 'login.class.php'; $this->registerClass(new login()); } } // create an instance of our test server $server = new TestServer(); // handle requests as needed $server->handleRequest(); ?> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/php/login.class.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/php/login.class.php +++ pear/HTML_AJAX/examples/login/php/login.class.php <?php /** * Email check function * * A typical use of the validating mode is to use: * [code] * if (checkEmail($email_address)) { * } else { * print 'Your email address is not valid'; * } * [/code] * * A typical use of the debugging mode is to use: * [code] * $return_msgs = checkEmail($email_address, true); * foreach($return_msgs[1] as $debug_msg) { print htmlspecialchars($debug_msg).'<br />'; } * print $return_msgs[0] ? 'Email address seems to be valid' : 'Email address does not seem to be valid'; * [/code] * * @param $email Email to be checked * @param $blnDebug Debug mode enabled * @return array(checked, valid, messages) */ function checkEmail($email, $blnDebug=false) { $msgs = Array(); $msgs[] = 'Received email address: '.$email; // // Check for email pattern (adapted and improved from http://karmak.org/archive/2003/02/validemail.html) //---------------------------------------------------------------------------------------------------- // incorrectly allows IP addresses with block numbers > 256, but those will fail to create sockets anyway // unicode norwegian chars cannot be used: C caron, D stroke, ENG, N acute, S caron, T stroke, Z caron (PHP unicode limitation) if (!preg_match("/^(([^<>()[\]\\\\.,;:\s@\"]+(\.[^<>()[\]\\\\.,;:\s@\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i", $email)) { $msgs[] = 'Email address was not recognised as a valid email pattern'; return $blnDebug ? Array(true, false, $msgs) : false; } $msgs[] = 'Email address was recognised as a valid email pattern'; // Get the mx host name if (preg_match("/@\[[\d.]*\]$/", $email)) { $mxHost[0] = preg_replace("/[\w\W]*@\[([\d.]+)\]$/", "$1", $email); $msgs[] = 'Email address contained IP address '.$mxHost[0].' - no need for MX lookup'; } else { // // Get all mx servers - if no MX records, assume domain is MX (SMTP RFC) $domain = preg_replace("/^[\w\W]*@([^@]*)$/i", "$1", $email); if (!@getmxrr($domain, $mxHost, $weightings)) { $mxHost[0] = $domain; $msgs[] = 'Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol'; } else { array_multisort($weightings, $mxHost); $cnt = ''; $co = 0; foreach ($mxHost as $ch) { $cnt.= ($cnt ? ', ' : '') . $ch . ' (' . $weightings[$co] . ')'; $co++; } $msgs[] = 'Obtained the following MX records for '.$domain.': '.$cnt; } } // // Check each server until you are given permission to connect, then check only that one server foreach ($mxHost as $currentHost) { $msgs[] = 'Checking MX server: '.$currentHost; if ($connection = @fsockopen($currentHost, 25)) { $msgs[] = 'Created socket ('.$connection.') to '.$currentHost; if (preg_match("/^2\d\d/", $cn = @fgets($connection, 1024))) { $msgs[] = $currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn; while (preg_match("/^2\d\d-/", $cn)) { $cn = @fgets($connection, 1024); $msgs[] = $currentHost.' sent extra connection header: '.$cn; } //throw away any extra rubbish // // Attempt to send an email from the user to themselves (not <> as some misconfigured servers reject it) $localHostIP = gethostbyname(preg_replace("/^.*@|:.*$/", '', getenv('HTTP_HOST'))); $localHostName = gethostbyaddr($localHostIP); @fputs($connection, 'HELO '.($localHostName ? $localHostName : ('['.$localHostIP.']'))."\r\n"); $hl = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $hl)) { $msgs[] = $currentHost.' sent HELO response: '.$hl; @fputs($connection, "MAIL FROM: <$email>\r\n"); $from = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $from)) { $msgs[] = $currentHost.' sent MAIL FROM response: '.$from; @fputs($connection, "RCPT TO: <$email>\r\n"); $to = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $to)) $msgs[] = $currentHost.' sent RCPT TO response: '.$to; else $msgs[] = $currentHost.' rejected recipient: '.$to; } else $msgs[] = $currentHost.' rejected MAIL FROM: '.$from; } else $msgs[] = $currentHost.' rejected HELO: '.$hl; @fputs($connection, "QUIT\r\n"); @fgets($connection, 1024); @fclose($connection); // // See if the transaction was permitted (i.e. does that email address exist) $blnReturn = ($success == '1') ? true : false; $msgs[] = $blnReturn ? ('Email address was accepted by '.$currentHost) : ('Email address was rejected by '.$currentHost); return $blnDebug ? Array(true, $blnReturn, $msgs) : $blnReturn; } elseif (preg_match("/^550/", $cn)) { $msgs[] = 'Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn; return $blnDebug ? Array(false, true, $msgs) : false; } else $msgs[] = $currentHost.' did not send SMTP connection header: '.$cn; } else $msgs[] = 'Failed to create socket to '.$currentHost; } $msgs[] = 'Could not establish SMTP session with any MX servers'; return $blnDebug ? Array(false, true, $msgs) : false; } // Error messages define(ERR_USERNAME_EMPTY, 'You forgot to enter a username, please try again'); define(ERR_USERNAME_INVALID, 'The username you entered is invalid. Please try "peter" (without quotes)'); define(ERR_PASSWORD_EMPTY, 'You forgot to enter a password, please try again'); define(ERR_PASSWORD_INVALID, 'The password you entered is invalid. Please try "gabriel" (without quotes)'); define(ERR_EMAIL_EMPTY, 'You forgot to enter an e-mail address'); define(ERR_EMAIL_INVALID, 'The e-mail address you entered is invalid. Please enter a valid e-mail address.'); /** * Login class used in the "login form" example * Please note: Constructors and private methods marked with _ are never exported in proxies to JavaScript * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ class login { function _checkEmail($strEmail) { } /** * Checks if the passed values are correct. * An array will be returned consisting of the following items: * 1) the result of this function * 2) the id's of the failing items * 3) the (error) messages * * @param string the username to be checked * @param string the password to be checked * @param string the email addres from the user */ function checklogin($strUsername, $strPassword, $strEmail) { // Initialize return values $arrResult = array(false, array(), array()); // Don't trust passed values :) $strUsername = trim($strUsername); $strPassword = trim($strPassword); $strEmail = trim($strEmail); // Check username if ($strUsername == '') { $arrResult[1][] = ERR_USERNAME_EMPTY; $arrResult[2][] = 'username'; } else if ($strUsername != 'peter') { $arrResult[1][] = ERR_USERNAME_INVALID; $arrResult[2][] = 'username'; } // Check password if ($strPassword == '') { $arrResult[1][] = ERR_PASSWORD_EMPTY; $arrResult[2][] = 'password'; } else if ($strPassword != 'gabriel') { $arrResult[1][] = ERR_PASSWORD_INVALID; $arrResult[2][] = 'password'; } // Check email if ($strEmail == '') { $arrResult[1][] = ERR_EMAIL_EMPTY; $arrResult[2][] = 'email'; } else if (checkEmail($strEmail) == false) { $arrResult[1][] = ERR_EMAIL_INVALID; $arrResult[2][] = 'email'; } // Set return values // (to avoid we are adding thesame "error message" or "id" twice, we use the array_unique() function.) $arrResult[0] = (count($arrResult[1]) == 0); $arrResult[1] = array_unique($arrResult[1]); $arrResult[2] = array_unique($arrResult[2]); return $arrResult; } } ?>
http://cvs.php.net/diff.php/pear/HTML_AJAX/examples/index.php?r1=1.1&r2=1.2&ty=u Index: pear/HTML_AJAX/examples/index.php diff -u pear/HTML_AJAX/examples/index.php:1.1 pear/HTML_AJAX/examples/index.php:1.2 --- pear/HTML_AJAX/examples/index.php:1.1 Mon Oct 24 23:27:41 2005 +++ pear/HTML_AJAX/examples/index.php Wed Oct 26 12:45:30 2005 @@ -26,6 +26,11 @@ <li><a href='helper_usage.php'>helper_usage.php</a> - An example showing the basics of the helper api</li> </ul> +<p>Real Life Examples</p> +<ul> +<li><a href='login/index.php'>login/index.php</a> - An example creating an AJAX driven login</a></li> +</ul> + <p> 2 server examples are provided </p> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/index.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/index.php +++ pear/HTML_AJAX/examples/login/index.php <?php /** * Example of using HTML_AJAX as a form validator * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ ?> <html> <head> <title>Form validation with HTML_AJAX</title> <script type="text/javascript" src="../server.php?client=Util,main,dispatcher,httpclient,request,json,loading"></script> <script type="text/javascript" src="./php/auto_server.php?stub=login"></script> <!-- STYLESHEET AND SCRIPT --> <link rel="stylesheet" type="text/css" href="./inc/login.css" /> <script type="text/javascript" src="./inc/login.js"></script> </head> <body> <p> <strong>Introduction:</strong><br> This example was built upon the example "queue_usage.php". I made this example to simulate a basic form validation with HTML_AJAX.<br> It became bigger than i thought, but nevertheless shows the power of HTML_AJAX i wanted it to show.<br> <br> <strong>Sample layout:</strong><br> - Of course there is an auto_server.php, which initializes the login class only when needed.<br> - Besides that there is a login.class.php, which consists of a "checkEmail" function, some error messages and a simple script.<br> - Then there is the client side code, located in "inc/login.js". The functions "setNormal" and "setError" are used by the callback function to colorize the form fields.<br> - The callback function receives an array counting 3 elements. The first being the result of the check, the second being an array of messages and the third being an array of id's which need to be colorized.<br> </p> <!-- THE ERROR MESSAGES --> <div id="messages" class="errorbox"></div> <!-- THE FORM --> <form method="post" action="javascript://" onSubmit="return false;"> <fieldset> <legend>And now, the big sample :)</legend> <table width="400" border="0" cellspacing="0" cellpadding="2"> <tr> <td><label for="username" id="lbl_username">Username:</label></td> <td><input type="text" name="username" id="txt_username" size="40" tabindex="1"></td> </tr> <tr> <td><label for="password" id="lbl_password">Password:</label></td> <td><input type="text" name="password" id="txt_password" size="40" tabindex="2"></td> </tr> <tr> <td><label for="email" id="lbl_email">E-mail:</label></td> <td><input type="text" name="email" id="txt_email" size="40" tabindex="3"></td> </tr> <tr> <td> </td> <td><input type="button" value=" login " onClick="processLogin()"> <input type="reset" value=" reset "></td> </tr> </table> </fieldset> </form> <script type="text/javascript"> // Reset the form colors setNormal('username'); setNormal('password'); setNormal('email'); // Set the focus on the first element document.getElementById('txt_username').focus(); </script> <p> This sample and all its code is <a href="http://www.opensource.org/licenses/lgpl-license.php">licensed under LGPL</a>.<br> <br> © 2005 www.webunity.nl<br> <br> <span style="font-size: 9px">(look at the source for my e-mail)</span> </p> </body> </html> <?php /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ ?> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/inc/login.css?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/inc/login.css +++ pear/HTML_AJAX/examples/login/inc/login.css /* -------------------------------------------------------------------------------------------------- General styles ------------------------------------------------------------------------------------------------- */ body, table { color: #000000; font-size: 70%; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; } table, fieldset { font-size: 1em; } body, td, th { line-height: 140%; } td, th { vertical-align: top; } form, ul { margin: 0; padding: 0; } legend { color: #0046D5; font-weight: bold; padding-right: 8px; margin-bottom: 5px; } ol, ul { margin-left: 6px; padding-left: 15px; } /* -------------------------------------------------------------------------------------------------- Custom styles ------------------------------------------------------------------------------------------------- */ label.normal { color: #006fcc; font-weight: normal; } label.error { color: #ff0000; font-weight: bold; } input.normal, input.error { padding: 1px 3px 1px 3px; border: 1px solid #B3B3B3; color: #000000; background-color: #ffffff; } input.error { color: #000000; background-color: #ffcccc; } ..errorbox { border: 1px solid #ff0000; display: none; padding: 8px; margin-top: 1em; margin-bottom: 1em; } http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/inc/login.js?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/inc/login.js +++ pear/HTML_AJAX/examples/login/inc/login.js function setNormal(sID) { try { document.getElementById('lbl_' + sID).className = 'normal'; document.getElementById('txt_' + sID).className = 'normal'; } catch (e) { // blub } } function setError(sID) { try { document.getElementById('lbl_' + sID).className = 'error'; document.getElementById('txt_' + sID).className = 'error'; } catch (e) { // blub } } // callback hash, outputs the results of the login class callback = { checklogin: function(result) { var iMessage; var sMessages; var iID; var sID; // Reset the form colors setNormal('username'); setNormal('password'); setNormal('email'); // If the form validated, bail this function if (result[0] == true) { alert('the form was succesfully filled in!'); exit; } // Create a new list out of the error messages sMessages = '<strong>The following errors occured:</strong><br>'; sMessages+= '<ul>'; for (iMessage in result[1]) { if (iMessage != '______array') { sMessages+= '<li>' + result[1][iMessage] + '</li>'; } } sMessages+= '</ul>'; // Assign the message string document.getElementById('messages').innerHTML = sMessages; document.getElementById('messages').style.display = 'block'; // Loop the array of ID's which we need to colorize for (iID in result[2]) { setError(result[2][iID]); } } } // setup our remote object var remoteObject = new login(callback); // The function which will be called when the form is submitted. // !!! Be sure to create a version for browsers which do not support ajax. !!! function processLogin() { var sUsername = document.getElementById('txt_username').value; var sPassword = document.getElementById('txt_password').value; var sEmail = document.getElementById('txt_email').value; remoteObject.checklogin(sUsername, sPassword, sEmail); } http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/php/auto_server.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/php/auto_server.php +++ pear/HTML_AJAX/examples/login/php/auto_server.php <?php /** * Very simple form script with error handling. * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ // include the server class include 'HTML/AJAX/Server.php'; // extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on class TestServer extends HTML_AJAX_Server { // this flag must be set to on init methods var $initMethods = true; // init method for the test class, includes needed files an registers it for ajax function initLogin() { include 'login.class.php'; $this->registerClass(new login()); } } // create an instance of our test server $server = new TestServer(); // handle requests as needed $server->handleRequest(); ?> http://cvs.php.net/co.php/pear/HTML_AJAX/examples/login/php/login.class.php?r=1.1&p=1 Index: pear/HTML_AJAX/examples/login/php/login.class.php +++ pear/HTML_AJAX/examples/login/php/login.class.php <?php /** * Email check function * * A typical use of the validating mode is to use: * [code] * if (checkEmail($email_address)) { * } else { * print 'Your email address is not valid'; * } * [/code] * * A typical use of the debugging mode is to use: * [code] * $return_msgs = checkEmail($email_address, true); * foreach($return_msgs[1] as $debug_msg) { print htmlspecialchars($debug_msg).'<br />'; } * print $return_msgs[0] ? 'Email address seems to be valid' : 'Email address does not seem to be valid'; * [/code] * * @param $email Email to be checked * @param $blnDebug Debug mode enabled * @return array(checked, valid, messages) */ function checkEmail($email, $blnDebug=false) { $msgs = Array(); $msgs[] = 'Received email address: '.$email; // // Check for email pattern (adapted and improved from http://karmak.org/archive/2003/02/validemail.html) //---------------------------------------------------------------------------------------------------- // incorrectly allows IP addresses with block numbers > 256, but those will fail to create sockets anyway // unicode norwegian chars cannot be used: C caron, D stroke, ENG, N acute, S caron, T stroke, Z caron (PHP unicode limitation) if (!preg_match("/^(([^<>()[\]\\\\.,;:\s@\"]+(\.[^<>()[\]\\\\.,;:\s@\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i", $email)) { $msgs[] = 'Email address was not recognised as a valid email pattern'; return $blnDebug ? Array(true, false, $msgs) : false; } $msgs[] = 'Email address was recognised as a valid email pattern'; // Get the mx host name if (preg_match("/@\[[\d.]*\]$/", $email)) { $mxHost[0] = preg_replace("/[\w\W]*@\[([\d.]+)\]$/", "$1", $email); $msgs[] = 'Email address contained IP address '.$mxHost[0].' - no need for MX lookup'; } else { // // Get all mx servers - if no MX records, assume domain is MX (SMTP RFC) $domain = preg_replace("/^[\w\W]*@([^@]*)$/i", "$1", $email); if (!@getmxrr($domain, $mxHost, $weightings)) { $mxHost[0] = $domain; $msgs[] = 'Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol'; } else { array_multisort($weightings, $mxHost); $cnt = ''; $co = 0; foreach ($mxHost as $ch) { $cnt.= ($cnt ? ', ' : '') . $ch . ' (' . $weightings[$co] . ')'; $co++; } $msgs[] = 'Obtained the following MX records for '.$domain.': '.$cnt; } } // // Check each server until you are given permission to connect, then check only that one server foreach ($mxHost as $currentHost) { $msgs[] = 'Checking MX server: '.$currentHost; if ($connection = @fsockopen($currentHost, 25)) { $msgs[] = 'Created socket ('.$connection.') to '.$currentHost; if (preg_match("/^2\d\d/", $cn = @fgets($connection, 1024))) { $msgs[] = $currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn; while (preg_match("/^2\d\d-/", $cn)) { $cn = @fgets($connection, 1024); $msgs[] = $currentHost.' sent extra connection header: '.$cn; } //throw away any extra rubbish // // Attempt to send an email from the user to themselves (not <> as some misconfigured servers reject it) $localHostIP = gethostbyname(preg_replace("/^.*@|:.*$/", '', getenv('HTTP_HOST'))); $localHostName = gethostbyaddr($localHostIP); @fputs($connection, 'HELO '.($localHostName ? $localHostName : ('['.$localHostIP.']'))."\r\n"); $hl = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $hl)) { $msgs[] = $currentHost.' sent HELO response: '.$hl; @fputs($connection, "MAIL FROM: <$email>\r\n"); $from = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $from)) { $msgs[] = $currentHost.' sent MAIL FROM response: '.$from; @fputs($connection, "RCPT TO: <$email>\r\n"); $to = @fgets($connection, 1024); if ($success = preg_match("/^2\d\d/", $to)) $msgs[] = $currentHost.' sent RCPT TO response: '.$to; else $msgs[] = $currentHost.' rejected recipient: '.$to; } else $msgs[] = $currentHost.' rejected MAIL FROM: '.$from; } else $msgs[] = $currentHost.' rejected HELO: '.$hl; @fputs($connection, "QUIT\r\n"); @fgets($connection, 1024); @fclose($connection); // // See if the transaction was permitted (i.e. does that email address exist) $blnReturn = ($success == '1') ? true : false; $msgs[] = $blnReturn ? ('Email address was accepted by '.$currentHost) : ('Email address was rejected by '.$currentHost); return $blnDebug ? Array(true, $blnReturn, $msgs) : $blnReturn; } elseif (preg_match("/^550/", $cn)) { $msgs[] = 'Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn; return $blnDebug ? Array(false, true, $msgs) : false; } else $msgs[] = $currentHost.' did not send SMTP connection header: '.$cn; } else $msgs[] = 'Failed to create socket to '.$currentHost; } $msgs[] = 'Could not establish SMTP session with any MX servers'; return $blnDebug ? Array(false, true, $msgs) : false; } // Error messages define(ERR_USERNAME_EMPTY, 'You forgot to enter a username, please try again'); define(ERR_USERNAME_INVALID, 'The username you entered is invalid. Please try "peter" (without quotes)'); define(ERR_PASSWORD_EMPTY, 'You forgot to enter a password, please try again'); define(ERR_PASSWORD_INVALID, 'The password you entered is invalid. Please try "gabriel" (without quotes)'); define(ERR_EMAIL_EMPTY, 'You forgot to enter an e-mail address'); define(ERR_EMAIL_INVALID, 'The e-mail address you entered is invalid. Please enter a valid e-mail address.'); /** * Login class used in the "login form" example * Please note: Constructors and private methods marked with _ are never exported in proxies to JavaScript * * @category HTML * @package AJAX * @author Gilles van den Hoven <gilles@webunity.nl> * @copyright 2005 Gilles van den Hoven * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version Release: @package_version@ * @link http://pear.php.net/package/HTML_AJAX */ class login { function _checkEmail($strEmail) { } /** * Checks if the passed values are correct. * An array will be returned consisting of the following items: * 1) the result of this function * 2) the id's of the failing items * 3) the (error) messages * * @param string the username to be checked * @param string the password to be checked * @param string the email addres from the user */ function checklogin($strUsername, $strPassword, $strEmail) { // Initialize return values $arrResult = array(false, array(), array()); // Don't trust passed values :) $strUsername = trim($strUsername); $strPassword = trim($strPassword); $strEmail = trim($strEmail); // Check username if ($strUsername == '') { $arrResult[1][] = ERR_USERNAME_EMPTY; $arrResult[2][] = 'username'; } else if ($strUsername != 'peter') { $arrResult[1][] = ERR_USERNAME_INVALID; $arrResult[2][] = 'username'; } // Check password if ($strPassword == '') { $arrResult[1][] = ERR_PASSWORD_EMPTY; $arrResult[2][] = 'password'; } else if ($strPassword != 'gabriel') { $arrResult[1][] = ERR_PASSWORD_INVALID; $arrResult[2][] = 'password'; } // Check email if ($strEmail == '') { $arrResult[1][] = ERR_EMAIL_EMPTY; $arrResult[2][] = 'email'; } else if (checkEmail($strEmail) == false) { $arrResult[1][] = ERR_EMAIL_INVALID; $arrResult[2][] = 'email'; } // Set return values // (to avoid we are adding thesame "error message" or "id" twice, we use the array_unique() function.) $arrResult[0] = (count($arrResult[1]) == 0); $arrResult[1] = array_unique($arrResult[1]); $arrResult[2] = array_unique($arrResult[2]); return $arrResult; } } ?>