admin.ajax.php 10.0 KB

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