changeset 107:9f976ae7a3e2

Reformat, modularize, cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 11 Dec 2013 16:38:15 +0200
parents e7bd58dec4f3
children 436aea5bf49f
files index.php
diffstat 1 files changed, 144 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Wed Dec 11 02:03:32 2013 +0200
+++ b/index.php	Wed Dec 11 16:38:15 2013 +0200
@@ -4,9 +4,11 @@
 // (C) Copyright 2010 - 2012 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
 // Yes, this code is rather horrible. :|
 //
+// Include framework
+require "mcommon.inc.php";
+
 $pageLang = "fi";
 $pageCharset = "utf-8";
-$internalCharset = "utf-8";
 $classDefault = "TTE1SNO";
 $classIDFile = "classes.txt";
 $courseCacheFile = "coursecache.txt";
@@ -79,10 +81,6 @@
 );
 
 
-// Include framework
-require "mcommon.inc.php";
-
-
 function lukGetDayName($day)
 {
   global $lukDayNames, $pageLang;
@@ -94,13 +92,6 @@
 }
 
 
-function lukChEntities($str)
-{
-  global $internalCharset;
-  return htmlentities($str, ENT_NOQUOTES, $internalCharset);
-}
-
-
 function lukCheckClassID(&$id)
 {
   global $classDefault;
@@ -148,7 +139,7 @@
 
 function lukMatchCourse($id)
 {
-  global $cache, $cacheDirty, $internalCharset, $pageLang;
+  global $cache, $cacheDirty, $pageCharset, $pageLang;
 
   $uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id;
 
@@ -164,7 +155,7 @@
     $data = @file_get_contents($uri);
     if ($data !== FALSE)
     {
-      $data = iconv("iso-8859-1", $internalCharset, $data);
+      $data = iconv("iso-8859-1", $pageCharset, $data);
       if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m))
       {
         // Add data to cache
@@ -175,11 +166,11 @@
   
   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>";
+    return "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id][$pageLang]["op"]." op").
+    "\" href=\"".chentities($uri)."\">".chentities($cache[$id][$pageLang]["desc"])."</a>";
   }
   else
-    return lukChEntities($id);
+    return chentities($id);
 }
 
 
@@ -205,7 +196,7 @@
       {
         $out .= "<td>";
         if (isset($col[$i]))
-          $out .= lukChEntities($col[$i]);
+          $out .= chentities($col[$i]);
         $out .= "</td>";
       }
       $out .= "</tr>";
@@ -219,7 +210,7 @@
 
     for ($i = 1; $i < count($data[0]); $i++)
     {
-      $out .= lukChEntities($data[0][$i])."<br />";
+      $out .= chentities($data[0][$i])."<br />";
     }
 
     return $out;
@@ -243,6 +234,35 @@
 }
 
 
+function lukReadClassFile($filename)
+{
+  // Attempt to open file for reading
+  if (($fp = @fopen($filename, "rb")) === false)
+    return false;
+
+  $classes = FALSE;
+
+  // Lock file so that we do not get clashes
+  if (flock($fp, LOCK_SH))
+  {
+    $classes = array();
+    // Read and parse data
+    while (!feof($fp))
+    {
+      $str = trim(fgets($fp, 128));
+      if (strlen($str) > 2 && $str[0] != "#")
+        $classes[] = $str;
+    }
+
+    // Release lock
+    flock($fp, LOCK_UN);
+  }
+
+  fclose($fp);
+  return $classes;
+}
+
+
 // Check given parameters
 // Language must be the first setting to be validated,
 // so that the translation support works properly.
@@ -341,41 +361,6 @@
 }
 
 
-// Read classfile
-$classes = array();
-if (($fp = @fopen($classIDFile, "rb")) !== false)
-{
-  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
-{
-  stError(stQM("classListNotFound"));
-}
-
-
-$dataFile = $cachePath.$luokka.".data";
-if (!file_exists($dataFile))
-{
-  stError(cmQM("classDataNotFound", lukChEntities($luokka)));
-  $haveData = FALSE;
-}
-else
-{
-  require($dataFile);
-  $haveData = isset($classInfo);
-}
-
-
 function lukPrintTimeTable($mini)
 {
   global $classInfo, $classHourDefs, $classHourTimes;
@@ -462,37 +447,104 @@
 }
 
 
-//
+
+// Main code starts
 //
 //
+
+// Read classfile
+if (($classes = lukReadClassFile($classIDFile)) === false)
+  stError(stQM("classListNotFound"));
+
+// Read class data
+$dataFile = $cachePath.$luokka.".data";
+if (!file_exists($dataFile))
+{
+  stError(cmQM("classDataNotFound", chentities($luokka)));
+  $haveData = FALSE;
+}
+else
+{
+  require($dataFile);
+  $haveData = isset($classInfo);
+}
+
+// Set some variables
 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka;
-cmPrintPageHeader($pageTitle);
+
+$extra = "";
+foreach (array(57 => FALSE, 76 => TRUE, 114 => TRUE, 120 => TRUE, 152 => TRUE) as $iconSize => $addSize)
+{
+  $extra .= "  <link rel=\"apple-touch-icon\" ".
+    ($addSize ? "sizes=\"".$iconSize."x".$iconSize."\" " : "").
+    "href=\"icon-".$iconSize."-precomposed.png\" />\n";
+}
 
-echo "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">
- <table>
-  <tr>
-   <th>
-    <select name=\"luokka\">
-";
+cmPrintPageHeader($pageTitle, $extra);
+?>
+<script type="text/javascript">
+function jsCreateXMLRequest()
+{
+  var req;
+  if (window.XMLHttpRequest)
+  {
+    // Modern browsers
+    req = new XMLHttpRequest();
+  }
+  else
+  {
+    // Old IE versions
+    req = new ActiveXObject("Microsoft.XMLHTTP");
+  }
+  return req;
+}
+
 
-foreach ($classes as $class) {
-  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n";
+function jsSendPOSTRequest(params, success, failure)
+{
+  var req = jsCreateXMLRequest();
+  req.open("POST", "ajaks.php", true);
+  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+  req.setRequestHeader("Content-length", params.length);
+  req.setRequestHeader("Connection", "close");
+  req.send(params);
+}
+
+function jsClickAdvert()
+{
+  jsSendPOSTRequest("a=HyperSpaceYard&t=DivisionCell");
+  return true;
+}
+</script>
+<?
+
+echo
+  "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">\n".
+  " <table>\n".
+  "  <tr>\n".
+  "   <th>\n".
+  "    <select name=\"luokka\">\n";
+
+foreach ($classes as $class)
+{
+  echo
+    "     <option ".($luokka == $class ? "selected=\"selected\" " : "").
+    "value=\"".$class."\">".chentities($class)."</option>\n";
 }
 echo 
-"    </select>
-   </th>
-   <th><input class=\"submit\" type=\"submit\" value=\"".cmQM("Switch")."\" /></th>
-";
+  "    </select>\n".
+  "   </th>\n".
+  "   <th><input class=\"submit\" type=\"submit\" value=\"".cmQM("Switch")."\" /></th>\n";
 
 if (!$mobileMode)
 {
   echo
-  "   <th><a id=\"next\" class=\"textctrl\" href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".
-  cmQM("Shown").": ".($nextPeriod ? cmQM("Next period") : cmQM("Current period")).
-  "</a></th>\n".
-  "   <th><a id=\"orig\" class=\"textctrl\" href=\"".$origBaseURI.$luokka.$origBaseExt."\">".cmQM("Original")."</a></th>\n".
-  "   <th><a id=\"xml\" class=\"textctrl\" href=\"".$baseURI.$cachePath."/".$luokka.".xml\">XML</a></th>\n".
-  "   <th><a class=\"textctrl mobile\" href=\"http://tnsp.org/mluk/\">Mobile</a></th>\n";
+    "   <th><a id=\"next\" class=\"textctrl\" href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".
+    cmQM("Shown").": ".($nextPeriod ? cmQM("Next period") : cmQM("Current period")).
+    "</a></th>\n".
+    "   <th><a id=\"orig\" class=\"textctrl\" href=\"".$origBaseURI.$luokka.$origBaseExt."\">".cmQM("Original")."</a></th>\n".
+    "   <th><a id=\"xml\" class=\"textctrl\" href=\"".$baseURI.$cachePath."/".$luokka.".xml\">XML</a></th>\n".
+    "   <th><a class=\"textctrl mobile\" href=\"http://tnsp.org/mluk/\">Mobile</a></th>\n";
 }
 
 echo
@@ -517,25 +569,29 @@
 }
 
 echo
-"<div id=\"footer\">".
-cmQM("contact", "ccr @ IRCNet", "ccr (at) tnsp (dot) org").
-//" <div style=\"color: red;\">".cmQM("beta")."</div>\n".
-"</div>\n";
+  "<div id=\"footer\">".
+  cmQM("contact", "ccr @ IRCNet", "ccr (at) tnsp (dot) org").
+  //" <div style=\"color: red;\">".cmQM("beta")."</div>\n".
+  "</div>\n";
 
 if (!$mobileMode)
 {
-echo "<div id=\"csssel\">\n".
-" <div id=\"ctitle\">Lukkari v2.0</div>\n".
-" <div>".cmQM("Style").": ";
+  echo
+    "<div id=\"csssel\">\n".
+    " <div id=\"ctitle\">Lukkari v2.0</div>\n".
+    " <div>".cmQM("Style").": ";
+
+  foreach ($pageCSSAlts as $name => $id)
+    echo "<a href=\"".$baseURI."?css=".$id."\">".$name."</a>";
 
-foreach ($pageCSSAlts as $name => $id)
-  echo "<a href=\"".$baseURI."?css=".$id."\">".$name."</a>";
+  echo
+    "</div>\n".
+    " <div id=\"clang\">";
 
-echo "</div>\n".
-" <div id=\"clang\">";
-foreach ($pageLanguages as $id)
-  echo "<a href=\"".$baseURI."?lang=".$id."\">".$id."</a>";
-echo "</div>\n";
+  foreach ($pageLanguages as $id)
+    echo "<a href=\"".$baseURI."?lang=".$id."\">".$id."</a>";
+
+  echo "</div>\n";
 }
 
 echo "</div>\n";