admin.ajax.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\BranchSync;
  4. use Chamilo\CoreBundle\Entity\Repository\BranchSyncRepository;
  5. use GuzzleHttp\Client;
  6. /**
  7. * Responses to AJAX calls.
  8. */
  9. require_once __DIR__.'/../global.inc.php';
  10. api_protect_admin_script();
  11. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  12. switch ($action) {
  13. case 'update_changeable_setting':
  14. $url_id = api_get_current_access_url_id();
  15. if (api_is_global_platform_admin() && $url_id == 1) {
  16. if (isset($_GET['id']) && !empty($_GET['id'])) {
  17. $params = ['variable = ? ' => [$_GET['id']]];
  18. $data = api_get_settings_params($params);
  19. if (!empty($data)) {
  20. foreach ($data as $item) {
  21. $params = ['id' => $item['id'], 'access_url_changeable' => $_GET['changeable']];
  22. api_set_setting_simple($params);
  23. }
  24. }
  25. echo '1';
  26. }
  27. }
  28. break;
  29. case 'version':
  30. // Fix session block when loading admin/index.php and changing page
  31. session_write_close();
  32. echo version_check();
  33. break;
  34. case 'get_extra_content':
  35. $blockName = isset($_POST['block']) ? Security::remove_XSS($_POST['block']) : null;
  36. if (empty($blockName)) {
  37. die;
  38. }
  39. if (api_is_multiple_url_enabled()) {
  40. $accessUrlId = api_get_current_access_url_id();
  41. if ($accessUrlId == -1) {
  42. die;
  43. }
  44. $urlInfo = api_get_access_url($accessUrlId);
  45. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $urlInfo['url']));
  46. $cleanUrl = str_replace('/', '-', $url);
  47. $newUrlDir = api_get_path(SYS_APP_PATH)."home/$cleanUrl/admin/";
  48. } else {
  49. $newUrlDir = api_get_path(SYS_APP_PATH)."home/admin/";
  50. }
  51. if (!file_exists($newUrlDir)) {
  52. die;
  53. }
  54. if (!Security::check_abs_path("{$newUrlDir}{$blockName}_extra.html", $newUrlDir)) {
  55. die;
  56. }
  57. if (!file_exists("{$newUrlDir}{$blockName}_extra.html")) {
  58. die;
  59. }
  60. echo file_get_contents("{$newUrlDir}{$blockName}_extra.html");
  61. break;
  62. case 'get_latest_news':
  63. if (api_get_configuration_value('admin_chamilo_announcements_disable') === true) {
  64. break;
  65. }
  66. try {
  67. $latestNews = getLatestNews();
  68. $latestNews = json_decode($latestNews, true);
  69. echo Security::remove_XSS($latestNews['text'], COURSEMANAGER);
  70. break;
  71. } catch (Exception $e) {
  72. break;
  73. }
  74. }
  75. /**
  76. * Displays either the text for the registration or the message that the installation is (not) up to date.
  77. *
  78. * @return string html code
  79. *
  80. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  81. *
  82. * @version august 2006
  83. *
  84. * @todo have a 6 monthly re-registration
  85. */
  86. function version_check()
  87. {
  88. $tbl_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  89. $sql = 'SELECT selected_value FROM '.$tbl_settings.' WHERE variable = "registered" ';
  90. $result = Database::query($sql);
  91. $row = Database::fetch_array($result, 'ASSOC');
  92. // The site has not been registered yet.
  93. $return = '';
  94. if ($row['selected_value'] == 'false') {
  95. $return .= get_lang('VersionCheckExplanation');
  96. $return .= '<form class="version-checking" action="'.api_get_path(WEB_CODE_PATH).'admin/index.php" id="VersionCheck" name="VersionCheck" method="post">';
  97. $return .= '<label class="checkbox"><input type="checkbox" name="donotlistcampus" value="1" id="checkbox" />'.get_lang('HideCampusFromPublicPlatformsList');
  98. $return .= '</label><button type="submit" class="btn btn-primary btn-block" name="Register" value="'.get_lang('EnableVersionCheck').'" id="register" >'.get_lang('EnableVersionCheck').'</button>';
  99. $return .= '</form>';
  100. check_system_version();
  101. } else {
  102. // site not registered. Call anyway
  103. $return .= check_system_version();
  104. }
  105. return $return;
  106. }
  107. /**
  108. * Check if the current installation is up to date
  109. * The code is borrowed from phpBB and slighlty modified.
  110. *
  111. * @throws \Exception
  112. * @throws \InvalidArgumentException
  113. *
  114. * @return string language string with some layout (color)
  115. */
  116. function check_system_version()
  117. {
  118. // Check if curl is available.
  119. if (!in_array('curl', get_loaded_extensions())) {
  120. return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
  121. }
  122. $url = 'https://version.chamilo.org';
  123. $options = [
  124. 'verify' => false,
  125. ];
  126. $urlValidated = false;
  127. try {
  128. $client = new GuzzleHttp\Client();
  129. $res = $client->request('GET', $url, $options);
  130. if ($res->getStatusCode() == '200' || $res->getStatusCode() == '301') {
  131. $urlValidated = true;
  132. }
  133. } catch (Exception $e) {
  134. }
  135. // the chamilo version of your installation
  136. $system_version = trim(api_get_configuration_value('system_version'));
  137. if ($urlValidated) {
  138. // The number of courses
  139. $number_of_courses = Statistics::countCourses();
  140. // The number of users
  141. $number_of_users = Statistics::countUsers();
  142. $number_of_active_users = Statistics::countUsers(
  143. null,
  144. null,
  145. null,
  146. true
  147. );
  148. // The number of sessions
  149. $number_of_sessions = SessionManager::count_sessions(api_get_current_access_url_id());
  150. $packager = api_get_configuration_value('packager');
  151. if (empty($packager)) {
  152. $packager = 'chamilo';
  153. }
  154. $uniqueId = '';
  155. $entityManager = Database::getManager();
  156. /** @var BranchSyncRepository $branch */
  157. $repository = $entityManager->getRepository('ChamiloCoreBundle:BranchSync');
  158. /** @var BranchSync $branch */
  159. $branch = $repository->getTopBranch();
  160. if (is_a($branch, '\Chamilo\CoreBundle\Entity\BranchSync')) {
  161. $uniqueId = $branch->getUniqueId();
  162. }
  163. $data = [
  164. 'url' => api_get_path(WEB_PATH),
  165. 'campus' => api_get_setting('siteName'),
  166. '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
  167. 'version' => $system_version,
  168. 'numberofcourses' => $number_of_courses, // to sum up into non-personal statistics - see https://version.chamilo.org/stats/
  169. 'numberofusers' => $number_of_users, // to sum up into non-personal statistics
  170. 'numberofactiveusers' => $number_of_active_users, // to sum up into non-personal statistics
  171. 'numberofsessions' => $number_of_sessions,
  172. //The donotlistcampus setting recovery should be improved to make
  173. // it true by default - this does not affect numbers counting
  174. 'donotlistcampus' => api_get_setting('donotlistcampus'),
  175. 'organisation' => api_get_setting('Institution'),
  176. 'language' => api_get_setting('platformLanguage'), //helps us know the spread of language usage for campuses, by main language
  177. 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'), //not sure this is necessary...
  178. '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
  179. // Reference to the packager system or provider through which
  180. // Chamilo is installed/downloaded. Packagers can change this in
  181. // the default config file (main/install/configuration.dist.php)
  182. // or in the installed config file. The default value is 'chamilo'
  183. 'packager' => $packager,
  184. 'unique_id' => $uniqueId,
  185. ];
  186. $version = null;
  187. $client = new GuzzleHttp\Client();
  188. $url .= '?';
  189. foreach ($data as $k => $v) {
  190. $url .= urlencode($k).'='.urlencode($v).'&';
  191. }
  192. $res = $client->request('GET', $url, $options);
  193. if ($res->getStatusCode() == '200') {
  194. $versionData = $res->getHeader('X-Chamilo-Version');
  195. if (isset($versionData[0])) {
  196. $version = trim($versionData[0]);
  197. }
  198. }
  199. if (version_compare($system_version, $version, '<')) {
  200. $output = '<span style="color:red">'.get_lang('YourVersionNotUpToDate').'<br />
  201. '.get_lang('LatestVersionIs').' <b>Chamilo '.$version.'</b>. <br />
  202. '.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>';
  203. } else {
  204. $output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version.'</span>';
  205. }
  206. return $output;
  207. }
  208. return '<span style="color:red">'.get_lang('ImpossibleToContactVersionServerPleaseTryAgain').'</span>';
  209. }
  210. /**
  211. * Display the latest news from the Chamilo Association for admins.
  212. *
  213. * @throws \GuzzleHttp\Exception\GuzzleException
  214. * @throws Exception
  215. *
  216. * @return string|void
  217. */
  218. function getLatestNews()
  219. {
  220. $url = 'https://version.chamilo.org/news/latest.php';
  221. $client = new Client();
  222. $response = $client->request(
  223. 'GET',
  224. $url,
  225. [
  226. 'query' => [
  227. 'language' => api_get_interface_language(),
  228. ],
  229. ]
  230. );
  231. if ($response->getStatusCode() !== 200) {
  232. throw new Exception(get_lang('DenyEntry'));
  233. }
  234. return $response->getBody()->getContents();
  235. }