system_management.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $token = Security::get_token();
  32. $params[self::PARAM_SECURITY_TOKEN] = $token;
  33. return Uri::here($params);
  34. }
  35. function display_default()
  36. {
  37. $message = get_lang('RemoveOldDatabaseMessage');
  38. $url = $this->url(array(self::PARAM_ACTION => 'drop_old_databases'));
  39. $go = get_lang('go');
  40. $url = api_get_current_access_url_id();
  41. $message2 = '';
  42. if ($url === 1) {
  43. if (api_is_windows_os()) {
  44. $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuresOnWindows');
  45. } else {
  46. $dir = api_get_path(SYS_PATH);
  47. $du = exec('du -sh '.$dir,$err);
  48. list($size,$none) = explode("\t",$du);
  49. $limit = $_configuration[$url]['hosting_limit_disk_space'];
  50. $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'),$size,$limit);
  51. }
  52. }
  53. if (!empty($message2)) {
  54. $message2 = '<li>'.$message2.'</li>';
  55. }
  56. echo <<<EOT
  57. <ul>
  58. <li>
  59. <div>$message</div>
  60. <a href=$url>$go</a>
  61. </li>
  62. $message2
  63. </ul>
  64. EOT;
  65. }
  66. function display_security_failed()
  67. {
  68. Display::display_error_message(get_lang('NotAuthorized'));
  69. }
  70. function display_content()
  71. {
  72. $action = $this->get_action();
  73. switch ($action) {
  74. case self::ACTION_DEFAULT:
  75. $this->display_default();
  76. return;
  77. case self::ACTION_SECURITY_FAILED:
  78. $this->display_security_failed();
  79. return;
  80. default:
  81. $f = array($this, $action);
  82. if (is_callable($f)) {
  83. call_user_func($f);
  84. return;
  85. } else {
  86. Display::display_error_message(get_lang('UnknownAction'));
  87. }
  88. return;
  89. }
  90. }
  91. /**
  92. *
  93. * @return ResultSet
  94. */
  95. function get_old_databases()
  96. {
  97. $course_db = Database::get_main_table(TABLE_MAIN_COURSE);
  98. $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";
  99. return new ResultSet($sql);
  100. }
  101. function drop_old_databases()
  102. {
  103. $result = array();
  104. $courses = $this->get_old_databases();
  105. $course_db = Database::get_main_table(TABLE_MAIN_COURSE);
  106. foreach ($courses as $course) {
  107. $drop_statement = 'DROP DATABASE ' . $course['db_name'];
  108. $success = Database::query($drop_statement);
  109. if ($sucess) {
  110. /*
  111. * Note that Database::update do not supports null statements so
  112. * we do it by hand here.
  113. */
  114. $id = $course['id'];
  115. $update_statement = "UPDATE $course_db SET db_name = NULL WHERE id = $id";
  116. Database::query($update_statement);
  117. $result[] = $course['db_name'];
  118. }
  119. }
  120. Display::display_confirmation_message(get_lang('OldDatabasesDeleted') . ' ' . count($result));
  121. return $result;
  122. }
  123. }
  124. $page = new SystemManagementPage();
  125. $page->display();