# HG changeset patch # User Matti Hamalainen # Date 1294787016 -7200 # Node ID 45987abe0d104c8e370d086c969c0fb7ff7f27d5 # Parent a2c61b010680de73e108c8088fde758d742b037c Move beta code into production. :P diff -r a2c61b010680 -r 45987abe0d10 beta.php --- a/beta.php Wed Jan 12 01:02:28 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,418 +0,0 @@ -XXXnXXX, käytetään vakioavoa ".$luokkaDefault."."); - $id = $luokkaDefault; - return FALSE; - } else - return TRUE; -} - - -// Check given parameters -if (isset($_GET["luokka"])) { - $luokka = $_GET["luokka"]; - if (checkClassID($luokka) && !isset($_["nyt"])) { - setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year - } -} else -if (isset($_COOKIE["lukluokka"])) { - $luokka = $_COOKIE["lukluokka"]; - checkClassID($luokka); -} else { - errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa ".$luokkaDefault."."); - $luokka = $luokkaDefault; -} - - -if (isset($_GET["tila"])) { - $tila = $_GET["tila"]; - echo "Luokkatilan näyttömoodi ei vielä tuettu.
\n"; - exit; - - $fp = @fopen($roomFile, "rb"); - if ($fp) { - fclose($fp); - } -} - - -function getHour($hour) -{ - global $hourStamps; - $stamp = mktime($hourStamps[$hour][0], $hourStamps[$hour][1], 0); - return "
".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."

"; -} - - -function matchCourse($id) -{ - global $cache, $infoURI, $cacheDirty; - - // Check if course exists in cache - if (!isset($cache[$id])) { - $cacheDirty = 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 matchClass($matches) -{ - global $baseURI; - return "".$matches[1]." ".$matches[2]; -} - - -// Global cache for course data -$cache = array(); -$cacheDirty = 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); -} - -$fp = @fopen($classFile, "rb"); -$classes = array(); -if ($fp) { - if (flock($fp, LOCK_SH)) { - while (!feof($fp)) { - $str = trim(fgets($fp, 128)); - if ($str[0] != "#" && strlen($str) > 2) - $classes[] = $str; - } - flock($fp, LOCK_UN); - } - fclose($fp); -} - - -if (!file_exists($dataDir.$luokka.".data")) { - errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!"); - $haveData = FALSE; -} else { - require($dataDir.$luokka.".data"); - $haveData = isset($classDefs); -} - - -function printHourInfo($dayHours, $showDays) -{ - $out = " \n Tunteja
(ryhmä/vv)\n"; - for ($day = 0; $day < $showDays; $day++) { - $out .= " ".$dayHours[$day]["total"]."h (".$dayHours[$day]["grouped"]."h)\n"; - } - return $out." \n"; -} - - -function findHours($start, $day, $cmp, $mark = FALSE) -{ - global $classHourTable, $classInfo; - $n = 0; - for ($i = $start; $i < $classInfo["maxhours"]; $i++) - if ($classHourTable[$i][$day] == $cmp) { - if ($mark) $classHourTable[$i][$day] = -9999; - $n++; - } else - break; - return $n; -} - - -function findNextHour(&$j, &$i, $day, $hour, $chk) -{ - global $classInfo, $classHourTable; - for ($j = $day; $j < $classInfo["maxdays"]; $j++) - for ($i = $hour; $i < $classInfo["maxhours"]; $i++) { -// echo "chk=$chk - i=$i / j=$j : ht=".$classHourTable[$i][$j]."
\n"; - if (($chk > 0 && $classHourTable[$i][$j] != $chk) || $classHourTable[$i][$j] > 0) - return $classHourTable[$i][$j]; - } - - return -1; -} - - -function getHourInfo($id) -{ - global $classDefs; - - if (isset($id)) { - if ($id >= 0 && isset($classDefs[$id])) { - $i = $classDefs[$id]; - $d = $i["data"]; - $out = ""; - - $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); - if ($isSplit) { - $out .= "". - ""; - for ($j = 2; $j < count($d); $j += 2) - $out .= ""; - $out .= "
".matchCourse($d[0])."".matchCourse($d[1])."
".htmlentities($d[$j])."".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."
"; - } else { - $out .= matchCourse($d[0])."
"; - for ($j = 1; $j < count($d); $j++) - $out .= htmlentities($d[$j])."
"; - } - - return $out; - } else - return "

Ei tunteja.

"; - } else - return "

Ei mitään.

"; - - return ""; -} - - -if (isset($_GET["nyt"])) { - $aika = getdate(); - $hour = $aika["hours"] - 8; - $day = $aika["wday"] - 1; - if ($day < 0) $day = 7; - - $chk = $classHourTable[$hour][$day]; - - $out = "

".$dayNames[$day]." ".$aika["mday"].".".$aika["mon"].".".$aika["year"]." - klo ".$aika["hours"].":".$aika["minutes"]."

\n". - getHourInfo($chk)."\n"; - -// $out .= "hour=".$hour."/day=".$day."
\n"; - if ($day >= $classInfo["maxdays"]) { - $day = $hour = 0; - $chk = -1; - } else - if ($hour < 0) { - $hour = 0; - $chk = -1; - } else - if ($hour >= $classInfo["maxhours"]) { - $day++; - $hour = 0; - $chk = -1; - } -// $out .= "hour=".$hour."/day=".$day."
\n"; - - $found = findNextHour($nday, $nhour, $day, $hour, $chk); - -// $out .= "hour=".$nhour."/day=".$nday."/found=".$found."
\n"; - - if ($found > 0) { - $stamp = mktime(8 + $nhour, 15, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]); - $aika = getdate($stamp); - $hour = $aika["hours"] - 8; - $day = $aika["wday"] - 1; - if ($day < 0) $day = 7; - $out .= "

Seuraavaksi: ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."

\n". - getHourInfo($found)."\n"; - } -} -else -if ($haveData) { - $totalHours = 0; - $totalGrouped = 0; - $dayHours = array(); - - for ($day = 0; $day < $classInfo["maxdays"]; $day++) { - $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; - - for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { - $id = $classHourTable[$hour][$day]; - - if ($id > 0) { - $totalHours++; - $dayHours[$day]["total"]++; - - if ($classDefs[$id]["grouped"]) { - $totalGrouped++; - $dayHours[$day]["grouped"]++; - } - } - } - } - - // Create the timetable table - $out = - "

".join("; ", $classInfo["general"])."

\n". - "
Viikossa yhteensä ".$totalHours." tuntia, joista ".$totalGrouped." ryhmissä tai vuoroviikoin.
\n". - "". - " \n \n"; - for ($day = 0; $day < $classInfo["maxdays"]; $day++) { - $out .= " \n"; - } - $out .= " \n"; - -for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { - $out .= " \n". - " \n"; - for ($day = 0; $day < $classInfo["maxdays"]; $day++) { - if (isset($classHourTable[$hour][$day])) { - $h = $classHourTable[$hour][$day]; - - if ($h < 1) { - if ($h > -9999) { - $n = findHours($hour, $day, $h, TRUE); - $out .= " \n"; - } - } else - if (isset($classDefs[$h])) { - if (!isset($classDefs[$h]["done"])) { - $classDefs[$h]["done"] = true; - $i = $classDefs[$h]; - $d = $classDefs[$h]["data"]; - - $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); - - $hours = findHours($hour, $day, $h, FALSE); - if ($hours != $i["hours"]) { - errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!"); - } - - $out .= " \n"; - } - } else - errorMsg("Internal error cell $hour / $day : hour id $h does not exist!"); - } else - errorMsg("Internal error, cell $hour / $day does not exist."); - } - $out .= " \n"; -} - - $out .= printHourInfo($dayHours, $classInfo["maxdays"]). - "
".$dayNames[$day]."
".getHour($hour).""; - - if ($isSplit) { - $out .= "". - ""; - for ($j = 2; $j < count($d); $j += 2) - $out .= ""; - $out .= "
".matchCourse($d[0])."".matchCourse($d[1])."
".htmlentities($d[$j])."".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."
"; - } else { - $out .= matchCourse($d[0])."
"; - for ($j = 1; $j < count($d); $j++) - $out .= htmlentities($d[$j])."
"; - } - $out .= "
".$hours."h
\n"; - -} // haveData - - -$pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; -printPageHeader($pageTitle); -echo "

".$pageTitle."

\n"; - -echo "
\n". -" \n". -" \n". -" \n". -" \n". -" \n". -" \n". -"
\n". -" \n". -" [Mini-info moodi]
\n". -"
\n"; - - -// Show error messages -if ($errorSet) { - echo "\n"; -} - -echo $out; - -printPageFooter(); - - -// Dump the course data cache, but only if it has changed -if ($cacheDirty) { - // 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 diff -r a2c61b010680 -r 45987abe0d10 index.php --- a/index.php Wed Jan 12 01:02:28 2011 +0200 +++ b/index.php Wed Jan 12 01:03:36 2011 +0200 @@ -1,57 +1,93 @@ XXXnXXX, käytetään vakioavoa ".$luokkaDefault."."); + $id = $luokkaDefault; + return FALSE; + } else + return TRUE; +} + // Check given parameters -$luokka = isset($_GET["luokka"]) ? $_GET["luokka"] : "TTE9SNO"; +if (isset($_GET["luokka"])) { + $luokka = $_GET["luokka"]; + if (checkClassID($luokka) && !isset($_["nyt"])) { + setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year + } +} else +if (isset($_COOKIE["lukluokka"])) { + $luokka = $_COOKIE["lukluokka"]; + checkClassID($luokka); +} else { + errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa ".$luokkaDefault."."); + $luokka = $luokkaDefault; +} -if (!preg_match("#^[A-Z]{3}\d\S+#", $luokka)) { - echo "Virhe! Luokan täytyy olla muotoa XXXnXXX.
\n"; - exit; -} if (isset($_GET["tila"])) { $tila = $_GET["tila"]; echo "Luokkatilan näyttömoodi ei vielä tuettu.
\n"; exit; - $fp = @fopen($classfile, "rb"); + $fp = @fopen($roomFile, "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 getHour($hour) +{ + global $hourStamps; + $stamp = mktime($hourStamps[$hour][0], $hourStamps[$hour][1], 0); + return "
".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."

"; } -function match_course($matches) +function matchCourse($id) { - global $cache, $infoURI, $dirty; - - $id = $matches[1]; + global $cache, $infoURI, $cacheDirty; // Check if course exists in cache if (!isset($cache[$id])) { - $dirty = TRUE; + $cacheDirty = TRUE; // Not cached, try to fetch data $data = @file_get_contents($infoURI.$id); if ($data !== FALSE) { @@ -70,44 +106,299 @@ } -function match_class($matches) +function matchClass($matches) { global $baseURI; - return "".$matches[1]." ".$matches[2]; + return "".$matches[1]." ".$matches[2]; +} + + +// Global cache for course data +$cache = array(); +$cacheDirty = 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); +} + +$fp = @fopen($classFile, "rb"); +$classes = array(); +if ($fp) { + if (flock($fp, LOCK_SH)) { + while (!feof($fp)) { + $str = trim(fgets($fp, 128)); + if ($str[0] != "#" && strlen($str) > 2) + $classes[] = $str; + } + flock($fp, LOCK_UN); + } + fclose($fp); +} + + +if (!file_exists($dataDir.$luokka.".data")) { + errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!"); + $haveData = FALSE; +} else { + require($dataDir.$luokka.".data"); + $haveData = isset($classDefs); +} + + +function printHourInfo($dayHours, $showDays) +{ + $out = " \n Tunteja
(ryhmä/vv)\n"; + for ($day = 0; $day < $showDays; $day++) { + $out .= " ".$dayHours[$day]["total"]."h (".$dayHours[$day]["grouped"]."h)\n"; + } + return $out." \n"; +} + + +function findHours($start, $day, $cmp, $mark = FALSE) +{ + global $classHourTable, $classInfo; + $n = 0; + for ($i = $start; $i < $classInfo["maxhours"]; $i++) + if ($classHourTable[$i][$day] == $cmp) { + if ($mark) $classHourTable[$i][$day] = -9999; + $n++; + } else + break; + return $n; +} + + +function findNextHour(&$j, &$i, $day, $hour, $chk) +{ + global $classInfo, $classHourTable; + for ($j = $day; $j < $classInfo["maxdays"]; $j++) + for ($i = $hour; $i < $classInfo["maxhours"]; $i++) { +// echo "chk=$chk - i=$i / j=$j : ht=".$classHourTable[$i][$j]."
\n"; + if (($chk > 0 && $classHourTable[$i][$j] != $chk) || $classHourTable[$i][$j] > 0) + return $classHourTable[$i][$j]; + } + + return -1; +} + + +function getHourInfo($id) +{ + global $classDefs; + + if (isset($id)) { + if ($id >= 0 && isset($classDefs[$id])) { + $i = $classDefs[$id]; + $d = $i["data"]; + $out = ""; + + $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); + if ($isSplit) { + $out .= "". + ""; + for ($j = 2; $j < count($d); $j += 2) + $out .= ""; + $out .= "
".matchCourse($d[0])."".matchCourse($d[1])."
".htmlentities($d[$j])."".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."
"; + } else { + $out .= matchCourse($d[0])."
"; + for ($j = 1; $j < count($d); $j++) + $out .= htmlentities($d[$j])."
"; + } + + return $out; + } else + return "

Ei tunteja.

"; + } else + return "

Ei mitään.

"; + + return ""; } -/* 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 (isset($_GET["nyt"])) { + $aika = getdate(); + $hour = $aika["hours"] - 8; + $day = $aika["wday"] - 1; + if ($day < 0) $day = 7; -if ($data !== FALSE) { - $data = preg_replace_callback("#([A-Z]\d+)\.?#", match_course, $data); + $chk = $classHourTable[$hour][$day]; + + $out = "

".$dayNames[$day]." ".$aika["mday"].".".$aika["mon"].".".$aika["year"]." - klo ".$aika["hours"].":".$aika["minutes"]."

\n". + getHourInfo($chk)."\n"; - $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); +// $out .= "hour=".$hour."/day=".$day."
\n"; + if ($day >= $classInfo["maxdays"]) { + $day = $hour = 0; + $chk = -1; + } else + if ($hour < 0) { + $hour = 0; + $chk = -1; + } else + if ($hour >= $classInfo["maxhours"]) { + $day++; + $hour = 0; + $chk = -1; + } +// $out .= "hour=".$hour."/day=".$day."
\n"; + + $found = findNextHour($nday, $nhour, $day, $hour, $chk); + +// $out .= "hour=".$nhour."/day=".$nday."/found=".$found."
\n"; + + if ($found > 0) { + $stamp = mktime(8 + $nhour, 15, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]); + $aika = getdate($stamp); + $hour = $aika["hours"] - 8; + $day = $aika["wday"] - 1; + if ($day < 0) $day = 7; + $out .= "

Seuraavaksi: ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."

\n". + getHourInfo($found)."\n"; + } +} +else +if ($haveData) { + $totalHours = 0; + $totalGrouped = 0; + $dayHours = array(); + + for ($day = 0; $day < $classInfo["maxdays"]; $day++) { + $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; + + for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { + $id = $classHourTable[$hour][$day]; + + if ($id > 0) { + $totalHours++; + $dayHours[$day]["total"]++; + + if ($classDefs[$id]["grouped"]) { + $totalGrouped++; + $dayHours[$day]["grouped"]++; + } + } + } + } + + // Create the timetable table + $out = + "

".join("; ", $classInfo["general"])."

\n". + "
Viikossa yhteensä ".$totalHours." tuntia, joista ".$totalGrouped." ryhmissä tai vuoroviikoin.
\n". + "". + " \n \n"; + for ($day = 0; $day < $classInfo["maxdays"]; $day++) { + $out .= " \n"; + } + $out .= " \n"; - $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"; +for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { + $out .= " \n". + " \n"; + for ($day = 0; $day < $classInfo["maxdays"]; $day++) { + if (isset($classHourTable[$hour][$day])) { + $h = $classHourTable[$hour][$day]; + + if ($h < 1) { + if ($h > -9999) { + $n = findHours($hour, $day, $h, TRUE); + $out .= " \n"; + } + } else + if (isset($classDefs[$h])) { + if (!isset($classDefs[$h]["done"])) { + $classDefs[$h]["done"] = true; + $i = $classDefs[$h]; + $d = $classDefs[$h]["data"]; + + $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); + + $hours = findHours($hour, $day, $h, FALSE); + if ($hours != $i["hours"]) { + errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!"); + } + + $out .= " \n"; + } + } else + errorMsg("Internal error cell $hour / $day : hour id $h does not exist!"); + } else + errorMsg("Internal error, cell $hour / $day does not exist."); + } + $out .= " \n"; } + $out .= printHourInfo($dayHours, $classInfo["maxdays"]). + "
".$dayNames[$day]."
".getHour($hour).""; + + if ($isSplit) { + $out .= "". + ""; + for ($j = 2; $j < count($d); $j += 2) + $out .= ""; + $out .= "
".matchCourse($d[0])."".matchCourse($d[1])."
".htmlentities($d[$j])."".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."
"; + } else { + $out .= matchCourse($d[0])."
"; + for ($j = 1; $j < count($d); $j++) + $out .= htmlentities($d[$j])."
"; + } + $out .= "
".$hours."h
\n"; + +} // haveData + + +$pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; +printPageHeader($pageTitle); +echo "

".$pageTitle."

\n"; + +echo "
\n". +" \n". +" \n". +" \n". +" \n". +" \n". +" \n". +"
\n". +" \n". +" [Mini-info moodi]
\n". +"
\n"; + + +// Show error messages +if ($errorSet) { + echo "\n"; +} + +echo $out; + +printPageFooter(); + // Dump the course data cache, but only if it has changed -if ($dirty) { +if ($cacheDirty) { // First try append mode - $fp = @fopen($cachefile, "rb+"); + $fp = @fopen($cacheFile, "rb+"); // If file didn't exist, try write mode if (!$fp) - $fp = @fopen($cachefile, "wb"); + $fp = @fopen($cacheFile, "wb"); if ($fp) { // Use locking to prevent concurrent access and dump data