Created
September 9, 2010 16:51
-
-
Save pedrofaria/572158 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Awproxy | |
{ | |
private static $_instance; | |
private static $url_base = 'http://www1.astrowars.com'; | |
public static function instance() { | |
if (!isset(Awproxy::$_instance)) { | |
Awproxy::$_instance = new Awproxy(); | |
} | |
return Awproxy::$_instance; | |
} | |
public function __construct() { | |
} | |
// Storing Cookies in Session | |
private function store_cookies(&$http) { | |
$cookies = $http->getResponseCookies(); | |
if (!count($cookies)) return; | |
$sess = Session::instance(); | |
$s_cookie = $sess->get('proxy_sess', array()); | |
foreach ($cookies as $c) { | |
foreach ($c->cookies as $k => $v) { | |
$s_cookie[$k] = $v; | |
if ($k == 'login') { | |
$sess->set('proxy_clicks', 0); | |
} | |
} | |
} | |
$sess->set('proxy_sess', $s_cookie); | |
} | |
// Loading Cookies in Session | |
private function load_cookies(&$http) { | |
$sess = Session::instance(); | |
if ($s_cookie = $sess->get('proxy_sess')) { | |
#print Kohana::debug($s_cookie); | |
$http->addCookies($s_cookie); | |
} | |
$clicks = $sess->get('proxy_clicks', 0); | |
$sess->set('proxy_clicks', ++$clicks); | |
} | |
private function setDefaultHeaders(&$http) { | |
$http->setHeaders(array('X-Forwarded-For' => $_SERVER['REMOTE_ADDR'])); | |
} | |
private function to_xml($body) { | |
$body = preg_replace('/<head>.*?<\/heads?>/si', '', $body); | |
$body = preg_replace('/<script.*?>.*?<\/script>/si', '', $body); | |
$body = preg_replace('/<noscript.*?>.*?<\/noscript>/si', '', $body); | |
$body = preg_replace('/<br>/si', '', $body); | |
$body = preg_replace('/<\/?center>/si', '', $body); | |
//$body = tidy_repair_string($body, array('output-html' => TRUE)); | |
//var_dump($body); | |
$doc = new DOMDocument; | |
@$doc->loadHTML($body); | |
//var_dump($doc); | |
$xml = simplexml_import_dom($doc); | |
//var_dump($xml); | |
return $xml; | |
} | |
public function get($path, $to_xml = TRUE) { | |
$url = Awproxy::$url_base . $path; | |
$h = new HttpRequest($url, HTTP_METH_GET); | |
$this->setDefaultHeaders($h); | |
$this->load_cookies($h); | |
$h->send(); | |
$this->store_cookies($h); | |
$body = $h->getResponseBody(); | |
if ($to_xml) return $this->to_xml($body); | |
else return $body; | |
} | |
public function post($path, $data, $to_xml = TRUE) { | |
$url = Awproxy::$url_base . $path; | |
$h = new HttpRequest($url, HTTP_METH_POST); | |
$h->addPostFields($data); | |
$this->setDefaultHeaders($h); | |
$this->load_cookies($h); | |
$h->send(); | |
$this->store_cookies($h); | |
$body = $h->getResponseBody(); | |
if ($to_xml) return $this->to_xml($body); | |
else return $body; | |
} | |
public function need_login($xml) { | |
$rs = $xml->xpath("//form[@action='/register/login.php']"); | |
return (bool) count($rs); | |
} | |
public function need_secure($xml) { | |
$rs = $xml->xpath("//img[@src='/0/secure.php']"); | |
return (bool) count($rs); | |
} | |
public function check_session($xml) { | |
if ($this->need_login($xml)) { | |
throw new Awproxy_Ex_NeedLogin; | |
} | |
if ($this->need_secure($xml)) { | |
throw new Awproxy_Ex_NeedSecure; | |
} | |
} | |
} | |
class Awproxy_Ex_NeedLogin extends Kohana_Exception { | |
public function __construct() { | |
parent::__construct('Need Login', NULL, 100); | |
} | |
} | |
class Awproxy_Ex_NeedSecure extends Kohana_Exception { | |
public function __construct() { | |
parent::__construct('Need Secure Login', NULL, 101); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment