<commit_before><?php

/**
 * Ping for PHP.
 *
 * This class pings a host.
 *
 * The ping() method pings a server using 'exec', 'socket', or 'fsockopen', and
 * and returns FALSE if the server is unreachable within the given timeout, or
 * the latency in milliseconds if the server is reachable.
 *
 * Example usage:
 * @code
 *   $ping = new Ping('www.example.com');
 *   $latency = $ping->ping();
 * @endcode
 *
 * More usage examples can be found in the included README file, and all methods
 * should have adequate documentation to get you started.
 *
 * @version 1.0-beta1
 * @author <NAME>.
 */

class Ping {

  private $host;
  private $timeout;
  private $data = 'Ping';

  /**
   * Called when the Ping object is created.
   *
   * @param $host (string)
   *   The host to be pinged.
   * @param $timeout (int)
   *   Timeout in seconds.
   *
   * @return (empty)
   */
  public function __construct($host, $timeout = 5) {
    if (!isset($host)) {
      throw new Exception("Error: Host name not supplied.");
    }

    $this->host = $host;
    $this->timeout = $timeout;
  }

  /**
   * Set the timeout (in seconds).
   *
   * @param $timeout (int)
   *   Timeout in seconds.
   *
   * @return (empty)
   */
  public function setTimeout($timeout) {
    $this->timeout = $timeout;
  }

  /**
   * Set the host.
   *
   * @param $host (string)
   *   Host name or IP address.
   *
   * @return (empty)
   */
  public function setHost($host) {
    $this->host = $host;
  }

  /**
   * Ping a host.
   *
   * @param $method (string)
   *   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.
   *
   * @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) {
      // 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!
      case 'exec':
        $timeout = escapeshellcmd($this->timeout);
        $host = escapeshellcmd($this->host);
        // -n = numeric output; -c = number of pings; -t = timeout.
        $str = exec('ping -n -c 1 -t ' . $timeout . ' ' . $host, $output, $return);
        // Second output line contains result of ping. Parse if not empty.
        if (!empty($output[1])) {
          $array = explode(' ', $output[1]);
          // Remove 'time=' from string.
          $latency = str_replace('time=', '', $array[6]);
          // Convert latency to microseconds.
          $latency = round($latency);
        }
        else {
          $latency = false;
        }
        break;

      // The fsockopen method simply tries to reach the host on port 80. This
      // method is often the fastest, but not necessarily the most reliable.
      // Even if a host doesn't respond, fsockopen may still make a connection.
      case 'fsockopen':
        $start = microtime(true);
        $fp = fsockopen($this->host, 80, $errno, $errstr, $this->timeout);
        if (!$fp) {
          $latency = false;
        }
        else {
          $latency = microtime(true) - $start;
          $latency = round($latency * 1000);
        }
        break;
<commit_msg>Issue #1: Fix TTL/Timeout Confusion.<commit_after><?php

/**
 * Ping for PHP.
 *
 * This class pings a host.
 *
 * The ping() method pings a server using 'exec', 'socket', or 'fsockopen', and
 * and returns FALSE if the server is unreachable within the given ttl/timeout,
 * or the latency in milliseconds if the server is reachable.
 *
 * Example usage:
 * @code
 *   $ping = new Ping('www.example.com');
 *   $latency = $ping->ping();
 * @endcode
 *
 * @version 1.0-beta1
 * @author <NAME>.
 */

class Ping {

  private $host;
  private $ttl;
  private $data = 'Ping';

  /**
   * Called when the Ping object is created.
   *
   * @param $host (string)
   *   The host to be pinged.
   * @param $ttl (int)
   *   Time-to-live (TTL) (You may get a 'Time to live exceeded' error if this
   *   value is set too low. The TTL value indicates the scope or range in which
   *   a packet may be forwarded. By convention:
   *     - 0 = same host
   *     - 1 = same subnet
   *     - 32 = same site
   *     - 64 = same region
   *     - 128 = same continent
   *     - 255 = unrestricted
   *   The TTL is also used as a general 'timeout' value for fsockopen(), so if
   *   you are using that method, you might want to set a default of 5-10 sec to
   *   avoid blocking network connections.
   *
   * @return (empty)
   */
  public function __construct($host, $ttl = 255) {
    if (!isset($host)) {
      throw new Exception("Error: Host name not supplied.");
    }

    $this->host = $host;
    $this->ttl = $ttl;
  }

  /**
   * Set the ttl (in hops).
   *
   * @param $ttl (int)
   *   TTL in hops.
   *
   * @return (empty)
   */
  public function setTtl($ttl) {
    $this->ttl = $ttl;
  }

  /**
   * Set the host.
   *
   * @param $host (string)
   *   Host name or IP address.
   *
   * @return (empty)
   */
  public function setHost($host) {
    $this->host = $host;
  }

  /**
   * Ping a host.
   *
   * @param $method (string)
   *   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.
   *
   * @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) {
      // 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!
      case 'exec':
        $ttl = escapeshellcmd($this->ttl);
        $host = escapeshellcmd($this->host);
        // -n = numeric output; -c = number of pings; -t = ttl.
        $str = exec('ping -n -c 1 -t ' . $ttl . ' ' . $host, $output, $return);
        // Second output line contains result of ping. Parse if not empty.
        if (!empty($output[1])) {
          $array = explode(' ', $output[1]);
          // Remove 'time=' from string.
          $latency = str_replace('time=', '', $array[6]);
          // Convert latency to microseconds.
          $latency = round($latency);
        }
        else {
          $latency = false;
        }
        break;

      // The fsockopen method simply tries to reach the host on port 80. This
      // method is often the fastest, but not necessarily the most reliable.
      // Even if a host doesn't respond, fsockopen may still make a connection.
      case 'fsockopen':
        $start = microtime(true);
        $fp = fsockopen($this->host, 80, $errno, $errstr, $this->ttl);
        if (!$fp) {
          $latency = false;
        }
        else {
          $latency = microtime(true) - $start;
          $latency = round($latency * 1000);
        }
        break;

{'fraction_non_alphanumeric': 0.12527964205816555, 'fraction_numerical': 0.007106198183971575, 'mean_word_length': 2.134020618556701, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4747857', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2340, 'n_words': 859}