define('LEERLING', 1);
define('DOCENT', 2);
define('LOKAAL', 3);
define('LESGROEP', 4);
function fatal_error($msg) {
echo('fatal: '.$msg."\n");
exit;
}
function curl_safe_setopt($ch, $option, $value) {
if (!curl_setopt($ch, $option, $value)) fatal_error('curl_setopt(): '.curl_error($ch));
}
function curl_rooster_init() {
global $useragent;
if (!($ch = curl_init())) fatal_error('curl_init() failed');
curl_safe_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_safe_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_safe_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_safe_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_safe_setopt($ch, CURLOPT_FAILONERROR, true);
curl_safe_setopt($ch, CURLOPT_HTTPHEADER, array('Host: rooster.ovc.nl'));
libxml_use_internal_errors(true);
return $ch;
}
function curl_rooster_req($ch, $url) {
curl_safe_setopt($ch, CURLOPT_HTTPGET, true);
curl_safe_setopt($ch, CURLOPT_URL, $url);
curl_safe_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
$ret = curl_exec($ch);
if (!curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
//echo('retrying becase '.curl_error($ch));
$ret = curl_exec($ch);
}
if (!curl_exec($ch)) fatal_error('curl_exec(): '.curl_error($ch));
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$doc = new DOMDocument();
if (!strncasecmp($content_type, 'text/html', 9)) {
$doc->loadHTML($ret);
} else if (!strncasecmp($content_type, 'text/xml', 8) || !strncasecmp($content_type, 'application/xml', 15)) {
$doc->loadXML($ret);
} else if (!strncasecmp($content_type, 'text/plain', 10)) {
return $ret; // return text data literally
} else fatal_error('unable to create xpath from '.$content_type);
return new DOMXPath($doc);
}
function xpath_query_error($xpath, $query, $contextnode = NULL) {
if (!($res = $xpath->query($query, $contextnode))) fatal_error($query.' malformed');
return $res;
}
function xpath_query_expect($xpath, $query, $contextnode = NULL) {
$res = xpath_query_error($xpath, $query, $contextnode);
if (!$res->length) fatal_error($query.' returned no results');
return $res;
}
require_once('MDB2.php');
function mdb2_open($dsn) {
$mdb2 =& MDB2::connect($dsn);
if (PEAR::isError($mdb2)) fatal_error($mdb2->getMessage());
return $mdb2;
}
function mdb2_res_table($res) {
$no_columns = $res->numCols();
$columns = $res->getColumnNames();
//print_r($columns);
if ($no_columns == 0) return;
?>
foreach($columns as $key => $value) { ?> echo($key) ?> | } ?>
while ($row = $res->fetchRow(MDB2_FETCHMODE_ORDERED)) { ?>
foreach($row as $data) { ?> if ($data === NULL) echo('NULL'); else echo($data); ?> | } ?>
} ?>
$res->seek(); // reset row pointer
}
// format characters
// %% -> %
// %q -> string to be escaped, must already be insides 's in $format
// %w -> string to be escaped, must already be insides 's in $format, also escapes wildcard characters
// %i -> positive integer
function mdb2_vprintf($mdb2, $format, $args) {
$out = '';
$arg = 0;
while (($pos = strpos($format, '%')) !== FALSE) {
$out .= substr($format, 0, $pos);
switch ($format[$pos + 1]) {
case '%':
$out .= '%';
break;
case 'q':
if (count($args) <= $arg) fatal_error('te weinig argumenten');
$out .= $mdb2->escape($args[$arg]);
$arg++;
break;
case 'w':
if (count($args) <= $arg) fatal_error('te weinig argumenten');
$out .= $mdb2->escape($args[$arg], true);
$arg++;
break;
case 'i':
if (count($args) <= $arg) fatal_error('te weinig argumenten');
$val = (int)$args[$arg];
if ($val == NULL) {
$out .= 'NULL';
} else {
if ($val != $args[$arg]) fatal_error('SQL argument is geen integer');
if ($val <= 0) fatal_error('SQL argument is geen positieve integer: '.$val);
$out .= $val;
}
$arg++;
break;
default:
fatal_error('onzinnig format character');
break;
}
$format = substr($format, $pos + 2);
}
if ($arg != count($args)) fatal_error('te veel argumenten voor format string');
//echo($out.$format."\n");
return $out.$format;
}
function mdb2_vquery($mdb2, $format, $args) {
$res =& $mdb2->query(mdb2_vprintf($mdb2, $format, $args));
if (PEAR::isError($res)) {
$errorInfo = $mdb2->errorInfo($res);
fatal_error($res->getMessage().': '.$errorInfo[2]);
}
return $res;
}
function mdb2_query($mdb2, $format) {
$args = func_get_args();
array_shift($args); array_shift($args);
return mdb2_vquery($mdb2, $format, $args);
}
function mdb2_vexec_error($mdb2, $err, $format, $args) {
$affected =& $mdb2->exec(mdb2_vprintf($mdb2, $format, $args));
if (PEAR::isError($affected)) {
if (PEAR::isError($affected, $err)) return false;
else {
$errorInfo = $mdb2->errorInfo($affected);
fatal_error($affected->getMessage().': '.$errorInfo[2]);
}
}
return true;
}
function mdb2_exec_error($mdb2, $err, $format) {
$args = func_get_args();
array_shift($args); array_shift($args); array_shift($args);
return mdb2_vexec_error($mdb2, $err, $format, $args);
}
function mdb2_vexec($mdb2, $format, $args) {
$affected =& $mdb2->query(mdb2_vprintf($mdb2, $format, $args));
if (PEAR::isError($affected)) {
$errorInfo = $mdb2->errorInfo($affected);
fatal_error($affected->getMessage().': '.$errorInfo[2]);
}
}
function mdb2_exec($mdb2, $format) {
$args = func_get_args();
array_shift($args); array_shift($args);
mdb2_vexec($mdb2, $format, $args);
}
function mdb2_vsingle_row($mdb2, $mode, $format, $args) {
$res = mdb2_vquery($mdb2, $format, $args);
$array = $res->fetchRow($mode);
$res->free();
return $array;
}
function mdb2_single_row($mdb2, $mode, $format) {
$args = func_get_args();
array_shift($args); array_shift($args); array_shift($args);
return mdb2_vsingle_row($mdb2, $mode, $format, $args);
}
function mdb2_single_array($mdb2, $format) {
$args = func_get_args();
array_shift($args); array_shift($args); array_shift($args);
return mdb2_vsingle_row($mdb2, MDB2_FETCHMODE_ORDERED, $format, $args);
}
function mdb2_single_assoc($mdb2, $format) {
$args = func_get_args();
array_shift($args); array_shift($args); array_shift($args);
return mdb2_vsingle_row($mdb2, MDB2_FETCHMODE_ASSOC, $format, $args);
}
function mdb2_single_val($mdb2, $format) {
$args = func_get_args();
array_shift($args); array_shift($args);
$res = mdb2_vquery($mdb2, $format, $args);
$array = $res->fetchRow();
$res->free();
if (isset($array[0])) return $array[0];
else return NULL;
}
?>