From 994437dd9a79559a2357b8a77d22f87baea62860 Mon Sep 17 00:00:00 2001 From: Dinesh Kesarwani Date: Fri, 5 Jan 2018 14:59:15 +0530 Subject: [PATCH 1/3] Extend Additoinal and Extrad Fields Settings Added support to provide additional parameters for Additional and Extra fields. --- src/WordPressHandler/WordPressHandler.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/WordPressHandler/WordPressHandler.php b/src/WordPressHandler/WordPressHandler.php index e283889..e66171d 100644 --- a/src/WordPressHandler/WordPressHandler.php +++ b/src/WordPressHandler/WordPressHandler.php @@ -96,14 +96,26 @@ public function initialize(array $record) // allow for Extra fields $extraFields = ''; - foreach ($record['extra'] as $key => $val) { - $extraFields.=",\n$key TEXT NULL DEFAULT NULL"; + foreach ($record['extra'] as $ef) { + if( isset($ef['name']) ){ + if( isset($ef['type']) ){ + $extraFields.=",\n{$ef['name']} {$ef['type']}"; + }else{ + $extraFields.=",\n{$ef['name']} TEXT NULL DEFAULT NULL"; + } + } } // additional fields $additionalFields = ''; - foreach ($this->additionalFields as $f) { - $additionalFields.=",\n$f TEXT NULL DEFAULT NULL"; + foreach ($this->additionalFields as $af) { + if( isset($af['name']) ){ + if( isset($af['type']) ){ + $additionalFields.=",\n{$af['name']} {$af['type']}"; + }else{ + $additionalFields.=",\n{$af['name']} TEXT NULL DEFAULT NULL"; + } + } } $sql = "CREATE TABLE $table_name ( From fd91db531fc4fe6fd6a37490fe0851c115877a94 Mon Sep 17 00:00:00 2001 From: Dinesh Kesarwani Date: Fri, 5 Jan 2018 15:09:05 +0530 Subject: [PATCH 2/3] Update README.md Updated README.md for changes in Additoinal and Extrad Fields Settings. --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d79d18c..48503e1 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ use WordPressHandler\WordPressHandler; global $wpdb; //Create WordPressHandler -$wordPressHandler = new WordPressHandler($wpdb, "log", array('username', 'userid'), \Monolog\Logger::DEBUG); +$wordPressHandler = new WordPressHandler($wpdb, "log", array(array('name' => 'username','type' => 'VARCHAR(255) DEFAULT NULL'), array('name' => 'userid','type' => 'VARCHAR(255) DEFAULT NULL')), \Monolog\Logger::DEBUG); // setup array of extra fields -$record = ['extra' => []]; +$record = ['extra' => ['extra_field' => 'userid','type' => 'VARCHAR(255) DEFAULT NULL']]; // creates database table if needed, add extra fields from above $wordPressHandler->initialize($record); @@ -68,12 +68,21 @@ function register_activation_hook(__FILE__, function() { $handler = new \WordPressHandler\WordPressHandler( $wpdb, "logs", - array('username', 'userid'), + array( + array( + 'name' => 'username', + 'type' => 'VARCHAR(255) DEFAULT NULL' + ), + array( + 'name' => 'userid', + 'type' => 'VARCHAR(255) DEFAULT NULL' + ) + ), \Monolog\Logger::DEBUG ); // setup array of extra fields - $record = ['extra' => []]; + $record = ['extra' => ['extra_field' => 'userid','type' => 'VARCHAR(255) DEFAULT NULL']]; // creates database table if needed, add extra fields from above $handler->initialize($record); From d7ec97db95f81711c37d0b4097f2a5557dfebf0e Mon Sep 17 00:00:00 2001 From: cyberwani Date: Fri, 5 Jan 2018 15:29:21 +0530 Subject: [PATCH 3/3] Added ip address Added support for ip address. --- src/WordPressHandler/WordPressHandler.php | 82 ++++++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/src/WordPressHandler/WordPressHandler.php b/src/WordPressHandler/WordPressHandler.php index e66171d..b0f6768 100644 --- a/src/WordPressHandler/WordPressHandler.php +++ b/src/WordPressHandler/WordPressHandler.php @@ -34,6 +34,10 @@ class WordPressHandler extends AbstractProcessingHandler * @var string the table prefix to store the logs in */ private $prefix = 'wp_'; + /** + * @var string ip address to store in database + */ + private $ip_address = 'unknown'; /** * @var string[] additional fields to be stored in the database * @@ -63,6 +67,9 @@ public function __construct( } $this->table = $table; $this->prefix = $this->wpdb->prefix; + + $ip_address = $this->get_ip_address(); + $this->ip_address = ($ip_address == '::1') ? 'localhost' : $ip_address; $this->additionalFields = $additionalFields; parent::__construct($level, $bubble); @@ -123,11 +130,11 @@ public function initialize(array $record) channel VARCHAR(255), level INTEGER, message LONGTEXT, - time INTEGER UNSIGNED$extraFields$additionalFields, + time INTEGER UNSIGNED + ip VARCHAR(255) DEFAULT NULL$extraFields$additionalFields, PRIMARY KEY (id) ) $charset_collate;"; - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); @@ -161,7 +168,8 @@ protected function write(array $record) 'channel' => $record['channel'], 'level' => $record['level'], 'message' => $record['message'], - 'time' => $record['datetime']->format('U') + 'time' => $record['datetime']->format('U'), + 'ip' => $this->ip_address ), $record['context']); // extra out formatted or unformatted extra values @@ -189,4 +197,72 @@ protected function write(array $record) $this->wpdb->insert( $table_name, $contentArray ); } + + /** + * Returns the ip address of user + * + * @return string + */ + public function get_ip_address() { + // check for shared internet/ISP IP + if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP'])) { + return $_SERVER['HTTP_CLIENT_IP']; + } + + // check for IPs passing through proxies + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + // check if multiple ips exist in var + if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) { + $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); + foreach ($iplist as $ip) { + if ($this->validate_ip($ip)) + return $ip; + } + } else { + if ($this->validate_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) + return $_SERVER['HTTP_X_FORWARDED_FOR']; + } + } + if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED'])) + return $_SERVER['HTTP_X_FORWARDED']; + if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) + return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; + if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR'])) + return $_SERVER['HTTP_FORWARDED_FOR']; + if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED'])) + return $_SERVER['HTTP_FORWARDED']; + + // return unreliable ip since all else failed + return $_SERVER['REMOTE_ADDR']; + } + + /** + * Ensures an ip address is both a valid IP and does not fall within + * a private network range. + */ + public function validate_ip($ip) { + if (strtolower($ip) === 'unknown') + return false; + + // generate ipv4 network address + $ip = ip2long($ip); + + // if the ip is set and not equivalent to 255.255.255.255 + if ($ip !== false && $ip !== -1) { + // make sure to get unsigned long representation of ip + // due to discrepancies between 32 and 64 bit OSes and + // signed numbers (ints default to signed in PHP) + $ip = sprintf('%u', $ip); + // do private network range checking + if ($ip >= 0 && $ip <= 50331647) return false; + if ($ip >= 167772160 && $ip <= 184549375) return false; + if ($ip >= 2130706432 && $ip <= 2147483647) return false; + if ($ip >= 2851995648 && $ip <= 2852061183) return false; + if ($ip >= 2886729728 && $ip <= 2887778303) return false; + if ($ip >= 3221225984 && $ip <= 3221226239) return false; + if ($ip >= 3232235520 && $ip <= 3232301055) return false; + if ($ip >= 4294967040) return false; + } + return true; + } }