manage.controller.php 14 KB

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