host = $host; $this->ttl = $ttl; $this->timeout = $timeout; } /** * Set the ttl (in hops). * * @param int $ttl * TTL in hops. */ public function setTtl($ttl) { $this->ttl = $ttl; } /** * Get the ttl. * * @return int * The current ttl for Ping. */ public function getTtl() { return $this->ttl; } /** * Set the timeout. * * @param int $timeout * Time to wait in seconds. */ public function setTimeout($timeout) { $this->timeout = $timeout; } /** * Get the timeout. * * @return int * Current timeout for Ping. */ public function getTimeout() { return $this->timeout; } /** * Set the host. * * @param string $host * Host name or IP address. */ public function setHost($host) { $this->host = $host; } /** * Get the host. * * @return string * The current hostname for Ping. */ public function getHost() { return $this->host; } /** * Set the port (only used for fsockopen method). * * Since regular pings use ICMP and don't need to worry about the concept of * 'ports', this is only used for the fsockopen method, which pings servers by * checking port 80 (by default). * * @param int $port * Port to use for fsockopen ping (defaults to 80 if not set). */ public function setPort($port) { $this->port = $port; } /** * Get the port (only used for fsockopen method). * * @return int * The port used by fsockopen pings. */ public function getPort() { return $this->port; } /** * Return the command output when method=exec. * @return string */ public function getCommandOutput() { return $this->commandOutput; } /** * Matches an IP on command output and returns. * @return string */ public function getIpAddress() { $out = array(); if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->commandOutput, $out)) { return $out[0]; } return null; } /** * Ping a host. * * @param string $method * Method to use when pinging: * - exec (default): Pings through the system ping command. Fast and * robust, but a security risk if you pass through user-submitted data. * - fsockopen: Pings a server on port 80. * - socket: Creates a RAW network socket. Only usable in some * environments, as creating a SOCK_RAW socket requires root privileges. * * @throws InvalidArgumentException if $method is not supported. * * @return mixed * Latency as integer, in ms, if host is reachable or FALSE if host is down. */ public function ping($method = 'exec') { $latency = false; switch ($method) { case 'exec': $latency = $this->pingExec(); break; case 'fsockopen': $latency = $this->pingFsockopen(); break; case 'socket': $latency = $this->pingSocket(); break; default: throw new \InvalidArgumentException('Unsupported ping method.'); } // Return the latency. return $latency; } /** * The exec method uses the possibly insecure exec() function, which passes * the input to the system. This is potentially VERY dangerous if you pass in * any user-submitted data. Be SURE you sanitize your inputs! * * @return int * Latency, in ms. */ private function pingExec() { $latency = false; $ttl = escapeshellcmd($this->ttl); $timeout = escapeshellcmd($this->timeout); $host = escapeshellcmd($this->host); // Exec string for Windows-based systems. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // -n = number of pings; -i = ttl; -w = timeout (in milliseconds). $exec_string = 'ping -n 1 -i ' . $ttl . ' -w ' . ($timeout * 1000) . ' ' . $host; } // Exec string for Darwin based systems (OS X). else if (strtoupper(PHP_OS) === 'DARWIN') { // -n = numeric output; -c = number of pings; -m = ttl; -t = timeout. $exec_string = 'ping -n -c 1 -m ' . $ttl . ' -t ' . $timeout . ' ' . $host; } // Exec string for other UNIX-based systems (Linux). else { // -n = numeric output; -c = number of pings; -t = ttl; -W = timeout $exec_string = 'ping -n -c 1 -t ' . $ttl . ' -W ' . $timeout . ' ' . $host . ' 2>&1'; } exec($exec_string, $output, $return); // Strip empty lines and reorder the indexes from 0 (to make results more // uniform across OS versions). $this->commandOutput = implode($output, ''); $output = array_values(array_filter($output)); // If the result line in the output is not empty, parse it. if (!empty($output[1])) { // Search for a 'time' value in the result line. $response = preg_match("/time(?:=|<)(?