aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php41
-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
5 files changed, 79 insertions, 21 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 17b53a01..77eb489e 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -14,6 +14,7 @@ use Wallabag\CoreBundle\Entity\Entry;
14use Wallabag\CoreBundle\Entity\Tag; 14use Wallabag\CoreBundle\Entity\Tag;
15use Wallabag\CoreBundle\Event\EntryDeletedEvent; 15use Wallabag\CoreBundle\Event\EntryDeletedEvent;
16use Wallabag\CoreBundle\Event\EntrySavedEvent; 16use Wallabag\CoreBundle\Event\EntrySavedEvent;
17use Wallabag\CoreBundle\Helper\UrlHasher;
17 18
18class EntryRestController extends WallabagRestController 19class EntryRestController extends WallabagRestController
19{ 20{
@@ -56,8 +57,8 @@ class EntryRestController extends WallabagRestController
56 } 57 }
57 58
58 $urlHashMap = []; 59 $urlHashMap = [];
59 foreach($urls as $urlToHash) { 60 foreach ($urls as $urlToHash) {
60 $urlHash = hash('sha1', $urlToHash); // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls 61 $urlHash = UrlHasher::hashUrl($urlToHash);
61 $hashedUrls[] = $urlHash; 62 $hashedUrls[] = $urlHash;
62 $urlHashMap[$urlHash] = $urlToHash; 63 $urlHashMap[$urlHash] = $urlToHash;
63 } 64 }
@@ -77,25 +78,11 @@ class EntryRestController extends WallabagRestController
77 78
78 if (!empty($url) || !empty($hashedUrl)) { 79 if (!empty($url) || !empty($hashedUrl)) {
79 $hu = array_keys($results)[0]; 80 $hu = array_keys($results)[0];
81
80 return $this->sendResponse(['exists' => $results[$hu]]); 82 return $this->sendResponse(['exists' => $results[$hu]]);
81 } 83 }
82 return $this->sendResponse($results);
83 }
84 84
85 /** 85 return $this->sendResponse($results);
86 * Replace the hashedUrl keys in $results with the unhashed URL from the
87 * request, as recorded in $urlHashMap.
88 */
89 private function replaceUrlHashes(array $results, array $urlHashMap) {
90 $newResults = [];
91 foreach($results as $hash => $res) {
92 if (isset($urlHashMap[$hash])) {
93 $newResults[$urlHashMap[$hash]] = $res;
94 } else {
95 $newResults[$hash] = $res;
96 }
97 }
98 return $newResults;
99 } 86 }
100 87
101 /** 88 /**
@@ -816,6 +803,24 @@ class EntryRestController extends WallabagRestController
816 } 803 }
817 804
818 /** 805 /**
806 * Replace the hashedUrl keys in $results with the unhashed URL from the
807 * request, as recorded in $urlHashMap.
808 */
809 private function replaceUrlHashes(array $results, array $urlHashMap)
810 {
811 $newResults = [];
812 foreach ($results as $hash => $res) {
813 if (isset($urlHashMap[$hash])) {
814 $newResults[$urlHashMap[$hash]] = $res;
815 } else {
816 $newResults[$hash] = $res;
817 }
818 }
819
820 return $newResults;
821 }
822
823 /**
819 * Retrieve value from the request. 824 * Retrieve value from the request.
820 * Used for POST & PATCH on a an entry. 825 * Used for POST & PATCH on a an entry.
821 * 826 *
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