view api.php @ 2:b83d9186c2af

Make sure that we're following the new update pattern
author IBBoard <dev@ibboard.co.uk>
date Mon, 29 May 2017 14:28:30 +0100
parents 42c058ce5b7c
children
line wrap: on
line source

<?php
if (!file_exists('./api-key.php')) {
	header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
	echo 'ERROR: API key not found';
	exit;	
}
require('./api-key.php');
if (!isset($api_key)){
	header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
	echo 'ERROR: API key not set';
	exit;	
}

$method = '';
$extras = array();
foreach ($_GET as $k => $v) {
	if ($k == 'method') {
		if (preg_match('/^flickr(\.[a-zA-Z]+){2,}$/', $v)) {
			$method = $v;
		}
	} else if (preg_match('/^[0-9a-zA-Z@\._-]+$/', $k) && preg_match('/^[0-9a-zA-Z@:\/\._ -]+$/', $v)) {
		$extras[$k] = $v;
	}
}

if (!$method){
	header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
	echo 'No Flickr API method specified';
	exit();
}

$extra = implode('&', array_map(function($k, $v) { return $k . '=' . urlencode($v); }, array_keys($extras), $extras));
if ($extra) {
	$extra = '&'.$extra;
}
$base_url = 'https://secure.flickr.com/services/rest/?api_key=' . $api_key .'&format=json&nojsoncallback=1&method=';

$cache = 'data/'.str_replace(array('&', '=', ' '), '-', $method . $extra).'.json';


if (!file_exists($cache) || (time() - filemtime($cache)) > (24 * 60 * 60)) {
	$ch = curl_init($base_url . $method . $extra);
	$fp = fopen($cache, "w");
	curl_setopt($ch, CURLOPT_FILE, $fp);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_exec($ch);
	curl_close($ch);
	fclose($fp);
}

if (!file_exists($cache)) {
	header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
echo file_get_contents($cache);