Created
October 5, 2018 06:02
-
-
Save ammarfaizi2/fac6bac1957d7f8689b9cf7b721616e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Ammar Faizi <[email protected]> | |
* @license MIT | |
* @version 0.0.1 | |
*/ | |
class TelegramMonitoringAPI | |
{ | |
/** | |
* @var string | |
*/ | |
private $apiMethod = ""; | |
/** | |
* Constructor. | |
*/ | |
public function __construct() | |
{ | |
if (!isset($_GET["method"])) { | |
$this->error("You need to provide the \"method\" parameter"); | |
} | |
if (!is_string($_GET["method"])) { | |
$this->error("\"method\" parameter must be a string"); | |
} | |
$this->apiMethod = trim(strtolower($_GET["method"])); | |
} | |
/** | |
* @return void | |
*/ | |
public function run(): void | |
{ | |
switch ($this->apiMethod) { | |
case "get_status": | |
$this->getStatus(); | |
break; | |
case "run_daemon": | |
$this->runDaemon(); | |
break; | |
case "kill_daemon": | |
$this->killDaemon(); | |
break; | |
default: | |
$this->error("Method \"{$this->apiMethod}\" does not exist"); | |
break; | |
} | |
} | |
/** | |
* @return void | |
*/ | |
private function getStatus(): void | |
{ | |
if (!isset($_GET["session_name"])) { | |
$this->error("You need to provide the \"session_name\" parameter"); | |
} | |
if (!is_string($_GET["session_name"])) { | |
$this->error("\"session_name\" parameter must be a string"); | |
} | |
$sessionFile = STORAGE_PATH."/telegram_sessions/".$_GET["session_name"]; | |
if (!file_exists($sessionFile)) { | |
$this->error("\"{$sessionFile}\" does not exists"); | |
} | |
$pidFile = STORAGE_PATH."/pid/{$_GET['session_name']}.pid"; | |
if (file_exists($pidFile)) { | |
$pidData = json_decode(file_get_contents($pidFile), true); | |
if (!is_array($pidData) || $pidData===[]) { | |
$this->success(["status" => "off"]); | |
} | |
$this->success( | |
[ | |
"status" => "on", | |
"pid" => $pidData | |
] | |
); | |
} else { | |
$this->success(["status" => "off"]); | |
} | |
} | |
/** | |
* @return void | |
*/ | |
private function killDaemon(): void | |
{ | |
if (!isset($_GET["session_name"])) { | |
$this->error("You need to provide the \"session_name\" parameter"); | |
} | |
$sessionFile = STORAGE_PATH."/telegram_sessions/".$_GET["session_name"]; | |
if (!file_exists($sessionFile)) { | |
$this->error("\"{$sessionFile}\" does not exists"); | |
} | |
$pidFile = STORAGE_PATH."/pid/{$_GET['session_name']}.pid"; | |
$started = true; | |
if (file_exists($pidFile)) { | |
$pidData = json_decode(file_get_contents($pidFile), true); | |
if (!is_array($pidData) || $pidData===[]) { | |
$started = false; | |
} else { | |
$msg = count($pidData)." processes has been killed"; | |
foreach ($pidData as $pid) { | |
for ($i=0; $i < 100; $i++) { | |
shell_exec("nohup kill {$pid} >> /dev/null 2>&1 &"); | |
} | |
} | |
} | |
} else { | |
$started = false; | |
$msg = "There is no process to be killed"; | |
} | |
if ($started) { | |
sleep(3); | |
} | |
$out = "Could not kill the daemon"; | |
if (!file_exists($pidFile)) { | |
$this->success( | |
[ | |
"status" => "success", | |
"message" => $msg | |
] | |
); | |
} else { | |
$this->success( | |
[ | |
"status" => "failed", | |
"out" => $out | |
] | |
); | |
} | |
} | |
/** | |
* @return void | |
*/ | |
private function runDaemon(): void | |
{ | |
if (!isset($_GET["session_name"])) { | |
$this->error("You need to provide the \"session_name\" parameter"); | |
} | |
$sessionFile = STORAGE_PATH."/telegram_sessions/".$_GET["session_name"]; | |
if (!file_exists($sessionFile)) { | |
$this->error("\"{$sessionFile}\" does not exists"); | |
} | |
$pidFile = STORAGE_PATH."/pid/{$_GET['session_name']}.pid"; | |
$start = false; | |
if (file_exists($pidFile)) { | |
$pidData = json_decode(file_get_contents($pidFile), true); | |
if (!is_array($pidData) || $pidData===[]) { | |
$start = true; | |
} | |
} else { | |
$start = true; | |
} | |
if ($start) { | |
$out = shell_exec("nohup /usr/bin/php7.2 ".BASEPATH."/bin/telegramd ".escapeshellarg($_GET["session_name"])." --daemonize --telegram-daemon --no-tty -f 1 2>&1 >> ".BASEPATH."/storage/logs/{$_GET['session_name']} 2>&1 &"); | |
// Give some delay to wait it reaches the pid handler. | |
sleep(3); | |
$msg = "Starting daemon success!"; | |
} else { | |
$msg = "No action to daemon starter since the daemon has already been started!"; | |
} | |
if (file_exists($pidFile)) { | |
$pidData = json_decode(file_get_contents($pidFile), true); | |
if (!is_array($pidData) || $pidData===[]) { | |
$this->success(["status" => "off"]); | |
} | |
$this->success( | |
[ | |
"status" => "success", | |
"message" => $msg, | |
"pid" => $pidData | |
] | |
); | |
} else { | |
$this->success( | |
[ | |
"status" => "failed", | |
"out" => $out | |
] | |
); | |
} | |
} | |
/** | |
* @param mixed $msg | |
* @param int $code | |
* @return void | |
*/ | |
public function success($msg, $code = 200): void | |
{ | |
http_response_code($code); | |
header("Content-Type: application/json"); | |
print json_encode( | |
[ | |
"status" => "success", | |
"message" => $msg | |
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | |
); | |
exit(0); | |
} | |
/** | |
* @param mixed $msg | |
* @param int $code | |
* @return void | |
*/ | |
private function error($msg, int $code = 400): void | |
{ | |
http_response_code($code); | |
header("Content-Type: application/json"); | |
print json_encode( | |
[ | |
"status" => "error", | |
"message" => $msg | |
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | |
); | |
exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment