trollolodn

#php #pop3 mail login and db mysql

Jan 22nd, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2.  
  3. class Pop{
  4.     function auth_pop3_ssl($username, $password)
  5.     {
  6.         $popserver = 'pop_server';
  7.         /*
  8.          Usage: auth_pop3_ssl('username', 'password', 'pop3.example.com')
  9.  
  10.          If the host is using SSL, prepend $popserver with "ssl://"
  11.  
  12.          Original code from:
  13.          http://www.php.happycodings.com/E-Mail/code18.html
  14.  
  15.          Modified by Lord Matt:
  16.          http://lordmatt.co.uk/item/1162/
  17.          Updates and fixes by Andrew Peng:
  18.          http://andrewpeng.net/computing/php-scripting/pop3-authentication
  19.  
  20.          This program is free software: you can redistribute it and/or modify
  21.          it under the terms of the GNU General Public License as published by
  22.          the Free Software Foundation, either version 3 of the License, or
  23.          (at your option) any later version.
  24.  
  25.          This program is distributed in the hope that it will be useful,
  26.          but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28.          GNU General Public License for more details.
  29.  
  30.          You should have received a copy of the GNU General Public License
  31.          along with this program. If not, see http://www.gnu.org/licenses/.
  32.          */
  33.  
  34.         $isSSL = 0;
  35.  
  36.         if(substr($popserver, 0, 6) == "ssl://")
  37.         {
  38.             $isSSL = 1;
  39.         }
  40.         if(trim($username)=='')
  41.         {
  42.             return false;
  43.         }
  44.         else
  45.         {
  46.             if($isSSL)
  47.             {
  48.                 $fp = fsockopen("$popserver", 995, $errno, $errstr);
  49.             }
  50.  
  51.             else
  52.             {
  53.                 $fp = fsockopen("$popserver", 110, $errno, $errstr);
  54.             }
  55.  
  56.             if(!$fp)
  57.             {
  58.                 // failed to open POP3
  59.                 return "status:connect_failed";
  60.             }
  61.  
  62.             else
  63.             {
  64.                 set_socket_blocking($fp,-1); // Turn off blocking
  65.  
  66.                 /*
  67.                  Clear the POP server's Banner Text.
  68.                  eg.. '+OK Welcome to etc etc'
  69.                  */
  70.  
  71.                 $trash = fgets($fp,128); // Trash to hold the banner
  72.                 fwrite($fp,"USER $username\r\n"); // POP3 USER CMD
  73.                 $user = fgets($fp,128);
  74.                 $user = ereg_replace("\n","",$user);
  75.  
  76.                 if ( ereg ("^\+OK(.+)", $user ) )
  77.                 {
  78.                     fwrite($fp,"PASS $password\r\n"); // POP3 PASS CMD
  79.                     $pass = fgets($fp,128);
  80.                     $pass = ereg_replace("\n","",$pass);
  81.  
  82.                     if ( ereg ("^\+OK(.+)", $pass ) )
  83.                     {
  84.                         // User has successfully authenticated
  85.                         $auth = "status:true";
  86.                     }
  87.  
  88.                     else
  89.                     {
  90.                         // bad password
  91.                         $auth = "status:false";
  92.                     }
  93.                 }
  94.  
  95.                 else
  96.                 {
  97.                     // bad username
  98.                     $auth = "status:false";
  99.                 }
  100.  
  101.                 fwrite($fp,"QUIT\r\n");
  102.                 fclose($fp);
  103.                 return $auth;
  104.             }
  105.         }
  106.     }
  107. }
  108. ?>
  109.  
  110. //Login by mail account pop 3 and check on DB
  111. // *twitter.com/quocvuongdn
  112. <?php
  113. $this->popMail = new POP();
  114. $mailStatus = $this->popMail->auth_pop3_ssl($email,$password);
  115. if($mailStatus == "status:true"){
  116.     $result = $this->userModel->GetUserByEmail($email); //Check account on DB
  117.     if(count($result) > 0){
  118.         if($result['Active'] == 1){
  119.             $this->userModel->UpdateLastLoginByEmail($email);
  120.             $_SESSION['user_account'] = $result;
  121.             echo 'data:success'; //Successfully sign in
  122.         } else{
  123.             echo 'data:error:inactive'; //Account inactive
  124.         }
  125.     }else{
  126.         //Insert to DB
  127.             $newUser = array("PermissionID" => 2, "Email" => "'".$email."'", "LastLogin" => 'now()', "Active" => 0);
  128.             $this->userModel->InsertNewUser($newUser); //Insert new account
  129.             echo 'data:error:inactive'; //Account inactive
  130.             }
  131. } elseif($mailStatus == "status:connect_failed"){
  132.             echo 'data:error:connect'; //Server issue
  133. } elseif($mailStatus == "status:false"){
  134.         echo 'data:error:failed'; //Invalid email or password
  135. }
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment