3 require_once 'OAuth.php';
5 // Replace this with some real function that pulls from the LMS.
6 function getLMSDummyData() {
8 "resource_link_id" => "120988f929-274612",
9 "resource_link_title" => "Weekly Blog",
10 "resource_link_description" => "Each student needs to reflect on the weekly reading. These should be one paragraph long.",
11 "user_id" => "292832126",
12 "roles" => "Instructor", // or Learner
13 "lis_person_name_full" => 'Jane Q. Public',
14 "lis_person_contact_email_primary" => "user@school.edu",
15 "lis_person_sourcedid" => "school.edu:user",
16 "context_id" => "456434513",
17 "context_title" => "Design of Personal Environments",
18 "context_label" => "SI182",
24 function validateDescriptor($descriptor)
26 $xml = new SimpleXMLElement($xmldata);
28 echo("Error parsing Descriptor XML\n");
31 $launch_url = $xml->secure_launch_url[0];
32 if ( ! $launch_url ) $launch_url = $xml->launch_url[0];
33 if ( $launch_url ) $launch_url = (string) $launch_url;
38 function launchInfo($xmldata) {
39 $xml = new SimpleXMLElement($xmldata);
41 echo("Error parsing Descriptor XML\n");
44 $launch_url = $xml->secure_launch_url[0];
45 if ( ! $launch_url ) $launch_url = $xml->launch_url[0];
46 if ( $launch_url ) $launch_url = (string) $launch_url;
48 if ( $xml->custom[0]->parameter )
49 foreach ( $xml->custom[0]->parameter as $resource) {
50 $key = (string) $resource['key'];
51 $key = strtolower($key);
53 for($i=0; $i < strlen($key); $i++) {
54 $ch = substr($key,$i,1);
55 if ( $ch >= "a" && $ch <= "z" ) $nk .= $ch;
56 else if ( $ch >= "0" && $ch <= "9" ) $nk .= $ch;
59 $value = (string) $resource;
60 $custom["custom_".$nk] = $value;
62 return array("launch_url" => $launch_url, "custom" => $custom ) ;
65 function split_custom_parameters($custom) {
67 return merge_custom_parameters($retval, $custom);
70 function merge_custom_parameters($retval, $custom) {
71 $lines = preg_split("/[\n;]/",$custom);
72 foreach ($lines as $line){
73 $pos = strpos($line,"=");
74 if ( $pos === false || $pos < 1 ) continue;
75 $key = trim(substr($line, 0, $pos));
76 $val = trim(substr($line, $pos+1));
77 $key = 'custom_'.map_keyname($key);
78 if ( isset($retval[$key])) continue;
84 function map_keyname($key) {
86 $key = strtolower(trim($key));
87 foreach (str_split($key) as $ch) {
88 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
97 function signParameters($oldparms, $endpoint, $method, $oauth_consumer_key, $oauth_consumer_secret,
98 $submit_text = false, $org_id = false, $org_desc = false)
100 global $last_base_string;
102 if ( ! isset($parms["lti_version"]) ) $parms["lti_version"] = "LTI-1p0";
103 if ( ! isset($parms["lti_message_type"]) ) $parms["lti_message_type"] = "basic-lti-launch-request";
104 if ( ! isset($parms["oauth_callback"]) ) $parms["oauth_callback"] = "about:blank";
105 if ( $org_id ) $parms["tool_consumer_instance_guid"] = $org_id;
106 if ( $org_desc ) $parms["tool_consumer_instance_description"] = $org_desc;
107 if ( $submit_text ) $parms["ext_submit"] = $submit_text;
111 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
112 $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
114 $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
115 $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
117 // Pass this back up "out of band" for debugging
118 $last_base_string = $acc_req->get_signature_base_string();
120 $newparms = $acc_req->get_parameters();
125 function postLaunchHTML($newparms, $endpoint, $debug=false, $iframeattr=false) {
126 global $last_base_string;
127 $r = "<div id=\"ltiLaunchFormSubmitArea\">\n";
129 $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" target=\"basicltiLaunchFrame\" encType=\"application/x-www-form-urlencoded\">\n" ;
131 $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n" ;
133 $submit_text = $newparms['ext_submit'];
134 foreach($newparms as $key => $value ) {
135 $key = htmlspecialchars($key);
136 $value = htmlspecialchars($value);
137 if ( $key == "ext_submit" ) {
138 $r .= "<input type=\"submit\" name=\"";
140 $r .= "<input type=\"hidden\" name=\"";
148 $r .= "<script language=\"javascript\"> \n";
149 $r .= " //<![CDATA[ \n" ;
150 $r .= "function basicltiDebugToggle() {\n";
151 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
152 $r .= " if(ele.style.display == \"block\") {\n";
153 $r .= " ele.style.display = \"none\";\n";
156 $r .= " ele.style.display = \"block\";\n";
161 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
162 $r .= get_string("toggle_debug_data","basiclti")."</a>\n";
163 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
164 $r .= "<b>".get_string("basiclti_endpoint","basiclti")."</b><br/>\n";
165 $r .= $endpoint . "<br/>\n <br/>\n";
166 $r .= "<b>".get_string("basiclti_parameters","basiclti")."</b><br/>\n";
167 foreach($newparms as $key => $value ) {
168 $key = htmlspecialchars($key);
169 $value = htmlspecialchars($value);
170 $r .= "$key = $value<br/>\n";
172 $r .= " <br/>\n";
173 $r .= "<p><b>".get_string("basiclti_base_string","basiclti")."</b><br/>\n".$last_base_string."</p>\n";
178 $r .= "<iframe name=\"basicltiLaunchFrame\" id=\"basicltiLaunchFrame\" src=\"\"\n";
179 $r .= $iframeattr . ">\n<p>".get_string("frames_required","basiclti")."</p>\n</iframe>\n";
182 $ext_submit = "ext_submit";
183 $ext_submit_text = $submit_text;
184 $r .= " <script type=\"text/javascript\"> \n" .
186 " document.getElementById(\"ltiLaunchForm\").style.display = \"none\";\n" .
187 " nei = document.createElement('input');\n" .
188 " nei.setAttribute('type', 'hidden');\n" .
189 " nei.setAttribute('name', '".$ext_submit."');\n" .
190 " nei.setAttribute('value', '".$ext_submit_text."');\n" .
191 " document.getElementById(\"ltiLaunchForm\").appendChild(nei);\n" .
192 " document.ltiLaunchForm.submit(); \n" .
200 /* This is a bit of homage to Moodle's pattern of internationalisation */
201 function get_string($key,$bundle) {
205 function do_post_request($url, $data, $optional_headers = null)
207 $params = array('http' => array(
211 if ($optional_headers !== null) {
212 $params['http']['header'] = $optional_headers;
214 $ctx = stream_context_create($params);
215 $fp = @fopen($url, 'rb', false, $ctx);
217 throw new Exception("Problem with $url, $php_errormsg");
219 $response = @stream_get_contents($fp);
220 if ($response === false) {
221 throw new Exception("Problem reading data from $url, $php_errormsg");