/libraries/openid/Auth/Yadis/PlainHTTPFetcher.php

https://github.com/shafiqissani/Jewelery-Ecommerce- · PHP · 251 lines · 159 code · 55 blank · 37 comment · 34 complexity · 1434dc5a00276fa18394fd77e150c70a MD5 · raw file

  1. <?php
  2. /**
  3. * This module contains the plain non-curl HTTP fetcher
  4. * implementation.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: See the COPYING file included in this distribution.
  9. *
  10. * @package OpenID
  11. * @author JanRain, Inc. <openid@janrain.com>
  12. * @copyright 2005-2008 Janrain, Inc.
  13. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  14. */
  15. /**
  16. * Interface import
  17. */
  18. require_once "Auth/Yadis/HTTPFetcher.php";
  19. /**
  20. * This class implements a plain, hand-built socket-based fetcher
  21. * which will be used in the event that CURL is unavailable.
  22. *
  23. * @package OpenID
  24. */
  25. class Auth_Yadis_PlainHTTPFetcher extends Auth_Yadis_HTTPFetcher {
  26. /**
  27. * Does this fetcher support SSL URLs?
  28. */
  29. function supportsSSL()
  30. {
  31. return function_exists('openssl_open');
  32. }
  33. function get($url, $extra_headers = null)
  34. {
  35. if (!$this->canFetchURL($url)) {
  36. return null;
  37. }
  38. $redir = true;
  39. $stop = time() + $this->timeout;
  40. $off = $this->timeout;
  41. while ($redir && ($off > 0)) {
  42. $parts = parse_url($url);
  43. $specify_port = true;
  44. // Set a default port.
  45. if (!array_key_exists('port', $parts)) {
  46. $specify_port = false;
  47. if ($parts['scheme'] == 'http') {
  48. $parts['port'] = 80;
  49. } elseif ($parts['scheme'] == 'https') {
  50. $parts['port'] = 443;
  51. } else {
  52. return null;
  53. }
  54. }
  55. if (!array_key_exists('path', $parts)) {
  56. $parts['path'] = '/';
  57. }
  58. $host = $parts['host'];
  59. if ($parts['scheme'] == 'https') {
  60. $host = 'ssl://' . $host;
  61. }
  62. $user_agent = Auth_OpenID_USER_AGENT;
  63. $headers = array(
  64. "GET ".$parts['path'].
  65. (array_key_exists('query', $parts) ?
  66. "?".$parts['query'] : "").
  67. " HTTP/1.0",
  68. "User-Agent: $user_agent",
  69. "Host: ".$parts['host'].
  70. ($specify_port ? ":".$parts['port'] : ""),
  71. "Range: 0-".
  72. (1024*Auth_OpenID_FETCHER_MAX_RESPONSE_KB),
  73. "Port: ".$parts['port']);
  74. $errno = 0;
  75. $errstr = '';
  76. if ($extra_headers) {
  77. foreach ($extra_headers as $h) {
  78. $headers[] = $h;
  79. }
  80. }
  81. @$sock = fsockopen($host, $parts['port'], $errno, $errstr,
  82. $this->timeout);
  83. if ($sock === false) {
  84. return false;
  85. }
  86. stream_set_timeout($sock, $this->timeout);
  87. fputs($sock, implode("\r\n", $headers) . "\r\n\r\n");
  88. $data = "";
  89. $kilobytes = 0;
  90. while (!feof($sock) &&
  91. $kilobytes < Auth_OpenID_FETCHER_MAX_RESPONSE_KB ) {
  92. $data .= fgets($sock, 1024);
  93. $kilobytes += 1;
  94. }
  95. fclose($sock);
  96. // Split response into header and body sections
  97. list($headers, $body) = explode("\r\n\r\n", $data, 2);
  98. $headers = explode("\r\n", $headers);
  99. $http_code = explode(" ", $headers[0]);
  100. $code = $http_code[1];
  101. if (in_array($code, array('301', '302'))) {
  102. $url = $this->_findRedirect($headers);
  103. $redir = true;
  104. } else {
  105. $redir = false;
  106. }
  107. $off = $stop - time();
  108. }
  109. $new_headers = array();
  110. foreach ($headers as $header) {
  111. if (preg_match("/:/", $header)) {
  112. $parts = explode(": ", $header, 2);
  113. if (count($parts) == 2) {
  114. list($name, $value) = $parts;
  115. $new_headers[$name] = $value;
  116. }
  117. }
  118. }
  119. return new Auth_Yadis_HTTPResponse($url, $code, $new_headers, $body);
  120. }
  121. function post($url, $body, $extra_headers = null)
  122. {
  123. if (!$this->canFetchURL($url)) {
  124. return null;
  125. }
  126. $parts = parse_url($url);
  127. $headers = array();
  128. $post_path = $parts['path'];
  129. if (isset($parts['query'])) {
  130. $post_path .= '?' . $parts['query'];
  131. }
  132. $headers[] = "POST ".$post_path." HTTP/1.0";
  133. $headers[] = "Host: " . $parts['host'];
  134. $headers[] = "Content-type: application/x-www-form-urlencoded";
  135. $headers[] = "Content-length: " . strval(strlen($body));
  136. if ($extra_headers &&
  137. is_array($extra_headers)) {
  138. $headers = array_merge($headers, $extra_headers);
  139. }
  140. // Join all headers together.
  141. $all_headers = implode("\r\n", $headers);
  142. // Add headers, two newlines, and request body.
  143. $request = $all_headers . "\r\n\r\n" . $body;
  144. // Set a default port.
  145. if (!array_key_exists('port', $parts)) {
  146. if ($parts['scheme'] == 'http') {
  147. $parts['port'] = 80;
  148. } elseif ($parts['scheme'] == 'https') {
  149. $parts['port'] = 443;
  150. } else {
  151. return null;
  152. }
  153. }
  154. if ($parts['scheme'] == 'https') {
  155. $parts['host'] = sprintf("ssl://%s", $parts['host']);
  156. }
  157. // Connect to the remote server.
  158. $errno = 0;
  159. $errstr = '';
  160. $sock = fsockopen($parts['host'], $parts['port'], $errno, $errstr,
  161. $this->timeout);
  162. if ($sock === false) {
  163. return null;
  164. }
  165. stream_set_timeout($sock, $this->timeout);
  166. // Write the POST request.
  167. fputs($sock, $request);
  168. // Get the response from the server.
  169. $response = "";
  170. while (!feof($sock)) {
  171. if ($data = fgets($sock, 128)) {
  172. $response .= $data;
  173. } else {
  174. break;
  175. }
  176. }
  177. // Split the request into headers and body.
  178. list($headers, $response_body) = explode("\r\n\r\n", $response, 2);
  179. $headers = explode("\r\n", $headers);
  180. // Expect the first line of the headers data to be something
  181. // like HTTP/1.1 200 OK. Split the line on spaces and take
  182. // the second token, which should be the return code.
  183. $http_code = explode(" ", $headers[0]);
  184. $code = $http_code[1];
  185. $new_headers = array();
  186. foreach ($headers as $header) {
  187. if (preg_match("/:/", $header)) {
  188. list($name, $value) = explode(": ", $header, 2);
  189. $new_headers[$name] = $value;
  190. }
  191. }
  192. return new Auth_Yadis_HTTPResponse($url, $code,
  193. $new_headers, $response_body);
  194. }
  195. }
  196. ?>