Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions VKAPI/Handlers/Notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use openvk\Web\Models\Repositories\Comments as CommentsRepo;
use openvk\Web\Models\Repositories\Photos as PhotosRepo;
use openvk\Web\Models\Repositories\Videos as VideosRepo;
use openvk\Web\Models\Entities\{Note, Comment};
use openvk\Web\Models\Entities\{User, Note, Comment};

final class Notes extends VKAPIRequestHandler
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public function createComment(int $note_id, int $owner_id, string $message, stri
$this->fail(15, "Access denied");
}

if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
if ($note->getOwner() instanceof User && !$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(15, "Access denied");
}

Expand Down Expand Up @@ -92,7 +92,9 @@ public function edit(string $note_id, string $title = "", string $text = "", int
$this->fail(15, "Access denied");
}

if (!$note->canBeModifiedBy($this->getUser())) {
if ($note->getOwner() instanceof User && !$note->canBeModifiedBy($this->getUser())) {
$this->fail(15, "Access denied");
} elseif (!$note->getOwner()->canBeModifiedBy($this->getUser()) || $note->getOwner()->isWikiPagesDisabledEnforced()) {
$this->fail(15, "Access denied");
}

Expand Down Expand Up @@ -174,7 +176,8 @@ public function getById(int $note_id, int $owner_id, bool $need_wiki = false)
$this->fail(15, "Access denied");
}

if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {

if ($note->getOwner() instanceof User && !$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(15, "Access denied");
}

Expand Down Expand Up @@ -203,7 +206,7 @@ public function getComments(int $note_id, int $owner_id, int $sort = 1, int $off
$this->fail(15, "Access denied");
}

if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
if ($note->getOwner() instanceof User && !$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(15, "Access denied");
}

Expand Down
36 changes: 35 additions & 1 deletion Web/Models/Entities/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{User, Manager};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Managers, Posts};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Managers, Notes, Posts};
use Nette\Database\Table\{ActiveRow, GroupedSelection};
use Chandler\Database\DatabaseConnection as DB;
use Chandler\Security\User as ChandlerUser;
Expand Down Expand Up @@ -538,4 +538,38 @@ public function toVkApiStruct(?User $user = null, string $fields = ''): object

return $res;
}

public function canCreateNote(?User $user): bool
{
return $this->canBeModifiedBy($user);
}

public function getMainNoteId(): ?int
{
if ($this->isWikiPagesDisabledEnforced()) {
return null;
}

return $this->getRecord()->main_note_id;
}

public function getMainNote(): ?Note
{
return (new Notes())->get($this->getMainNoteId() ?? 0);
}

public function isMainNoteExpanded(): bool
{
return (bool) $this->getRecord()->is_main_note_expanded;
}

public function isMainNoteExpandedEnforced(): bool
{
return (bool) $this->getRecord()->enforce_main_note_expanded;
}

public function isWikiPagesDisabledEnforced(): bool
{
return (bool) $this->getRecord()->enforce_wiki_pages_disabled;
}
}
30 changes: 28 additions & 2 deletions Web/Models/Entities/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use HTMLPurifier_Config;
use HTMLPurifier;
use HTMLPurifier_Filter;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\Repositories\Clubs;

class SecurityFilter extends HTMLPurifier_Filter
{
Expand Down Expand Up @@ -160,11 +164,19 @@ public function getSource(): string

public function canBeViewedBy(?User $user = null): bool
{
if ($this->isDeleted() || $this->getOwner()->isDeleted()) {
if ($this->isDeleted()) {
return false;
}

return $this->getOwner()->getPrivacyPermission('notes.read', $user) && $this->getOwner()->canBeViewedBy($user);
if ($this->getOwner() instanceof User) {
if ($this->getOwner()->isDeleted()) {
return false;
}

return $this->getOwner()->getPrivacyPermission('notes.read', $user) && $this->getOwner()->canBeViewedBy($user);
}

return $this->getOwner()->canBeViewedBy($user);
}

public function toVkApiStruct(): object
Expand All @@ -181,4 +193,18 @@ public function toVkApiStruct(): object

return $res;
}

public function getOwner(bool $real = false): RowModel
{
$oid = (int) $this->getRecord()->owner;
if (!$real && $this->isAnonymous()) {
$oid = (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"];
}

if ($oid > 0) {
return (new Users())->get($oid);
} else {
return (new Clubs())->get($oid * -1);
}
}
}
21 changes: 21 additions & 0 deletions Web/Models/Repositories/Notes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\Note;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\Club;
use Nette\Database\Table\ActiveRow;

class Notes
Expand Down Expand Up @@ -38,6 +39,21 @@ public function getUserNotes(User $user, int $page = 1, ?int $perPage = null, st
}
}

public function getClubNotes(Club $club, int $page = 1, ?int $perPage = null, string $sort = "DESC"): \Traversable
{
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
foreach ($this->notes->where("owner", $club->getId() * -1)->where("deleted", 0)->order("created $sort")->page($page, $perPage) as $album) {
yield new Note($album);
}
}

public function getAllClubNotes(Club $club, string $sort = "DESC"): \Traversable
{
foreach ($this->notes->where("owner", $club->getId() * -1)->where("deleted", 0)->order("created $sort") as $album) {
yield new Note($album);
}
}

public function getNoteById(int $owner, int $note): ?Note
{
$note = $this->notes->where(['owner' => $owner, 'virtual_id' => $note])->fetch();
Expand All @@ -52,4 +68,9 @@ public function getUserNotesCount(User $user): int
{
return sizeof($this->notes->where("owner", $user->getId())->where("deleted", 0));
}

public function getClubNotesCount(Club $club): int
{
return $this->notes->where("owner", $club->getId() * -1)->where("deleted", 0)->count('*');
}
}
2 changes: 2 additions & 0 deletions Web/Presenters/AdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ public function renderClub(int $id): void
$club->setVerified(empty($this->postParam("verify") ? 0 : 1));
$club->setHide_From_Global_Feed(empty($this->postParam("hide_from_global_feed") ? 0 : 1));
$club->setEnforce_Hiding_From_Global_Feed(empty($this->postParam("enforce_hiding_from_global_feed") ? 0 : 1));
$club->setEnforce_Main_Note_Expanded(empty($this->postParam("enforce_main_note_expanded") ? 0 : 1));
$club->setEnforce_Wiki_Pages_Disabled(empty($this->postParam("enforce_wiki_pages_disabled") ? 0 : 1));
$club->save();
break;
case "ban":
Expand Down
6 changes: 5 additions & 1 deletion Web/Presenters/CommentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace openvk\Web\Presenters;

use openvk\Web\Models\Entities\{Comment, Notifications\MentionNotification, Photo, Video, User, Topic, Post};
use openvk\Web\Models\Entities\{Comment, Club, Notifications\MentionNotification, Note, Photo, Video, User, Topic, Post};
use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\{Comments, Clubs, Videos, Photos, Audios};
use Nette\InvalidStateException as ISE;
Expand Down Expand Up @@ -84,6 +84,10 @@ public function renderMakeComment(string $repo, int $eId): void
$this->flashFail("err", tr("error"), tr("forbidden"));
}

if ($entity instanceof Note && $entity->getOwner() instanceof Club) {
$this->flashFail("err", tr("error"), tr("forbidden"));
}

$flags = 0;
if ($this->postParam("as_group") === "on" && !is_null($club) && $club->canBeModifiedBy($this->user->identity)) {
$flags |= 0b10000000;
Expand Down
44 changes: 43 additions & 1 deletion Web/Presenters/GroupPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use openvk\Web\Models\Entities\{Club, Photo, Post};
use Nette\InvalidStateException;
use openvk\Web\Models\Entities\Notifications\ClubModeratorNotification;
use openvk\Web\Models\Repositories\{Clubs, Users, Albums, Managers, Topics, Audios, Posts, Documents};
use openvk\Web\Models\Repositories\{Clubs, Users, Albums, Managers, Notes, Topics, Audios, Posts, Documents};
use Chandler\Security\Authenticator;
use Nette\InvalidStateException as ISE;

Expand Down Expand Up @@ -571,4 +571,46 @@ public function renderSuggested(int $id): void

$this->template->page = (int) ($this->queryParam("p") ?? 1);
}

public function renderNotes(int $id): void
{
$this->assertUserLoggedIn();

$club = $this->clubs->get($id);
if (!$club || $club->isBanned() || !$club->canBeModifiedBy($this->user->identity)) {
$this->notFound();
}

if ($club->isWikiPagesDisabledEnforced()) {
$this->flashFail("err", tr("forbidden"), tr("wiki_pages_banned_by_instance_admins"));
}

$this->template->club = $club;

if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (!$club->isMainNoteExpandedEnforced()) {
$club->setIs_Main_Note_Expanded((bool) $this->postParam("expanded"));
}

$nid = (int) $this->postParam("nid");
if ($nid === 0) {
$club->setMain_Note_Id(null);
} else {
$note = (new Notes())->get($nid);

if (!$note || $note->isDeleted() || !($note->getOwner() instanceof Club && $note->getOwner()->getId() === $club->getId())) {
$this->flashFail("err", tr("error"), tr("forbidden"));
}

$club->setMain_Note_Id($nid);
}

$club->save();

$this->flash("succ", tr("changes_saved"), tr("new_changes_desc"));
}

$this->template->notes = iterator_to_array((new Notes())->getAllClubNotes($club));
$this->template->count = (new Notes())->getClubNotesCount($club);
}
}
Loading
Loading