You are here

class Server in Anti Spam by CleanTalk 9.1.x

Same name and namespace in other branches
  1. 8.4 src/lib/Cleantalk/Common/Variables/Server.php \Cleantalk\Common\Variables\Server

Class Server Wrapper to safely get $_SERVER variables

@package \CleantalkSP\Variables

Hierarchy

Expanded class hierarchy of Server

8 files declare their use of Server
AntiCrawler.php in src/lib/Cleantalk/Common/Firewall/Modules/AntiCrawler.php
AntiFlood.php in src/lib/Cleantalk/Common/Firewall/Modules/AntiFlood.php
BootSubscriber.php in src/EventSubscriber/BootSubscriber.php
CleantalkFuncs.php in src/CleantalkFuncs.php
Firewall.php in src/lib/Cleantalk/Common/Firewall/Firewall.php

... See full list

File

src/lib/Cleantalk/Common/Variables/Server.php, line 11

Namespace

Cleantalk\Common\Variables
View source
class Server extends SuperGlobalVariables {
  public static $instance;

  /**
   * Gets given $_SERVER variable and save it to memory
   *
   * @param string $name
   *
   * @return mixed|string
   */
  protected function get_variable($name) {

    // Return from memory. From $this->server
    if (isset(static::$instance->variables[$name])) {
      return static::$instance->variables[$name];
    }
    $name = strtoupper($name);
    if (function_exists('filter_input')) {
      $value = filter_input(INPUT_SERVER, $name);
    }
    if (empty($value)) {
      $value = isset($_SERVER[$name]) ? $_SERVER[$name] : '';
    }

    // Convert to upper case for REQUEST_METHOD
    if (in_array($name, array(
      'REQUEST_METHOD',
    ))) {
      $value = strtoupper($value);
    }

    // Convert to lower case for REQUEST_METHOD
    if (in_array($name, array(
      'HTTPS',
    ))) {
      $value = strtolower($value);
    }

    // Convert HTML chars for HTTP_USER_AGENT, HTTP_USER_AGENT, SERVER_NAME
    if (in_array($name, array(
      'HTTP_USER_AGENT',
      'HTTP_USER_AGENT',
      'SERVER_NAME',
    ))) {
      $value = htmlspecialchars($value);
    }

    // Remember for thurther calls
    static::getInstance()
      ->remember_variable($name, $value);
    return $value;
  }

  /**
   * Checks if $_SERVER['REQUEST_URI'] contains string
   *
   * @param string $needle
   *
   * @return bool
   */
  public static function in_uri($needle) {
    return self::has_string('REQUEST_URI', $needle);
  }
  public static function in_host($needle) {
    return self::has_string('HTTP_HOST', $needle);
  }
  public static function get_domain() {
    preg_match('@\\.(\\S+)/?$@', self::get('HTTP_HOST'), $matches);
    return isset($matches[1]) ? $matches[1] : false;
  }
  public static function getHomeURL($scheme = null) {
    return (self::isSSL() ? 'https' : self::get('REQUEST_SCHEME')) . '://' . self::get('HTTP_HOST') . '/';
  }

  /**
   * Checks if $_SERVER['REQUEST_URI'] contains string
   *
   * @param string $needle needle
   *
   * @return bool
   */
  public static function in_referer($needle) {
    return self::has_string('HTTP_REFERER', $needle);
  }

  /**
   * Checks if $_SERVER['REQUEST_URI'] contains string
   *
   * @return bool
   */
  public static function is_post() {
    return self::get('REQUEST_METHOD') === 'POST';
  }

  /**
   * Determines if SSL is used.
   *
   * @return bool True if SSL, otherwise false.
   */
  public static function isSSL() {
    if (self::get('HTTPS') === 'on' || self::get('HTTPS') === '1' || self::get('SERVER_PORT') == '443') {
      return true;
    }
    return false;
  }
  public static function getURL() {
    return substr(self::getHomeURL(), 0, -1) . self::get('REQUEST_URI');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Server::$instance public static property Overrides Singleton::$instance
Server::getHomeURL public static function
Server::getURL public static function
Server::get_domain public static function
Server::get_variable protected function * Gets given $_SERVER variable and save it to memory * * Overrides SuperGlobalVariables::get_variable
Server::in_host public static function
Server::in_referer public static function * Checks if $_SERVER['REQUEST_URI'] contains string * *
Server::in_uri public static function * Checks if $_SERVER['REQUEST_URI'] contains string * *
Server::isSSL public static function Determines if SSL is used.
Server::is_post public static function * Checks if $_SERVER['REQUEST_URI'] contains string * *
Singleton::getInstance public static function Constructor
Singleton::init protected function Alternative constructor
Singleton::__clone public function
Singleton::__construct public function
Singleton::__wakeup public function
SuperGlobalVariables::$variables public property *
SuperGlobalVariables::equal static function * Checks if variable equal to $param * *
SuperGlobalVariables::get public static function * Gets variable from ${_SOMETHING} * *
SuperGlobalVariables::has_string static function * Checks if variable contains given string * *
SuperGlobalVariables::remember_variable protected function * Save variable to $this->variables[] * *