From c22fa57a5505fe95fd01860e3d3dfbb089f869cd Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 6 Jun 2020 14:01:03 +0200 Subject: Handle shaare creation/edition/deletion through Slim controllers --- index.php | 180 ++++++++------------------------------------------------------ 1 file changed, 22 insertions(+), 158 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 00e4a40b..fb528eeb 100644 --- a/index.php +++ b/index.php @@ -519,69 +519,20 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM // -------- User wants to rename a tag or delete it if ($targetPage == Router::$PAGE_CHANGETAG) { - header('./manage-tags'); + header('Location: ./manage-tags'); exit; } // -------- User wants to add a link without using the bookmarklet: Show form. if ($targetPage == Router::$PAGE_ADDLINK) { - $PAGE->assign('pagetitle', t('Shaare a new link') .' - '. $conf->get('general.title', 'Shaarli')); - $PAGE->renderPage('addlink'); + header('Location: ./shaare'); exit; } // -------- User clicked the "Save" button when editing a link: Save link to database. if (isset($_POST['save_edit'])) { - // Go away! - if (! $sessionManager->checkToken($_POST['token'])) { - die(t('Wrong token.')); - } - - // lf_id should only be present if the link exists. - $id = isset($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : null; - if ($id && $bookmarkService->exists($id)) { - // Edit - $bookmark = $bookmarkService->get($id); - } else { - // New link - $bookmark = new Bookmark(); - } - - $bookmark->setTitle($_POST['lf_title']); - $bookmark->setDescription($_POST['lf_description']); - $bookmark->setUrl($_POST['lf_url'], $conf->get('security.allowed_protocols')); - $bookmark->setPrivate(isset($_POST['lf_private'])); - $bookmark->setTagsString($_POST['lf_tags']); - - if ($conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE - && ! $bookmark->isNote() - ) { - $thumbnailer = new Thumbnailer($conf); - $bookmark->setThumbnail($thumbnailer->get($bookmark->getUrl())); - } - $bookmarkService->addOrSet($bookmark, false); - - // To preserve backward compatibility with 3rd parties, plugins still use arrays - $factory = new FormatterFactory($conf, $loginManager->isLoggedIn()); - $formatter = $factory->getFormatter('raw'); - $data = $formatter->format($bookmark); - $pluginManager->executeHooks('save_link', $data); - - $bookmark->fromArray($data); - $bookmarkService->set($bookmark); - - // If we are called from the bookmarklet, we must close the popup: - if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { - echo ''; - exit; - } - - $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?'; - $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); - // Scroll to the link which has been edited. - $location .= '#' . $bookmark->getShortUrl(); - // After saving the link, redirect to the page the user was on. - header('Location: '. $location); + // This route is no longer supported in legacy mode + header('Location: ./'); exit; } @@ -695,110 +646,13 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM // -------- User clicked the "EDIT" button on a link: Display link edit form. if (isset($_GET['edit_link'])) { $id = (int) escape($_GET['edit_link']); - try { - $link = $bookmarkService->get($id); // Read database - } catch (BookmarkNotFoundException $e) { - // Link not found in database. - header('Location: ?'); - exit; - } - - $factory = new FormatterFactory($conf, $loginManager->isLoggedIn()); - $formatter = $factory->getFormatter('raw'); - $formattedLink = $formatter->format($link); - $tags = $bookmarkService->bookmarksCountPerTag(); - if ($conf->get('formatter') === 'markdown') { - $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; - } - $data = array( - 'link' => $formattedLink, - 'link_is_new' => false, - 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), - 'tags' => $tags, - ); - $pluginManager->executeHooks('render_editlink', $data); - - foreach ($data as $key => $value) { - $PAGE->assign($key, $value); - } - - $PAGE->assign('pagetitle', t('Edit') .' '. t('Shaare') .' - '. $conf->get('general.title', 'Shaarli')); - $PAGE->renderPage('editlink'); + header('Location: ./shaare-' . $id); exit; } // -------- User want to post a new link: Display link edit form. if (isset($_GET['post'])) { - $url = cleanup_url($_GET['post']); - - $link_is_new = false; - // Check if URL is not already in database (in this case, we will edit the existing link) - $bookmark = $bookmarkService->findByUrl($url); - if (! $bookmark) { - $link_is_new = true; - // Get title if it was provided in URL (by the bookmarklet). - $title = empty($_GET['title']) ? '' : escape($_GET['title']); - // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] - $description = empty($_GET['description']) ? '' : escape($_GET['description']); - $tags = empty($_GET['tags']) ? '' : escape($_GET['tags']); - $private = !empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0; - - // If this is an HTTP(S) link, we try go get the page to extract - // the title (otherwise we will to straight to the edit form.) - if (empty($title) && strpos(get_url_scheme($url), 'http') !== false) { - $retrieveDescription = $conf->get('general.retrieve_description'); - // Short timeout to keep the application responsive - // The callback will fill $charset and $title with data from the downloaded page. - get_http_response( - $url, - $conf->get('general.download_timeout', 30), - $conf->get('general.download_max_size', 4194304), - get_curl_download_callback($charset, $title, $description, $tags, $retrieveDescription) - ); - if (! empty($title) && strtolower($charset) != 'utf-8') { - $title = mb_convert_encoding($title, 'utf-8', $charset); - } - } - - if ($url == '') { - $title = $conf->get('general.default_note_title', t('Note: ')); - } - $url = escape($url); - $title = escape($title); - - $link = [ - 'title' => $title, - 'url' => $url, - 'description' => $description, - 'tags' => $tags, - 'private' => $private, - ]; - } else { - $factory = new FormatterFactory($conf, $loginManager->isLoggedIn()); - $formatter = $factory->getFormatter('raw'); - $link = $formatter->format($bookmark); - } - - $tags = $bookmarkService->bookmarksCountPerTag(); - if ($conf->get('formatter') === 'markdown') { - $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; - } - $data = [ - 'link' => $link, - 'link_is_new' => $link_is_new, - 'http_referer' => (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']) : ''), - 'source' => (isset($_GET['source']) ? $_GET['source'] : ''), - 'tags' => $tags, - 'default_private_links' => $conf->get('privacy.default_private_links', false), - ]; - $pluginManager->executeHooks('render_editlink', $data); - - foreach ($data as $key => $value) { - $PAGE->assign($key, $value); - } - - $PAGE->assign('pagetitle', t('Shaare') .' - '. $conf->get('general.title', 'Shaarli')); - $PAGE->renderPage('editlink'); + header('Location: ./shaare?' . http_build_query($_GET)); exit; } @@ -1351,19 +1205,29 @@ $app->group('', function () { $this->post('/configure', '\Shaarli\Front\Controller\Admin\ConfigureController:save')->setName('saveConfigure'); $this->get('/manage-tags', '\Shaarli\Front\Controller\Admin\ManageTagController:index')->setName('manageTag'); $this->post('/manage-tags', '\Shaarli\Front\Controller\Admin\ManageTagController:save')->setName('saveManageTag'); + $this->get('/add-shaare', '\Shaarli\Front\Controller\Admin\PostBookmarkController:addShaare')->setName('addShaare'); + $this + ->get('/shaare', '\Shaarli\Front\Controller\Admin\PostBookmarkController:displayCreateForm') + ->setName('newShaare'); + $this + ->get('/shaare-{id}', '\Shaarli\Front\Controller\Admin\PostBookmarkController:displayEditForm') + ->setName('editShaare'); + $this + ->post('/shaare', '\Shaarli\Front\Controller\Admin\PostBookmarkController:save') + ->setName('saveShaare'); + $this + ->get('/delete-shaare', '\Shaarli\Front\Controller\Admin\PostBookmarkController:deleteBookmark') + ->setName('deleteShaare'); $this ->get('/links-per-page', '\Shaarli\Front\Controller\Admin\SessionFilterController:linksPerPage') - ->setName('filter-links-per-page') - ; + ->setName('filter-links-per-page'); $this ->get('/visibility/{visibility}', '\Shaarli\Front\Controller\Admin\SessionFilterController:visibility') - ->setName('visibility') - ; + ->setName('visibility'); $this ->get('/untagged-only', '\Shaarli\Front\Controller\Admin\SessionFilterController:untaggedOnly') - ->setName('untagged-only') - ; + ->setName('untagged-only'); })->add('\Shaarli\Front\ShaarliMiddleware'); $response = $app->run(true); -- cgit v1.2.3