jQueryServer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. `<?php
  2. /**
  3. * jQuery Server Plugin
  4. *
  5. * Backend class using phpQuery.
  6. *
  7. * @version 0.5.1
  8. * @author Tobiasz Cudnik <tobiasz.cudnik/gmail.com>
  9. * @link http://code.google.com/p/phpquery/wiki/jQueryServer
  10. * @link http://code.google.com/p/phpquery/
  11. * @todo local files support (safe...)
  12. * @todo respond with proper HTTP code
  13. * @todo persistant thread support (with timeout...)
  14. * @todo 2.0: JSON RPC - Zend_Json_Server
  15. * @todo 2.0: XML RPC ?
  16. */
  17. class jQueryServer {
  18. public $config = array(
  19. 'allowedRefererHosts' => array('.'),
  20. 'refererMustMatch' => true,
  21. );
  22. public $calls = null;
  23. public $options = null;
  24. public $allowedHosts = null;
  25. function __construct($data) {
  26. $pq = null;
  27. include_once(dirname(__FILE__).'/../phpQuery/phpQuery.php');
  28. if (file_exists(dirname(__FILE__).'/jQueryServer.config.php')) {
  29. include_once(dirname(__FILE__).'/jQueryServer.config.php');
  30. if ($jQueryServerConfig)
  31. $this->config = array_merge_recursive($this->config, $jQueryServerConfig);
  32. }
  33. if ($this->config['refererMustMatch']) {
  34. foreach($this->config['allowedRefererHosts'] as $i => $host)
  35. if ($host == '.')
  36. $this->config['allowedRefererHosts'][$i] = $_SERVER['HTTP_HOST'];
  37. $referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
  38. $authorized = $referer
  39. && in_array($referer, $this->config['allowedRefererHosts']);
  40. if (! $authorized) {
  41. throw new Exception("Host '{$_SERVER['HTTP_REFERER']}' not authorized to make requests.");
  42. return;
  43. }
  44. }
  45. // phpQueryClass::$debug = true;
  46. // if (! function_exists('json_decode')) {
  47. // include_once(dirname(__FILE__).'/JSON.php');
  48. // $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  49. // }
  50. // $data = $this->jsonDecode($data);
  51. $data = phpQuery::parseJSON($data);
  52. // load document (required for first $data element)
  53. if (is_array($data[0]) && isset($data[0]['url'])) {
  54. $this->options = $data[0];
  55. $ajax = $this->options;
  56. $this->calls = array_slice($data, 1);
  57. $ajax['success'] = array($this, 'success');
  58. phpQuery::ajax($ajax);
  59. } else {
  60. throw new Exception("URL needed to download content");
  61. }
  62. }
  63. public function success($response) {
  64. $pq = phpQuery::newDocument($response);
  65. foreach($this->calls as $k => $r) {
  66. // check if method exists
  67. if (! method_exists(get_class($pq), $r['method'])) {
  68. throw new Exception("Method '{$r['method']}' not implemented in phpQuery, sorry...");
  69. // execute method
  70. } else {
  71. $pq = call_user_func_array(
  72. array($pq, $r['method']),
  73. $r['arguments']
  74. );
  75. }
  76. }
  77. if (! isset($this->options['dataType']))
  78. $this->options['dataType'] = '';
  79. switch(strtolower($this->options['dataType'])) {
  80. case 'json':
  81. if ( $pq instanceof PHPQUERYOBJECT ) {
  82. $results = array();
  83. foreach($pq as $node)
  84. $results[] = pq($node)->htmlOuter();
  85. print phpQuery::toJSON($results);
  86. } else {
  87. print phpQuery::toJSON($pq);
  88. }
  89. break;
  90. default:
  91. print $pq;
  92. }
  93. // output results
  94. }
  95. // public function jsonEncode($data) {
  96. // return function_exists('json_encode')
  97. // ? json_encode($data)
  98. // : $this->json->encode($data);
  99. // }
  100. // public function jsonDecode($data) {
  101. // return function_exists('json_decode')
  102. // ? json_decode($data, true)
  103. // : $this->json->decode($data);
  104. // }
  105. }
  106. new jQueryServer($_POST['data']);
  107. ?>