/backup/cc/restore_cc.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 141 lines · 92 code · 29 blank · 20 comment · 18 complexity · 68aa25d4bf15d1d5bdf4cd0e1a6ca331 MD5 · raw file

  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. * @package moodlecore
  18. * @subpackage backup-imscc
  19. * @copyright 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com)
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  23. require_once($CFG->dirroot . '/backup/cc/includes/constants.php');
  24. require_once($CFG->dirroot . '/backup/cc/cc2moodle.php');
  25. function cc_convert ($dir) {
  26. $manifest_file = $dir . DIRECTORY_SEPARATOR . 'imsmanifest.xml';
  27. $moodle_file = $dir . DIRECTORY_SEPARATOR . 'moodle.xml';
  28. $schema_file = 'cc' . DIRECTORY_SEPARATOR . '' . DIRECTORY_SEPARATOR . 'schemas' . DIRECTORY_SEPARATOR . 'cclibxml2validator.xsd';
  29. if (is_readable($manifest_file) && !is_readable($moodle_file)) {
  30. $is_cc = detect_cc_format($manifest_file);
  31. if ($is_cc) {
  32. $detected_requirements = detect_requirements();
  33. if (!$detected_requirements["php5"]) {
  34. notify(get_string('cc_import_req_php5', 'imscc'));
  35. return false;
  36. }
  37. if (!$detected_requirements["dom"]) {
  38. notify(get_string('cc_import_req_dom', 'imscc'));
  39. return false;
  40. }
  41. if (!$detected_requirements["libxml"]) {
  42. notify(get_string('cc_import_req_libxml', 'imscc'));
  43. return false;
  44. }
  45. if (!$detected_requirements["libxmlminversion"]) {
  46. notify(get_string('cc_import_req_libxmlminversion', 'imscc'));
  47. return false;
  48. }
  49. if (!$detected_requirements["xsl"]) {
  50. notify(get_string('cc_import_req_xsl', 'imscc'));
  51. return false;
  52. }
  53. echo get_string('cc2moodle_checking_schema', 'imscc') . '<br />';
  54. $cc_manifest = new DOMDocument();
  55. if ($cc_manifest->load($manifest_file)) {
  56. if ($cc_manifest->schemaValidate($schema_file)) {
  57. echo get_string('cc2moodle_valid_schema', 'imscc') . '<br />';
  58. $cc2moodle = new cc2moodle($manifest_file);
  59. if (!$cc2moodle->is_auth()) {
  60. return $cc2moodle->generate_moodle_xml();
  61. } else {
  62. notify(get_string('cc2moodle_req_auth', 'imscc'));
  63. return false;
  64. }
  65. } else {
  66. notify(get_string('cc2moodle_invalid_schema', 'imscc'));
  67. return false;
  68. }
  69. } else {
  70. notify(get_string('cc2moodle_manifest_dont_load', 'imscc'));
  71. return false;
  72. }
  73. }
  74. }
  75. return true;
  76. }
  77. function detect_requirements () {
  78. if (floor(phpversion()) >= 5) {
  79. $detected["php5"] = true;
  80. } else {
  81. $detected["php5"] = false;
  82. }
  83. $detected["xsl"] = extension_loaded('xsl');
  84. $detected['dom'] = extension_loaded('dom');
  85. $detected['libxml'] = extension_loaded('libxml');
  86. $detected['libxmlminversion'] = extension_loaded('libxml') && version_compare(LIBXML_DOTTED_VERSION, '2.6.30', '>=');
  87. return $detected;
  88. }
  89. function detect_cc_format ($xml_file) {
  90. $inpos = 0;
  91. $xml_snippet = file_get_contents($xml_file, 0, NULL, 0, 500);
  92. if (!empty($xml_snippet)) {
  93. $xml_snippet = strtolower($xml_snippet);
  94. $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
  95. $xml_snippet = str_replace("'", '', $xml_snippet);
  96. $xml_snippet = str_replace('"', '', $xml_snippet);
  97. $search_string = "xmlns=" . NS_COMMON_CARTRIDGE;
  98. $inpos = strpos($xml_snippet, $search_string);
  99. if ($inpos) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. } else {
  105. return false;
  106. }
  107. }