Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace Packback\Lti1p3;
   4  
   5  use Packback\Lti1p3\Interfaces\ICookie;
   6  
   7  class Redirect
   8  {
   9      private $location;
  10      private $referer_query;
  11      private static $CAN_302_COOKIE = 'LTI_302_Redirect';
  12  
  13      public function __construct(string $location, string $referer_query = null)
  14      {
  15          $this->location = $location;
  16          $this->referer_query = $referer_query;
  17      }
  18  
  19      public function doRedirect()
  20      {
  21          header('Location: '.$this->location, true, 302);
  22          exit;
  23      }
  24  
  25      public function doHybridRedirect(ICookie $cookie)
  26      {
  27          if (!empty($cookie->getCookie(self::$CAN_302_COOKIE))) {
  28              return $this->doRedirect();
  29          }
  30          $cookie->setCookie(self::$CAN_302_COOKIE, 'true');
  31          $this->doJsRedirect();
  32      }
  33  
  34      public function getRedirectUrl()
  35      {
  36          return $this->location;
  37      }
  38  
  39      public function doJsRedirect()
  40      {
  41          ?>
  42          <a id="try-again" target="_blank">If you are not automatically redirected, click here to continue</a>
  43          <script>
  44  
  45          document.getElementById('try-again').href=<?php
  46          if (empty($this->referer_query)) {
  47              echo 'window.location.href';
  48          } else {
  49              echo "window.location.origin + window.location.pathname + '?".$this->referer_query."'";
  50          } ?>;
  51  
  52          var canAccessCookies = function() {
  53              if (!navigator.cookieEnabled) {
  54                  // We don't have access
  55                  return false;
  56              }
  57              // Firefox returns true even if we don't actually have access
  58              try {
  59                  if (!document.cookie || document.cookie == "" || document.cookie.indexOf('<?php echo self::$CAN_302_COOKIE; ?>') === -1) {
  60                      return false;
  61                  }
  62              } catch (e) {
  63                  return false;
  64              }
  65              return true;
  66          };
  67  
  68          if (canAccessCookies()) {
  69              // We have access, continue with redirect
  70              window.location = '<?php echo $this->location; ?>';
  71          } else {
  72              // We don't have access, reopen flow in a new window.
  73              var opened = window.open(document.getElementById('try-again').href, '_blank');
  74              if (opened) {
  75                  document.getElementById('try-again').innerText = "New window opened, click to reopen";
  76              } else {
  77                  document.getElementById('try-again').innerText = "Popup blocked, click to open in a new window";
  78              }
  79          }
  80  
  81          </script>
  82          <?php
  83      }
  84  }