From d8118392fa3dedc70956014f4fba47e1dc06275f Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 12 Apr 2016 16:00:12 +0200 Subject: [PATCH 1/9] Added 'whois' command --- src/Commands/AdminCommands/ChatsCommand.php | 11 +- src/Commands/AdminCommands/WhoisCommand.php | 141 ++++++++++++++++++ .../SystemCommands/GenericCommand.php | 7 +- src/DB.php | 1 + 4 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 src/Commands/AdminCommands/WhoisCommand.php diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index d1174f02f..a2241109a 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -65,21 +65,26 @@ public function execute() $result['id'] = $result['chat_id']; $chat = new Chat($result); + $whois = ''; + if ($this->telegram->getCommandObject('whois')) { + $whois = ' [/whois' . str_replace('-', 'g', $chat->getId()).']'; //We can't use '-' in command because part of it will become unclickable + } + if ($chat->isPrivateChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false || strpos(strtolower($chat->getFirstName()), strtolower($text)) !== false || strpos(strtolower($chat->getLastName()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- P ' . $chat->tryMention() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- P ' . $chat->tryMention() . $whois . "\n"; } ++$user_chats; } elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- S ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- S ' . $chat->getTitle() . $whois . "\n"; } ++$super_group_chats; } elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- G ' . $chat->getTitle() . ' (' . $chat->getId() . ')' . "\n"; + $text_back .= '- G ' . $chat->getTitle() . $whois . "\n"; } ++$group_chats; diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php new file mode 100644 index 000000000..0b1233b5a --- /dev/null +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -0,0 +1,141 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Written by Jack'lul + */ + +namespace Longman\TelegramBot\Commands\AdminCommands; + +use Longman\TelegramBot\Commands\AdminCommand; +use Longman\TelegramBot\DB; +use Longman\TelegramBot\Entities\Chat; +use Longman\TelegramBot\Request; + +/** + * Admin "/whois" command + */ +class WhoisCommand extends AdminCommand +{ + /**#@+ + * {@inheritdoc} + */ + protected $name = 'whois'; + protected $description = 'Lookup user or group info'; + protected $usage = '/whois '; + protected $version = '1.0.0'; + protected $need_mysql = true; + /**#@-*/ + + /** + * {@inheritdoc} + */ + public function execute() + { + $message = $this->getMessage(); + + $chat_id = $message->getChat()->getId(); + $command = $message->getCommand(); + $text = trim($message->getText(true)); + + $data = [ 'chat_id' => $chat_id ]; + + //No point in replying to messages in private chats + if (!$message->getChat()->isPrivateChat()) { + $data['reply_to_message_id'] = $message->getMessageId(); + } + + if ($command !== 'whois') { + $text = substr($command, 5); + + //We need that '-' now, bring it back + if ((substr($text, 0, 1) == 'g')) { + $text = str_replace('g', '-', $text); + } + } + + if ($text === '') { + $text = 'Provide the id to lookup: /whois '; + } else { + $user_id = $text; + + $results = DB::selectChats( + true, //Send to groups (group chat) + true, //Send to supergroups (group chat) + true, //Send to users (single chat) + null, //'yyyy-mm-dd hh:mm:ss' date range from + null //'yyyy-mm-dd hh:mm:ss' date range to + ); + + foreach ($results as $result) { + if ($result['chat_id'] == $user_id) { + $result['id'] = $result['chat_id']; + $chat = new Chat($result); + + $created_at = $result['chat_created_at']; + $updated_at = $result['chat_updated_at']; + $old_id = $result['old_id']; + + break; + } + } + + if ($chat != null) { + if ($chat->isPrivateChat()) { + $text = 'User ID: ' . $user_id . "\n"; + $text .= 'Name: ' . $chat->getFirstName() . ' ' . $chat->getLastName() . "\n"; + + if ($chat->getUsername() != '') { + $text .= 'Username: @' . $chat->getUsername() . "\n"; + } + + $text .= 'First time seen: ' . $created_at . "\n"; + $text .= 'Last activity: ' . $updated_at . "\n"; + + //Code from Whoami command + $limit = 10; + $offset = null; + $ServerResponse = Request::getUserProfilePhotos([ + 'user_id' => $user_id , + 'limit' => $limit, + 'offset' => $offset, + ]); + + if ($ServerResponse->isOk()) { + $UserProfilePhoto = $ServerResponse->getResult(); + $totalcount = $UserProfilePhoto->getTotalCount(); + } else { + $totalcount = 0; + } + + if ($totalcount > 0) { + $photos = $UserProfilePhoto->getPhotos(); + $photo = $photos[0][2]; + $file_id = $photo->getFileId(); + + $data['photo'] = $file_id; + $data['caption'] = $text; + + return Request::sendPhoto($data); + } + } elseif ($chat->isGroupChat()) { + $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: '.$old_id.')' : ''). "\n"; + $text .= 'Type: ' . ucfirst($chat->getType()) . "\n"; + $text .= 'Title: ' . $chat->getTitle() . "\n"; + $text .= 'Bot added to group: ' . $created_at . "\n"; + $text .= 'Last activity: ' . $updated_at . "\n"; + } + } else { + $text = 'Chat not found!'; + } + } + + $data['text'] = $text; + return Request::sendMessage($data); + } +} diff --git a/src/Commands/SystemCommands/GenericCommand.php b/src/Commands/SystemCommands/GenericCommand.php index 245b20e63..369495d13 100644 --- a/src/Commands/SystemCommands/GenericCommand.php +++ b/src/Commands/SystemCommands/GenericCommand.php @@ -34,8 +34,13 @@ public function execute() $message = $this->getMessage(); //You can use $command as param - $command = $message->getCommand(); $chat_id = $message->getChat()->getId(); + $user_id = $message->getFrom()->getId(); + $command = $message->getCommand(); + + if (in_array($user_id, $this->telegram->getAdminList()) && strtolower(substr($command, 0, 5)) == 'whois') { + return $this->telegram->executeCommand('whois', $this->update); + } $data = [ 'chat_id' => $chat_id, diff --git a/src/DB.php b/src/DB.php index 08fdb80a6..66170cd97 100644 --- a/src/DB.php +++ b/src/DB.php @@ -715,6 +715,7 @@ public static function selectChats( try { $query = 'SELECT * , ' . TB_CHAT . '.`id` AS `chat_id`, + ' . TB_CHAT . '.`created_at` AS `chat_created_at`, ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`, ' . TB_USER . '.`id` AS `user_id` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '` From a6bc208e4eb432993ca4b78e24b4fe100f492b8b Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Tue, 12 Apr 2016 16:14:19 +0200 Subject: [PATCH 2/9] Id will be shown instead of whois shortcut when it is not available --- src/Commands/AdminCommands/ChatsCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index a2241109a..325daa2dd 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -65,26 +65,26 @@ public function execute() $result['id'] = $result['chat_id']; $chat = new Chat($result); - $whois = ''; + $whois = $chat->getId(); if ($this->telegram->getCommandObject('whois')) { - $whois = ' [/whois' . str_replace('-', 'g', $chat->getId()).']'; //We can't use '-' in command because part of it will become unclickable + $whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable } if ($chat->isPrivateChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false || strpos(strtolower($chat->getFirstName()), strtolower($text)) !== false || strpos(strtolower($chat->getLastName()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- P ' . $chat->tryMention() . $whois . "\n"; + $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n"; } ++$user_chats; } elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- S ' . $chat->getTitle() . $whois . "\n"; + $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } ++$super_group_chats; } elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { if ($text != '') { - $text_back .= '- G ' . $chat->getTitle() . $whois . "\n"; + $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } ++$group_chats; From 29f103296f3ff96dd9d393547fd786fa51c24c64 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Wed, 13 Apr 2016 10:49:18 +0200 Subject: [PATCH 3/9] Optimized 'chats' and 'whois' commands a bit --- src/Commands/AdminCommands/ChatsCommand.php | 10 ++++++---- src/Commands/AdminCommands/WhoisCommand.php | 22 ++++++++++----------- src/DB.php | 14 ++++++++++++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index 325daa2dd..9d98c69d2 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -45,7 +45,9 @@ public function execute() true, //Send to supergroups (single chat) true, //Send to users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from - null //'yyyy-mm-dd hh:mm:ss' date range to + null, //'yyyy-mm-dd hh:mm:ss' date range to + null, //Specific chat_id to select + ($text === '' || $text == '*') ? null : $text //Text to search in user/group name ); $user_chats = 0; @@ -70,19 +72,19 @@ public function execute() $whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable } - if ($chat->isPrivateChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false || strpos(strtolower($chat->getFirstName()), strtolower($text)) !== false || strpos(strtolower($chat->getLastName()), strtolower($text)) !== false)) { + if ($chat->isPrivateChat()) { if ($text != '') { $text_back .= '- P ' . $chat->tryMention() . ' [' . $whois . ']' . "\n"; } ++$user_chats; - } elseif ($chat->isSuperGroup() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { + } elseif ($chat->isSuperGroup()) { if ($text != '') { $text_back .= '- S ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } ++$super_group_chats; - } elseif ($chat->isGroupChat() && ($text === '' || $text == '*' || strpos(strtolower($chat->tryMention()), strtolower($text)) !== false)) { + } elseif ($chat->isGroupChat()) { if ($text != '') { $text_back .= '- G ' . $chat->getTitle() . ' [' . $whois . ']' . "\n"; } diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index 0b1233b5a..be296db6a 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -64,25 +64,23 @@ public function execute() } else { $user_id = $text; - $results = DB::selectChats( + $result = DB::selectChats( true, //Send to groups (group chat) true, //Send to supergroups (group chat) true, //Send to users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from - null //'yyyy-mm-dd hh:mm:ss' date range to + null, //'yyyy-mm-dd hh:mm:ss' date range to + $user_id, //Specific chat_id to select + null //Text to search in user/group name ); - foreach ($results as $result) { - if ($result['chat_id'] == $user_id) { - $result['id'] = $result['chat_id']; - $chat = new Chat($result); + if (is_array($result[0])) { + $result[0]['id'] = $result[0]['chat_id']; + $chat = new Chat($result[0]); - $created_at = $result['chat_created_at']; - $updated_at = $result['chat_updated_at']; - $old_id = $result['old_id']; - - break; - } + $created_at = $result[0]['chat_created_at']; + $updated_at = $result[0]['chat_updated_at']; + $old_id = $result[0]['old_id']; } if ($chat != null) { diff --git a/src/DB.php b/src/DB.php index 66170cd97..098696484 100644 --- a/src/DB.php +++ b/src/DB.php @@ -702,7 +702,9 @@ public static function selectChats( $select_super_groups = true, $select_users = true, $date_from = null, - $date_to = null + $date_to = null, + $chat_id = null, + $text = null ) { if (!self::isDbConnected()) { return false; @@ -748,6 +750,16 @@ public static function selectChats( $tokens[':date_to'] = $date_to; } + if (! is_null($chat_id)) { + $where[] = TB_CHAT . '.`id` = :chat_id'; + $tokens[':chat_id'] = $chat_id; + } + + if (! is_null($text)) { + $where[] = '(LOWER('.TB_CHAT . '.`title`) LIKE :text OR LOWER(' . TB_USER . '.`first_name`) LIKE :text OR LOWER(' . TB_USER . '.`last_name`) LIKE :text OR LOWER(' . TB_USER . '.`username`) LIKE :text)'; + $tokens[':text'] = '%'.strtolower($text).'%'; + } + $a = 0; foreach ($where as $part) { if ($a) { From eb5782c5409c4c00af8195812c440c42059af022 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Wed, 13 Apr 2016 13:35:03 +0200 Subject: [PATCH 4/9] updated incorrect comment text --- src/Commands/AdminCommands/ChatsCommand.php | 2 +- src/Commands/AdminCommands/WhoisCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index 9d98c69d2..b0b205ead 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -42,7 +42,7 @@ public function execute() $results = DB::selectChats( true, //Send to groups (group chat) - true, //Send to supergroups (single chat) + true, //Send to supergroups (super group chat) true, //Send to users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range to diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index be296db6a..22b4bc1f6 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -66,7 +66,7 @@ public function execute() $result = DB::selectChats( true, //Send to groups (group chat) - true, //Send to supergroups (group chat) + true, //Send to supergroups (super group chat) true, //Send to users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range to From d33df8b404ef2095a9aa846941b44904b32e62d6 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Wed, 13 Apr 2016 13:37:54 +0200 Subject: [PATCH 5/9] this is unnecessary --- src/Commands/AdminCommands/WhoisCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index 22b4bc1f6..70f38a20c 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -70,8 +70,7 @@ public function execute() true, //Send to users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range to - $user_id, //Specific chat_id to select - null //Text to search in user/group name + $user_id //Specific chat_id to select ); if (is_array($result[0])) { From 5c115ea3deea51cafaefc38225910a779a85b1d8 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Wed, 13 Apr 2016 23:01:04 +0200 Subject: [PATCH 6/9] Update comments --- src/Commands/AdminCommands/ChatsCommand.php | 6 +++--- src/Commands/AdminCommands/WhoisCommand.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Commands/AdminCommands/ChatsCommand.php b/src/Commands/AdminCommands/ChatsCommand.php index b0b205ead..3f3c0d13f 100644 --- a/src/Commands/AdminCommands/ChatsCommand.php +++ b/src/Commands/AdminCommands/ChatsCommand.php @@ -41,9 +41,9 @@ public function execute() $text = trim($message->getText(true)); $results = DB::selectChats( - true, //Send to groups (group chat) - true, //Send to supergroups (super group chat) - true, //Send to users (single chat) + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range to null, //Specific chat_id to select diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index 70f38a20c..25f54f3fc 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -65,9 +65,9 @@ public function execute() $user_id = $text; $result = DB::selectChats( - true, //Send to groups (group chat) - true, //Send to supergroups (super group chat) - true, //Send to users (single chat) + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) null, //'yyyy-mm-dd hh:mm:ss' date range from null, //'yyyy-mm-dd hh:mm:ss' date range to $user_id //Specific chat_id to select From 38fde49aa94f4c826631669e8975a5c5b3256fbe Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Wed, 13 Apr 2016 23:01:49 +0200 Subject: [PATCH 7/9] Noticed this issue, should be looked into --- src/DB.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DB.php b/src/DB.php index 098696484..4fad1892c 100644 --- a/src/DB.php +++ b/src/DB.php @@ -689,6 +689,8 @@ public static function insertMessageRequest(Message &$message) /** * Select Group and single Chats * + * @todo Seems to not return anything when $select_users = false + * * @param bool $select_groups * @param bool $select_super_groups * @param bool $select_users From c07375774608e9d404a5e355bd5b9b1ab857da65 Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Thu, 14 Apr 2016 23:02:22 +0200 Subject: [PATCH 8/9] Providing text as argument will try to find a unique match with user/group name --- src/Commands/AdminCommands/WhoisCommand.php | 56 ++++++++++++++------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index 25f54f3fc..d2b2ebf9e 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -27,8 +27,8 @@ class WhoisCommand extends AdminCommand */ protected $name = 'whois'; protected $description = 'Lookup user or group info'; - protected $usage = '/whois '; - protected $version = '1.0.0'; + protected $usage = '/whois or /whois '; + protected $version = '1.1.0'; protected $need_mysql = true; /**#@-*/ @@ -64,22 +64,40 @@ public function execute() } else { $user_id = $text; - $result = DB::selectChats( - true, //Select groups (group chat) - true, //Select supergroups (super group chat) - true, //Select users (single chat) - null, //'yyyy-mm-dd hh:mm:ss' date range from - null, //'yyyy-mm-dd hh:mm:ss' date range to - $user_id //Specific chat_id to select - ); - - if (is_array($result[0])) { - $result[0]['id'] = $result[0]['chat_id']; - $chat = new Chat($result[0]); - - $created_at = $result[0]['chat_created_at']; - $updated_at = $result[0]['chat_updated_at']; - $old_id = $result[0]['old_id']; + if (is_numeric($text)) { + $result = DB::selectChats( + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) + null, //'yyyy-mm-dd hh:mm:ss' date range from + null, //'yyyy-mm-dd hh:mm:ss' date range to + $user_id //Specific chat_id to select + ); + + $result = $result[0]; + } else { + $results = DB::selectChats( + true, //Select groups (group chat) + true, //Select supergroups (super group chat) + true, //Select users (single chat) + null, //'yyyy-mm-dd hh:mm:ss' date range from + null, //'yyyy-mm-dd hh:mm:ss' date range to + null, //Specific chat_id to select + $text //Text to search in user/group name + ); + + if (is_array($results) && count($results) == 1) + $result = $results[0]; + } + + if (is_array($result)) { + $result['id'] = $result['chat_id']; + $chat = new Chat($result); + + $user_id = $result['id']; + $created_at = $result['chat_created_at']; + $updated_at = $result['chat_updated_at']; + $old_id = $result['old_id']; } if ($chat != null) { @@ -127,6 +145,8 @@ public function execute() $text .= 'Bot added to group: ' . $created_at . "\n"; $text .= 'Last activity: ' . $updated_at . "\n"; } + } elseif (is_array($results) && count($results) > 1) { + $text = 'Multiple chats matched!'; } else { $text = 'Chat not found!'; } From 13d60c938f0fb2c0255859af91451e88d45e513e Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Thu, 14 Apr 2016 23:04:10 +0200 Subject: [PATCH 9/9] I forgot about it.... again... sorry! --- src/Commands/AdminCommands/WhoisCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Commands/AdminCommands/WhoisCommand.php b/src/Commands/AdminCommands/WhoisCommand.php index d2b2ebf9e..54c42e069 100644 --- a/src/Commands/AdminCommands/WhoisCommand.php +++ b/src/Commands/AdminCommands/WhoisCommand.php @@ -86,8 +86,9 @@ public function execute() $text //Text to search in user/group name ); - if (is_array($results) && count($results) == 1) + if (is_array($results) && count($results) == 1) { $result = $results[0]; + } } if (is_array($result)) {