admin.ajax.php 9.8 KB

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