pax_global_header 0000666 0000000 0000000 00000000064 12252347775 0014530 g ustar 00root root 0000000 0000000 52 comment=8a35ff12a6d44da26ada513016a451a2703557ec
arc2-2.2.4/ 0000775 0000000 0000000 00000000000 12252347775 0012364 5 ustar 00root root 0000000 0000000 arc2-2.2.4/.gitignore 0000664 0000000 0000000 00000000065 12252347775 0014355 0 ustar 00root root 0000000 0000000 *.DS_Store
plugins/*
triggers/*
tests/coverage/*
arc2-2.2.4/ARC2.php 0000664 0000000 0000000 00000032351 12252347775 0013570 0 ustar 00root root 0000000 0000000
* @package ARC2
*/
/* E_STRICT hack */
if (function_exists('date_default_timezone_get')) {
date_default_timezone_set(@date_default_timezone_get());
}
class ARC2 {
static function getVersion() {
return '2011-12-01';
}
/* */
static function getIncPath($f = '') {
$r = realpath(dirname(__FILE__)) . '/';
$dirs = array(
'plugin' => 'plugins',
'trigger' => 'triggers',
'store' => 'store',
'serializer' => 'serializers',
'extractor' => 'extractors',
'sparqlscript' => 'sparqlscript',
'parser' => 'parsers',
);
foreach ($dirs as $k => $dir) {
if (preg_match('/' . $k . '/i', $f)) {
return $r . $dir . '/';
}
}
return $r;
}
static function getScriptURI() {
if (isset($_SERVER) && (isset($_SERVER['SERVER_NAME']) || isset($_SERVER['HTTP_HOST']))) {
$proto = preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL']));
$port = $_SERVER['SERVER_PORT'];
$server = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$script = $_SERVER['SCRIPT_NAME'];
/* https */
if (($proto == 'http') && $port == 443) {
$proto = 'https';
$port = 80;
}
return $proto . '://' . $server . ($port != 80 ? ':' . $port : '') . $script;
}
elseif (isset($_SERVER['SCRIPT_FILENAME'])) {
return 'file://' . realpath($_SERVER['SCRIPT_FILENAME']);
}
return 'http://localhost/unknown_path';
}
static function getRequestURI() {
if (isset($_SERVER) && isset($_SERVER['REQUEST_URI'])) {
return preg_replace('/^([a-z]+)\/.*$/', '\\1', strtolower($_SERVER['SERVER_PROTOCOL'])) .
'://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']) .
($_SERVER['SERVER_PORT'] != 80 ? ':' . $_SERVER['SERVER_PORT'] : '') .
$_SERVER['REQUEST_URI'];
}
return ARC2::getScriptURI();
}
static function inc($f, $path = '') {
$prefix = 'ARC2';
if (preg_match('/^([^\_]+)\_(.*)$/', $f, $m)) {
$prefix = $m[1];
$f = $m[2];
}
$inc_path = $path ? $path : ARC2::getIncPath($f);
$path = $inc_path . $prefix . '_' . urlencode($f) . '.php';
if (file_exists($path)) return include_once($path);
/* safe-mode hack */
if (@include_once($path)) return 1;
/* try other path */
if ($prefix != 'ARC2') {
$path = $inc_path . strtolower($prefix) . '/' . $prefix . '_' . urlencode($f) . '.php';
if (file_exists($path)) return include_once($path);
/* safe-mode hack */
if (@include_once($path)) return 1;
}
return 0;
}
/* */
static function mtime(){
return microtime(true);
}
static function x($re, $v, $options = 'si') {
return preg_match("/^\s*" . $re . "(.*)$/" . $options, $v, $m) ? $m : false;
}
/* */
static function getFormat($val, $mtype = '', $ext = '') {
ARC2::inc('getFormat');
return ARC2_getFormat($val, $mtype, $ext);
}
static function getPreferredFormat($default = 'plain') {
ARC2::inc('getPreferredFormat');
return ARC2_getPreferredFormat($default);
}
/* */
static function toUTF8($v) {
if (urlencode($v) === $v) return $v;
//if (utf8_decode($v) == $v) return $v;
$v = (strpos(utf8_decode(str_replace('?', '', $v)), '?') === false) ? utf8_decode($v) : $v;
/* custom hacks, mainly caused by bugs in PHP's json_decode */
$mappings = array(
'%18' => '‘',
'%19' => '’',
'%1C' => '“',
'%1D' => '”',
'%1E' => '„',
'%10' => '‐',
'%12' => '−',
'%13' => '–',
'%14' => '—',
'%26' => '&',
);
$froms = array_keys($mappings);
$tos = array_values($mappings);
foreach ($froms as $i => $from) $froms[$i] = urldecode($from);
$v = str_replace($froms, $tos, $v);
/* utf8 tweaks */
return preg_replace_callback('/([\x00-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5}|[^\x00-\x7f])/', array('ARC2', 'getUTF8Char'), $v);
}
static function getUTF8Char($v) {
$val = $v[1];
if (strlen(trim($val)) === 1) return utf8_encode($val);
if (preg_match('/^([\x00-\x7f])(.+)/', $val, $m)) return $m[1] . ARC2::toUTF8($m[2]);
return $val;
}
/* */
static function splitURI($v) {
/* the following namespaces may lead to conflated URIs,
* we have to set the split position manually
*/
if (strpos($v, 'www.w3.org')) {
$specials = array(
'http://www.w3.org/XML/1998/namespace',
'http://www.w3.org/2005/Atom',
'http://www.w3.org/1999/xhtml',
);
foreach ($specials as $ns) {
if (strpos($v, $ns) === 0) {
$local_part = substr($v, strlen($ns));
if (!preg_match('/^[\/\#]/', $local_part)) {
return array($ns, $local_part);
}
}
}
}
/* auto-splitting on / or # */
//$re = '^(.*?)([A-Z_a-z][-A-Z_a-z0-9.]*)$';
if (preg_match('/^(.*[\/\#])([^\/\#]+)$/', $v, $m)) return array($m[1], $m[2]);
/* auto-splitting on last special char, e.g. urn:foo:bar */
if (preg_match('/^(.*[\:\/])([^\:\/]+)$/', $v, $m)) return array($m[1], $m[2]);
return array($v, '');
}
/* */
static function getSimpleIndex($triples, $flatten_objects = 1, $vals = '') {
$r = array();
foreach ($triples as $t) {
$skip_t = 0;
foreach (array('s', 'p', 'o') as $term) {
$$term = $t[$term];
/* template var */
if (isset($t[$term . '_type']) && ($t[$term . '_type'] == 'var')) {
$val = isset($vals[$$term]) ? $vals[$$term] : '';
$skip_t = isset($vals[$$term]) ? $skip_t : 1;
$type = '';
$type = !$type && isset($vals[$$term . ' type']) ? $vals[$$term . ' type'] : $type;
$type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type;
if ($term == 'o') {
$type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type;
$type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type;
}
$type = !$type ? 'uri' : $type;
$t[$term . '_type'] = $type;
$$term = $val;
}
}
if ($skip_t) {
continue;
}
if (!isset($r[$s])) $r[$s] = array();
if (!isset($r[$s][$p])) $r[$s][$p] = array();
if ($flatten_objects) {
if (!in_array($o, $r[$s][$p])) $r[$s][$p][] = $o;
}
else {
$o = array('value' => $o);
foreach (array('lang', 'type', 'datatype') as $suffix) {
if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) {
$o[$suffix] = $t['o_' . $suffix];
}
elseif (isset($t['o ' . $suffix]) && $t['o ' . $suffix]) {
$o[$suffix] = $t['o ' . $suffix];
}
}
if (!in_array($o, $r[$s][$p])) {
$r[$s][$p][] = $o;
}
}
}
return $r;
}
static function getTriplesFromIndex($index) {
$r = array();
foreach ($index as $s => $ps) {
foreach ($ps as $p => $os) {
foreach ($os as $o) {
$r[] = array(
's' => $s,
'p' => $p,
'o' => $o['value'],
's_type' => preg_match('/^\_\:/', $s) ? 'bnode' : 'uri',
'o_type' => $o['type'],
'o_datatype' => isset($o['datatype']) ? $o['datatype'] : '',
'o_lang' => isset($o['lang']) ? $o['lang'] : '',
);
}
}
}
return $r;
}
static function getMergedIndex() {
$r = array();
foreach (func_get_args() as $index) {
foreach ($index as $s => $ps) {
if (!isset($r[$s])) $r[$s] = array();
foreach ($ps as $p => $os) {
if (!isset($r[$s][$p])) $r[$s][$p] = array();
foreach ($os as $o) {
if (!in_array($o, $r[$s][$p])) {
$r[$s][$p][] = $o;
}
}
}
}
}
return $r;
}
static function getCleanedIndex() {/* removes triples from a given index */
$indexes = func_get_args();
$r = $indexes[0];
for ($i = 1, $i_max = count($indexes); $i < $i_max; $i++) {
$index = $indexes[$i];
foreach ($index as $s => $ps) {
if (!isset($r[$s])) continue;
foreach ($ps as $p => $os) {
if (!isset($r[$s][$p])) continue;
$r_os = $r[$s][$p];
$new_os = array();
foreach ($r_os as $r_o) {
$r_o_val = is_array($r_o) ? $r_o['value'] : $r_o;
$keep = 1;
foreach ($os as $o) {
$del_o_val = is_array($o) ? $o['value'] : $o;
if ($del_o_val == $r_o_val) {
$keep = 0;
break;
}
}
if ($keep) {
$new_os[] = $r_o;
}
}
if ($new_os) {
$r[$s][$p] = $new_os;
}
else {
unset($r[$s][$p]);
}
}
}
}
/* check r */
$has_data = 0;
foreach ($r as $s => $ps) {
if ($ps) {
$has_data = 1;
break;
}
}
return $has_data ? $r : array();
}
/* */
static function getStructType($v) {
/* string */
if (is_string($v)) return 'string';
/* flat array, numeric keys */
if (in_array(0, array_keys($v))) {/* numeric keys */
/* simple array */
if (!is_array($v[0])) return 'array';
/* triples */
//if (isset($v[0]) && isset($v[0]['s']) && isset($v[0]['p'])) return 'triples';
if (in_array('p', array_keys($v[0]))) return 'triples';
}
/* associative array */
else {
/* index */
foreach ($v as $s => $ps) {
if (!is_array($ps)) break;
foreach ($ps as $p => $os) {
if (!is_array($os) || !is_array($os[0])) break;
if (in_array('value', array_keys($os[0]))) return 'index';
}
}
}
/* array */
return 'array';
}
/* */
static function getComponent($name, $a = '', $caller = '') {
ARC2::inc($name);
$prefix = 'ARC2';
if (preg_match('/^([^\_]+)\_(.+)$/', $name, $m)) {
$prefix = $m[1];
$name = $m[2];
}
$cls = $prefix . '_' . $name;
if (!$caller) $caller = new stdClass();
return new $cls($a, $caller);
}
/* graph */
static function getGraph($a = '') {
return ARC2::getComponent('Graph', $a);
}
/* resource */
static function getResource($a = '') {
return ARC2::getComponent('Resource', $a);
}
/* reader */
static function getReader($a = '') {
return ARC2::getComponent('Reader', $a);
}
/* parsers */
static function getParser($prefix, $a = '') {
return ARC2::getComponent($prefix . 'Parser', $a);
}
static function getRDFParser($a = '') {
return ARC2::getParser('RDF', $a);
}
static function getRDFXMLParser($a = '') {
return ARC2::getParser('RDFXML', $a);
}
static function getTurtleParser($a = '') {
return ARC2::getParser('Turtle', $a);
}
static function getRSSParser($a = '') {
return ARC2::getParser('RSS', $a);
}
static function getSemHTMLParser($a = '') {
return ARC2::getParser('SemHTML', $a);
}
static function getSPARQLParser($a = '') {
return ARC2::getComponent('SPARQLParser', $a);
}
static function getSPARQLPlusParser($a = '') {
return ARC2::getParser('SPARQLPlus', $a);
}
static function getSPARQLXMLResultParser($a = '') {
return ARC2::getParser('SPARQLXMLResult', $a);
}
static function getJSONParser($a = '') {
return ARC2::getParser('JSON', $a);
}
static function getSGAJSONParser($a = '') {
return ARC2::getParser('SGAJSON', $a);
}
static function getCBJSONParser($a = '') {
return ARC2::getParser('CBJSON', $a);
}
static function getSPARQLScriptParser($a = '') {
return ARC2::getParser('SPARQLScript', $a);
}
/* store */
static function getStore($a = '', $caller = '') {
return ARC2::getComponent('Store', $a, $caller);
}
static function getStoreEndpoint($a = '', $caller = '') {
return ARC2::getComponent('StoreEndpoint', $a, $caller);
}
static function getRemoteStore($a = '', $caller = '') {
return ARC2::getComponent('RemoteStore', $a, $caller);
}
static function getMemStore($a = '') {
return ARC2::getComponent('MemStore', $a);
}
/* serializers */
static function getSer($prefix, $a = '') {
return ARC2::getComponent($prefix . 'Serializer', $a);
}
static function getTurtleSerializer($a = '') {
return ARC2::getSer('Turtle', $a);
}
static function getRDFXMLSerializer($a = '') {
return ARC2::getSer('RDFXML', $a);
}
static function getNTriplesSerializer($a = '') {
return ARC2::getSer('NTriples', $a);
}
static function getRDFJSONSerializer($a = '') {
return ARC2::getSer('RDFJSON', $a);
}
static function getPOSHRDFSerializer($a = '') {/* deprecated */
return ARC2::getSer('POSHRDF', $a);
}
static function getMicroRDFSerializer($a = '') {
return ARC2::getSer('MicroRDF', $a);
}
static function getRSS10Serializer($a = '') {
return ARC2::getSer('RSS10', $a);
}
/* sparqlscript */
static function getSPARQLScriptProcessor($a = '') {
return ARC2::getComponent('SPARQLScriptProcessor', $a);
}
/* */
}
arc2-2.2.4/ARC2_Class.php 0000664 0000000 0000000 00000040712 12252347775 0014715 0 ustar 00root root 0000000 0000000
* @homepage
* @package ARC2
*/
class ARC2_Class {
function __construct($a, &$caller) {
$this->a = is_array($a) ? $a : array();
$this->caller = $caller;
$this->__init();
}
function __init() {/* base, time_limit */
if (!$_POST && isset($GLOBALS['HTTP_RAW_POST_DATA'])) parse_str($GLOBALS['HTTP_RAW_POST_DATA'], $_POST); /* php5 bug */
$this->inc_path = ARC2::getIncPath();
$this->ns_count = 0;
$rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
$this->nsp = array($rdf => 'rdf');
$this->used_ns = array($rdf);
$this->ns = array_merge(array('rdf' => $rdf), $this->v('ns', array(), $this->a));
$this->base = $this->v('base', ARC2::getRequestURI(), $this->a);
$this->errors = array();
$this->warnings = array();
$this->adjust_utf8 = $this->v('adjust_utf8', 0, $this->a);
$this->max_errors = $this->v('max_errors', 25, $this->a);
$this->has_pcre_unicode = @preg_match('/\pL/u', 'test');/* \pL = block/point which is a Letter */
}
/* */
function v($name, $default = false, $o = false) {/* value if set */
if ($o === false) $o = $this;
if (is_array($o)) {
return isset($o[$name]) ? $o[$name] : $default;
}
return isset($o->$name) ? $o->$name : $default;
}
function v1($name, $default = false, $o = false) {/* value if 1 (= not empty) */
if ($o === false) $o = $this;
if (is_array($o)) {
return (isset($o[$name]) && $o[$name]) ? $o[$name] : $default;
}
return (isset($o->$name) && $o->$name) ? $o->$name : $default;
}
function m($name, $a = false, $default = false, $o = false) {/* call method */
if ($o === false) $o = $this;
return method_exists($o, $name) ? $o->$name($a) : $default;
}
/* */
function camelCase($v, $lc_first = 0, $keep_boundaries = 0) {
$r = ucfirst($v);
while (preg_match('/^(.*)[^a-z0-9](.*)$/si', $r, $m)) {
/* don't fuse 2 upper-case chars */
if ($keep_boundaries && $m[1]) {
$boundary = substr($m[1], -1);
if (strtoupper($boundary) == $boundary) $m[1] .= 'CAMELCASEBOUNDARY';
}
$r = $m[1] . ucfirst($m[2]);
}
$r = str_replace('CAMELCASEBOUNDARY', '_', $r);
if ((strlen($r) > 1) && $lc_first && !preg_match('/[A-Z]/', $r[1])) $r = strtolower($r[0]) . substr($r, 1);
return $r;
}
function deCamelCase($v, $uc_first = 0) {
$r = str_replace('_', ' ', $v);
$r = preg_replace('/([a-z0-9])([A-Z])/e', '"\\1 " . strtolower("\\2")', $r);
return $uc_first ? ucfirst($r) : $r;
}
/**
* Tries to extract a somewhat human-readable label from a URI.
*/
function extractTermLabel($uri, $loops = 0) {
list($ns, $r) = $this->splitURI($uri);
/* encode apostrophe + s */
$r = str_replace('%27s', '_apostrophes_', $r);
/* normalize */
$r = $this->deCamelCase($this->camelCase($r, 1, 1));
/* decode apostrophe + s */
$r = str_replace(' apostrophes ', "'s ", $r);
/* typical RDF non-info URI */
if (($loops < 1) && preg_match('/^(self|it|this|me|id)$/i', $r)) {
return $this->extractTermLabel(preg_replace('/\#.+$/', '', $uri), $loops + 1);
}
/* trailing hash or slash */
if ($uri && !$r && ($loops < 2)) {
return $this->extractTermLabel(preg_replace('/[\#\/]$/', '', $uri), $loops + 1);
}
/* a de-camel-cased URL (will look like "www example com") */
if (preg_match('/^www (.+ [a-z]{2,4})$/', $r, $m)) {
return $this->getPrettyURL($uri);
}
return $r;
}
/**
* Generates a less ugly in-your-face URL.
*/
function getPrettyURL($r) {
$r = rtrim($r, '/');
$r = preg_replace('/^https?\:\/\/(www\.)?/', '', $r);
return $r;
}
/* */
function addError($v) {
if (!in_array($v, $this->errors)) {
$this->errors[] = $v;
}
if ($this->caller && method_exists($this->caller, 'addError')) {
$glue = strpos($v, ' in ') ? ' via ' : ' in ';
$this->caller->addError($v . $glue . get_class($this));
}
if (count($this->errors) > $this->max_errors) {
die('Too many errors (limit: ' . $this->max_errors . '): ' . print_r($this->errors, 1));
}
return false;
}
function getErrors() {
return $this->errors;
}
function getWarnings() {
return $this->warnings;
}
function resetErrors() {
$this->errors = array();
if ($this->caller && method_exists($this->caller, 'resetErrors')) {
$this->caller->resetErrors();
}
}
/* */
function splitURI($v) {
return ARC2::splitURI($v);
}
/* */
function getPName($v, $connector = ':') {
/* is already a pname */
$ns = $this->getPNameNamespace($v, $connector);
if ($ns) {
if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
return $v;
}
/* new pname */
$parts = $this->splitURI($v);
if ($parts) {
/* known prefix */
foreach ($this->ns as $prefix => $ns) {
if ($parts[0] == $ns) {
if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
return $prefix . $connector . $parts[1];
}
}
/* new prefix */
$prefix = $this->getPrefix($parts[0]);
return $prefix . $connector . $parts[1];
}
return $v;
}
function getPNameNamespace($v, $connector = ':') {
$re = '/^([a-z0-9\_\-]+)\:([a-z0-9\_\-\.\%]+)$/i';
if ($connector != ':') {
$connectors = array('\:', '\-', '\_', '\.');
$chars = join('', array_diff($connectors, array($connector)));
$re = '/^([a-z0-9' . $chars . ']+)\\' . $connector . '([a-z0-9\_\-\.\%]+)$/i';
}
if (!preg_match($re, $v, $m)) return 0;
if (!isset($this->ns[$m[1]])) return 0;
return $this->ns[$m[1]];
}
function setPrefix($prefix, $ns) {
$this->ns[$prefix] = $ns;
$this->nsp[$ns] = $prefix;
return $this;
}
function getPrefix($ns) {
if (!isset($this->nsp[$ns])) {
$this->ns['ns' . $this->ns_count] = $ns;
$this->nsp[$ns] = 'ns' . $this->ns_count;
$this->ns_count++;
}
if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
return $this->nsp[$ns];
}
function expandPName($v, $connector = ':') {
$re = '/^([a-z0-9\_\-]+)\:([a-z0-9\_\-\.\%]+)$/i';
if ($connector != ':') {
$connectors = array(':', '-', '_', '.');
$chars = '\\' . join('\\', array_diff($connectors, array($connector)));
$re = '/^([a-z0-9' . $chars . ']+)\\' . $connector . '([a-z0-9\_\-\.\%]+)$/Ui';
}
if (preg_match($re, $v, $m) && isset($this->ns[$m[1]])) {
return $this->ns[$m[1]] . $m[2];
}
return $v;
}
function expandPNames($index) {
$r = array();
foreach ($index as $s => $ps) {
$s = $this->expandPName($s);
$r[$s] = array();
foreach ($ps as $p => $os) {
$p = $this->expandPName($p);
if (!is_array($os)) $os = array($os);
foreach ($os as $i => $o) {
if (!is_array($o)) {
$o_val = $this->expandPName($o);
$o_type = preg_match('/^[a-z]+\:[^\s\<\>]+$/si', $o_val) ? 'uri' : 'literal';
$o = array('value' => $o_val, 'type' => $o_type);
}
$os[$i] = $o;
}
$r[$s][$p] = $os;
}
}
return $r;
}
/* */
function calcURI($path, $base = "") {
/* quick check */
if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */
return $path;
}
if (preg_match('/^\$\{.*\}/', $path)) {/* placeholder, assume abs URI */
return $path;
}
if (preg_match("/^\/\//", $path)) {/* net path, assume http */
return 'http:' . $path;
}
/* other URIs */
$base = $base ? $base : $this->base;
$base = preg_replace('/\#.*$/', '', $base);
if ($path === true) {/* empty (but valid) URIref via turtle parser: <> */
return $base;
}
$path = preg_replace("/^\.\//", '', $path);
$root = preg_match('/(^[a-z0-9]+\:[\/]{1,3}[^\/]+)[\/|$]/i', $base, $m) ? $m[1] : $base; /* w/o trailing slash */
$base .= ($base == $root) ? '/' : '';
if (preg_match('/^\//', $path)) {/* leading slash */
return $root . $path;
}
if (!$path) {
return $base;
}
if (preg_match('/^([\#\?])/', $path, $m)) {
return preg_replace('/\\' .$m[1]. '.*$/', '', $base) . $path;
}
if (preg_match('/^(\&)(.*)$/', $path, $m)) {/* not perfect yet */
return preg_match('/\?/', $base) ? $base . $m[1] . $m[2] : $base . '?' . $m[2];
}
if (preg_match("/^[a-z0-9]+\:/i", $path)) {/* abs path */
return $path;
}
/* rel path: remove stuff after last slash */
$base = substr($base, 0, strrpos($base, '/')+1);
/* resolve ../ */
while (preg_match('/^(\.\.\/)(.*)$/', $path, $m)) {
$path = $m[2];
$base = ($base == $root.'/') ? $base : preg_replace('/^(.*\/)[^\/]+\/$/', '\\1', $base);
}
return $base . $path;
}
/* */
function calcBase($path) {
$r = $path;
$r = preg_replace('/\#.*$/', '', $r);/* remove hash */
$r = preg_replace('/^\/\//', 'http://', $r);/* net path (//), assume http */
if (preg_match('/^[a-z0-9]+\:/', $r)) {/* scheme, abs path */
while (preg_match('/^(.+\/)(\.\.\/.*)$/U', $r, $m)) {
$r = $this->calcURI($m[1], $m[2]);
}
return $r;
}
return 'file://' . realpath($r);/* real path */
}
/* */
function getResource($uri, $store_or_props = '') {
$res = ARC2::getResource($this->a);
$res->setURI($uri);
if (is_array($store_or_props)) {
$res->setProps($store_or_props);
}
else {
$res->setStore($store_or_props);
}
return $res;
}
function toIndex($v) {
if (is_array($v)) {
if (isset($v[0]) && isset($v[0]['s'])) return ARC2::getSimpleIndex($v, 0);
return $v;
}
$parser = ARC2::getRDFParser($this->a);
if ($v && !preg_match('/\s/', $v)) {/* assume graph URI */
$parser->parse($v);
}
else {
$parser->parse('', $v);
}
return $parser->getSimpleIndex(0);
}
function toTriples($v) {
if (is_array($v)) {
if (isset($v[0]) && isset($v[0]['s'])) return $v;
return ARC2::getTriplesFromIndex($v);
}
$parser = ARC2::getRDFParser($this->a);
if ($v && !preg_match('/\s/', $v)) {/* assume graph URI */
$parser->parse($v);
}
else {
$parser->parse('', $v);
}
return $parser->getTriples();
}
/* */
function toNTriples($v, $ns = '', $raw = 0) {
ARC2::inc('NTriplesSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_NTriplesSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
}
function toTurtle($v, $ns = '', $raw = 0) {
ARC2::inc('TurtleSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_TurtleSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
}
function toRDFXML($v, $ns = '', $raw = 0) {
ARC2::inc('RDFXMLSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_RDFXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
}
function toRDFJSON($v, $ns = '') {
ARC2::inc('RDFJSONSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_RDFJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
}
function toRSS10($v, $ns = '') {
ARC2::inc('RSS10Serializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_RSS10Serializer(array_merge($this->a, array('ns' => $ns)), $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
}
function toLegacyXML($v, $ns = '') {
ARC2::inc('LegacyXMLSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_LegacyXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return $ser->getSerializedArray($v);
}
function toLegacyJSON($v, $ns = '') {
ARC2::inc('LegacyJSONSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_LegacyJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return $ser->getSerializedArray($v);
}
function toLegacyHTML($v, $ns = '') {
ARC2::inc('LegacyHTMLSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$ser = new ARC2_LegacyHTMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
return $ser->getSerializedArray($v);
}
function toHTML($v, $ns = '', $label_store = '') {
ARC2::inc('MicroRDFSerializer');
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$conf = array_merge($this->a, array('ns' => $ns));
if ($label_store) $conf['label_store'] = $label_store;
$ser = new ARC2_MicroRDFSerializer($conf, $this);
return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
}
/* */
function getFilledTemplate($t, $vals, $g = '') {
$parser = ARC2::getTurtleParser();
$parser->parse($g, $this->getTurtleHead() . $t);
return $parser->getSimpleIndex(0, $vals);
}
function getTurtleHead() {
$r = '';
$ns = $this->v('ns', array(), $this->a);
foreach ($ns as $k => $v) {
$r .= "@prefix " . $k . ": <" .$v. "> .\n";
}
return $r;
}
function completeQuery($q, $ns = '') {
if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
$added_prefixes = array();
$prologue = '';
foreach ($ns as $k => $v) {
$k = rtrim($k, ':');
if (in_array($k, $added_prefixes)) continue;
if (preg_match('/(^|\s)' . $k . ':/s', $q) && !preg_match('/PREFIX\s+' . $k . '\:/is', $q)) {
$prologue .= "\n" . 'PREFIX ' . $k . ': <' . $v . '>';
}
$added_prefixes[] = $k;
}
return $prologue . "\n" . $q;
}
/* */
function toUTF8($str) {
return $this->adjust_utf8 ? ARC2::toUTF8($str) : $str;
}
function toDataURI($str) {
return 'data:text/plain;charset=utf-8,' . rawurlencode($str);
}
function fromDataURI($str) {
return str_replace('data:text/plain;charset=utf-8,', '', rawurldecode($str));
}
/* prevent SQL injections via SPARQL REGEX */
function checkRegex($str) {
return addslashes($str); // @@todo extend
}
/* Microdata methods */
function getMicrodataAttrs($id, $type = '') {
$type = $type ? $this->expandPName($type) : $this->expandPName('owl:Thing');
return 'itemscope="" itemtype="' . htmlspecialchars($type) . '" itemid="' . htmlspecialchars($id) . '"';
}
function mdAttrs($id, $type = '') {
return $this->getMicrodataAttrs($id, $type);
}
/* central DB query hook */
function queryDB($sql, $con, $log_errors = 0) {
$t1 = ARC2::mtime();
$r = mysql_query($sql, $con);
if (0) {
$t2 = ARC2::mtime() - $t1;
$call_obj = $this;
$call_path = '';
while ($call_obj) {
$call_path = get_class($call_obj) . ' / ' . $call_path;
$call_obj = isset($call_obj->caller) ? $call_obj->caller : false;
}
echo "\n" . $call_path . " needed " . $t2 . ' secs for ' . str_replace("\n" , ' ', $sql);;
}
if ($log_errors && ($er = mysql_error($con))) $this->addError($er);
return $r;
}
/**
* Shortcut method to create an RDF/XML backup dump from an RDF Store object.
*/
function backupStoreData($store, $target_path, $offset = 0) {
$limit = 10;
$q = '
SELECT DISTINCT ?s WHERE {
?s ?p ?o .
}
ORDER BY ?s
LIMIT ' . $limit . '
' . ($offset ? 'OFFSET ' . $offset : '') . '
';
$rows = $store->query($q, 'rows');
$tc = count($rows);
$full_tc = $tc + $offset;
$mode = $offset ? 'ab' : 'wb';
$fp = fopen($target_path, $mode);
foreach ($rows as $row) {
$index = $store->query('DESCRIBE <' . $row['s'] . '>', 'raw');
if ($index) {
$doc = $this->toRDFXML($index);
fwrite($fp, $doc . "\n\n");
}
}
fclose($fp);
if ($tc == 10) {
set_time_limit(300);
$this->backupStoreData($store, $target_path, $offset + $limit);
}
return $full_tc;
}
}
arc2-2.2.4/ARC2_Graph.php 0000664 0000000 0000000 00000010165 12252347775 0014710 0 ustar 00root root 0000000 0000000
* @license W3C Software License
* @homepage
* @package ARC2
*/
ARC2::inc('Class');
class ARC2_Graph extends ARC2_Class {
protected $index;
function __construct($a, &$caller) {
parent::__construct($a, $caller);
}
function __init() {
parent::__init();
$this->index = array();
}
function setIndex($index) {
$this->index = $index;
return $this;
}
function getIndex() {
return $this->index;
}
function addIndex($index) {
$this->index = ARC2::getMergedIndex($this->index, $index);
return $this;
}
function addGraph($graph) {
// namespaces
foreach ($graph->ns as $prefix => $ns) {
$this->setPrefix($prefix, $ns);
}
// index
$this->addIndex($graph->getIndex());
return $this;
}
function addRdf($data, $format = null) {
if ($format == 'json') {
return $this->addIndex(json_decode($data, true));
}
else {// parse any other rdf format
return $this->addIndex($this->toIndex($data));
}
}
function hasSubject($s) {
return isset($this->index[$s]);
}
function hasTriple($s, $p, $o) {
if (!is_array($o)) {
return $this->hasLiteralTriple($s, $p, $o) || $this->hasLinkTriple($s, $p, $o);
}
if (!isset($this->index[$s])) return false;
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) return false;
return in_array($o, $this->index[$s][$p]);
}
function hasLiteralTriple($s, $p, $o) {
if (!isset($this->index[$s])) return false;
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) return false;
$os = $this->getObjects($s, $p, false);
foreach ($os as $object) {
if ($object['value'] == $o && $object['type'] == 'literal') {
return true;
}
}
return false;
}
function hasLinkTriple($s, $p, $o) {
if (!isset($this->index[$s])) return false;
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) return false;
$os = $this->getObjects($s, $p, false);
foreach ($os as $object) {
if ($object['value'] == $o && ($object['type'] == 'uri' || $object['type'] == 'bnode')) {
return true;
}
}
return false;
}
function addTriple($s, $p, $o, $oType = 'literal') {
$p = $this->expandPName($p);
if (!is_array($o)) $o = array('value' => $o, 'type' => $oType);
if ($this->hasTriple($s, $p, $o)) return;
if (!isset($this->index[$s])) $this->index[$s] = array();
if (!isset($this->index[$s][$p])) $this->index[$s][$p] = array();
$this->index[$s][$p][] = $o;
return $this;
}
function getSubjects($p = null, $o = null) {
if (!$p && !$o) return array_keys($this->index);
$result = array();
foreach ($this->index as $s => $ps) {
foreach ($ps as $predicate => $os) {
if ($p && $predicate != $p) continue;
foreach ($os as $object) {
if (!$o) {
$result[] = $s;
break;
}
else if (is_array($o) && $object == $o) {
$result[] = $s;
break;
}
else if ($o && $object['value'] == $o) {
$result[] = $s;
break;
}
}
}
}
return array_unique($result);
}
function getPredicates($s = null) {
$result = array();
$index = $s ? (array($s => isset($this->index[$s]) ? $this->index[$s] : array())) : $this->index;
foreach ($index as $subject => $ps) {
if ($s && $s != $subject) continue;
$result = array_merge($result, array_keys($ps));
}
return array_unique($result);
}
function getObjects($s, $p, $plain = false) {
if (!isset($this->index[$s])) return array();
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) return array();
$os = $this->index[$s][$p];
if ($plain) {
array_walk($os, function(&$o) {
$o = $o['value'];
});
}
return $os;
}
function getObject($s, $p, $plain = false, $default = null) {
$os = $this->getObjects($s, $p, $plain);
return empty($os) ? $default : $os[0];
}
function getNTriples() {
return parent::toNTriples($this->index, $this->ns);
}
function getTurtle() {
return parent::toTurtle($this->index, $this->ns);
}
function getRDFXML() {
return parent::toRDFXML($this->index, $this->ns);
}
function getJSON() {
return json_encode($this->index);
}
}
arc2-2.2.4/ARC2_Reader.php 0000775 0000000 0000000 00000037066 12252347775 0015065 0 ustar 00root root 0000000 0000000
* @homepage
* @package ARC2
* @version 2010-11-16
*/
ARC2::inc('Class');
class ARC2_Reader extends ARC2_Class {
function __construct($a, &$caller) {
parent::__construct($a, $caller);
}
function __init() {/* inc_path, proxy_host, proxy_port, proxy_skip, http_accept_header, http_user_agent_header, max_redirects */
parent::__init();
$this->http_method = $this->v('http_method', 'GET', $this->a);
$this->message_body = $this->v('message_body', '', $this->a);;
$this->http_accept_header = $this->v('http_accept_header', 'Accept: application/rdf+xml; q=0.9, text/turtle; q=0.8, */*; q=0.1', $this->a);
$this->http_user_agent_header = $this->v('http_user_agent_header', 'User-Agent: ARC Reader (http://arc.semsol.org/)', $this->a);
$this->http_custom_headers = $this->v('http_custom_headers', '', $this->a);
$this->max_redirects = $this->v('max_redirects', 3, $this->a);
$this->format = $this->v('format', false, $this->a);
$this->redirects = array();
$this->stream_id = '';
$this->timeout = $this->v('reader_timeout', 30, $this->a);
$this->response_headers = array();
$this->digest_auth = 0;
$this->auth_infos = $this->v('reader_auth_infos', array(), $this->a);
}
/* */
function setHTTPMethod($v) {
$this->http_method = $v;
}
function setMessageBody($v) {
$this->message_body = $v;
}
function setAcceptHeader($v) {
$this->http_accept_header = $v;
}
function setCustomHeaders($v) {
$this->http_custom_headers = $v;
}
function addCustomHeaders($v) {
if ($this->http_custom_headers) $this->http_custom_headers .= "\r\n";
$this->http_custom_headers .= $v;
}
/* */
function activate($path, $data = '', $ping_only = 0, $timeout = 0) {
$this->setCredentials($path);
$this->ping_only = $ping_only;
if ($timeout) $this->timeout = $timeout;
$id = md5($path . ' ' . $data);
if ($this->stream_id != $id) {
$this->stream_id = $id;
/* data uri? */
if (!$data && preg_match('/^data\:([^\,]+)\,(.*)$/', $path, $m)) {
$path = '';
$data = preg_match('/base64/', $m[1]) ? base64_decode($m[2]) : rawurldecode($m[2]);
}
$this->base = $this->calcBase($path);
$this->uri = $this->calcURI($path, $this->base);
$this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base, $ping_only);
if ($this->stream && !$this->ping_only) {
$this->getFormat();
}
}
}
/*
* HTTP Basic/Digest + Proxy authorization can be defined in the
* arc_reader_credentials config setting:
'arc_reader_credentials' => array(
'http://basic.example.com/' => 'user:pass', // shortcut for type=basic
'http://digest.example.com/' => 'user::pass', // shortcut for type=digest
'http://proxy.example.com/' => array('type' => 'basic', 'proxy', 'user' => 'user', 'pass' => 'pass'),
),
*/
function setCredentials($url) {
if (!$creds = $this->v('arc_reader_credentials', array(), $this->a)) return 0;
foreach ($creds as $pattern => $creds) {
/* digest shortcut (user::pass) */
if (!is_array($creds) && preg_match('/^(.+)\:\:(.+)$/', $creds, $m)) {
$creds = array('type' => 'digest', 'user' => $m[1], 'pass' => $m[2]);
}
/* basic shortcut (user:pass) */
if (!is_array($creds) && preg_match('/^(.+)\:(.+)$/', $creds, $m)) {
$creds = array('type' => 'basic', 'user' => $m[1], 'pass' => $m[2]);
}
if (!is_array($creds)) return 0;
$regex = '/' . preg_replace('/([\:\/\.\?])/', '\\\\\1', $pattern) . '/';
if (!preg_match($regex, $url)) continue;
$mthd = 'set' . $this->camelCase($creds['type']) . 'AuthCredentials';
if (method_exists($this, $mthd)) $this->$mthd($creds, $url);
}
}
function setBasicAuthCredentials($creds) {
$auth = 'Basic ' . base64_encode($creds['user'] . ':' . $creds['pass']);
$h = in_array('proxy', $creds) ? 'Proxy-Authorization' : 'Authorization';
$this->addCustomHeaders($h . ': ' . $auth);
//echo $h . ': ' . $auth . print_r($creds, 1);
}
function setDigestAuthCredentials($creds, $url) {
$path = $this->v1('path', '/', parse_url($url));
$auth = '';
$hs = $this->getResponseHeaders();
/* initial 401 */
$h = $this->v('www-authenticate', '', $hs);
if ($h && preg_match('/Digest/i', $h)) {
$auth = 'Digest ';
/* Digest realm="$realm", nonce="$nonce", qop="auth", opaque="$opaque" */
$ks = array('realm', 'nonce', 'opaque');/* skipping qop, assuming "auth" */
foreach ($ks as $i => $k) {
$$k = preg_match('/' . $k . '=\"?([^\"]+)\"?/i', $h, $m) ? $m[1] : '';
$auth .= ($i ? ', ' : '') . $k . '="' . $$k . '"';
$this->auth_infos[$k] = $$k;
}
$this->auth_infos['auth'] = $auth;
$this->auth_infos['request_count'] = 1;
}
/* initial 401 or repeated request */
if ($this->v('auth', 0, $this->auth_infos)) {
$qop = 'auth';
$auth = $this->auth_infos['auth'];
$rc = $this->auth_infos['request_count'];
$realm = $this->auth_infos['realm'];
$nonce = $this->auth_infos['nonce'];
$ha1 = md5($creds['user'] . ':' . $realm . ':' . $creds['pass']);
$ha2 = md5($this->http_method . ':' . $path);
$nc = dechex($rc);
$cnonce = dechex($rc * 2);
$resp = md5($ha1 . ':' . $nonce . ':' . $nc . ':' . $cnonce . ':' . $qop . ':' . $ha2);
$auth .= ', username="' . $creds['user'] . '"' .
', uri="' . $path . '"' .
', qop=' . $qop . '' .
', nc=' . $nc .
', cnonce="' . $cnonce . '"' .
', uri="' . $path . '"' .
', response="' . $resp . '"' .
'';
$this->auth_infos['request_count'] = $rc + 1;
}
if (!$auth) return 0;
$h = in_array('proxy', $creds) ? 'Proxy-Authorization' : 'Authorization';
$this->addCustomHeaders($h . ': ' . $auth);
}
/* */
function useProxy($url) {
if (!$this->v1('proxy_host', 0, $this->a)) {
return false;
}
$skips = $this->v1('proxy_skip', array(), $this->a);
foreach ($skips as $skip) {
if (strpos($url, $skip) !== false) {
return false;
}
}
return true;
}
/* */
function createStream($path, $data = '') {
$this->base = $this->calcBase($path);
$this->stream = ($data) ? $this->getDataStream($data) : $this->getSocketStream($this->base);
}
function getDataStream($data) {
return array('type' => 'data', 'pos' => 0, 'headers' => array(), 'size' => strlen($data), 'data' => $data, 'buffer' => '');
}
function getSocketStream($url) {
if ($url == 'file://') {
return $this->addError('Error: file does not exists or is not accessible');
}
$parts = parse_url($url);
$mappings = array('file' => 'File', 'http' => 'HTTP', 'https' => 'HTTP');
if ($scheme = $this->v(strtolower($parts['scheme']), '', $mappings)) {
return $this->m('get' . $scheme . 'Socket', $url, $this->getDataStream(''));
}
}
function getFileSocket($url) {
$parts = parse_url($url);
$s = file_exists($parts['path']) ? @fopen($parts['path'], 'rb') : false;
if (!$s) {
return $this->addError('Socket error: Could not open "' . $parts['path'] . '"');
}
return array('type' => 'socket', 'socket' =>& $s, 'headers' => array(), 'pos' => 0, 'size' => filesize($parts['path']), 'buffer' => '');
}
function getHTTPSocket($url, $redirs = 0, $prev_parts = '') {
$parts = parse_url($url);
/* relative redirect */
if (!isset($parts['scheme']) && $prev_parts) $parts['scheme'] = $prev_parts['scheme'];
if (!isset($parts['host']) && $prev_parts) $parts['host'] = $prev_parts['host'];
/* no scheme */
if (!$this->v('scheme', '', $parts)) return $this->addError('Socket error: Missing URI scheme.');
/* port tweaks */
$parts['port'] = ($parts['scheme'] == 'https') ? $this->v1('port', 443, $parts) : $this->v1('port', 80, $parts);
$nl = "\r\n";
$http_mthd = strtoupper($this->http_method);
if ($this->v1('user', 0, $parts) || $this->useProxy($url)) {
$h_code = $http_mthd . ' ' . $url;
}
else {
$h_code = $http_mthd . ' ' . $this->v1('path', '/', $parts) . (($v = $this->v1('query', 0, $parts)) ? '?' . $v : '') . (($v = $this->v1('fragment', 0, $parts)) ? '#' . $v : '');
}
$scheme_default_port = ($parts['scheme'] == 'https') ? 443 : 80;
$port_code = ($parts['port'] != $scheme_default_port) ? ':' . $parts['port'] : '';
$h_code .= ' HTTP/1.0' . $nl.
'Host: ' . $parts['host'] . $port_code . $nl .
(($v = $this->http_accept_header) ? $v . $nl : '') .
(($v = $this->http_user_agent_header) && !preg_match('/User\-Agent\:/', $this->http_custom_headers) ? $v . $nl : '') .
(($http_mthd == 'POST') ? 'Content-Length: ' . strlen($this->message_body) . $nl : '') .
($this->http_custom_headers ? trim($this->http_custom_headers) . $nl : '') .
$nl .
'';
/* post body */
if ($http_mthd == 'POST') {
$h_code .= $this->message_body . $nl;
}
/* connect */
if ($this->useProxy($url)) {
$s = @fsockopen($this->a['proxy_host'], $this->a['proxy_port'], $errno, $errstr, $this->timeout);
}
elseif (($parts['scheme'] == 'https') && function_exists('stream_socket_client')) {
// SSL options via config array, code by Hannes Muehleisen (muehleis@informatik.hu-berlin.de)
$context = stream_context_create();
foreach ($this->a as $k => $v) {
if (preg_match('/^arc_reader_ssl_(.+)$/', $k, $m)) {
stream_context_set_option($context, 'ssl', $m[1], $v);
}
}
$s = stream_socket_client('ssl://' . $parts['host'] . ":" . $parts['port'], $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context);
}
elseif ($parts['scheme'] == 'https') {
$s = @fsockopen('ssl://' . $parts['host'], $parts['port'], $errno, $errstr, $this->timeout);
}
elseif ($parts['scheme'] == 'http') {
$s = @fsockopen($parts['host'], $parts['port'], $errno, $errstr, $this->timeout);
}
if (!$s) {
return $this->addError('Socket error: Could not connect to "' . $url . '" (proxy: ' . ($this->useProxy($url) ? '1' : '0') . '): ' . $errstr);
}
/* request */
fwrite($s, $h_code);
/* timeout */
if ($this->timeout) {
//stream_set_blocking($s, false);
stream_set_timeout($s, $this->timeout);
}
/* response headers */
$h = array();
$this->response_headers = $h;
if (!$this->ping_only) {
do {
$line = trim(fgets($s, 4096));
$info = stream_get_meta_data($s);
if (preg_match("/^HTTP[^\s]+\s+([0-9]{1})([0-9]{2})(.*)$/i", $line, $m)) {/* response code */
$error = in_array($m[1], array('4', '5')) ? $m[1] . $m[2] . ' ' . $m[3] : '';
$error = ($m[1].$m[2] == '304') ? '304 '.$m[3] : $error;
$h['response-code'] = $m[1] . $m[2];
$h['error'] = $error;
$h['redirect'] = ($m[1] == '3') ? true : false;
}
elseif (preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $m)) {/* header */
$h_name = strtolower($m[1]);
if (!isset($h[$h_name])) {/* 1st value */
$h[$h_name] = trim($m[2]);
}
elseif (!is_array($h[$h_name])) {/* 2nd value */
$h[$h_name] = array($h[$h_name], trim($m[2]));
}
else {/* more values */
$h[$h_name][] = trim($m[2]);
}
}
} while(!$info['timed_out'] && !feof($s) && $line);
$h['format'] = strtolower(preg_replace('/^([^\s]+).*$/', '\\1', $this->v('content-type', '', $h)));
$h['encoding'] = preg_match('/(utf\-8|iso\-8859\-1|us\-ascii)/', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : '';
$h['encoding'] = preg_match('/charset=\s*([^\s]+)/si', $this->v('content-type', '', $h), $m) ? strtoupper($m[1]) : $h['encoding'];
$this->response_headers = $h;
/* result */
if ($info['timed_out']) {
return $this->addError('Connection timed out after ' . $this->timeout . ' seconds');
}
/* error */
if ($v = $this->v('error', 0, $h)) {
/* digest auth */
/* 401 received */
if (preg_match('/Digest/i', $this->v('www-authenticate', '', $h)) && !$this->digest_auth) {
$this->setCredentials($url);
$this->digest_auth = 1;
return $this->getHTTPSocket($url);
}
return $this->addError($error . ' "' . (!feof($s) ? trim(strip_tags(fread($s, 128))) . '..."' : ''));
}
/* redirect */
if ($this->v('redirect', 0, $h) && ($new_url = $this->v1('location', 0, $h))) {
fclose($s);
$this->redirects[$url] = $new_url;
$this->base = $new_url;
if ($redirs > $this->max_redirects) {
return $this->addError('Max numbers of redirects exceeded.');
}
return $this->getHTTPSocket($new_url, $redirs+1, $parts);
}
}
if ($this->timeout) {
stream_set_blocking($s, true);
}
return array('type' => 'socket', 'url' => $url, 'socket' =>& $s, 'headers' => $h, 'pos' => 0, 'size' => $this->v('content-length', 0, $h), 'buffer' => '');
}
function readStream($buffer_xml = true, $d_size = 1024) {
//if (!$s = $this->v('stream')) return '';
if (!$s = $this->v('stream')) return $this->addError('missing stream in "readStream" ' . $this->uri);
$s_type = $this->v('type', '', $s);
$r = $s['buffer'];
$s['buffer'] = '';
if ($s['size']) $d_size = min($d_size, $s['size'] - $s['pos']);
/* data */
if ($s_type == 'data') {
$d = ($d_size > 0) ? substr($s['data'], $s['pos'], $d_size) : '';
}
/* socket */
elseif ($s_type == 'socket') {
$d = ($d_size > 0) && !feof($s['socket']) ? fread($s['socket'], $d_size) : '';
}
$eof = $d ? false : true;
/* chunked despite HTTP 1.0 request */
if (isset($s['headers']) && isset($s['headers']['transfer-encoding']) && ($s['headers']['transfer-encoding'] == 'chunked')) {
$d = preg_replace('/(^|[\r\n]+)[0-9a-f]{1,4}[\r\n]+/', '', $d);
}
$s['pos'] += strlen($d);
if ($buffer_xml) {/* stop after last closing xml tag (if available) */
if (preg_match('/^(.*\>)([^\>]*)$/s', $d, $m)) {
$d = $m[1];
$s['buffer'] = $m[2];
}
elseif (!$eof) {
$s['buffer'] = $r . $d;
$this->stream = $s;
return $this->readStream(true, $d_size);
}
}
$this->stream = $s;
return $r . $d;
}
function closeStream() {
if (isset($this->stream)) {
if ($this->v('type', 0, $this->stream) == 'socket' && !empty($this->stream['socket'])) {
@fclose($this->stream['socket']);
}
unset($this->stream);
}
}
/* */
function getFormat() {
if (!$this->format) {
if (!$this->v('stream')) {
return $this->addError('missing stream in "getFormat"');
}
$v = $this->readStream(false);
$mtype = $this->v('format', '', $this->stream['headers']);
$this->stream['buffer'] = $v . $this->stream['buffer'];
$ext = preg_match('/\.([^\.]+)$/', $this->uri, $m) ? $m[1] : '';
$this->format = ARC2::getFormat($v, $mtype, $ext);
}
return $this->format;
}
/* */
function getResponseHeaders() {
if (isset($this->stream) && isset($this->stream['headers'])) {
return $this->stream['headers'];
}
return $this->response_headers;
}
function getEncoding($default = 'UTF-8') {
return $this->v1('encoding', $default, $this->stream['headers']);
}
function getRedirects() {
return $this->redirects;
}
function getAuthInfos() {
return $this->auth_infos;
}
/* */
}
arc2-2.2.4/ARC2_Resource.php 0000664 0000000 0000000 00000007020 12252347775 0015432 0 ustar 00root root 0000000 0000000
* @license http://arc.semsol.org/license
* @homepage
* @package ARC2
* @version 2011-01-19
*/
ARC2::inc('Class');
class ARC2_Resource extends ARC2_Class {
function __construct($a, &$caller) {
parent::__construct($a, $caller);
}
function __init() {
parent::__init();
$this->uri = '';
$this->index = array();
$this->fetched = array();
$this->store = '';
}
/* */
function setURI($uri) {
$this->uri = $uri;
}
function setIndex($index) {
$this->index = $index;
}
function getIndex() {
return $this->index;
}
function setProps($props, $s = '') {
if (!$s) $s = $this->uri;
$this->index[$s] = $props;
}
function setProp($p, $os, $s = '') {
if (!$s) $s = $this->uri;
/* single plain value */
if (!is_array($os)) $os = array('value' => $os, 'type' => 'literal');
/* single array value */
if (isset($os['value'])) $os = array($os);
/* list of values */
foreach ($os as $i => $o) {
if (!is_array($o)) $os[$i] = array('value' => $o, 'type' => 'literal');
}
$this->index[$s][$this->expandPName($p)] = $os;
}
/* add a relation to a URI. Allows for instance $res->setRel('rdf:type', 'doap:Project') */
function setRel($p, $r, $s = '') {
if(!is_array($r)) {
$uri = array (
'type' => 'uri',
'value' => $this->expandPName($r));
$this->setProp($p, $uri, $s);
} else {
if (!$s) $s = $this->uri;
foreach($r as $i => $x) {
if(!is_array($x)) {
$uri = array (
'type' => 'uri',
'value' => $this->expandPName($x));
$r[$i] = $uri;
}
}
$this->index[$s][$this->expandPName($p)] = $r;
}
}
/* Specialize setProp to set an xsd:dateTime typed literal. Example : $res->setPropXSDdateTime('dcterms:created', date('c')) */
function setPropXSDdateTime($p, $dt, $s = '') {
$datecreated=array('value' => $dt,
'type' => 'literal',
'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime');
$this->setProp($p, $datecreated, $s);
}
function setStore($store) {
$this->store = $store;
}
/* */
function fetchData($uri = '') {
if (!$uri) $uri = $this->uri;
if (!$uri) return 0;
if (in_array($uri, $this->fetched)) return 0;
$this->index[$uri] = array();
if ($this->store) {
$index = $this->store->query('CONSTRUCT { <' . $uri . '> ?p ?o . } WHERE { <' . $uri . '> ?p ?o . } ', 'raw');
}
else {
$index = $this->toIndex($uri);
}
$this->index = ARC2::getMergedIndex($this->index, $index);
$this->fetched[] = $uri;
}
/* */
function getProps($p = '', $s = '') {
if (!$s) $s = $this->uri;
if (!$s) return array();
if (!isset($this->index[$s])) $this->fetchData($s);
if (!$p) return $this->index[$s];
return $this->v($this->expandPName($p), array(), $this->index[$s]);
}
function getProp($p, $s = '') {
$props = $this->getProps($p, $s);
return $props ? $props[0] : '';
}
function getPropValue($p, $s = '') {
$prop = $this->getProp($p, $s);
return $prop ? $prop['value'] : '';
}
function getPropValues($p, $s = '') {
$r = array();
$props = $this->getProps($p, $s);
foreach ($props as $prop) {
$r[] = $prop['value'];
}
return $r;
}
function hasPropValue($p, $o, $s = '') {
$props = $this->getProps($p, $s);
$o = $this->expandPName($o);
foreach ($props as $prop) {
if ($prop['value'] == $o) return 1;
}
return 0;
}
/* */
}
arc2-2.2.4/ARC2_getFormat.php 0000775 0000000 0000000 00000005707 12252347775 0015610 0 ustar 00root root 0000000 0000000
* @license http://arc.semsol.org/license
* @package ARC2
* @version 2010-11-16
*/
function ARC2_getFormat($v, $mtype = '', $ext = '') {
$r = false;
/* mtype check (atom, rdf/xml, turtle, n3, mp3, jpg) */
$r = (!$r && preg_match('/\/atom\+xml/', $mtype)) ? 'atom' : $r;
$r = (!$r && preg_match('/\/rdf\+xml/', $mtype)) ? 'rdfxml' : $r;
$r = (!$r && preg_match('/\/(x\-)?turtle/', $mtype)) ? 'turtle' : $r;
$r = (!$r && preg_match('/\/rdf\+n3/', $mtype)) ? 'n3' : $r;
$r = (!$r && preg_match('/\/sparql-results\+xml/', $mtype)) ? 'sparqlxml' : $r;
/* xml sniffing */
if (
!$r &&
/* starts with angle brackets */
preg_match('/^\s*\<[^\s]/s', $v) &&
/* has an xmlns:* declaration or a matching pair of tags */
(preg_match('/\sxmlns\:?/', $v) || preg_match('/\<([^\s]+).+\<\/\\1\>/s', $v)) // &&
) {
while (preg_match('/^\s*\<\?xml[^\r\n]+\?\>\s*/s', $v)) {
$v = preg_replace('/^\s*\<\?xml[^\r\n]+\?\>\s*/s', '', $v);
}
while (preg_match('/^\s*\<\!--.+?--\>\s*/s', $v)) {
$v = preg_replace('/^\s*\<\!--.+?--\>\s*/s', '', $v);
}
/* doctype checks (html, rdf) */
$r = (!$r && preg_match('/^\s*\<\!DOCTYPE\s+html[\s|\>]/is', $v)) ? 'html' : $r;
$r = (!$r && preg_match('/^\s*\<\!DOCTYPE\s+[a-z0-9\_\-]\:RDF\s/is', $v)) ? 'rdfxml' : $r;
/* markup checks */
$v = preg_replace('/^\s*\<\!DOCTYPE\s.*\]\>/is', '', $v);
$r = (!$r && preg_match('/^\s*\]*version/s', $v)) ? 'rss' : $r;
$r = (!$r && preg_match('/^\s*\]+http\:\/\/www\.w3\.org\/2005\/Atom/s', $v)) ? 'atom' : $r;
$r = (!$r && preg_match('/^\s*\]/is', $v)) ? 'html' : $r;
$r = (!$r && preg_match('/^\s*\]+http\:\/\/www\.w3\.org\/2005\/sparql\-results\#/s', $v)) ? 'sparqlxml' : $r;
$r = (!$r && preg_match('/^\s*\<[^\>]+http\:\/\/www\.w3\.org\/2005\/sparql\-results#/s', $v)) ? 'srx' : $r;
$r = (!$r && preg_match('/^\s*\<[^\s]*RDF[\s\>]/s', $v)) ? 'rdfxml' : $r;
$r = (!$r && preg_match('/^\s*\<[^\>]+http\:\/\/www\.w3\.org\/1999\/02\/22\-rdf/s', $v)) ? 'rdfxml' : $r;
$r = !$r ? 'xml' : $r;
}
/* json|jsonp */
if (!$r && preg_match('/^[a-z0-9\.\(]*\s*[\{\[].*/s', trim($v))) {
/* google social graph api */
$r = (!$r && preg_match('/\"canonical_mapping\"/', $v)) ? 'sgajson' : $r;
/* crunchbase api */
$r = (!$r && preg_match('/\"permalink\"/', $v)) ? 'cbjson' : $r;
$r = !$r ? 'json' : $r;
}
/* turtle/n3 */
$r = (!$r && preg_match('/\@(prefix|base)/i', $v)) ? 'turtle' : $r;
$r = (!$r && preg_match('/^(ttl)$/', $ext)) ? 'turtle' : $r;
$r = (!$r && preg_match('/^(n3)$/', $ext)) ? 'n3' : $r;
/* ntriples */
$r = (!$r && preg_match('/^\s*(_:|<).+?\s+<[^>]+?>\s+\S.+?\s*\.\s*$/sm', $v)) ? 'ntriples' : $r;
$r = (!$r && preg_match('/^(nt)$/', $ext)) ? 'ntriples' : $r;
return $r;
}
arc2-2.2.4/ARC2_getPreferredFormat.php 0000775 0000000 0000000 00000002527 12252347775 0017444 0 ustar 00root root 0000000 0000000
* @homepage
* @package ARC2
* @version 2010-11-16
*/
function ARC2_getPreferredFormat($default = 'plain') {
$formats = array(
'html' => 'HTML', 'text/html' => 'HTML', 'xhtml+xml' => 'HTML',
'rdfxml' => 'RDFXML', 'rdf+xml' => 'RDFXML',
'ntriples' => 'NTriples',
'rdf+n3' => 'Turtle', 'x-turtle' => 'Turtle', 'turtle' => 'Turtle', 'text/turtle' => 'Turtle',
'rdfjson' => 'RDFJSON', 'json' => 'RDFJSON',
'xml' => 'XML',
'legacyjson' => 'LegacyJSON'
);
$prefs = array();
$o_vals = array();
/* accept header */
$vals = explode(',', $_SERVER['HTTP_ACCEPT']);
if ($vals) {
foreach ($vals as $val) {
if (preg_match('/(rdf\+n3|(x\-|text\/)turtle|rdf\+xml|text\/html|xhtml\+xml|xml|json)/', $val, $m)) {
$o_vals[$m[1]] = 1;
if (preg_match('/\;q\=([0-9\.]+)/', $val, $sub_m)) {
$o_vals[$m[1]] = 1 * $sub_m[1];
}
}
}
}
/* arg */
if (isset($_GET['format'])) $o_vals[$_GET['format']] = 1.1;
/* rank */
arsort($o_vals);
foreach ($o_vals as $val => $prio) {
$prefs[] = $val;
}
/* default */
$prefs[] = $default;
foreach ($prefs as $pref) {
if (isset($formats[$pref])) {
return $formats[$pref];
}
}
}
arc2-2.2.4/README.md 0000664 0000000 0000000 00000000427 12252347775 0013646 0 ustar 00root root 0000000 0000000 ARC2
====
ARC2 is a PHP 5.3 library for working with RDF.
It also provides a MySQL-based triplestore with SPARQL support.
Feature-wise, ARC2 is now in a stable state with no further feature additions planned.
Issues are still being fixed and Pull Requests are welcome, though. arc2-2.2.4/build.xml 0000664 0000000 0000000 00000001300 12252347775 0014177 0 ustar 00root root 0000000 0000000