123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- `<?php
- class jQueryServer {
- public $config = array(
- 'allowedRefererHosts' => array('.'),
- 'refererMustMatch' => true,
- );
- public $calls = null;
- public $options = null;
- public $allowedHosts = null;
- function __construct($data) {
- $pq = null;
- include_once(dirname(__FILE__).'/../phpQuery/phpQuery.php');
- if (file_exists(dirname(__FILE__).'/jQueryServer.config.php')) {
- include_once(dirname(__FILE__).'/jQueryServer.config.php');
- if ($jQueryServerConfig)
- $this->config = array_merge_recursive($this->config, $jQueryServerConfig);
- }
- if ($this->config['refererMustMatch']) {
- foreach($this->config['allowedRefererHosts'] as $i => $host)
- if ($host == '.')
- $this->config['allowedRefererHosts'][$i] = $_SERVER['HTTP_HOST'];
- $referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
- $authorized = $referer
- && in_array($referer, $this->config['allowedRefererHosts']);
- if (! $authorized) {
- throw new Exception("Host '{$_SERVER['HTTP_REFERER']}' not authorized to make requests.");
- return;
- }
- }
- $data = phpQuery::parseJSON($data);
-
- if (is_array($data[0]) && isset($data[0]['url'])) {
- $this->options = $data[0];
- $ajax = $this->options;
- $this->calls = array_slice($data, 1);
- $ajax['success'] = array($this, 'success');
- phpQuery::ajax($ajax);
- } else {
- throw new Exception("URL needed to download content");
- }
- }
- public function success($response) {
- $pq = phpQuery::newDocument($response);
- foreach($this->calls as $k => $r) {
-
- if (! method_exists(get_class($pq), $r['method'])) {
- throw new Exception("Method '{$r['method']}' not implemented in phpQuery, sorry...");
-
- } else {
- $pq = call_user_func_array(
- array($pq, $r['method']),
- $r['arguments']
- );
- }
- }
- if (! isset($this->options['dataType']))
- $this->options['dataType'] = '';
- switch(strtolower($this->options['dataType'])) {
- case 'json':
- if ( $pq instanceof PHPQUERYOBJECT ) {
- $results = array();
- foreach($pq as $node)
- $results[] = pq($node)->htmlOuter();
- print phpQuery::toJSON($results);
- } else {
- print phpQuery::toJSON($pq);
- }
- break;
- default:
- print $pq;
- }
-
- }
- }
- new jQueryServer($_POST['data']);
- ?>
|