[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/classes/local/ltiservice/ -> resource_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 resource
  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  
  33  
  34  /**
  35   * The mod_lti\local\ltiservice\resource_base class.
  36   *
  37   * @package    mod_lti
  38   * @since      Moodle 2.8
  39   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  abstract class resource_base {
  43  
  44      /** @var object Service associated with this resource. */
  45      private $service;
  46      /** @var string Type for this resource. */
  47      protected $type;
  48      /** @var string ID for this resource. */
  49      protected $id;
  50      /** @var string Template for this resource. */
  51      protected $template;
  52      /** @var array Custom parameter substitution variables associated with this resource. */
  53      protected $variables;
  54      /** @var array Media types supported by this resource. */
  55      protected $formats;
  56      /** @var array HTTP actions supported by this resource. */
  57      protected $methods;
  58      /** @var array Template variables parsed from the resource template. */
  59      protected $params;
  60  
  61  
  62      /**
  63       * Class constructor.
  64       *
  65       * @param mod_lti\local\ltiservice\service_base $service Service instance
  66       */
  67      public function __construct($service) {
  68  
  69          $this->service = $service;
  70          $this->type = 'RestService';
  71          $this->id = null;
  72          $this->template = null;
  73          $this->methods = array();
  74          $this->variables = array();
  75          $this->formats = array();
  76          $this->methods = array();
  77          $this->params = null;
  78  
  79      }
  80  
  81      /**
  82       * Get the resource ID.
  83       *
  84       * @return string
  85       */
  86      public function get_id() {
  87  
  88          return $this->id;
  89  
  90      }
  91  
  92      /**
  93       * Get the resource template.
  94       *
  95       * @return string
  96       */
  97      public function get_template() {
  98  
  99          return $this->template;
 100  
 101      }
 102  
 103      /**
 104       * Get the resource path.
 105       *
 106       * @return string
 107       */
 108      public function get_path() {
 109  
 110          return $this->get_template();
 111  
 112      }
 113  
 114      /**
 115       * Get the resource type.
 116       *
 117       * @return string
 118       */
 119      public function get_type() {
 120  
 121          return $this->type;
 122  
 123      }
 124  
 125      /**
 126       * Get the resource's service.
 127       *
 128       * @return mod_lti\local\ltiservice\service_base
 129       */
 130      public function get_service() {
 131  
 132          return $this->service;
 133  
 134      }
 135  
 136      /**
 137       * Get the resource methods.
 138       *
 139       * @return array
 140       */
 141      public function get_methods() {
 142  
 143          return $this->methods;
 144  
 145      }
 146  
 147      /**
 148       * Get the resource media types.
 149       *
 150       * @return array
 151       */
 152      public function get_formats() {
 153  
 154          return $this->formats;
 155  
 156      }
 157  
 158      /**
 159       * Get the resource template variables.
 160       *
 161       * @return array
 162       */
 163      public function get_variables() {
 164  
 165          return $this->variables;
 166  
 167      }
 168  
 169      /**
 170       * Get the resource fully qualified endpoint.
 171       *
 172       * @return string
 173       */
 174      public function get_endpoint() {
 175  
 176          $this->parse_template();
 177          $url = $this->get_service()->get_service_path() . $this->get_template();
 178          foreach ($this->params as $key => $value) {
 179              $url = str_replace('{' . $key . '}', $value, $url);
 180          }
 181          $toolproxy = $this->get_service()->get_tool_proxy();
 182          if (!empty($toolproxy)) {
 183              $url = str_replace('{tool_proxy_id}', $toolproxy->guid, $url);
 184          }
 185  
 186          return $url;
 187  
 188      }
 189  
 190      /**
 191       * Execute the request for this resource.
 192       *
 193       * @param mod_lti\local\ltiservice\response $response  Response object for this request.
 194       */
 195      public abstract function execute($response);
 196  
 197      /**
 198       * Check to make sure the request is valid.
 199       *
 200       * @param string $toolproxyguid Consumer key
 201       * @param string $body          Body of HTTP request message
 202       *
 203       * @return boolean
 204       */
 205      public function check_tool_proxy($toolproxyguid, $body = null) {
 206  
 207          $ok = false;
 208          if ($this->get_service()->check_tool_proxy($toolproxyguid, $body)) {
 209              $toolproxyjson = $this->get_service()->get_tool_proxy()->toolproxy;
 210              if (empty($toolproxyjson)) {
 211                  $ok = true;
 212              } else {
 213                  $toolproxy = json_decode($toolproxyjson);
 214                  if (!empty($toolproxy) && isset($toolproxy->security_contract->tool_service)) {
 215                      $contexts = lti_get_contexts($toolproxy);
 216                      $tpservices = $toolproxy->security_contract->tool_service;
 217                      foreach ($tpservices as $service) {
 218                          $fqid = lti_get_fqid($contexts, $service->service);
 219                          $id = explode('#', $fqid, 2);
 220                          if ($this->get_id() === $id[1]) {
 221                              $ok = true;
 222                              break;
 223                          }
 224                      }
 225                  }
 226                  if (!$ok) {
 227                      debugging('Requested service not included in tool proxy: ' . $this->get_id());
 228                  }
 229              }
 230          }
 231  
 232          return $ok;
 233  
 234      }
 235  
 236      /**
 237       * Parse a value for custom parameter substitution variables.
 238       *
 239       * @param string $value String to be parsed
 240       *
 241       * @return string
 242       */
 243      public function parse_value($value) {
 244  
 245          return $value;
 246  
 247      }
 248  
 249      /**
 250       * Parse the template for variables.
 251       *
 252       * @return array
 253       */
 254      protected function parse_template() {
 255  
 256          if (empty($this->params)) {
 257              $this->params = array();
 258              if (isset($_SERVER['PATH_INFO'])) {
 259                  $path = explode('/', $_SERVER['PATH_INFO']);
 260                  $parts = explode('/', $this->get_template());
 261                  for ($i = 0; $i < count($parts); $i++) {
 262                      if ((substr($parts[$i], 0, 1) == '{') && (substr($parts[$i], -1) == '}')) {
 263                          $value = '';
 264                          if ($i < count($path)) {
 265                              $value = $path[$i];
 266                          }
 267                          $this->params[substr($parts[$i], 1, -1)] = $value;
 268                      }
 269                  }
 270              }
 271          }
 272  
 273          return $this->params;
 274  
 275      }
 276  
 277  }


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