openvk/Web/Models/Repositories/Photos.php
mrilyew a5d80b8f9b
fix(thumbnails): use hash instead of real id (#1234)
Заменяется реальный id в ссылке превью на хэш, чтобы нельзя было делать
перебор или просмотр рандомных картинок. В данный момент не особо
критично (потому что можно перебирать посты), но при появлении вложений
в диалогах будет

Co-authored-by: Alexander Minkin <weryskok@gmail.com>
2025-05-18 17:53:39 +03:00

85 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
use openvk\Web\Models\Entities\{Photo, User};
use Chandler\Database\DatabaseConnection;
class Photos
{
private $context;
private $photos;
public function __construct()
{
$this->context = DatabaseConnection::i()->getContext();
$this->photos = $this->context->table("photos");
}
public function get(int $id): ?Photo
{
$photo = $this->photos->get($id);
if (!$photo) {
return null;
}
return new Photo($photo);
}
public function getByHash(string $hash): ?Photo
{
$photo = $this->photos->where("hash", $hash)->fetch();
if (!$photo) {
return null;
}
return new Photo($photo);
}
public function getByOwnerAndVID(int $owner, int $vId): ?Photo
{
$photo = $this->photos->where([
"owner" => $owner,
"virtual_id" => $vId,
"system" => 0,
"private" => 0,
])->fetch();
if (!$photo) {
return null;
}
return new Photo($photo);
}
public function getEveryUserPhoto(User $user, int $offset = 0, int $limit = 10): \Traversable
{
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
$photos = $this->photos->where([
"owner" => $user->getId(),
"deleted" => 0,
"system" => 0,
"private" => 0,
"anonymous" => 0,
])->order("id DESC");
foreach ($photos->limit($limit, $offset) as $photo) {
yield new Photo($photo);
}
}
public function getUserPhotosCount(User $user)
{
$photos = $this->photos->where([
"owner" => $user->getId(),
"deleted" => 0,
"system" => 0,
"private" => 0,
"anonymous" => 0,
]);
return sizeof($photos);
}
}