| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Openbuildings\Spiderling; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Use kohana requests to load urls, handle redirects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package    Openbuildings\Spiderling | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author     Ivan Kerin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright  (c) 2013 OpenBuildings Ltd. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license    http://spdx.org/licenses/BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Driver_Kohana_RequestFactory_Kohana implements Driver_Simple_RequestFactory { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	protected $_request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	protected $_response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	protected $_max_redirects = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	protected $_previous_url; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 4 |  | 	public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 4 |  | 		$this->user_agent('Spiderling Kohana Driver'); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  | 	public function max_redirects($max_redirects = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  | 		if ($max_redirects !== NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  | 			$this->_max_redirects = (int) $max_redirects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  | 			return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 		return $this->_max_redirects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * Getter / Setter for the user agent, used when performing the requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @param  string $user_agent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @return string|Driver_Simple_RequestFactory_HTTP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  | 	public function user_agent($user_agent = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 4 |  | 		if ($user_agent !== NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 4 |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 4 |  | 			\Request::$user_agent = $user_agent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  | 			return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  | 		return \Request::$user_agent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 4 |  | 	public function current_url() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  | 		return \URL::site($this->current_path(), TRUE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function previous_url() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		return $this->_previous_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  | 	public function current_path() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  | 		if ( ! $this->_request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  | 			return NULL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  | 		return '/'.ltrim($this->_request->uri(), '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  | 	public function request() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  | 		return $this->_request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  | 	public function response() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  | 		return $this->_response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  | 	public function execute($method, $url, array $post = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  | 		$redirects_count = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  | 		$this->_request = new \Request($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  | 		$this->_request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  | 			->method($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  | 			->post($post) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  | 			->body(http_build_query($post)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  | 		if ($this->_previous_url) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  | 			$this->_request->referrer($this->_previous_url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  | 		$this->_previous_url = $this->current_url().\URL::query($this->_request->query(), FALSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  | 		\Request::$initial = $this->_request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  | 		$this->_response = $this->_request->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  | 		while (($this->_response->status() >= 300 AND $this->_response->status() < 400)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  | 			$redirects_count++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  | 			if ($redirects_count >= $this->max_redirects()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  | 				throw new Exception_Toomanyredirects( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 2 |  | 					'Maximum Number of redirects (:max_redirects) for url :url', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 					array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  | 						':url' => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  | 						':max_redirects' => $this->max_redirects(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 					) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  | 			$url_parts = parse_url($this->_response->headers('location')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  | 			$query = isset($url_parts['query']) ? $url_parts['query'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  | 			parse_str($query, $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  | 			$_GET = $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  | 			$url = $url_parts['path']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  | 			$this->_request = new \Request($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  | 			$this->_request->query($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  | 			\Request::$initial = $this->_request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  | 			$this->_response = $this->_request->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  | 		return $this->_response->body(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |