fix_documents_path.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $pathToSearch = '#(http:\/\/\s*|https:\/\/\s*)?(www\s*)?(?(1)([.]\s*))?(?(2)([.]\s*))?([a-zA-Z0-9.-]{2,256})(\s*[.]\s*)(com|fr|)('.$slashes.')(.*?)/(.*?)/(.*?)/#';
  30. $pathToReplace = $newUrlAppend.'/courses/${10}/${11}/';
  31. // tests
  32. /*
  33. $contents = 'http://something.fr/../../../../../../../courses-lt-marie-chamilo/ABC/document/images/gallery/science.jpg';
  34. $contents .= '<br />http://something.something.fr/../../../../../../../courses-lt-marie-chamilo/CDE/document/science.jpg';
  35. $newContent = preg_replace($pathToSearch, $pathToReplace, $contents);
  36. echo $newContent;
  37. exit;*/
  38. $courseSysPath = api_get_path(SYS_COURSE_PATH);
  39. foreach ($courses as $course) {
  40. $courseId = $course['real_id'];
  41. $docsPath = $courseSysPath.$course['directory'].'/document/';
  42. echo "<h4>Course in: $docsPath</h4>".'<br />';
  43. if (is_dir($docsPath)) {
  44. $finder = new \Symfony\Component\Finder\Finder();
  45. $finder->files()->in($docsPath)->name('*.html');
  46. foreach ($finder as $file) {
  47. echo $file->getRealPath().'<br />';
  48. $contents = file_get_contents($file->getRealPath());
  49. $newContent = preg_replace($pathToSearch, $pathToReplace, $contents);
  50. file_put_contents($file->getRealPath(), $newContent);
  51. }
  52. // Updating exercises
  53. $sql = "SELECT iid, description FROM c_quiz WHERE c_id = $courseId";
  54. $result = Database::query($sql);
  55. $items = Database::store_result($result);
  56. foreach ($items as $item) {
  57. $id = $item['iid'];
  58. $newContent = preg_replace($pathToSearch, $pathToReplace, $item['description']);
  59. $newContent = Database::escape_string($newContent);
  60. $sql = "UPDATE c_quiz SET description = '$newContent' WHERE iid = $id";
  61. Database::query($sql);
  62. }
  63. // Updating questions
  64. $sql = "SELECT iid, question, description FROM c_quiz_question WHERE c_id = $courseId";
  65. $result = Database::query($sql);
  66. $items = Database::store_result($result);
  67. foreach ($items as $item) {
  68. $id = $item['iid'];
  69. $description = preg_replace($pathToSearch, $pathToReplace, $item['description']);
  70. $description = Database::escape_string($description);
  71. $question = preg_replace($pathToSearch, $pathToReplace, $item['question']);
  72. $question = Database::escape_string($question);
  73. $sql = "UPDATE c_quiz_question SET
  74. description = '$description',
  75. question = '$question'
  76. WHERE iid = $id";
  77. Database::query($sql);
  78. }
  79. // Updating answer
  80. $sql = "SELECT iid, answer, comment FROM c_quiz_answer WHERE c_id = $courseId";
  81. $result = Database::query($sql);
  82. $items = Database::store_result($result);
  83. foreach ($items as $item) {
  84. $id = $item['iid'];
  85. $answer = preg_replace($pathToSearch, $pathToReplace, $item['answer']);
  86. $answer = Database::escape_string($answer);
  87. $comment = preg_replace($pathToSearch, $pathToReplace, $item['comment']);
  88. $comment = Database::escape_string($comment);
  89. $sql = "UPDATE c_quiz_answer SET
  90. answer = '$answer',
  91. comment = '$comment'
  92. WHERE iid = $id";
  93. Database::query($sql);
  94. }
  95. // Updating intros
  96. $sql = "SELECT iid, intro_text FROM c_tool_intro WHERE c_id = $courseId";
  97. $result = Database::query($sql);
  98. $items = Database::store_result($result);
  99. foreach ($items as $item) {
  100. $id = $item['iid'];
  101. $text = preg_replace($pathToSearch, $pathToReplace, $item['intro_text']);
  102. $text = Database::escape_string($text);
  103. $sql = "UPDATE c_tool_intro SET
  104. intro_text = '$text'
  105. WHERE iid = $id";
  106. Database::query($sql);
  107. }
  108. // Updating Glossary
  109. $sql = "SELECT iid, description FROM c_glossary WHERE c_id = $courseId";
  110. $result = Database::query($sql);
  111. $items = Database::store_result($result);
  112. foreach ($items as $item) {
  113. $id = $item['iid'];
  114. $gdescription = preg_replace($pathToSearch, $pathToReplace, $item['description']);
  115. $gdescription = Database::escape_string($gdescription);
  116. $sql = "UPDATE c_glossary SET description = '$gdescription' WHERE iid = $id";
  117. Database::query($sql);
  118. }
  119. // Updating forums
  120. $sql = "SELECT iid, forum_comment FROM c_forum_forum WHERE c_id = $courseId";
  121. $result = Database::query($sql);
  122. $items = Database::store_result($result);
  123. foreach ($items as $item) {
  124. $id = $item['iid'];
  125. $text = preg_replace($pathToSearch, $pathToReplace, $item['forum_comment']);
  126. $text = Database::escape_string($text);
  127. $sql = "UPDATE c_forum_forum SET
  128. forum_comment = '$text'
  129. WHERE iid = $id";
  130. Database::query($sql);
  131. }
  132. // Updating posts
  133. $sql = "SELECT iid, post_text FROM c_forum_post WHERE c_id = $courseId";
  134. $result = Database::query($sql);
  135. $items = Database::store_result($result);
  136. foreach ($items as $item) {
  137. $id = $item['iid'];
  138. $text = preg_replace($pathToSearch, $pathToReplace, $item['post_text']);
  139. $text = Database::escape_string($text);
  140. $sql = "UPDATE c_forum_post SET
  141. post_text = '$text'
  142. WHERE iid = $id";
  143. Database::query($sql);
  144. }
  145. // Updating forum cats
  146. $sql = "SELECT iid, cat_comment FROM c_forum_category WHERE c_id = $courseId";
  147. $result = Database::query($sql);
  148. $items = Database::store_result($result);
  149. foreach ($items as $item) {
  150. $id = $item['iid'];
  151. $text = preg_replace($pathToSearch, $pathToReplace, $item['cat_comment']);
  152. $text = Database::escape_string($text);
  153. $sql = "UPDATE c_forum_category SET
  154. cat_comment = '$text'
  155. WHERE iid = $id";
  156. Database::query($sql);
  157. }
  158. } else {
  159. echo "<h4>Path doesn't exist</h4>".'<br />';
  160. }
  161. }