update-db-1.8.3-1.8.4.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2007 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. ==============================================================================
  22. * Update the Dokeos database from an older version
  23. * Notice : This script has to be included by index.php or update_courses.php
  24. *
  25. * @package chamilo.install
  26. * @todo
  27. * - conditional changing of tables. Currently we execute for example
  28. * ALTER TABLE `$dbNameForm`.`cours` instructions without checking wether this is necessary.
  29. * - reorganise code into functions
  30. * @todo use database library
  31. ==============================================================================
  32. */
  33. //load helper functions
  34. require_once 'install_upgrade.lib.php';
  35. //remove memory and time limits as much as possible as this might be a long process...
  36. if (function_exists('ini_set')) {
  37. ini_set('memory_limit', -1);
  38. ini_set('max_execution_time', 0);
  39. } else {
  40. error_log('Update-db script: could not change memory and time limits', 0);
  41. }
  42. /*
  43. ==============================================================================
  44. MAIN CODE
  45. ==============================================================================
  46. */
  47. //check if we come from index.php or update_courses.php - otherwise display error msg
  48. if (defined('SYSTEM_INSTALLATION') || defined('DOKEOS_COURSE_UPDATE')) {
  49. //check if the current Dokeos install is elligible for update
  50. if (!file_exists('../inc/conf/configuration.php')) {
  51. echo '<strong>'.get_lang('Error').' !</strong> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br /><br />
  52. '.get_lang('PleasGoBackToStep1').'.
  53. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  54. </td></tr></table></form></body></html>';
  55. exit ();
  56. }
  57. //get_config_param() comes from install_functions.inc.php and
  58. //actually gets the param from
  59. $_configuration['db_glue'] = get_config_param('dbGlu');
  60. if ($singleDbForm) {
  61. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  62. $_configuration['main_database'] = get_config_param('mainDbName');
  63. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  64. }
  65. $dbScormForm = preg_replace('/[^a-zA-Z0-9_\-]/', '', $dbScormForm);
  66. if (!empty($dbPrefixForm) && strpos($dbScormForm, $dbPrefixForm) !== 0) {
  67. $dbScormForm = $dbPrefixForm.$dbScormForm;
  68. }
  69. if (empty($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm) {
  70. $dbScormForm = $dbPrefixForm.'scorm';
  71. }
  72. $res = @Database::connect(array('server' => $dbHostForm, 'username' => $dbUsernameForm, 'password' => $dbPassForm));
  73. //if error on connection to the database, show error and exit
  74. if ($res === false) {
  75. //$no = Database::errno();
  76. //$msg = Database::error();
  77. //echo '<hr />['.$no.'] - '.$msg.'<hr />';
  78. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />' .
  79. ' '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  80. <strong>'.get_lang('DBHost').'</strong> : '.$dbHostForm.'<br />
  81. <strong>'.get_lang('DBLogin').'</strong> : '.$dbUsernameForm.'<br />
  82. <strong>'.get_lang('DBPassword').'</strong> : '.$dbPassForm.'<br /><br />
  83. '.get_lang('PleaseGoBackToStep').' '. (defined('SYSTEM_INSTALLATION') ? '3' : '1').'.
  84. <p><button type="submit" class="back" name="step'. (defined('SYSTEM_INSTALLATION') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  85. </td></tr></table></form></body></html>';
  86. exit ();
  87. }
  88. @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5)
  89. $dblist = Database::get_databases();
  90. /*
  91. -----------------------------------------------------------
  92. Normal upgrade procedure:
  93. start by updating main, statistic, user databases
  94. -----------------------------------------------------------
  95. */
  96. //if this script has been included by index.php, not update_courses.php, so
  97. // that we want to change the main databases as well...
  98. $only_test = false;
  99. $log = 0;
  100. if (defined('SYSTEM_INSTALLATION')) {
  101. if ($singleDbForm) {
  102. $dbStatsForm = $dbNameForm;
  103. $dbScormForm = $dbNameForm;
  104. $dbUserForm = $dbNameForm;
  105. }
  106. /**
  107. * Update the databases "pre" migration
  108. */
  109. include '../lang/english/create_course.inc.php';
  110. if ($languageForm != 'english') {
  111. //languageForm has been escaped in index.php
  112. include '../lang/'.$languageForm.'/create_course.inc.php';
  113. }
  114. //get the main queries list (m_q_list)
  115. $m_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql', 'main');
  116. if (count($m_q_list) > 0) {
  117. //now use the $m_q_list
  118. /**
  119. * We connect to the right DB first to make sure we can use the queries
  120. * without a database name
  121. */
  122. if (strlen($dbNameForm) > 40) {
  123. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  124. } elseif (!in_array($dbNameForm, $dblist)) {
  125. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  126. } else {
  127. Database::select_db($dbNameForm);
  128. foreach ($m_q_list as $query) {
  129. if ($only_test) {
  130. error_log("Database::query($dbNameForm, $query)", 0);
  131. } else {
  132. $res = Database::query($query);
  133. if ($log) {
  134. error_log("In $dbNameForm, executed: $query", 0);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. //get the stats queries list (s_q_list)
  141. $s_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql', 'stats');
  142. if (count($s_q_list) > 0) {
  143. //now use the $s_q_list
  144. /**
  145. * We connect to the right DB first to make sure we can use the queries
  146. * without a database name
  147. */
  148. if (strlen($dbStatsForm) > 40) {
  149. error_log('Database name '.$dbStatsForm.' is too long, skipping', 0);
  150. } elseif (!in_array($dbStatsForm, $dblist)) {
  151. error_log('Database '.$dbStatsForm.' was not found, skipping', 0);
  152. } else {
  153. Database::select_db($dbStatsForm);
  154. foreach ($s_q_list as $query) {
  155. if ($only_test) {
  156. error_log("Database::query($dbStatsForm,$query)", 0);
  157. } else {
  158. $res = Database::query($query);
  159. if ($log) {
  160. error_log("In $dbStatsForm, executed: $query", 0);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. //get the user queries list (u_q_list)
  167. $u_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql', 'user');
  168. if (count($u_q_list) > 0) {
  169. //now use the $u_q_list
  170. /**
  171. * We connect to the right DB first to make sure we can use the queries
  172. * without a database name
  173. */
  174. if (strlen($dbUserForm) > 40) {
  175. error_log('Database name '.$dbUserForm.' is too long, skipping', 0);
  176. } elseif (!in_array($dbUserForm, $dblist)){
  177. error_log('Database '.$dbUserForm.' was not found, skipping', 0);
  178. } else {
  179. Database::select_db($dbUserForm);
  180. foreach ($u_q_list as $query) {
  181. if ($only_test) {
  182. error_log("Database::query($dbUserForm,$query)", 0);
  183. error_log("In $dbUserForm, executed: $query", 0);
  184. } else {
  185. $res = Database::query($query);
  186. }
  187. }
  188. }
  189. }
  190. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  191. }
  192. /*
  193. -----------------------------------------------------------
  194. Update the Dokeos course databases
  195. this part can be accessed in two ways:
  196. - from the normal upgrade process
  197. - from the script update_courses.php,
  198. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  199. Every time this script is accessed, only
  200. MAX_COURSE_TRANSFER courses are upgraded.
  201. -----------------------------------------------------------
  202. */
  203. $prefix = '';
  204. if ($singleDbForm) {
  205. $prefix = $_configuration['table_prefix'];
  206. }
  207. //get the courses databases queries list (c_q_list)
  208. $c_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql', 'course');
  209. if (count($c_q_list) > 0) {
  210. //get the courses list
  211. if (strlen($dbNameForm) > 40) {
  212. error_log('Database name '.$dbNameForm.' is too long, skipping', 0);
  213. } elseif (!in_array($dbNameForm, $dblist)) {
  214. error_log('Database '.$dbNameForm.' was not found, skipping', 0);
  215. } else {
  216. Database::select_db($dbNameForm);
  217. $res = Database::query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  218. if ($res === false) { die('Error while querying the courses list in update_db-1.8.3-1.8.4.inc.php'); }
  219. if (Database::num_rows($res) > 0) {
  220. $i = 0;
  221. $list = array();
  222. //while( ($i < MAX_COURSE_TRANSFER) && ($row = Database::fetch_array($res)))
  223. while ($row = Database::fetch_array($res)) {
  224. $list[] = $row;
  225. $i++;
  226. }
  227. foreach ($list as $row_course) {
  228. //now use the $c_q_list
  229. /**
  230. * We connect to the right DB first to make sure we can use the queries
  231. * without a database name
  232. */
  233. if (!$singleDbForm) { //otherwise just use the main one
  234. Database::select_db($row_course['db_name']);
  235. }
  236. foreach ($c_q_list as $query) {
  237. if ($singleDbForm) { //otherwise just use the main one
  238. $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix{$row_course['db_name']}_$2$3", $query);
  239. }
  240. if ($only_test) {
  241. error_log("Database::query(".$row_course['db_name'].",$query)", 0);
  242. } else {
  243. $res = Database::query($query);
  244. if ($log) {
  245. error_log("In ".$row_course['db_name'].", executed: $query", 0);
  246. }
  247. }
  248. }
  249. //ensure each learnpath is present in the item_property table
  250. $prefix_course = '';
  251. if ($singleDbForm) {
  252. $prefix_course = $prefix.$row_course['db_name']."_";
  253. }
  254. $sql_ip = "SELECT * FROM ".$prefix_course."item_property WHERE tool='learnpath'";
  255. $res_ip = Database::query($sql_ip);
  256. $paths = array();
  257. while ($row_ip = Database::fetch_array($res_ip)) {
  258. $paths[] = $row_ip['ref'];
  259. }
  260. $sql_lp = "SELECT * FROM ".$prefix_course."lp";
  261. $res_lp = Database::query($sql_lp);
  262. $tbl_tool = $prefix_course."tool";
  263. while ($row_lp = Database::fetch_array($res_lp)) {
  264. $time = date("Y-m-d H:i:s", time());
  265. $vis = 'v';
  266. $input = stripslashes($row_lp['name']);
  267. $input = str_replace("'", "''", $input);
  268. $input = str_replace('"', "''", $input);
  269. $mylink = 'newscorm/lp_controller.php?action=view&lp_id='.$row_lp['id'];
  270. $sql2 = "SELECT * FROM $tbl_tool where (name='$input' and image='scormbuilder.gif' and link LIKE '$mylink%')";
  271. if (in_array($row_lp['id'], $paths)) {
  272. //the path is already in item_property, check the visibility is the
  273. //same as the homepage tool's
  274. $res2 = Database::query($sql2);
  275. if (Database::num_rows($res2) > 0) {
  276. $row2 = Database::fetch_array($res2);
  277. $vis = $row2['visibility'];
  278. }
  279. $visi = array('v' => 1, 'i' => 0);
  280. if ($visi[$vis] != $row_ip['visibility']) {
  281. $sql_upd = "UPDATE ".$prefix_course."item_propery SET visibility=".$visi[$vis]." WHERE tool='learnpath' AND ref='".$row_lp['id']."'";
  282. $res_upd = Database::query($sql_upd);
  283. }
  284. } else {
  285. //the path is not in item_property, insert it
  286. $res2 = Database::query($sql2);
  287. if (Database::num_rows($res2) > 0) {
  288. $row2 = Database::fetch_array($res2);
  289. $vis = $row2['visibility'];
  290. }
  291. $visi = array('v' => 1, 'i' => 0);
  292. $sql_ins = "INSERT INTO ".$prefix_course."item_property " .
  293. "(tool,ref,insert_date,last_edit_date,insert_user_id,lastedit_type,lastedit_user_id,visibility)" .
  294. "VALUES" .
  295. "('learnpath',".$row_lp['id'].",'$time','$time',1,'learnpathAdded',1,".$visi[$vis].")";
  296. $res_ins = Database::query($sql_ins);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. }
  303. } else {
  304. echo 'You are not allowed here !';
  305. }