private function main() { while ($this->isRunning === TRUE) { // Setup our listen arrays $sockReads = $sockWrites = $socketExcept = array(); if (!$this->isWindows) { $sockReads[] = STDIN; } // Add host sockets to the arrays as needed // While at it, check if we need to connect to any of the hosts. $this->hosts->getSelectableSockets($sockReads, $sockWrites); // Add http sockets to the arrays as needed $this->http->getSelectableSockets($sockReads, $sockWrites); // Add telnet sockets to the arrays as needed $this->telnet->getSelectableSockets($sockReads, $sockWrites); // Update timeout if there are timers waiting to be fired. $this->updateSelectTimeOut($this->sleep, $this->uSleep); # Error suppression used because this function returns a "Invalid CRT parameters detected" only on Windows. $numReady = @stream_select($sockReads, $sockWrites, $socketExcept, $this->sleep, $this->uSleep); // Keep looping until you've handled all activities on the sockets. while ($numReady > 0) { $numReady -= $this->hosts->checkTraffic($sockReads, $sockWrites); $numReady -= $this->http->checkTraffic($sockReads, $sockWrites); $numReady -= $this->telnet->checkTraffic($sockReads, $sockWrites); // KB input if (in_array(STDIN, $sockReads)) { $numReady--; $kbInput = trim(fread(STDIN, STREAM_READ_BYTES)); // Split up the input $exp = explode(' ', $kbInput); // Process the command (the first char or word of the line) switch ($exp[0]) { case 'c': console(sprintf('%32s - %64s', 'COMMAND', 'DESCRIPTOIN')); foreach ($this->plugins->getPlugins() as $plugin => $details) { foreach ($details->sayCommands as $command => $detail) { console(sprintf('%32s - %64s', $command, $detail['info'])); } } break; case 'h': console(sprintf('%14s %28s:%-5s %8s %22s', 'Host ID', 'IP', 'PORT', 'UDPPORT', 'STATUS')); foreach ($this->hosts->getHostsInfo() as $host) { $status = ($host['connStatus'] == CONN_CONNECTED ? '' : ($host['connStatus'] == CONN_VERIFIED ? 'VERIFIED &' : ' NOT')) . ' CONNECTED'; $socketType = $host['socketType'] == SOCKTYPE_TCP ? 'tcp://' : 'udp://'; console(sprintf('%14s %28s:%-5s %8s %22s', $host['id'], $socketType . $host['ip'], $host['port'], $host['udpPort'], $status)); } break; case 'I': console('RE-INITIALISING PRISM...'); $this->initialise(NULL, NULL); break; case 'p': console(sprintf('%28s %8s %24s %64s', 'NAME', 'VERSION', 'AUTHOR', 'DESCRIPTION')); foreach ($this->plugins->getPlugins() as $plugin => $details) { console(sprintf("%28s %8s %24s %64s", $plugin::NAME, $plugin::VERSION, $plugin::AUTHOR, $plugin::DESCRIPTION)); } break; case 'x': $this->isRunning = FALSE; break; case 'w': console(sprintf('%15s:%5s %5s', 'IP', 'PORT', 'LAST ACTIVITY')); foreach ($this->http->getHttpInfo() as $v) { $lastAct = time() - $v['lastActivity']; console(sprintf('%15s:%5s %13d', $v['ip'], $v['port'], $lastAct)); } console('Counted ' . $this->http->getHttpNumClients() . ' http client' . ($this->http->getHttpNumClients() == 1 ? '' : 's')); break; default: console('Available Commands:'); console(' h - show host info'); console(' I - re-initialise PRISM (reload ini files / reconnect to hosts / reset http socket'); console(' p - show plugin info'); console(' x - exit PHPInSimMod'); console(' w - show www connections'); console(' c - show command list'); } } } // End while(numReady) // No need to do the maintenance check every turn if ($this->nextMaintenance > time()) { continue; } $this->nextMaintenance = time() + MAINTENANCE_INTERVAL; if (!$this->hosts->maintenance()) { $this->isRunning = FALSE; } $this->http->maintenance(); PHPParser::cleanSessions(); } // End while(isRunning) }