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