admin.ajax.php 10 KB

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