| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) { |
| 4 |
if (!headers_sent()) { |
| 5 |
header('HTTP/1.1 403 Forbidden'); |
| 6 |
} |
| 7 |
exit(1); |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* WebTotem Option class. |
| 12 |
*/ |
| 13 |
class WebTotemOption { |
| 14 |
|
| 15 |
/** |
| 16 |
* Get all config options name. |
| 17 |
* |
| 18 |
* @param string $option |
| 19 |
* Option name. |
| 20 |
* |
| 21 |
* @return mixed |
| 22 |
* Returns saved data by option name. |
| 23 |
*/ |
| 24 |
public static function getAllOptions() { |
| 25 |
return [ |
| 26 |
'api_key', |
| 27 |
'activated', |
| 28 |
'auth_token_expired', |
| 29 |
'auth_token', |
| 30 |
'am_file', |
| 31 |
'waf_file', |
| 32 |
'av_file', |
| 33 |
'am_installed', |
| 34 |
'av_installed', |
| 35 |
'waf_installed', |
| 36 |
'time_zone_check', |
| 37 |
'time_zone_offset', |
| 38 |
'all_hosts', |
| 39 |
'plugin_version', |
| 40 |
'sessions', |
| 41 |
|
| 42 |
'host_id', |
| 43 |
'host_name', |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get config option. |
| 49 |
* |
| 50 |
* @param string $option |
| 51 |
* Option name. |
| 52 |
* |
| 53 |
* @return mixed |
| 54 |
* Returns saved data by option name. |
| 55 |
*/ |
| 56 |
public static function getOption($option) { |
| 57 |
// Control is passed to get_option() when the MultiSite mode is not used. |
| 58 |
return get_site_option('wtotem_' . $option); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Save multiple configuration options. |
| 63 |
* |
| 64 |
* @param array $options |
| 65 |
* Array of data, key is name of option. |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
* Returns TRUE after setting the options. |
| 69 |
*/ |
| 70 |
public static function setOptions(array $options) { |
| 71 |
|
| 72 |
foreach ($options as $option => $value) { |
| 73 |
//If the function is not used in a MultiSite assembly, then control is passed to |
| 74 |
// the update_option() function with the parameter $autoload = 'no' |
| 75 |
update_site_option('wtotem_' . $option, $value); |
| 76 |
} |
| 77 |
|
| 78 |
return TRUE; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Clear multiple configuration options. |
| 83 |
* |
| 84 |
* @param array $options |
| 85 |
* Array of data, key is name of option. |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
* Returns TRUE after clearing the options. |
| 89 |
*/ |
| 90 |
public static function clearOptions(array $options) { |
| 91 |
|
| 92 |
foreach ($options as $option) { |
| 93 |
delete_option('wtotem_' . $option); |
| 94 |
delete_site_option('wtotem_' . $option); |
| 95 |
} |
| 96 |
|
| 97 |
return TRUE; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Save multiple some options to session. |
| 102 |
* |
| 103 |
* @param array $options |
| 104 |
* Array of data, key is name of option. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
* Returns TRUE after setting the session options. |
| 108 |
*/ |
| 109 |
public static function setSessionOptions(array $options) { |
| 110 |
|
| 111 |
$sessions = self::getOption('sessions') ?: []; |
| 112 |
$user_id = get_current_user_id(); |
| 113 |
|
| 114 |
foreach ($options as $option => $value){ |
| 115 |
$sessions[$user_id][$option] = $value; |
| 116 |
} |
| 117 |
|
| 118 |
self::setOptions(['sessions' => $sessions]); |
| 119 |
|
| 120 |
return TRUE; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Get option from session. |
| 125 |
* |
| 126 |
* @param string $option |
| 127 |
* Option name. |
| 128 |
* |
| 129 |
* @return mixed |
| 130 |
* Returns saved data by option name. |
| 131 |
*/ |
| 132 |
public static function getSessionOption($option) { |
| 133 |
|
| 134 |
$sessions = self::getOption('sessions') ?: []; |
| 135 |
$user_id = get_current_user_id(); |
| 136 |
if(array_key_exists($user_id, $sessions) and array_key_exists($option, $sessions[$user_id])){ |
| 137 |
return $sessions[$user_id][$option]; |
| 138 |
} else { |
| 139 |
return []; |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Save authentication token and token expiration dates in settings. |
| 146 |
* |
| 147 |
* @param array $params |
| 148 |
* Parameters for authorization. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
* Returns TRUE after setting the options. |
| 152 |
*/ |
| 153 |
public static function login(array $params) { |
| 154 |
$token_expired = time() + $params['token']['expiresIn'] - 60; |
| 155 |
|
| 156 |
self::setOptions([ |
| 157 |
'activated' => TRUE, |
| 158 |
'auth_token_expired' => $token_expired, |
| 159 |
'auth_token' => $params['token']['value'], |
| 160 |
'api_key' => $params['api_key'], |
| 161 |
]); |
| 162 |
|
| 163 |
return TRUE; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Checks whether the user has activated the plugin using the API key. |
| 168 |
* |
| 169 |
* @return bool |
| 170 |
* Returns the module activation status. |
| 171 |
*/ |
| 172 |
public static function isActivated() { |
| 173 |
return (boolean) self::getOption('activated') or get_site_option('wtsec_authorized'); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Remove module settings. |
| 178 |
* |
| 179 |
* @return string |
| 180 |
* Returns TRUE after clearing the options. |
| 181 |
*/ |
| 182 |
public static function logout() { |
| 183 |
|
| 184 |
self::clearOptions([ |
| 185 |
'activated', |
| 186 |
'auth_token_expired', |
| 187 |
'auth_token', |
| 188 |
'api_key', |
| 189 |
'host_id', |
| 190 |
'host_name', |
| 191 |
]); |
| 192 |
return TRUE; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Set notification. |
| 197 |
* |
| 198 |
* @param string $type |
| 199 |
* Notification Type. |
| 200 |
* @param string $notice |
| 201 |
* Notification Text. |
| 202 |
*/ |
| 203 |
public static function setNotification($type, $notice) { |
| 204 |
$notifications = self::getSessionOption('notifications') ?: []; |
| 205 |
|
| 206 |
if (array_key_exists($type, $notifications)) { |
| 207 |
if (!in_array($notice, $notifications[$type])) { |
| 208 |
$notifications[$type][] = $notice; |
| 209 |
self::setSessionOptions(['notifications' => $notifications]); |
| 210 |
} |
| 211 |
} |
| 212 |
else { |
| 213 |
$notifications[$type][] = $notice; |
| 214 |
self::setSessionOptions(['notifications' => $notifications]); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get notifications. |
| 221 |
* |
| 222 |
* @return array |
| 223 |
* Notifications array. |
| 224 |
*/ |
| 225 |
public static function getNotificationsData() { |
| 226 |
$types = ['error', 'info', 'warning', 'success']; |
| 227 |
|
| 228 |
$notifications = self::getSessionOption('notifications') ?: []; |
| 229 |
$result = []; |
| 230 |
|
| 231 |
foreach ($types as $type) { |
| 232 |
if (array_key_exists($type, $notifications)) { |
| 233 |
foreach ($notifications[$type] as $notification) { |
| 234 |
$result[] = ['type' => $type, 'notice' => $notification]; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Remove notifications. |
| 240 |
self::setSessionOptions(['notifications' => []]); |
| 241 |
|
| 242 |
return $result; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Set host data. |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
public static function setHost($host_name, $host_id) { |
| 251 |
|
| 252 |
if(WebTotem::isMultiSite()){ |
| 253 |
$blog_id = self::getBlogId($host_name); |
| 254 |
|
| 255 |
add_blog_option($blog_id, 'wtotem_host_id', $host_id); |
| 256 |
add_blog_option($blog_id, 'wtotem_host_name', $host_name); |
| 257 |
|
| 258 |
if(!is_main_site($blog_id)){ |
| 259 |
$all_hosts = self::getOption('all_hosts') ?: []; |
| 260 |
$all_hosts[$host_name] = $host_id; |
| 261 |
|
| 262 |
self::setOptions([ |
| 263 |
'all_hosts' => $all_hosts, |
| 264 |
]); |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
else { |
| 269 |
self::setOptions([ |
| 270 |
'host_id' => $host_id, |
| 271 |
'host_name' => $host_name, |
| 272 |
]); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get host data. |
| 278 |
* |
| 279 |
* @param string $hid |
| 280 |
* Host id. |
| 281 |
* |
| 282 |
* @return array |
| 283 |
* Host data. |
| 284 |
*/ |
| 285 |
public static function getHost($hid = false) { |
| 286 |
if($hid){ |
| 287 |
$all_hosts = self::getAllHosts() ?: []; |
| 288 |
if($all_hosts and in_array($hid, $all_hosts)){ |
| 289 |
return [ |
| 290 |
'id' => $hid, |
| 291 |
'name' => array_search($hid, $all_hosts), |
| 292 |
]; |
| 293 |
} |
| 294 |
} |
| 295 |
return [ |
| 296 |
'id' => get_option('wtotem_host_id'), |
| 297 |
'name' => get_option('wtotem_host_name'), |
| 298 |
]; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Get host data. |
| 303 |
* |
| 304 |
* @return array |
| 305 |
* Host data. |
| 306 |
*/ |
| 307 |
public static function getAllHosts() { |
| 308 |
$all_hosts = self::getOption('all_hosts') ?: []; |
| 309 |
|
| 310 |
$main_host = self::getMainHost(); |
| 311 |
$all_hosts = ($main_host['id']) ? [$main_host['name'] => $main_host['id']] + $all_hosts : $all_hosts; |
| 312 |
|
| 313 |
return $all_hosts; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Get main host data. |
| 318 |
* |
| 319 |
* @return array |
| 320 |
* Main host data. |
| 321 |
*/ |
| 322 |
public static function getMainHost() { |
| 323 |
|
| 324 |
$host['id'] = get_blog_option(0, 'wtotem_host_id'); |
| 325 |
$host['name'] = get_blog_option(0, 'wtotem_host_name'); |
| 326 |
|
| 327 |
return $host; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Delete host data from DB. |
| 332 |
* |
| 333 |
* @return void |
| 334 |
*/ |
| 335 |
public static function clearAllHosts() { |
| 336 |
|
| 337 |
$data = WebTotemAPI::getSites(); |
| 338 |
foreach ($data['edges'] as $site) { |
| 339 |
$site = $site['node']; |
| 340 |
$blog_id = self::getBlogId($site['hostname']); |
| 341 |
delete_blog_option($blog_id, 'wtotem_host_id'); |
| 342 |
delete_blog_option($blog_id, 'wtotem_host_name'); |
| 343 |
} |
| 344 |
|
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Get an array of new sites. |
| 349 |
* |
| 350 |
* @return array |
| 351 |
* Returns either an empty array or an array with new sites. |
| 352 |
*/ |
| 353 |
// public static function checkNewSites() { |
| 354 |
// $hosts = self::getAllHosts(); |
| 355 |
// $sites = get_sites(); |
| 356 |
// $new_sites = []; |
| 357 |
// |
| 358 |
// foreach ($sites as $site){ |
| 359 |
// $host_name = untrailingslashit($site->domain . $site->path); |
| 360 |
// if(!array_key_exists($host_name, $hosts) and !array_key_exists('www.' . $host_name, $hosts)) { |
| 361 |
// $new_sites[] = $host_name; |
| 362 |
// } |
| 363 |
// } |
| 364 |
// return $new_sites; |
| 365 |
// } |
| 366 |
|
| 367 |
/** |
| 368 |
* Get host id from host name. |
| 369 |
* |
| 370 |
* @param $host_name |
| 371 |
* Host name. |
| 372 |
* |
| 373 |
* @return integer |
| 374 |
* Blog id. |
| 375 |
*/ |
| 376 |
public static function getBlogId($host_name){ |
| 377 |
$current_network = get_network(); |
| 378 |
$patterns = [ '/' . $current_network->domain . '/', '/\./', '/\//', ]; |
| 379 |
|
| 380 |
$slug = preg_replace( $patterns, '', $host_name ); |
| 381 |
return ($slug) ? get_id_from_blogname($slug) : 0; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Checking the old version of options. |
| 386 |
* |
| 387 |
* @return boolean |
| 388 |
* If there are old options, it will return true. |
| 389 |
*/ |
| 390 |
public static function checkOldOptions() { |
| 391 |
|
| 392 |
$api_key = get_option('wtsec_api_key'); |
| 393 |
$am_file = get_option('wtsec_am_installed_file'); |
| 394 |
$waf_file = get_option('wtsec_waf_installed_file'); |
| 395 |
|
| 396 |
if($api_key && $am_file && $waf_file){ |
| 397 |
|
| 398 |
self::setOptions([ |
| 399 |
'api_key' => $api_key, |
| 400 |
'am_file' => $am_file, |
| 401 |
'waf_file' => $waf_file, |
| 402 |
'activated' => true, |
| 403 |
'am_installed' => true, |
| 404 |
'av_installed' => true, |
| 405 |
'waf_installed' => true, |
| 406 |
]); |
| 407 |
|
| 408 |
$old_options = [ |
| 409 |
'api_key', |
| 410 |
'api_key_safe', |
| 411 |
'api_key_activated', |
| 412 |
'authorized', |
| 413 |
'authToken', |
| 414 |
'waf_installed_file', |
| 415 |
'am_installed_file', |
| 416 |
'am_installed', |
| 417 |
'logout', |
| 418 |
'av_installed', |
| 419 |
'waf_installed', |
| 420 |
'agents_installed', |
| 421 |
'api_url', |
| 422 |
'color_scheme' , |
| 423 |
'time_zone', |
| 424 |
'token_expired', |
| 425 |
'deactivated', |
| 426 |
'antivirus_event', |
| 427 |
'antivirus_permissions_changed', |
| 428 |
'antivirus_endCursor', |
| 429 |
'antivirus_hasNextPage', |
| 430 |
'firewall_endCursor', |
| 431 |
'firewall_hasNextPage', |
| 432 |
'reports_endCursor', |
| 433 |
'reports_hasNextPage' |
| 434 |
]; |
| 435 |
|
| 436 |
foreach ($old_options as $option) { |
| 437 |
delete_option('wtsec_' . $option); |
| 438 |
delete_site_option('wtsec_' .$option); |
| 439 |
} |
| 440 |
|
| 441 |
return true; |
| 442 |
} |
| 443 |
|
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
} |
| 448 |
|