@@ -3,9 +3,9 @@ |
||
| 3 | 3 | require_once('common.inc.php'); |
| 4 | 4 | |
| 5 | 5 | if ($ready) { |
| 6 | - header('Location: consumers.php'); |
|
| 6 | + header('Location: consumers.php'); |
|
| 7 | 7 | } else { |
| 8 | - header('Location: install.php'); |
|
| 8 | + header('Location: install.php'); |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | ?> |
| 12 | 12 | \ No newline at end of file |
@@ -10,72 +10,72 @@ |
||
| 10 | 10 | |
| 11 | 11 | /* validate new consumer information and save it */ |
| 12 | 12 | if (isset($_REQUEST['name']) && isset($_REQUEST['key']) && isset($_REQUEST['secret'])) { |
| 13 | - $valid = true; |
|
| 14 | - $message = 'Invalid consumer information. '; |
|
| 15 | - if (empty($_name = trim($_REQUEST['name']))) { |
|
| 16 | - $valid = false; |
|
| 17 | - $message .= 'Consumer name must not be empty. '; |
|
| 18 | - } |
|
| 19 | - if (empty($_key = trim($_REQUEST['key']))) { |
|
| 20 | - $valid = false; |
|
| 21 | - $message .= 'Consumer key must not be empty. '; |
|
| 22 | - } |
|
| 23 | - if (empty(trim($_REQUEST['secret']))) { // secret may contain intentional whitespace -- leave untrimmed |
|
| 24 | - $valid = false; |
|
| 25 | - $message .= 'Shared secret must not be empty. '; |
|
| 26 | - } |
|
| 13 | + $valid = true; |
|
| 14 | + $message = 'Invalid consumer information. '; |
|
| 15 | + if (empty($_name = trim($_REQUEST['name']))) { |
|
| 16 | + $valid = false; |
|
| 17 | + $message .= 'Consumer name must not be empty. '; |
|
| 18 | + } |
|
| 19 | + if (empty($_key = trim($_REQUEST['key']))) { |
|
| 20 | + $valid = false; |
|
| 21 | + $message .= 'Consumer key must not be empty. '; |
|
| 22 | + } |
|
| 23 | + if (empty(trim($_REQUEST['secret']))) { // secret may contain intentional whitespace -- leave untrimmed |
|
| 24 | + $valid = false; |
|
| 25 | + $message .= 'Shared secret must not be empty. '; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - if ($valid) { |
|
| 29 | - $consumer = new LTI_Tool_Consumer($_key, LTI_Data_Connector::getDataConnector($sql)); |
|
| 30 | - $consumer->name = $_name; |
|
| 31 | - $consumer->secret = $_REQUEST['secret']; |
|
| 32 | - $consumer->enabled = isset($_REQUEST['enabled']); |
|
| 33 | - if (!$consumer->save()) { |
|
| 34 | - $valid = false; |
|
| 35 | - $message = "<strong>Consumer could not be saved.</strong> {$sql->error}"; |
|
| 36 | - } |
|
| 37 | - } |
|
| 28 | + if ($valid) { |
|
| 29 | + $consumer = new LTI_Tool_Consumer($_key, LTI_Data_Connector::getDataConnector($sql)); |
|
| 30 | + $consumer->name = $_name; |
|
| 31 | + $consumer->secret = $_REQUEST['secret']; |
|
| 32 | + $consumer->enabled = isset($_REQUEST['enabled']); |
|
| 33 | + if (!$consumer->save()) { |
|
| 34 | + $valid = false; |
|
| 35 | + $message = "<strong>Consumer could not be saved.</strong> {$sql->error}"; |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - if (!$valid) { |
|
| 40 | - $smarty->addMessage( |
|
| 41 | - 'Required information missing', |
|
| 42 | - $message, |
|
| 43 | - NotificationMessage::ERROR |
|
| 44 | - ); |
|
| 45 | - } |
|
| 39 | + if (!$valid) { |
|
| 40 | + $smarty->addMessage( |
|
| 41 | + 'Required information missing', |
|
| 42 | + $message, |
|
| 43 | + NotificationMessage::ERROR |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | /* look up consumer to edit, if requested */ |
| 48 | 48 | } elseif (isset($_REQUEST['consumer_key'])) { |
| 49 | - $consumer = new LTI_Tool_Consumer($_REQUEST['consumer_key'], LTI_Data_Connector::getDataConnector($sql)); |
|
| 50 | - if (isset($_REQUEST['action'])) |
|
| 51 | - switch ($_REQUEST['action']) { |
|
| 52 | - case 'delete': { |
|
| 53 | - $consumer->delete(); |
|
| 54 | - break; |
|
| 55 | - } |
|
| 56 | - case 'select': { |
|
| 57 | - $name = $consumer->name; |
|
| 58 | - $key = $consumer->getKey(); |
|
| 59 | - $secret = $consumer->secret; |
|
| 60 | - $enabled = $consumer->enabled; |
|
| 61 | - break; |
|
| 62 | - } |
|
| 63 | - case 'update': |
|
| 64 | - case 'insert': |
|
| 65 | - default: { |
|
| 66 | - // leave default form values set |
|
| 67 | - } |
|
| 68 | - } |
|
| 49 | + $consumer = new LTI_Tool_Consumer($_REQUEST['consumer_key'], LTI_Data_Connector::getDataConnector($sql)); |
|
| 50 | + if (isset($_REQUEST['action'])) |
|
| 51 | + switch ($_REQUEST['action']) { |
|
| 52 | + case 'delete': { |
|
| 53 | + $consumer->delete(); |
|
| 54 | + break; |
|
| 55 | + } |
|
| 56 | + case 'select': { |
|
| 57 | + $name = $consumer->name; |
|
| 58 | + $key = $consumer->getKey(); |
|
| 59 | + $secret = $consumer->secret; |
|
| 60 | + $enabled = $consumer->enabled; |
|
| 61 | + break; |
|
| 62 | + } |
|
| 63 | + case 'update': |
|
| 64 | + case 'insert': |
|
| 65 | + default: { |
|
| 66 | + // leave default form values set |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /* display a list of consumers */ |
| 72 | 72 | $response = $sql->query("SELECT * FROM `" . LTI_Data_Connector::CONSUMER_TABLE_NAME . "` ORDER BY `name` ASC, `consumer_key` ASC"); |
| 73 | 73 | $consumers = array(); |
| 74 | 74 | while ($consumer = $response->fetch_assoc()) { |
| 75 | - $consumers[] = $consumer; |
|
| 75 | + $consumers[] = $consumer; |
|
| 76 | 76 | } |
| 77 | 77 | if (!empty($consumers)) { |
| 78 | - $smarty->assign('fields', array_keys($consumers[0])); |
|
| 78 | + $smarty->assign('fields', array_keys($consumers[0])); |
|
| 79 | 79 | } |
| 80 | 80 | $smarty->assign('consumers', $consumers); |
| 81 | 81 | |
@@ -3,30 +3,30 @@ |
||
| 3 | 3 | require_once('common.inc.php'); |
| 4 | 4 | |
| 5 | 5 | if (isset($_REQUEST['course_url'])) { |
| 6 | - $eventsApi = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 6 | + $eventsApi = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 7 | 7 | |
| 8 | - // TODO work nicely with the cache (purge uncached events, or only cached events, etc.) |
|
| 8 | + // TODO work nicely with the cache (purge uncached events, or only cached events, etc.) |
|
| 9 | 9 | |
| 10 | - $events = $eventsApi->get('calendar_events', |
|
| 11 | - array( |
|
| 12 | - 'type' => 'event', |
|
| 13 | - 'all_events' => true, |
|
| 14 | - 'context_codes[]' => preg_replace('|.*/courses/(\d+)/?.*|', "course_$1", $_REQUEST['course_url']) |
|
| 15 | - ) |
|
| 16 | - ); |
|
| 17 | - do { |
|
| 18 | - foreach($events as $event) { |
|
| 19 | - $api->delete("calendar_events/{$event['id']}", |
|
| 20 | - array( |
|
| 21 | - 'cancel_reason' => $metadata['APP_NAME'] . " course_url={$_REQUEST['course_url']}" |
|
| 22 | - ) |
|
| 23 | - ); |
|
| 24 | - } |
|
| 25 | - } while($events = $eventsApi->nextPage()); |
|
| 10 | + $events = $eventsApi->get('calendar_events', |
|
| 11 | + array( |
|
| 12 | + 'type' => 'event', |
|
| 13 | + 'all_events' => true, |
|
| 14 | + 'context_codes[]' => preg_replace('|.*/courses/(\d+)/?.*|', "course_$1", $_REQUEST['course_url']) |
|
| 15 | + ) |
|
| 16 | + ); |
|
| 17 | + do { |
|
| 18 | + foreach($events as $event) { |
|
| 19 | + $api->delete("calendar_events/{$event['id']}", |
|
| 20 | + array( |
|
| 21 | + 'cancel_reason' => $metadata['APP_NAME'] . " course_url={$_REQUEST['course_url']}" |
|
| 22 | + ) |
|
| 23 | + ); |
|
| 24 | + } |
|
| 25 | + } while($events = $eventsApi->nextPage()); |
|
| 26 | 26 | |
| 27 | - $smarty->assign('content', 'Calendar purged.'); |
|
| 27 | + $smarty->assign('content', 'Calendar purged.'); |
|
| 28 | 28 | } else { |
| 29 | - $smarty->assign('content', '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><label for="course_url">Course URL <input id="course_url" name="course_url" type="text" /><input type="submit" value="Purge All Calendar Events" /></form>'); |
|
| 29 | + $smarty->assign('content', '<form action="' . $_SERVER['PHP_SELF'] . '" method="post"><label for="course_url">Course URL <input id="course_url" name="course_url" type="text" /><input type="submit" value="Purge All Calendar Events" /></form>'); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | $smarty->display(); |
@@ -3,27 +3,27 @@ |
||
| 3 | 3 | require_once('common.inc.php'); |
| 4 | 4 | |
| 5 | 5 | if (isset($_REQUEST['course_url'])) { |
| 6 | - $courseId = preg_replace('|.*/courses/(\d+)/?.*|', '$1', parse_url($_REQUEST['course_url'], PHP_URL_PATH)); |
|
| 7 | - $course = $api->get("/courses/$courseId"); |
|
| 8 | - if ($course) { |
|
| 9 | - $webcalFeed = str_replace('https://', 'webcal://', $course['calendar']['ics']); |
|
| 10 | - $smarty->assign('content', ' |
|
| 6 | + $courseId = preg_replace('|.*/courses/(\d+)/?.*|', '$1', parse_url($_REQUEST['course_url'], PHP_URL_PATH)); |
|
| 7 | + $course = $api->get("/courses/$courseId"); |
|
| 8 | + if ($course) { |
|
| 9 | + $webcalFeed = str_replace('https://', 'webcal://', $course['calendar']['ics']); |
|
| 10 | + $smarty->assign('content', ' |
|
| 11 | 11 | <h3>Course Calendar ICS Feed</h3> |
| 12 | 12 | <p>You can subscribe to the calendar for <a href="https://' . |
| 13 | - parse_url(CANVAS_API_URL, PHP_URL_HOST) . '/courses/' . $courseId . |
|
| 14 | - '">' . $course['name'] . '</a> at <a href="' . |
|
| 15 | - $webcalFeed . '">' . $webcalFeed . |
|
| 16 | - '</a> in any calendar application that supports external ICS feeds.</p>' |
|
| 17 | - ); |
|
| 18 | - } else { |
|
| 19 | - $messages[] = array( |
|
| 20 | - 'class' => 'error', |
|
| 21 | - 'title' => 'Canvas API Error', |
|
| 22 | - 'content' => 'The course you requested could not be accessed.<pre>' . print_r($json, false) . '</pre>' |
|
| 23 | - ); |
|
| 24 | - } |
|
| 13 | + parse_url(CANVAS_API_URL, PHP_URL_HOST) . '/courses/' . $courseId . |
|
| 14 | + '">' . $course['name'] . '</a> at <a href="' . |
|
| 15 | + $webcalFeed . '">' . $webcalFeed . |
|
| 16 | + '</a> in any calendar application that supports external ICS feeds.</p>' |
|
| 17 | + ); |
|
| 18 | + } else { |
|
| 19 | + $messages[] = array( |
|
| 20 | + 'class' => 'error', |
|
| 21 | + 'title' => 'Canvas API Error', |
|
| 22 | + 'content' => 'The course you requested could not be accessed.<pre>' . print_r($json, false) . '</pre>' |
|
| 23 | + ); |
|
| 24 | + } |
|
| 25 | 25 | } else { |
| 26 | - $smarty->assign('content', ' |
|
| 26 | + $smarty->assign('content', ' |
|
| 27 | 27 | <form method="post" action="' . $_SERVER['PHP_SELF'] . '"> |
| 28 | 28 | <label for="course_url">Course URL <span class="comment">The URL to the course whose calendar you would like to export as an ICS feed</span></label> |
| 29 | 29 | <input id="course_url" name="course_url" type="text" /> |
@@ -7,41 +7,41 @@ |
||
| 7 | 7 | user is about to be asked to log into Canvas in the middle of Canvas */ |
| 8 | 8 | |
| 9 | 9 | try { |
| 10 | - $oauth = new OAuthNegotiator(); |
|
| 10 | + $oauth = new OAuthNegotiator(); |
|
| 11 | 11 | } catch (OAuthNegotiator_Exception $e) {} |
| 12 | 12 | |
| 13 | 13 | if (isset($_REQUEST['oauth'])) { |
| 14 | - switch ($_REQUEST['oauth']) { |
|
| 15 | - case 'request': { |
|
| 16 | - $smarty->assign('content', '<h1>Token Request</h1> |
|
| 14 | + switch ($_REQUEST['oauth']) { |
|
| 15 | + case 'request': { |
|
| 16 | + $smarty->assign('content', '<h1>Token Request</h1> |
|
| 17 | 17 | <p>This application requires access to the Canvas APIs. Canvas is about to ask you to give permission for this.</p> |
| 18 | 18 | <p><a href="' . $_SERVER['PHP_SELF'] . '?oauth=process">Click to continue</a></p>'); |
| 19 | - $smarty->display(); |
|
| 20 | - exit; |
|
| 21 | - } |
|
| 22 | - case 'process': { |
|
| 23 | - $oauth = new OAuthNegotiator( |
|
| 24 | - 'https://' . $toolProvider->user->getResourceLink()->settings['custom_canvas_api_domain'] . '/login/oauth2', |
|
| 25 | - (string) $secrets->oauth->id, |
|
| 26 | - (string) $secrets->oauth->key, |
|
| 27 | - "{$_SERVER['PHP_SELF']}?oauth=complete", |
|
| 28 | - (string) $secrets->app->name |
|
| 29 | - ); |
|
| 30 | - break; |
|
| 31 | - } |
|
| 32 | - case 'complete': { |
|
| 33 | - $user = new UserAPIToken($_SESSION['user_consumer_key'], $_SESSION['user_id'], $sql); |
|
| 34 | - $user->setToken($oauth->getToken()); |
|
| 35 | - $user->setAPIUrl("{$metadata['CANVAS_INSTANCE_URL']}/api/v1"); |
|
| 19 | + $smarty->display(); |
|
| 20 | + exit; |
|
| 21 | + } |
|
| 22 | + case 'process': { |
|
| 23 | + $oauth = new OAuthNegotiator( |
|
| 24 | + 'https://' . $toolProvider->user->getResourceLink()->settings['custom_canvas_api_domain'] . '/login/oauth2', |
|
| 25 | + (string) $secrets->oauth->id, |
|
| 26 | + (string) $secrets->oauth->key, |
|
| 27 | + "{$_SERVER['PHP_SELF']}?oauth=complete", |
|
| 28 | + (string) $secrets->app->name |
|
| 29 | + ); |
|
| 30 | + break; |
|
| 31 | + } |
|
| 32 | + case 'complete': { |
|
| 33 | + $user = new UserAPIToken($_SESSION['user_consumer_key'], $_SESSION['user_id'], $sql); |
|
| 34 | + $user->setToken($oauth->getToken()); |
|
| 35 | + $user->setAPIUrl("{$metadata['CANVAS_INSTANCE_URL']}/api/v1"); |
|
| 36 | 36 | |
| 37 | - $_SESSION['apiToken'] = $user->getToken(); |
|
| 38 | - $_SESSION['apiUrl'] = $user->getAPIUrl(); |
|
| 39 | - $_SESSION['isUserToken'] = true; |
|
| 37 | + $_SESSION['apiToken'] = $user->getToken(); |
|
| 38 | + $_SESSION['apiUrl'] = $user->getAPIUrl(); |
|
| 39 | + $_SESSION['isUserToken'] = true; |
|
| 40 | 40 | |
| 41 | - header("Location: {$metadata['APP_URL']}/app.php"); |
|
| 42 | - exit; |
|
| 43 | - } |
|
| 44 | - } |
|
| 41 | + header("Location: {$metadata['APP_URL']}/app.php"); |
|
| 42 | + exit; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | ?> |
| 48 | 48 | \ No newline at end of file |
@@ -4,15 +4,15 @@ discard block |
||
| 4 | 4 | use Battis\BootstrapSmarty\NotificationMessage; |
| 5 | 5 | |
| 6 | 6 | if (isset($_SESSION['toolProvider']->user)) { |
| 7 | - $_SESSION['canvasInstanceUrl'] = 'https://' . $_SESSION['toolProvider']->user->getResourceLink()->settings['custom_canvas_api_domain']; |
|
| 7 | + $_SESSION['canvasInstanceUrl'] = 'https://' . $_SESSION['toolProvider']->user->getResourceLink()->settings['custom_canvas_api_domain']; |
|
| 8 | 8 | } else { |
| 9 | - $_SESSION['canvasInstanceUrl'] = $metadata['CANVAS_INSTANCE_URL']; |
|
| 9 | + $_SESSION['canvasInstanceUrl'] = $metadata['CANVAS_INSTANCE_URL']; |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | if (isset($_SESSION['apiUrl']) && isset($_SESSION['apiToken'])) { |
| 13 | - $api = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 13 | + $api = new CanvasPest($_SESSION['apiUrl'], $_SESSION['apiToken']); |
|
| 14 | 14 | } else { |
| 15 | - $api = new CanvasPest($metadata['CANVAS_API_URL'], $metadata['CANVAS_API_TOKEN']); |
|
| 15 | + $api = new CanvasPest($metadata['CANVAS_API_URL'], $metadata['CANVAS_API_TOKEN']); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | /* argument values for sync */ |
@@ -59,18 +59,18 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | $log = null; |
| 61 | 61 | function postMessage($subject, $body, $flag = NotificationMessage::INFO) { |
| 62 | - global $log; |
|
| 63 | - global $smarty; |
|
| 64 | - if (php_sapi_name() != 'cli') { |
|
| 65 | - $smarty->addMessage($subject, $body, $flag); |
|
| 66 | - } else { |
|
| 67 | - $logEntry = "[$flag] $subject: $body"; |
|
| 68 | - if (is_a($log, Log::class)) { |
|
| 69 | - $log->log($logEntry); |
|
| 70 | - } else { |
|
| 71 | - echo "$logEntry\n"; |
|
| 72 | - } |
|
| 73 | - } |
|
| 62 | + global $log; |
|
| 63 | + global $smarty; |
|
| 64 | + if (php_sapi_name() != 'cli') { |
|
| 65 | + $smarty->addMessage($subject, $body, $flag); |
|
| 66 | + } else { |
|
| 67 | + $logEntry = "[$flag] $subject: $body"; |
|
| 68 | + if (is_a($log, Log::class)) { |
|
| 69 | + $log->log($logEntry); |
|
| 70 | + } else { |
|
| 71 | + echo "$logEntry\n"; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -78,45 +78,45 @@ discard block |
||
| 78 | 78 | * Canvas calendar |
| 79 | 79 | **/ |
| 80 | 80 | function getPairingHash($icsUrl, $canvasContext) { |
| 81 | - global $metadata; |
|
| 82 | - return md5($icsUrl . $canvasContext . $metadata['CANVAS_INSTANCE_URL']); |
|
| 81 | + global $metadata; |
|
| 82 | + return md5($icsUrl . $canvasContext . $metadata['CANVAS_INSTANCE_URL']); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $FIELD_MAP = array( |
| 86 | - 'calendar_event[title]' => 'SUMMARY', |
|
| 87 | - 'calendar_event[description]' => 'DESCRIPTION', |
|
| 88 | - 'calendar_event[start_at]' => array ( |
|
| 89 | - 0 => 'X-CURRENT-DTSTART', |
|
| 90 | - 1 => 'DTSTART' |
|
| 91 | - ), |
|
| 92 | - 'calendar_event[end_at]' => array( |
|
| 93 | - 0 => 'X-CURRENT-DTEND', |
|
| 94 | - 1 => 'DTEND' |
|
| 95 | - ), |
|
| 96 | - 'calendar_event[location_name]' => 'LOCATION' |
|
| 86 | + 'calendar_event[title]' => 'SUMMARY', |
|
| 87 | + 'calendar_event[description]' => 'DESCRIPTION', |
|
| 88 | + 'calendar_event[start_at]' => array ( |
|
| 89 | + 0 => 'X-CURRENT-DTSTART', |
|
| 90 | + 1 => 'DTSTART' |
|
| 91 | + ), |
|
| 92 | + 'calendar_event[end_at]' => array( |
|
| 93 | + 0 => 'X-CURRENT-DTEND', |
|
| 94 | + 1 => 'DTEND' |
|
| 95 | + ), |
|
| 96 | + 'calendar_event[location_name]' => 'LOCATION' |
|
| 97 | 97 | ); |
| 98 | 98 | |
| 99 | 99 | /** |
| 100 | 100 | * Generate a hash of this version of an event to cache in the database |
| 101 | 101 | **/ |
| 102 | 102 | function getEventHash($event) { |
| 103 | - global $FIELD_MAP; |
|
| 104 | - $blob = ''; |
|
| 105 | - foreach ($FIELD_MAP as $field) { |
|
| 106 | - if (is_array($field)) { |
|
| 107 | - foreach ($field as $option) { |
|
| 108 | - if (!empty($property = $event->getProperty($option))) { |
|
| 109 | - $blob .= serialize($property); |
|
| 110 | - break; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - } else { |
|
| 114 | - if (!empty($property = $event->getProperty($field))) { |
|
| 115 | - $blob .= serialize($property); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - return md5($blob); |
|
| 103 | + global $FIELD_MAP; |
|
| 104 | + $blob = ''; |
|
| 105 | + foreach ($FIELD_MAP as $field) { |
|
| 106 | + if (is_array($field)) { |
|
| 107 | + foreach ($field as $option) { |
|
| 108 | + if (!empty($property = $event->getProperty($option))) { |
|
| 109 | + $blob .= serialize($property); |
|
| 110 | + break; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + } else { |
|
| 114 | + if (!empty($property = $event->getProperty($field))) { |
|
| 115 | + $blob .= serialize($property); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + return md5($blob); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -124,14 +124,14 @@ discard block |
||
| 124 | 124 | **/ |
| 125 | 125 | $SYNC_TIMESTAMP = null; |
| 126 | 126 | function getSyncTimestamp() { |
| 127 | - global $SYNC_TIMESTAMP; |
|
| 128 | - if ($SYNC_TIMESTAMP) { |
|
| 129 | - return $SYNC_TIMESTAMP; |
|
| 130 | - } else { |
|
| 131 | - $timestamp = new DateTime(); |
|
| 132 | - $SYNC_TIMESTAMP = $timestamp->format(SYNC_TIMESTAMP_FORMAT) . SEPARATOR . md5((php_sapi_name() == 'cli' ? 'cli' : $_SERVER['REMOTE_ADDR']) . time()); |
|
| 133 | - return $SYNC_TIMESTAMP; |
|
| 134 | - } |
|
| 127 | + global $SYNC_TIMESTAMP; |
|
| 128 | + if ($SYNC_TIMESTAMP) { |
|
| 129 | + return $SYNC_TIMESTAMP; |
|
| 130 | + } else { |
|
| 131 | + $timestamp = new DateTime(); |
|
| 132 | + $SYNC_TIMESTAMP = $timestamp->format(SYNC_TIMESTAMP_FORMAT) . SEPARATOR . md5((php_sapi_name() == 'cli' ? 'cli' : $_SERVER['REMOTE_ADDR']) . time()); |
|
| 133 | + return $SYNC_TIMESTAMP; |
|
| 134 | + } |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | ?> |
@@ -1,12 +1,12 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (php_sapi_name() == 'cli') { |
| 4 | - // TODO there is a more streamlined way of doing this that escapes me just this second |
|
| 5 | - $_REQUEST['cal'] = $argv[1]; |
|
| 6 | - $_REQUEST['canvas_url'] = $argv[2]; |
|
| 7 | - $_REQUEST['schedule'] = $argv[3]; |
|
| 4 | + // TODO there is a more streamlined way of doing this that escapes me just this second |
|
| 5 | + $_REQUEST['cal'] = $argv[1]; |
|
| 6 | + $_REQUEST['canvas_url'] = $argv[2]; |
|
| 7 | + $_REQUEST['schedule'] = $argv[3]; |
|
| 8 | 8 | |
| 9 | - define ('IGNORE_LTI', true); |
|
| 9 | + define ('IGNORE_LTI', true); |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | require_once 'common.inc.php'; |
@@ -18,49 +18,49 @@ discard block |
||
| 18 | 18 | * Check to see if a URL exists |
| 19 | 19 | **/ |
| 20 | 20 | function urlExists($url) { |
| 21 | - $handle = fopen($url, 'r'); |
|
| 22 | - return $handle !== false; |
|
| 21 | + $handle = fopen($url, 'r'); |
|
| 22 | + return $handle !== false; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * compute the calendar context for the canvas object based on its URL |
| 27 | 27 | **/ |
| 28 | 28 | function getCanvasContext($canvasUrl) { |
| 29 | - global $metadata; |
|
| 30 | - |
|
| 31 | - // TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all) |
|
| 32 | - // FIXME: users aren't working |
|
| 33 | - // TODO: it would probably be better to look up users by email address than URL |
|
| 34 | - /* get the context (user, course or group) for the canvas URL */ |
|
| 35 | - $canvasContext = array(); |
|
| 36 | - if (preg_match('%(https?://)?(' . parse_url($metadata['CANVAS_INSTANCE_URL'], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $_REQUEST['canvas_url'], $matches)) { |
|
| 37 | - $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953 |
|
| 38 | - |
|
| 39 | - // course or account groups |
|
| 40 | - if (isset($matches[9]) || isset($matches[11])) { |
|
| 41 | - $canvasContext['context'] = 'group'; // used to for context_code in events |
|
| 42 | - $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]); |
|
| 43 | - $canvasContext['verification_url'] = "groups/{$canvasContext['id']}"; // used once to look up the object to be sure it really exists |
|
| 44 | - |
|
| 45 | - // courses |
|
| 46 | - } elseif (isset($matches[7])) { |
|
| 47 | - $canvasContext['context'] = 'course'; |
|
| 48 | - $canvasContext['id'] = $matches[7]; |
|
| 49 | - $canvasContext['verification_url'] = "courses/{$canvasContext['id']}"; |
|
| 50 | - |
|
| 51 | - // users |
|
| 52 | - } elseif (isset($matches[5])) { |
|
| 53 | - $canvasContext['context'] = 'user'; |
|
| 54 | - $canvasContext['id'] = $matches[5]; |
|
| 55 | - $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile"; |
|
| 56 | - |
|
| 57 | - // we're somewhere where we don't know where we are |
|
| 58 | - } else { |
|
| 59 | - return false; |
|
| 60 | - } |
|
| 61 | - return $canvasContext; |
|
| 62 | - } |
|
| 63 | - return false; |
|
| 29 | + global $metadata; |
|
| 30 | + |
|
| 31 | + // TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all) |
|
| 32 | + // FIXME: users aren't working |
|
| 33 | + // TODO: it would probably be better to look up users by email address than URL |
|
| 34 | + /* get the context (user, course or group) for the canvas URL */ |
|
| 35 | + $canvasContext = array(); |
|
| 36 | + if (preg_match('%(https?://)?(' . parse_url($metadata['CANVAS_INSTANCE_URL'], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $_REQUEST['canvas_url'], $matches)) { |
|
| 37 | + $canvasContext['canonical_url'] = "https://{$matches[2]}"; // https://stmarksschool.instructure.com/courses/953 |
|
| 38 | + |
|
| 39 | + // course or account groups |
|
| 40 | + if (isset($matches[9]) || isset($matches[11])) { |
|
| 41 | + $canvasContext['context'] = 'group'; // used to for context_code in events |
|
| 42 | + $canvasContext['id'] = ($matches[9] > $matches[11] ? $matches[9] : $matches[11]); |
|
| 43 | + $canvasContext['verification_url'] = "groups/{$canvasContext['id']}"; // used once to look up the object to be sure it really exists |
|
| 44 | + |
|
| 45 | + // courses |
|
| 46 | + } elseif (isset($matches[7])) { |
|
| 47 | + $canvasContext['context'] = 'course'; |
|
| 48 | + $canvasContext['id'] = $matches[7]; |
|
| 49 | + $canvasContext['verification_url'] = "courses/{$canvasContext['id']}"; |
|
| 50 | + |
|
| 51 | + // users |
|
| 52 | + } elseif (isset($matches[5])) { |
|
| 53 | + $canvasContext['context'] = 'user'; |
|
| 54 | + $canvasContext['id'] = $matches[5]; |
|
| 55 | + $canvasContext['verification_url'] = "users/{$canvasContext['id']}/profile"; |
|
| 56 | + |
|
| 57 | + // we're somewhere where we don't know where we are |
|
| 58 | + } else { |
|
| 59 | + return false; |
|
| 60 | + } |
|
| 61 | + return $canvasContext; |
|
| 62 | + } |
|
| 63 | + return false; |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -70,22 +70,22 @@ discard block |
||
| 70 | 70 | **/ |
| 71 | 71 | function filterEvent($event, $calendarCache) { |
| 72 | 72 | |
| 73 | - return ( |
|
| 74 | - // include this event if filtering is off... |
|
| 75 | - $calendarCache['enable_regexp_filter'] == false || |
|
| 76 | - ( |
|
| 77 | - ( |
|
| 78 | - ( // if filtering is on, and there's an include pattern test that pattern... |
|
| 79 | - !empty($calendarCache['include_regexp']) && |
|
| 80 | - preg_match("%{$calendarCache['include_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 81 | - ) |
|
| 82 | - ) && |
|
| 83 | - !( // if there is an exclude pattern, make sure that this event is NOT excluded |
|
| 84 | - !empty($calendarCache['exclude_regexp']) && |
|
| 85 | - preg_match("%{$calendarCache['exclude_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 86 | - ) |
|
| 87 | - ) |
|
| 88 | - ); |
|
| 73 | + return ( |
|
| 74 | + // include this event if filtering is off... |
|
| 75 | + $calendarCache['enable_regexp_filter'] == false || |
|
| 76 | + ( |
|
| 77 | + ( |
|
| 78 | + ( // if filtering is on, and there's an include pattern test that pattern... |
|
| 79 | + !empty($calendarCache['include_regexp']) && |
|
| 80 | + preg_match("%{$calendarCache['include_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 81 | + ) |
|
| 82 | + ) && |
|
| 83 | + !( // if there is an exclude pattern, make sure that this event is NOT excluded |
|
| 84 | + !empty($calendarCache['exclude_regexp']) && |
|
| 85 | + preg_match("%{$calendarCache['exclude_regexp']}%", $event->getProperty('SUMMARY')) |
|
| 86 | + ) |
|
| 87 | + ) |
|
| 88 | + ); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | // TODO: it would be nice to be able to cleanly remove a synched calendar |
@@ -97,55 +97,55 @@ discard block |
||
| 97 | 97 | object)? */ |
| 98 | 98 | if (isset($_REQUEST['cal']) && isset($_REQUEST['canvas_url'])) { |
| 99 | 99 | |
| 100 | - if ($canvasContext = getCanvasContext($_REQUEST['canvas_url'])) { |
|
| 101 | - /* check ICS feed to be sure it exists */ |
|
| 102 | - if(urlExists($_REQUEST['cal'])) { |
|
| 103 | - /* look up the canvas object -- mostly to make sure that it exists! */ |
|
| 104 | - if ($canvasObject = $api->get($canvasContext['verification_url'])) { |
|
| 100 | + if ($canvasContext = getCanvasContext($_REQUEST['canvas_url'])) { |
|
| 101 | + /* check ICS feed to be sure it exists */ |
|
| 102 | + if(urlExists($_REQUEST['cal'])) { |
|
| 103 | + /* look up the canvas object -- mostly to make sure that it exists! */ |
|
| 104 | + if ($canvasObject = $api->get($canvasContext['verification_url'])) { |
|
| 105 | 105 | |
| 106 | - /* calculate the unique pairing ID of this ICS feed and canvas object */ |
|
| 107 | - $pairingHash = getPairingHash($_REQUEST['cal'], $canvasContext['canonical_url']); |
|
| 108 | - $log = Log::singleton('file', __DIR__ . "/logs/$pairingHash.log"); |
|
| 109 | - postMessage('Sync started', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 106 | + /* calculate the unique pairing ID of this ICS feed and canvas object */ |
|
| 107 | + $pairingHash = getPairingHash($_REQUEST['cal'], $canvasContext['canonical_url']); |
|
| 108 | + $log = Log::singleton('file', __DIR__ . "/logs/$pairingHash.log"); |
|
| 109 | + postMessage('Sync started', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 110 | 110 | |
| 111 | - /* tell users that it's started and to cool their jets */ |
|
| 112 | - if (php_sapi_name() != 'cli') { |
|
| 113 | - $smarty->assign('content', ' |
|
| 111 | + /* tell users that it's started and to cool their jets */ |
|
| 112 | + if (php_sapi_name() != 'cli') { |
|
| 113 | + $smarty->assign('content', ' |
|
| 114 | 114 | <h3>Calendar Import Started</h3> |
| 115 | 115 | <p>The calendar import that you requested has begun. You may leave this page at anytime. You can see the progress of the import by visiting <a target="_blank" href="https://' . parse_url($metadata['CANVAS_INSTANCE_URL'], PHP_URL_HOST) . "/calendar?include_contexts={$canvasContext['context']}_{$canvasObject['id']}\">this calendar</a> in Canvas.</p>" |
| 116 | - ); |
|
| 117 | - $smarty->display('page.tpl'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /* parse the ICS feed */ |
|
| 121 | - $ics = new vcalendar( |
|
| 122 | - array( |
|
| 123 | - 'unique_id' => $metadata['APP_ID'], |
|
| 124 | - 'url' => $_REQUEST['cal'] |
|
| 125 | - ) |
|
| 126 | - ); |
|
| 127 | - $ics->parse(); |
|
| 128 | - |
|
| 129 | - /* log this pairing in the database cache, if it doesn't already exist */ |
|
| 130 | - $calendarCacheResponse = $sql->query(" |
|
| 116 | + ); |
|
| 117 | + $smarty->display('page.tpl'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /* parse the ICS feed */ |
|
| 121 | + $ics = new vcalendar( |
|
| 122 | + array( |
|
| 123 | + 'unique_id' => $metadata['APP_ID'], |
|
| 124 | + 'url' => $_REQUEST['cal'] |
|
| 125 | + ) |
|
| 126 | + ); |
|
| 127 | + $ics->parse(); |
|
| 128 | + |
|
| 129 | + /* log this pairing in the database cache, if it doesn't already exist */ |
|
| 130 | + $calendarCacheResponse = $sql->query(" |
|
| 131 | 131 | SELECT * |
| 132 | 132 | FROM `calendars` |
| 133 | 133 | WHERE |
| 134 | 134 | `id` = '$pairingHash' |
| 135 | 135 | "); |
| 136 | - $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 136 | + $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 137 | 137 | |
| 138 | - /* if the calendar is already cached, just update the sync timestamp */ |
|
| 139 | - if ($calendarCache) { |
|
| 140 | - $sql->query(" |
|
| 138 | + /* if the calendar is already cached, just update the sync timestamp */ |
|
| 139 | + if ($calendarCache) { |
|
| 140 | + $sql->query(" |
|
| 141 | 141 | UPDATE `calendars` |
| 142 | 142 | SET |
| 143 | 143 | `synced` = '" . getSyncTimestamp() . "' |
| 144 | 144 | WHERE |
| 145 | 145 | `id` = '$pairingHash' |
| 146 | 146 | "); |
| 147 | - } else { |
|
| 148 | - $sql->query(" |
|
| 147 | + } else { |
|
| 148 | + $sql->query(" |
|
| 149 | 149 | INSERT INTO `calendars` |
| 150 | 150 | ( |
| 151 | 151 | `id`, |
@@ -168,46 +168,46 @@ discard block |
||
| 168 | 168 | " . ($_REQUEST['enable_regexp_filter'] == VALUE_ENABLE_REGEXP_FILTER ? "'" . $sql->real_escape_string($_REQUEST['exclude_regexp']) . "'" : 'NULL') . " |
| 169 | 169 | ) |
| 170 | 170 | "); |
| 171 | - } |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /* refresh calendar information from cache database */ |
|
| 174 | - $calendarCacheResponse = $sql->query(" |
|
| 173 | + /* refresh calendar information from cache database */ |
|
| 174 | + $calendarCacheResponse = $sql->query(" |
|
| 175 | 175 | SELECT * |
| 176 | 176 | FROM `calendars` |
| 177 | 177 | WHERE |
| 178 | 178 | `id` = '$pairingHash' |
| 179 | 179 | "); |
| 180 | - $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 180 | + $calendarCache = $calendarCacheResponse->fetch_assoc(); |
|
| 181 | 181 | |
| 182 | - /* walk through $master_array and update the Canvas calendar to match the |
|
| 182 | + /* walk through $master_array and update the Canvas calendar to match the |
|
| 183 | 183 | ICS feed, caching changes in the database */ |
| 184 | - // TODO: would it be worth the performance improvement to just process things from today's date forward? (i.e. ignore old items, even if they've changed...) |
|
| 185 | - // TODO:0 the best window for syncing would be the term of the course in question, right? issue:12 |
|
| 186 | - // TODO:0 Arbitrarily selecting events in for a year on either side of today's date, probably a better system? issue:12 |
|
| 187 | - foreach ($ics->selectComponents( |
|
| 188 | - date('Y')-1, // startYear |
|
| 189 | - date('m'), // startMonth |
|
| 190 | - date('d'), // startDay |
|
| 191 | - date('Y')+1, // endYEar |
|
| 192 | - date('m'), // endMonth |
|
| 193 | - date('d'), // endDay |
|
| 194 | - 'vevent', // cType |
|
| 195 | - false, // flat |
|
| 196 | - true, // any |
|
| 197 | - true // split |
|
| 198 | - ) as $year) { |
|
| 199 | - foreach ($year as $month => $days) { |
|
| 200 | - foreach ($days as $day => $events) { |
|
| 201 | - foreach ($events as $i => $event) { |
|
| 202 | - |
|
| 203 | - /* does this event already exist in Canvas? */ |
|
| 204 | - $eventHash = getEventHash($event); |
|
| 205 | - |
|
| 206 | - /* if the event should be included... */ |
|
| 207 | - if (filterEvent($event, $calendarCache)) { |
|
| 208 | - |
|
| 209 | - /* have we cached this event already? */ |
|
| 210 | - $eventCacheResponse = $sql->query(" |
|
| 184 | + // TODO: would it be worth the performance improvement to just process things from today's date forward? (i.e. ignore old items, even if they've changed...) |
|
| 185 | + // TODO:0 the best window for syncing would be the term of the course in question, right? issue:12 |
|
| 186 | + // TODO:0 Arbitrarily selecting events in for a year on either side of today's date, probably a better system? issue:12 |
|
| 187 | + foreach ($ics->selectComponents( |
|
| 188 | + date('Y')-1, // startYear |
|
| 189 | + date('m'), // startMonth |
|
| 190 | + date('d'), // startDay |
|
| 191 | + date('Y')+1, // endYEar |
|
| 192 | + date('m'), // endMonth |
|
| 193 | + date('d'), // endDay |
|
| 194 | + 'vevent', // cType |
|
| 195 | + false, // flat |
|
| 196 | + true, // any |
|
| 197 | + true // split |
|
| 198 | + ) as $year) { |
|
| 199 | + foreach ($year as $month => $days) { |
|
| 200 | + foreach ($days as $day => $events) { |
|
| 201 | + foreach ($events as $i => $event) { |
|
| 202 | + |
|
| 203 | + /* does this event already exist in Canvas? */ |
|
| 204 | + $eventHash = getEventHash($event); |
|
| 205 | + |
|
| 206 | + /* if the event should be included... */ |
|
| 207 | + if (filterEvent($event, $calendarCache)) { |
|
| 208 | + |
|
| 209 | + /* have we cached this event already? */ |
|
| 210 | + $eventCacheResponse = $sql->query(" |
|
| 211 | 211 | SELECT * |
| 212 | 212 | FROM `events` |
| 213 | 213 | WHERE |
@@ -216,11 +216,11 @@ discard block |
||
| 216 | 216 | "); |
| 217 | 217 | |
| 218 | 218 | |
| 219 | - /* if we already have the event cached in its current form, just update |
|
| 219 | + /* if we already have the event cached in its current form, just update |
|
| 220 | 220 | the timestamp */ |
| 221 | - $eventCache = $eventCacheResponse->fetch_assoc(); |
|
| 222 | - if ($eventCache) { |
|
| 223 | - $sql->query(" |
|
| 221 | + $eventCache = $eventCacheResponse->fetch_assoc(); |
|
| 222 | + if ($eventCache) { |
|
| 223 | + $sql->query(" |
|
| 224 | 224 | UPDATE `events` |
| 225 | 225 | SET |
| 226 | 226 | `synced` = '" . getSyncTimestamp() . "' |
@@ -228,30 +228,30 @@ discard block |
||
| 228 | 228 | `id` = '{$eventCache['id']}' |
| 229 | 229 | "); |
| 230 | 230 | |
| 231 | - /* otherwise, add this new event and cache it */ |
|
| 232 | - } else { |
|
| 233 | - /* multi-day event instance start times need to be changed to _this_ date */ |
|
| 234 | - $start = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTSTART'))); |
|
| 235 | - $end = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTEND'))); |
|
| 236 | - if ($event->getProperty('X-RECURRENCE')) { |
|
| 237 | - $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
| 238 | - $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
| 239 | - } |
|
| 240 | - $start->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 241 | - $end->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 242 | - |
|
| 243 | - $calendarEvent = $api->post("/calendar_events", |
|
| 244 | - array( |
|
| 245 | - 'calendar_event[context_code]' => "{$canvasContext['context']}_{$canvasObject['id']}", |
|
| 246 | - 'calendar_event[title]' => preg_replace('%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', '\\1', strip_tags($event->getProperty('SUMMARY'))), |
|
| 247 | - 'calendar_event[description]' => \Michelf\Markdown::defaultTransform(str_replace('\n', "\n\n", $event->getProperty('DESCRIPTION', 1))), |
|
| 248 | - 'calendar_event[start_at]' => $start->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 249 | - 'calendar_event[end_at]' => $end->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 250 | - 'calendar_event[location_name]' => $event->getProperty('LOCATION') |
|
| 251 | - ) |
|
| 252 | - ); |
|
| 253 | - |
|
| 254 | - $sql->query(" |
|
| 231 | + /* otherwise, add this new event and cache it */ |
|
| 232 | + } else { |
|
| 233 | + /* multi-day event instance start times need to be changed to _this_ date */ |
|
| 234 | + $start = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTSTART'))); |
|
| 235 | + $end = new DateTime(iCalUtilityFunctions::_date2strdate($event->getProperty('DTEND'))); |
|
| 236 | + if ($event->getProperty('X-RECURRENCE')) { |
|
| 237 | + $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
|
| 238 | + $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
|
| 239 | + } |
|
| 240 | + $start->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 241 | + $end->setTimeZone(new DateTimeZone(LOCAL_TIMEZONE)); |
|
| 242 | + |
|
| 243 | + $calendarEvent = $api->post("/calendar_events", |
|
| 244 | + array( |
|
| 245 | + 'calendar_event[context_code]' => "{$canvasContext['context']}_{$canvasObject['id']}", |
|
| 246 | + 'calendar_event[title]' => preg_replace('%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', '\\1', strip_tags($event->getProperty('SUMMARY'))), |
|
| 247 | + 'calendar_event[description]' => \Michelf\Markdown::defaultTransform(str_replace('\n', "\n\n", $event->getProperty('DESCRIPTION', 1))), |
|
| 248 | + 'calendar_event[start_at]' => $start->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 249 | + 'calendar_event[end_at]' => $end->format(CANVAS_TIMESTAMP_FORMAT), |
|
| 250 | + 'calendar_event[location_name]' => $event->getProperty('LOCATION') |
|
| 251 | + ) |
|
| 252 | + ); |
|
| 253 | + |
|
| 254 | + $sql->query(" |
|
| 255 | 255 | INSERT INTO `events` |
| 256 | 256 | ( |
| 257 | 257 | `calendar`, |
@@ -266,102 +266,102 @@ discard block |
||
| 266 | 266 | '" . getSyncTimestamp() . "' |
| 267 | 267 | ) |
| 268 | 268 | "); |
| 269 | - } |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /* clean out previously synced events that are no longer correct */ |
|
| 277 | - $deletedEventsResponse = $sql->query(" |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /* clean out previously synced events that are no longer correct */ |
|
| 277 | + $deletedEventsResponse = $sql->query(" |
|
| 278 | 278 | SELECT * FROM `events` |
| 279 | 279 | WHERE |
| 280 | 280 | `calendar` = '{$calendarCache['id']}' AND |
| 281 | 281 | `synced` != '" . getSyncTimestamp() . "' |
| 282 | 282 | "); |
| 283 | - while ($deletedEventCache = $deletedEventsResponse->fetch_assoc()) { |
|
| 284 | - try { |
|
| 285 | - $deletedEvent = $api->delete("/calendar_events/{$deletedEventCache['calendar_event[id]']}", |
|
| 286 | - array( |
|
| 287 | - 'cancel_reason' => getSyncTimestamp(), |
|
| 288 | - 'as_user_id' => ($canvasContext['context'] == 'user' ? $canvasObject['id'] : '') // TODO: this feels skeevy -- like the empty string will break |
|
| 289 | - ) |
|
| 290 | - ); |
|
| 291 | - } catch (Pest_Unauthorized $e) { |
|
| 292 | - /* if the event has been deleted in Canvas, we'll get an error when |
|
| 283 | + while ($deletedEventCache = $deletedEventsResponse->fetch_assoc()) { |
|
| 284 | + try { |
|
| 285 | + $deletedEvent = $api->delete("/calendar_events/{$deletedEventCache['calendar_event[id]']}", |
|
| 286 | + array( |
|
| 287 | + 'cancel_reason' => getSyncTimestamp(), |
|
| 288 | + 'as_user_id' => ($canvasContext['context'] == 'user' ? $canvasObject['id'] : '') // TODO: this feels skeevy -- like the empty string will break |
|
| 289 | + ) |
|
| 290 | + ); |
|
| 291 | + } catch (Pest_Unauthorized $e) { |
|
| 292 | + /* if the event has been deleted in Canvas, we'll get an error when |
|
| 293 | 293 | we try to delete it a second time. We still need to delete it from |
| 294 | 294 | our cache database, however */ |
| 295 | - postMessage('Cache out-of-sync', "calendar_event[{$deletedEventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", NotificationMessage::INFO); |
|
| 296 | - } catch (Pest_ClientError $e) { |
|
| 297 | - postMessage( |
|
| 298 | - 'API Client Error', |
|
| 299 | - '<pre>' . print_r(array( |
|
| 300 | - 'Status' => $PEST->lastStatus(), |
|
| 301 | - 'Error' => $PEST->lastBody(), |
|
| 302 | - 'Verb' => $verb, |
|
| 303 | - 'URL' => $url, |
|
| 304 | - 'Data' => $data |
|
| 305 | - ), false) . '</pre>', |
|
| 306 | - NotificationMessage::ERROR |
|
| 307 | - ); |
|
| 308 | - if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 309 | - exit; |
|
| 310 | - } |
|
| 311 | - $sql->query(" |
|
| 295 | + postMessage('Cache out-of-sync', "calendar_event[{$deletedEventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", NotificationMessage::INFO); |
|
| 296 | + } catch (Pest_ClientError $e) { |
|
| 297 | + postMessage( |
|
| 298 | + 'API Client Error', |
|
| 299 | + '<pre>' . print_r(array( |
|
| 300 | + 'Status' => $PEST->lastStatus(), |
|
| 301 | + 'Error' => $PEST->lastBody(), |
|
| 302 | + 'Verb' => $verb, |
|
| 303 | + 'URL' => $url, |
|
| 304 | + 'Data' => $data |
|
| 305 | + ), false) . '</pre>', |
|
| 306 | + NotificationMessage::ERROR |
|
| 307 | + ); |
|
| 308 | + if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 309 | + exit; |
|
| 310 | + } |
|
| 311 | + $sql->query(" |
|
| 312 | 312 | DELETE FROM `events` |
| 313 | 313 | WHERE |
| 314 | 314 | `id` = '{$deletedEventCache['id']}' |
| 315 | 315 | "); |
| 316 | - } |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - /* if this was a scheduled import (i.e. a sync), update that schedule */ |
|
| 319 | - if (isset($_REQUEST['schedule'])) { |
|
| 320 | - $sql->query(" |
|
| 318 | + /* if this was a scheduled import (i.e. a sync), update that schedule */ |
|
| 319 | + if (isset($_REQUEST['schedule'])) { |
|
| 320 | + $sql->query(" |
|
| 321 | 321 | UPDATE `schedules` |
| 322 | 322 | SET |
| 323 | 323 | `synced` = '" . getSyncTimestamp() . "' |
| 324 | 324 | WHERE |
| 325 | 325 | `id` = '{$_REQUEST['schedule']}' |
| 326 | 326 | "); |
| 327 | - } |
|
| 328 | - /* are we setting up a regular synchronization? */ |
|
| 329 | - if (isset($_REQUEST['sync']) && $_REQUEST['sync'] != SCHEDULE_ONCE) { |
|
| 327 | + } |
|
| 328 | + /* are we setting up a regular synchronization? */ |
|
| 329 | + if (isset($_REQUEST['sync']) && $_REQUEST['sync'] != SCHEDULE_ONCE) { |
|
| 330 | 330 | |
| 331 | - // FIXME:0 CRON SYNC SETUP GOES HERE issue:15 issue:13 |
|
| 331 | + // FIXME:0 CRON SYNC SETUP GOES HERE issue:15 issue:13 |
|
| 332 | 332 | |
| 333 | - /* add to the cache database schedule, replacing any schedules for this |
|
| 333 | + /* add to the cache database schedule, replacing any schedules for this |
|
| 334 | 334 | calendar that are already there */ |
| 335 | - $schedulesResponse = $sql->query(" |
|
| 335 | + $schedulesResponse = $sql->query(" |
|
| 336 | 336 | SELECT * |
| 337 | 337 | FROM `schedules` |
| 338 | 338 | WHERE |
| 339 | 339 | `calendar` = '{$calendarCache['id']}' |
| 340 | 340 | "); |
| 341 | 341 | |
| 342 | - if ($schedule = $schedulesResponse->fetch_assoc()) { |
|
| 342 | + if ($schedule = $schedulesResponse->fetch_assoc()) { |
|
| 343 | 343 | |
| 344 | - /* only need to worry if the cached schedule is different from the |
|
| 344 | + /* only need to worry if the cached schedule is different from the |
|
| 345 | 345 | new one we just set */ |
| 346 | - if ($shellArguments[INDEX_SCHEDULE] != $schedule['schedule']) { |
|
| 347 | - /* was this the last schedule to require this trigger? */ |
|
| 348 | - $schedulesResponse = $sql->query(" |
|
| 346 | + if ($shellArguments[INDEX_SCHEDULE] != $schedule['schedule']) { |
|
| 347 | + /* was this the last schedule to require this trigger? */ |
|
| 348 | + $schedulesResponse = $sql->query(" |
|
| 349 | 349 | SELECT * |
| 350 | 350 | FROM `schedules` |
| 351 | 351 | WHERE |
| 352 | 352 | `calendar` != '{$calendarCache['id']}' AND |
| 353 | 353 | `schedule` == '{$schedule['schedule']}' |
| 354 | 354 | "); |
| 355 | - /* we're the last one, delete it from crontab */ |
|
| 356 | - if ($schedulesResponse->num_rows == 0) { |
|
| 357 | - $crontabs = preg_replace("%^.*{$schedule['schedule']}.*" . PHP_EOL . '%', '', shell_exec('crontab -l')); |
|
| 358 | - $filename = md5(getSyncTimestamp()) . '.txt'; |
|
| 359 | - file_put_contents("/tmp/$filename", $crontabs); |
|
| 360 | - shell_exec("crontab /tmp/$filename"); |
|
| 361 | - postMessage('Unused schedule', "removed schedule '{$schedule['schedule']}' from crontab", NotificationMessage::INFO); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - $sql->query(" |
|
| 355 | + /* we're the last one, delete it from crontab */ |
|
| 356 | + if ($schedulesResponse->num_rows == 0) { |
|
| 357 | + $crontabs = preg_replace("%^.*{$schedule['schedule']}.*" . PHP_EOL . '%', '', shell_exec('crontab -l')); |
|
| 358 | + $filename = md5(getSyncTimestamp()) . '.txt'; |
|
| 359 | + file_put_contents("/tmp/$filename", $crontabs); |
|
| 360 | + shell_exec("crontab /tmp/$filename"); |
|
| 361 | + postMessage('Unused schedule', "removed schedule '{$schedule['schedule']}' from crontab", NotificationMessage::INFO); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + $sql->query(" |
|
| 365 | 365 | UPDATE `schedules` |
| 366 | 366 | SET |
| 367 | 367 | `schedule` = '" . $shellArguments[INDEX_SCHEDULE] . "', |
@@ -369,9 +369,9 @@ discard block |
||
| 369 | 369 | WHERE |
| 370 | 370 | `calendar` = '{$calendarCache['id']}' |
| 371 | 371 | "); |
| 372 | - } |
|
| 373 | - } else { |
|
| 374 | - $sql->query(" |
|
| 372 | + } |
|
| 373 | + } else { |
|
| 374 | + $sql->query(" |
|
| 375 | 375 | INSERT INTO `schedules` |
| 376 | 376 | ( |
| 377 | 377 | `calendar`, |
@@ -384,46 +384,46 @@ discard block |
||
| 384 | 384 | '" . getSyncTimestamp() . "' |
| 385 | 385 | ) |
| 386 | 386 | "); |
| 387 | - } |
|
| 388 | - } |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | 389 | |
| 390 | - /* if we're ovewriting data (for example, if this is a recurring sync, we |
|
| 390 | + /* if we're ovewriting data (for example, if this is a recurring sync, we |
|
| 391 | 391 | need to remove the events that were _not_ synced this in this round */ |
| 392 | - if (isset($_REQUEST['overwrite']) && $_REQUEST['overwrite'] == VALUE_OVERWRITE_CANVAS_CALENDAR) { |
|
| 393 | - // TODO: actually deal with this |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - // TODO: deal with messaging based on context |
|
| 397 | - |
|
| 398 | - postMessage('Finished sync', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 399 | - exit; |
|
| 400 | - } else { |
|
| 401 | - postMessage( |
|
| 402 | - 'Canvas Object Not Found', |
|
| 403 | - 'The object whose URL you submitted could not be found.<pre>' . print_r(array( |
|
| 404 | - 'Canvas URL' => $_REQUEST['canvas_url'], |
|
| 405 | - 'Canvas Context' => $canvasContext, |
|
| 406 | - 'Canvas Object' => $canvasObject |
|
| 407 | - ), false) . '</pre>', |
|
| 408 | - NotificationMessage::ERROR |
|
| 409 | - ); |
|
| 410 | - } |
|
| 411 | - } else { |
|
| 412 | - postMessage( |
|
| 413 | - 'ICS feed Not Found', |
|
| 414 | - 'The calendar whose URL you submitted could not be found.<pre>' . $_REQUEST['cal'] . '</pre>', |
|
| 415 | - NotificationMessage::ERROR |
|
| 416 | - ); |
|
| 417 | - } |
|
| 418 | - } else { |
|
| 419 | - postMessage( |
|
| 420 | - 'Invalid Canvas URL', |
|
| 421 | - 'The Canvas URL you submitted could not be parsed.<pre>' . $_REQUEST['canvas_url'] . '</pre>', |
|
| 422 | - NotificationMessage::ERROR |
|
| 423 | - ); |
|
| 424 | - if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 425 | - exit; |
|
| 426 | - } |
|
| 392 | + if (isset($_REQUEST['overwrite']) && $_REQUEST['overwrite'] == VALUE_OVERWRITE_CANVAS_CALENDAR) { |
|
| 393 | + // TODO: actually deal with this |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + // TODO: deal with messaging based on context |
|
| 397 | + |
|
| 398 | + postMessage('Finished sync', getSyncTimestamp(), NotificationMessage::INFO); |
|
| 399 | + exit; |
|
| 400 | + } else { |
|
| 401 | + postMessage( |
|
| 402 | + 'Canvas Object Not Found', |
|
| 403 | + 'The object whose URL you submitted could not be found.<pre>' . print_r(array( |
|
| 404 | + 'Canvas URL' => $_REQUEST['canvas_url'], |
|
| 405 | + 'Canvas Context' => $canvasContext, |
|
| 406 | + 'Canvas Object' => $canvasObject |
|
| 407 | + ), false) . '</pre>', |
|
| 408 | + NotificationMessage::ERROR |
|
| 409 | + ); |
|
| 410 | + } |
|
| 411 | + } else { |
|
| 412 | + postMessage( |
|
| 413 | + 'ICS feed Not Found', |
|
| 414 | + 'The calendar whose URL you submitted could not be found.<pre>' . $_REQUEST['cal'] . '</pre>', |
|
| 415 | + NotificationMessage::ERROR |
|
| 416 | + ); |
|
| 417 | + } |
|
| 418 | + } else { |
|
| 419 | + postMessage( |
|
| 420 | + 'Invalid Canvas URL', |
|
| 421 | + 'The Canvas URL you submitted could not be parsed.<pre>' . $_REQUEST['canvas_url'] . '</pre>', |
|
| 422 | + NotificationMessage::ERROR |
|
| 423 | + ); |
|
| 424 | + if (php_sapi_name() != 'cli') $smarty->display('page.tpl'); |
|
| 425 | + exit; |
|
| 426 | + } |
|
| 427 | 427 | } |
| 428 | 428 | |
| 429 | 429 | ?> |