move code up one directory
[atutor.git] / mods / _standard / basiclti / launch / ims-blti / blti_util.php
1 <?php
2
3 require_once 'OAuth.php';
4
5   // Replace this with some real function that pulls from the LMS.
6   function getLMSDummyData() {
7     $parms = array( 
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",
19       );
20
21     return $parms;
22   }
23
24   function validateDescriptor($descriptor)
25   {
26     $xml = new SimpleXMLElement($xmldata);
27     if ( ! $xml ) {
28        echo("Error parsing Descriptor XML\n");
29        return;
30     }
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;
34     return $launch_url;
35   }
36
37   // Parse a descriptor
38   function launchInfo($xmldata) {
39     $xml = new SimpleXMLElement($xmldata);
40     if ( ! $xml ) {
41        echo("Error parsing Descriptor XML\n");
42        return;
43     }
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;
47     $custom = array();
48     if ( $xml->custom[0]->parameter ) 
49     foreach ( $xml->custom[0]->parameter as $resource) {
50       $key = (string) $resource['key'];
51       $key = strtolower($key);
52       $nk = "";
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;
57         else $nk .= "_";
58       }
59       $value = (string) $resource;
60       $custom["custom_".$nk] = $value;
61     }
62     return array("launch_url" => $launch_url, "custom" => $custom ) ;
63   }
64
65 function split_custom_parameters($custom) {
66     $retval = array();
67     return merge_custom_parameters($retval, $custom);
68 }
69
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;
79         $retval[$key] = $val;
80     }
81     return $retval;
82 }
83
84 function map_keyname($key) {
85     $newkey = "";
86     $key = strtolower(trim($key));
87     foreach (str_split($key) as $ch) {
88         if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
89             $newkey .= $ch;
90         } else {
91             $newkey .= '_';
92         }
93     }
94     return $newkey;
95 }
96
97 function signParameters($oldparms, $endpoint, $method, $oauth_consumer_key, $oauth_consumer_secret, 
98     $submit_text = false, $org_id = false, $org_desc = false)
99 {
100     global $last_base_string;
101     $parms = $oldparms;
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;
108
109     $test_token = '';
110
111     $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
112     $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
113
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);
116
117     // Pass this back up "out of band" for debugging
118     $last_base_string = $acc_req->get_signature_base_string();
119
120     $newparms = $acc_req->get_parameters();
121
122     return $newparms;
123 }
124
125 function postLaunchHTML($newparms, $endpoint, $debug=false, $iframeattr=false) {
126     global $last_base_string;
127     $r = "<div id=\"ltiLaunchFormSubmitArea\">\n";
128     if ( $iframeattr ) {
129         $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" target=\"basicltiLaunchFrame\" encType=\"application/x-www-form-urlencoded\">\n" ;
130     } else {
131         $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n" ;
132     }
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=\"";
139         } else {
140             $r .= "<input type=\"hidden\" name=\"";
141         }
142         $r .= $key;
143         $r .= "\" value=\"";
144         $r .= $value;
145         $r .= "\"/>\n";
146     }
147     if ( $debug ) {
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";
154         $r .= "    }\n";
155         $r .= "    else {\n";
156         $r .= "        ele.style.display = \"block\";\n";
157         $r .= "    }\n";
158         $r .= "} \n";
159         $r .= "  //]]> \n" ;
160         $r .= "</script>\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&nbsp;<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";
171         }
172         $r .= "&nbsp;<br/>\n";
173         $r .= "<p><b>".get_string("basiclti_base_string","basiclti")."</b><br/>\n".$last_base_string."</p>\n";
174         $r .= "</div>\n";
175     }
176     $r .= "</form>\n";
177     if ( $iframeattr ) {
178         $r .= "<iframe name=\"basicltiLaunchFrame\"  id=\"basicltiLaunchFrame\" src=\"\"\n";
179         $r .= $iframeattr . ">\n<p>".get_string("frames_required","basiclti")."</p>\n</iframe>\n";
180     }
181     if ( ! $debug ) {
182         $ext_submit = "ext_submit";
183         $ext_submit_text = $submit_text;
184         $r .= " <script type=\"text/javascript\"> \n" .
185             "  //<![CDATA[ \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" .
193             "  //]]> \n" .
194             " </script> \n";
195     }
196     $r .= "</div>\n";
197     return $r;
198 }
199
200 /* This is a bit of homage to Moodle's pattern of internationalisation */
201 function get_string($key,$bundle) {
202     return $key;
203 }
204
205 function do_post_request($url, $data, $optional_headers = null)
206 {
207   $params = array('http' => array(
208               'method' => 'POST',
209               'content' => $data
210             ));
211   if ($optional_headers !== null) {
212     $params['http']['header'] = $optional_headers;
213   }
214   $ctx = stream_context_create($params);
215   $fp = @fopen($url, 'rb', false, $ctx);
216   if (!$fp) {
217     throw new Exception("Problem with $url, $php_errormsg");
218   }
219   $response = @stream_get_contents($fp);
220   if ($response === false) {
221     throw new Exception("Problem reading data from $url, $php_errormsg");
222   }
223   return $response;
224 }
225