$value) { $value = is_array($value) ? json_encode($value) : $value; WebTotemDB::setOption($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) { WebTotemDB::deleteOption($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) { $sessions = json_decode(self::getOption('sessions'), true) ?: []; $user_id = get_current_user_id(); foreach ($options as $option => $value){ $sessions[$user_id][$option] = $value; } self::setOptions(['sessions' => $sessions]); return TRUE; } /** * Get option from session. * * @param string $option * Option name. * * @return mixed * Returns saved data by option name. */ public static function getSessionOption($option) { $sessions = json_decode(self::getOption('sessions'), true) ?: []; $user_id = get_current_user_id(); if(array_key_exists($user_id, $sessions) and array_key_exists($option, $sessions[$user_id])){ return $sessions[$user_id][$option]; } else { return []; } } /** * 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'], 'multisite_options' => WebTotem::isMultiSite() ]); 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') or get_site_option('wtsec_authorized'); } /** * 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 = json_decode(self::getOption('all_hosts'), true) ?: []; $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() { // Creating a database with plugin settings. if(WebTotemDB::install()){ $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){ 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); } } $api_key = get_site_option('wtotem_api_key'); $am_file = get_site_option('wtsec_am_installed_file'); $waf_file = get_site_option('wtsec_waf_installed_file'); if($api_key){ 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, ]); foreach (self::getAllOptions() as $option) { delete_option('wtotem_' . $option); delete_site_option('wtotem_' .$option); } } } return true; } /** * Check multisite. */ public static function multisiteCheck() { // Check the transition to/from the multisite. if ( ( WebTotem::isMultiSite() && ! WebTotemOption::getOption( 'multisite_options' ) ) or ( ! WebTotem::isMultiSite() && WebTotemOption::getOption( 'multisite_options' ) ) ) { self::setOptions([ 'multisite_options' => WebTotem::isMultiSite() ]); WebTotemAgentManager::removeAgents(); } } }