modrestsockclient.class.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once dirname(__FILE__) . '/modrestclient.class.php';
  11. /**
  12. *
  13. * @deprecated To be removed in 2.3. See modRest instead.
  14. *
  15. * @package modx
  16. * @subpackage rest
  17. */
  18. class modRestSockClient extends modRestClient {
  19. /**
  20. * Extends modRestClient::request to provide socket-specific request
  21. * handling
  22. *
  23. * @todo Ensure this strips whitespace that prevents this class from working
  24. *
  25. * @param string $host The host of the REST server.
  26. * @param string $path The path to request to on the REST server.
  27. * @param string $method The HTTP method to use for the request. May be GET,
  28. * PUT or POST.
  29. * @param array $params An array of parameters to send with the request.
  30. * @param array $options An array of options to pass to the request.
  31. * @return modRestResponse The response object.
  32. */
  33. public function request($host,$path,$method = 'GET',array $params = array(),array $options = array()) {
  34. $method = strtoupper($method);
  35. $purl = parse_url($host);
  36. $host = $purl['host'];
  37. $purl['path'] = !empty($purl['path']) ? $purl['path'] : $this->config[modRestClient::OPT_PATH];
  38. $purl['port'] = !empty($purl['port']) ? $purl['port'] : $this->config[modRestClient::OPT_PORT];
  39. $sock = @fsockopen($purl['host'], $purl['port'],$errno,$errstr,30);
  40. /* setup params */
  41. $q = http_build_query($params);
  42. if ($method == "GET") {
  43. $path .= "?" . $q;
  44. }
  45. $ip = $this->modx->request->getClientIp();
  46. $ip = $ip['ip'];
  47. $out = $method." ".$purl['path']."/$path ".$_SERVER['SERVER_PROTOCOL']."\r\n"
  48. ."Host: $host\r\n"
  49. ."User-Agent: ".$this->config[modRestClient::OPT_USERAGENT]."\r\n"
  50. ."Content-type: text/xml; charset=UTF-8\r\n"
  51. ."Accept: */*\r\n"
  52. ."Accept-Language: en-us,en;q=0.5\r\n"
  53. ."Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n"
  54. ."Accept-Encoding: gzip, deflate, compress;q=0.9\r\n"
  55. /*."Keep-Alive: 300\r\n" */
  56. ."Referer: ".$ip."\r\n"
  57. ."Connection: Close\r\n\r\n";
  58. fwrite($sock,$out);
  59. if ($method == 'POST') {
  60. fwrite($sock, "Content-length: ".strlen($q)."\r\n");
  61. fwrite($sock, $q);
  62. }
  63. $response = '';
  64. $i = 0;
  65. while ($line = fread($sock, 4096)) {
  66. if ($i > 20) break;
  67. $response .= $line;
  68. $i++;
  69. }
  70. fclose($sock);
  71. list($header,$response) = explode('<?xml',$response);
  72. $response = '<?xml'.$response;
  73. /* strip junk at end of string */
  74. $response = strrev($response);
  75. $response = strrev(substr($response,strpos($response,'>')));
  76. /* commented out for debugging */
  77. //echo '<textarea cols="180" rows="50">'.$response.'</textarea>'; die();
  78. //echo '<pre>'.htmlentities($xml->asXml()).'</pre>'; die();
  79. return $response;
  80. }
  81. }