aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
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.php22
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php29
4 files changed, 56 insertions, 3 deletions
diff --git a/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php b/src/Wallabag/CoreBundle/Command/GenerateUrlHashesCommand.php
index 45bd8c5f..775b0413 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,9 @@ 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(
70 UrlHasher::hashUrl($entry->getUrl())
71 );
69 $em->persist($entry); 72 $em->persist($entry);
70 73
71 if (0 === ($i % 20)) { 74 if (0 === ($i % 20)) {
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..e44f219a
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/UrlHasher.php
@@ -0,0 +1,22 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5/**
6 * Hash URLs for privacy and performance.
7 */
8class UrlHasher
9{
10 /** @var string */
11 const ALGORITHM = 'sha1';
12
13 /**
14 * @param string $url
15 *
16 * @return string hashed $url
17 */
18 public static function hashUrl(string $url)
19 {
20 return hash(static::ALGORITHM, $url);
21 }
22}
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 960b682d..37fc1000 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{
@@ -349,7 +350,7 @@ class EntryRepository extends EntityRepository
349 public function findByUrlAndUserId($url, $userId) 350 public function findByUrlAndUserId($url, $userId)
350 { 351 {
351 return $this->findByHashedUrlAndUserId( 352 return $this->findByHashedUrlAndUserId(
352 hash('sha1', $url), // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls 353 UrlHasher::hashUrl($url),
353 $userId); 354 $userId);
354 } 355 }
355 356
@@ -507,6 +508,32 @@ class EntryRepository extends EntityRepository
507 } 508 }
508 509
509 /** 510 /**
511 * Inject a UrlHasher.
512 *
513 * @param UrlHasher $hasher
514 */
515 public function setUrlHasher(UrlHasher $hasher)
516 {
517 $this->urlHasher = $hasher;
518 }
519
520 /**
521 * Get the UrlHasher, or create a default one if not injected.
522 *
523 * XXX: the default uses the default hash algorithm
524 *
525 * @return UrlHasher
526 */
527 protected function getUrlHasher()
528 {
529 if (!isset($this->urlHasher)) {
530 $this->setUrlHasher(new UrlHasher());
531 }
532
533 return $this->urlHasher;
534 }
535
536 /**
510 * Return a query builder to be used by other getBuilderFor* method. 537 * Return a query builder to be used by other getBuilderFor* method.
511 * 538 *
512 * @param int $userId 539 * @param int $userId