admin.ajax.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. //require_once '../global.inc.php';
  7. \Chamilo\CoreBundle\Framework\Container::$legacyTemplate = 'layout_empty.html.twig';
  8. api_protect_admin_script();
  9. $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
  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. echo version_check();
  29. break;
  30. case 'get_extra_content':
  31. $blockName = isset($_POST['block']) ? Security::remove_XSS($_POST['block']) : null;
  32. if (empty($blockName)) {
  33. die;
  34. }
  35. if (api_is_multiple_url_enabled()) {
  36. $accessUrlId = api_get_current_access_url_id();
  37. if ($accessUrlId == -1) {
  38. die;
  39. }
  40. $urlInfo = api_get_access_url($accessUrlId);
  41. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $urlInfo['url']));
  42. $cleanUrl = str_replace('/', '-', $url);
  43. $newUrlDir = api_get_path(SYS_APP_PATH) . "home/$cleanUrl/admin/";
  44. } else {
  45. $newUrlDir = api_get_path(SYS_APP_PATH) . "home/admin/";
  46. }
  47. if (!file_exists($newUrlDir)) {
  48. die;
  49. }
  50. if (!Security::check_abs_path("{$newUrlDir}{$blockName}_extra.html", $newUrlDir)) {
  51. die;
  52. }
  53. if (!file_exists("{$newUrlDir}{$blockName}_extra.html")) {
  54. die;
  55. }
  56. echo file_get_contents("{$newUrlDir}{$blockName}_extra.html");
  57. break;
  58. }
  59. /**
  60. * Displays either the text for the registration or the message that the installation is (not) up to date
  61. *
  62. * @return string html code
  63. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  64. * @version august 2006
  65. * @todo have a 6 monthly re-registration
  66. */
  67. function version_check()
  68. {
  69. $tbl_settings = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  70. $sql = 'SELECT selected_value FROM '.$tbl_settings.' WHERE variable = "registered" ';
  71. $result = Database::query($sql);
  72. $row = Database::fetch_array($result, 'ASSOC');
  73. // The site has not been registered yet.
  74. $return = '';
  75. if ($row['selected_value'] == 'false') {
  76. $return .= get_lang('VersionCheckExplanation');
  77. $return .= '<form class="version-checking" action="'.api_get_path(WEB_CODE_PATH).'admin/index.php" id="VersionCheck" name="VersionCheck" method="post">';
  78. $return .= '<label class="checkbox"><input type="checkbox" name="donotlistcampus" value="1" id="checkbox" />'.get_lang('HideCampusFromPublicPlatformsList');
  79. $return .= '</label><button type="submit" class="btn btn-primary btn-block" name="Register" value="'.get_lang('EnableVersionCheck').'" id="register" >'.get_lang('EnableVersionCheck').'</button>';
  80. $return .= '</form>';
  81. check_system_version();
  82. } else {
  83. // site not registered. Call anyway
  84. $return .= check_system_version();
  85. }
  86. return $return;
  87. }
  88. /**
  89. * Check if the current installation is up to date
  90. * The code is borrowed from phpBB and slighlty modified
  91. * @author The phpBB Group <support@phpbb.com> (the code)
  92. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University (the modifications)
  93. * @author Yannick Warnier <ywarnier@beeznest.org> for the move to HTTP request
  94. * @copyright (C) 2001 The phpBB Group
  95. * @return string language string with some layout (color)
  96. */
  97. function check_system_version()
  98. {
  99. // the chamilo version of your installation
  100. $system_version = trim(api_get_configuration_value('system_version'));
  101. if (ini_get('allow_url_fopen') == 1) {
  102. // The number of courses
  103. $number_of_courses = Statistics::countCourses();
  104. // The number of users
  105. $number_of_users = Statistics::countUsers();
  106. $number_of_active_users = Statistics::countUsers(null, null, null, true);
  107. // The number of sessions
  108. $number_of_sessions = Statistics::countSessions();
  109. $packager = api_get_configuration_value('packager');
  110. if (empty($packager)) {
  111. $packager = 'chamilo';
  112. }
  113. $data = array(
  114. 'url' => api_get_path(WEB_PATH),
  115. 'campus' => api_get_setting('platform.site_name'),
  116. 'contact' => api_get_setting('admin.administrator_email'),
  117. // the admin's e-mail, with the only purpose of being able to contact admins to inform about critical security issues
  118. 'version' => $system_version,
  119. 'numberofcourses' => $number_of_courses, // to sum up into non-personal statistics - see https://version.chamilo.org/stats/
  120. 'numberofusers' => $number_of_users, // to sum up into non-personal statistics
  121. 'numberofactiveusers' => $number_of_active_users, // to sum up into non-personal statistics
  122. 'numberofsessions' => $number_of_sessions,
  123. //The donotlistcampus setting recovery should be improved to make
  124. // it true by default - this does not affect numbers counting
  125. 'donotlistcampus' => api_get_setting('platform.donotlistcampus'),
  126. 'organisation' => api_get_setting('platform.institution'),
  127. 'language' => api_get_setting('language.platform_language'), //helps us know the spread of language usage for campuses, by main language
  128. 'adminname' => api_get_setting(
  129. 'admin.administrator_name'
  130. ).' '.api_get_setting('admin.administrator_surname'),
  131. //not sure this is necessary...
  132. '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
  133. // Reference to the packager system or provider through which
  134. // Chamilo is installed/downloaded. Packagers can change this in
  135. // the default config file (main/install/configuration.dist.php)
  136. // or in the installed config file. The default value is 'chamilo'
  137. 'packager' => $packager,
  138. );
  139. $version = null;
  140. // version.php has been updated to include the version in an HTTP header
  141. // called "X-Chamilo-Version", so that we don't have to worry about
  142. // issues with the content not being returned by fread for some reason
  143. $res = _http_request('version.chamilo.org', 80, '/version.php', $data, 5, null, true);
  144. $lines = preg_split('/\r\n/', $res);
  145. foreach ($lines as $line) {
  146. $elements = preg_split('/:/', $line);
  147. // extract the X-Chamilo-Version header from the version.php response
  148. if (strcmp(trim($elements[0]), 'X-Chamilo-Version') === 0) {
  149. $version = trim($elements[1]);
  150. }
  151. }
  152. if (substr($res, 0, 5) != 'Error') {
  153. if (empty($version)) {
  154. $version_info = $res;
  155. } else {
  156. $version_info = $version;
  157. }
  158. if (version_compare($system_version, $version_info, '<=')) {
  159. $output = '<span style="color:red">' . get_lang('YourVersionNotUpToDate') .'<br />
  160. '.get_lang('LatestVersionIs').' <b>Chamilo '.$version_info.'</b>. <br />
  161. '.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>';
  162. } else {
  163. $output = '<span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version_info.'</span>';
  164. }
  165. } else {
  166. $output = '<span style="color:red">' . get_lang('ImpossibleToContactVersionServerPleaseTryAgain') . '</span>';
  167. }
  168. } else {
  169. $output = '<span style="color:red">' . get_lang('AllowurlfopenIsSetToOff') . '</span>';
  170. }
  171. return $output;
  172. }
  173. /**
  174. * Function to make an HTTP request through fsockopen (specialised for GET)
  175. * Derived from Jeremy Saintot: http://www.php.net/manual/en/function.fsockopen.php#101872
  176. * @param string $ip IP or hostname
  177. * @param int $port Target port
  178. * @param string $uri URI (defaults to '/')
  179. * @param array $getdata GET data
  180. * @param int $timeout Timeout
  181. * @param bool $req_hdr Include HTTP Request headers?
  182. * @param bool $res_hdr Include HTTP Response headers?
  183. * @return string
  184. */
  185. function _http_request($ip, $port = 80, $uri = '/', $getdata = array(), $timeout = 5, $req_hdr = false, $res_hdr = false)
  186. {
  187. $verb = 'GET';
  188. $ret = '';
  189. $getdata_str = count($getdata) ? '?' : '';
  190. foreach ($getdata as $k => $v) {
  191. $getdata_str .= urlencode($k) .'='. urlencode($v) . '&';
  192. }
  193. $crlf = "\r\n";
  194. $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  195. $req .= 'Host: '. $ip . $crlf;
  196. $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
  197. $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
  198. $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
  199. $req .= 'Accept-Encoding: deflate' . $crlf;
  200. $req .= 'Accept-Charset: utf-8;q=0.7,*;q=0.7' . $crlf;
  201. $req .= $crlf;
  202. if ($req_hdr) {
  203. $ret .= $req;
  204. }
  205. if (($fp = @fsockopen($ip, $port, $errno, $errstr, $timeout)) == false) {
  206. return "Error $errno: $errstr\n";
  207. }
  208. stream_set_timeout($fp, $timeout);
  209. $r = fwrite($fp, $req);
  210. $line = @fread($fp, 512);
  211. $ret .= $line;
  212. fclose($fp);
  213. if (!$res_hdr) {
  214. $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  215. }
  216. return trim($ret);
  217. }