Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Additional Field Settings and Added IP Address Support #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ use WordPressHandler\WordPressHandler;

// Create WordPressHandler
$wordPressHandler = new WordPressHandler(null, "log", ['username', 'userid'], \Monolog\Logger::DEBUG);
// OR
$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);

// Configure maximum number of rows to keep (old entries are deleted when reached)
$wordPressHandler->conf_table_size_limiter( 250000 );

// Setup array of extra fields
$record = ['extra' => []];
$record = ['extra' => ['extra_field' => 'userid','type' => 'VARCHAR(255) DEFAULT NULL']];

// Create database table if needed, add extra fields from above
$wordPressHandler->initialize($record);
Expand All @@ -74,12 +76,21 @@ register_activation_hook(__FILE__, 'my_plugin_activation');
function my_plugin_activation() {
$handler = new \WordPressHandler\WordPressHandler(
null, "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 = array('extra' => array());
$record = array('extra' => array('extra_field' => 'userid','type' => 'VARCHAR(255) DEFAULT NULL'));

// creates database table if needed, add extra fields from above
$handler->initialize($record);
Expand Down
101 changes: 95 additions & 6 deletions src/WordPressHandler/WordPressHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class WordPressHandler extends AbstractProcessingHandler
* @var \wpdb wpdb object of database connection
*/
protected $wpdb;
/**
* @var string ip address to store in database
*/
private $ip_address = 'unknown';
/**
* @var string the table to store the logs in
*/
Expand Down Expand Up @@ -94,6 +98,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);
}
Expand Down Expand Up @@ -160,22 +167,35 @@ 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` TEXT NULL DEFAULT NULL";
}
}
}

$sql = "CREATE TABLE $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
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;";

Expand Down Expand Up @@ -240,7 +260,8 @@ protected function write(array $record): void
'channel' => $record['channel'],
'level' => $record['level'],
'message' => (isset($record['formatted']['message'])) ? $record['formatted']['message'] : $record['message'],
'time' => $record['datetime']->format('U')
'time' => $record['datetime']->format('U'),
'ip' => $this->ip_address
);

// Make sure to use the formatted values for context and extra, if available
Expand Down Expand Up @@ -271,4 +292,72 @@ protected function write(array $record): void
$this->wpdb->insert( $table_name, $contentArray );
$this->maybe_truncate();
}

/**
* 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;
}
}