2026-02-05 17:08:59 +03:00

172 lines
4.8 KiB
PHP

<?php
#[\AllowDynamicProperties]
class OCVMSettings {
public $optionName = 'onecom_vm_settings';
public $defaultSettings = array(
'last_scan' => null,
'vulnerabilities' => array(),
'scan_duration' => 'daily',
'dismiss_duration' => 24,
'self_mwp_intro_mail_sent' => 0,
'settings' => array(
'auto_update' => 1,
'notify_all' => 1,
'notify_admin' => 1,
'custom_emails' => array(),
'email_type' => array(
'email_detect' => 1,
'email_fixed' => 1,
),
),
);
const wpcron_hook = 'ocvm_scan';
const text_domain = OC_PLUGIN_DOMAIN;
const ocvm_endpoint = MIDDLEWARE_URL . '/vulnerability/check';
public $templateDir;
public $module_path;
public $module_url;
public function __construct() {
$this->templateDir = plugin_dir_path( __DIR__ ) . 'templates';
$settings = $this->get();
if ( empty( $settings ) ) {
$this->save_default();
}
// on construct/start check if settings exist.
// save_default if not.
// set other values
$this->module_path = plugin_dir_path( __DIR__ );
$this->module_url = plugin_dir_url( 'module/vulnerability-monitor/' );
}
/**
* Reset/Save default settings
* @param null
* @return void
*/
public function save_default(): bool {
//Disable the settings for non mwp
if ( ! $this->isPremium() ) {
$this->defaultSettings['settings']['auto_update'] = 0;
$this->defaultSettings['settings']['email_type']['email_fixed'] = 0;
}
return update_site_option( $this->optionName, json_encode( $this->defaultSettings ), 'no' );
}
/**
* Validate inputs
*/
public function validate( $array = array() ) {
// if Error call dispatchErrors and die;
// Else all good, return back array
return $array;
}
/**
* Dispatch Ajax errors
*/
public function dispatchErrors(): void {
// die
// send back json and show error on frontend
}
/**
* Update settings (admin_ajax)
*/
public function update( $arr = array() ): array {
$arr = $this->validate( $arr );
update_site_option( $this->optionName, json_encode( $arr ), 'no' );
return $this->get();
}
/**
* Get settings
*/
public function get() {
return json_decode( get_site_option( $this->optionName ), 1 );
}
/**
* Settings stats
* @param array $settings
* @return void
*/
public function settingsStats( $settings = array() ): void {
if ( empty( $settings ) ) {
return;
}
// exclude sensitive data
unset( $settings['custom_emails'] );
class_exists( 'OCPushStats' ) ?
OCPushStats::push_vul_monitor_stats( 'update', 'setting', 'vulnerability_monitor', array( 'settings' => $settings ) ) :
'';
}
public function updateSettings() {
$ajaxAction = $_POST['action'];
if ( $ajaxAction !== 'ocvm_updateSettings' ) {
return;
}
$ocvm_autoUpdateEnable = isset( $_POST['ocvm_autoUpdateEnable'] ) ? $_POST['ocvm_autoUpdateEnable'] : 0;
$ocvm_EmailNotificationEnable = isset( $_POST['ocvm_EmailNotificationEnable'] ) ? $_POST['ocvm_EmailNotificationEnable'] : 0;
$ocvm_notifyAdmin = isset( $_POST['ocvm_notifyAdmin'] ) ? $_POST['ocvm_notifyAdmin'] : 0;
$ocvm_otherEmail = ( isset( $_POST['ocvm_otherEmail'] ) && ! empty( $_POST['ocvm_otherEmail'] ) ) ? explode( ',', $_POST['ocvm_otherEmail'] ) : array();
$ocvm_vmdetectvunerability = isset( $_POST['ocvm_vmdetectvunerability'] ) ? $_POST['ocvm_vmdetectvunerability'] : 0;
$ocvm_vmautofix = isset( $_POST['ocvm_vmautofix'] ) ? $_POST['ocvm_vmautofix'] : 0;
//get values from db and then append updated values and send for update
$get_db_settings = $this->get();
$get_db_settings['settings']['auto_update'] = (int) $ocvm_autoUpdateEnable;
$get_db_settings['settings']['notify_all'] = (int) $ocvm_EmailNotificationEnable;
$get_db_settings['settings']['notify_admin'] = (int) $ocvm_notifyAdmin;
$get_db_settings['settings']['custom_emails'] = $ocvm_otherEmail;
$get_db_settings['settings']['email_type']['email_detect'] = (int) $ocvm_vmdetectvunerability;
$get_db_settings['settings']['email_type']['email_fixed'] = (int) $ocvm_vmautofix;
//update recent settings
$updatedSettings = $this->update( $get_db_settings );
$updatedSettings['success'] = true;
// send stats
$this->settingsStats( $get_db_settings['settings'] );
//execute scan if autoupdate is enabled
if ( $ocvm_autoUpdateEnable ) {
$scan = new OCVMScan();
$scan->scheduleCronAfterAutoupdate();
}
// send response to UI
wp_send_json( $updatedSettings );
}
public function isPremium() {
$features = oc_set_premi_flag();
if (
isset( $features['data'] ) &&
( ! empty( $features['data'] ) ) && ( in_array( 'MWP_ADDON', $features['data'] ) )
) {
return true;
}
return false;
}
}