data.php (3657B)
1 <?php 2 require_once 'init.php'; 3 $method = $_SERVER['REQUEST_METHOD']; 4 $params = url_params("/api/data/:collection/:id"); 5 if(is_null($params['collection'])) { 6 header('HTTP/1.0 400 Bad Request'); 7 echo 'Bad Request - No collection given'; 8 exit(0); 9 } 10 11 switch($method) { 12 case 'GET': 13 $dir = APPROOT.DS.'data'.DS.$params['collection'].DS; 14 if (!is_dir($dir)) { 15 header('HTTP/1.0 404 Not Found'); 16 echo 'Not Found - Collection does not exist'; 17 exit(0); 18 } 19 20 header('Content-Type: application/json'); 21 header('Access-Control-Allow-Origin: *'); 22 23 if (!isset($params['id'])) echo '['; 24 $dh = opendir($dir); 25 $sep = ''; 26 while( $file = readdir($dh) ) { 27 if ( substr($file,0,1) == '.' ) continue; 28 $id = explode('.',$file); 29 $ext = array_pop($id); 30 $id = implode('.',$id); 31 if ( $ext != 'json' ) continue; 32 if ( isset($params['id']) ) { 33 if ( $params['id'] == $id ) { 34 $entity = json_decode(file_get_contents($dir.$file), true); 35 $entity['_id'] = $id; 36 echo json_encode($entity); 37 exit(0); 38 } 39 continue; 40 } 41 $entity = json_decode(file_get_contents($dir.$file), true); 42 $entity['_id'] = $id; 43 if ( isset($params['filter']) && !entity_matches($entity,$params['filter']) ) { 44 continue; 45 } 46 echo $sep; 47 $sep = ','; 48 echo json_encode($entity); 49 } 50 if (!isset($params['id'])) echo ']'; 51 break; 52 53 case 'POST': 54 $dir = APPROOT.DS.'data'.DS.$params['collection'].DS; 55 if (!is_dir($dir)) { 56 mkdir($dir); 57 } 58 59 header('Content-Type: application/json'); 60 header('Access-Control-Allow-Origin: *'); 61 62 $id = isset($params['id']) ? $params['id'] : uuid($params['collection']); 63 $file = $dir.$id.'.json'; 64 file_put_contents($file,json_encode($_POST)); 65 $_POST['_id'] = $id; 66 echo json_encode($_POST); 67 68 break; 69 70 case 'DELETE': 71 $dir = APPROOT.DS.'data'.DS.$params['collection'].DS; 72 if (!is_dir($dir)) { 73 header('HTTP/1.0 404 Not Found'); 74 echo 'Not Found - Collection does not exist'; 75 exit(0); 76 } 77 if (!isset($params['id'])) { 78 header('HTTP/1.0 400 Not Found'); 79 echo 'Bad Request - No ID given'; 80 exit(0); 81 } 82 83 header('Content-Type: application/json'); 84 header('Access-Control-Allow-Origin: *'); 85 86 $file = $dir.$params['id'].'.json'; 87 if(!is_file($file)) { 88 header('HTTP/1.0 404 Not Found'); 89 echo 'Not Found - Entity does not exist'; 90 exit(0); 91 } 92 93 $entity = json_decode(file_get_contents($file), true); 94 $entity['_id'] = $params['id']; 95 unlink($file); 96 echo json_encode($entity); 97 break; 98 99 case 'OPTIONS': 100 header('Access-Control-Allow-Origin: *'); 101 header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE'); 102 if (array_key_exists('HTTP_ACCESS_CONTROL_REQUEST_HEADERS', $_SERVER)) { 103 header('Access-Control-Allow-Headers: '.$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']); 104 } else { 105 header('Access-Control-Allow-Headers: *'); 106 } 107 break; 108 109 default: 110 header('HTTP/1.0 400 Bad Request'); 111 echo 'Bad Request - Invalid method'; 112 break; 113 }