view index.php @ 0:02f0f79f98b6

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Jan 2011 20:42:54 +0200
parents
children
line wrap: on
line source

<?

$mapfile = "kartta.png";
$classfile = "luokkatilat.txt";
$cachefile = "coursecache.txt";
$baseURI = "http://tnsp.org/luk/?";
$lukuURI = "http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_";
$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";

// Check given parameters
$luokka = isset($_GET["luokka"]) ? $_GET["luokka"] : "TTE9SNO";

if (!preg_match("#^[A-Z]{3}\d\S+#", $luokka)) {
  echo "Virhe! Luokan t&auml;ytyy olla muotoa XXXnXXX.<br />\n";
  exit;
}

if (isset($_GET["tila"])) {
  $tila = $_GET["tila"];
  echo "Luokkatilan n&auml;ytt&ouml;moodi ei viel&auml; tuettu.<br />\n";
  exit;

  $fp = @fopen($classfile, "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 match_course($matches)
{
  global $cache, $infoURI, $dirty;

  $id = $matches[1];

  // Check if course exists in cache
  if (!isset($cache[$id])) {
    $dirty = TRUE;
    // Not cached, try to fetch data
    $data = @file_get_contents($infoURI.$id);
    if ($data !== FALSE) {
      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]));
      }
    }
  }
  
  if (isset($cache[$id]))
    return "<a target=\"_blank\" title=\"".htmlentities($id." - ".$cache[$id]["op"]." op").
    "\" href=\"".htmlentities($infoURI.$id)."\">".htmlentities($cache[$id]["desc"])."</a>";
  else
    return htmlentities($id);
}


function match_class($matches)
{
  global $baseURI;
  return "<b><a href=\"".$baseURI."tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2];
}


/* 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 ($data !== FALSE) {
  $data = preg_replace_callback("#<B>([A-Z]\d+)\.?</B>#", match_course, $data);
  
  $data = preg_replace_callback("#<B>(\d{4}|ATK\d+|[A-Z]{2,3}LAB\d+|[A-Z]{2,3}LAB|Audit\d+)(\(\d+\))?</B>#", match_class, $data);

  $data = preg_replace("#<body class=tt>#", "<body class=tt>\n".
  "<a href=\"".$lukuURI.$luokka.".htm\">[orig]</a>\n, Luokat: ".
  "<a href=\"".$baseURI."luokka=TTE0SNO"."\">[TTE0SNO]</a>\n".
  "<a href=\"".$baseURI."luokka=TTE9SNO"."\">[TTE9SNO]</a>\n", $data);
  echo $data;
} else {
  echo "Luokan datatiedostoa <b>".htmlentities($lukuURI.$luokka.".htm")."</b> ei saatu.<br />\n";
}


// Dump the course data cache, but only if it has changed
if ($dirty) {
  // 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, "<?\n\$cache = array(\n");
      foreach ($cache as $id => $data) {
        fwrite($fp, "  \"".addslashes($id)."\" => array(\"desc\" => \"".
        addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n");
      }
      fwrite($fp, ");\n?>");
    }
    fclose($fp);
  }
}

?>