index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Shaarli - The personal, minimalist, super-fast, database free, bookmarking service.
  4. *
  5. * Friendly fork by the Shaarli community:
  6. * - https://github.com/shaarli/Shaarli
  7. *
  8. * Original project by sebsauvage.net:
  9. * - http://sebsauvage.net/wiki/doku.php?id=php:shaarli
  10. * - https://github.com/sebsauvage/Shaarli
  11. *
  12. * Licence: http://www.opensource.org/licenses/zlib-license.php
  13. */
  14. require_once 'inc/rain.tpl.class.php';
  15. require_once __DIR__ . '/vendor/autoload.php';
  16. // Shaarli library
  17. require_once 'application/bookmark/LinkUtils.php';
  18. require_once 'application/config/ConfigPlugin.php';
  19. require_once 'application/http/HttpUtils.php';
  20. require_once 'application/http/UrlUtils.php';
  21. require_once 'application/TimeZone.php';
  22. require_once 'application/Utils.php';
  23. require_once __DIR__ . '/init.php';
  24. use Katzgrau\KLogger\Logger;
  25. use Psr\Log\LogLevel;
  26. use Shaarli\Config\ConfigManager;
  27. use Shaarli\Container\ContainerBuilder;
  28. use Shaarli\Languages;
  29. use Shaarli\Plugin\PluginManager;
  30. use Shaarli\Security\BanManager;
  31. use Shaarli\Security\CookieManager;
  32. use Shaarli\Security\LoginManager;
  33. use Shaarli\Security\SessionManager;
  34. use Slim\App;
  35. $conf = new ConfigManager();
  36. // Manually override root URL for complex server configurations
  37. define('SHAARLI_ROOT_URL', $conf->get('general.root_url', null));
  38. // In dev mode, throw exception on any warning
  39. if ($conf->get('dev.debug', false)) {
  40. // See all errors (for debugging only)
  41. error_reporting(-1);
  42. set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext = []) {
  43. // Skip PHP 8 deprecation warning with Pimple.
  44. if (strpos($errfile, 'src/Pimple/Container.php') !== -1 && strpos($errstr, 'ArrayAccess::') !== -1) {
  45. return error_log($errstr);
  46. }
  47. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  48. });
  49. }
  50. $logger = new Logger(
  51. is_writable($conf->get('resource.log')) ? dirname($conf->get('resource.log')) : 'php://temp',
  52. !$conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG,
  53. ['filename' => basename($conf->get('resource.log'))]
  54. );
  55. $sessionManager = new SessionManager($_SESSION, $conf, session_save_path());
  56. $sessionManager->initialize();
  57. $cookieManager = new CookieManager($_COOKIE);
  58. $banManager = new BanManager(
  59. $conf->get('security.trusted_proxies', []),
  60. $conf->get('security.ban_after'),
  61. $conf->get('security.ban_duration'),
  62. $conf->get('resource.ban_file', 'data/ipbans.php'),
  63. $logger
  64. );
  65. $loginManager = new LoginManager($conf, $sessionManager, $cookieManager, $banManager, $logger);
  66. $loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']);
  67. // Sniff browser language and set date format accordingly.
  68. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  69. autoLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  70. }
  71. new Languages(setlocale(LC_MESSAGES, 0), $conf);
  72. $conf->setEmpty('general.timezone', date_default_timezone_get());
  73. $conf->setEmpty('general.title', t('Shared bookmarks on ') . escape(index_url($_SERVER)));
  74. RainTPL::$tpl_dir = $conf->get('resource.raintpl_tpl') . '/' . $conf->get('resource.theme') . '/'; // template directory
  75. RainTPL::$cache_dir = $conf->get('resource.raintpl_tmp'); // cache directory
  76. date_default_timezone_set($conf->get('general.timezone', 'UTC'));
  77. $loginManager->checkLoginState(client_ip_id($_SERVER));
  78. $pluginManager = new PluginManager($conf);
  79. $pluginManager->load($conf->get('general.enabled_plugins', []));
  80. $containerBuilder = new ContainerBuilder(
  81. $conf,
  82. $sessionManager,
  83. $cookieManager,
  84. $loginManager,
  85. $pluginManager,
  86. $logger
  87. );
  88. $container = $containerBuilder->build();
  89. $app = new App($container);
  90. // Main Shaarli routes
  91. $app->group('', function () {
  92. $this->get('/install', '\Shaarli\Front\Controller\Visitor\InstallController:index')->setName('displayInstall');
  93. $this->get('/install/session-test', '\Shaarli\Front\Controller\Visitor\InstallController:sessionTest');
  94. $this->post('/install', '\Shaarli\Front\Controller\Visitor\InstallController:save')->setName('saveInstall');
  95. /* -- PUBLIC --*/
  96. $this->get('/', '\Shaarli\Front\Controller\Visitor\BookmarkListController:index');
  97. $this->get('/shaare/{hash}', '\Shaarli\Front\Controller\Visitor\BookmarkListController:permalink');
  98. $this->get('/login', '\Shaarli\Front\Controller\Visitor\LoginController:index')->setName('login');
  99. $this->post('/login', '\Shaarli\Front\Controller\Visitor\LoginController:login')->setName('processLogin');
  100. $this->get('/picture-wall', '\Shaarli\Front\Controller\Visitor\PictureWallController:index');
  101. $this->get('/tags/cloud', '\Shaarli\Front\Controller\Visitor\TagCloudController:cloud');
  102. $this->get('/tags/list', '\Shaarli\Front\Controller\Visitor\TagCloudController:list');
  103. $this->get('/daily', '\Shaarli\Front\Controller\Visitor\DailyController:index');
  104. $this->get('/daily-rss', '\Shaarli\Front\Controller\Visitor\DailyController:rss')->setName('rss');
  105. $this->get('/feed/atom', '\Shaarli\Front\Controller\Visitor\FeedController:atom')->setName('atom');
  106. $this->get('/feed/rss', '\Shaarli\Front\Controller\Visitor\FeedController:rss');
  107. $this->get('/open-search', '\Shaarli\Front\Controller\Visitor\OpenSearchController:index');
  108. $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\Visitor\TagController:addTag');
  109. $this->get('/remove-tag/{tag}', '\Shaarli\Front\Controller\Visitor\TagController:removeTag');
  110. $this->get('/links-per-page', '\Shaarli\Front\Controller\Visitor\PublicSessionFilterController:linksPerPage');
  111. $this->get('/untagged-only', '\Shaarli\Front\Controller\Visitor\PublicSessionFilterController:untaggedOnly');
  112. })->add('\Shaarli\Front\ShaarliMiddleware');
  113. $app->group('/admin', function () {
  114. $this->get('/logout', '\Shaarli\Front\Controller\Admin\LogoutController:index');
  115. $this->get('/tools', '\Shaarli\Front\Controller\Admin\ToolsController:index');
  116. $this->get('/password', '\Shaarli\Front\Controller\Admin\PasswordController:index');
  117. $this->post('/password', '\Shaarli\Front\Controller\Admin\PasswordController:change');
  118. $this->get('/configure', '\Shaarli\Front\Controller\Admin\ConfigureController:index');
  119. $this->post('/configure', '\Shaarli\Front\Controller\Admin\ConfigureController:save');
  120. $this->get('/tags', '\Shaarli\Front\Controller\Admin\ManageTagController:index');
  121. $this->post('/tags', '\Shaarli\Front\Controller\Admin\ManageTagController:save');
  122. $this->post('/tags/change-separator', '\Shaarli\Front\Controller\Admin\ManageTagController:changeSeparator');
  123. $this->get('/add-shaare', '\Shaarli\Front\Controller\Admin\ShaareAddController:addShaare');
  124. $this->get('/shaare', '\Shaarli\Front\Controller\Admin\ShaarePublishController:displayCreateForm');
  125. $this->get('/shaare/{id:[0-9]+}', '\Shaarli\Front\Controller\Admin\ShaarePublishController:displayEditForm');
  126. $this->get('/shaare/private/{hash}', '\Shaarli\Front\Controller\Admin\ShaareManageController:sharePrivate');
  127. $this->post('/shaare-batch', '\Shaarli\Front\Controller\Admin\ShaarePublishController:displayCreateBatchForms');
  128. $this->post('/shaare', '\Shaarli\Front\Controller\Admin\ShaarePublishController:save');
  129. $this->get('/shaare/delete', '\Shaarli\Front\Controller\Admin\ShaareManageController:deleteBookmark');
  130. $this->get('/shaare/visibility', '\Shaarli\Front\Controller\Admin\ShaareManageController:changeVisibility');
  131. $this->post('/shaare/update-tags', '\Shaarli\Front\Controller\Admin\ShaareManageController:addOrDeleteTags');
  132. $this->get('/shaare/{id:[0-9]+}/pin', '\Shaarli\Front\Controller\Admin\ShaareManageController:pinBookmark');
  133. $this->patch(
  134. '/shaare/{id:[0-9]+}/update-thumbnail',
  135. '\Shaarli\Front\Controller\Admin\ThumbnailsController:ajaxUpdate'
  136. );
  137. $this->get('/export', '\Shaarli\Front\Controller\Admin\ExportController:index');
  138. $this->post('/export', '\Shaarli\Front\Controller\Admin\ExportController:export');
  139. $this->get('/import', '\Shaarli\Front\Controller\Admin\ImportController:index');
  140. $this->post('/import', '\Shaarli\Front\Controller\Admin\ImportController:import');
  141. $this->get('/plugins', '\Shaarli\Front\Controller\Admin\PluginsController:index');
  142. $this->post('/plugins', '\Shaarli\Front\Controller\Admin\PluginsController:save');
  143. $this->get('/token', '\Shaarli\Front\Controller\Admin\TokenController:getToken');
  144. $this->get('/server', '\Shaarli\Front\Controller\Admin\ServerController:index');
  145. $this->get('/clear-cache', '\Shaarli\Front\Controller\Admin\ServerController:clearCache');
  146. $this->get('/thumbnails', '\Shaarli\Front\Controller\Admin\ThumbnailsController:index');
  147. $this->get('/metadata', '\Shaarli\Front\Controller\Admin\MetadataController:ajaxRetrieveTitle');
  148. $this->get('/visibility/{visibility}', '\Shaarli\Front\Controller\Admin\SessionFilterController:visibility');
  149. })->add('\Shaarli\Front\ShaarliAdminMiddleware');
  150. $app->group('/plugin', function () use ($pluginManager) {
  151. foreach ($pluginManager->getRegisteredRoutes() as $pluginName => $routes) {
  152. $this->group('/' . $pluginName, function () use ($routes) {
  153. foreach ($routes as $route) {
  154. $this->{strtolower($route['method'])}('/' . ltrim($route['route'], '/'), $route['callable']);
  155. }
  156. });
  157. }
  158. })->add('\Shaarli\Front\ShaarliMiddleware');
  159. // REST API routes
  160. $app->group('/api/v1', function () {
  161. $this->get('/info', '\Shaarli\Api\Controllers\Info:getInfo')->setName('getInfo');
  162. $this->get('/links', '\Shaarli\Api\Controllers\Links:getLinks')->setName('getLinks');
  163. $this->get('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:getLink')->setName('getLink');
  164. $this->post('/links', '\Shaarli\Api\Controllers\Links:postLink')->setName('postLink');
  165. $this->put('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:putLink')->setName('putLink');
  166. $this->delete('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:deleteLink')->setName('deleteLink');
  167. $this->get('/tags', '\Shaarli\Api\Controllers\Tags:getTags')->setName('getTags');
  168. $this->get('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:getTag')->setName('getTag');
  169. $this->put('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:putTag')->setName('putTag');
  170. $this->delete('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:deleteTag')->setName('deleteTag');
  171. $this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory');
  172. })->add('\Shaarli\Api\ApiMiddleware');
  173. try {
  174. $response = $app->run(true);
  175. $app->respond($response);
  176. } catch (Throwable $e) {
  177. die(nl2br(
  178. 'An unexpected error happened, and the error template could not be displayed.' . PHP_EOL . PHP_EOL .
  179. exception2text($e)
  180. ));
  181. }