/** * Update Gmail cookie * * @return bool true * @param string raw HTML header * @access private * @static * @author Neerav * @since 19 Apr 2006 */ function update_cookies($data = "") { // completely rewritten (expanded, fixed); Neerav; 25 Jan 2007 $new_cookies = GMailer::get_cookies($data); $parse_headers = $data == "" ? $this->gmail_data : $data; $replace_cookies = array("S", "GX", "GXAS_SEC", "GMAIL_AT", "GMAIL_HELP"); //Debugger::say("new cookies: ".print_r($new_cookies,true)); //Debugger::say("old cookie: ".print_r($this->cookie_str,true)); //Debugger::say("set new cookies: ".print_r($parse_headers,true)); foreach ($replace_cookies as $indexval => $cookie) { $matches = array(); if (preg_match("/" . $cookie . "=([^;]*);/U", $parse_headers, $matches)) { //Debugger::say("cookie matches: ".print_r($matches,true)); if (preg_match("/" . $cookie . "=([^;]*);/U", $this->cookie_str)) { $this->cookie_str = preg_replace("/" . $cookie . "=([^;]*);/U", $cookie . "=" . $matches[1] . ";", $this->cookie_str); } else { $this->cookie_str = $cookie . "=" . $matches[1] . "; " . $this->cookie_str; } } } // save updated cookie GMailer::saveSessionToBrowser(); //Debugger::say("new cookie: ".print_r($this->cookie_str,true)); return true; }
/** * Connect to Gmail without setting any session/cookie * * @return bool Connect to Gmail successfully or not */ function connectNoCookie() { Debugger::say("Start connecting without cookie..."); $postdata = "service=mail"; $postdata .= "&Email=" . urlencode($this->login); $postdata .= "&Passwd=" . urlencode($this->pwd); $postdata .= "&null=Sign%20in"; $postdata .= "&continue=https%3A%2F%2Fmail.google.com%2Fmail%3F"; // Added by Neerav; 28 June 2005 $postdata .= "&rm=false"; // not required but appears $postdata .= "&hl=en"; // Added by Neerav; 8 July 2005 // login challenge //id="logintoken" value="cpVIYkaTDTkVZ9ZHNM_384GVV79tjExj-ac2NFVgS3AVbm7lEn7Q967JHKe_sDzMP7plluysBDJRyUwkjuHQFw:D0cwussDwRyIgJGSdeMMnA" name="logintoken"> if (isset($this->logintoken) and $this->logintoken != "") { $postdata .= "&logintoken=" . $logintoken; } if (isset($this->logincaptcha) and $this->logincaptcha != "") { $postdata .= "&logincaptcha=" . $logincaptcha; } $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($c, CURLOPT_URL, GM_LNK_LOGIN); curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($c, CURLOPT_USERAGENT, GM_USER_AGENT); $this->CURL_PROXY($c); curl_setopt($c, CURLOPT_POST, 1); curl_setopt($c, CURLOPT_HEADER, 1); curl_setopt($c, CURLOPT_POSTFIELDS, $postdata); curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($c, CURLOPT_REFERER, GM_LNK_REFER); $this->gmail_data = curl_exec($c); curl_close($c); //Debugger::say("login result: ".$result); /** from here we have to perform "cookie-handshaking"... **/ Debugger::say("Start cookie-handshaking..."); $cookies = GMailer::get_cookies($this->gmail_data); Debugger::say("1st phase cookie obtained: " . $cookies); $this->logintoken = ""; $this->logincaptcha = ""; if (strpos($this->gmail_data, "errormsg_0_Passwd") > 0) { $this->cookie_str = ""; $this->cookie_ik_str = ""; // Added appropriate error message; by Neerav; 8 July 2005 $a = array("action" => "sign in", "status" => "failed", "message" => "Username and password do not match. (You provided " . $this->login . ")", "login_error" => "userpass"); array_unshift($this->return_status, $a); //Debugger::say("login incorrect: ".print_r($a,true)); Debugger::say("Connect FAILED: user/pass incorrect"); return false; // Added to support login challenge; by Neerav; 8 July 2005 } elseif (strpos($this->gmail_data, "errormsg_0_logincaptcha") > 0) { $this->cookie_str = ""; $this->cookie_ik_str = ""; ereg("id=\"logintoken\" value=\"([^\"]*)\" name=\"logintoken\"", $this->gmail_data, $matches); Debugger::say("Connect FAILED: login challenge"); //Debugger::say("Connect FAILED: login challenge: ".$this->gmail_data); //Debugger::say("ErrorLogin: "******"ErrorToken: ".$matches[1]); //Debugger::say("logintoken: ".print_r($matches,true)); // Added appropriate error message; by Neerav; 8 July 2005 $a = array("action" => "sign in", "status" => "failed", "message" => "login challenge", "login_token" => $matches[1], "login_error" => "challenge"); array_unshift($this->return_status, $a); //Debugger::say("login challenge: ".print_r($a,true)); return false; } Debugger::say("Received: " . $this->gmail_data); /*** js forward path (Gan: no longer used? 10 Sept 2005) $a = strpos($this->gmail_data, "top.location = \""); $b = strpos($this->gmail_data, ";", $a); $forward = substr($this->gmail_data, $a+16, $b-($a+16)-1); // forces relative url into absolute if not already; Added by Neerav; 31 July 2005 if (substr($forward,0,8) != "https://") { $forward = "https://mail.google.com/accounts/".$forward; } **/ $a = strpos($this->gmail_data, "Location: "); $b = strpos($this->gmail_data, "\n", $a); $forward = substr($this->gmail_data, $a + 10, $b - ($a + 10)); Debugger::say("Redirecting: " . $forward); $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); // Forward url is now absolute instead of relative; Fixed by Gan; 27 July 2005 //curl_setopt($c, CURLOPT_URL, "https://mail.google.com/accounts/".$forward); curl_setopt($c, CURLOPT_URL, $forward); curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($c, CURLOPT_USERAGENT, GM_USER_AGENT); curl_setopt($c, CURLOPT_HEADER, 1); $this->CURL_PROXY($c); curl_setopt($c, CURLOPT_COOKIE, $cookies); curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($c, CURLOPT_REFERER, GM_LNK_REFER); $ret = curl_exec($c); curl_close($c); $data = GMailer::get_cookies($ret); $d = $data ? $data : $cookies; $d = $d . ";TZ=" . $this->timezone; Debugger::say("2nd phase cookie obtained: " . $d); $this->cookie_str = $cookies . ";" . $d; // the cookie string obtained from gmail Debugger::say("Finished connecting without cookie."); return true; }
/** * Connect to Gmail without setting any session/cookie * * @return bool Connect to Gmail successfully or not */ function connectNoCookie() { $postdata = "service=mail"; $postdata .= "&Email=" . urlencode($this->login); $postdata .= "&Passwd=" . urlencode(str_replace(' ', '+', $this->pwd)); $postdata .= "&null=Sign%20in"; $postdata .= "&continue=https%3A%2F%2Fmail.google.com%2Fmail%3F"; // Added by Neerav; 28 June 2005 $postdata .= "&rm=false"; // not required but appears $postdata .= "&hl=en"; /* // Updated by Neerav; 4 Apr 2006 */ /* $postdata = array(); */ /* $postdata['ltempl'] = "yj_blanco"; */ /* $postdata['ltemplcache'] = 2; */ /* $postdata['continue'] = "http://mail.google.com/mail/"; */ /* $postdata['service'] = "mail"; */ /* $postdata['rm'] = "false"; */ /* $postdata['hl'] = "en"; */ /* $postdata['Email'] = $this->login; */ /* $postdata['Passwd'] = str_replace(' ','+',$this->pwd); */ /* $postdata['rmShown'] = 1; */ /* $postdata['null'] = "Sign in"; */ // Added by Neerav; 8 July 2005 // login challenge //id="logintoken" value="cpVIYkaTDTkVZ9ZHNM_384GVV79tjExj-ac2NFVgS3AVbm7lEn7Q967JHKe_sDzMP7plluysBDJRyUwkjuHQFw:D0cwussDwRyIgJGSdeMMnA" name="logintoken"> if (isset($this->logintoken) and $this->logintoken != "") { $postdata .= "&logintoken=" . $logintoken; } if (isset($this->logincaptcha) and $this->logincaptcha != "") { $postdata .= "&logincaptcha=" . $logincaptcha; } $this->gmail_data = GMailer::execute_curl(GM_LNK_LOGIN, GM_LNK_LOGIN_REFER, 'post', $postdata, 'nocookie', ""); /* Debugger::say("first phase: ".print_r($this->gmail_data,true)); */ $a = array("action" => "connecting to Gmail (without cookie)", "status" => $this->gmail_data != "" ? "success" : "failed", "message" => $this->gmail_data != "" ? "connected to Gmail (without cookie)" : "no response", "login_error" => $this->gmail_data != "" ? "" : "no response"); array_unshift($this->return_status, $a); if ($this->gmail_data == "") { return false; } /** from here we have to perform "cookie-handshaking"... **/ $cookies = GMailer::get_cookies($this->gmail_data); /* Debugger::say("first phase cookies: ".print_r($cookies,true)); */ /* print_r($cookies); */ $this->logintoken = ""; $this->logincaptcha = ""; if (strpos($this->gmail_data, "errormsg_0_Passwd") > 0) { $this->cookie_str = ""; $this->cookie_ik_str = ""; // Added appropriate error message; by Neerav; 8 July 2005 $a = array("action" => "sign in", "status" => "failed", "message" => "Username and password do not match. (You provided " . $this->login . ")", "login_error" => "userpass"); array_unshift($this->return_status, $a); return false; // Added to support login challenge; by Neerav; 8 July 2005 } elseif (strpos($this->gmail_data, "errormsg_0_logincaptcha") > 0) { $this->cookie_str = ""; $this->cookie_ik_str = ""; //id="logintoken" value="cpVIYkaTDTkVZ9ZHNM_384GVV79tjExj-ac2NFVgS3AVbm7lEn7Q967JHKe_sDzMP7plluysBDJRyUwkjuHQFw:D0cwussDwRyIgJGSdeMMnA" name="logintoken"> ereg("id=\"logintoken\" value=\"([^\"]*)\" name=\"logintoken\"", $this->gmail_data, $matches); //Debugger::say("Connect FAILED: login challenge: ".$this->gmail_data); //Debugger::say("ErrorLogin: "******"ErrorToken: ".$matches[1]); //Debugger::say("logintoken: ".print_r($matches,true)); // Added appropriate error message; by Neerav; 8 July 2005 $a = array("action" => "sign in", "status" => "failed", "message" => "login challenge", "login_token" => $matches[1], "login_error" => "challenge"); array_unshift($this->return_status, $a); return false; // Check if the Gmail URL has changed; Added by Neerav; 14 Sept 2005 } elseif (strpos($this->gmail_data, "Invalid request.")) { $this->cookie_str = ""; $this->cookie_ik_str = ""; $a = array("action" => "sign in", "status" => "failed", "message" => "Gmail: Invalid request. (libgmailer: Gmail seems to have changed the URL again.)", "login_error" => "URL"); array_unshift($this->return_status, $a); return false; // Check for a cookie as a way to check the Gmail URL; Added by Neerav; 14 Sept 2005 } elseif ($cookies == "") { $this->cookie_str = ""; $this->cookie_ik_str = ""; $a = array("action" => "sign in", "status" => "failed", "message" => "libgmailer: Phase one cookie not obtained. Gmail may be down.", "login_error" => "cookie"); array_unshift($this->return_status, $a); return false; } $a = array("action" => "phase one cookie", "status" => "success", "message" => "Received: " . $cookies); array_unshift($this->return_status, $a); /*** js forward path (Gan: no longer used? 10 Sept 2005) $a = strpos($this->gmail_data, "top.location = \""); $b = strpos($this->gmail_data, ";", $a); $forward = substr($this->gmail_data, $a+16, $b-($a+16)-1); // forces relative url into absolute if not already; Added by Neerav; 31 July 2005 if (substr($forward,0,8) != "https://") { $forward = "https://mail.google.com/accounts/".$forward; } **/ $a = strpos($this->gmail_data, "Location: "); $b = strpos($this->gmail_data, "\n", $a); $forward = substr($this->gmail_data, $a + 10, $b - ($a + 10)); $a = array("action" => "redirecting", "status" => "success", "message" => "Redirecting to: " . $forward); array_unshift($this->return_status, $a); // Forward url is now absolute instead of relative; Fixed by Gan; 27 July 2005 //curl_setopt($c, CURLOPT_URL, "https://mail.google.com/accounts/".$forward); /* $ret = GMailer::execute_curl($forward, GM_LNK_REFER, 'post', $postdata, "cookie", $cookies); */ // Added extra required cookie; by Neerav; 4 Apr 2006 $second = GMailer::execute_curl($forward, GM_LNK_LOGIN_REFER, 'get', "", "cookie", "GoogleAccountsLocale_session=en; " . $cookies); $data = GMailer::get_cookies($second); /* Debugger::say("second phase: ".print_r($second,true)); */ /* Debugger::say("second phase cookies: ".print_r($data,true)); */ /* print_r($data); */ $a = array("action" => "phase two cookie", "status" => "success", "message" => "Obtained: " . $d); array_unshift($this->return_status, $a); /* $this->cookie_str = $cookies.";".$d; // the cookie string obtained from gmail */ if (strpos($second, "SetSID") !== false) { $a = array("action" => "phase three required", "status" => "success", "message" => "Starting..."); /* $forward = preg_match("/<meta content=\"0;\s*url=([^\"]*)\"/",$second,$matches); */ /* print_r($matches); */ /* Debugger::say("third phase location: ".print_r($matches,true)); */ /* $third = GMailer::execute_curl( */ /* str_replace("&","&",$forward[1]), */ /* "", // no referrer */ /* 'get', "", */ /* "nocookie", ""); */ $forward = preg_match("/<meta content=\"0;\\s*url=([^\"]*)\"/", $second, $matches); /* print_r($matches); */ /* Debugger::say("third phase location: ".print_r($matches,true)); */ /* print_r(str_replace("&","&",$matches[1])); */ $third = GMailer::execute_curl(str_replace("&", "&", $matches[1]), "", 'get', "", "nocookie", ""); $data = GMailer::get_cookies($third); /* Debugger::say("third phase: ".print_r($third,true)); */ /* Debugger::say("third phase cookies: ".print_r($data,true)); */ $data = preg_replace("/GX=.*?;\\s?GX=/", "GX=", $data); /* Debugger::say("third phase cookies (corrected): ".print_r($data,true)); */ } $d = $data ? $data : $cookies; $d = $d . ";TZ=" . $this->timezone; $this->cookie_str = preg_replace("/LSID=mail[^;]*?;/", "", $d); // the cookie string obtained from gmail /* print_r($this->cookie_str); */ // cleanup redundant cookies /* $this->cookie_str = ereg_replace( */ /* "S=gmail=([^\:]*):gmail_yj=([^\:]*):gmproxy=([^\;]*);", */ /* "", */ /* $this->cookie_str */ /* ); */ /* $this->cookie_str .= ";S=gmail=".$matches[1].":gmail_yj=".$matches[2].":gmproxy=".$matches[3].";"; */ return true; }
/** * Update Gmail cookie * * @return bool true * @param string raw HTML header * @access private * @static * @author Neerav * @since 19 Apr 2006 */ function update_cookies($data = "") { // completely rewritten (expanded, fixed); Neerav; 25 Jan 2007 /* $this->cookie_str = GMailer::get_cookies($data, $this->cookie_str); */ //$new_cookies = GMailer::get_cookies($data); $new_cookies_array = GMailer::get_cookies($data, 'array'); $existing_cookies = explode(";", $this->cookie_str); //Debugger::say(basename(__FILE__).": ".__LINE__.": "."existing cookies: ".print_r($existing_cookies,true)); //Debugger::say(basename(__FILE__).": ".__LINE__.": "."cookies returned: ".print_r($new_cookies_array,true)); if (is_string($new_cookies_array) or count($new_cookies_array) == 0) { // no cookies to update $a = array("action" => "update cookies", "status" => "success", "message" => "no cookies to be updated"); array_unshift($this->return_status, $a); ///Debugger::say(basename(__FILE__).": ".__LINE__.": update_cookies():\n".print_r($a,true)); return true; } else { //Debugger::say(basename(__FILE__).": ".__LINE__.": "."new cookies: ".print_r($new_cookies_array,true)); } $all_cookies = array(); $cookie_count = count($existing_cookies); for ($i = 0; $i < $cookie_count; $i++) { $cookie = trim($existing_cookies[$i]); if ($cookie == "") { continue; } list($name, $value) = explode("=", $cookie, 2); $all_cookies["{$name}"] = "{$value}"; //Debugger::say(basename(__FILE__).": ".__LINE__.": "."this cookie: \n".$cookie."\nexploded:\n".$name."\n".$value); } //Debugger::say(basename(__FILE__).": ".__LINE__.": "."all existing cookies: \n".print_r($all_cookies,true)); foreach ($new_cookies_array as $name => $cookie) { //$replace_cookies = array("S","GX","GXAS_SEC","GMAIL_AT","GMAIL_HELP"); if (isset($all_cookies["{$name}"])) { $all_cookies["{$name}"] = $cookie['value']; } } $cookie_string = ""; foreach ($all_cookies as $name => $value) { $cookie_string .= $cookie_string == "" ? $name . "=" . $value : "; " . $name . "=" . $value; } $a = array("action" => "update cookies", "status" => "success", "message" => "Updated cookies"); array_unshift($this->return_status, $a); //Debugger::say(basename(__FILE__).": ".__LINE__.": update_cookies():\n".print_r($a,true)); // save updated cookie GMailer::saveSessionToBrowser(); ///Debugger::say(basename(__FILE__).": ".__LINE__.": update_cookies(): new cookie: ".print_r($this->cookie_str,true)); return true; }