/DemiBlog/lib/arc/ARC2_Class.php

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