blti_util.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. require_once 'OAuth.php';
  3. // Replace this with some real function that pulls from the LMS.
  4. function getLMSDummyData() {
  5. $parms = array(
  6. "resource_link_id" => "120988f929-274612",
  7. "resource_link_title" => "Weekly Blog",
  8. "resource_link_description" => "Each student needs to reflect on the weekly reading. These should be one paragraph long.",
  9. "user_id" => "292832126",
  10. "roles" => "Instructor", // or Learner
  11. "lis_person_name_full" => 'Jane Q. Public',
  12. "lis_person_contact_email_primary" => "user@school.edu",
  13. "lis_person_sourcedid" => "school.edu:user",
  14. "context_id" => "456434513",
  15. "context_title" => "Design of Personal Environments",
  16. "context_label" => "SI182",
  17. );
  18. return $parms;
  19. }
  20. function validateDescriptor($descriptor)
  21. {
  22. $xml = new SimpleXMLElement($xmldata);
  23. if ( ! $xml ) {
  24. echo("Error parsing Descriptor XML\n");
  25. return;
  26. }
  27. $launch_url = $xml->secure_launch_url[0];
  28. if ( ! $launch_url ) $launch_url = $xml->launch_url[0];
  29. if ( $launch_url ) $launch_url = (string) $launch_url;
  30. return $launch_url;
  31. }
  32. // Parse a descriptor
  33. function launchInfo($xmldata) {
  34. $xml = new SimpleXMLElement($xmldata);
  35. if ( ! $xml ) {
  36. echo("Error parsing Descriptor XML\n");
  37. return;
  38. }
  39. $launch_url = $xml->secure_launch_url[0];
  40. if ( ! $launch_url ) $launch_url = $xml->launch_url[0];
  41. if ( $launch_url ) $launch_url = (string) $launch_url;
  42. $custom = array();
  43. if ( $xml->custom[0]->parameter )
  44. foreach ( $xml->custom[0]->parameter as $resource) {
  45. $key = (string) $resource['key'];
  46. $key = strtolower($key);
  47. $nk = "";
  48. for($i=0; $i < strlen($key); $i++) {
  49. $ch = substr($key,$i,1);
  50. if ( $ch >= "a" && $ch <= "z" ) $nk .= $ch;
  51. else if ( $ch >= "0" && $ch <= "9" ) $nk .= $ch;
  52. else $nk .= "_";
  53. }
  54. $value = (string) $resource;
  55. $custom["custom_".$nk] = $value;
  56. }
  57. return array("launch_url" => $launch_url, "custom" => $custom ) ;
  58. }
  59. function split_custom_parameters($customstr) {
  60. $lines = preg_split("/[\n;]/",$customstr);
  61. $retval = array();
  62. foreach ($lines as $line){
  63. $pos = strpos($line,"=");
  64. if ( $pos === false || $pos < 1 ) continue;
  65. $key = trim(substr($line, 0, $pos));
  66. $val = trim(substr($line, $pos+1));
  67. $key = map_keyname($key);
  68. $retval['custom_'.$key] = $val;
  69. }
  70. return $retval;
  71. }
  72. function map_keyname($key) {
  73. $newkey = "";
  74. $key = strtolower(trim($key));
  75. foreach (str_split($key) as $ch) {
  76. if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
  77. $newkey .= $ch;
  78. } else {
  79. $newkey .= '_';
  80. }
  81. }
  82. return $newkey;
  83. }
  84. function signParameters($oldparms, $endpoint, $method, $oauth_consumer_key, $oauth_consumer_secret,
  85. $submit_text = false, $org_id = false, $org_desc = false)
  86. {
  87. global $last_base_string;
  88. $parms = $oldparms;
  89. if ( ! isset($parms["lti_version"]) ) $parms["lti_version"] = "LTI-1p0";
  90. if ( ! isset($parms["lti_message_type"]) ) $parms["lti_message_type"] = "basic-lti-launch-request";
  91. if ( ! isset($parms["oauth_callback"]) ) $parms["oauth_callback"] = "about:blank";
  92. if ( $org_id ) $parms["tool_consumer_instance_guid"] = $org_id;
  93. if ( $org_desc ) $parms["tool_consumer_instance_description"] = $org_desc;
  94. if ( $submit_text ) $parms["ext_submit"] = $submit_text;
  95. $test_token = '';
  96. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  97. $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
  98. $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
  99. $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
  100. // Pass this back up "out of band" for debugging
  101. $last_base_string = $acc_req->get_signature_base_string();
  102. $newparms = $acc_req->get_parameters();
  103. return $newparms;
  104. }
  105. function signOnly($oldparms, $endpoint, $method, $oauth_consumer_key, $oauth_consumer_secret)
  106. {
  107. global $last_base_string;
  108. $parms = $oldparms;
  109. $test_token = '';
  110. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  111. $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
  112. $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
  113. $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
  114. // Pass this back up "out of band" for debugging
  115. $last_base_string = $acc_req->get_signature_base_string();
  116. $newparms = $acc_req->get_parameters();
  117. return $newparms;
  118. }
  119. function postLaunchHTML($newparms, $endpoint, $debug=false, $iframeattr=false) {
  120. global $last_base_string;
  121. $r = "<div id=\"ltiLaunchFormSubmitArea\">\n";
  122. if ( $iframeattr ) {
  123. $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" target=\"basicltiLaunchFrame\" encType=\"application/x-www-form-urlencoded\">\n" ;
  124. } else {
  125. $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n" ;
  126. }
  127. $submit_text = $newparms['ext_submit'];
  128. foreach($newparms as $key => $value ) {
  129. $key = htmlspecialchars($key);
  130. $value = htmlspecialchars($value);
  131. if ( $key == "ext_submit" ) {
  132. $r .= "<input type=\"submit\" name=\"";
  133. } else {
  134. $r .= "<input type=\"hidden\" name=\"";
  135. }
  136. $r .= $key;
  137. $r .= "\" value=\"";
  138. $r .= $value;
  139. $r .= "\"/>\n";
  140. }
  141. if ( $debug ) {
  142. $r .= "<script language=\"javascript\"> \n";
  143. $r .= " //<![CDATA[ \n" ;
  144. $r .= "function basicltiDebugToggle() {\n";
  145. $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
  146. $r .= " if(ele.style.display == \"block\") {\n";
  147. $r .= " ele.style.display = \"none\";\n";
  148. $r .= " }\n";
  149. $r .= " else {\n";
  150. $r .= " ele.style.display = \"block\";\n";
  151. $r .= " }\n";
  152. $r .= "} \n";
  153. $r .= " //]]> \n" ;
  154. $r .= "</script>\n";
  155. $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
  156. $r .= get_string("toggle_debug_data","basiclti")."</a>\n";
  157. $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
  158. $r .= "<b>".get_string("basiclti_endpoint","basiclti")."</b><br/>\n";
  159. $r .= $endpoint . "<br/>\n&nbsp;<br/>\n";
  160. $r .= "<b>".get_string("basiclti_parameters","basiclti")."</b><br/>\n";
  161. foreach($newparms as $key => $value ) {
  162. $key = htmlspecialchars($key);
  163. $value = htmlspecialchars($value);
  164. $r .= "$key = $value<br/>\n";
  165. }
  166. $r .= "&nbsp;<br/>\n";
  167. $r .= "<p><b>".get_string("basiclti_base_string","basiclti")."</b><br/>\n".$last_base_string."</p>\n";
  168. $r .= "</div>\n";
  169. }
  170. $r .= "</form>\n";
  171. if ( $iframeattr ) {
  172. $r .= "<iframe name=\"basicltiLaunchFrame\" id=\"basicltiLaunchFrame\" src=\"\"\n";
  173. $r .= $iframeattr . ">\n<p>".get_string("frames_required","basiclti")."</p>\n</iframe>\n";
  174. }
  175. if ( ! $debug ) {
  176. $ext_submit = "ext_submit";
  177. $ext_submit_text = $submit_text;
  178. $r .= " <script type=\"text/javascript\"> \n" .
  179. " //<![CDATA[ \n" .
  180. " document.getElementById(\"ltiLaunchForm\").style.display = \"none\";\n" .
  181. " nei = document.createElement('input');\n" .
  182. " nei.setAttribute('type', 'hidden');\n" .
  183. " nei.setAttribute('name', '".$ext_submit."');\n" .
  184. " nei.setAttribute('value', '".$ext_submit_text."');\n" .
  185. " document.getElementById(\"ltiLaunchForm\").appendChild(nei);\n" .
  186. " document.ltiLaunchForm.submit(); \n" .
  187. " //]]> \n" .
  188. " </script> \n";
  189. }
  190. $r .= "</div>\n";
  191. return $r;
  192. }
  193. /* This is a bit of homage to Moodle's pattern of internationalisation */
  194. function get_string($key,$bundle) {
  195. return $key;
  196. }
  197. function do_post_request($url, $data, $optional_headers = null)
  198. {
  199. $params = array('http' => array(
  200. 'method' => 'POST',
  201. 'content' => $data
  202. ));
  203. if ($optional_headers !== null) {
  204. $header = $optional_headers . "\r\n";
  205. }
  206. // $header = $header . "Content-type: application/x-www-form-urlencoded\r\n";
  207. $params['http']['header'] = $header;
  208. $ctx = stream_context_create($params);
  209. $fp = @fopen($url, 'rb', false, $ctx);
  210. if (!$fp) {
  211. throw new Exception("Problem with $url, $php_errormsg");
  212. }
  213. $response = @stream_get_contents($fp);
  214. if ($response === false) {
  215. throw new Exception("Problem reading data from $url, $php_errormsg");
  216. }
  217. return $response;
  218. }