PageRenderTime 410ms CodeModel.GetById 80ms app.highlight 267ms RepoModel.GetById 56ms app.codeStats 1ms

/arc/ARC2_Reader.php

https://github.com/damz/foafssl-drupal
PHP | 354 lines | 287 code | 32 blank | 35 comment | 65 complexity | 1f76d3c03723da4b6f4e3c3ecf777e26 MD5 | raw file
  1<?php
  2/*
  3homepage: http://arc.semsol.org/
  4license:  http://arc.semsol.org/license
  5
  6class:    ARC2 Web Reader
  7author:   Benjamin Nowack
  8version:  2009-08-17
  9*/
 10
 11ARC2::inc('Class');
 12
 13class ARC2_Reader extends ARC2_Class {
 14
 15  function __construct($a = '', &$caller) {
 16    parent::__construct($a, $caller);
 17  }
 18  
 19  function ARC2_Reader($a = '', &$caller) {
 20    $this->__construct($a, $caller);
 21  }
 22
 23  function __init() {/* inc_path, proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects */
 24    parent::__init();
 25    $this->http_method = $this->v('http_method', 'GET', $this->a);
 26    $this->message_body = $this->v('message_body', '', $this->a);;
 27    $this->http_accept_header = $this->v('http_accept_header', 'Accept: application/rdf+xml; q=0.9, */*; q=0.1', $this->a);
 28    $this->http_user_agent_header = $this->v('http_user_agent_header', 'User-Agent: ARC Reader (http://arc.semsol.org/)', $this->a);
 29    $this->http_custom_headers = $this->v('http_custom_headers', '', $this->a);
 30    $this->max_redirects = $this->v('max_redirects', 3, $this->a);
 31    $this->format = $this->v('format', false, $this->a);
 32    $this->redirects = array();
 33    $this->stream_id = '';
 34    $this->timeout = $this->v('reader_timeout', 30, $this->a);
 35    $this->response_headers = array();
 36    $this->digest_auth = 0;
 37  }
 38
 39  /*  */
 40  
 41  function setHTTPMethod($v) {
 42    $this->http_method = $v;
 43  }
 44
 45  function setMessageBody($v) {
 46    $this->message_body = $v;
 47  }
 48
 49  function setAcceptHeader($v) {
 50    $this->http_accept_header = $v;
 51  }
 52
 53  function setCustomHeaders($v) {
 54    $this->http_custom_headers = $v;
 55  }
 56
 57  function addCustomHeaders($v) {
 58    if ($this->http_custom_headers) $this->http_custom_headers .= "\r\n";
 59    $this->http_custom_headers .= $v;
 60  }
 61
 62  /*  */
 63
 64  function activate($path, $data = '', $ping_only = 0, $timeout = 0) {
 65    $this->setCredentials($path);
 66    $this->ping_only = $ping_only;
 67    if ($timeout) $this->timeout = $timeout;
 68    $id = md5($path . ' ' . $data);
 69    if ($this->stream_id != $id) {
 70      $this->stream_id = $id;
 71      $this->base = $this->calcBase($path);
 72      $this->uri = $this->calcURI($path, $this->base);
 73      $this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base, $ping_only);
 74      if ($this->stream && !$this->ping_only) {
 75        $this->getFormat();
 76      }
 77    }
 78  }
 79
 80  function setCredentials($url) {
 81    if (!$creds = $this->v('arc_reader_credentials', array(), $this->a))  return 0;
 82    foreach ($creds as $pattern => $cred) {
 83      $regex = '/' . preg_replace('/([\:\/\.\?])/', '\\\\\1', $pattern) . '/';
 84      if (!preg_match($regex, $url)) continue;
 85      $parts = parse_url($url);
 86      $path = $this->v1('path', '/', $parts);
 87      /* Basic auth */
 88      $auth = 'Basic ' . base64_encode($cred);
 89      /* Digest auth */
 90      if (preg_match('/(.*)\:\:(.*)/', $cred, $m)) {
 91        $username = $m[1];
 92        $pwd = $m[2];
 93        $auth = '';
 94        $h = $this->v('www-authenticate', '', $this->getResponseHeaders());
 95        /* 401 received */
 96        if ($h && preg_match('/Digest/i', $h)) {
 97          $auth = 'Digest ';
 98          /* Digest realm="$realm", nonce="$nonce", qop="auth", opaque="$opaque" */
 99          $ks = array('realm', 'nonce', 'opaque');/* skipping qop, assuming "auth" */
100          $qop = 'auth';
101          foreach ($ks as $i => $k) {
102            $$k = preg_match('/' . $k . '=\"?([^\"]+)\"?/i', $h, $m) ? $m[1] : '';
103            $auth .= ($i ? ', ' : '') . $k . '="' . $$k . '"';
104          }
105          $ha1 = md5($username . ':' . $realm . ':' . $pwd);
106          $ha2 = md5($this->http_method . ':' . $path);
107          $nc = '0001';     /* @@todo proper request counting */
108          $cnonce = '0123'; /* @@todo proper request counting */
109          $resp = md5($ha1 . ':' . $nonce . ':' . $nc . ':' . $cnonce . ':' . $qop . ':' . $ha2);
110          $auth .= ', username="' . $username . '"' .
111            ', uri="' . $path . '"' .
112            ', qop=' . $qop . '' .
113            ', nc=' . $nc .
114            ', cnonce="' . $cnonce . '"' .  /* @@todo proper request counting */
115            ', uri="' . $path . '"' .
116            ', response="' . $resp . '"' .
117          '';
118        }
119      }
120      /* add header */
121      if ($auth) {
122        $this->setCustomHeaders('Authorization: ' . $auth);
123        break;
124      }
125    }
126  }
127
128  /*  */
129
130  function useProxy($url) {
131    if (!$this->v1('proxy_host', 0, $this->a)) {
132      return false;
133    }
134    $skips = $this->v1('proxy_skip', array(), $this->a);
135    foreach ($skips as $skip) {
136      if (strpos($url, $skip) !== false) {
137        return false;
138      }
139    }
140    return true;
141  }
142
143  /*  */
144  
145  function createStream($path, $data = '') {
146    $this->base = $this->calcBase($path);
147    $this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base);
148  }
149
150  function getDataStream($data) {
151    return array('type' => 'data', 'pos' => 0, 'headers' => array(), 'size' => strlen($data), 'data' => $data, 'buffer' => '');
152  }
153  
154  function getSocketStream($url) {
155    if ($url == 'file://') {
156      return $this->addError('Error: file does not exists or is not accessible');
157    }
158    $parts = parse_url($url);
159    $mappings = array('file' => 'File', 'http' => 'HTTP', 'https' => 'HTTP');
160    if ($scheme = $this->v(strtolower($parts['scheme']), '', $mappings)) {
161      return $this->m('get' . $scheme . 'Socket', $url, $this->getDataStream(''));
162    }
163  }
164  
165  function getFileSocket($url) {
166    $parts = parse_url($url);
167    $s = file_exists($parts['path']) ? @fopen($parts['path'], 'rb') : false;
168    if (!$s) {
169      return $this->addError('Socket error: Could not open "' . $parts['path'] . '"');
170    }
171    return array('type' => 'socket', 'socket' =>& $s, 'headers' => array(), 'pos' => 0, 'size' => filesize($parts['path']), 'buffer' => '');
172  }
173  
174  function getHTTPSocket($url, $redirs = 0) {
175    $parts = parse_url($url);
176    if (!isset($parts['scheme'])) {
177      return $this->addError('Socket error: No supported URI scheme detected.');
178    }
179    $parts['port'] = ($parts['scheme'] == 'https') ? $this->v1('port', 443, $parts) : $this->v1('port', 80, $parts);
180    $nl = "\r\n";
181    $http_mthd = strtoupper($this->http_method);
182    if ($this->v1('user', 0, $parts) || $this->useProxy($url)) {
183      $h_code = $http_mthd . ' ' . $url;
184    }
185    else {
186      $h_code = $http_mthd . ' ' . $this->v1('path', '/', $parts) . (($v = $this->v1('query', 0, $parts)) ? '?' . $v : '') . (($v = $this->v1('fragment', 0, $parts)) ? '#' . $v : '');
187    }
188    $h_code .= ' HTTP/1.0' . $nl.
189      'Host: ' . $parts['host'] . ':' . $parts['port'] . $nl .
190      (($v = $this->http_accept_header) ? $v . $nl : '') .
191      (($v = $this->http_user_agent_header) && !preg_match('/User\-Agent\:/', $this->http_custom_headers) ? $v . $nl : '') .
192      (($http_mthd == 'POST') ? 'Content-Length: ' . strlen($this->message_body) . $nl : '') .
193      ($this->http_custom_headers ? trim($this->http_custom_headers) . $nl : '') .
194      $nl .
195    '';
196    /* post body */
197    if ($http_mthd == 'POST') {
198      $h_code .= $this->message_body . $nl;
199    }
200    /* connect */
201    if ($this->useProxy($url)) {
202      $s = @fsockopen($this->a['proxy_host'], $this->a['proxy_port'], $errno, $errstr, $this->timeout);
203    }
204    elseif ($parts['scheme'] == 'https') {
205      $s = @fsockopen('ssl://' . $parts['host'], $parts['port'], $errno, $errstr, $this->timeout);
206    }
207    elseif ($parts['scheme'] == 'http') {
208      $s = fsockopen($parts['host'], $parts['port'], $errno, $errstr, $this->timeout);
209    }
210    if (!$s) {
211      return $this->addError('Socket error: Could not connect to "' . $url . '" (proxy: ' . ($this->useProxy($url) ? '1' : '0') . '): ' . $errstr);
212    }
213    /* request */
214    fwrite($s, $h_code);
215    /* timeout */
216    if ($this->timeout) {
217      //stream_set_blocking($s, false);
218      stream_set_timeout($s, $this->timeout);
219    }
220    /* response headers */
221    $h = array();
222    $this->response_headers = $h;
223    if (!$this->ping_only) {
224      do {
225        $line = trim(fgets($s, 256));
226        $info = stream_get_meta_data($s);
227        if (preg_match("/^HTTP[^\s]+\s+([0-9]{1})([0-9]{2})(.*)$/i", $line, $m)) {/* response code */
228          $error = in_array($m[1], array('4', '5')) ? $m[1] . $m[2] . ' ' . $m[3] : '';
229          $error = ($m[1].$m[2] == '304') ? '304 '.$m[3] : $error;
230          $h['response-code'] = $m[1] . $m[2];
231          $h['error'] = $error;
232          $h['redirect'] = ($m[1] == '3') ? true : false;
233        }
234        elseif (preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $m)) {/* header */
235          $h[strtolower($m[1])] = trim($m[2]);
236        }
237      } while(!$info['timed_out'] && !feof($s) && $line);
238      $h['format'] = strtolower(preg_replace('/^([^\s]+).*$/', '\\1', $this->v('content-type', '', $h)));
239      $h['encoding'] = preg_match('/(utf\-8|iso\-8859\-1|us\-ascii)/', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : '';
240      $h['encoding'] = preg_match('/charset=\s*([^\s]+)/si', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : $h['encoding'];
241      $this->response_headers = $h;
242      /* result */
243      if ($info['timed_out']) {
244        return $this->addError('Connection timed out after ' . $this->timeout . ' seconds');
245      }
246      /* error */
247      if ($v = $this->v('error', 0, $h)) {
248        /* digest auth */
249        /* 401 received */
250        if (preg_match('/Digest/i', $this->v('www-authenticate', '', $h)) && !$this->digest_auth) {
251          $this->setCredentials($url);
252          $this->digest_auth = 1;
253          return $this->getHTTPSocket($url);
254        }
255        return $this->addError($error . ' "' . (!feof($s) ? trim(strip_tags(fread($s, 64))) . '..."' : ''));
256      }
257      /* redirect */
258      if ($this->v('redirect', 0, $h) && ($new_url = $this->v1('location', 0, $h))) {
259        fclose($s);
260        $this->redirects[$url] = $new_url;
261        $this->base = $new_url;
262        if ($redirs > $this->max_redirects) {
263          return $this->addError('Max numbers of redirects exceeded.');
264        }
265        return $this->getHTTPSocket($new_url, $redirs+1);
266      }
267    }
268    if ($this->timeout) {
269      stream_set_blocking($s, true);
270    }
271    return array('type' => 'socket', 'url' => $url, 'socket' =>& $s, 'headers' => $h, 'pos' => 0, 'size' => $this->v('content-length', 0, $h), 'buffer' => '');
272  }
273
274  function readStream($buffer_xml = true, $d_size = 1024) {
275    //if (!$s = $this->v('stream')) return '';
276    if (!$s = $this->v('stream')) return $this->addError('missing stream in "readStream" ' . $this->uri);
277    $s_type = $this->v('type', '', $s);
278    $r = $s['buffer'];
279    $s['buffer'] = '';
280    if ($s['size']) $d_size = min($d_size, $s['size'] - $s['pos']);
281    /* data */
282    if ($s_type == 'data') {
283      $d = ($d_size > 0) ? substr($s['data'], $s['pos'], $d_size) : '';
284    }
285    /* socket */
286    elseif ($s_type == 'socket') {
287      $d = ($d_size > 0) && !feof($s['socket']) ? fread($s['socket'], $d_size) : '';
288    }
289    $eof = $d ? false : true;
290    /* chunked despite HTTP 1.0 request */
291    if (isset($s['headers']) && isset($s['headers']['transfer-encoding']) && ($s['headers']['transfer-encoding'] == 'chunked')) {
292      $d = preg_replace('/(^|[\r\n]+)[0-9a-f]{1,4}[\r\n]+/', '', $d);
293    }
294    $s['pos'] += strlen($d);
295    if ($buffer_xml) {/* stop after last closing xml tag (if available) */
296      if (preg_match('/^(.*\>)([^\>]*)$/s', $d, $m)) {
297        $d = $m[1];
298        $s['buffer'] = $m[2];
299      }
300      elseif (!$eof) {
301        $s['buffer'] = $r . $d;
302        $this->stream = $s;
303        return $this->readStream(true, $d_size);
304      }
305    }
306    $this->stream = $s;
307    return $r . $d;
308  }
309  
310  function closeStream() {
311    if (isset($this->stream)) {
312      if ($this->v('type', 0, $this->stream) == 'socket') {
313        @fclose($this->stream['socket']);
314      }
315      unset($this->stream);
316    }
317  }
318  
319  /*  */
320  
321  function getFormat() {
322    if (!$this->format) {
323      if (!$this->v('stream')) {
324        return $this->addError('missing stream in "getFormat"');
325      }
326      $v = $this->readStream(false);
327      $mtype = $this->v('format', '', $this->stream['headers']);
328      $this->stream['buffer'] = $v . $this->stream['buffer'];
329      $ext = preg_match('/\.([^\.]+)$/', $this->uri, $m) ? $m[1] : '';
330      $this->format = ARC2::getFormat($v, $mtype, $ext);
331    }
332    return $this->format;
333  }
334    
335  /*  */
336
337  function getResponseHeaders() {
338    if (isset($this->stream) && isset($this->stream['headers'])) {
339      return $this->stream['headers'];
340    }
341    return $this->response_headers;
342  }
343  
344  function getEncoding($default = 'UTF-8') {
345    return $this->v1('encoding', $default, $this->stream['headers']);
346  }
347
348  function getRedirects() {
349    return $this->redirects;
350  }
351  
352  /*  */
353  
354}