changeset 38:2195ede068c8

Add chentities() helper function and use it.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Mar 2012 07:30:59 +0200
parents 3ba4feff55cb
children 78d0c1cd7282
files index.php
diffstat 1 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Thu Mar 08 07:25:01 2012 +0200
+++ b/index.php	Thu Mar 08 07:30:59 2012 +0200
@@ -1,6 +1,7 @@
 <?
 $pageCSS = array("http://tnsp.org/docs1.css", "luk.css");
-$pageCharset = "iso-8859-15";
+$pageCharset = "utf-8";
+$internalCharset = "utf-8";
 $luokkaDefault = "TTE9SNO";
 $dataDir = "cache/";
 $classFile = "luokat.txt";
@@ -34,6 +35,13 @@
 
 
 // Helper functions
+function chentities($str)
+{
+  global $internalCharset;
+//  return $str;
+  return htmlentities($str, ENT_NOQUOTES, $internalCharset);
+}
+
 function checkClassID(&$id)
 {
   global $luokkaDefault;
@@ -97,7 +105,7 @@
 
 function matchCourse($id)
 {
-  global $cache, $infoURI, $cacheDirty;
+  global $cache, $infoURI, $cacheDirty, $internalCharset;
 
   // Check if course exists in cache
   if (!isset($cache[$id])) {
@@ -105,6 +113,7 @@
     // Not cached, try to fetch data
     $data = @file_get_contents($infoURI.$id);
     if ($data !== FALSE) {
+      $data = iconv("iso-8859-1", $internalCharset, $data);
       if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) {
         // Add data to cache
         $cache[$id] = array("desc" => $m[1], "op" => intval($m[2]));
@@ -113,10 +122,10 @@
   }
   
   if (isset($cache[$id]))
-    return "<a target=\"_blank\" title=\"".htmlentities($id." - ".$cache[$id]["op"]." op").
-    "\" href=\"".htmlentities($infoURI.$id)."\">".htmlentities($cache[$id]["desc"])."</a>";
+    return "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id]["op"]." op").
+    "\" href=\"".chentities($infoURI.$id)."\">".chentities($cache[$id]["desc"])."</a>";
   else
-    return htmlentities($id);
+    return chentities($id);
 }
 
 
@@ -165,7 +174,7 @@
   $dataFile = "cache/".$luokka.".data";
 
 if (!file_exists($dataFile)) {
-  errorMsg("Luokan ".htmlentities($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.");
+  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.");
   $haveData = FALSE;
 } else {
   require($dataFile);
@@ -221,14 +230,14 @@
     "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>";
 
     for ($j = 2; $j < count($d); $j += 2)
-      $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>";
+      $out .= "<tr><td>".chentities($d[$j])."</td><td>".(isset($d[$j+1]) ? chentities($d[$j+1]) : "")."</td></tr>";
     
     return $out."</table>";
   } else {
     $out = matchCourse($d[0])."<br />";
 
     for ($j = 1; $j < count($d); $j++)
-      $out .= htmlentities($d[$j])."<br />";
+      $out .= chentities($d[$j])."<br />";
 
     return $out;
   }
@@ -401,7 +410,7 @@
 ";
 
 foreach ($classes as $class) {
-  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n";
+  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".chentities($class)."</option>\n";
 }
 echo 
 "    </select>