diff --git a/Auth.php b/Auth.php index 08e7167..d039da9 100644 --- a/Auth.php +++ b/Auth.php @@ -1,4 +1,5 @@ cookie)) { $send_cookie = []; @@ -170,7 +171,9 @@ class Auth curl_setopt($curl, CURLOPT_COOKIE, join('; ', $send_cookie)); } - curl_setopt($curl, CURLOPT_HEADERFUNCTION, + curl_setopt( + $curl, + CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) { $len = strlen($header); $header = explode(':', $header, 2); diff --git a/composer.json b/composer.json index 5aa4b75..9f1f072 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "Набор классов для удобной работы с VK API. Форк SimpleVK от команды hood.su с переработкой под PSR-4 и оптимизацией кода", "keywords": ["vk","hood", "SimpleVK"], "homepage": "https://git.hood.su/vk", - "license": "GNU AGPLv3", + "license": "AGPL-3.0-or-later", "authors": [ { "name": "Arsen Mirzaev", @@ -18,7 +18,7 @@ }, "autoload": { "psr-4": { - "VK": "src" + "VK\\": "src" } } } \ No newline at end of file diff --git a/vk_api.php b/vk_api.php index 85db65e..769d9ad 100644 --- a/vk_api.php +++ b/vk_api.php @@ -11,7 +11,8 @@ require_once('config_library.php'); * Class vk_api * @package vk_api */ -class vk_api { +class vk_api +{ /** * @var string @@ -53,7 +54,8 @@ class vk_api { * @param null $also_version * @throws VkApiException */ - public function __construct($token, $version, $also_version = null) { + public function __construct($token, $version, $also_version = null) + { if ($token instanceof auth) { $this->auth = $token; $this->version = $version; @@ -77,7 +79,8 @@ class vk_api { * * @throws VkApiException */ - public static function create($token, $version, $also_version = null) { + public static function create($token, $version, $also_version = null) + { return new self($token, $version, $also_version); } @@ -85,7 +88,8 @@ class vk_api { * @param $str * @return vk_api */ - public function setConfirm($str) { + public function setConfirm($str) + { if (isset($this->data->type) && $this->data->type == 'confirmation') { //Если vk запрашивает ключ exit($str); //Завершаем скрипт отправкой ключа } @@ -101,13 +105,14 @@ class vk_api { * @param null $data * @return array|mixed|null */ - public function initVars(&$id = null, &$message = null, &$payload = null, &$user_id = null, &$type = null, &$data = null) { + public function initVars(&$id = null, &$message = null, &$payload = null, &$user_id = null, &$type = null, &$data = null) + { if (!$this->debug_mode) $this->sendOK(); $data = $this->data; $data_backup = $this->data; $type = isset($data->type) ? $data->type : null; - if(isset($data->object->message) and $type == 'message_new') { + if (isset($data->object->message) and $type == 'message_new') { $data->object = $data->object->message; //какая-то дичь с ссылками, но $this->data теперь тоже переопределился } $id = isset($data->object->peer_id) ? $data->object->peer_id : null; @@ -121,7 +126,8 @@ class vk_api { /** * @return bool */ - protected function sendOK() { + protected function sendOK() + { set_time_limit(0); ini_set('display_errors', 'Off'); @@ -151,7 +157,8 @@ class vk_api { * @return bool|mixed * @throws VkApiException */ - public function reply($message, $params = []) { + public function reply($message, $params = []) + { if ($this->data != []) { $message = $this->placeholders($this->data->object->peer_id, $message); return $this->request('messages.send', ['message' => $message, 'peer_id' => $this->data->object->peer_id] + $params); @@ -160,12 +167,14 @@ class vk_api { } } - public function forward($id, $id_messages, $params = []) { + public function forward($id, $id_messages, $params = []) + { $forward_messages = (is_array($id_messages)) ? join(',', $id_messages) : $id_messages; return $this->request('messages.send', ['peer_id' => $id, 'forward_messages' => $forward_messages] + $params); } - public function sendAllChats($message, $params = []) { + public function sendAllChats($message, $params = []) + { unset($this->request_ignore_error[array_search(10, $this->request_ignore_error)]); //убираем код 10 из исключений $i = 0; $count = 0; @@ -184,11 +193,12 @@ class vk_api { } } - protected function placeholders($id, $message) { - if($id >= 2000000000) { + protected function placeholders($id, $message) + { + if ($id >= 2000000000) { $id = isset($this->data->object->from_id) ? $this->data->object->from_id : null; } - if($id == null) { + if ($id == null) { print "Попытка использовать заполнители при передаче id беседы"; return $message; } else { @@ -210,7 +220,8 @@ class vk_api { * @return bool|mixed * @throws VkApiException */ - public function request($method, $params = []) { + public function request($method, $params = []) + { list($method, $params) = $this->editRequestParams($method, $params); $url = 'https://api.vk.com/method/' . $method; $params['access_token'] = $this->token; @@ -224,8 +235,7 @@ class vk_api { if (in_array($e->getCode(), $this->request_ignore_error)) { sleep(1); continue; - } - else + } else throw new VkApiException($e->getMessage(), $e->getCode()); } } @@ -237,7 +247,8 @@ class vk_api { * @param $params * @return array */ - protected function editRequestParams($method, $params) { + protected function editRequestParams($method, $params) + { return [$method, $params]; } @@ -247,7 +258,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function request_core($url, $params = []) { + private function request_core($url, $params = []) + { if (function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, [ @@ -287,17 +299,18 @@ class vk_api { * @param array $params * @throws VkApiException */ - public function sendAllDialogs($message, $keyboard = null, $filter = 'all', $params = []) { + public function sendAllDialogs($message, $keyboard = null, $filter = 'all', $params = []) + { $ids = []; for ($count_all = 1, $offset = 0; $offset <= $count_all; $offset += 200) { - $members = $this->request('messages.getConversations', ['count' => 200, 'offset' => $offset, 'filter' => $filter]);//'filter' => 'unread' + $members = $this->request('messages.getConversations', ['count' => 200, 'offset' => $offset, 'filter' => $filter]); //'filter' => 'unread' if ($count_all != 1) $offset += $members['count'] - $count_all; $count_all = $members['count']; foreach ($members["items"] as $user) if ($user['conversation']["can_write"]["allowed"] == true) - $ids [] = $user['conversation']['peer']['id']; + $ids[] = $user['conversation']['peer']['id']; } $ids = array_chunk($ids, 100); foreach ($ids as $ids_chunk) { @@ -315,7 +328,8 @@ class vk_api { * @return string * @throws VkApiException */ - public function getAlias($id, $n = null) { //получить обращение к юзеру или группе + public function getAlias($id, $n = null) + { //получить обращение к юзеру или группе if (!is_numeric($id)) { //если короткая ссылка $obj = $this->request('utils.resolveScreenName', ['screen_name' => $id]); //узнаем, кому принадлежит, сообществу или юзеру $id = ($obj["type"] == 'group') ? -$obj['object_id'] : $obj['object_id']; @@ -353,7 +367,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function userInfo($user_url = '', $scope = []) { + public function userInfo($user_url = '', $scope = []) + { $scope = ["fields" => join(",", $scope)]; if (isset($user_url)) { $user_url = preg_replace("!.*?/!", '', $user_url); @@ -373,7 +388,8 @@ class vk_api { * @return bool|null|string * @throws VkApiException */ - public function isAdmin($user_id, $chat_id) { //возвращает привелегию по id + public function isAdmin($user_id, $chat_id) + { //возвращает привелегию по id try { $members = $this->request('messages.getConversationMembers', ['peer_id' => $chat_id])['items']; } catch (\Exception $e) { @@ -393,7 +409,8 @@ class vk_api { * @return bool|mixed * @throws VkApiException */ - public function sendMessage($id, $message, $params = []) { + public function sendMessage($id, $message, $params = []) + { if ($id < 1) return 0; $message = $this->placeholders($id, $message); @@ -403,7 +420,8 @@ class vk_api { /** * */ - public function debug() { + public function debug() + { ini_set('error_reporting', E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); @@ -421,37 +439,45 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function sendButton($id, $message, $buttons = [], $inline = false, $one_time = False, $params = []) { + public function sendButton($id, $message, $buttons = [], $inline = false, $one_time = False, $params = []) + { $keyboard = $this->generateKeyboard($buttons, $inline, $one_time); $message = $this->placeholders($id, $message); return $this->request('messages.send', ['message' => $message, 'peer_id' => $id, 'keyboard' => $keyboard] + $params); } - public function buttonLocation($payload = null) { + public function buttonLocation($payload = null) + { return ['location', $payload]; } - public function buttonPayToGroup($group_id, $amount, $description = null, $data = null, $payload = null) { + public function buttonPayToGroup($group_id, $amount, $description = null, $data = null, $payload = null) + { return ['vkpay', $payload, 'pay-to-group', $group_id, $amount, $description, $data]; } - public function buttonPayToUser($user_id, $amount, $description = null, $payload = null) { + public function buttonPayToUser($user_id, $amount, $description = null, $payload = null) + { return ['vkpay', $payload, 'pay-to-user', $user_id, $amount, $description]; } - public function buttonDonateToGroup($group_id, $payload = null) { + public function buttonDonateToGroup($group_id, $payload = null) + { return ['vkpay', $payload, 'transfer-to-group', $group_id]; } - public function buttonDonateToUser($user_id, $payload = null) { + public function buttonDonateToUser($user_id, $payload = null) + { return ['vkpay', $payload, 'transfer-to-user', $user_id]; } - public function buttonApp($text, $app_id, $owner_id = null, $hash = null, $payload = null) { + public function buttonApp($text, $app_id, $owner_id = null, $hash = null, $payload = null) + { return ['open_app', $payload, $text, $app_id, $owner_id, $hash]; } - public function buttonText($text, $color, $payload = null) { + public function buttonText($text, $color, $payload = null) + { return ['text', $payload, $text, $color]; } @@ -461,7 +487,8 @@ class vk_api { * @param bool $one_time * @return array|false|string */ - public function generateKeyboard($buttons = [], $inline = false, $one_time = False) { + public function generateKeyboard($buttons = [], $inline = false, $one_time = False) + { $keyboard = []; $i = 0; foreach ($buttons as $button_str) { @@ -472,29 +499,29 @@ class vk_api { $keyboard[$i][$j]["action"]["payload"] = json_encode($button[1], JSON_UNESCAPED_UNICODE); switch ($button[0]) { case 'text': { - $color = $this->replaceColor($button[3]); - $keyboard[$i][$j]["color"] = $color; - $keyboard[$i][$j]["action"]["label"] = $button[2]; - break; - } + $color = $this->replaceColor($button[3]); + $keyboard[$i][$j]["color"] = $color; + $keyboard[$i][$j]["action"]["label"] = $button[2]; + break; + } case 'vkpay': { - $keyboard[$i][$j]["action"]["hash"] = "action={$button[2]}"; - $keyboard[$i][$j]["action"]["hash"] .= ($button[3] < 0) ? "&group_id=".$button[3]*-1 : "&user_id={$button[3]}"; - $keyboard[$i][$j]["action"]["hash"] .= (isset($button[4])) ? "&amount={$button[4]}" : ''; - $keyboard[$i][$j]["action"]["hash"] .= (isset($button[5])) ? "&description={$button[5]}" : ''; - $keyboard[$i][$j]["action"]["hash"] .= (isset($button[6])) ? "&data={$button[6]}" : ''; - $keyboard[$i][$j]["action"]["hash"] .= "&aid=1"; - break; - } + $keyboard[$i][$j]["action"]["hash"] = "action={$button[2]}"; + $keyboard[$i][$j]["action"]["hash"] .= ($button[3] < 0) ? "&group_id=" . $button[3] * -1 : "&user_id={$button[3]}"; + $keyboard[$i][$j]["action"]["hash"] .= (isset($button[4])) ? "&amount={$button[4]}" : ''; + $keyboard[$i][$j]["action"]["hash"] .= (isset($button[5])) ? "&description={$button[5]}" : ''; + $keyboard[$i][$j]["action"]["hash"] .= (isset($button[6])) ? "&data={$button[6]}" : ''; + $keyboard[$i][$j]["action"]["hash"] .= "&aid=1"; + break; + } case 'open_app': { - $keyboard[$i][$j]["action"]["label"] = $button[2]; - $keyboard[$i][$j]["action"]["app_id"] = $button[3]; - if(isset($button[4])) - $keyboard[$i][$j]["action"]["owner_id"] = $button[4]; - if(isset($button[5])) - $keyboard[$i][$j]["action"]["hash"] = $button[5]; - break; - } + $keyboard[$i][$j]["action"]["label"] = $button[2]; + $keyboard[$i][$j]["action"]["app_id"] = $button[3]; + if (isset($button[4])) + $keyboard[$i][$j]["action"]["owner_id"] = $button[4]; + if (isset($button[5])) + $keyboard[$i][$j]["action"]["hash"] = $button[5]; + break; + } } $j++; } @@ -509,7 +536,8 @@ class vk_api { * @param $color * @return string */ - private function replaceColor($color) { + private function replaceColor($color) + { switch ($color) { case 'red': $color = 'negative'; @@ -532,7 +560,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function groupInfo($group_url) { + public function groupInfo($group_url) + { $group_url = preg_replace("!.*?/!", '', $group_url); return current($this->request('groups.getById', ["group_ids" => $group_url])); } @@ -544,7 +573,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function sendImage($id, $local_file_path, $params = []) { + public function sendImage($id, $local_file_path, $params = []) + { $upload_file = $this->uploadImage($id, $local_file_path); return $this->request('messages.send', ['attachment' => "photo" . $upload_file[0]['owner_id'] . "_" . $upload_file[0]['id'], 'peer_id' => $id] + $params); } @@ -555,7 +585,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function uploadImage($id, $local_file_path) { + private function uploadImage($id, $local_file_path) + { $upload_url = $this->getUploadServerMessages($id, 'photo')['upload_url']; for ($i = 0; $i < $this->try_count_resend_file; ++$i) { try { @@ -578,7 +609,8 @@ class vk_api { * @return mixed|null * @throws VkApiException */ - private function getUploadServerMessages($peer_id, $selector = 'doc') { + private function getUploadServerMessages($peer_id, $selector = 'doc') + { $result = null; if ($selector == 'doc') $result = $this->request('docs.getMessagesUploadServer', ['type' => 'doc', 'peer_id' => $peer_id]); @@ -589,13 +621,15 @@ class vk_api { return $result; } - private function uploadVoice($id, $local_file_path) { + private function uploadVoice($id, $local_file_path) + { $upload_url = $this->getUploadServerMessages($id, 'audio_message')['upload_url']; $answer_vk = json_decode($this->sendFiles($upload_url, $local_file_path, 'file'), true); return $this->saveDocuments($answer_vk['file'], 'voice'); } - public function sendVoice($id, $local_file_path, $params = []) { + public function sendVoice($id, $local_file_path, $params = []) + { $upload_file = $this->uploadVoice($id, $local_file_path); return $this->request('messages.send', ['attachment' => "doc" . $upload_file['audio_message']['owner_id'] . "_" . $upload_file['audio_message']['id'], 'peer_id' => $id] + $params); } @@ -607,7 +641,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - protected function sendFiles($url, $local_file_path, $type = 'file') { + protected function sendFiles($url, $local_file_path, $type = 'file') + { $post_fields = [ $type => new CURLFile(realpath($local_file_path)) ]; @@ -639,7 +674,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function savePhoto($photo, $server, $hash) { + private function savePhoto($photo, $server, $hash) + { return $this->request('photos.saveMessagesPhoto', ['photo' => $photo, 'server' => $server, 'hash' => $hash]); } @@ -651,7 +687,8 @@ class vk_api { * * @throws VkApiException */ - public function uploadDocsGroup($groupID, $local_file_path, $title = null) { + public function uploadDocsGroup($groupID, $local_file_path, $title = null) + { return $this->uploadDocs($groupID, $local_file_path, $title); } @@ -662,7 +699,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function uploadDocs($id, $local_file_path, $title = null) { + private function uploadDocs($id, $local_file_path, $title = null) + { if (!isset($title)) $title = preg_replace("!.*?/!", '', $local_file_path); $upload_url = $this->getUploadServerPost($id)['upload_url']; @@ -676,7 +714,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function getUploadServerPost($peer_id = []) { + private function getUploadServerPost($peer_id = []) + { if ($peer_id < 0) $peer_id = ['group_id' => $peer_id * -1]; else @@ -691,7 +730,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function saveDocuments($file, $title) { + private function saveDocuments($file, $title) + { return $this->request('docs.save', ['file' => $file, 'title' => $title]); } @@ -703,7 +743,8 @@ class vk_api { * @return bool|mixed * @throws VkApiException */ - public function sendDocMessage($id, $local_file_path, $title = null, $params = []) { + public function sendDocMessage($id, $local_file_path, $title = null, $params = []) + { $upload_file = current($this->uploadDocsMessages($id, $local_file_path, $title)); if ($id != 0 and $id != '0') { return $this->request('messages.send', ['attachment' => "doc" . $upload_file['owner_id'] . "_" . $upload_file['id'], 'peer_id' => $id] + $params); @@ -719,7 +760,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function uploadDocsMessages($id, $local_file_path, $title = null) { + private function uploadDocsMessages($id, $local_file_path, $title = null) + { if (!isset($title)) $title = preg_replace("!.*?/!", '', $local_file_path); $upload_url = $this->getUploadServerMessages($id)['upload_url']; @@ -736,7 +778,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function createPost($id, $message = [], $props = [], $media = []) { + public function createPost($id, $message = [], $props = [], $media = []) + { $send_attachment = []; foreach ($media as $selector => $massive) { @@ -789,7 +832,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function sendWallComment($owner_id, $post_id, $message) { + public function sendWallComment($owner_id, $post_id, $message) + { return $this->request('wall.createComment', ['owner_id' => $owner_id, 'post_id' => $post_id, 'message' => $message]); } @@ -798,7 +842,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function getWallUploadServer($id) { + private function getWallUploadServer($id) + { if ($id < 0) { $id *= -1; return $this->request('photos.getWallUploadServer', ['group_id' => $id]); @@ -815,7 +860,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - private function savePhotoWall($photo, $server, $hash, $id) { + private function savePhotoWall($photo, $server, $hash, $id) + { if ($id < 0) { $id *= -1; return $this->request('photos.saveWallPhoto', ['photo' => $photo, 'server' => $server, 'hash' => $hash, 'group_id' => $id]); @@ -831,7 +877,8 @@ class vk_api { * * @throws VkApiException */ - public function uploadDocsUser($local_file_path, $title = null) { + public function uploadDocsUser($local_file_path, $title = null) + { return $this->uploadDocs([], $local_file_path, $title); } @@ -844,7 +891,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function createMessages($id, $message = [], $props = [], $media = [], $keyboard = []) { + public function createMessages($id, $message = [], $props = [], $media = [], $keyboard = []) + { if ($id < 1) return 0; $send_attachment = []; @@ -893,7 +941,8 @@ class vk_api { * @return mixed * @throws VkApiException */ - public function getGroupsUser($id = [], $extended = 1, $props = []) { + public function getGroupsUser($id = [], $extended = 1, $props = []) + { if (is_numeric($id)) $id = ['user_id' => $id]; if (!is_array($props)) @@ -909,7 +958,8 @@ class vk_api { * @param $var * @throws VkApiException */ - public function setTryCountResendFile($var) { + public function setTryCountResendFile($var) + { if (is_integer($var)) $this->try_count_resend_file = $var; else @@ -920,7 +970,8 @@ class vk_api { * @param $var * @throws VkApiException */ - public function setRequestIgnoreError($var) { + public function setRequestIgnoreError($var) + { if (is_array($var)) $this->request_ignore_error = $var; else if (is_integer($var)) @@ -933,7 +984,8 @@ class vk_api { * @param $id * @return mixed */ - public function dateRegistration($id) { + public function dateRegistration($id) + { $site = file_get_contents("https://vk.com/foaf.php?id={$id}"); preg_match('', $site, $data); $data = explode('T', $data[1]); @@ -945,14 +997,16 @@ class vk_api { /** * @return array */ - protected function copyAllDataclass() { + protected function copyAllDataclass() + { return [$this->token, $this->version, $this->auth, $this->request_ignore_error, $this->try_count_resend_file]; } /** * @param $id_vk_vars */ - protected function setAllDataclass($id_vk_vars) { + protected function setAllDataclass($id_vk_vars) + { list($this->token, $this->version, $this->auth, $this->request_ignore_error, $this->try_count_resend_file) = $id_vk_vars; } }