Esempio n. 1
0
 /**
  * Prepares connection with web socket server
  *
  * @return void
  */
 public function socketPrepare()
 {
     // log start
     Logger::notice('[SOCKET] Preparing connection...');
     /* 1ST REQUEST : COOKIES */
     $wssJson = $this->socketRequest($this->_cookie);
     // removing null chars
     $wssJson = urldecode(str_replace('%00%09%07%FF0', '', urlencode($wssJson)));
     // check if valid json object
     if (Fetch::isPageJson($wssJson)) {
         // storing sid
         $socketId = json_decode($wssJson)->sid;
         $sockConf = 'sid=' . $socketId;
         // appending io token to cookie
         $ioCookie = Fetch::removePageCookie($this->_cookie, 'PHPSESSID') . 'io=' . $socketId . ';';
         /* 2ND REQUEST : TEST */
         // if response returns data
         if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
             // generate payload
             $key = '["auth","' . $this->getAuthKey() . '"]';
             /* 3RD REQUEST : HANDSHAKE */
             // if handshake is ok
             if ($this->socketRequest($ioCookie, $sockConf, true, '45:' . (strlen($key) - 1) . $key) == 'ok') {
                 /* 4TH REQUEST: AUTH SUCCESS TEST */
                 // if response returns data
                 if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
                     // reset socket tick
                     $this->_sockTick = 0;
                     // log endn
                     Logger::notice('[SOCKET] Connection prepared');
                     // return sid
                     return array($ioCookie, $socketId);
                 } else {
                     Logger::crit('[SOCKET] 4: Auth success failed');
                 }
             } else {
                 Logger::crit('[SOCKET] 3: Handshake failed');
             }
         } else {
             Logger::crit('[SOCKET] 2: Test request failed');
         }
     } else {
         Logger::crit('[SOCKET] 1: SID fetch failed');
     }
 }
Esempio n. 2
0
 /**
  * Extends listen()
  *
  * @param string  $url       Web Socket URL
  * @param array   $sockConf  Web Socket Configuration
  * @param closure $callback  Anonymous method to return frames to
  *
  * @return void
  */
 private function _listen($url, $sockConf, $callback)
 {
     // connecting to websocket server
     $client = new \Devristo\Phpws\Client\WebSocket($url . '&sid=' . $sockConf[1], $this->_loop, Logger::getInstance(), array('http' => array('header' => 'Cookie:' . $sockConf[0])));
     // request
     $client->on("request", function ($headers) {
         // object created
         Logger::notice("[SOCKET] Request object created!");
     });
     // handshake
     $client->on("handshake", function () {
         // handshake made with server
         Logger::notice("[SOCKET] Handshake received!");
     });
     // connect
     $client->on("connect", function () use($client) {
         // connected to websocket
         Logger::notice("[SOCKET] Connected to Websocket!");
         // request connection
         $client->send('2probe');
     });
     // message
     $client->on("message", function ($message) use($client, $url, $sockConf, $callback) {
         // store data in frame
         $frame = $message->getData();
         // if socket has died out
         $this->handleFrame($frame, $client, $callback);
     });
     // start client listener
     $client->open();
     // start looping requests
     $this->_loop->run();
 }
Esempio n. 3
0
 /**
  * Outputs who the bot is currently listening to
  *
  * @param object $user   User Object
  * @param array  $msg    Message Array
  * 
  * @return void 
  */
 private function command_ping($user, $msg)
 {
     // check if user is owner
     if ($user['userId'] == $this->_userId) {
         // set user role based on permissions
         $role = $user['isMod'] ? 'Staff' : 'User';
         // log output message
         $outputMsg = 'Bot is listening to ' . $role . 's';
         // broadcast who the bot is listening to
         $this->internalSendMessage($outputMsg, true, $user);
         // log ping request to console
         Logger::notice('[OWNER] Performed ping command');
     }
 }
Esempio n. 4
0
 public function dropRainUser($tip)
 {
     // if username exists in the database
     if ($this->exists('rainers', $tip['user'])) {
         // store balance
         $balance = $this->getRainBalance($tip);
         // preparing drop query
         $this->customQuery('DELETE FROM `rainers` WHERE `rainers`.`username` = :username', array(':username' => $tip['user']), function ($response) use($tip) {
             // log deletion
             Logger::notice($tip['user'] . ' is not a rain user anymore');
         });
         // OK
         return $balance;
     }
     // No such username found in table
     return false;
 }
Esempio n. 5
0
 /**
  * Prepares connection with web socket server
  *
  * @return void
  */
 public function socketPrepare()
 {
     // log start
     Logger::notice('[SOCKET] Preparing connection...');
     /* 1ST REQUEST : COOKIES */
     $wssJson = $this->socketRequest(false);
     // removing null chars
     $wssJson = urldecode(str_replace('%00%09%07%FF0', '', urlencode($wssJson)));
     // check if valid json object
     if (Fetch::isPageJson($wssJson)) {
         // storing sid
         $socketId = json_decode($wssJson)->sid;
         $sockConf = 'sid=' . $socketId;
         // appending io token to cookie
         $ioCookie = 'io=' . $socketId . ';';
         /* 2ND REQUEST : TEST */
         // if response returns data
         if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
             // generate payload
             $stats = '42["stats"]';
             /* 3RD REQUEST : HANDSHAKE */
             // if handshake is ok
             if ($this->socketRequest($ioCookie, $sockConf, true, mb_strlen($stats, 'utf8') . ':' . $stats) == 'ok') {
                 /* 4TH REQUEST: TEST */
                 // if response returns data
                 if (isset($this->socketRequest($ioCookie, $sockConf)[0])) {
                     /* 5TH Auth TEST */
                     // generate payload
                     $partOne = '42["chat"]';
                     $partTwo = '42["user","' . $this->_accessToken . '"]';
                     // if connected
                     if ($this->socketRequest($ioCookie, $sockConf, true, mb_strlen($partOne, 'utf8') . ':' . $partOne . mb_strlen($partTwo, 'utf8') . ':' . $partTwo) == 'ok') {
                         // log end
                         Logger::notice('[SOCKET] Connection prepared');
                         // return sid
                         return array($ioCookie, $socketId);
                     } else {
                         Logger::crit('[SOCKET] 5: Chat Success failed');
                     }
                 } else {
                     Logger::crit('[SOCKET] 4: Stats request failed');
                 }
             } else {
                 Logger::crit('[SOCKET] 3: Handshake failed');
             }
         } else {
             Logger::crit('[SOCKET] 2: Test request failed');
         }
     } else {
         Logger::crit('[SOCKET] 1: SID fetch failed');
     }
 }