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