- foreach ($common_hooks as $name) {
- $plugin_data = array();
- $pluginManager->executeHooks(
- 'render_' . $name,
- $plugin_data,
- array(
- 'target' => $targetPage,
- 'loggedin' => $loginManager->isLoggedIn()
- )
- );
- $PAGE->assign('plugins_' . $name, $plugin_data);
- }
-
- // -------- Display login form.
- if ($targetPage == Router::$PAGE_LOGIN) {
- header('Location: ./login');
- exit;
- }
- // -------- User wants to logout.
- if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) {
- header('Location: ./logout');
- exit;
- }
-
- // -------- Picture wall
- if ($targetPage == Router::$PAGE_PICWALL) {
- header('Location: ./picture-wall');
- exit;
- }
-
- // -------- Tag cloud
- if ($targetPage == Router::$PAGE_TAGCLOUD) {
- header('Location: ./tags/cloud');
- exit;
- }
-
- // -------- Tag list
- if ($targetPage == Router::$PAGE_TAGLIST) {
- header('Location: ./tags/list');
- exit;
- }
-
- // Daily page.
- if ($targetPage == Router::$PAGE_DAILY) {
- $dayParam = !empty($_GET['day']) ? '?day=' . escape($_GET['day']) : '';
- header('Location: ./daily'. $dayParam);
- exit;
- }
-
- // ATOM and RSS feed.
- if ($targetPage == Router::$PAGE_FEED_ATOM || $targetPage == Router::$PAGE_FEED_RSS) {
- $feedType = $targetPage == Router::$PAGE_FEED_RSS ? FeedBuilder::$FEED_RSS : FeedBuilder::$FEED_ATOM;
-
- header('Location: ./feed/'. $feedType .'?'. http_build_query($_GET));
- exit;
- }
-
- // Display opensearch plugin (XML)
- if ($targetPage == Router::$PAGE_OPENSEARCH) {
- header('Location: ./open-search');
- exit;
- }
-
- // -------- User clicks on a tag in a link: The tag is added to the list of searched tags (searchtags=...)
- if (isset($_GET['addtag'])) {
- header('Location: ./add-tag/'. $_GET['addtag']);
- exit;
- }
-
- // -------- User clicks on a tag in result count: Remove the tag from the list of searched tags (searchtags=...)
- if (isset($_GET['removetag'])) {
- header('Location: ./remove-tag/'. $_GET['removetag']);
- exit;
- }
-
- // -------- User wants to change the number of bookmarks per page (linksperpage=...)
- if (isset($_GET['linksperpage'])) {
- header('Location: ./links-per-page?nb='. $_GET['linksperpage']);
- exit;
- }
-
- // -------- User wants to see only private bookmarks (toggle)
- if (isset($_GET['visibility'])) {
- header('Location: ./visibility/'. $_GET['visibility']);
- exit;
- }
-
- // -------- User wants to see only untagged bookmarks (toggle)
- if (isset($_GET['untaggedonly'])) {
- header('Location: ./untagged-only');
- exit;
- }
-
- // -------- Handle other actions allowed for non-logged in users:
- if (!$loginManager->isLoggedIn()) {
- // User tries to post new link but is not logged in:
- // Show login screen, then redirect to ?post=...
- if (isset($_GET['post'])) {
- header( // Redirect to login page, then back to post link.
- 'Location: ./login?post='.urlencode($_GET['post']).
- (!empty($_GET['title'])?'&title='.urlencode($_GET['title']):'').
- (!empty($_GET['description'])?'&description='.urlencode($_GET['description']):'').
- (!empty($_GET['tags'])?'&tags='.urlencode($_GET['tags']):'').
- (!empty($_GET['source'])?'&source='.urlencode($_GET['source']):'')
- );
- exit;
- }
-
- showLinkList($PAGE, $bookmarkService, $conf, $pluginManager, $loginManager);
- if (isset($_GET['edit_link'])) {
- header('Location: ./login?edit_link='. escape($_GET['edit_link']));
- exit;
- }
-
- exit; // Never remove this one! All operations below are reserved for logged in user.
- }
-
- // -------- All other functions are reserved for the registered user:
-
- // -------- Display the Tools menu if requested (import/export/bookmarklet...)
- if ($targetPage == Router::$PAGE_TOOLS) {
- header('Location: ./admin/tools');
- exit;
- }
-
- // -------- User wants to change his/her password.
- if ($targetPage == Router::$PAGE_CHANGEPASSWORD) {
- header('Location: ./admin/password');
- exit;
- }
-
- // -------- User wants to change configuration
- if ($targetPage == Router::$PAGE_CONFIGURE) {
- header('Location: ./admin/configure');
- exit;
- }
-
- // -------- User wants to rename a tag or delete it
- if ($targetPage == Router::$PAGE_CHANGETAG) {
- header('Location: ./admin/tags');
- exit;
- }
-
- // -------- User wants to add a link without using the bookmarklet: Show form.
- if ($targetPage == Router::$PAGE_ADDLINK) {
- header('Location: ./admin/shaare');
- exit;
- }
-
- // -------- User clicked the "Save" button when editing a link: Save link to database.
- if (isset($_POST['save_edit'])) {
- // This route is no longer supported in legacy mode
- header('Location: ./');
- exit;
- }
-
- // -------- User clicked the "Delete" button when editing a link: Delete link from database.
- if ($targetPage == Router::$PAGE_DELETELINK) {
- $ids = $_GET['lf_linkdate'] ?? '';
- $token = $_GET['token'] ?? '';
-
- header('Location: ./admin/shaare/delete?id=' . $ids . '&token=' . $token);
- exit;
- }
-
- // -------- User clicked either "Set public" or "Set private" bulk operation
- if ($targetPage == Router::$PAGE_CHANGE_VISIBILITY) {
- if (! $sessionManager->checkToken($_GET['token'])) {
- die(t('Wrong token.'));
- }
-
- $ids = trim($_GET['ids']);
- if (strpos($ids, ' ') !== false) {
- // multiple, space-separated ids provided
- $ids = array_values(array_filter(preg_split('/\s+/', escape($ids))));
- } else {
- // only a single id provided
- $ids = [$ids];
- }
-
- // assert at least one id is given
- if (!count($ids)) {
- die('no id provided');
- }
- // assert that the visibility is valid
- if (!isset($_GET['newVisibility']) || !in_array($_GET['newVisibility'], ['public', 'private'])) {
- die('invalid visibility');
- } else {
- $private = $_GET['newVisibility'] === 'private';
- }
- $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
- $formatter = $factory->getFormatter('raw');
- foreach ($ids as $id) {
- $id = (int) escape($id);
- $bookmark = $bookmarkService->get($id);
- $bookmark->setPrivate($private);
-
- // To preserve backward compatibility with 3rd parties, plugins still use arrays
- $data = $formatter->format($bookmark);
- $pluginManager->executeHooks('save_link', $data);
- $bookmark->fromArray($data);
-
- $bookmarkService->set($bookmark);
- }
- $bookmarkService->save();
-
- $location = '?';
- if (isset($_SERVER['HTTP_REFERER'])) {
- $location = generateLocation(
- $_SERVER['HTTP_REFERER'],
- $_SERVER['HTTP_HOST']
- );
- }
- header('Location: ' . $location); // After deleting the link, redirect to appropriate location
- exit;
- }
-
- // -------- User clicked the "EDIT" button on a link: Display link edit form.
- if (isset($_GET['edit_link'])) {
- $id = (int) escape($_GET['edit_link']);
- header('Location: ./admin/shaare/' . $id);
- exit;
- }
-
- // -------- User want to post a new link: Display link edit form.
- if (isset($_GET['post'])) {
- header('Location: ./admin/shaare?' . http_build_query($_GET));
- exit;
- }
-
- if ($targetPage == Router::$PAGE_PINLINK) {
- if (! isset($_GET['id']) || !$bookmarkService->exists($_GET['id'])) {
- // FIXME! Use a proper error system.
- $msg = t('Invalid link ID provided');
- echo '<script>alert("'. $msg .'");document.location=\''. index_url($_SERVER) .'\';</script>';
- exit;
- }
- if (! $sessionManager->checkToken($_GET['token'])) {
- die('Wrong token.');
- }
-
- $link = $bookmarkService->get($_GET['id']);
- $link->setSticky(! $link->isSticky());
- $bookmarkService->set($link);
- header('Location: '.index_url($_SERVER));
- exit;
- }
-
- if ($targetPage == Router::$PAGE_EXPORT) {
- // Export bookmarks as a Netscape Bookmarks file
-
- if (empty($_GET['selection'])) {
- $PAGE->assign('pagetitle', t('Export') .' - '. $conf->get('general.title', 'Shaarli'));
- $PAGE->renderPage('export');
- exit;
- }
-
- // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html
- $selection = $_GET['selection'];
- if (isset($_GET['prepend_note_url'])) {
- $prependNoteUrl = $_GET['prepend_note_url'];
- } else {
- $prependNoteUrl = false;
- }
-
- try {
- $factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
- $formatter = $factory->getFormatter('raw');
- $PAGE->assign(
- 'links',
- NetscapeBookmarkUtils::filterAndFormat(
- $bookmarkService,
- $formatter,
- $selection,
- $prependNoteUrl,
- index_url($_SERVER)
- )
- );
- } catch (Exception $exc) {
- header('Content-Type: text/plain; charset=utf-8');
- echo $exc->getMessage();
- exit;
- }
- $now = new DateTime();
- header('Content-Type: text/html; charset=utf-8');
- header(
- 'Content-disposition: attachment; filename=bookmarks_'
- .$selection.'_'.$now->format(Bookmark::LINK_DATE_FORMAT).'.html'
- );
- $PAGE->assign('date', $now->format(DateTime::RFC822));
- $PAGE->assign('eol', PHP_EOL);
- $PAGE->assign('selection', $selection);
- $PAGE->renderPage('export.bookmarks');
- exit;
- }
-
- if ($targetPage == Router::$PAGE_IMPORT) {
- // Upload a Netscape bookmark dump to import its contents
-
- if (! isset($_POST['token']) || ! isset($_FILES['filetoupload'])) {
- // Show import dialog
- $PAGE->assign(
- 'maxfilesize',
- get_max_upload_size(
- ini_get('post_max_size'),
- ini_get('upload_max_filesize'),
- false
- )
- );
- $PAGE->assign(
- 'maxfilesizeHuman',
- get_max_upload_size(
- ini_get('post_max_size'),
- ini_get('upload_max_filesize'),
- true
- )
- );
- $PAGE->assign('pagetitle', t('Import') .' - '. $conf->get('general.title', 'Shaarli'));
- $PAGE->renderPage('import');
- exit;
- }
-
- // Import bookmarks from an uploaded file
- if (isset($_FILES['filetoupload']['size']) && $_FILES['filetoupload']['size'] == 0) {
- // The file is too big or some form field may be missing.
- $msg = sprintf(
- t(
- 'The file you are trying to upload is probably bigger than what this webserver can accept'
- .' (%s). Please upload in smaller chunks.'
- ),
- get_max_upload_size(ini_get('post_max_size'), ini_get('upload_max_filesize'))
- );
- echo '<script>alert("'. $msg .'");document.location=\'./?do='.Router::$PAGE_IMPORT .'\';</script>';
- exit;
- }
- if (! $sessionManager->checkToken($_POST['token'])) {
- die('Wrong token.');
- }
- $status = NetscapeBookmarkUtils::import(
- $_POST,
- $_FILES,
- $bookmarkService,
- $conf,
- $history
- );
- echo '<script>alert("'.$status.'");document.location=\'./?do='
- .Router::$PAGE_IMPORT .'\';</script>';
- exit;
- }
-
- // Plugin administration page
- if ($targetPage == Router::$PAGE_PLUGINSADMIN) {
- $pluginMeta = $pluginManager->getPluginsMeta();
-
- // Split plugins into 2 arrays: ordered enabled plugins and disabled.
- $enabledPlugins = array_filter($pluginMeta, function ($v) {
- return $v['order'] !== false;
- });
- // Load parameters.
- $enabledPlugins = load_plugin_parameter_values($enabledPlugins, $conf->get('plugins', array()));
- uasort(
- $enabledPlugins,
- function ($a, $b) {
- return $a['order'] - $b['order'];