admin.ajax.php 8.3 KB

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