Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.11.x will end 9 May 2022 (12 months).
  • Bug fixes for security issues in 3.11.x will end 14 November 2022 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
  •    1  <?php
       2  
       3  // This file is part of Moodle - http://moodle.org/
       4  //
       5  // Moodle is free software: you can redistribute it and/or modify
       6  // it under the terms of the GNU General Public License as published by
       7  // the Free Software Foundation, either version 3 of the License, or
       8  // (at your option) any later version.
       9  //
      10  // Moodle is distributed in the hope that it will be useful,
      11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13  // GNU General Public License for more details.
      14  //
      15  // You should have received a copy of the GNU General Public License
      16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
      17  
      18  
      19  /**
      20   * Repository instance callback script
      21   *
      22   * @since Moodle 2.0
      23   * @package    core
      24   * @subpackage repository
      25   * @copyright  2009 Dongsheng Cai <dongsheng@moodle.com>
      26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      27   */
      28  
      29  require_once(__DIR__ . '/../config.php');
      30  require_once (__DIR__ . '/../lib/filelib.php');
      31  require_once (__DIR__.'/lib.php');
      32  
      33  require_login();
      34  
      35  /// Parameters
      36  $repo_id   = required_param('repo_id', PARAM_INT); // Repository ID
      37  
      38  /// Headers to make it not cacheable
      39  header('Cache-Control: no-cache, must-revalidate');
      40  header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
      41  
      42  /// Wait as long as it takes for this script to finish
      43  core_php_time_limit::raise();
      44  
      45  /// Get repository instance information
      46  $sql = 'SELECT i.name, i.typeid, r.type, i.contextid FROM {repository} r, {repository_instances} i '.
      47         'WHERE i.id=? AND i.typeid=r.id';
      48  
      49  $repository = $DB->get_record_sql($sql, array($repo_id), '*', MUST_EXIST);
      50  
      51  $type = $repository->type;
      52  
      53  if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
      54      require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
      55      $classname = 'repository_' . $type;
      56      $repo = new $classname($repo_id, $repository->contextid, array('type'=>$type));
      57  } else {
      58      print_error('invalidplugin', 'repository', $type);
      59  }
      60  
      61  // post callback
      62  $repo->callback();
      63  // call opener window to refresh repository
      64  // the callback url should be something like this:
      65  // http://xx.moodle.com/repository/repository_callback.php?repo_id=1&sid=xxx
      66  // sid is the attached auth token from external source
      67  // If Moodle is working on HTTPS mode, then we are not allowed to access
      68  // parent window, in this case, we need to alert user to refresh the repository
      69  // manually.
      70  $strhttpsbug = json_encode(get_string('cannotaccessparentwin', 'repository'));
      71  $strrefreshnonjs = get_string('refreshnonjsfilepicker', 'repository');
      72  $reloadparent = optional_param('reloadparent', false, PARAM_BOOL);
      73  // If this request is coming from a popup, close window and reload parent window.
      74  if ($reloadparent == true) {
      75      $js = <<<EOD
      76  <html>
      77  <head>
      78      <script type="text/javascript">
      79          window.opener.location.reload();
      80          window.close();
      81      </script>
      82  </head>
      83  <body></body>
      84  </html>
      85  EOD;
      86      die($js);
      87  }
      88  
      89  $js =<<<EOD
      90  <html>
      91  <head>
      92      <script type="text/javascript">
      93      try {
      94          if (window.opener) {
      95              window.opener.M.core_filepicker.active_filepicker.list();
      96              window.close();
      97          } else {
      98              throw new Error('Whoops!');
      99          }
     100      } catch (e) {
     101          alert({$strhttpsbug});
     102      }
     103      </script>
     104  </head>
     105  <body>
     106      <noscript>
     107      {$strrefreshnonjs}
     108      </noscript>
     109  </body>
     110  </html>
     111  EOD;
     112  
     113  die($js);