# HG changeset patch # User Matti Hamalainen # Date 1294771374 -7200 # Node ID 02f0f79f98b6b951440b5d5756ee451475992f81 Initial import. diff -r 000000000000 -r 02f0f79f98b6 index.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/index.php Tue Jan 11 20:42:54 2011 +0200 @@ -0,0 +1,127 @@ +\n"; + exit; +} + +if (isset($_GET["tila"])) { + $tila = $_GET["tila"]; + echo "Luokkatilan näyttömoodi ei vielä tuettu.
\n"; + exit; + + $fp = @fopen($classfile, "rb"); + if ($fp) { + fclose($fp); + } +} + + +// Global cache for course data +$cache = array(); +$dirty = FALSE; + + +// Try to read cachefile, if we can get file lock on it +$fp = @fopen($cachefile, "rb"); +if ($fp) { + if (flock($fp, LOCK_SH)) { + require($cachefile); + flock($fp, LOCK_UN); + } + fclose($fp); +} + + +function match_course($matches) +{ + global $cache, $infoURI, $dirty; + + $id = $matches[1]; + + // Check if course exists in cache + if (!isset($cache[$id])) { + $dirty = TRUE; + // Not cached, try to fetch data + $data = @file_get_contents($infoURI.$id); + if ($data !== FALSE) { + if (preg_match("#(.+?)\s+(\d+)\s*op\s*#", $data, $m)) { + // Add data to cache + $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); + } + } + } + + if (isset($cache[$id])) + return "".htmlentities($cache[$id]["desc"]).""; + else + return htmlentities($id); +} + + +function match_class($matches) +{ + global $baseURI; + return "".$matches[1]." ".$matches[2]; +} + + +/* Fetch HTML data and replace occurances of course IDs + * with information and a link + */ +if (file_exists("OR_".$luokka.".htm")) + $data = @file_get_contents("OR_".$luokka.".htm"); +else + $data = @file_get_contents($lukuURI.$luokka.".htm"); + +if ($data !== FALSE) { + $data = preg_replace_callback("#([A-Z]\d+)\.?#", match_course, $data); + + $data = preg_replace_callback("#(\d{4}|ATK\d+|[A-Z]{2,3}LAB\d+|[A-Z]{2,3}LAB|Audit\d+)(\(\d+\))?#", match_class, $data); + + $data = preg_replace("##", "\n". + "[orig]\n, Luokat: ". + "[TTE0SNO]\n". + "[TTE9SNO]\n", $data); + echo $data; +} else { + echo "Luokan datatiedostoa ".htmlentities($lukuURI.$luokka.".htm")." ei saatu.
\n"; +} + + +// Dump the course data cache, but only if it has changed +if ($dirty) { + // First try append mode + $fp = @fopen($cachefile, "rb+"); + + // If file didn't exist, try write mode + if (!$fp) + $fp = @fopen($cachefile, "wb"); + + if ($fp) { + // Use locking to prevent concurrent access and dump data + if (flock($fp, LOCK_EX)) { + ftruncate($fp, 0); + fwrite($fp, " $data) { + fwrite($fp, " \"".addslashes($id)."\" => array(\"desc\" => \"". + addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n"); + } + fwrite($fp, ");\n?>"); + } + fclose($fp); + } +} + +?> \ No newline at end of file