-kw

HS Merseburg E-Mail Benachrichtigung bei neuen Noten v2.5

-kw
Oct 12th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.74 KB | None | 0 0
  1. <?php
  2.  
  3. //about
  4. /*
  5. by -kw, 12.10.2016
  6. v2.5
  7. Dieses Script sollte etwa einmal die Stunde per Cron aufgerufen werden.
  8. Es sucht im HIS nach neuen Noten und sendet euch dann ggf. eine E-Mail.
  9.  
  10. Das Script muss im Verzeichnis eine Datei anlegen können.
  11. Das Script wird nur im HIS der HS Merseburg funktionieren.
  12.  
  13. Keine Haftung, Garantie, Gewährleistung und sonstiges. Benutzung auf eigene Gefahr.
  14. Sensible Daten sollten nicht auf Servern gespeichert werden!
  15.  
  16. Changelog
  17. ---------
  18. v2:
  19. + Changed: suche nichtmehr alle 0,0, sondern prüfe jede Note einzeln
  20. + E-Mail mit Fach und Note
  21. + erkennt nun auch nicht bestandene Prüfungen
  22. + schickt eine Mail mit der Notenübersicht raus.
  23.  
  24.  */
  25.  
  26. //config
  27. $mailto = "mail@example.com"; // E-Mail Adresse des zu Benachrichtigenden
  28. $mailto_uebersicht = "mail@example.com"; // E-Mail Adresse des zu Benachrichtigenden (Übersicht)
  29. $absender_mail = "mail@example.com"; // E-Mail des Absenders
  30. $absender = "Beispiel Name"; // Absendername
  31. $reply = "mail@example.com"; // Antwort an
  32.  
  33. $username = ""; //Rechenzentrum Nutzername
  34. $passwd = ""; //Rechenzentrum Passwort
  35. $matrikelnr = ""; //Matrikelnummer
  36.  
  37. //do not change from here
  38. date_default_timezone_set("Europe/Berlin");
  39. $savefile = "counter.txt";
  40. $cookies = "cookies.txt";
  41. if (!file_exists($cookies)) {
  42.     file_put_contents($cookies, "");
  43. }
  44.  
  45. $noten = array();
  46. $noten_old = array();
  47. $changed_marks = array();
  48. $old_counter = array();
  49. $new_marks = array();
  50. $mailtext = "Aktualisierte Notenübersicht:\n-----------------------------\n\n";
  51. $tabelle = "";
  52.  
  53. function execute($ho, $state, $array, $newsess) {
  54.     global $cookies;
  55.     $log_hoster = $array;
  56.     $ch = curl_init();
  57.  
  58.     $url = $log_hoster[$ho][0];
  59.     $postdata = $log_hoster[$ho][1];
  60.     $ref = $log_hoster[$ho][2];
  61.  
  62.     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  63.     curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  64.     curl_setopt($ch, CURLOPT_COOKIE, $cookies);
  65.     curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
  66.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
  67.     curl_setopt($ch, CURLOPT_COOKIESESSION, $newsess);
  68.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  69.     curl_setopt($ch, CURLOPT_HEADER, false);
  70.     curl_setopt($ch, CURLOPT_POST, true);
  71.     curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  72.     curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
  73.     curl_setopt($ch, CURLOPT_REFERER, $ref);
  74.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  75.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  76.     curl_setopt($ch, CURLOPT_URL, $url);
  77.     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  78.     $retu = curl_exec($ch);
  79.     sleep(1);
  80.     return $retu;
  81. }
  82.  
  83. function getsite($zielurl, $postdata = "", $ref = "", $newsess = true) {
  84.     $log_hoster = array(
  85.         array($zielurl, $postdata, $ref),
  86.     );
  87.     $temp = execute(0, true, $log_hoster, $newsess);
  88.     return $temp;
  89. }
  90.  
  91. //einlogen
  92. $URL = "https://hisqis.hs-merseburg.de/qisserver/rds?state=user&type=1&category=auth.login&startpage=portal.vm";
  93. $seite = getsite($URL, "username=" . urlencode($username) . "&password=" . urlencode($passwd) . "&mtknr=" . urlencode($matrikelnr) . "&submit=%C2%A0Anmelden%C2%A0");
  94.  
  95. //asi (SessionID) finden
  96. $URL = "https://hisqis.hs-merseburg.de/qisserver/rds?state=change&type=1&moduleParameter=studyPOSMenu&nextdir=change&next=menu.vm&subdir=applications&xml=menu&purge=y&navigationPosition=functions%2CstudyPOSMenu&breadcrumb=studyPOSMenu&topitem=functions&subitem=studyPOSMenu";
  97. $seite = explode("\n", getsite($URL, "", "https://hisqis.hs-merseburg.de/qisserver/", false));
  98. foreach ($seite as $key => $value) {
  99.     if (str_replace("<a href=\"https://hisqis.hs-merseburg.de/qisserver/rds?state=notenspiegel", "", $value) != $value) {
  100.         $start = strpos($value, "asi=");
  101.         $cut = substr($value, $start + 4);
  102.         $ende = strpos($cut, '"');
  103.         $asi = substr($cut, 0, $ende);
  104.     }
  105. }
  106.  
  107. if (!isset($asi)) {
  108.     exit();
  109. }
  110.  
  111. //Noten abrufen
  112. $URL = "https://hisqis.hs-merseburg.de/qisserver/rds?state=notenspiegelStudent&next=list.vm&nextdir=qispos/notenspiegel/student&createInfos=Y&struct=auswahlBaum&nodeID=auswahlBaum%7Cabschluss%3Aabschl%3D84%2Cstgnr%3D1&expand=0&asi=" . $asi;
  113. $seite = explode("\n", getsite($URL, "", "https://hisqis.hs-merseburg.de/qisserver/", false));
  114.  
  115. //Noten zählen
  116. $counter = 0;
  117. for ($i = 0; $i < count($seite); $i++) {
  118.     if (str_replace("bestanden", "", $seite[$i]) != $seite[$i] && str_replace("/QIS/images//his_info3.gif", "", $seite[$i - 3]) != $seite[$i - 3]) {
  119.         $counter++;
  120.         $index = trim($seite[$i - 11]);
  121.         while (isset($noten[$index])) {
  122.             $index .= "_";
  123.         }
  124.         $noten[$index] = trim($seite[$i - 5]);
  125.     }
  126. }
  127.  
  128. //alten Wert lesen/erzeugen
  129. if (file_exists($savefile)) {
  130.     $old_counter = file_get_contents($savefile);
  131.     $old_counter = explode("\n", $old_counter);
  132.     if (count($old_counter) > 0) {
  133.         foreach ($old_counter as $key => $value) {
  134.             $tmparr = explode(" - ", $value);
  135.             if (count($tmparr) > 1) {
  136.                 $noten_old[$tmparr[0]] = $tmparr[1];
  137.             }
  138.         }
  139.     }
  140. } else {
  141.     file_put_contents($savefile, "");
  142. }
  143.  
  144. function comparearrays() {
  145.     global $noten, $noten_old, $savefile, $tabelle, $changed_marks, $new_marks;
  146.     $retu = false;
  147.     if (count($noten) > 0) {
  148.         foreach ($noten as $key => $value) {
  149.             if (isset($noten_old[$key])) {
  150.                 if ($noten_old[$key] != $value) {
  151.                     //Note geändert
  152.                     $changed_marks[$key] = $value;
  153.                     $retu = true;
  154.                 }
  155.             } else {
  156.                 //neue Note
  157.                 $new_marks[$key] = $value;
  158.                 $retu = true;
  159.             }
  160.         }
  161.     }
  162.     if ($retu) {
  163.         if (count($changed_marks) > 0) {
  164.             foreach ($changed_marks as $key => $value) {
  165.                 $tabelle .= "Geändert: " . $key . " - " . $value . "\n";
  166.             }
  167.         }
  168.         if (count($new_marks) > 0) {
  169.             foreach ($new_marks as $key => $value) {
  170.                 $tabelle .= "Neu: " . $key . " - " . $value . "\n";
  171.             }
  172.         }
  173.         if (count($noten) > 0) {
  174.             file_put_contents($savefile, "");
  175.             foreach ($noten as $key => $value) {
  176.                 file_put_contents($savefile, $key . " - " . $value . "\n", FILE_APPEND);
  177.             }
  178.         }
  179.     }
  180.     return $retu;
  181. }
  182.  
  183. //Vergleich, ggf. Mail
  184. if (comparearrays()) {
  185.     mail($mailto, "Neue Note(n) online!", str_replace("\n", "<br />", htmlentities($mailtext)) . $tabelle, "Content-Type: text/html charset=\"iso-8859-1\"\r\n");
  186.     if (isset($mailto_uebersicht) && trim($mailto_uebersicht) != "") {
  187.         $message = "Hallo,\n\n im Anhang die aktualisierte Notenübersicht:\n\n";
  188.         $message .= $tabelle . "\n\n\n";
  189.         $message .= "Viele Grüße\n\nPS: Diese Mail wurde automatisch verschickt.";
  190.         $message = htmlentities($message);
  191.         $message = str_replace("\n", "<br />", $message);
  192.         // Download der Übersicht
  193.         $datei = "/tmp/Notenuebersicht_Studium_" . date("Y_m_d_H_i", time()) . ".pdf";
  194.         $ch = curl_init("https://hisqis.hs-merseburg.de/qisserver/rds?state=hisreports&status=receive&publishid=0&vmfile=no&moduleCall=abschluss&lastState=notenspiegelStudent&finishedObj=Y&xslobject=de&asi=" . $asi);
  195.         $zieldatei = fopen($datei, "w");
  196.         curl_setopt($ch, CURLOPT_FILE, $zieldatei);
  197.         curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
  198.         curl_setopt($ch, CURLOPT_COOKIE, $cookies);
  199.         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
  200.         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
  201.         curl_setopt($ch, CURLOPT_COOKIESESSION, false);
  202.         curl_exec($ch);
  203.         fclose($zieldatei);
  204.  
  205.         //mail versenden
  206.         $name = basename($datei);
  207.         $size = filesize($datei);
  208.         $data = file_get_contents($datei);
  209.         $type = mime_content_type($datei);
  210.  
  211.         $attachments[] = array("name" => $name, "size" => $size, "type" => $type, "data" => $data);
  212.  
  213.         $mime_boundary = "-----=" . md5(uniqid(mt_rand(), 1));
  214.  
  215.         $header = "From:" . $absender . "<" . $absender_mail . ">\n";
  216.         $header .= "Reply-To: " . $reply . "\n";
  217.  
  218.         $header .= "MIME-Version: 1.0\r\n";
  219.         $header .= "Content-Type: multipart/mixed;\r\n";
  220.         $header .= " boundary=\"" . $mime_boundary . "\"\r\n";
  221.  
  222.         $content = "This is a multi-part message in MIME format.\r\n\r\n";
  223.         $content .= "--" . $mime_boundary . "\r\n";
  224.         $content .= "Content-Type: text/html charset=\"iso-8859-1\"\r\n";
  225.         $content .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
  226.         $content .= $message . "\r\n";
  227.  
  228.         //$anhang ist ein Mehrdimensionals Array
  229.         //$anhang enthält mehrere Dateien
  230.         if (is_array($attachments) && is_array(current($attachments))) {
  231.             foreach ($attachments as $dat) {
  232.                 $data = chunk_split(base64_encode($dat['data']));
  233.                 $content .= "--" . $mime_boundary . "\r\n";
  234.                 $content .= "Content-Disposition: attachment;\r\n";
  235.                 $content .= "\tfilename=\"" . $dat['name'] . "\";\r\n";
  236.                 $content .= "Content-Length: ." . $dat['size'] . ";\r\n";
  237.                 $content .= "Content-Type: " . $dat['type'] . "; name=\"" . $dat['name'] . "\"\r\n";
  238.                 $content .= "Content-Transfer-Encoding: base64\r\n\r\n";
  239.                 $content .= $data . "\r\n";
  240.             }
  241.             $content .= "--" . $mime_boundary . "--";
  242.         }
  243.  
  244.         mail($mailto_uebersicht, "Aktualisierter Notenspiegel", $content, $header);
  245.     }
  246. }
Add Comment
Please, Sign In to add comment