remove old readme
[atutor.git] / docs / mods / _standard / basiclti / launch / ims-blti / blti.php
1 <?php
2
3 require_once 'OAuth.php';
4
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);
12    return false;
13 }
14
15 /**
16  * A Trivial memory-based store - no support for tokens
17  */
18 class TrivialOAuthDataStore extends OAuthDataStore {
19     private $consumers = array();
20
21     function add_consumer($consumer_key, $consumer_secret) {
22         $this->consumers[$consumer_key] = $consumer_secret;
23     }
24
25     function lookup_consumer($consumer_key) {
26         if ( strpos($consumer_key, "http://" ) === 0 ) {
27             $consumer = new OAuthConsumer($consumer_key,"secret", NULL);
28             return $consumer;
29         }
30         if ( $this->consumers[$consumer_key] ) {
31             $consumer = new OAuthConsumer($consumer_key,$this->consumers[$consumer_key], NULL);
32             return $consumer;
33         }
34         return NULL;
35     }
36
37     function lookup_token($consumer, $token_type, $token) {
38         return new OAuthToken($consumer, "");
39     }
40
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
47         return NULL;
48     }
49
50     function new_request_token($consumer) {
51         return NULL;
52     }
53
54     function new_access_token($token, $consumer) {
55         return NULL;
56     }
57 }
58
59
60 // Basic LTI Class that does the setup and provides utility
61 // functions
62 class BLTI {
63
64     public $valid = false;
65     public $complete = false;
66     public $message = false;
67     public $basestring = false;
68     public $info = false;
69     public $row = false;
70     public $context_id = false;  // Override context_id
71
72     function __construct($parm=false, $usesession=true, $doredirect=true) {
73
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'];
84                 if ( isset($info) ) {
85                     $this->info = $info;
86                     $this->valid = true;
87                     return;
88                 }
89                 $this->message = "Could not find context in session";
90                 return;
91             }
92             $this->message = "Session not available";
93             return;
94         }
95
96         // Insure we have a valid launch
97         if ( empty($_REQUEST["oauth_consumer_key"]) ) {
98             $this->message = "Missing oauth_consumer_key in request";
99             return;
100         }
101         $oauth_consumer_key = $_REQUEST["oauth_consumer_key"];
102
103         // Find the secret - either form the parameter as a string or
104         // look it up in a database from parameters we are given
105         $secret = false;
106         $row = false;
107         if ( is_string($parm) ) {
108             $secret = $parm;
109         } else if ( ! is_array($parm) ) {
110             $this->message = "Constructor requires a secret or database information.";
111             return;
112         } else {
113             $sql = 'SELECT * FROM '.$parm['table'].' WHERE '.
114                 ($parm['key_column'] ? $parm['key_column'] : 'oauth_consumer_key').
115                 '='.
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;
121                 return;
122             } else {
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;
127                     $this->row = $row;
128                     break;
129                 }
130                 if ( ! is_string($secret) ) {
131                     $this->message = "Could not retrieve secret oauth_consumer_key=".$oauth_consumer_key;
132                     return;
133                 }
134             }
135         }
136
137         // Verify the message signature
138         $store = new TrivialOAuthDataStore();
139         $store->add_consumer($oauth_consumer_key, $secret);
140
141         $server = new OAuthServer($store);
142
143         $method = new OAuthSignatureMethod_HMAC_SHA1();
144         $server->add_signature_method($method);
145         $request = OAuthRequest::from_request();
146         
147         $this->basestring = $request->get_signature_base_string();
148
149         try {
150             $server->verify_request($request);
151             $this->valid = true;
152         } catch (Exception $e) {
153             $this->message = $e->getMessage();
154             return;
155         }
156
157         // Store the launch information in the session for later
158         $newinfo = array();
159         foreach($_POST as $key => $value ) {
160             if ( $key == "basiclti_submit" ) continue;
161             if ( strpos($key, "oauth_") === false ) {
162                 $newinfo[$key] = $value;
163                 continue;
164             }
165             if ( $key == "oauth_consumer_key" ) {
166                 $newinfo[$key] = $value;
167                 continue;
168             }
169         }
170
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;
178         }
179
180         if ( $this->valid && $doredirect ) {
181             $this->redirect();
182             $this->complete = true;
183         }
184     }
185
186     function addSession($location) {
187         if ( ini_get('session.use_cookies') == 0 ) {
188             if ( strpos($location,'?') > 0 ) {
189                $location = $location . '&';
190             } else {
191                $location = $location . '?';
192             }
193             $location = $location . session_name() . '=' . session_id();
194         }
195         return $location;
196     }
197
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;
203         return false;
204     }
205
206     function getUserEmail() {
207         $email = $this->info['lis_person_contact_email_primary'];
208         if ( strlen($email) > 0 ) return $email;
209         # Sakai Hack
210         $email = $this->info['lis_person_contact_emailprimary'];
211         if ( strlen($email) > 0 ) return $email;
212         return false;
213     }
214
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();
224     }
225   
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();
235     }
236
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;
241         return false;
242     }
243
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;
249         $size = 40;
250         $grav_url = $_SERVER['HTTPS'] ? 'https://' : 'http://';
251         $grav_url = $grav_url . "www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&size=".$size;
252         return $grav_url;
253     }
254
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;
259         return false;
260     }
261
262     function getResourceTitle() {
263         $title = $this->info['resource_link_title'];
264         if ( strlen($title) > 0 ) return $title;
265         return false;
266     }
267
268     function getConsumerKey() {
269         $oauth = $this->info['oauth_consumer_key'];
270         return $oauth;
271     }
272
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;
278         return false;
279     }
280
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;
288         return false;
289     }
290
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");
299     }
300
301     function dump() { 
302         if ( ! $this->valid or $this->info == false ) return "Context not valid\n";
303         $ret = "";
304         if ( $this->isInstructor() ) {
305             $ret .= "isInstructor() = true\n";
306         } else {
307             $ret .= "isInstructor() = false\n";
308         }
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";
319         return $ret;
320     }
321
322 }
323
324 ?>