David Thomas Baker » Examples » Viewing File: class.google.php

<?php

class google{
    
    public 
$cookiejar;
    public 
$logged_in;
    private 
$ch;
    
    public function 
__construct(){
        
$this->logged_in false;
        
$this->cookiejar "/tmp/tmp_cookies";
    }
    private function 
posterize($post){
        
$postv '';
        foreach(
$post as $key=>$val){
            
$postv .= $key .'='.$val.'&';
        }
        return 
$postv;
    }
    private function 
clean($data){
        
$data preg_replace("/\r|\n/","",$data);
        
$data preg_replace("/\s+/"," ",$data);
        return 
$data;
    }
    public function 
init($service ""){
        
$this->google_service $service;
    
        
$this->ch curl_init();
        
curl_setopt($this->chCURLOPT_HEADER0);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($this->chCURLOPT_COOKIEJAR$this->cookiejar);
        
curl_setopt($this->chCURLOPT_COOKIEFILE$this->cookiejar);
        
curl_setopt($this->chCURLOPT_USERAGENT"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        
        
// start a session
        
$url "https://www.google.com/accounts/ServiceLogin?service=".$this->google_service;
        
curl_setopt($this->chCURLOPT_URL$url);
        
$data $this->clean(curl_exec($this->ch));
        
        
// if(<input type="hidden" name="Email")
        
if(preg_match('/<input type="hidden" name="Email" id="Email" value="([^"]+)"/',$data,$matches)){
            
// there is already an eamila ddress associated with this cookie
            
$this->got_user_cookie true;
            
$this->got_user_cookie_email $matches[1];
        }
        
//<input type="hidden" name="GALX" value="EeM8uSvHWCg" />
        // this value is also in the cookie
        
preg_match('/<input type="hidden" name="GALX" value="([^"]+)"/',$data,$matches);
        
$this->google_galx $matches[1];
        
    }
    public function 
signin($username,$password){
        
$url "https://www.google.com/accounts/ServiceLoginAuth";
        
curl_setopt($this->chCURLOPT_URL$url);
        
// login details
        
$post = array(
            
"GALX"        =>    $this->google_galx,
            
"Email"        =>    $username,
            
"Passwd"    =>    $password,
            
//"PersistentCookie"    =>    "yes",
            
"rmShown"    =>    "1",
            
"service"    =>    $this->google_service,
            
        );
        
curl_setopt($this->chCURLOPT_POSTtrue);
        
curl_setopt($this->chCURLOPT_POSTFIELDS$this->posterize($post));
        
$data $this->clean(curl_exec($this->ch));
        
curl_setopt($this->chCURLOPT_POSTfalse);
        
curl_setopt($this->chCURLOPT_POSTFIELDSfalse);

        if(
preg_match('/LoginDoneHtml/',$data)){
            
// login success
            
            /*$url = "https://www.google.com/accounts/CheckCookie?chtml=LoginDoneHtml";
            curl_setopt($this->ch, CURLOPT_URL, $url);
            $data = $this->clean(curl_exec($this->ch));*/
            
            
$this->logged_in true;
            return 
true;
        }else{
            
// echo $data; // uncomment for debug
            
return false;
        }
    }
    
    public function 
grab_url($url){
        
// grab and return the contents of the url 
        // we pass along the authenticated cookie information
        // so this is really only useful for google.com url's (eg: web history export)
        
curl_setopt($this->chCURLOPT_URL$url);
        
$data curl_exec($this->ch);
        return 
$data;
    }
    
    
}

?>