admin.ajax.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. require_once '../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_PATH) . "home/$cleanUrl/admin/";
  43. } else {
  44. $newUrlDir = api_get_path(SYS_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. global $_configuration;
  99. $system_version = trim($_configuration['system_version']); // the chamilo version of your installation
  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. $data = array(
  109. 'url' => api_get_path(WEB_PATH),
  110. 'campus' => api_get_setting('siteName'),
  111. 'contact' => api_get_setting('emailAdministrator'),
  112. 'version' => $system_version,
  113. 'numberofcourses' => $number_of_courses,
  114. 'numberofusers' => $number_of_users,
  115. 'numberofactiveusers' => $number_of_active_users,
  116. 'numberofsessions' => $number_of_sessions,
  117. //The donotlistcampus setting recovery should be improved to make
  118. // it true by default - this does not affect numbers counting
  119. 'donotlistcampus' => api_get_setting('donotlistcampus'),
  120. 'organisation' => api_get_setting('Institution'),
  121. 'language' => api_get_setting('platformLanguage'),
  122. 'adminname' => api_get_setting('administratorName').' '.api_get_setting('administratorSurname'),
  123. );
  124. $version = null;
  125. // version.php has been updated to include the version in an HTTP header
  126. // called "X-Chamilo-Version", so that we don't have to worry about
  127. // issues with the content not being returned by fread for some reason
  128. $res = _http_request('version.chamilo.org', 80, '/version.php', $data, 5, null, true);
  129. $lines = preg_split('/\r\n/', $res);
  130. foreach ($lines as $line) {
  131. $elements = preg_split('/:/', $line);
  132. // extract the X-Chamilo-Version header from the version.php response
  133. if (strcmp(trim($elements[0]), 'X-Chamilo-Version') === 0) {
  134. $version = trim($elements[1]);
  135. }
  136. }
  137. if (substr($res, 0, 5) != 'Error') {
  138. if (empty($version)) {
  139. $version_info = $res;
  140. } else {
  141. $version_info = $version;
  142. }
  143. if ($system_version != $version_info) {
  144. $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>';
  145. } else {
  146. $output = '<br /><span style="color:green">'.get_lang('VersionUpToDate').': Chamilo '.$version_info.'</span>';
  147. }
  148. } else {
  149. $output = '<span style="color:red">' . get_lang('ImpossibleToContactVersionServerPleaseTryAgain') . '</span>';
  150. }
  151. } else {
  152. $output = '<span style="color:red">' . get_lang('AllowurlfopenIsSetToOff') . '</span>';
  153. }
  154. return $output;
  155. }
  156. /**
  157. * Function to make an HTTP request through fsockopen (specialised for GET)
  158. * Derived from Jeremy Saintot: http://www.php.net/manual/en/function.fsockopen.php#101872
  159. * @param string IP or hostname
  160. * @param int Target port
  161. * @param string URI (defaults to '/')
  162. * @param array GET data
  163. * @param float Timeout
  164. * @param bool Include HTTP Request headers?
  165. * @param bool Include HTTP Response headers?
  166. */
  167. function _http_request($ip, $port = 80, $uri = '/', $getdata = array(), $timeout = 5, $req_hdr = false, $res_hdr = false)
  168. {
  169. $verb = 'GET';
  170. $ret = '';
  171. $getdata_str = count($getdata) ? '?' : '';
  172. foreach ($getdata as $k => $v) {
  173. $getdata_str .= urlencode($k) .'='. urlencode($v) . '&';
  174. }
  175. $crlf = "\r\n";
  176. $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  177. $req .= 'Host: '. $ip . $crlf;
  178. $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
  179. $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
  180. $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
  181. $req .= 'Accept-Encoding: deflate' . $crlf;
  182. $req .= 'Accept-Charset: utf-8;q=0.7,*;q=0.7' . $crlf;
  183. $req .= $crlf;
  184. if ($req_hdr) {
  185. $ret .= $req;
  186. }
  187. if (($fp = @fsockopen($ip, $port, $errno, $errstr, $timeout)) == false) {
  188. return "Error $errno: $errstr\n";
  189. }
  190. stream_set_timeout($fp, $timeout);
  191. $r = fwrite($fp, $req);
  192. $line = @fread($fp, 512);
  193. $ret .= $line;
  194. fclose($fp);
  195. if (!$res_hdr) {
  196. $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  197. }
  198. return trim($ret);
  199. }