diff options
Diffstat (limited to 'src/Wallabag/CoreBundle')
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; | |||
7 | use Symfony\Component\Console\Input\InputArgument; | 7 | use Symfony\Component\Console\Input\InputArgument; |
8 | use Symfony\Component\Console\Input\InputInterface; | 8 | use Symfony\Component\Console\Input\InputInterface; |
9 | use Symfony\Component\Console\Output\OutputInterface; | 9 | use Symfony\Component\Console\Output\OutputInterface; |
10 | use Wallabag\CoreBundle\Helper\UrlHasher; | ||
10 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
11 | 12 | ||
12 | class GenerateUrlHashesCommand extends ContainerAwareCommand | 13 | class 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; | |||
13 | use Symfony\Component\Validator\Constraints as Assert; | 13 | use Symfony\Component\Validator\Constraints as Assert; |
14 | use Wallabag\AnnotationBundle\Entity\Annotation; | 14 | use Wallabag\AnnotationBundle\Entity\Annotation; |
15 | use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; | 15 | use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; |
16 | use Wallabag\CoreBundle\Helper\UrlHasher; | ||
16 | use Wallabag\UserBundle\Entity\User; | 17 | use 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 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | /** | ||
6 | * Hash URLs for privacy and performance. | ||
7 | */ | ||
8 | class 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; | |||
9 | use Pagerfanta\Pagerfanta; | 9 | use Pagerfanta\Pagerfanta; |
10 | use Wallabag\CoreBundle\Entity\Entry; | 10 | use Wallabag\CoreBundle\Entity\Entry; |
11 | use Wallabag\CoreBundle\Entity\Tag; | 11 | use Wallabag\CoreBundle\Entity\Tag; |
12 | use Wallabag\CoreBundle\Helper\UrlHasher; | ||
12 | 13 | ||
13 | class EntryRepository extends EntityRepository | 14 | class 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 | /** |