Merge branch 'master' into banned-links-fixed

This commit is contained in:
mrilyew 2025-02-09 16:54:03 +03:00 committed by GitHub
commit 2e8cb81970
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
483 changed files with 47733 additions and 11470 deletions

2
.github/FUNDING.yml vendored
View file

@ -1 +1 @@
custom: "https://openvk.su/donate" custom: "https://ovk.to/donate"

View file

@ -2,11 +2,12 @@ name: Build base images
on: on:
schedule: schedule:
- cron: '0 0 * * *' - cron: "0 0 * * *"
workflow_dispatch:
env: env:
BASE_IMAGE_NAME: php BASE_IMAGE_NAME: php
BASE_IMAGE_VERSION: "8.1" BASE_IMAGE_VERSION: "8.2"
jobs: jobs:
build-cli: build-cli:
@ -16,23 +17,29 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: with:
lfs: false lfs: false
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v2 uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx - name: Set up Docker Buildx
id: buildx id: buildx
uses: docker/setup-buildx-action@v2 uses: docker/setup-buildx-action@v2
- name: Change repository string to lowercase
id: repositorystring
uses: Entepotenz/change-string-case-action-min-dependencies@v1.1.0
with:
string: ${{ github.repository }}
- name: Log into registry - name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build cli image - name: Build cli image
run: | run: |
IMAGE_NAME=ghcr.io/${{ github.repository }}/$BASE_IMAGE_NAME:$BASE_IMAGE_VERSION-cli IMAGE_NAME=ghcr.io/${{ steps.repositorystring.outputs.lowercase }}/$BASE_IMAGE_NAME:$BASE_IMAGE_VERSION-cli
docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/base-php-cli.Dockerfile --build-arg VERSION=$BASE_IMAGE_VERSION docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/base-php-cli.Dockerfile --build-arg VERSION=$BASE_IMAGE_VERSION
build-apache: build-apache:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -40,19 +47,25 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: with:
lfs: false lfs: false
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v2 uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx - name: Set up Docker Buildx
id: buildx id: buildx
uses: docker/setup-buildx-action@v2 uses: docker/setup-buildx-action@v2
- name: Change repository string to lowercase
id: repositorystring
uses: Entepotenz/change-string-case-action-min-dependencies@v1.1.0
with:
string: ${{ github.repository }}
- name: Log into registry - name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build apache image - name: Build apache image
run: | run: |
IMAGE_NAME=ghcr.io/${{ github.repository }}/$BASE_IMAGE_NAME:$BASE_IMAGE_VERSION-apache IMAGE_NAME=ghcr.io/${{ steps.repositorystring.outputs.lowercase }}/$BASE_IMAGE_NAME:$BASE_IMAGE_VERSION-apache
docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/base-php-apache.Dockerfile --build-arg VERSION=$BASE_IMAGE_VERSION docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/base-php-apache.Dockerfile --build-arg VERSION=$BASE_IMAGE_VERSION

View file

@ -1,64 +1,154 @@
name: Build images name: Build images
on: on: [push, pull_request]
push:
# Publish `master` as Docker `latest` image.
branches:
- master
# Publish `v1.2.3` tags as releases.
tags:
- v*
env: env:
BASE_IMAGE_NAME: openvk BASE_IMAGE_NAME: openvk
DB_IMAGE_NAME: mariadb DB_IMAGE_NAME: mariadb
EVENT_IMAGE_NAME: mariadb EVENT_IMAGE_NAME: mariadb
DB_VERSION: "10.9" DB_VERSION: '10.9'
jobs: jobs:
build: buildbase:
runs-on: ubuntu-latest name: Build base images
strategy:
matrix:
arch: ['x86_64']
if: github.event_name == 'push' runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
lfs: false
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Log into registry # 'push' runs on inner branches, 'pull_request' will run only on outer PRs
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin if: >
github.event_name == 'push'
|| (github.event_name == 'pull_request'
&& github.event.pull_request.head.repo.full_name != github.repository)
- name: Build base image steps:
run: | - name: Set up QEMU
IMAGE_ID=ghcr.io/${{ github.repository }}/$BASE_IMAGE_NAME uses: docker/setup-qemu-action@v3
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
[ "$VERSION" == "master" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_ID:$VERSION . --push -f install/automated/docker/openvk.Dockerfile --build-arg GITREPO=${{ github.repository }} - name: Set up Docker Buildx
id: buildx
- name: Build MariaDB primary image uses: docker/setup-buildx-action@v3
run: | with:
IMAGE_NAME=ghcr.io/${{ github.repository }}/$DB_IMAGE_NAME:$DB_VERSION-primary platforms: linux/amd64,linux/arm64
docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/mariadb-primary.Dockerfile --build-arg VERSION=$DB_VERSION - name: Change repository string to lowercase
id: repositorystring
- name: Build MariaDB event image uses: Entepotenz/change-string-case-action-min-dependencies@v1.1.0
run: | with:
IMAGE_NAME=ghcr.io/${{ github.repository }}/$EVENT_IMAGE_NAME:$DB_VERSION-eventdb string: ${{ github.repository }}
docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_NAME . --push -f install/automated/docker/mariadb-eventdb.Dockerfile --build-arg VERSION=$DB_VERSION - name: Base image meta
id: basemeta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repositorystring.outputs.lowercase }}/${{env.BASE_IMAGE_NAME}}
labels: |
org.opencontainers.image.documentation=https://github.com/OpenVK/openvk/blob/master/install/automated/docker/Readme.md
tags: |
type=sha
type=ref,event=branch
type=ref,event=pr
type=ref,event=tag
type=raw,value=latest,enable={{is_default_branch}}
- name: Log into registry
if: github.event_name != 'pull_request'
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build base image
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
file: install/automated/docker/openvk.Dockerfile
tags: ${{ steps.basemeta.outputs.tags }}
labels: ${{ steps.basemeta.outputs.labels }}
push: ${{ github.event_name != 'pull_request' }}
build-args: |
GITREPO=${{ steps.repositorystring.outputs.lowercase }}
builddb:
name: Build DB images
runs-on: ubuntu-latest
# 'push' runs on inner branches, 'pull_request' will run only on outer PRs
if: >
github.event_name == 'push'
|| (github.event_name == 'pull_request'
&& github.event.pull_request.head.repo.full_name != github.repository)
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/amd64,linux/arm64
- name: Change repository string to lowercase
id: repositorystring
uses: Entepotenz/change-string-case-action-min-dependencies@v1.1.0
with:
string: ${{ github.repository }}
- name: MariaDB primary meta
id: db-primarymeta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repositorystring.outputs.lowercase }}/${{env.DB_IMAGE_NAME}}
labels: |
org.opencontainers.image.title=OpenVK MariaDB (Primary)
org.opencontainers.image.description=OpenVK's image for MariaDB for primary database.
org.opencontainers.image.documentation=https://github.com/OpenVK/openvk/blob/master/install/automated/docker/Readme.md
tags: |
type=sha,prefix=${{env.DB_VERSION}}-primary-sha-
type=ref,event=branch,prefix=${{env.DB_VERSION}}-primary-
type=ref,event=pr,prefix=${{env.DB_VERSION}}-primary-pr-
type=ref,event=tag,prefix=${{env.DB_VERSION}}-primary-
type=raw,value=${{env.DB_VERSION}}-primary,enable={{is_default_branch}}
- name: MariaDB event meta
id: db-eventmeta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ steps.repositorystring.outputs.lowercase }}/${{env.DB_IMAGE_NAME}}
labels: |
org.opencontainers.image.title=OpenVK MariaDB (EventDB)
org.opencontainers.image.description=OpenVK's image for MariaDB for event database.
org.opencontainers.image.documentation=https://github.com/OpenVK/openvk/blob/master/install/automated/docker/Readme.md
tags: |
type=sha,prefix=${{env.DB_VERSION}}-eventdb-sha-
type=ref,event=branch,prefix=${{env.DB_VERSION}}-eventdb-
type=ref,event=pr,prefix=${{env.DB_VERSION}}-eventdb-pr-
type=ref,event=tag,prefix=${{env.DB_VERSION}}-eventdb-
type=raw,value=${{env.DB_VERSION}}-eventdb,enable={{is_default_branch}}
- name: Log into registry
if: github.event_name != 'pull_request'
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Build MariaDB primary image
uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64
file: install/automated/docker/mariadb-primary.Dockerfile
tags: ${{ steps.db-primarymeta.outputs.tags }}
labels: ${{ steps.db-primarymeta.outputs.labels }}
build-args: |
VERSION=${{env.DB_VERSION}}
- name: Build MariaDB event image
uses: docker/build-push-action@v6
with:
push: ${{ github.event_name != 'pull_request' }}
platforms: linux/amd64,linux/arm64
file: install/automated/docker/mariadb-eventdb.Dockerfile
tags: ${{ steps.db-eventmeta.outputs.tags }}
labels: ${{ steps.db-eventmeta.outputs.labels }}
build-args: |
VERSION=${{env.DB_VERSION}}

35
.github/workflows/lint.yaml vendored Normal file
View file

@ -0,0 +1,35 @@
name: Lint
on:
push:
pull_request:
jobs:
lint:
runs-on: ubuntu-20.04
# 'push' runs on inner branches, 'pull_request' will run only on outer PRs
if: >
github.event_name == 'push'
|| (github.event_name == 'pull_request'
&& github.event.pull_request.head.repo.full_name != github.repository)
permissions:
contents: read
steps:
- name: Code Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
extensions: gd, zip, intl, yaml, pdo_mysql, rdkafka, imagick
tools: composer:v2
coverage: none
- name: Install dependencies
run: composer install --no-interaction --no-progress --no-suggest --prefer-dist
- name: PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff

3
.gitignore vendored
View file

@ -15,4 +15,5 @@ themepacks/*
storage/* storage/*
!storage/.gitkeep !storage/.gitkeep
.idea .idea
.php-cs-fixer.cache

15
.php-cs-fixer.dist.php Normal file
View file

@ -0,0 +1,15 @@
<?php
$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->name('openvkctl')
;
return (new PhpCsFixer\Config())
->setRules([
'@PER-CS2.0' => true,
'@PHP82Migration' => true,
])
->setFinder($finder)
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
;

View file

@ -1,103 +1,107 @@
<?php declare(strict_types=1); <?php
namespace openvk\CLI;
use Chandler\Database\DatabaseConnection; declare(strict_types=1);
use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\Entities\Notifications\CoinsTransferNotification; namespace openvk\CLI;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Chandler\Database\DatabaseConnection;
use Symfony\Component\Console\Input\InputOption; use openvk\Web\Models\Repositories\Users;
use Symfony\Component\Console\Output\OutputInterface; use openvk\Web\Models\Entities\Notifications\CoinsTransferNotification;
use Nette\Utils\ImageException; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
define("NANOTON", 1000000000); use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class FetchToncoinTransactions extends Command use Nette\Utils\ImageException;
{
private $images; define("NANOTON", 1000000000);
protected static $defaultName = "fetch-ton"; class FetchToncoinTransactions extends Command
{
function __construct() private $images;
{
$this->transactions = DatabaseConnection::i()->getContext()->table("cryptotransactions"); protected static $defaultName = "fetch-ton";
parent::__construct(); public function __construct()
} {
$this->transactions = DatabaseConnection::i()->getContext()->table("cryptotransactions");
protected function configure(): void
{ parent::__construct();
$this->setDescription("Fetches TON transactions to top up the users' balance") }
->setHelp("This command checks for new transactions on TON Wallet and then top up the balance of specified users");
} protected function configure(): void
{
protected function execute(InputInterface $input, OutputInterface $output): int $this->setDescription("Fetches TON transactions to top up the users' balance")
{ ->setHelp("This command checks for new transactions on TON Wallet and then top up the balance of specified users");
$header = $output->section(); }
$header->writeln([ protected function execute(InputInterface $input, OutputInterface $output): int
"TONCOIN Fetcher", {
"=====================", $header = $output->section();
"",
]); $header->writeln([
"TONCOIN Fetcher",
if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["enabled"]) { "=====================",
$header->writeln("Sorry, but you handn't enabled the TON support in your config file yet."); "",
]);
return Command::FAILURE;
} if (!OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["enabled"]) {
$header->writeln("Sorry, but you handn't enabled the TON support in your config file yet.");
$testnetSubdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : "";
$url = "https://" . $testnetSubdomain . "toncenter.com/api/v2/getTransactions?"; return Command::FAILURE;
}
$opts = [
"http" => [ $testnetSubdomain = OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["testnet"] ? "testnet." : "";
"method" => "GET", $url = "https://" . $testnetSubdomain . "toncenter.com/api/v2/getTransactions?";
"header" => "Accept: application/json"
] $opts = [
]; "http" => [
"method" => "GET",
$selection = $this->transactions->select('hash, lt')->order("id DESC")->limit(1)->fetch(); "header" => "Accept: application/json",
$trHash = $selection->hash ?? NULL; ],
$trLt = $selection->lt ?? NULL; ];
$data = http_build_query([ $selection = $this->transactions->select('hash, lt')->order("id DESC")->limit(1)->fetch();
"address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"], $trHash = $selection->hash ?? null;
"limit" => 100, $trLt = $selection->lt ?? null;
"hash" => $trHash,
"to_lt" => $trLt $data = http_build_query([
]); "address" => OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["address"],
"limit" => 100,
$response = file_get_contents($url . $data, false, stream_context_create($opts)); "hash" => $trHash,
$response = json_decode($response, true); "to_lt" => $trLt,
]);
$header->writeln("Gonna up the balance of users");
foreach($response["result"] as $transfer) { $response = file_get_contents($url . $data, false, stream_context_create($opts));
$outputArray; $response = json_decode($response, true);
preg_match('/' . OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["regex"] . '/', $transfer["in_msg"]["message"], $outputArray);
$userId = ctype_digit($outputArray[1]) ? intval($outputArray[1]) : NULL; $header->writeln("Gonna up the balance of users");
if(is_null($userId)) { foreach ($response["result"] as $transfer) {
$header->writeln("Well, that's a donation. Thanks! XD"); $outputArray;
} else { preg_match('/' . OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["regex"] . '/', $transfer["in_msg"]["message"], $outputArray);
$user = (new Users)->get($userId); $userId = ctype_digit($outputArray[1]) ? intval($outputArray[1]) : null;
if(!$user) { if (is_null($userId)) {
$header->writeln("Well, that's a donation. Thanks! XD"); $header->writeln("Well, that's a donation. Thanks! XD");
} else { } else {
$value = ($transfer["in_msg"]["value"] / NANOTON) / OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["rate"]; $user = (new Users())->get($userId);
$user->setCoins($user->getCoins() + $value); if (!$user) {
$user->save(); $header->writeln("Well, that's a donation. Thanks! XD");
(new CoinsTransferNotification($user, (new Users)->get(OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"]), (int) $value, "Via TON cryptocurrency"))->emit(); } else {
$header->writeln($value . " coins are added to " . $user->getId() . " user id"); $value = ($transfer["in_msg"]["value"] / NANOTON) / OPENVK_ROOT_CONF["openvk"]["preferences"]["ton"]["rate"];
$this->transactions->insert([ $user->setCoins($user->getCoins() + $value);
"id" => NULL, $user->save();
"hash" => $transfer["transaction_id"]["hash"], (new CoinsTransferNotification($user, (new Users())->get(OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"]), (int) $value, "Via TON cryptocurrency"))->emit();
"lt" => $transfer["transaction_id"]["lt"] $header->writeln($value . " coins are added to " . $user->getId() . " user id");
]); $this->transactions->insert([
} "id" => null,
} "hash" => $transfer["transaction_id"]["hash"],
} "lt" => $transfer["transaction_id"]["lt"],
]);
$header->writeln("Processing finished :3"); }
}
return Command::SUCCESS; }
}
} $header->writeln("Processing finished :3");
return Command::SUCCESS;
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\CLI; namespace openvk\CLI;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Photos; use openvk\Web\Models\Repositories\Photos;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -14,7 +18,7 @@ class RebuildImagesCommand extends Command
protected static $defaultName = "build-images"; protected static $defaultName = "build-images";
function __construct() public function __construct()
{ {
$this->images = DatabaseConnection::i()->getContext()->table("photos"); $this->images = DatabaseConnection::i()->getContext()->table("photos");
@ -40,8 +44,9 @@ class RebuildImagesCommand extends Command
]); ]);
$filter = ["deleted" => false]; $filter = ["deleted" => false];
if($input->getOption("upgrade-only")) if ($input->getOption("upgrade-only")) {
$filter["sizes"] = NULL; $filter["sizes"] = null;
}
$selection = $this->images->select("id")->where($filter); $selection = $this->images->select("id")->where($filter);
$totalPics = $selection->count(); $totalPics = $selection->count();
@ -52,24 +57,25 @@ class RebuildImagesCommand extends Command
$errors = 0; $errors = 0;
$count = 0; $count = 0;
$avgTime = NULL; $avgTime = null;
$begin = new \DateTimeImmutable("now"); $begin = new \DateTimeImmutable("now");
foreach($selection as $idHolder) { foreach ($selection as $idHolder) {
$start = microtime(true); $start = microtime(true);
try { try {
$photo = (new Photos)->get($idHolder->id); $photo = (new Photos())->get($idHolder->id);
$photo->getSizes(true, true); $photo->getSizes(true, true);
$photo->getDimensions(); $photo->getDimensions();
} catch(ImageException $ex) { } catch (ImageException $ex) {
$errors++; $errors++;
} }
$timeConsumed = microtime(true) - $start; $timeConsumed = microtime(true) - $start;
if(!$avgTime) if (!$avgTime) {
$avgTime = $timeConsumed; $avgTime = $timeConsumed;
else } else {
$avgTime = ($avgTime + $timeConsumed) / 2; $avgTime = ($avgTime + $timeConsumed) / 2;
}
$eta = $begin->getTimestamp() + ceil($totalPics * $avgTime); $eta = $begin->getTimestamp() + ceil($totalPics * $avgTime);
$int = (new \DateTimeImmutable("now"))->diff(new \DateTimeImmutable("@$eta")); $int = (new \DateTimeImmutable("now"))->diff(new \DateTimeImmutable("@$eta"));
@ -83,4 +89,4 @@ class RebuildImagesCommand extends Command
return Command::SUCCESS; return Command::SUCCESS;
} }
} }

View file

@ -1,277 +0,0 @@
# Names
## Namespace Names
Namespaces should be written in PascalCase.
## File Names
Code directories should have their name written in PascalCase. Code files should contain only one class and have the name of that class.
In case of multiple class definitions in one file, it's name should be the same as the "primary" class name.
Non-code directories, non-class and non-code files should be named in lisp-case.
## Variable Names
Variable names should be written in camelCase. This also applies to function arguments, class instance names and methods.
## Constant Names
Constants are written in SCREAMING_SNAKE_CASE, but should be declared case-insensetive.
## Class Names
Classes in OpenVK should belong to `openvk\` namespace and be in the corresponding directory (according to PSR-4). Names of classes should be written in PascalCase.
## Function Names
camelCase and snake_case are allowed, but first one is the recommended way. This rule does not apply to class methods, which are written in camelCase only.
---
# Coding Rules
## File header
All OpenVK files must start with `<?php` open-tag followed by `declare(strict_types=1);` on the same line. The next line must contain namespace.
The lines after must contain use-declarations, each on it's own line (usage of {} operator is OK), if there is any. Then there must be an empty line. Example:
```php
<?php declare(strict_types=1);
namespace openvk;
use Chandler\Database\DatabaseConnection;
use Nette\Utils\{Image, FileSystem};
class ...
```
## NULL
Null should be written as constant, all-uppercase: `NULL`.
## Nullable (optional) pointer arguments
Optional pointer arguments should default to `nullptr`: `function something(&int? $myPointer = nullptr)`. `nullptr` must be written in lisp-case (lowercase).
## Comparing to NULL
In OpenVK `is_null` is the preferred way to check for equality to NULL. `??` must be used in assignments and where else possible.
In case if value can either be NULL or truthy, "boolean not" should be used to check if value is not null: `if(!$var)`.
## Arrays
Arrays must be defined with modern syntax: `[1, 2, 3]` (NOT `array(1, 2, 3)`).
Same applies to `list` construction: use `[$a, , $b] = $arr;` instead of `list($a, , $b) = $arr;`
## Casts
Typecasts must be done with modern syntax where possible: `(type) $var`. Int-to-string, string-to-int, etc conversions should also be dont with modern casting
syntax where possible, but should use `ctype_` functions where needed. `gettype`, `settype` should be used in dynamic programming only.
## Goto
```goto``` should be avoided.
## `continue n; `
It is preferable to use `continue n`, `break n` instead of guarding flags:
```php
# COOL AND GOOD
foreach($a as $b)
foreach($b as $c)
if($b == $c)
break 2;
# BRUH
foreach($a as $b) {
$shouldBreak = false;
foreach($b as $c)
if($b == $c)
$shouldBreak = true;
if($shouldBreak)
break;
}
```
## Comments
In OpenVK we use Perl-style `#` for single-line comments.
---
# Formatting
## Variables
It is preferable to declare only one variable per line in the code.
## Indentation
All things in OpenVK must be properly indented by a sequence of 4 spaces. Not tabs. \
When there are several variable declarations listed together, line up the variables:
```php
# OK
$photos = (new Photos)->where("meow", true);
$photo = $photos->fetch();
$arr = [
"a" => 10,
"bb" => true,
];
# NOT OK
$photos = (new Photos)->where("meow", true);
$photo = $photos->fetch();
$arr = [
"a" => 10,
"bb" => true,
];
```
## Tab/Space
+ **Do not use tabs**. Use spaces, as tabs are defined differently for different editors and printers.
+ Put one space after a comma and semicolons: `exp(1, 2)` `for($i = 1; $i < 100; $i++)`
+ Put one space around assignment operators: `$a = 1`
+ Always put a space around conditional operators: `$a = ($a > $b) ? $a : $b`
+ Do not put spaces between unary operators and their operands, primary operators and keywords:
```php
# OK
-$a;
$a++;
$b[1] = $a;
fun($b);
if($a) { ... }
# NOT OK
- $a;
$a ++;
$b [1] = $a;
fun ($b);
if ($a) { ... }
```
## Blank Lines
+ Use blank lines to create paragraphs in the code or comments to make the code more understandable
+ Use blank lines before `return` statement if it isn't the only statement in the block
+ Use blank lines after shorthand if/else/etc
```php
# OK
if($a)
return $x;
doSomething();
return "yay";
# NOT OK
if($a) return $x; # return must be on separate line
doSomething(); # doSomething must be separated by an extra blank line after short if/else
return "yay"; # do use blank lines before return statement
```
## Method/Function Arguments
+ When all arguments for a function do not fit on one line, try to line up the first argument in each line:
![image](https://user-images.githubusercontent.com/34442450/167248563-21fb01be-181d-48b9-ac0c-dc953c0a12cf.png)
+ If the argument lists are still too long to fit on the line, you may line up the arguments with the method name instead.
## Maximum characters per line
Lines should be no more than 80 characters long.
## Usage of curly braces
+ Curly braces should be on separate line for class, method, and function definitions.
+ In loops, if/else, try/catch, switch constructions the opening brace should be on the same line as the operator.
+ Braces must be ommited if the block contains only one statement **AND** the related blocks are also single statemented.
+ Nested single-statement+operator blocks must not be surrounded by braces.
```php
# OK
class A
{
function doSomethingFunny(): int
{
return 2;
}
}
if(true) {
doSomething();
doSomethingElse();
} else {
doSomethingFunny();
}
if($a)
return false;
else
doSomething();
foreach($b as $c => $d)
if($c == $d)
unset($b[$c]);
# NOT OK
class A {
function doSomethingFunny(): int {
return 2;
}
}
if(true) {
doSomething();
doSomethingElse();
} else
doSomethingFunny(); # why?
if($a) {
return false;
} else {
doSomething();
}
foreach($b as $c => $d) {
if($c == $d)
unset($b[$c]);
}
# lmao
if($a) { doSomething(); } else doSomethingElse();
```
## if/else, try/catch
+ Operators must not be indented with space from their operands but must have 1-space margin from braces:
```php
# OK
if($a) {
doSomething();
doSomethingElse();
} else if($b) {
try {
nukeSaintPetersburg('😈');
} finally {
return PEACE;
}
}
# NOT OK
if ($a) { # do not add space between control flow operator IF and it's operand
doSomething();
doSomethingElse();
}elseif($b){ # do add margin from braces; also ELSE and IF should be separate here
try{
nukeSaintPetersburg('😈');
}finally{
return PEACE;
}
}
```
## Switches
+ `break` must be on same indentation level as the code of le case (not the case definiton itself)
+ If there is no need to `break` a comment `# NOTICE falling through` must be places instead
```php
# OK
switch($a) {
case 1:
echo $a;
break;
case 2:
echo $a++;
# NOTICE falling through
default:
echo "c";
}
# NOT OK
switch($a) {
case 1:
echo $a;
break;
case 2:
echo $a++;
default:
echo "c";
}
```

148
DBEntity.updated.php Normal file
View file

@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace Chandler\Database;
use Chandler\Database\DatabaseConnection;
use Nette\Database\Table\Selection;
use Nette\Database\Table\ActiveRow;
use Nette\InvalidStateException as ISE;
use openvk\Web\Models\Repositories\CurrentUser;
use openvk\Web\Models\Repositories\Logs;
abstract class DBEntity
{
use \Nette\SmartObject;
protected $record;
protected $changes;
protected $deleted;
protected $user;
protected $tableName;
public function __construct(?ActiveRow $row = null)
{
if (is_null($row)) {
return;
}
$_table = $row->getTable()->getName();
if ($_table !== $this->tableName) {
throw new ISE("Invalid data supplied for model: table $_table is not compatible with table" . $this->tableName);
}
$this->record = $row;
}
public function __call(string $fName, array $args)
{
if (substr($fName, 0, 3) === "set") {
$field = mb_strtolower(substr($fName, 3));
$this->stateChanges($field, $args[0]);
} else {
throw new \Error("Call to undefined method " . get_class($this) . "::$fName");
}
}
private function getTable(): Selection
{
return DatabaseConnection::i()->getContext()->table($this->tableName);
}
protected function getRecord(): ?ActiveRow
{
return $this->record;
}
protected function stateChanges(string $column, $value): void
{
if (!is_null($this->record)) {
$t = $this->record->{$column};
} #Test if column exists
$this->changes[$column] = $value;
}
public function getId()
{
return $this->getRecord()->id;
}
public function isDeleted(): bool
{
return (bool) $this->getRecord()->deleted;
}
public function unwrap(): object
{
return (object) $this->getRecord()->toArray();
}
public function delete(bool $softly = true): void
{
$user = CurrentUser::i()->getUser();
$user_id = is_null($user) ? (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getId();
if (is_null($this->record)) {
throw new ISE("Can't delete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
}
(new Logs())->create($user_id, $this->getTable()->getName(), get_class($this), 2, $this->record->toArray(), $this->changes);
if ($softly) {
$this->record = $this->getTable()->where("id", $this->record->id)->update(["deleted" => true]);
} else {
$this->record->delete();
$this->deleted = true;
}
}
public function undelete(): void
{
if (is_null($this->record)) {
throw new ISE("Can't undelete a model, that hasn't been flushed to DB. Have you forgotten to call save() first?");
}
$user = CurrentUser::i()->getUser();
$user_id = is_null($user) ? (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getId();
(new Logs())->create($user_id, $this->getTable()->getName(), get_class($this), 3, $this->record->toArray(), ["deleted" => false]);
$this->getTable()->where("id", $this->record->id)->update(["deleted" => false]);
}
public function save(?bool $log = true): void
{
if ($log) {
$user = CurrentUser::i();
$user_id = is_null($user) ? (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["support"]["adminAccount"] : $user->getUser()->getId();
}
if (is_null($this->record)) {
$this->record = $this->getTable()->insert($this->changes);
if ($log && $this->getTable()->getName() !== "logs") {
(new Logs())->create($user_id, $this->getTable()->getName(), get_class($this), 0, $this->record->toArray(), $this->changes);
}
} else {
if ($log && $this->getTable()->getName() !== "logs") {
(new Logs())->create($user_id, $this->getTable()->getName(), get_class($this), 1, $this->record->toArray(), $this->changes);
}
if ($this->deleted) {
$this->record = $this->getTable()->insert((array) $this->record);
} else {
$this->getTable()->get($this->record->id)->update($this->changes);
$this->record = $this->getTable()->get($this->record->id);
}
}
$this->changes = [];
}
public function getTableName(): string
{
return $this->getTable()->getName();
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.7 KiB

View file

@ -46,17 +46,14 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="row"> <table class="row">
<tr> <tr>
<th class="small-12 large-12 columns first last"> <th class="small-12 large-12 columns first last">
<table class="row"> <table class="row">
<tr> <tr>
<td> <td>
<center>
<img src="pictures/lock.jpeg" align="center" class="float-center" width=128 height=128 />
</center>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -64,9 +61,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -74,11 +71,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Здравствуйте, {$name}! Вы вероятно изменили свой адрес электронной почты в OpenVK. Чтобы изменение вступило в силу, необходимо подтвердить ваш новый Email. Здравствуйте, {$name}! Вы вероятно изменили свой адрес электронной почты в OpenVK. Чтобы изменение вступило в силу, необходимо подтвердить ваш новый Email.
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -86,7 +83,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="button large expand success"> <table class="button large expand success">
<tr> <tr>
<td> <td>
@ -94,7 +91,7 @@
<tr> <tr>
<td> <td>
<center> <center>
<a href="http://{$_SERVER['HTTP_HOST']}/settings/change_email?key={rawurlencode($key)}" align="center" class="float-center">Подтвердить Email!</a> <a href="https://{$_SERVER['HTTP_HOST']}/settings/change_email?key={rawurlencode($key)}" align="center" class="float-center">Подтвердить Email!</a>
</center> </center>
</td> </td>
</tr> </tr>
@ -102,7 +99,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -110,30 +107,30 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя: Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя:
</p> </p>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner primary"> <th class="callout-inner primary">
<a href="http://{$_SERVER['HTTP_HOST']}/settings/change_email?key={$key}" style="color: #000; text-decoration: none;"> <a href="https://{$_SERVER['HTTP_HOST']}/settings/change_email?key={$key}" style="color: #000; text-decoration: none;">
http://{$_SERVER['HTTP_HOST']}/settings/change_email?key={$key} https://{$_SERVER['HTTP_HOST']}/settings/change_email?key={$key}
</a> </a>
</th> </th>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Обратите внимание на то, что эту ссылку нельзя: Обратите внимание на то, что эту ссылку нельзя:
</p> </p>
<ul> <ul>
<li>Передавать другим людям (даже друзьям, питомцам, соседам, любимым девушкам)</li> <li>Передавать другим людям (даже друзьям, питомцам, соседям, любимым девушкам)</li>
<li>Использовать, если прошло более двух дней с её генерации</li> <li>Использовать, если прошло более двух дней с её генерации</li>
</ul> </ul>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner alert"> <th class="callout-inner alert">
@ -144,7 +141,7 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -152,11 +149,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-right"> <p class="text-right">
С уважением, овк-тян. С уважением, овк-тян.
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -164,9 +161,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -174,7 +171,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
<small> <small>
Вы получили это письмо так как кто-то или вы изменили адрес электронной почты. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт. Вы получили это письмо так как кто-то или вы изменили адрес электронной почты. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт.
@ -201,4 +198,4 @@
</tr> </tr>
</table> </table>
</body> </body>
</html> </html>

View file

@ -12,7 +12,7 @@
<tr> <tr>
<td class="float-center" align="center" valign="top"> <td class="float-center" align="center" valign="top">
<center> <center>
Добро пожаловать в OpenVK! Приятного времяприпровождения, надеюсь вам понравится.<br><br>Если появились вопросы, касаемые нашего сайта, пишите <a href="https://openvk.su/support?act=new">сюда</a> Добро пожаловать в OpenVK! Приятного времяприпровождения, надеюсь вам понравится.<br><br>Если появились вопросы, касаемые нашего сайта, пишите <a href="https://{$_SERVER['HTTP_HOST']}/support?act=new">сюда</a>
</center> </center>
</td> </td>
</tr> </tr>

View file

@ -46,17 +46,14 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="row"> <table class="row">
<tr> <tr>
<th class="small-12 large-12 columns first last"> <th class="small-12 large-12 columns first last">
<table class="row"> <table class="row">
<tr> <tr>
<td> <td>
<center>
<img src="pictures/lock.jpeg" align="center" class="float-center" width=128 height=128 />
</center>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -64,9 +61,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -74,11 +71,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Здравствуйте, {$name}! Вы вероятно забыли пароль от аккаунта OpenVK? Мы идём к Вам на помощь! Здравствуйте, {$name}! Вы вероятно забыли пароль от аккаунта OpenVK? Мы идём к Вам на помощь!
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -86,7 +83,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="button large expand success"> <table class="button large expand success">
<tr> <tr>
<td> <td>
@ -94,7 +91,7 @@
<tr> <tr>
<td> <td>
<center> <center>
<a href="http://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={rawurlencode($key)}" align="center" class="float-center">Сбросить пароль!</a> <a href="https://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={rawurlencode($key)}" align="center" class="float-center">Сбросить пароль!</a>
</center> </center>
</td> </td>
</tr> </tr>
@ -102,7 +99,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -110,30 +107,30 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя: Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя:
</p> </p>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner primary"> <th class="callout-inner primary">
<a href="http://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={$key}" style="color: #000; text-decoration: none;"> <a href="https://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={$key}" style="color: #000; text-decoration: none;">
http://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={$key} https://{$_SERVER['HTTP_HOST']}/restore?act=finish&key={$key}
</a> </a>
</th> </th>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Обратите внимание на то, что эту ссылку нельзя: Обратите внимание на то, что эту ссылку нельзя:
</p> </p>
<ul> <ul>
<li>Передавать другим людям (даже друзьям, питомцам, соседам, любимым девушкам)</li> <li>Передавать другим людям (даже друзьям, питомцам, соседям, любимым девушкам)</li>
<li>Использовать, если прошло более двух дней с её генерации</li> <li>Использовать, если прошло более двух дней с её генерации</li>
</ul> </ul>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner alert"> <th class="callout-inner alert">
@ -144,7 +141,7 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -152,11 +149,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-right"> <p class="text-right">
С уважением, овк-тян. С уважением, овк-тян.
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -164,9 +161,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -174,7 +171,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
<small> <small>
Вы получили это письмо так как кто-то или вы отправили запрос на восстановлние пароля. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт. Вы получили это письмо так как кто-то или вы отправили запрос на восстановлние пароля. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт.
@ -201,4 +198,4 @@
</tr> </tr>
</table> </table>
</body> </body>
</html> </html>

View file

@ -46,17 +46,14 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="row"> <table class="row">
<tr> <tr>
<th class="small-12 large-12 columns first last"> <th class="small-12 large-12 columns first last">
<table class="row"> <table class="row">
<tr> <tr>
<td> <td>
<center>
<img src="pictures/lock.jpeg" align="center" class="float-center" width=128 height=128 />
</center>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -64,9 +61,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -74,11 +71,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Здравствуйте, {$name}! Вы вероятно зарегистрировались на одном из инстансов OpenVK. Чтобы ваш аккаунт активировался, необходимо подтвердить Email. Здравствуйте, {$name}! Вы вероятно зарегистрировались на одном из инстансов OpenVK. Чтобы ваш аккаунт активировался, необходимо подтвердить Email.
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -86,7 +83,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="button large expand success"> <table class="button large expand success">
<tr> <tr>
<td> <td>
@ -94,7 +91,7 @@
<tr> <tr>
<td> <td>
<center> <center>
<a href="http://{$_SERVER['HTTP_HOST']}/regFinish?key={rawurlencode($key)}" align="center" class="float-center">Подтвердить Email!</a> <a href="https://{$_SERVER['HTTP_HOST']}/regFinish?key={rawurlencode($key)}" align="center" class="float-center">Подтвердить Email!</a>
</center> </center>
</td> </td>
</tr> </tr>
@ -102,7 +99,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -110,30 +107,30 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя: Если кнопка не работает, вы можете попробовать скопировать и вставить эту ссылку в адресную строку вашего веб-обозревателя:
</p> </p>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner primary"> <th class="callout-inner primary">
<a href="http://{$_SERVER['HTTP_HOST']}/regFinish?key={$key}" style="color: #000; text-decoration: none;"> <a href="https://{$_SERVER['HTTP_HOST']}/regFinish?key={$key}" style="color: #000; text-decoration: none;">
http://{$_SERVER['HTTP_HOST']}/regFinish?key={$key} https://{$_SERVER['HTTP_HOST']}/regFinish?key={$key}
</a> </a>
</th> </th>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
Обратите внимание на то, что эту ссылку нельзя: Обратите внимание на то, что эту ссылку нельзя:
</p> </p>
<ul> <ul>
<li>Передавать другим людям (даже друзьям, питомцам, соседам, любимым девушкам)</li> <li>Передавать другим людям (даже друзьям, питомцам, соседям, любимым девушкам)</li>
<li>Использовать, если прошло более двух дней с её генерации</li> <li>Использовать, если прошло более двух дней с её генерации</li>
</ul> </ul>
<table class="callout"> <table class="callout">
<tr> <tr>
<th class="callout-inner alert"> <th class="callout-inner alert">
@ -144,7 +141,7 @@
</th> </th>
</tr> </tr>
</table> </table>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -152,11 +149,11 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-right"> <p class="text-right">
С уважением, овк-тян. С уважением, овк-тян.
</p> </p>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -164,9 +161,9 @@
</td> </td>
</tr> </tr>
</table> </table>
<hr/> <hr/>
<table class="spacer"> <table class="spacer">
<tr> <tr>
<td> <td>
@ -174,7 +171,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<p class="text-left"> <p class="text-left">
<small> <small>
Вы получили это письмо так как кто-то или вы зарегистрировались на инстансе OpenVK. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт. Вы получили это письмо так как кто-то или вы зарегистрировались на инстансе OpenVK. Это не рассылка и от неё нельзя отписаться. Если вы всё равно хотите перестать получать подобные письма, деактивируйте ваш аккаунт.
@ -201,4 +198,4 @@
</tr> </tr>
</table> </table>
</body> </body>
</html> </html>

View file

@ -1,12 +1,14 @@
# <img align="right" src="https://github.com/openvk/openvk/raw/master/Web/static/img/logo_shadow.png" alt="openvk" title="openvk" width="15%">OpenVK # <img align="right" src="/Web/static/img/logo_shadow.png" alt="openvk" title="openvk" width="15%">OpenVK
_[Русский](README_RU.md)_ _[Русский](README_RU.md)_
**OpenVK** is an attempt to create a simple CMS that ~~cosplays~~ imitates old VKontakte. Code provided here is not stable yet. **OpenVK** is an attempt to create a simple CMS that ~~cosplays~~ imitates old VKontakte. Code provided here is not stable yet.
VKontakte belongs to Pavel Durov and VK Group. This is fan project, not affiliated in any way with VKontakte and it's company VK Ltd. Below is the same message in russian.
To be honest, we don't know whether if it even works. However, this version is maintained and we will be happy to accept your bugreports [in our bug tracker](https://github.com/openvk/openvk/projects/1). You should also be able to submit them using [ticketing system](https://openvk.su/support?act=new) (you will need an OpenVK account for this). OpenVK является любительской разработкой и никак не связан с ВКонтакте и компанией ООО "VK"
To be honest, we don't know whether if it even works. However, this version is maintained and we will be happy to accept your bugreports [in our bug tracker](https://github.com/openvk/openvk/projects/1). You should also be able to submit them using [ticketing system](https://ovk.to/support?act=new) (you will need an OpenVK account for this).
## When's the release? ## When's the release?
@ -26,11 +28,19 @@ However, OVK makes use of Chandler Application Server. This software requires ex
If you want, you can add your instance to the list above so that people can register there. If you want, you can add your instance to the list above so that people can register there.
### System requirements
Here is our minimum hardware recommendation:
* **CPU: Recent** (AMD Zen2 or equivalent) quad-core 2GHz+ CPU
* **RAM:** At least 2GB RAM (we recommend 6GB or 8GB for OpenVK with Kafka)
* **Minimum database space:** 10GB
### Installation procedure ### Installation procedure
1. Install PHP 7.4, web-server, Composer, Node.js, Yarn and [Chandler](https://github.com/openvk/chandler) 1. Install PHP 8.2, web-server, Composer, Node.js, NPM and [Chandler](https://github.com/openvk/chandler)
* PHP 8.1 is supported too, however it was not tested carefully, so be aware. * PHP 8 is still being tested; the functionality of the engine on this version of PHP is not yet guaranteed.
2. Install MySQL-compatible database. 2. Install MySQL-compatible database.
@ -57,7 +67,7 @@ ln -s /path/to/chandler/extensions/available/openvk /path/to/chandler/extensions
7. Copy `openvk-example.yml` to `openvk.yml` and change options to your liking 7. Copy `openvk-example.yml` to `openvk.yml` and change options to your liking
8. Run `composer install` in OpenVK directory 8. Run `composer install` in OpenVK directory
9. Run `composer install` in commitcaptcha directory 9. Run `composer install` in commitcaptcha directory
10. Move to `Web/static/js` and execute `yarn install` 10. Move to `Web/static/js` and execute `npm install`
11. Set `openvk` as your root app in `chandler.yml` 11. Set `openvk` as your root app in `chandler.yml`
Once you are done, you can login as a system administrator on the network itself (no registration required): Once you are done, you can login as a system administrator on the network itself (no registration required):
@ -66,12 +76,12 @@ Once you are done, you can login as a system administrator on the network itself
* **Password**: `admin` * **Password**: `admin`
* It is recommended to change the password of the built-in account or disable it. * It is recommended to change the password of the built-in account or disable it.
💡Confused? Full installation walkthrough is available [here](https://docs.openvk.uk/openvk_engine/centos8_installation/) (CentOS 8 [and](https://almalinux.org/) [family](https://yum.oracle.com/oracle-linux-isos.html)). 💡 Confused? Full installation walkthrough is available [here](https://docs.ovk.to/openvk_engine/centos8_installation/) (CentOS 8 [and](https://almalinux.org/) [family](https://yum.oracle.com/oracle-linux-isos.html)).
### Looking for Docker or Kubernetes deployment? ### Looking for Docker or Kubernetes deployment?
See `install/automated/docker/README.md` and `install/automated/kubernetes/README.md` for Docker and Kubernetes deployment instructions. See `install/automated/docker/README.md` and `install/automated/kubernetes/README.md` for Docker and Kubernetes deployment instructions.
### If my website uses OpenVK, should I release it's sources? ### If my website uses OpenVK, should I release its sources?
It depends. You can keep the sources to yourself if you do not plan to distribute your website binaries. If your website software must be distributed, it can stay non-OSS provided the OpenVK is not used as a primary application and is not modified. If you modified OpenVK for your needs or your work is based on it and you are planning to redistribute this, then you should license it under terms of any LGPL-compatible license (like OSL, GPL, LGPL etc). It depends. You can keep the sources to yourself if you do not plan to distribute your website binaries. If your website software must be distributed, it can stay non-OSS provided the OpenVK is not used as a primary application and is not modified. If you modified OpenVK for your needs or your work is based on it and you are planning to redistribute this, then you should license it under terms of any LGPL-compatible license (like OSL, GPL, LGPL etc).
@ -80,13 +90,13 @@ It depends. You can keep the sources to yourself if you do not plan to distribut
You may reach out to us via: You may reach out to us via:
* [Bug Tracker](https://github.com/openvk/openvk/projects/1) * [Bug Tracker](https://github.com/openvk/openvk/projects/1)
* [Ticketing System](https://openvk.su/support?act=new) * [Ticketing System](https://ovk.to/support?act=new)
* Telegram Chat: Go to [our channel](https://t.me/openvkenglish) and open discussion in our channel menu. * Telegram Chat: Go to [our channel](https://t.me/openvkenglish) and open discussion in our channel menu.
* [Reddit](https://www.reddit.com/r/openvk/) * [Reddit](https://www.reddit.com/r/openvk/)
* [GitHub Discussions](https://github.com/openvk/openvk/discussions) * [GitHub Discussions](https://github.com/openvk/openvk/discussions)
* Matrix Chat: #openvk:matrix.org * Matrix Chat: #openvk:matrix.org
**Attention**: bug tracker, board, Telegram and Matrix chat are public places, ticketing system is being served by volunteers. If you need to report something that should not be immediately disclosed to general public (for instance, a vulnerability), please contact us directly via this email: **openvk [at] tutanota [dot] com** **Attention**: bug tracker, board, Telegram and Matrix chat are public places, ticketing system is being served by volunteers. If you need to report something that should not be immediately disclosed to general public (for instance, a vulnerability), please contact us directly via this email: **contact [at] ovk [dot] to**
<a href="https://codeberg.org/OpenVK/openvk"> <a href="https://codeberg.org/OpenVK/openvk">
<img alt="Get it on Codeberg" src="https://codeberg.org/Codeberg/GetItOnCodeberg/media/branch/main/get-it-on-blue-on-white.png" height="60"> <img alt="Get it on Codeberg" src="https://codeberg.org/Codeberg/GetItOnCodeberg/media/branch/main/get-it-on-blue-on-white.png" height="60">

View file

@ -1,12 +1,12 @@
# <img align="right" src="https://github.com/openvk/openvk/raw/master/Web/static/img/logo_shadow.png" alt="openvk" title="openvk" width="15%">OpenVK # <img align="right" src="/Web/static/img/logo_shadow.png" alt="openvk" title="openvk" width="15%">OpenVK
_[English](README.md)_ _[English](README.md)_
**OpenVK** — это попытка создать простую CMS, которая ~~косплеит~~ имитирует старый ВКонтакте. На данный момент, представленный здесь исходный код проекта пока не является стабильным. **OpenVK** — это попытка создать простую CMS, которая ~~косплеит~~ имитирует старый ВКонтакте. На данный момент, представленный здесь исходный код проекта пока не является стабильным.
ВКонтакте принадлежит Павлу Дурову и VK Group. **OpenVK является любительской разработкой и никак не связан с ВКонтакте и компанией ООО "ВК"**
Честно говоря, мы даже не знаем, работает ли она вообще. Однако, эта версия поддерживается, и мы будем рады принять ваши сообщения об ошибках [в нашем баг-трекере](https://github.com/openvk/openvk/projects/1). Вы также можете отправлять их через [вкладку "Помощь"](https://openvk.su/support?act=new) (для этого вам понадобится учетная запись OpenVK). Честно говоря, мы даже не знаем, работает ли она вообще. Однако, эта версия поддерживается, и мы будем рады принять ваши сообщения об ошибках [в нашем баг-трекере](https://github.com/openvk/openvk/projects/1). Вы также можете отправлять их через [вкладку "Помощь"](https://ovk.to/support?act=new) (для этого вам понадобится учетная запись OpenVK).
## Когда выйдет релизная версия? ## Когда выйдет релизная версия?
@ -28,9 +28,9 @@ _[English](README.md)_
### Процедура установки ### Процедура установки
1. Установите PHP 7.4, веб-сервер, Composer, Node.js, Yarn и [Chandler](https://github.com/openvk/chandler) 1. Установите PHP 8.2, веб-сервер, Composer, Node.js, NPM и [Chandler](https://github.com/openvk/chandler)
* PHP 8 еще **не** тестировался, поэтому не стоит ожидать, что он будет работать (UPD: он не работает). * PHP 8 пока ещё тестируется, работоспособность движка на этой версии PHP пока не гарантируется.
2. Установите MySQL-совместимую базу данных. 2. Установите MySQL-совместимую базу данных.
@ -57,7 +57,7 @@ ln -s /path/to/chandler/extensions/available/openvk /path/to/chandler/extensions
7. Скопируйте `openvk-example.yml` в `openvk.yml` и измените параметры под свои нужды 7. Скопируйте `openvk-example.yml` в `openvk.yml` и измените параметры под свои нужды
8. Запустите `composer install` в директории OpenVK 8. Запустите `composer install` в директории OpenVK
9. Запустите `composer install` в директории commitcaptcha 9. Запустите `composer install` в директории commitcaptcha
10. Перейдите в `Web/static/js` и выполните `yarn install` 10. Перейдите в `Web/static/js` и выполните `npm install`
11. Установите `openvk` в качестве корневого приложения в файле `chandler.yml` 11. Установите `openvk` в качестве корневого приложения в файле `chandler.yml`
После этого вы можете войти как системный администратор в саму сеть (регистрация не требуется): После этого вы можете войти как системный администратор в саму сеть (регистрация не требуется):
@ -66,7 +66,7 @@ ln -s /path/to/chandler/extensions/available/openvk /path/to/chandler/extensions
* **Пароль**: `admin` * **Пароль**: `admin`
* Перед использованием встроенной учетной записи рекомендуется сменить пароль или отключить её. * Перед использованием встроенной учетной записи рекомендуется сменить пароль или отключить её.
💡Запутались? Полное руководство по установке доступно [здесь](https://docs.openvk.uk/openvk_engine/centos8_installation/) (CentOS 8 [и](https://almalinux.org/ru/) [семейство](https://yum.oracle.com/oracle-linux-isos.html)). 💡Запутались? Полное руководство по установке доступно [здесь](https://docs.ovk.to/openvk_engine/centos8_installation/) (CentOS 8 [и](https://almalinux.org/ru/) [семейство](https://yum.oracle.com/oracle-linux-isos.html)).
# Установка в Docker/Kubernetes # Установка в Docker/Kubernetes
Подробные иструкции можно найти в `install/automated/docker/README.md` и `install/automated/kubernetes/README.md` соответственно. Подробные иструкции можно найти в `install/automated/docker/README.md` и `install/automated/kubernetes/README.md` соответственно.
@ -80,7 +80,7 @@ ln -s /path/to/chandler/extensions/available/openvk /path/to/chandler/extensions
Вы можете связаться с нами через: Вы можете связаться с нами через:
* [Баг-трекер](https://github.com/openvk/openvk/projects/1) * [Баг-трекер](https://github.com/openvk/openvk/projects/1)
* [Помощь в OVK](https://openvk.su/support?act=new) * [Помощь в OVK](https://ovk.to/support?act=new)
* Telegram-чат: Перейдите на [наш канал](https://t.me/openvk) и откройте обсуждение в меню нашего канала. * Telegram-чат: Перейдите на [наш канал](https://t.me/openvk) и откройте обсуждение в меню нашего канала.
* [Reddit](https://www.reddit.com/r/openvk/) * [Reddit](https://www.reddit.com/r/openvk/)
* [GitHub Discussions](https://github.com/openvk/openvk/discussions) * [GitHub Discussions](https://github.com/openvk/openvk/discussions)

View file

@ -1,26 +1,32 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\APIToken;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\APITokens;
use openvk\Web\Models\Repositories\Applications; use openvk\Web\Models\Repositories\Applications;
use WhichBrowser;
class Apps implements Handler class Apps implements Handler
{ {
private $user; private $user;
private $apps; private $apps;
public function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->apps = new Applications; $this->apps = new Applications();
} }
function getUserInfo(callable $resolve, callable $reject): void public function getUserInfo(callable $resolve, callable $reject): void
{ {
$hexId = dechex($this->user->getId()); $hexId = dechex($this->user->getId());
$sign = hash_hmac("sha512/224", $hexId, CHANDLER_ROOT_CONF["security"]["secret"], true); $sign = hash_hmac("sha512/224", $hexId, CHANDLER_ROOT_CONF["security"]["secret"], true);
$marketingId = $hexId . "_" . base64_encode($sign); $marketingId = $hexId . "_" . base64_encode($sign);
$resolve([ $resolve([
"id" => $this->user->getId(), "id" => $this->user->getId(),
"marketing_id" => $marketingId, "marketing_id" => $marketingId,
@ -32,61 +38,84 @@ class Apps implements Handler
"ava" => $this->user->getAvatarUrl(), "ava" => $this->user->getAvatarUrl(),
]); ]);
} }
function updatePermission(int $app, string $perm, string $state, callable $resolve, callable $reject): void public function updatePermission(int $app, string $perm, string $state, callable $resolve, callable $reject): void
{ {
$app = $this->apps->get($app); $app = $this->apps->get($app);
if(!$app || !$app->isEnabled()) { if (!$app || !$app->isEnabled()) {
$reject("No application with this id found");
return;
}
if(!$app->setPermission($this->user, $perm, $state == "yes"))
$reject("Invalid permission $perm");
$resolve(1);
}
function pay(int $appId, float $amount, callable $resolve, callable $reject): void
{
$app = $this->apps->get($appId);
if(!$app || !$app->isEnabled()) {
$reject("No application with this id found"); $reject("No application with this id found");
return; return;
} }
if($amount < 0) { if (!$app->setPermission($this->user, $perm, $state == "yes")) {
$reject("Invalid permission $perm");
}
$resolve(1);
}
public function pay(int $appId, float $amount, callable $resolve, callable $reject): void
{
$app = $this->apps->get($appId);
if (!$app || !$app->isEnabled()) {
$reject("No application with this id found");
return;
}
if ($amount < 0) {
$reject(552, "Payment amount is invalid"); $reject(552, "Payment amount is invalid");
return; return;
} }
$coinsLeft = $this->user->getCoins() - $amount; $coinsLeft = $this->user->getCoins() - $amount;
if($coinsLeft < 0) { if ($coinsLeft < 0) {
$reject(41, "Not enough money"); $reject(41, "Not enough money");
return; return;
} }
$this->user->setCoins($coinsLeft); $this->user->setCoins($coinsLeft);
$this->user->save(); $this->user->save();
$app->addCoins($amount); $app->addCoins($amount);
$t = time(); $t = time();
$resolve($t . "," . hash_hmac("whirlpool", "$appId:$amount:$t", CHANDLER_ROOT_CONF["security"]["secret"])); $resolve($t . "," . hash_hmac("whirlpool", "$appId:$amount:$t", CHANDLER_ROOT_CONF["security"]["secret"]));
} }
function withdrawFunds(int $appId, callable $resolve, callable $reject): void public function withdrawFunds(int $appId, callable $resolve, callable $reject): void
{ {
$app = $this->apps->get($appId); $app = $this->apps->get($appId);
if(!$app) { if (!$app) {
$reject("No application with this id found"); $reject("No application with this id found");
return; return;
} else if($app->getOwner()->getId() != $this->user->getId()) { } elseif ($app->getOwner()->getId() != $this->user->getId()) {
$reject("You don't have rights to edit this app"); $reject("You don't have rights to edit this app");
return; return;
} }
$coins = $app->getBalance(); $coins = $app->getBalance();
$app->withdrawCoins(); $app->withdrawCoins();
$resolve($coins); $resolve($coins);
} }
}
public function getRegularToken(string $clientName, bool $acceptsStale, callable $resolve, callable $reject): void
{
$token = null;
$stale = true;
if ($acceptsStale) {
$token = (new APITokens())->getStaleByUser($this->user->getId(), $clientName);
}
if (is_null($token)) {
$stale = false;
$token = new APIToken();
$token->setUser($this->user);
$token->setPlatform($clientName ?? (new WhichBrowser\Parser(getallheaders()))->toString());
$token->save();
}
$resolve([
'is_stale' => $stale,
'token' => $token->getFormattedToken(),
]);
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Clubs; use openvk\Web\Models\Repositories\Clubs;
@ -7,30 +11,30 @@ class Groups implements Handler
{ {
protected $user; protected $user;
protected $groups; protected $groups;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->groups = new Clubs; $this->groups = new Clubs();
} }
function getWriteableClubs(callable $resolve, callable $reject) public function getWriteableClubs(callable $resolve, callable $reject)
{ {
$clubs = []; $clubs = [];
$wclubs = $this->groups->getWriteableClubs($this->user->getId()); $wclubs = $this->groups->getWriteableClubs($this->user->getId());
$count = $this->groups->getWriteableClubsCount($this->user->getId()); $count = $this->groups->getWriteableClubsCount($this->user->getId());
if(!$count) { if (!$count) {
$reject("You don't have any groups with write access"); $reject("You don't have any groups with write access");
return; return;
} }
foreach($wclubs as $club) { foreach ($wclubs as $club) {
$clubs[] = [ $clubs[] = [
"name" => $club->getName(), "name" => $club->getName(),
"id" => $club->getId(), "id" => $club->getId(),
"avatar" => $club->getAvatarUrl() # если в овк когда-нибудь появится крутой список с аватарками, то можно использовать это поле "avatar" => $club->getAvatarUrl(), # если в овк когда-нибудь появится крутой список с аватарками, то можно использовать это поле
]; ];
} }

View file

@ -1,8 +1,12 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
interface Handler interface Handler
{ {
function __construct(?User $user); public function __construct(?User $user);
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Users, Clubs}; use openvk\Web\Models\Repositories\{Users, Clubs};
@ -7,16 +11,16 @@ class Mentions implements Handler
{ {
protected $user; protected $user;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
} }
function resolve(int $id, callable $resolve, callable $reject): void public function resolve(int $id, callable $resolve, callable $reject): void
{ {
if($id > 0) { if ($id > 0) {
$user = (new Users)->get($id); $user = (new Users())->get($id);
if(!$user) { if (!$user) {
$reject("Not found"); $reject("Not found");
return; return;
} }
@ -32,8 +36,8 @@ class Mentions implements Handler
return; return;
} }
$club = (new Clubs)->get(abs($id)); $club = (new Clubs())->get(abs($id));
if(!$club) { if (!$club) {
$reject("Not found"); $reject("Not found");
return; return;
} }

View file

@ -1,6 +1,7 @@
<?php <?php
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Notes as NoteRepo; use openvk\Web\Models\Repositories\Notes as NoteRepo;
@ -8,24 +9,30 @@ class Notes implements Handler
{ {
protected $user; protected $user;
protected $notes; protected $notes;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->notes = new NoteRepo; $this->notes = new NoteRepo();
} }
function getNote(int $noteId, callable $resolve, callable $reject): void public function getNote(int $noteId, callable $resolve, callable $reject): void
{ {
$note = $this->notes->get($noteId); $note = $this->notes->get($noteId);
if(!$note || $note->isDeleted()) if (!$note || $note->isDeleted()) {
$reject(83, "Note is gone"); $reject(83, "Note is gone");
}
$noteOwner = $note->getOwner(); $noteOwner = $note->getOwner();
assert($noteOwner instanceof User); assert($noteOwner instanceof User);
if(!$noteOwner->getPrivacyPermission("notes.read", $this->user)) if (!$noteOwner->getPrivacyPermission("notes.read", $this->user)) {
$reject(160, "You don't have permission to access this note"); $reject(160, "You don't have permission to access this note");
}
if (!$note->canBeViewedBy($this->user)) {
$reject(15, "Access to note denied");
}
$resolve([ $resolve([
"title" => $note->getName(), "title" => $note->getName(),
"link" => "/note" . $note->getPrettyId(), "link" => "/note" . $note->getPrettyId(),

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use Latte\Engine as TemplatingEngine; use Latte\Engine as TemplatingEngine;
use RdKafka\{Conf as RDKConf, KafkaConsumer}; use RdKafka\{Conf as RDKConf, KafkaConsumer};
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
@ -9,55 +13,55 @@ class Notifications implements Handler
{ {
protected $user; protected $user;
protected $notifs; protected $notifs;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->notifs = new N; $this->notifs = new N();
} }
function ack(callable $resolve, callable $reject): void public function ack(callable $resolve, callable $reject): void
{ {
$this->user->updateNotificationOffset(); $this->user->updateNotificationOffset();
$this->user->save(); $this->user->save();
$resolve("OK"); $resolve("OK");
} }
function fetch(callable $resolve, callable $reject): void public function fetch(callable $resolve, callable $reject): void
{ {
$kafkaConf = OPENVK_ROOT_CONF["openvk"]["credentials"]["notificationsBroker"]; $kafkaConf = OPENVK_ROOT_CONF["openvk"]["credentials"]["notificationsBroker"];
if(!$kafkaConf["enable"]) { if (!$kafkaConf["enable"]) {
$reject(1999, "Disabled"); $reject(1999, "Disabled");
return; return;
} }
$kafkaConf = $kafkaConf["kafka"]; $kafkaConf = $kafkaConf["kafka"];
$conf = new RDKConf(); $conf = new RDKConf();
$conf->set("metadata.broker.list", $kafkaConf["addr"] . ":" . $kafkaConf["port"]); $conf->set("metadata.broker.list", $kafkaConf["addr"] . ":" . $kafkaConf["port"]);
$conf->set("group.id", "UserFetch-" . $this->user->getId()); # Чтобы уведы приходили только на разные устройства одного чебупелика $conf->set("group.id", "UserFetch-" . $this->user->getId()); # Чтобы уведы приходили только на разные устройства одного чебупелика
$conf->set("auto.offset.reset", "latest"); $conf->set("auto.offset.reset", "latest");
set_time_limit(30); set_time_limit(30);
$consumer = new KafkaConsumer($conf); $consumer = new KafkaConsumer($conf);
$consumer->subscribe([ $kafkaConf["topic"] ]); $consumer->subscribe([ $kafkaConf["topic"] ]);
while(true) { while (true) {
$message = $consumer->consume(30*1000); $message = $consumer->consume(30 * 1000);
switch ($message->err) { switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR: case RD_KAFKA_RESP_ERR_NO_ERROR:
$descriptor = $message->payload; $descriptor = $message->payload;
[,$user,] = explode(",", $descriptor); [,$user,] = explode(",", $descriptor);
if(((int) $user) === $this->user->getId()) { if (((int) $user) === $this->user->getId()) {
$data = (object) []; $data = (object) [];
$notification = $this->notifs->fromDescriptor($descriptor, $data); $notification = $this->notifs->fromDescriptor($descriptor, $data);
if(!$notification) { if (!$notification) {
$reject(1982, "Server Error"); $reject(1982, "Server Error");
return; return;
} }
$tplDir = __DIR__ . "/../Web/Presenters/templates/components/notifications/"; $tplDir = __DIR__ . "/../Web/Presenters/templates/components/notifications/";
$tplId = "$tplDir$data->actionCode/_$data->originModelType" . "_" . $data->targetModelType . "_.xml"; $tplId = "$tplDir$data->actionCode/_$data->originModelType" . "_" . $data->targetModelType . "_.xml";
$latte = new TemplatingEngine; $latte = new TemplatingEngine();
$latte->setTempDirectory(CHANDLER_ROOT . "/tmp/cache/templates"); $latte->setTempDirectory(CHANDLER_ROOT . "/tmp/cache/templates");
$latte->addFilter("translate", fn($trId) => tr($trId)); $latte->addFilter("translate", fn($trId) => tr($trId));
$resolve([ $resolve([
@ -68,7 +72,7 @@ class Notifications implements Handler
]); ]);
return; return;
} }
break; break;
case RD_KAFKA_RESP_ERR__TIMED_OUT: case RD_KAFKA_RESP_ERR__TIMED_OUT:
case RD_KAFKA_RESP_ERR__PARTITION_EOF: case RD_KAFKA_RESP_ERR__PARTITION_EOF:

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use Chandler\MVC\Routing\Router; use Chandler\MVC\Routing\Router;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Exceptions\{AlreadyVotedException, InvalidOptionException, PollLockedException}; use openvk\Web\Models\Exceptions\{AlreadyVotedException, InvalidOptionException, PollLockedException};
@ -10,61 +14,61 @@ class Polls implements Handler
{ {
protected $user; protected $user;
protected $polls; protected $polls;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->polls = new PollRepo; $this->polls = new PollRepo();
} }
private function getPollHtml(int $poll): string private function getPollHtml(int $poll): string
{ {
return Router::i()->execute("/poll$poll", "SAPI"); return Router::i()->execute("/poll$poll", "SAPI");
} }
function vote(int $pollId, string $options, callable $resolve, callable $reject): void public function vote(int $pollId, string $options, callable $resolve, callable $reject): void
{ {
$poll = $this->polls->get($pollId); $poll = $this->polls->get($pollId);
if(!$poll) { if (!$poll) {
$reject("Poll not found"); $reject("Poll not found");
return; return;
} }
try { try {
$options = explode(",", $options); $options = explode(",", $options);
$poll->vote($this->user, $options); $poll->vote($this->user, $options);
} catch(AlreadyVotedException $ex) { } catch (AlreadyVotedException $ex) {
$reject("Poll state changed: user has already voted."); $reject("Poll state changed: user has already voted.");
return; return;
} catch(PollLockedException $ex) { } catch (PollLockedException $ex) {
$reject("Poll state changed: poll has ended."); $reject("Poll state changed: poll has ended.");
return; return;
} catch(InvalidOptionException $ex) { } catch (InvalidOptionException $ex) {
$reject("Foreign options passed."); $reject("Foreign options passed.");
return; return;
} catch(UnexpectedValueException $ex) { } catch (UnexpectedValueException $ex) {
$reject("Too much options passed."); $reject("Too much options passed.");
return; return;
} }
$resolve(["html" => $this->getPollHtml($pollId)]); $resolve(["html" => $this->getPollHtml($pollId)]);
} }
function unvote(int $pollId, callable $resolve, callable $reject): void public function unvote(int $pollId, callable $resolve, callable $reject): void
{ {
$poll = $this->polls->get($pollId); $poll = $this->polls->get($pollId);
if(!$poll) { if (!$poll) {
$reject("Poll not found"); $reject("Poll not found");
return; return;
} }
try { try {
$poll->revokeVote($this->user); $poll->revokeVote($this->user);
} catch(PollLockedException $ex) { } catch (PollLockedException $ex) {
$reject("Votes can't be revoked from this poll."); $reject("Votes can't be revoked from this poll.");
return; return;
} }
$resolve(["html" => $this->getPollHtml($pollId)]); $resolve(["html" => $this->getPollHtml($pollId)]);
} }
} }

View file

@ -1,76 +0,0 @@
<?php declare(strict_types=1);
namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\{User, Club};
use openvk\Web\Models\Repositories\{Users, Clubs, Videos};
use Chandler\Database\DatabaseConnection;
class Search implements Handler
{
protected $user;
private $users;
private $clubs;
private $videos;
function __construct(?User $user)
{
$this->user = $user;
$this->users = new Users;
$this->clubs = new Clubs;
$this->videos = new Videos;
}
function fastSearch(string $query, string $type = "users", callable $resolve, callable $reject)
{
if($query == "" || strlen($query) < 3)
$reject(12, "No input or input < 3");
$repo;
$sort;
switch($type) {
default:
case "users":
$repo = (new Users);
$sort = "rating DESC";
break;
case "groups":
$repo = (new Clubs);
$sort = "id ASC";
break;
case "videos":
$repo = (new Videos);
$sort = "created ASC";
break;
}
$res = $repo->find($query, ["doNotSearchMe" => $this->user->getId()], $sort);
$results = array_slice(iterator_to_array($res), 0, 5);
$count = sizeof($results);
$arr = [
"count" => $count,
"items" => []
];
if(sizeof($results) < 1) {
$reject(2, "No results");
}
foreach($results as $res) {
$arr["items"][] = [
"id" => $res->getId(),
"name" => $type == "users" ? $res->getCanonicalName() : $res->getName(),
"avatar" => $type != "videos" ? $res->getAvatarUrl() : $res->getThumbnailURL(),
"url" => $type != "videos" ? $res->getUrl() : "/video".$res->getPrettyId(),
"description" => ovk_proc_strtr($res->getDescription() ?? "...", 40)
];
}
$resolve($arr);
}
}

View file

@ -1,23 +1,27 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
class Service implements Handler class Service implements Handler
{ {
protected $user; protected $user;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
} }
function getTime(callable $resolve, callable $reject): void public function getTime(callable $resolve, callable $reject): void
{ {
$resolve(trim((new DateTime)->format("%e %B %G" . tr("time_at_sp") . "%X"))); $resolve(trim((new DateTime())->format("%e %B %G" . tr("time_at_sp") . "%X")));
} }
function getServerVersion(callable $resolve, callable $reject): void public function getServerVersion(callable $resolve, callable $reject): void
{ {
$resolve("OVK " . OPENVK_VERSION); $resolve("OVK " . OPENVK_VERSION);
} }

View file

@ -1,49 +1,64 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\ServiceAPI; namespace openvk\ServiceAPI;
use openvk\Web\Models\Entities\Post; use openvk\Web\Models\Entities\Post;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Posts, Notes}; use openvk\Web\Models\Repositories\{Posts, Notes, Videos};
class Wall implements Handler class Wall implements Handler
{ {
protected $user; protected $user;
protected $posts; protected $posts;
protected $notes; protected $notes;
function __construct(?User $user) public function __construct(?User $user)
{ {
$this->user = $user; $this->user = $user;
$this->posts = new Posts; $this->posts = new Posts();
$this->notes = new Notes; $this->notes = new Notes();
$this->videos = new Videos();
} }
function getPost(int $id, callable $resolve, callable $reject): void public function getPost(int $id, callable $resolve, callable $reject): void
{ {
$post = $this->posts->get($id); $post = $this->posts->get($id);
if(!$post || $post->isDeleted()) if (!$post || $post->isDeleted()) {
$reject("No post with id=$id"); $reject(53, "No post with id=$id");
}
if ($post->getSuggestionType() != 0) {
$reject(25, "Can't get suggested post");
}
if (!$post->canBeViewedBy($this->user)) {
$reject(12, "Access denied");
}
$res = (object) []; $res = (object) [];
$res->id = $post->getId(); $res->id = $post->getId();
$res->wall = $post->getTargetWall(); $res->wall = $post->getTargetWall();
$res->author = (($owner = $post->getOwner())) instanceof User $res->author = (($owner = $post->getOwner())) instanceof User
? ($owner->getId()) ? ($owner->getId())
: ($owner->getId() * -1); : ($owner->getId() * -1);
if($post->isSigned()) if ($post->isSigned()) {
$res->signedOffBy = $post->getOwnerPost(); $res->signedOffBy = $post->getOwnerPost();
}
$res->pinned = $post->isPinned(); $res->pinned = $post->isPinned();
$res->sponsored = $post->isAd(); $res->sponsored = $post->isAd();
$res->nsfw = $post->isExplicit(); $res->nsfw = $post->isExplicit();
$res->text = $post->getText(); $res->text = $post->getText();
$res->likes = [ $res->likes = [
"count" => $post->getLikesCount(), "count" => $post->getLikesCount(),
"hasLike" => $post->hasLikeFrom($this->user), "hasLike" => $post->hasLikeFrom($this->user),
"likedBy" => [], "likedBy" => [],
]; ];
foreach($post->getLikers() as $liker) { foreach ($post->getLikers() as $liker) {
$res->likes["likedBy"][] = [ $res->likes["likedBy"][] = [
"id" => $liker->getId(), "id" => $liker->getId(),
"url" => $liker->getURL(), "url" => $liker->getURL(),
@ -51,17 +66,17 @@ class Wall implements Handler
"avatar" => $liker->getAvatarURL(), "avatar" => $liker->getAvatarURL(),
]; ];
} }
$res->created = (string) $post->getPublicationTime(); $res->created = (string) $post->getPublicationTime();
$res->canPin = $post->canBePinnedBy($this->user); $res->canPin = $post->canBePinnedBy($this->user);
$res->canEdit = $res->canDelete = $post->canBeDeletedBy($this->user); $res->canEdit = $res->canDelete = $post->canBeDeletedBy($this->user);
$resolve((array) $res); $resolve((array) $res);
} }
function newStatus(string $text, callable $resolve, callable $reject): void public function newStatus(string $text, callable $resolve, callable $reject): void
{ {
$post = new Post; $post = new Post();
$post->setOwner($this->user->getId()); $post->setOwner($this->user->getId());
$post->setWall($this->user->getId()); $post->setWall($this->user->getId());
$post->setCreated(time()); $post->setCreated(time());
@ -70,29 +85,7 @@ class Wall implements Handler
$post->setFlags(0); $post->setFlags(0);
$post->setNsfw(false); $post->setNsfw(false);
$post->save(); $post->save();
$resolve($post->getId()); $resolve($post->getId());
} }
function getMyNotes(callable $resolve, callable $reject)
{
$count = $this->notes->getUserNotesCount($this->user);
$myNotes = $this->notes->getUserNotes($this->user, 1, $count);
$arr = [
"count" => $count,
"closed" => $this->user->getPrivacySetting("notes.read"),
"items" => [],
];
foreach($myNotes as $note) {
$arr["items"][] = [
"id" => $note->getId(),
"name" => ovk_proc_strtr($note->getName(), 30),
#"preview" => $note->getPreview()
];
}
$resolve($arr);
}
} }

View file

@ -1,5 +1,7 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Exceptions; namespace openvk\VKAPI\Exceptions;
class APIErrorException extends \Exception class APIErrorException extends \Exception {}
{}

View file

@ -1,28 +1,46 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Exceptions\InvalidUserNameException; use openvk\Web\Models\Exceptions\InvalidUserNameException;
final class Account extends VKAPIRequestHandler final class Account extends VKAPIRequestHandler
{ {
function getProfileInfo(): object public function getProfileInfo(): object
{ {
$this->requireUser(); $this->requireUser();
$user = $this->getUser();
return (object) [ $return_object = (object) [
"first_name" => $this->getUser()->getFirstName(), "first_name" => $user->getFirstName(),
"id" => $this->getUser()->getId(), "photo_200" => $user->getAvatarURL("normal"),
"last_name" => $this->getUser()->getLastName(), "nickname" => $user->getPseudo(),
"home_town" => $this->getUser()->getHometown(), "is_service_account" => false,
"status" => $this->getUser()->getStatus(), "id" => $user->getId(),
"bdate" => is_null($this->getUser()->getBirthday()) ? '01.01.1970' : $this->getUser()->getBirthday()->format('%e.%m.%Y'), "is_verified" => $user->isVerified(),
"bdate_visibility" => $this->getUser()->getBirthdayPrivacy(), "verification_status" => $user->isVerified() ? 'verified' : 'unverified',
"last_name" => $user->getLastName(),
"home_town" => $user->getHometown(),
"status" => $user->getStatus(),
"bdate" => is_null($user->getBirthday()) ? '01.01.1970' : $user->getBirthday()->format('%e.%m.%Y'),
"bdate_visibility" => $user->getBirthdayPrivacy(),
"phone" => "+420 ** *** 228", # TODO "phone" => "+420 ** *** 228", # TODO
"relation" => $this->getUser()->getMaritalStatus(), "relation" => $user->getMaritalStatus(),
"sex" => $this->getUser()->isFemale() ? 1 : 2 "screen_name" => $user->getShortCode(),
"sex" => $user->isFemale() ? 1 : 2,
#"email" => $user->getEmail(),
]; ];
$audio_status = $user->getCurrentAudioStatus();
if (!is_null($audio_status)) {
$return_object->audio_status = $audio_status->toVkApiStruct($user);
}
return $return_object;
} }
function getInfo(): object public function getInfo(): object
{ {
$this->requireUser(); $this->requireUser();
@ -37,58 +55,58 @@ final class Account extends VKAPIRequestHandler
"is_new_live_streaming_enabled" => false, "is_new_live_streaming_enabled" => false,
"lang" => 1, "lang" => 1,
"no_wall_replies" => 0, "no_wall_replies" => 0,
"own_posts_default" => 0 "own_posts_default" => 0,
]; ];
} }
function setOnline(): int public function setOnline(): int
{ {
$this->requireUser(); $this->requireUser();
$this->getUser()->updOnline($this->getPlatform()); $this->getUser()->updOnline($this->getPlatform());
return 1; return 1;
} }
function setOffline(): int public function setOffline(): int
{ {
$this->requireUser(); $this->requireUser();
# Цiй метод є заглушка # Цiй метод є заглушка
return 1; return 1;
} }
function getAppPermissions(): int public function getAppPermissions(): int
{ {
return 9355263; return 9355263;
} }
function getCounters(string $filter = ""): object public function getCounters(string $filter = ""): object
{ {
$this->requireUser(); $this->requireUser();
return (object) [ return (object) [
"friends" => $this->getUser()->getFollowersCount(), "friends" => $this->getUser()->getFollowersCount(),
"notifications" => $this->getUser()->getNotificationsCount(), "notifications" => $this->getUser()->getNotificationsCount(),
"messages" => $this->getUser()->getUnreadMessagesCount() "messages" => $this->getUser()->getUnreadMessagesCount(),
]; ];
# TODO: Filter # TODO: Filter
} }
function saveProfileInfo(string $first_name = "", string $last_name = "", string $screen_name = "", int $sex = -1, int $relation = -1, string $bdate = "", int $bdate_visibility = -1, string $home_town = "", string $status = ""): object public function saveProfileInfo(string $first_name = "", string $last_name = "", string $screen_name = "", int $sex = -1, int $relation = -1, string $bdate = "", int $bdate_visibility = -1, string $home_town = "", string $status = ""): object
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$user = $this->getUser(); $user = $this->getUser();
$output = [ $output = [
"changed" => 0, "changed" => 0,
]; ];
if(!empty($first_name) || !empty($last_name)) { if (!empty($first_name) || !empty($last_name)) {
$output["name_request"] = [ $output["name_request"] = [
"id" => random_int(1, 2048), # For compatibility with original VK API "id" => random_int(1, 2048), # For compatibility with original VK API
"status" => "success", "status" => "success",
@ -97,37 +115,44 @@ final class Account extends VKAPIRequestHandler
]; ];
try { try {
if(!empty($first_name)) if (!empty($first_name)) {
$user->setFirst_name($first_name); $user->setFirst_name($first_name);
if(!empty($last_name)) }
if (!empty($last_name)) {
$user->setLast_Name($last_name); $user->setLast_Name($last_name);
}
} catch (InvalidUserNameException $e) { } catch (InvalidUserNameException $e) {
$output["name_request"]["status"] = "declined"; $output["name_request"]["status"] = "declined";
return (object) $output; return (object) $output;
} }
} }
if(!empty($screen_name)) if (!empty($screen_name)) {
if (!$user->setShortCode($screen_name)) if (!$user->setShortCode($screen_name)) {
$this->fail(1260, "Invalid screen name"); $this->fail(1260, "Invalid screen name");
}
}
# For compatibility with original VK API # For compatibility with original VK API
if($sex > 0) if ($sex > 0) {
$user->setSex($sex == 1 ? 1 : 0); $user->setSex($sex == 1 ? 1 : 0);
}
if($relation > -1)
$user->setMarital_Status($relation);
if(!empty($bdate)) { if ($relation > -1) {
$user->setMarital_Status($relation);
}
if (!empty($bdate)) {
$birthday = strtotime($bdate); $birthday = strtotime($bdate);
if (!is_int($birthday)) if (!is_int($birthday)) {
$this->fail(100, "invalid value of bdate."); $this->fail(100, "invalid value of bdate.");
}
$user->setBirthday($birthday); $user->setBirthday($birthday);
} }
# For compatibility with original VK API # For compatibility with original VK API
switch($bdate_visibility) { switch ($bdate_visibility) {
case 0: case 0:
$this->fail(946, "Hiding date of birth is not implemented."); $this->fail(946, "Hiding date of birth is not implemented.");
break; break;
@ -137,18 +162,215 @@ final class Account extends VKAPIRequestHandler
case 2: case 2:
$user->setBirthday_privacy(1); $user->setBirthday_privacy(1);
} }
if(!empty($home_town))
$user->setHometown($home_town);
if(!empty($status)) if (!empty($home_town)) {
$user->setHometown($home_town);
}
if (!empty($status)) {
$user->setStatus($status); $user->setStatus($status);
}
if($sex > 0 || $relation > -1 || $bdate_visibility > 1 || !empty("$first_name$last_name$screen_name$bdate$home_town$status")) {
if ($sex > 0 || $relation > -1 || $bdate_visibility > 1 || !empty("$first_name$last_name$screen_name$bdate$home_town$status")) {
$output["changed"] = 1; $output["changed"] = 1;
$user->save(); $user->save();
} }
return (object) $output; return (object) $output;
} }
public function getBalance(): object
{
$this->requireUser();
if (!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) {
$this->fail(105, "Commerce is disabled on this instance");
}
return (object) ['votes' => $this->getUser()->getCoins()];
}
public function getOvkSettings(): object
{
$this->requireUser();
$user = $this->getUser();
$settings_list = (object) [
'avatar_style' => $user->getStyleAvatar(),
'style' => $user->getStyle(),
'show_rating' => !$user->prefersNotToSeeRating(),
'nsfw_tolerance' => $user->getNsfwTolerance(),
'post_view' => $user->hasMicroblogEnabled() ? 'microblog' : 'old',
'main_page' => $user->getMainPage() == 0 ? 'my_page' : 'news',
];
return $settings_list;
}
public function sendVotes(int $receiver, int $value, string $message = ""): object
{
$this->requireUser();
$this->willExecuteWriteAction();
if (!OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]) {
$this->fail(-105, "Commerce is disabled on this instance");
}
if ($receiver < 0) {
$this->fail(-248, "Invalid receiver id");
}
if ($value < 1) {
$this->fail(-248, "Invalid value");
}
if (iconv_strlen($message) > 255) {
$this->fail(-249, "Message is too long");
}
if ($this->getUser()->getCoins() < $value) {
$this->fail(-252, "Not enough votes");
}
$receiver_entity = (new \openvk\Web\Models\Repositories\Users())->get($receiver);
if (!$receiver_entity || $receiver_entity->isDeleted() || !$receiver_entity->canBeViewedBy($this->getUser())) {
$this->fail(-250, "Invalid receiver");
}
if ($receiver_entity->getId() === $this->getUser()->getId()) {
$this->fail(-251, "Can't transfer votes to yourself");
}
$this->getUser()->setCoins($this->getUser()->getCoins() - $value);
$this->getUser()->save();
$receiver_entity->setCoins($receiver_entity->getCoins() + $value);
$receiver_entity->save();
(new \openvk\Web\Models\Entities\Notifications\CoinsTransferNotification($receiver_entity, $this->getUser(), $value, $message))->emit();
return (object) ['votes' => $this->getUser()->getCoins()];
}
public function ban(int $owner_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
if ($owner_id < 0) {
return 1;
}
if ($owner_id == $this->getUser()->getId()) {
$this->fail(15, "Access denied: cannot blacklist yourself");
}
$config_limit = OPENVK_ROOT_CONF['openvk']['preferences']['blacklists']['limit'] ?? 100;
$user_blocks = $this->getUser()->getBlacklistSize();
if (($user_blocks + 1) > $config_limit) {
$this->fail(-7856, "Blacklist limit exceeded");
}
$entity = get_entity_by_id($owner_id);
if (!$entity || $entity->isDeleted()) {
return 0;
}
if ($entity->isBlacklistedBy($this->getUser())) {
return 1;
}
$this->getUser()->addToBlacklist($entity);
return 1;
}
public function unban(int $owner_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
if ($owner_id < 0) {
return 1;
}
if ($owner_id == $this->getUser()->getId()) {
return 1;
}
$entity = get_entity_by_id($owner_id);
if (!$entity) {
return 0;
}
if (!$entity->isBlacklistedBy($this->getUser())) {
return 1;
}
$this->getUser()->removeFromBlacklist($entity);
return 1;
}
public function getBanned(int $offset = 0, int $count = 100, string $fields = ""): object
{
$this->requireUser();
$result = (object) [
'count' => $this->getUser()->getBlacklistSize(),
'items' => [],
];
$banned = $this->getUser()->getBlacklist($offset, $count);
foreach ($banned as $ban) {
if (!$ban) {
continue;
}
$result->items[] = $ban->toVkApiStruct($this->getUser(), $fields);
}
return $result;
}
public function saveInterestsInfo(
string $interests = null,
string $fav_music = null,
string $fav_films = null,
string $fav_shows = null,
string $fav_books = null,
string $fav_quote = null,
string $fav_games = null,
string $about = null,
) {
$this->requireUser();
$this->willExecuteWriteAction();
$user = $this->getUser();
$changes = 0;
$changes_array = [
"interests" => $interests,
"fav_music" => $fav_music,
"fav_films" => $fav_films,
"fav_books" => $fav_books,
"fav_shows" => $fav_shows,
"fav_quote" => $fav_quote,
"fav_games" => $fav_games,
"about" => $about,
];
foreach ($changes_array as $change_name => $change_value) {
$set_name = "set" . ucfirst($change_name);
$get_name = "get" . str_replace("Fav", "Favorite", str_replace("_", "", ucfirst($change_name)));
if (!is_null($change_value) && $change_value !== $user->$get_name()) {
$user->$set_name(ovk_proc_strtr($change_value, 1000));
$changes += 1;
}
}
if ($changes > 0) {
$user->save();
}
return (object) [
"changed" => (int) ($changes > 0),
];
}
} }

View file

@ -1,22 +1,865 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\Audio as AEntity;
use openvk\Web\Models\Entities\Playlist;
use openvk\Web\Models\Repositories\Audios;
use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\Repositories\Util\EntityStream;
final class Audio extends VKAPIRequestHandler final class Audio extends VKAPIRequestHandler
{ {
function get(): object private function toSafeAudioStruct(?AEntity $audio, ?string $hash = null, bool $need_user = false): object
{ {
$serverUrl = ovk_scheme(true) . $_SERVER["SERVER_NAME"]; if (!$audio) {
$this->fail(0o404, "Audio not found");
return (object) [ } elseif (!$audio->canBeViewedBy($this->getUser())) {
"count" => 1, $this->fail(201, "Access denied to audio(" . $audio->getPrettyId() . ")");
"items" => [(object) [ }
"id" => 1,
"owner_id" => 1, # рофлан ебало
"artist" => "В ОВК ПОКА НЕТ МУЗЫКИ", $privApi = $hash && $GLOBALS["csrfCheck"];
"title" => "ЖДИТЕ :)))", $audioObj = $audio->toVkApiStruct($this->getUser());
"duration" => 22, if (!$privApi) {
"url" => $serverUrl . "/assets/packages/static/openvk/audio/nomusic.mp3" $audioObj->manifest = false;
]] $audioObj->keys = false;
]; }
}
if ($need_user) {
$user = (new \openvk\Web\Models\Repositories\Users())->get($audio->getOwner()->getId());
$audioObj->user = (object) [
"id" => $user->getId(),
"photo" => $user->getAvatarUrl(),
"name" => $user->getCanonicalName(),
"name_gen" => $user->getCanonicalName(),
];
}
return $audioObj;
}
private function streamToResponse(EntityStream $es, int $offset, int $count, ?string $hash = null): object
{
$items = [];
foreach ($es->offsetLimit($offset, $count) as $audio) {
$items[] = $this->toSafeAudioStruct($audio, $hash);
}
return (object) [
"count" => sizeof($items),
"items" => $items,
];
}
private function validateGenre(?string& $genre_str, ?int $genre_id): void
{
if (!is_null($genre_str)) {
if (!in_array($genre_str, AEntity::genres)) {
$this->fail(8, "Invalid genre_str");
}
} elseif (!is_null($genre_id)) {
$genre_str = array_flip(AEntity::vkGenres)[$genre_id] ?? null;
if (!$genre_str) {
$this->fail(8, "Invalid genre ID $genre_id");
}
}
}
private function audioFromAnyId(string $id): ?AEntity
{
$descriptor = explode("_", $id);
if (sizeof($descriptor) === 1) {
if (ctype_digit($descriptor[0])) {
$audio = (new Audios())->get((int) $descriptor[0]);
} else {
$aid = base64_decode($descriptor[0], true);
if (!$aid) {
$this->fail(8, "Invalid audio $id");
}
$audio = (new Audios())->get((int) $aid);
}
} elseif (sizeof($descriptor) === 2) {
$audio = (new Audios())->getByOwnerAndVID((int) $descriptor[0], (int) $descriptor[1]);
} else {
$this->fail(8, "Invalid audio $id");
}
return $audio;
}
public function getById(string $audios, ?string $hash = null, int $need_user = 0): object
{
$this->requireUser();
$audioIds = array_unique(explode(",", $audios));
if (sizeof($audioIds) === 1) {
$audio = $this->audioFromAnyId($audioIds[0]);
return (object) [
"count" => 1,
"items" => [
$this->toSafeAudioStruct($audio, $hash, (bool) $need_user),
],
];
} elseif (sizeof($audioIds) > 6000) {
$this->fail(1980, "Can't get more than 6000 audios at once");
}
$audios = [];
foreach ($audioIds as $id) {
$audios[] = $this->getById($id, $hash)->items[0];
}
return (object) [
"count" => sizeof($audios),
"items" => $audios,
];
}
public function isLagtrain(string $audio_id): int
{
$this->requireUser();
$audio = $this->audioFromAnyId($audio_id);
if (!$audio) {
$this->fail(0o404, "Audio not found");
}
# Possible information disclosure risks are acceptable :D
return (int) (strpos($audio->getName(), "Lagtrain") !== false);
}
// TODO stub
public function getRecommendations(): object
{
return (object) [
"count" => 0,
"items" => [],
];
}
public function getPopular(?int $genre_id = null, ?string $genre_str = null, int $offset = 0, int $count = 100, ?string $hash = null): object
{
$this->requireUser();
$this->validateGenre($genre_str, $genre_id);
$results = (new Audios())->getGlobal(Audios::ORDER_POPULAR, $genre_str);
return $this->streamToResponse($results, $offset, $count, $hash);
}
public function getFeed(?int $genre_id = null, ?string $genre_str = null, int $offset = 0, int $count = 100, ?string $hash = null): object
{
$this->requireUser();
$this->validateGenre($genre_str, $genre_id);
$results = (new Audios())->getGlobal(Audios::ORDER_NEW, $genre_str);
return $this->streamToResponse($results, $offset, $count, $hash);
}
public function search(string $q, int $auto_complete = 0, int $lyrics = 0, int $performer_only = 0, int $sort = 2, int $search_own = 0, int $offset = 0, int $count = 30, ?string $hash = null): object
{
$this->requireUser();
if (($auto_complete + $search_own) != 0) {
$this->fail(10, "auto_complete and search_own are not supported");
} elseif ($count > 300 || $count < 1) {
$this->fail(8, "count is invalid: $count");
}
$results = (new Audios())->search($q, $sort, (bool) $performer_only, (bool) $lyrics);
return $this->streamToResponse($results, $offset, $count, $hash);
}
public function getCount(int $owner_id, int $uploaded_only = 0): int
{
$this->requireUser();
if ($owner_id < 0) {
$owner_id *= -1;
$group = (new Clubs())->get($owner_id);
if (!$group) {
$this->fail(0o404, "Group not found");
}
return (new Audios())->getClubCollectionSize($group);
}
$user = (new \openvk\Web\Models\Repositories\Users())->get($owner_id);
if (!$user) {
$this->fail(0o404, "User not found");
}
if (!$user->getPrivacyPermission("audios.read", $this->getUser())) {
$this->fail(15, "Access denied");
}
if ($uploaded_only) {
return DatabaseConnection::i()->getContext()->table("audios")
->where([
"deleted" => false,
"owner" => $owner_id,
])->count();
}
return (new Audios())->getUserCollectionSize($user);
}
public function get(int $owner_id = 0, int $album_id = 0, string $audio_ids = '', int $need_user = 1, int $offset = 0, int $count = 100, int $uploaded_only = 0, int $need_seed = 0, ?string $shuffle_seed = null, int $shuffle = 0, ?string $hash = null): object
{
$this->requireUser();
$shuffleSeed = null;
$shuffleSeedStr = null;
if ($shuffle == 1) {
if (!$shuffle_seed) {
if ($need_seed == 1) {
$shuffleSeed = openssl_random_pseudo_bytes(6);
$shuffleSeedStr = base64_encode($shuffleSeed);
$shuffleSeed = hexdec(bin2hex($shuffleSeed));
} else {
$hOffset = ((int) date("i") * 60) + (int) date("s");
$thisHour = time() - $hOffset;
$shuffleSeed = $thisHour + $this->getUser()->getId();
$shuffleSeedStr = base64_encode(hex2bin(dechex($shuffleSeed)));
}
} else {
$shuffleSeed = hexdec(bin2hex(base64_decode($shuffle_seed)));
$shuffleSeedStr = $shuffle_seed;
}
}
if ($album_id != 0) {
$album = (new Audios())->getPlaylist($album_id);
if (!$album) {
$this->fail(0o404, "album_id invalid");
} elseif (!$album->canBeViewedBy($this->getUser())) {
$this->fail(600, "Can't open this album for reading");
}
$songs = [];
$list = $album->getAudios($offset, $count, $shuffleSeed);
foreach ($list as $song) {
$songs[] = $this->toSafeAudioStruct($song, $hash, $need_user == 1);
}
$response = (object) [
"count" => sizeof($songs),
"items" => $songs,
];
if (!is_null($shuffleSeed)) {
$response->shuffle_seed = $shuffleSeedStr;
}
return $response;
}
if (!empty($audio_ids)) {
$audio_ids = explode(",", $audio_ids);
if (!$audio_ids) {
$this->fail(10, "Audio::get@L0d186:explode(string): Unknown error");
} elseif (sizeof($audio_ids) < 1) {
$this->fail(8, "Invalid audio_ids syntax");
}
if (!is_null($shuffleSeed)) {
$audio_ids = knuth_shuffle($audio_ids, $shuffleSeed);
}
$obj = $this->getById(implode(",", $audio_ids), $hash, $need_user);
if (!is_null($shuffleSeed)) {
$obj->shuffle_seed = $shuffleSeedStr;
}
return $obj;
}
$dbCtx = DatabaseConnection::i()->getContext();
if ($uploaded_only == 1) {
if ($owner_id <= 0) {
$this->fail(8, "uploaded_only can only be used with owner_id > 0");
}
$user = (new \openvk\Web\Models\Repositories\Users())->get($owner_id);
if (!$user) {
$this->fail(0o602, "Invalid user");
}
if (!$user->getPrivacyPermission("audios.read", $this->getUser())) {
$this->fail(15, "Access denied: this user chose to hide his audios");
}
if (!is_null($shuffleSeed)) {
$audio_ids = [];
$query = $dbCtx->table("audios")->select("virtual_id")->where([
"owner" => $owner_id,
"deleted" => 0,
]);
foreach ($query as $res) {
$audio_ids[] = $res->virtual_id;
}
$audio_ids = knuth_shuffle($audio_ids, $shuffleSeed);
$audio_ids = array_slice($audio_ids, $offset, $count);
$audio_q = ""; # audio.getById query
foreach ($audio_ids as $aid) {
$audio_q .= ",$owner_id" . "_$aid";
}
$obj = $this->getById(substr($audio_q, 1), $hash, $need_user);
$obj->shuffle_seed = $shuffleSeedStr;
return $obj;
}
$res = (new Audios())->getByUploader((new \openvk\Web\Models\Repositories\Users())->get($owner_id));
return $this->streamToResponse($res, $offset, $count, $hash, $need_user);
}
$query = $dbCtx->table("audio_relations")->select("audio")->where("entity", $owner_id);
if (!is_null($shuffleSeed)) {
$audio_ids = [];
foreach ($query as $aid) {
$audio_ids[] = $aid->audio;
}
$audio_ids = knuth_shuffle($audio_ids, $shuffleSeed);
$audio_ids = array_slice($audio_ids, $offset, $count);
$audio_q = "";
foreach ($audio_ids as $aid) {
$audio_q .= ",$aid";
}
$obj = $this->getById(substr($audio_q, 1), $hash, $need_user);
$obj->shuffle_seed = $shuffleSeedStr;
return $obj;
}
$items = [];
if ($owner_id > 0) {
$user = (new \openvk\Web\Models\Repositories\Users())->get($owner_id);
if (!$user) {
$this->fail(50, "Invalid user");
}
if (!$user->getPrivacyPermission("audios.read", $this->getUser())) {
$this->fail(15, "Access denied: this user chose to hide his audios");
}
}
$audios = (new Audios())->getByEntityID($owner_id, $offset, $count);
foreach ($audios as $audio) {
$items[] = $this->toSafeAudioStruct($audio, $hash, $need_user == 1);
}
return (object) [
"count" => sizeof($items),
"items" => $items,
];
}
public function getLyrics(int $lyrics_id): object
{
$this->requireUser();
$audio = (new Audios())->get($lyrics_id);
if (!$audio || !$audio->getLyrics()) {
$this->fail(0o404, "Not found");
}
if (!$audio->canBeViewedBy($this->getUser())) {
$this->fail(201, "Access denied to lyrics");
}
return (object) [
"lyrics_id" => $lyrics_id,
"text" => preg_replace("%\r\n?%", "\n", $audio->getLyrics()),
];
}
public function beacon(int $aid, ?int $gid = null): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$audio = (new Audios())->get($aid);
if (!$audio) {
$this->fail(0o404, "Not Found");
} elseif (!$audio->canBeViewedBy($this->getUser())) {
$this->fail(201, "Insufficient permissions to listen this audio");
}
$group = null;
if (!is_null($gid)) {
$group = (new Clubs())->get($gid);
if (!$group) {
$this->fail(0o404, "Not Found");
} elseif (!$group->canBeModifiedBy($this->getUser())) {
$this->fail(203, "Insufficient rights to this group");
}
}
return (int) $audio->listen($group ?? $this->getUser());
}
public function setBroadcast(string $audio, string $target_ids): array
{
$this->requireUser();
[$owner, $aid] = explode("_", $audio);
$song = (new Audios())->getByOwnerAndVID((int) $owner, (int) $aid);
$ids = [];
foreach (explode(",", $target_ids) as $id) {
$id = (int) $id;
if ($id > 0) {
if ($id != $this->getUser()->getId()) {
$this->fail(600, "Can't listen on behalf of $id");
} else {
$ids[] = $id;
$this->beacon($song->getId());
continue;
}
}
$group = (new Clubs())->get($id * -1);
if (!$group) {
$this->fail(0o404, "Not Found");
} elseif (!$group->canBeModifiedBy($this->getUser())) {
$this->fail(203, "Insufficient rights to this group");
}
$ids[] = $id;
$this->beacon($song ? $song->getId() : 0, $id * -1);
}
return $ids;
}
public function getBroadcastList(string $filter = "all", int $active = 0, ?string $hash = null): object
{
$this->requireUser();
if (!in_array($filter, ["all", "friends", "groups"])) {
$this->fail(8, "Invalid filter $filter");
}
$broadcastList = $this->getUser()->getBroadcastList($filter);
$items = [];
foreach ($broadcastList as $res) {
$struct = $res->toVkApiStruct();
$status = $res->getCurrentAudioStatus();
$struct->status_audio = $status ? $this->toSafeAudioStruct($status) : null;
$items[] = $struct;
}
return (object) [
"count" => sizeof($items),
"items" => $items,
];
}
public function edit(int $owner_id, int $audio_id, ?string $artist = null, ?string $title = null, ?string $text = null, ?int $genre_id = null, ?string $genre_str = null, int $no_search = 0): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$audio = (new Audios())->getByOwnerAndVID($owner_id, $audio_id);
if (!$audio) {
$this->fail(0o404, "Not Found");
} elseif (!$audio->canBeModifiedBy($this->getUser())) {
$this->fail(201, "Insufficient permissions to edit this audio");
}
if (!is_null($genre_id)) {
$genre = array_flip(AEntity::vkGenres)[$genre_id] ?? null;
if (!$genre) {
$this->fail(8, "Invalid genre ID $genre_id");
}
$audio->setGenre($genre);
} elseif (!is_null($genre_str)) {
if (!in_array($genre_str, AEntity::genres)) {
$this->fail(8, "Invalid genre ID $genre_str");
}
$audio->setGenre($genre_str);
}
$lyrics = 0;
if (!is_null($text)) {
$audio->setLyrics($text);
$lyrics = $audio->getId();
}
if (!is_null($artist)) {
$audio->setPerformer($artist);
}
if (!is_null($title)) {
$audio->setName($title);
}
$audio->setSearchability(!((bool) $no_search));
$audio->setEdited(time());
$audio->save();
return $lyrics;
}
public function add(int $audio_id, int $owner_id, ?int $group_id = null, ?int $album_id = null): string
{
$this->requireUser();
$this->willExecuteWriteAction();
if (!is_null($album_id)) {
$this->fail(10, "album_id not implemented");
}
// TODO get rid of dups
$to = $this->getUser();
if (!is_null($group_id)) {
$group = (new Clubs())->get($group_id);
if (!$group) {
$this->fail(0o404, "Invalid group_id");
} elseif (!$group->canBeModifiedBy($this->getUser())) {
$this->fail(203, "Insufficient rights to this group");
}
$to = $group;
}
$audio = (new Audios())->getByOwnerAndVID($owner_id, $audio_id);
if (!$audio) {
$this->fail(0o404, "Not found");
} elseif (!$audio->canBeViewedBy($this->getUser())) {
$this->fail(201, "Access denied to audio(owner=$owner_id, vid=$audio_id)");
}
try {
$audio->add($to);
} catch (\OverflowException $ex) {
$this->fail(300, "Album is full");
}
return $audio->getPrettyId();
}
public function delete(int $audio_id, int $owner_id, ?int $group_id = null): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$from = $this->getUser();
if (!is_null($group_id)) {
$group = (new Clubs())->get($group_id);
if (!$group) {
$this->fail(0o404, "Invalid group_id");
} elseif (!$group->canBeModifiedBy($this->getUser())) {
$this->fail(203, "Insufficient rights to this group");
}
$from = $group;
}
$audio = (new Audios())->getByOwnerAndVID($owner_id, $audio_id);
if (!$audio) {
$this->fail(0o404, "Not found");
}
$audio->remove($from);
return 1;
}
public function restore(int $audio_id, int $owner_id, ?int $group_id = null, ?string $hash = null): object
{
$this->requireUser();
$vid = $this->add($audio_id, $owner_id, $group_id);
return $this->getById($vid, $hash)->items[0];
}
public function getAlbums(int $owner_id = 0, int $offset = 0, int $count = 50, int $drop_private = 1): object
{
$this->requireUser();
$owner_id = $owner_id == 0 ? $this->getUser()->getId() : $owner_id;
$playlists = [];
if ($owner_id > 0 && $owner_id != $this->getUser()->getId()) {
$user = (new \openvk\Web\Models\Repositories\Users())->get($owner_id);
if (!$user->getPrivacyPermission("audios.read", $this->getUser())) {
$this->fail(50, "Access to playlists denied");
}
}
foreach ((new Audios())->getPlaylistsByEntityId($owner_id, $offset, $count) as $playlist) {
if (!$playlist->canBeViewedBy($this->getUser())) {
if ($drop_private == 1) {
continue;
}
$playlists[] = null;
continue;
}
$playlists[] = $playlist->toVkApiStruct($this->getUser());
}
return (object) [
"count" => sizeof($playlists),
"items" => $playlists,
];
}
public function searchAlbums(string $query = '', int $offset = 0, int $limit = 25, int $drop_private = 0, int $order = 0, int $from_me = 0): object
{
$this->requireUser();
$playlists = [];
$params = [];
$order_str = (['id', 'length', 'listens'][$order] ?? 'id');
if ($from_me === 1) {
$params['from_me'] = $this->getUser()->getId();
}
$search = (new Audios())->findPlaylists($query, $params, ['type' => $order_str, 'invert' => false]);
foreach ($search->offsetLimit($offset, $limit) as $playlist) {
if (!$playlist->canBeViewedBy($this->getUser())) {
if ($drop_private == 0) {
$playlists[] = null;
}
continue;
}
$playlists[] = $playlist->toVkApiStruct($this->getUser());
}
return (object) [
"count" => $search->size(),
"items" => $playlists,
];
}
public function addAlbum(string $title, ?string $description = null, int $group_id = 0): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$group = null;
if ($group_id != 0) {
$group = (new Clubs())->get($group_id);
if (!$group) {
$this->fail(0o404, "Invalid group_id");
} elseif (!$group->canBeModifiedBy($this->getUser())) {
$this->fail(600, "Insufficient rights to this group");
}
}
$album = new Playlist();
$album->setName($title);
if (!is_null($group)) {
$album->setOwner($group_id * -1);
} else {
$album->setOwner($this->getUser()->getId());
}
if (!is_null($description)) {
$album->setDescription($description);
}
$album->save();
if (!is_null($group)) {
$album->bookmark($group);
} else {
$album->bookmark($this->getUser());
}
return $album->getId();
}
public function editAlbum(int $album_id, ?string $title = null, ?string $description = null): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($album_id);
if (!$album) {
$this->fail(0o404, "Album not found");
} elseif (!$album->canBeModifiedBy($this->getUser())) {
$this->fail(600, "Insufficient rights to this album");
}
if (!is_null($title)) {
$album->setName($title);
}
if (!is_null($description)) {
$album->setDescription($description);
}
$album->setEdited(time());
$album->save();
return (int) !(!$title && !$description);
}
public function deleteAlbum(int $album_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($album_id);
if (!$album) {
$this->fail(0o404, "Album not found");
} elseif (!$album->canBeModifiedBy($this->getUser())) {
$this->fail(600, "Insufficient rights to this album");
}
$album->delete();
return 1;
}
public function moveToAlbum(int $album_id, string $audio_ids): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($album_id);
if (!$album) {
$this->fail(0o404, "Album not found");
} elseif (!$album->canBeModifiedBy($this->getUser())) {
$this->fail(600, "Insufficient rights to this album");
}
$audios = [];
$audio_ids = array_unique(explode(",", $audio_ids));
if (sizeof($audio_ids) < 1 || sizeof($audio_ids) > 1000) {
$this->fail(8, "audio_ids must contain at least 1 audio and at most 1000");
}
foreach ($audio_ids as $audio_id) {
$audio = $this->audioFromAnyId($audio_id);
if (!$audio) {
continue;
} elseif (!$audio->canBeViewedBy($this->getUser())) {
continue;
}
$audios[] = $audio;
}
if (sizeof($audios) < 1) {
return 0;
}
$res = 1;
try {
foreach ($audios as $audio) {
$res = min($res, (int) $album->add($audio));
}
} catch (\OutOfBoundsException $ex) {
return 0;
}
return $res;
}
public function removeFromAlbum(int $album_id, string $audio_ids): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($album_id);
if (!$album) {
$this->fail(0o404, "Album not found");
} elseif (!$album->canBeModifiedBy($this->getUser())) {
$this->fail(600, "Insufficient rights to this album");
}
$audios = [];
$audio_ids = array_unique(explode(",", $audio_ids));
if (sizeof($audio_ids) < 1 || sizeof($audio_ids) > 1000) {
$this->fail(8, "audio_ids must contain at least 1 audio and at most 1000");
}
foreach ($audio_ids as $audio_id) {
$audio = $this->audioFromAnyId($audio_id);
if (!$audio) {
continue;
} elseif ($audio->canBeViewedBy($this->getUser())) {
continue;
}
$audios[] = $audio;
}
if (sizeof($audios) < 1) {
return 0;
}
foreach ($audios as $audio) {
$album->remove($audio);
}
return 1;
}
public function copyToAlbum(int $album_id, string $audio_ids): int
{
return $this->moveToAlbum($album_id, $audio_ids);
}
public function bookmarkAlbum(int $id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($id);
if (!$album) {
$this->fail(0o404, "Not found");
}
if (!$album->canBeViewedBy($this->getUser())) {
$this->fail(600, "Access error");
}
return (int) $album->bookmark($this->getUser());
}
public function unBookmarkAlbum(int $id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$album = (new Audios())->getPlaylist($id);
if (!$album) {
$this->fail(0o404, "Not found");
}
if (!$album->canBeViewedBy($this->getUser())) {
$this->fail(600, "Access error");
}
return (int) $album->unbookmark($this->getUser());
}
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\VKAPI\Handlers\Wall; use openvk\VKAPI\Handlers\Wall;
use openvk\Web\Models\Repositories\Topics as TopicsRepo; use openvk\Web\Models\Repositories\Topics as TopicsRepo;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo; use openvk\Web\Models\Repositories\Clubs as ClubsRepo;
@ -11,27 +15,28 @@ use openvk\Web\Models\Entities\{Topic, Comment, User, Photo, Video};
final class Board extends VKAPIRequestHandler final class Board extends VKAPIRequestHandler
{ {
# 13/13 # 13/13
function addTopic(int $group_id, string $title, string $text = "", bool $from_group = true, string $attachments = "") public function addTopic(int $group_id, string $title, string $text = "", bool $from_group = true, string $attachments = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$club = (new ClubsRepo)->get($group_id); $club = (new ClubsRepo())->get($group_id);
if(!$club) { if (!$club) {
$this->fail(403, "Invalid club"); $this->fail(403, "Invalid club");
} }
if(!$club->canBeModifiedBy($this->getUser()) && !$club->isEveryoneCanCreateTopics()) { if (!$club->canBeModifiedBy($this->getUser()) && !$club->isEveryoneCanCreateTopics()) {
$this->fail(403, "Access to club denied"); $this->fail(403, "Access to club denied");
} }
$flags = 0; $flags = 0;
if($from_group == true && $club->canBeModifiedBy($this->getUser())) if ($from_group == true && $club->canBeModifiedBy($this->getUser())) {
$flags |= 0b10000000; $flags |= 0b10000000;
}
$topic = new Topic;
$topic = new Topic();
$topic->setGroup($club->getId()); $topic->setGroup($club->getId());
$topic->setOwner($this->getUser()->getId()); $topic->setOwner($this->getUser()->getId());
$topic->setTitle(ovk_proc_strtr($title, 127)); $topic->setTitle(ovk_proc_strtr($title, 127));
@ -39,8 +44,8 @@ final class Board extends VKAPIRequestHandler
$topic->setFlags($flags); $topic->setFlags($flags);
$topic->save(); $topic->save();
if(!empty($text)) { if (!empty($text)) {
$comment = new Comment; $comment = new Comment();
$comment->setOwner($this->getUser()->getId()); $comment->setOwner($this->getUser()->getId());
$comment->setModel(get_class($topic)); $comment->setModel(get_class($topic));
$comment->setTarget($topic->getId()); $comment->setTarget($topic->getId());
@ -48,45 +53,51 @@ final class Board extends VKAPIRequestHandler
$comment->setCreated(time()); $comment->setCreated(time());
$comment->setFlags($flags); $comment->setFlags($flags);
$comment->save(); $comment->save();
if(!empty($attachments)) { if (!empty($attachments)) {
$attachmentsArr = explode(",", $attachments); $attachmentsArr = explode(",", $attachments);
# блин а мне это везде копировать типа # блин а мне это везде копировать типа
if(sizeof($attachmentsArr) > 10) if (sizeof($attachmentsArr) > 10) {
$this->fail(50, "Error: too many attachments"); $this->fail(50, "Error: too many attachments");
}
foreach($attachmentsArr as $attac) {
$attachmentType = NULL;
if(str_contains($attac, "photo")) foreach ($attachmentsArr as $attac) {
$attachmentType = null;
if (str_contains($attac, "photo")) {
$attachmentType = "photo"; $attachmentType = "photo";
elseif(str_contains($attac, "video")) } elseif (str_contains($attac, "video")) {
$attachmentType = "video"; $attachmentType = "video";
else } else {
$this->fail(205, "Unknown attachment type"); $this->fail(205, "Unknown attachment type");
}
$attachment = str_replace($attachmentType, "", $attac); $attachment = str_replace($attachmentType, "", $attac);
$attachmentOwner = (int)explode("_", $attachment)[0]; $attachmentOwner = (int) explode("_", $attachment)[0];
$attachmentId = (int)end(explode("_", $attachment)); $attachmentId = (int) end(explode("_", $attachment));
$attacc = NULL; $attacc = null;
if($attachmentType == "photo") { if ($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new PhotosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Photo does not exists"); $this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this photo"); $this->fail(43, "You do not have access to this photo");
}
$comment->attach($attacc); $comment->attach($attacc);
} elseif($attachmentType == "video") { } elseif ($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new VideosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Video does not exists"); $this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this video"); $this->fail(43, "You do not have access to this video");
}
$comment->attach($attacc); $comment->attach($attacc);
} }
@ -100,18 +111,18 @@ final class Board extends VKAPIRequestHandler
return $topic->getId(); return $topic->getId();
} }
function closeTopic(int $group_id, int $topic_id) public function closeTopic(int $group_id, int $topic_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
if(!$topic->isClosed()) { if (!$topic->isClosed()) {
$topic->setClosed(1); $topic->setClosed(1);
$topic->save(); $topic->save();
} }
@ -119,31 +130,32 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function createComment(int $group_id, int $topic_id, string $message = "", string $attachments = "", bool $from_group = true) public function createComment(int $group_id, int $topic_id, string $message = "", string $attachments = "", bool $from_group = true)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if(empty($message) && empty($attachments)) { if (empty($message) && empty($attachments)) {
$this->fail(100, "Required parameter 'message' missing."); $this->fail(100, "Required parameter 'message' missing.");
} }
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || $topic->isDeleted() || $topic->isClosed()) { if (!$topic || $topic->isDeleted() || $topic->isClosed()) {
$this->fail(100, "Topic is deleted, closed or invalid."); $this->fail(100, "Topic is deleted, closed or invalid.");
} }
$flags = 0; $flags = 0;
if($from_group != 0 && !is_null($topic->getClub()) && $topic->getClub()->canBeModifiedBy($this->user)) if ($from_group != 0 && !is_null($topic->getClub()) && $topic->getClub()->canBeModifiedBy($this->user)) {
$flags |= 0b10000000; $flags |= 0b10000000;
}
if(strlen($message) > 300) { if (strlen($message) > 300) {
$this->fail(20, "Comment is too long."); $this->fail(20, "Comment is too long.");
} }
$comment = new Comment; $comment = new Comment();
$comment->setOwner($this->getUser()->getId()); $comment->setOwner($this->getUser()->getId());
$comment->setModel(get_class($topic)); $comment->setModel(get_class($topic));
$comment->setTarget($topic->getId()); $comment->setTarget($topic->getId());
@ -152,43 +164,49 @@ final class Board extends VKAPIRequestHandler
$comment->setFlags($flags); $comment->setFlags($flags);
$comment->save(); $comment->save();
if(!empty($attachments)) { if (!empty($attachments)) {
$attachmentsArr = explode(",", $attachments); $attachmentsArr = explode(",", $attachments);
if(sizeof($attachmentsArr) > 10) if (sizeof($attachmentsArr) > 10) {
$this->fail(50, "Error: too many attachments"); $this->fail(50, "Error: too many attachments");
}
foreach($attachmentsArr as $attac) {
$attachmentType = NULL;
if(str_contains($attac, "photo")) foreach ($attachmentsArr as $attac) {
$attachmentType = null;
if (str_contains($attac, "photo")) {
$attachmentType = "photo"; $attachmentType = "photo";
elseif(str_contains($attac, "video")) } elseif (str_contains($attac, "video")) {
$attachmentType = "video"; $attachmentType = "video";
else } else {
$this->fail(205, "Unknown attachment type"); $this->fail(205, "Unknown attachment type");
}
$attachment = str_replace($attachmentType, "", $attac); $attachment = str_replace($attachmentType, "", $attac);
$attachmentOwner = (int)explode("_", $attachment)[0]; $attachmentOwner = (int) explode("_", $attachment)[0];
$attachmentId = (int)end(explode("_", $attachment)); $attachmentId = (int) end(explode("_", $attachment));
$attacc = NULL; $attacc = null;
if($attachmentType == "photo") { if ($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new PhotosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Photo does not exists"); $this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this photo"); $this->fail(43, "You do not have access to this photo");
}
$comment->attach($attacc); $comment->attach($attacc);
} elseif($attachmentType == "video") { } elseif ($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new VideosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Video does not exists"); $this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this video"); $this->fail(43, "You do not have access to this video");
}
$comment->attach($attacc); $comment->attach($attacc);
} }
@ -198,29 +216,30 @@ final class Board extends VKAPIRequestHandler
return $comment->getId(); return $comment->getId();
} }
function deleteComment(int $comment_id, int $group_id = 0, int $topic_id = 0) public function deleteComment(int $comment_id, int $group_id = 0, int $topic_id = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$comment = (new CommentsRepo)->get($comment_id); $comment = (new CommentsRepo())->get($comment_id);
if($comment->isDeleted() || !$comment || !$comment->canBeDeletedBy($this->getUser())) if ($comment->isDeleted() || !$comment || !$comment->canBeDeletedBy($this->getUser())) {
$this->fail(403, "Access to comment denied"); $this->fail(403, "Access to comment denied");
}
$comment->delete(); $comment->delete();
return 1; return 1;
} }
function deleteTopic(int $group_id, int $topic_id) public function deleteTopic(int $group_id, int $topic_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
@ -229,7 +248,7 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function editComment(int $comment_id, int $group_id = 0, int $topic_id = 0, string $message, string $attachments) public function editComment(int $comment_id, int $group_id = 0, int $topic_id = 0, string $message, string $attachments)
{ {
/* /*
$this->requireUser(); $this->requireUser();
@ -239,7 +258,7 @@ final class Board extends VKAPIRequestHandler
if($comment->getOwner() != $this->getUser()->getId()) if($comment->getOwner() != $this->getUser()->getId())
$this->fail(15, "Access to comment denied"); $this->fail(15, "Access to comment denied");
$comment->setContent($message); $comment->setContent($message);
$comment->setEdited(time()); $comment->setEdited(time());
$comment->save(); $comment->save();
@ -247,14 +266,14 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function editTopic(int $group_id, int $topic_id, string $title) public function editTopic(int $group_id, int $topic_id, string $title)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || $topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
@ -265,14 +284,14 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function fixTopic(int $group_id, int $topic_id) public function fixTopic(int $group_id, int $topic_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
@ -283,32 +302,31 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function getComments(int $group_id, int $topic_id, bool $need_likes = false, int $start_comment_id = 0, int $offset = 0, int $count = 40, bool $extended = false, string $sort = "asc") public function getComments(int $group_id, int $topic_id, bool $need_likes = false, int $start_comment_id = 0, int $offset = 0, int $count = 40, bool $extended = false, string $sort = "asc")
{ {
# start_comment_id ne robit # start_comment_id ne robit
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || $topic->isDeleted()) { $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if (!$topic || !$topic->getClub() || $topic->isDeleted()) {
$this->fail(5, "Invalid topic"); $this->fail(5, "Invalid topic");
} }
$arr = [ $arr = [
"items" => [] "items" => [],
]; ];
$comms = array_slice(iterator_to_array($topic->getComments(1, $count + $offset)), $offset); $comms = array_slice(iterator_to_array($topic->getComments(1, $count + $offset)), $offset);
foreach($comms as $comm) { foreach ($comms as $comm) {
$arr["items"][] = $this->getApiBoardComment($comm, $need_likes); $arr["items"][] = $this->getApiBoardComment($comm, $need_likes);
if($extended) { if ($extended) {
if($comm->getOwner() instanceof \openvk\Web\Models\Entities\User) { if ($comm->getOwner() instanceof \openvk\Web\Models\Entities\User) {
$arr["profiles"][] = $comm->getOwner()->toVkApiStruct(); $arr["profiles"][] = $comm->getOwner()->toVkApiStruct();
} }
if($comm->getOwner() instanceof \openvk\Web\Models\Entities\Club) { if ($comm->getOwner() instanceof \openvk\Web\Models\Entities\Club) {
$arr["groups"][] = $comm->getOwner()->toVkApiStruct(); $arr["groups"][] = $comm->getOwner()->toVkApiStruct();
} }
} }
@ -317,35 +335,36 @@ final class Board extends VKAPIRequestHandler
return $arr; return $arr;
} }
function getTopics(int $group_id, string $topic_ids = "", int $order = 1, int $offset = 0, int $count = 40, bool $extended = false, int $preview = 0, int $preview_length = 90) public function getTopics(int $group_id, string $topic_ids = "", int $order = 1, int $offset = 0, int $count = 40, bool $extended = false, int $preview = 0, int $preview_length = 90)
{ {
# order и extended ничё не делают # order и extended ничё не делают
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$arr = []; $arr = [];
$club = (new ClubsRepo)->get($group_id); $club = (new ClubsRepo())->get($group_id);
$topics = array_slice(iterator_to_array((new TopicsRepo)->getClubTopics($club, 1, $count + $offset)), $offset); $topics = array_slice(iterator_to_array((new TopicsRepo())->getClubTopics($club, 1, $count + $offset)), $offset);
$arr["count"] = (new TopicsRepo)->getClubTopicsCount($club); $arr["count"] = (new TopicsRepo())->getClubTopicsCount($club);
$arr["items"] = []; $arr["items"] = [];
$arr["default_order"] = $order; $arr["default_order"] = $order;
$arr["can_add_topics"] = $club->canBeModifiedBy($this->getUser()) ? true : $club->isEveryoneCanCreateTopics() ? true : false; $arr["can_add_topics"] = $club->canBeModifiedBy($this->getUser()) ? true : ($club->isEveryoneCanCreateTopics() ? true : false);
$arr["profiles"] = []; $arr["profiles"] = [];
if(empty($topic_ids)) { if (empty($topic_ids)) {
foreach($topics as $topic) { foreach ($topics as $topic) {
if($topic->isDeleted()) continue; if ($topic->isDeleted()) {
continue;
}
$arr["items"][] = $topic->toVkApiStruct($preview, $preview_length > 1 ? $preview_length : 90); $arr["items"][] = $topic->toVkApiStruct($preview, $preview_length > 1 ? $preview_length : 90);
} }
} else { } else {
$topics = explode(',', $topic_ids); $topics = explode(',', $topic_ids);
foreach($topics as $topic) { foreach ($topics as $topic) {
$id = explode("_", $topic); $id = explode("_", $topic);
$topicy = (new TopicsRepo)->getTopicById((int)$id[0], (int)$id[1]); $topicy = (new TopicsRepo())->getTopicById((int) $id[0], (int) $id[1]);
if($topicy && !$topicy->isDeleted()) { if ($topicy && !$topicy->isDeleted()) {
$arr["items"][] = $topicy->toVkApiStruct($preview, $preview_length > 1 ? $preview_length : 90); $arr["items"][] = $topicy->toVkApiStruct($preview, $preview_length > 1 ? $preview_length : 90);
} }
} }
@ -354,18 +373,18 @@ final class Board extends VKAPIRequestHandler
return $arr; return $arr;
} }
function openTopic(int $group_id, int $topic_id) public function openTopic(int $group_id, int $topic_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || !$topic->isDeleted() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
if($topic->isClosed()) { if ($topic->isClosed()) {
$topic->setClosed(0); $topic->setClosed(0);
$topic->save(); $topic->save();
} }
@ -373,23 +392,23 @@ final class Board extends VKAPIRequestHandler
return 1; return 1;
} }
function restoreComment(int $group_id, int $topic_id, int $comment_id) public function restoreComment(int $group_id, int $topic_id, int $comment_id)
{ {
$this->fail(501, "Not implemented"); $this->fail(501, "Not implemented");
} }
function unfixTopic(int $group_id, int $topic_id) public function unfixTopic(int $group_id, int $topic_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$topic = (new TopicsRepo)->getTopicById($group_id, $topic_id); $topic = (new TopicsRepo())->getTopicById($group_id, $topic_id);
if(!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) { if (!$topic || !$topic->getClub() || !$topic->getClub()->canBeModifiedBy($this->getUser())) {
return 0; return 0;
} }
if($topic->isPinned()) { if ($topic->isPinned()) {
$topic->setClosed(0); $topic->setClosed(0);
$topic->save(); $topic->save();
} }
@ -411,21 +430,22 @@ final class Board extends VKAPIRequestHandler
$res->text = $comment->getText(false); $res->text = $comment->getText(false);
$res->attachments = []; $res->attachments = [];
$res->likes = []; $res->likes = [];
if($need_likes) { if ($need_likes) {
$res->likes = [ $res->likes = [
"count" => $comment->getLikesCount(), "count" => $comment->getLikesCount(),
"user_likes" => (int) $comment->hasLikeFrom($this->getUser()), "user_likes" => (int) $comment->hasLikeFrom($this->getUser()),
"can_like" => 1 # а чё типо не может ахахаххахах "can_like" => 1, # а чё типо не может ахахаххахах
]; ];
} }
foreach($comment->getChildren() as $attachment) { foreach ($comment->getChildren() as $attachment) {
if($attachment->isDeleted()) if ($attachment->isDeleted()) {
continue; continue;
}
$res->attachments[] = $attachment->toVkApiStruct(); $res->attachments[] = $attachment->toVkApiStruct();
} }
return $res; return $res;
} }
} }

242
VKAPI/Handlers/Docs.php Normal file
View file

@ -0,0 +1,242 @@
<?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\Document;
use openvk\Web\Models\Repositories\Documents;
final class Docs extends VKAPIRequestHandler
{
public function add(int $owner_id, int $doc_id, ?string $access_key): string
{
$this->requireUser();
$this->willExecuteWriteAction();
$doc = (new Documents())->getDocumentById($owner_id, $doc_id, $access_key);
if (!$doc || $doc->isDeleted()) {
$this->fail(1150, "Invalid document id");
}
if (!$doc->checkAccessKey($access_key)) {
$this->fail(15, "Access denied");
}
if ($doc->isCopiedBy($this->getUser())) {
$this->fail(100, "One of the parameters specified was missing or invalid: this document already added");
}
$new_doc = $doc->copy($this->getUser());
return $new_doc->getPrettyId();
}
public function delete(int $owner_id, int $doc_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$doc = (new Documents())->getDocumentByIdUnsafe($owner_id, $doc_id);
if (!$doc || $doc->isDeleted()) {
$this->fail(1150, "Invalid document id");
}
if (!$doc->canBeModifiedBy($this->getUser())) {
$this->fail(1153, "Access to document is denied");
}
$doc->delete();
return 1;
}
public function restore(int $owner_id, int $doc_id): int
{
$this->requireUser();
$this->willExecuteWriteAction();
return $this->add($owner_id, $doc_id, "");
}
public function edit(int $owner_id, int $doc_id, ?string $title = "", ?string $tags = "", ?int $folder_id = 0, int $owner_hidden = -1): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$doc = (new Documents())->getDocumentByIdUnsafe($owner_id, $doc_id);
if (!$doc || $doc->isDeleted()) {
$this->fail(1150, "Invalid document id");
}
if (!$doc->canBeModifiedBy($this->getUser())) {
$this->fail(1153, "Access to document is denied");
}
if (iconv_strlen($title ?? "") > 128 || iconv_strlen($title ?? "") < 0) {
$this->fail(1152, "Invalid document title");
}
if (iconv_strlen($tags ?? "") > 256) {
$this->fail(1154, "Invalid tags");
}
if ($title) {
$doc->setName($title);
}
$doc->setTags($tags);
if (in_array($folder_id, [0, 3])) {
$doc->setFolder_id($folder_id);
}
if (in_array($owner_hidden, [0, 1])) {
$doc->setOwner_hidden($owner_hidden);
}
try {
$doc->setEdited(time());
$doc->save();
} catch (\Throwable $e) {
return 0;
}
return 1;
}
public function get(int $count = 30, int $offset = 0, int $type = -1, int $owner_id = null, int $return_tags = 0, int $order = 0): object
{
$this->requireUser();
if (!$owner_id) {
$owner_id = $this->getUser()->getId();
}
if ($owner_id > 0 && $owner_id != $this->getUser()->getId()) {
$this->fail(15, "Access denied");
}
$documents = (new Documents())->getDocumentsByOwner($owner_id, $order, $type);
$res = (object) [
"count" => $documents->size(),
"items" => [],
];
foreach ($documents->offsetLimit($offset, $count) as $doc) {
$res->items[] = $doc->toVkApiStruct($this->getUser(), $return_tags == 1);
}
return $res;
}
public function getById(string $docs, int $return_tags = 0): array
{
$this->requireUser();
$item_ids = explode(",", $docs);
$response = [];
if (sizeof($item_ids) < 1) {
$this->fail(100, "One of the parameters specified was missing or invalid: docs is undefined");
}
foreach ($item_ids as $id) {
$splitted_id = explode("_", $id);
$doc = (new Documents())->getDocumentById((int) $splitted_id[0], (int) $splitted_id[1], $splitted_id[2]);
if (!$doc || $doc->isDeleted()) {
continue;
}
$response[] = $doc->toVkApiStruct($this->getUser(), $return_tags === 1);
}
return $response;
}
public function getTypes(?int $owner_id)
{
$this->requireUser();
if (!$owner_id) {
$owner_id = $this->getUser()->getId();
}
if ($owner_id > 0 && $owner_id != $this->getUser()->getId()) {
$this->fail(15, "Access denied");
}
$types = (new Documents())->getTypes($owner_id);
return [
"count" => sizeof($types),
"items" => $types,
];
}
public function getTags(?int $owner_id, ?int $type = 0)
{
$this->requireUser();
if (!$owner_id) {
$owner_id = $this->getUser()->getId();
}
if ($owner_id > 0 && $owner_id != $this->getUser()->getId()) {
$this->fail(15, "Access denied");
}
$tags = (new Documents())->getTags($owner_id, $type);
return $tags;
}
public function search(string $q = "", int $search_own = -1, int $order = -1, int $count = 30, int $offset = 0, int $return_tags = 0, int $type = 0, ?string $tags = null): object
{
$this->requireUser();
$params = [];
$o_order = ["type" => "id", "invert" => false];
if (iconv_strlen($q) > 512) {
$this->fail(100, "One of the parameters specified was missing or invalid: q should be not more 512 letters length");
}
if (in_array($type, [1,2,3,4,5,6,7,8])) {
$params["type"] = $type;
}
if (iconv_strlen($tags ?? "") < 512) {
$params["tags"] = $tags;
}
if ($search_own === 1) {
$params["from_me"] = $this->getUser()->getId();
}
$documents = (new Documents())->find($q, $params, $o_order);
$res = (object) [
"count" => $documents->size(),
"items" => [],
];
foreach ($documents->offsetLimit($offset, $count) as $doc) {
$res->items[] = $doc->toVkApiStruct($this->getUser(), $return_tags == 1);
}
return $res;
}
public function getUploadServer(?int $group_id = null)
{
$this->requireUser();
$this->willExecuteWriteAction();
return 0;
}
public function getWallUploadServer(?int $group_id = null)
{
$this->requireUser();
$this->willExecuteWriteAction();
return 0;
}
public function save(string $file, string $title, string $tags, ?int $return_tags = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();
return 0;
}
}

View file

@ -1,167 +1,195 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
final class Friends extends VKAPIRequestHandler final class Friends extends VKAPIRequestHandler
{ {
function get(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object public function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 100): object
{ {
$i = 0; $i = 0;
$offset++; $offset++;
$friends = []; $friends = [];
$users = new UsersRepo; $users = new UsersRepo();
$this->requireUser(); $this->requireUser();
foreach($users->get($user_id)->getFriends($offset, $count) as $friend) {
$friends[$i] = $friend->getId();
$i++;
}
$response = $friends; if ($user_id == 0) {
$user_id = $this->getUser()->getId();
}
$usersApi = new Users($this->getUser()); $user = $users->get($user_id);
if(!is_null($fields)) if (!$user || $user->isDeleted()) {
$response = $usersApi->get(implode(',', $friends), $fields, 0, $count); # FIXME $this->fail(100, "Invalid user");
}
return (object) [ if (!$user->getPrivacyPermission("friends.read", $this->getUser())) {
"count" => $users->get($user_id)->getFriendsCount(), $this->fail(15, "Access denied: this user chose to hide his friends.");
"items" => $response }
];
}
function getLists(): object foreach ($user->getFriends($offset, $count) as $friend) {
{ $friends[$i] = $friend->getId();
$this->requireUser(); $i++;
}
return (object) [ $response = $friends;
"count" => 0,
"items" => (array)[]
];
}
function deleteList(): int $usersApi = new Users($this->getUser());
{
$this->requireUser();
return 1; if (!is_null($fields)) {
} $response = $usersApi->get(implode(',', $friends), $fields, 0, $count);
} # FIXME
function edit(): int return (object) [
{ "count" => $users->get($user_id)->getFriendsCount(),
$this->requireUser(); "items" => $response,
];
}
return 1; public function getLists(): object
} {
$this->requireUser();
function editList(): int return (object) [
{ "count" => 0,
$this->requireUser(); "items" => (array) [],
];
}
return 1; public function deleteList(): int
} {
$this->requireUser();
function add(string $user_id): int return 1;
{ }
$this->requireUser();
public function edit(): int
{
$this->requireUser();
return 1;
}
public function editList(): int
{
$this->requireUser();
return 1;
}
public function add(string $user_id): int
{
$this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$users = new UsersRepo; $users = new UsersRepo();
$user = $users->get(intval($user_id)); $user = $users->get(intval($user_id));
if(is_null($user)) {
$this->fail(177, "Cannot add this user to friends as user not found");
} else if($user->getId() == $this->getUser()->getId()) {
$this->fail(174, "Cannot add user himself as friend");
}
switch($user->getSubscriptionStatus($this->getUser())) { if (is_null($user)) {
case 0: $this->fail(177, "Cannot add this user to friends as user not found");
$user->toggleSubscription($this->getUser()); } elseif ($user->getId() == $this->getUser()->getId()) {
return 1; $this->fail(174, "Cannot add user himself as friend");
}
case 1: switch ($user->getSubscriptionStatus($this->getUser())) {
$user->toggleSubscription($this->getUser()); case 0:
return 2; $user->toggleSubscription($this->getUser());
return 1;
case 3: case 1:
return 2; $user->toggleSubscription($this->getUser());
return 2;
default:
return 1;
}
}
function delete(string $user_id): int case 3:
{ return 2;
$this->requireUser();
default:
return 1;
}
}
public function delete(string $user_id): int
{
$this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$users = new UsersRepo; $users = new UsersRepo();
$user = $users->get(intval($user_id)); $user = $users->get(intval($user_id));
switch($user->getSubscriptionStatus($this->getUser())) { switch ($user->getSubscriptionStatus($this->getUser())) {
case 3: case 3:
$user->toggleSubscription($this->getUser()); $user->toggleSubscription($this->getUser());
return 1; return 1;
default:
$this->fail(15, "Access denied: No friend or friend request found.");
}
}
function areFriends(string $user_ids): array default:
{ $this->fail(15, "Access denied: No friend or friend request found.");
$this->requireUser(); }
}
$users = new UsersRepo; public function areFriends(string $user_ids): array
{
$this->requireUser();
$friends = explode(',', $user_ids); $users = new UsersRepo();
$response = []; $friends = explode(',', $user_ids);
for($i=0; $i < sizeof($friends); $i++) { $response = [];
$friend = $users->get(intval($friends[$i]));
$response[] = (object)[ for ($i = 0; $i < sizeof($friends); $i++) {
"friend_status" => $friend->getSubscriptionStatus($this->getUser()), $friend = $users->get(intval($friends[$i]));
"user_id" => $friend->getId()
];
}
return $response; $response[] = (object) [
} "friend_status" => $friend->getSubscriptionStatus($this->getUser()),
"user_id" => $friend->getId(),
];
}
function getRequests(string $fields = "", int $offset = 0, int $count = 100, int $extended = 0): object return $response;
{ }
if ($count >= 1000)
$this->fail(100, "One of the required parameters was not passed or is invalid.");
$this->requireUser(); public function getRequests(string $fields = "", int $out = 0, int $offset = 0, int $count = 100, int $extended = 0): object
{
if ($count >= 1000) {
$this->fail(100, "One of the required parameters was not passed or is invalid.");
}
$i = 0; $this->requireUser();
$offset++;
$followers = [];
foreach($this->getUser()->getFollowers($offset, $count) as $follower) { $i = 0;
$followers[$i] = $follower->getId(); $offset++;
$i++; $followers = [];
}
$response = $followers; if ($out != 0) {
$usersApi = new Users($this->getUser()); foreach ($this->getUser()->getFollowers($offset, $count) as $follower) {
$followers[$i] = $follower->getId();
$i++;
}
} else {
foreach ($this->getUser()->getRequests($offset, $count) as $follower) {
$followers[$i] = $follower->getId();
$i++;
}
}
$response = $usersApi->get(implode(',', $followers), $fields, 0, $count); $response = $followers;
$usersApi = new Users($this->getUser());
foreach($response as $user) $response = $usersApi->get(implode(',', $followers), $fields, 0, $count);
$user->user_id = $user->id;
return (object) [ foreach ($response as $user) {
"count" => $this->getUser()->getFollowersCount(), $user->user_id = $user->id;
"items" => $response }
];
} return (object) [
"count" => $this->getUser()->getFollowersCount(),
"items" => $response,
];
}
} }

View file

@ -1,90 +1,121 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Gifts as GiftsRepo; use openvk\Web\Models\Repositories\Gifts as GiftsRepo;
use openvk\Web\Models\Entities\Notifications\GiftNotification; use openvk\Web\Models\Entities\Notifications\GiftNotification;
final class Gifts extends VKAPIRequestHandler final class Gifts extends VKAPIRequestHandler
{ {
function get(int $user_id, int $count = 10, int $offset = 0) public function get(int $user_id = null, int $count = 10, int $offset = 0)
{ {
$this->requireUser(); $this->requireUser();
$i = 0; $i = 0;
$i += $offset; $i += $offset;
$server_url = ovk_scheme(true) . $_SERVER["HTTP_HOST"];
$user = (new UsersRepo)->get($user_id); if ($user_id) {
$user = (new UsersRepo())->get($user_id);
} else {
$user = $this->getUser();
}
if(!$user || $user->isDeleted()) if (!$user || $user->isDeleted()) {
$this->fail(177, "Invalid user"); $this->fail(177, "Invalid user");
}
if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
/*
if(!$user->getPrivacyPermission('gifts.read', $this->getUser()))
$this->fail(15, "Access denied: this user chose to hide his gifts");*/
if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
$gift_item = []; $gift_item = [];
$userGifts = array_slice(iterator_to_array($user->getGifts(1, $count, false)), $offset); $userGifts = array_slice(iterator_to_array($user->getGifts(1, $count, false)), $offset);
if(sizeof($userGifts) < 0) { if (sizeof($userGifts) < 0) {
return NULL; return null;
} }
foreach($userGifts as $gift) { foreach ($userGifts as $gift) {
if($i < $count) { if ($i < $count) {
$gift_item[] = [ $gift_item[] = [
"id" => $i, "id" => $i,
"from_id" => $gift->anon == true ? 0 : $gift->sender->getId(), "from_id" => $gift->anon == true ? 0 : $gift->sender->getId(),
"message" => $gift->caption == NULL ? "" : $gift->caption, "message" => $gift->caption == null ? "" : $gift->caption,
"date" => $gift->sent->timestamp(), "date" => $gift->sent->timestamp(),
"gift" => [ "gift" => [
"id" => $gift->gift->getId(), "id" => $gift->gift->getId(),
"thumb_256" => $gift->gift->getImage(2), "thumb_256" => $server_url . $gift->gift->getImage(2),
"thumb_96" => $gift->gift->getImage(2), "thumb_96" => $server_url . $gift->gift->getImage(2),
"thumb_48" => $gift->gift->getImage(2) "thumb_48" => $server_url . $gift->gift->getImage(2),
], ],
"privacy" => 0 "privacy" => 0,
]; ];
} }
$i+=1; $i += 1;
} }
return $gift_item; return $gift_item;
} }
function send(int $user_ids, int $gift_id, string $message = "", int $privacy = 0) public function send(int $user_ids, int $gift_id, string $message = "", int $privacy = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$user = (new UsersRepo)->get((int) $user_ids); $user = (new UsersRepo())->get((int) $user_ids);
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) if (!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) {
$this->fail(105, "Commerce is disabled on this instance"); $this->fail(105, "Commerce is disabled on this instance");
}
if(!$user || $user->isDeleted())
if (!$user || $user->isDeleted()) {
$this->fail(177, "Invalid user"); $this->fail(177, "Invalid user");
}
$gift = (new GiftsRepo)->get($gift_id); if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
if(!$gift) $gift = (new GiftsRepo())->get($gift_id);
if (!$gift) {
$this->fail(165, "Invalid gift"); $this->fail(165, "Invalid gift");
}
$price = $gift->getPrice(); $price = $gift->getPrice();
$coinsLeft = $this->getUser()->getCoins() - $price; $coinsLeft = $this->getUser()->getCoins() - $price;
if(!$gift->canUse($this->getUser())) if (!$gift->canUse($this->getUser())) {
return (object) return (object)
[ [
"success" => 0, "success" => 0,
"user_ids" => $user_ids, "user_ids" => $user_ids,
"error" => "You don't have any more of these gifts." "error" => "You don't have any more of these gifts.",
]; ];
}
if($coinsLeft < 0) if ($coinsLeft < 0) {
return (object) return (object)
[ [
"success" => 0, "success" => 0,
"user_ids" => $user_ids, "user_ids" => $user_ids,
"error" => "You don't have enough voices." "error" => "You don't have enough voices.",
]; ];
}
$user->gift($this->getUser(), $gift, $message); $user->gift($this->getUser(), $gift, $message);
$gift->used(); $gift->used();
@ -99,11 +130,11 @@ final class Gifts extends VKAPIRequestHandler
[ [
"success" => 1, "success" => 1,
"user_ids" => $user_ids, "user_ids" => $user_ids,
"withdraw_votes" => $price "withdraw_votes" => $price,
]; ];
} }
function delete() public function delete()
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
@ -111,27 +142,29 @@ final class Gifts extends VKAPIRequestHandler
$this->fail(501, "Not implemented"); $this->fail(501, "Not implemented");
} }
# этих методов не было в ВК, но я их добавил чтобы можно было отобразить список подарков # в vk кстати называется gifts.getCatalog
function getCategories(bool $extended = false, int $page = 1) public function getCategories(bool $extended = false, int $page = 1)
{ {
$cats = (new GiftsRepo)->getCategories($page); $cats = (new GiftsRepo())->getCategories($page);
$categ = []; $categ = [];
$i = 0; $i = 0;
$server_url = ovk_scheme(true) . $_SERVER["HTTP_HOST"];
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) if (!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) {
$this->fail(105, "Commerce is disabled on this instance"); $this->fail(105, "Commerce is disabled on this instance");
}
foreach($cats as $cat) { foreach ($cats as $cat) {
$categ[$i] = [ $categ[$i] = [
"name" => $cat->getName(), "name" => $cat->getName(),
"description" => $cat->getDescription(), "description" => $cat->getDescription(),
"id" => $cat->getId(), "id" => $cat->getId(),
"thumbnail" => $cat->getThumbnailURL(), "thumbnail" => $server_url . $cat->getThumbnailURL(),
]; ];
if($extended == true) { if ($extended == true) {
$categ[$i]["localizations"] = []; $categ[$i]["localizations"] = [];
foreach(getLanguages() as $lang) { foreach (getLanguages() as $lang) {
$code = $lang["code"]; $code = $lang["code"];
$categ[$i]["localizations"][$code] = $categ[$i]["localizations"][$code] =
[ [
@ -142,30 +175,32 @@ final class Gifts extends VKAPIRequestHandler
} }
$i++; $i++;
} }
return $categ; return $categ;
} }
function getGiftsInCategory(int $id, int $page = 1) public function getGiftsInCategory(int $id, int $page = 1)
{ {
$this->requireUser(); $this->requireUser();
if(!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) if (!OPENVK_ROOT_CONF['openvk']['preferences']['commerce']) {
$this->fail(105, "Commerce is disabled on this instance"); $this->fail(105, "Commerce is disabled on this instance");
}
if(!(new GiftsRepo)->getCat($id)) if (!(new GiftsRepo())->getCat($id)) {
$this->fail(177, "Category not found"); $this->fail(177, "Category not found");
}
$giftz = ((new GiftsRepo)->getCat($id))->getGifts($page); $giftz = ((new GiftsRepo())->getCat($id))->getGifts($page);
$gifts = []; $gifts = [];
foreach($giftz as $gift) { foreach ($giftz as $gift) {
$gifts[] = [ $gifts[] = [
"name" => $gift->getName(), "name" => $gift->getName(),
"image" => $gift->getImage(2), "image" => $gift->getImage(2),
"usages_left" => (int)$gift->getUsagesLeft($this->getUser()), "usages_left" => (int) $gift->getUsagesLeft($this->getUser()),
"price" => $gift->getPrice(), # голосов "price" => $gift->getPrice(),
"is_free" => $gift->isFree() "is_free" => $gift->isFree(),
]; ];
} }

View file

@ -1,42 +1,61 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Clubs as ClubsRepo; use openvk\Web\Models\Repositories\Clubs as ClubsRepo;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
use openvk\Web\Models\Entities\Club; use openvk\Web\Models\Entities\Club;
final class Groups extends VKAPIRequestHandler final class Groups extends VKAPIRequestHandler
{ {
function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false): object public function get(int $user_id = 0, string $fields = "", int $offset = 0, int $count = 6, bool $online = false, string $filter = "groups"): object
{ {
$this->requireUser(); $this->requireUser();
if($user_id == 0) { # InfoApp fix
foreach($this->getUser()->getClubs($offset, false, $count, true) as $club) if ($filter == "admin" && ($user_id != 0 && $user_id != $this->getUser()->getId())) {
$clbs[] = $club; $this->fail(15, 'Access denied: filter admin is available only for current user');
$clbsCount = $this->getUser()->getClubCount();
} else {
$users = new UsersRepo;
$user = $users->get($user_id);
if(is_null($user))
$this->fail(15, "Access denied");
foreach($user->getClubs($offset, false, $count, true) as $club)
$clbs[] = $club;
$clbsCount = $user->getClubCount();
} }
$clbs = [];
if ($user_id == 0) {
foreach ($this->getUser()->getClubs($offset, $filter == "admin", $count, true) as $club) {
$clbs[] = $club;
}
$clbsCount = $this->getUser()->getClubCount();
} else {
$users = new UsersRepo();
$user = $users->get($user_id);
if (is_null($user) || $user->isDeleted()) {
$this->fail(15, "Access denied");
}
if (!$user->getPrivacyPermission('groups.read', $this->getUser())) {
$this->fail(15, "Access denied: this user chose to hide his groups.");
}
foreach ($user->getClubs($offset, $filter == "admin", $count, true) as $club) {
$clbs[] = $club;
}
$clbsCount = $user->getClubCount();
}
$rClubs; $rClubs;
$ic = sizeof($clbs); $ic = sizeof($clbs);
if(sizeof($clbs) > $count) if (sizeof($clbs) > $count) {
$ic = $count; $ic = $count;
}
if(!empty($clbs)) { if (!empty($clbs)) {
for($i=0; $i < $ic; $i++) { for ($i = 0; $i < $ic; $i++) {
$usr = $clbs[$i]; $usr = $clbs[$i];
if(is_null($usr)) { if (is_null($usr)) {
} else { } else {
$rClubs[$i] = (object) [ $rClubs[$i] = (object) [
@ -49,8 +68,8 @@ final class Groups extends VKAPIRequestHandler
$flds = explode(',', $fields); $flds = explode(',', $fields);
foreach($flds as $field) { foreach ($flds as $field) {
switch($field) { switch ($field) {
case "verified": case "verified":
$rClubs[$i]->verified = intval($usr->isVerified()); $rClubs[$i]->verified = intval($usr->isVerified());
break; break;
@ -80,6 +99,23 @@ final class Groups extends VKAPIRequestHandler
break; break;
case "members_count": case "members_count":
$rClubs[$i]->members_count = $usr->getFollowersCount(); $rClubs[$i]->members_count = $usr->getFollowersCount();
break;
case "can_suggest":
$rClubs[$i]->can_suggest = !$usr->canBeModifiedBy($this->getUser()) && $usr->getWallType() == 2;
break;
case "background":
$backgrounds = $usr->getBackDropPictureURLs();
$rClubs[$i]->background = $backgrounds;
break;
# unstandard feild
case "suggested_count":
if ($usr->getWallType() != 2) {
$rClubs[$i]->suggested_count = null;
break;
}
$rClubs[$i]->suggested_count = $usr->getSuggestedPostsCount($this->getUser());
break; break;
} }
} }
@ -90,58 +126,63 @@ final class Groups extends VKAPIRequestHandler
} }
return (object) [ return (object) [
"count" => $clbsCount, "count" => $clbsCount,
"items" => $rClubs "items" => $rClubs,
]; ];
} }
function getById(string $group_ids = "", string $group_id = "", string $fields = "", int $offset = 0, int $count = 500): ?array public function getById(string $group_ids = "", string $group_id = "", string $fields = "", int $offset = 0, int $count = 500): ?array
{ {
/* Both offset and count SHOULD be used only in OpenVK code, /* Both offset and count SHOULD be used only in OpenVK code,
not in your app or script, since it's not oficially documented by VK */ not in your app or script, since it's not oficially documented by VK */
$clubs = new ClubsRepo; $clubs = new ClubsRepo();
if(empty($group_ids) && !empty($group_id)) if (empty($group_ids) && !empty($group_id)) {
$group_ids = $group_id; $group_ids = $group_id;
}
if(empty($group_ids) && empty($group_id))
if (empty($group_ids) && empty($group_id)) {
$this->fail(100, "One of the parameters specified was missing or invalid: group_ids is undefined"); $this->fail(100, "One of the parameters specified was missing or invalid: group_ids is undefined");
}
$clbs = explode(',', $group_ids); $clbs = explode(',', $group_ids);
$response = array(); $response = [];
$ic = sizeof($clbs); $ic = sizeof($clbs);
if(sizeof($clbs) > $count) if (sizeof($clbs) > $count) {
$ic = $count; $ic = $count;
}
$clbs = array_slice($clbs, $offset * $count); $clbs = array_slice($clbs, $offset * $count);
for($i=0; $i < $ic; $i++) { for ($i = 0; $i < $ic; $i++) {
if($i > 500 || $clbs[$i] == 0) if ($i > 500 || $clbs[$i] == 0) {
break; break;
}
if($clbs[$i] < 0) if ($clbs[$i] < 0) {
$this->fail(100, "ты ошибся чутка, у айди группы убери минус"); $this->fail(100, "ты ошибся чутка, у айди группы убери минус");
}
$clb = $clubs->get((int) $clbs[$i]); $clb = $clubs->get((int) $clbs[$i]);
if(is_null($clb)) { if (is_null($clb)) {
$response[$i] = (object)[ $response[$i] = (object) [
"id" => intval($clbs[$i]), "id" => intval($clbs[$i]),
"name" => "DELETED", "name" => "DELETED",
"screen_name" => "club".intval($clbs[$i]), "screen_name" => "club" . intval($clbs[$i]),
"type" => "group", "type" => "group",
"description" => "This group was deleted or it doesn't exist" "description" => "This group was deleted or it doesn't exist",
]; ];
} else if($clbs[$i] == NULL) { } elseif ($clbs[$i] == null) {
} else { } else {
$response[$i] = (object)[ $response[$i] = (object) [
"id" => $clb->getId(), "id" => $clb->getId(),
"name" => $clb->getName(), "name" => $clb->getName(),
"screen_name" => $clb->getShortCode() ?? "club".$clb->getId(), "screen_name" => $clb->getShortCode() ?? "club" . $clb->getId(),
"is_closed" => false, "is_closed" => false,
"type" => "group", "type" => "group",
"is_member" => !is_null($this->getUser()) ? (int) $clb->getSubscriptionStatus($this->getUser()) : 0, "is_member" => !is_null($this->getUser()) ? (int) $clb->getSubscriptionStatus($this->getUser()) : 0,
@ -150,20 +191,20 @@ final class Groups extends VKAPIRequestHandler
$flds = explode(',', $fields); $flds = explode(',', $fields);
foreach($flds as $field) { foreach ($flds as $field) {
switch($field) { switch ($field) {
case "verified": case "verified":
$response[$i]->verified = intval($clb->isVerified()); $response[$i]->verified = intval($clb->isVerified());
break; break;
case "has_photo": case "has_photo":
$response[$i]->has_photo = is_null($clb->getAvatarPhoto()) ? 0 : 1; $response[$i]->has_photo = is_null($clb->getAvatarPhoto()) ? 0 : 1;
break; break;
case "photo_max_orig": case "photo_max_orig":
$response[$i]->photo_max_orig = $clb->getAvatarURL(); $response[$i]->photo_max_orig = $clb->getAvatarURL();
break; break;
case "photo_max": case "photo_max":
$response[$i]->photo_max = $clb->getAvatarURL(); $response[$i]->photo_max = $clb->getAvatarURL();
break; break;
case "photo_50": case "photo_50":
$response[$i]->photo_50 = $clb->getAvatarURL(); $response[$i]->photo_50 = $clb->getAvatarURL();
break; break;
@ -179,33 +220,52 @@ final class Groups extends VKAPIRequestHandler
case "photo_400_orig": case "photo_400_orig":
$response[$i]->photo_400_orig = $clb->getAvatarURL("normal"); $response[$i]->photo_400_orig = $clb->getAvatarURL("normal");
break; break;
case "members_count": case "members_count":
$response[$i]->members_count = $clb->getFollowersCount(); $response[$i]->members_count = $clb->getFollowersCount();
break;
case "site":
$response[$i]->site = $clb->getWebsite();
break;
case "description":
$response[$i]->description = $clb->getDescription();
break; break;
case "contacts": case "site":
$response[$i]->site = $clb->getWebsite();
break;
case "description":
$response[$i]->description = $clb->getDescription();
break;
case "can_suggest":
$response[$i]->can_suggest = !$clb->canBeModifiedBy($this->getUser()) && $clb->getWallType() == 2;
break;
case "background":
$backgrounds = $clb->getBackDropPictureURLs();
$response[$i]->background = $backgrounds;
break;
# unstandard feild
case "suggested_count":
if ($clb->getWallType() != 2) {
$response[$i]->suggested_count = null;
break;
}
$response[$i]->suggested_count = $clb->getSuggestedPostsCount($this->getUser());
break;
case "contacts":
$contacts; $contacts;
$contactTmp = $clb->getManagers(1, true); $contactTmp = $clb->getManagers(1, true);
foreach($contactTmp as $contact) foreach ($contactTmp as $contact) {
$contacts[] = array( $contacts[] = [
"user_id" => $contact->getUser()->getId(), "user_id" => $contact->getUser()->getId(),
"desc" => $contact->getComment() "desc" => $contact->getComment(),
); ];
}
$response[$i]->contacts = $contacts; $response[$i]->contacts = $contacts;
break; break;
case "can_post": case "can_post":
if(!is_null($this->getUser())) if (!is_null($this->getUser())) {
if($clb->canBeModifiedBy($this->getUser())) if ($clb->canBeModifiedBy($this->getUser())) {
$response[$i]->can_post = true; $response[$i]->can_post = true;
else } else {
$response[$i]->can_post = $clb->canPost(); $response[$i]->can_post = $clb->canPost();
}
}
break; break;
} }
} }
@ -215,62 +275,72 @@ final class Groups extends VKAPIRequestHandler
return $response; return $response;
} }
function search(string $q, int $offset = 0, int $count = 100) public function search(string $q, int $offset = 0, int $count = 100, string $fields = "screen_name,is_admin,is_member,is_advertiser,photo_50,photo_100,photo_200")
{ {
$clubs = new ClubsRepo; if ($count > 100) {
$this->fail(100, "One of the parameters specified was missing or invalid: count should be less or equal to 100");
$array = []; }
$find = $clubs->find($q);
foreach ($find as $group) $clubs = new ClubsRepo();
$array = [];
$find = $clubs->find($q);
foreach ($find->offsetLimit($offset, $count) as $group) {
$array[] = $group->getId(); $array[] = $group->getId();
}
if (!$array || sizeof($array) < 1) {
return (object) [
"count" => 0,
"items" => [],
];
}
return (object) [ return (object) [
"count" => $find->size(), "count" => $find->size(),
"items" => $this->getById(implode(',', $array), "", "is_admin,is_member,is_advertiser,photo_50,photo_100,photo_200", $offset, $count) "items" => $this->getById(implode(',', $array), "", $fields),
/*
* As there is no thing as "fields" by the original documentation
* i'll just bake this param by the example shown here: https://dev.vk.com/method/groups.search
*/
]; ];
} }
function join(int $group_id) public function join(int $group_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$club = (new ClubsRepo)->get($group_id); $club = (new ClubsRepo())->get($group_id);
$isMember = !is_null($this->getUser()) ? (int) $club->getSubscriptionStatus($this->getUser()) : 0; $isMember = !is_null($this->getUser()) ? (int) $club->getSubscriptionStatus($this->getUser()) : 0;
if($isMember == 0) if ($isMember == 0) {
$club->toggleSubscription($this->getUser()); $club->toggleSubscription($this->getUser());
}
return 1; return 1;
} }
function leave(int $group_id) public function leave(int $group_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$club = (new ClubsRepo)->get($group_id); $club = (new ClubsRepo())->get($group_id);
$isMember = !is_null($this->getUser()) ? (int) $club->getSubscriptionStatus($this->getUser()) : 0; $isMember = !is_null($this->getUser()) ? (int) $club->getSubscriptionStatus($this->getUser()) : 0;
if($isMember == 1) if ($isMember == 1) {
$club->toggleSubscription($this->getUser()); $club->toggleSubscription($this->getUser());
}
return 1; return 1;
} }
function create(string $title, string $description = "", string $type = "group", int $public_category = 1, int $public_subcategory = 1, int $subtype = 1) public function create(string $title, string $description = "", string $type = "group", int $public_category = 1, int $public_subcategory = 1, int $subtype = 1)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$club = new Club; $club = new Club();
$club->setName($title); $club->setName($title);
$club->setAbout($description); $club->setAbout($description);
@ -279,55 +349,80 @@ final class Groups extends VKAPIRequestHandler
$club->toggleSubscription($this->getUser()); $club->toggleSubscription($this->getUser());
return $this->getById((string)$club->getId()); return $this->getById((string) $club->getId());
} }
function edit( public function edit(
int $group_id, int $group_id,
string $title = NULL, string $title = null,
string $description = NULL, string $description = null,
string $screen_name = NULL, string $screen_name = null,
string $website = NULL, string $website = null,
int $wall = NULL, int $wall = -1,
int $topics = NULL, int $topics = null,
int $adminlist = NULL, int $adminlist = null,
int $topicsAboveWall = NULL, int $topicsAboveWall = null,
int $hideFromGlobalFeed = NULL) int $hideFromGlobalFeed = null,
{ int $audio = null
) {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$club = (new ClubsRepo)->get($group_id); $club = (new ClubsRepo())->get($group_id);
if(!$club) $this->fail(203, "Club not found"); if (!$club) {
if(!$club || !$club->canBeModifiedBy($this->getUser())) $this->fail(15, "You can't modify this group."); $this->fail(203, "Club not found");
if(!empty($screen_name) && !$club->setShortcode($screen_name)) $this->fail(103, "Invalid shortcode."); }
if (!$club || !$club->canBeModifiedBy($this->getUser())) {
$this->fail(15, "You can't modify this group.");
}
if (!empty($screen_name) && !$club->setShortcode($screen_name)) {
$this->fail(103, "Invalid shortcode.");
}
!is_null($title) ? $club->setName($title) : NULL; !empty($title) ? $club->setName($title) : null;
!is_null($description) ? $club->setAbout($description) : NULL; !empty($description) ? $club->setAbout($description) : null;
!is_null($screen_name) ? $club->setShortcode($screen_name) : NULL; !empty($screen_name) ? $club->setShortcode($screen_name) : null;
!is_null($website) ? $club->setWebsite((!parse_url($website, PHP_URL_SCHEME) ? "https://" : "") . $website) : NULL; !empty($website) ? $club->setWebsite((!parse_url($website, PHP_URL_SCHEME) ? "https://" : "") . $website) : null;
!is_null($wall) ? $club->setWall($wall) : NULL;
!is_null($topics) ? $club->setEveryone_Can_Create_Topics($topics) : NULL;
!is_null($adminlist) ? $club->setAdministrators_List_Display($adminlist) : NULL;
!is_null($topicsAboveWall) ? $club->setDisplay_Topics_Above_Wall($topicsAboveWall) : NULL;
!is_null($hideFromGlobalFeed) ? $club->setHide_From_Global_Feed($hideFromGlobalFeed) : NULL;
$club->save(); try {
$wall != -1 ? $club->setWall($wall) : null;
} catch (\Exception $e) {
$this->fail(50, "Invalid wall value");
}
!empty($topics) ? $club->setEveryone_Can_Create_Topics($topics) : null;
!empty($adminlist) ? $club->setAdministrators_List_Display($adminlist) : null;
!empty($topicsAboveWall) ? $club->setDisplay_Topics_Above_Wall($topicsAboveWall) : null;
if (!$club->isHidingFromGlobalFeedEnforced()) {
!empty($hideFromGlobalFeed) ? $club->setHide_From_Global_Feed($hideFromGlobalFeed) : null;
}
in_array($audio, [0, 1]) ? $club->setEveryone_can_upload_audios($audio) : null;
try {
$club->save();
} catch (\TypeError $e) {
$this->fail(15, "Nothing changed");
} catch (\Exception $e) {
$this->fail(18, "An unknown error occurred: maybe you set an incorrect value?");
}
return 1; return 1;
} }
function getMembers(string $group_id, string $sort = "id_asc", int $offset = 0, int $count = 100, string $fields = "", string $filter = "any") public function getMembers(string $group_id, string $sort = "id_asc", int $offset = 0, int $count = 100, string $fields = "", string $filter = "any")
{ {
# bdate,can_post,can_see_all_posts,can_see_audio,can_write_private_message,city,common_count,connections,contacts,country,domain,education,has_mobile,last_seen,lists,online,online_mobile,photo_100,photo_200,photo_200_orig,photo_400_orig,photo_50,photo_max,photo_max_orig,relation,relatives,schools,sex,site,status,universities # bdate,can_post,can_see_all_posts,can_see_audio,can_write_private_message,city,common_count,connections,contacts,country,domain,education,has_mobile,last_seen,lists,online,online_mobile,photo_100,photo_200,photo_200_orig,photo_400_orig,photo_50,photo_max,photo_max_orig,relation,relatives,schools,sex,site,status,universities
$club = (new ClubsRepo)->get((int) $group_id); $club = (new ClubsRepo())->get((int) $group_id);
if(!$club) if (!$club) {
$this->fail(125, "Invalid group id"); $this->fail(125, "Invalid group id");
}
$sorter = "follower ASC"; $sorter = "follower ASC";
switch($sort) { switch ($sort) {
default: default:
case "time_asc": case "time_asc":
case "id_asc": case "id_asc":
@ -341,14 +436,14 @@ final class Groups extends VKAPIRequestHandler
$members = array_slice(iterator_to_array($club->getFollowers(1, $count, $sorter)), $offset); $members = array_slice(iterator_to_array($club->getFollowers(1, $count, $sorter)), $offset);
$arr = (object) [ $arr = (object) [
"count" => count($members), "count" => count($members),
"items" => array()]; "items" => []];
$filds = explode(",", $fields); $filds = explode(",", $fields);
$i = 0; $i = 0;
foreach($members as $member) { foreach ($members as $member) {
if($i > $count) { if ($i > $count) {
break; break;
} }
@ -358,10 +453,16 @@ final class Groups extends VKAPIRequestHandler
"last_name" => $member->getLastName(), "last_name" => $member->getLastName(),
]; ];
foreach($filds as $fild) { foreach ($filds as $fild) {
switch($fild) { $canView = $member->canBeViewedBy($this->getUser());
switch ($fild) {
case "bdate": case "bdate":
$arr->items[$i]->bdate = $member->getBirthday()->format('%e.%m.%Y'); if (!$canView) {
$arr->items[$i]->bdate = "01.01.1970";
break;
}
$arr->items[$i]->bdate = $member->getBirthday() ? $member->getBirthday()->format('%e.%m.%Y') : null;
break; break;
case "can_post": case "can_post":
$arr->items[$i]->can_post = $club->canBeModifiedBy($member); $arr->items[$i]->can_post = $club->canBeModifiedBy($member);
@ -370,7 +471,7 @@ final class Groups extends VKAPIRequestHandler
$arr->items[$i]->can_see_all_posts = 1; $arr->items[$i]->can_see_all_posts = 1;
break; break;
case "can_see_audio": case "can_see_audio":
$arr->items[$i]->can_see_audio = 0; $arr->items[$i]->can_see_audio = 1;
break; break;
case "can_write_private_message": case "can_write_private_message":
$arr->items[$i]->can_write_private_message = 0; $arr->items[$i]->can_write_private_message = 0;
@ -382,6 +483,11 @@ final class Groups extends VKAPIRequestHandler
$arr->items[$i]->connections = 1; $arr->items[$i]->connections = 1;
break; break;
case "contacts": case "contacts":
if (!$canView) {
$arr->items[$i]->contacts = "secret@gmail.com";
break;
}
$arr->items[$i]->contacts = $member->getContactEmail(); $arr->items[$i]->contacts = $member->getContactEmail();
break; break;
case "country": case "country":
@ -397,15 +503,30 @@ final class Groups extends VKAPIRequestHandler
$arr->items[$i]->has_mobile = false; $arr->items[$i]->has_mobile = false;
break; break;
case "last_seen": case "last_seen":
if (!$canView) {
$arr->items[$i]->last_seen = 0;
break;
}
$arr->items[$i]->last_seen = $member->getOnline()->timestamp(); $arr->items[$i]->last_seen = $member->getOnline()->timestamp();
break; break;
case "lists": case "lists":
$arr->items[$i]->lists = ""; $arr->items[$i]->lists = "";
break; break;
case "online": case "online":
if (!$canView) {
$arr->items[$i]->online = false;
break;
}
$arr->items[$i]->online = $member->isOnline(); $arr->items[$i]->online = $member->isOnline();
break; break;
case "online_mobile": case "online_mobile":
if (!$canView) {
$arr->items[$i]->online_mobile = false;
break;
}
$arr->items[$i]->online_mobile = $member->getOnlinePlatform() == "android" || $member->getOnlinePlatform() == "iphone" || $member->getOnlinePlatform() == "mobile"; $arr->items[$i]->online_mobile = $member->getOnlinePlatform() == "android" || $member->getOnlinePlatform() == "iphone" || $member->getOnlinePlatform() == "mobile";
break; break;
case "photo_100": case "photo_100":
@ -436,12 +557,27 @@ final class Groups extends VKAPIRequestHandler
$arr->items[$i]->schools = 0; $arr->items[$i]->schools = 0;
break; break;
case "sex": case "sex":
if (!$canView) {
$arr->items[$i]->sex = -1;
break;
}
$arr->items[$i]->sex = $member->isFemale() ? 1 : 2; $arr->items[$i]->sex = $member->isFemale() ? 1 : 2;
break; break;
case "site": case "site":
if (!$canView) {
$arr->items[$i]->site = null;
break;
}
$arr->items[$i]->site = $member->getWebsite(); $arr->items[$i]->site = $member->getWebsite();
break; break;
case "status": case "status":
if (!$canView) {
$arr->items[$i]->status = "r";
break;
}
$arr->items[$i]->status = $member->getStatus(); $arr->items[$i]->status = $member->getStatus();
break; break;
case "universities": case "universities":
@ -454,22 +590,23 @@ final class Groups extends VKAPIRequestHandler
return $arr; return $arr;
} }
function getSettings(string $group_id) public function getSettings(string $group_id)
{ {
$this->requireUser(); $this->requireUser();
$club = (new ClubsRepo)->get((int)$group_id); $club = (new ClubsRepo())->get((int) $group_id);
if(!$club || !$club->canBeModifiedBy($this->getUser())) if (!$club || !$club->canBeModifiedBy($this->getUser())) {
$this->fail(15, "You can't get settings of this group."); $this->fail(15, "You can't get settings of this group.");
}
$arr = (object) [ $arr = (object) [
"title" => $club->getName(), "title" => $club->getName(),
"description" => $club->getDescription() != NULL ? $club->getDescription() : "", "description" => $club->getDescription() != null ? $club->getDescription() : "",
"address" => $club->getShortcode(), "address" => $club->getShortcode(),
"wall" => $club->canPost() == true ? 1 : 0, "wall" => $club->getWallType(), # отличается от вкшных но да ладно
"photos" => 1, "photos" => 1,
"video" => 0, "video" => 0,
"audio" => 0, "audio" => $club->isEveryoneCanUploadAudios() ? 1 : 0,
"docs" => 0, "docs" => 0,
"topics" => $club->isEveryoneCanCreateTopics() == true ? 1 : 0, "topics" => $club->isEveryoneCanCreateTopics() == true ? 1 : 0,
"wiki" => 0, "wiki" => 0,
@ -480,13 +617,13 @@ final class Groups extends VKAPIRequestHandler
"access" => 1, "access" => 1,
"subject" => 1, "subject" => 1,
"subject_list" => [ "subject_list" => [
0 => "в", 0 => "в",
1 => "опенвк", 1 => "опенвк",
2 => "нет", 2 => "нет",
3 => "категорий", 3 => "категорий",
4 => "групп", 4 => "групп",
], ],
"rss" => "/club".$club->getId()."/rss", "rss" => "/club" . $club->getId() . "/rss",
"website" => $club->getWebsite(), "website" => $club->getWebsite(),
"age_limits" => 0, "age_limits" => 0,
"market" => [], "market" => [],
@ -495,24 +632,27 @@ final class Groups extends VKAPIRequestHandler
return $arr; return $arr;
} }
function isMember(string $group_id, int $user_id, string $user_ids = "", bool $extended = false) public function isMember(string $group_id, int $user_id, string $user_ids = "", bool $extended = false)
{ {
$this->requireUser(); $this->requireUser();
$id = $user_id != NULL ? $user_id : explode(",", $user_ids); $id = $user_id != null ? $user_id : explode(",", $user_ids);
if($group_id < 0) if ($group_id < 0) {
$this->fail(228, "Remove the minus from group_id"); $this->fail(228, "Remove the minus from group_id");
}
$club = (new ClubsRepo)->get((int)$group_id); $club = (new ClubsRepo())->get((int) $group_id);
$usver = (new UsersRepo)->get((int)$id); $usver = (new UsersRepo())->get((int) $id);
if(!$club || $group_id == 0) if (!$club || $group_id == 0) {
$this->fail(203, "Invalid club"); $this->fail(203, "Invalid club");
}
if(!$usver || $usver->isDeleted() || $user_id == 0) if (!$usver || $usver->isDeleted() || $user_id == 0) {
$this->fail(30, "Invalid user"); $this->fail(30, "Invalid user");
}
if($extended == false) { if ($extended == false) {
return $club->getSubscriptionStatus($usver) ? 1 : 0; return $club->getSubscriptionStatus($usver) ? 1 : 0;
} else { } else {
return (object) return (object)
@ -521,12 +661,12 @@ final class Groups extends VKAPIRequestHandler
"request" => 0, "request" => 0,
"invitation" => 0, "invitation" => 0,
"can_invite" => 0, "can_invite" => 0,
"can_recall" => 0 "can_recall" => 0,
]; ];
} }
} }
function remove(int $group_id, int $user_id) public function remove(int $group_id, int $user_id)
{ {
$this->requireUser(); $this->requireUser();

View file

@ -1,71 +1,217 @@
<?php declare(strict_types=1); <?php
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Users as UsersRepo; declare(strict_types=1);
use openvk\Web\Models\Repositories\Posts as PostsRepo;
namespace openvk\VKAPI\Handlers;
final class Likes extends VKAPIRequestHandler
{ use openvk\Web\Models\Repositories\Users as UsersRepo;
function add(string $type, int $owner_id, int $item_id): object use openvk\Web\Models\Repositories\Posts as PostsRepo;
{ use openvk\Web\Models\Repositories\Comments as CommentsRepo;
$this->requireUser(); use openvk\Web\Models\Repositories\Videos as VideosRepo;
$this->willExecuteWriteAction(); use openvk\Web\Models\Repositories\Photos as PhotosRepo;
use openvk\Web\Models\Repositories\Notes as NotesRepo;
switch($type) {
case "post": final class Likes extends VKAPIRequestHandler
$post = (new PostsRepo)->getPostById($owner_id, $item_id); {
if(is_null($post)) public function add(string $type, int $owner_id, int $item_id): object
$this->fail(100, "One of the parameters specified was missing or invalid: object not found"); {
$this->requireUser();
$post->setLike(true, $this->getUser()); $this->willExecuteWriteAction();
return (object) [ $postable = null;
"likes" => $post->getLikesCount() switch ($type) {
]; case "post":
default: $post = (new PostsRepo())->getPostById($owner_id, $item_id);
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type"); $postable = $post;
} break;
} case "comment":
$comment = (new CommentsRepo())->get($item_id);
function delete(string $type, int $owner_id, int $item_id): object $postable = $comment;
{ break;
$this->requireUser(); case "video":
$this->willExecuteWriteAction(); $video = (new VideosRepo())->getByOwnerAndVID($owner_id, $item_id);
$postable = $video;
switch($type) { break;
case "post": case "photo":
$post = (new PostsRepo)->getPostById($owner_id, $item_id); $photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $item_id);
if (is_null($post)) $postable = $photo;
$this->fail(100, "One of the parameters specified was missing or invalid: object not found"); break;
case "note":
$post->setLike(false, $this->getUser()); $note = (new NotesRepo())->getNoteById($owner_id, $item_id);
return (object) [ $postable = $note;
"likes" => $post->getLikesCount() break;
]; default:
default: $this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type"); }
}
} if (is_null($postable) || $postable->isDeleted()) {
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object }
{
$this->requireUser(); if (!$postable->canBeViewedBy($this->getUser() ?? null)) {
$this->fail(2, "Access to postable denied");
switch($type) { }
case "post":
$user = (new UsersRepo)->get($user_id); $postable->setLike(true, $this->getUser());
if (is_null($user))
$this->fail(100, "One of the parameters specified was missing or invalid: user not found"); return (object) [
"likes" => $postable->getLikesCount(),
$post = (new PostsRepo)->getPostById($owner_id, $item_id); ];
if (is_null($post)) }
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
public function delete(string $type, int $owner_id, int $item_id): object
return (object) [ {
"liked" => (int) $post->hasLikeFrom($user), $this->requireUser();
"copied" => 0 # TODO: handle this $this->willExecuteWriteAction();
];
default: $postable = null;
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type"); switch ($type) {
} case "post":
} $post = (new PostsRepo())->getPostById($owner_id, $item_id);
} $postable = $post;
break;
case "comment":
$comment = (new CommentsRepo())->get($item_id);
$postable = $comment;
break;
case "video":
$video = (new VideosRepo())->getByOwnerAndVID($owner_id, $item_id);
$postable = $video;
break;
case "photo":
$photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $item_id);
$postable = $photo;
break;
case "note":
$note = (new NotesRepo())->getNoteById($owner_id, $item_id);
$postable = $note;
break;
default:
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
if (is_null($postable) || $postable->isDeleted()) {
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
}
if (!$postable->canBeViewedBy($this->getUser() ?? null)) {
$this->fail(2, "Access to postable denied");
}
if (!is_null($postable)) {
$postable->setLike(false, $this->getUser());
return (object) [
"likes" => $postable->getLikesCount(),
];
}
}
public function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object
{
$this->requireUser();
$user = (new UsersRepo())->get($user_id);
if (is_null($user) || $user->isDeleted()) {
$this->fail(100, "One of the parameters specified was missing or invalid: user not found");
}
if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(1984, "Access denied: you can't see this user");
}
$postable = null;
switch ($type) {
case "post":
$post = (new PostsRepo())->getPostById($owner_id, $item_id);
$postable = $post;
break;
case "comment":
$comment = (new CommentsRepo())->get($item_id);
$postable = $comment;
break;
case "video":
$video = (new VideosRepo())->getByOwnerAndVID($owner_id, $item_id);
$postable = $video;
break;
case "photo":
$photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $item_id);
$postable = $photo;
break;
case "note":
$note = (new NotesRepo())->getNoteById($owner_id, $item_id);
$postable = $note;
break;
default:
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
if (is_null($postable) || $postable->isDeleted()) {
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
}
if (!$postable->canBeViewedBy($this->getUser())) {
$this->fail(665, "Access to postable denied");
}
return (object) [
"liked" => (int) $postable->hasLikeFrom($user),
"copied" => 0,
];
}
public function getList(string $type, int $owner_id, int $item_id, bool $extended = false, int $offset = 0, int $count = 10, bool $skip_own = false)
{
$this->requireUser();
$object = null;
switch ($type) {
case "post":
$object = (new PostsRepo())->getPostById($owner_id, $item_id);
break;
case "comment":
$object = (new CommentsRepo())->get($item_id);
break;
case "photo":
$object = (new PhotosRepo())->getByOwnerAndVID($owner_id, $item_id);
break;
case "video":
$object = (new VideosRepo())->getByOwnerAndVID($owner_id, $item_id);
break;
default:
$this->fail(58, "Invalid type");
break;
}
if (!$object || $object->isDeleted()) {
$this->fail(56, "Invalid postable");
}
if (!$object->canBeViewedBy($this->getUser())) {
$this->fail(665, "Access to postable denied");
}
$res = (object) [
"count" => $object->getLikesCount(),
"items" => [],
];
$likers = array_slice(iterator_to_array($object->getLikers(1, $offset + $count)), $offset);
foreach ($likers as $liker) {
if ($skip_own && $liker->getId() == $this->getUser()->getId()) {
continue;
}
if (!$extended) {
$res->items[] = $liker->getId();
} else {
$res->items[] = $liker->toVkApiStruct(null, 'photo_50');
}
}
return $res;
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Events\NewMessageEvent; use openvk\Web\Events\NewMessageEvent;
use openvk\Web\Models\Entities\{Correspondence, Message}; use openvk\Web\Models\Entities\{Correspondence, Message};
use openvk\Web\Models\Repositories\{Messages as MSGRepo, Users as USRRepo}; use openvk\Web\Models\Repositories\{Messages as MSGRepo, Users as USRRepo};
@ -11,37 +15,39 @@ final class Messages extends VKAPIRequestHandler
{ {
private function resolvePeer(int $user_id = -1, int $peer_id = -1): ?int private function resolvePeer(int $user_id = -1, int $peer_id = -1): ?int
{ {
if($user_id === -1) { if ($user_id === -1) {
if($peer_id === -1) if ($peer_id === -1) {
return NULL; return null;
else if($peer_id < 0) } elseif ($peer_id < 0) {
return NULL; return null;
else if(($peer_id - 2000000000) > 0) } elseif (($peer_id - 2000000000) > 0) {
return NULL; return null;
}
return $peer_id; return $peer_id;
} }
return $user_id; return $user_id;
} }
function getById(string $message_ids, int $preview_length = 0, int $extended = 0): object public function getById(string $message_ids, int $preview_length = 0, int $extended = 0): object
{ {
$this->requireUser(); $this->requireUser();
$msgs = new MSGRepo; $msgs = new MSGRepo();
$ids = preg_split("%, ?%", $message_ids); $ids = preg_split("%, ?%", $message_ids);
$items = []; $items = [];
foreach($ids as $id) { foreach ($ids as $id) {
$message = $msgs->get((int) $id); $message = $msgs->get((int) $id);
if(!$message) if (!$message) {
continue; continue;
else if($message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId()) } elseif ($message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId()) {
continue; continue;
}
$author = $message->getSender()->getId() === $this->getUser()->getId() ? $message->getRecipient()->getId() : $message->getSender()->getId(); $author = $message->getSender()->getId() === $this->getUser()->getId() ? $message->getRecipient()->getId() : $message->getSender()->getId();
$rMsg = new APIMsg; $rMsg = new APIMsg();
$rMsg->id = $message->getId(); $rMsg->id = $message->getId();
$rMsg->user_id = $author; $rMsg->user_id = $author;
$rMsg->from_id = $message->getSender()->getId(); $rMsg->from_id = $message->getSender()->getId();
@ -51,150 +57,186 @@ final class Messages extends VKAPIRequestHandler
$rMsg->body = $message->getText(false); $rMsg->body = $message->getText(false);
$rMsg->text = $message->getText(false); $rMsg->text = $message->getText(false);
$rMsg->emoji = true; $rMsg->emoji = true;
if($preview_length > 0) if ($preview_length > 0) {
$rMsg->body = ovk_proc_strtr($rMsg->body, $preview_length); $rMsg->body = ovk_proc_strtr($rMsg->body, $preview_length);
$rMsg->text = ovk_proc_strtr($rMsg->text, $preview_length); }
$rMsg->text = ovk_proc_strtr($rMsg->text, $preview_length);
$items[] = $rMsg; $items[] = $rMsg;
} }
return (object) [ return (object) [
"count" => sizeof($items), "count" => sizeof($items),
"items" => $items, "items" => $items,
]; ];
} }
function send(int $user_id = -1, int $peer_id = -1, string $domain = "", int $chat_id = -1, string $user_ids = "", string $message = "", int $sticker_id = -1, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0) public function send(
int $user_id = -1,
int $peer_id = -1,
string $domain = "",
int $chat_id = -1,
string $user_ids = "",
string $message = "",
int $sticker_id = -1,
int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0,
string $attachment = ""
) { # интересно почему не attachments
$this->requireUser();
$this->willExecuteWriteAction();
if ($forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0) {
$this->getUser()->updOnline($this->getPlatform());
}
if ($chat_id !== -1) {
$this->fail(946, "Chats are not implemented");
} elseif ($sticker_id !== -1) {
$this->fail(-151, "Stickers are not implemented");
}
if (empty($message) && empty($attachment)) {
$this->fail(100, "Message text is empty or invalid");
}
# lol recursion
if (!empty($user_ids)) {
$rIds = [];
$ids = preg_split("%, ?%", $user_ids);
if (sizeof($ids) > 100) {
$this->fail(913, "Too many recipients");
}
foreach ($ids as $id) {
$rIds[] = $this->send(-1, $id, "", -1, "", $message);
}
return $rIds;
}
if (!empty($domain)) {
$peer = (new USRRepo())->getByShortCode($domain);
} else {
$peer = $this->resolvePeer($user_id, $peer_id);
$peer = (new USRRepo())->get($peer);
}
if (!$peer) {
$this->fail(936, "There is no peer with this id");
}
if ($this->getUser()->getId() !== $peer->getId() && !$peer->getPrivacyPermission('messages.write', $this->getUser())) {
$this->fail(945, "This chat is disabled because of privacy settings");
}
# Finally we get to send a message!
$chat = new Correspondence($this->getUser(), $peer);
$msg = new Message();
$msg->setContent($message);
$msg = $chat->sendMessage($msg, true);
if (!$msg) {
$this->fail(950, "Internal error");
} elseif (!empty($attachment)) {
$attachs = parseAttachments($attachment);
# Работают только фотки, остальное просто не будет отображаться.
if (sizeof($attachs) >= 10) {
$this->fail(15, "Too many attachments");
}
foreach ($attachs as $attach) {
if ($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) {
$msg->attach($attach);
} else {
$this->fail(52, "One of the attachments is invalid");
}
}
}
return $msg->getId();
}
public function delete(string $message_ids, int $spam = 0, int $delete_for_all = 0): object
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if($forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0) $msgs = new MSGRepo();
{
$this->getUser()->updOnline($this->getPlatform());
}
if($chat_id !== -1)
$this->fail(946, "Chats are not implemented");
else if($sticker_id !== -1)
$this->fail(-151, "Stickers are not implemented");
else if(empty($message))
$this->fail(100, "Message text is empty or invalid");
# lol recursion
if(!empty($user_ids)) {
$rIds = [];
$ids = preg_split("%, ?%", $user_ids);
if(sizeof($ids) > 100)
$this->fail(913, "Too many recipients");
foreach($ids as $id)
$rIds[] = $this->send(-1, $id, "", -1, "", $message);
return $rIds;
}
if(!empty($domain)) {
$peer = (new USRRepo)->getByShortCode($domain);
} else {
$peer = $this->resolvePeer($user_id, $peer_id);
$peer = (new USRRepo)->get($peer);
}
if(!$peer)
$this->fail(936, "There is no peer with this id");
if($this->getUser()->getId() !== $peer->getId() && !$peer->getPrivacyPermission('messages.write', $this->getUser()))
$this->fail(945, "This chat is disabled because of privacy settings");
# Finally we get to send a message!
$chat = new Correspondence($this->getUser(), $peer);
$msg = new Message;
$msg->setContent($message);
$msg = $chat->sendMessage($msg, true);
if(!$msg)
$this->fail(950, "Internal error");
else
return $msg->getId();
}
function delete(string $message_ids, int $spam = 0, int $delete_for_all = 0): object
{
$this->requireUser();
$this->willExecuteWriteAction();
$msgs = new MSGRepo;
$ids = preg_split("%, ?%", $message_ids); $ids = preg_split("%, ?%", $message_ids);
$items = []; $items = [];
foreach($ids as $id) { foreach ($ids as $id) {
$message = $msgs->get((int) $id); $message = $msgs->get((int) $id);
if(!$message || $message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId()) if (!$message || $message->getSender()->getId() !== $this->getUser()->getId() && $message->getRecipient()->getId() !== $this->getUser()->getId()) {
$items[$id] = 0; $items[$id] = 0;
}
$message->delete(); $message->delete();
$items[$id] = 1; $items[$id] = 1;
} }
return (object) $items; return (object) $items;
} }
function restore(int $message_id): int public function restore(int $message_id): int
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$msg = (new MSGRepo)->get($message_id); $msg = (new MSGRepo())->get($message_id);
if(!$msg) if (!$msg) {
return 0; return 0;
else if($msg->getSender()->getId() !== $this->getUser()->getId()) } elseif ($msg->getSender()->getId() !== $this->getUser()->getId()) {
return 0; return 0;
}
$msg->undelete(); $msg->undelete();
return 1; return 1;
} }
function getConversations(int $offset = 0, int $count = 20, string $filter = "all", int $extended = 0, string $fields = ""): object public function getConversations(int $offset = 0, int $count = 20, string $filter = "all", int $extended = 0, string $fields = ""): object
{ {
$this->requireUser(); $this->requireUser();
$convos = (new MSGRepo)->getCorrespondencies($this->getUser(), -1, $count, $offset); $convos = (new MSGRepo())->getCorrespondencies($this->getUser(), -1, $count, $offset);
$convosCount = (new MSGRepo)->getCorrespondenciesCount($this->getUser()); $convosCount = (new MSGRepo())->getCorrespondenciesCount($this->getUser());
$list = []; $list = [];
$users = []; $users = [];
foreach($convos as $convo) { foreach ($convos as $convo) {
$correspondents = $convo->getCorrespondents(); $correspondents = $convo->getCorrespondents();
if($correspondents[0]->getId() === $this->getUser()->getId()) if ($correspondents[0]->getId() === $this->getUser()->getId()) {
$peer = $correspondents[1]; $peer = $correspondents[1];
else } else {
$peer = $correspondents[0]; $peer = $correspondents[0];
}
$lastMessage = $convo->getPreviewMessage(); $lastMessage = $convo->getPreviewMessage();
$listConvo = new APIConvo; $listConvo = new APIConvo();
$listConvo->peer = [ $listConvo->peer = [
"id" => $peer->getId(), "id" => $peer->getId(),
"type" => "user", "type" => "user",
"local_id" => $peer->getId(), "local_id" => $peer->getId(),
]; ];
$canWrite = $peer->getSubscriptionStatus($this->getUser()) === 3; $canWrite = $peer->getSubscriptionStatus($this->getUser()) === 3;
$listConvo->can_write = [ $listConvo->can_write = [
"allowed" => $canWrite, "allowed" => $canWrite,
]; ];
$lastMessagePreview = NULL; $lastMessagePreview = null;
if(!is_null($lastMessage)) { if (!is_null($lastMessage)) {
$listConvo->last_message_id = $lastMessage->getId(); $listConvo->last_message_id = $lastMessage->getId();
if($lastMessage->getSender()->getId() === $this->getUser()->getId()) if ($lastMessage->getSender()->getId() === $this->getUser()->getId()) {
$author = $lastMessage->getRecipient()->getId(); $author = $lastMessage->getRecipient()->getId();
else } else {
$author = $lastMessage->getSender()->getId(); $author = $lastMessage->getSender()->getId();
}
$lastMessagePreview = new APIMsg;
$lastMessagePreview = new APIMsg();
$lastMessagePreview->id = $lastMessage->getId(); $lastMessagePreview->id = $lastMessage->getId();
$lastMessagePreview->user_id = $author; $lastMessagePreview->user_id = $author;
$lastMessagePreview->from_id = $lastMessage->getSender()->getId(); $lastMessagePreview->from_id = $lastMessage->getSender()->getId();
@ -204,19 +246,19 @@ final class Messages extends VKAPIRequestHandler
$lastMessagePreview->body = $lastMessage->getText(false); $lastMessagePreview->body = $lastMessage->getText(false);
$lastMessagePreview->text = $lastMessage->getText(false); $lastMessagePreview->text = $lastMessage->getText(false);
$lastMessagePreview->emoji = true; $lastMessagePreview->emoji = true;
if($extended == 1) { if ($extended == 1) {
$users[] = $author; $users[] = $author;
} }
} }
$list[] = [ $list[] = [
"conversation" => $listConvo, "conversation" => $listConvo,
"last_message" => $lastMessagePreview, "last_message" => $lastMessagePreview,
]; ];
} }
if($extended == 0){ if ($extended == 0) {
return (object) [ return (object) [
"count" => $convosCount, "count" => $convosCount,
"items" => $list, "items" => $list,
@ -228,12 +270,12 @@ final class Messages extends VKAPIRequestHandler
return (object) [ return (object) [
"count" => $convosCount, "count" => $convosCount,
"items" => $list, "items" => $list,
"profiles" => (!empty($users) ? (new APIUsers)->get(implode(',', $users), $fields, 0, $count+1) : []) "profiles" => (!empty($users) ? (new APIUsers())->get(implode(',', $users), $fields, 0, $count + 1) : []),
]; ];
} }
} }
function getConversationsById(string $peer_ids, int $extended = 0, string $fields = "") public function getConversationsById(string $peer_ids, int $extended = 0, string $fields = "")
{ {
$this->requireUser(); $this->requireUser();
@ -241,21 +283,23 @@ final class Messages extends VKAPIRequestHandler
$output = [ $output = [
"count" => 0, "count" => 0,
"items" => [] "items" => [],
]; ];
$userslist = []; $userslist = [];
foreach($peers as $peer) { foreach ($peers as $peer) {
if(key($peers) > 100) if (key($peers) > 100) {
continue; continue;
}
if(is_null($user_id = $this->resolvePeer((int) $peer))) if (is_null($user_id = $this->resolvePeer((int) $peer))) {
$this->fail(-151, "Chats are not implemented"); $this->fail(-151, "Chats are not implemented");
}
$user = (new USRRepo)->get((int) $peer); $user = (new USRRepo())->get((int) $peer);
if($user) { if ($user) {
$dialogue = new Correspondence($this->getUser(), $user); $dialogue = new Correspondence($this->getUser(), $user);
$iterator = $dialogue->getMessages(Correspondence::CAP_BEHAVIOUR_START_MESSAGE_ID, 0, 1, 0, false); $iterator = $dialogue->getMessages(Correspondence::CAP_BEHAVIOUR_START_MESSAGE_ID, 0, 1, 0, false);
$msg = $iterator[0]->unwrap(); // шоб удобнее было $msg = $iterator[0]->unwrap(); // шоб удобнее было
@ -263,7 +307,7 @@ final class Messages extends VKAPIRequestHandler
"peer" => [ "peer" => [
"id" => $user->getId(), "id" => $user->getId(),
"type" => "user", "type" => "user",
"local_id" => $user->getId() "local_id" => $user->getId(),
], ],
"last_message_id" => $msg->id, "last_message_id" => $msg->id,
"in_read" => $msg->id, "in_read" => $msg->id,
@ -278,41 +322,43 @@ final class Messages extends VKAPIRequestHandler
"is_marked_unread" => $iterator[0]->isUnread(), "is_marked_unread" => $iterator[0]->isUnread(),
"important" => false, // целестора когда релиз "important" => false, // целестора когда релиз
"can_write" => [ "can_write" => [
"allowed" => ($user->getId() === $this->getUser()->getId() || $user->getPrivacyPermission('messages.write', $this->getUser()) === true) "allowed" => ($user->getId() === $this->getUser()->getId() || $user->getPrivacyPermission('messages.write', $this->getUser()) === true),
] ],
]; ];
$userslist[] = $user->getId(); $userslist[] = $user->getId();
} }
} }
if($extended == 1) { if ($extended == 1) {
$userslist = array_unique($userslist); $userslist = array_unique($userslist);
$output['profiles'] = (!empty($userslist) ? (new APIUsers)->get(implode(',', $userslist), $fields) : []); $output['profiles'] = (!empty($userslist) ? (new APIUsers())->get(implode(',', $userslist), $fields) : []);
} }
$output['count'] = sizeof($output['items']); $output['count'] = sizeof($output['items']);
return (object) $output; return (object) $output;
} }
function getHistory(int $offset = 0, int $count = 20, int $user_id = -1, int $peer_id = -1, int $start_message_id = 0, int $rev = 0, int $extended = 0, string $fields = ""): object public function getHistory(int $offset = 0, int $count = 20, int $user_id = -1, int $peer_id = -1, int $start_message_id = 0, int $rev = 0, int $extended = 0, string $fields = ""): object
{ {
$this->requireUser(); $this->requireUser();
if(is_null($user_id = $this->resolvePeer($user_id, $peer_id))) if (is_null($user_id = $this->resolvePeer($user_id, $peer_id))) {
$this->fail(-151, "Chats are not implemented"); $this->fail(-151, "Chats are not implemented");
}
$peer = (new USRRepo)->get($user_id);
if(!$peer) $peer = (new USRRepo())->get($user_id);
if (!$peer) {
$this->fail(1, "ошибка про то что пира нет"); $this->fail(1, "ошибка про то что пира нет");
}
$results = []; $results = [];
$dialogue = new Correspondence($this->getUser(), $peer); $dialogue = new Correspondence($this->getUser(), $peer);
$iterator = $dialogue->getMessages(Correspondence::CAP_BEHAVIOUR_START_MESSAGE_ID, $start_message_id, $count, abs($offset), $rev === 1); $iterator = $dialogue->getMessages(Correspondence::CAP_BEHAVIOUR_START_MESSAGE_ID, $start_message_id, $count, abs($offset), $rev === 1);
foreach($iterator as $message) { foreach ($iterator as $message) {
$msgU = $message->unwrap(); # Why? As of OpenVK 2 Public Preview Two database layer doesn't work correctly and refuses to cache entities. $msgU = $message->unwrap(); # Why? As of OpenVK 2 Public Preview Two database layer doesn't work correctly and refuses to cache entities.
# UPDATE: the issue seems to be caused by debug mode and json_encode (bruh_encode). ~~Dorothy # UPDATE: the issue seems to be caused by debug mode and json_encode (bruh_encode). ~~Dorothy
$rMsg = new APIMsg; $rMsg = new APIMsg();
$rMsg->id = $msgU->id; $rMsg->id = $msgU->id;
$rMsg->user_id = $msgU->sender_id === $this->getUser()->getId() ? $msgU->recipient_id : $msgU->sender_id; $rMsg->user_id = $msgU->sender_id === $this->getUser()->getId() ? $msgU->recipient_id : $msgU->sender_id;
$rMsg->from_id = $msgU->sender_id; $rMsg->from_id = $msgU->sender_id;
@ -322,10 +368,10 @@ final class Messages extends VKAPIRequestHandler
$rMsg->body = $message->getText(false); $rMsg->body = $message->getText(false);
$rMsg->text = $message->getText(false); $rMsg->text = $message->getText(false);
$rMsg->emoji = true; $rMsg->emoji = true;
$results[] = $rMsg; $results[] = $rMsg;
} }
$output = [ $output = [
"count" => sizeof($results), "count" => sizeof($results),
"items" => $results, "items" => $results,
@ -339,58 +385,114 @@ final class Messages extends VKAPIRequestHandler
return (object) $output; return (object) $output;
} }
function getLongPollHistory(int $ts = -1, int $preview_length = 0, int $events_limit = 1000, int $msgs_limit = 1000): object public function getLongPollHistory(int $ts = -1, int $preview_length = 0, int $events_limit = 1000, int $msgs_limit = 1000): object
{ {
$this->requireUser(); $this->requireUser();
$res = [ $res = [
"history" => [], "history" => [],
"messages" => [], "messages" => [],
"profiles" => [], "profiles" => [],
"new_pts" => 0, "new_pts" => 0,
]; ];
$manager = SignalManager::i(); $manager = SignalManager::i();
$events = $manager->getHistoryFor($this->getUser()->getId(), $ts === -1 ? NULL : $ts, min($events_limit, $msgs_limit)); $events = $manager->getHistoryFor($this->getUser()->getId(), $ts === -1 ? null : $ts, min($events_limit, $msgs_limit));
foreach($events as $event) { foreach ($events as $event) {
if(!($event instanceof NewMessageEvent)) if (!($event instanceof NewMessageEvent)) {
continue; continue;
}
$message = $this->getById((string) $event->getLongPoolSummary()->message["uuid"], $preview_length, 1)->items[0]; $message = $this->getById((string) $event->getLongPoolSummary()->message["uuid"], $preview_length, 1)->items[0];
if(!$message) if (!$message) {
continue; continue;
}
$res["messages"][] = $message; $res["messages"][] = $message;
$res["history"][] = $event->getVKAPISummary($this->getUser()->getId()); $res["history"][] = $event->getVKAPISummary($this->getUser()->getId());
} }
$res["messages"] = [ $res["messages"] = [
"count" => sizeof($res["messages"]), "count" => sizeof($res["messages"]),
"items" => $res["messages"], "items" => $res["messages"],
]; ];
return (object) $res; return (object) $res;
} }
function getLongPollServer(int $need_pts = 1, int $lp_version = 3, ?int $group_id = NULL): array public function getLongPollServer(int $need_pts = 1, int $lp_version = 3, ?int $group_id = null): array
{ {
$this->requireUser(); $this->requireUser();
if($group_id > 0) if ($group_id > 0) {
$this->fail(-151, "Not implemented"); $this->fail(-151, "Not implemented");
}
$url = "http" . (ovk_is_ssl() ? "s" : "") . "://$_SERVER[HTTP_HOST]/nim" . $this->getUser()->getId(); $url = "http" . (ovk_is_ssl() ? "s" : "") . "://$_SERVER[HTTP_HOST]/nim" . $this->getUser()->getId();
$key = openssl_random_pseudo_bytes(8); $key = openssl_random_pseudo_bytes(8);
$key = bin2hex($key) . bin2hex($key ^ ( ~CHANDLER_ROOT_CONF["security"]["secret"] | ((string) $this->getUser()->getId()) )); $key = bin2hex($key) . bin2hex($key ^ (~CHANDLER_ROOT_CONF["security"]["secret"] | ((string) $this->getUser()->getId())));
$res = [ $res = [
"key" => $key, "key" => $key,
"server" => $url, "server" => $url,
"ts" => time(), "ts" => time(),
]; ];
if($need_pts === 1) if ($need_pts === 1) {
$res["pts"] = -1; $res["pts"] = -1;
}
return $res; return $res;
} }
public function edit(int $message_id, string $message = "", string $attachment = "", int $peer_id = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();
$msg = (new MSGRepo())->get($message_id);
if (empty($message) && empty($attachment)) {
$this->fail(100, "Required parameter 'message' missing.");
}
if (!$msg || $msg->isDeleted()) {
$this->fail(102, "Invalid message");
}
if ($msg->getSender()->getId() != $this->getUser()->getId()) {
$this->fail(15, "Access to message denied");
}
if (!empty($message)) {
$msg->setContent($message);
}
$msg->setEdited(time());
$msg->save(true);
if (!empty($attachment)) {
$attachs = parseAttachments($attachment);
$newAttachmentsCount = sizeof($attachs);
$postsAttachments = iterator_to_array($msg->getChildren());
if (sizeof($postsAttachments) >= 10) {
$this->fail(15, "Message have too many attachments");
}
if (($newAttachmentsCount + sizeof($postsAttachments)) > 10) {
$this->fail(158, "Message will have too many attachments");
}
foreach ($attachs as $attach) {
if ($attach && !$attach->isDeleted() && $attach->getOwner()->getId() == $this->getUser()->getId()) {
$msg->attach($attach);
} else {
$this->fail(52, "One of the attachments is invalid");
}
}
}
return 1;
}
} }

View file

@ -1,76 +1,273 @@
<?php declare(strict_types=1); <?php
namespace openvk\VKAPI\Handlers;
use Chandler\Database\DatabaseConnection; declare(strict_types=1);
use openvk\Web\Models\Repositories\Posts as PostsRepo;
use openvk\Web\Models\Entities\User; namespace openvk\VKAPI\Handlers;
use openvk\VKAPI\Handlers\Wall;
use Chandler\Database\DatabaseConnection;
final class Newsfeed extends VKAPIRequestHandler use openvk\Web\Models\Repositories\Posts as PostsRepo;
{ use openvk\Web\Models\Entities\User;
function get(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0) use openvk\VKAPI\Handlers\Wall;
{
$this->requireUser(); final class Newsfeed extends VKAPIRequestHandler
{
if($forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0) public function get(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $forGodSakePleaseDoNotReportAboutMyOnlineActivity = 0)
{ {
$this->getUser()->updOnline($this->getPlatform()); $this->requireUser();
}
if ($forGodSakePleaseDoNotReportAboutMyOnlineActivity == 0) {
$id = $this->getUser()->getId(); $this->getUser()->updOnline($this->getPlatform());
$subs = DatabaseConnection::i() }
->getContext()
->table("subscriptions") $id = $this->getUser()->getId();
->where("follower", $id); $subs = DatabaseConnection::i()
$ids = array_map(function($rel) { ->getContext()
return $rel->target * ($rel->model === "openvk\Web\Models\Entities\User" ? 1 : -1); ->table("subscriptions")
}, iterator_to_array($subs)); ->where("follower", $id);
$ids[] = $this->getUser()->getId(); $ids = array_map(function ($rel) {
return $rel->target * ($rel->model === "openvk\Web\Models\Entities\User" ? 1 : -1);
$posts = DatabaseConnection::i() }, iterator_to_array($subs));
->getContext() $ids[] = $this->getUser()->getId();
->table("posts")
->select("id") $posts = DatabaseConnection::i()
->where("wall IN (?)", $ids) ->getContext()
->where("deleted", 0) ->table("posts")
->where("id < (?)", empty($start_from) ? PHP_INT_MAX : $start_from) ->select("id")
->where("? <= created", empty($start_time) ? 0 : $start_time) ->where("wall IN (?)", $ids)
->where("? >= created", empty($end_time) ? PHP_INT_MAX : $end_time) ->where("deleted", 0)
->order("created DESC"); ->where("suggested", 0)
->where("id < (?)", empty($start_from) ? PHP_INT_MAX : $start_from)
$rposts = []; ->where("? <= created", empty($start_time) ? 0 : $start_time)
foreach($posts->page((int) ($offset + 1), $count) as $post) ->where("? >= created", empty($end_time) ? PHP_INT_MAX : $end_time)
$rposts[] = (new PostsRepo)->get($post->id)->getPrettyId(); ->order("created DESC");
$response = (new Wall)->getById(implode(',', $rposts), $extended, $fields, $this->getUser()); $rposts = [];
$response->next_from = end(end($posts->page((int) ($offset + 1), $count))); // ну и костыли пиздец конечно) foreach ($posts->page((int) ($offset + 1), $count) as $post) {
$rposts[] = (new PostsRepo())->get($post->id)->getPrettyId();
return $response; }
}
$response = (new Wall())->getById(implode(',', $rposts), $extended, $fields, $this->getUser());
function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0) $response->next_from = end(end($posts->page((int) ($offset + 1), $count))); // ну и костыли пиздец конечно)
{
$this->requireUser(); return $response;
}
$queryBase = "FROM `posts` LEFT JOIN `groups` ON GREATEST(`posts`.`wall`, 0) = 0 AND `groups`.`id` = ABS(`posts`.`wall`) WHERE (`groups`.`hide_from_global_feed` = 0 OR `groups`.`name` IS NULL) AND `posts`.`deleted` = 0";
public function getGlobal(string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $rss = 0)
if($this->getUser()->getNsfwTolerance() === User::NSFW_INTOLERANT) {
$queryBase .= " AND `nsfw` = 0"; $this->requireUser();
$start_from = empty($start_from) ? PHP_INT_MAX : $start_from; $queryBase = "FROM `posts` LEFT JOIN `groups` ON GREATEST(`posts`.`wall`, 0) = 0 AND `groups`.`id` = ABS(`posts`.`wall`) LEFT JOIN `profiles` ON LEAST(`posts`.`wall`, 0) = 0 AND `profiles`.`id` = ABS(`posts`.`wall`)";
$start_time = empty($start_time) ? 0 : $start_time; $queryBase .= "WHERE (`groups`.`hide_from_global_feed` = 0 OR `groups`.`name` IS NULL) AND (`profiles`.`profile_type` = 0 OR `profiles`.`first_name` IS NULL) AND `posts`.`deleted` = 0 AND `posts`.`suggested` = 0";
$end_time = empty($end_time) ? PHP_INT_MAX : $end_time;
$posts = DatabaseConnection::i()->getConnection()->query("SELECT `posts`.`id` " . $queryBase . " AND `posts`.`id` <= " . $start_from . " AND " . $start_time . " <= `posts`.`created` AND `posts`.`created` <= " . $end_time . " ORDER BY `created` DESC LIMIT " . $count . " OFFSET " . $offset); if ($this->getUser()->getNsfwTolerance() === User::NSFW_INTOLERANT) {
$queryBase .= " AND `nsfw` = 0";
$rposts = []; }
$ids = [];
foreach($posts as $post) { if ($return_banned == 0) {
$rposts[] = (new PostsRepo)->get($post->id)->getPrettyId(); $ignored_sources_ids = $this->getUser()->getIgnoredSources(0, OPENVK_ROOT_CONF['openvk']['preferences']['newsfeed']['ignoredSourcesLimit'] ?? 50, true);
$ids[] = $post->id;
} if (sizeof($ignored_sources_ids) > 0) {
$imploded_ids = implode("', '", $ignored_sources_ids);
$response = (new Wall)->getById(implode(',', $rposts), $extended, $fields, $this->getUser()); $queryBase .= " AND `posts`.`wall` NOT IN ('$imploded_ids')";
$response->next_from = end($ids); }
}
return $response;
} $start_from = empty($start_from) ? PHP_INT_MAX : $start_from;
} $start_time = empty($start_time) ? 0 : $start_time;
$end_time = empty($end_time) ? PHP_INT_MAX : $end_time;
$posts = DatabaseConnection::i()->getConnection()->query("SELECT `posts`.`id` " . $queryBase . " AND `posts`.`id` <= " . $start_from . " AND " . $start_time . " <= `posts`.`created` AND `posts`.`created` <= " . $end_time . " ORDER BY `created` DESC LIMIT " . $count . " OFFSET " . $offset);
$rposts = [];
$ids = [];
if ($rss == 1) {
$channel = new \Bhaktaraz\RSSGenerator\Channel();
$channel->title("Global Feed — " . OPENVK_ROOT_CONF['openvk']['appearance']['name'])
->description('OVK Global feed')
->url(ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/feed/all");
foreach ($posts as $item) {
$post = (new PostsRepo())->get($item->id);
if (!$post || $post->isDeleted()) {
continue;
}
$output = $post->toRss();
$output->appendTo($channel);
}
return $channel;
}
foreach ($posts as $post) {
$rposts[] = (new PostsRepo())->get($post->id)->getPrettyId();
$ids[] = $post->id;
}
$response = (new Wall())->getById(implode(',', $rposts), $extended, $fields, $this->getUser());
$response->next_from = end($ids);
return $response;
}
public function getByType(string $feed_type = 'top', string $fields = "", int $start_from = 0, int $start_time = 0, int $end_time = 0, int $offset = 0, int $count = 30, int $extended = 0, int $return_banned = 0)
{
$this->requireUser();
switch ($feed_type) {
case 'top':
return $this->getGlobal($fields, $start_from, $start_time, $end_time, $offset, $count, $extended, $return_banned);
break;
default:
return $this->get($fields, $start_from, $start_time, $end_time, $offset, $count, $extended);
break;
}
}
public function getBanned(int $extended = 0, string $fields = "", string $name_case = "nom", int $merge = 0): object
{
$this->requireUser();
$offset = 0;
$count = OPENVK_ROOT_CONF['openvk']['preferences']['newsfeed']['ignoredSourcesLimit'] ?? 50;
$banned = $this->getUser()->getIgnoredSources($offset, $count, ($extended != 1));
$return_object = (object) [
'groups' => [],
'members' => [],
];
if ($extended == 0) {
foreach ($banned as $ban) {
if ($ban > 0) {
$return_object->members[] = $ban;
} else {
$return_object->groups[] = $ban;
}
}
} else {
if ($merge == 1) {
$return_object = (object) [
'count' => sizeof($banned),
'items' => [],
];
foreach ($banned as $ban) {
$return_object->items[] = $ban->toVkApiStruct($this->getUser(), $fields);
}
} else {
$return_object = (object) [
'groups' => [],
'profiles' => [],
];
foreach ($banned as $ban) {
if ($ban->getRealId() > 0) {
$return_object->profiles[] = $ban->toVkApiStruct($this->getUser(), $fields);
} else {
$return_object->groups[] = $ban->toVkApiStruct($this->getUser(), $fields);
}
}
}
}
return $return_object;
}
public function addBan(string $user_ids = "", string $group_ids = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
# Formatting input ids
if (!empty($user_ids)) {
$user_ids = array_map(function ($el) {
return (int) $el;
}, explode(',', $user_ids));
$user_ids = array_unique($user_ids);
} else {
$user_ids = [];
}
if (!empty($group_ids)) {
$group_ids = array_map(function ($el) {
return abs((int) $el) * -1;
}, explode(',', $group_ids));
$group_ids = array_unique($group_ids);
} else {
$group_ids = [];
}
$ids = array_merge($user_ids, $group_ids);
if (sizeof($ids) < 1) {
return 0;
}
if (sizeof($ids) > 10) {
$this->fail(-10, "Limit of 'ids' is 10");
}
$config_limit = OPENVK_ROOT_CONF['openvk']['preferences']['newsfeed']['ignoredSourcesLimit'] ?? 50;
$user_ignores = $this->getUser()->getIgnoredSourcesCount();
if (($user_ignores + sizeof($ids)) > $config_limit) {
$this->fail(-50, "Ignoring limit exceeded");
}
$entities = get_entities($ids);
$successes = 0;
foreach ($entities as $entity) {
if (!$entity || $entity->getRealId() === $this->getUser()->getRealId() || $entity->isHideFromGlobalFeedEnabled() || $entity->isIgnoredBy($this->getUser())) {
continue;
}
$entity->addIgnore($this->getUser());
$successes += 1;
}
return 1;
}
public function deleteBan(string $user_ids = "", string $group_ids = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
if (!empty($user_ids)) {
$user_ids = array_map(function ($el) {
return (int) $el;
}, explode(',', $user_ids));
$user_ids = array_unique($user_ids);
} else {
$user_ids = [];
}
if (!empty($group_ids)) {
$group_ids = array_map(function ($el) {
return abs((int) $el) * -1;
}, explode(',', $group_ids));
$group_ids = array_unique($group_ids);
} else {
$group_ids = [];
}
$ids = array_merge($user_ids, $group_ids);
if (sizeof($ids) < 1) {
return 0;
}
if (sizeof($ids) > 10) {
$this->fail(-10, "Limit of ids is 10");
}
$entities = get_entities($ids);
$successes = 0;
foreach ($entities as $entity) {
if (!$entity || $entity->getRealId() === $this->getUser()->getRealId() || !$entity->isIgnoredBy($this->getUser())) {
continue;
}
$entity->removeIgnore($this->getUser());
$successes += 1;
}
return 1;
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Notes as NotesRepo; use openvk\Web\Models\Repositories\Notes as NotesRepo;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Comments as CommentsRepo; use openvk\Web\Models\Repositories\Comments as CommentsRepo;
@ -9,12 +13,12 @@ use openvk\Web\Models\Entities\{Note, Comment};
final class Notes extends VKAPIRequestHandler final class Notes extends VKAPIRequestHandler
{ {
function add(string $title, string $text, int $privacy = 0, int $comment_privacy = 0, string $privacy_view = "", string $privacy_comment = "") public function add(string $title, string $text, int $privacy = 0, int $comment_privacy = 0, string $privacy_view = "", string $privacy_comment = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$note = new Note; $note = new Note();
$note->setOwner($this->getUser()->getId()); $note->setOwner($this->getUser()->getId());
$note->setCreated(time()); $note->setCreated(time());
$note->setName($title); $note->setName($title);
@ -25,28 +29,37 @@ final class Notes extends VKAPIRequestHandler
return $note->getVirtualId(); return $note->getVirtualId();
} }
function createComment(string $note_id, int $owner_id, string $message, int $reply_to = 0, string $attachments = "") public function createComment(string $note_id, int $owner_id, string $message, int $reply_to = 0, string $attachments = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$note = (new NotesRepo)->getNoteById((int)$owner_id, (int)$note_id); $note = (new NotesRepo())->getNoteById((int) $owner_id, (int) $note_id);
if(!$note) if (!$note) {
$this->fail(180, "Note not found"); $this->fail(180, "Note not found");
}
if($note->isDeleted())
if ($note->isDeleted()) {
$this->fail(189, "Note is deleted"); $this->fail(189, "Note is deleted");
}
if($note->getOwner()->isDeleted())
if ($note->getOwner()->isDeleted()) {
$this->fail(403, "Owner is deleted"); $this->fail(403, "Owner is deleted");
}
if(!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) if (!$note->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(43, "No access"); $this->fail(43, "No access");
}
if(empty($message) && empty($attachments)) if (empty($message) && empty($attachments)) {
$this->fail(100, "Required parameter 'message' missing."); $this->fail(100, "Required parameter 'message' missing.");
}
$comment = new Comment; $comment = new Comment();
$comment->setOwner($this->getUser()->getId()); $comment->setOwner($this->getUser()->getId());
$comment->setModel(get_class($note)); $comment->setModel(get_class($note));
$comment->setTarget($note->getId()); $comment->setTarget($note->getId());
@ -54,43 +67,49 @@ final class Notes extends VKAPIRequestHandler
$comment->setCreated(time()); $comment->setCreated(time());
$comment->save(); $comment->save();
if(!empty($attachments)) { if (!empty($attachments)) {
$attachmentsArr = explode(",", $attachments); $attachmentsArr = explode(",", $attachments);
if(sizeof($attachmentsArr) > 10) if (sizeof($attachmentsArr) > 10) {
$this->fail(50, "Error: too many attachments"); $this->fail(50, "Error: too many attachments");
}
foreach($attachmentsArr as $attac) {
$attachmentType = NULL;
if(str_contains($attac, "photo")) foreach ($attachmentsArr as $attac) {
$attachmentType = null;
if (str_contains($attac, "photo")) {
$attachmentType = "photo"; $attachmentType = "photo";
elseif(str_contains($attac, "video")) } elseif (str_contains($attac, "video")) {
$attachmentType = "video"; $attachmentType = "video";
else } else {
$this->fail(205, "Unknown attachment type"); $this->fail(205, "Unknown attachment type");
}
$attachment = str_replace($attachmentType, "", $attac); $attachment = str_replace($attachmentType, "", $attac);
$attachmentOwner = (int)explode("_", $attachment)[0]; $attachmentOwner = (int) explode("_", $attachment)[0];
$attachmentId = (int)end(explode("_", $attachment)); $attachmentId = (int) end(explode("_", $attachment));
$attacc = NULL; $attacc = null;
if($attachmentType == "photo") { if ($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new PhotosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Photo does not exists"); $this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this photo"); $this->fail(43, "You do not have access to this photo");
}
$comment->attach($attacc); $comment->attach($attacc);
} elseif($attachmentType == "video") { } elseif ($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new VideosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Video does not exists"); $this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this video"); $this->fail(43, "You do not have access to this video");
}
$comment->attach($attacc); $comment->attach($attacc);
} }
@ -100,118 +119,96 @@ final class Notes extends VKAPIRequestHandler
return $comment->getId(); return $comment->getId();
} }
function delete(string $note_id) public function delete(string $note_id)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$note = (new NotesRepo)->get((int)$note_id); $note = (new NotesRepo())->get((int) $note_id);
if(!$note) if (!$note) {
$this->fail(180, "Note not found"); $this->fail(180, "Note not found");
}
if(!$note->canBeModifiedBy($this->getUser()))
if (!$note->canBeModifiedBy($this->getUser())) {
$this->fail(15, "Access to note denied"); $this->fail(15, "Access to note denied");
}
$note->delete(); $note->delete();
return 1; return 1;
} }
function deleteComment(int $comment_id, int $owner_id = 0) public function edit(string $note_id, string $title = "", string $text = "", int $privacy = 0, int $comment_privacy = 0, string $privacy_view = "", string $privacy_comment = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$comment = (new CommentsRepo)->get($comment_id); $note = (new NotesRepo())->getNoteById($this->getUser()->getId(), (int) $note_id);
if(!$comment || !$comment->canBeDeletedBy($this->getUser())) if (!$note) {
$this->fail(403, "Access to comment denied");
$comment->delete();
return 1;
}
function edit(string $note_id, string $title = "", string $text = "", int $privacy = 0, int $comment_privacy = 0, string $privacy_view = "", string $privacy_comment = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
$note = (new NotesRepo)->getNoteById($this->getUser()->getId(), (int)$note_id);
if(!$note)
$this->fail(180, "Note not found"); $this->fail(180, "Note not found");
}
if($note->isDeleted())
if ($note->isDeleted()) {
$this->fail(189, "Note is deleted"); $this->fail(189, "Note is deleted");
}
if(!$note->canBeModifiedBy($this->getUser())) if (!$note->canBeModifiedBy($this->getUser())) {
$this->fail(403, "No access"); $this->fail(403, "No access");
}
!empty($title) ? $note->setName($title) : NULL; !empty($title) ? $note->setName($title) : null;
!empty($text) ? $note->setSource($text) : NULL; !empty($text) ? $note->setSource($text) : null;
$note->setCached_Content(NULL); $note->setCached_Content(null);
$note->setEdited(time()); $note->setEdited(time());
$note->save(); $note->save();
return 1; return 1;
} }
function editComment(int $comment_id, string $message, int $owner_id = NULL) public function get(int $user_id, string $note_ids = "", int $offset = 0, int $count = 10, int $sort = 0)
{
/*
$this->requireUser();
$this->willExecuteWriteAction();
$comment = (new CommentsRepo)->get($comment_id);
if($comment->getOwner() != $this->getUser()->getId())
$this->fail(15, "Access to comment denied");
$comment->setContent($message);
$comment->setEdited(time());
$comment->save();
*/
return 1;
}
function get(int $user_id, string $note_ids = "", int $offset = 0, int $count = 10, int $sort = 0)
{ {
$this->requireUser(); $this->requireUser();
$user = (new UsersRepo)->get($user_id); $user = (new UsersRepo())->get($user_id);
if(!$user || $user->isDeleted()) if (!$user || $user->isDeleted()) {
$this->fail(15, "Invalid user"); $this->fail(15, "Invalid user");
}
if(!$user->getPrivacyPermission('notes.read', $this->getUser()))
$this->fail(43, "Access denied: this user chose to hide his notes"); if (!$user->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(15, "Access denied: this user chose to hide his notes");
if(empty($note_ids)) { }
$notes = array_slice(iterator_to_array((new NotesRepo)->getUserNotes($user, 1, $count + $offset, $sort == 0 ? "ASC" : "DESC")), $offset);
if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
if (empty($note_ids)) {
$notes = array_slice(iterator_to_array((new NotesRepo())->getUserNotes($user, 1, $count + $offset, $sort == 0 ? "ASC" : "DESC")), $offset);
$nodez = (object) [ $nodez = (object) [
"count" => (new NotesRepo)->getUserNotesCount((new UsersRepo)->get($user_id)), "count" => (new NotesRepo())->getUserNotesCount((new UsersRepo())->get($user_id)),
"notes" => [] "notes" => [],
]; ];
foreach($notes as $note) { foreach ($notes as $note) {
if($note->isDeleted()) continue; if ($note->isDeleted()) {
continue;
}
$nodez->notes[] = $note->toVkApiStruct(); $nodez->notes[] = $note->toVkApiStruct();
} }
} else { } else {
$notes = explode(',', $note_ids); $notes = explode(',', $note_ids);
foreach($notes as $note) foreach ($notes as $note) {
{
$id = explode("_", $note); $id = explode("_", $note);
$items = []; $items = [];
$note = (new NotesRepo)->getNoteById((int)$id[0], (int)$id[1]); $note = (new NotesRepo())->getNoteById((int) $id[0], (int) $id[1]);
if($note) { if ($note && !$note->isDeleted()) {
$nodez->notes[] = $note->toVkApiStruct(); $nodez->notes[] = $note->toVkApiStruct();
} }
} }
@ -220,63 +217,79 @@ final class Notes extends VKAPIRequestHandler
return $nodez; return $nodez;
} }
function getById(int $note_id, int $owner_id, bool $need_wiki = false) public function getById(int $note_id, int $owner_id, bool $need_wiki = false)
{ {
$this->requireUser(); $this->requireUser();
$note = (new NotesRepo)->getNoteById($owner_id, $note_id); $note = (new NotesRepo())->getNoteById($owner_id, $note_id);
if(!$note) if (!$note) {
$this->fail(180, "Note not found"); $this->fail(180, "Note not found");
}
if($note->isDeleted())
if ($note->isDeleted()) {
$this->fail(189, "Note is deleted"); $this->fail(189, "Note is deleted");
}
if(!$note->getOwner() || $note->getOwner()->isDeleted()) if (!$note->getOwner() || $note->getOwner()->isDeleted()) {
$this->fail(177, "Owner does not exists"); $this->fail(177, "Owner does not exists");
}
if(!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(40, "Access denied: this user chose to hide his notes"); $this->fail(40, "Access denied: this user chose to hide his notes");
}
if (!$note->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access to note denied");
}
return $note->toVkApiStruct(); return $note->toVkApiStruct();
} }
function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100) public function getComments(int $note_id, int $owner_id, int $sort = 1, int $offset = 0, int $count = 100)
{ {
$this->requireUser(); $this->requireUser();
$note = (new NotesRepo)->getNoteById($owner_id, $note_id); $note = (new NotesRepo())->getNoteById($owner_id, $note_id);
if(!$note) if (!$note) {
$this->fail(180, "Note not found"); $this->fail(180, "Note not found");
}
if($note->isDeleted())
$this->fail(189, "Note is deleted");
if(!$note->getOwner())
$this->fail(177, "Owner does not exists");
if(!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) if ($note->isDeleted()) {
$this->fail(189, "Note is deleted");
}
if (!$note->getOwner()) {
$this->fail(177, "Owner does not exists");
}
if (!$note->getOwner()->getPrivacyPermission('notes.read', $this->getUser())) {
$this->fail(14, "No access"); $this->fail(14, "No access");
}
if (!$note->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access to note denied");
}
$arr = (object) [ $arr = (object) [
"count" => $note->getCommentsCount(), "count" => $note->getCommentsCount(),
"comments" => []]; "comments" => []];
$comments = array_slice(iterator_to_array($note->getComments(1, $count + $offset)), $offset); $comments = array_slice(iterator_to_array($note->getComments(1, $count + $offset)), $offset);
foreach($comments as $comment) { foreach ($comments as $comment) {
$arr->comments[] = $comment->toVkApiStruct($this->getUser(), false, false, $note); $arr->comments[] = $comment->toVkApiStruct($this->getUser(), false, false, $note);
} }
return $arr; return $arr;
} }
function getFriendsNotes(int $offset = 0, int $count = 0) public function getFriendsNotes(int $offset = 0, int $count = 0)
{ {
$this->fail(501, "Not implemented"); $this->fail(501, "Not implemented");
} }
function restoreComment(int $comment_id = 0, int $owner_id = 0) public function restoreComment(int $comment_id = 0, int $owner_id = 0)
{ {
$this->fail(501, "Not implemented"); $this->fail(501, "Not implemented");
} }

View file

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\Club;
use openvk\Web\Models\Repositories\{Notifications as Notifs, Clubs, Users};
final class Notifications extends VKAPIRequestHandler
{
public function get(
int $count = 10,
string $from = "",
int $offset = 0,
string $start_from = "",
string $filters = "",
int $start_time = 0,
int $end_time = 0,
int $archived = 0
) {
$this->requireUser();
$res = (object) [
"items" => [],
"profiles" => [],
"groups" => [],
"last_viewed" => $this->getUser()->getNotificationOffset(),
];
if ($count > 100) {
$this->fail(125, "Count is too big");
}
if (!eventdb()) {
$this->fail(1289, "EventDB is disabled on this instance");
}
$notifs = array_slice(iterator_to_array((new Notifs())->getNotificationsByUser($this->getUser(), $this->getUser()->getNotificationOffset(), (bool) $archived, 1, $offset + $count)), $offset);
$tmpProfiles = [];
foreach ($notifs as $notif) {
$sxModel = $notif->getModel(1);
if (!method_exists($sxModel, "getAvatarUrl")) {
$sxModel = $notif->getModel(0);
}
$tmpProfiles[] = $sxModel instanceof Club ? $sxModel->getId() * -1 : $sxModel->getId();
$res->items[] = $notif->toVkApiStruct();
}
foreach (array_unique($tmpProfiles) as $id) {
if ($id > 0) {
$sxModel = (new Users())->get($id);
$result = (object) [
"uid" => $sxModel->getId(),
"first_name" => $sxModel->getFirstName(),
"last_name" => $sxModel->getLastName(),
"photo" => $sxModel->getAvatarUrl(),
"photo_medium_rec" => $sxModel->getAvatarUrl("tiny"),
"screen_name" => $sxModel->getShortCode(),
];
$res->profiles[] = $result;
} else {
$sxModel = (new Clubs())->get(abs($id));
$result = $sxModel->toVkApiStruct($this->getUser());
$res->groups[] = $result;
}
}
return $res;
}
public function markAsViewed()
{
$this->requireUser();
$this->willExecuteWriteAction();
try {
$this->getUser()->updateNotificationOffset();
$this->getUser()->save();
} catch (\Throwable $e) {
return 0;
}
return 1;
}
}

View file

@ -1,15 +1,19 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\{Users as UsersRepo, Clubs as ClubsRepo, Posts as PostsRepo}; use openvk\Web\Models\Repositories\{Users as UsersRepo, Clubs as ClubsRepo, Posts as PostsRepo};
final class Ovk extends VKAPIRequestHandler final class Ovk extends VKAPIRequestHandler
{ {
function version(): string public function version(): string
{ {
return OPENVK_VERSION; return OPENVK_VERSION;
} }
function test(): object public function test(): object
{ {
return (object) [ return (object) [
"authorized" => $this->userAuthorized(), "authorized" => $this->userAuthorized(),
@ -17,58 +21,59 @@ final class Ovk extends VKAPIRequestHandler
"version" => VKAPI_DECL_VER, "version" => VKAPI_DECL_VER,
]; ];
} }
function chickenWings(): string public function chickenWings(): string
{ {
return "крылышки"; return "крылышки";
} }
function aboutInstance(string $fields = "statistics,administrators,popular_groups,links", string $admin_fields = "", string $group_fields = ""): object public function aboutInstance(string $fields = "statistics,administrators,popular_groups,links", string $admin_fields = "", string $group_fields = ""): object
{ {
$fields = explode(',', $fields); $fields = explode(',', $fields);
$response = (object) []; $response = (object) [];
if(in_array("statistics", $fields)) { if (in_array("statistics", $fields)) {
$usersStats = (new UsersRepo)->getStatistics(); $usersStats = (new UsersRepo())->getStatistics();
$clubsCount = (new ClubsRepo)->getCount(); $clubsCount = (new ClubsRepo())->getCount();
$postsCount = (new PostsRepo)->getCount(); $postsCount = (new PostsRepo())->getCount();
$response->statistics = (object) [ $response->statistics = (object) [
"users_count" => $usersStats->all, "users_count" => $usersStats->all,
"online_users_count" => $usersStats->online, "online_users_count" => $usersStats->online,
"active_users_count" => $usersStats->active, "active_users_count" => $usersStats->active,
"groups_count" => $clubsCount, "groups_count" => $clubsCount,
"wall_posts_count" => $postsCount "wall_posts_count" => $postsCount,
]; ];
} }
if(in_array("administrators", $fields)) { if (in_array("administrators", $fields)) {
$admins = iterator_to_array((new UsersRepo)->getInstanceAdmins()); $admins = iterator_to_array((new UsersRepo())->getInstanceAdmins());
$adminsResponse = (new Users($this->getUser()))->get(implode(',', array_map(function($admin) { $adminsResponse = (new Users($this->getUser()))->get(implode(',', array_map(function ($admin) {
return $admin->getId(); return $admin->getId();
}, $admins)), $admin_fields, 0, sizeof($admins)); }, $admins)), $admin_fields, 0, sizeof($admins));
$response->administrators = (object) [ $response->administrators = (object) [
"count" => sizeof($admins), "count" => sizeof($admins),
"items" => $adminsResponse "items" => $adminsResponse,
]; ];
} }
if(in_array("popular_groups", $fields)) { if (in_array("popular_groups", $fields)) {
$popularClubs = iterator_to_array((new ClubsRepo)->getPopularClubs()); $popularClubs = iterator_to_array((new ClubsRepo())->getPopularClubs());
$clubsResponse = (new Groups($this->getUser()))->getById(implode(',', array_map(function($entry) { $clubsResponse = (new Groups($this->getUser()))->getById(implode(',', array_map(function ($entry) {
return $entry->club->getId(); return $entry->club->getId();
}, $popularClubs)), "", "members_count, " . $group_fields); }, $popularClubs)), "", "members_count, " . $group_fields);
$response->popular_groups = (object) [ $response->popular_groups = (object) [
"count" => sizeof($popularClubs), "count" => sizeof($popularClubs),
"items" => $clubsResponse "items" => $clubsResponse,
]; ];
} }
if(in_array("links", $fields)) if (in_array("links", $fields)) {
$response->links = (object) [ $response->links = (object) [
"count" => sizeof(OPENVK_ROOT_CONF['openvk']['preferences']['about']['links']), "count" => sizeof(OPENVK_ROOT_CONF['openvk']['preferences']['about']['links']),
"items" => is_null(OPENVK_ROOT_CONF['openvk']['preferences']['about']['links']) ? [] : OPENVK_ROOT_CONF['openvk']['preferences']['about']['links'] "items" => is_null(OPENVK_ROOT_CONF['openvk']['preferences']['about']['links']) ? [] : OPENVK_ROOT_CONF['openvk']['preferences']['about']['links'],
]; ];
}
return $response; return $response;
} }

View file

@ -1,42 +1,49 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Applications; use openvk\Web\Models\Repositories\Applications;
final class Pay extends VKAPIRequestHandler final class Pay extends VKAPIRequestHandler
{ {
function getIdByMarketingId(string $marketing_id): int public function getIdByMarketingId(string $marketing_id): int
{ {
[$hexId, $signature] = explode("_", $marketing_id); [$hexId, $signature] = explode("_", $marketing_id);
try { try {
$key = CHANDLER_ROOT_CONF["security"]["secret"]; $key = CHANDLER_ROOT_CONF["security"]["secret"];
if(sodium_memcmp(base64_decode($signature), hash_hmac("sha512/224", $hexId, $key, true)) == -1) if (sodium_memcmp(base64_decode($signature), hash_hmac("sha512/224", $hexId, $key, true)) == -1) {
$this->fail(4, "Invalid marketing id"); $this->fail(4, "Invalid marketing id");
}
} catch (\SodiumException $e) { } catch (\SodiumException $e) {
$this->fail(4, "Invalid marketing id"); $this->fail(4, "Invalid marketing id");
} }
return hexdec($hexId); return hexdec($hexId);
} }
function verifyOrder(int $app_id, float $amount, string $signature): bool public function verifyOrder(int $app_id, float $amount, string $signature): bool
{ {
$this->requireUser(); $this->requireUser();
$app = (new Applications())->get($app_id); $app = (new Applications())->get($app_id);
if(!$app) if (!$app) {
$this->fail(26, "No app found with this id"); $this->fail(26, "No app found with this id");
else if($app->getOwner()->getId() != $this->getUser()->getId()) } elseif ($app->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(15, "Access error"); $this->fail(15, "Access error");
}
[$time, $signature] = explode(",", $signature); [$time, $signature] = explode(",", $signature);
try { try {
$key = CHANDLER_ROOT_CONF["security"]["secret"]; $key = CHANDLER_ROOT_CONF["security"]["secret"];
if(sodium_memcmp($signature, hash_hmac("whirlpool", "$app_id:$amount:$time", $key)) == -1) if (sodium_memcmp($signature, hash_hmac("whirlpool", "$app_id:$amount:$time", $key)) == -1) {
$this->fail(4, "Invalid order"); $this->fail(4, "Invalid order");
}
} catch (\SodiumException $e) { } catch (\SodiumException $e) {
$this->fail(4, "Invalid order"); $this->fail(4, "Invalid order");
} }
return true; return true;
} }
} }

View file

@ -1,4 +1,7 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use Nette\InvalidStateException; use Nette\InvalidStateException;
@ -33,31 +36,34 @@ final class Photos extends VKAPIRequestHandler
return ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/upload/photo/$uploadHash?$uploadInfo"; return ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/upload/photo/$uploadHash?$uploadInfo";
} }
private function getImagePath(string $photo, string $hash, ?string& $up = NULL, ?string& $group = NULL): string private function getImagePath(string $photo, string $hash, ?string& $up = null, ?string& $group = null): string
{ {
$secret = CHANDLER_ROOT_CONF["security"]["secret"]; $secret = CHANDLER_ROOT_CONF["security"]["secret"];
if(!hash_equals(hash_hmac("sha3-224", $photo, $secret), $hash)) if (!hash_equals(hash_hmac("sha3-224", $photo, $secret), $hash)) {
$this->fail(121, "Incorrect hash"); $this->fail(121, "Incorrect hash");
}
[$up, $image, $group] = explode("|", $photo); [$up, $image, $group] = explode("|", $photo);
$imagePath = __DIR__ . "/../../tmp/api-storage/photos/$up" . "_$image.oct"; $imagePath = __DIR__ . "/../../tmp/api-storage/photos/$up" . "_$image.oct";
if(!file_exists($imagePath)) if (!file_exists($imagePath)) {
$this->fail(10, "Invalid image"); $this->fail(10, "Invalid image");
}
return $imagePath; return $imagePath;
} }
function getOwnerPhotoUploadServer(int $owner_id = 0): object public function getOwnerPhotoUploadServer(int $owner_id = 0): object
{ {
$this->requireUser(); $this->requireUser();
if($owner_id < 0) { if ($owner_id < 0) {
$club = (new Clubs)->get(abs($owner_id)); $club = (new Clubs())->get(abs($owner_id));
if(!$club) if (!$club) {
$this->fail(0404, "Club not found"); $this->fail(0o404, "Club not found");
else if(!$club->canBeModifiedBy($this->getUser())) } elseif (!$club->canBeModifiedBy($this->getUser())) {
$this->fail(200, "Access: Club can't be 'written' by user"); $this->fail(200, "Access: Club can't be 'written' by user");
}
} }
return (object) [ return (object) [
@ -65,19 +71,19 @@ final class Photos extends VKAPIRequestHandler
]; ];
} }
function saveOwnerPhoto(string $photo, string $hash): object public function saveOwnerPhoto(string $photo, string $hash): object
{ {
$imagePath = $this->getImagePath($photo, $hash, $uploader, $group); $imagePath = $this->getImagePath($photo, $hash, $uploader, $group);
if($group == 0) { if ($group == 0) {
$user = (new \openvk\Web\Models\Repositories\Users)->get((int) $uploader); $user = (new \openvk\Web\Models\Repositories\Users())->get((int) $uploader);
$album = (new Albums)->getUserAvatarAlbum($user); $album = (new Albums())->getUserAvatarAlbum($user);
} else { } else {
$club = (new Clubs)->get((int) $group); $club = (new Clubs())->get((int) $group);
$album = (new Albums)->getClubAvatarAlbum($club); $album = (new Albums())->getClubAvatarAlbum($club);
} }
try { try {
$avatar = new Photo; $avatar = new Photo();
$avatar->setOwner((int) $uploader); $avatar->setOwner((int) $uploader);
$avatar->setDescription("Profile photo"); $avatar->setDescription("Profile photo");
$avatar->setCreated(time()); $avatar->setCreated(time());
@ -88,30 +94,31 @@ final class Photos extends VKAPIRequestHandler
$avatar->save(); $avatar->save();
$album->addPhoto($avatar); $album->addPhoto($avatar);
unlink($imagePath); unlink($imagePath);
} catch(ImageException | InvalidStateException $e) { } catch (ImageException | InvalidStateException $e) {
unlink($imagePath); unlink($imagePath);
$this->fail(129, "Invalid image file"); $this->fail(129, "Invalid image file");
} }
return (object) [ return (object) [
"photo_hash" => NULL, "photo_hash" => null,
"photo_src" => $avatar->getURL(), "photo_src" => $avatar->getURL(),
]; ];
} }
function getWallUploadServer(?int $group_id = NULL): object public function getWallUploadServer(?int $group_id = null): object
{ {
$this->requireUser(); $this->requireUser();
$album = NULL; $album = null;
if(!is_null($group_id)) { if (!is_null($group_id)) {
$club = (new Clubs)->get(abs($group_id)); $club = (new Clubs())->get(abs($group_id));
if(!$club) if (!$club) {
$this->fail(0404, "Club not found"); $this->fail(0o404, "Club not found");
else if(!$club->canBeModifiedBy($this->getUser())) } elseif (!$club->canBeModifiedBy($this->getUser())) {
$this->fail(200, "Access: Club can't be 'written' by user"); $this->fail(200, "Access: Club can't be 'written' by user");
}
} else { } else {
$album = (new Albums)->getUserWallAlbum($this->getUser()); $album = (new Albums())->getUserWallAlbum($this->getUser());
} }
return (object) [ return (object) [
@ -121,20 +128,21 @@ final class Photos extends VKAPIRequestHandler
]; ];
} }
function saveWallPhoto(string $photo, string $hash, int $group_id = 0, ?string $caption = NULL): array public function saveWallPhoto(string $photo, string $hash, int $group_id = 0, ?string $caption = null): array
{ {
$imagePath = $this->getImagePath($photo, $hash, $uploader, $group); $imagePath = $this->getImagePath($photo, $hash, $uploader, $group);
if($group_id != $group) if ($group_id != $group) {
$this->fail(8, "group_id doesn't match"); $this->fail(8, "group_id doesn't match");
}
$album = NULL; $album = null;
if($group_id != 0) { if ($group_id != 0) {
$uploader = (new \openvk\Web\Models\Repositories\Users)->get((int) $uploader); $uploader = (new \openvk\Web\Models\Repositories\Users())->get((int) $uploader);
$album = (new Albums)->getUserWallAlbum($uploader); $album = (new Albums())->getUserWallAlbum($uploader);
} }
try { try {
$photo = new Photo; $photo = new Photo();
$photo->setOwner((int) $uploader); $photo->setOwner((int) $uploader);
$photo->setCreated(time()); $photo->setCreated(time());
$photo->setFile([ $photo->setFile([
@ -142,25 +150,27 @@ final class Photos extends VKAPIRequestHandler
"error" => 0, "error" => 0,
]); ]);
if (!is_null($caption)) if (!is_null($caption)) {
$photo->setDescription($caption); $photo->setDescription($caption);
}
$photo->save(); $photo->save();
unlink($imagePath); unlink($imagePath);
} catch(ImageException | InvalidStateException $e) { } catch (ImageException | InvalidStateException $e) {
unlink($imagePath); unlink($imagePath);
$this->fail(129, "Invalid image file"); $this->fail(129, "Invalid image file");
} }
if(!is_null($album)) if (!is_null($album)) {
$album->addPhoto($photo); $album->addPhoto($photo);
}
return [ return [
$photo->toVkApiStruct(), $photo->toVkApiStruct(),
]; ];
} }
function getUploadServer(?int $album_id = NULL): object public function getUploadServer(?int $album_id = null): object
{ {
$this->requireUser(); $this->requireUser();
@ -172,34 +182,37 @@ final class Photos extends VKAPIRequestHandler
]; ];
} }
function save(string $photos_list, string $hash, int $album_id = 0, ?string $caption = NULL): object public function save(string $photos_list, string $hash, int $album_id = 0, ?string $caption = null): object
{ {
$this->requireUser(); $this->requireUser();
$secret = CHANDLER_ROOT_CONF["security"]["secret"]; $secret = CHANDLER_ROOT_CONF["security"]["secret"];
if(!hash_equals(hash_hmac("sha3-224", $photos_list, $secret), $hash)) if (!hash_equals(hash_hmac("sha3-224", $photos_list, $secret), $hash)) {
$this->fail(121, "Incorrect hash"); $this->fail(121, "Incorrect hash");
}
$album = NULL; $album = null;
if($album_id != 0) { if ($album_id != 0) {
$album_ = (new Albums)->get($album_id); $album_ = (new Albums())->get($album_id);
if(!$album_) if (!$album_) {
$this->fail(0404, "Invalid album"); $this->fail(0o404, "Invalid album");
else if(!$album_->canBeModifiedBy($this->getUser())) } elseif (!$album_->canBeModifiedBy($this->getUser())) {
$this->fail(15, "Access: Album can't be 'written' by user"); $this->fail(15, "Access: Album can't be 'written' by user");
}
$album = $album_; $album = $album_;
} }
$pList = json_decode($photos_list); $pList = json_decode($photos_list);
$imagePaths = []; $imagePaths = [];
foreach($pList as $pDesc) foreach ($pList as $pDesc) {
$imagePaths[] = __DIR__ . "/../../tmp/api-storage/photos/$pDesc->keyholder" . "_$pDesc->resource.oct"; $imagePaths[] = __DIR__ . "/../../tmp/api-storage/photos/$pDesc->keyholder" . "_$pDesc->resource.oct";
}
$images = []; $images = [];
try { try {
foreach($imagePaths as $imagePath) { foreach ($imagePaths as $imagePath) {
$photo = new Photo; $photo = new Photo();
$photo->setOwner($this->getUser()->getId()); $photo->setOwner($this->getUser()->getId());
$photo->setCreated(time()); $photo->setCreated(time());
$photo->setFile([ $photo->setFile([
@ -207,20 +220,23 @@ final class Photos extends VKAPIRequestHandler
"error" => 0, "error" => 0,
]); ]);
if (!is_null($caption)) if (!is_null($caption)) {
$photo->setDescription($caption); $photo->setDescription($caption);
}
$photo->save(); $photo->save();
unlink($imagePath); unlink($imagePath);
if(!is_null($album)) if (!is_null($album)) {
$album->addPhoto($photo); $album->addPhoto($photo);
}
$images[] = $photo->toVkApiStruct(); $images[] = $photo->toVkApiStruct();
} }
} catch(ImageException | InvalidStateException $e) { } catch (ImageException | InvalidStateException $e) {
foreach($imagePaths as $imagePath) foreach ($imagePaths as $imagePath) {
unlink($imagePath); unlink($imagePath);
}
$this->fail(129, "Invalid image file"); $this->fail(129, "Invalid image file");
} }
@ -231,20 +247,20 @@ final class Photos extends VKAPIRequestHandler
]; ];
} }
function createAlbum(string $title, int $group_id = 0, string $description = "", int $privacy = 0) public function createAlbum(string $title, int $group_id = 0, string $description = "", int $privacy = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if($group_id != 0) { if ($group_id != 0) {
$club = (new Clubs)->get((int) $group_id); $club = (new Clubs())->get((int) $group_id);
if(!$club || !$club->canBeModifiedBy($this->getUser())) { if (!$club || !$club->canBeModifiedBy($this->getUser())) {
$this->fail(20, "Invalid club"); $this->fail(20, "Invalid club");
} }
} }
$album = new Album; $album = new Album();
$album->setOwner(isset($club) ? $club->getId() * -1 : $this->getUser()->getId()); $album->setOwner(isset($club) ? $club->getId() * -1 : $this->getUser()->getId());
$album->setName($title); $album->setName($title);
$album->setDescription($description); $album->setDescription($description);
@ -254,26 +270,26 @@ final class Photos extends VKAPIRequestHandler
return $album->toVkApiStruct($this->getUser()); return $album->toVkApiStruct($this->getUser());
} }
function editAlbum(int $album_id, int $owner_id, string $title, string $description = "", int $privacy = 0) public function editAlbum(int $album_id, int $owner_id, string $title, string $description = "", int $privacy = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$album = (new Albums)->getAlbumByOwnerAndId($owner_id, $album_id); $album = (new Albums())->getAlbumByOwnerAndId($owner_id, $album_id);
if(!$album || $album->isDeleted()) { if (!$album || $album->isDeleted()) {
$this->fail(2, "Invalid album"); $this->fail(2, "Invalid album");
} }
if(empty($title)) { if (empty($title)) {
$this->fail(25, "Title is empty"); $this->fail(25, "Title is empty");
} }
if($album->isCreatedBySystem()) { if ($album->isCreatedBySystem()) {
$this->fail(40, "You can't change system album"); $this->fail(40, "You can't change system album");
} }
if(!$album->canBeModifiedBy($this->getUser())) { if (!$album->canBeModifiedBy($this->getUser())) {
$this->fail(2, "Access to album denied"); $this->fail(2, "Access to album denied");
} }
@ -285,52 +301,55 @@ final class Photos extends VKAPIRequestHandler
return $album->toVkApiStruct($this->getUser()); return $album->toVkApiStruct($this->getUser());
} }
function getAlbums(int $owner_id, string $album_ids = "", int $offset = 0, int $count = 100, bool $need_system = true, bool $need_covers = true, bool $photo_sizes = false) public function getAlbums(int $owner_id, string $album_ids = "", int $offset = 0, int $count = 100, bool $need_system = true, bool $need_covers = true, bool $photo_sizes = false)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$res = []; $res = [];
if(empty($album_ids)) { if (empty($album_ids)) {
if($owner_id > 0) { if ($owner_id > 0) {
$user = (new UsersRepo)->get($owner_id); $user = (new UsersRepo())->get($owner_id);
$res = [ $res = [
"count" => (new Albums)->getUserAlbumsCount($user), "count" => (new Albums())->getUserAlbumsCount($user),
"items" => [] "items" => [],
]; ];
if(!$user || $user->isDeleted()) if (!$user || $user->isDeleted()) {
$this->fail(2, "Invalid user"); $this->fail(2, "Invalid user");
}
if(!$user->getPrivacyPermission('photos.read', $this->getUser())) if (!$user->getPrivacyPermission('photos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his albums."); $this->fail(21, "This user chose to hide his albums.");
}
$albums = array_slice(iterator_to_array((new Albums)->getUserAlbums($user, 1, $count + $offset)), $offset); $albums = array_slice(iterator_to_array((new Albums())->getUserAlbums($user, 1, $count + $offset)), $offset);
foreach($albums as $album) { foreach ($albums as $album) {
if(!$need_system && $album->isCreatedBySystem()) continue; if (!$need_system && $album->isCreatedBySystem()) {
continue;
}
$res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes); $res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes);
} }
} } else {
$club = (new Clubs())->get($owner_id * -1);
else {
$club = (new Clubs)->get($owner_id * -1);
$res = [ $res = [
"count" => (new Albums)->getClubAlbumsCount($club), "count" => (new Albums())->getClubAlbumsCount($club),
"items" => [] "items" => [],
]; ];
if(!$club) if (!$club) {
$this->fail(2, "Invalid club"); $this->fail(2, "Invalid club");
}
$albums = array_slice(iterator_to_array((new Albums)->getClubAlbums($club, 1, $count + $offset)), $offset);
foreach($albums as $album) { $albums = array_slice(iterator_to_array((new Albums())->getClubAlbums($club, 1, $count + $offset)), $offset);
if(!$need_system && $album->isCreatedBySystem()) continue;
foreach ($albums as $album) {
if (!$need_system && $album->isCreatedBySystem()) {
continue;
}
$res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes); $res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes);
} }
} }
@ -340,16 +359,17 @@ final class Photos extends VKAPIRequestHandler
$res = [ $res = [
"count" => sizeof($albums), "count" => sizeof($albums),
"items" => [] "items" => [],
]; ];
foreach($albums as $album) foreach ($albums as $album) {
{
$id = explode("_", $album); $id = explode("_", $album);
$album = (new Albums)->getAlbumByOwnerAndId((int)$id[0], (int)$id[1]); $album = (new Albums())->getAlbumByOwnerAndId((int) $id[0], (int) $id[1]);
if($album && !$album->isDeleted()) { if ($album && !$album->isDeleted()) {
if(!$need_system && $album->isCreatedBySystem()) continue; if (!$need_system && $album->isCreatedBySystem()) {
continue;
}
$res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes); $res["items"][] = $album->toVkApiStruct($this->getUser(), $need_covers, $photo_sizes);
} }
} }
@ -358,62 +378,54 @@ final class Photos extends VKAPIRequestHandler
return $res; return $res;
} }
function getAlbumsCount(int $user_id = 0, int $group_id = 0) public function getAlbumsCount(int $user_id = 0, int $group_id = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
if($user_id == 0 && $group_id == 0 || $user_id > 0 && $group_id > 0) { if ($user_id == 0 && $group_id == 0 || $user_id > 0 && $group_id > 0) {
$this->fail(21, "Select user_id or group_id"); $this->fail(21, "Select user_id or group_id");
} }
if($user_id > 0) { if ($user_id > 0) {
$us = (new UsersRepo())->get($user_id);
$us = (new UsersRepo)->get($user_id); if (!$us || $us->isDeleted()) {
if(!$us || $us->isDeleted()) {
$this->fail(21, "Invalid user"); $this->fail(21, "Invalid user");
} }
if(!$us->getPrivacyPermission('photos.read', $this->getUser())) { if (!$us->getPrivacyPermission('photos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his albums."); $this->fail(21, "This user chose to hide his albums.");
} }
return (new Albums)->getUserAlbumsCount($us); return (new Albums())->getUserAlbumsCount($us);
} }
if($group_id > 0) if ($group_id > 0) {
{ $cl = (new Clubs())->get($group_id);
$cl = (new Clubs)->get($group_id); if (!$cl) {
if(!$cl) {
$this->fail(21, "Invalid club"); $this->fail(21, "Invalid club");
} }
return (new Albums)->getClubAlbumsCount($cl); return (new Albums())->getClubAlbumsCount($cl);
} }
} }
function getById(string $photos, bool $extended = false, bool $photo_sizes = false) public function getById(string $photos, bool $extended = false, bool $photo_sizes = false)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$phts = explode(",", $photos); $phts = explode(",", $photos);
$res = []; $res = [];
foreach($phts as $phota) { foreach ($phts as $phota) {
$ph = explode("_", $phota); $ph = explode("_", $phota);
$photo = (new PhotosRepo)->getByOwnerAndVID((int)$ph[0], (int)$ph[1]); $photo = (new PhotosRepo())->getByOwnerAndVID((int) $ph[0], (int) $ph[1]);
if(!$photo || $photo->isDeleted()) { if (!$photo || $photo->isDeleted()) {
$this->fail(21, "Invalid photo"); $this->fail(21, "Invalid photo");
} }
if($photo->getOwner()->isDeleted()) { if (!$photo->canBeViewedBy($this->getUser())) {
$this->fail(21, "Owner of this photo is deleted"); $this->fail(15, "Access denied");
}
if(!$photo->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his photos.");
} }
$res[] = $photo->toVkApiStruct($photo_sizes, $extended); $res[] = $photo->toVkApiStruct($photo_sizes, $extended);
@ -422,29 +434,30 @@ final class Photos extends VKAPIRequestHandler
return $res; return $res;
} }
function get(int $owner_id, int $album_id, string $photo_ids = "", bool $extended = false, bool $photo_sizes = false, int $offset = 0, int $count = 10) public function get(int $owner_id, int $album_id, string $photo_ids = "", bool $extended = false, bool $photo_sizes = false, int $offset = 0, int $count = 10)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$res = []; $res = [];
if(empty($photo_ids)) { if (empty($photo_ids)) {
$album = (new Albums)->getAlbumByOwnerAndId($owner_id, $album_id); $album = (new Albums())->getAlbumByOwnerAndId($owner_id, $album_id);
if(!$album->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) { if (!$album || $album->isDeleted()) {
$this->fail(21, "This user chose to hide his albums.");
}
if(!$album || $album->isDeleted()) {
$this->fail(21, "Invalid album"); $this->fail(21, "Invalid album");
} }
$photos = array_slice(iterator_to_array($album->getPhotos(1, $count + $offset)), $offset);
$res["count"] = sizeof($photos);
foreach($photos as $photo) { if (!$album->canBeViewedBy($this->getUser())) {
if(!$photo || $photo->isDeleted()) continue; $this->fail(15, "Access denied");
}
$photos = array_slice(iterator_to_array($album->getPhotos(1, $count + $offset)), $offset);
$res["count"] = $album->size();
foreach ($photos as $photo) {
if (!$photo || $photo->isDeleted()) {
continue;
}
$res["items"][] = $photo->toVkApiStruct($photo_sizes, $extended); $res["items"][] = $photo->toVkApiStruct($photo_sizes, $extended);
} }
@ -453,15 +466,14 @@ final class Photos extends VKAPIRequestHandler
$res = [ $res = [
"count" => sizeof($photos), "count" => sizeof($photos),
"items" => [] "items" => [],
]; ];
foreach($photos as $photo) foreach ($photos as $photo) {
{
$id = explode("_", $photo); $id = explode("_", $photo);
$phot = (new PhotosRepo)->getByOwnerAndVID((int)$id[0], (int)$id[1]); $phot = (new PhotosRepo())->getByOwnerAndVID((int) $id[0], (int) $id[1]);
if($phot && !$phot->isDeleted()) { if ($phot && !$phot->isDeleted() && $phot->canBeViewedBy($this->getUser())) {
$res["items"][] = $phot->toVkApiStruct($photo_sizes, $extended); $res["items"][] = $phot->toVkApiStruct($photo_sizes, $extended);
} }
} }
@ -470,18 +482,18 @@ final class Photos extends VKAPIRequestHandler
return $res; return $res;
} }
function deleteAlbum(int $album_id, int $group_id = 0) public function deleteAlbum(int $album_id, int $group_id = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$album = (new Albums)->get($album_id); $album = (new Albums())->get($album_id);
if(!$album || $album->canBeModifiedBy($this->getUser())) { if (!$album || $album->canBeModifiedBy($this->getUser())) {
$this->fail(21, "Invalid album"); $this->fail(21, "Invalid album");
} }
if($album->isDeleted()) { if ($album->isDeleted()) {
$this->fail(22, "Album already deleted"); $this->fail(22, "Album already deleted");
} }
@ -490,22 +502,22 @@ final class Photos extends VKAPIRequestHandler
return 1; return 1;
} }
function edit(int $owner_id, int $photo_id, string $caption = "") public function edit(int $owner_id, int $photo_id, string $caption = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $photo_id); $photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $photo_id);
if(!$photo) { if (!$photo) {
$this->fail(21, "Invalid photo"); $this->fail(21, "Invalid photo");
} }
if($photo->isDeleted()) { if ($photo->isDeleted()) {
$this->fail(21, "Photo is deleted"); $this->fail(21, "Photo is deleted");
} }
if(!empty($caption)) { if (!empty($caption)) {
$photo->setDescription($caption); $photo->setDescription($caption);
$photo->save(); $photo->save();
} }
@ -513,45 +525,44 @@ final class Photos extends VKAPIRequestHandler
return 1; return 1;
} }
function delete(int $owner_id, int $photo_id, string $photos = "") public function delete(int $owner_id, int $photo_id, string $photos = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if(empty($photos)) { if (empty($photos)) {
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $photo_id); $photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $photo_id);
if($this->getUser()->getId() !== $photo->getOwner()->getId()) { if ($this->getUser()->getId() !== $photo->getOwner()->getId()) {
$this->fail(21, "You can't delete another's photo"); $this->fail(21, "You can't delete another's photo");
} }
if(!$photo) { if (!$photo) {
$this->fail(21, "Invalid photo"); $this->fail(21, "Invalid photo");
} }
if($photo->isDeleted()) { if ($photo->isDeleted()) {
$this->fail(21, "Photo already deleted"); $this->fail(21, "Photo is already deleted");
} }
$photo->delete(); $photo->delete();
} else { } else {
$photozs = explode(',', $photos); $photozs = explode(',', $photos);
foreach($photozs as $photo) foreach ($photozs as $photo) {
{
$id = explode("_", $photo); $id = explode("_", $photo);
$phot = (new PhotosRepo)->getByOwnerAndVID((int)$id[0], (int)$id[1]);
if($this->getUser()->getId() !== $phot->getOwner()->getId()) { $phot = (new PhotosRepo())->getByOwnerAndVID((int) $id[0], (int) $id[1]);
if ($this->getUser()->getId() !== $phot->getOwner()->getId()) {
$this->fail(21, "You can't delete another's photo"); $this->fail(21, "You can't delete another's photo");
} }
if(!$phot) { if (!$phot) {
$this->fail(21, "Invalid photo"); $this->fail(21, "Invalid photo");
} }
if($phot->isDeleted()) { if ($phot->isDeleted()) {
$this->fail(21, "Photo already deleted"); $this->fail(21, "Photo already deleted");
} }
@ -562,27 +573,23 @@ final class Photos extends VKAPIRequestHandler
return 1; return 1;
} }
function getAllComments(int $owner_id, int $album_id, bool $need_likes = false, int $offset = 0, int $count = 100) public function getAllComments(int $owner_id, int $album_id, bool $need_likes = false, int $offset = 0, int $count = 100)
{ {
$this->fail(501, "Not implemented"); $this->fail(501, "Not implemented");
} }
function deleteComment(int $comment_id, int $owner_id = 0) public function deleteComment(int $comment_id, int $owner_id = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
$comment = (new CommentsRepo)->get($comment_id); $comment = (new CommentsRepo())->get($comment_id);
if(!$comment) { if (!$comment) {
$this->fail(21, "Invalid comment"); $this->fail(21, "Invalid comment");
} }
if(!$comment->canBeModifiedBy($this->getUser())) { if (!$comment->canBeModifiedBy($this->getUser())) {
$this->fail(21, "Forbidden"); $this->fail(21, "Access denied");
}
if($comment->isDeleted()) {
$this->fail(4, "Comment already deleted");
} }
$comment->delete(); $comment->delete();
@ -590,27 +597,26 @@ final class Photos extends VKAPIRequestHandler
return 1; return 1;
} }
function createComment(int $owner_id, int $photo_id, string $message = "", string $attachments = "", bool $from_group = false) public function createComment(int $owner_id, int $photo_id, string $message = "", string $attachments = "", bool $from_group = false)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if(empty($message) && empty($attachments)) { if (empty($message) && empty($attachments)) {
$this->fail(100, "Required parameter 'message' missing."); $this->fail(100, "Required parameter 'message' missing.");
} }
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $photo_id); $photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $photo_id);
if(!$photo->getAlbum()->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) { if (!$photo || $photo->isDeleted()) {
$this->fail(21, "This user chose to hide his albums."); $this->fail(180, "Invalid photo");
} }
if(!$photo) if (!$photo->canBeViewedBy($this->getUser())) {
$this->fail(180, "Photo not found"); $this->fail(15, "Access to photo denied");
if($photo->isDeleted()) }
$this->fail(189, "Photo is deleted");
$comment = new Comment; $comment = new Comment();
$comment->setOwner($this->getUser()->getId()); $comment->setOwner($this->getUser()->getId());
$comment->setModel(get_class($photo)); $comment->setModel(get_class($photo));
$comment->setTarget($photo->getId()); $comment->setTarget($photo->getId());
@ -618,43 +624,49 @@ final class Photos extends VKAPIRequestHandler
$comment->setCreated(time()); $comment->setCreated(time());
$comment->save(); $comment->save();
if(!empty($attachments)) { if (!empty($attachments)) {
$attachmentsArr = explode(",", $attachments); $attachmentsArr = explode(",", $attachments);
if(sizeof($attachmentsArr) > 10) if (sizeof($attachmentsArr) > 10) {
$this->fail(50, "Error: too many attachments"); $this->fail(50, "Error: too many attachments");
}
foreach($attachmentsArr as $attac) {
$attachmentType = NULL;
if(str_contains($attac, "photo")) foreach ($attachmentsArr as $attac) {
$attachmentType = null;
if (str_contains($attac, "photo")) {
$attachmentType = "photo"; $attachmentType = "photo";
elseif(str_contains($attac, "video")) } elseif (str_contains($attac, "video")) {
$attachmentType = "video"; $attachmentType = "video";
else } else {
$this->fail(205, "Unknown attachment type"); $this->fail(205, "Unknown attachment type");
}
$attachment = str_replace($attachmentType, "", $attac); $attachment = str_replace($attachmentType, "", $attac);
$attachmentOwner = (int)explode("_", $attachment)[0]; $attachmentOwner = (int) explode("_", $attachment)[0];
$attachmentId = (int)end(explode("_", $attachment)); $attachmentId = (int) end(explode("_", $attachment));
$attacc = NULL; $attacc = null;
if($attachmentType == "photo") { if ($attachmentType == "photo") {
$attacc = (new PhotosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new PhotosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Photo does not exists"); $this->fail(100, "Photo does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this photo"); $this->fail(43, "You do not have access to this photo");
}
$comment->attach($attacc); $comment->attach($attacc);
} elseif($attachmentType == "video") { } elseif ($attachmentType == "video") {
$attacc = (new VideosRepo)->getByOwnerAndVID($attachmentOwner, $attachmentId); $attacc = (new VideosRepo())->getByOwnerAndVID($attachmentOwner, $attachmentId);
if(!$attacc || $attacc->isDeleted()) if (!$attacc || $attacc->isDeleted()) {
$this->fail(100, "Video does not exists"); $this->fail(100, "Video does not exists");
if($attacc->getOwner()->getId() != $this->getUser()->getId()) }
if ($attacc->getOwner()->getId() != $this->getUser()->getId()) {
$this->fail(43, "You do not have access to this video"); $this->fail(43, "You do not have access to this video");
}
$comment->attach($attacc); $comment->attach($attacc);
} }
@ -664,65 +676,63 @@ final class Photos extends VKAPIRequestHandler
return $comment->getId(); return $comment->getId();
} }
function getAll(int $owner_id, bool $extended = false, int $offset = 0, int $count = 100, bool $photo_sizes = false) public function getAll(int $owner_id, bool $extended = false, int $offset = 0, int $count = 100, bool $photo_sizes = false)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
if($owner_id < 0) { if ($owner_id < 0) {
$this->fail(4, "This method doesn't works with clubs"); $this->fail(4, "This method doesn't works with clubs");
} }
$user = (new UsersRepo)->get($owner_id); $user = (new UsersRepo())->get($owner_id);
if (!$user) {
if(!$user) {
$this->fail(4, "Invalid user"); $this->fail(4, "Invalid user");
} }
if(!$user->getPrivacyPermission('photos.read', $this->getUser())) { if (!$user->getPrivacyPermission('photos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his albums."); $this->fail(21, "This user chose to hide his albums.");
} }
$photos = array_slice(iterator_to_array((new PhotosRepo)->getEveryUserPhoto($user, 1, $count + $offset)), $offset); $photos = (new PhotosRepo())->getEveryUserPhoto($user, $offset, $count);
$res = []; $res = [
"count" => (new PhotosRepo())->getUserPhotosCount($user),
"items" => [],
];
foreach($photos as $photo) { foreach ($photos as $photo) {
if(!$photo || $photo->isDeleted()) continue; if (!$photo || $photo->isDeleted()) {
continue;
}
$res["items"][] = $photo->toVkApiStruct($photo_sizes, $extended); $res["items"][] = $photo->toVkApiStruct($photo_sizes, $extended);
} }
return $res; return $res;
} }
function getComments(int $owner_id, int $photo_id, bool $need_likes = false, int $offset = 0, int $count = 100, bool $extended = false, string $fields = "") public function getComments(int $owner_id, int $photo_id, bool $need_likes = false, int $offset = 0, int $count = 100, bool $extended = false, string $fields = "")
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction();
$photo = (new PhotosRepo)->getByOwnerAndVID($owner_id, $photo_id); $photo = (new PhotosRepo())->getByOwnerAndVID($owner_id, $photo_id);
$comms = array_slice(iterator_to_array($photo->getComments(1, $offset + $count)), $offset); $comms = array_slice(iterator_to_array($photo->getComments(1, $offset + $count)), $offset);
if(!$photo) { if (!$photo || $photo->isDeleted()) {
$this->fail(4, "Invalid photo"); $this->fail(4, "Invalid photo");
} }
if(!$photo->getAlbum()->getOwner()->getPrivacyPermission('photos.read', $this->getUser())) { if (!$photo->canBeViewedBy($this->getUser())) {
$this->fail(21, "This user chose to hide his photos."); $this->fail(21, "Access denied");
}
if($photo->isDeleted()) {
$this->fail(4, "Photo is deleted");
} }
$res = [ $res = [
"count" => sizeof($comms), "count" => sizeof($comms),
"items" => [] "items" => [],
]; ];
foreach($comms as $comment) { foreach ($comms as $comment) {
$res["items"][] = $comment->toVkApiStruct($this->getUser(), $need_likes, $extended); $res["items"][] = $comment->toVkApiStruct($this->getUser(), $need_likes, $extended);
if($extended) { if ($extended) {
if($comment->getOwner() instanceof \openvk\Web\Models\Entities\User) { if ($comment->getOwner() instanceof \openvk\Web\Models\Entities\User) {
$res["profiles"][] = $comment->getOwner()->toVkApiStruct(); $res["profiles"][] = $comment->getOwner()->toVkApiStruct();
} }
} }
@ -730,4 +740,4 @@ final class Photos extends VKAPIRequestHandler
return $res; return $res;
} }
} }

View file

@ -1,107 +1,184 @@
<?php declare(strict_types=1); <?php
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User; declare(strict_types=1);
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Entities\Poll; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Exceptions\AlreadyVotedException;
use openvk\Web\Models\Exceptions\InvalidOptionException; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Exceptions\PollLockedException; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Polls as PollsRepo; use openvk\Web\Models\Entities\Poll;
use openvk\Web\Models\Exceptions\AlreadyVotedException;
final class Polls extends VKAPIRequestHandler use openvk\Web\Models\Exceptions\InvalidOptionException;
{ use openvk\Web\Models\Exceptions\PollLockedException;
function getById(int $poll_id, bool $extended = false, string $fields = "sex,screen_name,photo_50,photo_100,online_info,online") use openvk\Web\Models\Repositories\Polls as PollsRepo;
{
$poll = (new PollsRepo)->get($poll_id); final class Polls extends VKAPIRequestHandler
{
if (!$poll) public function getById(int $poll_id, bool $extended = false, string $fields = "sex,screen_name,photo_50,photo_100,online_info,online")
$this->fail(100, "One of the parameters specified was missing or invalid: poll_id is incorrect"); {
$poll = (new PollsRepo())->get($poll_id);
$users = array();
$answers = array(); if (!$poll) {
foreach($poll->getResults()->options as $answer) { $this->fail(100, "One of the parameters specified was missing or invalid: poll_id is incorrect");
$answers[] = (object)[ }
"id" => $answer->id,
"rate" => $answer->pct, $users = [];
"text" => $answer->name, $answers = [];
"votes" => $answer->votes foreach ($poll->getResults()->options as $answer) {
]; $answers[] = (object) [
} "id" => $answer->id,
"rate" => $answer->pct,
$userVote = array(); "text" => $answer->name,
foreach($poll->getUserVote($this->getUser()) as $vote) "votes" => $answer->votes,
$userVote[] = $vote[0]; ];
}
$response = [
"multiple" => $poll->isMultipleChoice(), $userVote = [];
"end_date" => $poll->endsAt() == NULL ? 0 : $poll->endsAt()->timestamp(), foreach ($poll->getUserVote($this->getUser()) as $vote) {
"closed" => $poll->hasEnded(), $userVote[] = $vote[0];
"is_board" => false, }
"can_edit" => false,
"can_vote" => $poll->canVote($this->getUser()), $response = [
"can_report" => false, "multiple" => $poll->isMultipleChoice(),
"can_share" => true, "end_date" => $poll->endsAt() == null ? 0 : $poll->endsAt()->timestamp(),
"created" => 0, "closed" => $poll->hasEnded(),
"id" => $poll->getId(), "is_board" => false,
"owner_id" => $poll->getOwner()->getId(), "can_edit" => false,
"question" => $poll->getTitle(), "can_vote" => $poll->canVote($this->getUser()),
"votes" => $poll->getVoterCount(), "can_report" => false,
"disable_unvote" => $poll->isRevotable(), "can_share" => true,
"anonymous" => $poll->isAnonymous(), "created" => 0,
"answer_ids" => $userVote, "id" => $poll->getId(),
"answers" => $answers, "owner_id" => $poll->getOwner()->getId(),
"author_id" => $poll->getOwner()->getId(), "question" => $poll->getTitle(),
]; "votes" => $poll->getVoterCount(),
"disable_unvote" => $poll->isRevotable(),
if ($extended) { "anonymous" => $poll->isAnonymous(),
$response["profiles"] = (new Users)->get(strval($poll->getOwner()->getId()), $fields, 0, 1); "answer_ids" => $userVote,
/* Currently there is only one person that can be shown trough "Extended" param. "answers" => $answers,
* As "friends" param will be implemented, "profiles" will show more users "author_id" => $poll->getOwner()->getId(),
*/ ];
}
if ($extended) {
return (object) $response; $response["profiles"] = (new Users())->get(strval($poll->getOwner()->getId()), $fields, 0, 1);
} /* Currently there is only one person that can be shown trough "Extended" param.
* As "friends" param will be implemented, "profiles" will show more users
function addVote(int $poll_id, string $answers_ids) */
{ }
$this->requireUser();
$this->willExecuteWriteAction(); return (object) $response;
}
$poll = (new PollsRepo)->get($poll_id);
public function addVote(int $poll_id, string $answers_ids)
if(!$poll) {
$this->fail(251, "Invalid poll id"); $this->requireUser();
$this->willExecuteWriteAction();
try {
$poll->vote($this->getUser(), explode(",", $answers_ids)); $poll = (new PollsRepo())->get($poll_id);
return 1;
} catch(AlreadyVotedException $ex) { if (!$poll) {
return 0; $this->fail(251, "Invalid poll id");
} catch(PollLockedException $ex) { }
return 0;
} catch(InvalidOptionException $ex) { try {
$this->fail(8, "бдсм вибратор купить в киеве"); $poll->vote($this->getUser(), explode(",", $answers_ids));
} return 1;
} } catch (AlreadyVotedException $ex) {
return 0;
function deleteVote(int $poll_id) } catch (PollLockedException $ex) {
{ return 0;
$this->requireUser(); } catch (InvalidOptionException $ex) {
$this->willExecuteWriteAction(); $this->fail(8, "бдсм вибратор купить в киеве");
}
$poll = (new PollsRepo)->get($poll_id); }
if(!$poll) public function deleteVote(int $poll_id)
$this->fail(251, "Invalid poll id"); {
$this->requireUser();
try { $this->willExecuteWriteAction();
$poll->revokeVote($this->getUser());
return 1; $poll = (new PollsRepo())->get($poll_id);
} catch(PollLockedException $ex) {
$this->fail(15, "Access denied: Poll is locked or isn't revotable"); if (!$poll) {
} catch(InvalidOptionException $ex) { $this->fail(251, "Invalid poll id");
$this->fail(8, "how.to. ook.bacon.in.microwova."); }
}
} try {
} $poll->revokeVote($this->getUser());
return 1;
} catch (PollLockedException $ex) {
$this->fail(15, "Access denied: Poll is locked or isn't revotable");
} catch (InvalidOptionException $ex) {
$this->fail(8, "how.to. ook.bacon.in.microwova.");
}
}
public function getVoters(int $poll_id, int $answer_ids, int $offset = 0, int $count = 6)
{
$this->requireUser();
$poll = (new PollsRepo())->get($poll_id);
if (!$poll) {
$this->fail(251, "Invalid poll");
}
if ($poll->isAnonymous()) {
$this->fail(251, "Access denied: poll is anonymous.");
}
$voters = array_slice($poll->getVoters($answer_ids, 1, $offset + $count), $offset);
$res = (object) [
"answer_id" => $answer_ids,
"users" => [],
];
foreach ($voters as $voter) {
$res->users[] = $voter->toVkApiStruct();
}
return $res;
}
public function create(string $question, string $add_answers, bool $disable_unvote = false, bool $is_anonymous = false, bool $is_multiple = false, int $end_date = 0)
{
$this->requireUser();
$this->willExecuteWriteAction();
$options = json_decode($add_answers);
if (!$options || empty($options)) {
$this->fail(62, "Invalid options");
}
if (sizeof($options) > ovkGetQuirk("polls.max-opts")) {
$this->fail(51, "Too many options");
}
$poll = new Poll();
$poll->setOwner($this->getUser());
$poll->setTitle($question);
$poll->setMultipleChoice($is_multiple);
$poll->setAnonymity($is_anonymous);
$poll->setRevotability(!$disable_unvote);
$poll->setOptions($options);
if ($end_date > time()) {
if ($end_date > time() + (DAY * 365)) {
$this->fail(89, "End date is too big");
}
$poll->setEndDate($end_date);
}
$poll->save();
return $this->getById($poll->getId());
}
public function edit()
{
#todo
return 1;
}
}

View file

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\Report;
use openvk\Web\Models\Repositories\Reports as ReportsRepo;
final class Reports extends VKAPIRequestHandler
{
public function add(int $owner_id = 0, string $comment = "", int $reason = 0, string $type = "", string $report_source = ""): int
{
$this->requireUser();
$this->willExecuteWriteAction();
$allowed_types = ["post", "photo", "video", "group", "comment", "note", "app", "user", "audio"];
if ($type == "" || !in_array($type, $allowed_types)) {
$this->fail(100, "One of the parameters specified was missing or invalid: type should be " . implode(", ", $allowed_types));
}
if ($owner_id <= 0) {
$this->fail(100, "One of the parameters specified was missing or invalid: Bad input");
}
if (mb_strlen($comment) === 0) {
$this->fail(100, "One of the parameters specified was missing or invalid: Comment can't be empty");
}
if ($type == "user" && $owner_id == $this->getUser()->getId()) {
return 1;
}
if ($this->getUser()->isBannedInSupport()) {
return 0;
}
if (sizeof(iterator_to_array((new ReportsRepo())->getDuplicates($type, $owner_id, null, $this->getUser()->getId()))) > 0) {
return 1;
}
try {
$report = new Report();
$report->setUser_id($this->getUser()->getId());
$report->setTarget_id($owner_id);
$report->setType($type);
$report->setReason($comment);
$report->setCreated(time());
$report->save();
} catch (\Throwable $e) {
$this->fail(-1, "Unknown error failed");
}
return 1;
}
}

View file

@ -1,34 +1,54 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
final class Status extends VKAPIRequestHandler final class Status extends VKAPIRequestHandler
{ {
function get(int $user_id = 0, int $group_id = 0) public function get(int $user_id = 0, int $group_id = 0)
{ {
$this->requireUser(); $this->requireUser();
if($user_id == 0 && $group_id == 0) {
return $this->getUser()->getStatus(); if ($user_id == 0 && $group_id == 0) {
$user_id = $this->getUser()->getId();
}
if ($group_id > 0) {
$this->fail(501, "Group statuses are not implemented");
} else { } else {
if($group_id > 0) $user = (new UsersRepo())->get($user_id);
$this->fail(501, "Group statuses are not implemented");
else if (!$user || $user->isDeleted() || !$user->canBeViewedBy($this->getUser())) {
return (new UsersRepo)->get($user_id)->getStatus(); $this->fail(15, "Invalid user");
}
$audioStatus = $user->getCurrentAudioStatus();
if ($audioStatus) {
return [
"status" => $user->getStatus(),
"audio" => $audioStatus->toVkApiStruct(),
];
}
return $user->getStatus();
} }
} }
function set(string $text, int $group_id = 0) public function set(string $text, int $group_id = 0)
{ {
$this->requireUser(); $this->requireUser();
$this->willExecuteWriteAction(); $this->willExecuteWriteAction();
if($group_id > 0) { if ($group_id > 0) {
$this->fail(501, "Group statuses are not implemented"); $this->fail(501, "Group statuses are not implemented");
} else { } else {
$this->getUser()->setStatus($text); $this->getUser()->setStatus($text);
$this->getUser()->save(); $this->getUser()->save();
return 1; return 1;
} }
} }

View file

@ -1,292 +1,498 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Entities\{User, Report};
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\{Photos, Clubs, Albums, Videos, Notes, Audios};
use openvk\Web\Models\Repositories\Reports;
final class Users extends VKAPIRequestHandler final class Users extends VKAPIRequestHandler
{ {
function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $count = 100, User $authuser = null /* костыль(( */): array public function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $count = 100, User $authuser = null /* костыль(( */): array
{ {
if($authuser == NULL) $authuser = $this->getUser(); if ($authuser == null) {
$authuser = $this->getUser();
}
$users = new UsersRepo();
if ($user_ids == "0") {
if (!$authuser) {
return [];
}
$user_ids = (string) $authuser->getId();
}
$users = new UsersRepo;
if($user_ids == "0")
$user_ids = (string) $authuser->getId();
$usrs = explode(',', $user_ids); $usrs = explode(',', $user_ids);
$response = array(); $response = [];
$ic = sizeof($usrs); $ic = sizeof($usrs);
if(sizeof($usrs) > $count) if (sizeof($usrs) > $count) {
$ic = $count; $ic = $count;
}
$usrs = array_slice($usrs, $offset * $count); $usrs = array_slice($usrs, $offset * $count);
for($i=0; $i < $ic; $i++) { for ($i = 0; $i < $ic; $i++) {
if($usrs[$i] != 0) { if ($usrs[$i] != 0) {
$usr = $users->get((int) $usrs[$i]); $usr = $users->get((int) $usrs[$i]);
if(is_null($usr) || $usr->isDeleted()) { if (is_null($usr) || $usr->isDeleted()) {
$response[$i] = (object)[ $response[$i] = (object) [
"id" => (int) $usrs[$i], "id" => (int) $usrs[$i],
"first_name" => "DELETED", "first_name" => "DELETED",
"last_name" => "", "last_name" => "",
"deactivated" => "deleted" "deactivated" => "deleted",
]; ];
} else if($usr->isBanned()) { } elseif ($usr->isBanned()) {
$response[$i] = (object)[ $response[$i] = (object) [
"id" => $usr->getId(), "id" => $usr->getId(),
"first_name" => $usr->getFirstName(), "first_name" => $usr->getFirstName(true),
"last_name" => $usr->getLastName(), "last_name" => $usr->getLastName(true),
"deactivated" => "banned", "deactivated" => "banned",
"ban_reason" => $usr->getBanReason() "ban_reason" => $usr->getBanReason(),
]; ];
} else if($usrs[$i] == NULL) { } elseif ($usrs[$i] == null) {
} else { } else {
$response[$i] = (object)[ $response[$i] = (object) [
"id" => $usr->getId(), "id" => $usr->getId(),
"first_name" => $usr->getFirstName(), "first_name" => $usr->getFirstName(true),
"last_name" => $usr->getLastName(), "last_name" => $usr->getLastName(true),
"is_closed" => false, "is_closed" => $usr->isClosed(),
"can_access_closed" => true, "can_access_closed" => (bool) $usr->canBeViewedBy($this->getUser()),
]; ];
$flds = explode(',', $fields); $flds = explode(',', $fields);
$canView = $usr->canBeViewedBy($this->getUser());
foreach ($flds as $field) {
switch ($field) {
case "verified":
$response[$i]->verified = intval($usr->isVerified());
break;
case "sex":
$response[$i]->sex = $usr->isFemale() ? 1 : ($usr->isNeutral() ? 0 : 2);
break;
case "has_photo":
$response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break;
case "photo_max_orig":
$response[$i]->photo_max_orig = $usr->getAvatarURL();
break;
case "photo_max":
$response[$i]->photo_max = $usr->getAvatarURL("original");
break;
case "photo_50":
$response[$i]->photo_50 = $usr->getAvatarURL();
break;
case "photo_100":
$response[$i]->photo_100 = $usr->getAvatarURL("tiny");
break;
case "photo_200":
$response[$i]->photo_200 = $usr->getAvatarURL("normal");
break;
case "photo_200_orig": # вообще не ебу к чему эта строка ну пусть будет кек
$response[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break;
case "photo_400_orig":
$response[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break;
foreach($flds as $field) { # Она хочет быть выебанной видя матан
switch($field) { # Покайфу когда ты Виет а вокруг лишь дискриминант
case "verified":
$response[$i]->verified = intval($usr->isVerified());
break;
case "sex":
$response[$i]->sex = $usr->isFemale() ? 1 : 2;
break;
case "has_photo":
$response[$i]->has_photo = is_null($usr->getAvatarPhoto()) ? 0 : 1;
break;
case "photo_max_orig":
$response[$i]->photo_max_orig = $usr->getAvatarURL();
break;
case "photo_max":
$response[$i]->photo_max = $usr->getAvatarURL("original");
break;
case "photo_50":
$response[$i]->photo_50 = $usr->getAvatarURL();
break;
case "photo_100":
$response[$i]->photo_100 = $usr->getAvatarURL("tiny");
break;
case "photo_200":
$response[$i]->photo_200 = $usr->getAvatarURL("normal");
break;
case "photo_200_orig": # вообще не ебу к чему эта строка ну пусть будет кек
$response[$i]->photo_200_orig = $usr->getAvatarURL("normal");
break;
case "photo_400_orig":
$response[$i]->photo_400_orig = $usr->getAvatarURL("normal");
break;
# Она хочет быть выебанной видя матан
# Покайфу когда ты Виет а вокруг лишь дискриминант
# ору а когда я это успел написать # ору а когда я это успел написать
# вова кстати не матерись в коде мамка же спалит азщазаззазщазазаззазазазх # вова кстати не матерись в коде мамка же спалит азщазаззазщазазаззазазазх
case "status": case "status":
if($usr->getStatus() != NULL) if ($usr->getStatus() != null) {
$response[$i]->status = $usr->getStatus(); $response[$i]->status = $usr->getStatus();
break; }
case "screen_name":
if($usr->getShortCode() != NULL)
$response[$i]->screen_name = $usr->getShortCode();
break;
case "friend_status":
switch($usr->getSubscriptionStatus($authuser)) {
case 3:
# NOTICE falling through
case 0:
$response[$i]->friend_status = $usr->getSubscriptionStatus($authuser);
break;
case 1:
$response[$i]->friend_status = 2;
break;
case 2:
$response[$i]->friend_status = 1;
break;
}
break;
case "last_seen":
if ($usr->onlineStatus() == 0) {
$platform = $usr->getOnlinePlatform(true);
switch ($platform) {
case 'iphone':
$platform = 2;
break;
case 'android': $audioStatus = $usr->getCurrentAudioStatus();
$platform = 4;
break;
case NULL: if ($audioStatus) {
$platform = 7; $response[$i]->status_audio = $audioStatus->toVkApiStruct();
break; }
default:
$platform = 1;
break;
}
$response[$i]->last_seen = (object) [ break;
"platform" => $platform, case "screen_name":
"time" => $usr->getOnline()->timestamp() if ($usr->getShortCode() != null) {
]; $response[$i]->screen_name = $usr->getShortCode();
} }
case "music": break;
$response[$i]->music = $usr->getFavoriteMusic(); case "friend_status":
break; switch ($usr->getSubscriptionStatus($authuser)) {
case "movies": case 3:
$response[$i]->movies = $usr->getFavoriteFilms(); # NOTICE falling through
break; case 0:
case "tv": $response[$i]->friend_status = $usr->getSubscriptionStatus($authuser);
$response[$i]->tv = $usr->getFavoriteShows(); break;
break; case 1:
case "books": $response[$i]->friend_status = 2;
$response[$i]->books = $usr->getFavoriteBooks(); break;
break; case 2:
case "city": $response[$i]->friend_status = 1;
$response[$i]->city = $usr->getCity(); break;
break; }
case "interests": break;
$response[$i]->interests = $usr->getInterests(); case "last_seen":
break; if ($usr->onlineStatus() == 0) {
case "rating": $platform = $usr->getOnlinePlatform(true);
$response[$i]->rating = $usr->getRating(); switch ($platform) {
break; case 'iphone':
} $platform = 2;
} break;
if($usr->getOnline()->timestamp() + 300 > time()) case 'android':
$response[$i]->online = 1; $platform = 4;
else break;
$response[$i]->online = 0;
} case null:
} $platform = 7;
break;
default:
$platform = 1;
break;
}
$response[$i]->last_seen = (object) [
"platform" => $platform,
"time" => $usr->getOnline()->timestamp(),
];
}
// no break
case "music":
if (!$canView) {
break;
}
$response[$i]->music = $usr->getFavoriteMusic();
break;
case "movies":
if (!$canView) {
break;
}
$response[$i]->movies = $usr->getFavoriteFilms();
break;
case "tv":
if (!$canView) {
break;
}
$response[$i]->tv = $usr->getFavoriteShows();
break;
case "books":
if (!$canView) {
break;
}
$response[$i]->books = $usr->getFavoriteBooks();
break;
case "city":
if (!$canView) {
break;
}
$response[$i]->city = $usr->getCity();
break;
case "interests":
if (!$canView) {
break;
}
$response[$i]->interests = $usr->getInterests();
break;
case "quotes":
if (!$canView) {
break;
}
$response[$i]->quotes = $usr->getFavoriteQuote();
break;
case "games":
if (!$canView) {
break;
}
$response[$i]->games = $usr->getFavoriteGames();
break;
case "email":
if (!$canView) {
break;
}
$response[$i]->email = $usr->getContactEmail();
break;
case "telegram":
if (!$canView) {
break;
}
$response[$i]->telegram = $usr->getTelegram();
break;
case "about":
if (!$canView) {
break;
}
$response[$i]->about = $usr->getDescription();
break;
case "rating":
if (!$canView) {
break;
}
$response[$i]->rating = $usr->getRating();
break;
case "counters":
$response[$i]->counters = (object) [
"friends_count" => $usr->getFriendsCount(),
"photos_count" => (new Photos())->getUserPhotosCount($usr),
"videos_count" => (new Videos())->getUserVideosCount($usr),
"audios_count" => (new Audios())->getUserCollectionSize($usr),
"notes_count" => (new Notes())->getUserNotesCount($usr),
];
break;
case "correct_counters":
$response[$i]->counters = (object) [
"friends" => $usr->getFriendsCount(),
"photos" => (new Photos())->getUserPhotosCount($usr),
"videos" => (new Videos())->getUserVideosCount($usr),
"audios" => (new Audios())->getUserCollectionSize($usr),
"notes" => (new Notes())->getUserNotesCount($usr),
"groups" => $usr->getClubCount(),
"online_friends" => $usr->getFriendsOnlineCount(),
];
break;
case "guid":
$response[$i]->guid = $usr->getChandlerGUID();
break;
case 'background':
$backgrounds = $usr->getBackDropPictureURLs();
$response[$i]->background = $backgrounds;
break;
case 'reg_date':
if (!$canView) {
break;
}
$response[$i]->reg_date = $usr->getRegistrationTime()->timestamp();
break;
case 'is_dead':
$response[$i]->is_dead = $usr->isDead();
break;
case 'nickname':
$response[$i]->nickname = $usr->getPseudo();
break;
case 'blacklisted_by_me':
if (!$authuser) {
continue;
}
$response[$i]->blacklisted_by_me = (int) $usr->isBlacklistedBy($this->getUser());
break;
case 'blacklisted':
if (!$authuser) {
continue;
}
$response[$i]->blacklisted = (int) $this->getUser()->isBlacklistedBy($usr);
break;
case "custom_fields":
if (sizeof($usrs) > 1) {
break;
}
$c_fields = \openvk\Web\Models\Entities\UserInfoEntities\AdditionalField::getByOwner($usr->getId());
$append_array = [];
foreach ($c_fields as $c_field) {
$append_array[] = $c_field->toVkApiStruct();
}
$response[$i]->custom_fields = $append_array;
break;
}
}
if ($usr->getOnline()->timestamp() + 300 > time()) {
$response[$i]->online = 1;
} else {
$response[$i]->online = 0;
}
}
}
} }
return $response; return $response;
} }
function getFollowers(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object public function getFollowers(int $user_id, string $fields = "", int $offset = 0, int $count = 100): object
{ {
$offset++; $offset++;
$followers = []; $followers = [];
$users = new UsersRepo; $users = new UsersRepo();
$this->requireUser(); $this->requireUser();
foreach($users->get($user_id)->getFollowers($offset, $count) as $follower) $user = $users->get($user_id);
if (!$user || $user->isDeleted()) {
$this->fail(14, "Invalid user");
}
if (!$user->canBeViewedBy($this->getUser())) {
$this->fail(15, "Access denied");
}
foreach ($users->get($user_id)->getFollowers($offset, $count) as $follower) {
$followers[] = $follower->getId(); $followers[] = $follower->getId();
}
$response = $followers; $response = $followers;
if(!is_null($fields)) if (!is_null($fields)) {
$response = $this->get(implode(',', $followers), $fields, 0, $count); $response = $this->get(implode(',', $followers), $fields, 0, $count);
}
return (object) [ return (object) [
"count" => $users->get($user_id)->getFollowersCount(), "count" => $users->get($user_id)->getFollowersCount(),
"items" => $response "items" => $response,
]; ];
} }
function search(string $q, public function search(
string $fields = "", string $q,
int $offset = 0, string $fields = "",
int $count = 100, int $offset = 0,
string $city = "", int $count = 100,
string $hometown = "", string $city = "",
int $sex = 2, string $hometown = "",
int $status = 0, # это про marital status int $sex = 3,
bool $online = false, int $status = 0, # marital_status
# дальше идут параметры которых нету в vkapi но есть на сайте bool $online = false,
string $profileStatus = "", # а это уже нормальный статус # non standart params:
int $sort = 0, int $sort = 0,
int $before = 0, int $polit_views = 0,
int $politViews = 0, string $fav_music = "",
int $after = 0, string $fav_films = "",
string $interests = "", string $fav_shows = "",
string $fav_music = "", string $fav_books = ""
string $fav_films = "", ) {
string $fav_shows = "", if ($count > 100) {
string $fav_books = "", $this->fail(100, "One of the parameters specified was missing or invalid: count should be less or equal to 100");
string $fav_quotes = "" }
)
{
$users = new UsersRepo;
$sortg = "id ASC";
$nfilds = $fields; $users = new UsersRepo();
$output_sort = ['type' => 'id', 'invert' => false];
$output_params = [
"ignore_private" => true,
];
switch($sort) { switch ($sort) {
default:
case 0: case 0:
$sortg = "id DESC"; $output_sort = ['type' => 'id', 'invert' => false];
break; break;
case 1: case 1:
$sortg = "id ASC"; $output_sort = ['type' => 'id', 'invert' => true];
break;
case 2:
$sortg = "first_name DESC";
break;
case 3:
$sortg = "first_name ASC";
break; break;
case 4: case 4:
$sortg = "rating DESC"; $output_sort = ['type' => 'rating', 'invert' => false];
if(!str_contains($nfilds, "rating")) {
$nfilds .= "rating";
}
break;
case 5:
$sortg = "rating DESC";
if(!str_contains($nfilds, "rating")) {
$nfilds .= "rating";
}
break; break;
} }
if (!empty($city)) {
$output_params['city'] = $city;
}
if (!empty($hometown)) {
$output_params['hometown'] = $hometown;
}
if ($sex != 3) {
$output_params['gender'] = $sex;
}
if ($status != 0) {
$output_params['marital_status'] = $status;
}
if ($polit_views != 0) {
$output_params['polit_views'] = $polit_views;
}
if (!empty($interests)) {
$output_params['interests'] = $interests;
}
if (!empty($fav_music)) {
$output_params['fav_music'] = $fav_music;
}
if (!empty($fav_films)) {
$output_params['fav_films'] = $fav_films;
}
if (!empty($fav_shows)) {
$output_params['fav_shows'] = $fav_shows;
}
if (!empty($fav_books)) {
$output_params['fav_books'] = $fav_books;
}
if ($online) {
$output_params['is_online'] = 1;
}
$array = []; $array = [];
$find = $users->find($q, $output_params, $output_sort);
$parameters = [ foreach ($find->offsetLimit($offset, $count) as $user) {
"city" => !empty($city) ? $city : NULL,
"hometown" => !empty($hometown) ? $hometown : NULL,
"gender" => $sex < 2 ? $sex : NULL,
"maritalstatus" => (bool)$status ? $status : NULL,
"politViews" => (bool)$politViews ? $politViews : NULL,
"is_online" => $online ? 1 : NULL,
"status" => !empty($profileStatus) ? $profileStatus : NULL,
"before" => $before != 0 ? $before : NULL,
"after" => $after != 0 ? $after : NULL,
"interests" => !empty($interests) ? $interests : NULL,
"fav_music" => !empty($fav_music) ? $fav_music : NULL,
"fav_films" => !empty($fav_films) ? $fav_films : NULL,
"fav_shows" => !empty($fav_shows) ? $fav_shows : NULL,
"fav_books" => !empty($fav_books) ? $fav_books : NULL,
"fav_quotes" => !empty($fav_quotes) ? $fav_quotes : NULL,
];
$find = $users->find($q, $parameters, $sortg);
foreach ($find as $user)
$array[] = $user->getId(); $array[] = $user->getId();
}
if (!$array || sizeof($array) < 1) {
return (object) [
"count" => 0,
"items" => [],
];
}
return (object) [ return (object) [
"count" => $find->size(), "count" => $find->size(),
"items" => $this->get(implode(',', $array), $nfilds, $offset, $count) "items" => $this->get(implode(',', $array), $fields),
]; ];
} }
public function report(int $user_id, string $type = "spam", string $comment = "")
{
$this->requireUser();
$this->willExecuteWriteAction();
if ($user_id == $this->getUser()->getId()) {
$this->fail(12, "Can't report yourself.");
}
if (sizeof(iterator_to_array((new Reports())->getDuplicates("user", $user_id, null, $this->getUser()->getId()))) > 0) {
return 1;
}
$report = new Report();
$report->setUser_id($this->getUser()->getId());
$report->setTarget_id($user_id);
$report->setType("user");
$report->setReason($comment);
$report->setCreated(time());
$report->save();
return 1;
}
} }

View file

@ -1,46 +1,62 @@
<?php declare(strict_types=1); <?php
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\{Users, Clubs}; declare(strict_types=1);
final class Utils extends VKAPIRequestHandler namespace openvk\VKAPI\Handlers;
{
function getServerTime(): int use openvk\Web\Models\Repositories\{Users, Clubs};
{
return time(); final class Utils extends VKAPIRequestHandler
} {
public function getServerTime(): int
function resolveScreenName(string $screen_name): object {
{ return time();
if(\Chandler\MVC\Routing\Router::i()->getMatchingRoute("/$screen_name")[0]->presenter !== "UnknownTextRouteStrategy") { }
if(substr($screen_name, 0, strlen("id")) === "id") {
return (object) [ public function resolveScreenName(string $screen_name): object
"object_id" => (int) substr($screen_name, strlen("id")), {
"type" => "user" if (\Chandler\MVC\Routing\Router::i()->getMatchingRoute("/$screen_name")[0]->presenter !== "UnknownTextRouteStrategy") {
]; if (substr($screen_name, 0, strlen("id")) === "id") {
} else if(substr($screen_name, 0, strlen("club")) === "club") { return (object) [
return (object) [ "object_id" => (int) substr($screen_name, strlen("id")),
"object_id" => (int) substr($screen_name, strlen("club")), "type" => "user",
"type" => "group" ];
]; } elseif (substr($screen_name, 0, strlen("club")) === "club") {
} return (object) [
} else { "object_id" => (int) substr($screen_name, strlen("club")),
$user = (new Users)->getByShortURL($screen_name); "type" => "group",
if($user) { ];
return (object) [ } else {
"object_id" => $user->getId(), $this->fail(104, "Not found");
"type" => "user" }
]; } else {
} $user = (new Users())->getByShortURL($screen_name);
if ($user) {
$club = (new Clubs)->getByShortURL($screen_name); return (object) [
if($club) { "object_id" => $user->getId(),
return (object) [ "type" => "user",
"object_id" => $club->getId(), ];
"type" => "group" }
];
} $club = (new Clubs())->getByShortURL($screen_name);
if ($club) {
return (object) []; return (object) [
} "object_id" => $club->getId(),
} "type" => "group",
} ];
}
$this->fail(104, "Not found");
}
}
public function resolveGuid(string $guid): object
{
$user = (new Users())->getByChandlerUserId($guid);
if (is_null($user)) {
$this->fail(104, "Not found");
}
return $user->toVkApiStruct($this->getUser());
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\VKAPI\Exceptions\APIErrorException; use openvk\VKAPI\Exceptions\APIErrorException;
use openvk\Web\Models\Entities\IP; use openvk\Web\Models\Entities\IP;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
@ -9,50 +13,51 @@ abstract class VKAPIRequestHandler
{ {
protected $user; protected $user;
protected $platform; protected $platform;
function __construct(?User $user = NULL, ?string $platform = NULL) public function __construct(?User $user = null, ?string $platform = null)
{ {
$this->user = $user; $this->user = $user;
$this->platform = $platform; $this->platform = $platform;
} }
protected function fail(int $code, string $message): void protected function fail(int $code, string $message): void
{ {
throw new APIErrorException($message, $code); throw new APIErrorException($message, $code);
} }
protected function getUser(): ?User protected function getUser(): ?User
{ {
return $this->user; return $this->user;
} }
protected function getPlatform(): ?string protected function getPlatform(): ?string
{ {
return $this->platform; return $this->platform ?? "";
} }
protected function userAuthorized(): bool protected function userAuthorized(): bool
{ {
return !is_null($this->getUser()); return !is_null($this->getUser());
} }
protected function requireUser(): void protected function requireUser(): void
{ {
if(!$this->userAuthorized()) if (!$this->userAuthorized()) {
$this->fail(5, "User authorization failed: no access_token passed."); $this->fail(5, "User authorization failed: no access_token passed.");
}
} }
protected function willExecuteWriteAction(): void protected function willExecuteWriteAction(): void
{ {
$ip = (new IPs)->get(CONNECTING_IP); $ip = (new IPs())->get(CONNECTING_IP);
$res = $ip->rateLimit(); $res = $ip->rateLimit();
if(!($res === IP::RL_RESET || $res === IP::RL_CANEXEC)) { if (!($res === IP::RL_RESET || $res === IP::RL_CANEXEC)) {
if($res === IP::RL_BANNED && OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["autoban"]) { if ($res === IP::RL_BANNED && OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["autoban"]) {
$this->user->ban("User account has been suspended for breaking API terms of service", false); $this->user->ban("User account has been suspended for breaking API terms of service", false);
$this->fail(18, "User account has been suspended due to repeated violation of API rate limits."); $this->fail(18, "User account has been suspended due to repeated violation of API rate limits.");
} }
$this->fail(29, "You have been rate limited."); $this->fail(29, "You have been rate limited.");
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Handlers; namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\Users as UsersRepo; use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Entities\Club; use openvk\Web\Models\Entities\Club;
@ -11,47 +15,181 @@ use openvk\Web\Models\Repositories\Comments as CommentsRepo;
final class Video extends VKAPIRequestHandler final class Video extends VKAPIRequestHandler
{ {
function get(int $owner_id, string $videos, int $offset = 0, int $count = 30, int $extended = 0): object public function get(int $owner_id = 0, string $videos = "", string $fields = "", int $offset = 0, int $count = 30, int $extended = 0): object
{ {
$this->requireUser(); $this->requireUser();
if ($videos) { if (!empty($videos)) {
$vids = explode(',', $videos); $vids = explode(',', $videos);
$profiles = [];
foreach($vids as $vid) $groups = [];
{ foreach ($vids as $vid) {
$id = explode("_", $vid); $id = explode("_", $vid);
$items = []; $items = [];
$video = (new VideosRepo)->getByOwnerAndVID(intval($id[0]), intval($id[1])); $video = (new VideosRepo())->getByOwnerAndVID(intval($id[0]), intval($id[1]));
if($video) { if ($video && !$video->isDeleted()) {
$items[] = $video->getApiStructure(); $out_video = $video->getApiStructure($this->getUser())->video;
$items[] = $out_video;
if ($out_video['owner_id']) {
if ($out_video['owner_id'] > 0) {
$profiles[] = $out_video['owner_id'];
} else {
$groups[] = abs($out_video['owner_id']);
}
}
} }
} }
if ($extended == 1) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$profilesFormatted = [];
$groupsFormatted = [];
foreach ($profiles as $prof) {
$profile = (new UsersRepo())->get($prof);
$profilesFormatted[] = $profile->toVkApiStruct($this->getUser(), $fields);
}
foreach ($groups as $gr) {
$group = (new ClubsRepo())->get($gr);
$groupsFormatted[] = $group->toVkApiStruct($this->getUser(), $fields);
}
return (object) [
"count" => sizeof($items),
"items" => $items,
"profiles" => $profilesFormatted,
"groups" => $groupsFormatted,
];
}
return (object) [ return (object) [
"count" => count($items), "count" => count($items),
"items" => $items "items" => $items,
]; ];
} else { } else {
if ($owner_id > 0) if ($owner_id > 0) {
$user = (new UsersRepo)->get($owner_id); $user = (new UsersRepo())->get($owner_id);
else } else {
$this->fail(1, "Not implemented"); $this->fail(1, "Not implemented");
$videos = (new VideosRepo)->getByUser($user, $offset + 1, $count);
$videosCount = (new VideosRepo)->getUserVideosCount($user);
$items = [];
foreach ($videos as $video) {
$items[] = $video->getApiStructure();
} }
if (!$user || $user->isDeleted()) {
$this->fail(14, "Invalid user");
}
if (!$user->getPrivacyPermission('videos.read', $this->getUser())) {
$this->fail(21, "This user chose to hide his videos.");
}
$videos = (new VideosRepo())->getByUserLimit($user, $offset, $count);
$videosCount = (new VideosRepo())->getUserVideosCount($user);
$items = [];
$profiles = [];
$groups = [];
foreach ($videos as $video) {
$video = $video->getApiStructure($this->getUser())->video;
$items[] = $video;
if ($video['owner_id']) {
if ($video['owner_id'] > 0) {
$profiles[] = $video['owner_id'];
} else {
$groups[] = abs($video['owner_id']);
}
}
}
if ($extended == 1) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$profilesFormatted = [];
$groupsFormatted = [];
foreach ($profiles as $prof) {
$profile = (new UsersRepo())->get($prof);
$profilesFormatted[] = $profile->toVkApiStruct($this->getUser(), $fields);
}
foreach ($groups as $gr) {
$group = (new ClubsRepo())->get($gr);
$groupsFormatted[] = $group->toVkApiStruct($this->getUser(), $fields);
}
return (object) [
"count" => $videosCount,
"items" => $items,
"profiles" => $profilesFormatted,
"groups" => $groupsFormatted,
];
}
return (object) [ return (object) [
"count" => $videosCount, "count" => $videosCount,
"items" => $items "items" => $items,
]; ];
} }
} }
public function search(string $q = '', int $sort = 0, int $offset = 0, int $count = 10, bool $extended = false, string $fields = ''): object
{
$this->requireUser();
$params = [];
$db_sort = ['type' => 'id', 'invert' => false];
$videos = (new VideosRepo())->find($q, $params, $db_sort);
$items = iterator_to_array($videos->offsetLimit($offset, $count));
$count = $videos->size();
$return_items = [];
$profiles = [];
$groups = [];
foreach ($items as $item) {
$return_item = $item->getApiStructure($this->getUser());
$return_item = $return_item->video;
$return_items[] = $return_item;
if ($return_item['owner_id']) {
if ($return_item['owner_id'] > 0) {
$profiles[] = $return_item['owner_id'];
} else {
$groups[] = abs($return_item['owner_id']);
}
}
}
if ($extended) {
$profiles = array_unique($profiles);
$groups = array_unique($groups);
$profilesFormatted = [];
$groupsFormatted = [];
foreach ($profiles as $prof) {
$profile = (new UsersRepo())->get($prof);
$profilesFormatted[] = $profile->toVkApiStruct($this->getUser(), $fields);
}
foreach ($groups as $gr) {
$group = (new ClubsRepo())->get($gr);
$groupsFormatted[] = $group->toVkApiStruct($this->getUser(), $fields);
}
return (object) [
"count" => $count,
"items" => $return_items,
"profiles" => $profilesFormatted,
"groups" => $groupsFormatted,
];
}
return (object) [
"count" => $count,
"items" => $return_items,
];
}
} }

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,7 @@ exceptions. It is still a work-in-progress functionality.
**Note**: requests to API are routed through **Note**: requests to API are routed through
openvk.Web.Presenters.VKAPIPresenter, this dir contains only handlers. openvk.Web.Presenters.VKAPIPresenter, this dir contains only handlers.
[Documentation for API clients](https://docs.openvk.uk/openvk_engine/api/description/) [Documentation for API clients](https://docs.ovk.to/openvk_engine/api/description/)
## Implementing API methods ## Implementing API methods

View file

@ -1,4 +1,7 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Structures; namespace openvk\VKAPI\Structures;
final class Conversation final class Conversation

View file

@ -1,4 +1,7 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\VKAPI\Structures; namespace openvk\VKAPI\Structures;
final class Message final class Message
@ -17,5 +20,5 @@ final class Message
public $emoji; public $emoji;
public $important = true; public $important = true;
public $deleted = 0; public $deleted = 0;
public $random_id = NULL; public $random_id = null;
} }

View file

@ -1,7 +1,10 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Events; namespace openvk\Web\Events;
interface ILPEmitable interface ILPEmitable
{ {
function getLongPoolSummary(): object; public function getLongPoolSummary(): object;
} }

View file

@ -1,32 +1,37 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Events; namespace openvk\Web\Events;
use openvk\Web\Models\Entities\Message; use openvk\Web\Models\Entities\Message;
use openvk\Web\Models\Repositories\Messages; use openvk\Web\Models\Repositories\Messages;
class NewMessageEvent implements ILPEmitable class NewMessageEvent implements ILPEmitable
{ {
protected $payload; protected $payload;
function __construct(Message $message) public function __construct(Message $message)
{ {
$this->payload = $message->simplify(); $this->payload = $message->simplify();
} }
function getLongPoolSummary(): object public function getLongPoolSummary(): object
{ {
return (object) [ return (object) [
"type" => "newMessage", "type" => "newMessage",
"message" => $this->payload, "message" => $this->payload,
]; ];
} }
function getVKAPISummary(int $userId): array public function getVKAPISummary(int $userId): array
{ {
$msg = (new Messages)->get($this->payload["uuid"]); $msg = (new Messages())->get($this->payload["uuid"]);
$peer = $msg->getSender()->getId(); $peer = $msg->getSender()->getId();
if($peer === $userId) if ($peer === $userId) {
$peer = $msg->getRecipient()->getId(); $peer = $msg->getRecipient()->getId();
}
/* /*
* Source: * Source:
* https://github.com/danyadev/longpoll-doc * https://github.com/danyadev/longpoll-doc
@ -43,7 +48,7 @@ class NewMessageEvent implements ILPEmitable
[], # empty attachments [], # empty attachments
$msg->getId() << 2, # id as random_id $msg->getId() << 2, # id as random_id
$peer, # conversation id $peer, # conversation id
0 # not edited yet 0, # not edited yet
]; ];
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;
use Nette\InvalidStateException as ISE; use Nette\InvalidStateException as ISE;
@ -7,52 +11,53 @@ use Nette\InvalidStateException as ISE;
class APIToken extends RowModel class APIToken extends RowModel
{ {
protected $tableName = "api_tokens"; protected $tableName = "api_tokens";
function getUser(): User public function getUser(): User
{ {
return (new Users)->get($this->getRecord()->user); return (new Users())->get($this->getRecord()->user);
} }
function getSecret(): string public function getSecret(): string
{ {
return $this->getRecord()->secret; return $this->getRecord()->secret;
} }
function getFormattedToken(): string public function getFormattedToken(): string
{ {
return $this->getId() . "-" . chunk_split($this->getSecret(), 8, "-") . "jill"; return $this->getId() . "-" . chunk_split($this->getSecret(), 8, "-") . "jill";
} }
function getPlatform(): ?string public function getPlatform(): ?string
{ {
return $this->getRecord()->platform; return $this->getRecord()->platform;
} }
function isRevoked(): bool public function isRevoked(): bool
{ {
return $this->isDeleted(); return $this->isDeleted();
} }
function setUser(User $user): void public function setUser(User $user): void
{ {
$this->stateChanges("user", $user->getId()); $this->stateChanges("user", $user->getId());
} }
function setSecret(string $secret): void public function setSecret(string $secret): void
{ {
throw new ISE("Setting secret manually is prohbited"); throw new ISE("Setting secret manually is prohbited");
} }
function revoke(): void public function revoke(): void
{ {
$this->delete(); $this->delete();
} }
function save(): void public function save(?bool $log = false): void
{ {
if(is_null($this->getRecord())) if (is_null($this->getRecord())) {
$this->stateChanges("secret", bin2hex(openssl_random_pseudo_bytes(36))); $this->stateChanges("secret", bin2hex(openssl_random_pseudo_bytes(36)));
}
parent::save(); parent::save();
} }
} }

View file

@ -1,93 +1,115 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Photos; use openvk\Web\Models\Repositories\Photos;
class Album extends MediaCollection class Album extends MediaCollection
{ {
const SPECIAL_AVATARS = 16; public const SPECIAL_AVATARS = 16;
const SPECIAL_WALL = 32; public const SPECIAL_WALL = 32;
protected $tableName = "albums"; protected $tableName = "albums";
protected $relTableName = "album_relations"; protected $relTableName = "album_relations";
protected $entityTableName = "photos"; protected $entityTableName = "photos";
protected $entityClassName = 'openvk\Web\Models\Entities\Photo'; protected $entityClassName = 'openvk\Web\Models\Entities\Photo';
protected $specialNames = [ protected $specialNames = [
16 => "_avatar_album", 16 => "_avatar_album",
32 => "_wall_album", 32 => "_wall_album",
64 => "_saved_photos_album", 64 => "_saved_photos_album",
]; ];
function getCoverURL(): ?string public function getCoverURL(): ?string
{ {
$coverPhoto = $this->getCoverPhoto(); $coverPhoto = $this->getCoverPhoto();
if(!$coverPhoto) if (!$coverPhoto) {
return "/assets/packages/static/openvk/img/camera_200.png"; return "/assets/packages/static/openvk/img/camera_200.png";
}
return $coverPhoto->getURL(); return $coverPhoto->getURL();
} }
function getCoverPhoto(): ?Photo public function getCoverPhoto(): ?Photo
{ {
$cover = $this->getRecord()->cover_photo; $cover = $this->getRecord()->cover_photo;
if(!$cover) { if (!$cover) {
$photos = iterator_to_array($this->getPhotos(1, 1)); $photos = iterator_to_array($this->getPhotos(1, 1));
$photo = $photos[0] ?? NULL; $photo = $photos[0] ?? null;
if(!$photo || $photo->isDeleted()) if (!$photo || $photo->isDeleted()) {
return NULL; return null;
else } else {
return $photo; return $photo;
}
} }
return (new Photos)->get($cover); return (new Photos())->get($cover);
} }
function getPhotos(int $page = 1, ?int $perPage = NULL): \Traversable public function getPhotos(int $page = 1, ?int $perPage = null): \Traversable
{ {
return $this->fetch($page, $perPage); return $this->fetch($page, $perPage);
} }
function getPhotosCount(): int public function getPhotosCount(): int
{ {
return $this->size(); return $this->size();
} }
function addPhoto(Photo $photo): void public function addPhoto(Photo $photo): void
{ {
$this->add($photo); $this->add($photo);
} }
function removePhoto(Photo $photo): void public function removePhoto(Photo $photo): void
{ {
$this->remove($photo); $this->remove($photo);
} }
function hasPhoto(Photo $photo): bool public function hasPhoto(Photo $photo): bool
{ {
return $this->has($photo); return $this->has($photo);
} }
function toVkApiStruct(?User $user = NULL, bool $need_covers = false, bool $photo_sizes = false): object public function canBeViewedBy(?User $user = null): bool
{
if ($this->isDeleted()) {
return false;
}
$owner = $this->getOwner();
if (get_class($owner) == "openvk\\Web\\Models\\Entities\\User") {
return $owner->canBeViewedBy($user) && $owner->getPrivacyPermission('photos.read', $user);
} else {
return $owner->canBeViewedBy($user);
}
}
public function toVkApiStruct(?User $user = null, bool $need_covers = false, bool $photo_sizes = false): object
{ {
$res = (object) []; $res = (object) [];
$res->id = $this->getPrettyId(); $res->id = $this->getPrettyId();
$res->vid = $this->getId();
$res->thumb_id = !is_null($this->getCoverPhoto()) ? $this->getCoverPhoto()->getPrettyId() : 0; $res->thumb_id = !is_null($this->getCoverPhoto()) ? $this->getCoverPhoto()->getPrettyId() : 0;
$res->owner_id = $this->getOwner()->getId(); $res->owner_id = $this->getOwner()->getId();
$res->title = $this->getName(); $res->title = $this->getName();
$res->description = $this->getDescription(); $res->description = $this->getDescription();
$res->created = $this->getCreationTime()->timestamp(); $res->created = $this->getCreationTime()->timestamp();
$res->updated = $this->getEditTime() ? $this->getEditTime()->timestamp() : NULL; $res->updated = $this->getEditTime() ? $this->getEditTime()->timestamp() : null;
$res->size = $this->size(); $res->size = $this->size();
$res->privacy_comment = 1; $res->privacy_comment = 1;
$res->upload_by_admins_only = 1; $res->upload_by_admins_only = 1;
$res->comments_disabled = 0; $res->comments_disabled = 0;
$res->can_upload = $this->canBeModifiedBy($user); # thisUser недоступен в entities $res->can_upload = $this->canBeModifiedBy($user); # thisUser недоступен в entities
if($need_covers) { if ($need_covers) {
$res->thumb_src = $this->getCoverURL(); $res->thumb_src = $this->getCoverURL();
if($photo_sizes) { if ($photo_sizes) {
$res->sizes = !is_null($this->getCoverPhoto()) ? $this->getCoverPhoto()->getVkApiSizes() : NULL; $res->sizes = !is_null($this->getCoverPhoto()) ? $this->getCoverPhoto()->getVkApiSizes() : null;
} }
} }

View file

@ -1,4 +1,7 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
@ -8,27 +11,28 @@ use openvk\Web\Models\Repositories\{Users, Clubs};
class Alias extends RowModel class Alias extends RowModel
{ {
protected $tableName = "aliases"; protected $tableName = "aliases";
function getOwnerId(): int public function getOwnerId(): int
{ {
return $this->getRecord()->owner_id; return $this->getRecord()->owner_id;
} }
function getType(): string public function getType(): string
{ {
if ($this->getOwnerId() < 0) if ($this->getOwnerId() < 0) {
return "club"; return "club";
}
return "user"; return "user";
} }
function getUser(): ?User public function getUser(): ?User
{ {
return (new Users)->get($this->getOwnerId()); return (new Users())->get($this->getOwnerId());
} }
function getClub(): ?Club public function getClub(): ?Club
{ {
return (new Clubs)->get($this->getOwnerId() * -1); return (new Clubs())->get($this->getOwnerId() * -1);
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use Nette\Utils\Image; use Nette\Utils\Image;
use Nette\Utils\UnknownImageFileException; use Nette\Utils\UnknownImageFileException;
@ -10,8 +14,8 @@ use openvk\Web\Models\RowModel;
class Application extends RowModel class Application extends RowModel
{ {
protected $tableName = "apps"; protected $tableName = "apps";
const PERMS = [ public const PERMS = [
"notify", "notify",
"friends", "friends",
"photos", "photos",
@ -31,44 +35,46 @@ class Application extends RowModel
"email", "email",
"market", "market",
]; ];
private function getAvatarsDir(): string private function getAvatarsDir(): string
{ {
$uploadSettings = OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]; $uploadSettings = OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"];
if($uploadSettings["mode"] === "server" && $uploadSettings["server"]["kind"] === "cdn") if ($uploadSettings["mode"] === "server" && $uploadSettings["server"]["kind"] === "cdn") {
return $uploadSettings["server"]["directory"]; return $uploadSettings["server"]["directory"];
else } else {
return OPENVK_ROOT . "/storage/"; return OPENVK_ROOT . "/storage/";
}
} }
function getId(): int public function getId(): int
{ {
return $this->getRecord()->id; return $this->getRecord()->id;
} }
function getOwner(): User public function getOwner(): User
{ {
return (new Users)->get($this->getRecord()->owner); return (new Users())->get($this->getRecord()->owner);
} }
function getName(): string public function getName(): string
{ {
return $this->getRecord()->name; return $this->getRecord()->name;
} }
function getDescription(): string public function getDescription(): string
{ {
return $this->getRecord()->description; return $this->getRecord()->description;
} }
function getAvatarUrl(): string public function getAvatarUrl(): string
{ {
$serverUrl = ovk_scheme(true) . $_SERVER["HTTP_HOST"]; $serverUrl = ovk_scheme(true) . $_SERVER["HTTP_HOST"];
if(is_null($this->getRecord()->avatar_hash)) if (is_null($this->getRecord()->avatar_hash)) {
return "$serverUrl/assets/packages/static/openvk/img/camera_200.png"; return "$serverUrl/assets/packages/static/openvk/img/camera_200.png";
}
$hash = $this->getRecord()->avatar_hash; $hash = $this->getRecord()->avatar_hash;
switch(OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) { switch (OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) {
default: default:
case "default": case "default":
case "basic": case "basic":
@ -85,155 +91,168 @@ class Application extends RowModel
); );
} }
} }
function getNote(): ?Note public function getNote(): ?Note
{ {
if(!$this->getRecord()->news) if (!$this->getRecord()->news) {
return NULL; return null;
}
return (new Notes)->get($this->getRecord()->news);
return (new Notes())->get($this->getRecord()->news);
} }
function getNoteLink(): string public function getNoteLink(): string
{ {
$note = $this->getNote(); $note = $this->getNote();
if(!$note) if (!$note) {
return ""; return "";
}
return ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/note" . $note->getPrettyId(); return ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/note" . $note->getPrettyId();
} }
function getBalance(): float public function getBalance(): float
{ {
return $this->getRecord()->coins; return $this->getRecord()->coins;
} }
function getURL(): string public function getURL(): string
{ {
return $this->getRecord()->address; return $this->getRecord()->address;
} }
function getOrigin(): string public function getOrigin(): string
{ {
$parsed = parse_url($this->getURL()); $parsed = parse_url($this->getURL());
return ( return (
($parsed["scheme"] ?? "https") . "://" ($parsed["scheme"] ?? "https") . "://"
. ($parsed["host"] ?? "127.0.0.1") . ":" . ($parsed["host"] ?? "127.0.0.1") . ":"
. ($parsed["port"] ?? "443") . ($parsed["port"] ?? "443")
); );
} }
function getUsersCount(): int public function getUsersCount(): int
{ {
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
return sizeof($cx->table("app_users")->where("app", $this->getId())); return sizeof($cx->table("app_users")->where("app", $this->getId()));
} }
function getInstallationEntry(User $user): ?array public function getInstallationEntry(User $user): ?array
{ {
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
$entry = $cx->table("app_users")->where([ $entry = $cx->table("app_users")->where([
"app" => $this->getId(), "app" => $this->getId(),
"user" => $user->getId(), "user" => $user->getId(),
])->fetch(); ])->fetch();
if(!$entry) if (!$entry) {
return NULL; return null;
}
return $entry->toArray(); return $entry->toArray();
} }
function getPermissions(User $user): array public function getPermissions(User $user): array
{ {
$permMask = 0; $permMask = 0;
$installInfo = $this->getInstallationEntry($user); $installInfo = $this->getInstallationEntry($user);
if(!$installInfo) if (!$installInfo) {
$this->install($user); $this->install($user);
else } else {
$permMask = $installInfo["access"]; $permMask = $installInfo["access"];
$res = [];
for($i = 0; $i < sizeof(self::PERMS); $i++) {
$checkVal = 1 << $i;
if(($permMask & $checkVal) > 0)
$res[] = self::PERMS[$i];
} }
$res = [];
for ($i = 0; $i < sizeof(self::PERMS); $i++) {
$checkVal = 1 << $i;
if (($permMask & $checkVal) > 0) {
$res[] = self::PERMS[$i];
}
}
return $res; return $res;
} }
function isInstalledBy(User $user): bool public function isInstalledBy(User $user): bool
{ {
return !is_null($this->getInstallationEntry($user)); return !is_null($this->getInstallationEntry($user));
} }
function setNoteLink(?string $link): bool public function setNoteLink(?string $link): bool
{ {
if(!$link) { if (!$link) {
$this->stateChanges("news", NULL); $this->stateChanges("news", null);
return true; return true;
} }
preg_match("%note([0-9]+)_([0-9]+)$%", $link, $matches); preg_match("%note([0-9]+)_([0-9]+)$%", $link, $matches);
if(sizeof($matches) != 3) if (sizeof($matches) != 3) {
return false; return false;
}
$owner = is_null($this->getRecord()) ? $this->changes["owner"] : $this->getRecord()->owner; $owner = is_null($this->getRecord()) ? $this->changes["owner"] : $this->getRecord()->owner;
[, $ownerId, $vid] = $matches; [, $ownerId, $vid] = $matches;
if($ownerId != $owner) if ($ownerId != $owner) {
return false; return false;
}
$note = (new Notes)->getNoteById((int) $ownerId, (int) $vid);
if(!$note) $note = (new Notes())->getNoteById((int) $ownerId, (int) $vid);
if (!$note) {
return false; return false;
}
$this->stateChanges("news", $note->getId()); $this->stateChanges("news", $note->getId());
return true; return true;
} }
function setAvatar(array $file): int public function setAvatar(array $file): int
{ {
if($file["error"] !== UPLOAD_ERR_OK) if ($file["error"] !== UPLOAD_ERR_OK) {
return -1; return -1;
}
try { try {
$image = Image::fromFile($file["tmp_name"]); $image = Image::fromFile($file["tmp_name"]);
} catch (UnknownImageFileException $e) { } catch (UnknownImageFileException $e) {
return -2; return -2;
} }
$hash = hash_file("adler32", $file["tmp_name"]); $hash = hash_file("adler32", $file["tmp_name"]);
if(!is_dir($this->getAvatarsDir() . substr($hash, 0, 2))) if (!is_dir($this->getAvatarsDir() . substr($hash, 0, 2))) {
if(!mkdir($this->getAvatarsDir() . substr($hash, 0, 2))) if (!mkdir($this->getAvatarsDir() . substr($hash, 0, 2))) {
return -3; return -3;
}
}
$image->resize(140, 140, Image::STRETCH); $image->resize(140, 140, Image::STRETCH);
$image->save($this->getAvatarsDir() . substr($hash, 0, 2) . "/$hash" . "_app_avatar.png"); $image->save($this->getAvatarsDir() . substr($hash, 0, 2) . "/$hash" . "_app_avatar.png");
$this->stateChanges("avatar_hash", $hash); $this->stateChanges("avatar_hash", $hash);
return 0; return 0;
} }
function setPermission(User $user, string $perm, bool $enabled): bool public function setPermission(User $user, string $perm, bool $enabled): bool
{ {
$permMask = 0; $permMask = 0;
$installInfo = $this->getInstallationEntry($user); $installInfo = $this->getInstallationEntry($user);
if(!$installInfo) if (!$installInfo) {
$this->install($user); $this->install($user);
else } else {
$permMask = $installInfo["access"]; $permMask = $installInfo["access"];
}
$index = array_search($perm, self::PERMS); $index = array_search($perm, self::PERMS);
if($index === false) if ($index === false) {
return false; return false;
}
$permVal = 1 << $index; $permVal = 1 << $index;
$permMask = $enabled ? ($permMask | $permVal) : ($permMask ^ $permVal); $permMask = $enabled ? ($permMask | $permVal) : ($permMask ^ $permVal);
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
$cx->table("app_users")->where([ $cx->table("app_users")->where([
"app" => $this->getId(), "app" => $this->getId(),
@ -241,30 +260,30 @@ class Application extends RowModel
])->update([ ])->update([
"access" => $permMask, "access" => $permMask,
]); ]);
return true; return true;
} }
function isEnabled(): bool public function isEnabled(): bool
{ {
return (bool) $this->getRecord()->enabled; return (bool) $this->getRecord()->enabled;
} }
function enable(): void public function enable(): void
{ {
$this->stateChanges("enabled", 1); $this->stateChanges("enabled", 1);
$this->save(); $this->save();
} }
function disable(): void public function disable(): void
{ {
$this->stateChanges("enabled", 0); $this->stateChanges("enabled", 0);
$this->save(); $this->save();
} }
function install(User $user): void public function install(User $user): void
{ {
if(!$this->getInstallationEntry($user)) { if (!$this->getInstallationEntry($user)) {
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
$cx->table("app_users")->insert([ $cx->table("app_users")->insert([
"app" => $this->getId(), "app" => $this->getId(),
@ -272,8 +291,8 @@ class Application extends RowModel
]); ]);
} }
} }
function uninstall(User $user): void public function uninstall(User $user): void
{ {
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
$cx->table("app_users")->where([ $cx->table("app_users")->where([
@ -281,36 +300,42 @@ class Application extends RowModel
"user" => $user->getId(), "user" => $user->getId(),
])->delete(); ])->delete();
} }
function addCoins(float $coins): float public function addCoins(float $coins): float
{ {
$res = $this->getBalance() + $coins; $res = $this->getBalance() + $coins;
$this->stateChanges("coins", $res); $this->stateChanges("coins", $res);
$this->save(); $this->save();
return $res; return $res;
} }
function withdrawCoins(): void public function withdrawCoins(): void
{ {
$balance = $this->getBalance(); $balance = $this->getBalance();
$tax = ($balance / 100) * OPENVK_ROOT_CONF["openvk"]["preferences"]["apps"]["withdrawTax"]; $tax = ($balance / 100) * OPENVK_ROOT_CONF["openvk"]["preferences"]["apps"]["withdrawTax"];
$owner = $this->getOwner(); $owner = $this->getOwner();
$owner->setCoins($owner->getCoins() + ($balance - $tax)); $owner->setCoins($owner->getCoins() + ($balance - $tax));
$this->setCoins(0.0); $this->setCoins(0.0);
$this->save(); $this->save();
$owner->save(); $owner->save();
} }
function delete(bool $softly = true): void public function delete(bool $softly = true): void
{ {
if($softly) if ($softly) {
throw new \UnexpectedValueException("Can't delete apps softly."); throw new \UnexpectedValueException("Can't delete apps softly.");
} // why
$cx = DatabaseConnection::i()->getContext(); $cx = DatabaseConnection::i()->getContext();
$cx->table("app_users")->where("app", $this->getId())->delete(); $cx->table("app_users")->where("app", $this->getId())->delete();
parent::delete(false); parent::delete(false);
} }
}
public function getPublicationTime(): string
{
return tr("recently");
}
}

View file

@ -1,38 +1,42 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
abstract class Attachable extends RowModel abstract class Attachable extends RowModel
{ {
function getId(): int public function getId(): int
{ {
return $this->getRecord()->id; return $this->getRecord()->id;
} }
function getParents(): \Traversable public function getParents(): \Traversable
{ {
$sel = $this->getRecord() $sel = $this->getRecord()
->related("attachments.attachable_id") ->related("attachments.attachable_id")
->where("attachments.attachable_type", get_class($this)); ->where("attachments.attachable_type", get_class($this));
foreach($sel as $rel) { foreach ($sel as $rel) {
$repoName = $rel->target_type . "s"; $repoName = $rel->target_type . "s";
$repoName = str_replace("Entities", "Repositories", $repoName); $repoName = str_replace("Entities", "Repositories", $repoName);
$repo = new $repoName; $repo = new $repoName();
yield $repo->get($rel->target_id); yield $repo->get($rel->target_id);
} }
} }
/** /**
* Deletes together with all references. * Deletes together with all references.
*/ */
function delete(bool $softly = true): void public function delete(bool $softly = true): void
{ {
$this->getRecord() $this->getRecord()
->related("attachments.attachable_id") ->related("attachments.attachable_id")
->where("attachments.attachable_type", get_class($this)) ->where("attachments.attachable_type", get_class($this))
->delete(); ->delete();
parent::delete(); parent::delete();
} }
} }

View file

@ -0,0 +1,508 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Util\Shell\Exceptions\UnknownCommandException;
use openvk\Web\Util\Shell\Shell;
/**
* @method setName(string)
* @method setPerformer(string)
* @method setLyrics(string)
* @method setExplicit(bool)
*/
class Audio extends Media
{
protected $tableName = "audios";
protected $fileExtension = "mpd";
# Taken from winamp :D
public const genres = [
'A Cappella', 'Abstract', 'Acid', 'Acid Jazz', 'Acid Punk', 'Acoustic', 'AlternRock', 'Alternative', 'Ambient', 'Anime', 'Art Rock', 'Audio Theatre', 'Audiobook', 'Avantgarde', 'Ballad', 'Baroque', 'Bass', 'Beat', 'Bebob', 'Bhangra', 'Big Band', 'Big Beat', 'Black Metal', 'Bluegrass', 'Blues', 'Booty Bass', 'Breakbeat', 'BritPop', 'Cabaret', 'Celtic', 'Chamber Music', 'Chanson', 'Chillout', 'Chorus', 'Christian Gangsta Rap', 'Christian Rap', 'Christian Rock', 'Classic Rock', 'Classical', 'Club', 'Club-House', 'Comedy', 'Contemporary Christian', 'Country', 'Crossover', 'Cult', 'Dance', 'Dance Hall', 'Darkwave', 'Death Metal', 'Disco', 'Downtempo', 'Dream', 'Drum & Bass', 'Drum Solo', 'Dub', 'Dubstep', 'Duet', 'EBM', 'Easy Listening', 'Eclectic', 'Electro', 'Electroclash', 'Electronic', 'Emo', 'Ethnic', 'Euro-House', 'Euro-Techno', 'Eurodance', 'Experimental', 'Fast Fusion', 'Folk', 'Folk-Rock', 'Folklore', 'Freestyle', 'Funk', 'Fusion', 'G-Funk', 'Game', 'Gangsta Rap', 'Garage', 'Garage Rock', 'Global', 'Goa', 'Gospel', 'Gothic', 'Gothic Rock', 'Grunge', 'Hard Rock', 'Hardcore', 'Heavy Metal', 'Hip-Hop', 'House', 'Humour', 'IDM', 'Illbient', 'Indie', 'Indie Rock', 'Industrial', 'Industro-Goth', 'Instrumental', 'Instrumental Pop', 'Instrumental Rock', 'JPop', 'Jam Band', 'Jazz', 'Jazz+Funk', 'Jungle', 'Krautrock', 'Latin', 'Leftfield', 'Lo-Fi', 'Lounge', 'Math Rock', 'Meditative', 'Merengue', 'Metal', 'Musical', 'National Folk', 'Native American', 'Negerpunk', 'Neoclassical', 'Neue Deutsche Welle', 'New Age', 'New Romantic', 'New Wave', 'Noise', 'Nu-Breakz', 'Oldies', 'Opera', 'Other', 'Podcast', 'Polka', 'Polsk Punk', 'Pop', 'Pop / Funk', 'Pop-Folk', 'Porn Groove', 'Post-Punk', 'Post-Rock', 'Power Ballad', 'Pranks', 'Primus', 'Progressive Rock', 'Psybient', 'Psychedelic', 'Psychedelic Rock', 'Psychobilly', 'Psytrance', 'Punk', 'Punk Rock', 'R&B', 'Rap', 'Rave', 'Reggae', 'Retro', 'Revival', 'Rhythmic Soul', 'Rock', 'Rock & Roll', 'Salsa', 'Samba', 'Satire', 'Shoegaze', 'Showtunes', 'Ska', 'Slow Jam', 'Slow Rock', 'Sonata', 'Soul', 'Sound Clip', 'Soundtrack', 'Southern Rock', 'Space', 'Space Rock', 'Speech', 'Swing', 'Symphonic Rock', 'Symphony', 'Synthpop', 'Tango', 'Techno', 'Techno-Industrial', 'Terror', 'Thrash Metal', 'Top 40', 'Touhou', 'Trailer', 'Trance', 'Tribal', 'Trip-Hop', 'Trop Rock', 'Vocal', 'World Music',
];
# Taken from: https://web.archive.org/web/20220322153107/https://dev.vk.com/reference/objects/audio-genres
public const vkGenres = [
"Rock" => 1,
"Pop" => 2,
"Rap" => 3,
"Hip-Hop" => 3, # VK API lists №3 as Rap & Hip-Hop, but these genres are distinct in OpenVK
"Easy Listening" => 4,
"House" => 5,
"Dance" => 5,
"Instrumental" => 6,
"Metal" => 7,
"Alternative" => 21,
"Dubstep" => 8,
"Jazz" => 1001,
"Blues" => 1001,
"Drum & Bass" => 10,
"Trance" => 11,
"Chanson" => 12,
"Ethnic" => 13,
"Acoustic" => 14,
"Vocal" => 14,
"Reggae" => 15,
"Classical" => 16,
"Indie Pop" => 17,
"Speech" => 19,
"Disco" => 22,
"Other" => 18,
];
private function fileLength(string $filename): int
{
if (!Shell::commandAvailable("ffmpeg") || !Shell::commandAvailable("ffprobe")) {
throw new \Exception();
}
$error = null;
$streams = Shell::ffprobe("-i", $filename, "-show_streams", "-select_streams a", "-loglevel error")->execute($error);
if ($error !== 0) {
throw new \DomainException("$filename is not recognized as media container");
} elseif (empty($streams) || ctype_space($streams)) {
throw new \DomainException("$filename does not contain any audio streams");
}
$vstreams = Shell::ffprobe("-i", $filename, "-show_streams", "-select_streams v", "-loglevel error")->execute($error);
# check if audio has cover (attached_pic)
preg_match("%attached_pic=([0-1])%", $vstreams, $hasCover);
if (!empty($vstreams) && !ctype_space($vstreams) && ((int) ($hasCover[1]) !== 1)) {
throw new \DomainException("$filename is a video");
}
$durations = [];
preg_match_all('%duration=([0-9\.]++)%', $streams, $durations);
if (sizeof($durations[1]) === 0) {
throw new \DomainException("$filename does not contain any meaningful audio streams");
}
$length = 0;
foreach ($durations[1] as $duration) {
$duration = floatval($duration);
if ($duration < 1.0 || $duration > 65536.0) {
throw new \DomainException("$filename does not contain any meaningful audio streams");
} else {
$length = max($length, $duration);
}
}
return (int) round($length, 0, PHP_ROUND_HALF_EVEN);
}
/**
* @throws \Exception
*/
protected function saveFile(string $filename, string $hash): bool
{
$duration = $this->fileLength($filename);
$kid = openssl_random_pseudo_bytes(16);
$key = openssl_random_pseudo_bytes(16);
$tok = openssl_random_pseudo_bytes(28);
$ss = ceil($duration / 15);
$this->stateChanges("kid", $kid);
$this->stateChanges("key", $key);
$this->stateChanges("token", $tok);
$this->stateChanges("segment_size", $ss);
$this->stateChanges("length", $duration);
try {
$args = [
str_replace("enabled", "available", OPENVK_ROOT),
str_replace("enabled", "available", $this->getBaseDir()),
$hash,
$filename,
bin2hex($kid),
bin2hex($key),
bin2hex($tok),
$ss,
];
if (Shell::isPowershell()) {
Shell::powershell("-executionpolicy bypass", "-File", __DIR__ . "/../shell/processAudio.ps1", ...$args)
->start();
} else {
Shell::bash(__DIR__ . "/../shell/processAudio.sh", ...$args) // Pls workkkkk
->start(); // idk, not tested :")
}
# Wait until processAudio will consume the file
$start = time();
while (file_exists($filename)) {
if (time() - $start > 5) {
throw new \RuntimeException("Timed out waiting FFMPEG");
}
}
} catch (UnknownCommandException $ucex) {
exit(OPENVK_ROOT_CONF["openvk"]["debug"] ? "bash/pwsh is not installed" : VIDEOS_FRIENDLY_ERROR);
}
return true;
}
public function getTitle(): string
{
return $this->getRecord()->name;
}
public function getPerformer(): string
{
return $this->getRecord()->performer;
}
public function getPerformers(): array
{
return explode(", ", $this->getRecord()->performer);
}
public function getName(): string
{
return $this->getPerformer() . "" . $this->getTitle();
}
public function getDownloadName(): string
{
return preg_replace('/[\\/:*?"<>|]/', '_', str_replace(' ', '_', $this->getName()));
}
public function getGenre(): ?string
{
return $this->getRecord()->genre;
}
public function getLyrics(): ?string
{
return !is_null($this->getRecord()->lyrics) ? htmlspecialchars($this->getRecord()->lyrics, ENT_DISALLOWED | ENT_XHTML) : null;
}
public function getLength(): int
{
return $this->getRecord()->length;
}
public function getFormattedLength(): string
{
$len = $this->getLength();
$mins = floor($len / 60);
$secs = $len - ($mins * 60);
return (
str_pad((string) $mins, 2, "0", STR_PAD_LEFT)
. ":" .
str_pad((string) $secs, 2, "0", STR_PAD_LEFT)
);
}
public function getSegmentSize(): float
{
return $this->getRecord()->segment_size;
}
public function getListens(): int
{
return $this->getRecord()->listens;
}
public function getOriginalURL(bool $force = false): string
{
$disallowed = !OPENVK_ROOT_CONF["openvk"]["preferences"]["music"]["exposeOriginalURLs"] && !$force;
if (!$this->isAvailable() || $disallowed) {
return ovk_scheme(true)
. $_SERVER["HTTP_HOST"] . ":"
. $_SERVER["HTTP_PORT"]
. "/assets/packages/static/openvk/audio/nomusic.mp3";
}
$key = bin2hex($this->getRecord()->token);
return str_replace(".mpd", "_fragments", $this->getURL()) . "/original_$key.mp3";
}
public function getURL(?bool $force = false): string
{
if ($this->isWithdrawn()) {
return "";
}
return parent::getURL();
}
public function getKeys(): array
{
$keys[bin2hex($this->getRecord()->kid)] = bin2hex($this->getRecord()->key);
return $keys;
}
public function isAnonymous(): bool
{
return false;
}
public function isExplicit(): bool
{
return (bool) $this->getRecord()->explicit;
}
public function isWithdrawn(): bool
{
return (bool) $this->getRecord()->withdrawn;
}
public function isUnlisted(): bool
{
return (bool) $this->getRecord()->unlisted;
}
# NOTICE may flush model to DB if it was just processed
public function isAvailable(): bool
{
if ($this->getRecord()->processed) {
return true;
}
# throttle requests to isAvailable to prevent DoS attack if filesystem is actually an S3 storage
if (time() - $this->getRecord()->checked < 5) {
return false;
}
try {
$fragments = str_replace(".mpd", "_fragments", $this->getFileName());
$original = "original_" . bin2hex($this->getRecord()->token) . ".mp3";
if (file_exists("$fragments/$original")) {
# Original gets uploaded after fragments
$this->stateChanges("processed", 0x01);
return true;
}
} finally {
$this->stateChanges("checked", time());
$this->save();
}
return false;
}
public function isInLibraryOf($entity): bool
{
return sizeof(DatabaseConnection::i()->getContext()->table("audio_relations")->where([
"entity" => $entity->getId() * ($entity instanceof Club ? -1 : 1),
"audio" => $this->getId(),
])) != 0;
}
public function add($entity): bool
{
if ($this->isInLibraryOf($entity)) {
return false;
}
$entityId = $entity->getId() * ($entity instanceof Club ? -1 : 1);
$audioRels = DatabaseConnection::i()->getContext()->table("audio_relations");
if (sizeof($audioRels->where("entity", $entityId)) > 65536) {
throw new \OverflowException("Can't have more than 65536 audios in a playlist");
}
$audioRels->insert([
"entity" => $entityId,
"audio" => $this->getId(),
]);
return true;
}
public function remove($entity): bool
{
if (!$this->isInLibraryOf($entity)) {
return false;
}
DatabaseConnection::i()->getContext()->table("audio_relations")->where([
"entity" => $entity->getId() * ($entity instanceof Club ? -1 : 1),
"audio" => $this->getId(),
])->delete();
return true;
}
public function listen($entity, Playlist $playlist = null): bool
{
$listensTable = DatabaseConnection::i()->getContext()->table("audio_listens");
$lastListen = $listensTable->where([
"entity" => $entity->getRealId(),
"audio" => $this->getId(),
])->order("index DESC")->fetch();
if (!$lastListen || (time() - $lastListen->time >= $this->getLength())) {
$listensTable->insert([
"entity" => $entity->getRealId(),
"audio" => $this->getId(),
"time" => time(),
"playlist" => $playlist ? $playlist->getId() : null,
]);
if ($entity instanceof User) {
$this->stateChanges("listens", ($this->getListens() + 1));
$this->save();
if ($playlist) {
$playlist->incrementListens();
$playlist->save();
}
}
$entity->setLast_played_track($this->getId());
$entity->save();
return true;
}
$lastListen->update([
"time" => time(),
]);
return false;
}
/**
* Returns compatible with VK API 4.x, 5.x structure.
*
* Always sets album(_id) to NULL at this time.
* If genre is not present in VK genre list, fallbacks to "Other".
* The url and manifest properties will be set to false if the audio can't be played (processing, removed).
*
* Aside from standard VK properties, this method will also return some OVK extended props:
* 1. added - Is in the library of $user?
* 2. editable - Can be edited by $user?
* 3. withdrawn - Removed due to copyright request?
* 4. ready - Can be played at this time?
* 5. genre_str - Full name of genre, NULL if it's undefined
* 6. manifest - URL to MPEG-DASH manifest
* 7. keys - ClearKey DRM keys
* 8. explicit - Marked as NSFW?
* 9. searchable - Can be found via search?
* 10. unique_id - Unique ID of audio
*
* @notice that in case if exposeOriginalURLs is set to false in config, "url" will always contain link to nomusic.mp3,
* unless $forceURLExposure is set to true.
*
* @notice may trigger db flush if the audio is not processed yet, use with caution on unsaved models.
*
* @param ?User $user user, relative to whom "added", "editable" will be set
* @param bool $forceURLExposure force set "url" regardless of config
*/
public function toVkApiStruct(?User $user = null, bool $forceURLExposure = false): object
{
$obj = (object) [];
$obj->unique_id = base64_encode((string) $this->getId());
$obj->id = $obj->aid = $this->getVirtualId();
$obj->artist = $this->getPerformer();
$obj->title = $this->getTitle();
$obj->duration = $this->getLength();
$obj->album_id = $obj->album = null; # i forgor to implement
$obj->url = false;
$obj->manifest = false;
$obj->keys = false;
$obj->genre_id = $obj->genre = self::vkGenres[$this->getGenre() ?? ""] ?? 18; # return Other if no match
$obj->genre_str = $this->getGenre();
$obj->owner_id = $this->getOwner()->getId();
if ($this->getOwner() instanceof Club) {
$obj->owner_id *= -1;
}
$obj->lyrics = null;
if (!is_null($this->getLyrics())) {
$obj->lyrics = $this->getId();
}
$obj->added = $user && $this->isInLibraryOf($user);
$obj->editable = $user && $this->canBeModifiedBy($user);
$obj->searchable = !$this->isUnlisted();
$obj->explicit = $this->isExplicit();
$obj->withdrawn = $this->isWithdrawn();
$obj->ready = $this->isAvailable() && !$obj->withdrawn;
if ($obj->ready) {
$obj->url = $this->getOriginalURL($forceURLExposure);
$obj->manifest = $this->getURL();
$obj->keys = $this->getKeys();
}
return $obj;
}
public function setOwner(int $oid): void
{
# WARNING: API implementation won't be able to handle groups like that, don't remove
if ($oid <= 0) {
throw new \OutOfRangeException("Only users can be owners of audio!");
}
$this->stateChanges("owner", $oid);
}
public function setGenre(string $genre): void
{
if (!in_array($genre, Audio::genres)) {
$this->stateChanges("genre", null);
return;
}
$this->stateChanges("genre", $genre);
}
public function setCopyrightStatus(bool $withdrawn = true): void
{
$this->stateChanges("withdrawn", $withdrawn);
}
public function setSearchability(bool $searchable = true): void
{
$this->stateChanges("unlisted", !$searchable);
}
public function setToken(string $tok): void
{
throw new \LogicException("Changing keys is not supported.");
}
public function setKid(string $kid): void
{
throw new \LogicException("Changing keys is not supported.");
}
public function setKey(string $key): void
{
throw new \LogicException("Changing keys is not supported.");
}
public function setLength(int $len): void
{
throw new \LogicException("Changing length is not supported.");
}
public function setSegment_Size(int $len): void
{
throw new \LogicException("Changing length is not supported.");
}
public function delete(bool $softly = true): void
{
$ctx = DatabaseConnection::i()->getContext();
$ctx->table("audio_relations")->where("audio", $this->getId())
->delete();
$ctx->table("audio_listens")->where("audio", $this->getId())
->delete();
$ctx->table("playlist_relations")->where("media", $this->getId())
->delete();
parent::delete($softly);
}
}

View file

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime;
use openvk\Web\Models\Repositories\{Users};
use Nette\Database\Table\ActiveRow;
class Ban extends RowModel
{
protected $tableName = "bans";
public function getId(): int
{
return $this->getRecord()->id;
}
public function getReason(): ?string
{
return $this->getRecord()->reason;
}
public function getUser(): ?User
{
return (new Users())->get($this->getRecord()->user);
}
public function getInitiator(): ?User
{
return (new Users())->get($this->getRecord()->initiator);
}
public function getStartTime(): int
{
return $this->getRecord()->iat;
}
public function getEndTime(): int
{
return $this->getRecord()->exp;
}
public function getTime(): int
{
return $this->getRecord()->time;
}
public function isPermanent(): bool
{
return $this->getEndTime() === 0;
}
public function isRemovedManually(): bool
{
return (bool) $this->getRecord()->removed_manually;
}
public function isOver(): bool
{
return $this->isRemovedManually();
}
public function whoRemoved(): ?User
{
return (new Users())->get($this->getRecord()->removed_by);
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{User}; use openvk\Web\Models\Entities\{User};
use openvk\Web\Models\Repositories\{Users}; use openvk\Web\Models\Repositories\{Users};
@ -10,39 +14,39 @@ class BannedLink extends RowModel
protected $tableName = "links_banned"; protected $tableName = "links_banned";
private $overrideContentColumn = "reason"; private $overrideContentColumn = "reason";
function getId(): int public function getId(): int
{ {
return $this->getRecord()->id; return $this->getRecord()->id;
} }
function getDomain(): string public function getDomain(): string
{ {
return $this->getRecord()->domain; return $this->getRecord()->domain;
} }
function getReason(): string public function getReason(): string
{ {
return $this->getRecord()->reason ?? tr("url_is_banned_default_reason"); return $this->getRecord()->reason ?? tr("url_is_banned_default_reason");
} }
function getInitiator(): ?User public function getInitiator(): ?User
{ {
return (new Users)->get($this->getRecord()->initiator); return (new Users())->get($this->getRecord()->initiator);
} }
function getComment(): string public function getComment(): string
{ {
return OPENVK_ROOT_CONF["openvk"]["preferences"]["susLinks"]["showReason"] return OPENVK_ROOT_CONF["openvk"]["preferences"]["susLinks"]["showReason"]
? tr("url_is_banned_comment_r", OPENVK_ROOT_CONF["openvk"]["appearance"]["name"], $this->getReason()) ? tr("url_is_banned_comment_r", OPENVK_ROOT_CONF["openvk"]["appearance"]["name"], $this->getReason())
: tr("url_is_banned_comment", OPENVK_ROOT_CONF["openvk"]["appearance"]["name"]); : tr("url_is_banned_comment", OPENVK_ROOT_CONF["openvk"]["appearance"]["name"]);
} }
function getRegexpRule(): string public function getRegexpRule(): string
{ {
return "/^" . $this->getDomain() . "\/" . $this->getRawRegexp() . "$/i"; return "/^" . $this->getDomain() . "\/" . $this->getRawRegexp() . "$/i";
} }
function getRawRegexp(): string public function getRawRegexp(): string
{ {
return $this->getRecord()->regexp_rule; return $this->getRecord()->regexp_rule;
} }

View file

@ -1,220 +1,263 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{User, Manager}; use openvk\Web\Models\Entities\{User, Manager};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Managers}; use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Managers, Posts};
use Nette\Database\Table\{ActiveRow, GroupedSelection}; use Nette\Database\Table\{ActiveRow, GroupedSelection};
use Chandler\Database\DatabaseConnection as DB; use Chandler\Database\DatabaseConnection as DB;
use Chandler\Security\User as ChandlerUser; use Chandler\Security\User as ChandlerUser;
class Club extends RowModel class Club extends RowModel
{ {
use Traits\TBackDrops;
use Traits\TSubscribable;
use Traits\TAudioStatuses;
use Traits\TIgnorable;
protected $tableName = "groups"; protected $tableName = "groups";
const TYPE_GROUP = 1; public const TYPE_GROUP = 1;
const TYPE_PUBLIC = 1; public const TYPE_PUBLIC = 1;
const TYPE_EVENT = 2; public const TYPE_EVENT = 2;
const OPEN = 0; public const OPEN = 0;
const CLOSED = 1; public const CLOSED = 1;
const PRIVATE = 2; public const PRIVATE = 2;
const NOT_RELATED = 0; public const NOT_RELATED = 0;
const SUBSCRIBED = 1; public const SUBSCRIBED = 1;
const REQUEST_SENT = 2; public const REQUEST_SENT = 2;
function getId(): int public const WALL_CLOSED = 0;
public const WALL_OPEN = 1;
public const WALL_LIMITED = 2;
public function getId(): int
{ {
return $this->getRecord()->id; return $this->getRecord()->id;
} }
function getAvatarPhoto(): ?Photo public function getAvatarPhoto(): ?Photo
{ {
$avAlbum = (new Albums)->getClubAvatarAlbum($this); $avAlbum = (new Albums())->getClubAvatarAlbum($this);
$avCount = $avAlbum->getPhotosCount(); $avCount = $avAlbum->getPhotosCount();
$avPhotos = $avAlbum->getPhotos($avCount, 1); $avPhotos = $avAlbum->getPhotos($avCount, 1);
return iterator_to_array($avPhotos)[0] ?? NULL; return iterator_to_array($avPhotos)[0] ?? null;
} }
function getAvatarUrl(string $size = "miniscule"): string public function getAvatarUrl(string $size = "miniscule", $avPhoto = null): string
{ {
$serverUrl = ovk_scheme(true) . $_SERVER["HTTP_HOST"]; $serverUrl = ovk_scheme(true) . $_SERVER["HTTP_HOST"];
$avPhoto = $this->getAvatarPhoto(); if (!$avPhoto) {
$avPhoto = $this->getAvatarPhoto();
}
return is_null($avPhoto) ? "$serverUrl/assets/packages/static/openvk/img/camera_200.png" : $avPhoto->getURLBySizeId($size); return is_null($avPhoto) ? "$serverUrl/assets/packages/static/openvk/img/camera_200.png" : $avPhoto->getURLBySizeId($size);
} }
function getAvatarLink(): string public function getWallType(): int
{
return $this->getRecord()->wall;
}
public function getAvatarLink(): string
{ {
$avPhoto = $this->getAvatarPhoto(); $avPhoto = $this->getAvatarPhoto();
if(!$avPhoto) return "javascript:void(0)"; if (!$avPhoto) {
return "javascript:void(0)";
}
$pid = $avPhoto->getPrettyId(); $pid = $avPhoto->getPrettyId();
$aid = (new Albums)->getClubAvatarAlbum($this)->getId(); $aid = (new Albums())->getClubAvatarAlbum($this)->getId();
return "/photo$pid?from=album$aid"; return "/photo$pid?from=album$aid";
} }
function getURL(): string public function getURL(): string
{ {
if(!is_null($this->getShortCode())) if (!is_null($this->getShortCode())) {
return "/" . $this->getShortCode(); return "/" . $this->getShortCode();
else } else {
return "/club" . $this->getId(); return "/club" . $this->getId();
}
} }
function getName(): string public function getName(): string
{ {
return $this->getRecord()->name; return $this->getRecord()->name;
} }
function getCanonicalName(): string public function getCanonicalName(): string
{ {
return $this->getName(); return $this->getName();
} }
function getOwner(): ?User public function getOwner(): ?User
{ {
return (new Users)->get($this->getRecord()->owner); return (new Users())->get($this->getRecord()->owner);
} }
function getOwnerComment(): string public function getOwnerComment(): string
{ {
return is_null($this->getRecord()->owner_comment) ? "" : $this->getRecord()->owner_comment; return is_null($this->getRecord()->owner_comment) ? "" : $this->getRecord()->owner_comment;
} }
function isOwnerHidden(): bool public function isOwnerHidden(): bool
{ {
return (bool) $this->getRecord()->owner_hidden; return (bool) $this->getRecord()->owner_hidden;
} }
function isOwnerClubPinned(): bool public function isOwnerClubPinned(): bool
{ {
return (bool) $this->getRecord()->owner_club_pinned; return (bool) $this->getRecord()->owner_club_pinned;
} }
function getDescription(): ?string public function getDescription(): ?string
{ {
return $this->getRecord()->about; return $this->getRecord()->about;
} }
function getDescriptionHtml(): ?string public function getDescriptionHtml(): ?string
{ {
if(!is_null($this->getDescription())) if (!is_null($this->getDescription())) {
return nl2br(htmlspecialchars($this->getDescription(), ENT_DISALLOWED | ENT_XHTML)); return nl2br(htmlspecialchars($this->getDescription(), ENT_DISALLOWED | ENT_XHTML));
else } else {
return NULL; return null;
}
} }
function getShortCode(): ?string public function getShortCode(): ?string
{ {
return $this->getRecord()->shortcode; return $this->getRecord()->shortcode;
} }
function getBanReason(): ?string public function getBanReason(): ?string
{ {
return $this->getRecord()->block_reason; return $this->getRecord()->block_reason;
} }
function getOpennesStatus(): int public function getOpennesStatus(): int
{ {
return $this->getRecord()->closed; return $this->getRecord()->closed;
} }
function getAdministratorsListDisplay(): int public function getAdministratorsListDisplay(): int
{ {
return $this->getRecord()->administrators_list_display; return $this->getRecord()->administrators_list_display;
} }
function isEveryoneCanCreateTopics(): bool public function isEveryoneCanCreateTopics(): bool
{ {
return (bool) $this->getRecord()->everyone_can_create_topics; return (bool) $this->getRecord()->everyone_can_create_topics;
} }
function isDisplayTopicsAboveWallEnabled(): bool public function isDisplayTopicsAboveWallEnabled(): bool
{ {
return (bool) $this->getRecord()->display_topics_above_wall; return (bool) $this->getRecord()->display_topics_above_wall;
} }
function isHideFromGlobalFeedEnabled(): bool public function isHideFromGlobalFeedEnabled(): bool
{ {
return (bool) $this->getRecord()->hide_from_global_feed; return (bool) $this->getRecord()->hide_from_global_feed;
} }
function getType(): int public function isHidingFromGlobalFeedEnforced(): bool
{
return (bool) $this->getRecord()->enforce_hiding_from_global_feed;
}
public function getType(): int
{ {
return $this->getRecord()->type; return $this->getRecord()->type;
} }
function isVerified(): bool public function isVerified(): bool
{ {
return (bool) $this->getRecord()->verified; return (bool) $this->getRecord()->verified;
} }
function isBanned(): bool public function isBanned(): bool
{ {
return !is_null($this->getBanReason()); return !is_null($this->getBanReason());
} }
function canPost(): bool public function canPost(): bool
{ {
return (bool) $this->getRecord()->wall; return (bool) $this->getRecord()->wall;
} }
function setShortCode(?string $code = NULL): ?bool public function setShortCode(?string $code = null): ?bool
{ {
if(!is_null($code)) { if (!is_null($code)) {
if(!preg_match("%^[a-z][a-z0-9\\.\\_]{0,30}[a-z0-9]$%", $code)) if (!preg_match("%^[a-z][a-z0-9\\.\\_]{0,30}[a-z0-9]$%", $code)) {
return false; return false;
if(in_array($code, OPENVK_ROOT_CONF["openvk"]["preferences"]["shortcodes"]["forbiddenNames"])) }
if (in_array($code, OPENVK_ROOT_CONF["openvk"]["preferences"]["shortcodes"]["forbiddenNames"])) {
return false; return false;
if(\Chandler\MVC\Routing\Router::i()->getMatchingRoute("/$code")[0]->presenter !== "UnknownTextRouteStrategy") }
if (\Chandler\MVC\Routing\Router::i()->getMatchingRoute("/$code")[0]->presenter !== "UnknownTextRouteStrategy") {
return false; return false;
}
$pUser = DB::i()->getContext()->table("profiles")->where("shortcode", $code)->fetch(); $pUser = DB::i()->getContext()->table("profiles")->where("shortcode", $code)->fetch();
if(!is_null($pUser)) if (!is_null($pUser)) {
return false; return false;
}
} }
$this->stateChanges("shortcode", $code); $this->stateChanges("shortcode", $code);
return true; return true;
} }
function isSubscriptionAccepted(User $user): bool public function setWall(int $type)
{
if ($type > 2 || $type < 0) {
throw new \LogicException("Invalid wall");
}
$this->stateChanges("wall", $type);
}
public function isSubscriptionAccepted(User $user): bool
{ {
return !is_null($this->getRecord()->related("subscriptions.follower")->where([ return !is_null($this->getRecord()->related("subscriptions.follower")->where([
"follower" => $this->getId(), "follower" => $this->getId(),
"target" => $user->getId(), "target" => $user->getId(),
])->fetch());; ])->fetch());
;
} }
function getPostViewStats(bool $unique = false): ?array public function getPostViewStats(bool $unique = false): ?array
{ {
$edb = eventdb(); $edb = eventdb();
if(!$edb) if (!$edb) {
return NULL; return null;
}
$subs = []; $subs = [];
$viral = []; $viral = [];
$total = []; $total = [];
for($i = 1; $i < 8; $i++) { for ($i = 1; $i < 8; $i++) {
$begin = strtotime("-" . $i . "day midnight"); $begin = strtotime("-" . $i . "day midnight");
$end = $i === 1 ? time() + 10 : strtotime("-" . ($i - 1) . "day midnight"); $end = $i === 1 ? time() + 10 : strtotime("-" . ($i - 1) . "day midnight");
$query = "SELECT COUNT(" . ($unique ? "DISTINCT profile" : "*") . ") AS cnt FROM postViews"; $query = "SELECT COUNT(" . ($unique ? "DISTINCT profile" : "*") . ") AS cnt FROM postViews";
$query .= " WHERE `group`=1 AND owner=" . $this->getId(); $query .= " WHERE `group`=1 AND owner=" . $this->getId();
$query .= " AND timestamp > $begin AND timestamp < $end"; $query .= " AND timestamp > $begin AND timestamp < $end";
$sub = $edb->getConnection()->query("$query AND NOT subscribed=0")->fetch()->cnt; $sub = $edb->getConnection()->query("$query AND NOT subscribed=0")->fetch()->cnt;
$vir = $edb->getConnection()->query("$query AND subscribed=0")->fetch()->cnt; $vir = $edb->getConnection()->query("$query AND subscribed=0")->fetch()->cnt;
$subs[] = $sub; $subs[] = $sub;
$viral[] = $vir; $viral[] = $vir;
$total[] = $sub + $vir; $total[] = $sub + $vir;
} }
return [ return [
"total" => [ "total" => [
"x" => array_reverse(range(1, 7)), "x" => array_reverse(range(1, 7)),
@ -224,7 +267,7 @@ class Club extends RowModel
"shape" => "spline", "shape" => "spline",
"color" => "#597da3", "color" => "#597da3",
], ],
"name" => $unique ? "Полный охват" : "Все просмотры", "name" => $unique ? tr("full_coverage") : tr("all_views"),
], ],
"subs" => [ "subs" => [
"x" => array_reverse(range(1, 7)), "x" => array_reverse(range(1, 7)),
@ -235,7 +278,7 @@ class Club extends RowModel
"color" => "#b05c91", "color" => "#b05c91",
], ],
"fill" => "tozeroy", "fill" => "tozeroy",
"name" => $unique ? "Охват подписчиков" : "Просмотры подписчиков", "name" => $unique ? tr("subs_coverage") : tr("subs_views"),
], ],
"viral" => [ "viral" => [
"x" => array_reverse(range(1, 7)), "x" => array_reverse(range(1, 7)),
@ -246,85 +289,109 @@ class Club extends RowModel
"color" => "#4d9fab", "color" => "#4d9fab",
], ],
"fill" => "tozeroy", "fill" => "tozeroy",
"name" => $unique ? "Виральный охват" : "Виральные просмотры", "name" => $unique ? tr("viral_coverage") : tr("viral_views"),
], ],
]; ];
} }
function getSubscriptionStatus(User $user): bool public function getSubscriptionStatus(User $user): bool
{ {
$subbed = !is_null($this->getRecord()->related("subscriptions.target")->where([ $subbed = !is_null($this->getRecord()->related("subscriptions.target")->where([
"target" => $this->getId(), "target" => $this->getId(),
"model" => static::class, "model" => static::class,
"follower" => $user->getId(), "follower" => $user->getId(),
])->fetch()); ])->fetch());
return $subbed && ($this->getOpennesStatus() === static::CLOSED ? $this->isSubscriptionAccepted($user) : true); return $subbed && ($this->getOpennesStatus() === static::CLOSED ? $this->isSubscriptionAccepted($user) : true);
} }
function getFollowersQuery(string $sort = "follower ASC"): GroupedSelection public function getFollowersQuery(string $sort = "follower ASC"): GroupedSelection
{ {
$query = $this->getRecord()->related("subscriptions.target"); $query = $this->getRecord()->related("subscriptions.target");
if($this->getOpennesStatus() === static::OPEN) { if ($this->getOpennesStatus() === static::OPEN) {
$query = $query->where("model", "openvk\\Web\\Models\\Entities\\Club")->order($sort); $query = $query->where("model", "openvk\\Web\\Models\\Entities\\Club")->order($sort);
} else { } else {
return false; return false;
} }
return $query->group("follower"); return $query->group("follower");
} }
function getFollowersCount(): int public function getFollowersCount(): int
{ {
return sizeof($this->getFollowersQuery()); return sizeof($this->getFollowersQuery());
} }
function getFollowers(int $page = 1, int $perPage = 6, string $sort = "follower ASC"): \Traversable public function getFollowers(int $page = 1, int $perPage = 6, string $sort = "follower ASC"): \Traversable
{ {
$rels = $this->getFollowersQuery($sort)->page($page, $perPage); $rels = $this->getFollowersQuery($sort)->page($page, $perPage);
foreach($rels as $rel) { foreach ($rels as $rel) {
$rel = (new Users)->get($rel->follower); $rel = (new Users())->get($rel->follower);
if(!$rel) continue; if (!$rel) {
continue;
}
yield $rel; yield $rel;
} }
} }
function getManagers(int $page = 1, bool $ignoreHidden = false): \Traversable public function getSuggestedPostsCount(User $user = null)
{
$count = 0;
if (is_null($user)) {
return null;
}
if ($this->canBeModifiedBy($user)) {
$count = (new Posts())->getSuggestedPostsCount($this->getId());
} else {
$count = (new Posts())->getSuggestedPostsCountByUser($this->getId(), $user->getId());
}
return $count;
}
public function getManagers(int $page = 1, bool $ignoreHidden = false): \Traversable
{ {
$rels = $this->getRecord()->related("group_coadmins.club")->page($page, 6); $rels = $this->getRecord()->related("group_coadmins.club")->page($page, 6);
if($ignoreHidden) if ($ignoreHidden) {
$rels = $rels->where("hidden", false); $rels = $rels->where("hidden", false);
}
foreach($rels as $rel) {
$rel = (new Managers)->get($rel->id); foreach ($rels as $rel) {
if(!$rel) continue; $rel = (new Managers())->get($rel->id);
if (!$rel) {
continue;
}
yield $rel; yield $rel;
} }
} }
function getManager(User $user, bool $ignoreHidden = false): ?Manager public function getManager(User $user, bool $ignoreHidden = false): ?Manager
{ {
$manager = (new Managers)->getByUserAndClub($user->getId(), $this->getId()); $manager = (new Managers())->getByUserAndClub($user->getId(), $this->getId());
if ($ignoreHidden && $manager !== NULL && $manager->isHidden()) if ($ignoreHidden && $manager !== null && $manager->isHidden()) {
return NULL; return null;
}
return $manager; return $manager;
} }
function getManagersCount(bool $ignoreHidden = false): int public function getManagersCount(bool $ignoreHidden = false): int
{ {
if($ignoreHidden) if ($ignoreHidden) {
return sizeof($this->getRecord()->related("group_coadmins.club")->where("hidden", false)) + (int) !$this->isOwnerHidden(); return sizeof($this->getRecord()->related("group_coadmins.club")->where("hidden", false)) + (int) !$this->isOwnerHidden();
}
return sizeof($this->getRecord()->related("group_coadmins.club")) + 1; return sizeof($this->getRecord()->related("group_coadmins.club")) + 1;
} }
function addManager(User $user, ?string $comment = NULL): void public function addManager(User $user, ?string $comment = null): void
{ {
DB::i()->getContext()->table("group_coadmins")->insert([ DB::i()->getContext()->table("group_coadmins")->insert([
"club" => $this->getId(), "club" => $this->getId(),
@ -332,63 +399,138 @@ class Club extends RowModel
"comment" => $comment, "comment" => $comment,
]); ]);
} }
function removeManager(User $user): void public function removeManager(User $user): void
{ {
DB::i()->getContext()->table("group_coadmins")->where([ DB::i()->getContext()->table("group_coadmins")->where([
"club" => $this->getId(), "club" => $this->getId(),
"user" => $user->getId(), "user" => $user->getId(),
])->delete(); ])->delete();
} }
function canBeModifiedBy(User $user): bool public function canBeModifiedBy(User $user): bool
{ {
$id = $user->getId(); $id = $user->getId();
if($this->getOwner()->getId() === $id) if ($this->getOwner()->getId() === $id) {
return true; return true;
}
return !is_null($this->getRecord()->related("group_coadmins.club")->where("user", $id)->fetch()); return !is_null($this->getRecord()->related("group_coadmins.club")->where("user", $id)->fetch());
} }
function getWebsite(): ?string public function getWebsite(): ?string
{ {
return $this->getRecord()->website; return $this->getRecord()->website;
} }
function getAlert(): ?string public function ban(string $reason): void
{
$this->setBlock_Reason($reason);
$this->save();
}
public function unban(): void
{
$this->setBlock_Reason(null);
$this->save();
}
public function canBeViewedBy(?User $user = null)
{
return is_null($this->getBanReason());
}
public function getAlert(): ?string
{ {
return $this->getRecord()->alert; return $this->getRecord()->alert;
} }
function toVkApiStruct(?User $user = NULL): object public function getRealId(): int
{ {
$res = []; return $this->getId() * -1;
}
public function isEveryoneCanUploadAudios(): bool
{
return (bool) $this->getRecord()->everyone_can_upload_audios;
}
public function canUploadAudio(?User $user): bool
{
if (!$user) {
return null;
}
return $this->isEveryoneCanUploadAudios() || $this->canBeModifiedBy($user);
}
public function canUploadDocs(?User $user): bool
{
if (!$user) {
return false;
}
return $this->canBeModifiedBy($user);
}
public function getAudiosCollectionSize()
{
return (new \openvk\Web\Models\Repositories\Audios())->getClubCollectionSize($this);
}
public function toVkApiStruct(?User $user = null, string $fields = ''): object
{
$res = (object) [];
$res->id = $this->getId(); $res->id = $this->getId();
$res->name = $this->getName(); $res->name = $this->getName();
$res->screen_name = $this->getShortCode(); $res->screen_name = $this->getShortCode() ?? "club" . $this->getId();
$res->is_closed = 0; $res->is_closed = false;
$res->deactivated = NULL; $res->type = 'group';
$res->is_admin = $this->canBeModifiedBy($user); $res->is_member = $user ? (int) $this->getSubscriptionStatus($user) : 0;
$res->deactivated = null;
$res->can_access_closed = true;
if($this->canBeModifiedBy($user)) { if (!is_array($fields)) {
$res->admin_level = 3; $fields = explode(',', $fields);
} }
$res->is_member = $this->getSubscriptionStatus($user) ? 1 : 0; $avatar_photo = $this->getAvatarPhoto();
foreach ($fields as $field) {
switch ($field) {
case 'verified':
$res->verified = (int) $this->isVerified();
break;
case 'site':
$res->site = $this->getWebsite();
break;
case 'description':
$res->description = $this->getDescription();
break;
case 'background':
$res->background = $this->getBackDropPictureURLs();
break;
case 'photo_50':
$res->photo_50 = $this->getAvatarUrl('miniscule', $avatar_photo);
break;
case 'photo_100':
$res->photo_100 = $this->getAvatarUrl('tiny', $avatar_photo);
break;
case 'photo_200':
$res->photo_200 = $this->getAvatarUrl('normal', $avatar_photo);
break;
case 'photo_max':
$res->photo_max = $this->getAvatarUrl('original', $avatar_photo);
break;
case 'members_count':
$res->members_count = $this->getFollowersCount();
break;
case 'real_id':
$res->real_id = $this->getRealId();
break;
}
}
$res->type = "group"; return $res;
$res->photo_50 = $this->getAvatarUrl("miniscule");
$res->photo_100 = $this->getAvatarUrl("tiny");
$res->photo_200 = $this->getAvatarUrl("normal");
$res->can_create_topic = $this->canBeModifiedBy($user) ? 1 : ($this->isEveryoneCanCreateTopics() ? 1 : 0);
$res->can_post = $this->canBeModifiedBy($user) ? 1 : ($this->canPost() ? 1 : 0);
return (object) $res;
} }
use Traits\TBackDrops;
use Traits\TSubscribable;
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Clubs; use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Note}; use openvk\Web\Models\Entities\{Note};
@ -8,53 +12,64 @@ class Comment extends Post
{ {
protected $tableName = "comments"; protected $tableName = "comments";
protected $upperNodeReferenceColumnName = "owner"; protected $upperNodeReferenceColumnName = "owner";
function getPrettyId(): string public function getPrettyId(): string
{ {
return $this->getRecord()->id; return (string) $this->getRecord()->id;
} }
function getVirtualId(): int public function getVirtualId(): int
{ {
return 0; return 0;
} }
function getTarget(): ?Postable public function getTarget(): ?Postable
{ {
$entityClassName = $this->getRecord()->model; $entityClassName = $this->getRecord()->model;
$repoClassName = str_replace("Entities", "Repositories", $entityClassName) . "s"; $repoClassName = str_replace("Entities", "Repositories", $entityClassName) . "s";
$entity = (new $repoClassName)->get($this->getRecord()->target); $entity = (new $repoClassName())->get($this->getRecord()->target);
return $entity; return $entity;
} }
public function getPageURL(): string
{
return '#';
}
/** /**
* May return fake owner (group), if flags are [1, (*)] * May return fake owner (group), if flags are [1, (*)]
* *
* @param bool $honourFlags - check flags * @param bool $honourFlags - check flags
*/ */
function getOwner(bool $honourFlags = true, bool $real = false): RowModel public function getOwner(bool $honourFlags = true, bool $real = false): RowModel
{ {
if($honourFlags && $this->isPostedOnBehalfOfGroup()) { if ($honourFlags && $this->isPostedOnBehalfOfGroup()) {
if($this->getTarget() instanceof Post) if ($this->getTarget() instanceof Post) {
return (new Clubs)->get(abs($this->getTarget()->getTargetWall())); return (new Clubs())->get(abs($this->getTarget()->getTargetWall()));
}
if($this->getTarget() instanceof Topic) if ($this->getTarget() instanceof Topic) {
return $this->getTarget()->getClub(); return $this->getTarget()->getClub();
}
} }
return parent::getOwner($honourFlags, $real); return parent::getOwner($honourFlags, $real);
} }
function canBeDeletedBy(User $user): bool public function canBeDeletedBy(User $user = null): bool
{ {
if (!$user) {
return false;
}
return $this->getOwner()->getId() == $user->getId() || return $this->getOwner()->getId() == $user->getId() ||
$this->getTarget()->getOwner()->getId() == $user->getId() || $this->getTarget()->getOwner()->getId() == $user->getId() ||
$this->getTarget() instanceof Post && $this->getTarget()->getTargetWall() < 0 && (new Clubs)->get(abs($this->getTarget()->getTargetWall()))->canBeModifiedBy($user) || $this->getTarget() instanceof Post && $this->getTarget()->getTargetWall() < 0 && (new Clubs())->get(abs($this->getTarget()->getTargetWall()))->canBeModifiedBy($user) ||
$this->getTarget() instanceof Topic && $this->getTarget()->canBeModifiedBy($user); $this->getTarget() instanceof Topic && $this->getTarget()->canBeModifiedBy($user);
} }
function toVkApiStruct(?User $user = NULL, bool $need_likes = false, bool $extended = false, ?Note $note = NULL): object public function toVkApiStruct(?User $user = null, bool $need_likes = false, bool $extended = false, ?Note $note = null): object
{ {
$res = (object) []; $res = (object) [];
@ -64,25 +79,111 @@ class Comment extends Post
$res->text = $this->getText(false); $res->text = $this->getText(false);
$res->attachments = []; $res->attachments = [];
$res->parents_stack = []; $res->parents_stack = [];
if(!is_null($note)) { if (!is_null($note)) {
$res->uid = $this->getOwner()->getId(); $res->uid = $this->getOwner()->getId();
$res->nid = $note->getId(); $res->nid = $note->getId();
$res->oid = $note->getOwner()->getId(); $res->oid = $note->getOwner()->getId();
} }
foreach($this->getChildren() as $attachment) { foreach ($this->getChildren() as $attachment) {
if($attachment->isDeleted()) if ($attachment->isDeleted()) {
continue; continue;
}
$res->attachments[] = $attachment->toVkApiStruct();
if ($attachment instanceof \openvk\Web\Models\Entities\Photo) {
$res->attachments[] = $attachment->toVkApiStruct();
} elseif ($attachment instanceof \openvk\Web\Models\Entities\Video) {
$res->attachments[] = $attachment->toVkApiStruct($this->getUser());
}
} }
if($need_likes) { if ($need_likes) {
$res->count = $this->getLikesCount(); $res->count = $this->getLikesCount();
$res->user_likes = (int)$this->hasLikeFrom($user); $res->user_likes = (int) $this->hasLikeFrom($user);
$res->can_like = 1; $res->can_like = 1;
} }
return $res; return $res;
} }
public function getURL(): string
{
return "/wall" . $this->getTarget()->getPrettyId() . "#_comment" . $this->getId();
}
public function canBeViewedBy(?User $user = null): bool
{
if ($this->isDeleted() || $this->getTarget()->isDeleted()) {
return false;
}
return $this->getTarget()->canBeViewedBy($user);
}
public function isFromPostAuthor($target = null)
{
if (!$target) {
$target = $this->getTarget();
}
$target_owner = $target->getOwner();
$comment_owner = $this->getOwner();
if ($target_owner->getRealId() === $comment_owner->getRealId()) {
return true;
}
# TODO: make it work with signer_id
return false;
}
public function toNotifApiStruct()
{
$res = (object) [];
$res->id = $this->getId();
$res->owner_id = $this->getOwner()->getId();
$res->date = $this->getPublicationTime()->timestamp();
$res->text = $this->getText(false);
$res->post = null; # todo
return $res;
}
public function canBeEditedBy(?User $user = null): bool
{
if (!$user) {
return false;
}
return $user->getId() == $this->getOwner(false)->getId();
}
public function getTargetURL(): string
{
$target = $this->getTarget();
$target_name = 'wall';
if (!$target) {
return '/404';
}
switch (get_class($target)) {
case 'openvk\Web\Models\Entities\Note':
$target_name = 'note';
break;
case 'openvk\Web\Models\Entities\Photo':
$target_name = 'photo';
break;
case 'openvk\Web\Models\Entities\Video':
$target_name = 'video';
break;
case 'openvk\Web\Models\Entities\Topic':
$target_name = 'topic';
break;
}
return $target_name . $target->getPrettyId();
}
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use Chandler\Signaling\SignalManager; use Chandler\Signaling\SignalManager;
use Chandler\Security\Authenticator; use Chandler\Security\Authenticator;
@ -12,7 +16,7 @@ use Nette\Database\Table\ActiveRow;
/** /**
* A repository of messages sent between correspondents. * A repository of messages sent between correspondents.
* *
* A pseudo-repository that operates with messages * A pseudo-repository that operates with messages
* sent between users. * sent between users.
*/ */
@ -26,136 +30,141 @@ class Correspondence
* @var \Nette\Database\Table\Selection Messages table * @var \Nette\Database\Table\Selection Messages table
*/ */
private $messages; private $messages;
const CAP_BEHAVIOUR_END_MESSAGE_ID = 1; public const CAP_BEHAVIOUR_END_MESSAGE_ID = 1;
const CAP_BEHAVIOUR_START_MESSAGE_ID = 2; public const CAP_BEHAVIOUR_START_MESSAGE_ID = 2;
/** /**
* Correspondence constructor. * Correspondence constructor.
* *
* Requires two users/clubs to construct. * Requires two users/clubs to construct.
* *
* @param $correspondent - first correspondent * @param $correspondent - first correspondent
* @param $anotherCorrespondents - another correspondent * @param $anotherCorrespondents - another correspondent
*/ */
function __construct(RowModel $correspondent, RowModel $anotherCorrespondent) public function __construct(RowModel $correspondent, RowModel $anotherCorrespondent)
{ {
$this->correspondents = [$correspondent, $anotherCorrespondent]; $this->correspondents = [$correspondent, $anotherCorrespondent];
$this->messages = DatabaseConnection::i()->getContext()->table("messages"); $this->messages = DatabaseConnection::i()->getContext()->table("messages");
} }
/** /**
* Get /im?sel url. * Get /im?sel url.
* *
* @returns string - URL * @returns string - URL
*/ */
function getURL(): string public function getURL(): string
{ {
$id = $this->correspondents[1]->getId(); $id = $this->correspondents[1]->getId();
$id = get_class($this->correspondents[1]) === 'openvk\Web\Models\Entities\Club' ? $id * -1 : $id; $id = get_class($this->correspondents[1]) === 'openvk\Web\Models\Entities\Club' ? $id * -1 : $id;
return "/im?sel=$id"; return "/im?sel=$id";
} }
function getID(): int public function getID(): int
{ {
$id = $this->correspondents[1]->getId(); $id = $this->correspondents[1]->getId();
$id = get_class($this->correspondents[1]) === 'openvk\Web\Models\Entities\Club' ? $id * -1 : $id; $id = get_class($this->correspondents[1]) === 'openvk\Web\Models\Entities\Club' ? $id * -1 : $id;
return $id; return $id;
} }
/** /**
* Get correspondents as array. * Get correspondents as array.
* *
* @returns RowModel[] Array of correspondents (usually two) * @returns RowModel[] Array of correspondents (usually two)
*/ */
function getCorrespondents(): array public function getCorrespondents(): array
{ {
return $this->correspondents; return $this->correspondents;
} }
/** /**
* Fetch messages. * Fetch messages.
* *
* Fetch messages on per page basis. * Fetch messages on per page basis.
* *
* @param $cap - page (defaults to first) * @param $cap - page (defaults to first)
* @param $limit - messages per page (defaults to default per page count) * @param $limit - messages per page (defaults to default per page count)
* @returns \Traversable - iterable messages cursor * @returns \Traversable - iterable messages cursor
*/ */
function getMessages(int $capBehavior = 1, ?int $cap = NULL, ?int $limit = NULL, ?int $padding = NULL, bool $reverse = false): array public function getMessages(int $capBehavior = 1, ?int $cap = null, ?int $limit = null, ?int $padding = null, bool $reverse = false): array
{ {
$query = file_get_contents(__DIR__ . "/../sql/get-messages.tsql"); $query = file_get_contents(__DIR__ . "/../sql/get-messages.tsql");
$params = [ $params = [
[get_class($this->correspondents[0]), get_class($this->correspondents[1])], [get_class($this->correspondents[0]), get_class($this->correspondents[1])],
[$this->correspondents[0]->getId(), $this->correspondents[1]->getId()], [$this->correspondents[0]->getId(), $this->correspondents[1]->getId()],
[$limit ?? OPENVK_DEFAULT_PER_PAGE] [$limit ?? OPENVK_DEFAULT_PER_PAGE],
]; ];
$params = array_merge($params[0], $params[1], array_reverse($params[0]), array_reverse($params[1]), $params[2]); $params = array_merge($params[0], $params[1], array_reverse($params[0]), array_reverse($params[1]), $params[2]);
if ($limit === NULL) if ($limit === null) {
DatabaseConnection::i()->getConnection()->query("UPDATE messages SET unread = 0 WHERE sender_id = ".$this->correspondents[1]->getId()); DatabaseConnection::i()->getConnection()->query("UPDATE messages SET unread = 0 WHERE sender_id = " . $this->correspondents[1]->getId());
}
if(is_null($cap)) {
if (is_null($cap)) {
$query = str_replace("\n AND (`id` > ?)", "", $query); $query = str_replace("\n AND (`id` > ?)", "", $query);
} else { } else {
if($capBehavior === 1) if ($capBehavior === 1) {
$query = str_replace("\n AND (`id` > ?)", "\n AND (`id` < ?)", $query); $query = str_replace("\n AND (`id` > ?)", "\n AND (`id` < ?)", $query);
}
array_unshift($params, $cap); array_unshift($params, $cap);
} }
if(is_null($padding)) if (is_null($padding)) {
$query = str_replace("\nOFFSET\n?", "", $query); $query = str_replace("\nOFFSET\n?", "", $query);
else } else {
$params[] = $padding; $params[] = $padding;
}
if($reverse)
if ($reverse) {
$query = str_replace("`created` DESC", "`created` ASC", $query); $query = str_replace("`created` DESC", "`created` ASC", $query);
}
$msgs = DatabaseConnection::i()->getConnection()->query($query, ...$params); $msgs = DatabaseConnection::i()->getConnection()->query($query, ...$params);
$msgs = array_map(function($message) { $msgs = array_map(function ($message) {
$message = new ActiveRow((array) $message, $this->messages); #Directly creating ActiveRow is faster than making query $message = new ActiveRow((array) $message, $this->messages); #Directly creating ActiveRow is faster than making query
return new Message($message); return new Message($message);
}, iterator_to_array($msgs)); }, iterator_to_array($msgs));
return $msgs; return $msgs;
} }
/** /**
* Get last message from correspondence. * Get last message from correspondence.
* *
* @returns Message|null - message, if any * @returns Message|null - message, if any
*/ */
function getPreviewMessage(): ?Message public function getPreviewMessage(): ?Message
{ {
$messages = $this->getMessages(1, NULL, 1); $messages = $this->getMessages(1, null, 1, 0);
return $messages[0] ?? NULL; return $messages[0] ?? null;
} }
/** /**
* Send message. * Send message.
* *
* @deprecated * @deprecated
* @returns Message|false - resulting message, or false in case of non-successful transaction * @returns Message|false - resulting message, or false in case of non-successful transaction
*/ */
function sendMessage(Message $message, bool $dontReverse = false) public function sendMessage(Message $message, bool $dontReverse = false)
{ {
if(!$dontReverse) { if (!$dontReverse) {
$user = (new Users)->getByChandlerUser(Authenticator::i()->getUser()); $user = (new Users())->getByChandlerUser(Authenticator::i()->getUser());
if(!$user) if (!$user) {
return false; return false;
}
} }
$ids = [$this->correspondents[0]->getId(), $this->correspondents[1]->getId()]; $ids = [$this->correspondents[0]->getId(), $this->correspondents[1]->getId()];
$classes = [get_class($this->correspondents[0]), get_class($this->correspondents[1])]; $classes = [get_class($this->correspondents[0]), get_class($this->correspondents[1])];
if(!$dontReverse && $ids[1] === $user->getId()) { if (!$dontReverse && $ids[1] === $user->getId()) {
$ids = array_reverse($ids); $ids = array_reverse($ids);
$classes = array_reverse($classes); $classes = array_reverse($classes);
} }
$message->setSender_Id($ids[0]); $message->setSender_Id($ids[0]);
$message->setRecipient_Id($ids[1]); $message->setRecipient_Id($ids[1]);
$message->setSender_Type($classes[0]); $message->setSender_Type($classes[0]);
@ -163,15 +172,15 @@ class Correspondence
$message->setCreated(time()); $message->setCreated(time());
$message->setUnread(1); $message->setUnread(1);
$message->save(); $message->save();
DatabaseConnection::i()->getConnection()->query("UPDATE messages SET unread = 0 WHERE sender_id = ".$this->correspondents[1]->getId()); DatabaseConnection::i()->getConnection()->query("UPDATE messages SET unread = 0 WHERE sender_id = " . $this->correspondents[1]->getId());
# да # да
if($ids[0] !== $ids[1]) { if ($ids[0] !== $ids[1]) {
$event = new NewMessageEvent($message); $event = new NewMessageEvent($message);
(SignalManager::i())->triggerEvent($event, $ids[1]); (SignalManager::i())->triggerEvent($event, $ids[1]);
} }
return $message; return $message;
} }
} }

View file

@ -0,0 +1,441 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\{Clubs, Users, Photos};
use openvk\Web\Models\Entities\{Photo};
use openvk\Web\Models\RowModel;
use Nette\InvalidStateException as ISE;
use Chandler\Database\DatabaseConnection;
class Document extends Media
{
protected $tableName = "documents";
protected $fileExtension = "gif";
private $tmp_format = null;
public const VKAPI_TYPE_TEXT = 1;
public const VKAPI_TYPE_ARCHIVE = 2;
public const VKAPI_TYPE_GIF = 3;
public const VKAPI_TYPE_IMAGE = 4;
public const VKAPI_TYPE_AUDIO = 5;
public const VKAPI_TYPE_VIDEO = 6;
public const VKAPI_TYPE_BOOKS = 7;
public const VKAPI_TYPE_UNKNOWN = 8;
public const VKAPI_FOLDER_PRIVATE = 0;
public const VKAPI_FOLDER_STUDY = 1;
public const VKAPI_FOLDER_BOOK = 2;
public const VKAPI_FOLDER_PUBLIC = 3;
protected function pathFromHash(string $hash): string
{
$dir = $this->getBaseDir() . substr($hash, 0, 2);
if (!is_dir($dir)) {
mkdir($dir);
}
return "$dir/$hash." . $this->getFileExtension();
}
public function getURL(): string
{
$hash = $this->getRecord()->hash;
$filetype = $this->getFileExtension();
switch (OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) {
default:
case "default":
case "basic":
return "http://" . $_SERVER['HTTP_HOST'] . "/blob_" . substr($hash, 0, 2) . "/$hash.$filetype";
break;
case "accelerated":
return "http://" . $_SERVER['HTTP_HOST'] . "/openvk-datastore/$hash.$filetype";
break;
case "server":
$settings = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["server"];
return (
$settings->protocol ?? ovk_scheme() .
"://" . $settings->host .
$settings->path .
substr($hash, 0, 2) . "/$hash.$filetype"
);
break;
}
}
protected function saveFile(string $filename, string $hash): bool
{
move_uploaded_file($filename, $this->pathFromHash($hash));
return true;
}
protected function makePreview(string $tmp_name, string $filename, int $owner): bool
{
$preview_photo = new Photo();
$preview_photo->setOwner($owner);
$preview_photo->setDescription("internal use");
$preview_photo->setCreated(time());
$preview_photo->setSystem(1);
$preview_photo->setFile([
"tmp_name" => $tmp_name,
"error" => 0,
]);
$preview_photo->save();
$this->stateChanges("preview", "photo_" . $preview_photo->getId());
return true;
}
private function updateHash(string $hash): bool
{
$this->stateChanges("hash", $hash);
return true;
}
public function setFile(array $file): void
{
if ($file["error"] !== UPLOAD_ERR_OK) {
throw new ISE("File uploaded is corrupted");
}
$original_name = $file["name"];
$file_format = end(explode(".", $original_name));
$file_size = $file["size"];
$type = Document::detectTypeByFormat($file_format);
if (!$file_format) {
throw new \TypeError("No file format");
}
if (!in_array(mb_strtolower($file_format), OPENVK_ROOT_CONF["openvk"]["preferences"]["docs"]["allowedFormats"])) {
throw new \TypeError("Forbidden file format");
}
if ($file_size < 1 || $file_size > (OPENVK_ROOT_CONF["openvk"]["preferences"]["docs"]["maxSize"] * 1024 * 1024)) {
throw new \ValueError("Invalid filesize");
}
$hash = hash_file("whirlpool", $file["tmp_name"]);
$this->stateChanges("original_name", ovk_proc_strtr($original_name, 255));
$this->tmp_format = mb_strtolower($file_format);
$this->stateChanges("format", mb_strtolower($file_format));
$this->stateChanges("filesize", $file_size);
$this->stateChanges("hash", $hash);
$this->stateChanges("access_key", bin2hex(random_bytes(9)));
$this->stateChanges("type", $type);
if (in_array($type, [3, 4])) {
$this->makePreview($file["tmp_name"], $original_name, $file["preview_owner"]);
}
$this->saveFile($file["tmp_name"], $hash);
}
public function hasPreview(): bool
{
return $this->getRecord()->preview != null;
}
public function isOwnerHidden(): bool
{
return (bool) $this->getRecord()->owner_hidden;
}
public function isCopy(): bool
{
return $this->getRecord()->copy_of != null;
}
public function isLicensed(): bool
{
return false;
}
public function isUnsafe(): bool
{
return false;
}
public function isAnonymous(): bool
{
return false;
}
public function isPrivate(): bool
{
return $this->getFolder() == Document::VKAPI_FOLDER_PRIVATE;
}
public function isImage(): bool
{
return in_array($this->getVKAPIType(), [3, 4]);
}
public function isBook(): bool
{
return in_array($this->getFileExtension(), ["pdf"]);
}
public function isAudio(): bool
{
return in_array($this->getVKAPIType(), [Document::VKAPI_TYPE_AUDIO]);
}
public function isGif(): bool
{
return $this->getVKAPIType() == 3;
}
public function isCopiedBy($user = null): bool
{
if (!$user) {
return false;
}
if ($user->getRealId() === $this->getOwnerID()) {
return true;
}
return DatabaseConnection::i()->getContext()->table("documents")->where([
"owner" => $user->getRealId(),
"copy_of" => $this->getId(),
"deleted" => 0,
])->count() > 0;
}
public function copy(User $user): Document
{
$item = DatabaseConnection::i()->getContext()->table("documents")->where([
"owner" => $user->getId(),
"copy_of" => $this->getId(),
]);
if ($item->count() > 0) {
$older = new Document($item->fetch());
}
$this_document_array = $this->getRecord()->toArray();
$new_document = new Document();
$new_document->setOwner($user->getId());
$new_document->updateHash($this_document_array["hash"]);
$new_document->setOwner_hidden(1);
$new_document->setCopy_of($this->getId());
$new_document->setName($this->getId());
$new_document->setOriginal_name($this->getOriginalName());
$new_document->setAccess_key(bin2hex(random_bytes(9)));
$new_document->setFormat($this_document_array["format"]);
$new_document->setType($this->getVKAPIType());
$new_document->setFolder_id(0);
$new_document->setPreview($this_document_array["preview"]);
$new_document->setTags($this_document_array["tags"]);
$new_document->setFilesize($this_document_array["filesize"]);
$new_document->save();
return $new_document;
}
public function setTags(?string $tags): bool
{
if (is_null($tags)) {
$this->stateChanges("tags", null);
return true;
}
$parsed = explode(",", $tags);
if (sizeof($parsed) < 1 || $parsed[0] == "") {
$this->stateChanges("tags", null);
return true;
}
$result = "";
foreach ($parsed as $tag) {
$result .= trim($tag) . ($tag != end($parsed) ? "," : '');
}
$this->stateChanges("tags", ovk_proc_strtr($result, 500));
return true;
}
public function getOwner(bool $real = false): RowModel
{
$oid = (int) $this->getRecord()->owner;
if ($oid > 0) {
return (new Users())->get($oid);
} else {
return (new Clubs())->get($oid * -1);
}
}
public function getFileExtension(): string
{
if ($this->tmp_format) {
return $this->tmp_format;
}
return $this->getRecord()->format;
}
public function getPrettyId(): string
{
return $this->getVirtualId() . "_" . $this->getId();
}
public function getPrettiestId(): string
{
return $this->getVirtualId() . "_" . $this->getId() . "_" . $this->getAccessKey();
}
public function getOriginal(): Document
{
return $this->getRecord()->copy_of;
}
public function getName(): string
{
return $this->getRecord()->name;
}
public function getOriginalName(): string
{
return $this->getRecord()->original_name;
}
public function getVKAPIType(): int
{
return $this->getRecord()->type;
}
public function getFolder(): int
{
return $this->getRecord()->folder_id;
}
public function getTags(): array
{
$tags = $this->getRecord()->tags;
if (!$tags) {
return [];
}
return explode(",", $tags ?? "");
}
public function getFilesize(): int
{
return $this->getRecord()->filesize;
}
public function getPreview(): ?RowModel
{
$preview_array = $this->getRecord()->preview;
$preview = explode(",", $this->getRecord()->preview)[0];
$model = null;
$exploded = explode("_", $preview);
switch ($exploded[0]) {
case "photo":
$model = (new Photos())->get((int) $exploded[1]);
break;
}
return $model;
}
public function getOwnerID(): int
{
return $this->getRecord()->owner;
}
public function toApiPreview(): object
{
$preview = $this->getPreview();
if ($preview instanceof Photo) {
return (object) [
"photo" => [
"sizes" => array_values($preview->getVkApiSizes()),
],
];
}
}
public function canBeModifiedBy(User $user = null): bool
{
if (!$user) {
return false;
}
if ($this->getOwnerID() < 0) {
return (new Clubs())->get(abs($this->getOwnerID()))->canBeModifiedBy($user);
}
return $this->getOwnerID() === $user->getId();
}
public function toVkApiStruct(?User $user = null, bool $return_tags = false): object
{
$res = new \stdClass();
$res->id = $this->getId();
$res->owner_id = $this->getVirtualId();
$res->title = $this->getName();
$res->size = $this->getFilesize();
$res->ext = $this->getFileExtension();
$res->url = $this->getURL();
$res->date = $this->getPublicationTime()->timestamp();
$res->type = $this->getVKAPIType();
$res->is_hidden = (int) $this->isOwnerHidden();
$res->is_licensed = (int) $this->isLicensed();
$res->is_unsafe = (int) $this->isUnsafe();
$res->folder_id = (int) $this->getFolder();
$res->access_key = $this->getAccessKey();
$res->private_url = "";
if ($user) {
$res->can_manage = $this->canBeModifiedBy($user);
}
if ($this->hasPreview()) {
$res->preview = $this->toApiPreview();
}
if ($return_tags) {
$res->tags = $this->getTags();
}
return $res;
}
public function delete(bool $softly = true, bool $all_copies = false): void
{
if ($all_copies) {
$ctx = DatabaseConnection::i()->getContext();
$ctx->table("documents")->where("copy_of", $this->getId())->delete();
}
parent::delete($softly);
}
public static function detectTypeByFormat(string $format)
{
switch (mb_strtolower($format)) {
case "txt": case "docx": case "doc": case "odt": case "pptx": case "ppt": case "xlsx": case "xls": case "md":
return 1;
case "zip": case "rar": case "7z":
return 2;
case "gif": case "apng":
return 3;
case "jpg": case "jpeg": case "png": case "psd": case "ps": case "webp":
return 4;
case "mp3":
return 5;
case "mp4": case "avi":
return 6;
case "pdf": case "djvu": case "epub": case "fb2":
return 7;
default:
return 8;
}
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
@ -8,7 +12,7 @@ class EmailChangeVerification extends PasswordReset
{ {
protected $tableName = "email_change_verifications"; protected $tableName = "email_change_verifications";
function getNewEmail(): string public function getNewEmail(): string
{ {
return $this->getRecord()->new_email; return $this->getRecord()->new_email;
} }

View file

@ -1,10 +1,14 @@
<?php declare(strict_types=1); <?php
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Users; declare(strict_types=1);
use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime; namespace openvk\Web\Models\Entities;
class EmailVerification extends PasswordReset use openvk\Web\Models\Repositories\Users;
{ use openvk\Web\Models\RowModel;
protected $tableName = "email_verifications"; use openvk\Web\Util\DateTime;
}
class EmailVerification extends PasswordReset
{
protected $tableName = "email_verifications";
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
@ -7,53 +11,54 @@ use Nette\Utils\{Image, ImageException};
class Gift extends RowModel class Gift extends RowModel
{ {
const IMAGE_MAXSIZE = 131072; public const IMAGE_MAXSIZE = 131072;
const IMAGE_BINARY = 0; public const IMAGE_BINARY = 0;
const IMAGE_BASE64 = 1; public const IMAGE_BASE64 = 1;
const IMAGE_URL = 2; public const IMAGE_URL = 2;
const PERIOD_IGNORE = 0; public const PERIOD_IGNORE = 0;
const PERIOD_SET = 1; public const PERIOD_SET = 1;
const PERIOD_SET_IF_NONE = 2; public const PERIOD_SET_IF_NONE = 2;
protected $tableName = "gifts"; protected $tableName = "gifts";
function getName(): string public function getName(): string
{ {
return $this->getRecord()->internal_name; return $this->getRecord()->internal_name;
} }
function getPrice(): int public function getPrice(): int
{ {
return $this->getRecord()->price; return $this->getRecord()->price;
} }
function getUsages(): int public function getUsages(): int
{ {
return $this->getRecord()->usages; return $this->getRecord()->usages;
} }
function getUsagesBy(User $user, ?int $since = NULL): int public function getUsagesBy(User $user, ?int $since = null): int
{ {
$sent = $this->getRecord() $sent = $this->getRecord()
->related("gift_user_relations.gift") ->related("gift_user_relations.gift")
->where("sender", $user->getId()) ->where("sender", $user->getId())
->where("sent >= ?", $since ?? $this->getRecord()->limit_period ?? 0); ->where("sent >= ?", $since ?? $this->getRecord()->limit_period ?? 0);
return sizeof($sent); return sizeof($sent);
} }
function getUsagesLeft(User $user): float public function getUsagesLeft(User $user): float
{ {
if($this->getLimit() === INF) if ($this->getLimit() === INF) {
return INF; return INF;
}
return max(0, $this->getLimit() - $this->getUsagesBy($user)); return max(0, $this->getLimit() - $this->getUsagesBy($user));
} }
function getImage(int $type = 0): /* ?binary */ string public function getImage(int $type = 0): /* ?binary */ string
{ {
switch($type) { switch ($type) {
default: default:
case static::IMAGE_BINARY: case static::IMAGE_BINARY:
return $this->getRecord()->image ?? ""; return $this->getRecord()->image ?? "";
@ -66,99 +71,100 @@ class Gift extends RowModel
break; break;
} }
} }
function getLimit(): float public function getLimit(): float
{ {
$limit = $this->getRecord()->limit; $limit = $this->getRecord()->limit;
return !$limit ? INF : (float) $limit; return !$limit ? INF : (float) $limit;
} }
function getLimitResetTime(): ?DateTime public function getLimitResetTime(): ?DateTime
{ {
return is_null($t = $this->getRecord()->limit_period) ? NULL : new DateTime($t); return is_null($t = $this->getRecord()->limit_period) ? null : new DateTime($t);
} }
function getUpdateDate(): DateTime public function getUpdateDate(): DateTime
{ {
return new DateTime($this->getRecord()->updated); return new DateTime($this->getRecord()->updated);
} }
function canUse(User $user): bool public function canUse(User $user): bool
{ {
return $this->getUsagesLeft($user) > 0; return $this->getUsagesLeft($user) > 0;
} }
function isFree(): bool public function isFree(): bool
{ {
return $this->getPrice() === 0; return $this->getPrice() === 0;
} }
function used(): void public function used(): void
{ {
$this->stateChanges("usages", $this->getUsages() + 1); $this->stateChanges("usages", $this->getUsages() + 1);
$this->save(); $this->save();
} }
function setName(string $name): void public function setName(string $name): void
{ {
$this->stateChanges("internal_name", $name); $this->stateChanges("internal_name", $name);
} }
function setImage(string $file): bool public function setImage(string $file): bool
{ {
$imgBlob; $imgBlob;
try { try {
$image = Image::fromFile($file); $image = Image::fromFile($file);
$image->resize(512, 512, Image::SHRINK_ONLY); $image->resize(512, 512, Image::SHRINK_ONLY);
$imgBlob = $image->toString(Image::PNG); $imgBlob = $image->toString(Image::PNG);
} catch(ImageException $ex) { } catch (ImageException $ex) {
return false; return false;
} }
if(strlen($imgBlob) > (2**24 - 1)) { if (strlen($imgBlob) > (2 ** 24 - 1)) {
return false; return false;
} else { } else {
$this->stateChanges("updated", time()); $this->stateChanges("updated", time());
$this->stateChanges("image", $imgBlob); $this->stateChanges("image", $imgBlob);
} }
return true; return true;
} }
function setLimit(?float $limit = NULL, int $periodBehaviour = 0): void public function setLimit(?float $limit = null, int $periodBehaviour = 0): void
{ {
$limit ??= $this->getLimit(); $limit ??= $this->getLimit();
$limit = $limit === INF ? NULL : (int) $limit; $limit = $limit === INF ? null : (int) $limit;
$this->stateChanges("limit", $limit); $this->stateChanges("limit", $limit);
if(!$limit) { if (!$limit) {
$this->stateChanges("limit_period", NULL); $this->stateChanges("limit_period", null);
return; return;
} }
switch($periodBehaviour) { switch ($periodBehaviour) {
default: default:
case static::PERIOD_IGNORE: case static::PERIOD_IGNORE:
break; break;
case static::PERIOD_SET: case static::PERIOD_SET:
$this->stateChanges("limit_period", time()); $this->stateChanges("limit_period", time());
break; break;
case static::PERIOD_SET_IF_NONE: case static::PERIOD_SET_IF_NONE:
if(is_null($this->getRecord()) || is_null($this->getRecord()->limit_period)) if (is_null($this->getRecord()) || is_null($this->getRecord()->limit_period)) {
$this->stateChanges("limit_period", time()); $this->stateChanges("limit_period", time());
}
break; break;
} }
} }
function delete(bool $softly = true): void public function delete(bool $softly = true): void
{ {
$this->getRecord()->related("gift_relations.gift")->delete(); $this->getRecord()->related("gift_relations.gift")->delete();
parent::delete($softly); parent::delete($softly);
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection as DB; use Chandler\Database\DatabaseConnection as DB;
use openvk\Web\Models\Repositories\Gifts; use openvk\Web\Models\Repositories\Gifts;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
@ -8,19 +12,20 @@ use Transliterator;
class GiftCategory extends RowModel class GiftCategory extends RowModel
{ {
protected $tableName = "gift_categories"; protected $tableName = "gift_categories";
private function getLocalization(string $language): object private function getLocalization(string $language): object
{ {
return $this->getRecord() return $this->getRecord()
->related("gift_categories_locales.category") ->related("gift_categories_locales.category")
->where("language", $language); ->where("language", $language);
} }
private function createLocalizationIfNotExists(string $language): void private function createLocalizationIfNotExists(string $language): void
{ {
if(!is_null($this->getLocalization($language)->fetch())) if (!is_null($this->getLocalization($language)->fetch())) {
return; return;
}
DB::i()->getContext()->table("gift_categories_locales")->insert([ DB::i()->getContext()->table("gift_categories_locales")->insert([
"category" => $this->getId(), "category" => $this->getId(),
"language" => $language, "language" => $language,
@ -28,8 +33,8 @@ class GiftCategory extends RowModel
"description" => "Sample Text", "description" => "Sample Text",
]); ]);
} }
function getSlug(): string public function getSlug(): string
{ {
return str_replace("ʹ", "-", Transliterator::createFromRules( return str_replace("ʹ", "-", Transliterator::createFromRules(
":: Any-Latin;" ":: Any-Latin;"
@ -41,116 +46,123 @@ class GiftCategory extends RowModel
. "[:Separator:] > '-'" . "[:Separator:] > '-'"
)->transliterate($this->getName())); )->transliterate($this->getName()));
} }
function getThumbnailURL(): string public function getThumbnailURL(): string
{ {
$primeGift = iterator_to_array($this->getGifts(1, 1))[0]; $primeGift = iterator_to_array($this->getGifts(1, 1))[0];
$serverUrl = ovk_scheme(true) . $_SERVER["SERVER_NAME"]; $serverUrl = ovk_scheme(true) . $_SERVER["SERVER_NAME"];
if(!$primeGift) if (!$primeGift) {
return "$serverUrl/assets/packages/static/openvk/img/camera_200.png"; return "$serverUrl/assets/packages/static/openvk/img/camera_200.png";
}
return $primeGift->getImage(Gift::IMAGE_URL); return $primeGift->getImage(Gift::IMAGE_URL);
} }
function getName(string $language = "_", bool $returnNull = false): ?string public function getName(string $language = "_", bool $returnNull = false): ?string
{ {
$loc = $this->getLocalization($language)->fetch(); $loc = $this->getLocalization($language)->fetch();
if(!$loc) { if (!$loc) {
if($returnNull) if ($returnNull) {
return NULL; return null;
}
return $language === "_" ? "Unlocalized" : $this->getName(); return $language === "_" ? "Unlocalized" : $this->getName();
} }
return $loc->name; return $loc->name;
} }
function getDescription(string $language = "_", bool $returnNull = false): ?string public function getDescription(string $language = "_", bool $returnNull = false): ?string
{ {
$loc = $this->getLocalization($language)->fetch(); $loc = $this->getLocalization($language)->fetch();
if(!$loc) { if (!$loc) {
if($returnNull) if ($returnNull) {
return NULL; return null;
}
return $language === "_" ? "Unlocalized" : $this->getDescription(); return $language === "_" ? "Unlocalized" : $this->getDescription();
} }
return $loc->description; return $loc->description;
} }
function getGifts(int $page = -1, ?int $perPage = NULL, &$count = nullptr): \Traversable public function getGifts(int $page = -1, ?int $perPage = null, &$count = nullptr): \Traversable
{ {
$gifts = $this->getRecord()->related("gift_relations.category"); $gifts = $this->getRecord()->related("gift_relations.category");
if($page !== -1) { if ($page !== -1) {
$count = $gifts->count(); $count = $gifts->count();
$gifts = $gifts->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE); $gifts = $gifts->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE);
} }
foreach($gifts as $rel) foreach ($gifts as $rel) {
yield (new Gifts)->get($rel->gift); yield (new Gifts())->get($rel->gift);
}
} }
function isMagical(): bool public function isMagical(): bool
{ {
return !is_null($this->getRecord()->autoquery); return !is_null($this->getRecord()->autoquery);
} }
function hasGift(Gift $gift): bool public function hasGift(Gift $gift): bool
{ {
$rels = $this->getRecord()->related("gift_relations.category"); $rels = $this->getRecord()->related("gift_relations.category");
return $rels->where("gift", $gift->getId())->count() > 0; return $rels->where("gift", $gift->getId())->count() > 0;
} }
function addGift(Gift $gift): void public function addGift(Gift $gift): void
{ {
if($this->hasGift($gift)) if ($this->hasGift($gift)) {
return; return;
}
DB::i()->getContext()->table("gift_relations")->insert([ DB::i()->getContext()->table("gift_relations")->insert([
"category" => $this->getId(), "category" => $this->getId(),
"gift" => $gift->getId(), "gift" => $gift->getId(),
]); ]);
} }
function removeGift(Gift $gift): void public function removeGift(Gift $gift): void
{ {
if(!$this->hasGift($gift)) if (!$this->hasGift($gift)) {
return; return;
}
DB::i()->getContext()->table("gift_relations")->where([ DB::i()->getContext()->table("gift_relations")->where([
"category" => $this->getId(), "category" => $this->getId(),
"gift" => $gift->getId(), "gift" => $gift->getId(),
])->delete(); ])->delete();
} }
function setName(string $language, string $name): void public function setName(string $language, string $name): void
{ {
$this->createLocalizationIfNotExists($language); $this->createLocalizationIfNotExists($language);
$this->getLocalization($language)->update([ $this->getLocalization($language)->update([
"name" => $name, "name" => $name,
]); ]);
} }
function setDescription(string $language, string $description): void public function setDescription(string $language, string $description): void
{ {
$this->createLocalizationIfNotExists($language); $this->createLocalizationIfNotExists($language);
$this->getLocalization($language)->update([ $this->getLocalization($language)->update([
"description" => $description, "description" => $description,
]); ]);
} }
function setAutoQuery(?array $query = NULL): void public function setAutoQuery(?array $query = null): void
{ {
if(is_null($query)) { if (is_null($query)) {
$this->stateChanges("autoquery", NULL); $this->stateChanges("autoquery", null);
return; return;
} }
$allowedColumns = ["price", "usages"]; $allowedColumns = ["price", "usages"];
if(array_diff_key($query, array_flip($allowedColumns))) if (array_diff_key($query, array_flip($allowedColumns))) {
throw new \LogicException("Invalid query"); throw new \LogicException("Invalid query");
}
$this->stateChanges("autoquery", serialize($query)); $this->stateChanges("autoquery", serialize($query));
} }
} }

View file

@ -1,45 +1,49 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
class IP extends RowModel class IP extends RowModel
{ {
protected $tableName = "ip"; protected $tableName = "ip";
const RL_RESET = 0; public const RL_RESET = 0;
const RL_CANEXEC = 1; public const RL_CANEXEC = 1;
const RL_VIOLATION = 2; public const RL_VIOLATION = 2;
const RL_BANNED = 3; public const RL_BANNED = 3;
function getIp(): string public function getIp(): string
{ {
return inet_ntop($this->getRecord()->ip); return inet_ntop($this->getRecord()->ip);
} }
function getDiscoveryDate(): DateTime public function getDiscoveryDate(): DateTime
{ {
return new DateTime($this->getRecord()->first_seen); return new DateTime($this->getRecord()->first_seen);
} }
function isBanned(): bool public function isBanned(): bool
{ {
return (bool) $this->getRecord()->banned; return (bool) $this->getRecord()->banned;
} }
function ban(): void public function ban(): void
{ {
$this->stateChanges("banned", true); $this->stateChanges("banned", true);
$this->save(); $this->save();
} }
function pardon(): void public function pardon(): void
{ {
$this->stateChanges("banned", false); $this->stateChanges("banned", false);
$this->save(); $this->save();
} }
function clear(): void public function clear(): void
{ {
$this->stateChanges("rate_limit_counter_start", 0); $this->stateChanges("rate_limit_counter_start", 0);
$this->stateChanges("rate_limit_counter", 0); $this->stateChanges("rate_limit_counter", 0);
@ -47,69 +51,71 @@ class IP extends RowModel
$this->stateChanges("rate_limit_violation_counter", 0); $this->stateChanges("rate_limit_violation_counter", 0);
$this->save(); $this->save();
} }
function rateLimit(int $actionComplexity = 1): int public function rateLimit(int $actionComplexity = 1): int
{ {
$counterSessionStart = $this->getRecord()->rate_limit_counter_start; $counterSessionStart = $this->getRecord()->rate_limit_counter_start;
$vCounterSessionStart = $this->getRecord()->rate_limit_violation_counter_start; $vCounterSessionStart = $this->getRecord()->rate_limit_violation_counter_start;
$aCounter = $this->getRecord()->rate_limit_counter; $aCounter = $this->getRecord()->rate_limit_counter;
$vCounter = $this->getRecord()->rate_limit_violation_counter; $vCounter = $this->getRecord()->rate_limit_violation_counter;
$config = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]; $config = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"];
try { try {
if((time() - $config->time) > $counterSessionStart) { if ((time() - $config->time) > $counterSessionStart) {
$counterSessionStart = time(); $counterSessionStart = time();
$aCounter = $actionComplexity; $aCounter = $actionComplexity;
return static::RL_RESET; return static::RL_RESET;
} }
if(($aCounter + $actionComplexity) <= $config->actions) { if (($aCounter + $actionComplexity) <= $config->actions) {
$aCounter += $actionComplexity; $aCounter += $actionComplexity;
return static::RL_CANEXEC; return static::RL_CANEXEC;
} }
if((time() - $config->maxViolationsAge) > $vCounterSessionStart) { if ((time() - $config->maxViolationsAge) > $vCounterSessionStart) {
$vCounterSessionStart = time(); $vCounterSessionStart = time();
$vCounter = 1; $vCounter = 1;
return static::RL_VIOLATION; return static::RL_VIOLATION;
} }
$vCounter += 1; $vCounter += 1;
if($vCounter >= $config->maxViolations) { if ($vCounter >= $config->maxViolations) {
$this->stateChanges("banned", true); $this->stateChanges("banned", true);
return static::RL_BANNED; return static::RL_BANNED;
} }
return static::RL_VIOLATION; return static::RL_VIOLATION;
} finally { } finally {
$this->stateChanges("rate_limit_counter_start", $counterSessionStart); $this->stateChanges("rate_limit_counter_start", $counterSessionStart);
$this->stateChanges("rate_limit_counter", $aCounter); $this->stateChanges("rate_limit_counter", $aCounter);
$this->stateChanges("rate_limit_violation_counter_start", $vCounterSessionStart); $this->stateChanges("rate_limit_violation_counter_start", $vCounterSessionStart);
$this->stateChanges("rate_limit_violation_counter", $vCounter); $this->stateChanges("rate_limit_violation_counter", $vCounter);
$this->save(); $this->save(false);
} }
} }
function setIp(string $ip): void public function setIp(string $ip): void
{ {
$ip = inet_pton($ip); $ip = inet_pton($ip);
if(!$ip) if (!$ip) {
throw new \UnexpectedValueException("Malformed IP address"); throw new \UnexpectedValueException("Malformed IP address");
}
$this->stateChanges("ip", $ip); $this->stateChanges("ip", $ip);
} }
function save(): void public function save(?bool $log = false): void
{ {
if(is_null($this->getRecord())) if (is_null($this->getRecord())) {
$this->stateChanges("first_seen", time()); $this->stateChanges("first_seen", time());
}
parent::save();
parent::save($log);
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Photo, Message, Correspondence}; use openvk\Web\Models\Entities\{Photo, Message, Correspondence};
@ -10,47 +14,46 @@ use Chandler\Security\User as ChandlerUser;
class Manager extends RowModel class Manager extends RowModel
{ {
use Traits\TSubscribable;
protected $tableName = "group_coadmins"; protected $tableName = "group_coadmins";
function getId(): int public function getId(): int
{ {
return $this->getRecord()->id; return $this->getRecord()->id;
} }
function getUserId(): int public function getUserId(): int
{ {
return $this->getRecord()->user; return $this->getRecord()->user;
} }
function getUser(): ?User public function getUser(): ?User
{ {
return (new Users)->get($this->getRecord()->user); return (new Users())->get($this->getRecord()->user);
} }
function getClubId(): int public function getClubId(): int
{ {
return $this->getRecord()->club; return $this->getRecord()->club;
} }
function getClub(): ?Club public function getClub(): ?Club
{ {
return (new Clubs)->get($this->getRecord()->club); return (new Clubs())->get($this->getRecord()->club);
} }
function getComment(): string public function getComment(): string
{ {
return is_null($this->getRecord()->comment) ? "" : $this->getRecord()->comment; return is_null($this->getRecord()->comment) ? "" : $this->getRecord()->comment;
} }
function isHidden(): bool public function isHidden(): bool
{ {
return (bool) $this->getRecord()->hidden; return (bool) $this->getRecord()->hidden;
} }
function isClubPinned(): bool public function isClubPinned(): bool
{ {
return (bool) $this->getRecord()->club_pinned; return (bool) $this->getRecord()->club_pinned;
} }
use Traits\TSubscribable;
} }

View file

@ -1,68 +1,77 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Nette\InvalidStateException as ISE; use Nette\InvalidStateException as ISE;
abstract class Media extends Postable abstract class Media extends Postable
{ {
protected $fileExtension = "oct"; #octet stream xddd protected $fileExtension = "oct"; #octet stream xddd
protected $upperNodeReferenceColumnName = "owner"; protected $upperNodeReferenceColumnName = "owner";
protected $processingPlaceholder = NULL; protected $processingPlaceholder = null;
protected $processingTime = 30; protected $processingTime = 30;
function __destruct() public function __destruct()
{ {
#Remove data, if model wasn't presisted #Remove data, if model wasn't presisted
if(isset($this->changes["hash"])) if (isset($this->changes["hash"])) {
unlink($this->pathFromHash($this->changes["hash"])); unlink($this->pathFromHash($this->changes["hash"]));
}
} }
protected function getBaseDir(): string protected function getBaseDir(): string
{ {
$uploadSettings = OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]; $uploadSettings = OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"];
if($uploadSettings["mode"] === "server" && $uploadSettings["server"]["kind"] === "cdn") if ($uploadSettings["mode"] === "server" && $uploadSettings["server"]["kind"] === "cdn") {
return $uploadSettings["server"]["directory"]; return $uploadSettings["server"]["directory"];
else } else {
return OPENVK_ROOT . "/storage/"; return OPENVK_ROOT . "/storage/";
}
} }
protected function checkIfFileIsProcessed(): bool protected function checkIfFileIsProcessed(): bool
{ {
throw new \LogicException("checkIfFileIsProcessed is not implemented"); throw new \LogicException("checkIfFileIsProcessed is not implemented");
} }
abstract protected function saveFile(string $filename, string $hash): bool; abstract protected function saveFile(string $filename, string $hash): bool;
protected function pathFromHash(string $hash): string protected function pathFromHash(string $hash): string
{ {
$dir = $this->getBaseDir() . substr($hash, 0, 2); $dir = $this->getBaseDir() . substr($hash, 0, 2);
if(!is_dir($dir)) if (!is_dir($dir)) {
mkdir($dir); mkdir($dir);
}
return "$dir/$hash." . $this->fileExtension; return "$dir/$hash." . $this->fileExtension;
} }
function getFileName(): string public function getFileName(): string
{ {
return $this->pathFromHash($this->getRecord()->hash); return $this->pathFromHash($this->getRecord()->hash);
} }
function getURL(): string public function getURL(): string
{ {
if(!is_null($this->processingPlaceholder)) if (!is_null($this->processingPlaceholder)) {
if(!$this->isProcessed()) if (!$this->isProcessed()) {
return "/assets/packages/static/openvk/$this->processingPlaceholder.$this->fileExtension"; return "/assets/packages/static/openvk/$this->processingPlaceholder.$this->fileExtension";
}
}
$hash = $this->getRecord()->hash; $hash = $this->getRecord()->hash;
switch(OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) { switch (OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["mode"]) {
default: default:
case "default": case "default":
case "basic": case "basic":
return "http://" . $_SERVER['HTTP_HOST'] . "/blob_" . substr($hash, 0, 2) . "/$hash.$this->fileExtension"; return "http://" . $_SERVER['HTTP_HOST'] . "/blob_" . substr($hash, 0, 2) . "/$hash.$this->fileExtension";
break; break;
case "accelerated": case "accelerated":
return "http://" . $_SERVER['HTTP_HOST'] . "/openvk-datastore/$hash.$this->fileExtension"; return "http://" . $_SERVER['HTTP_HOST'] . "/openvk-datastore/$hash.$this->fileExtension";
break; break;
case "server": case "server":
$settings = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["server"]; $settings = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["uploads"]["server"];
return ( return (
@ -71,26 +80,29 @@ abstract class Media extends Postable
$settings->path . $settings->path .
substr($hash, 0, 2) . "/$hash.$this->fileExtension" substr($hash, 0, 2) . "/$hash.$this->fileExtension"
); );
break; break;
} }
} }
function getDescription(): ?string public function getDescription(): ?string
{ {
return $this->getRecord()->description; return $this->getRecord()->description;
} }
protected function isProcessed(): bool protected function isProcessed(): bool
{ {
if(is_null($this->processingPlaceholder)) if (is_null($this->processingPlaceholder)) {
return true; return true;
}
if($this->getRecord()->processed) if ($this->getRecord()->processed) {
return true; return true;
}
$timeDiff = time() - $this->getRecord()->last_checked; $timeDiff = time() - $this->getRecord()->last_checked;
if($timeDiff < $this->processingTime) if ($timeDiff < $this->processingTime) {
return false; return false;
}
$res = $this->checkIfFileIsProcessed(); $res = $this->checkIfFileIsProcessed();
$this->stateChanges("last_checked", time()); $this->stateChanges("last_checked", time());
@ -99,52 +111,55 @@ abstract class Media extends Postable
return $res; return $res;
} }
function isDeleted(): bool public function isDeleted(): bool
{ {
return (bool) $this->getRecord()->deleted; return (bool) $this->getRecord()->deleted;
} }
function setHash(string $hash): void public function setHash(string $hash): void
{ {
throw new ISE("Setting file hash manually is forbidden"); throw new ISE("Setting file hash manually is forbidden");
} }
function setFile(array $file): void public function setFile(array $file): void
{ {
if($file["error"] !== UPLOAD_ERR_OK) if ($file["error"] !== UPLOAD_ERR_OK) {
throw new ISE("File uploaded is corrupted"); throw new ISE("File uploaded is corrupted");
}
$hash = hash_file("whirlpool", $file["tmp_name"]); $hash = hash_file("whirlpool", $file["tmp_name"]);
$this->saveFile($file["tmp_name"], $hash); $this->saveFile($file["tmp_name"], $hash);
$this->stateChanges("hash", $hash); $this->stateChanges("hash", $hash);
} }
function save(): void public function save(?bool $log = false): void
{ {
if(!is_null($this->processingPlaceholder) && is_null($this->getRecord())) { if (!is_null($this->processingPlaceholder) && is_null($this->getRecord())) {
$this->stateChanges("processed", 0); $this->stateChanges("processed", 0);
$this->stateChanges("last_checked", time()); $this->stateChanges("last_checked", time());
} }
parent::save(); parent::save($log);
} }
function delete(bool $softly = true): void public function delete(bool $softly = true): void
{ {
$deleteQuirk = ovkGetQuirk("blobs.erase-upon-deletion"); $deleteQuirk = ovkGetQuirk("blobs.erase-upon-deletion");
if($deleteQuirk === 2 || ($deleteQuirk === 1 && !$softly)) if ($deleteQuirk === 2 || ($deleteQuirk === 1 && !$softly)) {
@unlink($this->getFileName()); @unlink($this->getFileName());
}
parent::delete($softly); parent::delete($softly);
} }
function undelete(): void public function undelete(): void
{ {
if(ovkGetQuirk("blobs.erase-upon-deletion") === 2) if (ovkGetQuirk("blobs.erase-upon-deletion") === 2) {
throw new \LogicException("Can't undelete model which is tied to blob, because of config constraint (quriks.yml:blobs.erase-upon-deletion)"); throw new \LogicException("Can't undelete model which is tied to blob, because of config constraint (quriks.yml:blobs.erase-upon-deletion)");
}
parent::undelete(); parent::undelete();
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Nette\Database\Table\ActiveRow; use Nette\Database\Table\ActiveRow;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
@ -10,144 +14,212 @@ use Chandler\Database\DatabaseConnection;
abstract class MediaCollection extends RowModel abstract class MediaCollection extends RowModel
{ {
use Traits\TOwnable;
protected $relTableName; protected $relTableName;
protected $entityTableName; protected $entityTableName;
protected $entityClassName; protected $entityClassName;
protected $allowDuplicates = true; protected $allowDuplicates = true;
protected $specialNames = []; protected $specialNames = [];
private $relations; protected $relations;
function __construct(?ActiveRow $ar = NULL) /**
* Maximum amount of items Collection can have
*/
public const MAX_ITEMS = INF;
/**
* Maximum amount of Collections with same "owner" allowed
*/
public const MAX_COUNT = INF;
public function __construct(?ActiveRow $ar = null)
{ {
parent::__construct($ar); parent::__construct($ar);
$this->relations = DatabaseConnection::i()->getContext()->table($this->relTableName); $this->relations = DatabaseConnection::i()->getContext()->table($this->relTableName);
} }
private function entitySuitable(RowModel $entity): bool private function entitySuitable(RowModel $entity): bool
{ {
if(($class = get_class($entity)) !== $this->entityClassName) if (($class = get_class($entity)) !== $this->entityClassName) {
throw new \UnexpectedValueException("This MediaCollection can only store '$this->entityClassName' (not '$class')."); throw new \UnexpectedValueException("This MediaCollection can only store '$this->entityClassName' (not '$class').");
}
return true; return true;
} }
function getOwner(): RowModel public function getOwner(): RowModel
{ {
$oid = $this->getRecord()->owner; $oid = $this->getRecord()->owner;
if($oid > 0) if ($oid > 0) {
return (new Users)->get($oid); return (new Users())->get($oid);
else } else {
return (new Clubs)->get($oid * -1); return (new Clubs())->get($oid * -1);
}
} }
function getPrettyId(): string public function getPrettyId(): string
{ {
return $this->getRecord()->owner . "_" . $this->getRecord()->id; return $this->getRecord()->owner . "_" . $this->getRecord()->id;
} }
function getName(): string public function getName(): string
{ {
$special = $this->getRecord()->special_type; $special = $this->getRecord()->special_type;
if($special === 0) if ($special === 0) {
return $this->getRecord()->name; return $this->getRecord()->name;
}
$sName = $this->specialNames[$special]; $sName = $this->specialNames[$special];
if(!$sName) if (!$sName) {
return $this->getRecord()->name; return $this->getRecord()->name;
}
if($sName[0] === "_")
if ($sName[0] === "_") {
$sName = tr(substr($sName, 1)); $sName = tr(substr($sName, 1));
}
return $sName; return $sName;
} }
function getDescription(): ?string public function getDescription(): ?string
{ {
return $this->getRecord()->description; return $this->getRecord()->description;
} }
abstract function getCoverURL(): ?string; abstract public function getCoverURL(): ?string;
function fetch(int $page = 1, ?int $perPage = NULL): \Traversable public function fetchClassic(int $offset = 0, ?int $limit = null): \Traversable
{ {
$related = $this->getRecord()->related("$this->relTableName.collection")->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE)->order("media ASC"); $related = $this->getRecord()->related("$this->relTableName.collection")
foreach($related as $rel) { ->limit($limit ?? OPENVK_DEFAULT_PER_PAGE, $offset)
->order("media ASC");
foreach ($related as $rel) {
$media = $rel->ref($this->entityTableName, "media"); $media = $rel->ref($this->entityTableName, "media");
if(!$media) if (!$media) {
continue; continue;
}
yield new $this->entityClassName($media); yield new $this->entityClassName($media);
} }
} }
function size(): int public function fetch(int $page = 1, ?int $perPage = null): \Traversable
{
$page = max(1, $page);
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
return $this->fetchClassic($perPage * ($page - 1), $perPage);
}
public function size(): int
{ {
return sizeof($this->getRecord()->related("$this->relTableName.collection")); return sizeof($this->getRecord()->related("$this->relTableName.collection"));
} }
function getCreationTime(): DateTime public function getCreationTime(): DateTime
{ {
return new DateTime($this->getRecord()->created); return new DateTime($this->getRecord()->created);
} }
function getPublicationTime(): DateTime public function getPublicationTime(): DateTime
{ {
return $this->getCreationTime(); return $this->getCreationTime();
} }
function getEditTime(): ?DateTime public function getEditTime(): ?DateTime
{ {
$edited = $this->getRecord()->edited; $edited = $this->getRecord()->edited;
if(is_null($edited)) return NULL; if (is_null($edited)) {
return null;
}
return new DateTime($edited); return new DateTime($edited);
} }
function isCreatedBySystem(): bool public function isCreatedBySystem(): bool
{ {
return $this->getRecord()->special_type !== 0; return $this->getRecord()->special_type !== 0;
} }
function add(RowModel $entity): bool public function add(RowModel $entity): bool
{ {
$this->entitySuitable($entity); $this->entitySuitable($entity);
if(!$this->allowDuplicates) if (!$this->allowDuplicates) {
if($this->has($entity)) if ($this->has($entity)) {
return false; return false;
}
}
if (self::MAX_ITEMS != INF) {
if (sizeof($this->relations->where("collection", $this->getId())) > self::MAX_ITEMS) {
throw new \OutOfBoundsException("Collection is full");
}
}
$this->relations->insert([ $this->relations->insert([
"collection" => $this->getId(), "collection" => $this->getId(),
"media" => $entity->getId(), "media" => $entity->getId(),
]); ]);
return true; return true;
} }
function remove(RowModel $entity): void public function remove(RowModel $entity): bool
{ {
$this->entitySuitable($entity); $this->entitySuitable($entity);
$this->relations->where([ return $this->relations->where([
"collection" => $this->getId(), "collection" => $this->getId(),
"media" => $entity->getId(), "media" => $entity->getId(),
])->delete(); ])->delete() > 0;
} }
function has(RowModel $entity): bool public function has(RowModel $entity): bool
{ {
$this->entitySuitable($entity); $this->entitySuitable($entity);
$rel = $this->relations->where([ $rel = $this->relations->where([
"collection" => $this->getId(), "collection" => $this->getId(),
"media" => $entity->getId(), "media" => $entity->getId(),
])->fetch(); ])->fetch();
return !is_null($rel); return !is_null($rel);
} }
use Traits\TOwnable; public function save(?bool $log = false): void
{
$thisTable = DatabaseConnection::i()->getContext()->table($this->tableName);
if (self::MAX_COUNT != INF) {
if (isset($this->changes["owner"])) {
if (sizeof($thisTable->where("owner", $this->changes["owner"])) > self::MAX_COUNT) {
throw new \OutOfBoundsException("Maximum amount of collections");
}
}
}
if (is_null($this->getRecord())) {
if (!isset($this->changes["created"])) {
$this->stateChanges("created", time());
} else {
$this->stateChanges("edited", time());
}
}
parent::save($log);
}
public function delete(bool $softly = true): void
{
if (!$softly) {
$this->relations->where("collection", $this->getId())
->delete();
}
parent::delete($softly);
}
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\Clubs; use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;
@ -12,108 +16,114 @@ use openvk\Web\Util\DateTime;
*/ */
class Message extends RowModel class Message extends RowModel
{ {
use Traits\TRichText;
use Traits\TAttachmentHost;
protected $tableName = "messages"; protected $tableName = "messages";
/** /**
* Get origin of the message. * Get origin of the message.
* *
* Returns either user or club. * Returns either user or club.
* *
* @returns User|Club * @returns User|Club
*/ */
function getSender(): ?RowModel public function getSender(): ?RowModel
{ {
if($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\User') if ($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\User') {
return (new Users)->get($this->getRecord()->sender_id); return (new Users())->get($this->getRecord()->sender_id);
else if($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\Club') } elseif ($this->getRecord()->sender_type === 'openvk\Web\Models\Entities\Club') {
return (new Clubs)->get($this->getRecord()->sender_id); return (new Clubs())->get($this->getRecord()->sender_id);
}
} }
/** /**
* Get the destination of the message. * Get the destination of the message.
* *
* Returns either user or club. * Returns either user or club.
* *
* @returns User|Club * @returns User|Club
*/ */
function getRecipient(): ?RowModel public function getRecipient(): ?RowModel
{ {
if($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\User') if ($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\User') {
return (new Users)->get($this->getRecord()->recipient_id); return (new Users())->get($this->getRecord()->recipient_id);
else if($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\Club') } elseif ($this->getRecord()->recipient_type === 'openvk\Web\Models\Entities\Club') {
return (new Clubs)->get($this->getRecord()->recipient_id); return (new Clubs())->get($this->getRecord()->recipient_id);
}
} }
function getUnreadState(): int public function getUnreadState(): int
{ {
trigger_error("TODO: use isUnread", E_USER_DEPRECATED); trigger_error("TODO: use isUnread", E_USER_DEPRECATED);
return (int) $this->isUnread(); return (int) $this->isUnread();
} }
/** /**
* Get date of initial publication. * Get date of initial publication.
* *
* @returns DateTime * @returns DateTime
*/ */
function getSendTime(): DateTime public function getSendTime(): DateTime
{ {
return new DateTime($this->getRecord()->created); return new DateTime($this->getRecord()->created);
} }
function getSendTimeHumanized(): string public function getSendTimeHumanized(): string
{ {
$dateTime = new DateTime($this->getRecord()->created); $dateTime = new DateTime($this->getRecord()->created);
if($dateTime->format("%d.%m.%y") == ovk_strftime_safe("%d.%m.%y", time())) { if ($dateTime->format("%d.%m.%y") == ovk_strftime_safe("%d.%m.%y", time())) {
return $dateTime->format("%T %p"); return $dateTime->format("%T");
} else { } else {
return $dateTime->format("%d.%m.%y"); return $dateTime->format("%d.%m.%y");
} }
} }
/** /**
* Get date of last edit, if any edits were made, otherwise null. * Get date of last edit, if any edits were made, otherwise null.
* *
* @returns DateTime|null * @returns DateTime|null
*/ */
function getEditTime(): ?DateTime public function getEditTime(): ?DateTime
{ {
$edited = $this->getRecord()->edited; $edited = $this->getRecord()->edited;
if(is_null($edited)) return NULL; if (is_null($edited)) {
return null;
}
return new DateTime($edited); return new DateTime($edited);
} }
/** /**
* Is this message an ad? * Is this message an ad?
* *
* Messages can never be ads. * Messages can never be ads.
* *
* @returns false * @returns false
*/ */
function isAd(): bool public function isAd(): bool
{ {
return false; return false;
} }
function isUnread(): bool public function isUnread(): bool
{ {
return (bool) $this->getRecord()->unread; return (bool) $this->getRecord()->unread;
} }
/** /**
* Simplify to array * Simplify to array
* *
* @returns array * @returns array
*/ */
function simplify(): array public function simplify(): array
{ {
$author = $this->getSender(); $author = $this->getSender();
$attachments = []; $attachments = [];
foreach($this->getChildren() as $attachment) { foreach ($this->getChildren() as $attachment) {
if($attachment instanceof Photo) { if ($attachment instanceof Photo) {
$attachments[] = [ $attachments[] = [
"type" => "photo", "type" => "photo",
"link" => "/photo" . $attachment->getPrettyId(), "link" => "/photo" . $attachment->getPrettyId(),
@ -123,28 +133,29 @@ class Message extends RowModel
], ],
]; ];
} else { } else {
throw new \Exception("Unknown attachment type: " . get_class($attachment)); $attachments[] = [
"type" => "unknown",
];
# throw new \Exception("Unknown attachment type: " . get_class($attachment));
} }
} }
return [ return [
"uuid" => $this->getId(), "uuid" => $this->getId(),
"sender" => [ "sender" => [
"id" => $author->getId(), "id" => $author->getId(),
"link" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $author->getURL(), "link" => $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $author->getURL(),
"avatar" => $author->getAvatarUrl(), "avatar" => $author->getAvatarUrl(),
"name" => $author->getFirstName().$unreadmsg, "name" => $author->getFirstName() . $unreadmsg,
], ],
"timing" => [ "timing" => [
"sent" => (string) $this->getSendTimeHumanized(), "sent" => (string) $this->getSendTimeHumanized(),
"edited" => is_null($this->getEditTime()) ? NULL : (string) $this->getEditTime(), "edited" => is_null($this->getEditTime()) ? null : (string) $this->getEditTime(),
], ],
"text" => $this->getText(), "text" => $this->getText(),
"read" => !$this->isUnread(), "read" => !$this->isUnread(),
"attachments" => $attachments, "attachments" => $attachments,
]; ];
} }
use Traits\TRichText;
use Traits\TAttachmentHost;
} }

View file

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime;
use openvk\Web\Models\Repositories\{Users};
use Nette\Database\Table\ActiveRow;
class NoSpamLog extends RowModel
{
protected $tableName = "noSpam_templates";
public function getId(): int
{
return $this->getRecord()->id;
}
public function getUser(): ?User
{
return (new Users())->get($this->getRecord()->user);
}
public function getModel(): string
{
return $this->getRecord()->model;
}
public function getRegex(): ?string
{
return $this->getRecord()->regex;
}
public function getRequest(): ?string
{
return $this->getRecord()->request;
}
public function getCount(): int
{
return $this->getRecord()->count;
}
public function getTime(): DateTime
{
return new DateTime($this->getRecord()->time);
}
public function getItems(): ?array
{
return explode(",", $this->getRecord()->items);
}
public function getTypeRaw(): int
{
return $this->getRecord()->ban_type;
}
public function getType(): string
{
switch ($this->getTypeRaw()) {
case 1: return "О";
case 2: return "Б";
case 3: return "ОБ";
default: return (string) $this->getTypeRaw();
}
}
public function isRollbacked(): bool
{
return !is_null($this->getRecord()->rollback);
}
}

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use HTMLPurifier_Config; use HTMLPurifier_Config;
use HTMLPurifier; use HTMLPurifier;
class Note extends Postable class Note extends Postable
{ {
protected $tableName = "notes"; protected $tableName = "notes";
protected function renderHTML(): string protected function renderHTML(): string
{ {
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
@ -74,64 +78,75 @@ class Note extends Postable
$config->set("Attr.AllowedClasses", [ $config->set("Attr.AllowedClasses", [
"underline", "underline",
]); ]);
$source = NULL; $source = null;
if(is_null($this->getRecord())) { if (is_null($this->getRecord())) {
if(isset($this->changes["source"])) if (isset($this->changes["source"])) {
$source = $this->changes["source"]; $source = $this->changes["source"];
else } else {
throw new \LogicException("Can't render note without content set."); throw new \LogicException("Can't render note without content set.");
}
} else { } else {
$source = $this->getRecord()->source; $source = $this->getRecord()->source;
} }
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
return $purifier->purify($source); return $purifier->purify($source);
} }
function getName(): string public function getName(): string
{ {
return $this->getRecord()->name; return $this->getRecord()->name;
} }
function getPreview(int $length = 25): string public function getPreview(int $length = 25): string
{ {
return ovk_proc_strtr(strip_tags($this->getRecord()->source), $length); return ovk_proc_strtr(strip_tags($this->getRecord()->source), $length);
} }
function getText(): string public function getText(): string
{ {
if(is_null($this->getRecord())) if (is_null($this->getRecord())) {
return $this->renderHTML(); return $this->renderHTML();
}
$cached = $this->getRecord()->cached_content; $cached = $this->getRecord()->cached_content;
if(!$cached) { if (!$cached) {
$cached = $this->renderHTML(); $cached = $this->renderHTML();
$this->setCached_Content($cached); $this->setCached_Content($cached);
$this->save(); $this->save();
} }
return $cached; return $cached;
} }
function getSource(): string public function getSource(): string
{ {
return $this->getRecord()->source; return $this->getRecord()->source;
} }
function toVkApiStruct(): object public function canBeViewedBy(?User $user = null): bool
{
if ($this->isDeleted() || $this->getOwner()->isDeleted()) {
return false;
}
return $this->getOwner()->getPrivacyPermission('notes.read', $user) && $this->getOwner()->canBeViewedBy($user);
}
public function toVkApiStruct(): object
{ {
$res = (object) []; $res = (object) [];
$res->type = "note"; $res->type = "note";
$res->id = $this->getId(); $res->id = $this->getVirtualId();
$res->owner_id = $this->getOwner()->getId(); $res->owner_id = $this->getOwner()->getId();
$res->title = $this->getName(); $res->title = $this->getName();
$res->text = $this->getText(); $res->text = $this->getText();
$res->date = $this->getPublicationTime()->timestamp(); $res->date = $this->getPublicationTime()->timestamp();
$res->comments = $this->getCommentsCount(); $res->comments = $this->getCommentsCount();
$res->read_comments = $this->getCommentsCount(); $res->read_comments = $this->getCommentsCount();
$res->view_url = "/note".$this->getOwner()->getId()."_".$this->getId(); $res->view_url = "/note" . $this->getOwner()->getId() . "_" . $this->getVirtualId();
$res->privacy_view = 1; $res->privacy_view = 1;
$res->can_comment = 1; $res->can_comment = 1;
$res->text_wiki = "r"; $res->text_wiki = "r";

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Club}; use openvk\Web\Models\Entities\{User, Club};
final class ClubModeratorNotification extends Notification final class ClubModeratorNotification extends Notification
{ {
protected $actionCode = 5; protected $actionCode = 5;
function __construct(User $recipient, Club $group, User $admin) public function __construct(User $recipient, Club $group, User $admin)
{ {
parent::__construct($recipient, $group, $admin, time(), ""); parent::__construct($recipient, $group, $admin, time(), "");
} }

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
final class CoinsTransferNotification extends Notification final class CoinsTransferNotification extends Notification
{ {
protected $actionCode = 9602; protected $actionCode = 9602;
function __construct(User $receiver, User $sender, int $value, string $message) public function __construct(User $receiver, User $sender, int $value, string $message)
{ {
parent::__construct($receiver, $receiver, $sender, time(), $value . " " . $message); parent::__construct($receiver, $receiver, $sender, time(), $value . " " . $message);
} }

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Post, Comment}; use openvk\Web\Models\Entities\{User, Post, Comment};
final class CommentNotification extends Notification final class CommentNotification extends Notification
{ {
protected $actionCode = 2; protected $actionCode = 2;
function __construct(User $recipient, Comment $comment, $postable, User $commenter) public function __construct(User $recipient, Comment $comment, $postable, User $commenter)
{ {
parent::__construct($recipient, $postable, $commenter, time(), ovk_proc_strtr(strip_tags($comment->getText()), 400)); parent::__construct($recipient, $postable, $commenter, time(), ovk_proc_strtr(strip_tags($comment->getText()), 400));
} }

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
final class FriendRemovalNotification extends Notification final class FriendRemovalNotification extends Notification
{ {
protected $actionCode = 4; protected $actionCode = 4;
function __construct(User $recipient, User $friend, User $remover) public function __construct(User $recipient, User $friend, User $remover)
{ {
parent::__construct($recipient, $friend, $remover, time(), ""); parent::__construct($recipient, $friend, $remover, time(), "");
} }

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Gift}; use openvk\Web\Models\Entities\{User, Gift};
final class GiftNotification extends Notification final class GiftNotification extends Notification
{ {
protected $actionCode = 9601; protected $actionCode = 9601;
function __construct(User $receiver, User $sender, Gift $gift, ?string $comment) public function __construct(User $receiver, User $sender, Gift $gift, ?string $comment)
{ {
parent::__construct($receiver, $gift, $sender, time(), $comment ?? ""); parent::__construct($receiver, $gift, $sender, time(), $comment ?? "");
} }

View file

@ -1,13 +1,17 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Post}; use openvk\Web\Models\Entities\{User, Post};
final class LikeNotification extends Notification final class LikeNotification extends Notification
{ {
protected $actionCode = 0; protected $actionCode = 0;
protected $threshold = 120; protected $threshold = 120;
function __construct(User $recipient, Post $post, User $liker) public function __construct(User $recipient, Post $post, User $liker)
{ {
parent::__construct($recipient, $post, $liker, time(), ""); parent::__construct($recipient, $post, $liker, time(), "");
} }

View file

@ -1,13 +1,17 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\Postable; use openvk\Web\Models\Entities\Postable;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
final class MentionNotification extends Notification final class MentionNotification extends Notification
{ {
protected $actionCode = 4; protected $actionCode = 4;
function __construct(User $recipient, Postable $discussionHost, $mentioner, string $quote = "") public function __construct(User $recipient, Postable $discussionHost, $mentioner, string $quote = "")
{ {
parent::__construct($recipient, $mentioner, $discussionHost, time(), $quote); parent::__construct($recipient, $mentioner, $discussionHost, time(), $quote);
} }

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Club};
final class NewSuggestedPostsNotification extends Notification
{
protected $actionCode = 7;
public function __construct(User $owner, Club $group)
{
parent::__construct($owner, $owner, $group, time(), "");
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{User}; use openvk\Web\Models\Entities\{User};
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
@ -12,11 +16,11 @@ class Notification
private $targetModel; private $targetModel;
private $time; private $time;
private $data; private $data;
protected $actionCode = NULL; protected $actionCode = null;
protected $threshold = -1; protected $threshold = -1;
function __construct(User $recipient, $originModel, $targetModel, ?int $time = NULL, string $data = "") public function __construct(User $recipient, $originModel, $targetModel, ?int $time = null, string $data = "")
{ {
$this->recipient = $recipient; $this->recipient = $recipient;
$this->originModel = $originModel; $this->originModel = $originModel;
@ -24,67 +28,68 @@ class Notification
$this->time = $time ?? time(); $this->time = $time ?? time();
$this->data = $data; $this->data = $data;
} }
private function encodeType(object $model): int private function encodeType(object $model): int
{ {
return (int) json_decode(file_get_contents(__DIR__ . "/../../../../data/modelCodes.json"), true)[get_class($model)]; return (int) json_decode(file_get_contents(__DIR__ . "/../../../../data/modelCodes.json"), true)[get_class($model)];
} }
function reverseModelOrder(): bool public function reverseModelOrder(): bool
{ {
return false; return false;
} }
function getActionCode(): int public function getActionCode(): int
{ {
return $this->actionCode; return $this->actionCode;
} }
function setActionCode(int $code): void public function setActionCode(int $code): void
{ {
$this->actionCode = $this->actionCode ?? $code; $this->actionCode ??= $code;
} }
function getTemplatePath(): string public function getTemplatePath(): string
{ {
return implode("_", [ return implode("_", [
"./../components/notifications/$this->actionCode/", "./../components/notifications/$this->actionCode/",
$this->encodeType($this->originModel), $this->encodeType($this->originModel),
$this->encodeType($this->targetModel), $this->encodeType($this->targetModel),
".xml" ".xml",
]); ]);
} }
function getRecipient(): User public function getRecipient(): User
{ {
return $this->recipient; return $this->recipient;
} }
function getModel(int $index): RowModel public function getModel(int $index): RowModel
{ {
switch($index) { switch ($index) {
case 0: case 0:
return $this->originModel; return $this->originModel;
case 1: case 1:
return $this->targetModel; return $this->targetModel;
} }
} }
function getData(): string public function getData(): string
{ {
return $this->data; return $this->data;
} }
function getDateTime(): DateTime public function getDateTime(): DateTime
{ {
return new DateTime($this->time); return new DateTime($this->time);
} }
function emit(): bool public function emit(): bool
{ {
if(!($e = eventdb())) if (!($e = eventdb())) {
return false; return false;
}
$data = [ $data = [
"recipient" => $this->recipient->getId(), "recipient" => $this->recipient->getId(),
"originModelType" => $this->encodeType($this->originModel), "originModelType" => $this->encodeType($this->originModel),
@ -95,41 +100,176 @@ class Notification
"additionalPayload" => $this->data, "additionalPayload" => $this->data,
"timestamp" => $this->time, "timestamp" => $this->time,
]; ];
$edb = $e->getConnection(); $edb = $e->getConnection();
if($this->threshold !== -1) { if ($this->threshold !== -1) {
# Event is thersholded, check if there is similar event # Event is thersholded, check if there is similar event
$query = <<<'QUERY' $query = <<<'QUERY'
SELECT * FROM `notifications` WHERE `recipientType`=0 AND `recipientId`=? AND `originModelType`=? AND `originModelId`=? AND `targetModelType`=? AND `targetModelId`=? AND `modelAction`=? AND `additionalData`=? AND `timestamp` > (? - ?) SELECT * FROM `notifications` WHERE `recipientType`=0 AND `recipientId`=? AND `originModelType`=? AND `originModelId`=? AND `targetModelType`=? AND `targetModelId`=? AND `modelAction`=? AND `additionalData`=? AND `timestamp` > (? - ?)
QUERY; QUERY;
$result = $edb->query($query, ...array_merge(array_values($data), [ $this->threshold ])); $result = $edb->query($query, ...array_merge(array_values($data), [ $this->threshold ]));
if($result->getRowCount() > 0) if ($result->getRowCount() > 0) {
return false; return false;
}
} }
$edb->query("INSERT INTO notifications VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?)", ...array_values($data)); $edb->query("INSERT INTO notifications VALUES (0, ?, ?, ?, ?, ?, ?, ?, ?)", ...array_values($data));
$kafkaConf = OPENVK_ROOT_CONF["openvk"]["credentials"]["notificationsBroker"]; $kafkaConf = OPENVK_ROOT_CONF["openvk"]["credentials"]["notificationsBroker"];
if($kafkaConf["enable"]) { if ($kafkaConf["enable"]) {
$kafkaConf = $kafkaConf["kafka"]; $kafkaConf = $kafkaConf["kafka"];
$brokerConf = new Conf(); $brokerConf = new Conf();
$brokerConf->set("log_level", (string) LOG_DEBUG); $brokerConf->set("log_level", (string) LOG_DEBUG);
$brokerConf->set("debug", "all"); $brokerConf->set("debug", "all");
$producer = new Producer($brokerConf); $producer = new Producer($brokerConf);
$producer->addBrokers($kafkaConf["addr"] . ":" . $kafkaConf["port"]); $producer->addBrokers($kafkaConf["addr"] . ":" . $kafkaConf["port"]);
$descriptor = implode(",", [ $descriptor = implode(",", [
str_replace("\\", ".", get_class($this)), str_replace("\\", ".", get_class($this)),
$this->recipient->getId(), $this->recipient->getId(),
base64_encode(serialize((object) $data)), base64_encode(serialize((object) $data)),
]); ]);
$notifTopic = $producer->newTopic($kafkaConf["topic"]); $notifTopic = $producer->newTopic($kafkaConf["topic"]);
$notifTopic->produce(RD_KAFKA_PARTITION_UA, RD_KAFKA_MSG_F_BLOCK, $descriptor); $notifTopic->produce(RD_KAFKA_PARTITION_UA, RD_KAFKA_MSG_F_BLOCK, $descriptor);
$producer->flush(100); $producer->flush(100);
} }
return true; return true;
} }
public function getVkApiInfo()
{
$origin_m = $this->encodeType($this->originModel);
$target_m = $this->encodeType($this->targetModel);
$info = [
"type" => "",
"parent" => null,
"feedback" => null,
];
switch ($this->getActionCode()) {
case 0:
$info["type"] = "like_post";
$info["parent"] = $this->getModel(0)->toNotifApiStruct();
$info["feedback"] = $this->getModel(1)->toVkApiStruct();
break;
case 1:
$info["type"] = "copy_post";
$info["parent"] = $this->getModel(0)->toNotifApiStruct();
$info["feedback"] = null; # todo
break;
case 2:
switch ($origin_m) {
case 19:
$info["type"] = "comment_video";
$info["parent"] = $this->getModel(0)->toNotifApiStruct();
$info["feedback"] = null; # айди коммента не сохраняется в бд( ну пиздец блять
break;
case 13:
$info["type"] = "comment_photo";
$info["parent"] = $this->getModel(0)->toNotifApiStruct();
$info["feedback"] = null;
break;
# unstandart (vk forgor about notes)
case 10:
$info["type"] = "comment_note";
$info["parent"] = $this->getModel(0)->toVkApiStruct();
$info["feedback"] = null;
break;
case 14:
$info["type"] = "comment_post";
$info["parent"] = $this->getModel(0)->toNotifApiStruct();
$info["feedback"] = null;
break;
# unused (users don't have topics bruh)
case 21:
$info["type"] = "comment_topic";
$info["parent"] = $this->getModel(0)->toVkApiStruct(0, 90);
break;
default:
$info["type"] = "comment_unknown";
break;
}
break;
case 3:
$info["type"] = "wall";
$info["feedback"] = $this->getModel(0)->toNotifApiStruct();
break;
case 4:
switch ($target_m) {
case 14:
$info["type"] = "mention";
$info["feedback"] = $this->getModel(1)->toNotifApiStruct();
break;
case 19:
$info["type"] = "mention_comment_video";
$info["parent"] = $this->getModel(1)->toNotifApiStruct();
break;
case 13:
$info["type"] = "mention_comment_photo";
$info["parent"] = $this->getModel(1)->toNotifApiStruct();
break;
# unstandart
case 10:
$info["type"] = "mention_comment_note";
$info["parent"] = $this->getModel(1)->toVkApiStruct();
break;
case 21:
$info["type"] = "mention_comments";
break;
default:
$info["type"] = "mention_comment_unknown";
break;
}
break;
case 5:
$info["type"] = "make_you_admin";
$info["parent"] = $this->getModel(0)->toVkApiStruct($this->getModel(1));
break;
# Нужно доделать после мержа #935
case 6:
$info["type"] = "wall_publish";
break;
# В вк не было такого уведомления, так что unstandart
case 7:
$info["type"] = "new_posts_in_club";
break;
# В вк при передаче подарков приходит сообщение, а не уведомление, так что unstandart
case 9601:
$info["type"] = "sent_gift";
$info["parent"] = $this->getModel(1)->toVkApiStruct($this->getModel(1));
break;
case 9602:
$info["type"] = "voices_transfer";
$info["parent"] = $this->getModel(1)->toVkApiStruct($this->getModel(1));
break;
case 9603:
$info["type"] = "up_rating";
$info["parent"] = $this->getModel(1)->toVkApiStruct($this->getModel(1));
$info["parent"]->count = $this->getData();
break;
default:
$info["type"] = null;
break;
}
return $info;
}
public function toVkApiStruct()
{
$res = (object) [];
$info = $this->getVkApiInfo();
$res->type = $info["type"];
$res->date = $this->getDateTime()->timestamp();
$res->parent = $info["parent"];
$res->feedback = $info["feedback"];
$res->reply = null; # Ответы на комментарии не реализованы
return $res;
}
} }

View file

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Club, Post};
final class PostAcceptedNotification extends Notification
{
protected $actionCode = 6;
public function __construct(User $author, Post $post, Club $group)
{
parent::__construct($author, $post, $group, time(), "");
}
}

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
final class RatingUpNotification extends Notification final class RatingUpNotification extends Notification
{ {
protected $actionCode = 9603; protected $actionCode = 9603;
function __construct(User $receiver, User $sender, int $value, string $message) public function __construct(User $receiver, User $sender, int $value, string $message)
{ {
parent::__construct($receiver, $receiver, $sender, time(), $value . " " . $message); parent::__construct($receiver, $receiver, $sender, time(), $value . " " . $message);
} }

View file

@ -1,13 +1,17 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Post}; use openvk\Web\Models\Entities\{User, Post};
final class RepostNotification extends Notification final class RepostNotification extends Notification
{ {
protected $actionCode = 1; protected $actionCode = 1;
protected $threshold = 120; protected $threshold = 120;
function __construct(User $recipient, Post $post, User $reposter) public function __construct(User $recipient, Post $post, User $reposter)
{ {
parent::__construct($recipient, $post, $reposter, time(), ""); parent::__construct($recipient, $post, $reposter, time(), "");
} }

View file

@ -1,12 +1,16 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities\Notifications; namespace openvk\Web\Models\Entities\Notifications;
use openvk\Web\Models\Entities\{User, Post}; use openvk\Web\Models\Entities\{User, Post};
final class WallPostNotification extends Notification final class WallPostNotification extends Notification
{ {
protected $actionCode = 3; protected $actionCode = 3;
function __construct(User $recipient, Post $post, User $poster) public function __construct(User $recipient, Post $post, User $poster)
{ {
parent::__construct($recipient, $post, $poster, time(), ovk_proc_strtr($post->getText(), 10)); parent::__construct($recipient, $post, $poster, time(), ovk_proc_strtr($post->getText(), 10));
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
@ -7,58 +11,58 @@ use openvk\Web\Util\DateTime;
class PasswordReset extends RowModel class PasswordReset extends RowModel
{ {
protected $tableName = "password_resets"; protected $tableName = "password_resets";
function getUser(): User public function getUser(): User
{ {
return (new Users)->get($this->getRecord()->profile); return (new Users())->get($this->getRecord()->profile);
} }
function getKey(): string public function getKey(): string
{ {
return $this->getRecord()->key; return $this->getRecord()->key;
} }
function getToken(): string public function getToken(): string
{ {
return $this->getKey(); return $this->getKey();
} }
function getCreationTime(): DateTime public function getCreationTime(): DateTime
{ {
return new DateTime($this->getRecord()->timestamp); return new DateTime($this->getRecord()->timestamp);
} }
/** /**
* User can request password reset only if he does not have any "new" password resets. * User can request password reset only if he does not have any "new" password resets.
* Password reset becomes "old" after 5 minutes and one second. * Password reset becomes "old" after 5 minutes and one second.
*/ */
function isNew(): bool public function isNew(): bool
{ {
return $this->getRecord()->timestamp > (time() - (5 * MINUTE)); return $this->getRecord()->timestamp > (time() - (5 * MINUTE));
} }
/** /**
* Token is valid only for 3 days. * Token is valid only for 3 days.
*/ */
function isStillValid(): bool public function isStillValid(): bool
{ {
return $this->getRecord()->timestamp > (time() - (3 * DAY)); return $this->getRecord()->timestamp > (time() - (3 * DAY));
} }
function verify(string $token): bool public function verify(string $token): bool
{ {
try { try {
return $this->isStillValid() ? sodium_memcmp($this->getKey(), $token) : false; return $this->isStillValid() ? sodium_memcmp($this->getKey(), $token) : false;
} catch(\SodiumException $ex) { } catch (\SodiumException $ex) {
return false; return false;
} }
} }
function save(): void public function save(?bool $log = false): void
{ {
$this->stateChanges("key", base64_encode(openssl_random_pseudo_bytes(46))); $this->stateChanges("key", base64_encode(openssl_random_pseudo_bytes(46)));
$this->stateChanges("timestamp", time()); $this->stateChanges("timestamp", time());
parent::save(); parent::save($log);
} }
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use MessagePack\MessagePack; use MessagePack\MessagePack;
use Nette\Utils\ImageException; use Nette\Utils\ImageException;
use Nette\Utils\UnknownImageFileException; use Nette\Utils\UnknownImageFileException;
@ -14,8 +18,8 @@ class Photo extends Media
protected $tableName = "photos"; protected $tableName = "photos";
protected $fileExtension = "jpeg"; protected $fileExtension = "jpeg";
const ALLOWED_SIDE_MULTIPLIER = 7; public const ALLOWED_SIDE_MULTIPLIER = 7;
/** /**
* @throws \ImagickException * @throws \ImagickException
* @throws ImageException * @throws ImageException
@ -25,70 +29,78 @@ class Photo extends Media
{ {
$res = [false]; $res = [false];
$requiresProportion = ((string) $size["requireProp"]) != "none"; $requiresProportion = ((string) $size["requireProp"]) != "none";
if($requiresProportion) { if ($requiresProportion) {
$props = explode(":", (string) $size["requireProp"]); $props = explode(":", (string) $size["requireProp"]);
$px = (int) $props[0]; $px = (int) $props[0];
$py = (int) $props[1]; $py = (int) $props[1];
if(($image->getImageWidth() / $image->getImageHeight()) > ($px / $py)) { if (($image->getImageWidth() / $image->getImageHeight()) > ($px / $py)) {
$height = (int) ceil(($px * $image->getImageWidth()) / $py); $height = (int) ceil(($px * $image->getImageWidth()) / $py);
$image->cropImage($image->getImageWidth(), $height, 0, 0); $image->cropImage($image->getImageWidth(), $height, 0, 0);
$res[0] = true; $res[0] = true;
} }
} }
if(isset($size["maxSize"])) { if (isset($size["maxSize"])) {
$maxSize = (int) $size["maxSize"]; $maxSize = (int) $size["maxSize"];
$sizes = Image::calculateSize($image->getImageWidth(), $image->getImageHeight(), $maxSize, $maxSize, Image::SHRINK_ONLY | Image::FIT); $sizes = Image::calculateSize($image->getImageWidth(), $image->getImageHeight(), $maxSize, $maxSize, Image::SHRINK_ONLY | Image::FIT);
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1); $image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
} else if(isset($size["maxResolution"])) { } elseif (isset($size["maxResolution"])) {
$resolution = explode("x", (string) $size["maxResolution"]); $resolution = explode("x", (string) $size["maxResolution"]);
$sizes = Image::calculateSize( $sizes = Image::calculateSize(
$image->getImageWidth(), $image->getImageHeight(), (int) $resolution[0], (int) $resolution[1], Image::SHRINK_ONLY | Image::FIT $image->getImageWidth(),
$image->getImageHeight(),
(int) $resolution[0],
(int) $resolution[1],
Image::SHRINK_ONLY | Image::FIT
); );
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1); $image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
} else { } else {
throw new \RuntimeException("Malformed size description: " . (string) $size["id"]); throw new \RuntimeException("Malformed size description: " . (string) $size["id"]);
} }
$res[1] = $image->getImageWidth(); $res[1] = $image->getImageWidth();
$res[2] = $image->getImageHeight(); $res[2] = $image->getImageHeight();
if($res[1] <= 300 || $res[2] <= 300) if ($res[1] <= 300 || $res[2] <= 300) {
$image->writeImage("$outputDir/$size[id].gif"); $image->writeImage("$outputDir/$size[id].gif");
else } else {
$image->writeImage("$outputDir/$size[id].jpeg"); $image->writeImage("$outputDir/$size[id].jpeg");
}
$res[3] = true; $res[3] = true;
$image->destroy(); $image->destroy();
unset($image); unset($image);
return $res; return $res;
} }
private function saveImageResizedCopies(?\Imagick $image, string $filename, string $hash): void private function saveImageResizedCopies(?\Imagick $image, string $filename, string $hash): void
{ {
if(!$image) { if (!$image) {
$image = new \Imagick; $image = new \Imagick();
$image->readImage($filename); $image->readImage($filename);
} }
$dir = dirname($this->pathFromHash($hash)); $dir = dirname($this->pathFromHash($hash));
$dir = "$dir/$hash" . "_cropped"; $dir = "$dir/$hash" . "_cropped";
if(!is_dir($dir)) { if (!is_dir($dir)) {
@unlink($dir); # Added to transparently bypass issues with dead pesudofolders summoned by buggy SWIFT impls (selectel) @unlink($dir); # Added to transparently bypass issues with dead pesudofolders summoned by buggy SWIFT impls (selectel)
mkdir($dir); mkdir($dir);
} }
$sizes = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml"); $sizes = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
if(!$sizes) if (!$sizes) {
throw new \RuntimeException("Could not load photosizes.xml!"); throw new \RuntimeException("Could not load photosizes.xml!");
}
$sizesMeta = []; $sizesMeta = [];
if(OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["photoSaving"] === "quick") { if (OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["photoSaving"] === "quick") {
foreach($sizes->Size as $size) foreach ($sizes->Size as $size) {
$sizesMeta[(string)$size["id"]] = [false, false, false, false]; $sizesMeta[(string) $size["id"]] = [false, false, false, false];
}
} else { } else {
foreach($sizes->Size as $size) foreach ($sizes->Size as $size) {
$sizesMeta[(string)$size["id"]] = $this->resizeImage(clone $image, $dir, $size); $sizesMeta[(string) $size["id"]] = $this->resizeImage(clone $image, $dir, $size);
}
} }
$sizesMeta = MessagePack::pack($sizesMeta); $sizesMeta = MessagePack::pack($sizesMeta);
@ -97,73 +109,92 @@ class Photo extends Media
protected function saveFile(string $filename, string $hash): bool protected function saveFile(string $filename, string $hash): bool
{ {
$image = new \Imagick; $input_image = new \Imagick();
$image->readImage($filename); $input_image->readImage($filename);
$h = $image->getImageHeight(); $h = $input_image->getImageHeight();
$w = $image->getImageWidth(); $w = $input_image->getImageWidth();
if(($h >= ($w * Photo::ALLOWED_SIDE_MULTIPLIER)) || ($w >= ($h * Photo::ALLOWED_SIDE_MULTIPLIER))) if (($h >= ($w * Photo::ALLOWED_SIDE_MULTIPLIER)) || ($w >= ($h * Photo::ALLOWED_SIDE_MULTIPLIER))) {
throw new ISE("Invalid layout: image is too wide/short"); throw new ISE("Invalid layout: image is too wide/short");
}
# gif fix 10.01.2025
if ($input_image->getImageFormat() === 'GIF') {
$input_image->setIteratorIndex(0);
}
# png workaround (transparency to white)
$image = new \Imagick();
$bg = new \ImagickPixel('white');
$image->newImage($w, $h, $bg);
$image->compositeImage($input_image, \Imagick::COMPOSITE_OVER, 0, 0);
$sizes = Image::calculateSize( $sizes = Image::calculateSize(
$image->getImageWidth(), $image->getImageHeight(), 8192, 4320, Image::SHRINK_ONLY | Image::FIT $image->getImageWidth(),
$image->getImageHeight(),
8192,
4320,
Image::SHRINK_ONLY | Image::FIT
); );
$image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1); $image->resizeImage($sizes[0], $sizes[1], \Imagick::FILTER_HERMITE, 1);
$image->writeImage($this->pathFromHash($hash)); $image->writeImage($this->pathFromHash($hash));
$this->saveImageResizedCopies($image, $filename, $hash); $this->saveImageResizedCopies($image, $filename, $hash);
return true; return true;
} }
function crop(real $left, real $top, real $width, real $height): void public function crop(float $left, float $top, float $width, float $height): void
{ {
if(isset($this->changes["hash"])) if (isset($this->changes["hash"])) {
$hash = $this->changes["hash"]; $hash = $this->changes["hash"];
else if(!is_null($this->getRecord())) } elseif (!is_null($this->getRecord())) {
$hash = $this->getRecord()->hash; $hash = $this->getRecord()->hash;
else } else {
throw new ISE("Cannot crop uninitialized image. Please call setFile(\$_FILES[...]) first."); throw new ISE("Cannot crop uninitialized image. Please call setFile(\$_FILES[...]) first.");
}
$image = Image::fromFile($this->pathFromHash($hash)); $image = Image::fromFile($this->pathFromHash($hash));
$image->crop($left, $top, $width, $height); $image->crop($left, $top, $width, $height);
$image->save($this->pathFromHash($hash)); $image->save($this->pathFromHash($hash));
} }
function isolate(): void public function isolate(): void
{ {
if(is_null($this->getRecord())) if (is_null($this->getRecord())) {
throw new ISE("Cannot isolate unpresisted image. Please save() it first."); throw new ISE("Cannot isolate unpresisted image. Please save() it first.");
}
DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete(); DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete();
} }
function getSizes(bool $upgrade = false, bool $forceUpdate = false): ?array public function getSizes(bool $upgrade = false, bool $forceUpdate = false): ?array
{ {
$sizes = $this->getRecord()->sizes; $sizes = $this->getRecord()->sizes;
if(!$sizes || $forceUpdate) { if (!$sizes || $forceUpdate) {
if($forceUpdate || $upgrade || OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["upgradeStructure"]) { if ($forceUpdate || $upgrade || OPENVK_ROOT_CONF["openvk"]["preferences"]["photos"]["upgradeStructure"]) {
$hash = $this->getRecord()->hash; $hash = $this->getRecord()->hash;
$this->saveImageResizedCopies(NULL, $this->pathFromHash($hash), $hash); $this->saveImageResizedCopies(null, $this->pathFromHash($hash), $hash);
$this->save(); $this->save();
return $this->getSizes(); return $this->getSizes();
} }
return NULL; return null;
} }
$res = []; $res = [];
$sizes = MessagePack::unpack($sizes); $sizes = MessagePack::unpack($sizes);
foreach($sizes as $id => $meta) { foreach ($sizes as $id => $meta) {
if(isset($meta[3]) && !$meta[3]) { if (isset($meta[3]) && !$meta[3]) {
$res[$id] = (object) [ $res[$id] = (object) [
"url" => ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/photos/thumbnails/" . $this->getId() . "_$id.jpeg", "url" => ovk_scheme(true) . $_SERVER["HTTP_HOST"] . "/photos/thumbnails/" . $this->getId() . "_$id.jpeg",
"width" => NULL, "width" => null,
"height" => NULL, "height" => null,
"crop" => NULL "crop" => null,
]; ];
continue; continue;
} }
$url = $this->getURL(); $url = $this->getURL();
$url = str_replace(".$this->fileExtension", "_cropped/$id.", $url); $url = str_replace(".$this->fileExtension", "_cropped/$id.", $url);
$url .= ($meta[1] <= 300 || $meta[2] <= 300) ? "gif" : "jpeg"; $url .= ($meta[1] <= 300 || $meta[2] <= 300) ? "gif" : "jpeg";
@ -172,7 +203,7 @@ class Photo extends Media
"url" => $url, "url" => $url,
"width" => $meta[1], "width" => $meta[1],
"height" => $meta[2], "height" => $meta[2],
"crop" => $meta[0] "crop" => $meta[0],
]; ];
} }
@ -181,69 +212,78 @@ class Photo extends Media
"url" => $this->getURL(), "url" => $this->getURL(),
"width" => $x, "width" => $x,
"height" => $y, "height" => $y,
"crop" => false "crop" => false,
]; ];
return $res; return $res;
} }
function forceSize(string $sizeName): bool public function forceSize(string $sizeName): bool
{ {
$hash = $this->getRecord()->hash; $hash = $this->getRecord()->hash;
$sizes = MessagePack::unpack($this->getRecord()->sizes); $sizes = MessagePack::unpack($this->getRecord()->sizes);
$size = $sizes[$sizeName] ?? false; $size = $sizes[$sizeName] ?? false;
if(!$size) if (!$size) {
return $size; return $size;
}
if(!isset($size[3]) || $size[3] === true)
if (!isset($size[3]) || $size[3] === true) {
return true; return true;
}
$path = $this->pathFromHash($hash); $path = $this->pathFromHash($hash);
$dir = dirname($this->pathFromHash($hash)); $dir = dirname($this->pathFromHash($hash));
$dir = "$dir/$hash" . "_cropped"; $dir = "$dir/$hash" . "_cropped";
if(!is_dir($dir)) { if (!is_dir($dir)) {
@unlink($dir); @unlink($dir);
mkdir($dir); mkdir($dir);
} }
$sizeMetas = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml"); $sizeMetas = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
if(!$sizeMetas) if (!$sizeMetas) {
throw new \RuntimeException("Could not load photosizes.xml!"); throw new \RuntimeException("Could not load photosizes.xml!");
}
$sizeInfo = NULL;
foreach($sizeMetas->Size as $size) $sizeInfo = null;
if($size["id"] == $sizeName) foreach ($sizeMetas->Size as $size) {
if ($size["id"] == $sizeName) {
$sizeInfo = $size; $sizeInfo = $size;
}
if(!$sizeInfo) }
if (!$sizeInfo) {
return false; return false;
}
$pic = new \Imagick;
$pic = new \Imagick();
$pic->readImage($path); $pic->readImage($path);
$sizes[$sizeName] = $this->resizeImage($pic, $dir, $sizeInfo); $sizes[$sizeName] = $this->resizeImage($pic, $dir, $sizeInfo);
$this->stateChanges("sizes", MessagePack::pack($sizes)); $this->stateChanges("sizes", MessagePack::pack($sizes));
$this->save(); $this->save();
return $sizes[$sizeName][3]; return $sizes[$sizeName][3];
} }
function getVkApiSizes(): ?array public function getVkApiSizes(): ?array
{ {
$res = []; $res = [];
$sizes = $this->getSizes(); $sizes = $this->getSizes();
if(!$sizes) if (!$sizes) {
return NULL; return null;
}
$manifest = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml"); $manifest = simplexml_load_file(OPENVK_ROOT . "/data/photosizes.xml");
if(!$manifest) if (!$manifest) {
return NULL; return null;
}
$mappings = []; $mappings = [];
foreach($manifest->Size as $size) foreach ($manifest->Size as $size) {
$mappings[(string) $size["id"]] = (string) $size["vkId"]; $mappings[(string) $size["id"]] = (string) $size["vkId"];
}
foreach($sizes as $id => $meta) { foreach ($sizes as $id => $meta) {
$type = $mappings[$id] ?? $id; $type = $mappings[$id] ?? $id;
$meta->type = $type; $meta->type = $type;
$res[$type] = $meta; $res[$type] = $meta;
@ -252,24 +292,26 @@ class Photo extends Media
return $res; return $res;
} }
function getURLBySizeId(string $size): string public function getURLBySizeId(string $size): string
{ {
$sizes = $this->getSizes(); $sizes = $this->getSizes();
if(!$sizes) if (!$sizes) {
return $this->getURL(); return $this->getURL();
}
$size = $sizes[$size]; $size = $sizes[$size];
if(!$size) if (!$size) {
return $this->getURL(); return $this->getURL();
}
return $size->url; return $size->url;
} }
function getDimensions(): array public function getDimensions(): array
{ {
$x = $this->getRecord()->width; $x = $this->getRecord()->width;
$y = $this->getRecord()->height; $y = $this->getRecord()->height;
if(!$x) { # no sizes in database if (!$x) { # no sizes in database
$hash = $this->getRecord()->hash; $hash = $this->getRecord()->hash;
$image = Image::fromFile($this->pathFromHash($hash)); $image = Image::fromFile($this->pathFromHash($hash));
@ -283,32 +325,33 @@ class Photo extends Media
return [$x, $y]; return [$x, $y];
} }
function getPageURL(): string public function getPageURL(): string
{ {
if($this->isAnonymous()) if ($this->isAnonymous()) {
return "/photos/" . base_convert((string) $this->getId(), 10, 32); return "/photos/" . base_convert((string) $this->getId(), 10, 32);
}
return "/photo" . $this->getPrettyId(); return "/photo" . $this->getPrettyId();
} }
function getAlbum(): ?Album public function getAlbum(): ?Album
{ {
return (new Albums)->getAlbumByPhotoId($this); return (new Albums())->getAlbumByPhotoId($this);
} }
function toVkApiStruct(bool $photo_sizes = true, bool $extended = false): object public function toVkApiStruct(bool $photo_sizes = true, bool $extended = false): object
{ {
$res = (object) []; $res = (object) [];
$res->id = $res->pid = $this->getVirtualId(); $res->id = $res->pid = $this->getVirtualId();
$res->owner_id = $res->user_id = $this->getOwner()->getId(); $res->owner_id = $res->user_id = $this->getOwner()->getId();
$res->aid = $res->album_id = NULL; $res->aid = $res->album_id = null;
$res->width = $this->getDimensions()[0]; $res->width = $this->getDimensions()[0];
$res->height = $this->getDimensions()[1]; $res->height = $this->getDimensions()[1];
$res->date = $res->created = $this->getPublicationTime()->timestamp(); $res->date = $res->created = $this->getPublicationTime()->timestamp();
if($photo_sizes) { if ($photo_sizes) {
$res->sizes = $this->getVkApiSizes(); $res->sizes = array_values($this->getVkApiSizes());
$res->src_small = $res->photo_75 = $this->getURLBySizeId("miniscule"); $res->src_small = $res->photo_75 = $this->getURLBySizeId("miniscule");
$res->src = $res->photo_130 = $this->getURLBySizeId("tiny"); $res->src = $res->photo_130 = $this->getURLBySizeId("tiny");
$res->src_big = $res->photo_604 = $this->getURLBySizeId("normal"); $res->src_big = $res->photo_604 = $this->getURLBySizeId("normal");
@ -318,7 +361,7 @@ class Photo extends Media
$res->src_original = $res->url = $this->getURLBySizeId("UPLOADED_MAXRES"); $res->src_original = $res->url = $this->getURLBySizeId("UPLOADED_MAXRES");
} }
if($extended) { if ($extended) {
$res->likes = $this->getLikesCount(); # их нету но пусть будут $res->likes = $this->getLikesCount(); # их нету но пусть будут
$res->comments = $this->getCommentsCount(); $res->comments = $this->getCommentsCount();
$res->tags = 0; $res->tags = 0;
@ -329,9 +372,22 @@ class Photo extends Media
return $res; return $res;
} }
static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL, bool $anon = false): Photo public function canBeViewedBy(?User $user = null): bool
{ {
$photo = new static; if ($this->isDeleted() || $this->getOwner()->isDeleted()) {
return false;
}
if (!is_null($this->getAlbum())) {
return $this->getAlbum()->canBeViewedBy($user);
} else {
return $this->getOwner()->canBeViewedBy($user);
}
}
public static function fastMake(int $owner, string $description = "", array $file, ?Album $album = null, bool $anon = false): Photo
{
$photo = new static();
$photo->setOwner($owner); $photo->setOwner($owner);
$photo->setDescription(iconv_substr($description, 0, 36) . "..."); $photo->setDescription(iconv_substr($description, 0, 36) . "...");
$photo->setAnonymous($anon); $photo->setAnonymous($anon);
@ -339,7 +395,7 @@ class Photo extends Media
$photo->setFile($file); $photo->setFile($file);
$photo->save(); $photo->save();
if(!is_null($album)) { if (!is_null($album)) {
$album->addPhoto($photo); $album->addPhoto($photo);
$album->setEdited(time()); $album->setEdited(time());
$album->save(); $album->save();
@ -347,4 +403,20 @@ class Photo extends Media
return $photo; return $photo;
} }
public function toNotifApiStruct()
{
$res = (object) [];
$res->id = $this->getVirtualId();
$res->owner_id = $this->getOwner()->getId();
$res->aid = 0;
$res->src = $this->getURLBySizeId("tiny");
$res->src_big = $this->getURLBySizeId("normal");
$res->src_small = $this->getURLBySizeId("miniscule");
$res->text = $this->getDescription();
$res->created = $this->getPublicationTime()->timestamp();
return $res;
}
} }

View file

@ -0,0 +1,300 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection;
use Nette\Database\Table\ActiveRow;
use openvk\Web\Models\Repositories\Audios;
use openvk\Web\Models\Repositories\Photos;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\Photo;
/**
* @method setName(string $name)
* @method setDescription(?string $desc)
*/
class Playlist extends MediaCollection
{
protected $tableName = "playlists";
protected $relTableName = "playlist_relations";
protected $entityTableName = "audios";
protected $entityClassName = 'openvk\Web\Models\Entities\Audio';
protected $allowDuplicates = false;
private $importTable;
public const MAX_COUNT = 1000;
public const MAX_ITEMS = 10000;
public function __construct(?ActiveRow $ar = null)
{
parent::__construct($ar);
$this->importTable = DatabaseConnection::i()->getContext()->table("playlist_imports");
}
public function getCoverURL(string $size = "normal"): ?string
{
$photo = (new Photos())->get((int) $this->getRecord()->cover_photo_id);
return is_null($photo) ? "/assets/packages/static/openvk/img/song.jpg" : $photo->getURLBySizeId($size);
}
public function getLength(): int
{
return $this->getRecord()->length;
}
public function fetchClassic(int $offset = 0, ?int $limit = null): \Traversable
{
$related = $this->getRecord()->related("$this->relTableName.collection")
->limit($limit ?? OPENVK_DEFAULT_PER_PAGE, $offset)
->order("index ASC");
foreach ($related as $rel) {
$media = $rel->ref($this->entityTableName, "media");
if (!$media) {
continue;
}
yield new $this->entityClassName($media);
}
}
public function getAudios(int $offset = 0, ?int $limit = null, ?int $shuffleSeed = null): \Traversable
{
if (!$shuffleSeed) {
foreach ($this->fetchClassic($offset, $limit) as $e) {
yield $e;
} # No, I can't return, it will break with []
return;
}
$ids = [];
foreach ($this->relations->select("media AS i")->where("collection", $this->getId()) as $rel) {
$ids[] = $rel->i;
}
$ids = knuth_shuffle($ids, $shuffleSeed);
$ids = array_slice($ids, $offset, $limit ?? OPENVK_DEFAULT_PER_PAGE);
foreach ($ids as $id) {
yield (new Audios())->get($id);
}
}
public function add(RowModel $audio): bool
{
if ($res = parent::add($audio)) {
$this->stateChanges("length", $this->getRecord()->length + $audio->getLength());
$this->save();
}
return $res;
}
public function remove(RowModel $audio): bool
{
if ($res = parent::remove($audio)) {
$this->stateChanges("length", $this->getRecord()->length - $audio->getLength());
$this->save();
}
return $res;
}
public function isBookmarkedBy(RowModel $entity): bool
{
$id = $entity->getId();
if ($entity instanceof Club) {
$id *= -1;
}
return !is_null($this->importTable->where([
"entity" => $id,
"playlist" => $this->getId(),
])->fetch());
}
public function bookmark(RowModel $entity): bool
{
if ($this->isBookmarkedBy($entity)) {
return false;
}
$id = $entity->getId();
if ($entity instanceof Club) {
$id *= -1;
}
if ($this->importTable->where("entity", $id)->count() > self::MAX_COUNT) {
throw new \OutOfBoundsException("Maximum amount of playlists");
}
$this->importTable->insert([
"entity" => $id,
"playlist" => $this->getId(),
]);
return true;
}
public function unbookmark(RowModel $entity): bool
{
$id = $entity->getId();
if ($entity instanceof Club) {
$id *= -1;
}
$count = $this->importTable->where([
"entity" => $id,
"playlist" => $this->getId(),
])->delete();
return $count > 0;
}
public function getDescription(): ?string
{
return $this->getRecord()->description;
}
public function getDescriptionHTML(): ?string
{
return htmlspecialchars($this->getRecord()->description, ENT_DISALLOWED | ENT_XHTML);
}
public function getListens()
{
return $this->getRecord()->listens;
}
public function toVkApiStruct(?User $user = null): object
{
$oid = $this->getOwner()->getId();
if ($this->getOwner() instanceof Club) {
$oid *= -1;
}
return (object) [
"id" => $this->getId(),
"owner_id" => $oid,
"title" => $this->getName(),
"description" => $this->getDescription(),
"size" => $this->size(),
"length" => $this->getLength(),
"created" => $this->getCreationTime()->timestamp(),
"modified" => $this->getEditTime() ? $this->getEditTime()->timestamp() : null,
"accessible" => $this->canBeViewedBy($user),
"editable" => $this->canBeModifiedBy($user),
"bookmarked" => $this->isBookmarkedBy($user),
"listens" => $this->getListens(),
"cover_url" => $this->getCoverURL(),
"searchable" => !$this->isUnlisted(),
];
}
public function setLength(): void
{
throw new \LogicException("Can't set length of playlist manually");
}
public function resetLength(): bool
{
$this->stateChanges("length", 0);
return true;
}
public function delete(bool $softly = true): void
{
$ctx = DatabaseConnection::i()->getContext();
$ctx->table("playlist_imports")->where("playlist", $this->getId())
->delete();
parent::delete($softly);
}
public function hasAudio(Audio $audio): bool
{
$ctx = DatabaseConnection::i()->getContext();
return !is_null($ctx->table("playlist_relations")->where([
"collection" => $this->getId(),
"media" => $audio->getId(),
])->fetch());
}
public function getCoverPhotoId(): ?int
{
return $this->getRecord()->cover_photo_id;
}
public function getCoverPhoto(): ?Photo
{
return (new Photos())->get((int) $this->getRecord()->cover_photo_id);
}
public function canBeModifiedBy(User $user): bool
{
if (!$user) {
return false;
}
if ($this->getOwner() instanceof User) {
return $user->getId() == $this->getOwner()->getId();
} else {
return $this->getOwner()->canBeModifiedBy($user);
}
}
public function getLengthInMinutes(): int
{
return (int) round($this->getLength() / 60, PHP_ROUND_HALF_DOWN);
}
public function fastMakeCover(int $owner, array $file)
{
$cover = new Photo();
$cover->setOwner($owner);
$cover->setDescription("Playlist cover image");
$cover->setFile($file);
$cover->setCreated(time());
$cover->save();
$this->setCover_photo_id($cover->getId());
return $cover;
}
public function getURL(): string
{
return "/playlist" . $this->getOwner()->getRealId() . "_" . $this->getId();
}
public function incrementListens()
{
$this->stateChanges("listens", ($this->getListens() + 1));
}
public function getMetaDescription(): string
{
$length = $this->getLengthInMinutes();
$props = [];
$props[] = tr("audios_count", $this->size());
$props[] = "<span id='listensCount'>" . tr("listens_count", $this->getListens()) . "</span>";
if ($length > 0) {
$props[] = tr("minutes_count", $length);
}
$props[] = tr("created_playlist") . " " . $this->getPublicationTime();
# if($this->getEditTime()) $props[] = tr("updated_playlist") . " " . $this->getEditTime();
return implode("", $props);
}
public function isUnlisted(): bool
{
return (bool) $this->getRecord()->unlisted;
}
}

View file

@ -1,10 +1,14 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Exceptions\TooMuchOptionsException; use openvk\Web\Models\Exceptions\TooMuchOptionsException;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use \UnexpectedValueException; use UnexpectedValueException;
use Nette\InvalidStateException; use Nette\InvalidStateException;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\{Users, Posts};
use Chandler\Database\DatabaseConnection; use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Exceptions\PollLockedException; use openvk\Web\Models\Exceptions\PollLockedException;
use openvk\Web\Models\Exceptions\AlreadyVotedException; use openvk\Web\Models\Exceptions\AlreadyVotedException;
@ -13,178 +17,198 @@ use openvk\Web\Models\Exceptions\InvalidOptionException;
class Poll extends Attachable class Poll extends Attachable
{ {
protected $tableName = "polls"; protected $tableName = "polls";
private $choicesToPersist = []; private $choicesToPersist = [];
function getTitle(): string public function getTitle(): string
{ {
return $this->getRecord()->title; return $this->getRecord()->title;
} }
function getMetaDescription(): string public function getMetaDescription(): string
{ {
$props = []; $props = [];
$props[] = tr($this->isAnonymous() ? "poll_anon" : "poll_public"); $props[] = tr($this->isAnonymous() ? "poll_anon" : "poll_public");
if($this->isMultipleChoice()) $props[] = tr("poll_multi"); if ($this->isMultipleChoice()) {
if(!$this->isRevotable()) $props[] = tr("poll_lock"); $props[] = tr("poll_multi");
if(!is_null($this->endsAt())) $props[] = tr("poll_until", $this->endsAt()); }
if (!$this->isRevotable()) {
$props[] = tr("poll_lock");
}
if (!is_null($this->endsAt())) {
$props[] = tr("poll_until", $this->endsAt());
}
return implode("", $props); return implode("", $props);
} }
function getOwner(): User public function getOwner(): User
{ {
return (new Users)->get($this->getRecord()->owner); return (new Users())->get($this->getRecord()->owner);
} }
function getOptions(): array public function getOptions(): array
{ {
$options = $this->getRecord()->related("poll_options.poll"); $options = $this->getRecord()->related("poll_options.poll");
$res = []; $res = [];
foreach($options as $opt) foreach ($options as $opt) {
$res[$opt->id] = $opt->name; $res[$opt->id] = $opt->name;
}
return $res; return $res;
} }
function getUserVote(User $user): ?array public function getUserVote(User $user): ?array
{ {
$ctx = DatabaseConnection::i()->getContext(); $ctx = DatabaseConnection::i()->getContext();
$votedOpts = $ctx->table("poll_votes") $votedOpts = $ctx->table("poll_votes")
->where(["user" => $user->getId(), "poll" => $this->getId()]); ->where(["user" => $user->getId(), "poll" => $this->getId()]);
if($votedOpts->count() == 0) if ($votedOpts->count() == 0) {
return NULL; return null;
}
$res = []; $res = [];
foreach($votedOpts as $votedOpt) { foreach ($votedOpts as $votedOpt) {
$option = $ctx->table("poll_options")->get($votedOpt->option); $option = $ctx->table("poll_options")->get($votedOpt->option);
$res[] = [$option->id, $option->name]; $res[] = [$option->id, $option->name];
} }
return $res; return $res;
} }
function getVoters(int $optionId, int $page = 1, ?int $perPage = NULL): array public function getVoters(int $optionId, int $page = 1, ?int $perPage = null): array
{ {
$res = []; $res = [];
$ctx = DatabaseConnection::i()->getContext(); $ctx = DatabaseConnection::i()->getContext();
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; $perPage ??= OPENVK_DEFAULT_PER_PAGE;
$voters = $ctx->table("poll_votes")->where(["poll" => $this->getId(), "option" => $optionId]); $voters = $ctx->table("poll_votes")->where(["poll" => $this->getId(), "option" => $optionId]);
foreach($voters->page($page, $perPage) as $vote) foreach ($voters->page($page, $perPage) as $vote) {
$res[] = (new Users)->get($vote->user); $res[] = (new Users())->get($vote->user);
}
return $res; return $res;
} }
function getVoterCount(?int $optionId = NULL): int public function getVoterCount(?int $optionId = null): int
{ {
$votes = DatabaseConnection::i()->getContext()->table("poll_votes"); $votes = DatabaseConnection::i()->getContext()->table("poll_votes");
if(!$optionId) if (!$optionId) {
return $votes->select("COUNT(DISTINCT user) AS c")->where("poll", $this->getId())->fetch()->c; return $votes->select("COUNT(DISTINCT user) AS c")->where("poll", $this->getId())->fetch()->c;
}
return $votes->where(["poll" => $this->getId(), "option" => $optionId])->count(); return $votes->where(["poll" => $this->getId(), "option" => $optionId])->count();
} }
function getResults(?User $user = NULL): object public function getResults(?User $user = null): object
{ {
$ctx = DatabaseConnection::i()->getContext(); $ctx = DatabaseConnection::i()->getContext();
$voted = NULL; $voted = null;
if(!is_null($user)) if (!is_null($user)) {
$voted = $this->getUserVote($user); $voted = $this->getUserVote($user);
}
$result = (object) []; $result = (object) [];
$result->totalVotes = $this->getVoterCount(); $result->totalVotes = $this->getVoterCount();
$unsOptions = []; $unsOptions = [];
foreach($this->getOptions() as $id => $title) { foreach ($this->getOptions() as $id => $title) {
$option = (object) []; $option = (object) [];
$option->id = $id; $option->id = $id;
$option->name = $title; $option->name = $title;
$option->votes = $this->getVoterCount($id); $option->votes = $this->getVoterCount($id);
$option->pct = $result->totalVotes == 0 ? 0 : min(100, floor(($option->votes / $result->totalVotes) * 100)); $option->pct = $result->totalVotes == 0 ? 0 : min(100, floor(($option->votes / $result->totalVotes) * 100));
$option->voters = $this->getVoters($id, 1, 10); $option->voters = $this->getVoters($id, 1, 10);
if(!$user || !$voted) if (!$user || !$voted) {
$option->voted = NULL; $option->voted = null;
else } else {
$option->voted = in_array([$id, $title], $voted); $option->voted = in_array([$id, $title], $voted);
}
$unsOptions[$id] = $option; $unsOptions[$id] = $option;
} }
$optionsC = sizeof($unsOptions); $optionsC = sizeof($unsOptions);
$sOptions = $unsOptions; $sOptions = $unsOptions;
usort($sOptions, function($a, $b) { return $a->votes <=> $b->votes; }); usort($sOptions, function ($a, $b) { return $a->votes <=> $b->votes; });
for($i = 0; $i < $optionsC; $i++) for ($i = 0; $i < $optionsC; $i++) {
$unsOptions[$id]->rate = $optionsC - $i - 1; $unsOptions[$id]->rate = $optionsC - $i - 1;
}
$result->options = array_values($unsOptions); $result->options = array_values($unsOptions);
return $result; return $result;
} }
function isAnonymous(): bool public function isAnonymous(): bool
{ {
return (bool) $this->getRecord()->is_anonymous; return (bool) $this->getRecord()->is_anonymous;
} }
function isMultipleChoice(): bool public function isMultipleChoice(): bool
{ {
return (bool) $this->getRecord()->allows_multiple; return (bool) $this->getRecord()->allows_multiple;
} }
function isRevotable(): bool public function isRevotable(): bool
{ {
return (bool) $this->getRecord()->can_revote; return (bool) $this->getRecord()->can_revote;
} }
function endsAt(): ?DateTime public function endsAt(): ?DateTime
{ {
if(!$this->getRecord()->until) if (!$this->getRecord()->until) {
return NULL; return null;
}
return new DateTime($this->getRecord()->until); return new DateTime($this->getRecord()->until);
} }
function hasEnded(): bool public function hasEnded(): bool
{ {
if($this->getRecord()->ended) if ($this->getRecord()->ended) {
return true; return true;
}
if(!is_null($this->getRecord()->until))
if (!is_null($this->getRecord()->until)) {
return time() >= $this->getRecord()->until; return time() >= $this->getRecord()->until;
}
return false; return false;
} }
function hasVoted(User $user): bool public function hasVoted(User $user): bool
{ {
return !is_null($this->getUserVote($user)); return !is_null($this->getUserVote($user));
} }
function canVote(User $user): bool public function canVote(User $user): bool
{ {
return !$this->hasEnded() && !$this->hasVoted($user); return !$this->hasEnded() && !$this->hasVoted($user) && !is_null($this->getAttachedPost()) && $this->getAttachedPost()->getSuggestionType() == 0;
} }
function vote(User $user, array $optionIds): void public function vote(User $user, array $optionIds): void
{ {
if($this->hasEnded()) if ($this->hasEnded()) {
throw new PollLockedException; throw new PollLockedException();
}
if($this->hasVoted($user))
throw new AlreadyVotedException; if ($this->hasVoted($user)) {
throw new AlreadyVotedException();
$optionIds = array_map(function($x) { return (int) $x; }, array_unique($optionIds)); }
$optionIds = array_map(function ($x) { return (int) $x; }, array_unique($optionIds));
$validOpts = array_keys($this->getOptions()); $validOpts = array_keys($this->getOptions());
if(empty($optionIds) || (sizeof($optionIds) > 1 && !$this->isMultipleChoice())) if (empty($optionIds) || (sizeof($optionIds) > 1 && !$this->isMultipleChoice())) {
throw new UnexpectedValueException; throw new UnexpectedValueException();
}
if(sizeof(array_diff($optionIds, $validOpts)) > 0)
throw new InvalidOptionException; if (sizeof(array_diff($optionIds, $validOpts)) > 0) {
throw new InvalidOptionException();
foreach($optionIds as $opt) { }
foreach ($optionIds as $opt) {
DatabaseConnection::i()->getContext()->table("poll_votes")->insert([ DatabaseConnection::i()->getContext()->table("poll_votes")->insert([
"user" => $user->getId(), "user" => $user->getId(),
"poll" => $this->getId(), "poll" => $this->getId(),
@ -192,64 +216,69 @@ class Poll extends Attachable
]); ]);
} }
} }
function revokeVote(User $user): void public function revokeVote(User $user): void
{ {
if(!$this->isRevotable()) if (!$this->isRevotable()) {
throw new PollLockedException; throw new PollLockedException();
}
$this->getRecord()->related("poll_votes.poll") $this->getRecord()->related("poll_votes.poll")
->where("user", $user->getId())->delete(); ->where("user", $user->getId())->delete();
} }
function setOwner(User $owner): void public function setOwner(User $owner): void
{ {
$this->stateChanges("owner", $owner->getId()); $this->stateChanges("owner", $owner->getId());
} }
function setEndDate(int $timestamp): void public function setEndDate(int $timestamp): void
{ {
if(!is_null($this->getRecord())) if (!is_null($this->getRecord())) {
throw new PollLockedException; throw new PollLockedException();
}
$this->stateChanges("until", $timestamp); $this->stateChanges("until", $timestamp);
} }
function setEnded(): void public function setEnded(): void
{ {
$this->stateChanges("ended", 1); $this->stateChanges("ended", 1);
} }
function setOptions(array $options): void public function setOptions(array $options): void
{ {
if(!is_null($this->getRecord())) if (!is_null($this->getRecord())) {
throw new PollLockedException; throw new PollLockedException();
}
if(sizeof($options) > ovkGetQuirk("polls.max-opts"))
throw new TooMuchOptionsException; if (sizeof($options) > ovkGetQuirk("polls.max-opts")) {
throw new TooMuchOptionsException();
}
$this->choicesToPersist = $options; $this->choicesToPersist = $options;
} }
function setRevotability(bool $canReVote): void public function setRevotability(bool $canReVote): void
{ {
if(!is_null($this->getRecord())) if (!is_null($this->getRecord())) {
throw new PollLockedException; throw new PollLockedException();
}
$this->stateChanges("can_revote", $canReVote); $this->stateChanges("can_revote", $canReVote);
} }
function setAnonymity(bool $anonymous): void public function setAnonymity(bool $anonymous): void
{ {
$this->stateChanges("is_anonymous", $anonymous); $this->stateChanges("is_anonymous", $anonymous);
} }
function setMultipleChoice(bool $mc): void public function setMultipleChoice(bool $mc): void
{ {
$this->stateChanges("allows_multiple", $mc); $this->stateChanges("allows_multiple", $mc);
} }
function importXML(User $owner, string $xml): void public function importXML(User $owner, string $xml): void
{ {
$xml = simplexml_load_string($xml); $xml = simplexml_load_string($xml);
$this->setOwner($owner); $this->setOwner($owner);
@ -257,39 +286,69 @@ class Poll extends Attachable
$this->setMultipleChoice(($xml["multiple"] ?? "no") == "yes"); $this->setMultipleChoice(($xml["multiple"] ?? "no") == "yes");
$this->setAnonymity(($xml["anonymous"] ?? "no") == "yes"); $this->setAnonymity(($xml["anonymous"] ?? "no") == "yes");
$this->setRevotability(($xml["locked"] ?? "no") == "no"); $this->setRevotability(($xml["locked"] ?? "no") == "no");
if(ctype_digit((string) ($xml["duration"] ?? ""))) if (ctype_digit((string) ($xml["duration"] ?? ""))) {
$this->setEndDate(time() + ((86400 * (int) $xml["duration"]))); $this->setEndDate(time() + ((86400 * (int) $xml["duration"])));
}
$options = []; $options = [];
foreach($xml->options->option as $opt) foreach ($xml->options->option as $opt) {
$options[] = (string) $opt; $options[] = (string) $opt;
}
if(empty($options))
throw new UnexpectedValueException; if (empty($options)) {
throw new UnexpectedValueException();
}
$this->setOptions($options); $this->setOptions($options);
} }
static function import(User $owner, string $xml): Poll public static function import(User $owner, string $xml): Poll
{ {
$poll = new Poll; $poll = new Poll();
$poll->importXML($owner, $xml); $poll->importXML($owner, $xml);
$poll->save(); $poll->save();
return $poll; return $poll;
} }
function save(): void public function canBeViewedBy(?User $user = null): bool
{ {
if(empty($this->choicesToPersist)) # waiting for #935 :(
throw new InvalidStateException; /*if(!is_null($this->getAttachedPost())) {
return $this->getAttachedPost()->canBeViewedBy($user);
parent::save(); } else {*/
foreach($this->choicesToPersist as $option) { return true;
#}
}
public function save(?bool $log = false): void
{
if (empty($this->choicesToPersist)) {
throw new InvalidStateException();
}
parent::save($log);
foreach ($this->choicesToPersist as $option) {
DatabaseConnection::i()->getContext()->table("poll_options")->insert([ DatabaseConnection::i()->getContext()->table("poll_options")->insert([
"poll" => $this->getId(), "poll" => $this->getId(),
"name" => $option, "name" => $option,
]); ]);
} }
} }
public function getAttachedPost()
{
$post = DatabaseConnection::i()->getContext()->table("attachments")
->where(
["attachable_type" => static::class,
"attachable_id" => $this->getId()]
)->fetch();
if (!is_null($post->target_id)) {
return (new Posts())->get($post->target_id);
} else {
return null;
}
}
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use Chandler\Database\DatabaseConnection as DB; use Chandler\Database\DatabaseConnection as DB;
use openvk\Web\Models\Repositories\{Clubs, Users}; use openvk\Web\Models\Repositories\{Clubs, Users};
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
@ -7,6 +11,7 @@ use openvk\Web\Models\Entities\Notifications\LikeNotification;
class Post extends Postable class Post extends Postable
{ {
use Traits\TRichText;
protected $tableName = "posts"; protected $tableName = "posts";
protected $upperNodeReferenceColumnName = "wall"; protected $upperNodeReferenceColumnName = "wall";
@ -18,54 +23,60 @@ class Post extends Postable
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
]; ];
if((sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) !== $liked) { if ((sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) !== $liked) {
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment) if ($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment) {
(new LikeNotification($this->getOwner(false), $this, $user))->emit(); (new LikeNotification($this->getOwner(false), $this, $user))->emit();
}
parent::setLike($liked, $user); parent::setLike($liked, $user);
} }
if($depth < ovkGetQuirk("wall.repost-liking-recursion-limit")) if ($depth < ovkGetQuirk("wall.repost-liking-recursion-limit")) {
foreach($this->getChildren() as $attachment) foreach ($this->getChildren() as $attachment) {
if($attachment instanceof Post) if ($attachment instanceof Post) {
$attachment->setLikeRecursively($liked, $user, $depth + 1); $attachment->setLikeRecursively($liked, $user, $depth + 1);
}
}
}
} }
/** /**
* May return fake owner (group), if flags are [1, (*)] * May return fake owner (group), if flags are [1, (*)]
* *
* @param bool $honourFlags - check flags * @param bool $honourFlags - check flags
*/ */
function getOwner(bool $honourFlags = true, bool $real = false): RowModel public function getOwner(bool $honourFlags = true, bool $real = false): RowModel
{ {
if($honourFlags && $this->isPostedOnBehalfOfGroup()) { if ($honourFlags && $this->isPostedOnBehalfOfGroup()) {
if($this->getRecord()->wall < 0) if ($this->getRecord()->wall < 0) {
return (new Clubs)->get(abs($this->getRecord()->wall)); return (new Clubs())->get(abs($this->getRecord()->wall));
}
} }
return parent::getOwner($real); return parent::getOwner($real);
} }
function getPrettyId(): string public function getPrettyId(): string
{ {
return $this->getRecord()->wall . "_" . $this->getVirtualId(); return $this->getRecord()->wall . "_" . $this->getVirtualId();
} }
function getTargetWall(): int public function getTargetWall(): int
{ {
return $this->getRecord()->wall; return $this->getRecord()->wall;
} }
function getWallOwner() public function getWallOwner()
{ {
$w = $this->getRecord()->wall; $w = $this->getRecord()->wall;
if($w < 0) if ($w < 0) {
return (new Clubs)->get(abs($w)); return (new Clubs())->get(abs($w));
}
return (new Users)->get($w); return (new Users())->get($w);
} }
function getRepostCount(): int public function getRepostCount(): int
{ {
return sizeof( return sizeof(
$this->getRecord() $this->getRecord()
@ -73,56 +84,97 @@ class Post extends Postable
->where("attachable_type", get_class($this)) ->where("attachable_type", get_class($this))
); );
} }
function isPinned(): bool public function isPinned(): bool
{ {
return (bool) $this->getRecord()->pinned; return (bool) $this->getRecord()->pinned;
} }
function isAd(): bool public function hasSource(): bool
{
return $this->getRecord()->source != null;
}
public function getSource(bool $format = false)
{
$orig_source = $this->getRecord()->source;
if (!str_contains($orig_source, "https://") && !str_contains($orig_source, "http://")) {
$orig_source = "https://" . $orig_source;
}
if (!$format) {
return $orig_source;
}
return $this->formatLinks($orig_source);
}
public function setSource(string $source)
{
$result = check_copyright_link($source);
$this->stateChanges("source", $source);
}
public function resetSource()
{
$this->stateChanges("source", null);
}
public function getVkApiCopyright(): object
{
return (object) [
'id' => 0,
'link' => $this->getSource(false),
'name' => $this->getSource(false),
'type' => 'link',
];
}
public function isAd(): bool
{ {
return (bool) $this->getRecord()->ad; return (bool) $this->getRecord()->ad;
} }
function isPostedOnBehalfOfGroup(): bool public function isPostedOnBehalfOfGroup(): bool
{ {
return ($this->getRecord()->flags & 0b10000000) > 0; return ($this->getRecord()->flags & 0b10000000) > 0;
} }
function isSigned(): bool public function isSigned(): bool
{ {
return ($this->getRecord()->flags & 0b01000000) > 0; return ($this->getRecord()->flags & 0b01000000) > 0;
} }
function isDeactivationMessage(): bool public function isDeactivationMessage(): bool
{ {
return (($this->getRecord()->flags & 0b00100000) > 0) && ($this->getRecord()->owner > 0); return (($this->getRecord()->flags & 0b00100000) > 0) && ($this->getRecord()->owner > 0);
} }
function isUpdateAvatarMessage(): bool public function isUpdateAvatarMessage(): bool
{ {
return (($this->getRecord()->flags & 0b00010000) > 0) && ($this->getRecord()->owner > 0); return (($this->getRecord()->flags & 0b00010000) > 0) && ($this->getRecord()->owner > 0);
} }
function isExplicit(): bool public function isExplicit(): bool
{ {
return (bool) $this->getRecord()->nsfw; return (bool) $this->getRecord()->nsfw;
} }
function isDeleted(): bool public function isDeleted(): bool
{ {
return (bool) $this->getRecord()->deleted; return (bool) $this->getRecord()->deleted;
} }
function getOwnerPost(): int public function getOwnerPost(): int
{ {
return $this->getOwner(false)->getId(); return $this->getOwner(false)->getId();
} }
function getPlatform(bool $forAPI = false): ?string public function getPlatform(bool $forAPI = false): ?string
{ {
$platform = $this->getRecord()->api_source_name; $platform = $this->getRecord()->api_source_name;
if($forAPI) { if ($forAPI) {
switch ($platform) { switch ($platform) {
case 'openvk_refresh_android': case 'openvk_refresh_android':
case 'openvk_legacy_android': case 'openvk_legacy_android':
@ -133,16 +185,20 @@ class Post extends Postable
case 'openvk_legacy_ios': case 'openvk_legacy_ios':
return 'iphone'; return 'iphone';
break; break;
case 'windows_phone':
return 'wphone';
break;
case 'vika_touch': // кика хохотач ахахахаххахахахахах case 'vika_touch': // кика хохотач ахахахаххахахахахах
case 'vk4me': case 'vk4me':
return 'mobile'; return 'mobile';
break; break;
case NULL: case null:
return NULL; return null;
break; break;
default: default:
return 'api'; return 'api';
break; break;
@ -152,17 +208,17 @@ class Post extends Postable
} }
} }
function getPlatformDetails(): array public function getPlatformDetails(): array
{ {
$clients = simplexml_load_file(OPENVK_ROOT . "/data/clients.xml"); $clients = simplexml_load_file(OPENVK_ROOT . "/data/clients.xml");
foreach($clients as $client) { foreach ($clients as $client) {
if($client['tag'] == $this->getPlatform()) { if ($client['tag'] == $this->getPlatform()) {
return [ return [
"tag" => $client['tag'], "tag" => $client['tag'],
"name" => $client['name'], "name" => $client['name'],
"url" => $client['url'], "url" => $client['url'],
"img" => $client['img'] "img" => $client['img'],
]; ];
break; break;
} }
@ -170,13 +226,40 @@ class Post extends Postable
return [ return [
"tag" => $this->getPlatform(), "tag" => $this->getPlatform(),
"name" => NULL, "name" => null,
"url" => NULL, "url" => null,
"img" => NULL "img" => null,
]; ];
} }
function pin(): void public function getPostSourceInfo(): array
{
$post_source = ["type" => "vk"];
if ($this->getPlatform(true) !== null) {
$post_source = [
"type" => "api",
"platform" => $this->getPlatform(true),
];
}
if ($this->isUpdateAvatarMessage()) {
$post_source['data'] = 'profile_photo';
}
return $post_source;
}
public function getVkApiType(): string
{
$type = 'post';
if ($this->getSuggestionType() != 0) {
$type = 'suggest';
}
return $type;
}
public function pin(): void
{ {
DB::i() DB::i()
->getContext() ->getContext()
@ -186,65 +269,264 @@ class Post extends Postable
"pinned" => true, "pinned" => true,
]) ])
->update(["pinned" => false]); ->update(["pinned" => false]);
$this->stateChanges("pinned", true); $this->stateChanges("pinned", true);
$this->save(); $this->save();
} }
function unpin(): void public function unpin(): void
{ {
$this->stateChanges("pinned", false); $this->stateChanges("pinned", false);
$this->save(); $this->save();
} }
function canBePinnedBy(User $user): bool public function canBePinnedBy(User $user = null): bool
{ {
if($this->getTargetWall() < 0) if (!$user) {
return (new Clubs)->get(abs($this->getTargetWall()))->canBeModifiedBy($user); return false;
}
if ($this->getTargetWall() < 0) {
return (new Clubs())->get(abs($this->getTargetWall()))->canBeModifiedBy($user);
}
return $this->getTargetWall() === $user->getId(); return $this->getTargetWall() === $user->getId();
} }
function canBeDeletedBy(User $user): bool public function canBeDeletedBy(User $user = null): bool
{ {
if (!$user) {
return false;
}
if ($this->getTargetWall() < 0 && !$this->getWallOwner()->canBeModifiedBy($user) && $this->getWallOwner()->getWallType() != 1 && $this->getSuggestionType() == 0) {
return false;
}
return $this->getOwnerPost() === $user->getId() || $this->canBePinnedBy($user); return $this->getOwnerPost() === $user->getId() || $this->canBePinnedBy($user);
} }
function setContent(string $content): void public function setContent(string $content): void
{ {
if(ctype_space($content)) if (ctype_space($content)) {
throw new \LengthException("Content length must be at least 1 character (not counting whitespaces)."); throw new \LengthException("Content length must be at least 1 character (not counting whitespaces).");
else if(iconv_strlen($content) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxSize"]) } elseif (iconv_strlen($content) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["maxSize"]) {
throw new \LengthException("Content is too large."); throw new \LengthException("Content is too large.");
}
$this->stateChanges("content", $content); $this->stateChanges("content", $content);
} }
function toggleLike(User $user): bool public function toggleLike(User $user): bool
{ {
$liked = parent::toggleLike($user); $liked = parent::toggleLike($user);
if($this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment) if (!$user->isPrivateLikes() && $this->getOwner(false)->getId() !== $user->getId() && !($this->getOwner() instanceof Club) && !$this instanceof Comment) {
(new LikeNotification($this->getOwner(false), $this, $user))->emit(); (new LikeNotification($this->getOwner(false), $this, $user))->emit();
}
foreach($this->getChildren() as $attachment) foreach ($this->getChildren() as $attachment) {
if($attachment instanceof Post) if ($attachment instanceof Post) {
$attachment->setLikeRecursively($liked, $user, 2); $attachment->setLikeRecursively($liked, $user, 2);
}
}
return $liked; return $liked;
} }
function setLike(bool $liked, User $user): void public function setLike(bool $liked, User $user): void
{ {
$this->setLikeRecursively($liked, $user, 1); $this->setLikeRecursively($liked, $user, 1);
} }
function deletePost(): void public function deletePost(): void
{ {
$this->setDeleted(1); $this->setDeleted(1);
$this->unwire(); $this->unwire();
$this->save(); $this->save();
} }
use Traits\TRichText; public function canBeViewedBy(?User $user = null): bool
{
if ($this->isDeleted()) {
return false;
}
return $this->getWallOwner()->canBeViewedBy($user);
}
public function getSuggestionType()
{
return $this->getRecord()->suggested;
}
public function getPageURL(): string
{
return "/wall" . $this->getPrettyId();
}
public function toNotifApiStruct()
{
$res = (object) [];
$res->id = $this->getVirtualId();
$res->to_id = $this->getOwner() instanceof Club ? $this->getOwner()->getId() * -1 : $this->getOwner()->getId();
$res->from_id = $res->to_id;
$res->date = $this->getPublicationTime()->timestamp();
$res->text = $this->getText(false);
$res->attachments = []; # todo
$res->copy_owner_id = null; # todo
$res->copy_post_id = null; # todo
return $res;
}
public function canBeEditedBy(?User $user = null): bool
{
if (!$user) {
return false;
}
if ($this->isDeactivationMessage() || $this->isUpdateAvatarMessage()) {
return false;
}
if ($this->getTargetWall() > 0) {
return $this->getPublicationTime()->timestamp() + WEEK > time() && $user->getId() == $this->getOwner(false)->getId();
} else {
if ($this->isPostedOnBehalfOfGroup()) {
return $this->getWallOwner()->canBeModifiedBy($user);
} else {
return $user->getId() == $this->getOwner(false)->getId();
}
}
return $user->getId() == $this->getOwner(false)->getId();
}
public function toRss(): \Bhaktaraz\RSSGenerator\Item
{
$domain = ovk_scheme(true) . $_SERVER["HTTP_HOST"];
$description = $this->getText(false);
$title = str_replace("\n", "", ovk_proc_strtr($description, 79));
$description_html = $description;
$url = $domain . "/wall" . $this->getPrettyId();
if ($this->isUpdateAvatarMessage()) {
$title = tr('upd_in_general');
}
if ($this->isDeactivationMessage()) {
$title = tr('post_deact_in_general');
}
$author = $this->getOwner();
$target_wall = $this->getWallOwner();
$author_name = escape_html($author->getCanonicalName());
if ($this->isExplicit()) {
$title = 'NSFW: ' . $title;
}
foreach ($this->getChildren() as $child) {
if ($child instanceof Photo) {
$child_page = $domain . $child->getPageURL();
$child_url = $child->getURL();
$description_html .= "<br /><a href='$child_page'><img src='$child_url'></a><br />";
} elseif ($child instanceof Video) {
$child_page = $domain . '/video' . $child->getPrettyId();
if ($child->getType() != 1) {
$description_html .= "" .
"<br />" .
"<video width=\"320\" height=\"240\" controls><source src=\"" . $child->getURL() . "\" type=\"video/mp4\"></video><br />" .
"<b>" . escape_html($child->getName()) . "</b><br />";
} else {
$description_html .= "" .
"<br />" .
"<a href=\"" . $child->getVideoDriver()->getURL() . "\"><b>" . escape_html($child->getName()) . "</b></a><br />";
}
} elseif ($child instanceof Audio) {
if (!$child->isWithdrawn()) {
$description_html .= "<br />"
. "<b>" . escape_html($child->getName()) . "</b>:"
. "<br />"
. "<audio controls>"
. "<source src=\"" . $child->getOriginalURL() . "\" type=\"audio/mpeg\"></audio>"
. "<br />";
}
} elseif ($child instanceof Poll) {
$description_html .= "<br />" . tr('poll') . ": " . escape_html($child->getTitle());
} elseif ($child instanceof Note) {
$description_html .= "<br />" . tr('note') . ": " . escape_html($child->getName());
}
}
$description_html .= "<br />" . tr('author') . ": <img width='15px' src='" . $author->getAvatarURL() . "'><a href='" . $author->getURL() . "'>" . $author_name . "</a>";
if ($target_wall->getRealId() != $author->getRealId()) {
$description_html .= "<br />" . tr('on_wall') . ": <img width='15px' src='" . $target_wall->getAvatarURL() . "'><a href='" . $target_wall->getURL() . "'>" . escape_html($target_wall->getCanonicalName()) . "</a>";
}
if ($this->isSigned()) {
$signer = $this->getOwner(false);
$description_html .= "<br />" . tr('sign_short') . ": <img width='15px' src='" . $signer->getAvatarURL() . "'><a href='" . $signer->getURL() . "'>" . escape_html($signer->getCanonicalName()) . "</a>";
}
if ($this->hasSource()) {
$description_html .= "<br />" . tr('source') . ": " . escape_html($this->getSource());
}
$item = new \Bhaktaraz\RSSGenerator\Item();
$item->title($title)
->url($url)
->guid($url)
->creator($author_name)
->pubDate($this->getPublicationTime()->timestamp())
->content(str_replace("\n", "<br />", $description_html));
return $item;
}
public function getGeo(): ?object
{
if (!$this->getRecord()->geo) {
return null;
}
return (object) json_decode($this->getRecord()->geo, true, JSON_UNESCAPED_UNICODE);
}
public function setGeo($encoded_object): void
{
$final_geo = $encoded_object['name'];
$neutral_names = ["Россия", "Russia", "Росія", "Россія", "Украина", "Ukraine", "Україна", "Украіна"];
foreach ($neutral_names as $name) {
if (str_contains($final_geo, $name . ", ")) {
$final_geo = str_replace($name . ", ", "", $final_geo);
}
}
$encoded_object['name'] = ovk_proc_strtr($final_geo, 255);
$encoded = json_encode($encoded_object);
$this->stateChanges("geo", $encoded);
}
public function getLat(): ?float
{
return (float) $this->getRecord()->geo_lat ?? null;
}
public function getLon(): ?float
{
return (float) $this->getRecord()->geo_lon ?? null;
}
public function getVkApiGeo(): object
{
return (object) [
'type' => 'point',
'coordinates' => $this->getLat() . ',' . $this->getLon(),
'name' => $this->getGeo()->name,
];
}
} }

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\User;
@ -12,99 +16,132 @@ use Nette\Database\Table\Selection;
abstract class Postable extends Attachable abstract class Postable extends Attachable
{ {
use Traits\TAttachmentHost;
use Traits\TOwnable;
/** /**
* Column name, that references to an object, that * Column name, that references to an object, that
* is hieararchically higher than this Postable. * is hieararchically higher than this Postable.
* *
* For example: Images belong to User, but Posts belong to Wall. * For example: Images belong to User, but Posts belong to Wall.
* Formally users still own posts, but walls also own posts and they are * Formally users still own posts, but walls also own posts and they are
* their direct parent. * their direct parent.
* *
* @var string * @var string
*/ */
protected $upperNodeReferenceColumnName = "owner"; protected $upperNodeReferenceColumnName = "owner";
private function getTable(): Selection private function getTable(): Selection
{ {
return DB::i()->getContext()->table($this->tableName); return DB::i()->getContext()->table($this->tableName);
} }
function getOwner(bool $real = false): RowModel public function getOwner(bool $real = false): RowModel
{ {
$oid = (int) $this->getRecord()->owner; $oid = (int) $this->getRecord()->owner;
if(!$real && $this->isAnonymous()) if (!$real && $this->isAnonymous()) {
$oid = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"]; $oid = (int) OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"];
}
if($oid > 0)
return (new Users)->get($oid); $oid = abs($oid);
else if ($oid > 0) {
return (new Clubs)->get($oid * -1); return (new Users())->get($oid);
} else {
return (new Clubs())->get($oid * -1);
}
} }
function getVirtualId(): int public function getVirtualId(): int
{ {
return $this->getRecord()->virtual_id; return $this->getRecord()->virtual_id;
} }
function getPrettyId(): string public function getPrettyId(): string
{ {
return $this->getRecord()->owner . "_" . $this->getVirtualId(); return $this->getRecord()->owner . "_" . $this->getVirtualId();
} }
function getPublicationTime(): DateTime public function getPublicationTime(): DateTime
{ {
return new DateTime($this->getRecord()->created); return new DateTime($this->getRecord()->created);
} }
function getEditTime(): ?DateTime public function getEditTime(): ?DateTime
{ {
$edited = $this->getRecord()->edited; $edited = $this->getRecord()->edited;
if(is_null($edited)) return NULL; if (is_null($edited)) {
return null;
}
return new DateTime($edited); return new DateTime($edited);
} }
function getComments(int $page, ?int $perPage = NULL): \Traversable public function getComments(int $page, ?int $perPage = null): \Traversable
{ {
return (new Comments)->getCommentsByTarget($this, $page, $perPage); return (new Comments())->getCommentsByTarget($this, $page, $perPage);
}
function getCommentsCount(): int
{
return (new Comments)->getCommentsCountByTarget($this);
} }
function getLastComments(int $count): \Traversable public function getCommentsCount(): int
{ {
return (new Comments)->getLastCommentsByTarget($this, $count); return (new Comments())->getCommentsCountByTarget($this);
} }
function getLikesCount(): int public function getLastComments(int $count): \Traversable
{
return (new Comments())->getLastCommentsByTarget($this, $count);
}
public function getLikesCount(): int
{ {
return sizeof(DB::i()->getContext()->table("likes")->where([ return sizeof(DB::i()->getContext()->table("likes")->where([
"model" => static::class, "model" => static::class,
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
])->group("origin")); ])->group("origin"));
} }
# TODO add pagination public function getLikers(int $page = 1, ?int $perPage = null): \Traversable
function getLikers(): \Traversable
{ {
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
$sel = DB::i()->getContext()->table("likes")->where([ $sel = DB::i()->getContext()->table("likes")->where([
"model" => static::class, "model" => static::class,
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
]); ])->page($page, $perPage);
foreach($sel as $like) foreach ($sel as $like) {
yield (new Users)->get($like->origin); $user = (new Users())->get($like->origin);
if ($user->isPrivateLikes() && OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["enable"]) {
$user = (new Users())->get((int) OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["account"]);
}
yield $user;
}
} }
function isAnonymous(): bool public function getAccessKey(): string
{
return $this->getRecord()->access_key;
}
public function checkAccessKey(?string $access_key): bool
{
if ($this->getAccessKey() === $access_key) {
return true;
}
return !$this->isPrivate();
}
public function isPrivate(): bool
{
return (bool) $this->getRecord()->unlisted;
}
public function isAnonymous(): bool
{ {
return (bool) $this->getRecord()->anonymous; return (bool) $this->getRecord()->anonymous;
} }
function toggleLike(User $user): bool public function toggleLike(User $user): bool
{ {
$searchData = [ $searchData = [
"origin" => $user->getId(), "origin" => $user->getId(),
@ -112,7 +149,7 @@ abstract class Postable extends Attachable
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
]; ];
if(sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) { if (sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0) {
DB::i()->getContext()->table("likes")->where($searchData)->delete(); DB::i()->getContext()->table("likes")->where($searchData)->delete();
return false; return false;
} }
@ -121,7 +158,7 @@ abstract class Postable extends Attachable
return true; return true;
} }
function setLike(bool $liked, User $user): void public function setLike(bool $liked, User $user): void
{ {
$searchData = [ $searchData = [
"origin" => $user->getId(), "origin" => $user->getId(),
@ -129,50 +166,54 @@ abstract class Postable extends Attachable
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
]; ];
if($liked) if ($liked) {
DB::i()->getContext()->table("likes")->insert($searchData); if (!$this->hasLikeFrom($user)) {
else DB::i()->getContext()->table("likes")->insert($searchData);
DB::i()->getContext()->table("likes")->where($searchData)->delete(); }
} else {
if ($this->hasLikeFrom($user)) {
DB::i()->getContext()->table("likes")->where($searchData)->delete();
}
}
} }
function hasLikeFrom(User $user): bool public function hasLikeFrom(User $user): bool
{ {
$searchData = [ $searchData = [
"origin" => $user->getId(), "origin" => $user->getId(),
"model" => static::class, "model" => static::class,
"target" => $this->getRecord()->id, "target" => $this->getRecord()->id,
]; ];
return sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0; return sizeof(DB::i()->getContext()->table("likes")->where($searchData)) > 0;
} }
function setVirtual_Id(int $id): void public function setVirtual_Id(int $id): void
{ {
throw new ISE("Setting virtual id manually is forbidden"); throw new ISE("Setting virtual id manually is forbidden");
} }
function save(): void public function save(?bool $log = false): void
{ {
$vref = $this->upperNodeReferenceColumnName; $vref = $this->upperNodeReferenceColumnName;
$vid = $this->getRecord()->{$vref} ?? $this->changes[$vref]; $vid = $this->getRecord()->{$vref} ?? $this->changes[$vref];
if(!$vid) if (!$vid) {
throw new ISE("Can't presist post due to inability to calculate it's $vref post count. Have you set it?"); throw new ISE("Can't presist post due to inability to calculate it's $vref post count. Have you set it?");
$pCount = sizeof($this->getTable()->where($vref, $vid));
if(is_null($this->getRecord())) {
# lol allow ppl to taint created value
if(!isset($this->changes["created"]))
$this->stateChanges("created", time());
$this->stateChanges("virtual_id", $pCount + 1);
} else {
$this->stateChanges("edited", time());
} }
parent::save(); $pCount = sizeof($this->getTable()->where($vref, $vid));
if (is_null($this->getRecord())) {
# lol allow ppl to taint created value
if (!isset($this->changes["created"])) {
$this->stateChanges("created", time());
}
$this->stateChanges("virtual_id", $pCount + 1);
} /*else {
$this->stateChanges("edited", time());
}*/
parent::save($log);
} }
use Traits\TAttachmentHost;
use Traits\TOwnable;
} }

View file

@ -0,0 +1,177 @@
<?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime;
use Nette\Database\Table\ActiveRow;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\Club;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Repositories\{Applications, Comments, Notes, Reports, Audios, Documents, Users, Posts, Photos, Videos, Clubs};
use Chandler\Database\DatabaseConnection as DB;
use Nette\InvalidStateException as ISE;
use Nette\Database\Table\Selection;
class Report extends RowModel
{
protected $tableName = "reports";
public function getId(): int
{
return $this->getRecord()->id;
}
public function getStatus(): int
{
return $this->getRecord()->status;
}
public function getContentType(): string
{
return $this->getRecord()->type;
}
public function getReason(): string
{
return $this->getRecord()->reason;
}
public function getTime(): DateTime
{
return new DateTime($this->getRecord()->date);
}
public function isDeleted(): bool
{
if ($this->getRecord()->deleted === 0) {
return false;
} elseif ($this->getRecord()->deleted === 1) {
return true;
}
}
public function authorId(): int
{
return $this->getRecord()->user_id;
}
public function getUser(): User
{
return (new Users())->get((int) $this->getRecord()->user_id);
}
public function getContentId(): int
{
return (int) $this->getRecord()->target_id;
}
public function getContentObject()
{
if ($this->getContentType() == "post") {
return (new Posts())->get($this->getContentId());
} elseif ($this->getContentType() == "photo") {
return (new Photos())->get($this->getContentId());
} elseif ($this->getContentType() == "video") {
return (new Videos())->get($this->getContentId());
} elseif ($this->getContentType() == "group") {
return (new Clubs())->get($this->getContentId());
} elseif ($this->getContentType() == "comment") {
return (new Comments())->get($this->getContentId());
} elseif ($this->getContentType() == "note") {
return (new Notes())->get($this->getContentId());
} elseif ($this->getContentType() == "app") {
return (new Applications())->get($this->getContentId());
} elseif ($this->getContentType() == "user") {
return (new Users())->get($this->getContentId());
} elseif ($this->getContentType() == "audio") {
return (new Audios())->get($this->getContentId());
} elseif ($this->getContentType() == "doc") {
return (new Documents())->get($this->getContentId());
} else {
return null;
}
}
public function getAuthor(): RowModel
{
return $this->getContentObject()->getOwner();
}
public function getReportAuthor(): User
{
return (new Users())->get($this->getRecord()->user_id);
}
public function banUser($initiator)
{
$reason = $this->getContentType() !== "user" ? ("**content-" . $this->getContentType() . "-" . $this->getContentId() . "**") : ("Подозрительная активность");
$this->getAuthor()->ban($reason, false, time() + $this->getAuthor()->getNewBanTime(), $initiator);
}
public function deleteContent()
{
if ($this->getContentType() !== "user") {
$pubTime = $this->getContentObject()->getPublicationTime();
if (method_exists($this->getContentObject(), "getName")) {
$name = $this->getContentObject()->getName();
$placeholder = "$pubTime ($name)";
} else {
$placeholder = "$pubTime";
}
if ($this->getAuthor() instanceof Club) {
$name = $this->getAuthor()->getName();
$this->getAuthor()->getOwner()->adminNotify("Ваш контент, который опубликовали $placeholder в созданной вами группе \"$name\" был удалён модераторами инстанса. За повторные или серьёзные нарушения группу могут заблокировать.");
} else {
$this->getAuthor()->adminNotify("Ваш контент, который вы опубликовали $placeholder был удалён модераторами инстанса. За повторные или серьёзные нарушения вас могут заблокировать.");
}
$this->getContentObject()->delete($this->getContentType() !== "app");
}
$this->delete();
}
public function getDuplicates(): \Traversable
{
return (new Reports())->getDuplicates($this->getContentType(), $this->getContentId(), $this->getId());
}
public function getDuplicatesCount(): int
{
return count(iterator_to_array($this->getDuplicates()));
}
public function hasDuplicates(): bool
{
return $this->getDuplicatesCount() > 0;
}
public function getContentName(): string
{
$content_object = $this->getContentObject();
if (!$content_object) {
return 'unknown';
}
if (method_exists($content_object, "getCanonicalName")) {
return $content_object->getCanonicalName();
}
return $this->getContentType() . " #" . $this->getContentId();
}
public function delete(bool $softly = true): void
{
if ($this->hasDuplicates()) {
foreach ($this->getDuplicates() as $duplicate) {
$duplicate->setDeleted(1);
$duplicate->save();
}
}
$this->setDeleted(1);
$this->save();
}
}

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1); <?php
declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Models\Repositories\Users; use openvk\Web\Models\Repositories\Users;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
@ -7,33 +11,33 @@ class SupportAgent extends RowModel
{ {
protected $tableName = "support_names"; protected $tableName = "support_names";
function getAgentId(): int public function getAgentId(): int
{ {
return $this->getRecord()->agent; return $this->getRecord()->agent;
} }
function getName(): ?string public function getName(): ?string
{ {
return $this->getRecord()->name; return $this->getRecord()->name;
} }
function getCanonicalName(): string public function getCanonicalName(): string
{ {
return $this->getName(); return $this->getName();
} }
function getAvatarURL(): ?string public function getAvatarURL(): ?string
{ {
return $this->getRecord()->icon; return $this->getRecord()->icon;
} }
function isShowNumber(): int public function isShowNumber(): int
{ {
return $this->getRecord()->numerate; return $this->getRecord()->numerate;
} }
function getRealName(): string public function getRealName(): string
{ {
return (new Users)->get($this->getAgentId())->getCanonicalName(); return (new Users())->get($this->getAgentId())->getCanonicalName();
} }
} }

Some files were not shown because too many files have changed in this diff Show more