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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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><input type="submit" name="step1" value="&lt; '.get_lang('Back').'"></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><input type="submit" name="step'. (defined('DOKEOS_INSTALL') ? '3' : '1').'" value="&lt; '.get_lang('Back').'"></p>
  92. </td></tr></table></form></body></html>';
  93. exit ();
  94. }
  95. $dblistres = mysql_list_dbs();
  96. $dblist = array();
  97. while ($row = mysql_fetch_object($dblistres)) {
  98. $dblist[] = $row->Database;
  99. }
  100. /*
  101. -----------------------------------------------------------
  102. Normal upgrade procedure:
  103. start by updating main, statistic, user databases
  104. -----------------------------------------------------------
  105. */
  106. //if this script has been included by index.php, not update_courses.php, so
  107. // that we want to change the main databases as well...
  108. $only_test = false;
  109. $log = 0;
  110. if (defined('DOKEOS_INSTALL'))
  111. {
  112. if ($singleDbForm)
  113. {
  114. $dbStatsForm = $dbNameForm;
  115. $dbScormForm = $dbNameForm;
  116. $dbUserForm = $dbNameForm;
  117. }
  118. /**
  119. * Update the databases "pre" migration
  120. */
  121. include ("../lang/english/create_course.inc.php");
  122. if ($languageForm != 'english')
  123. {
  124. //languageForm has been escaped in index.php
  125. include ("../lang/$languageForm/create_course.inc.php");
  126. }
  127. //get the main queries list (m_q_list)
  128. $m_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql','main');
  129. if(count($m_q_list)>0)
  130. {
  131. //now use the $m_q_list
  132. /**
  133. * We connect to the right DB first to make sure we can use the queries
  134. * without a database name
  135. */
  136. if(strlen($dbNameForm)>40){
  137. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  138. }elseif(!in_array($dbNameForm,$dblist)){
  139. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  140. }else{
  141. mysql_select_db($dbNameForm);
  142. foreach($m_q_list as $query){
  143. if($only_test){
  144. error_log("mysql_query($dbNameForm,$query)",0);
  145. }else{
  146. $res = mysql_query($query);
  147. if($log)
  148. {
  149. error_log("In $dbNameForm, executed: $query",0);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. //get the stats queries list (s_q_list)
  156. $s_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql','stats');
  157. if(count($s_q_list)>0)
  158. {
  159. //now use the $s_q_list
  160. /**
  161. * We connect to the right DB first to make sure we can use the queries
  162. * without a database name
  163. */
  164. if(strlen($dbStatsForm)>40){
  165. error_log('Database name '.$dbStatsForm.' is too long, skipping',0);
  166. }elseif(!in_array($dbStatsForm,$dblist)){
  167. error_log('Database '.$dbStatsForm.' was not found, skipping',0);
  168. }else{
  169. mysql_select_db($dbStatsForm);
  170. foreach($s_q_list as $query){
  171. if($only_test){
  172. error_log("mysql_query($dbStatsForm,$query)",0);
  173. }else{
  174. $res = mysql_query($query);
  175. if($log)
  176. {
  177. error_log("In $dbStatsForm, executed: $query",0);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. //get the user queries list (u_q_list)
  184. $u_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql','user');
  185. if(count($u_q_list)>0)
  186. {
  187. //now use the $u_q_list
  188. /**
  189. * We connect to the right DB first to make sure we can use the queries
  190. * without a database name
  191. */
  192. if(strlen($dbUserForm)>40){
  193. error_log('Database name '.$dbUserForm.' is too long, skipping',0);
  194. }elseif(!in_array($dbUserForm,$dblist)){
  195. error_log('Database '.$dbUserForm.' was not found, skipping',0);
  196. }else{
  197. mysql_select_db($dbUserForm);
  198. foreach($u_q_list as $query){
  199. if($only_test){
  200. error_log("mysql_query($dbUserForm,$query)",0);
  201. error_log("In $dbUserForm, executed: $query",0);
  202. }else{
  203. $res = mysql_query($query);
  204. }
  205. }
  206. }
  207. }
  208. //the SCORM database doesn't need a change in the pre-migrate part - ignore
  209. }
  210. /*
  211. -----------------------------------------------------------
  212. Update the Dokeos course databases
  213. this part can be accessed in two ways:
  214. - from the normal upgrade process
  215. - from the script update_courses.php,
  216. which is used to upgrade more than MAX_COURSE_TRANSFER courses
  217. Every time this script is accessed, only
  218. MAX_COURSE_TRANSFER courses are upgraded.
  219. -----------------------------------------------------------
  220. */
  221. $prefix = '';
  222. if ($singleDbForm)
  223. {
  224. $prefix = $_configuration['table_prefix'];
  225. }
  226. //get the courses databases queries list (c_q_list)
  227. $c_q_list = get_sql_file_contents('migrate-db-1.8.3-1.8.4-pre.sql','course');
  228. if(count($c_q_list)>0)
  229. {
  230. //get the courses list
  231. if(strlen($dbNameForm)>40){
  232. error_log('Database name '.$dbNameForm.' is too long, skipping',0);
  233. }elseif(!in_array($dbNameForm,$dblist)){
  234. error_log('Database '.$dbNameForm.' was not found, skipping',0);
  235. }else{
  236. mysql_select_db($dbNameForm);
  237. $res = mysql_query("SELECT code,db_name,directory,course_language FROM course WHERE target_course_code IS NULL");
  238. if($res===false){die('Error while querying the courses list in update_db.inc.php');}
  239. if(mysql_num_rows($res)>0)
  240. {
  241. $i=0;
  242. //while( ($i < MAX_COURSE_TRANSFER) && ($row = mysql_fetch_array($res)))
  243. while($row = mysql_fetch_array($res))
  244. {
  245. $list[] = $row;
  246. $i++;
  247. }
  248. foreach($list as $row_course)
  249. {
  250. //now use the $c_q_list
  251. /**
  252. * We connect to the right DB first to make sure we can use the queries
  253. * without a database name
  254. */
  255. if (!$singleDbForm) //otherwise just use the main one
  256. {
  257. mysql_select_db($row_course['db_name']);
  258. }
  259. foreach($c_q_list as $query)
  260. {
  261. if ($singleDbForm) //otherwise just use the main one
  262. {
  263. $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);
  264. }
  265. if($only_test)
  266. {
  267. error_log("mysql_query(".$row_course['db_name'].",$query)",0);
  268. }else{
  269. $res = mysql_query($query);
  270. if($log)
  271. {
  272. error_log("In ".$row_course['db_name'].", executed: $query",0);
  273. }
  274. }
  275. }
  276. //ensure each learnpath is present in the item_property table
  277. $prefix_course = '';
  278. if($singleDbForm)
  279. {
  280. $prefix_course = $prefix.$row_course['db_name']."_";
  281. }
  282. $sql_ip = "SELECT * FROM ".$prefix_course."item_property WHERE tool='learnpath'";
  283. $res_ip = mysql_query($sql_ip);
  284. $paths = array();
  285. while($row_ip = mysql_fetch_array($res_ip))
  286. {
  287. $paths[] = $row_ip['ref'];
  288. }
  289. $sql_lp = "SELECT * FROM ".$prefix_course."lp";
  290. $res_lp = mysql_query($sql_lp);
  291. $tbl_tool = $prefix_course."tool";
  292. while($row_lp = mysql_fetch_array($res_lp))
  293. {
  294. $time = date("Y-m-d H:i:s", time());
  295. $vis = 'v';
  296. $input = stripslashes($row_lp['name']);
  297. $input = str_replace("'", "''", $input);
  298. $input = str_replace('"', "''", $input);
  299. $mylink = 'newscorm/lp_controller.php?action=view&lp_id='.$row_lp['id'];
  300. $sql2="SELECT * FROM $tbl_tool where (name='$input' and image='scormbuilder.gif' and link LIKE '$mylink%')";
  301. if(in_array($row_lp['id'],$paths))
  302. {
  303. //the path is already in item_property, check the visibility is the
  304. //same as the homepage tool's
  305. $res2 = api_sql_query($sql2,__FILE__,__LINE__);
  306. if(mysql_num_rows($res2)>0){
  307. $row2 = mysql_fetch_array($res2);
  308. $vis = $row2['visibility'];
  309. }
  310. $visi = array('v'=>1,'i'=>0);
  311. if($visi[$vis] != $row_ip['visibility'])
  312. {
  313. $sql_upd = "UPDATE ".$prefix_course."item_propery SET visibility=".$visi[$vis]." WHERE tool='learnpath' AND ref='".$row_lp['id']."'";
  314. $res_upd = mysql_query($sql_upd);
  315. }
  316. }
  317. else
  318. {
  319. //the path is not in item_property, insert it
  320. $res2 = api_sql_query($sql2,__FILE__,__LINE__);
  321. if(mysql_num_rows($res2)>0){
  322. $row2 = mysql_fetch_array($res2);
  323. $vis = $row2['visibility'];
  324. }
  325. $visi = array('v'=>1,'i'=>0);
  326. $sql_ins = "INSERT INTO ".$prefix_course."item_property " .
  327. "(tool,ref,insert_date,last_edit_date,insert_user_id,lastedit_type,lastedit_user_id,visibility)" .
  328. "VALUES" .
  329. "('learnpath',".$row_lp['id'].",'$time','$time',1,'learnpathAdded',1,".$visi[$vis].")";
  330. $res_ins = mysql_query($sql_ins);
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. else
  339. {
  340. echo 'You are not allowed here !';
  341. }
  342. ?>