global.inc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * It is recommended that ALL Chamilo scripts include this important file.
  5. * This script manages
  6. * - http get, post, post_files, session, server-vars extraction into global namespace;
  7. * (which doesn't occur anymore when servertype config setting is set to test,
  8. * and which will disappear completely in Dokeos 1.6.1)
  9. * - include of /conf/configuration.php;
  10. * - include of several libraries: main_api, database, display, text, security;
  11. * - selecting the main database;
  12. * - include of language files.
  13. *
  14. * @package chamilo.include
  15. * @todo isn't configuration.php renamed to configuration.inc.php yet?
  16. * @todo use the $_configuration array for all the needed variables
  17. * @todo remove the code that displays the button that links to the install page
  18. * but use a redirect immediately. By doing so the $already_installed variable can be removed.
  19. * @todo make it possible to enable / disable the tracking through the Chamilo config page.
  20. *
  21. */
  22. //@todo will be removed before a stable release
  23. $mtime = microtime();
  24. $mtime = explode(" ",$mtime);
  25. $mtime = $mtime[1] + $mtime[0];
  26. $starttime = $mtime;
  27. define('START', $starttime);
  28. // Showing/hiding error codes in global error messages.
  29. define('SHOW_ERROR_CODES', false);
  30. // Determine the directory path where this current file lies.
  31. // This path will be useful to include the other intialisation files.
  32. $includePath = dirname(__FILE__);
  33. // @todo Isn't this file renamed to configuration.inc.php yet?
  34. // Include the main Chamilo platform configuration file.
  35. $main_configuration_file_path = $includePath.'/conf/configuration.php';
  36. $already_installed = false;
  37. if (file_exists($main_configuration_file_path)) {
  38. require_once $main_configuration_file_path;
  39. $already_installed = true;
  40. } else {
  41. $_configuration = array();
  42. }
  43. //Redirects to the main/install/ page
  44. if (!$already_installed) {
  45. $global_error_code = 2;
  46. // The system has not been installed yet.
  47. require $includePath.'/global_error_message.inc.php';
  48. die();
  49. }
  50. // Ensure that _configuration is in the global scope before loading
  51. // main_api.lib.php. This is particularly helpful for unit tests
  52. if (!isset($GLOBALS['_configuration'])) {
  53. $GLOBALS['_configuration'] = $_configuration;
  54. }
  55. // Code for trnasitional purposes, it can be removed right before the 1.8.7 release.
  56. if (empty($_configuration['system_version'])) {
  57. $_configuration['system_version'] = (!empty($_configuration['dokeos_version'])?$_configuration['dokeos_version']:'');
  58. $_configuration['system_stable'] = (!empty($_configuration['dokeos_stable'])?$_configuration['dokeos_stable']:'');
  59. $_configuration['software_url'] = 'http://www.chamilo.org/';
  60. }
  61. // For backward compatibility.
  62. $_configuration['dokeos_version'] = $_configuration['system_version'];
  63. $_configuration['dokeos_stable'] = $_configuration['system_stable'];
  64. $userPasswordCrypted = (!empty($_configuration['password_encryption']) ? $_configuration['password_encryption'] : 'sha1');
  65. // Include the main Chamilo platform library file.
  66. require_once $includePath.'/lib/main_api.lib.php';
  67. // Specification for usernames:
  68. // 1. ASCII-letters, digits, "." (dot), "_" (underscore) are acceptable, 40 characters maximum length.
  69. // 2. Empty username is formally valid, but it is reserved for the anonymous user.
  70. // 3. Checking the login_is_email portal setting in order to accept 100 chars maximum
  71. $default_username_length = 40;
  72. if (api_get_setting('login_is_email') == 'true') {
  73. $default_username_length = 100;
  74. }
  75. define('USERNAME_MAX_LENGTH', $default_username_length);
  76. // Do not over-use this variable. It is only for this script's local use.
  77. $lib_path = api_get_path(LIBRARY_PATH);
  78. // Fix bug in IIS that doesn't fill the $_SERVER['REQUEST_URI'].
  79. api_request_uri();
  80. // Add the path to the pear packages to the include path
  81. ini_set('include_path', api_create_include_path_setting());
  82. // This is for compatibility with MAC computers.
  83. ini_set('auto_detect_line_endings', '1');
  84. //Include the libraries that are necessary everywhere
  85. //require_once dirname(__FILE__).'/autoload.inc.php';
  86. //Fixes Htmlpurifier autoloader issue with composer
  87. define('HTMLPURIFIER_PREFIX', $lib_path.'htmlpurifier/library');
  88. //mpdf constants
  89. define("_MPDF_TEMP_PATH", api_get_path(SYS_ARCHIVE_PATH));
  90. define('_MPDF_PATH', api_get_path(LIBRARY_PATH).'mpdf/');
  91. //Composer autoloader
  92. require_once __DIR__.'../../../vendor/autoload.php';
  93. //Start Silex
  94. use Silex\Application;
  95. $app = new Application();
  96. $app['configuration_file'] = $main_configuration_file_path;
  97. //require_once __DIR__.'/../../resources/config/prod.php';
  98. require_once __DIR__.'/../../resources/config/dev.php';
  99. //Setting HttpCacheService provider in order to use do: $app['http_cache']->run();
  100. /*
  101. $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
  102. 'http_cache.cache_dir' => $app['http_cache.cache_dir'].'/',
  103. ));*/
  104. $app->register(new Silex\Provider\ValidatorServiceProvider());
  105. //URL generator provider
  106. $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
  107. $app->register(new Silex\Provider\TranslationServiceProvider(),array(
  108. 'locale_fallback' => 'en'
  109. ));
  110. //Form provider
  111. $app->register(new Silex\Provider\FormServiceProvider());
  112. //Monolog
  113. $app->register(new Silex\Provider\MonologServiceProvider(), array(
  114. 'monolog.logfile' => api_get_path(SYS_ARCHIVE_PATH).'chamilo_development.log',
  115. 'monolog.name' => 'chamilo',
  116. ));
  117. /*
  118. //Monolog examples
  119. $app['monolog']->addDebug('Testing the Monolog logging.');
  120. $app['monolog']->addInfo('Testing the Monolog logging.');
  121. $app['monolog']->addError('Testing the Monolog logging.');
  122. */
  123. $app['translator.messages'] = array();
  124. //Setting the Twig service provider
  125. $app->register(new Silex\Provider\TwigServiceProvider(), array(
  126. 'twig.path' => array(
  127. api_get_path(SYS_CODE_PATH).'template', //template folder
  128. api_get_path(SYS_PLUGIN_PATH) //plugin folder
  129. ),
  130. 'twig.form.templates' => array('form_div_layout.html.twig', 'default/form/form_custom_template.tpl'),
  131. 'twig.options' => array(
  132. 'debug' => $app['debug'],
  133. 'charset' => 'utf-8',
  134. 'strict_variables' => false,
  135. 'autoescape' => false,
  136. 'cache' => $app['debug'] ? false : $app['cache.path'].'twig',
  137. 'optimizations' => -1, // turn on optimizations with -1
  138. )
  139. ));
  140. //Setting Twig options
  141. $app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
  142. $twig->addFilter('get_lang', new Twig_Filter_Function('get_lang'));
  143. $twig->addFilter('get_path', new Twig_Filter_Function('api_get_path'));
  144. $twig->addFilter('get_setting', new Twig_Filter_Function('api_get_setting'));
  145. $twig->addFilter('var_dump', new Twig_Filter_Function('var_dump'));
  146. $twig->addFilter('return_message', new Twig_Filter_Function('Display::return_message_and_translate'));
  147. $twig->addFilter('display_page_header', new Twig_Filter_Function('Display::page_header_and_translate'));
  148. $twig->addFilter('display_page_subheader', new Twig_Filter_Function('Display::page_subheader_and_translate'));
  149. $twig->addFilter('icon', new Twig_Filter_Function('Template::get_icon_path'));
  150. $twig->addFilter('format_date', new Twig_Filter_Function('Template::format_date'));
  151. return $twig;
  152. }));
  153. //Setting Doctrine service provider (DBAL)
  154. $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
  155. 'db.options' => array(
  156. 'driver' => 'pdo_mysql',
  157. 'dbname' => $_configuration['main_database'],
  158. 'user' => $_configuration['db_user'],
  159. 'password' => $_configuration['db_password'],
  160. 'host' => $_configuration['db_host'],
  161. )
  162. ));
  163. //Setting Doctrine ORM
  164. $app->register(new Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array(
  165. "orm.proxies_dir" => $app['db.orm.proxies_dir'],
  166. "orm.em.options" => array(
  167. "mappings" => array(
  168. array(
  169. "type" => "annotation",
  170. "namespace" => "Entity",
  171. "path" => api_get_path(INCLUDE_PATH).'Entity',
  172. )
  173. ),
  174. ),
  175. ));
  176. //Setting Doctrine2 extensions
  177. $timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
  178. $app['db.event_manager']->addEventSubscriber($timestampableListener);
  179. $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
  180. $app['db.event_manager']->addEventSubscriber($sluggableListener);
  181. $sortableListener = new \Gedmo\Sortable\SortableListener();
  182. $app['db.event_manager']->addEventSubscriber($sortableListener);
  183. //Testing with another silex service provider
  184. /*
  185. // Register Doctrine ORM
  186. $app->register(new Nutwerk\Provider\DoctrineORMServiceProvider(), array(
  187. 'db.orm.proxies_dir' => $app['db.orm.proxies_dir'],
  188. 'db.orm.proxies_namespace' => 'DoctrineProxy',
  189. 'db.orm.cache' =>
  190. !$app['debug'] && extension_loaded('apc') ? new ApcCache() : new ArrayCache(),
  191. 'db.orm.auto_generate_proxies' => true,
  192. 'db.orm.entities' => array(array(
  193. 'type' => 'annotation', // entity definition
  194. 'path' => api_get_path(INCLUDE_PATH).'Entity',
  195. 'namespace' => 'Entity', // your classes namespace
  196. )),
  197. ));*/
  198. //Creating Chamilo service provider
  199. use Silex\ServiceProviderInterface;
  200. class ChamiloServiceProvider implements ServiceProviderInterface {
  201. public function register(Application $app) {
  202. //Template
  203. $app['template'] = $app->share(function() use($app){
  204. return new Template(null, $app);
  205. });
  206. }
  207. public function boot(Application $app) {
  208. }
  209. }
  210. //Registering Chamilo service provider
  211. $app->register(new ChamiloServiceProvider(), array());
  212. //Controllers
  213. $app->register(new Silex\Provider\ServiceControllerServiceProvider());
  214. $app['pages.controller'] = $app->share(function() use ($app) {
  215. return new PagesController($app['pages.repository']);
  216. });
  217. //Manage error messages
  218. /*
  219. $app->error(function (\Exception $e, $code) use ($app) {
  220. if ($app['debug']) {
  221. //return;
  222. }
  223. if (isset($code)) {
  224. switch ($code) {
  225. case 404:
  226. $message = 'The requested page could not be found.';
  227. break;
  228. default:
  229. //$message = 'We are sorry, but something went terribly wrong.';
  230. $message = $e->getMessage();
  231. }
  232. } else {
  233. $code = null;
  234. $message = null;
  235. }
  236. //$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
  237. $app['template']->assign('error_code', $code);
  238. $app['template']->assign('error_message', $message);
  239. $response = $app['template']->render_layout('error.tpl');
  240. return new Response($response);
  241. });
  242. *
  243. */
  244. /*
  245. use Symfony\Component\HttpKernel\Debug\ErrorHandler;
  246. ErrorHandler::register();
  247. */
  248. //Filters
  249. $app->before(function() use ($app) {
  250. //Check the PHP version
  251. if (api_check_php_version() == false) {
  252. $app->abort(500, "Incorrect PHP version"); //error 1
  253. }
  254. if (!file_exists($app['configuration_file'])) {
  255. $app->abort(500, "Chamilo has not been installed"); //error 2
  256. }
  257. });
  258. $app->after(function() {
  259. });
  260. $app->finish(function() use ($app) {
  261. //@todo will be removed before a stable release
  262. $mtime = microtime();
  263. $mtime = explode(" ",$mtime);
  264. $mtime = $mtime[1] + $mtime[0];
  265. $message = "Page loaded in:".($mtime-START);
  266. $app['monolog']->addInfo($message);
  267. $message = "memory_get_usage: ".format_file_size(memory_get_usage(true));
  268. $app['monolog']->addInfo($message);
  269. $message = "memory_get_peak_usage: ".format_file_size(memory_get_peak_usage(true));
  270. $app['monolog']->addInfo($message);
  271. });
  272. $app['template.show_header'] = true;
  273. $app['template.show_footer'] = true;
  274. $app['template.show_learnpath'] = true;
  275. $app['template.hide_global_chat'] = true;
  276. $app['template.load_plugins'] = true;
  277. //Default template style
  278. $app['template_style'] = 'default';
  279. $app['default_layout'] = 'layout_1_col.tpl';
  280. //Database constants
  281. require_once $lib_path.'database.constants.inc.php';
  282. require_once $lib_path.'fileManage.lib.php';
  283. require_once $lib_path.'text.lib.php';
  284. require_once $lib_path.'array.lib.php';
  285. require_once $lib_path.'events.lib.inc.php';
  286. require_once $lib_path.'online.inc.php';
  287. /* DATABASE CONNECTION */
  288. // @todo: this shouldn't be done here. It should be stored correctly during installation.
  289. if (empty($_configuration['statistics_database']) && $already_installed) {
  290. $_configuration['statistics_database'] = $_configuration['main_database'];
  291. }
  292. global $database_connection;
  293. // Connect to the server database and select the main chamilo database.
  294. if (!($conn_return = @Database::connect(
  295. array(
  296. 'server' => $_configuration['db_host'],
  297. 'username' => $_configuration['db_user'],
  298. 'password' => $_configuration['db_password'],
  299. 'persistent' => $_configuration['db_persistent_connection'] // When $_configuration['db_persistent_connection'] is set, it is expected to be a boolean type.
  300. )))) {
  301. $app->abort(500, "Database is unavailable"); //error 3
  302. }
  303. if (!$_configuration['db_host']) {
  304. $app->abort(500, "Database is unavailable"); //error 3
  305. }
  306. /* RETRIEVING ALL THE CHAMILO CONFIG SETTINGS FOR MULTIPLE URLs FEATURE*/
  307. if (!empty($_configuration['multiple_access_urls'])) {
  308. $_configuration['access_url'] = 1;
  309. $access_urls = api_get_access_urls();
  310. $protocol = ((!empty($_SERVER['HTTPS']) && strtoupper($_SERVER['HTTPS']) != 'OFF') ? 'https' : 'http').'://';
  311. $request_url1 = $protocol.$_SERVER['SERVER_NAME'].'/';
  312. $request_url2 = $protocol.$_SERVER['HTTP_HOST'].'/';
  313. foreach ($access_urls as & $details) {
  314. if ($request_url1 == $details['url'] or $request_url2 == $details['url']) {
  315. $_configuration['access_url'] = $details['id'];
  316. }
  317. }
  318. } else {
  319. $_configuration['access_url'] = 1;
  320. }
  321. // The system has not been designed to use special SQL modes that were introduced since MySQL 5.
  322. Database::query("set session sql_mode='';");
  323. if (!Database::select_db($_configuration['main_database'], $database_connection)) {
  324. $app->abort(500, "Database is unavailable"); //error 3
  325. }
  326. /* Initialization of the default encodings */
  327. // The platform's character set must be retrieved at this early moment.
  328. $sql = "SELECT selected_value FROM settings_current WHERE variable = 'platform_charset';";
  329. $result = Database::query($sql);
  330. while ($row = @Database::fetch_array($result)) {
  331. $charset = $row[0];
  332. }
  333. if (empty($charset)) {
  334. $charset = 'UTF-8';
  335. }
  336. // Preserving the value of the global variable $charset.
  337. $charset_initial_value = $charset;
  338. // Initialization of the internationalization library.
  339. api_initialize_internationalization();
  340. // Initialization of the default encoding that will be used by the multibyte string routines in the internationalization library.
  341. api_set_internationalization_default_encoding($charset);
  342. // Initialization of the database encoding to be used.
  343. Database::query("SET SESSION character_set_server='utf8';");
  344. Database::query("SET SESSION collation_server='utf8_general_ci';");
  345. if (api_is_utf8($charset)) {
  346. // See Bug #1802: For UTF-8 systems we prefer to use "SET NAMES 'utf8'" statement in order to avoid a bizarre problem with Chinese language.
  347. Database::query("SET NAMES 'utf8';");
  348. } else {
  349. Database::query("SET CHARACTER SET '" . Database::to_db_encoding($charset) . "';");
  350. }
  351. Database::query("SET NAMES 'utf8';");
  352. // Start session after the internationalization library has been initialized.
  353. Chamilo::session()->start($already_installed);
  354. $settings_refresh_info = api_get_settings_params_simple(array('variable = ?' => 'settings_latest_update'));
  355. $settings_latest_update = $settings_refresh_info ? $settings_refresh_info['selected_value'] : null;
  356. $_setting = isset($_SESSION['_setting']) ? $_SESSION['_setting'] : null;
  357. $_plugins = isset($_SESSION['_plugins']) ? $_SESSION['_plugins'] : null;
  358. if (!isset($_setting)) {
  359. api_set_settings_and_plugins();
  360. } else {
  361. if (isset($_setting['settings_latest_update']) && $_setting['settings_latest_update'] != $settings_latest_update) {
  362. api_set_settings_and_plugins();
  363. $_setting = isset($_SESSION['_setting']) ? $_SESSION['_setting'] : null;
  364. $_plugins = isset($_SESSION['_plugins']) ? $_SESSION['_plugins'] : null;
  365. }
  366. }
  367. // Load allowed tag definitions for kses and/or HTMLPurifier.
  368. require_once $lib_path.'formvalidator/Rule/allowed_tags.inc.php';
  369. // Load HTMLPurifier.
  370. //require_once $lib_path.'htmlpurifier/library/HTMLPurifier.auto.php'; // It will be loaded later, in a lazy manner.
  371. // Before we call local.inc.php, let's define a global $this_section variable
  372. // which will then be usable from the banner and header scripts
  373. $this_section = SECTION_GLOBAL;
  374. // include the local (contextual) parameters of this course or section
  375. require $includePath.'/local.inc.php';
  376. //Include Chamilo Mail conf this is added here because the api_get_setting works
  377. //Fixes bug in Chamilo 1.8.7.1 array was not set
  378. $administrator['email'] = isset($administrator['email']) ? $administrator['email'] : 'admin@example.com';
  379. $administrator['name'] = isset($administrator['name']) ? $administrator['name'] : 'Admin';
  380. $mail_conf = api_get_path(CONFIGURATION_PATH).'mail.conf.php';
  381. if (file_exists($mail_conf)) {
  382. require_once $mail_conf;
  383. }
  384. $mail_settings = array();
  385. $app->register(new Silex\Provider\SwiftmailerServiceProvider(), array(
  386. 'swiftmailer.options' => array(
  387. 'host' => $platform_email['SMTP_HOST'],
  388. 'port' => $platform_email['SMTP_PORT'],
  389. 'username' => $platform_email['SMTP_USER'],
  390. 'password' => $platform_email['SMTP_PASS'],
  391. 'encryption' => null,
  392. 'auth_mode' => null
  393. )
  394. ));
  395. //if (isset($platform_email['SMTP_MAILER']) && $platform_email['SMTP_MAILER'] == 'smtp') {
  396. $app['mailer'] = $app->share(function ($app) {
  397. return new \Swift_Mailer($app['swiftmailer.transport']);
  398. });
  399. // check and modify the date of user in the track.e.online table
  400. if (!$x = strpos($_SERVER['PHP_SELF'], 'whoisonline.php')) {
  401. LoginCheck(isset($_user['user_id']) ? $_user['user_id'] : '');
  402. }
  403. //error_reporting(-1);
  404. if (api_get_setting('server_type') == 'test') {
  405. //error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  406. } else {
  407. /*
  408. Server type is not test
  409. - normal error reporting level
  410. - full fake register globals block
  411. */
  412. error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
  413. // TODO: These obsolete variables $HTTP_* to be check whether they are actually used.
  414. if (!isset($HTTP_GET_VARS)) { $HTTP_GET_VARS = $_GET; }
  415. if (!isset($HTTP_POST_VARS)) { $HTTP_POST_VARS = $_POST; }
  416. if (!isset($HTTP_POST_FILES)) { $HTTP_POST_FILES = $_FILES; }
  417. if (!isset($HTTP_SESSION_VARS)) { $HTTP_SESSION_VARS = $_SESSION; }
  418. if (!isset($HTTP_SERVER_VARS)) { $HTTP_SERVER_VARS = $_SERVER; }
  419. // Register SESSION variables into $GLOBALS
  420. if (sizeof($HTTP_SESSION_VARS)) {
  421. if (!is_array($_SESSION)) {
  422. $_SESSION = array();
  423. }
  424. foreach ($HTTP_SESSION_VARS as $key => $val) {
  425. $_SESSION[$key] = $HTTP_SESSION_VARS[$key];
  426. $GLOBALS[$key] = $HTTP_SESSION_VARS[$key];
  427. }
  428. }
  429. // Register SERVER variables into $GLOBALS
  430. if (sizeof($HTTP_SERVER_VARS)) {
  431. $_SERVER = array();
  432. foreach ($HTTP_SERVER_VARS as $key => $val) {
  433. $_SERVER[$key] = $HTTP_SERVER_VARS[$key];
  434. if (!isset($_SESSION[$key]) && $key != 'includePath' && $key != 'rootSys' && $key!= 'lang_path' && $key!= 'extAuthSource' && $key!= 'thisAuthSource' && $key!= 'main_configuration_file_path' && $key!= 'phpDigIncCn' && $key!= 'drs') {
  435. $GLOBALS[$key]=$HTTP_SERVER_VARS[$key];
  436. }
  437. }
  438. }
  439. }
  440. /* LOAD LANGUAGE FILES SECTION */
  441. // if we use the javascript version (without go button) we receive a get
  442. // if we use the non-javascript version (with the go button) we receive a post
  443. $user_language = '';
  444. if (!empty($_GET['language'])) {
  445. $user_language = $_GET['language'];
  446. }
  447. if (!empty($_POST['language_list'])) {
  448. $user_language = str_replace('index.php?language=', '', $_POST['language_list']);
  449. }
  450. // Include all files (first english and then current interface language)
  451. $langpath = api_get_path(SYS_LANG_PATH);
  452. /* This will only work if we are in the page to edit a sub_language */
  453. if (isset($this_script) && $this_script == 'sub_language') {
  454. require_once '../admin/sub_language.class.php';
  455. // getting the arrays of files i.e notification, trad4all, etc
  456. $language_files_to_load = SubLanguageManager:: get_lang_folder_files_list(api_get_path(SYS_LANG_PATH).'english', true);
  457. //getting parent info
  458. $parent_language = SubLanguageManager::get_all_information_of_language($_REQUEST['id']);
  459. //getting sub language info
  460. $sub_language = SubLanguageManager::get_all_information_of_language($_REQUEST['sub_language_id']);
  461. $english_language_array = $parent_language_array = $sub_language_array = array();
  462. foreach ($language_files_to_load as $language_file_item) {
  463. $lang_list_pre = array_keys($GLOBALS);
  464. //loading english
  465. $path = $langpath.'english/'.$language_file_item.'.inc.php';
  466. if (file_exists($path)) {
  467. include $path;
  468. }
  469. $lang_list_post = array_keys($GLOBALS);
  470. $lang_list_result = array_diff($lang_list_post, $lang_list_pre);
  471. unset($lang_list_pre);
  472. // english language array
  473. $english_language_array[$language_file_item] = compact($lang_list_result);
  474. //cleaning the variables
  475. foreach ($lang_list_result as $item) {
  476. unset(${$item});
  477. }
  478. $parent_file = $langpath.$parent_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  479. if (file_exists($parent_file) && is_file($parent_file)) {
  480. include_once $parent_file;
  481. }
  482. // parent language array
  483. $parent_language_array[$language_file_item] = compact($lang_list_result);
  484. //cleaning the variables
  485. foreach($lang_list_result as $item) {
  486. unset(${$item});
  487. }
  488. $sub_file = $langpath.$sub_language['dokeos_folder'].'/'.$language_file_item.'.inc.php';
  489. if (file_exists($sub_file) && is_file($sub_file)) {
  490. include $sub_file;
  491. }
  492. // sub language array
  493. $sub_language_array[$language_file_item] = compact($lang_list_result);
  494. //cleaning the variables
  495. foreach($lang_list_result as $item) {
  496. unset(${$item});
  497. }
  498. }
  499. }
  500. // Checking if we have a valid language. If not we set it to the platform language.
  501. $valid_languages = api_get_languages();
  502. if (!empty($valid_languages)) {
  503. if (!in_array($user_language, $valid_languages['folder'])) {
  504. $user_language = api_get_setting('platformLanguage');
  505. }
  506. $language_priority1 = api_get_setting('languagePriority1');
  507. $language_priority2 = api_get_setting('languagePriority2');
  508. $language_priority3 = api_get_setting('languagePriority3');
  509. $language_priority4 = api_get_setting('languagePriority4');
  510. if (in_array($user_language, $valid_languages['folder']) && (isset($_GET['language']) || isset($_POST['language_list']))) {
  511. $user_selected_language = $user_language; // $_GET['language'];
  512. $_SESSION['user_language_choice'] = $user_selected_language;
  513. $platformLanguage = $user_selected_language;
  514. }
  515. if (!empty($language_priority4) && api_get_language_from_type($language_priority4) !== false ) {
  516. $language_interface = api_get_language_from_type($language_priority4);
  517. } else {
  518. $language_interface = api_get_setting('platformLanguage');
  519. }
  520. if (!empty($language_priority3) && api_get_language_from_type($language_priority3) !== false ) {
  521. $language_interface = api_get_language_from_type($language_priority3);
  522. } else {
  523. if (isset($_SESSION['user_language_choice'])) {
  524. $language_interface = $_SESSION['user_language_choice'];
  525. }
  526. }
  527. if (!empty($language_priority2) && api_get_language_from_type($language_priority2) !== false ) {
  528. $language_interface = api_get_language_from_type($language_priority2);
  529. } else {
  530. if (isset($_user['language'])) {
  531. $language_interface = $_user['language'];
  532. }
  533. }
  534. if (!empty($language_priority1) && api_get_language_from_type($language_priority1) !== false ) {
  535. $language_interface = api_get_language_from_type($language_priority1);
  536. } else {
  537. if ($_course['language']) {
  538. $language_interface = $_course['language'];
  539. }
  540. }
  541. }
  542. // Sometimes the variable $language_interface is changed
  543. // temporarily for achieving translation in different language.
  544. // We need to save the genuine value of this variable and
  545. // to use it within the function get_lang(...).
  546. $language_interface_initial_value = $language_interface;
  547. /**
  548. * Include all necessary language files
  549. * - trad4all
  550. * - notification
  551. * - custom tool language files
  552. */
  553. $language_files = array();
  554. $language_files[] = 'trad4all';
  555. $language_files[] = 'notification';
  556. $language_files[] = 'accessibility';
  557. if (isset($language_file)) {
  558. if (!is_array($language_file)) {
  559. $language_files[] = $language_file;
  560. } else {
  561. $language_files = array_merge($language_files, $language_file);
  562. }
  563. }
  564. // if a set of language files has been properly defined
  565. if (is_array($language_files)) {
  566. // if the sub-language feature is on
  567. if (api_get_setting('allow_use_sub_language') == 'true') {
  568. require_once api_get_path(SYS_CODE_PATH).'admin/sub_language.class.php';
  569. $parent_path = SubLanguageManager::get_parent_language_path($language_interface);
  570. foreach ($language_files as $index => $language_file) {
  571. // include English
  572. include $langpath.'english/'.$language_file.'.inc.php';
  573. // prepare string for current language and its parent
  574. $lang_file = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  575. $parent_lang_file = $langpath.$parent_path.'/'.$language_file.'.inc.php';
  576. // load the parent language file first
  577. if (file_exists($parent_lang_file)) {
  578. include $parent_lang_file;
  579. }
  580. // overwrite the parent language translations if there is a child
  581. if (file_exists($lang_file)) {
  582. include $lang_file;
  583. }
  584. }
  585. } else {
  586. // if the sub-languages feature is not on, then just load the
  587. // set language interface
  588. foreach ($language_files as $index => $language_file) {
  589. // include English
  590. include $langpath.'english/'.$language_file.'.inc.php';
  591. // prepare string for current language
  592. $langfile = $langpath.$language_interface.'/'.$language_file.'.inc.php';
  593. if (file_exists($langfile)) {
  594. include $langfile;
  595. }
  596. }
  597. }
  598. }
  599. // The global variable $charset has been defined in a language file too (trad4all.inc.php), this is legacy situation.
  600. // So, we have to reassign this variable again in order to keep its value right.
  601. $charset = $charset_initial_value;
  602. // The global variable $text_dir has been defined in the language file trad4all.inc.php.
  603. // For determing text direction correspondent to the current language we use now information from the internationalization library.
  604. $text_dir = api_get_text_direction();
  605. //Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
  606. if (!isset($_SESSION['login_as']) && isset($_user)) {
  607. // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  608. $tbl_track_login = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  609. $sql_last_connection = "SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='".$_user["user_id"]."' ORDER BY login_date DESC LIMIT 0,1";
  610. $q_last_connection = Database::query($sql_last_connection);
  611. if (Database::num_rows($q_last_connection) > 0) {
  612. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  613. // is the latest logout_date still relevant?
  614. $sql_logout_date = "SELECT logout_date FROM $tbl_track_login WHERE login_id=$i_id_last_connection";
  615. $q_logout_date = Database::query($sql_logout_date);
  616. $res_logout_date = convert_sql_date(Database::result($q_logout_date,0,'logout_date'));
  617. if ($res_logout_date < time() - $_configuration['session_lifetime']) {
  618. // it isn't, we should create a fresh entry
  619. event_login();
  620. // now that it's created, we can get its ID and carry on
  621. $q_last_connection = Database::query($sql_last_connection);
  622. $i_id_last_connection = Database::result($q_last_connection, 0, 'login_id');
  623. }
  624. $s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  625. Database::query($s_sql_update_logout_date);
  626. }
  627. }
  628. // Add language_measure_frequency to your main/inc/conf/configuration.php in
  629. // order to generate language variables frequency measurements (you can then
  630. // see them through main/cron/lang/langstats.php)
  631. // The langstat object will then be used in the get_lang() function.
  632. // This block can be removed to speed things up a bit as it should only ever
  633. // be used in development versions.
  634. if (isset($_configuration['language_measure_frequency']) && $_configuration['language_measure_frequency'] == 1) {
  635. require_once api_get_path(SYS_CODE_PATH).'/cron/lang/langstats.class.php';
  636. $langstats = new langstats();
  637. }
  638. //Default quota for the course documents folder
  639. $default_quota = api_get_setting('default_document_quotum');
  640. //Just in case the setting is not correctly set
  641. if (empty($default_quota)) {
  642. $default_quota = 100000000;
  643. }
  644. define('DEFAULT_DOCUMENT_QUOTA', $default_quota);
  645. return $app;