editinstance.controller.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  3. api_protect_admin_script();
  4. if (!defined('CHAMILO_INTERNAL')) die('You cannot use this script this way');
  5. if ($data->what == 'addinstance' || $data->what == 'registerinstance') {
  6. unset($data->what);
  7. unset($data->submitbutton);
  8. unset($data->id);
  9. unset($data->vid);
  10. unset($data->testconnection);
  11. unset($data->testdatapath);
  12. $registeronly = $data->registeronly;
  13. unset($data->registeronly);
  14. $data->lastcron = 0;
  15. $data->lastcrongap = 0;
  16. $data->croncount = 0;
  17. if (!empty($data->template)) {
  18. $template = $data->template;
  19. } else {
  20. $template = '';
  21. }
  22. $mainDatabase = api_get_configuration_value('main_database');
  23. if ($mainDatabase == $data->main_database) {
  24. Display::addFlash(
  25. Display::return_message('You cannot use the same database as the chamilo master', 'error')
  26. );
  27. return ;
  28. }
  29. ctrace("Registering: ".$data->root_web);
  30. $tablename = Database::get_main_table('vchamilo');
  31. $sql = "SELECT * FROM $tablename
  32. WHERE root_web = '".Database::escape_string($data->root_web)."'";
  33. $result = Database::query($sql);
  34. if (Database::num_rows($result)) {
  35. $sql = "SELECT * FROM $tablename
  36. WHERE root_web = '".Database::escape_string($data->root_web)."'";
  37. Database::update($tablename, $data, ['root_web = ?' => $data->root_web]);
  38. $virtualInfo = Database::fetch_array($result);
  39. $slug = $virtualInfo['slug'];
  40. } else {
  41. $slug = $data->slug = vchamilo_get_slug_from_url($data->root_web);
  42. Database::insert($tablename, (array) $data);
  43. }
  44. if ($registeronly) {
  45. // Stop it now.
  46. ctrace("Registering only. out.");
  47. vchamilo_redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
  48. }
  49. // or we continue with physical creation
  50. // Create course directory for operations.
  51. // this is very important here (DO NOT USE api_get_path() !!) because storage may be remotely located
  52. $absalternatecourse = vchamilo_get_config('vchamilo', 'course_real_root');
  53. $coursedir = $absalternatecourse.'/'.$slug;
  54. if (!is_dir($coursedir)) {
  55. ctrace("Creating physical course dir in $coursedir");
  56. mkdir($coursedir, 0777, true);
  57. // initiate default index
  58. $INDEX = fopen($coursedir.'/index.html', 'w');
  59. fputs($INDEX, vchamilo_get_default_course_index_fragment());
  60. fclose($INDEX);
  61. $HTACCESS = fopen($coursedir.'/.htaccess', 'w');
  62. fputs($HTACCESS, vchamilo_get_htaccess_fragment($slug));
  63. fclose($HTACCESS);
  64. }
  65. // if real coursedir IS NOT under chamilo install, link to it
  66. /*$standardlocation = str_replace('//', '/', $_configuration['root_sys'].'/'.$data->course_folder); // where it should be
  67. ctrace("Checking course dir against standard $standardlocation ");
  68. ctrace("checking standard location : ".is_dir($standardlocation));
  69. if ($coursedir != $standardlocation) {
  70. // The standard location dir SHOULD NOT EXIST YET
  71. assert(!is_dir($standardlocation));
  72. ctrace("Linking virtual coursedir ");
  73. chdir(dirname($standardlocation));
  74. if (!symlink($coursedir, basename($coursedir))) {
  75. ctrace("Could not link $standardlocation => $coursedir ");
  76. }
  77. } else {
  78. ctrace("Course dir in standard location");
  79. }*/
  80. // create homedir
  81. // Structure of virtualized home folders :
  82. /*
  83. * {LegacyHomeContainer} => {VChamiloSubcontainer} => {BrandedAccessUrlHome}
  84. *
  85. */
  86. $absalternatehome = vchamilo_get_config('vchamilo', 'home_real_root');
  87. // absalternatehome is a vchamilo config setting that tells where the
  88. // real physical storage for home pages are.
  89. $homedir = str_replace('//', '/', $absalternatehome.'/'.$slug);
  90. ctrace("Making home dir as $homedir");
  91. if (!is_dir($homedir)) {
  92. if (!mkdir($homedir, 0777, true)) {
  93. ctrace("Error creating home dir $homedir \n");
  94. }
  95. }
  96. // if real homedir IS NOT under chamilo install, link to it
  97. // Seems not be necessary as we can globally link the whole Home container
  98. /*
  99. $standardlocation = $_configuration['root_sys'].'home/'.$home_folder; // where it should be
  100. if ($homedir != $standardlocation){
  101. ctrace("Linking virtual home dir ");
  102. if (!symlink($homedir, $standardlocation)){
  103. ctrace("could not link $standardlocation => $homedir ");
  104. }
  105. }
  106. */
  107. // create archive
  108. $absalternatearchive = vchamilo_get_config('vchamilo', 'archive_real_root');
  109. $archivedir = $absalternatearchive.'/'.$slug;
  110. ctrace("Making archive dir as $archivedir ");
  111. if (!is_dir($archivedir)) {
  112. if (!mkdir($archivedir, 0777, true)) {
  113. ctrace("Error creating archive dir $archivedir\n");
  114. }
  115. }
  116. // if real archivedir IS NOT under chamilo install, link to it
  117. // Seems not be necessary as we can globally link the whole Home container
  118. /*
  119. $standardlocation = $_configuration['root_sys'].'archive/'.$archive_folder; // where it should be
  120. if ($archivedir != $standardlocation){
  121. ctrace("Linking virtual archive dir ");
  122. if (!symlink($archivedir, $standardlocation)){
  123. ctrace("could not link $standardlocation => $archivedir ");
  124. }
  125. }
  126. */
  127. if (!$template) {
  128. // Create empty database for install
  129. ctrace("Creating databases (empty)");
  130. vchamilo_create_databases($data);
  131. } else {
  132. // Deploy template database
  133. ctrace("Creating databases from template $template ");
  134. vchamilo_create_databases($data);
  135. ctrace("Loading data template $template ");
  136. vchamilo_load_db_template($data, $template);
  137. ctrace("Coying files from template $template ");
  138. vchamilo_load_files_from_template($data, $template);
  139. }
  140. // pluging in site name institution
  141. $settingstable = $data->main_database.'.settings_current';
  142. $accessurltable = $data->main_database.'.access_url';
  143. $sitename = Database::escape_string($data->sitename);
  144. $institution = Database::escape_string($data->institution);
  145. $sqls[] = "UPDATE {$settingstable} SET selected_value = '{$sitename}'
  146. WHERE variable = 'siteName' AND category = 'Platform' ";
  147. $sqls[] = "UPDATE {$settingstable} SET selected_value = '{$institution}'
  148. WHERE variable = 'institution' AND category = 'Platform' ";
  149. $sqls[] = "UPDATE {$accessurltable} SET url = '{$data->root_web}' WHERE id = '1' ";
  150. foreach ($sqls as $sql) {
  151. Database::query($sql);
  152. }
  153. ctrace("Finished");
  154. echo '<a class="btn btn-primary" href="'.api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php'.'">Continue</a>';
  155. // vchamilo_redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
  156. die;
  157. }
  158. if ($data->what == 'updateinstance') {
  159. unset($data->what);
  160. unset($data->submitbutton);
  161. unset($data->registeronly);
  162. unset($data->template);
  163. $data->lastcron = 0;
  164. $data->lastcrongap = 0;
  165. $data->croncount = 0;
  166. $id = $data->vid;
  167. unset($data->vid);
  168. unset($data->testconnection);
  169. unset($data->testdatapath);
  170. unset($data->vid);
  171. Database::update('vchamilo', (array) $data, array('id = ?' => $id), true);
  172. Display::addFlash(Display::return_message(get_lang('Updated')));
  173. vchamilo_redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
  174. }