admin.ajax.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. // Language files that need to be included.
  7. $language_file = array('admin');
  8. require_once '../global.inc.php';
  9. require_once api_get_path(SYS_CODE_PATH).'admin/statistics/statistics.lib.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 = array('variable = ? ' => array($_GET['id']));
  18. $data = api_get_settings_params($params);
  19. if (!empty($data)) {
  20. foreach ($data as $item) {
  21. $params = array('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. echo version_check();
  31. break;
  32. }
  33. /**
  34. * Displays either the text for the registration or the message that the installation is (not) up to date
  35. *
  36. * @return string html code
  37. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  38. * @version august 2006
  39. * @todo have a 6 monthly re-registration
  40. */
  41. function version_check()
  42. {
  43. $tbl_settings = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  44. $sql = 'SELECT selected_value FROM '.$tbl_settings.' WHERE variable = "registered" ';
  45. $result = Database::query($sql);
  46. $row = Database::fetch_array($result, 'ASSOC');
  47. // The site has not been registered yet.
  48. $return = '';
  49. if ($row['selected_value'] == 'false') {
  50. $return .= get_lang('VersionCheckExplanation');
  51. $return .= '<form class="well" action="'.api_get_path(WEB_CODE_PATH).'admin/index.php" id="VersionCheck" name="VersionCheck" method="post">';
  52. $return .= '<label class="checkbox"><input type="checkbox" name="donotlistcampus" value="1" id="checkbox" />'.get_lang('HideCampusFromPublicPlatformsList');
  53. $return .= '</label><button type="submit" class="btn btn-primary" name="Register" value="'.get_lang('EnableVersionCheck').'" id="register" >'.get_lang('EnableVersionCheck').'</button>';
  54. $return .= '</form>';
  55. check_system_version();
  56. } else {
  57. // site not registered. Call anyway
  58. $return .= check_system_version();
  59. }
  60. return $return;
  61. }
  62. /**
  63. * Check if the current installation is up to date
  64. * The code is borrowed from phpBB and slighlty modified
  65. * @author The phpBB Group <support@phpbb.com> (the code)
  66. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University (the modifications)
  67. * @author Yannick Warnier <ywarnier@beeznest.org> for the move to HTTP request
  68. * @copyright (C) 2001 The phpBB Group
  69. * @return string language string with some layout (color)
  70. */
  71. function check_system_version()
  72. {
  73. global $_configuration;
  74. $system_version = trim($_configuration['system_version']); // the chamilo version of your installation
  75. if (ini_get('allow_url_fopen') == 1) {
  76. // The number of courses
  77. $number_of_courses = Statistics::count_courses();
  78. // The number of users
  79. $number_of_users = Statistics::count_users();
  80. $number_of_active_users = Statistics::count_users(null, null, null, true);
  81. // The number of sessions
  82. $number_of_sessions = Statistics::count_sessions();
  83. $data = array(
  84. 'url' => api_get_path(WEB_PATH),
  85. 'campus' => api_get_setting('siteName'),
  86. 'contact' => api_get_setting('emailAdministrator'),
  87. 'version' => $system_version,
  88. 'numberofcourses' => $number_of_courses,
  89. 'numberofusers' => $number_of_users,
  90. 'numberofactiveusers' => $number_of_active_users,
  91. 'numberofsessions' => $number_of_sessions,
  92. //The donotlistcampus setting recovery should be improved to make
  93. // it true by default - this does not affect numbers counting
  94. 'donotlistcampus' => api_get_setting('donotlistcampus'),
  95. 'organisation' => api_get_setting('Institution'),
  96. 'language' => api_get_setting('platformLanguage'),
  97. 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'),
  98. );
  99. $version = null;
  100. // version.php has been updated to include the version in an HTTP header
  101. // called "X-Chamilo-Version", so that we don't have to worry about
  102. // issues with the content not being returned by fread for some reason
  103. $res = _http_request('version.chamilo.org', 80, '/version.php', $data, 5, null, true);
  104. $lines = preg_split('/\r\n/', $res);
  105. foreach ($lines as $line) {
  106. $elements = preg_split('/:/', $line);
  107. // extract the X-Chamilo-Version header from the version.php response
  108. if (strcmp(trim($elements[0]), 'X-Chamilo-Version') === 0) {
  109. $version = trim($elements[1]);
  110. }
  111. }
  112. if (substr($res, 0, 5) != 'Error') {
  113. if (empty($version)) {
  114. $version_info = $res;
  115. } else {
  116. $version_info = $version;
  117. }
  118. if ($system_version != $version_info) {
  119. $output = '<br /><span style="color:red">' . get_lang('YourVersionNotUpToDate') . '. '.get_lang('LatestVersionIs').' <b>Chamilo '.$version_info.'</b>. '.get_lang('YourVersionIs').' <b>Chamilo '.$system_version. '</b>. '.str_replace('http://www.chamilo.org', '<a href="http://www.chamilo.org">http://www.chamilo.org</a>', get_lang('PleaseVisitOurWebsite')).'</span>';
  120. } else {
  121. $output = '<br /><span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version_info.'</span>';
  122. }
  123. } else {
  124. $output = '<span style="color:red">' . get_lang('ImpossibleToContactVersionServerPleaseTryAgain') . '</span>';
  125. }
  126. } else {
  127. $output = '<span style="color:red">' . get_lang('AllowurlfopenIsSetToOff') . '</span>';
  128. }
  129. return $output;
  130. }
  131. /**
  132. * Function to make an HTTP request through fsockopen (specialised for GET)
  133. * Derived from Jeremy Saintot: http://www.php.net/manual/en/function.fsockopen.php#101872
  134. * @param string IP or hostname
  135. * @param int Target port
  136. * @param string URI (defaults to '/')
  137. * @param array GET data
  138. * @param float Timeout
  139. * @param bool Include HTTP Request headers?
  140. * @param bool Include HTTP Response headers?
  141. */
  142. function _http_request($ip, $port = 80, $uri = '/', $getdata = array(), $timeout = 5, $req_hdr = false, $res_hdr = false)
  143. {
  144. $verb = 'GET';
  145. $ret = '';
  146. $getdata_str = count($getdata) ? '?' : '';
  147. foreach ($getdata as $k => $v) {
  148. $getdata_str .= urlencode($k) .'='. urlencode($v) . '&';
  149. }
  150. $crlf = "\r\n";
  151. $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  152. $req .= 'Host: '. $ip . $crlf;
  153. $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
  154. $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
  155. $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
  156. $req .= 'Accept-Encoding: deflate' . $crlf;
  157. $req .= 'Accept-Charset: utf-8;q=0.7,*;q=0.7' . $crlf;
  158. $req .= $crlf;
  159. if ($req_hdr) {
  160. $ret .= $req;
  161. }
  162. if (($fp = @fsockopen($ip, $port, $errno, $errstr, $timeout)) == false) {
  163. return "Error $errno: $errstr\n";
  164. }
  165. stream_set_timeout($fp, $timeout);
  166. $r = fwrite($fp, $req);
  167. $line = @fread($fp, 512);
  168. $ret .= $line;
  169. fclose($fp);
  170. if (!$res_hdr) {
  171. $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  172. }
  173. return trim($ret);
  174. }