- $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;