Mercurial > hg > lukkari
view index.php @ 72:6fd715063abc
Clean up some parsing operations.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 24 Oct 2012 06:31:32 +0300 |
parents | 0329105ddedc |
children | 241839ebb09f |
line wrap: on
line source
<?php // // Lukkari v2.0 // (C) Copyright 2010 - 2012 Matti 'ccr' Hämäläinen <ccr@tnsp.org> // Yes, this code is rather horrible. :| // $pageLang = "fi"; $pageCharset = "utf-8"; $internalCharset = "utf-8"; $classDefault = "TTE9SNO"; $classFile = "classes.txt"; $cacheFile = "coursecache.txt"; $baseURI = "http://tnsp.org/luk/"; $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk"); $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3", "dark" => "4"); $pageLanguages = array("fi", "en"); $lukDayNames = array( "fi" => array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"), "en" => array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"), ); $lukTranslations = array( "Original" => array("fi" => "Alkuperäinen"), "Next period" => array("fi" => "Seuraava periodi"), "Current period" => array("fi" => "Nykyinen periodi"), "Switch" => array("fi" => "Vaihda"), "contact" => array( "en" => "Contact <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. The author can't be held responsible for any errors in the data!", "fi" => "Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!", ), "beta" => array( "en" => " NOTICE! Lukkari v2.0beta is still under development. There may be bugs. Some old features (mini-mode) may not return.", "fi" => " HUOM! Lukkari v2.0beta on vielä keskeneräinen. Bugeja voi olla. Jotkut vanhat ominaisuudet (mini-moodi) eivät välttämättä tule takaisin.", ), "viikossa" => array( "en" => "Total of <b>%1</b> hours in the week.", "fi" => "Viikossa yhteensä <b>%1</b> tuntia.", ), "classNotSet" => array( "en" => "Class not set, using default <b>%1</b>.", "fi" => "Luokkaa ei asetettu, käytetään vakioarvoa <b>%1</b>.", ), "Style" => array("fi" => "Tyyli"), // "" => array("fi" => ), ); // Include framework require "mcommon.inc.php"; require "merrors.inc.php"; // Translation function function lukMsg($msg) { global $lukTranslations, $pageLang; if (isset($lukTranslations[$msg]) && isset($lukTranslations[$msg][$pageLang])) $str = $lukTranslations[$msg][$pageLang]; else $str = $msg; foreach (func_get_args() as $argn => $argv) { $str = preg_replace("/\%".$argn."/", $argv, $str); } return $str; } function lukGetDayName($day) { global $lukDayNames, $pageLang; if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day])) return $lukDayNames[$pageLang][$day]; else return $lukDayNames["en"][$day]; } function lukChEntities($str) { global $internalCharset; return htmlentities($str, ENT_NOQUOTES, $internalCharset); } function lukCheckClassID(&$id) { global $classDefault; if (preg_match("#^([A-Z]{3}\d[A-Za-z0-9_]{1,6}|ccr|Ryh_[A-Z]{3}\d[A-Za-z0-9_]{1,6})$#", $id, $m)) { $id = $m[1]; return TRUE; } else { errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$classDefault."</b>."); $id = $classDefault; return FALSE; } } function lukGetWeekdayFromTimestamp($stamp) { $info = getdate($stamp); $day = $info["wday"]; return ($day > 0) ? $day - 1 : $day + 6; } function lukGetHourStamp($stamp) { return date("H:i", mktime(0, 0, $stamp, 0, 0, 0)); } function lukGetHourStr($hour) { global $classHourTimes; if (isset($classHourTimes[$hour])) { return "<br />". lukGetHourStamp($classHourTimes[$hour]["start"]). " - ". lukGetHourStamp($classHourTimes[$hour]["end"]). "<br /><br />"; } else return "ERROR"; } function lukMatchCourse($id) { global $cache, $cacheDirty, $internalCharset, $pageLang; $uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id; // Create the index if (!isset($cache[$id])) $cache[$id] = array(); // Check if course exists in cache if (!isset($cache[$id][$pageLang])) { // Not cached, try to fetch data $cacheDirty = TRUE; $data = @file_get_contents($uri); if ($data !== FALSE) { $data = iconv("iso-8859-1", $internalCharset, $data); if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) { // Add data to cache $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2])); } } } if (isset($cache[$id]) && isset($cache[$id][$pageLang])) { return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id][$pageLang]["op"]." op"). "\" href=\"".lukChEntities($uri)."\">".lukChEntities($cache[$id][$pageLang]["desc"])."</a>"; } else return lukChEntities($id); } function lukGetClassInfo($grouped, $data) { if ($grouped) { $out = "<table><tr>"; $maxCount = 0; foreach ($data as $col) { $out .= "<td>".lukMatchCourse($col[0])."</td>"; if (count($col) > $maxCount) $maxCount = count($col); } $out .= "</tr>"; for ($i = 1; $i < $maxCount; $i++) { $out .= "<tr>"; foreach ($data as $col) { $out .= "<td>"; if (isset($col[$i])) $out .= lukChEntities($col[$i]); $out .= "</td>"; } $out .= "</tr>"; } return $out."</table>"; } else { $out = lukMatchCourse($data[0][0])."<br />"; for ($i = 1; $i < count($data[0]); $i++) { $out .= lukChEntities($data[0][$i])."<br />"; } return $out; } } function lukFindClass($day, $hour) { global $classHourDefs, $classDayTable; if (isset($classDayTable[$day])) { foreach ($classDayTable[$day] as $id) { if ($hour >= $classHourDefs[$id]["start"] && $hour < $classHourDefs[$id]["start"] + $classHourDefs[$id]["hours"]) return $id; } } return 0; } // Check given parameters if (isset($_GET["luokka"])) { $luokka = $_GET["luokka"]; if (lukCheckClassID($luokka)) { setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year } } else if (isset($_COOKIE["lukluokka"])) { $luokka = $_COOKIE["lukluokka"]; lukCheckClassID($luokka); } else { errorMsg(lukMsg("classNotSet", $classDefault)); $luokka = $classDefault; } if (isset($_GET["lang"])) { $tmp = strtolower($_GET["lang"]); if (in_array($tmp, $pageLanguages)) { $pageLang = $tmp; setcookie("luklang", $tmp, time() + 365*24*60*60); // expire in a year } } else if (isset($_COOKIE["luklang"])) { $tmp = $_COOKIE["luklang"]; if (in_array($tmp, $pageLanguages)) $pageLang = $tmp; } if (isset($_GET["next"])) { $nextPeriod = TRUE; $dataFile = "cache-next/".$luokka.".data"; } else { $nextPeriod = FALSE; $dataFile = "cache/".$luokka.".data"; } // 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); } // Read classfile $classes = array(); $fp = @fopen($classFile, "rb"); if ($fp) { if (flock($fp, LOCK_SH)) { while (!feof($fp)) { $str = trim(fgets($fp, 128)); if (strlen($str) > 2 && $str[0] != "#") $classes[] = $str; } flock($fp, LOCK_UN); } fclose($fp); } else { errorMsg("Luokkien listaa ei löytynyt. Kokeile ladata sivu uudelleen hetken kuluttua."); } if (!file_exists($dataFile)) { errorMsg("Luokan ".lukChEntities($luokka)." tietoja ei löytynyt! ". "Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan ". "päivityksen aikana. Luokkatiedot päivitetään kerran viikossa."); $haveData = FALSE; } else { require($dataFile); $haveData = isset($classInfo); } function lukNormalTimeTable() { global $classInfo, $classHourDefs, $classHourTimes; // $currStamp = time() + ((3 * 60) + 45) * 60; $currStamp = time(); $currTime = $currStamp - mktime(0, 0, 0); $currDay = lukGetWeekdayFromTimestamp($currStamp); // Create the timetable table $out = "<p>".join("; ", $classInfo["general"])."</p>\n". "<div>".lukMsg("viikossa", $classInfo["totalHours"])."</div>\n". "<table class=\"timetable\">". " <tr>\n <th></th>\n"; $cellWidth = 100 / $classInfo["maxDays"]; if ($cellWidth > 25) $cellWidth = 25; for ($day = 0; $day < $classInfo["maxDays"]; $day++) { $out .= " <th style=\"width: ".$cellWidth."%;\" class=\"days\">".lukGetDayName($day)."</th>\n"; } $out .= " </tr>\n"; for ($hour = $classInfo["firstHour"]; $hour < $classInfo["lastHour"]; $hour++) { $out .= " <tr>\n". " <th class=\"hours\">".lukGetHourStr($hour)."</th>\n"; for ($day = 0; $day < $classInfo["maxDays"]; $day++) { $id = lukFindClass($day, $hour); if ($id > 0) { $class = &$classHourDefs[$id]; if (!isset($class["set"])) { $class["set"] = TRUE; $isActive = $day == $currDay && $currTime >= $classHourTimes[$class["start"]]["start"] && $currTime < $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]; $out .= " <td rowspan=\"".$class["hours"]. "\" class=\"".($isActive ? "clactive " : "").($class["grouped"] ? "clgrouped" : "clnormal")."\">". lukGetClassInfo($class["grouped"], $class["data"]). "<div class=\"nhours\"><span>".$class["hours"]."h</span></div></td>\n"; } } else { $out .= " <td class=\"clnothing\"></td>\n"; } } $out .= " </tr>\n"; } return $out."</table>\n"; } // // // $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; printPageHeader($pageTitle); echo "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\"> <table> <tr> <th> <select name=\"luokka\"> "; foreach ($classes as $class) { echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n"; } echo " </select> </th> <th><input class=\"submit\" type=\"submit\" value=\"".lukMsg("Switch")."\" /></th> <th><a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">".lukMsg("Original")."</a></th> <th><a href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">". ($nextPeriod ? lukMsg("Current period") : lukMsg("Next period"))."</a></th> </tr> </table> </form> "; echo "<h1>".$pageTitle."</h1>\n"; // Show error messages if ($errorSet) { echo "<ul>\n"; foreach ($errorMsgs as $msg) echo "<li>$msg</li>\n"; echo "</ul>\n"; } if ($haveData) { echo lukNormalTimeTable(); } echo "<div id=\"footer\">". lukMsg("contact"). "<div style=\"color: red;\">". lukMsg("beta"). "</div> </div> <div id=\"csssel\"> <div id=\"ctitle\">Lukkari v2.0 beta</div> <div>".lukMsg("Style").": "; foreach ($pageCSSAlts as $name => $id) echo "<a href=\"".$baseURI."?css=".$id."\">".$name."</a>"; echo "</div><div id=\"clang\">"; foreach ($pageLanguages as $id) echo "<a href=\"".$baseURI."?lang=".$id."\">".$id."</a>"; echo " </div> </div> "; printPageFooter(); // Dump the course data cache, but only if it has changed if ($cacheDirty) { $str = "<?\n\$cache = ".var_export($cache, TRUE)."\n?>"; if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) { // Can't do much anything here .. } } ?>