| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) exit; |
| 4 |
if (!class_exists('WPRWatchCallback')) : |
| 5 |
|
| 6 |
class WPRWatchCallback extends WPRCallbackBase { |
| 7 |
public $db; |
| 8 |
public $settings; |
| 9 |
|
| 10 |
const WATCH_WING_VERSION = 1.7; |
| 11 |
|
| 12 |
public function __construct($callback_handler) { |
| 13 |
$this->db = $callback_handler->db; |
| 14 |
$this->settings = $callback_handler->settings; |
| 15 |
} |
| 16 |
|
| 17 |
public function getData($table, $limit = 0, $filter = "") { |
| 18 |
$result = array(); |
| 19 |
$data = array(); |
| 20 |
$rows = $this->db->getTableContent($table, '*', $filter, $limit); |
| 21 |
$last_id = 0; |
| 22 |
foreach ($rows as $row) { |
| 23 |
$result[] = $row; |
| 24 |
$last_id = $row['id']; |
| 25 |
} |
| 26 |
$data['last_id'] = $last_id; |
| 27 |
$data['rows'] = $result; |
| 28 |
return $data; |
| 29 |
} |
| 30 |
|
| 31 |
public function deleteBvDynamicEvents($filter = "") { |
| 32 |
$name = BVWPDynSync::$dynsync_table; |
| 33 |
return $this->db->deleteBVTableContent($name, $filter); |
| 34 |
} |
| 35 |
|
| 36 |
public function setWatchTime() { |
| 37 |
return $this->settings->updateOption('bvwatchtime', time()); |
| 38 |
} |
| 39 |
|
| 40 |
// we use fread to read limited fw_prepend_log, with wp function, we will load entire fw_prepend_log at once which may have perfromance issues |
| 41 |
public function getFWPrependLog($params) { |
| 42 |
$result = array(); |
| 43 |
$fname = $params['fname']; |
| 44 |
$limit = intval($params['limit']); |
| 45 |
|
| 46 |
if (WPRWPFileSystem::getInstance()->exists($fname) === true) { |
| 47 |
|
| 48 |
$result['exists'] = true; |
| 49 |
$tmpfname = $fname."tmp"; |
| 50 |
|
| 51 |
if (WPRWPFileSystem::getInstance()->move($fname, $tmpfname, true) === false) { |
| 52 |
$result = array('status' => 'Error', 'message' => 'UNABLE_TO_RENAME_LOGFILE'); |
| 53 |
|
| 54 |
} else { |
| 55 |
|
| 56 |
if (WPRWPFileSystem::getInstance()->exists($tmpfname) === true) { |
| 57 |
|
| 58 |
$fsize = WPRWPFileSystem::getInstance()->size($tmpfname); |
| 59 |
$result["size"] = $fsize; |
| 60 |
|
| 61 |
if ($fsize <= $limit) { |
| 62 |
$result['content'] = WPRWPFileSystem::getInstance()->getContents($tmpfname); |
| 63 |
} else { |
| 64 |
$handle = fopen($tmpfname, "rb"); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen |
| 65 |
$result['content'] = fread($handle, $limit); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread |
| 66 |
$result['incomplete'] = true; |
| 67 |
fclose($handle); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 68 |
} |
| 69 |
|
| 70 |
$result['tmpfile'] = unlink($tmpfname); |
| 71 |
} else { |
| 72 |
$result['tmpfile'] = 'DOES_NOT_EXISTS'; |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $result; |
| 79 |
} |
| 80 |
|
| 81 |
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fsockopen, WordPress.WP.AlternativeFunctions.file_system_operations_fwrite, WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 82 |
public function getDomainInfo($params) { |
| 83 |
$domain = $params["domain"]; |
| 84 |
if (empty($domain)) { |
| 85 |
return array('domainData' => "DOMAIN_NOT_PRESENT"); |
| 86 |
} |
| 87 |
|
| 88 |
$whoisServer = $params["host"]; |
| 89 |
if (empty($whoisServer)) { |
| 90 |
return array('domainData' => "WHOIS_SERVER_NOT_PRESENT : $domain"); |
| 91 |
} |
| 92 |
|
| 93 |
$conn = @fsockopen($whoisServer, $params["port"], $errno, $errstr, $params["timeout"]); |
| 94 |
if (!$conn) { |
| 95 |
return array('domainData' => "UNABLE_TO_CONNECT_TO_WHOIS_SERVER : $whoisServer : DOMAIN : $domain : ERROR : $errstr ($errno)"); |
| 96 |
} |
| 97 |
|
| 98 |
try { |
| 99 |
fwrite($conn, "$domain\r\n"); |
| 100 |
|
| 101 |
$response = ''; |
| 102 |
while (!feof($conn)) { |
| 103 |
$response .= fgets($conn, 1024); |
| 104 |
} |
| 105 |
|
| 106 |
fclose($conn); |
| 107 |
} catch (Exception $e) { |
| 108 |
fclose($conn); |
| 109 |
return array('domainData' => "ERROR_WHILE_FETCHING_DATA : " . $e->getMessage()); |
| 110 |
} |
| 111 |
|
| 112 |
return array('domainData' => $response); |
| 113 |
} |
| 114 |
// phpcs:enable WordPress.WP.AlternativeFunctions.file_system_operations_fsockopen, WordPress.WP.AlternativeFunctions.file_system_operations_fwrite, WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 115 |
|
| 116 |
public function getOffsetResetInfo($params, $offset, $table, $col_name = "id") { |
| 117 |
if (!isset($params['enforce_auto_incr_check']) || !($params['enforce_auto_incr_check'] === true)) { |
| 118 |
return array(); |
| 119 |
} |
| 120 |
$last_id = $this->db->getLastRowId($col_name, $table); |
| 121 |
if ($last_id === null) { |
| 122 |
return array("error" => "UNABLE_TO_FETCH_LAST_ROW_ID"); |
| 123 |
} else if ($last_id < $offset) { |
| 124 |
return array("last_row_id" => $last_id, "offset_reset_required" => true); |
| 125 |
} else { |
| 126 |
return array(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function process($request) { |
| 131 |
$db = $this->db; |
| 132 |
$settings = $this->settings; |
| 133 |
$this->setWatchTime(); |
| 134 |
$params = $request->params; |
| 135 |
|
| 136 |
switch ($request->method) { |
| 137 |
case "getdata": |
| 138 |
$resp = array(); |
| 139 |
|
| 140 |
if (isset($params['domain_params']) && is_array($params['domain_params'])) { |
| 141 |
$resp = array_merge($resp, $this->getDomainInfo($params['domain_params'])); |
| 142 |
} |
| 143 |
|
| 144 |
if (array_key_exists('lp', $params)) { |
| 145 |
require_once dirname( __FILE__ ) . '/../../protect/lp.php'; |
| 146 |
$lp_params = $params['lp']; |
| 147 |
if (!isset($lp_params['bv_check_table']) || $db->isTablePresent($db->getBVTable(WPRProtectLP_V672::TABLE_NAME))) { |
| 148 |
$limit = intval($lp_params['limit']); |
| 149 |
$filter = $lp_params['filter']; |
| 150 |
$offset = isset($lp_params['offset']) ? intval($lp_params['offset']) : 0; |
| 151 |
$table = $db->getBVTable(WPRProtectLP_V672::TABLE_NAME); |
| 152 |
$resp["lplogs"] = $this->getOffsetResetInfo($lp_params, $offset, $table); |
| 153 |
if (empty($resp["lplogs"])) { |
| 154 |
$db->deleteBVTableContent(WPRProtectLP_V672::TABLE_NAME, $lp_params['rmfilter']); |
| 155 |
$resp["lplogs"] = $this->getData($table, $limit, $filter); |
| 156 |
} |
| 157 |
} else { |
| 158 |
$resp["lplogs"] = array("status" => "TABLE_NOT_PRESENT"); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (array_key_exists('prelog', $params)) { |
| 163 |
$prelog_params = $params['prelog']; |
| 164 |
$resp["prelog"] = $this->getFWPrependLog($prelog_params); |
| 165 |
} |
| 166 |
|
| 167 |
if (array_key_exists('fw', $params)) { |
| 168 |
require_once dirname( __FILE__ ) . '/../../protect/fw.php'; |
| 169 |
$fw_params = $params['fw']; |
| 170 |
if (!isset($fw_params['bv_check_table']) || $db->isTablePresent($db->getBVTable(WPRProtectFW_V672::TABLE_NAME))) { |
| 171 |
$limit = intval($fw_params['limit']); |
| 172 |
$filter = $fw_params['filter']; |
| 173 |
$offset = isset($fw_params['offset']) ? intval($fw_params['offset']) : 0; |
| 174 |
$table = $db->getBVTable(WPRProtectFW_V672::TABLE_NAME); |
| 175 |
$resp["fwlogs"] = $this->getOffsetResetInfo($fw_params, $offset, $table); |
| 176 |
if (empty($resp["fwlogs"])){ |
| 177 |
$db->deleteBVTableContent(WPRProtectFW_V672::TABLE_NAME, $fw_params['rmfilter']); |
| 178 |
$resp["fwlogs"] = $this->getData($table, $limit, $filter); |
| 179 |
} |
| 180 |
} else { |
| 181 |
$resp["fwlogs"] = array("status" => "TABLE_NOT_PRESENT"); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if (array_key_exists('dynevent', $params)) { |
| 186 |
require_once dirname( __FILE__ ) . '/../../wp_dynsync.php'; |
| 187 |
$isdynsyncactive = $settings->getOption('bvDynSyncActive'); |
| 188 |
if ($isdynsyncactive == 'yes') { |
| 189 |
if (!isset($params['bv_check_table']) || $db->isTablePresent($db->getBVTable(BVWPDynSync::$dynsync_table))) { |
| 190 |
$limit = intval($params['limit']); |
| 191 |
$filter = $params['filter']; |
| 192 |
$offset = isset($params['offset']) ? intval($params['offset']) : 0; |
| 193 |
$table = $db->getBVTable(BVWPDynSync::$dynsync_table); |
| 194 |
$offset_reset_info = $this->getOffsetResetInfo($params, $offset, $table); |
| 195 |
if (empty($offset_reset_info)) { |
| 196 |
$this->deleteBvDynamicEvents($params['rmfilter']); |
| 197 |
$data = $this->getData($table, $limit, $filter); |
| 198 |
|
| 199 |
$resp['last_id'] = $data['last_id']; |
| 200 |
$resp['events'] = $data['rows']; |
| 201 |
$resp['timestamp'] = time(); |
| 202 |
$resp["status"] = true; |
| 203 |
} else { |
| 204 |
if (array_key_exists("error", $offset_reset_info)) { |
| 205 |
$resp["error"] = $offset_reset_info["error"]; |
| 206 |
} else { |
| 207 |
$resp["offset_reset_required"] = $offset_reset_info["offset_reset_required"]; |
| 208 |
$resp["last_row_id"] = $offset_reset_info["last_row_id"]; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if (array_key_exists('actlog', $params)) { |
| 216 |
require_once dirname( __FILE__ ) . '/../../wp_actlog.php'; |
| 217 |
$actlog_params = $params['actlog']; |
| 218 |
if (!isset($actlog_params['bv_check_table']) || $db->isTablePresent($db->getBVTable(BVWPActLog::$actlog_table))) { |
| 219 |
$table = $db->getBVTable(BVWPActLog::$actlog_table); |
| 220 |
$limit = intval($actlog_params['limit']); |
| 221 |
$filter = $actlog_params['filter']; |
| 222 |
$offset = isset($actlog_params['offset']) ? intval($actlog_params['offset']) : 0; |
| 223 |
$resp["actlogs"] = $this->getOffsetResetInfo($actlog_params, $offset, $table); |
| 224 |
if (empty($resp["actlogs"])) { |
| 225 |
$db->deleteBVTableContent(BVWPActLog::$actlog_table, $actlog_params['rmfilter']); |
| 226 |
$resp["actlogs"] = $this->getData($table, $limit, $filter); |
| 227 |
} |
| 228 |
} else { |
| 229 |
$resp["actlogs"] = array("status" => "TABLE_NOT_PRESENT"); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if (array_key_exists('airlift_stats', $params)) { |
| 234 |
$airlift_stats_table = "airlift_stats"; |
| 235 |
$airlift_stats_params = $params['airlift_stats']; |
| 236 |
$table = $db->getBVTable($airlift_stats_table); |
| 237 |
if (!isset($airlift_stats_params['bv_check_table']) || $db->isTablePresent($table)) { |
| 238 |
$limit = intval($airlift_stats_params['limit']); |
| 239 |
$filter = $airlift_stats_params['filter']; |
| 240 |
$offset = isset($airlift_stats_params['offset']) ? intval($airlift_stats_params['offset']) : 0; |
| 241 |
$resp["airlift_stats"] = $this->getOffsetResetInfo($airlift_stats_params, $offset, $table); |
| 242 |
if (empty($resp["airlift_stats"])) { |
| 243 |
$db->deleteBVTableContent($airlift_stats_table, $airlift_stats_params['rmfilter']); |
| 244 |
$resp["airlift_stats"] = $this->getData($table, $limit, $filter); |
| 245 |
} |
| 246 |
} else { |
| 247 |
$resp["airlift_stats"] = array("status" => "TABLE_NOT_PRESENT"); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (array_key_exists('php_error_monitoring', $params)) { |
| 252 |
require_once dirname( __FILE__ ) . '/../../php_error_monitoring/monitoring.php'; |
| 253 |
$php_error_monit_params = $params['php_error_monitoring']; |
| 254 |
$table = $db->getBVTable(WPRWPPHPErrorMonitoring::ERROR_TABLE); |
| 255 |
if (!isset($php_error_monit_params['bv_check_table']) || $db->isTablePresent($table)) { |
| 256 |
$limit = intval($php_error_monit_params['limit']); |
| 257 |
$filter = $php_error_monit_params['filter']; |
| 258 |
$offset = isset($php_error_monit_params['offset']) ? intval($php_error_monit_params['offset']) : 0; |
| 259 |
$resp["php_error_monitoring"] = $this->getOffsetResetInfo($php_error_monit_params, $offset, $table); |
| 260 |
if (empty($resp["php_error_monitoring"])) { |
| 261 |
$db->deleteBVTableContent(WPRWPPHPErrorMonitoring::ERROR_TABLE, $php_error_monit_params['rmfilter']); |
| 262 |
$resp["php_error_monitoring"] = $this->getData($table, $limit, $filter); |
| 263 |
} |
| 264 |
} else { |
| 265 |
$resp["php_error_monitoring"] = array("status" => "TABLE_NOT_PRESENT"); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
$resp["status"] = "done"; |
| 270 |
break; |
| 271 |
case "rmdata": |
| 272 |
require_once dirname( __FILE__ ) . '/../../wp_dynsync.php'; |
| 273 |
$filter = $params['filter']; |
| 274 |
$resp = array("status" => $this->deleteBvDynamicEvents($filter)); |
| 275 |
break; |
| 276 |
default: |
| 277 |
$resp = false; |
| 278 |
} |
| 279 |
return $resp; |
| 280 |
} |
| 281 |
} |
| 282 |
endif; |
| 283 |
|