[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/lti/ims-blti/ -> blti.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1