PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/lti/ims-blti/blti.php

https://github.com/pauln/moodle
PHP | 277 lines | 222 code | 34 blank | 21 comment | 57 complexity | 1210121969364c8438e219f993a40ae4 MD5 | raw file
  1. <?php
  2. require_once($CFG->dirroot . '/enrol/lti/ims-blti/OAuth.php');
  3. require_once($CFG->dirroot . '/enrol/lti/ims-blti/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" or $_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. if ( $usesession === false ) return;
  28. if ( strlen(session_id()) > 0 ) {
  29. $row = $_SESSION['_basiclti_lti_row'];
  30. if ( isset($row) ) $this->row = $row;
  31. $context_id = $_SESSION['_basiclti_lti_context_id'];
  32. if ( isset($context_id) ) $this->context_id = $context_id;
  33. $info = $_SESSION['_basic_lti_context'];
  34. if ( isset($info) ) {
  35. $this->info = $info;
  36. $this->valid = true;
  37. return;
  38. }
  39. $this->message = "Could not find context in session";
  40. return;
  41. }
  42. $this->message = "Session not available";
  43. return;
  44. }
  45. // Insure we have a valid launch
  46. if ( empty($_REQUEST["oauth_consumer_key"]) ) {
  47. $this->message = "Missing oauth_consumer_key in request";
  48. return;
  49. }
  50. $oauth_consumer_key = $_REQUEST["oauth_consumer_key"];
  51. // Find the secret - either form the parameter as a string or
  52. // look it up in a database from parameters we are given
  53. $secret = false;
  54. $row = false;
  55. if ( is_string($parm) ) {
  56. $secret = $parm;
  57. } else if ( ! is_array($parm) ) {
  58. $this->message = "Constructor requires a secret or database information.";
  59. return;
  60. }
  61. // Verify the message signature
  62. $store = new TrivialOAuthDataStore();
  63. $store->add_consumer($oauth_consumer_key, $secret);
  64. $server = new OAuthServer($store);
  65. $method = new OAuthSignatureMethod_HMAC_SHA1();
  66. $server->add_signature_method($method);
  67. $request = OAuthRequest::from_request();
  68. $this->basestring = $request->get_signature_base_string();
  69. try {
  70. $server->verify_request($request);
  71. $this->valid = true;
  72. } catch (Exception $e) {
  73. $this->message = $e->getMessage();
  74. return;
  75. }
  76. // Store the launch information in the session for later
  77. $newinfo = array();
  78. foreach($_POST as $key => $value ) {
  79. if ( $key == "basiclti_submit" ) continue;
  80. if ( strpos($key, "oauth_") === false ) {
  81. $newinfo[$key] = $value;
  82. continue;
  83. }
  84. if ( $key == "oauth_consumer_key" ) {
  85. $newinfo[$key] = $value;
  86. continue;
  87. }
  88. }
  89. //Added abertranb to decode base 64 20120801
  90. if (isset($newinfo['custom_lti_message_encoded_base64']) && $newinfo['custom_lti_message_encoded_base64']==1){
  91. $newinfo = $this->decodeBase64($newinfo);
  92. }
  93. $this->info = $newinfo;
  94. if ( $usesession == true and strlen(session_id()) > 0 ) {
  95. $_SESSION['_basic_lti_context'] = $this->info;
  96. unset($_SESSION['_basiclti_lti_row']);
  97. unset($_SESSION['_basiclti_lti_context_id']);
  98. if ( $this->row ) $_SESSION['_basiclti_lti_row'] = $this->row;
  99. if ( $this->context_id ) $_SESSION['_basiclti_lti_context_id'] = $this->context_id;
  100. }
  101. if ( $this->valid && $doredirect ) {
  102. $this->redirect();
  103. $this->complete = true;
  104. }
  105. }
  106. function addSession($location) {
  107. if ( ini_get('session.use_cookies') == 0 ) {
  108. if ( strpos($location,'?') > 0 ) {
  109. $location = $location . '&';
  110. } else {
  111. $location = $location . '?';
  112. }
  113. $location = $location . session_name() . '=' . session_id();
  114. }
  115. return $location;
  116. }
  117. function isInstructor() {
  118. $roles = $this->info['roles'];
  119. $roles = strtolower($roles);
  120. if ( ! ( strpos($roles,"instructor") === false ) ) return true;
  121. if ( ! ( strpos($roles,"administrator") === false ) ) return true;
  122. return false;
  123. }
  124. function getUserEmail() {
  125. # set default email in the event privacy settings don't pass in email.
  126. $email = $this->info['user_id'] . "@ltiuser.com";
  127. if ( isset($this->info['lis_person_contact_email_primary']) ) $email = $this->info['lis_person_contact_email_primary'];
  128. # Sakai Hack
  129. if ( isset($this->info['lis_person_contact_emailprimary']) ) $email = $this->info['lis_person_contact_emailprimary'];
  130. return $email;
  131. }
  132. function getUserShortName() {
  133. $email = $this->getUserEmail();
  134. $givenname = $this->info['lis_person_name_given'];
  135. $familyname = $this->info['lis_person_name_family'];
  136. $fullname = $this->info['lis_person_name_full'];
  137. if ( strlen($email) > 0 ) return $email;
  138. if ( strlen($givenname) > 0 ) return $givenname;
  139. if ( strlen($familyname) > 0 ) return $familyname;
  140. return $this->getUserName();
  141. }
  142. function getUserName() {
  143. $givenname = $this->info['lis_person_name_given'];
  144. $familyname = $this->info['lis_person_name_family'];
  145. $fullname = $this->info['lis_person_name_full'];
  146. if ( strlen($fullname) > 0 ) return $fullname;
  147. if ( strlen($familyname) > 0 and strlen($givenname) > 0 ) return $givenname + $familyname;
  148. if ( strlen($givenname) > 0 ) return $givenname;
  149. if ( strlen($familyname) > 0 ) return $familyname;
  150. return $this->getUserEmail();
  151. }
  152. function getUserKey() {
  153. $oauth = $this->info['oauth_consumer_key'];
  154. $id = $this->info['user_id'];
  155. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  156. return false;
  157. }
  158. function getUserImage() {
  159. $image = $this->info['user_image'];
  160. if ( strlen($image) > 0 ) return $image;
  161. $email = $this->getUserEmail();
  162. if ( $email === false ) return false;
  163. $size = 40;
  164. $grav_url = $_SERVER['HTTPS'] ? 'https://' : 'http://';
  165. $grav_url = $grav_url . "www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&size=".$size;
  166. return $grav_url;
  167. }
  168. function getResourceKey() {
  169. $oauth = $this->info['oauth_consumer_key'];
  170. $id = $this->info['resource_link_id'];
  171. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  172. return false;
  173. }
  174. function getResourceTitle() {
  175. $title = $this->info['resource_link_title'];
  176. if ( strlen($title) > 0 ) return $title;
  177. return false;
  178. }
  179. function getConsumerKey() {
  180. $oauth = $this->info['oauth_consumer_key'];
  181. return $oauth;
  182. }
  183. function getCourseKey() {
  184. if ( $this->context_id ) return $this->context_id;
  185. $oauth = $this->info['oauth_consumer_key'];
  186. $id = $this->info['context_id'];
  187. if ( strlen($id) > 0 and strlen($oauth) > 0 ) return $oauth . ':' . $id;
  188. return false;
  189. }
  190. function getCourseName() {
  191. $label = $this->info['context_label'];
  192. $title = $this->info['context_title'];
  193. $id = $this->info['context_id'];
  194. if ( strlen($label) > 0 ) return $label;
  195. if ( strlen($title) > 0 ) return $title;
  196. if ( strlen($id) > 0 ) return $id;
  197. return false;
  198. }
  199. // TODO: Add javasript version if headers are already sent
  200. function redirect() {
  201. $host = $_SERVER['HTTP_HOST'];
  202. $uri = $_SERVER['PHP_SELF'];
  203. $location = $_SERVER['HTTPS'] ? 'https://' : 'http://';
  204. $location = $location . $host . $uri;
  205. $location = $this->addSession($location);
  206. header("Location: $location");
  207. }
  208. function dump() {
  209. if ( ! $this->valid or $this->info == false ) return "Context not valid\n";
  210. $ret = "";
  211. if ( $this->isInstructor() ) {
  212. $ret .= "isInstructor() = true\n";
  213. } else {
  214. $ret .= "isInstructor() = false\n";
  215. }
  216. $ret .= "getUserKey() = ".$this->getUserKey()."\n";
  217. $ret .= "getUserEmail() = ".$this->getUserEmail()."\n";
  218. $ret .= "getUserShortName() = ".$this->getUserShortName()."\n";
  219. $ret .= "getUserName() = ".$this->getUserName()."\n";
  220. $ret .= "getUserImage() = ".$this->getUserImage()."\n";
  221. $ret .= "getResourceKey() = ".$this->getResourceKey()."\n";
  222. $ret .= "getResourceTitle() = ".$this->getResourceTitle()."\n";
  223. $ret .= "getCourseName() = ".$this->getCourseName()."\n";
  224. $ret .= "getCourseKey() = ".$this->getCourseKey()."\n";
  225. $ret .= "getConsumerKey() = ".$this->getConsumerKey()."\n";
  226. return $ret;
  227. }
  228. /**
  229. * Data submitter are in base64 then we have to decode
  230. * @author Antoni Bertran (antoni@tresipunt.com)
  231. * @param $info array
  232. * @date 20120801
  233. */
  234. function decodeBase64($info) {
  235. $keysNoEncode = array("lti_version", "lti_message_type", "tool_consumer_instance_description", "tool_consumer_instance_guid", "oauth_consumer_key", "custom_lti_message_encoded_base64", "oauth_nonce", "oauth_version", "oauth_callback", "oauth_timestamp", "basiclti_submit", "oauth_signature_method", "ext_ims_lis_memberships_id", "ext_ims_lis_memberships_url");
  236. foreach ($info as $key => $item){
  237. if (!in_array($key, $keysNoEncode))
  238. $info[$key] = base64_decode($item);
  239. }
  240. return $info;
  241. }
  242. }
  243. ?>