init.php (2650B)
1 <?php 2 3 require_once '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'autoload.php'; 4 5 function url_params( $template ) { 6 $data = $_GET; 7 $url = $_SERVER['REQUEST_URI']; 8 $url = explode('?',$url); 9 $url = explode('/',array_shift($url)); 10 $template = explode('/',$template); 11 foreach ( $template as $index => $name ) { 12 if (substr($name,0,1)!=':') continue; 13 $name = substr($name,1); 14 $data[$name] = isset($url[$index]) ? $url[$index] : null; 15 } 16 return $data; 17 } 18 19 function get_deep( $data, $key = "" ) { 20 if (is_string($key)) $key = explode('.', $key); 21 if (!is_array($key)) return null; 22 if ( is_object($data) ) $data = (array) $data; 23 if ( !is_array($data) ) return null; 24 if ( count($key) == 1 ) { 25 $key = array_shift($key); 26 if ( isset($data[$key]) ) { 27 return $data[$key]; 28 } 29 return null; 30 } 31 $current_key = array_shift($key); 32 return get_deep( $data[$current_key], $key ); 33 } 34 35 function entity_matches( $entity, $filter ) { 36 foreach ( $filter as $key => $query ) { 37 $value = get_deep( $entity, $key ); 38 switch(substr($query,0,1)) { 39 case '/': 40 if ( !preg_match($query, $value) ) return false; 41 break; 42 case '>': 43 $query = substr($query,1); 44 if ( !is_numeric($query) ) return false; 45 if ( !is_numeric($value) ) return false; 46 if (!(floatval($value) > floatval($query))) return false; 47 break; 48 case '<': 49 $query = substr($query,1); 50 if ( !is_numeric($query) ) return false; 51 if ( !is_numeric($value) ) return false; 52 if (!(floatval($value) < floatval($query))) return false; 53 break; 54 case '!': 55 $query = substr($query,1); 56 if ( $query == $value ) return false; 57 break; 58 default: 59 if ( $query != $value ) return false; 60 break; 61 } 62 } 63 return true; 64 } 65 66 function random_char() { 67 $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 68 return $alphabet[rand(0, strlen($alphabet)-1)]; 69 } 70 71 function uuid( $collection = null ) { 72 if (is_null($collection)) return uniqid('_'); 73 $dir = APPROOT.DS.'data'.DS.$collection.DS; 74 if (!is_dir($dir)) return uniqid('_'); 75 $output = '_'; 76 while ( strlen($output) < 5 ) $output .= random_char(); 77 while ( is_file($dir.$output.'.json') ) $output .= random_char(); 78 return $output; 79 }