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