mirror of
https://github.com/wallabag/wallabag.git
synced 2025-06-05 23:18:47 +00:00
Add Wallabag\CoreBundle\Helper\UrlHasher
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
This commit is contained in:
parent
d5744bf0df
commit
4a5516376b
5 changed files with 79 additions and 21 deletions
|
@ -14,6 +14,7 @@ use Wallabag\CoreBundle\Entity\Entry;
|
||||||
use Wallabag\CoreBundle\Entity\Tag;
|
use Wallabag\CoreBundle\Entity\Tag;
|
||||||
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
|
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
|
||||||
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
||||||
|
use Wallabag\CoreBundle\Helper\UrlHasher;
|
||||||
|
|
||||||
class EntryRestController extends WallabagRestController
|
class EntryRestController extends WallabagRestController
|
||||||
{
|
{
|
||||||
|
@ -56,8 +57,8 @@ class EntryRestController extends WallabagRestController
|
||||||
}
|
}
|
||||||
|
|
||||||
$urlHashMap = [];
|
$urlHashMap = [];
|
||||||
foreach($urls as $urlToHash) {
|
foreach ($urls as $urlToHash) {
|
||||||
$urlHash = hash('sha1', $urlToHash); // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls
|
$urlHash = UrlHasher::hashUrl($urlToHash);
|
||||||
$hashedUrls[] = $urlHash;
|
$hashedUrls[] = $urlHash;
|
||||||
$urlHashMap[$urlHash] = $urlToHash;
|
$urlHashMap[$urlHash] = $urlToHash;
|
||||||
}
|
}
|
||||||
|
@ -77,25 +78,11 @@ class EntryRestController extends WallabagRestController
|
||||||
|
|
||||||
if (!empty($url) || !empty($hashedUrl)) {
|
if (!empty($url) || !empty($hashedUrl)) {
|
||||||
$hu = array_keys($results)[0];
|
$hu = array_keys($results)[0];
|
||||||
|
|
||||||
return $this->sendResponse(['exists' => $results[$hu]]);
|
return $this->sendResponse(['exists' => $results[$hu]]);
|
||||||
}
|
}
|
||||||
return $this->sendResponse($results);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $this->sendResponse($results);
|
||||||
* Replace the hashedUrl keys in $results with the unhashed URL from the
|
|
||||||
* request, as recorded in $urlHashMap.
|
|
||||||
*/
|
|
||||||
private function replaceUrlHashes(array $results, array $urlHashMap) {
|
|
||||||
$newResults = [];
|
|
||||||
foreach($results as $hash => $res) {
|
|
||||||
if (isset($urlHashMap[$hash])) {
|
|
||||||
$newResults[$urlHashMap[$hash]] = $res;
|
|
||||||
} else {
|
|
||||||
$newResults[$hash] = $res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $newResults;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -815,6 +802,24 @@ class EntryRestController extends WallabagRestController
|
||||||
return $this->sendResponse($results);
|
return $this->sendResponse($results);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the hashedUrl keys in $results with the unhashed URL from the
|
||||||
|
* request, as recorded in $urlHashMap.
|
||||||
|
*/
|
||||||
|
private function replaceUrlHashes(array $results, array $urlHashMap)
|
||||||
|
{
|
||||||
|
$newResults = [];
|
||||||
|
foreach ($results as $hash => $res) {
|
||||||
|
if (isset($urlHashMap[$hash])) {
|
||||||
|
$newResults[$urlHashMap[$hash]] = $res;
|
||||||
|
} else {
|
||||||
|
$newResults[$hash] = $res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $newResults;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve value from the request.
|
* Retrieve value from the request.
|
||||||
* Used for POST & PATCH on a an entry.
|
* Used for POST & PATCH on a an entry.
|
||||||
|
|
|
@ -7,6 +7,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Wallabag\CoreBundle\Helper\UrlHasher;
|
||||||
use Wallabag\UserBundle\Entity\User;
|
use Wallabag\UserBundle\Entity\User;
|
||||||
|
|
||||||
class GenerateUrlHashesCommand extends ContainerAwareCommand
|
class GenerateUrlHashesCommand extends ContainerAwareCommand
|
||||||
|
@ -65,7 +66,9 @@ class GenerateUrlHashesCommand extends ContainerAwareCommand
|
||||||
|
|
||||||
$i = 1;
|
$i = 1;
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
$entry->setHashedUrl(hash('sha1', $entry->getUrl()));
|
$entry->setHashedUrl(
|
||||||
|
UrlHasher::hashUrl($entry->getUrl())
|
||||||
|
);
|
||||||
$em->persist($entry);
|
$em->persist($entry);
|
||||||
|
|
||||||
if (0 === ($i % 20)) {
|
if (0 === ($i % 20)) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ use JMS\Serializer\Annotation\XmlRoot;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
use Wallabag\AnnotationBundle\Entity\Annotation;
|
use Wallabag\AnnotationBundle\Entity\Annotation;
|
||||||
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
|
use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
|
||||||
|
use Wallabag\CoreBundle\Helper\UrlHasher;
|
||||||
use Wallabag\UserBundle\Entity\User;
|
use Wallabag\UserBundle\Entity\User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -324,7 +325,7 @@ class Entry
|
||||||
public function setUrl($url)
|
public function setUrl($url)
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
$this->hashedUrl = hash('sha1', $url);
|
$this->hashedUrl = UrlHasher::hashUrl($url);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
22
src/Wallabag/CoreBundle/Helper/UrlHasher.php
Normal file
22
src/Wallabag/CoreBundle/Helper/UrlHasher.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Helper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hash URLs for privacy and performance.
|
||||||
|
*/
|
||||||
|
class UrlHasher
|
||||||
|
{
|
||||||
|
/** @var string */
|
||||||
|
const ALGORITHM = 'sha1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
*
|
||||||
|
* @return string hashed $url
|
||||||
|
*/
|
||||||
|
public static function hashUrl(string $url)
|
||||||
|
{
|
||||||
|
return hash(static::ALGORITHM, $url);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@ use Pagerfanta\Adapter\DoctrineORMAdapter;
|
||||||
use Pagerfanta\Pagerfanta;
|
use Pagerfanta\Pagerfanta;
|
||||||
use Wallabag\CoreBundle\Entity\Entry;
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
use Wallabag\CoreBundle\Entity\Tag;
|
use Wallabag\CoreBundle\Entity\Tag;
|
||||||
|
use Wallabag\CoreBundle\Helper\UrlHasher;
|
||||||
|
|
||||||
class EntryRepository extends EntityRepository
|
class EntryRepository extends EntityRepository
|
||||||
{
|
{
|
||||||
|
@ -349,7 +350,7 @@ class EntryRepository extends EntityRepository
|
||||||
public function findByUrlAndUserId($url, $userId)
|
public function findByUrlAndUserId($url, $userId)
|
||||||
{
|
{
|
||||||
return $this->findByHashedUrlAndUserId(
|
return $this->findByHashedUrlAndUserId(
|
||||||
hash('sha1', $url), // XXX: the hash logic would better be in a separate util to avoid duplication with GenerateUrlHashesCommand::generateHashedUrls
|
UrlHasher::hashUrl($url),
|
||||||
$userId);
|
$userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,6 +507,32 @@ class EntryRepository extends EntityRepository
|
||||||
return $this->find($randomId);
|
return $this->find($randomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inject a UrlHasher.
|
||||||
|
*
|
||||||
|
* @param UrlHasher $hasher
|
||||||
|
*/
|
||||||
|
public function setUrlHasher(UrlHasher $hasher)
|
||||||
|
{
|
||||||
|
$this->urlHasher = $hasher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the UrlHasher, or create a default one if not injected.
|
||||||
|
*
|
||||||
|
* XXX: the default uses the default hash algorithm
|
||||||
|
*
|
||||||
|
* @return UrlHasher
|
||||||
|
*/
|
||||||
|
protected function getUrlHasher()
|
||||||
|
{
|
||||||
|
if (!isset($this->urlHasher)) {
|
||||||
|
$this->setUrlHasher(new UrlHasher());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->urlHasher;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a query builder to be used by other getBuilderFor* method.
|
* Return a query builder to be used by other getBuilderFor* method.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue