3 require_once('../include/db.php');
5 header('Content-type: application/json');
6 $maxage = config('jsonCacheMaxAge');
7 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $maxage) . ' GMT');
8 header("Cache-Control: maxage=$maxage");
10 function exit_with_error($status, $details = array()) {
11 $details['status'] = $status;
12 echo json_encode($details);
16 function echo_success($details = array()) {
17 $details['status'] = 'OK';
18 echo json_encode($details);
21 function exit_with_success($details = array()) {
22 echo_success($details);
29 exit_with_error('DatabaseConnectionError');
33 function camel_case_words_separated_by_underscore($name) {
34 return implode('', array_map('ucfirst', explode('_', $name)));
37 function require_format($key, $value, $pattern) {
38 if (!preg_match($pattern, $value))
39 exit_with_error('Invalid' . camel_case_words_separated_by_underscore($key), array('value' => $value));
42 function require_existence_of($array, $list_of_arguments, $prefix = '') {
45 foreach ($list_of_arguments as $key => $pattern) {
46 $name = camel_case_words_separated_by_underscore($prefix . $key);
47 if (!array_key_exists($key, $array))
48 exit_with_error($name . 'NotSpecified');
49 require_format($name, $array[$key], $pattern);