manage.controller.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $table = Database::get_main_table('vchamilo');
  4. if (!defined('CHAMILO_INTERNAL')) {
  5. die('You cannot use this script this way');
  6. }
  7. $vidlist = isset($_REQUEST['vids']) ? implode("','", array_map('intval', $_REQUEST['vids'])) : '';
  8. switch ($action) {
  9. case 'upgrade':
  10. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/upgrade.php?vid='.$vidlist);
  11. break;
  12. case 'import':
  13. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/import.php');
  14. break;
  15. case 'newinstance':
  16. case 'instance':
  17. $registeronly = isset($_REQUEST['registeronly']) ? $_REQUEST['registeronly'] : 0;
  18. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/editinstance.php?registeronly='.$registeronly);
  19. break;
  20. case 'editinstance':
  21. case 'updateinstance':
  22. $vid = $_REQUEST['vid'];
  23. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/editinstance.php?vid='.$vid);
  24. break;
  25. case 'deleteinstances':
  26. case 'disableinstances':
  27. if (!empty($vidlist)) {
  28. Display::addFlash(Display::return_message("Disabling instance"));
  29. // Make it not visible.
  30. $sql = "UPDATE $table SET visible = 0 WHERE id IN ('$vidlist')";
  31. Database::query($sql);
  32. }
  33. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
  34. break;
  35. case 'enableinstances':
  36. if (!empty($vidlist)) {
  37. Display::addFlash(Display::return_message("Enabling instance"));
  38. $sql = "UPDATE $table SET visible = 1 WHERE id IN ('$vidlist') ";
  39. Database::query($sql);
  40. }
  41. Virtual::redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
  42. break;
  43. case 'fulldeleteinstances':
  44. $todelete = [];
  45. // Removes everything.
  46. if (empty($automation)) {
  47. if (!empty($vidlist)) {
  48. $todelete = Database::select('*', 'vchamilo', ['where' => ["id IN ('$vidlist')" => []]]);
  49. }
  50. } else {
  51. $todelete = Database::select(
  52. '*',
  53. 'vchamilo',
  54. ['where' => ["root_web = '{$n->root_web}' " => []]]
  55. );
  56. }
  57. if ($todelete) {
  58. foreach ($todelete as $fooid => $instance) {
  59. $slug = $instance['slug'];
  60. if (!empty($slug)) {
  61. // Remove all files and eventual symlinks
  62. $absalternatecourse = Virtual::getConfig('vchamilo', 'course_real_root');
  63. $coursedir = $absalternatecourse.$slug;
  64. Display::addFlash(Display::return_message("Deleting $coursedir"));
  65. removeDir($coursedir);
  66. if ($absalternatehome = Virtual::getConfig('vchamilo', 'home_real_root')) {
  67. $homedir = $absalternatehome.'/'.$slug;
  68. Display::addFlash(Display::return_message("Deleting $homedir"));
  69. removeDir($homedir);
  70. }
  71. // delete archive
  72. if ($absalternatearchive = Virtual::getConfig('vchamilo', 'archive_real_root')) {
  73. $archivedir = $absalternatearchive.'/'.$slug;
  74. Display::addFlash(Display::return_message("Deleting $archivedir"));
  75. removeDir($archivedir);
  76. }
  77. // Delete upload
  78. if ($dir = Virtual::getConfig('vchamilo', 'upload_real_root')) {
  79. $dir = $dir.'/'.$slug;
  80. Display::addFlash(Display::return_message("Deleting $dir"));
  81. removeDir($dir);
  82. }
  83. }
  84. $sql = "DELETE FROM {$table} WHERE id = ".$instance['id'];
  85. Database::query($sql);
  86. Display::addFlash(Display::return_message("Removing instance: ".$instance['root_web']));
  87. Virtual::dropDatabase((object) $instance);
  88. }
  89. }
  90. break;
  91. case 'snapshotinstance':
  92. $interbreadcrumb[] = ['url' => 'manage.php', 'name' => get_lang('VChamilo')];
  93. $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : '';
  94. if ($vid) {
  95. $vhosts = Database::select('*', 'vchamilo', ['where' => ['id = ?' => $vid]]);
  96. $vhost = (object) array_pop($vhosts);
  97. } else {
  98. $vhost = (object) $_configuration;
  99. $vhost->slug = Virtual::getSlugFromUrl($vhost->root_web);
  100. $vhost->id = 0;
  101. }
  102. // Parsing url for building the template name.
  103. $wwwroot = $vhost->root_web;
  104. $vchamilostep = isset($_REQUEST['step']) ? $_REQUEST['step'] : '';
  105. // Make template directory (files and SQL).
  106. $separator = DIRECTORY_SEPARATOR;
  107. $dirMode = api_get_permissions_for_new_directories();
  108. $backupDir = api_get_path(SYS_PATH).'plugin'.$separator.'vchamilo'.$separator.'templates'.$separator.$vhost->slug.$separator;
  109. $absolute_datadir = $backupDir.'data';
  110. $absolute_sqldir = $backupDir.'dump.sql';
  111. if (!is_dir($backupDir)) {
  112. $result = mkdir($backupDir, $dirMode, true);
  113. if ($result) {
  114. Display::addFlash(
  115. Display::return_message('Directory created: '.$backupDir)
  116. );
  117. } else {
  118. Display::addFlash(
  119. Display::return_message("Cannot create directory: $backupDir check the folder permissions", 'error')
  120. );
  121. }
  122. }
  123. if ($vchamilostep == 0) {
  124. // Create directories, if necessary.
  125. if (!is_dir($absolute_datadir)) {
  126. mkdir($absolute_datadir, $dirMode, true);
  127. mkdir($absolute_datadir.'/home', $dirMode, true);
  128. }
  129. if (empty($fullautomation)) {
  130. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  131. $content = '<form name"single" action="'.$actionurl.'">';
  132. $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
  133. $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
  134. $content .= '<input type="hidden" name="step" value="1" />';
  135. $content .= '<input type="submit" class="btn btn-primary" name="go_btn" value="'.$plugin->get_lang('continue').'" />';
  136. $content .= '</form>';
  137. $content .= '</div>';
  138. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  139. $tpl->assign('message', '<h4>'.$plugin->get_lang('vchamilosnapshot1').'</h4>');
  140. $tpl->assign('content', $content);
  141. $tpl->display_one_col_template();
  142. exit;
  143. } else {
  144. // continue next step
  145. $vchamilostep = 1;
  146. }
  147. }
  148. if ($vchamilostep >= 1) {
  149. if ($wwwroot == $_configuration['root_web']) {
  150. // Make fake Vchamilo record.
  151. $vchamilo = Virtual::makeThis();
  152. $coursePath = api_get_path(SYS_COURSE_PATH);
  153. $homePath = api_get_path(SYS_HOME_PATH);
  154. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  155. $uploadPath = api_get_path(SYS_UPLOAD_PATH);
  156. } else {
  157. // Get Vchamilo known record.
  158. $vchamilo = Database::select('*', 'vchamilo', ['where' => ['root_web = ?' => [$wwwroot]]], 'first');
  159. $vchamilo = (object) $vchamilo;
  160. $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
  161. $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
  162. $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
  163. $uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
  164. $coursePath = $coursePath.'/'.$vchamilo->slug;
  165. $homePath = $homePath.'/'.$vchamilo->slug;
  166. $archivePath = $archivePath.'/'.$vchamilo->slug;
  167. $uploadPath = $uploadPath.'/'.$vchamilo->slug;
  168. }
  169. $content = '';
  170. if ($vchamilostep == 1) {
  171. // Auto dump the databases in a master template folder.
  172. // this will create three files : dump.sql
  173. $result = Virtual::backupDatabase($vchamilo, $absolute_sqldir);
  174. if (empty($fullautomation)) {
  175. if (!$result) {
  176. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  177. $content .= '<p><form name"single" action="'.$actionurl.'">';
  178. $content .= '<input type="submit" name="go_btn" value="'.$plugin->get_lang('cancel').'" />';
  179. $content .= '</form></p>';
  180. } else {
  181. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  182. $message = $plugin->get_lang('vchamilosnapshot2');
  183. Display::addFlash(
  184. Display::return_message('Database file created: '.$absolute_sqldir)
  185. );
  186. $content .= '<form name"single" action="'.$actionurl.'">';
  187. $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
  188. $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
  189. $content .= '<input type="hidden" name="step" value="2" />';
  190. $content .= '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('continue').'" />';
  191. $content .= '</form>';
  192. }
  193. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  194. $tpl->assign('message', '<h4>'.$message.'</h4>');
  195. $tpl->assign('content', $content);
  196. $tpl->display_one_col_template();
  197. die;
  198. }
  199. }
  200. Display::addFlash(Display::return_message("Copying from '$homePath' to '{$absolute_datadir}/home' "));
  201. copyDirTo($homePath, $absolute_datadir.'/home/', false);
  202. Display::addFlash(Display::return_message("Copying from '$coursePath' to '$absolute_datadir/courses' "));
  203. copyDirTo($coursePath, $absolute_datadir.'/courses/', false);
  204. Display::addFlash(Display::return_message("Copying from '$uploadPath' to '$absolute_datadir/upload' "));
  205. copyDirTo($uploadPath, $absolute_datadir.'/upload/', false);
  206. // Store original hostname and some config info for further database or filestore replacements.
  207. $FILE = fopen($backupDir.$separator.'manifest.php', 'w');
  208. fwrite($FILE, '<'.'?php ');
  209. fwrite($FILE, "\$templatewwwroot = '".$wwwroot."';\n");
  210. fwrite($FILE, '?'.'>');
  211. fclose($FILE);
  212. // Every step was SUCCESS.
  213. if (empty($fullautomation)) {
  214. Display::addFlash(
  215. Display::return_message(
  216. $plugin->get_lang('successfinishedcapture'),
  217. 'success'
  218. )
  219. );
  220. if (empty($vid)) {
  221. $template = Virtual::getConfig('vchamilo', 'default_template');
  222. if (empty($template)) {
  223. Display::addFlash(
  224. Display::return_message('Set default template as <b>'.$vhost->slug.'</b>', 'success', false)
  225. );
  226. $params = [
  227. 'subkey' => 'vchamilo',
  228. 'title' => 'default_template',
  229. 'type' => 'setting',
  230. 'category' => 'Plugins',
  231. 'variable' => 'vchamilo_default_template',
  232. 'selected_value' => $vhost->slug,
  233. 'access_url_changeable' => 0,
  234. ];
  235. api_set_setting_simple($params);
  236. } else {
  237. Display::addFlash(
  238. Display::return_message('Default template is: <b>'.$vhost->slug.'</b>', 'success', false)
  239. );
  240. }
  241. }
  242. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  243. $content .= '<form name"single" action="'.$actionurl.'">';
  244. $content .= '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('backtoindex').'" />';
  245. $content .= '</form>';
  246. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  247. $tpl->assign('message', $plugin->get_lang('vchamilosnapshot3'));
  248. $tpl->assign('content', $content);
  249. $tpl->display_one_col_template();
  250. exit;
  251. }
  252. }
  253. break;
  254. case 'clearcache':
  255. // Removes cache directory.
  256. if (empty($automation)) {
  257. if (array_key_exists('vids', $_REQUEST)) {
  258. $toclear = Database::select('*', 'vchamilo', ['where' => ["id IN ('$vidlist')" => []]]);
  259. } else {
  260. $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : 0;
  261. if ($vid) {
  262. $vhosts = Database::select('*', 'vchamilo', ['where' => ['id = ?' => $vid]]);
  263. $vhost = (object) array_pop($vhosts);
  264. $toclear[$vhost->id] = $vhost;
  265. } else {
  266. $toclear[0] = (object) $_configuration;
  267. }
  268. }
  269. } else {
  270. $toclear = Database::select(
  271. '*',
  272. 'vchamilo',
  273. ['where' => ["root_web = '{$n->root_web}' " => []]]
  274. );
  275. }
  276. foreach ($toclear as $fooid => $instance) {
  277. if ($fooid == 0) {
  278. $templatepath = api_get_path(SYS_ARCHIVE_PATH).'twig';
  279. Display::addFlash(Display::return_message("Deleting master cache $templatepath \n"));
  280. removeDir($templatepath);
  281. } else {
  282. $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
  283. $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
  284. $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
  285. //$uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
  286. // Get instance archive
  287. $archivepath = api_get_path(SYS_ARCHIVE_PATH, (array) $instance);
  288. $templatepath = $archivePath.'/'.$instance['slug'].'/twig';
  289. Display::addFlash(Display::return_message("Deleting cache $templatepath \n"));
  290. removeDir($templatepath);
  291. }
  292. }
  293. break;
  294. case 'setconfigvalue':
  295. $select = '<select name="preset" onchange="setpreset(this.form, this)">';
  296. $settings = api_get_settings();
  297. foreach ($settings as $setting) {
  298. $select .= '<option name="'.$setting['variable'].'/'.$setting['subkey'].'">'.
  299. $setting['variable'].' - '.$setting['subkey'].
  300. '</option>';
  301. }
  302. $select .= '</select>';
  303. if (empty($vidlist)) {
  304. api_not_allowed(true, 'No virtual chamilo selected');
  305. }
  306. Display::display_header();
  307. echo '<h2>'.$plugin->get_lang('sendconfigvalue').'</h2>';
  308. echo '<form name="setconfigform">';
  309. echo '<input type="hidden" name="vidlist" value="'.$vidlist.'" />';
  310. echo '<input type="hidden" name="confirm" value="1" />';
  311. echo '<table>';
  312. echo '<tr><td>'.get_lang('variable').'</td><td>'.get_lang('subkey').'</td></tr>';
  313. echo '<tr><td><input type="text" name="variable" value="" size="30" /></td>';
  314. echo '<td><input type="text" name="subkey" value="" size="30" /></td></tr>';
  315. echo '<tr><td colspan="2">'.$select.'</td></tr>';
  316. echo '<tr><td colspan="2">';
  317. echo '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('distributevalue').'"</td></tr>';
  318. echo '</table>';
  319. echo '</form>';
  320. Display::display_footer();
  321. exit;
  322. break;
  323. }