admin.ajax.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. api_protect_admin_script();
  8. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  9. \Chamilo\CoreBundle\Framework\Container::$legacyTemplate = '@ChamiloTheme/Layout/no_layout.html.twig';
  10. switch ($action) {
  11. case 'update_changeable_setting':
  12. $url_id = api_get_current_access_url_id();
  13. if (api_is_global_platform_admin() && $url_id == 1) {
  14. if (isset($_GET['id']) && !empty($_GET['id'])) {
  15. $params = array('variable = ? ' => array($_GET['id']));
  16. $data = api_get_settings_params($params);
  17. if (!empty($data)) {
  18. foreach ($data as $item) {
  19. $params = array('id' =>$item['id'], 'access_url_changeable' => $_GET['changeable']);
  20. api_set_setting_simple($params);
  21. }
  22. }
  23. echo '1';
  24. }
  25. }
  26. break;
  27. case 'version':
  28. // Fix session block when loading admin/index.php and changing page
  29. session_write_close();
  30. echo version_check();
  31. break;
  32. case 'get_extra_content':
  33. $blockName = isset($_POST['block']) ? Security::remove_XSS($_POST['block']) : null;
  34. if (empty($blockName)) {
  35. die;
  36. }
  37. if (api_is_multiple_url_enabled()) {
  38. $accessUrlId = api_get_current_access_url_id();
  39. if ($accessUrlId == -1) {
  40. die;
  41. }
  42. $urlInfo = api_get_access_url($accessUrlId);
  43. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $urlInfo['url']));
  44. $cleanUrl = str_replace('/', '-', $url);
  45. $newUrlDir = api_get_path(SYS_APP_PATH)."home/$cleanUrl/admin/";
  46. } else {
  47. $newUrlDir = api_get_path(SYS_APP_PATH)."home/admin/";
  48. }
  49. if (!file_exists($newUrlDir)) {
  50. die;
  51. }
  52. if (!Security::check_abs_path("{$newUrlDir}{$blockName}_extra.html", $newUrlDir)) {
  53. die;
  54. }
  55. if (!file_exists("{$newUrlDir}{$blockName}_extra.html")) {
  56. die;
  57. }
  58. echo file_get_contents("{$newUrlDir}{$blockName}_extra.html");
  59. break;
  60. }
  61. /**
  62. * Displays either the text for the registration or the message that the installation is (not) up to date
  63. *
  64. * @return string html code
  65. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  66. * @version august 2006
  67. * @todo have a 6 monthly re-registration
  68. */
  69. function version_check()
  70. {
  71. $tbl_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  72. $sql = 'SELECT selected_value FROM '.$tbl_settings.' WHERE variable = "registered" ';
  73. $result = Database::query($sql);
  74. $row = Database::fetch_array($result, 'ASSOC');
  75. // The site has not been registered yet.
  76. $return = '';
  77. if ($row['selected_value'] == 'false') {
  78. $return .= get_lang('VersionCheckExplanation');
  79. $return .= '<form class="version-checking" action="'.api_get_path(WEB_CODE_PATH).'admin/index.php" id="VersionCheck" name="VersionCheck" method="post">';
  80. $return .= '<label class="checkbox"><input type="checkbox" name="donotlistcampus" value="1" id="checkbox" />'.get_lang('HideCampusFromPublicPlatformsList');
  81. $return .= '</label><button type="submit" class="btn btn-primary btn-block" name="Register" value="'.get_lang('EnableVersionCheck').'" id="register" >'.get_lang('EnableVersionCheck').'</button>';
  82. $return .= '</form>';
  83. check_system_version();
  84. } else {
  85. // site not registered. Call anyway
  86. $return .= check_system_version();
  87. }
  88. return $return;
  89. }
  90. /**
  91. * Check if the current installation is up to date
  92. * The code is borrowed from phpBB and slighlty modified
  93. * @author The phpBB Group <support@phpbb.com> (the code)
  94. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University (the modifications)
  95. * @author Yannick Warnier <ywarnier@beeznest.org> for the move to HTTP request
  96. * @copyright (C) 2001 The phpBB Group
  97. * @return string language string with some layout (color)
  98. */
  99. function check_system_version()
  100. {
  101. // Check if curl is available.
  102. if (!in_array('curl', get_loaded_extensions())) {
  103. return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
  104. }
  105. $url = 'https://version.chamilo.org';
  106. $options = [
  107. 'verify' => false
  108. ];
  109. $client = new GuzzleHttp\Client();
  110. $res = $client->request('GET', $url, $options);
  111. $urlValidated = false;
  112. if ($res->getStatusCode() == '200' || $res->getStatusCode() == '301') {
  113. $urlValidated = true;
  114. }
  115. // the chamilo version of your installation
  116. $system_version = trim(api_get_configuration_value('system_version'));
  117. if ($urlValidated) {
  118. // The number of courses
  119. $number_of_courses = Statistics::countCourses();
  120. // The number of users
  121. $number_of_users = Statistics::countUsers();
  122. $number_of_active_users = Statistics::countUsers(
  123. null,
  124. null,
  125. null,
  126. true
  127. );
  128. // The number of sessions
  129. $number_of_sessions = Statistics::countSessions();
  130. $packager = api_get_configuration_value('packager');
  131. if (empty($packager)) {
  132. $packager = 'chamilo';
  133. }
  134. $data = array(
  135. 'url' => api_get_path(WEB_PATH),
  136. 'campus' => api_get_setting('siteName'),
  137. 'contact' => api_get_setting('emailAdministrator'), // the admin's e-mail, with the only purpose of being able to contact admins to inform about critical security issues
  138. 'version' => $system_version,
  139. 'numberofcourses' => $number_of_courses, // to sum up into non-personal statistics - see https://version.chamilo.org/stats/
  140. 'numberofusers' => $number_of_users, // to sum up into non-personal statistics
  141. 'numberofactiveusers' => $number_of_active_users, // to sum up into non-personal statistics
  142. 'numberofsessions' => $number_of_sessions,
  143. //The donotlistcampus setting recovery should be improved to make
  144. // it true by default - this does not affect numbers counting
  145. 'donotlistcampus' => api_get_setting('donotlistcampus'),
  146. 'organisation' => api_get_setting('Institution'),
  147. 'language' => api_get_setting('platformLanguage'), //helps us know the spread of language usage for campuses, by main language
  148. 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'), //not sure this is necessary...
  149. 'ip' => $_SERVER['REMOTE_ADDR'], //the admin's IP address, with the only purpose of trying to geolocate portals around the globe to draw a map
  150. // Reference to the packager system or provider through which
  151. // Chamilo is installed/downloaded. Packagers can change this in
  152. // the default config file (main/install/configuration.dist.php)
  153. // or in the installed config file. The default value is 'chamilo'
  154. 'packager' => $packager,
  155. );
  156. $version = null;
  157. $client = new GuzzleHttp\Client();
  158. $url .= '?';
  159. foreach ($data as $k => $v) {
  160. $url .= urlencode($k).'='.urlencode($v).'&';
  161. }
  162. $res = $client->request('GET', $url, $options);
  163. if ($res->getStatusCode() == '200') {
  164. $versionData = $res->getHeader('X-Chamilo-Version');
  165. if (isset($versionData[0])) {
  166. $version = trim($versionData[0]);
  167. }
  168. }
  169. if (version_compare($system_version, $version, '<')) {
  170. $output = '<span style="color:red">'.get_lang('YourVersionNotUpToDate').'<br />
  171. '.get_lang('LatestVersionIs').' <b>Chamilo '.$version.'</b>. <br />
  172. '.get_lang('YourVersionIs').' <b>Chamilo '.$system_version.'</b>. <br />'.str_replace('http://www.chamilo.org', '<a href="http://www.chamilo.org">http://www.chamilo.org</a>', get_lang('PleaseVisitOurWebsite')).'</span>';
  173. } else {
  174. $output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version.'</span>';
  175. }
  176. return $output;
  177. }
  178. return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
  179. }