From: ArthurHoaro Date: Thu, 3 Sep 2020 06:48:51 +0000 (+0200) Subject: Merge pull request #1523 from ArthurHoaro/fix/default-colors-generation X-Git-Tag: v0.12.0-beta-1~25 X-Git-Url: https://git.immae.eu/?p=github%2Fshaarli%2FShaarli.git;a=commitdiff_plain;h=865f0a0e014bb114cc2f528be10a023de4057ff4;hp=972daa4513420d3293d46a808ca2d5be0313e78b Merge pull request #1523 from ArthurHoaro/fix/default-colors-generation Default colors plugin: generate CSS file during initialization --- diff --git a/application/Languages.php b/application/Languages.php index 5cda802e..d83e0765 100644 --- a/application/Languages.php +++ b/application/Languages.php @@ -179,9 +179,10 @@ class Languages { return [ 'auto' => t('Automatic'), + 'de' => t('German'), 'en' => t('English'), 'fr' => t('French'), - 'de' => t('German'), + 'jp' => t('Japanese'), ]; } } diff --git a/application/bookmark/BookmarkArray.php b/application/bookmark/BookmarkArray.php index d87d43b4..3bd5eb20 100644 --- a/application/bookmark/BookmarkArray.php +++ b/application/bookmark/BookmarkArray.php @@ -234,16 +234,17 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * Also update the urls and ids mapping arrays. * - * @param string $order ASC|DESC + * @param string $order ASC|DESC + * @param bool $ignoreSticky If set to true, sticky bookmarks won't be first */ - public function reorder($order = 'DESC') + public function reorder(string $order = 'DESC', bool $ignoreSticky = false): void { $order = $order === 'ASC' ? -1 : 1; // Reorder array by dates. - usort($this->bookmarks, function ($a, $b) use ($order) { + usort($this->bookmarks, function ($a, $b) use ($order, $ignoreSticky) { /** @var $a Bookmark */ /** @var $b Bookmark */ - if ($a->isSticky() !== $b->isSticky()) { + if (false === $ignoreSticky && $a->isSticky() !== $b->isSticky()) { return $a->isSticky() ? -1 : 1; } return $a->getCreated() < $b->getCreated() ? 1 * $order : -1 * $order; diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index b3a90ed4..e3a61146 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php @@ -114,8 +114,13 @@ class BookmarkFileService implements BookmarkServiceInterface /** * @inheritDoc */ - public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false) - { + public function search( + $request = [], + $visibility = null, + $caseSensitive = false, + $untaggedOnly = false, + bool $ignoreSticky = false + ) { if ($visibility === null) { $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; } @@ -124,6 +129,10 @@ class BookmarkFileService implements BookmarkServiceInterface $searchtags = isset($request['searchtags']) ? $request['searchtags'] : ''; $searchterm = isset($request['searchterm']) ? $request['searchterm'] : ''; + if ($ignoreSticky) { + $this->bookmarks->reorder('DESC', true); + } + return $this->bookmarkFilter->filter( BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, [$searchtags, $searchterm], diff --git a/application/bookmark/BookmarkServiceInterface.php b/application/bookmark/BookmarkServiceInterface.php index ce8bd912..b9b483eb 100644 --- a/application/bookmark/BookmarkServiceInterface.php +++ b/application/bookmark/BookmarkServiceInterface.php @@ -49,10 +49,17 @@ interface BookmarkServiceInterface * @param string $visibility * @param bool $caseSensitive * @param bool $untaggedOnly + * @param bool $ignoreSticky * * @return Bookmark[] */ - public function search($request = [], $visibility = null, $caseSensitive = false, $untaggedOnly = false); + public function search( + $request = [], + $visibility = null, + $caseSensitive = false, + $untaggedOnly = false, + bool $ignoreSticky = false + ); /** * Get a single bookmark by its ID. diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index 269ad877..3653c32f 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -102,7 +102,7 @@ class FeedBuilder } // Optionally filter the results: - $linksToDisplay = $this->linkDB->search($userInput); + $linksToDisplay = $this->linkDB->search($userInput, null, false, false, true); $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput); diff --git a/application/front/ShaarliMiddleware.php b/application/front/ShaarliMiddleware.php index c015c0c6..d1aa1399 100644 --- a/application/front/ShaarliMiddleware.php +++ b/application/front/ShaarliMiddleware.php @@ -94,7 +94,7 @@ class ShaarliMiddleware && $this->container->conf->get('privacy.force_login') // and the current page isn't already the login page // and the user is not requesting a feed (which would lead to a different content-type as expected) - && !in_array($next->getName(), ['login', 'atom', 'rss'], true) + && !in_array($next->getName(), ['login', 'processLogin', 'atom', 'rss'], true) ) { throw new UnauthorizedException(); } diff --git a/application/legacy/LegacyController.php b/application/legacy/LegacyController.php index 26465d2c..e16dd0f4 100644 --- a/application/legacy/LegacyController.php +++ b/application/legacy/LegacyController.php @@ -40,28 +40,33 @@ class LegacyController extends ShaarliVisitorController public function post(Request $request, Response $response): Response { $parameters = count($request->getQueryParams()) > 0 ? '?' . http_build_query($request->getQueryParams()) : ''; + $route = '/admin/shaare'; if (!$this->container->loginManager->isLoggedIn()) { - return $this->redirect($response, '/login' . $parameters); + return $this->redirect($response, '/login?returnurl='. $this->getBasePath() . $route . $parameters); } - return $this->redirect($response, '/admin/shaare' . $parameters); + return $this->redirect($response, $route . $parameters); } /** Legacy route: ?addlink= */ protected function addlink(Request $request, Response $response): Response { + $route = '/admin/add-shaare'; + if (!$this->container->loginManager->isLoggedIn()) { - return $this->redirect($response, '/login'); + return $this->redirect($response, '/login?returnurl=' . $this->getBasePath() . $route); } - return $this->redirect($response, '/admin/add-shaare'); + return $this->redirect($response, $route); } /** Legacy route: ?do=login */ protected function login(Request $request, Response $response): Response { - return $this->redirect($response, '/login'); + $returnUrl = $request->getQueryParam('returnurl'); + + return $this->redirect($response, '/login' . ($returnUrl ? '?returnurl=' . $returnUrl : '')); } /** Legacy route: ?do=logout */ @@ -127,4 +132,21 @@ class LegacyController extends ShaarliVisitorController return $this->redirect($response, '/feed/' . $feedType . $parameters); } + + /** Legacy route: ?do=configure */ + protected function configure(Request $request, Response $response): Response + { + $route = '/admin/configure'; + + if (!$this->container->loginManager->isLoggedIn()) { + return $this->redirect($response, '/login?returnurl=' . $this->getBasePath() . $route); + } + + return $this->redirect($response, $route); + } + + protected function getBasePath(): string + { + return $this->container->basePath ?: ''; + } } diff --git a/application/render/PageBuilder.php b/application/render/PageBuilder.php index 7a716673..c52e3b76 100644 --- a/application/render/PageBuilder.php +++ b/application/render/PageBuilder.php @@ -149,6 +149,8 @@ class PageBuilder $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); + $this->tpl->assign('links_per_page', $this->session['LINKS_PER_PAGE']); + // To be removed with a proper theme configuration. $this->tpl->assign('conf', $this->conf); } diff --git a/application/security/SessionManager.php b/application/security/SessionManager.php index 76b0afe8..36df8c1c 100644 --- a/application/security/SessionManager.php +++ b/application/security/SessionManager.php @@ -183,7 +183,6 @@ class SessionManager unset($this->session['expires_on']); unset($this->session['username']); unset($this->session['visibility']); - unset($this->session['untaggedonly']); } } diff --git a/assets/default/js/base.js b/assets/default/js/base.js index 0f29799d..27938823 100644 --- a/assets/default/js/base.js +++ b/assets/default/js/base.js @@ -25,16 +25,18 @@ function findParent(element, tagName, attributes) { /** * Ajax request to refresh the CSRF token. */ -function refreshToken(basePath) { - console.log('refresh'); +function refreshToken(basePath, callback) { const xhr = new XMLHttpRequest(); xhr.open('GET', `${basePath}/admin/token`); xhr.onload = () => { const elements = document.querySelectorAll('input[name="token"]'); [...elements].forEach((element) => { - console.log(element); element.setAttribute('value', xhr.responseText); }); + + if (callback) { + callback(xhr.response); + } }; xhr.send(); } @@ -622,4 +624,15 @@ function init(description) { [...autocompleteFields].forEach((autocompleteField) => { awesomepletes.push(createAwesompleteInstance(autocompleteField)); }); + + const exportForm = document.querySelector('#exportform'); + if (exportForm != null) { + exportForm.addEventListener('submit', (event) => { + event.preventDefault(); + + refreshToken(basePath, () => { + event.target.submit(); + }); + }); + } })(); diff --git a/assets/default/scss/shaarli.scss b/assets/default/scss/shaarli.scss index 759dff29..7ab09d3f 100644 --- a/assets/default/scss/shaarli.scss +++ b/assets/default/scss/shaarli.scss @@ -616,6 +616,11 @@ body, padding: 5px; text-decoration: none; color: $dark-grey; + + &.selected { + background: var(--main-color); + color: $white; + } } input { diff --git a/inc/languages/ja/LC_MESSAGES/shaarli.po b/inc/languages/jp/LC_MESSAGES/shaarli.po similarity index 100% rename from inc/languages/ja/LC_MESSAGES/shaarli.po rename to inc/languages/jp/LC_MESSAGES/shaarli.po diff --git a/index.php b/index.php index e7471823..869f42de 100644 --- a/index.php +++ b/index.php @@ -95,7 +95,7 @@ $app->group('', function () { $this->get('/add-tag/{newTag}', '\Shaarli\Front\Controller\Visitor\TagController:addTag'); $this->get('/remove-tag/{tag}', '\Shaarli\Front\Controller\Visitor\TagController:removeTag'); $this->get('/links-per-page', '\Shaarli\Front\Controller\Visitor\PublicSessionFilterController:linksPerPage'); - $this->get('/untagged-only', '\Shaarli\Front\Controller\Admin\PublicSessionFilterController:untaggedOnly'); + $this->get('/untagged-only', '\Shaarli\Front\Controller\Visitor\PublicSessionFilterController:untaggedOnly'); })->add('\Shaarli\Front\ShaarliMiddleware'); $app->group('/admin', function () { diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php index 5c2aaedb..fe37d5f2 100644 --- a/tests/feed/FeedBuilderTest.php +++ b/tests/feed/FeedBuilderTest.php @@ -87,7 +87,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); // Test first not pinned link (note link) - $link = $data['links'][array_keys($data['links'])[2]]; + $link = $data['links'][array_keys($data['links'])[0]]; $this->assertEquals(41, $link['id']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); @@ -128,7 +128,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); - $link = $data['links'][array_keys($data['links'])[2]]; + $link = $data['links'][array_keys($data['links'])[0]]; $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); } @@ -173,7 +173,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $feedBuilder->setLocale(self::$LOCALE); $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); $this->assertEquals(3, count($data['links'])); - $link = $data['links'][array_keys($data['links'])[2]]; + $link = $data['links'][array_keys($data['links'])[0]]; $this->assertEquals(41, $link['id']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); } @@ -195,7 +195,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertTrue($data['usepermalinks']); // First link is a permalink - $link = $data['links'][array_keys($data['links'])[2]]; + $link = $data['links'][array_keys($data['links'])[0]]; $this->assertEquals(41, $link['id']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); @@ -203,7 +203,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $this->assertContains('Direct link', $link['description']); $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']); // Second link is a direct link - $link = $data['links'][array_keys($data['links'])[3]]; + $link = $data['links'][array_keys($data['links'])[1]]; $this->assertEquals(8, $link['id']); $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']); @@ -270,7 +270,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase ); // Test first link (note link) - $link = $data['links'][array_keys($data['links'])[2]]; + $link = $data['links'][array_keys($data['links'])[0]]; $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']); $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']); $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']); diff --git a/tests/front/controller/admin/ConfigureControllerTest.php b/tests/front/controller/admin/ConfigureControllerTest.php index f2f84bac..612f20f1 100644 --- a/tests/front/controller/admin/ConfigureControllerTest.php +++ b/tests/front/controller/admin/ConfigureControllerTest.php @@ -62,7 +62,7 @@ class ConfigureControllerTest extends TestCase static::assertSame('privacy.hide_public_links', $assignedVariables['hide_public_links']); static::assertSame('api.enabled', $assignedVariables['api_enabled']); static::assertSame('api.secret', $assignedVariables['api_secret']); - static::assertCount(4, $assignedVariables['languages']); + static::assertCount(5, $assignedVariables['languages']); static::assertArrayHasKey('gd_enabled', $assignedVariables); static::assertSame('thumbnails.mode', $assignedVariables['thumbnails_mode']); } diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index 759a5b2a..4e52f3e1 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -66,11 +66,11 @@ class LegacyControllerTest extends TestCase { return [ ['post', [], '/admin/shaare', true], - ['post', [], '/login', false], + ['post', [], '/login?returnurl=/subfolder/admin/shaare', false], ['post', ['title' => 'test'], '/admin/shaare?title=test', true], - ['post', ['title' => 'test'], '/login?title=test', false], + ['post', ['title' => 'test'], '/login?returnurl=/subfolder/admin/shaare?title=test', false], ['addlink', [], '/admin/add-shaare', true], - ['addlink', [], '/login', false], + ['addlink', [], '/login?returnurl=/subfolder/admin/add-shaare', false], ['login', [], '/login', true], ['login', [], '/login', false], ['logout', [], '/admin/logout', true], @@ -94,6 +94,8 @@ class LegacyControllerTest extends TestCase ['opensearch', [], '/open-search', true], ['dailyrss', [], '/daily-rss', false], ['dailyrss', [], '/daily-rss', true], + ['configure', [], '/login?returnurl=/subfolder/admin/configure', false], + ['configure', [], '/admin/configure', true], ]; } } diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index 60695dcf..11a59f9c 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php @@ -207,15 +207,16 @@ class SessionManagerTest extends TestCase 'expires_on' => time() + 1000, 'username' => 'johndoe', 'visibility' => 'public', - 'untaggedonly' => false, + 'untaggedonly' => true, ]; $this->sessionManager->logout(); - $this->assertFalse(isset($this->session['ip'])); - $this->assertFalse(isset($this->session['expires_on'])); - $this->assertFalse(isset($this->session['username'])); - $this->assertFalse(isset($this->session['visibility'])); - $this->assertFalse(isset($this->session['untaggedonly'])); + $this->assertArrayNotHasKey('ip', $this->session); + $this->assertArrayNotHasKey('expires_on', $this->session); + $this->assertArrayNotHasKey('username', $this->session); + $this->assertArrayNotHasKey('visibility', $this->session); + $this->assertArrayHasKey('untaggedonly', $this->session); + $this->assertTrue($this->session['untaggedonly']); } /** diff --git a/tpl/default/linklist.paging.html b/tpl/default/linklist.paging.html index 7b320eaf..aa637868 100644 --- a/tpl/default/linklist.paging.html +++ b/tpl/default/linklist.paging.html @@ -1,27 +1,29 @@