global.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. use Patchwork\Utf8\Bootup;
  5. use Symfony\Component\Dotenv\Dotenv;
  6. /**
  7. * It is recommended that ALL Chamilo scripts include this important file.
  8. * This script manages
  9. * - include of /app/config/configuration.php;
  10. * - include of several libraries: api, database, display, text, security;
  11. * - selecting the main database;
  12. * - include of language files.
  13. *
  14. * @package chamilo.include
  15. *
  16. * @todo remove the code that displays the button that links to the install page
  17. */
  18. // Specification for usernames:
  19. // 1. ASCII-letters, digits, "." (dot), "_" (underscore) are acceptable, 40 characters maximum length.
  20. // 2. Empty username is formally valid, but it is reserved for the anonymous user.
  21. // 3. Checking the login_is_email portal setting in order to accept 100 chars maximum
  22. define('USERNAME_MAX_LENGTH', 100);
  23. require_once __DIR__.'/../../vendor/autoload.php';
  24. try {
  25. // Check the PHP version
  26. api_check_php_version();
  27. // Get settings from .env file created when installation Chamilo
  28. $envFile = __DIR__.'/../../.env.local';
  29. if (file_exists($envFile)) {
  30. (new Dotenv())->load($envFile);
  31. } else {
  32. throw new \RuntimeException('APP_ENV environment variable is not defined.
  33. You need to define environment variables for configuration to load variables from a .env.local file.');
  34. }
  35. $env = $_SERVER['APP_ENV'] ?? 'dev';
  36. $append = $_SERVER['APP_URL_APPEND'] ?? '';
  37. $kernel = new Chamilo\Kernel($env, true);
  38. $request = Sonata\PageBundle\Request\RequestFactory::createFromGlobals('host_with_path_by_locale');
  39. // This 'load_legacy' variable is needed to know that symfony is loaded using old style legacy mode,
  40. // and not called from a symfony controller from public/
  41. $request->request->set('load_legacy', true);
  42. // @todo fix URL loading
  43. $request->setBaseUrl($request->getRequestUri());
  44. $kernel->boot();
  45. if (!empty($append)) {
  46. if (substr($append, 0, 1) !== '/') {
  47. echo 'APP_URL_APPEND must start with "/"';
  48. exit;
  49. }
  50. $append = "$append/";
  51. }
  52. $container = $kernel->getContainer();
  53. $router = $container->get('router');
  54. $context = $router->getContext();
  55. $context->setBaseUrl($append);
  56. $router->setContext($context);
  57. $response = $kernel->handle($request);
  58. $context = Container::getRouter()->getContext();
  59. //var_dump($append);
  60. $append .= 'public';
  61. $context->setBaseUrl($append);
  62. $container = $kernel->getContainer();
  63. if ($kernel->isInstalled()) {
  64. require_once $kernel->getConfigurationFile();
  65. } else {
  66. throw new Exception('Chamilo is not installed');
  67. }
  68. //$kernel->setApi($_configuration);
  69. if (!isset($GLOBALS['_configuration'])) {
  70. $GLOBALS['_configuration'] = $_configuration;
  71. }
  72. // Do not over-use this variable. It is only for this script's local use.
  73. $libraryPath = __DIR__.'/lib/';
  74. $container = $kernel->getContainer();
  75. // Symfony uses request_stack now
  76. $container->get('request_stack')->push($request);
  77. // Connect Chamilo with the Symfony container
  78. // Container::setContainer($container);
  79. // Container::setLegacyServices($container);
  80. // The code below is not needed. The connections is now made in the file:
  81. // src/CoreBundle/EventListener/LegacyListener.php
  82. // This is called when when doing the $kernel->handle
  83. // Fix chamilo URL when used inside a folder: example.com/chamilo
  84. /*$append = $kernel->getUrlAppend();
  85. $appendValue = '';
  86. if (!empty($append)) {
  87. $appendValue = "/$append/";
  88. }*/
  89. /*$router = $container->get('router');
  90. $context = $container->get('router.request_context');
  91. $host = $router->getContext()->getHost();
  92. $context->setBaseUrl($appendValue);
  93. $container->set('router.request_context', $context);*/
  94. /*$version = new Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy();
  95. $newDefault = new Symfony\Component\Asset\PathPackage($appendValue.'public/', $version);
  96. $packages = $container->get('assets.packages');
  97. $packages->setDefaultPackage($newDefault);
  98. $container->get('chamilo_core.menu.nav_builder')->setContainer($container);*/
  99. /* RETRIEVING ALL THE CHAMILO CONFIG SETTINGS FOR MULTIPLE URLs FEATURE*/
  100. /*if (!empty($_configuration['multiple_access_urls'])) {
  101. $_configuration['access_url'] = 1;
  102. $access_urls = api_get_access_urls();
  103. $root_rel = api_get_self();
  104. $root_rel = substr($root_rel, 1);
  105. $pos = strpos($root_rel, '/');
  106. $root_rel = substr($root_rel, 0, $pos);
  107. $protocol = 'http://';
  108. if (!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') {
  109. $protocol = 'https://';
  110. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  111. $protocol = 'https://';
  112. }
  113. //urls with subdomains (HTTP_HOST is preferred - see #6764)
  114. $request_url_root = '';
  115. if (empty($_SERVER['HTTP_HOST'])) {
  116. if (empty($_SERVER['SERVER_NAME'])) {
  117. $request_url_root = $protocol.'localhost/';
  118. } else {
  119. $request_url_root = $protocol.$_SERVER['SERVER_NAME'].'/';
  120. }
  121. } else {
  122. $request_url_root = $protocol.$_SERVER['HTTP_HOST'].'/';
  123. }
  124. //urls with subdirs
  125. $request_url_sub = $request_url_root.$root_rel.'/';
  126. // You can use subdirs as multi-urls, but in this case none of them can be
  127. // the root dir. The admin portal should be something like https://host/adm/
  128. // At this time, subdirs will still hold a share cookie, so not ideal yet
  129. // see #6510
  130. foreach ($access_urls as $details) {
  131. if ($request_url_sub == $details['url']) {
  132. $_configuration['access_url'] = $details['id'];
  133. break; //found one match with subdir, get out of foreach
  134. }
  135. // Didn't find any? Now try without subdirs
  136. if ($request_url_root == $details['url']) {
  137. $_configuration['access_url'] = $details['id'];
  138. break; //found one match, get out of foreach
  139. }
  140. }
  141. } else {
  142. $_configuration['access_url'] = 1;
  143. }*/
  144. // Check if APCu is available. If so, store the value in $_configuration
  145. /*if (extension_loaded('apcu')) {
  146. $apcEnabled = ini_get('apc.enabled');
  147. if (!empty($apcEnabled) && $apcEnabled != 'Off' && $apcEnabled != 'off') {
  148. $_configuration['apc'] = true;
  149. $_configuration['apc_prefix'] = $_configuration['main_database'].'_'.$_configuration['access_url'].'_';
  150. }
  151. }*/
  152. $charset = 'UTF-8';
  153. // Enables the portability layer and configures PHP for UTF-8
  154. Bootup::initAll();
  155. // access_url == 1 is the default chamilo location
  156. /*if ($_configuration['access_url'] != 1) {
  157. $url_info = api_get_access_url($_configuration['access_url']);
  158. if ($url_info['active'] == 1) {
  159. $settings_by_access = &api_get_settings(null, 'list', $_configuration['access_url'], 1);
  160. foreach ($settings_by_access as &$row) {
  161. if (empty($row['variable'])) {
  162. $row['variable'] = 0;
  163. }
  164. if (empty($row['subkey'])) {
  165. $row['subkey'] = 0;
  166. }
  167. if (empty($row['category'])) {
  168. $row['category'] = 0;
  169. }
  170. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = $row;
  171. }
  172. }
  173. }*/
  174. /*$result = &api_get_settings(null, 'list', 1);
  175. foreach ($result as &$row) {
  176. if ($_configuration['access_url'] != 1) {
  177. if ($url_info['active'] == 1) {
  178. $var = empty($row['variable']) ? 0 : $row['variable'];
  179. $subkey = empty($row['subkey']) ? 0 : $row['subkey'];
  180. $category = empty($row['category']) ? 0 : $row['category'];
  181. }
  182. if ($row['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  183. if (isset($settings_by_access_list[$var]) &&
  184. isset($settings_by_access_list[$var][$subkey]) &&
  185. $settings_by_access_list[$var][$subkey][$category]['selected_value'] != '') {
  186. if ($row['subkey'] == null) {
  187. $_setting[$row['variable']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  188. } else {
  189. $_setting[$row['variable']][$row['subkey']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  190. }
  191. } else {
  192. if ($row['subkey'] == null) {
  193. $_setting[$row['variable']] = $row['selected_value'];
  194. } else {
  195. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  196. }
  197. }
  198. } else {
  199. if ($row['subkey'] == null) {
  200. $_setting[$row['variable']] = $row['selected_value'];
  201. } else {
  202. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  203. }
  204. }
  205. } else {
  206. if ($row['subkey'] == null) {
  207. $_setting[$row['variable']] = $row['selected_value'];
  208. } else {
  209. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  210. }
  211. }
  212. }*/
  213. /*$result = &api_get_settings('Plugins', 'list', $_configuration['access_url']);
  214. $_plugins = [];
  215. foreach ($result as &$row) {
  216. $key = &$row['variable'];
  217. if (isset($_setting[$key]) && is_string($_setting[$key])) {
  218. $_setting[$key] = [];
  219. }
  220. if ($row['subkey'] == null) {
  221. $_setting[$key][] = $row['selected_value'];
  222. $_plugins[$key][] = $row['selected_value'];
  223. } else {
  224. $_setting[$key][$row['subkey']] = $row['selected_value'];
  225. $_plugins[$key][$row['subkey']] = $row['selected_value'];
  226. }
  227. }*/
  228. // Error reporting settings.
  229. /*if (api_get_setting('server_type') === 'test') {
  230. ini_set('display_errors', '1');
  231. ini_set('html_errors', '1');
  232. error_reporting(-1);
  233. if (function_exists('opcache_reset')) {
  234. opcache_reset();
  235. }
  236. } else {
  237. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  238. }*/
  239. ini_set('log_errors', '1');
  240. // Load allowed tag definitions for kses and/or HTMLPurifier.
  241. //require_once $libraryPath.'formvalidator/Rule/allowed_tags.inc.php';
  242. // Before we call local.inc.php, let's define a global $this_section variable
  243. // which will then be usable from the banner and header scripts
  244. $this_section = SECTION_GLOBAL;
  245. // Include Chamilo Mail conf this is added here because the api_get_setting works
  246. // Fixes bug in Chamilo 1.8.7.1 array was not set
  247. //$administrator['email'] = isset($administrator['email']) ? $administrator['email'] : 'admin@example.com';
  248. //$administrator['name'] = isset($administrator['name']) ? $administrator['name'] : 'Admin';
  249. /* LOAD LANGUAGE FILES SECTION */
  250. // if we use the javascript version (without go button) we receive a get
  251. // if we use the non-javascript version (with the go button) we receive a post
  252. /*$user_language = '';
  253. $browser_language = '';
  254. // see #8149
  255. if (!empty($_SESSION['user_language_choice'])) {
  256. $user_language = $_SESSION['user_language_choice'];
  257. }
  258. if (!empty($_GET['language'])) {
  259. $user_language = $_GET['language'];
  260. }
  261. if (!empty($_POST['language_list'])) {
  262. $user_language = str_replace('index.php?language=', '', $_POST['language_list']);
  263. }
  264. if (empty($user_language) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && !isset($_SESSION['_user'])) {
  265. $l = SubLanguageManager::getLanguageFromBrowserPreference($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  266. if (!empty($l)) {
  267. $user_language = $browser_language = $l;
  268. }
  269. }*/
  270. // Include all files (first english and then current interface language)
  271. /*$langpath = api_get_path(SYS_LANG_PATH);
  272. // This will only work if we are in the page to edit a sub_language
  273. if (isset($this_script) && $this_script == 'sub_language') {
  274. // getting the arrays of files i.e notification, trad4all, etc
  275. $language_files_to_load = SubLanguageManager:: get_lang_folder_files_list(
  276. api_get_path(SYS_LANG_PATH).'english',
  277. true
  278. );
  279. //getting parent info
  280. $parent_language = SubLanguageManager::get_all_information_of_language($_REQUEST['id']);
  281. //getting sub language info
  282. $sub_language = SubLanguageManager::get_all_information_of_language($_REQUEST['sub_language_id']);
  283. $english_language_array = $parent_language_array = $sub_language_array = [];
  284. foreach ($language_files_to_load as $language_file_item) {
  285. $lang_list_pre = array_keys($GLOBALS);
  286. //loading english
  287. $path = $langpath.'english/'.$language_file_item.'.inc.php';
  288. if (file_exists($path)) {
  289. include $path;
  290. }
  291. $lang_list_post = array_keys($GLOBALS);
  292. $lang_list_result = array_diff($lang_list_post, $lang_list_pre);
  293. unset($lang_list_pre);
  294. // english language array
  295. $english_language_array[$language_file_item] = compact($lang_list_result);
  296. //cleaning the variables
  297. foreach ($lang_list_result as $item) {
  298. unset(${$item});
  299. }
  300. $parent_file = $langpath.$parent_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  301. if (file_exists($parent_file) && is_file($parent_file)) {
  302. include_once $parent_file;
  303. }
  304. // parent language array
  305. $parent_language_array[$language_file_item] = compact($lang_list_result);
  306. //cleaning the variables
  307. foreach ($lang_list_result as $item) {
  308. unset(${$item});
  309. }
  310. $sub_file = $langpath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  311. if (file_exists($sub_file) && is_file($sub_file)) {
  312. include $sub_file;
  313. }
  314. // sub language array
  315. $sub_language_array[$language_file_item] = compact($lang_list_result);
  316. //cleaning the variables
  317. foreach ($lang_list_result as $item) {
  318. unset(${$item});
  319. }
  320. }
  321. }*/
  322. /**
  323. * Include the trad4all language file.
  324. */
  325. // if the sub-language feature is on
  326. /*$parent_path = SubLanguageManager::get_parent_language_path($language_interface);
  327. if (!empty($parent_path)) {
  328. // include English
  329. include $langpath.'english/trad4all.inc.php';
  330. // prepare string for current language and its parent
  331. $lang_file = $langpath.$language_interface.'/trad4all.inc.php';
  332. $parent_lang_file = $langpath.$parent_path.'/trad4all.inc.php';
  333. // load the parent language file first
  334. if (file_exists($parent_lang_file)) {
  335. include $parent_lang_file;
  336. }
  337. // overwrite the parent language translations if there is a child
  338. if (file_exists($lang_file)) {
  339. include $lang_file;
  340. }
  341. } else {
  342. // if the sub-languages feature is not on, then just load the
  343. // set language interface
  344. // include English
  345. include $langpath.'english/trad4all.inc.php';
  346. // prepare string for current language
  347. $langfile = $langpath.$language_interface.'/trad4all.inc.php';
  348. if (file_exists($langfile)) {
  349. include $langfile;
  350. }
  351. }*/
  352. // include the local (contextual) parameters of this course or section
  353. //require_once __DIR__.'/local.inc.php';
  354. //$_user = api_get_user_info();
  355. // The global variable $text_dir has been defined in the language file trad4all.inc.php.
  356. // For determining text direction correspondent to the current language
  357. // we use now information from the internationalization library.
  358. //$text_dir = api_get_text_direction();
  359. // ===== "who is logged in?" module section =====
  360. // check and modify the date of user in the track.e.online table
  361. /*if (isset($_user['user_id'])) {
  362. if (!$x = strpos($_SERVER['PHP_SELF'], 'whoisonline.php')) {
  363. preventMultipleLogin($_user['user_id']);
  364. LoginCheck($_user['user_id']);
  365. }
  366. // Update of the logout_date field in the table track_e_login
  367. // (needed for the calculation of the total connection time)
  368. if (!isset($_SESSION['login_as'])) {
  369. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  370. $tbl_track_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  371. $sql = "SELECT login_id, login_date
  372. FROM $tbl_track_login
  373. WHERE
  374. login_user_id='".$_user["user_id"]."'
  375. ORDER BY login_date DESC
  376. LIMIT 0,1";
  377. $q_last_connection = Database::query($sql);
  378. if (Database::num_rows($q_last_connection) > 0) {
  379. $now = api_get_utc_datetime();
  380. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  381. // is the latest logout_date still relevant?
  382. $sql = "SELECT logout_date FROM $tbl_track_login
  383. WHERE login_id = $i_id_last_connection";
  384. $q_logout_date = Database::query($sql);
  385. $res_logout_date = convert_sql_date(Database::result($q_logout_date, 0, 'logout_date'));
  386. $lifeTime = api_get_configuration_value('session_lifetime');
  387. if ($res_logout_date < time() - $lifeTime) {
  388. // it isn't, we should create a fresh entry
  389. // now that it's created, we can get its ID and carry on
  390. Event::eventLogin($_user['user_id']);
  391. } else {
  392. $sql = "UPDATE $tbl_track_login SET logout_date = '$now'
  393. WHERE login_id = '$i_id_last_connection'";
  394. Database::query($sql);
  395. }
  396. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  397. $sql = "UPDATE $tableUser SET last_login = '$now'
  398. WHERE user_id = ".$_user["user_id"];
  399. Database::query($sql);
  400. }
  401. }
  402. }*/
  403. //Default quota for the course documents folder
  404. /*$default_quota = api_get_setting('default_document_quotum');
  405. //Just in case the setting is not correctly set
  406. if (empty($default_quota)) {
  407. $default_quota = 100000000;
  408. }
  409. define('DEFAULT_DOCUMENT_QUOTA', $default_quota);*/
  410. // Forcing PclZip library to use a custom temporary folder.
  411. //define('PCLZIP_TEMPORARY_DIR', api_get_path(SYS_ARCHIVE_PATH));
  412. } catch (Exception $e) {
  413. var_dump($e->getMessage());
  414. var_dump($e->getCode());
  415. var_dump($e->getLine());
  416. /*echo $e->getMessage(); exit;
  417. var_dump($e->getMessage());
  418. var_dump($e->getCode());
  419. var_dump($e->getLine());
  420. echo $e->getTraceAsString();
  421. exit;*/
  422. }