00001 <?php 00007 $all = ''; 00008 $term_code = ''; 00009 $section = ''; 00010 if (isset($_SERVER['PATH_INFO'])) { 00011 $parts = explode('/', $_SERVER['PATH_INFO']); 00012 if (isset($parts[1])) { 00013 if ($parts[1] === 'help') { 00014 header('Content-Type: text/plain'); 00015 die(file_get_contents('README.utf8books')); 00016 } 00017 $term_code = substr(strip_tags(str_replace(array("'",'"'," ","/"),'', urldecode($parts[1]))),-3); 00018 } 00019 if (isset($parts[2])) { 00020 if ($parts[2] === 'all') { 00021 $all = true; 00022 } else { 00023 $section = strip_tags(str_replace(array("'",'"'," ","/"),'', urldecode($parts[2]))); 00024 } 00025 } 00026 } else if (isset($_GET['term']) && isset($_GET['section'])) { 00027 $term_code = substr(strip_tags(str_replace(array("'",'"'," ","/"),'', urldecode($_GET['term']))),-3); 00028 if ($_GET['section'] == 'all') { 00029 $all = true; 00030 } else { 00031 $section = strip_tags(str_replace(array("'",'"'," ","/"),'', urldecode($_GET['section']))); 00032 } 00033 } else if (isset($_GET['term'])) { 00034 $term_code = substr(strip_tags(str_replace(array("'",'"'," ","/"),'', urldecode($_GET['term']))),-3); 00035 } 00036 00037 $callback = ''; 00038 if (isset($_GET['callback'])) { 00039 $callback = strip_tags(str_replace(array("'",'"'," ","/"),'',urldecode($_GET['callback']))); 00040 } 00041 00042 if ($all === true && $term_code != '') { 00043 $filenames = glob('booklist/' . $term_code . '-*.json'); 00044 $result = ''; 00045 foreach ($filenames as $filename) { 00046 $books = json_decode(file_get_contents($filename),true); 00047 for ($i = 0; $i < count($books); $i += 1) { 00048 foreach ($books[$i] as $key => $value) { 00049 $books[$i][$key] = mb_convert_encoding($value,'UTF-8'); 00050 } 00051 } 00052 if ($result === '') { 00053 $result .= json_encode($books); 00054 } else { 00055 $result .= ',' . json_encode($books); 00056 } 00057 unset($books); 00058 } 00059 $result .= ''; 00060 } else if ($section != '' && $term_code != '') { 00061 $filename = 'booklist/' . substr($term_code,-3) . '-' . $section . '.json'; 00062 if (file_exists($filename)) { 00063 $books = json_decode(file_get_contents($filename),true); 00064 for ($i = 0; $i < count($books); $i += 1) { 00065 foreach ($books[$i] as $key => $value) { 00066 $books[$i][$key] = mb_convert_encoding($value,'UTF-8'); 00067 } 00068 } 00069 $result = json_encode($books); 00070 } else { 00071 $result = '{"error":"' . "Can't find $filename" . '"}'; 00072 } 00073 } else if ($term_code != '') { 00074 $files = glob("booklist/" . substr($term_code,-3) . "-?????.json"); 00075 $booklists = array(); 00076 foreach ($files as $file) { 00077 list($term_code, $section) = explode('-', str_replace(array('booklist/','.json'),'',$file)); 00078 $booklists[] = $section; 00079 } 00080 $result = json_encode($booklists); 00081 } else { 00082 $filenames = glob("booklist/???-?????.json"); 00083 $term_codes = array(); 00084 foreach ($filenames as $filename) { 00085 $term_codes[] = '20'. substr($filename,9,3); 00086 } 00087 $result = json_encode(array_unique($term_codes,SORT_NUMERIC)); 00088 } 00089 00090 00091 if ($callback !== "") { 00092 header('Content-Type: application/javascript'); 00093 echo $callback . '('; 00094 } else { 00095 header('Content-Type: application/json'); 00096 } 00097 echo $result; 00098 if ($callback !== "") { 00099 echo ')'; 00100 } 00101 ?>