Skip to content

Commit

Permalink
v1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
skrtdev committed Sep 14, 2020
1 parent 29e377e commit adbecbd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
23 changes: 19 additions & 4 deletions src/novagram/bot_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ class Bot {
private array $json;
private bool $payloaded = false;

public \Telegram\Update $update; // write-only
public array $raw_update; // write-only
public \Telegram\Update $update; // read-only
public array $raw_update; // read-only
public int $id; // read-only


public function __construct(string $token, array $settings = []) {
if(!Utils::isTokenValid($token)){
throw new Exception("Not a valid Telegram Bot Token provided ($token)");
}
$this->token = $token;
$this->id = Utils::getIDByToken($token);
$this->settings = (object) $settings;


Expand Down Expand Up @@ -64,8 +70,7 @@ public function APICall(string $method, array $data, bool $payload = false, bool
if($payload){
if(!$this->payloaded){
$this->payloaded = true;
$data['method'] = $method;
echo json_encode($data);
echo json_encode($data + ['method' => $method]);
return true;
}
else{
Expand Down Expand Up @@ -170,6 +175,16 @@ public function createObject(string $type, array $json){
return new $obj($type, $json, $this);
}

public function debug($value){
if($this->settings->debug){
return $this->sendMessage([
"chat_id" => $this->settings->debug,
"text" => "<pre>".htmlspecialchars(print_r($value, true))."</pre>",
]);
}
else throw new Exception("debug chat id is not set");
}

public function getJSON(): array{
return $this->json;
}
Expand Down
24 changes: 15 additions & 9 deletions src/novagram/db.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Database{
private PDO $PDO;
private string $prefix;

public function __construct(array $settings, string $prefix = "novagram"){
public function __construct(array $settings, ?string $prefix = "novagram"){

$settings_array = [
"driver" => "mysql",
Expand Down Expand Up @@ -41,7 +41,7 @@ public function __construct(array $settings, string $prefix = "novagram"){
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
# PDO::ATTR_EMULATE_PREPARES => false,
];

$this->PDO = new \PDO("$driver:$connection", $dbuser, $dbpass, $options);
Expand Down Expand Up @@ -76,15 +76,15 @@ public function initializeQueries(): void{

private function initializeDatabase(): void{
$this->query("CREATE TABLE IF NOT EXISTS {$this->tableNames['users']} (
id INTEGER PRIMARY KEY,
user_id INTEGER UNIQUE
id INTEGER PRIMARY KEY AUTO_INCREMENT,
user_id BIGINT(255) UNIQUE
)");
}

public function initializeConversations(): void{
$this->query("CREATE TABLE IF NOT EXISTS {$this->tableNames['conversations']} (
id INTEGER PRIMARY KEY,
chat_id INTEGER UNIQUE,
id INTEGER PRIMARY KEY AUTO_INCREMENT,
chat_id BIGINT(255),
name VARCHAR(64) NOT NULL,
value VARCHAR(64) NOT NULL,
additional_param VARCHAR(256) NOT NULL
Expand All @@ -111,14 +111,15 @@ public function getConversation(int $chat_id, string $name, ?\Telegram\Update $u
':name' => $name,
])->fetch();

if($row === false) return;

$value = $row['value'];
$additional_param = unserialize($row['additional_param']);


$is_permanent = $additional_param['is_permanent'];
unset($additional_param['is_permanent']);

if($name === "status"){
$is_permanent = $additional_param['is_permanent'];
unset($additional_param['is_permanent']);
if(!empty($additional_param)){
//var_dump($update);
//if(!isset($update) || !isset($update->message->text)){
Expand Down Expand Up @@ -147,6 +148,7 @@ public function getConversation(int $chat_id, string $name, ?\Telegram\Update $u
var_dump($additional_param);
#['value'];
if(!$is_permanent){
trigger_error("$chat_id->$name has to be deleted (is_permanent: $is_permanent)");
$this->deleteConversation($chat_id, $name);
}
return $value;
Expand Down Expand Up @@ -175,6 +177,10 @@ public function existQuery(string $query, array $params = []): bool{
var_dump($sth);
return !is_bool($sth->fetch());
}

public function getPDO(): PDO{
return $this->PDO;
}
}

?>
10 changes: 9 additions & 1 deletion src/novagram/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ static function ip_in_range( $ip, $range ) {
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}

static function isCloudFlare() {
$cf_ips = ['173.245.48.0/20','103.21.244.0/22','103.22.200.0/22','103.31.4.0/22','141.101.64.0/18','108.162.192.0/18','190.93.240.0/20','188.114.96.0/20','197.234.240.0/22','198.41.128.0/17','162.158.0.0/15','104.16.0.0/12','172.64.0.0/13','131.0.72.0/22'];
foreach ($cf_ips as $cf_ip) if (ip_in_range($_SERVER['REMOTE_ADDR'], $cf_ip)) return true;
return false;
}

static function isTokenValid(string $token){
return preg_match('/^\d+:[\w\d_-]+$/', $token) === 1;
}
static function getIDByToken(string $token){
preg_match('/^(\d)+:[\w\d_-]+$/', $token, $matches);
return (int) $matches[0];
}
}

?>
10 changes: 5 additions & 5 deletions src/traits/conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace NovaGram;

trait conversations{
public function conversation(string $name, $value = null, array $additional_param = []){
public function conversation(string $name, $value = null, bool $permanent = false, array $additional_param = null){

$db = $this->Bot->db or exit;
$db = $this->Bot->db or exit(trigger_error("404 DB NOT FOUND"));

if(isset($value)){
return $db->setConversation($this->id, $name, $value, $additional_param);
return $db->setConversation($this->id, $name, $value, ( $additional_param ?? [] ) + ["is_permanent" => $permanent]);
}
else{
return $db->getConversation($this->id, $name, $this->Bot->update ?? null);
Expand All @@ -19,15 +19,15 @@ public function clearConversation(string $name){

#echo "conversation to {$this->id}; \nname: $name\nvalue: $value\n\n";

$db = $this->Bot->db or exit;
$db = $this->Bot->db or exit(trigger_error("404 DB NOT FOUND"));

return $db->deleteConversation($this->id, $name);

}

// $options is string|array;;;
public function status(string $value = null, ?array $options = null, bool $permanent = false){
return $this->conversation("status", $value, $options ?? [] + ["is_permanent" => $permanent]);
return $this->conversation("status", $value, $permanent, $options);
}
public function clearStatus(){
return $this->clearConversation("status");
Expand Down

0 comments on commit adbecbd

Please sign in to comment.