WebBrowser.php
12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* WebBrowser plugin.
*
*/
class phpQueryObjectPlugin_WebBrowser {
/**
* Limit binded methods to specified ones.
*
* @var array
*/
public static $phpQueryMethods = null;
/**
* Enter description here...
*
* @param phpQueryObject $self
* @todo support 'reset' event
*/
public static function WebBrowser($self, $callback = null, $location = null) {
$self = $self->_clone()->toRoot();
$location = $location
? $location
// TODO use document.location
: $self->document->xhr->getUri(true);
// FIXME tmp
$self->document->WebBrowserCallback = $callback;
if (! $location)
throw new Exception('Location needed to activate WebBrowser plugin !');
else {
$self->bind('click', array($location, $callback), array('phpQueryPlugin_WebBrowser', 'hadleClick'));
$self->bind('submit', array($location, $callback), array('phpQueryPlugin_WebBrowser', 'handleSubmit'));
}
}
public static function browser($self, $callback = null, $location = null) {
return $self->WebBrowser($callback, $location);
}
public static function downloadTo($self, $dir = null, $filename = null) {
$url = null;
if ($self->is('a[href]'))
$url = $self->attr('href');
else if ($self->find('a')->length)
$url = $self->find('a')->attr('href');
if ($url) {
$url = resolve_url($self->document->location, $url);
if (! $dir)
$dir = getcwd();
// TODO resolv name from response headers
if (! $filename) {
$matches = null;
preg_match('@/([^/]+)$@', $url, $matches);
$filename = $matches[1];
}
//print $url;
$path = rtrim($dir, '/').'/'.$filename;
phpQuery::debug("Requesting download of $url\n");
// TODO use AJAX instead of file_get_contents
file_put_contents($path, file_get_contents($url));
}
return $self;
}
/**
* Method changing browser location.
* Fires callback registered with WebBrowser(), if any.
* @param $self
* @param $url
* @return unknown_type
*/
public static function location($self, $url = null) {
// TODO if ! $url return actual location ???
$xhr = isset($self->document->xhr)
? $self->document->xhr
: null;
$xhr = phpQuery::ajax(array(
'url' => $url,
), $xhr);
$return = false;
if ($xhr->getLastResponse()->isSuccessful()) {
$return = phpQueryPlugin_WebBrowser::browserReceive($xhr);
if (isset($self->document->WebBrowserCallback))
phpQuery::callbackRun(
$self->document->WebBrowserCallback,
array($return)
);
}
return $return;
}
}
class phpQueryPlugin_WebBrowser {
/**
*
* @param $url
* @param $callback
* @param $param1
* @param $param2
* @param $param3
* @return Zend_Http_Client
*/
public static function browserGet($url, $callback,
$param1 = null, $param2 = null, $param3 = null) {
phpQuery::debug("[WebBrowser] GET: $url");
self::authorizeHost($url);
$xhr = phpQuery::ajax(array(
'type' => 'GET',
'url' => $url,
'dataType' => 'html',
));
$paramStructure = null;
if (func_num_args() > 2) {
$paramStructure = func_get_args();
$paramStructure = array_slice($paramStructure, 2);
}
if ($xhr->getLastResponse()->isSuccessful()) {
phpQuery::callbackRun($callback,
array(self::browserReceive($xhr)->WebBrowser()),
$paramStructure
);
// phpQuery::callbackRun($callback, array(
// self::browserReceive($xhr)//->WebBrowser($callback)
// ));
return $xhr;
} else {
throw new Exception("[WebBrowser] GET request failed; url: $url");
return false;
}
}
/**
*
* @param $url
* @param $data
* @param $callback
* @param $param1
* @param $param2
* @param $param3
* @return Zend_Http_Client
*/
public static function browserPost($url, $data, $callback,
$param1 = null, $param2 = null, $param3 = null) {
self::authorizeHost($url);
$xhr = phpQuery::ajax(array(
'type' => 'POST',
'url' => $url,
'dataType' => 'html',
'data' => $data,
));
$paramStructure = null;
if (func_num_args() > 3) {
$paramStructure = func_get_args();
$paramStructure = array_slice($paramStructure, 3);
}
if ($xhr->getLastResponse()->isSuccessful()) {
phpQuery::callbackRun($callback,
array(self::browserReceive($xhr)->WebBrowser()),
$paramStructure
);
// phpQuery::callbackRun($callback, array(
// self::browserReceive($xhr)//->WebBrowser($callback)
// ));
return $xhr;
} else
return false;
}
/**
*
* @param $ajaxSettings
* @param $callback
* @param $param1
* @param $param2
* @param $param3
* @return Zend_Http_Client
*/
public static function browser($ajaxSettings, $callback,
$param1 = null, $param2 = null, $param3 = null) {
self::authorizeHost($ajaxSettings['url']);
$xhr = phpQuery::ajax(
self::ajaxSettingsPrepare($ajaxSettings)
);
$paramStructure = null;
if (func_num_args() > 2) {
$paramStructure = func_get_args();
$paramStructure = array_slice($paramStructure, 2);
}
if ($xhr->getLastResponse()->isSuccessful()) {
phpQuery::callbackRun($callback,
array(self::browserReceive($xhr)->WebBrowser()),
$paramStructure
);
// phpQuery::callbackRun($callback, array(
// self::browserReceive($xhr)//->WebBrowser($callback)
// ));
return $xhr;
} else
return false;
}
protected static function authorizeHost($url) {
$host = parse_url($url, PHP_URL_HOST);
if ($host)
phpQuery::ajaxAllowHost($host);
}
protected static function ajaxSettingsPrepare($settings) {
unset($settings['success']);
unset($settings['error']);
return $settings;
}
/**
* @param Zend_Http_Client $xhr
*/
public static function browserReceive($xhr) {
phpQuery::debug("[WebBrowser] Received from ".$xhr->getUri(true));
// TODO handle meta redirects
$body = $xhr->getLastResponse()->getBody();
// XXX error ???
if (strpos($body, '<!doctype html>') !== false) {
$body = '<html>'
.str_replace('<!doctype html>', '', $body)
.'</html>';
}
$pq = phpQuery::newDocument($body);
$pq->document->xhr = $xhr;
$pq->document->location = $xhr->getUri(true);
$refresh = $pq->find('meta[http-equiv=refresh]')
->add('meta[http-equiv=Refresh]');
if ($refresh->size()) {
// print htmlspecialchars(var_export($xhr->getCookieJar()->getAllCookies(), true));
// print htmlspecialchars(var_export($xhr->getLastResponse()->getHeader('Set-Cookie'), true));
phpQuery::debug("Meta redirect... '{$refresh->attr('content')}'\n");
// there is a refresh, so get the new url
$content = $refresh->attr('content');
$urlRefresh = substr($content, strpos($content, '=')+1);
$urlRefresh = trim($urlRefresh, '\'"');
// XXX not secure ?!
phpQuery::ajaxAllowURL($urlRefresh);
// $urlRefresh = urldecode($urlRefresh);
// make ajax call, passing last $xhr object to preserve important stuff
$xhr = phpQuery::ajax(array(
'type' => 'GET',
'url' => $urlRefresh,
'dataType' => 'html',
), $xhr);
if ($xhr->getLastResponse()->isSuccessful()) {
// if all is ok, repeat this method...
return call_user_func_array(
array('phpQueryPlugin_WebBrowser', 'browserReceive'), array($xhr)
);
}
} else
return $pq;
}
/**
*
* @param $e
* @param $callback
* @return unknown_type
*/
public static function hadleClick($e, $callback = null) {
$node = phpQuery::pq($e->target);
$type = null;
if ($node->is('a[href]')) {
// TODO document.location
$xhr = isset($node->document->xhr)
? $node->document->xhr
: null;
$xhr = phpQuery::ajax(array(
'url' => resolve_url($e->data[0], $node->attr('href')),
'referer' => $node->document->location,
), $xhr);
if ((! $callback || !($callback instanceof Callback)) && $e->data[1])
$callback = $e->data[1];
if ($xhr->getLastResponse()->isSuccessful() && $callback)
phpQuery::callbackRun($callback, array(
self::browserReceive($xhr)
));
} else if ($node->is(':submit') && $node->parents('form')->size())
$node->parents('form')->trigger('submit', array($e));
}
/**
* Enter description here...
*
* @param unknown_type $e
* @TODO trigger submit for form after form's submit button has a click event
*/
public static function handleSubmit($e, $callback = null) {
$node = phpQuery::pq($e->target);
if (!$node->is('form') || !$node->is('[action]'))
return;
// TODO document.location
$xhr = isset($node->document->xhr)
? $node->document->xhr
: null;
$submit = pq($e->relatedTarget)->is(':submit')
? $e->relatedTarget
// will this work ?
// : $node->find(':submit:first')->get(0);
: $node->find('*:submit:first')->get(0);
$data = array();
foreach($node->serializeArray($submit) as $r)
// XXXt.c maybe $node->not(':submit')->add($sumit) would be better ?
// foreach($node->serializeArray($submit) as $r)
$data[ $r['name'] ] = $r['value'];
$options = array(
'type' => $node->attr('method')
? $node->attr('method')
: 'GET',
'url' => resolve_url($e->data[0], $node->attr('action')),
'data' => $data,
'referer' => $node->document->location,
// 'success' => $e->data[1],
);
if ($node->attr('enctype'))
$options['contentType'] = $node->attr('enctype');
$xhr = phpQuery::ajax($options, $xhr);
if ((! $callback || !($callback instanceof Callback)) && $e->data[1])
$callback = $e->data[1];
if ($xhr->getLastResponse()->isSuccessful() && $callback)
phpQuery::callbackRun($callback, array(
self::browserReceive($xhr)
));
}
}
/**
*
* @param unknown_type $parsed
* @return unknown
* @link http://www.php.net/manual/en/function.parse-url.php
* @author stevenlewis at hotmail dot com
*/
function glue_url($parsed)
{
if (! is_array($parsed)) return false;
$uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '':'//'): '';
$uri .= isset($parsed['user']) ? $parsed['user'].($parsed['pass']? ':'.$parsed['pass']:'').'@':'';
$uri .= isset($parsed['host']) ? $parsed['host'] : '';
$uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
if(isset($parsed['path']))
{
$uri .= (substr($parsed['path'],0,1) == '/')?$parsed['path']:'/'.$parsed['path'];
}
$uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
$uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
return $uri;
}
/**
* Enter description here...
*
* @param unknown_type $base
* @param unknown_type $url
* @return unknown
* @author adrian-php at sixfingeredman dot net
*/
function resolve_url($base, $url) {
if (!strlen($base)) return $url;
// Step 2
if (!strlen($url)) return $base;
// Step 3
if (preg_match('!^[a-z]+:!i', $url)) return $url;
$base = parse_url($base);
if ($url{0} == "#") {
// Step 2 (fragment)
$base['fragment'] = substr($url, 1);
return unparse_url($base);
}
unset($base['fragment']);
unset($base['query']);
if (substr($url, 0, 2) == "//") {
// Step 4
return unparse_url(array(
'scheme'=>$base['scheme'],
'path'=>substr($url,2),
));
} else if ($url{0} == "/") {
// Step 5
$base['path'] = $url;
} else {
// Step 6
$path = explode('/', $base['path']);
$url_path = explode('/', $url);
// Step 6a: drop file from base
array_pop($path);
// Step 6b, 6c, 6e: append url while removing "." and ".." from
// the directory portion
$end = array_pop($url_path);
foreach ($url_path as $segment) {
if ($segment == '.') {
// skip
} else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
array_pop($path);
} else {
$path[] = $segment;
}
}
// Step 6d, 6f: remove "." and ".." from file portion
if ($end == '.') {
$path[] = '';
} else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
$path[sizeof($path)-1] = '';
} else {
$path[] = $end;
}
// Step 6h
$base['path'] = join('/', $path);
}
// Step 7
return glue_url($base);
}