repository_callback.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * Repository instance callback script
  18. *
  19. * @since Moodle 2.0
  20. * @package core
  21. * @subpackage repository
  22. * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. require_once(__DIR__ . '/../config.php');
  26. require_once(__DIR__ . '/../lib/filelib.php');
  27. require_once(__DIR__.'/lib.php');
  28. require_login();
  29. /// Parameters
  30. $repo_id = required_param('repo_id', PARAM_INT); // Repository ID
  31. /// Headers to make it not cacheable
  32. header('Cache-Control: no-cache, must-revalidate');
  33. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
  34. /// Wait as long as it takes for this script to finish
  35. core_php_time_limit::raise();
  36. /// Get repository instance information
  37. $sql = 'SELECT i.name, i.typeid, r.type, i.contextid FROM {repository} r, {repository_instances} i '.
  38. 'WHERE i.id=? AND i.typeid=r.id';
  39. $repository = $DB->get_record_sql($sql, array($repo_id), '*', MUST_EXIST);
  40. $type = $repository->type;
  41. if (file_exists($CFG->dirroot.'/repository/'.$type.'/lib.php')) {
  42. require_once($CFG->dirroot.'/repository/'.$type.'/lib.php');
  43. $classname = 'repository_' . $type;
  44. $repo = new $classname($repo_id, $repository->contextid, array('type'=>$type));
  45. } else {
  46. print_error('invalidplugin', 'repository', $type);
  47. }
  48. // post callback
  49. $repo->callback();
  50. // call opener window to refresh repository
  51. // the callback url should be something like this:
  52. // http://xx.moodle.com/repository/repository_callback.php?repo_id=1&sid=xxx
  53. // sid is the attached auth token from external source
  54. // If Moodle is working on HTTPS mode, then we are not allowed to access
  55. // parent window, in this case, we need to alert user to refresh the repository
  56. // manually.
  57. $strhttpsbug = json_encode(get_string('cannotaccessparentwin', 'repository'));
  58. $strrefreshnonjs = get_string('refreshnonjsfilepicker', 'repository');
  59. $js =<<<EOD
  60. <html>
  61. <head>
  62. <script type="text/javascript">
  63. try {
  64. if (window.opener) {
  65. window.opener.M.core_filepicker.active_filepicker.list();
  66. window.close();
  67. } else {
  68. throw new Error('Whoops!');
  69. }
  70. } catch (e) {
  71. alert({$strhttpsbug});
  72. }
  73. </script>
  74. </head>
  75. <body>
  76. <noscript>
  77. {$strrefreshnonjs}
  78. </noscript>
  79. </body>
  80. </html>
  81. EOD;
  82. die($js);