[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/classes/local/ltiservice/ -> service_base.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file contains an abstract definition of an LTI service
  19   *
  20   * @package    mod_lti
  21   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  22   * @author     Stephen Vickers
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  namespace mod_lti\local\ltiservice;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->dirroot . '/mod/lti/locallib.php');
  32  require_once($CFG->dirroot . '/mod/lti/OAuthBody.php');
  33  
  34  // TODO: Switch to core oauthlib once implemented - MDL-30149.
  35  use moodle\mod\lti as lti;
  36  
  37  
  38  /**
  39   * The mod_lti\local\ltiservice\service_base class.
  40   *
  41   * @package    mod_lti
  42   * @since      Moodle 2.8
  43   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  abstract class service_base {
  47  
  48      /** Label representing an LTI 2 message type */
  49      const LTI_VERSION2P0 = 'LTI-2p0';
  50  
  51      /** @var string ID for the service. */
  52      protected $id;
  53      /** @var string Human readable name for the service. */
  54      protected $name;
  55      /** @var boolean <code>true</code> if requests for this service do not need to be signed. */
  56      protected $unsigned;
  57      /** @var stdClass Tool proxy object for the current service request. */
  58      private $toolproxy;
  59      /** @var array Instances of the resources associated with this service. */
  60      protected $resources;
  61  
  62  
  63      /**
  64       * Class constructor.
  65       */
  66      public function __construct() {
  67  
  68          $this->id = null;
  69          $this->name = null;
  70          $this->unsigned = false;
  71          $this->toolproxy = null;
  72          $this->resources = null;
  73  
  74      }
  75  
  76      /**
  77       * Get the service ID.
  78       *
  79       * @return string
  80       */
  81      public function get_id() {
  82  
  83          return $this->id;
  84  
  85      }
  86  
  87      /**
  88       * Get the service name.
  89       *
  90       * @return string
  91       */
  92      public function get_name() {
  93  
  94          return $this->name;
  95  
  96      }
  97  
  98      /**
  99       * Get whether the service requests need to be signed.
 100       *
 101       * @return boolean
 102       */
 103      public function is_unsigned() {
 104  
 105          return $this->unsigned;
 106  
 107      }
 108  
 109      /**
 110       * Get the tool proxy object.
 111       *
 112       * @return stdClass
 113       */
 114      public function get_tool_proxy() {
 115  
 116          return $this->toolproxy;
 117  
 118      }
 119  
 120      /**
 121       * Set the tool proxy object.
 122       *
 123       * @param object $toolproxy The tool proxy for this service request
 124       *
 125       * @var stdClass
 126       */
 127      public function set_tool_proxy($toolproxy) {
 128  
 129          $this->toolproxy = $toolproxy;
 130  
 131      }
 132  
 133      /**
 134       * Get the resources for this service.
 135       *
 136       * @return array
 137       */
 138      abstract public function get_resources();
 139  
 140      /**
 141       * Get the path for service requests.
 142       *
 143       * @return string
 144       */
 145      public static function get_service_path() {
 146  
 147          $url = new \moodle_url('/mod/lti/services.php');
 148  
 149          return $url->out(false);
 150  
 151      }
 152  
 153      /**
 154       * Parse a string for custom substitution parameter variables supported by this service's resources.
 155       *
 156       * @param string $value  Value to be parsed
 157       *
 158       * @return string
 159       */
 160      public function parse_value($value) {
 161  
 162          if (empty($this->resources)) {
 163              $this->resources = $this->get_resources();
 164          }
 165          if (!empty($this->resources)) {
 166              foreach ($this->resources as $resource) {
 167                  $value = $resource->parse_value($value);
 168              }
 169          }
 170  
 171          return $value;
 172  
 173      }
 174  
 175      /**
 176       * Check that the request has been properly signed.
 177       *
 178       * @param string $toolproxyguid  Tool Proxy GUID
 179       * @param string $body           Request body (null if none)
 180       *
 181       * @return boolean
 182       */
 183      public function check_tool_proxy($toolproxyguid, $body = null) {
 184  
 185          $ok = false;
 186          $toolproxy = null;
 187          $consumerkey = lti\get_oauth_key_from_headers();
 188          if (empty($toolproxyguid)) {
 189              $toolproxyguid = $consumerkey;
 190          }
 191  
 192          if (!empty($toolproxyguid)) {
 193              $toolproxy = lti_get_tool_proxy_from_guid($toolproxyguid);
 194              if ($toolproxy !== false) {
 195                  if (!$this->is_unsigned() && ($toolproxy->guid == $consumerkey)) {
 196                      $ok = $this->check_signature($toolproxy->guid, $toolproxy->secret, $body);
 197                  } else {
 198                      $ok = $this->is_unsigned();
 199                  }
 200              }
 201          }
 202          if ($ok) {
 203              $this->toolproxy = $toolproxy;
 204          }
 205  
 206          return $ok;
 207  
 208      }
 209  
 210      /**
 211       * Check the request signature.
 212       *
 213       * @param string $consumerkey    Consumer key
 214       * @param string $secret         Shared secret
 215       * @param string $body           Request body
 216       *
 217       * @return boolean
 218       */
 219      private function check_signature($consumerkey, $secret, $body) {
 220  
 221          $ok = true;
 222          try {
 223              // TODO: Switch to core oauthlib once implemented - MDL-30149.
 224              lti\handle_oauth_body_post($consumerkey, $secret, $body);
 225          } catch (\Exception $e) {
 226              debugging($e->getMessage() . "\n");
 227              $ok = false;
 228          }
 229  
 230          return $ok;
 231  
 232      }
 233  
 234  }


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