2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Presenters;
|
2023-10-03 19:40:13 +03:00
|
|
|
use openvk\Web\Models\Repositories\{Posts, Comments};
|
2020-06-07 19:04:43 +03:00
|
|
|
use MessagePack\MessagePack;
|
2022-01-22 15:54:57 +03:00
|
|
|
use Chandler\Session\Session;
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
final class InternalAPIPresenter extends OpenVKPresenter
|
|
|
|
{
|
|
|
|
private function fail(int $code, string $message): void
|
|
|
|
{
|
|
|
|
header("HTTP/1.1 400 Bad Request");
|
|
|
|
exit(MessagePack::pack([
|
|
|
|
"brpc" => 1,
|
|
|
|
"error" => [
|
|
|
|
"code" => $code,
|
|
|
|
"message" => $message,
|
|
|
|
],
|
|
|
|
"id" => hexdec(hash("crc32b", (string) time())),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function succ($payload): void
|
|
|
|
{
|
|
|
|
exit(MessagePack::pack([
|
|
|
|
"brpc" => 1,
|
|
|
|
"result" => $payload,
|
|
|
|
"id" => hexdec(hash("crc32b", (string) time())),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderRoute(): void
|
|
|
|
{
|
2022-08-26 22:10:13 +03:00
|
|
|
if($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
|
|
header("HTTP/1.1 405 Method Not Allowed");
|
2020-06-07 19:04:43 +03:00
|
|
|
exit("ты дебил это точка апи");
|
2022-08-26 22:10:13 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
try {
|
|
|
|
$input = (object) MessagePack::unpack(file_get_contents("php://input"));
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->fail(-32700, "Parse error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_null($input->brpc ?? NULL) || is_null($input->method ?? NULL))
|
|
|
|
$this->fail(-32600, "Invalid BIN-RPC");
|
|
|
|
else if($input->brpc !== 1)
|
|
|
|
$this->fail(-32610, "Invalid version");
|
|
|
|
|
|
|
|
$method = explode(".", $input->method);
|
|
|
|
if(sizeof($method) !== 2)
|
|
|
|
$this->fail(-32601, "Procedure not found");
|
|
|
|
|
|
|
|
[$class, $method] = $method;
|
|
|
|
$class = '\openvk\ServiceAPI\\' . $class;
|
|
|
|
if(!class_exists($class))
|
|
|
|
$this->fail(-32601, "Procedure not found");
|
|
|
|
|
|
|
|
$handler = new $class(is_null($this->user) ? NULL : $this->user->identity);
|
|
|
|
if(!is_callable([$handler, $method]))
|
|
|
|
$this->fail(-32601, "Procedure not found");
|
|
|
|
|
|
|
|
try {
|
|
|
|
$params = array_merge($input->params ?? [], [function($data) {
|
|
|
|
$this->succ($data);
|
2021-10-15 23:05:27 +03:00
|
|
|
}, function(int $errno, string $errstr) {
|
|
|
|
$this->fail($errno, $errstr);
|
2020-06-07 19:04:43 +03:00
|
|
|
}]);
|
|
|
|
$handler->{$method}(...$params);
|
|
|
|
} catch(\TypeError $te) {
|
|
|
|
$this->fail(-32602, "Invalid params");
|
|
|
|
} catch(\Exception $ex) {
|
|
|
|
$this->fail(-32603, "Uncaught " . get_class($ex));
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 15:54:57 +03:00
|
|
|
|
|
|
|
function renderTimezone() {
|
2022-08-26 22:10:13 +03:00
|
|
|
if($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
|
|
header("HTTP/1.1 405 Method Not Allowed");
|
2022-01-22 15:54:57 +03:00
|
|
|
exit("ты дебил это метод апи");
|
2022-08-26 22:10:13 +03:00
|
|
|
}
|
2022-01-22 15:54:57 +03:00
|
|
|
$sessionOffset = Session::i()->get("_timezoneOffset");
|
2022-01-22 16:12:01 +03:00
|
|
|
if(is_numeric($this->postParam("timezone", false))) {
|
|
|
|
$postTZ = intval($this->postParam("timezone", false));
|
2022-01-22 15:54:57 +03:00
|
|
|
if ($postTZ != $sessionOffset || $sessionOffset == null) {
|
|
|
|
Session::i()->set("_timezoneOffset", $postTZ ? $postTZ : 3 * MINUTE );
|
|
|
|
$this->returnJson([
|
2022-05-08 13:06:26 +03:00
|
|
|
"success" => 1 # If it's new value
|
2022-01-22 15:54:57 +03:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->returnJson([
|
2022-05-08 13:06:26 +03:00
|
|
|
"success" => 2 # If it's the same value (if for some reason server will call this func)
|
2022-01-22 15:54:57 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->returnJson([
|
|
|
|
"success" => 0
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 19:40:13 +03:00
|
|
|
|
|
|
|
function renderGetPhotosFromPost(int $owner_id, int $post_id) {
|
|
|
|
if($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
|
|
header("HTTP/1.1 405 Method Not Allowed");
|
|
|
|
exit("иди нахуй заебал");
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->postParam("parentType", false) == "post") {
|
2023-11-16 19:44:12 +03:00
|
|
|
$post = (new Posts)->getPostById($owner_id, $post_id, true);
|
2023-10-03 19:40:13 +03:00
|
|
|
} else {
|
|
|
|
$post = (new Comments)->get($post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(is_null($post)) {
|
|
|
|
$this->returnJson([
|
|
|
|
"success" => 0
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$response = [];
|
|
|
|
$attachments = $post->getChildren();
|
|
|
|
foreach($attachments as $attachment)
|
|
|
|
{
|
|
|
|
if($attachment instanceof \openvk\Web\Models\Entities\Photo)
|
|
|
|
{
|
2024-11-17 13:03:49 +03:00
|
|
|
$response[$attachment->getPrettyId()] = [
|
|
|
|
"url" => $attachment->getURLBySizeId('larger'),
|
|
|
|
"id" => $attachment->getPrettyId(),
|
2023-10-03 19:40:13 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->returnJson([
|
|
|
|
"success" => 1,
|
|
|
|
"body" => $response
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-11-17 13:03:49 +03:00
|
|
|
|
|
|
|
function renderGetPostTemplate(int $owner_id, int $post_id) {
|
|
|
|
if($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
|
|
header("HTTP/1.1 405 Method Not Allowed");
|
|
|
|
exit("ты не по адресу");
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $this->queryParam("type", false);
|
|
|
|
if($type == "post") {
|
|
|
|
$post = (new Posts)->getPostById($owner_id, $post_id, true);
|
|
|
|
} else {
|
|
|
|
$post = (new Comments)->get($post_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$post || !$post->canBeEditedBy($this->user->identity)) {
|
|
|
|
exit('');
|
|
|
|
}
|
|
|
|
|
|
|
|
header("Content-Type: text/plain");
|
|
|
|
|
|
|
|
if($type == 'post') {
|
|
|
|
$this->template->_template = 'components/post.xml';
|
|
|
|
$this->template->post = $post;
|
|
|
|
$this->template->commentSection = false;
|
|
|
|
} elseif($type == 'comment') {
|
|
|
|
$this->template->_template = 'components/comment.xml';
|
|
|
|
$this->template->comment = $post;
|
|
|
|
} else {
|
|
|
|
exit('');
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 23:05:27 +03:00
|
|
|
}
|