2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Presenters;
|
|
|
|
use openvk\Web\Models\Entities\Video;
|
2023-06-15 12:36:36 +03:00
|
|
|
use openvk\Web\Models\Repositories\{Users, Videos, Blacklists};
|
2020-06-07 19:04:43 +03:00
|
|
|
use Nette\InvalidStateException as ISE;
|
|
|
|
|
|
|
|
final class VideosPresenter extends OpenVKPresenter
|
|
|
|
{
|
|
|
|
private $videos;
|
|
|
|
private $users;
|
2022-09-17 00:19:46 +03:00
|
|
|
protected $presenterName = "videos";
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
function __construct(Videos $videos, Users $users)
|
|
|
|
{
|
|
|
|
$this->videos = $videos;
|
|
|
|
$this->users = $users;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderList(int $id): void
|
|
|
|
{
|
|
|
|
$user = $this->users->get($id);
|
|
|
|
if(!$user) $this->notFound();
|
2021-12-14 16:00:12 +03:00
|
|
|
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL))
|
|
|
|
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$this->template->user = $user;
|
2020-06-24 18:25:43 +03:00
|
|
|
$this->template->videos = $this->videos->getByUser($user, (int) ($this->queryParam("p") ?? 1));
|
2020-06-07 19:04:43 +03:00
|
|
|
$this->template->count = $this->videos->getUserVideosCount($user);
|
|
|
|
$this->template->paginatorConf = (object) [
|
|
|
|
"count" => $this->template->count,
|
2020-06-24 18:25:43 +03:00
|
|
|
"page" => (int) ($this->queryParam("p") ?? 1),
|
2020-06-07 19:04:43 +03:00
|
|
|
"amount" => NULL,
|
2020-06-24 18:25:43 +03:00
|
|
|
"perPage" => 7,
|
2020-06-07 19:04:43 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderView(int $owner, int $vId): void
|
|
|
|
{
|
|
|
|
$user = $this->users->get($owner);
|
2023-12-02 20:21:16 +03:00
|
|
|
$video = $this->videos->getByOwnerAndVID($owner, $vId);
|
2023-06-15 12:36:36 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
if(!$user) $this->notFound();
|
2023-12-02 20:21:16 +03:00
|
|
|
if(!$video || $video->isDeleted()) $this->notFound();
|
2021-12-14 16:00:12 +03:00
|
|
|
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL))
|
|
|
|
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$this->template->user = $user;
|
|
|
|
$this->template->video = $this->videos->getByOwnerAndVID($owner, $vId);
|
|
|
|
$this->template->cCount = $this->template->video->getCommentsCount();
|
|
|
|
$this->template->cPage = (int) ($this->queryParam("p") ?? 1);
|
|
|
|
$this->template->comments = iterator_to_array($this->template->video->getComments($this->template->cPage));
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderUpload(): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2023-05-27 09:31:20 +03:00
|
|
|
|
|
|
|
if(OPENVK_ROOT_CONF['openvk']['preferences']['videos']['disableUploading'])
|
2023-09-17 16:22:59 +03:00
|
|
|
$this->flashFail("err", tr("error"), tr("video_uploads_disabled"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
if($_SERVER["REQUEST_METHOD"] === "POST") {
|
2024-11-17 13:03:49 +03:00
|
|
|
$is_ajax = (int)($this->postParam('ajax') ?? '0') == 1;
|
2020-06-11 12:50:23 +03:00
|
|
|
if(!empty($this->postParam("name"))) {
|
2020-06-07 19:04:43 +03:00
|
|
|
$video = new Video;
|
|
|
|
$video->setOwner($this->user->id);
|
2021-09-14 23:54:50 +03:00
|
|
|
$video->setName(ovk_proc_strtr($this->postParam("name"), 61));
|
|
|
|
$video->setDescription(ovk_proc_strtr($this->postParam("desc"), 300));
|
2020-06-07 19:04:43 +03:00
|
|
|
$video->setCreated(time());
|
|
|
|
|
|
|
|
try {
|
|
|
|
if(isset($_FILES["blob"]) && file_exists($_FILES["blob"]["tmp_name"]))
|
|
|
|
$video->setFile($_FILES["blob"]);
|
|
|
|
else if(!empty($this->postParam("link")))
|
|
|
|
$video->setLink($this->postParam("link"));
|
|
|
|
else
|
2024-11-17 13:03:49 +03:00
|
|
|
$this->flashFail("err", tr("no_video_error"), tr("no_video_description"), 10, $is_ajax);
|
2021-10-15 12:34:59 +03:00
|
|
|
} catch(\DomainException $ex) {
|
2024-11-17 13:03:49 +03:00
|
|
|
$this->flashFail("err", tr("error_video"), tr("file_corrupted"), 10, $is_ajax);
|
2020-06-07 19:04:43 +03:00
|
|
|
} catch(ISE $ex) {
|
2024-11-17 13:03:49 +03:00
|
|
|
$this->flashFail("err", tr("error_video"), tr("link_incorrect"), 10, $is_ajax);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2024-11-17 13:03:49 +03:00
|
|
|
if((int)($this->postParam("unlisted") ?? '0') == 1) {
|
|
|
|
$video->setUnlisted(true);
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
$video->save();
|
|
|
|
|
2024-11-17 13:03:49 +03:00
|
|
|
if($is_ajax) {
|
|
|
|
$object = $video->getApiStructure();
|
|
|
|
$this->returnJson([
|
|
|
|
'payload' => $object->video,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:52:36 +03:00
|
|
|
$this->redirect("/video" . $video->getPrettyId());
|
2020-06-11 12:50:23 +03:00
|
|
|
} else {
|
2024-11-17 13:03:49 +03:00
|
|
|
$this->flashFail("err", tr("error_video"), tr("no_name_error"), 10, $is_ajax);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderEdit(int $owner, int $vId): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$video = $this->videos->getByOwnerAndVID($owner, $vId);
|
|
|
|
if(!$video)
|
|
|
|
$this->notFound();
|
|
|
|
if(is_null($this->user) || $this->user->id !== $owner)
|
2023-11-15 11:41:18 +03:00
|
|
|
$this->flashFail("err", tr("access_denied_error"), tr("access_denied_error_description"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
if($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
|
|
$video->setName(empty($this->postParam("name")) ? NULL : $this->postParam("name"));
|
|
|
|
$video->setDescription(empty($this->postParam("desc")) ? NULL : $this->postParam("desc"));
|
|
|
|
$video->save();
|
|
|
|
|
2023-11-15 11:41:18 +03:00
|
|
|
$this->flash("succ", tr("changes_saved"), tr("changes_saved_video_comment"));
|
2022-08-09 08:52:36 +03:00
|
|
|
$this->redirect("/video" . $video->getPrettyId());
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->template->video = $video;
|
|
|
|
}
|
2020-06-17 21:58:25 +03:00
|
|
|
|
|
|
|
function renderRemove(int $owner, int $vid): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
2021-01-01 00:18:53 +03:00
|
|
|
$this->willExecuteWriteAction();
|
2020-06-17 21:58:25 +03:00
|
|
|
|
|
|
|
$video = $this->videos->getByOwnerAndVID($owner, $vid);
|
|
|
|
if(!$video)
|
|
|
|
$this->notFound();
|
|
|
|
$user = $this->user->id;
|
|
|
|
|
|
|
|
if(!is_null($user)) {
|
|
|
|
if($video->getOwnerVideo() == $user) {
|
|
|
|
$video->deleteVideo($owner, $vid);
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-15 11:41:18 +03:00
|
|
|
$this->flashFail("err", tr("cant_delete_video"), tr("cant_delete_video_comment"));
|
2020-06-17 21:58:25 +03:00
|
|
|
}
|
|
|
|
|
2022-08-09 08:52:36 +03:00
|
|
|
$this->redirect("/videos" . $owner);
|
2020-06-17 21:58:25 +03:00
|
|
|
}
|
2023-11-15 11:41:18 +03:00
|
|
|
|
|
|
|
function renderLike(int $owner, int $video_id): void
|
|
|
|
{
|
|
|
|
$this->assertUserLoggedIn();
|
|
|
|
$this->willExecuteWriteAction();
|
|
|
|
$this->assertNoCSRF();
|
|
|
|
|
|
|
|
$video = $this->videos->getByOwnerAndVID($owner, $video_id);
|
|
|
|
if(!$video || $video->isDeleted() || $video->getOwner()->isDeleted()) $this->notFound();
|
|
|
|
|
|
|
|
if(method_exists($video, "canBeViewedBy") && !$video->canBeViewedBy($this->user->identity)) {
|
|
|
|
$this->flashFail("err", tr("error"), tr("forbidden"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($this->user)) {
|
|
|
|
$video->toggleLike($this->user->identity);
|
|
|
|
}
|
2024-11-17 13:03:49 +03:00
|
|
|
|
|
|
|
if($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
|
|
$this->returnJson([
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->redirect("$_SERVER[HTTP_REFERER]");
|
2023-11-15 11:41:18 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|