Mercurial > hg > lukkari
comparison index.php @ 107:9f976ae7a3e2
Reformat, modularize, cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 11 Dec 2013 16:38:15 +0200 |
parents | e7bd58dec4f3 |
children | 436aea5bf49f |
comparison
equal
deleted
inserted
replaced
106:e7bd58dec4f3 | 107:9f976ae7a3e2 |
---|---|
2 // | 2 // |
3 // Lukkari v2.0 | 3 // Lukkari v2.0 |
4 // (C) Copyright 2010 - 2012 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | 4 // (C) Copyright 2010 - 2012 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
5 // Yes, this code is rather horrible. :| | 5 // Yes, this code is rather horrible. :| |
6 // | 6 // |
7 // Include framework | |
8 require "mcommon.inc.php"; | |
9 | |
7 $pageLang = "fi"; | 10 $pageLang = "fi"; |
8 $pageCharset = "utf-8"; | 11 $pageCharset = "utf-8"; |
9 $internalCharset = "utf-8"; | |
10 $classDefault = "TTE1SNO"; | 12 $classDefault = "TTE1SNO"; |
11 $classIDFile = "classes.txt"; | 13 $classIDFile = "classes.txt"; |
12 $courseCacheFile = "coursecache.txt"; | 14 $courseCacheFile = "coursecache.txt"; |
13 $baseURI = "http://tnsp.org/luk/"; | 15 $baseURI = "http://tnsp.org/luk/"; |
14 | 16 |
77 "Style" => array("fi" => "Tyyli"), | 79 "Style" => array("fi" => "Tyyli"), |
78 // "" => array("fi" => ), | 80 // "" => array("fi" => ), |
79 ); | 81 ); |
80 | 82 |
81 | 83 |
82 // Include framework | |
83 require "mcommon.inc.php"; | |
84 | |
85 | |
86 function lukGetDayName($day) | 84 function lukGetDayName($day) |
87 { | 85 { |
88 global $lukDayNames, $pageLang; | 86 global $lukDayNames, $pageLang; |
89 | 87 |
90 if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day])) | 88 if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day])) |
91 return $lukDayNames[$pageLang][$day]; | 89 return $lukDayNames[$pageLang][$day]; |
92 else | 90 else |
93 return $lukDayNames["en"][$day]; | 91 return $lukDayNames["en"][$day]; |
94 } | |
95 | |
96 | |
97 function lukChEntities($str) | |
98 { | |
99 global $internalCharset; | |
100 return htmlentities($str, ENT_NOQUOTES, $internalCharset); | |
101 } | 92 } |
102 | 93 |
103 | 94 |
104 function lukCheckClassID(&$id) | 95 function lukCheckClassID(&$id) |
105 { | 96 { |
146 } | 137 } |
147 | 138 |
148 | 139 |
149 function lukMatchCourse($id) | 140 function lukMatchCourse($id) |
150 { | 141 { |
151 global $cache, $cacheDirty, $internalCharset, $pageLang; | 142 global $cache, $cacheDirty, $pageCharset, $pageLang; |
152 | 143 |
153 $uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id; | 144 $uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id; |
154 | 145 |
155 // Create the index | 146 // Create the index |
156 if (!isset($cache[$id])) | 147 if (!isset($cache[$id])) |
162 // Not cached, try to fetch data | 153 // Not cached, try to fetch data |
163 $cacheDirty = TRUE; | 154 $cacheDirty = TRUE; |
164 $data = @file_get_contents($uri); | 155 $data = @file_get_contents($uri); |
165 if ($data !== FALSE) | 156 if ($data !== FALSE) |
166 { | 157 { |
167 $data = iconv("iso-8859-1", $internalCharset, $data); | 158 $data = iconv("iso-8859-1", $pageCharset, $data); |
168 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) | 159 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) |
169 { | 160 { |
170 // Add data to cache | 161 // Add data to cache |
171 $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2])); | 162 $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2])); |
172 } | 163 } |
173 } | 164 } |
174 } | 165 } |
175 | 166 |
176 if (isset($cache[$id]) && isset($cache[$id][$pageLang])) | 167 if (isset($cache[$id]) && isset($cache[$id][$pageLang])) |
177 { | 168 { |
178 return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id][$pageLang]["op"]." op"). | 169 return "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id][$pageLang]["op"]." op"). |
179 "\" href=\"".lukChEntities($uri)."\">".lukChEntities($cache[$id][$pageLang]["desc"])."</a>"; | 170 "\" href=\"".chentities($uri)."\">".chentities($cache[$id][$pageLang]["desc"])."</a>"; |
180 } | 171 } |
181 else | 172 else |
182 return lukChEntities($id); | 173 return chentities($id); |
183 } | 174 } |
184 | 175 |
185 | 176 |
186 function lukGetClassInfo($grouped, $data) | 177 function lukGetClassInfo($grouped, $data) |
187 { | 178 { |
203 $out .= "<tr>"; | 194 $out .= "<tr>"; |
204 foreach ($data as $col) | 195 foreach ($data as $col) |
205 { | 196 { |
206 $out .= "<td>"; | 197 $out .= "<td>"; |
207 if (isset($col[$i])) | 198 if (isset($col[$i])) |
208 $out .= lukChEntities($col[$i]); | 199 $out .= chentities($col[$i]); |
209 $out .= "</td>"; | 200 $out .= "</td>"; |
210 } | 201 } |
211 $out .= "</tr>"; | 202 $out .= "</tr>"; |
212 } | 203 } |
213 | 204 |
217 { | 208 { |
218 $out = lukMatchCourse($data[0][0])."<br />"; | 209 $out = lukMatchCourse($data[0][0])."<br />"; |
219 | 210 |
220 for ($i = 1; $i < count($data[0]); $i++) | 211 for ($i = 1; $i < count($data[0]); $i++) |
221 { | 212 { |
222 $out .= lukChEntities($data[0][$i])."<br />"; | 213 $out .= chentities($data[0][$i])."<br />"; |
223 } | 214 } |
224 | 215 |
225 return $out; | 216 return $out; |
226 } | 217 } |
227 } | 218 } |
238 $hour < $classHourDefs[$id]["start"] + $classHourDefs[$id]["hours"]) | 229 $hour < $classHourDefs[$id]["start"] + $classHourDefs[$id]["hours"]) |
239 return $id; | 230 return $id; |
240 } | 231 } |
241 } | 232 } |
242 return 0; | 233 return 0; |
234 } | |
235 | |
236 | |
237 function lukReadClassFile($filename) | |
238 { | |
239 // Attempt to open file for reading | |
240 if (($fp = @fopen($filename, "rb")) === false) | |
241 return false; | |
242 | |
243 $classes = FALSE; | |
244 | |
245 // Lock file so that we do not get clashes | |
246 if (flock($fp, LOCK_SH)) | |
247 { | |
248 $classes = array(); | |
249 // Read and parse data | |
250 while (!feof($fp)) | |
251 { | |
252 $str = trim(fgets($fp, 128)); | |
253 if (strlen($str) > 2 && $str[0] != "#") | |
254 $classes[] = $str; | |
255 } | |
256 | |
257 // Release lock | |
258 flock($fp, LOCK_UN); | |
259 } | |
260 | |
261 fclose($fp); | |
262 return $classes; | |
243 } | 263 } |
244 | 264 |
245 | 265 |
246 // Check given parameters | 266 // Check given parameters |
247 // Language must be the first setting to be validated, | 267 // Language must be the first setting to be validated, |
336 { | 356 { |
337 require($courseCacheFile); | 357 require($courseCacheFile); |
338 flock($fp, LOCK_UN); | 358 flock($fp, LOCK_UN); |
339 } | 359 } |
340 fclose($fp); | 360 fclose($fp); |
341 } | |
342 | |
343 | |
344 // Read classfile | |
345 $classes = array(); | |
346 if (($fp = @fopen($classIDFile, "rb")) !== false) | |
347 { | |
348 if (flock($fp, LOCK_SH)) | |
349 { | |
350 while (!feof($fp)) | |
351 { | |
352 $str = trim(fgets($fp, 128)); | |
353 if (strlen($str) > 2 && $str[0] != "#") | |
354 $classes[] = $str; | |
355 } | |
356 flock($fp, LOCK_UN); | |
357 } | |
358 fclose($fp); | |
359 } | |
360 else | |
361 { | |
362 stError(stQM("classListNotFound")); | |
363 } | |
364 | |
365 | |
366 $dataFile = $cachePath.$luokka.".data"; | |
367 if (!file_exists($dataFile)) | |
368 { | |
369 stError(cmQM("classDataNotFound", lukChEntities($luokka))); | |
370 $haveData = FALSE; | |
371 } | |
372 else | |
373 { | |
374 require($dataFile); | |
375 $haveData = isset($classInfo); | |
376 } | 361 } |
377 | 362 |
378 | 363 |
379 function lukPrintTimeTable($mini) | 364 function lukPrintTimeTable($mini) |
380 { | 365 { |
460 | 445 |
461 return $out."</table>\n"; | 446 return $out."</table>\n"; |
462 } | 447 } |
463 | 448 |
464 | 449 |
450 | |
451 // Main code starts | |
465 // | 452 // |
466 // | 453 // |
467 // | 454 |
455 // Read classfile | |
456 if (($classes = lukReadClassFile($classIDFile)) === false) | |
457 stError(stQM("classListNotFound")); | |
458 | |
459 // Read class data | |
460 $dataFile = $cachePath.$luokka.".data"; | |
461 if (!file_exists($dataFile)) | |
462 { | |
463 stError(cmQM("classDataNotFound", chentities($luokka))); | |
464 $haveData = FALSE; | |
465 } | |
466 else | |
467 { | |
468 require($dataFile); | |
469 $haveData = isset($classInfo); | |
470 } | |
471 | |
472 // Set some variables | |
468 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; | 473 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; |
469 cmPrintPageHeader($pageTitle); | 474 |
470 | 475 $extra = ""; |
471 echo "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\"> | 476 foreach (array(57 => FALSE, 76 => TRUE, 114 => TRUE, 120 => TRUE, 152 => TRUE) as $iconSize => $addSize) |
472 <table> | 477 { |
473 <tr> | 478 $extra .= " <link rel=\"apple-touch-icon\" ". |
474 <th> | 479 ($addSize ? "sizes=\"".$iconSize."x".$iconSize."\" " : ""). |
475 <select name=\"luokka\"> | 480 "href=\"icon-".$iconSize."-precomposed.png\" />\n"; |
476 "; | 481 } |
477 | 482 |
478 foreach ($classes as $class) { | 483 cmPrintPageHeader($pageTitle, $extra); |
479 echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n"; | 484 ?> |
485 <script type="text/javascript"> | |
486 function jsCreateXMLRequest() | |
487 { | |
488 var req; | |
489 if (window.XMLHttpRequest) | |
490 { | |
491 // Modern browsers | |
492 req = new XMLHttpRequest(); | |
493 } | |
494 else | |
495 { | |
496 // Old IE versions | |
497 req = new ActiveXObject("Microsoft.XMLHTTP"); | |
498 } | |
499 return req; | |
500 } | |
501 | |
502 | |
503 function jsSendPOSTRequest(params, success, failure) | |
504 { | |
505 var req = jsCreateXMLRequest(); | |
506 req.open("POST", "ajaks.php", true); | |
507 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
508 req.setRequestHeader("Content-length", params.length); | |
509 req.setRequestHeader("Connection", "close"); | |
510 req.send(params); | |
511 } | |
512 | |
513 function jsClickAdvert() | |
514 { | |
515 jsSendPOSTRequest("a=HyperSpaceYard&t=DivisionCell"); | |
516 return true; | |
517 } | |
518 </script> | |
519 <? | |
520 | |
521 echo | |
522 "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">\n". | |
523 " <table>\n". | |
524 " <tr>\n". | |
525 " <th>\n". | |
526 " <select name=\"luokka\">\n"; | |
527 | |
528 foreach ($classes as $class) | |
529 { | |
530 echo | |
531 " <option ".($luokka == $class ? "selected=\"selected\" " : ""). | |
532 "value=\"".$class."\">".chentities($class)."</option>\n"; | |
480 } | 533 } |
481 echo | 534 echo |
482 " </select> | 535 " </select>\n". |
483 </th> | 536 " </th>\n". |
484 <th><input class=\"submit\" type=\"submit\" value=\"".cmQM("Switch")."\" /></th> | 537 " <th><input class=\"submit\" type=\"submit\" value=\"".cmQM("Switch")."\" /></th>\n"; |
485 "; | |
486 | 538 |
487 if (!$mobileMode) | 539 if (!$mobileMode) |
488 { | 540 { |
489 echo | 541 echo |
490 " <th><a id=\"next\" class=\"textctrl\" href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">". | 542 " <th><a id=\"next\" class=\"textctrl\" href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">". |
491 cmQM("Shown").": ".($nextPeriod ? cmQM("Next period") : cmQM("Current period")). | 543 cmQM("Shown").": ".($nextPeriod ? cmQM("Next period") : cmQM("Current period")). |
492 "</a></th>\n". | 544 "</a></th>\n". |
493 " <th><a id=\"orig\" class=\"textctrl\" href=\"".$origBaseURI.$luokka.$origBaseExt."\">".cmQM("Original")."</a></th>\n". | 545 " <th><a id=\"orig\" class=\"textctrl\" href=\"".$origBaseURI.$luokka.$origBaseExt."\">".cmQM("Original")."</a></th>\n". |
494 " <th><a id=\"xml\" class=\"textctrl\" href=\"".$baseURI.$cachePath."/".$luokka.".xml\">XML</a></th>\n". | 546 " <th><a id=\"xml\" class=\"textctrl\" href=\"".$baseURI.$cachePath."/".$luokka.".xml\">XML</a></th>\n". |
495 " <th><a class=\"textctrl mobile\" href=\"http://tnsp.org/mluk/\">Mobile</a></th>\n"; | 547 " <th><a class=\"textctrl mobile\" href=\"http://tnsp.org/mluk/\">Mobile</a></th>\n"; |
496 } | 548 } |
497 | 549 |
498 echo | 550 echo |
499 " </tr>\n". | 551 " </tr>\n". |
500 " </table>\n". | 552 " </table>\n". |
515 { | 567 { |
516 echo lukPrintTimeTable($mobileMode); | 568 echo lukPrintTimeTable($mobileMode); |
517 } | 569 } |
518 | 570 |
519 echo | 571 echo |
520 "<div id=\"footer\">". | 572 "<div id=\"footer\">". |
521 cmQM("contact", "ccr @ IRCNet", "ccr (at) tnsp (dot) org"). | 573 cmQM("contact", "ccr @ IRCNet", "ccr (at) tnsp (dot) org"). |
522 //" <div style=\"color: red;\">".cmQM("beta")."</div>\n". | 574 //" <div style=\"color: red;\">".cmQM("beta")."</div>\n". |
523 "</div>\n"; | 575 "</div>\n"; |
524 | 576 |
525 if (!$mobileMode) | 577 if (!$mobileMode) |
526 { | 578 { |
527 echo "<div id=\"csssel\">\n". | 579 echo |
528 " <div id=\"ctitle\">Lukkari v2.0</div>\n". | 580 "<div id=\"csssel\">\n". |
529 " <div>".cmQM("Style").": "; | 581 " <div id=\"ctitle\">Lukkari v2.0</div>\n". |
530 | 582 " <div>".cmQM("Style").": "; |
531 foreach ($pageCSSAlts as $name => $id) | 583 |
532 echo "<a href=\"".$baseURI."?css=".$id."\">".$name."</a>"; | 584 foreach ($pageCSSAlts as $name => $id) |
533 | 585 echo "<a href=\"".$baseURI."?css=".$id."\">".$name."</a>"; |
534 echo "</div>\n". | 586 |
535 " <div id=\"clang\">"; | 587 echo |
536 foreach ($pageLanguages as $id) | 588 "</div>\n". |
537 echo "<a href=\"".$baseURI."?lang=".$id."\">".$id."</a>"; | 589 " <div id=\"clang\">"; |
538 echo "</div>\n"; | 590 |
591 foreach ($pageLanguages as $id) | |
592 echo "<a href=\"".$baseURI."?lang=".$id."\">".$id."</a>"; | |
593 | |
594 echo "</div>\n"; | |
539 } | 595 } |
540 | 596 |
541 echo "</div>\n"; | 597 echo "</div>\n"; |
542 cmPrintPageFooter(); | 598 cmPrintPageFooter(); |
543 | 599 |