3 require_once 'OAuth.php';
5 // Returns true if this is a Basic LTI message
6 // with minimum values to meet the protocol
7 function is_basic_lti_request() {
8 $good_message_type = $_REQUEST["lti_message_type"] == "basic-lti-launch-request";
9 $good_lti_version = $_REQUEST["lti_version"] == "LTI-1p0";
10 $resource_link_id = $_REQUEST["resource_link_id"];
11 if ($good_message_type and $good_lti_version and isset($resource_link_id) ) return(true);
16 * A Trivial memory-based store - no support for tokens
18 class TrivialOAuthDataStore extends OAuthDataStore {
19 private $consumers = array();
21 function add_consumer($consumer_key, $consumer_secret) {
22 $this->consumers[$consumer_key] = $consumer_secret;
25 function lookup_consumer($consumer_key) {
26 if ( strpos($consumer_key, "http://" ) === 0 ) {
27 $consumer = new OAuthConsumer($consumer_key,"secret", NULL);
30 if ( $this->consumers[$consumer_key] ) {
31 $consumer = new OAuthConsumer($consumer_key,$this->consumers[$consumer_key], NULL);
37 function lookup_token($consumer, $token_type, $token) {
38 return new OAuthToken($consumer, "");
41 // Return NULL if the nonce has not been used
42 // Return $nonce if the nonce was previously used
43 function lookup_nonce($consumer, $token, $nonce, $timestamp) {
44 // Should add some clever logic to keep nonces from
45 // being reused - for no we are really trusting
46 // that the timestamp will save us
50 function new_request_token($consumer) {
54 function new_access_token($token, $consumer) {
60 // Basic LTI Class that does the setup and provides utility
64 public $valid = false;
65 public $complete = false;
66 public $message = false;
67 public $basestring = false;
70 public $context_id = false; // Override context_id
72 function __construct($parm=false, $usesession=true, $doredirect=true) {
74 // If this request is not an LTI Launch, either
75 // give up or try to retrieve the context from session
76 if ( ! is_basic_lti_request() ) {
77 if ( $usesession === false ) return;
78 if ( strlen(session_id()) > 0 ) {
79 $row = $_SESSION['_basiclti_lti_row'];
80 if ( isset($row) ) $this->row = $row;
81 $context_id = $_SESSION['_basiclti_lti_context_id'];
82 if ( isset($context_id) ) $this->context_id = $context_id;
83 $info = $_SESSION['_basic_lti_context'];
89 $this->message = "Could not find context in session";
92 $this->message = "Session not available";
96 // Insure we have a valid launch
97 if ( empty($_REQUEST["oauth_consumer_key"]) ) {
98 $this->message = "Missing oauth_consumer_key in request";
101 $oauth_consumer_key = $_REQUEST["oauth_consumer_key"];
103 // Find the secret - either form the parameter as a string or
104 // look it up in a database from parameters we are given
107 if ( is_string($parm) ) {
109 } else if ( ! is_array($parm) ) {
110 $this->message = "Constructor requires a secret or database information.";
113 $sql = 'SELECT * FROM '.$parm['table'].' WHERE '.
114 ($parm['key_column'] ? $parm['key_column'] : 'oauth_consumer_key').
116 "'".mysql_real_escape_string($oauth_consumer_key)."'";
117 $result = mysql_query($sql);
118 $num_rows = mysql_num_rows($result);
119 if ( $num_rows != 1 ) {
120 $this->message = "Your consumer is not authorized oauth_consumer_key=".$oauth_consumer_key;
123 while ($row = mysql_fetch_assoc($result)) {
124 $secret = $row[$parms['secret_column']?$parms['secret_column']:'secret'];
125 $context_id = $row[$parms['context_column']?$parms['context_column']:'context_id'];
126 if ( $context_id ) $this->context_id = $context_id;
130 if ( ! is_string($secret) ) {
131 $this->message = "Could not retrieve secret oauth_consumer_key=".$oauth_consumer_key;
137 // Verify the message signature
138 $store = new TrivialOAuthDataStore();
139 $store->add_consumer($oauth_consumer_key, $secret);
141 $server = new OAuthServer($store);
143 $method = new OAuthSignatureMethod_HMAC_SHA1();
144 $server->add_signature_method($method);
145 $request = OAuthRequest::from_request();
147 $this->basestring = $request->get_signature_base_string();
150 $server->verify_request($request);
152 } catch (Exception $e) {
153 $this->message = $e->getMessage();
157 // Store the launch information in the session for later
159 foreach($_POST as $key => $value ) {
160 if ( $key == "basiclti_submit" ) continue;
161 if ( strpos($key, "oauth_") === false ) {
162 $newinfo[$key] = $value;
165 if ( $key == "oauth_consumer_key" ) {
166 $newinfo[$key] = $value;
171 $this->info = $newinfo;
172 if ( $usesession == true and strlen(session_id()) > 0 ) {
173 $_SESSION['_basic_lti_context'] = $this->info;
174 unset($_SESSION['_basiclti_lti_row']);
175 unset($_SESSION['_basiclti_lti_context_id']);
176 if ( $this->row ) $_SESSION['_basiclti_lti_row'] = $this->row;
177 if ( $this->context_id ) $_SESSION['_basiclti_lti_context_id'] = $this->context_id;
180 if ( $this->valid && $doredirect ) {
182 $this->complete = true;
186 function addSession($location) {
187 if ( ini_get('session.use_cookies') == 0 ) {
188 if ( strpos($location,'?') > 0 ) {
189 $location = $location . '&';
191 $location = $location . '?';
193 $location = $location . session_name() . '=' . session_id();
198 function isInstructor() {
199 $roles = $this->info['roles'];
200 $roles = strtolower($roles);
201 if ( ! ( strpos($roles,"instructor") === false ) ) return true;
202 if ( ! ( strpos($roles,"administrator") === false ) ) return true;
206 function getUserEmail() {
207 $email = $this->info['lis_person_contact_email_primary'];
208 if ( strlen($email) > 0 ) return $email;
210 $email = $this->info['lis_person_contact_emailprimary'];
211 if ( strlen($email) > 0 ) return $email;
215 function getUserShortName() {
216 $email = $this->getUserEmail();
217 $givenname = $this->info['lis_person_name_given'];
218 $familyname = $this->info['lis_person_name_family'];
219 $fullname = $this->info['lis_person_name_full'];
220 if ( strlen($email) > 0 ) return $email;
221 if ( strlen($givenname) > 0 ) return $givenname;
222 if ( strlen($familyname) > 0 ) return $familyname;
223 return $this->getUserName();
226 function getUserName() {
227 $givenname = $this->info['lis_person_name_given'];
228 $familyname = $this->info['lis_person_name_family'];
229 $fullname = $this->info['lis_person_name_full'];
230 if ( strlen($fullname) > 0 ) return $fullname;
231 if ( strlen($familyname) > 0 and strlen($givenname) > 0 ) return $givenname + $familyname;
232 if ( strlen($givenname) > 0 ) return $givenname;
233 if ( strlen($familyname) > 0 ) return $familyname;
234 return $this->getUserEmail();
237 function getUserKey() {
238 $oauth = $this->info['oauth_consumer_key'];
239 $id = $this->info['user_id'];
240 if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
244 function getUserImage() {
245 $image = $this->info['user_image'];
246 if ( strlen($image) > 0 ) return $image;
247 $email = $this->getUserEmail();
248 if ( $email === false ) return false;
250 $grav_url = $_SERVER['HTTPS'] ? 'https://' : 'http://';
251 $grav_url = $grav_url . "www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&size=".$size;
255 function getResourceKey() {
256 $oauth = $this->info['oauth_consumer_key'];
257 $id = $this->info['resource_link_id'];
258 if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
262 function getResourceTitle() {
263 $title = $this->info['resource_link_title'];
264 if ( strlen($title) > 0 ) return $title;
268 function getConsumerKey() {
269 $oauth = $this->info['oauth_consumer_key'];
273 function getCourseKey() {
274 if ( $this->context_id ) return $this->context_id;
275 $oauth = $this->info['oauth_consumer_key'];
276 $id = $this->info['context_id'];
277 if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
281 function getCourseName() {
282 $label = $this->info['context_label'];
283 $title = $this->info['context_title'];
284 $id = $this->info['context_id'];
285 if ( strlen($label) > 0 ) return $label;
286 if ( strlen($title) > 0 ) return $title;
287 if ( strlen($id) > 0 ) return $id;
291 // TODO: Add javasript version if headers are already sent
292 function redirect() {
293 $host = $_SERVER['HTTP_HOST'];
294 $uri = $_SERVER['PHP_SELF'];
295 $location = $_SERVER['HTTPS'] ? 'https://' : 'http://';
296 $location = $location . $host . $uri;
297 $location = $this->addSession($location);
298 header("Location: $location");
302 if ( ! $this->valid or $this->info == false ) return "Context not valid\n";
304 if ( $this->isInstructor() ) {
305 $ret .= "isInstructor() = true\n";
307 $ret .= "isInstructor() = false\n";
309 $ret .= "getUserKey() = ".$this->getUserKey()."\n";
310 $ret .= "getUserEmail() = ".$this->getUserEmail()."\n";
311 $ret .= "getUserShortName() = ".$this->getUserShortName()."\n";
312 $ret .= "getUserName() = ".$this->getUserName()."\n";
313 $ret .= "getUserImage() = ".$this->getUserImage()."\n";
314 $ret .= "getResourceKey() = ".$this->getResourceKey()."\n";
315 $ret .= "getResourceTitle() = ".$this->getResourceTitle()."\n";
316 $ret .= "getCourseName() = ".$this->getCourseName()."\n";
317 $ret .= "getCourseKey() = ".$this->getCourseKey()."\n";
318 $ret .= "getConsumerKey() = ".$this->getConsumerKey()."\n";