Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  namespace Packback\Lti1p3\ImsStorage;
   4  
   5  use Packback\Lti1p3\Interfaces\ICache;
   6  
   7  class ImsCache implements ICache
   8  {
   9      private $cache;
  10  
  11      public function getLaunchData(string $key): ?array
  12      {
  13          $this->loadCache();
  14  
  15          return $this->cache[$key] ?? null;
  16      }
  17  
  18      public function cacheLaunchData(string $key, array $jwtBody): void
  19      {
  20          $this->loadCache();
  21  
  22          $this->cache[$key] = $jwtBody;
  23          $this->saveCache();
  24      }
  25  
  26      public function cacheNonce(string $nonce, string $state): void
  27      {
  28          $this->loadCache();
  29  
  30          $this->cache['nonce'][$nonce] = $state;
  31          $this->saveCache();
  32      }
  33  
  34      public function checkNonceIsValid(string $nonce, string $state): bool
  35      {
  36          $this->loadCache();
  37  
  38          return isset($this->cache['nonce'][$nonce]) &&
  39              $this->cache['nonce'][$nonce] === $state;
  40      }
  41  
  42      public function cacheAccessToken(string $key, string $accessToken): void
  43      {
  44          $this->loadCache();
  45  
  46          $this->cache[$key] = $accessToken;
  47          $this->saveCache();
  48      }
  49  
  50      public function getAccessToken(string $key): ?string
  51      {
  52          $this->loadCache();
  53  
  54          return $this->cache[$key] ?? null;
  55      }
  56  
  57      public function clearAccessToken(string $key): void
  58      {
  59          $this->loadCache();
  60  
  61          unset($this->cache[$key]);
  62          $this->saveCache();
  63      }
  64  
  65      private function loadCache()
  66      {
  67          $cache = file_get_contents(sys_get_temp_dir().'/lti_cache.txt');
  68          if (empty($cache)) {
  69              file_put_contents(sys_get_temp_dir().'/lti_cache.txt', '{}');
  70              $this->cache = [];
  71          }
  72          $this->cache = json_decode($cache, true);
  73      }
  74  
  75      private function saveCache()
  76      {
  77          file_put_contents(sys_get_temp_dir().'/lti_cache.txt', json_encode($this->cache));
  78      }
  79  }