PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/arc/ARC2_Class.php

https://github.com/damz/foafssl-drupal
PHP | 406 lines | 325 code | 52 blank | 29 comment | 70 complexity | 634db13e9dedcf52bb457a168b39b7a8 MD5 | raw file
  1. <?php
  2. /**
  3. * ARC2 base class
  4. *
  5. * @author Benjamin Nowack
  6. * @license <http://arc.semsol.org/license>
  7. * @homepage <http://arc.semsol.org/>
  8. * @package ARC2
  9. * @version 2009-09-23
  10. */
  11. class ARC2_Class {
  12. /* */
  13. function __construct($a = '', &$caller) {
  14. $a = is_array($a) ? $a : array();
  15. $this->a = $a;
  16. $this->caller = &$caller;
  17. $this->__init();
  18. }
  19. function ARC2_Class($a = '', &$caller) {
  20. $this->__construct($a, $caller);
  21. }
  22. function __destruct() {
  23. //echo "\ndestructing " . get_class($this);
  24. }
  25. function __init() {/* base, time_limit */
  26. if (!$_POST && isset($GLOBALS['HTTP_RAW_POST_DATA'])) parse_str($GLOBALS['HTTP_RAW_POST_DATA'], $_POST); /* php5 bug */
  27. $this->inc_path = ARC2::getIncPath();
  28. $this->ns_count = 0;
  29. $this->nsp = array('http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf');
  30. $this->used_ns = array('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
  31. $this->ns = $this->v('ns', array(), $this->a);
  32. $this->base = $this->v('base', ARC2::getRequestURI(), $this->a);
  33. $this->errors = array();
  34. $this->warnings = array();
  35. $this->adjust_utf8 = $this->v('adjust_utf8', 0, $this->a);
  36. $this->max_errors = $this->v('max_errors', 25, $this->a);
  37. }
  38. /* */
  39. function v($name, $default = false, $o = false) {/* value if set */
  40. $o = ($o !== false) ? $o : $this;
  41. if (is_array($o)) {
  42. return isset($o[$name]) ? $o[$name] : $default;
  43. }
  44. return isset($o->$name) ? $o->$name : $default;
  45. }
  46. function v1($name, $default = false, $o = false) {/* value if 1 (= not empty) */
  47. $o = ($o !== false) ? $o : $this;
  48. if (is_array($o)) {
  49. return (isset($o[$name]) && $o[$name]) ? $o[$name] : $default;
  50. }
  51. return (isset($o->$name) && $o->$name) ? $o->$name : $default;
  52. }
  53. function m($name, $a = false, $default = false, $o = false) {/* call method */
  54. $o = ($o !== false) ? $o : $this;
  55. return method_exists($o, $name) ? $o->$name($a) : $default;
  56. }
  57. /* */
  58. function camelCase($v, $lc_first = 0) {
  59. $r = ucfirst($v);
  60. while (preg_match('/^(.*)[\-\_ ](.*)$/', $r, $m)) {
  61. $r = $m[1] . ucfirst($m[2]);
  62. }
  63. return $lc_first ? strtolower(substr($r, 0, 1)) . substr($r, 1) : $r;
  64. }
  65. /* */
  66. function addError($v) {
  67. if (!in_array($v, $this->errors)) {
  68. $this->errors[] = $v;
  69. }
  70. if ($this->caller && method_exists($this->caller, 'addError')) {
  71. $glue = strpos($v, ' in ') ? ' via ' : ' in ';
  72. $this->caller->addError($v . $glue . get_class($this));
  73. }
  74. if (count($this->errors) > $this->max_errors) {
  75. die('Too many errors: ' . print_r($this->errors, 1));
  76. }
  77. return false;
  78. }
  79. function getErrors() {
  80. return $this->errors;
  81. }
  82. function getWarnings() {
  83. return $this->warnings;
  84. }
  85. function resetErrors() {
  86. $this->errors = array();
  87. if ($this->caller && method_exists($this->caller, 'resetErrors')) {
  88. $this->caller->resetErrors();
  89. }
  90. }
  91. /* */
  92. function splitURI($v) {
  93. return ARC2::splitURI($v);
  94. }
  95. /* */
  96. function getPName($v, $connector = ':') {
  97. /* is already a pname */
  98. if ($ns = $this->getPNameNamespace($v)) {
  99. if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
  100. return $v;
  101. }
  102. /* new pname */
  103. if ($parts = $this->splitURI($v)) {
  104. /* known prefix */
  105. foreach ($this->ns as $prefix => $ns) {
  106. if ($parts[0] == $ns) {
  107. if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
  108. return $prefix . $connector . $parts[1];
  109. }
  110. }
  111. /* new prefix */
  112. $prefix = $this->getPrefix($parts[0]);
  113. return $prefix . $connector . $parts[1];
  114. }
  115. return $v;
  116. }
  117. function getPNameNamespace($v) {
  118. if (!preg_match('/^([a-z0-9\_\-]+)\:([a-z0-9\_\-\.\%]*)$/i', $v, $m)) return 0;
  119. if (!isset($this->ns[$m[1]])) return 0;
  120. return $this->ns[$m[1]];
  121. }
  122. function getPrefix($ns) {
  123. if (!isset($this->nsp[$ns])) {
  124. $this->ns['ns' . $this->ns_count] = $ns;
  125. $this->nsp[$ns] = 'ns' . $this->ns_count;
  126. $this->ns_count++;
  127. }
  128. if (!in_array($ns, $this->used_ns)) $this->used_ns[] = $ns;
  129. return $this->nsp[$ns];
  130. }
  131. function expandPName($v, $connector = ':') {
  132. $re = '/^([a-z0-9\_\-]+)\:([a-z0-9\_\-]+)$/i';
  133. if ($connector == '-') {
  134. $re = '/^([a-z0-9\_]+)\-([a-z0-9\_\-]+)$/i';
  135. }
  136. if (preg_match($re, $v, $m) && isset($this->ns[$m[1]])) {
  137. return $this->ns[$m[1]] . $m[2];
  138. }
  139. return $v;
  140. }
  141. function expandPNames($index) {
  142. $r = array();
  143. foreach ($index as $s => $ps) {
  144. $s = $this->expandPName($s);
  145. $r[$s] = array();
  146. foreach ($ps as $p => $os) {
  147. $p = $this->expandPName($p);
  148. if (!is_array($os)) $os = array($os);
  149. foreach ($os as $i => $o) {
  150. if (!is_array($o)) {
  151. $o_val = $this->expandPName($o);
  152. $o_type = preg_match('/^[a-z]+\:[^\s]+$/si', $o_val) ? 'uri' : 'literal';
  153. $o = array('value' => $o_val, 'type' => $o_type);
  154. }
  155. $os[$i] = $o;
  156. }
  157. $r[$s][$p] = $os;
  158. }
  159. }
  160. return $r;
  161. }
  162. /* */
  163. function calcURI($path, $base = "") {
  164. /* quick check */
  165. if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */
  166. return $path;
  167. }
  168. if (preg_match('/^\$\{.*\}/', $path)) {/* placeholder, assume abs URI */
  169. return $path;
  170. }
  171. if (preg_match("/^\/\//", $path)) {/* net path, assume http */
  172. return 'http:' . $path;
  173. }
  174. /* other URIs */
  175. $base = $base ? $base : $this->base;
  176. $base = preg_replace('/\#.*$/', '', $base);
  177. if ($path === true) {/* empty (but valid) URIref via turtle parser: <> */
  178. return $base;
  179. }
  180. $path = preg_replace("/^\.\//", '', $path);
  181. $root = preg_match('/(^[a-z0-9]+\:[\/]{1,3}[^\/]+)[\/|$]/i', $base, $m) ? $m[1] : $base; /* w/o trailing slash */
  182. $base .= ($base == $root) ? '/' : '';
  183. if (preg_match('/^\//', $path)) {/* leading slash */
  184. return $root . $path;
  185. }
  186. if (!$path) {
  187. return $base;
  188. }
  189. if (preg_match('/^([\#\?])/', $path, $m)) {
  190. return preg_replace('/\\' .$m[1]. '.*$/', '', $base) . $path;
  191. }
  192. if (preg_match('/^(\&)(.*)$/', $path, $m)) {/* not perfect yet */
  193. return preg_match('/\?/', $base) ? $base . $m[1] . $m[2] : $base . '?' . $m[2];
  194. }
  195. if (preg_match("/^[a-z0-9]+\:/i", $path)) {/* abs path */
  196. return $path;
  197. }
  198. /* rel path: remove stuff after last slash */
  199. $base = substr($base, 0, strrpos($base, '/')+1);
  200. /* resolve ../ */
  201. while (preg_match('/^(\.\.\/)(.*)$/', $path, $m)) {
  202. $path = $m[2];
  203. $base = ($base == $root.'/') ? $base : preg_replace('/^(.*\/)[^\/]+\/$/', '\\1', $base);
  204. }
  205. return $base . $path;
  206. }
  207. /* */
  208. function calcBase($path) {
  209. $r = $path;
  210. $r = preg_replace('/\#.*$/', '', $r);/* remove hash */
  211. $r = preg_replace('/^\/\//', 'http://', $r);/* net path (//), assume http */
  212. if (preg_match('/^[a-z0-9]+\:/', $r)) {/* scheme, abs path */
  213. while (preg_match('/^(.+\/)(\.\.\/.*)$/U', $r, $m)) {
  214. $r = $this->calcURI($m[1], $m[2]);
  215. }
  216. return $r;
  217. }
  218. return 'file://' . realpath($r);/* real path */
  219. }
  220. /* */
  221. function getResource($uri, $store_or_props = '') {
  222. $res = ARC2::getResource($this->a);
  223. $res->setURI($uri);
  224. if (is_array($store_or_props)) {
  225. $res->setProps($store_or_props);
  226. }
  227. else {
  228. $res->setStore($store_or_props);
  229. }
  230. return $res;
  231. }
  232. function toIndex($v) {
  233. if (is_array($v)) {
  234. if (isset($v[0]) && isset($v[0]['s'])) return ARC2::getSimpleIndex($v, 0);
  235. return $v;
  236. }
  237. $parser = ARC2::getRDFParser($this->a);
  238. if ($v && !preg_match('/\s/', $v)) {/* assume graph URI */
  239. $parser->parse($v);
  240. }
  241. else {
  242. $parser->parse('', $v);
  243. }
  244. return $parser->getSimpleIndex(0);
  245. }
  246. function toTriples($v) {
  247. if (is_array($v)) {
  248. if (isset($v[0]) && isset($v[0]['s'])) return $v;
  249. return ARC2::getTriplesFromIndex($v);
  250. }
  251. $parser = ARC2::getRDFParser($this->a);
  252. if ($v && !preg_match('/\s/', $v)) {/* assume graph URI */
  253. $parser->parse($v);
  254. }
  255. else {
  256. $parser->parse('', $v);
  257. }
  258. return $parser->getTriples();
  259. }
  260. /* */
  261. function toNTriples($v, $ns = '', $raw = 0) {
  262. ARC2::inc('NTriplesSerializer');
  263. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  264. $ser = new ARC2_NTriplesSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  265. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  266. }
  267. function toTurtle($v, $ns = '', $raw = 0) {
  268. ARC2::inc('TurtleSerializer');
  269. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  270. $ser = new ARC2_TurtleSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  271. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  272. }
  273. function toRDFXML($v, $ns = '', $raw = 0) {
  274. ARC2::inc('RDFXMLSerializer');
  275. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  276. $ser = new ARC2_RDFXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  277. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v, $raw) : $ser->getSerializedIndex($v, $raw);
  278. }
  279. function toRDFJSON($v, $ns = '') {
  280. ARC2::inc('RDFJSONSerializer');
  281. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  282. $ser = new ARC2_RDFJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  283. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
  284. }
  285. function toLegacyXML($v, $ns = '') {
  286. ARC2::inc('LegacyXMLSerializer');
  287. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  288. $ser = new ARC2_LegacyXMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  289. return $ser->getSerializedArray($v);
  290. }
  291. function toLegacyJSON($v, $ns = '') {
  292. ARC2::inc('LegacyJSONSerializer');
  293. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  294. $ser = new ARC2_LegacyJSONSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  295. return $ser->getSerializedArray($v);
  296. }
  297. function toLegacyHTML($v, $ns = '') {
  298. ARC2::inc('LegacyHTMLSerializer');
  299. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  300. $ser = new ARC2_LegacyHTMLSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  301. return $ser->getSerializedArray($v);
  302. }
  303. function toHTML($v, $ns = '') {
  304. ARC2::inc('POSHRDFSerializer');
  305. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  306. $ser = new ARC2_POSHRDFSerializer(array_merge($this->a, array('ns' => $ns)), $this);
  307. return (isset($v[0]) && isset($v[0]['s'])) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v);
  308. }
  309. /* */
  310. function getFilledTemplate($t, $vals, $g = '') {
  311. $parser = ARC2::getTurtleParser();
  312. $parser->parse($g, $this->getTurtleHead() . $t);
  313. return $parser->getSimpleIndex(0, $vals);
  314. }
  315. function getTurtleHead() {
  316. $r = '';
  317. $ns = $this->v('ns', array(), $this->a);
  318. foreach ($ns as $k => $v) {
  319. $r .= "@prefix " . $k . ": <" .$v. "> .\n";
  320. }
  321. return $r;
  322. }
  323. function completeQuery($q, $ns = '') {
  324. if (!$ns) $ns = isset($this->a['ns']) ? $this->a['ns'] : array();
  325. $added_prefixes = array();
  326. $prologue = '';
  327. foreach ($ns as $k => $v) {
  328. $k = rtrim($k, ':');
  329. if (in_array($k, $added_prefixes)) continue;
  330. if (preg_match('/(^|\s)' . $k . ':/s', $q) && !preg_match('/PREFIX\s+' . $k . '\:/is', $q)) {
  331. $prologue .= "\n" . 'PREFIX ' . $k . ': <' . $v . '>';
  332. }
  333. $added_prefixes[] = $k;
  334. }
  335. return $prologue . "\n" . $q;
  336. }
  337. /* */
  338. function toUTF8($str) {
  339. return $this->adjust_utf8 ? ARC2::toUTF8($str) : $str;
  340. }
  341. function toDataURI($str) {
  342. return 'data:text/plain;charset=utf-8,' . rawurlencode($str);
  343. }
  344. function fromDataURI($str) {
  345. return str_replace('data:text/plain;charset=utf-8,', '', rawurldecode($str));
  346. }
  347. /* prevent SQL injections via SPARQL REGEX */
  348. function checkRegex($str) {
  349. return addslashes($str);
  350. }
  351. /* */
  352. }