Skip to content

Instantly share code, notes, and snippets.

@0x46616c6b
Forked from nyom/gist:850074
Created March 3, 2011 16:15
Show Gist options
  • Save 0x46616c6b/853025 to your computer and use it in GitHub Desktop.
Save 0x46616c6b/853025 to your computer and use it in GitHub Desktop.
<?php
ini_set('default_charset', 'UTF-8');
$debug = 1;
$debug_cookie = 0;
$mode = NULL;
$matrikel = NULL;
$password = NULL;
if ($debug) {
if ( isset($_GET['matrikel']) && isset($_GET['password']) ) {
// curl http://localhost/test.php?matrikel=<matrikel>&password=<password>
$matrikel = $_GET['matrikel'];
$password = $_GET['password'];
$mode = 'http';
} else if ( isset($_SERVER['argc']) && $_SERVER['argc']>=1 ) {
// php -f file.php <matrikel> <password>
$matrikel = $_SERVER['argv']['1'];
$password = $_SERVER['argv']['2'];
$mode = 'shell';
}
}
function fetchsite($url = NULL, $matrikel = NULL, $password = NULL) {
global $debug, $debug_cookie, $mode;
$login_url = "http://www.ba-dresden.de/index2.php?menu=5";
$cookie = tempnam(sys_get_temp_dir(), 'cookie');
if ( $url && $matrikel && $password ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "matrikelnr=" . $matrikel . "&passwort=" . $password);
ob_start(); // prevent any output
curl_exec($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close($ch);
unset($ch);
if ($debug && $debug_cookie) {
if (file_exists($cookie)) {
$f = fopen($cookie, "r");
if ($mode == 'http') echo "<pre>+ + + C O O K I E + + +";
if ($mode == 'shell') echo "+ + + C O O K I E + + +\n";
while (!feof($f)) {
$buffer = fgets($f, 4096);
if ( strpos($buffer, "#") === false ) echo $buffer;
}
fclose($f);
if ($mode == 'http') echo "+ + + + + + + + + + + +</pre>";
if ($mode == 'shell') echo "+ + + + + + + + + + + +";
} else {
if ($mode == 'http') echo "<pre>+ + + N O C O O K I E + + +</pre>";
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
unset($ch);
unlink($cookie);
return html_entity_decode(htmlentities($result));
} else {
if ($debug) echo "Worst!";
}
}
function render2json($type = NULL) {
global $matrikel, $password;
if ( $type == "news" ) {
$url = "http://www.ba-dresden.de/index2.php?menu=5&item=20";
$html = fetchsite($url, $matrikel, $password);
$result = array();
$pattern = "/<td width=\"60\" class=\"line.*.\">(.+)<td width=\"15\"class=\"line.*.\">/siU";
$ret = preg_match_all($pattern, $html, $events);
foreach($events[1] as $event) {
$html = $event;
$ret = preg_match_all("/(.+)<\/td>.*" .
"<td width=\"90\" class=\"line.*.\">(.+)<\/td>.*" .
"<td class=\"line.*.\">(.+)<\/td>" .// auslesen in data1-3 datum,person,inhalt
"/siU", $html, $data, PREG_SET_ORDER);
$tmp = array(
"date" => $data[0][1],
"author" => $data[0][2],
"message" => $data[0][3]
);
array_push($result, $tmp, $_ = null);
}
return json_encode($result);
} else if ( $type == "marks" ) {
$url = "http://www.ba-dresden.de/index2.php?menu=5&item=23";
$html = fetchsite($url, $matrikel, $password);
$pattern = "/<div id=\"std_inh\">(.+)<tr>.*.<td class=\"textkleinschwarz\">/siU";
$ret = preg_match_all($pattern, $html, $events);
$result = array(
"table" => $events[0]
);
return json_encode($result);
} else if ( $type =="documents1" ) {
$url = "http://www.ba-dresden.de/index2.php?menu=5&item=40&sub=31";
$html = fetchsite($url, $matrikel, $password);
$pattern = "/<td class=\"doc_liste\" >&nbsp;(.+)<\/td>/siU";
$ret = preg_match_all($pattern, $html, $events);
$result = array();
foreach($events[1] as $event) {
$html = mb_convert_encoding($event, 'utf-8', 'iso-8859-1');
$ret = preg_match_all("/<a href=\"(.+)\" class=\"text\" target=\"about_blank\">(.+)<\/a>/siU", $html, $data, PREG_SET_ORDER);
$tmp = array(
"link" => "http://www.ba-dresden.de/" . $data[0][1],
"name" => $data[0][2]
);
array_push($result, $tmp, $_ = null);
}
return json_encode($result);
} else if ( $type =="documents2" ) {
$url = "http://www.ba-dresden.de/index2.php?menu=5&item=40";
$html = fetchsite($url, $matrikel, $password);
$pattern = "/<td class=\"doc_liste\" >&nbsp;(.+)<\/td>/siU";
$ret = preg_match_all($pattern, $html, $events);
$semester = 0;
$result = array();
foreach($events[1] as $event) {
$html = mb_convert_encoding($event, 'utf-8', 'iso-8859-1');
$ret = preg_match_all("/<a href=\"(.+)\" class=\"text\" target=\"about_blank\">(.+)<\/a>/siU", $html, $data, PREG_SET_ORDER);
if (substr($data[0][2], 0, 2) == "1 ")
{
$semester = $semester +1;
}
$tmp = array(
"link" => "http://www.ba-dresden.de/" . $data[0][1],
"name" => $data[0][2],
"semester" => $semester . " Semester"
);
array_push($result, $tmp, $_ = null);
}
return json_encode($result);
} else {
die();
}
}
//echo render2json("documents2");
echo render2json("news");
//echo render2json("marks");
if ($debug) {
if ($mode == "shell") echo "\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment