get_option( 'activity-extension-activated', $default ); } /** * Updates extension activated option. * * @param string $value - Value of the option. * @return boolean */ public function set_extension_activated( $value ) { return $this->update_option( 'activity-extension-activated', $value ); } /** * Datetime used in the Alerts. * * @param bool $line_break – Line break. */ public function get_date_time_format( $line_break = true ) { if ( $line_break ) { $date_time_format = $this->get_date_format() . '<\b\r>' . $this->get_time_format(); } else { $date_time_format = $this->get_date_format() . ' ' . $this->get_time_format(); } $wp_time_format = get_option( 'time_format' ); if ( stripos( $wp_time_format, 'A' ) !== false ) { $date_time_format .= '.$$$&\n\b\s\p;A'; } else { $date_time_format .= '.$$$'; } return $date_time_format; } /** * Date Format from WordPress General Settings. */ public function get_date_format() { $wp_date_format = get_option( 'date_format' ); $search = array( 'F', 'M', 'n', 'j', ' ', '/', 'y', 'S', ',', 'l', 'D' ); $replace = array( 'm', 'm', 'm', 'd', '-', '-', 'Y', '', '', '', '' ); $date_format = str_replace( $search, $replace, $wp_date_format ); return $date_format; } /** * Time Format from WordPress General Settings. */ public function get_time_format() { $wp_time_format = get_option( 'time_format' ); $search = array( 'a', 'A', 'T', ' ' ); $replace = array( '', '', '', '' ); $time_format = str_replace( $search, $replace, $wp_time_format ); return $time_format; } /** * Get MainWP Child Sites. * * @return array */ public function get_mwp_child_sites() { $activity_log = \WSAL\MainWPExtension\Activity_Log::get_instance(); return apply_filters( 'mainwp-getsites', $activity_log->get_child_file(), $activity_log->get_child_key(), null ); } /** * Set Alert Views Per Page. * * @param int $newvalue – New value. */ public function set_view_per_page( $newvalue ) { $perpage = max( $newvalue, 1 ); $this->update_option( 'items-per-page', $perpage ); } /** * Get Alert Views Per Page. * * @return int */ public function get_view_per_page() { return (int) $this->get_option( 'items-per-page', 10 ); } /** * Return Site ID. * * @return integer */ public function get_view_site_id() { // @codingStandardsIgnoreStart $site_id = isset( $_GET['mwpal-site-id'] ) ? sanitize_text_field( $_GET['mwpal-site-id'] ) : 0; // Site ID. // @codingStandardsIgnoreEnd if ( 'dashboard' !== $site_id ) { return (int) $site_id; } return $site_id; } /** * Method: Get number of hours since last logged alert. * * @return mixed – False if $created_on is empty | Number of hours otherwise. * * @param float $created_on – Timestamp of last logged alert. */ public function get_hours_since_last_alert( $created_on ) { // If $created_on is empty, then return. if ( empty( $created_on ) ) { return false; } // Last alert date. $created_date = new \DateTime( date( 'Y-m-d H:i:s', $created_on ) ); // Current date. $current_date = new \DateTime( 'NOW' ); // Calculate time difference. $time_diff = $current_date->diff( $created_date ); $diff_days = $time_diff->d; // Difference in number of days. $diff_hrs = $time_diff->h; // Difference in number of hours. $total_hrs = ( $diff_days * 24 ) + $diff_hrs; // Total number of hours. // Return difference in hours. return $total_hrs; } /** * Return audit log columns. * * @return array */ public function get_columns() { $columns = array( 'site' => '1', 'alert_code' => '1', 'type' => '1', 'date' => '1', 'username' => '1', 'source_ip' => '1', 'message' => '1', ); // Get selected columns. $selected = $this->get_columns_selected(); if ( ! empty( $selected ) ) { $columns = array( 'site' => '0', 'alert_code' => '0', 'type' => '0', 'date' => '0', 'username' => '0', 'source_ip' => '0', 'message' => '0', ); $selected = (array) json_decode( $selected ); $columns = array_merge( $columns, $selected ); } return $columns; } /** * Get Selected Columns. * * @return string */ public function get_columns_selected() { return $this->get_option( 'columns' ); } /** * Set Columns. * * @param array $columns – Columns. */ public function set_columns( $columns ) { $this->update_option( 'columns', wp_json_encode( $columns ) ); } /** * Get WSAL Child Sites. * * @return array */ public function get_wsal_child_sites() { // Check if the WSAL child sites option exists. $child_sites = $this->get_option( 'wsal-child-sites' ); // Get MainWP Child sites. $mwp_sites = $this->get_mwp_child_sites(); $activity_log = \WSAL\MainWPExtension\Activity_Log::get_instance(); if ( empty( $child_sites ) && ! is_array( $child_sites ) && ! empty( $mwp_sites ) ) { foreach ( $mwp_sites as $site ) { $post_data = array( 'action' => 'check_wsal' ); // Call to child sites to check if WSAL is installed on them or not. $results[ $site['id'] ] = apply_filters( 'mainwp_fetchurlauthed', $activity_log->get_child_file(), $activity_log->get_child_key(), $site['id'], 'extra_excution', $post_data ); } if ( ! empty( $results ) && is_array( $results ) ) { $child_sites = array(); foreach ( $results as $site_id => $site_obj ) { if ( empty( $site_obj ) ) { continue; } elseif ( true === $site_obj->wsal_installed ) { $child_sites[ $site_id ] = $site_obj; } } $this->update_option( 'wsal-child-sites', $child_sites ); } } return $child_sites; } /** * Set WSAL Child Sites. * * @param array $site_ids – Array of Site ids. * @return void */ public function set_wsal_child_sites( $site_ids ) { $wsal_sites = $this->get_wsal_child_sites(); // Get WSAL child sites. $disabled_sites = $this->get_option( 'disabled-wsal-sites', array() ); // Get already disabled sites. $new_sites = array(); // Set new WSAL sites. if ( ! empty( $site_ids ) && is_array( $site_ids ) ) { foreach ( $site_ids as $id ) { if ( isset( $wsal_sites[ $id ] ) ) { $new_sites[ $id ] = $wsal_sites[ $id ]; unset( $wsal_sites[ $id ] ); } elseif ( isset( $disabled_sites[ $id ] ) ) { $new_sites[ $id ] = $disabled_sites[ $id ]; unset( $disabled_sites[ $id ] ); } else { $new_sites[ $id ] = new \stdClass(); } } } // Remove events of removed sites from the DB. if ( ! empty( $wsal_sites ) && is_array( $wsal_sites ) ) { foreach ( $wsal_sites as $site_id => $site ) { // Delete events by site id. $delete_query = new \WSAL\MainWPExtension\Models\OccurrenceQuery(); $delete_query->addCondition( 'site_id = %s ', $site_id ); $delete_query->getAdapter()->Delete( $delete_query ); $disabled_sites[ $site_id ] = $site; } } $this->update_option( 'wsal-child-sites', $new_sites ); $this->update_option( 'disabled-wsal-sites', $disabled_sites ); } /** * Get Timezone. * * @return string */ public function get_timezone() { return $this->get_option( 'timezone', 'wp' ); } /** * Set Timezone. * * @param string $newvalue – New value. */ public function set_timezone( $newvalue ) { $this->update_option( 'timezone', $newvalue ); } /** * Get Username Type. * * @return string */ public function get_type_username() { return $this->get_option( 'type_username', 'display_name' ); } /** * Set Username Type. * * @param string $newvalue – New value. */ public function set_type_username( $newvalue ) { $this->update_option( 'type_username', $newvalue ); } /** * Get number of child site events. * * @return integer */ public function get_child_site_events() { return $this->get_option( 'child_site_events', 100 ); } /** * Set number of child site events. * * @param integer $newvalue – New value. */ public function set_child_site_events( $newvalue ) { $this->update_option( 'child_site_events', $newvalue ); } /** * Get Events Frequency. * * @return integer */ public function get_events_frequency() { return $this->get_option( 'events_frequency', 3 ); } /** * Set Events Frequency. * * @param integer $newvalue – New value. */ public function set_events_frequency( $newvalue ) { $this->update_option( 'events_frequency', $newvalue ); } /** * Get Current User Roles. * * @param array $base_roles – Base roles. * @return array */ public function get_current_user_roles( $base_roles = null ) { if ( null === $base_roles ) { $base_roles = wp_get_current_user()->roles; } if ( function_exists( 'is_super_admin' ) && is_super_admin() ) { $base_roles[] = 'superadmin'; } return $base_roles; } /** * Check if the user is super admin. * * @param string $username – Username. * @return boolean */ public function is_login_super_admin( $username ) { $user_id = username_exists( $username ); if ( function_exists( 'is_super_admin' ) && is_super_admin( $user_id ) ) { return true; } else { return false; } } /** * Get Server IP. * * @return string */ public function get_server_ip() { $result = null; if ( isset( $_SERVER['SERVER_ADDR'] ) ) { $result = $this->normalize_ip( sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) ); if ( ! $this->validate_ip( $result ) ) { $result = 'Error ' . self::ERROR_CODE_INVALID_IP . ': Invalid IP Address'; } } return $result; } /** * Get Client IP. * * @return string */ public function get_main_client_ip() { $result = null; if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { $result = $this->normalize_ip( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ); if ( ! $this->validate_ip( $result ) ) { $result = 'Error ' . self::ERROR_CODE_INVALID_IP . ': Invalid IP Address'; } } return $result; } /** * Normalize IP Address. * * @param string $ip – IP Address. * @return string */ protected function normalize_ip( $ip ) { $ip = trim( $ip ); if ( strpos( $ip, ':' ) !== false && substr_count( $ip, '.' ) == 3 && strpos( $ip, '[' ) === false ) { // IPv4 with a port (eg: 11.22.33.44:80). $ip = explode( ':', $ip ); $ip = $ip[0]; } else { // IPv6 with a port (eg: [::1]:80). $ip = explode( ']', $ip ); $ip = ltrim( $ip[0], '[' ); } return $ip; } /** * Validate IP Address. * * @param string $ip – IP Address. * @return string */ protected function validate_ip( $ip ) { $opts = FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6; $filtered_ip = filter_var( $ip, FILTER_VALIDATE_IP, $opts ); if ( ! $filtered_ip || empty( $filtered_ip ) ) { // Regex IPV4. if ( preg_match( '/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip ) ) { return $ip; } // Regex IPV6. elseif ( preg_match( '/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/', $ip ) ) { return $ip; } return false; } else { return $filtered_ip; } } /** * Search & Return MainWP site. * * @param string $column – Column name. * @param string $value – Column value. * @return mixed */ public function get_mwp_site_by( $column = 'id', $value ) { // Get MainWP sites. $mwp_sites = $this->get_mwp_child_sites(); // Search by column name. $key = array_search( $value, array_column( $mwp_sites, $column ), true ); if ( false !== $key ) { return $mwp_sites[ $key ]; } return false; } /** * Get last checked timestamp by site id. * * @since 1.0.1 * * @param integer $site_id - Site id. * @return mixed */ public function get_last_checked_by_siteid( $site_id = 0 ) { if ( $site_id ) { return $this->get_option( 'mwpal_last_checked_site_' . $site_id ); } return false; } /** * Set last checked timestamp by site id. * * @since 1.0.1 * * @param integer $site_id - Site id. * @param string $last_checked - Last checked timestamp. */ public function set_last_checked_by_siteid( $site_id = 0, $last_checked ) { $activity_log = \WSAL\MainWPExtension\Activity_Log::get_instance(); if ( $site_id ) { $this->update_option( 'mwpal_last_checked_site_' . $site_id, $last_checked ); } } }