|
1
|
|
|
` |
|
|
|
|
|
|
2
|
|
|
<?php |
|
3
|
|
|
use PhpQuery\PhpQueryObject; |
|
4
|
|
|
use PhpQuery\PhpQuery; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* jQuery Server Plugin |
|
8
|
|
|
* |
|
9
|
|
|
* Backend class using PhpQuery. |
|
10
|
|
|
* |
|
11
|
|
|
* @version 0.5.1 |
|
12
|
|
|
* @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com> |
|
13
|
|
|
* @link http://code.google.com/p/phpquery/wiki/jQueryServer |
|
14
|
|
|
* @link http://code.google.com/p/phpquery/ |
|
15
|
|
|
* @todo local files support (safe...) |
|
16
|
|
|
* @todo respond with proper HTTP code |
|
17
|
|
|
* @todo persistant thread support (with timeout...) |
|
18
|
|
|
* @todo 2.0: JSON RPC - Zend_Json_Server |
|
19
|
|
|
* @todo 2.0: XML RPC ? |
|
20
|
|
|
*/ |
|
21
|
|
|
class jQueryServer { |
|
|
|
|
|
|
22
|
|
|
public $config = array( |
|
23
|
|
|
'allowedRefererHosts' => array( |
|
24
|
|
|
'.' |
|
25
|
|
|
), |
|
26
|
|
|
'refererMustMatch' => true, |
|
27
|
|
|
); |
|
28
|
|
|
public $calls = null; |
|
29
|
|
|
public $options = null; |
|
30
|
|
|
public $allowedHosts = null; |
|
31
|
|
|
function __construct($data) { |
|
|
|
|
|
|
32
|
|
|
$pq = null; |
|
|
|
|
|
|
33
|
|
|
include_once(dirname(__FILE__) . '/../PhpQuery/PhpQuery.php'); |
|
34
|
|
|
if (file_exists(dirname(__FILE__) . '/jQueryServer.config.php')) { |
|
35
|
|
|
include_once(dirname(__FILE__) . '/jQueryServer.config.php'); |
|
36
|
|
|
if ($jQueryServerConfig) |
|
37
|
|
|
$this->config = array_merge_recursive($this->config, $jQueryServerConfig); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
if ($this->config['refererMustMatch']) { |
|
40
|
|
|
foreach ($this->config['allowedRefererHosts'] as $i => $host) |
|
41
|
|
|
if ($host == '.') |
|
42
|
|
|
$this->config['allowedRefererHosts'][$i] = $_SERVER['HTTP_HOST']; |
|
43
|
|
|
$referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); |
|
44
|
|
|
$authorized = $referer |
|
|
|
|
|
|
45
|
|
|
&& in_array($referer, $this->config['allowedRefererHosts']); |
|
46
|
|
|
if (!$authorized) { |
|
47
|
|
|
throw new \Exception("Host '{$_SERVER['HTTP_REFERER']}' not authorized to make requests."); |
|
48
|
|
|
return; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
// PhpQueryClass::$debug = true; |
|
|
|
|
|
|
52
|
|
|
// if (! function_exists('json_decode')) { |
|
|
|
|
|
|
53
|
|
|
// include_once(dirname(__FILE__).'/JSON.php'); |
|
|
|
|
|
|
54
|
|
|
// $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|
|
|
|
|
|
55
|
|
|
// } |
|
56
|
|
|
// $data = $this->jsonDecode($data); |
|
|
|
|
|
|
57
|
|
|
$data = PhpQuery::parseJSON($data); |
|
58
|
|
|
// load document (required for first $data element) |
|
59
|
|
|
if (is_array($data[0]) && isset($data[0]['url'])) { |
|
60
|
|
|
$this->options = $data[0]; |
|
61
|
|
|
$ajax = $this->options; |
|
62
|
|
|
$this->calls = array_slice($data, 1); |
|
63
|
|
|
$ajax['success'] = array( |
|
64
|
|
|
$this, |
|
65
|
|
|
'success' |
|
66
|
|
|
); |
|
67
|
|
|
PhpQuery::ajax($ajax); |
|
68
|
|
|
} |
|
69
|
|
|
else { |
|
70
|
|
|
throw new \Exception("URL needed to download content"); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
public function success($response) { |
|
74
|
|
|
$pq = PhpQuery::newDocument($response); |
|
75
|
|
|
foreach ($this->calls as $k => $r) { |
|
76
|
|
|
// check if method exists |
|
77
|
|
|
if (!method_exists(get_class($pq), $r['method'])) { |
|
78
|
|
|
throw new \Exception("Method '{$r['method']}' not implemented in PhpQuery, sorry..."); |
|
79
|
|
|
// execute method |
|
80
|
|
|
} |
|
81
|
|
|
else { |
|
82
|
|
|
$pq = call_user_func_array(array( |
|
83
|
|
|
$pq, |
|
84
|
|
|
$r['method'] |
|
85
|
|
|
), $r['arguments']); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
if (!isset($this->options['dataType'])) |
|
89
|
|
|
$this->options['dataType'] = ''; |
|
90
|
|
|
switch (strtolower($this->options['dataType'])) { |
|
91
|
|
|
case 'json': |
|
92
|
|
|
if ($pq instanceof PhpQueryObject) { |
|
93
|
|
|
$results = array(); |
|
94
|
|
|
foreach ($pq as $node) |
|
95
|
|
|
$results[] = pq($node)->htmlOuter(); |
|
96
|
|
|
print PhpQuery::toJSON($results); |
|
97
|
|
|
} |
|
98
|
|
|
else { |
|
99
|
|
|
print PhpQuery::toJSON($pq); |
|
100
|
|
|
} |
|
101
|
|
|
break; |
|
102
|
|
|
default: |
|
103
|
|
|
print $pq; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
// output results |
|
106
|
|
|
} |
|
107
|
|
|
// public function jsonEncode($data) { |
|
|
|
|
|
|
108
|
|
|
// return function_exists('json_encode') |
|
|
|
|
|
|
109
|
|
|
// ? json_encode($data) |
|
|
|
|
|
|
110
|
|
|
// : $this->json->encode($data); |
|
|
|
|
|
|
111
|
|
|
// } |
|
112
|
|
|
// public function jsonDecode($data) { |
|
|
|
|
|
|
113
|
|
|
// return function_exists('json_decode') |
|
|
|
|
|
|
114
|
|
|
// ? json_decode($data, true) |
|
|
|
|
|
|
115
|
|
|
// : $this->json->decode($data); |
|
|
|
|
|
|
116
|
|
|
// } |
|
117
|
|
|
} |
|
118
|
|
|
new jQueryServer($_POST['data']); |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.