-
- // Plugin administration form action
- if ($targetPage == Router::$PAGE_SAVE_PLUGINSADMIN) {
- try {
- if (isset($_POST['parameters_form'])) {
- $pluginManager->executeHooks('save_plugin_parameters', $_POST);
- unset($_POST['parameters_form']);
- foreach ($_POST as $param => $value) {
- $conf->set('plugins.'. $param, escape($value));
- }
- } else {
- $conf->set('general.enabled_plugins', save_plugin_config($_POST));
- }
- $conf->write($loginManager->isLoggedIn());
- $history->updateSettings();
- } catch (Exception $e) {
- error_log(
- 'ERROR while saving plugin configuration:.' . PHP_EOL .
- $e->getMessage()
- );
-
- // TODO: do not handle exceptions/errors in JS.
- echo '<script>alert("'
- . $e->getMessage()
- .'");document.location=\'./?do='
- . Router::$PAGE_PLUGINSADMIN
- .'\';</script>';
- exit;
- }
- header('Location: ./?do='. Router::$PAGE_PLUGINSADMIN);
- exit;
- }
-
- // Get a fresh token
- if ($targetPage == Router::$GET_TOKEN) {
- header('Content-Type:text/plain');
- echo $sessionManager->generateToken();
- exit;
- }
-
- // -------- Thumbnails Update
- if ($targetPage == Router::$PAGE_THUMBS_UPDATE) {
- $ids = [];
- foreach ($bookmarkService->search() as $bookmark) {
- // A note or not HTTP(S)
- if ($bookmark->isNote() || ! startsWith(strtolower($bookmark->getUrl()), 'http')) {
- continue;
- }
- $ids[] = $bookmark->getId();
- }
- $PAGE->assign('ids', $ids);
- $PAGE->assign('pagetitle', t('Thumbnails update') .' - '. $conf->get('general.title', 'Shaarli'));
- $PAGE->renderPage('thumbnails');
- exit;
- }
-
- // -------- Single Thumbnail Update
- if ($targetPage == Router::$AJAX_THUMB_UPDATE) {
- if (! isset($_POST['id']) || ! ctype_digit($_POST['id'])) {
- http_response_code(400);
- exit;
- }
- $id = (int) $_POST['id'];
- if (! $bookmarkService->exists($id)) {
- http_response_code(404);
- exit;
- }
- $thumbnailer = new Thumbnailer($conf);
- $bookmark = $bookmarkService->get($id);
- $bookmark->setThumbnail($thumbnailer->get($bookmark->getUrl()));
- $bookmarkService->set($bookmark);
-
- $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
- echo json_encode($factory->getFormatter('raw')->format($bookmark));
- exit;
- }
-
- // -------- Otherwise, simply display search form and bookmarks:
- showLinkList($PAGE, $bookmarkService, $conf, $pluginManager, $loginManager);
- exit;
-}
-
-/**
- * Template for the list of bookmarks (<div id="linklist">)
- * This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
- *
- * @param pageBuilder $PAGE pageBuilder instance.
- * @param BookmarkServiceInterface $linkDb LinkDB instance.
- * @param ConfigManager $conf Configuration Manager instance.
- * @param PluginManager $pluginManager Plugin Manager instance.
- * @param LoginManager $loginManager LoginManager instance
- */
-function buildLinkList($PAGE, $linkDb, $conf, $pluginManager, $loginManager)
-{
- $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
- $formatter = $factory->getFormatter();
-
- // Used in templates
- if (isset($_GET['searchtags'])) {
- if (! empty($_GET['searchtags'])) {
- $searchtags = escape(normalize_spaces($_GET['searchtags']));
- } else {
- $searchtags = false;
- }
- } else {
- $searchtags = '';
- }
- $searchterm = !empty($_GET['searchterm']) ? escape(normalize_spaces($_GET['searchterm'])) : '';
-
- // Smallhash filter
- if (! empty($_SERVER['QUERY_STRING'])
- && preg_match('/^[a-zA-Z0-9-_@]{6}($|&|#)/', $_SERVER['QUERY_STRING'])) {
- try {
- $linksToDisplay = $linkDb->findByHash($_SERVER['QUERY_STRING']);
- } catch (BookmarkNotFoundException $e) {
- $PAGE->render404($e->getMessage());
- exit;
- }
- } else {
- // Filter bookmarks according search parameters.
- $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : null;
- $request = [
- 'searchtags' => $searchtags,
- 'searchterm' => $searchterm,
- ];
- $linksToDisplay = $linkDb->search($request, $visibility, false, !empty($_SESSION['untaggedonly']));
- }
-
- // ---- Handle paging.
- $keys = array();
- foreach ($linksToDisplay as $key => $value) {
- $keys[] = $key;
- }
-
- // Select articles according to paging.
- $pagecount = ceil(count($keys) / $_SESSION['LINKS_PER_PAGE']);
- $pagecount = $pagecount == 0 ? 1 : $pagecount;
- $page= empty($_GET['page']) ? 1 : intval($_GET['page']);
- $page = $page < 1 ? 1 : $page;
- $page = $page > $pagecount ? $pagecount : $page;
- // Start index.
- $i = ($page-1) * $_SESSION['LINKS_PER_PAGE'];
- $end = $i + $_SESSION['LINKS_PER_PAGE'];
-
- $thumbnailsEnabled = $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE;
- if ($thumbnailsEnabled) {
- $thumbnailer = new Thumbnailer($conf);
- }
-
- $linkDisp = array();
- while ($i<$end && $i<count($keys)) {
- $link = $formatter->format($linksToDisplay[$keys[$i]]);
-
- // Logged in, thumbnails enabled, not a note,
- // and (never retrieved yet or no valid cache file)
- if ($loginManager->isLoggedIn()
- && $thumbnailsEnabled
- && !$linksToDisplay[$keys[$i]]->isNote()
- && $linksToDisplay[$keys[$i]]->getThumbnail() !== false
- && ! is_file($linksToDisplay[$keys[$i]]->getThumbnail())
- ) {
- $linksToDisplay[$keys[$i]]->setThumbnail($thumbnailer->get($link['url']));
- $linkDb->set($linksToDisplay[$keys[$i]], false);
- $updateDB = true;
- $link['thumbnail'] = $linksToDisplay[$keys[$i]]->getThumbnail();
- }
-
- // Check for both signs of a note: starting with ? and 7 chars long.
-// if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
-// $link['url'] = index_url($_SERVER) . $link['url'];
-// }
-
- $linkDisp[$keys[$i]] = $link;
- $i++;
- }
-
- // If we retrieved new thumbnails, we update the database.
- if (!empty($updateDB)) {
- $linkDb->save();
- }
-
- // Compute paging navigation
- $searchtagsUrl = $searchtags === '' ? '' : '&searchtags=' . urlencode($searchtags);
- $searchtermUrl = empty($searchterm) ? '' : '&searchterm=' . urlencode($searchterm);
- $previous_page_url = '';
- if ($i != count($keys)) {
- $previous_page_url = '?page=' . ($page+1) . $searchtermUrl . $searchtagsUrl;
- }
- $next_page_url='';
- if ($page>1) {
- $next_page_url = '?page=' . ($page-1) . $searchtermUrl . $searchtagsUrl;
- }
-
- // Fill all template fields.
- $data = array(
- 'previous_page_url' => $previous_page_url,
- 'next_page_url' => $next_page_url,
- 'page_current' => $page,
- 'page_max' => $pagecount,
- 'result_count' => count($linksToDisplay),
- 'search_term' => $searchterm,
- 'search_tags' => $searchtags,
- 'visibility' => ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '',
- 'links' => $linkDisp,
- );
-
- // If there is only a single link, we change on-the-fly the title of the page.
- if (count($linksToDisplay) == 1) {
- $data['pagetitle'] = $linksToDisplay[$keys[0]]->getTitle() .' - '. $conf->get('general.title');
- } elseif (! empty($searchterm) || ! empty($searchtags)) {
- $data['pagetitle'] = t('Search: ');
- $data['pagetitle'] .= ! empty($searchterm) ? $searchterm .' ' : '';
- $bracketWrap = function ($tag) {
- return '['. $tag .']';
- };
- $data['pagetitle'] .= ! empty($searchtags)
- ? implode(' ', array_map($bracketWrap, preg_split('/\s+/', $searchtags))).' '
- : '';
- $data['pagetitle'] .= '- '. $conf->get('general.title');
- }
-
- $pluginManager->executeHooks('render_linklist', $data, array('loggedin' => $loginManager->isLoggedIn()));
-
- foreach ($data as $key => $value) {
- $PAGE->assign($key, $value);
- }
-
- return;
-}
-
-/**
- * Installation
- * This function should NEVER be called if the file data/config.php exists.
- *
- * @param ConfigManager $conf Configuration Manager instance.
- * @param SessionManager $sessionManager SessionManager instance
- * @param LoginManager $loginManager LoginManager instance
- */
-function install($conf, $sessionManager, $loginManager)
-{
- // On free.fr host, make sure the /sessions directory exists, otherwise login will not work.
- if (endsWith($_SERVER['HTTP_HOST'], '.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'].'/sessions')) {
- mkdir($_SERVER['DOCUMENT_ROOT'].'/sessions', 0705);
- }
-
-
- // This part makes sure sessions works correctly.
- // (Because on some hosts, session.save_path may not be set correctly,
- // or we may not have write access to it.)
- if (isset($_GET['test_session'])
- && ( !isset($_SESSION) || !isset($_SESSION['session_tested']) || $_SESSION['session_tested']!='Working')) {
- // Step 2: Check if data in session is correct.
- $msg = t(
- '<pre>Sessions do not seem to work correctly on your server.<br>'.
- 'Make sure the variable "session.save_path" is set correctly in your PHP config, '.
- 'and that you have write access to it.<br>'.
- 'It currently points to %s.<br>'.
- 'On some browsers, accessing your server via a hostname like \'localhost\' '.
- 'or any custom hostname without a dot causes cookie storage to fail. '.
- 'We recommend accessing your server via it\'s IP address or Fully Qualified Domain Name.<br>'
- );
- $msg = sprintf($msg, session_save_path());
- echo $msg;
- echo '<br><a href="?">'. t('Click to try again.') .'</a></pre>';
- die;
- }
- if (!isset($_SESSION['session_tested'])) {
- // Step 1 : Try to store data in session and reload page.
- $_SESSION['session_tested'] = 'Working'; // Try to set a variable in session.
- header('Location: '.index_url($_SERVER).'?test_session'); // Redirect to check stored data.
- }
- if (isset($_GET['test_session'])) {
- // Step 3: Sessions are OK. Remove test parameter from URL.
- header('Location: '.index_url($_SERVER));
- }
-
-
- if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) {
- $tz = 'UTC';
- if (!empty($_POST['continent']) && !empty($_POST['city'])
- && isTimeZoneValid($_POST['continent'], $_POST['city'])
- ) {
- $tz = $_POST['continent'].'/'.$_POST['city'];
- }
- $conf->set('general.timezone', $tz);
- $login = $_POST['setlogin'];
- $conf->set('credentials.login', $login);
- $salt = sha1(uniqid('', true) .'_'. mt_rand());
- $conf->set('credentials.salt', $salt);
- $conf->set('credentials.hash', sha1($_POST['setpassword'] . $login . $salt));
- if (!empty($_POST['title'])) {
- $conf->set('general.title', escape($_POST['title']));
- } else {
- $conf->set('general.title', 'Shared bookmarks on '.escape(index_url($_SERVER)));
- }
- $conf->set('translation.language', escape($_POST['language']));
- $conf->set('updates.check_updates', !empty($_POST['updateCheck']));
- $conf->set('api.enabled', !empty($_POST['enableApi']));
- $conf->set(
- 'api.secret',
- generate_api_secret(
- $conf->get('credentials.login'),
- $conf->get('credentials.salt')
- )
- );
- try {
- // Everything is ok, let's create config file.
- $conf->write($loginManager->isLoggedIn());
- } catch (Exception $e) {
- error_log(
- 'ERROR while writing config file after installation.' . PHP_EOL .
- $e->getMessage()
- );
-
- // TODO: do not handle exceptions/errors in JS.
- echo '<script>alert("'. $e->getMessage() .'");document.location=\'?\';</script>';
- exit;
- }
-
- $history = new History($conf->get('resource.history'));
- $bookmarkService = new BookmarkFileService($conf, $history, true);
- if ($bookmarkService->count() === 0) {
- $bookmarkService->initialize();
- }
-
- echo '<script>alert('
- .'"Shaarli is now configured. '
- .'Please enter your login/password and start shaaring your bookmarks!"'
- .');document.location=\'./login\';</script>';
- exit;
- }
-
- $PAGE = new PageBuilder($conf, $_SESSION, null, $sessionManager->generateToken());
- list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get());
- $PAGE->assign('continents', $continents);
- $PAGE->assign('cities', $cities);
- $PAGE->assign('languages', Languages::getAvailableLanguages());
- $PAGE->renderPage('install');
- exit;
-}
-
-if (!isset($_SESSION['LINKS_PER_PAGE'])) {
- $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20);
-}
-
-try {
- $history = new History($conf->get('resource.history'));
-} catch (Exception $e) {
- die($e->getMessage());
-}
-
-$linkDb = new BookmarkFileService($conf, $history, $loginManager->isLoggedIn());
-
-if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) {
- header('Location: ./daily-rss');
- exit;
-}
-
-$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager);
-$container = $containerBuilder->build();
-$app = new App($container);