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