global.inc.php 30 KB

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