blti.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. require_once 'OAuth.php';
  3. require_once 'TrivialOAuthDataStore.php';
  4. // Returns true if this is a Basic LTI message
  5. // with minimum values to meet the protocol
  6. function is_basic_lti_request() {
  7. $good_message_type = $_REQUEST["lti_message_type"] == "basic-lti-launch-request";
  8. $good_lti_version = ($_REQUEST["lti_version"] == "LTI-1p0") || ($_REQUEST["lti_version"] == 'LTI-1.0');
  9. $resource_link_id = $_REQUEST["resource_link_id"];
  10. if ($good_message_type and $good_lti_version and isset($resource_link_id) ) return(true);
  11. return false;
  12. }
  13. // Basic LTI Class that does the setup and provides utility
  14. // functions
  15. class BLTI {
  16. public $valid = false;
  17. public $complete = false;
  18. public $message = false;
  19. public $basestring = false;
  20. public $info = false;
  21. public $row = false;
  22. public $context_id = false; // Override context_id
  23. function __construct($parm=false, $usesession=true, $doredirect=true) {
  24. // If this request is not an LTI Launch, either
  25. // give up or try to retrieve the context from session
  26. if ( ! is_basic_lti_request() ) {
  27. $this->message = "Not a basic LTI request";
  28. return;
  29. if ( $usesession === false ) return;
  30. if ( strlen(session_id()) > 0 ) {
  31. $row = $_SESSION['_basiclti_lti_row'];
  32. if ( isset($row) ) $this->row = $row;
  33. $context_id = $_SESSION['_basiclti_lti_context_id'];
  34. if ( isset($context_id) ) $this->context_id = $context_id;
  35. $info = $_SESSION['_basic_lti_context'];
  36. if ( isset($info) ) {
  37. $this->info = $info;
  38. $this->valid = true;
  39. return;
  40. }
  41. $this->message = "Could not find context in session";
  42. return;
  43. }
  44. $this->message = "Session not available";
  45. return;
  46. }
  47. // Insure we have a valid launch
  48. if ( empty($_REQUEST["oauth_consumer_key"]) ) {
  49. $this->message = "Missing oauth_consumer_key in request";
  50. return;
  51. }
  52. $oauth_consumer_key = $_REQUEST["oauth_consumer_key"];
  53. // Find the secret - either form the parameter as a string or
  54. // look it up in a database from parameters we are given
  55. $secret = false;
  56. $row = false;
  57. if ( is_string($parm) ) {
  58. $secret = $parm;
  59. } else if ( ! is_array($parm) ) {
  60. $this->message = "Constructor requires a secret or database information.";
  61. return;
  62. } else {
  63. $sql = 'SELECT * FROM '.$parm['table'].' WHERE '.
  64. ($parm['key_column'] ? $parm['key_column'] : 'oauth_consumer_key').
  65. '='.
  66. "'".mysql_real_escape_string($oauth_consumer_key)."'";
  67. $result = mysql_query($sql);
  68. $num_rows = mysql_num_rows($result);
  69. if ( $num_rows != 1 ) {
  70. $this->message = "Your consumer is not authorized oauth_consumer_key=".$oauth_consumer_key;
  71. return;
  72. } else {
  73. while ($row = mysql_fetch_assoc($result)) {
  74. $secret = $row[$parms['secret_column']?$parms['secret_column']:'secret'];
  75. $context_id = $row[$parms['context_column']?$parms['context_column']:'context_id'];
  76. if ( $context_id ) $this->context_id = $context_id;
  77. $this->row = $row;
  78. break;
  79. }
  80. if ( ! is_string($secret) ) {
  81. $this->message = "Could not retrieve secret oauth_consumer_key=".$oauth_consumer_key;
  82. return;
  83. }
  84. }
  85. }
  86. // Verify the message signature
  87. $store = new TrivialOAuthDataStore();
  88. $store->add_consumer($oauth_consumer_key, $secret);
  89. $server = new OAuthServer($store);
  90. $method = new OAuthSignatureMethod_HMAC_SHA1();
  91. $server->add_signature_method($method);
  92. $request = OAuthRequest::from_request();
  93. $this->basestring = $request->get_signature_base_string();
  94. try {
  95. $server->verify_request($request);
  96. $this->valid = true;
  97. } catch (Exception $e) {
  98. $this->message = $e->getMessage();
  99. return;
  100. }
  101. // Store the launch information in the session for later
  102. $newinfo = array();
  103. foreach($_POST as $key => $value ) {
  104. if ( $key == "basiclti_submit" ) continue;
  105. if ( strpos($key, "oauth_") === false ) {
  106. $newinfo[$key] = $value;
  107. continue;
  108. }
  109. if ( $key == "oauth_consumer_key" ) {
  110. $newinfo[$key] = $value;
  111. continue;
  112. }
  113. }
  114. $this->info = $newinfo;
  115. if ( $usesession == true and strlen(session_id()) > 0 ) {
  116. $_SESSION['_basic_lti_context'] = $this->info;
  117. unset($_SESSION['_basiclti_lti_row']);
  118. unset($_SESSION['_basiclti_lti_context_id']);
  119. if ( $this->row ) $_SESSION['_basiclti_lti_row'] = $this->row;
  120. if ( $this->context_id ) $_SESSION['_basiclti_lti_context_id'] = $this->context_id;
  121. }
  122. if ( $this->valid && $doredirect ) {
  123. $this->redirect();
  124. $this->complete = true;
  125. }
  126. }
  127. function addSession($location) {
  128. if ( ini_get('session.use_cookies') == 0 ) {
  129. if ( strpos($location,'?') > 0 ) {
  130. $location = $location . '&';
  131. } else {
  132. $location = $location . '?';
  133. }
  134. $location = $location . session_name() . '=' . session_id();
  135. }
  136. return $location;
  137. }
  138. function isInstructor() {
  139. $roles = $this->info['roles'];
  140. $roles = strtolower($roles);
  141. if ( ! ( strpos($roles,"instructor") === false ) ) return true;
  142. if ( ! ( strpos($roles,"administrator") === false ) ) return true;
  143. return false;
  144. }
  145. function getUserEmail() {
  146. $email = $this->info['lis_person_contact_email_primary'];
  147. if ( strlen($email) > 0 ) return $email;
  148. # Sakai Hack
  149. $email = $this->info['lis_person_contact_emailprimary'];
  150. if ( strlen($email) > 0 ) return $email;
  151. return false;
  152. }
  153. function getUserShortName() {
  154. $email = $this->getUserEmail();
  155. $givenname = $this->info['lis_person_name_given'];
  156. $familyname = $this->info['lis_person_name_family'];
  157. $fullname = $this->info['lis_person_name_full'];
  158. if ( strlen($email) > 0 ) return $email;
  159. if ( strlen($givenname) > 0 ) return $givenname;
  160. if ( strlen($familyname) > 0 ) return $familyname;
  161. return $this->getUserName();
  162. }
  163. function getUserName() {
  164. $givenname = $this->info['lis_person_name_given'];
  165. $familyname = $this->info['lis_person_name_family'];
  166. $fullname = $this->info['lis_person_name_full'];
  167. if ( strlen($fullname) > 0 ) return $fullname;
  168. if ( strlen($familyname) > 0 and strlen($givenname) > 0 ) return $givenname + $familyname;
  169. if ( strlen($givenname) > 0 ) return $givenname;
  170. if ( strlen($familyname) > 0 ) return $familyname;
  171. return $this->getUserEmail();
  172. }
  173. function getUserKey() {
  174. $oauth = $this->info['oauth_consumer_key'];
  175. $id = $this->info['user_id'];
  176. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  177. return false;
  178. }
  179. function getUserImage() {
  180. $image = $this->info['user_image'];
  181. if ( strlen($image) > 0 ) return $image;
  182. $email = $this->getUserEmail();
  183. if ( $email === false ) return false;
  184. $size = 40;
  185. $grav_url = $_SERVER['HTTPS'] ? 'https://' : 'http://';
  186. $grav_url = $grav_url . "www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&size=".$size;
  187. return $grav_url;
  188. }
  189. function getResourceKey() {
  190. $oauth = $this->info['oauth_consumer_key'];
  191. $id = $this->info['resource_link_id'];
  192. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  193. return false;
  194. }
  195. function getResourceTitle() {
  196. $title = $this->info['resource_link_title'];
  197. if ( strlen($title) > 0 ) return $title;
  198. return false;
  199. }
  200. function getConsumerKey() {
  201. $oauth = $this->info['oauth_consumer_key'];
  202. return $oauth;
  203. }
  204. function getCourseKey() {
  205. if ( $this->context_id ) return $this->context_id;
  206. $oauth = $this->info['oauth_consumer_key'];
  207. $id = $this->info['context_id'];
  208. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  209. return false;
  210. }
  211. function getCourseName() {
  212. $label = $this->info['context_label'];
  213. $title = $this->info['context_title'];
  214. $id = $this->info['context_id'];
  215. if ( strlen($label) > 0 ) return $label;
  216. if ( strlen($title) > 0 ) return $title;
  217. if ( strlen($id) > 0 ) return $id;
  218. return false;
  219. }
  220. // TODO: Add javasript version if headers are already sent
  221. function redirect() {
  222. $host = $_SERVER['HTTP_HOST'];
  223. $uri = $_SERVER['PHP_SELF'];
  224. $location = $_SERVER['HTTPS'] ? 'https://' : 'http://';
  225. $location = $location . $host . $uri;
  226. $location = $this->addSession($location);
  227. header("Location: $location");
  228. }
  229. function dump() {
  230. if ( ! $this->valid or $this->info == false ) return "Context not valid\n";
  231. $ret = "";
  232. if ( $this->isInstructor() ) {
  233. $ret .= "isInstructor() = true\n";
  234. } else {
  235. $ret .= "isInstructor() = false\n";
  236. }
  237. $ret .= "getUserKey() = ".$this->getUserKey()."\n";
  238. $ret .= "getUserEmail() = ".$this->getUserEmail()."\n";
  239. $ret .= "getUserShortName() = ".$this->getUserShortName()."\n";
  240. $ret .= "getUserName() = ".$this->getUserName()."\n";
  241. $ret .= "getUserImage() = ".$this->getUserImage()."\n";
  242. $ret .= "getResourceKey() = ".$this->getResourceKey()."\n";
  243. $ret .= "getResourceTitle() = ".$this->getResourceTitle()."\n";
  244. $ret .= "getCourseName() = ".$this->getCourseName()."\n";
  245. $ret .= "getCourseKey() = ".$this->getCourseKey()."\n";
  246. $ret .= "getConsumerKey() = ".$this->getConsumerKey()."\n";
  247. return $ret;
  248. }
  249. }
  250. ?>