X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=index.php;h=eb717536c58f33d8b8a63b7933765c075d364b35;hb=4fa9a3c5d83a1024678596a586afe5df14a345b5;hp=28dfd3b473dbd3f7b39d61295a0ec055681afacd;hpb=1b93137e16694f52952c930848e1a7928e8a00a6;p=github%2Fshaarli%2FShaarli.git diff --git a/index.php b/index.php index 28dfd3b4..eb717536 100644 --- a/index.php +++ b/index.php @@ -74,13 +74,13 @@ require_once 'application/Url.php'; require_once 'application/Utils.php'; require_once 'application/PluginManager.php'; require_once 'application/Router.php'; -require_once 'application/Thumbnailer.php'; require_once 'application/Updater.php'; -use \Shaarli\Languages; -use \Shaarli\ThemeUtils; use \Shaarli\Config\ConfigManager; +use \Shaarli\Languages; use \Shaarli\Security\LoginManager; use \Shaarli\Security\SessionManager; +use \Shaarli\ThemeUtils; +use \Shaarli\Thumbnailer; // Ensure the PHP version is supported try { @@ -356,7 +356,6 @@ function showDailyRSS($conf, $loginManager) { $conf->get('redirector.url'), $conf->get('redirector.encode_url') ); - $link['thumbnail'] = thumbnail($conf, $link['url']); $link['timestamp'] = $link['created']->getTimestamp(); if (startsWith($link['url'], '?')) { $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute @@ -371,6 +370,7 @@ function showDailyRSS($conf, $loginManager) { $tpl->assign('links', $links); $tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS))); $tpl->assign('hide_timestamps', $conf->get('privacy.hide_timestamps', false)); + $tpl->assign('index_url', $pageaddr); $html = $tpl->draw('dailyrss', true); echo $html . PHP_EOL; @@ -433,7 +433,6 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager, $loginManager) $conf->get('redirector.url'), $conf->get('redirector.encode_url') ); - $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp(); } @@ -514,7 +513,8 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, read_updates_file($conf->get('resource.updates')), $LINKSDB, $conf, - $loginManager->isLoggedIn() + $loginManager->isLoggedIn(), + $_SESSION ); try { $newUpdates = $updater->update(); @@ -529,7 +529,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, die($e->getMessage()); } - $PAGE = new PageBuilder($conf, $LINKSDB, $sessionManager->generateToken(), $loginManager->isLoggedIn()); + $PAGE = new PageBuilder($conf, $_SESSION, $LINKSDB, $sessionManager->generateToken(), $loginManager->isLoggedIn()); $PAGE->assign('linkcount', count($LINKSDB)); $PAGE->assign('privateLinkcount', count_private($LINKSDB)); $PAGE->assign('plugin_errors', $pluginManager->getErrors()); @@ -602,8 +602,10 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, // -------- Picture wall if ($targetPage == Router::$PAGE_PICWALL) { - if (! $conf->get('thumbnails.enabled')) { - header('Location: ?'); + $PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli')); + if (! $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) { + $PAGE->assign('linksToDisplay', []); + $PAGE->renderPage('picwall'); exit; } @@ -611,43 +613,13 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $links = $LINKSDB->filterSearch($_GET); $linksToDisplay = array(); - $thumbnailer = new Thumbnailer($conf); - - - $cpt = 0; // Get only links which have a thumbnail. - foreach($links as $link) + // Note: we do not retrieve thumbnails here, the request is too heavy. + foreach($links as $key => $link) { - $permalink='?'.$link['shorturl']; - // Not a note, - // and (never retrieved yet or no valid cache file) - if ($link['url'][0] != '?' - && (! isset($link['thumbnail']) || ($link['thumbnail'] !== false && ! is_file($link['thumbnail']))) - ) { - $link['thumbnail'] = $thumbnailer->get($link['url']); - // FIXME! we really need to get rid of ArrayAccess... - $item = $LINKSDB[$link['linkdate']]; - $item['thumbnail'] = $link['thumbnail']; - $LINKSDB[$link['linkdate']] = $item; - $updateDB = true; - $cpt++; - } - if (isset($link['thumbnail']) && $link['thumbnail'] !== false) { $linksToDisplay[] = $link; // Add to array. } - - // If we retrieved new thumbnails, we update the database every 20 links. - // Downloading everything the first time may take a very long time - if (!empty($updateDB) && $cpt == 20) { - $LINKSDB->save($conf->get('resource.page_cache')); - $updateDB = false; - $cpt = 0; - } - } - - if (!empty($updateDB)) { - $LINKSDB->save($conf->get('resource.page_cache')); } $data = array( @@ -659,7 +631,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $PAGE->assign($key, $value); } - $PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli')); + $PAGE->renderPage('picwall'); exit; } @@ -1041,7 +1013,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $conf->set('api.enabled', !empty($_POST['enableApi'])); $conf->set('api.secret', escape($_POST['apiSecret'])); $conf->set('translation.language', escape($_POST['language'])); - $conf->set('thumbnails.enabled', !empty($_POST['enableThumbnails'])); + + $thumbnailsMode = extension_loaded('gd') ? $_POST['enableThumbnails'] : Thumbnailer::MODE_NONE; + if ($thumbnailsMode !== Thumbnailer::MODE_NONE + && $thumbnailsMode !== $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) + ) { + $_SESSION['warnings'][] = t( + 'You have enabled or changed thumbnails mode. Please synchronize them.' + ); + } + $conf->set('thumbnails.mode', $thumbnailsMode); try { $conf->write($loginManager->isLoggedIn()); @@ -1081,6 +1062,8 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $PAGE->assign('api_secret', $conf->get('api.secret')); $PAGE->assign('languages', Languages::getAvailableLanguages()); $PAGE->assign('language', $conf->get('translation.language')); + $PAGE->assign('gd_enabled', extension_loaded('gd')); + $PAGE->assign('thumbnails_mode', $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE)); $PAGE->assign('pagetitle', t('Configure') .' - '. $conf->get('general.title', 'Shaarli')); $PAGE->renderPage('configure'); exit; @@ -1101,7 +1084,8 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, die(t('Wrong token.')); } - $alteredLinks = $LINKSDB->renameTag(escape($_POST['fromtag']), escape($_POST['totag'])); + $toTag = isset($_POST['totag']) ? escape($_POST['totag']) : null; + $alteredLinks = $LINKSDB->renameTag(escape($_POST['fromtag']), $toTag); $LINKSDB->save($conf->get('resource.page_cache')); foreach ($alteredLinks as $link) { $history->updateLink($link); @@ -1137,7 +1121,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, // Linkdate is kept here to: // - use the same permalink for notes as they're displayed when creating them // - let users hack creation date of their posts - // See: https://shaarli.readthedocs.io/en/master/Various-hacks/#changing-the-timestamp-for-a-shaare + // See: https://shaarli.readthedocs.io/en/master/guides/various-hacks/#changing-the-timestamp-for-a-shaare $linkdate = escape($_POST['lf_linkdate']); if (isset($LINKSDB[$id])) { // Edit @@ -1182,7 +1166,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $link['title'] = $link['url']; } - if ($conf->get('thumbnails.enabled')) { + if ($conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE) { $thumbnailer = new Thumbnailer($conf); $link['thumbnail'] = $thumbnailer->get($url); } @@ -1253,10 +1237,10 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $id = (int) escape($id); $link = $LINKSDB[$id]; $pluginManager->executeHooks('delete_link', $link); + $history->deleteLink($link); unset($LINKSDB[$id]); } $LINKSDB->save($conf->get('resource.page_cache')); // save to disk - $history->deleteLink($link); // If we are called from the bookmarklet, we must close the popup: if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo ''; exit; } @@ -1525,6 +1509,43 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, exit; } + // -------- Thumbnails Update + if ($targetPage == Router::$PAGE_THUMBS_UPDATE) { + $ids = []; + foreach ($LINKSDB as $link) { + // A note or not HTTP(S) + if ($link['url'][0] === '?' || ! startsWith(strtolower($link['url']), 'http')) { + continue; + } + $ids[] = $link['id']; + } + $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 (empty($LINKSDB[$id])) { + http_response_code(404); + exit; + } + $thumbnailer = new Thumbnailer($conf); + $link = $LINKSDB[$id]; + $link['thumbnail'] = $thumbnailer->get($link['url']); + $LINKSDB[$id] = $link; + $LINKSDB->save($conf->get('resource.page_cache')); + + echo json_encode($link); + exit; + } + // -------- Otherwise, simply display search form and links: showLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager); exit; @@ -1589,7 +1610,8 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager) $i = ($page-1) * $_SESSION['LINKS_PER_PAGE']; $end = $i + $_SESSION['LINKS_PER_PAGE']; - if ($conf->get('thumbnails.enabled')) { + $thumbnailsEnabled = $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE; + if ($thumbnailsEnabled) { $thumbnailer = new Thumbnailer($conf); } @@ -1614,17 +1636,16 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager) uasort($taglist, 'strcasecmp'); $link['taglist'] = $taglist; - // Thumbnails enabled, not a note, + // Logged in, thumbnails enabled, not a note, // and (never retrieved yet or no valid cache file) - if ($conf->get('thumbnails.enabled') && $link['url'][0] != '?' + if ($loginManager->isLoggedIn() && $thumbnailsEnabled && $link['url'][0] != '?' && (! isset($link['thumbnail']) || ($link['thumbnail'] !== false && ! is_file($link['thumbnail']))) ) { - $link['thumbnail'] = $thumbnailer->get($link['url']); - // FIXME! we really need to get rid of ArrayAccess... - $item = $LINKSDB[$keys[$i]]; - $item['thumbnail'] = $link['thumbnail']; - $LINKSDB[$keys[$i]] = $item; + $elem = $LINKSDB[$keys[$i]]; + $elem['thumbnail'] = $thumbnailer->get($link['url']); + $LINKSDB[$keys[$i]] = $elem; $updateDB = true; + $link['thumbnail'] = $elem['thumbnail']; } // Check for both signs of a note: starting with ? and 7 chars long. @@ -1782,7 +1803,7 @@ function install($conf, $sessionManager, $loginManager) { exit; } - $PAGE = new PageBuilder($conf, null, $sessionManager->generateToken()); + $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); @@ -1791,7 +1812,11 @@ function install($conf, $sessionManager, $loginManager) { exit; } -if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { showDailyRSS($conf); exit; } +if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { + showDailyRSS($conf, $loginManager); + exit; +} + if (!isset($_SESSION['LINKS_PER_PAGE'])) { $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20); } @@ -1824,6 +1849,12 @@ $app->group('/api/v1', function() { $this->post('/links', '\Shaarli\Api\Controllers\Links:postLink')->setName('postLink'); $this->put('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:putLink')->setName('putLink'); $this->delete('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:deleteLink')->setName('deleteLink'); + + $this->get('/tags', '\Shaarli\Api\Controllers\Tags:getTags')->setName('getTags'); + $this->get('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:getTag')->setName('getTag'); + $this->put('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:putTag')->setName('putTag'); + $this->delete('/tags/{tagName:[\w]+}', '\Shaarli\Api\Controllers\Tags:deleteTag')->setName('deleteTag'); + $this->get('/history', '\Shaarli\Api\Controllers\History:getHistory')->setName('getHistory'); })->add('\Shaarli\Api\ApiMiddleware');