Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\libs;
- use app\libs\config;
- use app\libs\log;
- use app\libs\db;
- // use app\libs\JYMEngine;
- use app\libs\ymClient;
- /**
- *
- */
- class ym
- {
- private $username = null;
- private $shell = null;
- private $runOnBackground = true;
- public $exec = null;
- private $touchID = null;
- private $touchSequence = null;
- private $nSleep = 5;
- function __construct($username, $password, $consumer_key, $secret_key)
- {
- ini_set('max_execution_time', 0);
- $this->consumer_key = $consumer_key;
- $this->secret_key = $secret_key;
- $this->username = $username;
- $this->password = $password;
- $this->exec = new ymClient(
- $this->username,
- $this->password,
- $this->consumer_key,
- $this->secret_key
- );
- }
- public function setRunOnReceive($shell,$runOnBackground=true)
- {
- $this->shell = $shell;
- $this->runOnBackground = $runOnBackground;
- }
- public function connect($stage = 0)
- {
- var_dump("-P-p_P_P_P_p-P_P_P__p-P__-p-p--_P__P___");
- # login
- $this->exec->logIn();
- # check session
- if ($this->exec->hasSession() == true) {
- $this->exec->keepAliveSession();
- $this->touchHistory();
- }
- else{
- return false;
- }
- }
- public function listen()
- {
- # START prepare variables
- $ymDB = new db;
- $oldNotifs = [];
- $notifSequence = 0;
- # END prepare variables
- # START check active session
- if ($this->touchID==null) {
- return false;
- }
- # END check active session
- # START infinite LOOP
- for ($i=0; $i >= 0; $i++) {
- # START touch history
- $this->touchHistory();
- # END touch history
- # [START] get notifs
- $fullNotifs = $this->exec->fetchNotifications($notifSequence);
- $notifs = $fullNotifs["responses"];
- # [END] get
- # [START] Sending chat
- $this->runInjectedChat();
- # [END] Sending chat
- dump($notifs, CUSTOM_LOGS . "ymListener.log");
- # [START] check new notifs
- if (count($notifs) < 1) continue;
- foreach ($notifs as $index => $array) {
- # START check the message notif
- if (isset($array["message"])) {
- # START save message
- $sequence = $this->saveMessage(
- $array["message"]["sender"],
- $array["message"]["msg"]
- );
- # END save message
- # START onReceive shell
- $this->runOnReceive($sequence);
- # END onReceive shell
- }
- # END check the message notif
- # START update notifSequence
- $notifSequence++;
- # END update notifSequence
- }
- # STOP check new notifs
- # [START] delay and unset any thin
- unset($fullNotifs);
- unset($notifs);
- sleep(1);
- # [END] delay
- }
- # END infinite LOOP
- }
- public function runOnReceive($sequence)
- {
- # [START] run on receive
- if ($this->shell !== null) {
- $backgroundShell = " > /dev/null 2>/dev/null & ";
- $shell = $this->shell . " " . $sequence. ($this->runOnBackground === true ? $backgroundShell: null);
- shell_exec($shell);
- }
- # [END] run on receive
- }
- public function saveMessage($sender, $message, $recipient = null)
- {
- $ymDB = new db();
- $recipient = $recipient === null ? $this->username : $recipient;
- # [START] store it to db
- $insertQuery = "INSERT INTO globalSpace
- (
- `globalSenderAccount`,
- `globalRecipientAccount`,
- `globalContent`,
- `serviceProvider`,
- `method`,
- `globalSpaceType`
- )
- VALUES
- (
- '".$sender."',
- '".$recipient."',
- '".$message."',
- 'ym',
- 'chat',
- 'inbox'
- )";
- $ymDB->query($insertQuery)->execute();
- $sequence = $ymDB->lastInsertId();
- $ymDB->null();
- return $sequence;
- # [END] store
- }
- static public function injectChat(
- $globalSenderAccount,
- $globalRecipientAccount,
- $globalContent,
- $serviceProvider = "ym",
- $method = "chat"
- ) {
- $db = new db;
- $query = "INSERT INTO `outgoingQueue` (
- `globalSenderAccount`,
- `globalRecipientAccount`,
- `globalContent`,
- `serviceProvider`,
- `method`)
- VALUES (
- '$globalSenderAccount',
- '$globalRecipientAccount',
- '$globalContent',
- 'ym',
- 'chat')";
- $db->query($query)->execute();
- }
- public function runInjectedChat()
- {
- $db = new db;
- # get out going queue
- $queueQuery = "SELECT * FROM outgoingQueue WHERE serviceProvider = 'ym' AND method='chat' AND globalSenderAccount ='".$this->username."' ORDER BY sequence ASC";
- $db->query($queueQuery)->execute();
- $fetchALL = $db->fetchALL();
- if (!isset($fetchALL[0])) {
- return false;
- # code...
- }
- # foreach
- # delete query
- $deleteQueue = null;
- $insertGlobalSpace = null;
- foreach ($fetchALL as $key => $fetch) {
- $this->exec->sendMessage(
- $fetch["globalContent"],
- $fetch["globalRecipientAccount"]);
- $deleteQueue .= "
- DELETE FROM outgoingQueue
- WHERE sequence = '".$fetch["sequence"]."';";
- $insertGlobalSpace .="
- INSERT INTO globalSpace
- (
- `globalSenderAccount`,
- `globalRecipientAccount`,
- `globalContent`,
- `globalSpaceType`,
- `serviceProvider`,
- `method`
- )
- VALUES
- (
- '".$this->username."',
- '".$fetch["globalRecipientAccount"]."',
- '".$fetch["globalContent"]."',
- 'outbox',
- 'ym',
- 'chat'
- );";
- }
- $db->null();
- # insert global Space
- $db->query($insertGlobalSpace)->execute();
- $db->null();
- # delete queue
- $db->query($deleteQueue)->execute();
- $db->null();
- }
- public function touchHistory()
- {
- $db = new db();
- if ($this->touchID === null) {
- $touchQuery = "
- INSERT INTO loginHistory
- (
- `globalAccount`,
- `serviceProvider`,
- `method`,
- `nTouch`
- )
- VALUES
- (
- '".$this->username."',
- 'ym',
- 'chat',
- '1'
- )";
- $db->query($touchQuery)->execute();
- $this->touchID = $db->lastInsertId();
- $this->touchSequence = 1;
- }
- else{
- $this->touchSequence++;
- $touchQuery = "
- UPDATE loginHistory
- SET nTouch = '".$this->touchSequence."'
- WHERE sequence = '".$this->touchID."'";
- $db->query($touchQuery)->execute();
- }
- }
- public function reconnect()
- {
- $this->exec->signoff();
- $this->touchID = null;
- $this->touchSequence = null;
- sleep($this->nSleep);
- $this->nSleep = $this->nSleep * 2;
- $this->exec = null;
- $this->connect();
- }
- }
Add Comment
Please, Sign In to add comment