Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ob_start('ob_gzhandler');
- $allowed_formats = array("xml", "json", "csv");
- $allowed_classes = array("#^(A|B|C|D|E)\d{2}$#", "#^(WI|WING)[[:alpha:]]{0,1}\d{2}$#", "#^MB[[:alpha:]]{0,1}\d{1,2}$#");
- $format;
- if (!isset($_GET["format"])) {
- $format = "xml";
- } else {
- $format = strtolower($_GET["format"]);
- }
- if (!in_array($format, $allowed_formats)) {
- printError_XML("param_error", "Ungültiger Format-Parameter; Erlaubt sind: "
- . implode(", ", $allowed_formats));
- exit();
- }
- if (!isset($_GET["klasse"]) || is_array($_GET["klasse"])) {
- if ($format == "xml" or $format == "csv") {
- printError_XML("param_error", "Keine oder mehrere Klasse(n) angegeben");
- } else if ($format == "json") {
- printError_JSON("param_error", "Keine oder mehrere Klasse(n) angegeben");
- }
- exit();
- }
- if (!isset($_GET["block"]) || is_array($_GET["block"])) {
- if ($format == "xml" or $format == "csv") {
- printError_XML("param_error", "Keine oder mehrere Block-Nummer(n) angegeben");
- } else if ($format == "json") {
- printError_JSON("param_error", "Keine oder mehrere Block-Nummer(n) angegeben");
- }
- exit();
- }
- $block = $_GET["block"];
- $i = intval($block);
- if (!is_numeric($block) || $i < 1 || $i > 9) {
- if ($format == "xml" or $format == "csv") {
- printError_XML("param_error", "Ungültige Block-Nummer: Block-Nummern muessen Zahlen zwischen 1 und 6 sein");
- } else if ($format == "json") {
- printError_JSON("param_error", "Ungültige Block-Nummer: Block-Nummern muessen Zahlen zwischen 1 und 6 sein");
- }
- exit();
- }
- $klasse = strtoupper($_GET["klasse"]);
- $classValid = FALSE;
- foreach ($allowed_classes as $pattern) {
- if (preg_match($pattern, $klasse) == 1) {
- $classValid = TRUE;
- break;
- }
- }
- if (!$classValid) {
- if ($format == "xml" or $format == "csv") {
- printError_XML("param_error", "Ungültige Klasse: Klassen müssen heißen, wie in der Stundenplanübersicht");
- } else if ($format == "json") {
- printError_JSON("param_error", "Ungültige Klasse: Klassen müssen heißen, wie in der Stundenplanübersicht");
- }
- exit();
- }
- $GLOBALS["format"] = $format;
- $GLOBALS["klasse"] = $klasse;
- $GLOBALS["block"] = $block;
- $sced_url = "http://www.asw-berufsakademie.de/fileadmin/download/download/Sked%20Stundenplan/";
- $sced_url .= $klasse . "-" . $block . ".%20Block.html";
- $scedHtml = grabUrl($sced_url);
- if ($scedHtml == FALSE || is_null($scedHtml) || strlen($scedHtml) < 1 || $scedHtml == 404) {
- if ($format == "xml" or $format == "csv") {
- printError_XML("error", "Download des Stundenplans nicht erfolgreich. Bitte ggf. die Klasse und den Block überprüfen!");
- } else if ($format == "json") {
- printError_JSON("error", "Download des Stundenplans nicht erfolgreich. Bitte ggf. die Klasse und den Block überprüfen!");
- }
- exit();
- }
- // Remove newlines, convert single quotes to double quotes
- $scedHtml = (string) str_replace(array("\r", "\r\n", "\n"), "", $scedHtml);
- $scedHtml = (string) str_replace("'", "\"", $scedHtml);
- $stundenplan = parseAswSchedule($scedHtml);
- if ($format == "xml") {
- printDoc_XML(planToXML($stundenplan));
- } else if ($format == "json") {
- printDoc_JSON(planToJSON($stundenplan));
- } else if($format == "csv") {
- $filename = $stundenplan->getKlasse() . '_Block-' . $stundenplan->getBlock() . '.csv';
- printDoc_CSV(planToCSV($stundenplan), $filename);
- }
- class ParsingException extends Exception {
- }
- class Stundenplan {
- private $klasse;
- private $block;
- private $wochen;
- function __construct($_klasse, $_block) {
- $this->klasse = $_klasse;
- $this->block = $_block;
- $this->wochen = array();
- }
- public function addWoche($woche) {
- $this->wochen[] = $woche;
- }
- public function getWochen() {
- return $this->wochen;
- }
- public function getKlasse() {
- return $this->klasse;
- }
- public function getBlock() {
- return $this->block;
- }
- }
- class Woche {
- private $lfdNr;
- private $startDatum;
- private $endDatum;
- private $kalendertage;
- function __construct($_lfdNr, $_startDatum, $_endDatum) {
- $this->lfdNr = $_lfdNr;
- $this->startDatum = $_startDatum;
- $this->endDatum = $_endDatum;
- $this->datums = array();
- }
- public function addKalendertag($kalendertag) {
- $this->kalendertage[] = $kalendertag;
- }
- public function getKalendertage() {
- return $this->kalendertage;
- }
- public function getLfdNr() {
- return $this->lfdNr;
- }
- public function getStartDatum() {
- return $this->startDatum;
- }
- public function getEndDatum() {
- return $this->endDatum;
- }
- }
- class Kalendertag {
- private $timestamp;
- private $termine;
- private $isSorted = false;
- function __construct($_timestamp) {
- $this->timestamp = $_timestamp;
- $this->termine = array();
- }
- public function addTermin($termin) {
- $this->termine[] = $termin;
- }
- public function addTermine($termine) {
- $this->termine = array_merge($this->termine, $termine);
- }
- public function getTermine() {
- if (!$this->isSorted) {
- usort($this->termine, "compareTermin");
- }
- return $this->termine;
- }
- public function getTimestamp() {
- return $this->timestamp;
- }
- public function getTerminCount() {
- return count($this->termine);
- }
- }
- class Termin {
- private $zeitVon;
- private $zeitBis;
- private $typ;
- private $name;
- private $saal;
- function __construct($_zeit_von, $_zeit_bis, $_typ, $_name, $_saal) {
- $this->zeitVon = $_zeit_von;
- $this->zeitBis = $_zeit_bis;
- $this->typ = $_typ;
- $this->name = $_name;
- $this->saal = $_saal;
- }
- public function getZeitVon() {
- return $this->zeitVon;
- }
- public function getZeitBis() {
- return $this->zeitBis;
- }
- public function getTyp() {
- return $this->typ;
- }
- public function getName() {
- return $this->name;
- }
- public function getSaal() {
- return $this->saal;
- }
- }
- class Doppeltermin {
- private $zeitVon;
- private $zeitBis;
- private $typen;
- private $namen;
- private $saele;
- function __construct($_zeit_von, $_zeit_bis, $_typen, $_namen, $_saele) {
- $this->zeitVon = $_zeit_von;
- $this->zeitBis = $_zeit_bis;
- $this->typen = $_typen;
- $this->namen = $_namen;
- $this->saele = $_saele;
- }
- public function getZeitVon() {
- return $this->zeitVon;
- }
- public function getZeitBis() {
- return $this->zeitBis;
- }
- public function getTypen() {
- return $this->typen;
- }
- public function getNamen() {
- return $this->namen;
- }
- public function getSaele() {
- return $this->saele;
- }
- }
- function grabUrl($submit_url) { // returns String
- $curl = curl_init();
- $url_parts = parse_url($submit_url);
- $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
- curl_setopt($curl, CURLOPT_HEADER, false);
- if (isset($url_parts['query'])) {
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $url_parts['query']);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_URL, $url);
- $response = curl_exec($curl);
- $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($httpCode == 404) {
- return $httpCode;
- }
- curl_close($curl);
- return $response;
- }
- function planToXML(Stundenplan $plan) {
- $doc = new DOMDocument();
- $doc->formatOutput = true;
- $planElm = $doc->createElement("stundenplan");
- $planElm->setAttribute("klasse", $plan->getKlasse());
- $planElm->setAttribute("block", $plan->getBlock());
- foreach ($plan->getWochen() as $woche) {
- $wocheElm = $doc->createElement("woche");
- $wocheElm->setAttribute("von", $woche->getStartDatum());
- $wocheElm->setAttribute("bis", $woche->getEndDatum());
- $wocheElm->setAttribute("lfdnr", $woche->getLfdNr());
- foreach ($woche->getKalendertage() as $kalTag) {
- if ($kalTag->getTerminCount() < 1) {
- continue;
- }
- $kalTagElm = $doc->createElement("kalendertag");
- $kalTagElm->setAttribute("datum", $kalTag->getTimestamp());
- foreach ($kalTag->getTermine() as $termin) {
- if ($termin instanceof Doppeltermin) {
- $terminElm = $doc->createElement("doppeltermin");
- $typen = $termin->getTypen();
- $namen = $termin->getNamen();
- $saele = $termin->getSaele();
- $terminElm->setAttribute("typ_a", $typen[0]);
- $terminElm->setAttribute("typ_b", $typen[1]);
- $terminElm->setAttribute("name_a", $namen[0]);
- $terminElm->setAttribute("name_b", $namen[1]);
- $terminElm->setAttribute("saal_a", $saele[0]);
- $terminElm->setAttribute("saal_b", $saele[1]);
- } else {
- $terminElm = $doc->createElement("termin");
- $terminElm->setAttribute("typ", $termin->getTyp());
- $terminElm->setAttribute("name", $termin->getName());
- $terminElm->setAttribute("saal", $termin->getSaal());
- }
- $terminElm->setAttribute("von", $termin->getZeitVon());
- $terminElm->setAttribute("bis", $termin->getZeitBis());
- $kalTagElm->appendChild($terminElm);
- }
- $wocheElm->appendChild($kalTagElm);
- }
- $planElm->appendChild($wocheElm);
- }
- $doc->appendChild($planElm);
- return $doc->saveXML();
- }
- function planToJSON(Stundenplan $plan) {
- $jsonArr = array("stundenplan" => array());
- $jsonArr["stundenplan"]["klasse"] = $GLOBALS["klasse"];
- $jsonArr["stundenplan"]["block"] = intval($GLOBALS["block"]);
- $jsonArr["stundenplan"]["wochen"] = array();
- foreach ($plan->getWochen() as $woche) {
- $wocheElm = array();
- $wocheElm["von"] = $woche->getStartDatum();
- $wocheElm["bis"] = $woche->getEndDatum();
- $wocheElm["lfdnr"] = intval($woche->getLfdNr());
- $wocheElm["kalendertage"] = array();
- foreach ($woche->getKalendertage() as $kalTag) {
- if ($kalTag->getTerminCount() < 1) {
- continue;
- }
- $kalTagElm = array();
- $kalTagElm["datum"] = $kalTag->getTimestamp();
- $kalTagElm["termine"] = array();
- foreach ($kalTag->getTermine() as $termin) {
- $terminElm = array();
- if ($termin instanceof Doppeltermin) {
- $typen = $termin->getTypen();
- $namen = $termin->getNamen();
- $saele = $termin->getSaele();
- $terminElm["doppel"] = true;
- $terminElm["typ_a"] = $typen[0];
- $terminElm["typ_b"] = $typen[1];
- $terminElm["name_a"] = $namen[0];
- $terminElm["name_b"] = $namen[1];
- $terminElm["saal_a"] = $saele[0];
- $terminElm["saal_b"] = $saele[1];
- } else {
- $terminElm["typ"] = $termin->getTyp();
- $terminElm["name"] = $termin->getName();
- $terminElm["saal"] = $termin->getSaal();
- }
- $terminElm["von"] = $termin->getZeitVon();
- $terminElm["bis"] = $termin->getZeitBis();
- $kalTagElm["termine"][] = $terminElm;
- }
- $wocheElm["kalendertage"][] = $kalTagElm;
- }
- $jsonArr["stundenplan"]["wochen"][] = $wocheElm;
- }
- return json_encode($jsonArr);
- }
- function planToCSV(Stundenplan $plan) {
- // Kopf-Zeilen Schreiben
- $out = '"Beginnt am","Endet am","Beginnt um","Endet um","Betreff","Ort"';
- foreach ($plan->getWochen() as $woche) {
- foreach ($woche->getKalendertage() as $kalTag) {
- if ($kalTag->getTerminCount() < 1) {
- continue;
- }
- $aDatum = $kalTag->getTimestamp();
- $aDatum = '"' . $aDatum .'","' . $aDatum . '"';
- foreach ($kalTag->getTermine() as $termin) {
- $aVon = $termin->getZeitVon();
- $aBis = $termin->getZeitBis();
- $aLine = $aDatum;
- $aLine .= ',"' . $aVon . '","' . $aBis . '"';
- $betreff;
- $ort;
- if ($termin instanceof Doppeltermin) {
- $typen = $termin->getTypen();
- $namen = $termin->getNamen();
- $betreff = '"' . $typen[0] . ' ' . $namen[0] . ' / ';
- $betreff .= $typen[1] . ' ' . $namen[1] . '"';
- $ort = '"ASW ' . implode(" / ", $termin->getSaele()) . '"';
- } else {
- $typ = $termin->getTyp();
- $name = $termin->getName();
- $saal = $termin->getSaal();
- $betreff = '"' . $typ . ' ' . $name . '"';
- $ort = '"ASW ' . $saal . '"';
- }
- $aLine .= ',' . $betreff . ',' . $ort;
- $out .= chr(10) . $aLine;
- }
- }
- }
- return $out;
- }
- function parseAswSchedule($htmlCont) {
- // Extract single tables (=schedule weeks)
- if (!preg_match_all("#<table.*?</table>#", $htmlCont, $tables)) {
- return null;
- }
- // Extract consecutive numbers of the weeks
- $lfdNrs = array();
- if (preg_match_all("#<div class=\"w2\">(\d+)#", $htmlCont, $lfdNrs)) {
- $lfdNrs = $lfdNrs[1];
- }
- $tables = $tables[0];
- $plan = new Stundenplan($GLOBALS["klasse"], $GLOBALS["block"]);
- for ($i = 0; $i < count($tables); $i++) {
- if (isset($lfdNrs[$i])) {
- $lfdNr = $lfdNrs[$i];
- } else {
- $lfdNr = $i;
- }
- $table = $tables[$i];
- $aWoche = parseSkedTable($table, $lfdNr);
- if (is_null($aWoche)) {
- continue;
- }
- $plan->addWoche($aWoche);
- }
- return $plan;
- }
- function parseSkedTable($htmlTable, $lfdNr) {
- // Convert single quotes to double quotes
- $htmlTable = (string) str_replace("'", "\"", $htmlTable);
- // Normalize all td.class = r2, rz2, rdm2, rdl2, rdr2
- $htmlTable = (string) str_replace("class=\"rz2", "class=\"rz1", $htmlTable);
- $htmlTable = (string) str_replace("class=\"rdm2", "class=\"rdm1", $htmlTable);
- $htmlTable = (string) str_replace("class=\"rdl2", "class=\"rdl1", $htmlTable);
- $htmlTable = (string) str_replace("class=\"r2", "class=\"r1", $htmlTable);
- $htmlTable = (string) str_replace("class=\"rdr2", "class=\"rdr1", $htmlTable);
- // Split table by time-of-the-day rows (08:00, 09:00 and so on)
- $zeilen = explode("<tr><td class=\"rz1", $htmlTable);
- // Extract dates of the weekdays in the first row
- if (!preg_match_all("#<td class=\"t\".*?>.*?(\d+\.\d+\.\d+)#", $zeilen[0], $datums)) {
- return null;
- }
- $datums = $datums[1];
- // Convert dates from DD.MM.YYYY to YYYY-MM-DD
- for ($i = 0; $i < count($datums); $i++) {
- $tokens = explode(".", $datums[$i]);
- $tokens = array_reverse($tokens);
- $datums[$i] = implode("-", $tokens);
- }
- $woche = new Woche($lfdNr, $datums[0], $datums[count($datums) - 1]);
- // Split rows into "Header" and "Content" areas
- // First row will be irrelevant
- // One Header will represent one time-of-the-day row
- $headers = array();
- $contents = array();
- foreach ($zeilen as $zeile) {
- $splitted = explode("</tr>", $zeile, 2);
- $headers[] = $splitted[0];
- $contents[] = $splitted[1];
- }
- $daysWithDouble = array();
- // Check for days with "double courses", indicated in the 08:00 row
- $days = explode("class=\"r1", $headers[1]);
- for ($i = 1; $i < count($days) && $i < 7; $i++) {
- // If there are border-left/right style-tags
- // => double courses on this day
- if (strpos($days[$i], "style=\"border-right") !== FALSE) {
- $daysWithDouble[] = $datums[$i - 1];
- }
- }
- // Extract revelant data from the header:
- // $header["relDays"] : Days, at which there are courses defined in the content area
- // $header["tage"] : Assoc. Array: (date => array with "Termin" [=course])
- // $header["kein_doppel"] : Dates, at which there are no double courses
- // though indicated
- $kein_doppel = array();
- $tage = array();
- $relTage = array();
- foreach ($datums as $datum) {
- $tage[$datum] = new Kalendertag($datum);
- }
- for ($i = 1; $i < count($headers); $i++) {
- $aHour = $i + 7;
- $header = parseHeader($headers[$i], $datums, $daysWithDouble);
- foreach ($header["kein_doppel"] as $aDate) {
- if (!isset($kein_doppel[$aDate])) {
- $kein_doppel[$aDate] = array();
- }
- // Assign time of the day to the corresponding date
- $kein_doppel[$aDate][] = $aHour;
- }
- foreach ($header["tage"] as $aDate => $termine) {
- if (count($termine) > 0) {
- $tage[$aDate]->addTermine($termine);
- }
- }
- // Filter collisions of relevant days
- // rdm/rdl tags become irrelevant (and error producing)
- // if there already is a course at that exact point in time
- $relTage[$i] = array();
- for ($j = 0; $j < count($header["relDays"]); $j++) {
- $aDate = $header["relDays"][$j];
- if (!isset($tage[$aDate])) {
- continue;
- }
- $termine = $tage[$aDate]->getTermine();
- $aVon = strtotime($aHour . ":00");
- $aBis = strtotime($aHour . ":59");
- foreach ($termine as $termin) {
- $tVon = strtotime($termin->getZeitVon());
- $tBis = strtotime($termin->getZeitBis());
- // Whole hour is blocked by another course
- // => discard
- if ($aVon >= $tVon && $aBis <= $tBis) {
- unset($header["relDays"][$j]);
- }
- }
- $relTage[$i] = array_values($header["relDays"]);
- }
- }
- // Extract courses from content (if necessary)
- for ($i = 1; $i < count($contents); $i++) {
- // If courses are indicated in header: parse, else: assign null
- if (count($relTage[$i] > 0)) {
- $contents[$i] = parseContent($contents[$i]);
- if (is_null($contents[$i])) {
- $contents[$i] = null;
- }
- } else {
- $contents[$i] = null;
- }
- }
- // Assign found courses to the relevant days
- for ($i = 1; $i < count($contents); $i++) {
- if (is_null($contents[$i])) {
- continue;
- }
- $aVorl = $contents[$i];
- $aRelTage = $relTage[$i];
- $aHour = $i + 7;
- $vorlInd = 0;
- for ($d = 0; $d < count($aRelTage) && $vorlInd < count($aVorl); $d++) {
- $relTag = $aRelTage[$d];
- $termin = createNewTermin($aVorl[$vorlInd]);
- $tVon = strtotime($termin->getZeitVon());
- $tBis = strtotime($termin->getZeitBis());
- // Check if date is already blocked...
- // if yes, discard
- $aTermine = $tage[$relTag]->getTermine();
- $belegt = false;
- for ($j = 0; $j < count($aTermine); $j++) {
- $aTermin = $aTermine[$j];
- $aVon = strtotime($aTermin->getZeitVon());
- $aBis = strtotime($aTermin->getZeitBis());
- if ($tBis < $aVon) {
- continue;
- }
- if ($tVon == $aVon) {
- $belegt = true;
- break;
- } else if ($tVon < $aVon && $tBis > $aVon) {
- $belegt = true;
- break;
- } else if ($tVon > $aVon && $tVon <= $aBis) {
- $belegt = true;
- break;
- }
- }
- if($belegt) {
- continue;
- }
- // If double course was initially indicated and later not ruled out:
- // => Create "Doppeltermin" (double course) with the next two extracted courses
- $isDoppel = false;
- if (in_array($relTag, $daysWithDouble)) {
- $isDoppel = true;
- foreach ($kein_doppel[$relTag] as $irelHour) {
- $aVon = strtotime($irelHour . ":00");
- $aBis = strtotime($irelHour . ":59");
- if ($tVon > $aBis) {
- continue;
- }
- if ($tBis < $aVon) {
- break;
- }
- if ($tVon == $aVon) {
- $isDoppel = false;
- break;
- } else if ($tVon < $aVon && $tBis > $aVon) {
- $isDoppel = false;
- break;
- } else if ($tVon > $aVon && $tVon <= $aBis) {
- $isDoppel = false;
- break;
- }
- }
- }
- if ($isDoppel) {
- $termin = createNewDoppeltermin($aVorl[$vorlInd], $aVorl[$vorlInd + 1]);
- $vorlInd += 2;
- } else { // Else keep simple course
- $vorlInd++;
- }
- $tage[$relTag]->addTermin($termin);
- $pos;
- $aHourTmp;
- for ($j = $i + 1; $j < count($relTage); $j++) {
- $pos = array_search($relTag, $relTage[$j]);
- if ($pos == false) {
- continue;
- }
- $aHourTmp = $j + 7;
- $aVon = strtotime($aHourTmp . ":00");
- $aBis = strtotime($aHourTmp . ":59");
- if ($aVon >= $tVon && $aBis <= $tBis) {
- unset($relTage[$j][$pos]);
- $relTage[$j] = array_values($relTage[$j]);
- }
- }
- }
- }
- foreach ($tage as $kalTag) {
- $woche->addKalendertag($kalTag);
- }
- return $woche;
- }
- function parseHeader($header, $datums, $daysWithDouble) {
- $relClasses = array("class=\"rdm", "class=\"rdl", "class=\"rdr");
- // Split Header in single days (separated by <td class="r1")
- $days = explode("class=\"r1", $header);
- // Look for courses in single days
- // Courses exist, if:
- // 1. directly defined in <td class="v", or
- // 2. indicated by <td class="rd(l|m|r)1" (definition will then be in the content area)
- $out = array();
- $out["relDays"] = array();
- $out["tage"] = array();
- $out["kein_doppel"] = array();
- foreach ($datums as $datum) {
- $out["tage"][$datum] = array();
- }
- // Relevant Indices are 1 to 6, $j stands for the weekday
- for ($j = 1; $j < count($days) && $j < 7; $j++) {
- $aDate = $datums[$j - 1];
- // If there is a border-left defined and no border-right (or the other way around): No double course
- if (in_array($aDate, $daysWithDouble)) {
- if (strpos($days[$j], "style=\"border-left") != FALSE
- xor strpos($days[$j], "style=\"border-right") != FALSE) {
- $aDate = $datums[$j - 1];
- $out["kein_doppel"][] = $aDate;
- }
- }
- // If directly defined, simple parse
- if (strpos($days[$j], "class=\"v") !== FALSE) {
- $termin;
- // Extract course data
- $pattern = "#class=\"v.*?>(.*?)</td>#";
- if (preg_match_all($pattern, $days[$j], $vorl)) {
- $vorl = $vorl[1];
- $aDate = $datums[$j - 1];
- // If multiple matches -> double course
- if (count($vorl) > 1) {
- $termin = createNewDoppeltermin($vorl[0], $vorl[1]);
- $out["tage"][$aDate][] = $termin;
- } else { // simple course
- $termin = createNewTermin($vorl[0]);
- $out["tage"][$aDate][] = $termin;
- }
- }
- continue;
- }
- foreach ($relClasses as $relClass) {
- // If class="rd(l|m|r)1" contained, save day index
- if (strpos($days[$j], $relClass) !== FALSE) {
- $aDate = $datums[$j - 1];
- $out["relDays"][] = $aDate;
- break;
- }
- }
- }
- return $out;
- }
- function parseContent($content) {
- // Extract body of all <td class="v"> in content
- // This body represents the courses
- $pattern = "#class=\"v.*?>(.*?)</td>#";
- if (preg_match_all($pattern, $content, $vorl) > 0) {
- return $vorl[1];
- } else {
- return null;
- }
- }
- function createNewDoppeltermin($vorl_1_cont, $vorl_2_cont) {
- $vorl_1 = parseVorlesung($vorl_1_cont);
- $vorl_2 = parseVorlesung($vorl_2_cont);
- $zeit = $vorl_1["zeit"];
- $typen = array($vorl_1["typ"], $vorl_2["typ"]);
- $namen = array($vorl_1["name"], $vorl_2["name"]);
- $saele = array($vorl_1["saal"], $vorl_2["saal"]);
- $termin = new Doppeltermin($zeit[0], $zeit[1], $typen, $namen, $saele);
- return $termin;
- }
- function createNewTermin($vorl_cont) {
- $vorl = parseVorlesung($vorl_cont);
- $termin = new Termin($vorl["zeit"][0], $vorl["zeit"][1],
- $vorl["typ"], $vorl["name"], $vorl["saal"]);
- return $termin;
- }
- function parseVorlesung($vorl) { // returns String-Array
- // Split by <br> tags
- $parts = preg_split("#<br\s*(/\s*|)>#", $vorl);
- // Parse time of the day entries
- $out["zeit"] = (string) str_replace("Uhr", "", $parts[0]);
- $out["zeit"] = explode(" - ", $out["zeit"]);
- $out["zeit"][0] = trim($out["zeit"][0]);
- $out["zeit"][1] = trim($out["zeit"][1]);
- // Decode all left over html-entities
- $out["typ"] = trim(html_entity_decode($parts[1], ENT_COMPAT | ENT_HTML401, "UTF-8"));
- $out["name"] = trim(html_entity_decode($parts[2], ENT_COMPAT | ENT_HTML401, "UTF-8"));
- if (isset($parts[3])) {
- // Remove weird Anchor-Tag
- $out["saal"] = strip_tags($parts[3]);
- $out["saal"] = trim(html_entity_decode($out["saal"], ENT_COMPAT | ENT_HTML401, "UTF-8"));
- } else {
- $out["saal"] = "";
- }
- return $out;
- }
- function compareTermin($terminA, $terminB) {
- $tsA = strtotime($terminA->getzeitVon());
- $tsB = strtotime($terminB->getzeitVon());
- if ($tsA < $tsB) {
- return -1;
- } else if ($tsA == $tsB) {
- return 0;
- } else {
- return 1;
- }
- }
- function printDoc_XML($xmlCont) {
- header("Content-Type: text/xml");
- echo trim($xmlCont);
- }
- function printDoc_JSON($jsonCont) {
- header("Content-Type: application/json");
- echo $jsonCont;
- }
- function printDoc_CSV($csvCont, $filename) {
- header("Content-Type: text/csv");
- header("Content-Disposition: attachment;filename=" . $filename);
- echo $csvCont;
- }
- function printError_XML($tagName, $message) {
- header("HTTP/1.0 400 Bad Request");
- header("Content-Type: text/xml");
- $doc = new DOMDocument();
- $doc->formatOutput = true;
- $errTag = $doc->createElement($tagName);
- $errMsg = $doc->createTextNode($message);
- $errTag->appendChild($errMsg);
- $doc->appendChild($errTag);
- echo $doc->saveXML();
- }
- function printError_JSON($tagName, $message) {
- header("HTTP/1.0 400 Bad Request");
- header("Content-Type: application/json");
- $err = array();
- $err[$tagName] = $message;
- echo json_encode($err);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement