function not_teletop_credentials() {
if (!$_SESSION['teletop_username'] || !$_SESSION['teletop_password'] ||
$_SESSION['teletop_username'] == '' || $_SESSION['teletop_password'] == '') return true;
return false;
}
function curl_teletop_init() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//curl_setopt($ch, CURLINFO_REFERER, NULL);
if (isset($_SESSION['teletop_session'])) curl_setopt($ch, CURLOPT_COOKIE, 'DomAuthSessId='.$_SESSION['teletop_session']);
return $ch;
}
function find_session_cookie($ch, $line) {
/* check if we got the session cookie */
$cookie_id = 'Set-Cookie: DomAuthSessId=';
if (strpos($line, $cookie_id) === FALSE) return strlen($line);
$_SESSION['teletop_session'] =
substr($line, strlen($cookie_id), strpos($line, ';') - strlen($cookie_id));
return strlen($line);
}
function null_callback($ch, $line) {
return strlen($line);
}
// de url dient een loginform in text/html te geven indien de gebruiker niet ingelogd is
// $url is het volledige pad zonder de server MET de root slash
//
// we retourneren een xpath object geladen met de gevraagde pagina of geparste json data
function curl_teletop_req($ch, $url, $postdata = NULL, $additional_headers = array(), $recurse = 0) {
global $http_path, $teletop_server;
if ($recurse > 1) regular_error($http_path.'/', (array) NULL,
'curl_teletop_req() heeft zichzelf te vaak aangeroepen, kennelijk is er een BUG');
curl_setopt($ch, CURLOPT_URL, $teletop_server.$url);
if ($postdata) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_POST, true);
} else curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers);
$ret = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADER, (array) NULL);
if (curl_errno($ch)) regular_error($http_path.'/', (array) NULL, 'Fout bij het laden van '.
curl_getinfo($ch, CURLINFO_EFFECTIVE_URL).': '.curl_error($ch));
// indien we niet ingelogd zijn, of onvoldoende rechten hebben, hebben we een HTML document gehad met
// een login formulier
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if (!strncasecmp($content_type, 'text/html', 9)) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($ret);
$xpath = new DOMXPath($doc);
$form = $xpath->query('//form')->item(0);
if (!$form || $form->getAttribute('action') != '/names.nsf?Login') return $xpath;
$errordiv = $xpath->query('//div[@class="errorLogin"]')->item(0);
if ($errordiv) {
$error_txt = htmlspecialchars($errordiv->nodeValue, ENT_QUOTES, 'UTF-8');
if (!preg_match('/sessie/', $error_txt) || !preg_match('/is verlopen/', $error_txt))
regular_error($http_path.'/', (array) NULL, 'Error accessing '.curl_getinfo($ch, CURLINFO_EFFECTIVE_URL).': '. $error_txt);
}
if (not_teletop_credentials())
regular_error($http_path.'/', (array) NULL, 'Geen TeleTOP® gebruikersnaam en/of '.
'wachtwoord beschikbaar, vul je informatie in bij TeleTOP® login informatie');
dom_form_set_input($form, $xpath, 'Username', $_SESSION['teletop_username'].'/Abvo');
dom_form_set_input($form, $xpath, 'UsernameInput', $_SESSION['teletop_username'].'/Abvo');
dom_form_set_input($form, $xpath, 'Password', $_SESSION['teletop_password']);
if (strtoupper($form->getAttribute('method')) != 'POST')
regular_error($http_path.'/', (array) NULL, 'Structuur van het TeleTOP® login formulier is gewijzigd.');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $teletop_server.$form->getAttribute('action'));
curl_setopt($ch, CURLOPT_POSTFIELDS, dom_form($form, $xpath));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, find_session_cookie);
unset($_SESSION['teletop_session']);
curl_exec($ch);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, null_callback);
//curl_setopt($ch, CURLOPT_HEADERFUNCTION, NULL);
if (curl_errno($ch)) regular_error($http_path.'/', (array) NULL,
'Fout bij het inloggen op TeleTOP®: '.curl_error($ch));
if (!$_SESSION['teletop_session']) regular_error($http_path.'/', (array) NULL,
'Inloggen bij TeleTOP® mislukt; gebruikersnaam en wachtwoord worden niet geaccepteerd, ga naar de TeleTOP® login informatiepagina om je gegevens goed in te stellen');
curl_setopt($ch, CURLOPT_COOKIE, 'DomAuthSessId='.$_SESSION['teletop_session']);
// we zijn ingelogd, probeer opnieuw
return curl_teletop_req($ch, $url, $postdata, $additional_headers, $recurse + 1);
} else if (!strncasecmp($content_type, 'application/x-json', 18)) {
// wegens een bug in de JSON encoder van TeleTOP moeten we hier de TAB alsnog escapen
$clean = str_replace("\t", "\\t", $ret);
$ret2 = json_decode($clean, true);
if (!$ret2 && $_SESSION['ppl_id'] == 3490) {
echo $clean;
$err = json_last_error();
echo JSON_ERROR_CTRL_CHAR.' JSON_ERROR_CTRL_CHAR
';
echo JSON_ERROR_SYNTAX.' JSON_ERROR_SYNTAX
';
echo "error decoding JSON data from TeleTOP® ".json_last_error().'
';
$fp = fopen('/tmp/test.bla', 'w');
fwrite($fp, $ret);
fclose($fp);
}
return $ret2;
}
return NULL;
}
function delete_from_teletop($notitie_id) {
global $schooljaar_long, $http_path, $teletop_vaksite_prefix, $teletop_server;
$result = mysql_query_safe("SELECT vaksite, vaksite_id, doc_id FROM notities2teletop JOIN vaksites USING (vaksite_id) WHERE notitie_id = '%s'", mysql_escape_safe($notitie_id));
if (!mysql_numrows($result)) return; // notitiestaat niet meer in TeleTOP
$vaksite = $teletop_vaksite_prefix.mysql_result($result, 0, 'vaksite').'.nsf';
$doc_id = mysql_result($result, 0, 'doc_id');
$ch = curl_teletop_init();
curl_teletop_req($ch, $vaksite.'/a-DeleteDocuments?OpenAgent&id='.$doc_id.'&docType=rosterrow');
// assume success
mysql_query_safe("DELETE FROM notities2teletop WHERE notitie_id = '%s'", mysql_escape_safe($notitie_id));
}
function teletop_check_conflict($ch, $datum, $klas) {
$json_data = curl_teletop_req($ch, '/tt/abvo/updates.nsf/a-SearchRosterRows?OpenAgent&rand='.rand(0,99999998),
'sa=1&dformat=mm/dd/yyyy&date='.$datum.'&ug='.$klas.'&',
array('X-Requested-With: XMLHttpRequest'));
if (!$json_data) $_SESSION['warningmsg'] = 'De server van TeleTOP® heeft ongeldige JSON data naar ons teruggestuurd, we hebben helaas niet kunnen constateren of er toetsconflicten waren.';
else {
$bla = '';
foreach ($json_data['result']['rows'] as $value) $bla = $value['cn'].' '.$value['sa'].'
';
if ($bla != '') {
$_SESSION['warningmsg'] = 'Voor deze klas zijn de volgende belastende activiteiten gepland. De nieuwe notitie is in TeleTOP® geplaatst, maar moet wellicht nog worden verplaatst.