update-db-1.8.2-1.8.3.inc.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 dokeos.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. {
  38. ini_set('memory_limit',-1);
  39. ini_set('max_execution_time',0);
  40. }else{
  41. error_log('Update-db script: could not change memory and time limits',0);
  42. }
  43. /*
  44. ==============================================================================
  45. MAIN CODE
  46. ==============================================================================
  47. */
  48. //check if we come from index.php or update_courses.php - otherwise display error msg
  49. if (defined('DOKEOS_INSTALL') || defined('DOKEOS_COURSE_UPDATE'))
  50. {
  51. //check if the current Dokeos install is elligible for update
  52. if (!file_exists('../inc/conf/configuration.php'))
  53. {
  54. echo '<b>'.get_lang('Error').' !</b> Dokeos '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').'.<br><br>
  55. '.get_lang('PleasGoBackToStep1').'.
  56. <p><button type="submit" class="back" name="step1" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  57. </td></tr></table></form></body></html>';
  58. exit ();
  59. }
  60. //get_config_param() comes from install_functions.inc.php and
  61. //actually gets the param from
  62. $_configuration['db_glue'] = get_config_param('dbGlu');
  63. if ($singleDbForm)
  64. {
  65. $_configuration['table_prefix'] = get_config_param('courseTablePrefix');
  66. $_configuration['main_database'] = get_config_param('mainDbName');
  67. $_configuration['db_prefix'] = get_config_param('dbNamePrefix');
  68. }
  69. $dbScormForm = eregi_replace('[^a-z0-9_-]', '', $dbScormForm);
  70. if (!empty ($dbPrefixForm) && !ereg('^'.$dbPrefixForm, $dbScormForm))
  71. {
  72. $dbScormForm = $dbPrefixForm.$dbScormForm;
  73. }
  74. if (empty ($dbScormForm) || $dbScormForm == 'mysql' || $dbScormForm == $dbPrefixForm)
  75. {
  76. $dbScormForm = $dbPrefixForm.'scorm';
  77. }
  78. $res = @mysql_connect($dbHostForm, $dbUsernameForm, $dbPassForm);
  79. //if error on connection to the database, show error and exit
  80. if ($res === false)
  81. {
  82. //$no = mysql_errno();
  83. //$msg = mysql_error();
  84. //echo '<hr>['.$no.'] - '.$msg.'<hr>';
  85. echo get_lang('DBServerDoesntWorkOrLoginPassIsWrong').'.<br /><br />' .
  86. ' '.get_lang('PleaseCheckTheseValues').' :<br /><br />
  87. <b>'.get_lang('DBHost').'</b> : '.$dbHostForm.'<br />
  88. <b>'.get_lang('DBLogin').'</b> : '.$dbUsernameForm.'<br />
  89. <b>'.get_lang('DBPassword').'</b> : '.$dbPassForm.'<br /><br />
  90. '.get_lang('PleaseGoBackToStep').' '. (defined('DOKEOS_INSTALL') ? '3' : '1').'.
  91. <p><button type="submit" class="back" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'">'.get_lang('Back').'</button></p>
  92. </td></tr></table></form></body></html>';
  93. exit ();
  94. }
  95. // The system has not been designed to use special SQL modes that were introduced since MySQL 5
  96. @mysql_query("set session sql_mode='';");
  97. $dblistres = mysql_list_dbs();
  98. $dblist = array();
  99. while ($row = mysql_fetch_object($dblistres)) {
  100. $dblist[] = $row->Database;
  101. }
  102. /*
  103. -----------------------------------------------------------
  104. Normal upgrade procedure:
  105. start by updating main, statistic, user databases
  106. -----------------------------------------------------------
  107. */
  108. //if this script has been included by index.php, not update_courses.php, so
  109. // that we want to change the main databases as well...
  110. $only_test = false;
  111. $log = 0;
  112. if (defined('DOKEOS_INSTALL'))
  113. {
  114. if ($singleDbForm)
  115. {
  116. $dbStatsForm = $dbNameForm;
  117. $dbScormForm = $dbNameForm;
  118. $dbUserForm = $dbNameForm;
  119. }
  120. /**
  121. * Update the databases "pre" migration
  122. */
  123. include ("../lang/english/create_course.inc.php");
  124. if ($languageForm != 'english')
  125. {
  126. //languageForm has been escaped in index.php
  127. include ("../lang/$languageForm/create_course.inc.php");
  128. }
  129. //get the main queries list (m_q_list)
  130. $m_q_list = get_sql_file_contents('migrate-db-1.8.2-1.8.3-pre.sql','main');
  131. if(count($m_q_list)>0)
  132. {
  133. //now use the $m_q_list
  134. /**
  135. * We connect to the right DB first to make sure we can use the queries
  136. * without a database name
  137. */
  138. if(strlen($dbNameForm)>40){
  139. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  140. }elseif(!in_array($dbNameForm,$dblist)){
  141. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  142. }else{
  143. mysql_select_db($dbNameForm);
  144. foreach($m_q_list as $query){
  145. if($only_test){
  146. error_log("mysql_query($dbNameForm,$query)",0);
  147. }else{
  148. $res = mysql_query($query);
  149. if($log)
  150. {
  151. error_log("In $dbNameForm, executed: $query",0);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. //get the stats queries list (s_q_list)
  158. $s_q_list = get_sql_file_contents('migrate-db-1.8.2-1.8.3-pre.sql','stats');
  159. if(count($s_q_list)>0)
  160. {
  161. //now use the $s_q_list
  162. /**
  163. * We connect to the right DB first to make sure we can use the queries
  164. * without a database name
  165. */
  166. if(strlen($dbStatsForm)>40){
  167. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  168. }elseif(!in_array($dbStatsForm,$dblist)){
  169. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  170. }else{
  171. mysql_select_db($dbStatsForm);
  172. foreach($s_q_list as $query){
  173. if($only_test){
  174. error_log("mysql_query($dbStatsForm,$query)",0);
  175. }else{
  176. $res = mysql_query($query);
  177. if($log)
  178. {
  179. error_log("In $dbStatsForm, executed: $query",0);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //get the user queries list (u_q_list)
  186. $u_q_list = get_sql_file_contents('migrate-db-1.8.2-1.8.3-pre.sql','user');
  187. if(count($u_q_list)>0)
  188. {
  189. //now use the $u_q_list
  190. /**
  191. * We connect to the right DB first to make sure we can use the queries
  192. * without a database name
  193. */
  194. if(strlen($dbUserForm)>40){
  195. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  196. }elseif(!in_array($dbUserForm,$dblist)){
  197. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  198. }else{
  199. mysql_select_db($dbUserForm);
  200. foreach($u_q_list as $query){
  201. if($only_test){
  202. error_log("mysql_query($dbUserForm,$query)",0);
  203. error_log("In $dbUserForm, executed: $query",0);
  204. }else{
  205. $res = mysql_query($query);
  206. }
  207. }
  208. }
  209. }
  210. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  211. }
  212. /*
  213. -----------------------------------------------------------
  214. Update the Dokeos course databases
  215. this part can be accessed in two ways:
  216. - from the normal upgrade process
  217. - from the script update_courses.php,
  218. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  219. Every time this script is accessed, only
  220. MAX_COURSE_TRANSFER courses are upgraded.
  221. -----------------------------------------------------------
  222. */
  223. $prefix = '';
  224. if ($singleDbForm)
  225. {
  226. $prefix = $_configuration['table_prefix'];
  227. }
  228. //get the courses databases queries list (c_q_list)
  229. $c_q_list = get_sql_file_contents('migrate-db-1.8.2-1.8.3-pre.sql','course');
  230. if(count($c_q_list)>0)
  231. {
  232. //get the courses list
  233. if(strlen($dbNameForm)>40){
  234. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  235. }elseif(!in_array($dbNameForm,$dblist)){
  236. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  237. }else{
  238. mysql_select_db($dbNameForm);
  239. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  240. if($res===false){die('Error while querying the courses list in update_db-1.8.2-1.8.3.inc.php');}
  241. if(mysql_num_rows($res)>0)
  242. {
  243. $i=0;
  244. $list = array();
  245. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  246. while($row = mysql_fetch_array($res))
  247. {
  248. $list[] = $row;
  249. $i++;
  250. }
  251. foreach($list as $row_course)
  252. {
  253. //now use the $c_q_list
  254. /**
  255. * We connect to the right DB first to make sure we can use the queries
  256. * without a database name
  257. */
  258. if (!$singleDbForm) //otherwise just use the main one
  259. {
  260. mysql_select_db($row_course['db_name']);
  261. }
  262. foreach($c_q_list as $query)
  263. {
  264. if ($singleDbForm) //otherwise just use the main one
  265. {
  266. $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);
  267. }
  268. if($only_test)
  269. {
  270. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  271. }else{
  272. $res = mysql_query($query);
  273. if($log)
  274. {
  275. error_log("In ".$row_course['db_name'].", executed: $query",0);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. else
  285. {
  286. echo 'You are not allowed here !';
  287. }
  288. ?>