manage.controller.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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', array('where' => array("id IN ('$vidlist')" => array())));
  49. }
  50. } else {
  51. $todelete = Database::select(
  52. '*',
  53. 'vchamilo',
  54. array('where' => array("root_web = '{$n->root_web}' " => array()))
  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[] = array('url' => 'manage.php', 'name' => get_lang('VChamilo'));
  93. $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : '';
  94. if ($vid) {
  95. $vhosts = Database::select('*', 'vchamilo', array('where' => array('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. $backupDir = api_get_path(SYS_PATH).'plugin'.$separator.'vchamilo'.$separator.'templates'.$separator.$vhost->slug.$separator;
  108. $absolute_datadir = $backupDir.'data';
  109. $absolute_sqldir = $backupDir.'dump.sql';
  110. if (!is_dir($backupDir)) {
  111. $result = mkdir($backupDir, 0777, true);
  112. if ($result) {
  113. Display::addFlash(
  114. Display::return_message('Directory created: '.$backupDir)
  115. );
  116. } else {
  117. Display::addFlash(
  118. Display::return_message("Cannot create directory: $backupDir check the folder permissions", 'error')
  119. );
  120. }
  121. }
  122. if ($vchamilostep == 0) {
  123. // Create directories, if necessary.
  124. if (!is_dir($absolute_datadir)) {
  125. mkdir($absolute_datadir, 0777, true);
  126. mkdir($absolute_datadir.'/home', 0777, true);
  127. }
  128. if (empty($fullautomation)) {
  129. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  130. $content = '<form name"single" action="'.$actionurl.'">';
  131. $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
  132. $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
  133. $content .= '<input type="hidden" name="step" value="1" />';
  134. $content .= '<input type="submit" class="btn btn-primary" name="go_btn" value="'.$plugin->get_lang('continue').'" />';
  135. $content .= '</form>';
  136. $content .= '</div>';
  137. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  138. $tpl->assign('message', '<h4>'.$plugin->get_lang('vchamilosnapshot1').'</h4>');
  139. $tpl->assign('content', $content);
  140. $tpl->display_one_col_template();
  141. exit;
  142. } else {
  143. // continue next step
  144. $vchamilostep = 1;
  145. }
  146. }
  147. if ($vchamilostep >= 1) {
  148. if ($wwwroot == $_configuration['root_web']) {
  149. // Make fake Vchamilo record.
  150. $vchamilo = Virtual::makeThis();
  151. $coursePath = api_get_path(SYS_COURSE_PATH);
  152. $homePath = api_get_path(SYS_HOME_PATH);
  153. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  154. $uploadPath = api_get_path(SYS_UPLOAD_PATH);
  155. } else {
  156. // Get Vchamilo known record.
  157. $vchamilo = Database::select('*', 'vchamilo', array('where' => array('root_web = ?' => array($wwwroot))), 'first');
  158. $vchamilo = (object) $vchamilo;
  159. $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
  160. $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
  161. $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
  162. $uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
  163. $coursePath = $coursePath.'/'.$vchamilo->slug;
  164. $homePath = $homePath.'/'.$vchamilo->slug;
  165. $archivePath = $archivePath.'/'.$vchamilo->slug;
  166. $uploadPath = $uploadPath.'/'.$vchamilo->slug;
  167. }
  168. $content = '';
  169. if ($vchamilostep == 1) {
  170. // Auto dump the databases in a master template folder.
  171. // this will create three files : dump.sql
  172. $result = Virtual::backupDatabase($vchamilo, $absolute_sqldir);
  173. if (empty($fullautomation)) {
  174. if (!$result) {
  175. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  176. $content .= '<p><form name"single" action="'.$actionurl.'">';
  177. $content .= '<input type="submit" name="go_btn" value="'.$plugin->get_lang('cancel').'" />';
  178. $content .= '</form></p>';
  179. } else {
  180. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  181. $message = $plugin->get_lang('vchamilosnapshot2');
  182. Display::addFlash(
  183. Display::return_message('Database file created: '.$absolute_sqldir)
  184. );
  185. $content .= '<form name"single" action="'.$actionurl.'">';
  186. $content .= '<input type="hidden" name="what" value="snapshotinstance" />';
  187. $content .= '<input type="hidden" name="vid" value="'.$vhost->id.'" />';
  188. $content .= '<input type="hidden" name="step" value="2" />';
  189. $content .= '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('continue').'" />';
  190. $content .= '</form>';
  191. }
  192. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  193. $tpl->assign('message', '<h4>'.$message.'</h4>');
  194. $tpl->assign('content', $content);
  195. $tpl->display_one_col_template();
  196. die;
  197. }
  198. }
  199. Display::addFlash(Display::return_message("Copying from '$homePath' to '{$absolute_datadir}/home' "));
  200. copyDirTo($homePath, $absolute_datadir.'/home/', false);
  201. Display::addFlash(Display::return_message("Copying from '$coursePath' to '$absolute_datadir/courses' "));
  202. copyDirTo($coursePath, $absolute_datadir.'/courses/', false);
  203. Display::addFlash(Display::return_message("Copying from '$uploadPath' to '$absolute_datadir/upload' "));
  204. copyDirTo($uploadPath, $absolute_datadir.'/upload/', false);
  205. // Store original hostname and some config info for further database or filestore replacements.
  206. $FILE = fopen($backupDir.$separator.'manifest.php', 'w');
  207. fwrite($FILE, '<'.'?php ');
  208. fwrite($FILE, "\$templatewwwroot = '".$wwwroot."';\n");
  209. fwrite($FILE, '?'.'>');
  210. fclose($FILE);
  211. // Every step was SUCCESS.
  212. if (empty($fullautomation)) {
  213. Display::addFlash(
  214. Display::return_message(
  215. $plugin->get_lang('successfinishedcapture'),
  216. 'success'
  217. )
  218. );
  219. if (empty($vid)) {
  220. $template = Virtual::getConfig('vchamilo', 'default_template');
  221. if (empty($template)) {
  222. Display::addFlash(
  223. Display::return_message('Set default template as <b>'.$vhost->slug.'</b>', 'success', false)
  224. );
  225. $params = [
  226. 'subkey' => 'vchamilo',
  227. 'title' => 'default_template',
  228. 'type' => 'setting',
  229. 'category' => 'Plugins',
  230. 'variable' => 'vchamilo_default_template',
  231. 'selected_value' => $vhost->slug,
  232. 'access_url_changeable' => 0
  233. ];
  234. api_set_setting_simple($params);
  235. } else {
  236. Display::addFlash(
  237. Display::return_message('Default template is: <b>'.$vhost->slug.'</b>', 'success', false)
  238. );
  239. }
  240. }
  241. $actionurl = $_configuration['root_web'].'/plugin/vchamilo/views/manage.php';
  242. $content .= '<form name"single" action="'.$actionurl.'">';
  243. $content .= '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('backtoindex').'" />';
  244. $content .= '</form>';
  245. $tpl = new Template(get_lang('Snapshot'), true, true, false, true, false);
  246. $tpl->assign('message', $plugin->get_lang('vchamilosnapshot3'));
  247. $tpl->assign('content', $content);
  248. $tpl->display_one_col_template();
  249. exit;
  250. }
  251. }
  252. break;
  253. case 'clearcache':
  254. // Removes cache directory.
  255. if (empty($automation)) {
  256. if (array_key_exists('vids', $_REQUEST)) {
  257. $toclear = Database::select('*', 'vchamilo', array('where' => array("id IN ('$vidlist')" => array())));
  258. } else {
  259. $vid = isset($_REQUEST['vid']) ? $_REQUEST['vid'] : 0;
  260. if ($vid) {
  261. $vhosts = Database::select('*', 'vchamilo', array('where' => array('id = ?' => $vid)));
  262. $vhost = (object) array_pop($vhosts);
  263. $toclear[$vhost->id] = $vhost;
  264. } else {
  265. $toclear[0] = (object) $_configuration;
  266. }
  267. }
  268. } else {
  269. $toclear = Database::select(
  270. '*',
  271. 'vchamilo',
  272. array('where' => array("root_web = '{$n->root_web}' " => array()))
  273. );
  274. }
  275. foreach ($toclear as $fooid => $instance) {
  276. if ($fooid == 0) {
  277. $templatepath = api_get_path(SYS_ARCHIVE_PATH).'twig';
  278. Display::addFlash(Display::return_message("Deleting master cache $templatepath \n"));
  279. removeDir($templatepath);
  280. } else {
  281. $coursePath = Virtual::getConfig('vchamilo', 'course_real_root');
  282. $homePath = Virtual::getConfig('vchamilo', 'home_real_root');
  283. $archivePath = Virtual::getConfig('vchamilo', 'archive_real_root');
  284. //$uploadPath = Virtual::getConfig('vchamilo', 'upload_real_root');
  285. // Get instance archive
  286. $archivepath = api_get_path(SYS_ARCHIVE_PATH, (array) $instance);
  287. $templatepath = $archivePath.'/'.$instance['slug'].'/twig';
  288. Display::addFlash(Display::return_message("Deleting cache $templatepath \n"));
  289. removeDir($templatepath);
  290. }
  291. }
  292. break;
  293. case 'setconfigvalue':
  294. $select = '<select name="preset" onchange="setpreset(this.form, this)">';
  295. $settings = api_get_settings();
  296. foreach ($settings as $setting) {
  297. $select .= '<option name="'.$setting['variable'].'/'.$setting['subkey'].'">'.
  298. $setting['variable'].' - '.$setting['subkey'].
  299. '</option>';
  300. }
  301. $select .= '</select>';
  302. if (empty($vidlist)) {
  303. api_not_allowed(true, 'No virtual chamilo selected');
  304. }
  305. Display::display_header();
  306. echo '<h2>'.$plugin->get_lang('sendconfigvalue').'</h2>';
  307. echo '<form name="setconfigform">';
  308. echo '<input type="hidden" name="vidlist" value="'.$vidlist.'" />';
  309. echo '<input type="hidden" name="confirm" value="1" />';
  310. echo '<table>';
  311. echo '<tr><td>'.get_lang('variable').'</td><td>'.get_lang('subkey').'</td></tr>';
  312. echo '<tr><td><input type="text" name="variable" value="" size="30" /></td>';
  313. echo '<td><input type="text" name="subkey" value="" size="30" /></td></tr>';
  314. echo '<tr><td colspan="2">'.$select.'</td></tr>';
  315. echo '<tr><td colspan="2">';
  316. echo '<input class="btn btn-primary" type="submit" name="go_btn" value="'.$plugin->get_lang('distributevalue').'"</td></tr>';
  317. echo '</table>';
  318. echo '</form>';
  319. Display::display_footer();
  320. exit;
  321. break;
  322. }