admin.ajax.php 9.8 KB

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