# wt-security/2.4.2/lib/Option.php

WebTotem Security, version 2.4.2. 441 lines.

- Page: https://pluginprobe.com/plugins/wt-security/2.4.2/code/lib/Option.php
- Raw: https://pluginprobe.com/plugins/wt-security/2.4.2/raw/lib/Option.php
- Modified: 2022-05-05T03:36:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wt-security/2.4.2/code/lib/Option.php#L10-L20`.

```php
<?php

if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
	if (!headers_sent()) {
		header('HTTP/1.1 403 Forbidden');
	}
	exit(1);
}

/**
 * WebTotem Option class.
 */
class WebTotemOption {

	/**
	 * Get all config options name.
	 *
	 * @param string $option
	 *   Option name.
	 *
	 * @return mixed
	 *   Returns saved data by option name.
	 */
	public static function getAllOptions() {
		return [
			'api_key',
			'activated',
			'auth_token_expired',
			'auth_token',
			'am_file',
			'waf_file',
			'av_file',
			'am_installed',
			'av_installed',
			'waf_installed',
			'time_zone_check',
			'time_zone_offset',
			'all_hosts',
			'plugin_version',

			'host_id',
			'host_name',
		];
	}

  /**
   * Get config option.
   *
   * @param string $option
   *   Option name.
   *
   * @return mixed
   *   Returns saved data by option name.
   */
  public static function getOption($option) {
  	// Control is passed to get_option() when the MultiSite mode is not used.
    return get_site_option('wtotem_' . $option);
  }

  /**
   * Save multiple configuration options.
   *
   * @param array $options
   *   Array of data, key is name of option.
   *
   * @return bool
   *   Returns TRUE after setting the options.
   */
  public static function setOptions(array $options) {

    foreach ($options as $option => $value) {
    	//If the function is not used in a MultiSite assembly, then control is passed to
	    // the update_option() function with the parameter $autoload = 'no'
	    update_site_option('wtotem_' . $option, $value);
    }

    return TRUE;
  }

  /**
   * Clear multiple configuration options.
   *
   * @param array $options
   *   Array of data, key is name of option.
   *
   * @return bool
   *   Returns TRUE after clearing the options.
   */
  public static function clearOptions(array $options) {

    foreach ($options as $option) {
	    delete_option('wtotem_' . $option);
	    delete_site_option('wtotem_' . $option);
    }

    return TRUE;
  }

  /**
   * Save multiple some options to session.
   *
   * @param array $options
   *   Array of data, key is name of option.
   *
   * @return bool
   *   Returns TRUE after setting the session options.
   */
  public static function setSessionOptions(array $options) {

    foreach ($options as $option => $value) {
	    $_SESSION['wtotem.' . $option] = $value;
    }
    return TRUE;
  }

  /**
   * Get option from session.
   *
   * @param string $option
   *   Option name.
   *
   * @return mixed
   *   Returns saved data by option name.
   */
  public static function getSessionOption($option) {
  	if(!isset($_SESSION)) {
  		return [];
	  }
	  if(isset($_SESSION['wtotem.' . $option])){
		  return $_SESSION['wtotem.' . $option];
	  }
	  else {
  		return FALSE;
	  }
  }

  /**
   * Save authentication token and token expiration dates in settings.
   *
   * @param array $params
   *   Parameters for authorization.
   *
   * @return string
   *   Returns TRUE after setting the options.
   */
  public static function login(array $params) {
    $token_expired = time() + $params['token']['expiresIn'] - 60;

    self::setOptions([
      'activated' => TRUE,
      'auth_token_expired' => $token_expired,
      'auth_token' => $params['token']['value'],
      'api_key' => $params['api_key'],
    ]);

    return TRUE;
  }

  /**
   * Checks whether the user has activated the plugin using the API key.
   *
   * @return bool
   *   Returns the module activation status.
   */
  public static function isActivated() {
    return (boolean) self::getOption('activated');
  }

  /**
   * Remove module settings.
   *
   * @return string
   *   Returns TRUE after clearing the options.
   */
  public static function logout() {

    self::clearOptions([
      'activated',
      'auth_token_expired',
      'auth_token',
      'api_key',
	    'host_id',
	    'host_name',
    ]);
    return TRUE;
  }

  /**
   * Set notification.
   *
   * @param string $type
   *   Notification Type.
   * @param string $notice
   *   Notification Text.
   */
  public static function setNotification($type, $notice) {
    $notifications = self::getSessionOption('notifications') ?: [];

    if (array_key_exists($type, $notifications)) {
      if (!in_array($notice, $notifications[$type])) {
        $notifications[$type][] = $notice;
        self::setSessionOptions(['notifications' => $notifications]);
      }
    }
    else {
      $notifications[$type][] = $notice;
      self::setSessionOptions(['notifications' => $notifications]);
    }

  }

  /**
   * Get notifications.
   *
   * @return array
   *   Notifications array.
   */
  public static function getNotificationsData() {
    $types = ['error', 'info', 'warning', 'success'];

    $notifications = self::getSessionOption('notifications') ?: [];
	  $result = [];

    foreach ($types as $type) {
      if (array_key_exists($type, $notifications)) {
        foreach ($notifications[$type] as $notification) {
          $result[] = ['type' => $type, 'notice' => $notification];
        }
      }
    }

    // Remove notifications.
    self::setSessionOptions(['notifications' => []]);

    return $result;
  }

	/**
	 * Set host data.
	 *
	 * @return void
	 */
	public static function setHost($host_name, $host_id) {

		if(WebTotem::isMultiSite()){
			$blog_id = self::getBlogId($host_name);

			add_blog_option($blog_id, 'wtotem_host_id', $host_id);
			add_blog_option($blog_id, 'wtotem_host_name', $host_name);

			if(!is_main_site($blog_id)){
				$all_hosts = self::getOption('all_hosts') ?: [];
				$all_hosts[$host_name] = $host_id;

				self::setOptions([
					'all_hosts' => $all_hosts,
				]);
			}

		}
		else {
			self::setOptions([
				'host_id' => $host_id,
				'host_name' => $host_name,
			]);
		}
	}

	/**
	 * Get host data.
	 *
	 * @param string $hid
	 *   Host id.
	 *
	 * @return array
	 *   Host data.
	 */
	public static function getHost($hid = false) {
		if($hid){
			$all_hosts = self::getAllHosts() ?: [];
			if($all_hosts and in_array($hid, $all_hosts)){
				return [
					'id' => $hid,
					'name' => array_search($hid, $all_hosts),
				];
			}
		}
		return [
			'id' => get_option('wtotem_host_id'),
			'name' => get_option('wtotem_host_name'),
		];
	}

	/**
	 * Get host data.
	 *
	 * @return array
	 *   Host data.
	 */
	public static function getAllHosts() {
		$all_hosts = self::getOption('all_hosts') ?: [];

		$main_host = self::getMainHost();
		$all_hosts = ($main_host['id']) ? [$main_host['name'] => $main_host['id']] + $all_hosts : $all_hosts;

		return $all_hosts;
	}

	/**
	 * Get main host data.
	 *
	 * @return array
	 *   Main host data.
	 */
	public static function getMainHost() {

		$host['id'] = get_blog_option(0, 'wtotem_host_id');
		$host['name'] = get_blog_option(0, 'wtotem_host_name');

		return $host;
	}

	/**
	 * Delete host data from DB.
	 *
	 * @return void
	 */
	public static function clearAllHosts() {

		$data = WebTotemAPI::getSites();
		foreach ($data['edges'] as $site) {
			$site = $site['node'];
			$blog_id = self::getBlogId($site['hostname']);
			delete_blog_option($blog_id, 'wtotem_host_id');
			delete_blog_option($blog_id, 'wtotem_host_name');
		}

	}

	/**
	 * Get an array of new sites.
	 *
	 * @return array
	 *   Returns either an empty array or an array with new sites.
	 */
//	public static function checkNewSites() {
//		$hosts = self::getAllHosts();
//		$sites = get_sites();
//		$new_sites = [];
//
//		foreach ($sites as $site){
//			$host_name = untrailingslashit($site->domain . $site->path);
//			if(!array_key_exists($host_name, $hosts) and !array_key_exists('www.' . $host_name, $hosts)) {
//				$new_sites[] = $host_name;
//			}
//		}
//		return $new_sites;
//	}

	/**
	 * Get host id from host name.
	 *
	 * @param $host_name
	 *   Host name.
	 *
	 * @return integer
	 *   Blog id.
	 */
	public static function getBlogId($host_name){
		$current_network = get_network();
		$patterns = [ '/' . $current_network->domain . '/', '/\./', '/\//', ];

		$slug = preg_replace( $patterns, '', $host_name );
		return ($slug) ? get_id_from_blogname($slug) : 0;
	}

	/**
	 * Checking the old version of options.
	 *
	 * @return boolean
	 *   If there are old options, it will return true.
	 */
	public static function checkOldOptions() {

		$api_key = get_option('wtsec_api_key');
		$am_file = get_option('wtsec_am_installed_file');
		$waf_file = get_option('wtsec_waf_installed_file');

		if($api_key && $am_file && $waf_file){

			self::setOptions([
				'api_key' => $api_key,
				'am_file' => $am_file,
				'waf_file' => $waf_file,
				'activated' => true,
				'am_installed' => true,
				'av_installed' => true,
				'waf_installed' => true,
			]);

			$old_options = [
				'api_key',
				'api_key_safe',
				'api_key_activated',
				'authorized',
				'authToken',
				'waf_installed_file',
				'am_installed_file',
				'am_installed',
				'logout',
				'av_installed',
				'waf_installed',
				'agents_installed',
				'api_url',
				'color_scheme' ,
				'time_zone',
				'token_expired',
				'deactivated',
				'antivirus_event',
				'antivirus_permissions_changed',
				'antivirus_endCursor',
				'antivirus_hasNextPage',
				'firewall_endCursor',
				'firewall_hasNextPage',
				'reports_endCursor',
				'reports_hasNextPage'
			];

			foreach ($old_options as $option) {
				delete_option('wtsec_' . $option);
				delete_site_option('wtsec_' .$option);
			}

			return true;
		}

		return false;
	}

}

```
