From f44d37a204b1f0b0e6874c04893bbfc050a54fbd Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 21 Sep 2016 18:38:59 +0400 Subject: Final touches --- demo/components/class-application.php | 117 --------------------------------- demo/components/functions.php | 120 ++++++++++++++++++++++++++++++++++ demo/index.php | 2 +- src/bot.php | 33 +++++----- 4 files changed, 137 insertions(+), 135 deletions(-) delete mode 100644 demo/components/class-application.php create mode 100644 demo/components/functions.php diff --git a/demo/components/class-application.php b/demo/components/class-application.php deleted file mode 100644 index 317795d..0000000 --- a/demo/components/class-application.php +++ /dev/null @@ -1,117 +0,0 @@ -session($_COOKIE['BOT_SESSION']); - } else { // Open a new one - $session = $bot->session(); - SetCookie("BOT_SESSION", $session); - } - - // We issue results - if (isset($session)) { - return $session; - } else { - return 0; - } -} - -/** -* The function overrides the current ID to -* the specified or the next. If any missing ID -* returns the one. -* @param $id - Current ID. -*/ -function SetID($id) { - // Process the specified ID... - if (isset($id)) { - $id = $id + 1; - // ...Or are saved... - } elseif (isset($_COOKIE['CURRENT_ID'])) { - $id = (int)$_COOKIE['CURRENT_ID'] + 1; - // ...Or return one - } else { - $id = 1; - } - SetCookie('CURRENT_ID', $id, time()+300); // Save the result - return $id; // We issue results -} - -/** -* Cleaning function of the parameter that is passed to it. -* @param $type - Type cleansed. -*/ -function ClearingCache($type) { - switch ($type) { - // Clear the cookies - case 'cookies': - foreach ($_COOKIE as $key => $value) { - SetCookie($key, $value, time()-1000); - } - break; - // Clear the POST - case 'post': - $_POST = array(); - break; - // ... - default: - break; - } -} - -/** -* The function maintains a history of conversations, -* using a database of cookie files. -* @param $currentid - Message ID. -* @param $textarea - Message text. -* @param $type - Who said (Man / Bot). -*/ -function SavingStories($currentid, $textarea, $type) { - // We form an array with values - $cookie = array( - 'type' => $type, - 'textarea' => $textarea, - 'time' => time()); - // Save by encoding in JSON-string - SetCookie("TALK".$currentid, json_encode($cookie), time()+60); -} - -/** -* Function line formation. -* @param $id - Variable Message ID. -*/ -function ShowRecord($id) { - for ($i=0; $i < $id; $i++) { - if (isset($_COOKIE['TALK'.$i])) { - $cookie = json_decode($_COOKIE['TALK'.$i], true); - echo(""); - echo("".$cookie['type'].""); - echo("".$cookie['textarea'].""); - echo("".date("H:i:s", $cookie['time']).""); - echo(""); - } - } -} ?> diff --git a/demo/components/functions.php b/demo/components/functions.php new file mode 100644 index 0000000..e3f872a --- /dev/null +++ b/demo/components/functions.php @@ -0,0 +1,120 @@ +session($_COOKIE['BOT_SESSION']); + } else { // Open a new one + $session = $bot->session(); + SetCookie("BOT_SESSION", $session); + } + + // We issue results + if (isset($session)) { + return $session; + } else { + return 0; + } +} + +/** +* The function overrides the current ID to +* the specified or the next. If any missing ID +* returns the one. +* @param $id - Current ID. +* @return integer - Next ID. +*/ +function SetID($id) { + // Process the specified ID... + if (isset($id)) { + $id = $id + 1; + // ...Or are saved... + } elseif (isset($_COOKIE['CURRENT_ID'])) { + $id = (int)$_COOKIE['CURRENT_ID'] + 1; + // ...Or return one + } else { + $id = 1; + } + SetCookie('CURRENT_ID', $id, time()+300); // Save the result + return $id; // We issue results +} + +/** +* Cleaning function of the parameter that is passed to it. +* @param $type - Type cleansed. +*/ +function ClearingCache($type) { + switch ($type) { + // Clear the cookies + case 'cookies': + foreach ($_COOKIE as $key => $value) { + SetCookie($key, $value, time()-1000); + } + break; + // Clear the POST + case 'post': + $_POST = array(); + break; + // ... + default: + break; + } +} + +/** +* The function maintains a history of conversations, +* using a database of cookie files. +* @param $currentid - Message ID. +* @param $textarea - Message text. +* @param $type - Who said (Man / Bot). +*/ +function SavingStories($currentid, $textarea, $type) { + // We form an array with values + $cookie = array( + 'type' => $type, + 'textarea' => $textarea, + 'time' => time()); + // Save by encoding in JSON-string + SetCookie("TALK".$currentid, json_encode($cookie), time()+60); +} + +/** +* Function line formation. +* @param $id - Variable Message ID. +*/ +function ShowRecord($id) { + for ($i=0; $i < $id; $i++) { + if (isset($_COOKIE['TALK'.$i])) { + $cookie = json_decode($_COOKIE['TALK'.$i], true); + echo(""); + echo("".$cookie['type'].""); + echo("".$cookie['textarea'].""); + echo("".date("H:i:s", $cookie['time']).""); + echo(""); + } + } +} ?> diff --git a/demo/index.php b/demo/index.php index ec526d1..cb000c3 100644 --- a/demo/index.php +++ b/demo/index.php @@ -19,7 +19,7 @@ diff --git a/src/bot.php b/src/bot.php index c04bb1c..86042c5 100644 --- a/src/bot.php +++ b/src/bot.php @@ -5,15 +5,16 @@ private $salt = 'some very-very long string without any non-latin characters due to different string representations inside of variable programming languages'; /** - * @param $key - Ключ из урла после создания инфа + * @param $key - The session key of the row address. */ public function __construct($key) { $this->key = $key; } /** - * @param null $session - Идентификатор сессии существующей, если нет то создается новая - * @return string Идентификатор текущей сессии + * The function of creating the session. + * @param $session - Session ID. + * @return string - The ID of the current session. */ public function session($session = null) { if ($session === null) { @@ -22,14 +23,14 @@ } else { $this->session = $session; } - + // We issue results return $this->session; } /** - * ОТправить сообщение боту - * @param string $message Сообщение - * @return string Ответ + * Function send a message to the bot. + * @param $message - Message text. + * @return string - Returns a response from a bot. */ public function say($message) { $request = '["'.$this->session.'","'.$message.'"]'; @@ -42,41 +43,39 @@ )); $response = curl_exec($myCurl); curl_close($myCurl); - + // We issue results return $this->decode($response)->result->text->tts; } /** - * Кодирование сообщения - * @param $message - * @return string + * Encode message before sending it. + * @param $message - The response from the bot. + * @return string - A coded message. */ private function encode($message) { $message = base64_encode($message); $ml = strlen($message); $kl = strlen($this->salt); - $encoded = ""; for ($i = 0; $i < $ml; $i++) { $encoded = $encoded . ($message[$i] ^ $this->salt[$i % $kl]); } - + // We issue results return base64_encode($encoded); } /** - * Декодирование сообщения - * @param $message + * The function of decoding the received message. + * @param $message - The response from the bot. * @return mixed|null */ private function decode($message) { $msg = base64_decode($message); $ml = strlen($msg); $kl = strlen($this->salt); - $decoded = ""; for ($i = 0; $i < $ml; $i++) { $decoded.= ($msg[$i] ^ $this->salt[$i % $kl]); } - + // We issue results return json_decode(base64_decode($decoded)); } } -- cgit v1.2.3