system_management.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. *
  4. *
  5. * @package chamilo.admin
  6. * @author Laurent Opprecht <laurent@opprecht.info>
  7. * @license see /license.txt
  8. */
  9. $language_file = array('admin');
  10. $cidReset = true;
  11. require_once '../inc/global.inc.php';
  12. require_once __DIR__ . '/admin_page.class.php';
  13. class SystemManagementPage extends AdminPage
  14. {
  15. const PARAM_ACTION = 'action';
  16. const PARAM_SECURITY_TOKEN = 'sec_token';
  17. const ACTION_DEFAULT = 'list';
  18. const ACTION_SECURITY_FAILED = 'security_failed';
  19. function get_action()
  20. {
  21. $result = Request::get(self::PARAM_ACTION, self::ACTION_DEFAULT);
  22. if ($result != self::ACTION_DEFAULT) {
  23. $passed = Security::check_token('get');
  24. Security::clear_token();
  25. $result = $passed ? $result : self::ACTION_SECURITY_FAILED;
  26. }
  27. return $result;
  28. }
  29. function url($params)
  30. {
  31. return Uri::here($params);
  32. }
  33. function display_default()
  34. {
  35. $message = get_lang('RemoveOldDatabaseMessage');
  36. $message_table = get_lang('RemoveOldTables');
  37. $message_table .= "<br />".implode(' , ', self::get_tables_to_delete());
  38. $token = Security::get_token();
  39. $url = $this->url(array(self::PARAM_ACTION => 'drop_old_databases', self::PARAM_SECURITY_TOKEN => $token));
  40. $url_table = $this->url(array(self::PARAM_ACTION => 'drop_old_tables', self::PARAM_SECURITY_TOKEN => $token));
  41. $go = get_lang('Go');
  42. $access_url_id = api_get_current_access_url_id();
  43. $message2 = '';
  44. if ($access_url_id === 1) {
  45. if (api_is_windows_os()) {
  46. $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows');
  47. } else {
  48. $dir = api_get_path(SYS_PATH);
  49. $du = exec('du -sh ' . $dir, $err);
  50. list($size, $none) = explode("\t", $du);
  51. $limit = $_configuration[$url]['hosting_limit_disk_space'];
  52. $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit);
  53. }
  54. }
  55. if (!empty($message2)) {
  56. $message2 = '<li>' . $message2 . '</li>';
  57. }
  58. echo <<<EOT
  59. <ul>
  60. <li>
  61. <div>$message</div>
  62. <a class="btn" href=$url>$go</a>
  63. </li>
  64. <li>
  65. <div>$message_table</div>
  66. <a class="btn" href=$url_table>$go</a>
  67. </li>
  68. $message2
  69. </ul>
  70. EOT;
  71. }
  72. function display_security_failed()
  73. {
  74. Display::display_error_message(get_lang('NotAuthorized'));
  75. }
  76. function display_content()
  77. {
  78. $action = $this->get_action();
  79. switch ($action) {
  80. case self::ACTION_DEFAULT:
  81. $this->display_default();
  82. return;
  83. case self::ACTION_SECURITY_FAILED:
  84. $this->display_security_failed();
  85. return;
  86. default:
  87. $f = array($this, $action);
  88. if (is_callable($f)) {
  89. call_user_func($f);
  90. return;
  91. } else {
  92. Display::display_error_message(get_lang('UnknownAction'));
  93. }
  94. return;
  95. }
  96. }
  97. /**
  98. *
  99. * @return ResultSet
  100. */
  101. function get_old_databases()
  102. {
  103. $course_db = Database::get_main_table(TABLE_MAIN_COURSE);
  104. $sql = "SELECT id, code, db_name, directory, course_language FROM $course_db WHERE target_course_code IS NULL AND db_name IS NOT NULL ORDER BY code";
  105. return new ResultSet($sql);
  106. }
  107. function drop_old_tables()
  108. {
  109. $tables_to_remove = self::get_tables_to_delete();
  110. $number_tables_deleted = 0;
  111. $tables_deleted = '';
  112. foreach ($tables_to_remove as $table) {
  113. //Deleting tables
  114. $drop_table = "DROP TABLE $table";
  115. $success = Database::query($drop_table);
  116. $success =true;
  117. if ($success) {
  118. $tables_deleted .= $table.'<br />';
  119. $number_tables_deleted++;
  120. }
  121. }
  122. Display::display_confirmation_message(sprintf(get_lang('XOldTablesDeleted'),$number_tables_deleted));
  123. Display::display_confirmation_message($tables_deleted, false);
  124. }
  125. function get_tables_to_delete() {
  126. $tables_to_remove = array(
  127. Database::get_main_table(TABLE_MAIN_CLASS),
  128. Database::get_main_table(TABLE_MAIN_CLASS_USER),
  129. Database::get_main_table(TABLE_MAIN_COURSE_CLASS),
  130. );
  131. return $tables_to_remove;
  132. }
  133. function drop_old_databases()
  134. {
  135. $result = array();
  136. $courses = $this->get_old_databases();
  137. $course_db = Database::get_main_table(TABLE_MAIN_COURSE);
  138. foreach ($courses as $course) {
  139. $drop_statement = 'DROP DATABASE ' . $course['db_name'];
  140. $success = Database::query($drop_statement);
  141. if ($success) {
  142. /*
  143. * Note that Database::update do not supports null statements so
  144. * we do it by hand here.
  145. */
  146. $id = $course['id'];
  147. $update_statement = "UPDATE $course_db SET db_name = NULL WHERE id = $id";
  148. Database::query($update_statement);
  149. $result[] = $course['db_name'];
  150. }
  151. }
  152. Display::display_confirmation_message(sprintf(get_lang('XOldDatabasesDeleted'),count($result)));
  153. return $result;
  154. }
  155. }
  156. $page = new SystemManagementPage(get_lang('SystemManagement'));
  157. $page->display();