home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / validateemail.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  8.5 KB  |  288 lines

  1. <?php
  2.  
  3. /*
  4.  
  5.  
  6. validateEmail() by Shane Y. Gibson 
  7.  
  8. This is version validateEmail() augments the work that Jon Stevens originally wrote by (almost) fully documenting the code, and expands the $return array to include more (useful) information. An example chunk of code is also provided 
  9.  
  10.  
  11.  Originally 
  12.  By: Jon S. Stevens jon@clearink.com
  13.  Copyright 1998 Jon S. Stevens, Clear Ink
  14.  This code has all the normal disclaimers.
  15.  It is free for any use, just keep the credits intact.
  16.  
  17.  Enacements and modifications:
  18.  
  19.            By:  Shane Y. Gibson  shane@tuna.org
  20.  Organization:  The Unix Network Archives (http://www.tuna.org./)
  21.          Date:  November 16th, 1998
  22.       Changes:  Added **all** comments, as original code lacked them.
  23.                 Added some return codes to include a bit more description
  24.                 for useability.
  25.  
  26.  I disclaim nothing...nor do I claim anything...but
  27.  it would be nice if you included this disclaimer...
  28.  
  29. */
  30.  
  31. /*  This function takes in an email address (say 'shane@tuna.org')
  32.  *  and tests to see if it's a valid email address.
  33.  *
  34.  *  An array with the results is passed back to the caller.
  35.  *
  36.  *  Possible result codes for the array items are:
  37.  *
  38.  *  Item 0:  [true|false]        true for valid email address
  39.  *                    false for NON-valid email address
  40.  *
  41.  *  Item 1:  [SMTP Code]        if a valid MX mail server found, then
  42.  *                    fill this array in with failed SMTP
  43.  *                    reply codes
  44.  *
  45.  *  Item 2:  [true|false]        true for valid mail server found for
  46.  *                    host/domain
  47.  *                    false if no valid mail server found
  48.  *
  49.  *  Item 3:  [MX server]        if a valid MX host was found and
  50.  *                    connected to then fill in this item 
  51.  *                    with the MX server hostname
  52.  *
  53.  *  EXAMPLE code for use is at the very end of this function.
  54.  */
  55.  
  56. function validateEmail ( $email )
  57. {
  58.     // used for SMTP HELO argument
  59.     global $SERVER_NAME;  
  60.  
  61.     // initialize our return array, populating with default values
  62.     $return = array ( false, "", "", "" );
  63.  
  64.     // assign our user part and domain parts respectively to seperate             // variables
  65.     list ( $user, $domain )  = split ( "@", $email, 2 );
  66.     
  67.     // split up the domain name into sub-parts
  68.     $arr = explode ( ".", $domain );
  69.  
  70.     // figure out how many parts to the host/domain name portion there are
  71.     $count = count ( $arr );
  72.  
  73.     // get our Top-Level Domain portion (i.e. foobar.org)
  74.     $tld = $arr[$count - 2] . "." . $arr[$count - 1];
  75.  
  76.     // check that an MX record exists for Top-Level Domain, and if so
  77.     // start our email address checking
  78.     if ( checkdnsrr ( $tld, "MX" ) )
  79.     {
  80.         // Okay...valid dns reverse record; test that MX record for 
  81.         // host exists, and then fill the 'mxhosts' and 'weight'
  82.         // arrays with the correct information
  83.         //
  84.         if ( getmxrr ( $tld, $mxhosts, $weight ) )
  85.         {
  86.             // sift through the 'mxhosts' connecting to each host
  87.             for ( $i = 0; $i < count ( $mxhosts ); $i++ )
  88.             {
  89.                 // open socket on port 25 to mxhosts, setting                         // returned file pointer to the variable 'fp'
  90.                 $fp = fsockopen ( $mxhosts[$i], 25 );
  91.  
  92.                 // if the 'fp' was set, then goto work
  93.                 if ( $fp )
  94.                 {
  95.                     // work variables
  96.                     $s = 0;
  97.                     $c = 0;
  98.                     $out = "";
  99.         
  100.                     // set our created socket for 'fp' to     
  101.                     // non-blocking mode
  102.                     // so our fgets() calls will return
  103.                     // right away
  104.                     set_socket_blocking ( $fp, false );
  105.  
  106.                     // as long as our 'out' variable has a
  107.                     // null value ("")
  108.                     // keep looping (do) until we get
  109.                     // something
  110.                     //
  111.                     do
  112.                     {
  113.                         // output of the stream assigned
  114.                         // to 'out' variable
  115.                         $out = fgets ( $fp, 2500 );
  116.  
  117.                         // if we get an "220" code (service ready code (i.e greeting))
  118.                         // increment our work (code (c)) variable, and null
  119.                         // out our output variable for a later loop test
  120.                         //
  121.                         if ( ereg ( "^220", $out ) )
  122.                         {
  123.                             $s = 0;
  124.                             $out = "";
  125.                             $c++;
  126.                             $return[2] = true;
  127.                             $return[3] = $mxhosts[$i];
  128.                         }
  129.                         // elseif c is greater than 0
  130.                         // and 'out' is null (""), 
  131.                         // we got a code back from some
  132.                         // server, and we've passed
  133.                         // through this loop at least
  134.                         // once
  135.                         //
  136.                         else if ( ( $c > 0 ) && ( $out == "" ) )
  137.                         { 
  138.                             $return[2] = true;
  139.                             break; 
  140.                         }
  141.  
  142.                         // else increment our 's'
  143.                         // counter
  144.                         else
  145.                         { $s++;    }
  146.                     
  147.                         // and if 's' is 9999, break, to
  148.                         // keep from looping
  149.                         // infinetly
  150.                         if ( $s == 9999 ) { break; }
  151.                     
  152.                     } while ( $out == "" );
  153.  
  154.                     // reset our file pointer to blocking
  155.                     // mode, so we wait
  156.                     // for communication to finish before 
  157.                     // moving on...
  158.                     set_socket_blocking ( $fp, true );
  159.  
  160.                     // talk to the MX mail server,
  161.                     // validating ourself (HELO)
  162.                     fputs ( $fp, "HELO $SERVER_NAME\n" );
  163.  
  164.                     // get the mail servers reply, assign to
  165.                     // 'output' (ignored)
  166.                     $output = fgets ( $fp, 2000 );
  167.  
  168.                     // give a bogus "MAIL FROM:" header to
  169.                     // the server
  170.                     fputs ( $fp, "MAIL FROM: <info@" . $tld . ">\n" );
  171.  
  172.                     // get output again (ignored)
  173.                     $output = fgets ( $fp, 2000 );
  174.  
  175.                     // give RCPT TO: header for the email
  176.                     // address we are testing
  177.                     fputs ( $fp, "RCPT TO: <$email>\n" );                
  178.  
  179.                     // get final output for validity testing
  180.                     // (used)
  181.                     $output = fgets ( $fp, 2000 );
  182.  
  183.                     // test the reply code from the mail
  184.                     // server for the 250 (okay) code
  185.                     if ( ereg ( "^250", $output ) )
  186.                     {
  187.                         // set our true/false(ness)
  188.                         // array item for testing
  189.                         $return[0] = true;
  190.                     }
  191.                     else
  192.                     {
  193.                         // otherwise, bogus address,
  194.                         // fillin the 2nd array item
  195.                         // with the mail servers reply
  196.                         // code for user to test if they
  197.                         // want
  198.                         $return[0] = false;
  199.                         $return[1] = $output;
  200.                     }
  201.                 
  202.                     // tell the mail server we are done
  203.                     // talking to it
  204.                     fputs ( $fp, "QUIT\n" );
  205.  
  206.                     // close the file pointer
  207.                     fclose( $fp );
  208.  
  209.                     // if we got a good value break,
  210.                     // otherwise, we'll keep
  211.                     // trying MX records until we get a good
  212.                     // value, or we
  213.                     // exhaust our possible MX servers
  214.                     if ( $return[0] == true )
  215.                     { break; }
  216.                 }
  217.             }
  218.         }
  219.     } else {
  220.     // No MX record appears for the specified Top-Level Domain; possibly
  221.     // an invalid host/domain name was specified.
  222.         $return[0] = false;
  223.         $return[1] = "Invalid email address (bad domain name)";
  224.         $return[2] = false;
  225.     } // end if checkdnsrr()
  226.  
  227.     // return the array for the user to test against
  228.     return $return;
  229. }
  230. ?>
  231.  
  232. <?php
  233.     // Here is an example chunk of code...  Normally, you'd call the
  234.     // validate email with a form or some other method to provide
  235.     // the input email address
  236.     //
  237.     //  NOTE:  You need to delete the /* and */ comment entries
  238.  
  239. /*  
  240. // Minimal HTML code to test validateEmail() function.  Save this
  241. // to a file to test...try different email addresses in the
  242. // 'email_address' variable field to see how it behaves.
  243. //
  244. <html>
  245. <head>
  246. <tilte> Validate an email address. </title>
  247. </head>
  248.  
  249. <body bgcolor="#000000" TEXT="#FFFFCC" LINK="#33CCFF" VLINK="#9999CC" ALINK="#FFFF00">
  250.  
  251. // Assumes that you put the validateEmail() function in a file 
  252. // named "validateEmail.php3", which is in the current directory
  253. <?php  include( "validateEmail.php3" ); 
  254.  
  255.     $email_address = "shane@tuna.org";
  256.     $val_results = array( "", "" );
  257.  
  258.     $val_results = validateEmail( $email_address ); 
  259.  
  260.     if ( $val_results[0] == true )
  261.         {
  262.             $value = "<font color=\"green\">a valid</font>";
  263.         } else {
  264.             $value = "<font color=\"red\">not a valid</font>";
  265.         }
  266.  
  267.    // print debugging info
  268.     print( "<br>Debugging Info:<br><br>" );
  269.     print( "<pre>" );
  270.     print( "(valid/not valid)                   <font color=\"white\">value</font> is: " . $value. "<br>");
  271.     print( "(true/false valid email)   <font color=\"white\">val_results[0]</font> is: " . $val_results[0] . "<br>");
  272.     print( "(SMTP code if false)       <font color=\"white\">val_results[1]</font> is: " . $val_results[1] . "<br>");
  273.     print( "(true/false valid MX host) <font color=\"white\">val_results[2]</font> is: " . $val_results[2] . "<br>");
  274.     print( "(MX host that answered)    <font color=\"white\">val_results[3]</font> is: " . $val_results[3] . "<br>");
  275.     print( "</pre>" );
  276.     print( "<pre>true is represented by a 1 (one)<br>false is represented by null output</pre><br>");
  277.     // end debugging info
  278.  
  279.     print( "The address <font color=\"white\"><tt>" . $email_address . "</tt></font> is " . $value . " email address.");
  280. */
  281.     
  282. ?>
  283.  
  284. </body>
  285. </html>
  286.  
  287.  
  288.