PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/connectors/openbiblio/index.php

https://github.com/rsinger/Jangle
PHP | 384 lines | 358 code | 26 blank | 0 comment | 107 complexity | e094816848a7deafe69be12730c1f758 MD5 | raw file
  1. <?php
  2. require 'lib/jangle.php';
  3. require 'lib/spyc.php5';
  4. require 'lib/models.php';
  5. $config = Spyc::YAMLLoad('config/config.yml');
  6. $config["base_uri"] = getenv('HTTP_X_CONNECTOR_BASE');
  7. $url = parse_url($_SERVER['REQUEST_URI']);
  8. $base_path = explode("/",str_replace($config["global_options"]["path_to_connector"],"",$url["path"]));
  9. $path = array();
  10. foreach($base_path as $p) {
  11. if($p) {
  12. array_push($path, $p);
  13. }
  14. }
  15. $request_uri = $config["base_uri"]."/".join($path,"/");
  16. if(preg_match("/\/$/", $url["path"])) {
  17. $request_uri .= "/";
  18. }
  19. if(count($_GET) > 0) {
  20. $request_uri .= "?".http_build_query($_GET);
  21. }
  22. $explain = false;
  23. $search = false;
  24. $_REQUEST['resource'] = $path[0];
  25. if($path[1]) {
  26. if($path[1] == "-") {
  27. $_REQUEST["category"] = $path[2];
  28. } elseif($path[1] == "search") {
  29. if($path[2] == "description") {
  30. $explain = true;
  31. } else {
  32. $search = true;
  33. }
  34. } else {
  35. $_REQUEST["id"] = $path[1];
  36. if($path[2]) {
  37. $_REQUEST["relation"] = $path[2];
  38. }
  39. if($path[3]) {
  40. $_REQUEST["category"] = $path[4];
  41. }
  42. }
  43. }
  44. checkRequestSanity($_REQUEST, $explain, $search, $config);
  45. if(getenv('HTTP_ACCEPT') == "application/json") {
  46. header("Content-type: application/json");
  47. } elseif (getenv('HTTP_ACCEPT') == "text/json") {
  48. header("Content-type: text/json");
  49. } else {
  50. header("Content-type: text/plain");
  51. }
  52. if($_REQUEST['resource'] == "services") {
  53. $j = new JangleService($request_uri);
  54. cacheCheck($j);
  55. buildServiceResponseFromConfig($j,$config);
  56. } elseif ($explain) {
  57. $j = new JangleExplain($request_uri);
  58. buildExplainResponseFromConfig($j,$config);
  59. } else {
  60. switch($_REQUEST['resource']){
  61. case 'actors':
  62. $resources = new Actors($config);
  63. break;
  64. case 'collections':
  65. $resources = new Collections($config);
  66. break;
  67. case 'items':
  68. $resources = new Items($config);
  69. break;
  70. case 'resources':
  71. $resources = new Resources($config);
  72. break;
  73. }
  74. if($_REQUEST['offset']) {
  75. $offset = $_REQUEST['offset'];
  76. } else {
  77. $offset = 0;
  78. }
  79. if($_REQUEST['format']) {
  80. $format = $_REQUEST['format'];
  81. } else {
  82. if($_REQUEST['relation']) {
  83. $format = $config["entities"][$_REQUEST['relation']]["record_types"][0];
  84. } else {
  85. $format = $config["entities"][$_REQUEST['resource']]["record_types"][0];
  86. }
  87. }
  88. if($search) {
  89. $j = new JangleSearch($request_uri);
  90. } else {
  91. $j = new JangleFeed($request_uri);
  92. }
  93. if($_REQUEST["id"]) {
  94. if($_REQUEST["relation"]) {
  95. if($_REQUEST["category"]) {
  96. $entries = $resources->relationFindByFilter($_REQUEST["id"],$_REQUEST["relation"],$_REQUEST["category"],$offset,$format);
  97. $j->setTotalResults($resources->relationFilterCount($_REQUEST["id"],$_REQUEST["relation"],$_REQUEST["category"]));
  98. $j->addCategory($_REQUEST['category']);
  99. } else {
  100. $entries = $resources->relationFind($_REQUEST["id"], $_REQUEST["relation"], $offset, $format);
  101. if(count($entries) == 0) {
  102. header("HTTP/1.1 404 Not Found");
  103. exit();
  104. }
  105. $j->setTotalResults($resources->relationCount($_REQUEST["id"], $_REQUEST["relation"]));
  106. }
  107. addAlternateFormatsToFeed($j,$format,$config["entities"][$_REQUEST["relation"]]["record_types"],$config["record_types"]);
  108. } else {
  109. $entries = $resources->find($_REQUEST["id"], $offset, $format);
  110. if(count($entries) == 0) {
  111. header("HTTP/1.1 404 Not Found");
  112. exit();
  113. }
  114. $j->setTotalResults($resources->count($_REQUEST["id"]));
  115. addAlternateFormatsToFeed($j,$format,$config["entities"][$_REQUEST["resource"]]["record_types"],$config["record_types"]);
  116. }
  117. } else {
  118. if($_REQUEST["category"]) {
  119. $entries = $resources->findByFilter($_REQUEST["category"],$offset,$format);
  120. $j->setTotalResults($resources->filterCount($_REQUEST["category"]));
  121. $j->addCategory($_REQUEST['category']);
  122. } elseif($search) {
  123. $j->setTotalResults($resources->searchCount($_REQUEST["query"]));
  124. if($j->totalResults > 0) {
  125. $limit = NULL;
  126. if($_REQUEST["limit"]) {
  127. $limit = $_REQUEST["limit"];
  128. }
  129. $entries = $resources->search($_REQUEST["query"],$offset, $format, $limit);
  130. } else {
  131. $entries = array();
  132. }
  133. } else {
  134. $entries = $resources->all($offset, $format);
  135. cacheCheck($j);
  136. $j->setTotalResults($resources->count());
  137. }
  138. addAlternateFormatsToFeed($j,$format,$config["entities"][$_REQUEST["resource"]]["record_types"],$config["record_types"]);
  139. }
  140. for($i = 0; $i < count($entries); $i++) {
  141. $j->addDataItem($entries[$i]);
  142. }
  143. if($config["record_types"][$format]["stylesheets"]) {
  144. if($config["record_types"][$format]["stylesheets"]["feed"]) {
  145. if(array_search($resources->entity(), $config["record_types"][$format]["stylesheets"]["feed"]["entities"]) !== False) {
  146. $j->addStylesheet($config["record_types"][$format]["stylesheets"]["feed"]["uri"]);
  147. }
  148. }
  149. }
  150. for($i = 0; $i < count($j->dataItems); $i++) {
  151. if(!in_array($j->dataItems[$i]->format,$j->formats)) {
  152. $j->addFormat($j->dataItems[$i]->format);
  153. }
  154. }
  155. }
  156. echo $j->out();
  157. function buildServiceResponseFromConfig(&$response,$config) {
  158. $entityMap = array("actors"=>"Actor","resources"=>"Resource","items"=>"Item","collections"=>"Collection");
  159. $response->setTitle($config["global_options"]["service_name"]);
  160. for($i = 0; $i < count($config["entities"]); $i++) {
  161. $entity = new JangleServiceEntity($entityMap[key($config["entities"])]);
  162. $entity->setTitle($config["entities"][key($config["entities"])]["title"]);
  163. $entity->setPath($config["global_options"]["path_to_connector"]."/".key($config["entities"]));
  164. for($x = 0; $x < count($config["entities"][key($config["entities"])]["categories"]); $x++) {
  165. $entity->addCategory($config["entities"][key($config["entities"])]["categories"][$x]);
  166. }
  167. if($config["entities"][key($config["entities"])]["search"]) {
  168. $entity->setSearchDocument($config["global_options"]["path_to_connector"]."/".key($config["entities"])."/search/description/");
  169. }
  170. $response->addEntity($entity);
  171. next($config["entities"]);
  172. }
  173. for($i = 0; $i < count($config["categories"]); $i++) {
  174. $category = new JangleServiceCategory(key($config["categories"]));
  175. $category->setScheme($config["categories"][key($config["categories"])]["scheme"]);
  176. $response->addCategory($category);
  177. }
  178. }
  179. function buildExplainResponseFromConfig(&$response,$config) {
  180. $c = $config["entities"][$_REQUEST["resource"]]["search"];
  181. $response->setShortName($config["entities"][$_REQUEST["resource"]]["title"]);
  182. if($c["longname"]) {
  183. $response->setLongName($c["longname"]);
  184. }
  185. if($c["description"]) {
  186. $response->setDescription($c["description"]);
  187. }
  188. if($c["contact"]) {
  189. $response->setContact($c["contact"]);
  190. }
  191. if($c["tags"]) {
  192. for($i = 0; $i < count($c["tags"]); $i++) {
  193. $response->addTag($c["tags"][$i]);
  194. }
  195. }
  196. $opensearch_params = array("searchTerms","count","startIndex","startPage","language","inputEncoding","outputEncoding");
  197. $url = $config['base_uri'];
  198. if(!ereg("/\/$/",$url)) {
  199. $url .= "/";
  200. }
  201. $url .= $_REQUEST['resource']."/search?";
  202. if($c["parameters"]) {
  203. $query_args = array();
  204. foreach(array_keys($c["parameters"]) as $key) {
  205. $query_arg = $key."=";
  206. if(array_search(ereg_replace("\?$",'',$c["parameters"][$key]),$opensearch_params) !== False) {
  207. $query_arg .= "{".$c["parameters"][$key]."}";
  208. } else {
  209. $query_arg .= "{jangle:".$c["parameters"][$key]."}";
  210. }
  211. array_push($query_args, $query_arg);
  212. }
  213. $url .= join($query_args,"&");
  214. } else {
  215. $url .= "query={searchTerms}";
  216. }
  217. $response->setTemplate($url);
  218. if($c["image"]) {
  219. $response->setImageLocation($c["image"]["location"]);
  220. if($c["image"]["height"]) {
  221. $response->setImageHeight($c["image"]["height"]);
  222. }
  223. if($c["image"]["width"]) {
  224. $response->setImageWidth($c["image"]["width"]);
  225. }
  226. if($c["image"]["type"]) {
  227. $response->setImageType($c["image"]["type"]);
  228. }
  229. }
  230. if($c["query"]) {
  231. $response->setExampleQuery($c["query"]);
  232. }
  233. if($c["developer"]) {
  234. $response->setDeveloper($c["developer"]);
  235. }
  236. if($c["attribution"]) {
  237. $response->setAttribution($c["attribution"]);
  238. }
  239. if($c["syndicationright"]) {
  240. $response->setSyndicationRight($c["syndicationright"]);
  241. }
  242. if($c["adultcontent"]) {
  243. $response->setAdultContent($c["adultcontent"]);
  244. }
  245. if($c["language"]) {
  246. $response->setLanguage($c["language"]);
  247. }
  248. if($c["inputencoding"]) {
  249. $response->addInputEncoding($c["inputencoding"]);
  250. }
  251. if($c["outencoding"]) {
  252. $response->addInputEncoding($c["outputencoding"]);
  253. }
  254. $ctx_sets = array();
  255. for($i = 0; $i < count($c["indexes"]); $i++) {
  256. list($name, $index) = explode(".",$c["indexes"][$i]);
  257. if(!$ctx_sets[$name]) {
  258. $ctx_sets[$name] = array();
  259. }
  260. if(!in_array($index, $ctx_sets[$name])) {
  261. array_push($ctx_sets[$name],$index);
  262. }
  263. }
  264. if(count($ctx_sets) > 0) {
  265. foreach(array_keys($ctx_sets) as $ctx_set) {
  266. $ident = $config["context-sets"][$ctx_set]["identifier"];
  267. $ctx = new JangleContextSet($ctx_set, $ident);
  268. if(count($ctx_sets[$ctx_set]) > 0) {
  269. foreach($ctx_sets[$ctx_set] as $index) {
  270. $ctx->addIndex($index);
  271. }
  272. }
  273. $response->addContextSet($ctx);
  274. }
  275. }
  276. return($response);
  277. }
  278. function checkRequestSanity($vars, $explain, $search, $config) {
  279. if(!$vars["resource"]) {
  280. header("HTTP/1.1 404 Not Found");
  281. exit();
  282. }
  283. if($vars["resource"] == "services") {
  284. if($vars["relation"] || $vars["id"] || $vars["category"]) {
  285. header("HTTP/1.1 404 Not Found");
  286. exit();
  287. }
  288. return;
  289. }
  290. if(!in_array($vars["resource"], array_keys($config["entities"]))) {
  291. header("HTTP/1.1 404 Not Found");
  292. exit();
  293. }
  294. if($vars["relation"]) {
  295. if(!in_array($vars["relation"], $config["entities"][$vars["resource"]]["services"])) {
  296. header("HTTP/1.1 404 Not Found");
  297. exit();
  298. }
  299. if($vars["format"]) {
  300. if(!in_array($vars["format"], $config["entities"][$vars["relation"]]["record_types"])) {
  301. header("HTTP/1.1 400 Bad Request");
  302. exit();
  303. }
  304. }
  305. if($vars["category"]) {
  306. if(!in_array($vars["category"], $config["entities"][$vars["relation"]]["categories"])) {
  307. header("HTTP/1.1 404 Not Found");
  308. exit();
  309. }
  310. }
  311. return;
  312. }
  313. if($vars["category"]) {
  314. if(!in_array($vars["category"], $config["entities"][$vars["resource"]]["categories"])) {
  315. header("HTTP/1.1 404 Not Found");
  316. exit();
  317. }
  318. }
  319. if($search || $explain) {
  320. if(!$config["entities"][$vars["resource"]]["search"]) {
  321. header("HTTP/1.1 404 Not Found");
  322. exit();
  323. }
  324. }
  325. if($vars["format"]) {
  326. if(!in_array($vars["format"], $config["entities"][$vars["resource"]]["record_types"])) {
  327. header("HTTP/1.1 400 Bad Request");
  328. exit();
  329. }
  330. }
  331. }
  332. function addAlternateFormatsToFeed(&$feed,$format,$resourceRecordTypes,$allRecordTypes) {
  333. if(count($resourceRecordTypes)) {
  334. foreach($resourceRecordTypes as $type) {
  335. if($type != $format) {
  336. $u = parse_url($feed->uri);
  337. if($u['query']) {
  338. parse_str($u['query'], $q);
  339. $q['format'] = $type;
  340. $uri = $u['scheme']."://".$u['host'];
  341. if($u['port']) {
  342. $uri .= ":".$u['port'];
  343. }
  344. $uri .= $u['path']."?".http_build_query($q);
  345. } else {
  346. $uri = $feed->uri."?format=".$type;
  347. }
  348. $feed->addAlternateFormat($allRecordTypes[$type]["uri"],$uri);
  349. }
  350. }
  351. }
  352. }
  353. function cacheCheck(&$obj) {
  354. if(!method_exists($obj, "cache")) {
  355. return;
  356. }
  357. }
  358. ?>