function siemens_push_str($phone_ip, $postdata) { $prov_host = gs_get_conf('GS_PROV_HOST'); $data = "POST /server_push.html/ServerPush HTTP/1.1\r\n"; $data .= "User-Agent: Gemeinschaft\r\n"; $data .= "Host: {$phone_ip}:8085\r\n"; $data .= "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"; $data .= "Connection: keep-alive\r\n"; $data .= "Content-Type: application/x-www-form-urlencoded\r\n"; $data .= "Content-Length: " . strLen($postdata) . "\r\n\r\n"; $data .= $postdata; $socket = @fSockOpen($phone_ip, 8085, $error_no, $error_str, 4); if (!$socket) { gs_log(GS_LOG_NOTICE, "Siemens: Failed to open socket - IP: {$phone_ip}"); return 0; } stream_set_timeout($socket, 4); $bytes_written = (int) @fWrite($socket, $data, strLen($data)); @fFlush($socket); $response = @fGetS($socket); @fClose($socket); if (strPos($response, '200') === false) { gs_log(GS_LOG_WARNING, "Siemens: Failed to push to phone {$phone_ip}"); return 0; } gs_log(GS_LOG_DEBUG, "Siemens: Pushed {$bytes_written} bytes to phone {$phone_ip}"); return $bytes_written; }
function ping($host, $port, $timeout) { $tB = microtime(true); $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); if (!$fP) { return "Server Status:<div id='h2'>Offline</div> @Ping:" . "N/A"; } $tA = microtime(true); return "Server Status<div id='h1'>Online</div> @Ping: " . round(($tA - $tB) * 1000, 0) . " ms"; }
function ping($config, $timeout) { $tB = microtime(true); $fP = fSockOpen($config['host'], $config['port'], $timeout); if (!$fP) { return false; } $tA = microtime(true); return true; }
function ping($host, $port, $timeout) { $tB = microtime(true); $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); if (!$fP) { return "down"; } $tA = microtime(true); return round(($tA - $tB) * 1000, 0) . " ms"; }
function ping($host, $port, $timeout) { $tB = microtime(true); $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); $tA = microtime(true); if (!$fP) { return false; } else { return round(($tA - $tB) * 1000, 0); } }
function open($method, $url) { $this->method = $method; $this->url = @parse_url($url); if ($this->url === false) { throw new Exception('Invalid URL format'); } $this->sock = @fSockOpen($this->url['host'], array_key_exists('port', $this->url) ? $this->url['port'] : 80, $errno, $errstr, $this->timeout); if (!$this->sock) { throw new Exception($errstr, $errno); } }
private function proxy_is_available($proxy) { $ip = explode(":", $proxy)[0]; $port = explode(":", $proxy)[1]; $proxy_is_up = true; $socket = fSockOpen($ip, $port, $errno, $errstr, 5); if (!$socket) { $proxy_is_up = false; } else { fclose($socket); } return $proxy_is_up; }
protected function getPing($server_host, $port = 80, $timeout = 1) { $pingMs = 0; if (!function_exists('fSockOpen')) { return 0; } $pingStart = microtime(true); $fsock = @fSockOpen($server_host, $port, $errno, $errstr, $timeout); $pingEnd = microtime(true); if (is_resource($fsock)) { $pingMs = round(($pingEnd - $pingStart) * 1000, 0); fclose($fsock); } return $pingMs; }
function gs_send_phone_desktop_msg($ip, $port, $ext, $registrar, $text, $extra = array()) { static $sock = null; static $lAddr = '0.0.0.0'; static $lPort = 0; static $have_sockets = null; if ($have_sockets === null) { $have_sockets = function_exists('socket_create'); // about 15 to 45 % faster } if (is_array($extra) && array_key_exists('fake_callid', $extra)) { $fake_callid = $extra['fake_callid']; } else { $fake_callid = rand(1000000000, 2000000000); } if ($have_sockets) { if (!$sock) { $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if (!$sock) { return false; } //socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1); //socket_bind($sock, '0.0.0.0', 12345); @socket_getSockName($sock, $lAddr, $lPort); //echo "Local socket is at $lAddr:$lPort\n"; @socket_set_nonblock($sock); } } else { $sock = @fSockOpen('udp://' . $ip, $port, $err, $errmsg, 1); if (!$sock) { return false; } @stream_set_blocking($sock, 0); @stream_set_timeout($sock, 1); } $sipmsg = 'MESSAGE sip:' . $ext . '@' . $ip . ' SIP/2.0' . "\r\n" . 'Via: SIP/2.0/UDP ' . $registrar . ':' . $lPort . "\r\n" . 'To: sip:' . $ext . '@' . $ip . '' . "\r\n" . 'Call-ID: ' . $fake_callid . '@' . $registrar . "\r\n" . 'CSeq: 1 MESSAGE' . "\r\n" . 'Content-Type: text/plain; charset=utf-8' . "\r\n" . 'Max-Forwards: 9' . "\r\n" . 'From: sip:fake@' . $registrar . ':' . $lPort . ';tag=fake' . "\r\n" . 'Content-Length: ' . strLen($text) . "\r\n" . 'Content-Disposition: desktop' . "\r\n" . "\r\n" . $text; if ($have_sockets) { return @socket_sendto($sock, $sipmsg, strLen($sipmsg), 0, $ip, $port); } else { $written = @fWrite($sock, $sipmsg, strlen($sipmsg)); @fClose($sock); return $written; } }
public function onJoin(PlayerJoinEvent $event) { $player = $event->getPlayer(); $player_name = $player->getName(); $player_ip = $player->getAddress(); $get_ping = 'max-ping: '; $config_file = file_get_contents($this->getDataFolder() . "ping.txt"); $max_ping = substr(strstr($config_file, $get_ping), strlen($get_ping)); $tB = microtime(true); $fP = fSockOpen($player_ip, 80, $errno, $errstr, 10); if (!$fP) { $sender->sendMessage(TF::RED . $player . "'s IP: " . $player_ip . " was unreachable."); } $tA = microtime(true); $ping_result = round(($tA - $tB) * 1000, 0); if ($ping_result >= $max_ping) { $player->kick("Sorry, your ping is too high(too laggy), so you have been kicked."); } }
function aastra_push_str($phone_ip, $xml) { $prov_host = gs_get_conf('GS_PROV_HOST'); //FIXME - call wget or something. this function should not block // for so long! // see _gs_prov_phone_checkcfg_by_ip_do_aastra() in // opt/gemeinschaft/inc/gs-fns/gs_prov_phone_checkcfg.php //$xml = utf8_decode($xml); if (subStr($xml, 0, 5) !== '<' . '?xml') { $xmlpi = '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' . "\n"; } else { $xmlpi = ''; } $data = "POST / HTTP/1.1\r\n"; $data .= "Host: {$phone_ip}\r\n"; $data .= "Referer: {$prov_host}\r\n"; $data .= "Connection: Close\r\n"; $data .= "Content-Type: text/xml; charset=utf-8\r\n"; $data .= "Content-Length: " . (strLen('xml=') + strLen($xmlpi) + strLen($xml)) . "\r\n"; $data .= "\r\n"; $data .= 'xml=' . $xmlpi . $xml; $socket = @fSockOpen($phone_ip, 80, $error_no, $error_str, 4); if (!$socket) { gs_log(GS_LOG_NOTICE, "Aastra: Failed to open socket - IP: {$phone_ip}"); return 0; } stream_set_timeout($socket, 4); $bytes_written = (int) @fWrite($socket, $data, strLen($data)); @fFlush($socket); $response = @fGetS($socket); @fClose($socket); if (strPos($response, '200') === false) { gs_log(GS_LOG_WARNING, "Aastra: Failed to push XML to phone {$phone_ip}"); return 0; } gs_log(GS_LOG_DEBUG, "Aastra: Pushed {$bytes_written} bytes to phone {$phone_ip}"); return $bytes_written; }
public function ami_login($username, $password, $host, $port) { $this->_socket = fSockOpen($host, $port, $err, $errmsg, 2); if (!$this->_check_socket()) { gs_log(GS_LOG_WARNING, 'Connection to AMI on ' . $host . ' failed'); return false; } # check: is AMI? $tStart = time(); $data = ''; while (!fEof($this->_socket) && time() < $tStart + 3) { $data .= @fRead($this->_socket, 8192); if (@preg_match('/[\\r\\n]/', $data)) { break; } usleep(1000); # sleep 0.001 secs } if (!preg_match('/^Asterisk [^\\/]+\\/(\\d(?:\\.\\d)?)/mis', $data, $m)) { gs_log(GS_LOG_WARNING, 'Incompatible Asterisk Manager Interface on ' . $host); $m = array(1 => '0.0'); } else { if ($m[1] > '1.1') { # Asterisk 1.4: manager 1.0 # Asterisk 1.6: manager 1.1 gs_log(GS_LOG_WARNING, 'Asterisk manager interface on ' . $host . ' speaks a new protocol version (' . $m[1] . ')'); # let's try anyway and hope to understand it } $req = 'Action: Login' . "\r\n" . 'Username: '******'Secret: ' . $password . "\r\n" . 'Events: off' . "\r\n" . "\r\n"; $data = $this->ami_send_command($req); if (strToLower($data['Message']) !== 'authentication accepted') { gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed'); return false; } } return true; }
@session_write_close(); header('Content-Type: text/html; charset=utf-8'); header('Pragma: no-cache'); header('Cache-Control: private, no-cache, must-revalidate'); header('Expires: 0'); header('Vary: *'); ini_set('implicit_flush', 0); ob_implicit_flush(0); @ini_set('max_execution_time', $maxtime + 10); $html_start = '<html><head><title>-</title>' . "\n" . '<script type="text/javascript">' . "\n" . 'function e(s) {try{ window.parent.gs_e(s); }catch(e){}}' . "\n" . 'function m(o) {try{ window.parent.gs_m(o); }catch(e){}}' . "\n" . '</script>' . "\n" . '</head><body>' . "\n\n"; $html_end = "\n" . '</body></html>' . "\n"; $msg_open = '<script type="text/javascript">' . "\n"; $msg_close = '</script>' . '.' . "\n"; // padding for the stupid MSIE: $msie_pad = str_repeat(' ', 256 - 1) . "\n"; $sock = @fSockOpen($host, $port, $err, $errMsg, 5); if (!is_resource($sock)) { header('HTTP/1.0 500 Internal Server Error', true, 500); header('Status: 500 Internal Server Error', true, 500); echo $html_start; echo $msg_open; echo 'e("daemondown");', "\n"; echo $msie_pad; echo $msg_close; echo $html_end; @ob_flush(); @flush(); gs_log(GS_LOG_NOTICE, 'Extension state daemon not running.'); sleep(1); die; }
function gs_extstate($host, $exts) { static $hosts = array(); if (!is_array($exts)) { $exts = array($exts); $return_single = true; } else { $return_single = false; } if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $host = '127.0.0.1'; } if (!isset($hosts[$host])) { $hosts[$host] = array('sock' => null, 'lasttry' => 0); } if (!is_resource($hosts[$host]['sock'])) { if ($hosts[$host]['lasttry'] > time() - 60) { # we have tried less than a minute ago $hosts[$host]['lasttry'] = time(); return $return_single ? AST_MGR_EXT_UNKNOWN : array(); } $hosts[$host]['lasttry'] = time(); $sock = @fSockOpen($host, 5038, $err, $errMsg, 2); if (!is_resource($sock)) { gs_log(GS_LOG_WARNING, 'Connection to AMI on ' . $host . ' failed'); return $return_single ? AST_MGR_EXT_UNKNOWN : array(); } $data = _sock_read($sock, 3, '/[\\r\\n]/'); if (!preg_match('/^Asterisk [^\\/]+\\/(\\d(?:\\.\\d)?)/mis', $data, $m)) { gs_log(GS_LOG_WARNING, 'Incompatible Asterisk manager interface on ' . $host); $m = array(1 => '0.0'); } else { if ($m[1] > '1.1') { # Asterisk 1.4: manager 1.0 # Asterisk 1.6: manager 1.1 gs_log(GS_LOG_NOTICE, 'Asterisk manager interface on ' . $host . ' speaks a new protocol version (' . $m[1] . ')'); # let's try anyway and hope to understand it } } $hosts[$host]['sock'] = $sock; $req = "Action: Login\r\n" . "Username: "******"gscc" . "\r\n" . "Secret: " . "gspass" . "\r\n" . "Events: off\r\n" . "\r\n"; @fWrite($sock, $req, strLen($req)); @fFlush($sock); $data = _sock_read($sock, 3, '/\\r\\n\\r\\n/S'); if ($data === false) { gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed (timeout)'); $hosts[$host]['sock'] = null; return $return_single ? AST_MGR_EXT_UNKNOWN : array(); } elseif (!preg_match('/Authentication accepted/i', $data)) { gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed'); $hosts[$host]['sock'] = null; return $return_single ? AST_MGR_EXT_UNKNOWN : array(); } } else { $sock = $hosts[$host]['sock']; } $states = array(); foreach ($exts as $ext) { $req = "Action: ExtensionState\r\n" . "Context: to-internal-users\r\n" . "Exten: " . $ext . "\r\n" . "\r\n"; @fWrite($sock, $req, strLen($req)); @fFlush($sock); $resp = trim(_sock_read($sock, 3, '/\\r\\n\\r\\n/S')); //echo "\n$resp\n\n"; $states[$ext] = AST_MGR_EXT_UNKNOWN; if (!preg_match('/^Response:\\s*Success/is', $resp)) { continue; } if (!preg_match('/^Exten:\\s*([\\da-z]+)/mis', $resp, $m)) { continue; } $resp_ext = $m[1]; if (!preg_match('/^Status:\\s*(-?\\d+)/mis', $resp, $m)) { continue; } $resp_state = (int) $m[1]; $states[$resp_ext] = $resp_state; } if ($return_single) { return array_key_exists($exts[0], $states) ? $states[$exts[0]] : AST_MGR_EXT_UNKNOWN; } else { return $states; } }
function gs_queue_status($host, $ext, $getMembers, $getCallers) { static $hosts = array(); if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { $host = '127.0.0.1'; } if (!isset($hosts[$host])) { $hosts[$host] = array('sock' => null, 'lasttry' => 0); } if (!is_resource($hosts[$host]['sock'])) { if ($hosts[$host]['lasttry'] > time() - 60) { # we have tried less than a minute ago $hosts[$host]['lasttry'] = time(); return false; } $hosts[$host]['lasttry'] = time(); $sock = @fSockOpen($host, 5038, $err, $errMsg, 2); if (!is_resource($sock)) { gs_log(GS_LOG_WARNING, 'Connection to AMI on ' . $host . ' failed'); return false; } $data = _sock_read($sock, 3, '/[\\r\\n]/'); if (!preg_match('/^Asterisk [^\\/]+\\/(\\d(?:\\.\\d)?)/mis', $data, $m)) { gs_log(GS_LOG_WARNING, 'Incompatible Asterisk manager interface on ' . $host); $m = array(1 => '0.0'); } else { if ($m[1] > '1.1') { # Asterisk 1.4: manager 1.0 # Asterisk 1.6: manager 1.1 gs_log(GS_LOG_NOTICE, 'Asterisk manager interface on ' . $host . ' speaks a new protocol version (' . $m[1] . ')'); # let's try anyway and hope to understand it } } $hosts[$host]['sock'] = $sock; $req = "Action: Login\r\n" . "Username: "******"gscc" . "\r\n" . "Secret: " . "gspass" . "\r\n" . "Events: off\r\n" . "\r\n"; @fWrite($sock, $req, strLen($req)); @fFlush($sock); $data = _sock_read2($sock, 5, '/\\r\\n\\r\\n/S'); if (!preg_match('/Authentication accepted/i', $data)) { gs_log(GS_LOG_WARNING, 'Authentication to AMI on ' . $host . ' failed'); $hosts[$host]['sock'] = null; return false; } } else { $sock = $hosts[$host]['sock']; } $queue_stats = array('maxlen' => null, 'calls' => null, 'holdtime' => null, 'completed' => null, 'abandoned' => null, 'sl' => null, 'slp' => null); if ($getMembers) { $queue_stats['members'] = array(); } if ($getCallers) { $queue_stats['callers'] = array(); } $default_member = array('dynamic' => null, 'calls' => null, 'lastcall' => null, 'devstate' => null, 'paused' => null); $default_caller = array('channel' => null, 'cidnum' => null, 'cidname' => null, 'wait' => null); $req = "Action: QueueStatus\r\n" . "Queue: " . $ext . "\r\n" . "\r\n"; @fWrite($sock, $req, strLen($req)); @fFlush($sock); $resp = trim(_sock_read2($sock, 2, '/Event:\\s*QueueStatusComplete\\r\\n\\r\\n/i')); //echo "\n$resp\n\n"; if (!preg_match('/^Response:\\s*Success/is', $resp)) { return false; } $resp = preg_split('/\\r\\n\\r\\n/S', $resp); /* echo "<pre>"; print_r($resp); echo "</pre>"; */ $manager_ok = false; foreach ($resp as $pkt) { $pkt = lTrim($pkt); if (preg_match('/^Event:\\s*QueueParams/is', $pkt)) { if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) { continue; } //echo $pkt, "\n\n"; if (preg_match('/^Max:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['maxlen'] = (int) $m[1] > 0 ? (int) $m[1] : null; } if (preg_match('/^Calls:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['calls'] = (int) $m[1]; } if (preg_match('/^Holdtime:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['holdtime'] = (int) $m[1]; } if (preg_match('/^Completed:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['completed'] = (int) $m[1]; } if (preg_match('/^Abandoned:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['abandoned'] = (int) $m[1]; } if (preg_match('/^ServiceLevel:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['sl'] = (int) $m[1]; } if (preg_match('/^ServiceLevelPerf:\\s*(\\d+?(\\.\\d+)?)/mis', $pkt, $m)) { $queue_stats['slp'] = (double) $m[1]; } $manager_ok = true; } elseif ($getMembers && preg_match('/^Event:\\s*QueueMember/is', $pkt)) { if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) { continue; } if (!preg_match('/^Location:\\s*([A-Z\\d\\/]+)/mis', $pkt, $m)) { continue; } $loc = $m[1]; $queue_stats['members'][$loc] = $default_member; //echo $pkt, "\n\n"; if (preg_match('/^Membership:\\s*([a-z]+)/mis', $pkt, $m)) { $queue_stats['members'][$loc]['dynamic'] = $m[1] != 'static'; } if (preg_match('/^CallsTaken:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['members'][$loc]['calls'] = (int) $m[1]; } if (preg_match('/^LastCall:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['members'][$loc]['lastcall'] = (int) $m[1]; } if (preg_match('/^Status:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['members'][$loc]['devstate'] = (int) $m[1]; } if (preg_match('/^Paused:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['members'][$loc]['paused'] = (int) $m[1] > 0; } } elseif ($getCallers && preg_match('/^Event:\\s*QueueEntry/is', $pkt)) { if (!preg_match('/^Queue:\\s*' . $ext . '/mis', $pkt)) { continue; } if (!preg_match('/^Position:\\s*(\\d+)/mis', $pkt, $m)) { continue; } $pos = (int) $m[1]; $queue_stats['callers'][$pos] = $default_caller; //echo $pkt, "\n\n"; if (preg_match('/^Channel:\\s*([^\\n\\r]+)/mis', $pkt, $m)) { $queue_stats['callers'][$pos]['dynamic'] = trim($m[1]); } if (preg_match('/^CallerID:\\s*([^\\n\\r]+)/mis', $pkt, $m)) { $queue_stats['callers'][$pos]['cidnum'] = strToLower(trim($m[1])) != 'unknown' ? trim($m[1]) : null; } if (preg_match('/^CallerIDName:\\s*([^\\n\\r]+)/mis', $pkt, $m)) { $queue_stats['callers'][$pos]['cidname'] = strToLower(trim($m[1])) != 'unknown' ? trim($m[1]) : null; } if (preg_match('/^Wait:\\s*(\\d+)/mis', $pkt, $m)) { $queue_stats['callers'][$pos]['wait'] = (int) $m[1]; } } } if (!$manager_ok && $getMembers) { # failed to get information about the queue from the manager # interface. this happens after a reload of Asterisk when # no call has entered the queue using Queue() yet $queue_stats['calls'] = 0; $queue_stats['completed'] = 0; $queue_stats['abandoned'] = 0; $queue_stats['holdtime'] = 0; include_once GS_DIR . 'inc/db_connect.php'; $db = @gs_db_slave_connect(); if (!$db) { return $queue_stats; } $maxlen = (int) $db->executeGetOne('SELECT `maxlen` FROM `ast_queues` WHERE `name`=\'' . $db->escape($ext) . '\''); $queue_stats['maxlen'] = $maxlen > 0 ? $maxlen : null; $rs = $db->execute('SELECT `interface` FROM `ast_queue_members` WHERE `queue_name`=\'' . $db->escape($ext) . '\''); $queue_members = array(); while ($r = $rs->fetchRow()) { if (strToUpper(subStr($r['interface'], 0, 4)) == 'SIP/') { $queue_members[] = subStr($r['interface'], 4); } else { $queue_members[] = $r['interface']; } } if (count($queue_members) < 1) { return $queue_stats; } foreach ($queue_members as $queue_member) { $queue_stats['members']['SIP/' . $queue_member] = $default_member; } $ext_states = @gs_extstate($host, $queue_members); if (!is_array($ext_states)) { return $queue_stats; } foreach ($queue_members as $queue_member) { $queue_stats['members']['SIP/' . $queue_member]['devstate'] = extstate_to_devstate(@$ext_states[$queue_member]); } } /* echo "<pre>"; print_r($queue_stats); echo "</pre>"; */ return $queue_stats; }
public static function setSMTPconnectionParams() { $configs = array(array('ssl://', 465), array('tls://', 587), array('', 587), array('', 588), array('tls://', 25), array('', 25)); $host = Configuration::get('PS_MAIL_SERVER'); $connected = FALSE; for ($i = 0; $i < count($configs); ++$i) { $soc = @fSockOpen($configs[$i][0] . $host, $configs[$i][1], $errno, $errstr, 5); if ($soc) { fClose($soc); $connected = TRUE; break; } } if ($connected) { if ('ssl://' == $configs[$i][0]) { Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', 'ssl'); } elseif ('tls://' == $configs[$i][0]) { Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', 'tls'); } else { Configuration::updateValue('PS_MAIL_SMTP_ENCRYPTION', ''); } Configuration::updateValue('PS_MAIL_SMTP_PORT', $configs[$i][1]); } }
function availableUrl($host, $port = 80, $timeout = 5) { $fp = fSockOpen($host, $port, $errno, $errstr, $timeout); return $fp != false; }
private function streamOpen() { $errno = 0; $errstr = ''; $host = $this->isSSL() ? 'ssl://' : ''; $host .= $this->getHost(); $this->fp = @fSockOpen($host, $this->getPort(), $errno, $errstr, 30); if (!$this->fp) { throw new TSimpleHttpClientException('streamopen_failed', $errno, $errstr, $this->getHost(), $this->getPort()); } }
/** * Check if the device is available * * @author Ivan Amat <*****@*****.**> * @copyright Copyright 2016, Iván Amat * * @param string $host Host to connect * @param integer $port Port to connect * @param integer $timeout Router timeout. * @return boolean */ public function available($host, $port = 8728, $timeout = 1) { $fp = @fSockOpen($host, $port, $errno, $errstr, $timeout); return $fp != false; }
function gs_log_syslog($level, $msg, $logfile = null) { global $gs_is_in_gs_syslog; static $facility = 1; # 1 = user-level static $hostname = null; static $sock = null; static $num_failed = 0; static $levels = array(GS_LOG_DEBUG => 7, GS_LOG_NOTICE => 5, GS_LOG_WARNING => 3, GS_LOG_FATAL => 2); if (@$gs_is_in_gs_syslog) { return false; } # prevent recursive calls to gs_log_syslog() if ($level > GS_LOG_LEVEL) { return true; } $gs_is_in_gs_syslog = true; if ($hostname === null) { $hostname = 'localhost'; $err = 0; $out = array(); @exec('hostname 2>>/dev/null', $out, $err); if ($err == 0) { $ret = preg_replace('/[^\\x21-\\x7E]/', '', implode('', $out)); if ($ret != '') { $hostname = $ret; } } } if (!$sock) { $use_tcp = gs_get_conf('GS_SYSLOG_TCP'); $timeout = $use_tcp ? 5 : 1; if ($num_failed > 2) { $timeout = 1; } $sock = @fSockOpen(($use_tcp ? 'tcp' : 'udp') . '://' . gs_get_conf('GS_SYSLOG_HOST'), 514, $err, $errmsg, $timeout); if (!$sock) { ++$num_failed; $gs_is_in_gs_syslog = false; return false; } @stream_set_blocking($sock, 0); @stream_set_timeout($sock, $timeout); } $slLevel = array_key_exists($level, $levels) ? $levels[$level] : 5; $pri = $facility * 8 + $slLevel; $msg = str_replace(GS_DIR, '', $msg); $msg = str_replace("\n", ' ## ', $msg); $msg = preg_replace('/[^\\x20-\\x7E]/', '', $msg); $dateFn = GS_LOG_GMT ? 'gmDate' : 'date'; $t = time(); $backtrace = debug_backtrace(); if (is_array($backtrace) && isset($backtrace[0])) { $line = @$backtrace[0]['line']; $file = @$backtrace[0]['file']; if (subStr($file, 0, strLen(GS_DIR)) == GS_DIR) { $file = str_replace(GS_DIR, '', $file); } if (strLen($file) <= 32) { $tag = $file; } else { $file = baseName($file); $tag = subStr($file, 0, 32); } $where = $tag . '[' . $line . ']'; } else { $where = '-' . '[' . '0' . ']'; } $logstr = '<' . $pri . '>' . $dateFn('M', $t) . ' ' . preg_replace('/^0/', ' ', $dateFn('m', $t)) . ' ' . $dateFn('H:i:s', $t) . ' ' . $hostname . ' ' . $where . ':' . $msg; if (strLen($logstr) > 1022) { $logstr = subStr($logstr, 0, 1022) . '..'; } $ok = @fWrite($sock, $logstr, strLen($logstr)); $gs_is_in_gs_syslog = false; return $ok; }
function tivoli_http_request() { global $scheme, $host, $port, $timeout, $sessionName; /* echo "<pre>"; print_r($_SERVER); print_r($_FILES); echo "</pre>"; */ if (is_array($_FILES) && count($_FILES) > 0) { $errout = 'File uploads cannot be handled by the WebSeal simulation script.'; } else { $errout = ''; } $postVals = array(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($_POST as $k => $v) { if (subStr($k, 0, 10) != '_x_tivoli_') { $postVals[] = urlEncode($k) . '=' . urlEncode($v); } } } $postData = implode('&', $postVals); $headers = array(); foreach ($_SERVER as $k => $v) { if (subStr($k, 0, 5) == 'HTTP_') { $k = strToLower(str_replace('_', '-', subStr($k, 5))); $headers[$k] = $v; } } $headers['accept-encoding'] = 'identity'; $headers['connection'] = 'close'; $headers['host'] = $host . ':' . $port; unset($headers['keep-alive']); $headers['iv-user'] = $_SESSION['user']; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $headers['content-type'] = 'application/x-www-form-urlencoded'; $headers['content-length'] = strLen($postData); } if (isset($headers['cookie'])) { $headers['cookie'] = preg_replace('/' . $sessionName . '=[^\\s]*/', '', $headers['cookie']); } if (isset($headers['referer'])) { $headers['referer'] = str_replace('http://' . $_SERVER['HTTP_HOST'] . '/', 'http://' . $host . ':' . $port . '/', $headers['referer']); if ($port == 80) { $headers['referer'] = str_replace(':80', '', $headers['referer']); } } $req = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' HTTP/1.0' . "\r\n"; foreach ($headers as $k => $v) { $req .= ucWords($k) . ': ' . $v . "\r\n"; } $req .= "\r\n"; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $req .= $postData; } unset($headers); $sock = @fSockOpen('tcp://' . $host, $port, $err, $errMsg, $timeout); if (!$sock) { echo "Could not connect to host {$host}, port {$port}\n"; return false; } fWrite($sock, $req, strLen($req)); unset($req); $response = ''; while (!fEof($sock)) { $response .= fRead($sock, 10000); } my_http_handle_response($response, $errout); }
function sendMailToServer($params, $headers, $Message) { // Ouverture de la connexion au serveur $sock = fSockOpen($params["host"], $params["port"]); if ($sock) { socket_set_timeout($sock, 5, 0); $s = fgets($sock, 512); // Message de connexion fputs($sock, "HELO " . $params["host"] . "\r\n"); $s = fgets($sock, 512); // De qui provient le message fputs($sock, "MAIL FROM: <" . $headers["From"] . ">\r\n"); $s = fgets($sock, 512); // Liste des destinataire - chacun avec une instruction de connexion for ($j = 0; $j < 2; $j++) { if ($j == 0) { $field = "Cc"; } else { $field = "To"; } $liste = ""; if (is_array($headers[$field])) { foreach ($headers[$field] as $unDestinataire) { if ($unDestinataire != "") { fputs($sock, "RCPT TO: {$unDestinataire}\r\n"); $s = fgets($sock, 512); } } } else { if ($headers[$field] != "") { $unDestinataire = $headers[$field]; fputs($sock, "RCPT TO: {$unDestinataire}\r\n"); $s = fgets($sock, 512); } } } // Le message lui même fputs($sock, "DATA\r\n"); $s = fgets($sock, 512); // Entête du message if ($headers["Subject"] != "") { fputs($sock, "Subject: " . $headers["Subject"] . "\r\n"); } else { fputs($sock, "Subject: Message From Server\r\n"); } if ($headers["From"] != "") { fputs($sock, "From: " . $headers["From"] . "\r\n"); } for ($j = 0; $j < 2; $j++) { if ($j == 0) { $field = "Cc"; } else { $field = "To"; } $liste = ""; if (is_array($headers[$field])) { foreach ($headers[$field] as $unDestinataire) { if ($unDestinataire != "") { if ($liste != "") { $liste .= ","; } $liste .= $unDestinataire; } } } else { if ($headers[$field] != "") { $liste = $headers[$field]; } } if (strlen($liste) >= 1024) { $liste = ""; } if ($liste != "") { fputs($sock, "{$field}: {$liste}\r\n"); } } // Le contenu du message est passé en HTML fputs($sock, "Content-Type: text/html; charset=utf-8\r\n"); fputs($sock, "<html>\r\n"); fputs($sock, "<head>\r\n"); fputs($sock, "</head>\r\n"); fputs($sock, "<body>\r\n"); fputs($sock, $Message . "\r\n"); fputs($sock, "</body>\r\n"); fputs($sock, "</html>\r\n"); // Fermeture des données fputs($sock, ".\r\n"); $s = fgets($sock, 512); // On quitte la connexion fputs($sock, "QUIT\r\n"); $s = fgets($sock, 512); fclose($sock); } }
function ping($host, $port, $timeout) { $tB = microtime(true); $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); if (!$fP) { return '<font color="red">' . $host . ' DOWN from here. </font>'; } $tA = microtime(true); return '<font color="green">' . $host . ' ' . round(($tA - $tB) * 1000, 0) . ' ms UP</font>'; }