You are here

class Utility in Rocket.Chat 8

Same name and namespace in other branches
  1. 8.2 src/Utility.php \Drupal\rocket_chat\Utility

Check the form values.

Hierarchy

Expanded class hierarchy of Utility

1 file declares its use of Utility
RocketChatSettingsForm.php in src/Form/RocketChatSettingsForm.php
Contains \Drupal\rocket_chat\Form\RocketChatSettingsForm.

File

src/Utility.php, line 33
Contains \Drupal\rocket_chat\FormManager.

Namespace

Drupal\rocket_chat
View source
class Utility {

  /**
   * ServerRun.
   *
   * @param string $url
   *   Url to use.
   *
   * @return bool
   *   Connection Worked?
   */
  public static function serverRun($url) {
    $urlSplit = Utility::parseUrl($url);
    try {
      if ($ping = fsockopen($urlSplit['url'], $urlSplit['port'], $errCode, $errStr, 10)) {
        fclose($ping);
        return TRUE;
      }
      else {
        return FALSE;
      }
    } catch (\Exception $exception) {
      error_log("serverRun encountered and exception, check [{$url}] for valid URL");
      return FALSE;
    }
  }

  /**
   * Helper function to split an URL into its base components.
   *
   * @param string $url
   *   Url to parse.
   *
   * @return array
   *   Url in its separated Parts.
   *
   * @throws \HttpUrlException
   *   Throws when scheme is missing.
   */
  public static function parseUrl($url) {
    $returnValue = parse_url($url);
    if (!isset($returnValue['scheme'])) {
      throw new \HttpUrlException("Missing Scheme.", 404);
    }
    if (!isset($returnValue['host'])) {
      $returnValue['hosts'] = 'localhost';
    }
    if (!isset($returnValue['path'])) {
      $returnValue['path'] = "";
    }
    if (!isset($returnValue['port'])) {
      switch ($returnValue['scheme']) {
        case "http":
          $returnValue['port'] = 80;
          break;
        case "https":
          $returnValue['port'] = 443;
          break;
      }
    }
    $returnValue['baseUrl'] = $returnValue['host'] . $returnValue['path'];
    switch ($returnValue['scheme']) {
      default:
        $returnValue['url'] = "tcp://" . $returnValue['baseUrl'];
        break;
      case "https":
        $returnValue['url'] = "tls://" . $returnValue['baseUrl'];
        break;
    }
    return $returnValue;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Utility::parseUrl public static function Helper function to split an URL into its base components.
Utility::serverRun public static function ServerRun.