|
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
|
|
|
|
|
16
|
|
|
protected $_request; |
|
17
|
|
|
protected $_response; |
|
18
|
|
|
protected $_max_redirects = 5; |
|
19
|
|
|
protected $_previous_url; |
|
20
|
|
|
|
|
21
|
2 |
|
public function max_redirects($max_redirects = NULL) |
|
22
|
|
|
{ |
|
23
|
2 |
|
if ($max_redirects !== NULL) |
|
24
|
2 |
|
{ |
|
25
|
1 |
|
$this->_max_redirects = (int) $max_redirects; |
|
26
|
1 |
|
return $this; |
|
27
|
|
|
} |
|
28
|
2 |
|
return $this->_max_redirects; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
public function user_agent() |
|
32
|
|
|
{ |
|
33
|
1 |
|
return \Request::$user_agent; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
public function current_url() |
|
37
|
|
|
{ |
|
38
|
4 |
|
return \URL::site($this->current_path(), TRUE); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function previous_url() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->_previous_url; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
public function current_path() |
|
47
|
|
|
{ |
|
48
|
4 |
|
if ( ! $this->_request) |
|
49
|
4 |
|
return NULL; |
|
50
|
|
|
|
|
51
|
4 |
|
return '/'.ltrim($this->_request->uri(), '/'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function request() |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->_request; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function response() |
|
60
|
|
|
{ |
|
61
|
2 |
|
return $this->_response; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
public function execute($method, $url, array $post = array()) |
|
65
|
|
|
{ |
|
66
|
4 |
|
$redirects_count = 1; |
|
67
|
|
|
|
|
68
|
4 |
|
\Request::$initial = NULL; |
|
69
|
|
|
|
|
70
|
4 |
|
$this->_request = \Request::factory($url) |
|
71
|
4 |
|
->method($method) |
|
72
|
4 |
|
->post($post) |
|
73
|
4 |
|
->body(http_build_query($post)); |
|
74
|
|
|
|
|
75
|
4 |
|
if ($this->_previous_url) { |
|
76
|
2 |
|
$this->_request->referrer($this->_previous_url); |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$this->_previous_url = $this->current_url().\URL::query($this->_request->query(), FALSE); |
|
80
|
|
|
|
|
81
|
4 |
|
\Request::$initial = $this->_request; |
|
82
|
|
|
|
|
83
|
4 |
|
$this->_response = $this->_request->execute(); |
|
84
|
|
|
|
|
85
|
4 |
|
while (($this->_response->status() >= 300 AND $this->_response->status() < 400)) |
|
86
|
|
|
{ |
|
87
|
2 |
|
$redirects_count++; |
|
88
|
|
|
|
|
89
|
2 |
|
if ($redirects_count >= $this->max_redirects()) |
|
90
|
2 |
|
throw new Exception_Toomanyredirects('Maximum Number of redirects (5) for url :url', array(':url' => $url)); |
|
91
|
|
|
|
|
92
|
2 |
|
$url_parts = parse_url($this->_response->headers('location')); |
|
93
|
|
|
|
|
94
|
2 |
|
$query = isset($url_parts['query']) ? $url_parts['query'] : ''; |
|
95
|
2 |
|
parse_str($query, $query); |
|
96
|
|
|
|
|
97
|
2 |
|
$_GET = $query; |
|
98
|
|
|
|
|
99
|
2 |
|
$url = $url_parts['path']; |
|
100
|
|
|
|
|
101
|
2 |
|
\Request::$initial = NULL; |
|
102
|
|
|
|
|
103
|
2 |
|
$this->_request = \Request::factory($url); |
|
104
|
|
|
|
|
105
|
2 |
|
\Request::$initial = $this->_request; |
|
106
|
|
|
|
|
107
|
2 |
|
$this->_response = $this->_request->execute(); |
|
108
|
2 |
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
return $this->_response->body(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|