changeset 43:7621bfc47491

Various bits and pieces -- a commit that defies logic. Oh well.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Aug 2012 07:56:07 +0300
parents 76900cdfc066
children 57af36f0864e
files index.php
diffstat 1 files changed, 44 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Mon Aug 27 07:55:22 2012 +0300
+++ b/index.php	Mon Aug 27 07:56:07 2012 +0300
@@ -1,4 +1,7 @@
 <?php
+//
+// Yes, this is horrible. :|
+//
 $pageCSS = array("http://tnsp.org/docs1.css", "luk.css");
 $pageCharset = "utf-8";
 $internalCharset = "utf-8";
@@ -34,27 +37,27 @@
 
 
 // Helper functions
-function chentities($str)
+function lukChEntities($str)
 {
   global $internalCharset;
 //  return $str;
   return htmlentities($str, ENT_NOQUOTES, $internalCharset);
 }
 
-function checkClassID(&$id)
+function lukCheckClassID(&$id)
 {
   global $luokkaDefault;
   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>".$luokkaDefault."</b>.");
+    lukErrorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
     $id = $luokkaDefault;
     return FALSE;
   }
 }
 
-function getHourStamp($hour, $sec = 0, $mon = 0, $day = 0, $year = 0)
+function lukMakeHourStamp($hour, $sec = 0, $mon = 0, $day = 0, $year = 0)
 {
   global $hourStamps;
   if (isset($hourStamps[$hour]))
@@ -66,12 +69,12 @@
 
 function getHour($hour)
 {
-  $stamp = getHourStamp($hour);
+  $stamp = lukMakeHourStamp($hour);
   return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />";
 }
 
 
-function matchCourse($id)
+function lukMatchCourse($id)
 {
   global $cache, $infoURI, $cacheDirty, $internalCharset;
 
@@ -90,14 +93,14 @@
   }
   
   if (isset($cache[$id]))
-    return "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id]["op"]." op").
-    "\" href=\"".chentities($infoURI.$id)."\">".chentities($cache[$id]["desc"])."</a>";
+    return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id]["op"]." op").
+    "\" href=\"".lukChEntities($infoURI.$id)."\">".lukChEntities($cache[$id]["desc"])."</a>";
   else
-    return chentities($id);
+    return lukChEntities($id);
 }
 
 
-function matchClass($matches)
+function lukMatchClass($matches)
 {
   global $baseURI;
   return "<b><a href=\"".$baseURI."?tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2];
@@ -110,15 +113,15 @@
 // Check given parameters
 if (isset($_GET["luokka"])) {
   $luokka = $_GET["luokka"];
-  if (checkClassID($luokka) && !$miniMode) {
+  if (lukCheckClassID($luokka) && !$miniMode) {
     setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year
   }
 } else
 if (isset($_COOKIE["lukluokka"])) {
   $luokka = $_COOKIE["lukluokka"];
-  checkClassID($luokka);
+  lukCheckClassID($luokka);
 } else {
-  errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
+  lukErrorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
   $luokka = $luokkaDefault;
 }
 
@@ -158,6 +161,7 @@
   fclose($fp);
 }
 
+
 // Read classfile
 $fp = @fopen($classFile, "rb");
 $classes = array();
@@ -165,19 +169,19 @@
   if (flock($fp, LOCK_SH)) { 
     while (!feof($fp)) {
       $str = trim(fgets($fp, 128));
-      if ($str[0] != "#" && strlen($str) > 2)
+      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.");
+  lukErrorMsg("Luokkien listaa ei löytynyt. Kokeile ladata sivu uudelleen hetken kuluttua.");
 }
 
 
 if (!file_exists($dataFile)) {
-  errorMsg("Luokan ".chentities($luokka)." tietoja ei löytynyt! Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan päivityksen aikana. Luokkatiedot päivitetään 2 kertaa vuorokaudessa.");
+  lukErrorMsg("Luokan ".lukChEntities($luokka)." tietoja ei löytynyt! Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan päivityksen aikana. Luokkatiedot päivitetään 2 kertaa vuorokaudessa.");
   $haveData = FALSE;
 } else {
   require($dataFile);
@@ -195,7 +199,7 @@
 }
 
 
-function findHours($start, $day, $cmp, $mark = FALSE)
+function lukResolveHours($start, $day, $cmp, $mark = FALSE)
 {
   global $classHourTable, $classInfo;
   $n = 0;
@@ -230,17 +234,17 @@
 {
   if ($isSplit) {
     $out = "<table>".
-    "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>";
+    "<tr><td>".lukMatchCourse($d[0])."</td><td>".lukMatchCourse($d[1])."</td></tr>";
 
     for ($j = 2; $j < count($d); $j += 2)
-      $out .= "<tr><td>".chentities($d[$j])."</td><td>".(isset($d[$j+1]) ? chentities($d[$j+1]) : "")."</td></tr>";
+      $out .= "<tr><td>".lukChEntities($d[$j])."</td><td>".(isset($d[$j+1]) ? lukChEntities($d[$j+1]) : "")."</td></tr>";
     
     return $out."</table>";
   } else {
-    $out = matchCourse($d[0])."<br />";
+    $out = lukMatchCourse($d[0])."<br />";
 
     for ($j = 1; $j < count($d); $j++)
-      $out .= chentities($d[$j])."<br />";
+      $out .= lukChEntities($d[$j])."<br />";
 
     return $out;
   }
@@ -265,23 +269,27 @@
 }
 
 
-if ($miniMode) {
+if ($miniMode && $haveData) {
   $stamp = time();
   $aika = getdate($stamp);
   $day = $aika["wday"] - 1;
   if ($day < 0) $day = 7;
   $hour = $aika["hours"] - 8;
 
-  $id = $classHourTable[$hour][$day];
-
-  $out = "<div class=\"content\">\n".
-  "<h2>".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
-  getHourInfoData($id)."\n";
+  $out = "<div class=\"content\">\n";
+ 
+  if ($hour >= 0 && $day >= 0)
+  {
+    $id = $classHourTable[$hour][$day];
+    $out .=
+    "<h2>".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
+    getHourInfoData($id)."\n";
+  }
   
 
   if (isset($id) && $id >= 0 && isset($classDefs[$id])) {
     $i = $classDefs[$id];
-    $start = getHourStamp($i["start"]);
+    $start = lukMakeHourStamp($i["start"]);
   }
 
 //  echo "day=$day, hour=$hour, id=$id<br />\n";
@@ -307,7 +315,7 @@
 
   $out .= "<h2>Seuraavaksi";
   if ($found > 0) {
-    $stamp = getHourStamp($nhour, 0, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]);
+    $stamp = lukMakeHourStamp($nhour, 0, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]);
     $aika = getdate($stamp);
     $out .= ": ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
     getHourInfoData($found)."\n";
@@ -363,7 +371,7 @@
         
       if ($h < 1) {
         if ($h > -9999) {
-          $n = findHours($hour, $day, $h, TRUE);
+          $n = lukResolveHours($hour, $day, $h, TRUE);
           $out .= "  <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n";
         }
       } else
@@ -375,9 +383,9 @@
           
           $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
           
-          $hours = findHours($hour, $day, $h, FALSE);
+          $hours = lukResolveHours($hour, $day, $h, FALSE);
           if ($hours != $i["hours"]) {
-            errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!");
+            lukErrorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!");
           }
           
           $out .= "  <td rowspan=\"".$hours.
@@ -386,9 +394,9 @@
           "<div class=\"nhours\"><span>".$hours."h</span></div></td>\n";
         }
       } else
-        errorMsg("Internal error cell $hour / $day : hour id $h does not exist!");
+        lukErrorMsg("Internal error cell $hour / $day : hour id $h does not exist!");
     } else
-      errorMsg("Internal error, cell $hour / $day does not exist.");
+      lukErrorMsg("Internal error, cell $hour / $day does not exist.");
   }
   $out .= " </tr>\n";
 }
@@ -413,7 +421,7 @@
 ";
 
 foreach ($classes as $class) {
-  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".chentities($class)."</option>\n";
+  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n";
 }
 echo 
 "    </select>
@@ -440,7 +448,7 @@
 // Show error messages
 if ($errorSet) {
   echo "<ul>\n";
-  foreach ($errorMsgs as $msg)
+  foreach ($lukErrorMsgs as $msg)
     echo "<li>$msg</li>\n";
   echo "</ul>\n";
 }