update_files.inc.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * Updates the Dokeos files from an older version
  22. * IMPORTANT: This script has to be included by install/index.php and update_courses.php
  23. *
  24. * DOKEOS_INSTALL is defined in the install/index.php
  25. * DOKEOS_COURSE_UPDATE is defined in update_courses.php
  26. *
  27. * When DOKEOS_INSTALL or DOKEOS_COURSE_UPDATE is defined, do for every course:
  28. * - remove the .htaccess in the document folder
  29. * - remove the index.php in the group folder
  30. * - write a new group/index.php file, make it an empty html file
  31. * - remove the index.php of the course folder
  32. * - write a new index.php file in the course folder, with some settings
  33. * - create a 'temp' directory in the course folder
  34. * - move the course folder inside the courses folder of the new Dokeos installation
  35. * - move the group documents from the group folder to the document folder,
  36. * keeping subfolders intact
  37. * - stores all documents inside the database (document and item_property tables)
  38. * - remove the visibility field from the document table
  39. * - update the item properties of the group documents
  40. *
  41. * Additionally, when DOKEOS_INSTALL is defined
  42. * - write a config file, claro_main.conf.php, with important settings
  43. * - write a .htaccess file (with instructions for Apache) in the courses directory
  44. * - remove the new main/upload/users directory and rename the main/img/users
  45. * directory of the old version to main/upload/users
  46. * - rename the old claro_main.conf.php to claro_main.conf.php.old,
  47. * or if this fails delete the old claro_main.conf.php
  48. *
  49. * @package dokeos.install
  50. ==============================================================================
  51. */
  52. /*
  53. ==============================================================================
  54. FUNCTIONS
  55. ==============================================================================
  56. */
  57. /**
  58. * This function puts the documents of the upgraded courses
  59. * into the necessary tables of the new version:
  60. * the document and item_property tables.
  61. *
  62. * It is used to upgrade from Dokeos 1.5.x versions to
  63. * Dokeos 1.6
  64. *
  65. * @return boolean true if everything worked, false otherwise
  66. */
  67. function fill_document_table($dir)
  68. {
  69. global $newPath, $course, $mysql_base_course, $dbGlu;
  70. $documentPath = $newPath.'courses/'.$course.'/document';
  71. if (!@ $opendir = opendir($dir))
  72. {
  73. return false;
  74. }
  75. while ($readdir = readdir($opendir))
  76. {
  77. if ($readdir != '..' && $readdir != '.' && $readdir != '.htaccess')
  78. {
  79. $path = str_replace($documentPath, '', $dir.'/'.$readdir);
  80. $file_date = date("Y-m-d H:i:s", filemtime($dir.'/'.$readdir));
  81. if (is_file($dir.'/'.$readdir))
  82. {
  83. $file_size = filesize($dir.'/'.$readdir);
  84. $result = mysql_query("SELECT id,visibility FROM `$mysql_base_course".$dbGlu."document` WHERE path='".addslashes($path)."' LIMIT 0,1");
  85. if (list ($id, $visibility) = mysql_fetch_row($result))
  86. {
  87. mysql_query("UPDATE `$mysql_base_course".$dbGlu."document` SET filetype='file',title='".addslashes($readdir)."',size='$file_size' WHERE id='$id' AND path='".addslashes($path)."'");
  88. }
  89. else
  90. {
  91. mysql_query("INSERT INTO `$mysql_base_course".$dbGlu."document`(path,filetype,title,size) VALUES('".addslashes($path)."','file','".addslashes($readdir)."','$file_size')");
  92. $id = mysql_insert_id();
  93. }
  94. $visibility = ($visibility == 'v') ? 1 : 0;
  95. mysql_query("INSERT INTO `$mysql_base_course".$dbGlu."item_property`(tool,ref,visibility,lastedit_type,to_group_id,insert_date,lastedit_date) VALUES('document','$id','$visibility','DocumentAdded','0','".$file_date."','".$file_date."')");
  96. }
  97. elseif (is_dir($dir.'/'.$readdir))
  98. {
  99. $result = mysql_query("SELECT id,visibility FROM `$mysql_base_course".$dbGlu."document` WHERE path='".addslashes($path)."' LIMIT 0,1");
  100. if (list ($id, $visibility) = mysql_fetch_row($result))
  101. {
  102. mysql_query("UPDATE `$mysql_base_course".$dbGlu."document` SET filetype='folder',title='".addslashes($readdir)."' WHERE id='$id' AND path='".addslashes($path)."'");
  103. }
  104. else
  105. {
  106. mysql_query("INSERT INTO `$mysql_base_course".$dbGlu."document`(path,filetype,title) VALUES('".addslashes($path)."','folder','".addslashes($readdir)."')");
  107. $id = mysql_insert_id();
  108. }
  109. $visibility = ($visibility == 'v') ? 1 : 0;
  110. mysql_query("INSERT INTO `$mysql_base_course".$dbGlu."item_property`(tool,ref,visibility, lastedit_type, to_group_id,insert_date,lastedit_date) VALUES('document','$id','$visibility','FolderCreated','0','".$file_date."','".$file_date."')");
  111. if (!fill_document_table($dir.'/'.$readdir))
  112. {
  113. return false;
  114. }
  115. }
  116. }
  117. }
  118. closedir($opendir);
  119. return true;
  120. }
  121. /*
  122. ==============================================================================
  123. INIT SECTION
  124. ==============================================================================
  125. */
  126. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  127. {
  128. $newPath = str_replace('\\', '/', realpath('../..')).'/';
  129. $oldPath = $_POST['updatePath'];
  130. foreach ($coursePath as $key => $course)
  131. {
  132. $mysql_base_course = $courseDB[$key];
  133. @ unlink($oldPath.$course.'/document/.htaccess');
  134. @ unlink($oldPath.$course.'/group/index.php');
  135. if ($fp = @ fopen($oldPath.$course.'/group/index.php', 'w'))
  136. {
  137. fputs($fp, '<html></html>');
  138. fclose($fp);
  139. }
  140. @ unlink($oldPath.$course.'/index.php');
  141. if ($fp = @ fopen($oldPath.$course.'/index.php', 'w'))
  142. {
  143. fputs($fp, '<?php
  144. $cidReq = "'.$key.'";
  145. $dbname = "'.str_replace($dbPrefixForm, '', $mysql_base_course).'";
  146. include("../../main/course_home/course_home.php");
  147. ?>');
  148. fclose($fp);
  149. }
  150. @ mkdir($oldPath.$course.'/temp', 0777);
  151. @ rename($oldPath.$course, $newPath.'courses/'.$course);
  152. // Move group documents to document folder of the course
  153. $group_dir = $newPath.'courses/'.$course.'/group';
  154. if ($dir = @ opendir($group_dir))
  155. {
  156. while (($entry = readdir($dir)) !== false)
  157. {
  158. if ($entry != '.' && $entry != '..' && is_dir($group_dir.'/'.$entry))
  159. {
  160. $from_dir = $group_dir.'/'.$entry;
  161. $to_dir = $newPath.'courses/'.$course.'/document/'.$entry;
  162. @ rename($from_dir, $to_dir);
  163. }
  164. }
  165. closedir($dir);
  166. }
  167. fill_document_table($newPath.'courses/'.$course.'/document');
  168. mysql_query("ALTER TABLE `$mysql_base_course".$dbGlu."document` DROP `visibility`");
  169. // Update item_properties of group documents
  170. $sql = "SELECT d.id AS doc_id, g.id AS group_id FROM `$mysql_base_course".$dbGlu."group_info` g,`$mysql_base_course".$dbGlu."document` d WHERE path LIKE CONCAT(g.secret_directory,'%')";
  171. $res = mysql_query($sql);
  172. while ($group_doc = mysql_fetch_object($res))
  173. {
  174. $sql = "UPDATE `$mysql_base_course".$dbGlu."item_property` SET to_group_id = '".$group_doc->group_id."', visibility = '1' WHERE ref = '".$group_doc->doc_id."' AND tool = '".TOOL_DOCUMENT."'";
  175. mysql_query($sql);
  176. }
  177. }
  178. if (defined('DOKEOS_INSTALL'))
  179. {
  180. // Write the Dokeos config file
  181. write_dokeos_config_file($newPath.'main/inc/conf/claro_main.conf.php');
  182. // Write a distribution file with the config as a backup for the admin
  183. write_dokeos_config_file($newPath.'main/inc/conf/claro_main.conf.dist.php');
  184. // Write a .htaccess file in the course repository
  185. write_courses_htaccess_file($urlAppendPath);
  186. require_once ('../inc/lib/fileManage.lib.php');
  187. // First remove the upload/users directory in the new installation
  188. removeDir($newPath.'main/upload/users');
  189. // Move the old user images to the new installation
  190. @ rename($oldPath.'main/img/users', $newPath.'main/upload/users');
  191. if (!@ rename($oldPath.'main/inc/conf/claro_main.conf.php', $oldPath.'main/inc/conf/claro_main.conf.php.old'))
  192. {
  193. unlink($oldPath.'main/inc/conf/claro_main.conf.php');
  194. }
  195. }
  196. }
  197. else
  198. {
  199. echo 'You are not allowed here !';
  200. }
  201. ?>