manage.controller.php 13 KB

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