-
- $PAGE->assign('enabledPlugins', $enabledPlugins);
- $PAGE->assign('disabledPlugins', $disabledPlugins);
- $PAGE->assign('pagetitle', t('Plugin administration') .' - '. $conf->get('general.title', 'Shaarli'));
- $PAGE->renderPage('pluginsadmin');
- exit;
- }
-
- // 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');