diff --git a/student_auto_feed/config.php b/student_auto_feed/config.php index 2aa4e60..c6d4164 100644 --- a/student_auto_feed/config.php +++ b/student_auto_feed/config.php @@ -129,10 +129,23 @@ //Allows "\r" EOL encoding. This is rare but exists (e.g. Excel for Macintosh). ini_set('auto_detect_line_endings', true); +/* DATA SOURCING -------------------------------------------------------------- + * The Student Autofeed provides helper scripts to retrieve the CSV file for + * processing. Shell script ssaf.sh is used to invoke one of the helper + * scripts and then execute the autofeed. Current options are csv_local.php, + * imap_remote.php, and json_remote.php + * ------------------------------------------------------------------------- */ + +//Local CSV +//This is used by csv_local.php to reference where the CSV file is provided. +define('LOCAL_SOURCE_CSV', '/path/to/csv'); + //Remote IMAP //This is used by imap_remote.php to login and retrieve a student enrollment //datasheet, should datasheets be provided via an IMAP email box. This also -//works with exchange servers with IMAP enabled. +//works with exchange servers (local network and cloud) with IMAP and basic +//authentication enabled. +//Note that this does NOT work should exchange require OAuth2. //IMAP_FOLDER is the folder where the data sheets can be found. //IMAP_OPTIONS: q.v. "Optional flags for names" at https://www.php.net/manual/en/function.imap-open.php //IMAP_FROM is for validation. Make sure it matches the identity of who sends the data sheets @@ -163,7 +176,8 @@ define('JSON_REMOTE_PASSWORD', 'json_password'); //DO NOT USE IN PRODUCTION define('JSON_REMOTE_PATH', '/path/to/files/'); -// Add/Drop Reporting +/* ADD/DROP REPORTING ------------------------------------------------------ */ + // Where to email reports. Set to null to disable sending email. // Sendmail (or equivalent) needs to be installed on the server and configured // in php.ini. Reports are sent "unauthenticated". diff --git a/student_auto_feed/csv_local.php b/student_auto_feed/csv_local.php new file mode 100644 index 0000000..a2d7c83 --- /dev/null +++ b/student_auto_feed/csv_local.php @@ -0,0 +1,91 @@ +#!/usr/bin/env php +validate_csv(): + case $this->copy_csv(): + // If we wind up here, something went wrong in the main process. + fprintf(STDERR, "%s", self::$err); + exit(1); + } + } + + /** + * Validate CSV file before copy. + * + * Check's for the file's existence and tries to check that the file was + * provided/refreshed on the same day as the autofeed was run. The day + * check is to help prevent the auto feed from blindly running the same CSV + * multiple days in a row and alert the sysadmin that an expected file + * refresh did not happen. $this->err is set with an error message when + * validation fails. + * + * @return boolean true when CSV is validated, false otherwise. + */ + private function validate_csv() { + clearstatcache(); + + if (!file_exists(self::$source_file)) { + self::$err = sprintf("CSV upload missing: %s\n", self::$source_file); + return false; + } + + $file_modified = filemtime(self::$source_file); + $today = time(); + // There are 86400 seconds in a day. + if (intdiv($today, 86400) !== intdiv($file_modified, 86400)) { + $today = date("m-d-Y", $today); + $file_modified = date("m-d-Y", $file_modified); + $hash = md5(file_get_contents(self::$source_file)); + self::$err = sprintf("CSV upload modified time mismatch.\nToday: %s\nUploaded File: %s\nUploaded File Hash: %s\n", $today, $file_modified, $hash); + return false; + } + + return true; + } + + /** + * Copy CSV file. + * + * $this->err is set with an error message when file copy fails. + * + * @return boolean true when copy is successful, false otherwise. + */ + private function copy_csv() { + if (file_exists(self::$dest_file)) { + unlink(self::$dest_file); + } + + if (!copy(self::$source_file, self::$dest_file)) { + self::$err = sprintf("Failed to copy file.\nSource: %s\nDest: %s\n", self::$source_file, self::$dest_file); + return false; + } + + return true; + } +} diff --git a/student_auto_feed/ssaf.sh b/student_auto_feed/ssaf.sh new file mode 100644 index 0000000..ebc7d15 --- /dev/null +++ b/student_auto_feed/ssaf.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Use this bash script to run a data sourcing script before +# submitty_student_auto_feed.php. This is intended to be used with cron. +# +# Author: Peter Bailie, Rensselaer Polytechnic Institute + +display_usage() { + cat << EOM +usage: ssaf.sh (data_source) (term) [DB_auth] + +data_source: csv_local|imap_remote|json_remote + Which data sourcing script to run first: csv_local.php, + imap_remote.php, or json_remote.php (required) +term: Term code to pass to submitty_student_auto_feed.php (required) +DB_auth: DB auth string for submitty_student_auto_feed.php [optional] +EOM + exit 1 +} + +if [ $# -ne 2 ] && [ $# -ne 3 ]; then + display_usage +fi + +CWD=$(dirname "$0") +if [ "$1" = "csv_local" ] || [ "$1" = "imap_remote" ] || [ "$1" = "json_remote" ]; then + SOURCE="${CWD}/${1}.php" +else + display_usage +fi + +if $SOURCE; then + if [ "$3" != "" ]; then + DASH_A="-a$3" + fi + + DASH_T="-t$2" + "$CWD"/submitty_student_auto_feed.php "$DASH_T" "$DASH_A" +else + echo "${1}.php exited $?. Auto feed not run." +fi diff --git a/student_auto_feed/ssaf_remote.sh b/student_auto_feed/ssaf_remote.sh deleted file mode 100755 index 9c2f98f..0000000 --- a/student_auto_feed/ssaf_remote.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash - -# Use this bash script to run either imap_remote.php or json_remote.php before -# submitty_student_auto_feed.php. This is intended to be used with cron. -# -# Author: Peter Bailie, Rensselaer Polytechnic Institute - -display_usage() { - cat << EOM -usage: ssaf_remote.sh (imap|json) (term) [DB_auth] - -imap|json: first run either imap_remote.php or json_remote.php (required) -term: term code to pass to submitty_student_auto_feed.php (required) -DB_auth: DB auth string for submitty_student_auto_feed.php [optional] -EOM - exit 1 -} - -if [ $# -ne 2 ] && [ $# -ne 3 ]; then - display_usage -fi - -CWD=$(dirname "$0") -if [ "$1" = "imap" ] || [ "$1" = "json" ]; then - REMOTE="${CWD}/${1}_remote.php" -else - display_usage -fi - -if $REMOTE; then - if [ "$3" != "" ]; then - DASH_A="-a$3" - fi - - DASH_T="-t$2" - "$CWD"/submitty_student_auto_feed.php "$DASH_T" "$DASH_A" -else - echo "${1}_remote.php exited $?. Auto feed not run." -fi