update_files.inc.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, configuration.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 configuration.php to configuration.php.old,
  47. * or if this fails delete the old configuration.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, $_configuration;
  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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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".$_configuration['db_glue']."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. $perm = api_get_setting('permissions_for_new_directories');
  131. $perm = octdec(!empty($perm)?$perm:'0770');
  132. foreach ($coursePath as $key => $course)
  133. {
  134. $mysql_base_course = $courseDB[$key];
  135. @ unlink($oldPath.$course.'/document/.htaccess');
  136. @ unlink($oldPath.$course.'/group/index.php');
  137. if ($fp = @ fopen($oldPath.$course.'/group/index.php', 'w'))
  138. {
  139. fputs($fp, '<html></html>');
  140. fclose($fp);
  141. }
  142. @ unlink($oldPath.$course.'/index.php');
  143. if ($fp = @ fopen($oldPath.$course.'/index.php', 'w'))
  144. {
  145. fputs($fp, '<?php
  146. $cidReq = "'.$key.'";
  147. $dbname = "'.str_replace($dbPrefixForm, '', $mysql_base_course).'";
  148. include("../../main/course_home/course_home.php");
  149. ?>');
  150. fclose($fp);
  151. }
  152. @ mkdir($oldPath.$course.'/temp', $perm);
  153. @ chmod($oldPath.$course.'/temp', $perm);
  154. @ rename($oldPath.$course, $newPath.'courses/'.$course);
  155. // Move group documents to document folder of the course
  156. $group_dir = $newPath.'courses/'.$course.'/group';
  157. if ($dir = @ opendir($group_dir))
  158. {
  159. while (($entry = readdir($dir)) !== false)
  160. {
  161. if ($entry != '.' && $entry != '..' && is_dir($group_dir.'/'.$entry))
  162. {
  163. $from_dir = $group_dir.'/'.$entry;
  164. $to_dir = $newPath.'courses/'.$course.'/document/'.$entry;
  165. @ rename($from_dir, $to_dir);
  166. }
  167. }
  168. closedir($dir);
  169. }
  170. fill_document_table($newPath.'courses/'.$course.'/document');
  171. mysql_query("ALTER TABLE `$mysql_base_course".$_configuration['db_glue']."document` DROP `visibility`");
  172. // Update item_properties of group documents
  173. $sql = "SELECT d.id AS doc_id, g.id AS group_id FROM `$mysql_base_course".$_configuration['db_glue']."group_info` g,`$mysql_base_course".$_configuration['db_glue']."document` d WHERE path LIKE CONCAT(g.secret_directory,'%')";
  174. $res = mysql_query($sql);
  175. while ($group_doc = mysql_fetch_object($res))
  176. {
  177. $sql = "UPDATE `$mysql_base_course".$_configuration['db_glue']."item_property` SET to_group_id = '".$group_doc->group_id."', visibility = '1' WHERE ref = '".$group_doc->doc_id."' AND tool = '".TOOL_DOCUMENT."'";
  178. mysql_query($sql);
  179. }
  180. }
  181. if (defined('DOKEOS_INSTALL'))
  182. {
  183. // Write the Dokeos config file
  184. write_dokeos_config_file($newPath.'main/inc/conf/configuration.php');
  185. // Write a distribution file with the config as a backup for the admin
  186. write_dokeos_config_file($newPath.'main/inc/conf/configuration.dist.php');
  187. // Write a .htaccess file in the course repository
  188. write_courses_htaccess_file($urlAppendPath);
  189. require_once ('../inc/lib/fileManage.lib.php');
  190. // First remove the upload/users directory in the new installation
  191. removeDir($newPath.'main/upload/users');
  192. // Move the old user images to the new installation
  193. @ rename($oldPath.'main/img/users', $newPath.'main/upload/users');
  194. if (!@ rename($oldPath.'main/inc/conf/configuration.php', $oldPath.'main/inc/conf/configuration.php.old'))
  195. {
  196. unlink($oldPath.'main/inc/conf/configuration.php');
  197. }
  198. }
  199. }
  200. else
  201. {
  202. echo 'You are not allowed here !';
  203. }
  204. ?>