aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2019-05-28 14:18:33 +0200
committerGitHub <noreply@github.com>2019-05-28 14:18:33 +0200
commit2cbee36a0184786644470a84fdd8c720cfcac58e (patch)
tree33c6040c050f85c537f8dbf5e91d8c281db092cd /src/Wallabag/CoreBundle
parent48d136d3a08d7f4ca8e0d434d8104c746d31957d (diff)
parent629a3797bcef33943df8ef5631328e05d12634ed (diff)
downloadwallabag-2cbee36a0184786644470a84fdd8c720cfcac58e.tar.gz
wallabag-2cbee36a0184786644470a84fdd8c720cfcac58e.tar.zst
wallabag-2cbee36a0184786644470a84fdd8c720cfcac58e.zip
Merge pull request #3944 from shtrom/always-hash-exists-url
Always hash exists url
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php5
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php3
-rw-r--r--src/Wallabag/CoreBundle/Helper/UrlHasher.php23
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php16
4 files changed, 33 insertions, 14 deletions
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
index 45bd8c5f..8f2bff11 100644
--- a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
+++ b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
@@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument; 7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface; 8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface; 9use Symfony\Component\Console\Output\OutputInterface;
10use Wallabag\CoreBundle\Helper\UrlHasher;
10use Wallabag\UserBundle\Entity\User; 11use Wallabag\UserBundle\Entity\User;
11 12
12class GenerateUrlHashesCommand extends ContainerAwareCommand 13class GenerateUrlHashesCommand extends ContainerAwareCommand
@@ -65,7 +66,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
65 66
66 $i = 1; 67 $i = 1;
67 foreach ($entries as $entry) { 68 foreach ($entries as $entry) {
68 $entry->setHashedUrl(hash('sha1', $entry->getUrl())); 69 $entry->setHashedUrl(UrlHasher::hashUrl($entry->getUrl()));
69 $em->persist($entry); 70 $em->persist($entry);
70 71
71 if (0 === ($i % 20)) { 72 if (0 === ($i % 20)) {
@@ -84,7 +85,7 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
84 * 85 *
85 * @param string $username 86 * @param string $username
86 * 87 *
87 * @return \Wallabag\UserBundle\Entity\User 88 * @return User
88 */ 89 */
89 private function getUser($username) 90 private function getUser($username)
90 { 91 {
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index c3fb87d2..1b4367fd 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -13,6 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot;
13use Symfony\Component\Validator\Constraints as Assert; 13use Symfony\Component\Validator\Constraints as Assert;
14use Wallabag\AnnotationBundle\Entity\Annotation; 14use Wallabag\AnnotationBundle\Entity\Annotation;
15use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; 15use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
16use Wallabag\CoreBundle\Helper\UrlHasher;
16use Wallabag\UserBundle\Entity\User; 17use Wallabag\UserBundle\Entity\User;
17 18
18/** 19/**
@@ -324,7 +325,7 @@ class Entry
324 public function setUrl($url) 325 public function setUrl($url)
325 { 326 {
326 $this->url = $url; 327 $this->url = $url;
327 $this->hashedUrl = hash('sha1', $url); 328 $this->hashedUrl = UrlHasher::hashUrl($url);
328 329
329 return $this; 330 return $this;
330 } 331 }
diff --git a/src/Wallabag/CoreBundle/Helper/UrlHasher.php b/src/Wallabag/CoreBundle/Helper/UrlHasher.php
new file mode 100644
index 00000000..d123eaba
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/UrlHasher.php
@@ -0,0 +1,23 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5/**
6 * Hash URLs for privacy and performance.
7 */
8class UrlHasher
9{
10 /**
11 * Hash the given url using the given algorithm.
12 * Hashed url are faster to be retrieved in the database than the real url.
13 *
14 * @param string $url
15 * @param string $algorithm
16 *
17 * @return string
18 */
19 public static function hashUrl(string $url, $algorithm = 'sha1')
20 {
21 return hash($algorithm, urldecode($url));
22 }
23}
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 3990932e..880e7c65 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -9,6 +9,7 @@ use Pagerfanta\Adapter\DoctrineORMAdapter;
9use Pagerfanta\Pagerfanta; 9use Pagerfanta\Pagerfanta;
10use Wallabag\CoreBundle\Entity\Entry; 10use Wallabag\CoreBundle\Entity\Entry;
11use Wallabag\CoreBundle\Entity\Tag; 11use Wallabag\CoreBundle\Entity\Tag;
12use Wallabag\CoreBundle\Helper\UrlHasher;
12 13
13class EntryRepository extends EntityRepository 14class EntryRepository extends EntityRepository
14{ 15{
@@ -348,17 +349,10 @@ class EntryRepository extends EntityRepository
348 */ 349 */
349 public function findByUrlAndUserId($url, $userId) 350 public function findByUrlAndUserId($url, $userId)
350 { 351 {
351 $res = $this->createQueryBuilder('e') 352 return $this->findByHashedUrlAndUserId(
352 ->where('e.url = :url')->setParameter('url', urldecode($url)) 353 UrlHasher::hashUrl($url),
353 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) 354 $userId
354 ->getQuery() 355 );
355 ->getResult();
356
357 if (\count($res)) {
358 return current($res);
359 }
360
361 return false;
362 } 356 }
363 357
364 /** 358 /**