PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/www/docs/api/api_functions.php

https://github.com/openaustralia/twfy
PHP | 371 lines | 253 code | 27 blank | 91 comment | 46 complexity | dd388923c7f88f19660b90f018465c19 MD5 | raw file
  1. <?
  2. include_once '../../../../phplib/rabx.php';
  3. # The METHODS
  4. $methods = array(
  5. /* 'convertURL' => array(
  6. 'parameters' => array('url'),
  7. 'required' => true,
  8. 'help' => 'Converts a parliament.uk Hansard URL into a TheyWorkForYou one, if possible',
  9. ),
  10. */
  11. 'getDivisions' => array(
  12. # 'parameters' => array('date', 'search', 'latitude', 'longitude', 'distance'),
  13. 'parameters' => array('postcode', 'date', 'search'),
  14. 'required' => false,
  15. 'help' => 'Returns list of electoral divisions',
  16. ),
  17. 'getRepresentative' => array(
  18. 'parameters' => array('id', 'division', 'always_return'),
  19. 'required' => true,
  20. 'help' => 'Returns main details for a member of the House of Representatives'
  21. ),
  22. /* 'getMPInfo' => array(
  23. 'parameters' => array('id', 'fields'),
  24. 'required' => true,
  25. 'help' => 'Returns extra information for a person'
  26. ),
  27. 'getMPsInfo' => array(
  28. 'parameters' => array('id', 'fields'),
  29. 'required' => true,
  30. 'help' => 'Returns extra information for one or more people'
  31. ),
  32. */
  33. 'getRepresentatives' => array(
  34. 'parameters' => array('postcode', 'party', 'date', 'search'),
  35. 'required' => false,
  36. 'help' => 'Returns list of members of the House of Representatives',
  37. ),
  38. 'getSenator' => array(
  39. 'parameters' => array('id'),
  40. 'required' => true,
  41. 'help' => 'Returns details for a Senator'
  42. ),
  43. 'getSenators' => array(
  44. 'parameters' => array('date', 'party', 'state', 'search'),
  45. 'required' => false,
  46. 'help' => 'Returns list of Senators',
  47. ),
  48. /* 'getMLAs' => array(
  49. 'parameters' => array('date', 'party', 'search'),
  50. 'required' => false,
  51. 'help' => 'Returns list of MLAs',
  52. ),
  53. 'getMSP' => array(
  54. 'parameters' => array('id', 'constituency', 'postcode'),
  55. 'required' => true,
  56. 'help' => 'Returns details for an MSP'
  57. ),
  58. 'getMSPs' => array(
  59. 'parameters' => array('date', 'party', 'search'),
  60. 'required' => false,
  61. 'help' => 'Returns list of MSPs',
  62. ),
  63. 'getGeometry' => array(
  64. 'new' => true,
  65. 'parameters' => array('name'),
  66. 'required' => false,
  67. 'help' => 'Returns centre, bounding box of constituencies'
  68. ),
  69. */
  70. /* 'getBoundary' => array(
  71. 'parameters' => array('name'),
  72. 'required' => true,
  73. 'help' => 'Returns boundary polygon of constituency'
  74. ),
  75. */
  76. /* 'getCommittee' => array(
  77. 'new' => true,
  78. 'parameters' => array('name', 'date'),
  79. 'required' => true,
  80. 'help' => 'Returns members of Select Committee',
  81. ),
  82. */
  83. 'getDebates' => array(
  84. 'parameters' => array('type', 'date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  85. 'required' => true,
  86. 'help' => 'Returns Debates (either House of Representatives or Senate)',
  87. ),
  88. /* 'getWrans' => array(
  89. 'parameters' => array('date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  90. 'required' => true,
  91. 'help' => 'Returns Written Answers',
  92. ),
  93. 'getWMS' => array(
  94. 'parameters' => array('date', 'search', 'person', 'gid', 'year', 'order', 'page', 'num'),
  95. 'required' => true,
  96. 'help' => 'Returns Written Ministerial Statements',
  97. ),
  98. */
  99. 'getHansard' => array(
  100. 'parameters' => array('search', 'person', 'order', 'page', 'num'),
  101. 'required' => true,
  102. 'help' => 'Returns any of the above',
  103. ),
  104. 'getComments' => array(
  105. 'parameters' => array('search', 'page', 'num', 'pid'),
  106. 'required' => false,
  107. 'help' => 'Returns comments'
  108. ),
  109. /* 'postComment' => array(
  110. 'parameters' => array('user_id', 'gid?'),
  111. 'working' => false,
  112. 'required' => true,
  113. 'help' => 'Posts a comment - needs authentication!'
  114. ),
  115. */
  116. );
  117. # Key-related functions
  118. function api_log_call($key) {
  119. if ($key=='DOCS') return;
  120. $ip = $_SERVER['REMOTE_ADDR'];
  121. $query = $_SERVER['REQUEST_URI'];
  122. $query = preg_replace('#key=[A-Za-z0-9]+&?#', '', $query);
  123. $db = new ParlDB;
  124. $db->query("INSERT INTO api_stats (api_key, ip_address, query_time, query)
  125. VALUES ('$key', '$ip', NOW(), '" . mysql_real_escape_string($query) . "')");
  126. }
  127. function api_check_key($key) {
  128. $db = new ParlDB;
  129. $q = $db->query('SELECT user_id FROM api_key WHERE api_key="' . mysql_real_escape_string($key) . '"');
  130. if (!$q->rows())
  131. return false;
  132. return true;
  133. }
  134. function api_key_current_message() { }
  135. # Front-end sidebar of all methods
  136. function api_sidebar() {
  137. global $methods;
  138. $sidebar = '<div class="block"><h4>API Functions</h4> <div class="blockbody"><ul>';
  139. foreach ($methods as $method => $data){
  140. $sidebar .= '<li';
  141. if (isset($data['new']))
  142. $sidebar .= ' style="border-top: solid 1px #999999;"';
  143. $sidebar .= '>';
  144. if (!isset($data['working']) || $data['working'])
  145. $sidebar .= '<a href="' . WEBPATH . 'api/docs/' . $method . '">';
  146. $sidebar .= $method;
  147. if (!isset($data['working']) || $data['working'])
  148. $sidebar .= '</a>';
  149. else
  150. $sidebar .= ' - <em>not written yet</em>';
  151. # if ($data['required'])
  152. # $sidebar .= ' (parameter required)';
  153. # else
  154. # $sidebar .= ' (parameter optional)';
  155. $sidebar .= '<br>' . $data['help'];
  156. # $sidebar .= '<ul>';
  157. # foreach ($data['parameters'] as $parameter) {
  158. # $sidebar .= '<li>' . $parameter . '</li>';
  159. # }
  160. # $sidebar .= '</ul>';
  161. $sidebar .= '</li>';
  162. }
  163. $sidebar .= '</ul></div></div>';
  164. $sidebar = array(
  165. 'type' => 'html',
  166. 'content' => $sidebar
  167. );
  168. return $sidebar;
  169. }
  170. # Output functions
  171. function api_output($arr, $last_mod=null) {
  172. $output = get_http_var('output');
  173. if (!get_http_var('docs')) {
  174. $cond = api_header($output, $last_mod);
  175. if ($cond) return;
  176. }
  177. if ($output == 'xml') {
  178. $out = '<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
  179. $out .= '<result>' . api_output_xml($arr) . '</result>';
  180. } elseif ($output == 'php') {
  181. $out = api_output_php($arr);
  182. } elseif ($output == 'rabx') {
  183. $out = api_output_rabx($arr);
  184. } else { # JS
  185. $out = api_output_js($arr);
  186. $callback = get_http_var('callback');
  187. if (preg_match('#^[A-Za-z0-9._[\]]+$#', $callback)) {
  188. $out = "$callback($out)";
  189. }
  190. }
  191. print $out;
  192. }
  193. function api_header($o, $last_mod=null) {
  194. if ($last_mod && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
  195. $t = cond_parse_http_date($_SERVER['HTTP_IF_MODIFIED_SINCE']);
  196. if (isset($t) && $t >= $last_mod) {
  197. header('HTTP/1.0 304 Not Modified');
  198. header('Last-Modified: ' . date('r', $last_mod));
  199. return true;
  200. }
  201. }
  202. if ($o == 'xml') {
  203. $type = 'text/xml';
  204. } elseif ($o == 'php') {
  205. $type = 'text/php';
  206. } elseif ($o == 'rabx') {
  207. $type = 'application/octet-stream';
  208. } else {
  209. $type = 'text/javascript';
  210. }
  211. #$type = 'text/plain';
  212. header("Content-Type: $type; charset=iso-8859-1");
  213. if ($last_mod>0)
  214. header('Last-Modified: ' . date('r', $last_mod));
  215. return false;
  216. }
  217. function api_error($e) {
  218. api_output(array('error' => $e));
  219. }
  220. function api_output_php($arr) {
  221. $out = serialize($arr);
  222. if (get_http_var('verbose')) $out = str_replace(';', ";\n", $out);
  223. return $out;
  224. }
  225. function api_output_rabx($arr) {
  226. $out = '';
  227. rabx_wire_wr($arr, $out);
  228. if (get_http_var('verbose')) $out = str_replace(',', ",\n", $out);
  229. return $out;
  230. }
  231. $api_xml_arr = 0;
  232. function api_output_xml($v, $k=null) {
  233. global $api_xml_arr;
  234. $verbose = get_http_var('verbose') ? "\n" : '';
  235. if (is_array($v)) {
  236. if (count($v) && array_keys($v) === range(0, count($v)-1)) {
  237. $elt = 'match';
  238. $api_xml_arr++;
  239. $out = "<$elt>";
  240. $out .= join("</$elt>$verbose<$elt>", array_map('api_output_xml', $v));
  241. $out .= "</$elt>$verbose";
  242. return $out;
  243. }
  244. $out = '';
  245. foreach ($v as $k => $vv) {
  246. $out .= "<$k>";
  247. $out .= api_output_xml($vv, $k);
  248. $out .= "</$k>$verbose";
  249. }
  250. return $out;
  251. } else {
  252. return htmlspecialchars($v);
  253. }
  254. }
  255. function api_output_js($v, $level=0) {
  256. $verbose = get_http_var('verbose') ? "\n" : '';
  257. if (is_array($v)) {
  258. # PHP arrays are both JS arrays and objects
  259. if (count($v) && array_keys($v) === range(0, count($v)-1))
  260. return '[' . join(",$verbose" , array_map('api_output_js', $v)) . ']';
  261. $out = '{' . $verbose;
  262. $b = false;
  263. foreach ($v as $k => $vv) {
  264. if ($b) $out .= ",$verbose";
  265. if ($verbose) {
  266. $out .= str_repeat(' ', ($level+1)*2);
  267. $out .= '"' . $k . '" : ';
  268. } else {
  269. $out .= '"' . $k . '":';
  270. }
  271. $out .= api_output_js($vv, $level+1);
  272. $b = true;
  273. }
  274. if ($verbose) $out .= "\n" . str_repeat(' ', $level*2);
  275. $out .= '}';
  276. return $out;
  277. } elseif (is_null($v)) {
  278. return "null";
  279. } elseif (is_string($v)) {
  280. return '"' . str_replace(
  281. array("\\",'"',"\n","\t","\r"),
  282. array("\\\\",'\"','\n','\t','\r'), $v) . '"';
  283. } elseif (is_bool($v)) {
  284. return $v ? 'true' : 'false';
  285. } elseif (is_int($v) || is_float($v)) {
  286. return $v;
  287. }
  288. }
  289. # Call an API function
  290. function api_call_user_func_or_error($function, $params, $error, $type) {
  291. if (function_exists($function))
  292. call_user_func_array($function, $params);
  293. elseif ($type == 'api')
  294. api_error($error);
  295. else
  296. print "<p style='color:#cc0000'>$error</p>";
  297. }
  298. # Used for testing for conditional responses
  299. $cond_wkday_re = '(Sun|Mon|Tue|Wed|Thu|Fri|Sat)';
  300. $cond_weekday_re = '(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)';
  301. $cond_month_re = '(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
  302. $cond_month_map = array(
  303. 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4,
  304. 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8,
  305. 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12
  306. );
  307. $cond_date1_re = '(\d\d) ' . $cond_month_re . ' (\d\d\d\d)';
  308. $cond_date2_re = '(\d\d)-' . $cond_month_re . '-(\d\d)';
  309. $cond_date3_re = $cond_month_re . ' (\d\d| \d)';
  310. $cond_time_re = '([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|6[012])';
  311. function cond_parse_http_date($date) {
  312. $H = $M = $S = 0;
  313. $Y = $m = $d = 0;
  314. $ma = array();
  315. global $cond_wkday_re, $cond_weekday_re, $cond_month_re, $cond_month_map,
  316. $cond_date1_re, $cond_date2_re, $cond_date3_re, $cond_time_re;
  317. if (preg_match("/^$cond_wkday_re, $cond_date1_re $cond_time_re GMT\$/", $date, $ma)) {
  318. /* RFC 1123 */
  319. $d = $ma[2];
  320. $m = $cond_month_map[$ma[3]];
  321. $Y = $ma[4];
  322. $H = $ma[5];
  323. $M = $ma[6];
  324. $S = $ma[7];
  325. } else if (preg_match("/^$cond_weekday_re, $cond_date2_re $cond_time_re GMT\$/", $date, $ma)) {
  326. /* RFC 850 */
  327. $d = $ma[2];
  328. $m = $cond_month_map[$ma[3]];
  329. $Y = $ma[4] + ($ma[4] < 50 ? 2000 : 1900); /* XXX */
  330. $H = $ma[5];
  331. $M = $ma[6];
  332. $S = $ma[7];
  333. } else if (preg_match("/^$cond_wkday_re $cond_date3_re $cond_time_re (\\d{4})\$/", $date, $ma)) {
  334. /* asctime(3) */
  335. $d = preg_replace('/ /', '', $ma[3]);
  336. $m = $cond_month_map[$ma[2]];
  337. $Y = $ma[7];
  338. $H = $ma[4];
  339. $M = $ma[5];
  340. $S = $ma[6];
  341. } else
  342. return null;
  343. return gmmktime($H, $M, $S, $m, $d, $Y);
  344. }