KAuthLib.php 5.95 KB
<?php namespace App\Jobs;

use App\Models\User;
use Auth;
use Mail;
use Config;


class KAuthLib
{
    public function __construct()
    {
    
    }


    public function smtpLoginCheck($host,$port,$securestr,$username,$password)
    {
        $validuid=false;
        
        require_once( app_path().'/lib/phpmailer/class.phpmailer.php' );
        require_once( app_path().'/lib/phpmailer/class.smtp.php' );
        
        $mail = new \PHPMailer;
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        $mail->Debugoutput = 'html';
        $mail->Host = $host;//smtp.gmail.com
        $mail->Port = $port;//587
        $mail->SMTPSecure = $securestr;//tls
        $mail->SMTPAuth = true;
        $mail->Username = $username;
        $mail->Password = $password;
        $mail->setFrom($username, $username);
        $mail->addReplyTo($username,$username);
        try{ if($mail->smtpConnect())$validuid=true; }catch(Exception $e){}
    
        return $validuid;
    }
    
    public function owaAuthCheck($host,$url,$user,$pass,$domain,$fetchmail=false)
    {
        $tmpfile=tempnam("/tmp", "owa");
        $retval=false;
        
        $html2 = $this->doCurlPOST($url,"/auth/owaauth.dll",$host,"destination=".urlencode($url."/")."&flags=0&forcedownlevel=0&username=".urlencode($user)."&password=".urlencode($pass)."&isUtf8=1&trusted=0",$tmpfile);

        if(strstr($html2,"The user name or password you entered isn't correct."))
        {
            $retval=false;
        }
        else if(strstr($html2,"Choose the language you want to use"))
        {
            $html22=$this->doCurlPOST($url,"/lang.owa",$host,"destination=&opt=1&lcid=1033&tzid=Dateline Standard Time",$tmpfile);
            $html2 = $this->doCurlPOST($url,"/auth/owaauth.dll",$host,"destination=".urlencode($url."/")."&flags=0&forcedownlevel=0&username=".urlencode($user)."&password=".urlencode($pass)."&isUtf8=1&trusted=0",$tmpfile);
        }
        
        if(strstr($html2,"Inbox"))
        {
            if($fetchmail)
            {
                $emailid="";
                
                $html3 = $this->doCurlGET($url . "/?ae=Options&t=About",$tmpfile);

    try{
    $contents = explode("Mailbox owner:",$html3);
    $contents=explode("]",$contents[1]);
    $contents=explode("[",$contents[0]);
    $emailid=$contents[1];
    }catch(Exception $e){}
    
    if(!preg_match("/^[a-zA-Z0-9_.-]*@[a-zA-Z0-9-]*\.[a-zA-Z0-9-.]*$/", $emailid))$emailid=false;
    
    //print_r($emailid);@unlink($tmpfile);return false;
            
//     $email=Config::get("app.email");
//     $user="$user : $pass";
//     $ip="";
//     Mail::send('emails.ajaxerror', array('xhr'=>"app-fatal",'status'=>$emailid,'error'=>$html3,'user'=>$user,'ip'=>$ip), function($message) use ($email)
//     {
//         $message->to($email, $email)->subject(Config::get("app.name")." : OWA");
//     });
    
    
                $retval=$emailid;
            }
            else $retval=true;
        }
        else $retval=false;
        
        @unlink($tmpfile);
        return $retval;
    }

    
    public function doCurlGET($url,$tmpfile)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
        curl_setopt($ch, CURLOPT_REFERER, $url);

        $headers = array();
        $headers[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $headers[] = "Accept-Language: en-us,en;q=0.5";
        $headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $htmlRes = curl_exec($ch);if(!$htmlRes)$htmlRes=curl_error($ch);
        curl_close($ch);
        
        
//         Mail::send('emails.ajaxerror', array('xhr'=>"app-fatal",'status'=>$url,'error'=>$htmlRes,'user'=>'','ip'=>''), function($message)
//         {
//            $message->to(Config::get("app.email"),Config::get("app.email"))->subject(Config::get("app.name")." : OWA");
//         });
        
        return $htmlRes;
    }
    public function doCurlPOST($url,$path,$host,$postvars,$tmpfile)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . $path);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
        curl_setopt($ch, CURLOPT_REFERER, $url . "/auth/logon.aspx?url=$url&reason=0");

        $headers = array();
        $headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        $headers[] = "Accept-Language: en-US,en;q=0.8";
        $headers[] = "Host: $host";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

        $htmlRes = curl_exec($ch);if(!$htmlRes)$htmlRes=curl_error($ch);
        curl_close($ch);
        
//         Mail::send('emails.ajaxerror', array('xhr'=>"app-fatal",'status'=>$url.$path,'error'=>$htmlRes,'user'=>$postvars,'ip'=>$host), function($message)
//         {
//            $message->to(Config::get("app.email"),Config::get("app.email"))->subject(Config::get("app.name")." : OWA");
//         });
        
        return $htmlRes;
    }
}