comparison api.php @ 0:42c058ce5b7c

Initial public commit of Exif-Graphr
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Aug 2016 20:46:16 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:42c058ce5b7c
1 <?php
2 if (!file_exists('./api-key.php')) {
3 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
4 echo 'ERROR: API key not found';
5 exit;
6 }
7 require('./api-key.php');
8 if (!isset($api_key)){
9 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
10 echo 'ERROR: API key not set';
11 exit;
12 }
13
14 $method = '';
15 $extras = array();
16 foreach ($_GET as $k => $v) {
17 if ($k == 'method') {
18 if (preg_match('/^flickr(\.[a-zA-Z]+){2,}$/', $v)) {
19 $method = $v;
20 }
21 } else if (preg_match('/^[0-9a-zA-Z@\._-]+$/', $k) && preg_match('/^[0-9a-zA-Z@:\/\._ -]+$/', $v)) {
22 $extras[$k] = $v;
23 }
24 }
25
26 if (!$method){
27 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
28 echo 'No Flickr API method specified';
29 exit();
30 }
31
32 $extra = implode('&', array_map(function($k, $v) { return $k . '=' . urlencode($v); }, array_keys($extras), $extras));
33 if ($extra) {
34 $extra = '&'.$extra;
35 }
36 $base_url = 'https://secure.flickr.com/services/rest/?api_key=' . $api_key .'&format=json&nojsoncallback=1&method=';
37
38 $cache = 'data/'.str_replace(array('&', '=', ' '), '-', $method . $extra).'.json';
39
40
41 if (!file_exists($cache) || (time() - filemtime($cache)) > (24 * 60 * 60)) {
42 $ch = curl_init($base_url . $method . $extra);
43 $fp = fopen($cache, "w");
44 curl_setopt($ch, CURLOPT_FILE, $fp);
45 curl_setopt($ch, CURLOPT_HEADER, 0);
46 curl_exec($ch);
47 curl_close($ch);
48 fclose($fp);
49 }
50
51 if (!file_exists($cache)) {
52 header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
53 }
54 echo file_get_contents($cache);