PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/v1/mod_semanticweb/arc/ARC2_Reader.php

https://code.google.com/p/goodrelations-for-joomla/
PHP | 289 lines | 231 code | 32 blank | 26 comment | 54 complexity | 529ed077d228f8675e5f7f663f5716b1 MD5 | raw file
  1. <?php
  2. /*
  3. homepage: http://arc.semsol.org/
  4. license: http://arc.semsol.org/license
  5. class: ARC2 Web Reader
  6. author: Benjamin Nowack
  7. version: 2009-02-18 (Tweak: Improved error message on empty file:// base)
  8. */
  9. ARC2::inc('Class');
  10. class ARC2_Reader extends ARC2_Class {
  11. function __construct($a = '', &$caller) {
  12. parent::__construct($a, $caller);
  13. }
  14. function ARC2_Reader($a = '', &$caller) {
  15. $this->__construct($a, $caller);
  16. }
  17. function __init() {/* inc_path, proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects */
  18. parent::__init();
  19. $this->http_method = $this->v('http_method', 'GET', $this->a);
  20. $this->message_body = $this->v('message_body', '', $this->a);;
  21. $this->http_accept_header = $this->v('http_accept_header', 'Accept: application/rdf+xml; q=0.9, */*; q=0.1', $this->a);
  22. $this->http_user_agent_header = $this->v('http_user_agent_header', 'User-Agent: ARC Reader (http://arc.semsol.org/)', $this->a);
  23. $this->http_custom_headers = $this->v('http_custom_headers', '', $this->a);
  24. $this->max_redirects = $this->v('max_redirects', 3, $this->a);
  25. $this->format = $this->v('format', false, $this->a);
  26. $this->redirects = array();
  27. $this->stream_id = '';
  28. $this->timeout = $this->v('reader_timeout', 0, $this->a);;
  29. }
  30. /* */
  31. function setHTTPMethod($v) {
  32. $this->http_method = $v;
  33. }
  34. function setMessageBody($v) {
  35. $this->message_body = $v;
  36. }
  37. function setAcceptHeader($v) {
  38. $this->http_accept_header = $v;
  39. }
  40. function setCustomHeaders($v) {
  41. $this->http_custom_headers = $v;
  42. }
  43. function addCustomHeaders($v) {
  44. if ($this->http_custom_headers) $this->http_custom_headers .= "\r\n";
  45. $this->http_custom_headers .= $v;
  46. }
  47. /* */
  48. function activate($path, $data = '', $ping_only = 0, $timeout = 0) {
  49. $this->setCredentials($path);
  50. $this->ping_only = $ping_only;
  51. if ($timeout) $this->timeout = $timeout;
  52. $id = md5($path . ' ' . $data);
  53. if ($this->stream_id != $id) {
  54. $this->stream_id = $id;
  55. $this->base = $this->calcBase($path);
  56. $this->uri = $this->calcURI($path, $this->base);
  57. $this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base, $ping_only);
  58. if ($this->stream && !$this->ping_only) {
  59. $this->getFormat();
  60. }
  61. }
  62. }
  63. function setCredentials($path) {
  64. if ($creds = $this->v('arc_reader_credentials', array(), $this->a)) {
  65. foreach ($creds as $pattern => $cred) {
  66. $regex = '/' . preg_replace('/([\:\/\.\?])/', '\\\\\1', $pattern) . '/';
  67. if (preg_match($regex, $path)) {
  68. $this->setCustomHeaders('Authorization: Basic ' . base64_encode($cred));
  69. }
  70. }
  71. }
  72. }
  73. /* */
  74. function useProxy($url) {
  75. if (!$this->v1('proxy_host', 0, $this->a)) {
  76. return false;
  77. }
  78. $skips = $this->v1('proxy_skip', array(), $this->a);
  79. foreach ($skips as $skip) {
  80. if (strpos($url, $skip) !== false) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. /* */
  87. function createStream($path, $data = '') {
  88. $this->base = $this->calcBase($path);
  89. $this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base);
  90. }
  91. function getDataStream($data) {
  92. return array('type' => 'data', 'pos' => 0, 'headers' => array(), 'size' => strlen($data), 'data' => $data, 'buffer' => '');
  93. }
  94. function getSocketStream($url) {
  95. if ($url == 'file://') {
  96. return $this->addError('Error: file does not exists or is not accessible');
  97. }
  98. $parts = parse_url($url);
  99. $mappings = array('file' => 'File', 'http' => 'HTTP', 'https' => 'HTTP');
  100. if ($scheme = $this->v(strtolower($parts['scheme']), '', $mappings)) {
  101. return $this->m('get' . $scheme . 'Socket', $url, $this->getDataStream(''));
  102. }
  103. }
  104. function getFileSocket($url) {
  105. $parts = parse_url($url);
  106. $s = file_exists($parts['path']) ? @fopen($parts['path'], 'rb') : false;
  107. if (!$s) {
  108. return $this->addError('Socket error: Could not open "' . $parts['path'] . '"');
  109. }
  110. return array('type' => 'socket', 'socket' =>& $s, 'headers' => array(), 'pos' => 0, 'size' => filesize($parts['path']), 'buffer' => '');
  111. }
  112. function getHTTPSocket($url, $redirs = 0) {
  113. $parts = parse_url($url);
  114. if (!isset($parts['scheme'])) {
  115. return $this->addError('Socket error: No supported URI scheme detected.');
  116. }
  117. $parts['port'] = ($parts['scheme'] == 'https') ? $this->v1('port', 443, $parts) : $this->v1('port', 80, $parts);
  118. $nl = "\r\n";
  119. $http_mthd = strtoupper($this->http_method);
  120. if ($this->v1('user', 0, $parts) || $this->useProxy($url)) {
  121. $h_code = $http_mthd . ' ' . $url;
  122. }
  123. else {
  124. $h_code = $http_mthd . ' ' . $this->v1('path', '/', $parts) . (($v = $this->v1('query', 0, $parts)) ? '?' . $v : '') . (($v = $this->v1('fragment', 0, $parts)) ? '#' . $v : '');
  125. }
  126. $h_code .= ' HTTP/1.0' . $nl.
  127. 'Host: ' . $parts['host'] . $nl.
  128. (($v = $this->http_accept_header) ? $v . $nl : '') .
  129. (($v = $this->http_user_agent_header) && !preg_match('/User\-Agent\:/', $this->http_custom_headers) ? $v . $nl : '') .
  130. (($http_mthd == 'POST') ? 'Content-Length: ' . strlen($this->message_body) . $nl : '') .
  131. ($this->http_custom_headers ? trim($this->http_custom_headers) . $nl : '') .
  132. $nl .
  133. '';
  134. /* post body */
  135. if ($http_mthd == 'POST') {
  136. $h_code .= $this->message_body . $nl;
  137. }
  138. /* connect */
  139. if ($this->useProxy($url)) {
  140. $s = @fsockopen($this->a['proxy_host'], $this->a['proxy_port']);
  141. }
  142. elseif ($parts['scheme'] == 'https') {
  143. $s = @fsockopen('ssl://' . $parts['host'], $parts['port']);
  144. }
  145. elseif ($parts['scheme'] == 'http') {
  146. $s = @fsockopen($parts['host'], $parts['port']);
  147. }
  148. if (!$s) {
  149. return $this->addError('Socket error: Could not connect to "' . $url . '" (proxy: ' . ($this->useProxy($url) ? '1' : '0') . ')');
  150. }
  151. /* request */
  152. fwrite($s, $h_code);
  153. /* timeout */
  154. if ($this->timeout) stream_set_timeout($s, $this->timeout);
  155. /* response headers */
  156. $h = array();
  157. if (!$this->ping_only) {
  158. do {
  159. $line = trim(fgets($s, 256));
  160. if (preg_match("/^HTTP[^\s]+\s+([0-9]{1})([0-9]{2})(.*)$/i", $line, $m)) {/* response code */
  161. $error = in_array($m[1], array('4', '5')) ? $m[1] . $m[2] . ' ' . $m[3] : '';
  162. $error = ($m[1].$m[2] == '304') ? '304 '.$m[3] : $error;
  163. $h['response-code'] = $m[1] . $m[2];
  164. $h['error'] = $error;
  165. $h['redirect'] = ($m[1] == '3') ? true : false;
  166. }
  167. elseif (preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $m)) {/* header */
  168. $h[strtolower($m[1])] = trim($m[2]);
  169. }
  170. } while(!feof($s) && $line);
  171. $h['format'] = strtolower(preg_replace('/^([^\s]+).*$/', '\\1', $this->v('content-type', '', $h)));
  172. $h['encoding'] = preg_match('/(utf\-8|iso\-8859\-1|us\-ascii)/', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : '';
  173. $h['encoding'] = preg_match('/charset=\s*([^\s]+)/si', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : $h['encoding'];
  174. /* result */
  175. if ($v = $this->v('error', 0, $h)) {
  176. //return $this->addError($error);
  177. return $this->addError($error . ' "' . (!feof($s) ? trim(strip_tags(fread($s, 64))) . '..."' : ''));
  178. }
  179. if ($this->v('redirect', 0, $h) && ($new_url = $this->v1('location', 0, $h))) {
  180. fclose($s);
  181. $this->redirects[$url] = $new_url;
  182. $this->base = $new_url;
  183. if ($redirs > $this->max_redirects) {
  184. return $this->addError('Max numbers of redirects exceeded.');
  185. }
  186. return $this->getHTTPSocket($new_url, $redirs+1);
  187. }
  188. }
  189. return array('type' => 'socket', 'url' => $url, 'socket' =>& $s, 'headers' => $h, 'pos' => 0, 'size' => $this->v('content-length', 0, $h), 'buffer' => '');
  190. }
  191. function readStream($buffer_xml = true, $d_size = 1024) {
  192. //if (!$s = $this->v('stream')) return '';
  193. if (!$s = $this->v('stream')) return $this->addError('missing stream in "readStream" ' . $this->uri);
  194. $s_type = $this->v('type', '', $s);
  195. $r = $s['buffer'];
  196. $s['buffer'] = '';
  197. if ($s['size']) $d_size = min($d_size, $s['size'] - $s['pos']);
  198. /* data */
  199. if ($s_type == 'data') {
  200. $d = ($d_size > 0) ? substr($s['data'], $s['pos'], $d_size) : '';
  201. }
  202. /* socket */
  203. elseif ($s_type == 'socket') {
  204. $d = ($d_size > 0) && !feof($s['socket']) ? fread($s['socket'], $d_size) : '';
  205. }
  206. $eof = $d ? false : true;
  207. /* chunked despite HTTP 1.0 request */
  208. if (isset($s['headers']) && isset($s['headers']['transfer-encoding']) && ($s['headers']['transfer-encoding'] == 'chunked')) {
  209. $d = preg_replace('/(^|[\r\n]+)[0-9a-f]{1,4}[\r\n]+/', '', $d);
  210. }
  211. $s['pos'] += strlen($d);
  212. if ($buffer_xml) {/* stop after last closing xml tag (if available) */
  213. if (preg_match('/^(.*\>)([^\>]*)$/s', $d, $m)) {
  214. $d = $m[1];
  215. $s['buffer'] = $m[2];
  216. }
  217. elseif (!$eof) {
  218. $s['buffer'] = $r . $d;
  219. $this->stream = $s;
  220. return $this->readStream(true, $d_size);
  221. }
  222. }
  223. $this->stream = $s;
  224. return $r . $d;
  225. }
  226. function closeStream() {
  227. if (isset($this->stream)) {
  228. if ($this->v('type', 0, $this->stream) == 'socket') {
  229. @fclose($this->stream['socket']);
  230. }
  231. unset($this->stream);
  232. }
  233. }
  234. /* */
  235. function getFormat() {
  236. if (!$this->format) {
  237. if (!$this->v('stream')) {
  238. return $this->addError('missing stream in "getFormat"');
  239. }
  240. $v = $this->readStream(false);
  241. $mtype = $this->v('format', '', $this->stream['headers']);
  242. $this->stream['buffer'] = $v . $this->stream['buffer'];
  243. $ext = preg_match('/\.([^\.]+)$/', $this->uri, $m) ? $m[1] : '';
  244. $this->format = ARC2::getFormat($v, $mtype, $ext);
  245. }
  246. return $this->format;
  247. }
  248. /* */
  249. function getEncoding($default = 'UTF-8') {
  250. return $this->v1('encoding', $default, $this->stream['headers']);
  251. }
  252. function getRedirects() {
  253. return $this->redirects;
  254. }
  255. /* */
  256. }