fix_documents_path.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script will find paths with:
  5. *
  6. * https://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
  7. *
  8. * And will be transformed to:
  9. *
  10. * /NEWFOLDER/courses/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
  11. *
  12. * You need to choose the /NEWFOLDER in the $newUrlAppend variable
  13. *
  14. * The script will edit HTML documents inside every document course folder and also edit
  15. * exercises (question, answer) and course description in the database.
  16. *
  17. */
  18. exit;
  19. require_once __DIR__.'/../../main/inc/global.inc.php';
  20. /*$test = '
  21. https://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
  22. http://example.fr/../../../../../../../LOLOS/CHAMILO/document/Chamilo/Fonctionalite_de_groupe.html
  23. ';*/
  24. $courses = CourseManager::get_courses_list();
  25. // New values
  26. $newUrlAppend = '/NEWFOLDER'; // Url append of new portal
  27. $slashes = '/../../../../../../../';
  28. $pathToSearch = '#http(.*)://(.*)'.$slashes.'([^/]+)/(.*)#';
  29. $courseSysPath = api_get_path(SYS_COURSE_PATH);
  30. foreach ($courses as $course) {
  31. $courseId = $course['real_id'];
  32. $docsPath = $courseSysPath.$course['directory'].'/document/';
  33. echo "<h4>Course in: $docsPath</h4>".'<br />';
  34. if (is_dir($docsPath)) {
  35. $finder = new \Symfony\Component\Finder\Finder();
  36. $finder->files()->in($docsPath)->name('*.html');
  37. foreach ($finder as $file) {
  38. echo $file->getRealPath().'<br />';
  39. $contents = file_get_contents($file->getRealPath());
  40. $newContent = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $contents);
  41. file_put_contents($file->getRealPath(), $newContent);
  42. }
  43. // Updating exercises
  44. $sql = "SELECT iid, description FROM c_quiz WHERE c_id = $courseId";
  45. $result = Database::query($sql);
  46. $items = Database::store_result($result);
  47. foreach ($items as $item) {
  48. $id = $item['iid'];
  49. $newContent = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['description']);
  50. $newContent = Database::escape_string($newContent);
  51. $sql = "UPDATE c_quiz SET description = '$newContent' WHERE iid = $id";
  52. Database::query($sql);
  53. }
  54. // Updating questions
  55. $sql = "SELECT iid, question, description FROM c_quiz_question WHERE c_id = $courseId";
  56. $result = Database::query($sql);
  57. $items = Database::store_result($result);
  58. foreach ($items as $item) {
  59. $id = $item['iid'];
  60. $description = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['description']);
  61. $description = Database::escape_string($description);
  62. $question = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['question']);
  63. $question = Database::escape_string($question);
  64. $sql = "UPDATE c_quiz_question SET
  65. description = '$description',
  66. question = '$question'
  67. WHERE iid = $id";
  68. Database::query($sql);
  69. }
  70. // Updating answer
  71. $sql = "SELECT iid, answer, comment FROM c_quiz_answer WHERE c_id = $courseId";
  72. $result = Database::query($sql);
  73. $items = Database::store_result($result);
  74. foreach ($items as $item) {
  75. $id = $item['iid'];
  76. $answer = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['answer']);
  77. $answer = Database::escape_string($answer);
  78. $comment = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['comment']);
  79. $comment = Database::escape_string($comment);
  80. $sql = "UPDATE c_quiz_answer SET
  81. answer = '$answer',
  82. comment = '$comment'
  83. WHERE iid = $id";
  84. Database::query($sql);
  85. }
  86. // Updating intros
  87. $sql = "SELECT iid, intro_text FROM c_tool_intro WHERE c_id = $courseId";
  88. $result = Database::query($sql);
  89. $items = Database::store_result($result);
  90. foreach ($items as $item) {
  91. $id = $item['iid'];
  92. $text = preg_replace($pathToSearch, $newUrlAppend.'/courses/${4}', $item['intro_text']);
  93. $text = Database::escape_string($text);
  94. $sql = "UPDATE c_tool_intro SET
  95. intro_text = '$text'
  96. WHERE iid = $id";
  97. Database::query($sql);
  98. }
  99. } else {
  100. echo "<h4>Path doesn't exist</h4>".'<br />';
  101. }
  102. }