)
* This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
*
- * @param pageBuilder $PAGE pageBuilder instance.
- * @param LinkDB $LINKSDB LinkDB instance.
- * @param ConfigManager $conf Configuration Manager instance.
- * @param PluginManager $pluginManager Plugin Manager instance.
- * @param LoginManager $loginManager LoginManager instance
+ * @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, $LINKSDB, $conf, $pluginManager, $loginManager)
+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'])) {
@@ -1598,19 +1676,19 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
if (! empty($_SERVER['QUERY_STRING'])
&& preg_match('/^[a-zA-Z0-9-_@]{6}($|&|#)/', $_SERVER['QUERY_STRING'])) {
try {
- $linksToDisplay = $LINKSDB->filterHash($_SERVER['QUERY_STRING']);
- } catch (LinkNotFoundException $e) {
+ $linksToDisplay = $linkDb->findByHash($_SERVER['QUERY_STRING']);
+ } catch (BookmarkNotFoundException $e) {
$PAGE->render404($e->getMessage());
exit;
}
} else {
- // Filter links according search parameters.
- $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
+ // Filter bookmarks according search parameters.
+ $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : null;
$request = [
'searchtags' => $searchtags,
'searchterm' => $searchterm,
];
- $linksToDisplay = $LINKSDB->filterSearch($request, false, $visibility, !empty($_SESSION['untaggedonly']));
+ $linksToDisplay = $linkDb->search($request, $visibility, false, !empty($_SESSION['untaggedonly']));
}
// ---- Handle paging.
@@ -1635,42 +1713,27 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
}
$linkDisp = array();
- while ($i<$end && $i
get('redirector.url'),
- $conf->get('redirector.encode_url')
- );
- $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
- $link['class'] = $link['private'] == 0 ? $classLi : 'private';
- $link['timestamp'] = $link['created']->getTimestamp();
- if (! empty($link['updated'])) {
- $link['updated_timestamp'] = $link['updated']->getTimestamp();
- } else {
- $link['updated_timestamp'] = '';
- }
- $taglist = preg_split('/\s+/', $link['tags'], -1, PREG_SPLIT_NO_EMPTY);
- uasort($taglist, 'strcasecmp');
- $link['taglist'] = $taglist;
+ while ($i<$end && $iformat($linksToDisplay[$keys[$i]]);
// Logged in, thumbnails enabled, not a note,
// and (never retrieved yet or no valid cache file)
- if ($loginManager->isLoggedIn() && $thumbnailsEnabled && $link['url'][0] != '?'
- && (! isset($link['thumbnail']) || ($link['thumbnail'] !== false && ! is_file($link['thumbnail'])))
+ if ($loginManager->isLoggedIn()
+ && $thumbnailsEnabled
+ && !$linksToDisplay[$keys[$i]]->isNote()
+ && $linksToDisplay[$keys[$i]]->getThumbnail() !== false
+ && ! is_file($linksToDisplay[$keys[$i]]->getThumbnail())
) {
- $elem = $LINKSDB[$keys[$i]];
- $elem['thumbnail'] = $thumbnailer->get($link['url']);
- $LINKSDB[$keys[$i]] = $elem;
+ $linksToDisplay[$keys[$i]]->setThumbnail($thumbnailer->get($link['url']));
+ $linkDb->set($linksToDisplay[$keys[$i]], false);
$updateDB = true;
- $link['thumbnail'] = $elem['thumbnail'];
+ $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'];
- }
+// if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
+// $link['url'] = index_url($_SERVER) . $link['url'];
+// }
$linkDisp[$keys[$i]] = $link;
$i++;
@@ -1678,7 +1741,7 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
// If we retrieved new thumbnails, we update the database.
if (!empty($updateDB)) {
- $LINKSDB->save($conf->get('resource.page_cache'));
+ $linkDb->save();
}
// Compute paging navigation
@@ -1703,13 +1766,12 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
'search_term' => $searchterm,
'search_tags' => $searchtags,
'visibility' => ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '',
- 'redirector' => $conf->get('redirector.url'), // Optional redirector URL.
'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]]['title'] .' - '. $conf->get('general.title');
+ $data['pagetitle'] = $linksToDisplay[$keys[0]]->getTitle() .' - '. $conf->get('general.title');
} elseif (! empty($searchterm) || ! empty($searchtags)) {
$data['pagetitle'] = t('Search: ');
$data['pagetitle'] .= ! empty($searchterm) ? $searchterm .' ' : '';
@@ -1739,16 +1801,19 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
* @param SessionManager $sessionManager SessionManager instance
* @param LoginManager $loginManager LoginManager instance
*/
-function install($conf, $sessionManager, $loginManager) {
+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);
+ 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'))
- {
+ 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(
'Sessions do not seem to work correctly on your server.
'.
@@ -1764,19 +1829,18 @@ function install($conf, $sessionManager, $loginManager) {
echo '
'. t('Click to try again.') .'
';
die;
}
- if (!isset($_SESSION['session_tested']))
- { // Step 1 : Try to store data in session and reload page.
+ 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.
+ 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']))
- {
+ if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) {
$tz = 'UTC';
if (!empty($_POST['continent']) && !empty($_POST['city'])
&& isTimeZoneValid($_POST['continent'], $_POST['city'])
@@ -1792,7 +1856,7 @@ function install($conf, $sessionManager, $loginManager) {
if (!empty($_POST['title'])) {
$conf->set('general.title', escape($_POST['title']));
} else {
- $conf->set('general.title', 'Shared links on '.escape(index_url($_SERVER)));
+ $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']));
@@ -1807,18 +1871,27 @@ function install($conf, $sessionManager, $loginManager) {
try {
// Everything is ok, let's create config file.
$conf->write($loginManager->isLoggedIn());
- }
- catch(Exception $e) {
+ } catch (Exception $e) {
error_log(
- 'ERROR while writing config file after installation.' . PHP_EOL .
+ 'ERROR while writing config file after installation.' . PHP_EOL .
$e->getMessage()
- );
+ );
// TODO: do not handle exceptions/errors in JS.
echo '';
exit;
}
- echo '';
+
+ $history = new History($conf->get('resource.history'));
+ $bookmarkService = new BookmarkFileService($conf, $history, true);
+ if ($bookmarkService->count() === 0) {
+ $bookmarkService->initialize();
+ }
+
+ echo '';
exit;
}
@@ -1831,37 +1904,29 @@ function install($conf, $sessionManager, $loginManager) {
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);
}
try {
$history = new History($conf->get('resource.history'));
-} catch(Exception $e) {
+} catch (Exception $e) {
die($e->getMessage());
}
-$linkDb = new LinkDB(
- $conf->get('resource.datastore'),
- $loginManager->isLoggedIn(),
- $conf->get('privacy.hide_public_links'),
- $conf->get('redirector.url'),
- $conf->get('redirector.encode_url')
-);
+$linkDb = new BookmarkFileService($conf, $history, $loginManager->isLoggedIn());
-$container = new \Slim\Container();
-$container['conf'] = $conf;
-$container['plugins'] = $pluginManager;
-$container['history'] = $history;
-$app = new \Slim\App($container);
+if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) {
+ showDailyRSS($linkDb, $conf, $loginManager);
+ exit;
+}
+
+$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager);
+$container = $containerBuilder->build();
+$app = new App($container);
// REST API routes
-$app->group('/api/v1', function() {
+$app->group('/api/v1', function () {
$this->get('/info', '\Shaarli\Api\Controllers\Info:getInfo')->setName('getInfo');
$this->get('/links', '\Shaarli\Api\Controllers\Links:getLinks')->setName('getLinks');
$this->get('/links/{id:[\d]+}', '\Shaarli\Api\Controllers\Links:getLink')->setName('getLink');
@@ -1874,9 +1939,13 @@ $app->group('/api/v1', function() {
$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');
+ $this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory');
})->add('\Shaarli\Api\ApiMiddleware');
+$app->group('', function () {
+ $this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
+})->add('\Shaarli\Front\ShaarliMiddleware');
+
$response = $app->run(true);
// Hack to make Slim and Shaarli router work together: