Skip to content

Commit

Permalink
Commands update
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitmare committed Sep 20, 2017
1 parent 0cfb7aa commit 101ff45
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 21 deletions.
65 changes: 65 additions & 0 deletions Commands/getAuthkeyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\AdminCommands;
use Hitmare\UnlockPTB\Unlock;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Commands\AdminCommand;
use Longman\TelegramBot\Request;
/**
* User "/help" command
*/
class getAuthkeyCommand extends AdminCommand
{
/**
* @var string
*/
protected $name = 'getAuthkey';
/**
* @var string
*/
protected $description = 'Creates a Authkey to unlock the Bot';
/**
* @var string
*/
protected $usage = '/getAuthkey <channel-id>';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$key = trim($message->getText(true));
$isUnlocked = Unlock::isUnlocked($chat_id);




$key = Unlock::getAuthkey($chat_id);
$text = 'The Authkey for the Channel ' . $chat_id . ': ' . $key;


$data = [
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);



}
}
72 changes: 72 additions & 0 deletions Commands/lockCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Hitmare\UnlockPTB\Unlock;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
/**
* User "/help" command
*/
class lockCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'lock';
/**
* @var string
*/
protected $description = 'Locks the Bot';
/**
* @var string
*/
protected $usage = '/lock';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function execute()
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$key = trim($message->getText(true));
$isUnlocked = Unlock::isUnlocked($chat_id);


if (!$isUnlocked) {
$text='The Bot is allready Locked here';
}
else{
$unlock = Unlock::lockChannel($chat_id);
if (is_bool($unlock)) {
(!$unlock)?$text = 'Bot succsessfully locked':$text = 'Could not lock the Bot';
}
elseif(is_string($unlock)) {
$text = $unlock;
}

}
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);



}
}
42 changes: 21 additions & 21 deletions src/Unlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ private static function createRow($chat_id1) {
$sth->bindParam(':chat', $chat_id1, PDO::PARAM_INT);
$sth->bindParam(':status', $val, PDO::PARAM_INT);
$sth->execute();

return self::rowExist($chat_id1);


}
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
Expand Down Expand Up @@ -55,7 +55,7 @@ private static function rowExist($chat_id2) {
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}

}

/**
Expand All @@ -69,7 +69,7 @@ public static function isUnlocked($chat_id3) {
}
}
try {

$pdo = DB::getPdo();
$sql = 'SELECT `status` FROM `chat_unlock` WHERE `chat` = :chat';
$sth = $pdo->prepare($sql);
Expand All @@ -78,7 +78,7 @@ public static function isUnlocked($chat_id3) {
$row = $sth->fetch();

return boolval($row['status']);

}
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
Expand All @@ -95,31 +95,31 @@ public static function unlockChannel($chat_id4, $key) {
if(!self::createRow($chat_id4)) {
return FALSE;
}
}
}

try {
$val = 1;
$pdo = DB::getPdo();

$sql = 'SELECT `key` FROM `chat_unlock` WHERE `chat` = :chat';
$sth = $pdo->prepare($sql);
$sth->bindParam(':chat', $chat_id4, PDO::PARAM_INT);
$sth->execute();
$row = $sth->fetch();

if ($row['key'] == $key) {
$sql = 'UPDATE `chat_unlock` SET `status` = :status WHERE `chat` = :chat AND `key` = :key';
$sql = 'UPDATE `chat_unlock` SET `status` = :status WHERE `chat` = :chat AND `key` = :key';
$sth = $pdo->prepare($sql);
$sth->bindParam(':chat', $chat_id4, PDO::PARAM_INT);
$sth->bindParam(':status', $val, PDO::PARAM_INT);
$sth->execute();

return self::isUnlocked($chat_id4);
}
else {
return 'Wrong Key';
}

}
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
Expand All @@ -131,7 +131,7 @@ public static function unlockChannel($chat_id4, $key) {
* @return bool|string
*/
public static function lockChannel($chat_id5) {

try {
$val = 0;
$pdo = DB::getPdo();
Expand All @@ -140,15 +140,15 @@ public static function lockChannel($chat_id5) {
$sth->bindParam(':chat', $chat_id5, PDO::PARAM_INT);
$sth->bindParam(':status', $val, PDO::PARAM_INT);
$sth->execute();

return self::isUnlocked($chat_id5);


}
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
}
/**
* @param $chat_id6
* @return string
Expand All @@ -169,26 +169,26 @@ public static function getAuthkey($chat_id6) {
$sth->bindParam(':chat', $chat_id6, PDO::PARAM_INT);
$sth->bindParam(':authkey', $authkey, PDO::PARAM_STR);
$sth->execute();

// check if key is stored properly

$sql2 = 'SELECT `key` FROM `chat_unlock` WHERE `chat` = :chat';
$sth2 = $pdo->prepare($sql2);
$sth2->bindParam(':chat', $chat_id6, PDO::PARAM_INT);
$sth2->execute();
$row2 = $sth2->fetch();
// send key if the key is stored properly. Else send Error message
// send key if the key is stored properly. Else send Error message
if ($row2['key'] === $authkey) {
return $authkey;
}
else{
return 'Error creating the Auth Key.';
}
}
}
catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
}
}

?>

0 comments on commit 101ff45

Please sign in to comment.