init.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use Shaarli\Helper\ApplicationUtils;
  4. use Shaarli\Security\SessionManager;
  5. // Set 'UTC' as the default timezone if it is not defined in php.ini
  6. // See http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
  7. if (date_default_timezone_get() == '') {
  8. date_default_timezone_set('UTC');
  9. }
  10. // High execution time in case of problematic imports/exports.
  11. ini_set('max_input_time', '60');
  12. // Try to set max upload file size and read
  13. ini_set('memory_limit', '128M');
  14. ini_set('post_max_size', '16M');
  15. ini_set('upload_max_filesize', '16M');
  16. // See all error except warnings
  17. error_reporting(E_ALL & ~E_WARNING & ~E_DEPRECATED);
  18. // 3rd-party libraries
  19. if (! file_exists(__DIR__ . '/vendor/autoload.php')) {
  20. header('Content-Type: text/plain; charset=utf-8');
  21. echo "Error: missing Composer configuration\n\n"
  22. ."If you installed Shaarli through Git or using the development branch,\n"
  23. ."please refer to the installation documentation to install PHP"
  24. ." dependencies using Composer:\n"
  25. ."- https://shaarli.readthedocs.io/en/master/Server-configuration/\n"
  26. ."- https://shaarli.readthedocs.io/en/master/Download-and-Installation/";
  27. exit;
  28. }
  29. // Ensure the PHP version is supported
  30. try {
  31. ApplicationUtils::checkPHPVersion('7.1', PHP_VERSION);
  32. } catch (Exception $exc) {
  33. header('Content-Type: text/plain; charset=utf-8');
  34. echo $exc->getMessage();
  35. exit;
  36. }
  37. // Force cookie path (but do not change lifetime)
  38. $cookie = session_get_cookie_params();
  39. $cookiedir = '';
  40. if (dirname($_SERVER['SCRIPT_NAME']) != '/') {
  41. $cookiedir = dirname($_SERVER["SCRIPT_NAME"]).'/';
  42. }
  43. // Set default cookie expiration and path.
  44. session_set_cookie_params($cookie['lifetime'], $cookiedir, $_SERVER['SERVER_NAME']);
  45. // Set session parameters on server side.
  46. // Use cookies to store session.
  47. ini_set('session.use_cookies', 1);
  48. // Force cookies for session (phpsessionID forbidden in URL).
  49. ini_set('session.use_only_cookies', 1);
  50. // Prevent PHP form using sessionID in URL if cookies are disabled.
  51. ini_set('session.use_trans_sid', false);
  52. define('SHAARLI_VERSION', ApplicationUtils::getVersion(__DIR__ .'/'. ApplicationUtils::$VERSION_FILE));
  53. define('SHAARLI_MUTEX_FILE', __FILE__);
  54. session_name('shaarli');
  55. // Start session if needed (Some server auto-start sessions).
  56. if (session_status() == PHP_SESSION_NONE) {
  57. session_start();
  58. }
  59. // Regenerate session ID if invalid or not defined in cookie.
  60. if (isset($_COOKIE['shaarli']) && !SessionManager::checkId($_COOKIE['shaarli'])) {
  61. session_regenerate_id(true);
  62. $_COOKIE['shaarli'] = session_id();
  63. }
  64. // LC_MESSAGES isn't defined without php-intl, in this case use LC_COLLATE locale instead.
  65. if (! defined('LC_MESSAGES')) {
  66. define('LC_MESSAGES', LC_COLLATE);
  67. }
  68. // Prevent caching on client side or proxy: (yes, it's ugly)
  69. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  70. header("Cache-Control: no-store, no-cache, must-revalidate");
  71. header("Cache-Control: post-check=0, pre-check=0", false);
  72. header("Pragma: no-cache");