first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_Array {
|
||||
|
||||
public static function findOffset($array, $key) {
|
||||
$offset = 0;
|
||||
foreach ($array as $index => $value) {
|
||||
if ($index === $key)
|
||||
return $offset;
|
||||
$offset++;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function insertAfter(&$array, $targetKey, $key, $value) {
|
||||
$offset = self::findOffset($array, $targetKey);
|
||||
if ($offset === null)
|
||||
return false;
|
||||
$array = array_merge(
|
||||
array_slice($array, 0, $offset + 1),
|
||||
array( $key => $value ),
|
||||
array_slice($array, $offset + 1)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
use WordfenceLS\Crypto\Model_Base2n;
|
||||
|
||||
class Utility_BaseConversion {
|
||||
|
||||
public static function get_base32() {
|
||||
static $base32 = null;
|
||||
if ($base32 === null)
|
||||
$base32 = new Model_Base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', false, true, true);
|
||||
return $base32;
|
||||
}
|
||||
|
||||
public static function base32_encode($data) {
|
||||
$base32 = self::get_base32();
|
||||
return $base32->encode($data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class Utility_DatabaseLock implements Utility_Lock {
|
||||
|
||||
const DEFAULT_TIMEOUT = 30;
|
||||
const MAX_TIMEOUT = 120;
|
||||
|
||||
private $wpdb;
|
||||
private $table;
|
||||
private $key;
|
||||
private $timeout;
|
||||
private $expirationTimestamp;
|
||||
|
||||
public function __construct($dbController, $key, $timeout = null) {
|
||||
$this->wpdb = $dbController->get_wpdb();
|
||||
$this->table = $dbController->settings;
|
||||
$this->key = "lock:{$key}";
|
||||
$this->timeout = self::resolveTimeout($timeout);
|
||||
}
|
||||
|
||||
private static function resolveTimeout($timeout) {
|
||||
if ($timeout === null)
|
||||
$timeout = ini_get('max_execution_time');
|
||||
$timeout = (int) $timeout;
|
||||
if ($timeout <= 0 || $timeout > self::MAX_TIMEOUT)
|
||||
return self::DEFAULT_TIMEOUT;
|
||||
return $timeout;
|
||||
}
|
||||
|
||||
private function clearExpired($timestamp) {
|
||||
$this->wpdb->query($this->wpdb->prepare(<<<SQL
|
||||
DELETE
|
||||
FROM {$this->table}
|
||||
WHERE
|
||||
name = %s
|
||||
AND value < %d
|
||||
SQL
|
||||
, $this->key, $timestamp));
|
||||
}
|
||||
|
||||
private function insert($expirationTimestamp) {
|
||||
$result = $this->wpdb->query($this->wpdb->prepare(<<<SQL
|
||||
INSERT IGNORE
|
||||
INTO {$this->table}
|
||||
(name, value, autoload)
|
||||
VALUES(%s, %d, 'no')
|
||||
SQL
|
||||
, $this->key, $expirationTimestamp));
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
public function acquire($delay = self::DEFAULT_DELAY) {
|
||||
$attempts = (int) ($this->timeout * 1000000 / $delay);
|
||||
for (; $attempts > 0; $attempts--) {
|
||||
$timestamp = time();
|
||||
$this->clearExpired($timestamp);
|
||||
$expirationTimestamp = $timestamp + $this->timeout;
|
||||
$locked = $this->insert($expirationTimestamp);
|
||||
if ($locked) {
|
||||
$this->expirationTimestamp = $expirationTimestamp;
|
||||
return;
|
||||
}
|
||||
usleep($delay);
|
||||
}
|
||||
throw new RuntimeException("Failed to acquire lock {$this->key}");
|
||||
}
|
||||
|
||||
private function delete($expirationTimestamp) {
|
||||
$this->wpdb->delete(
|
||||
$this->table,
|
||||
array (
|
||||
'name' => $this->key,
|
||||
'value' => $expirationTimestamp
|
||||
),
|
||||
array (
|
||||
'%s',
|
||||
'%d'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function release() {
|
||||
if ($this->expirationTimestamp === null)
|
||||
return;
|
||||
$this->delete($this->expirationTimestamp);
|
||||
$this->expirationTimestamp = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
interface Utility_Lock {
|
||||
|
||||
const DEFAULT_DELAY = 100000;
|
||||
|
||||
public function acquire($delay = self::DEFAULT_DELAY);
|
||||
|
||||
public function release();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
/**
|
||||
* An implementation of the Utility_Lock that doesn't actually do any locking
|
||||
*/
|
||||
class Utility_NullLock implements Utility_Lock {
|
||||
|
||||
public function acquire($delay = self::DEFAULT_DELAY) {
|
||||
//Do nothing
|
||||
}
|
||||
|
||||
public function release() {
|
||||
//Do nothing
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
class Utility_Number {
|
||||
|
||||
public static function isInteger($value, $min = null, $max = null) {
|
||||
$options = array();
|
||||
if ($min !== null)
|
||||
$options['min_range'] = $min;
|
||||
if ($max !== null)
|
||||
$options['max_range'] = $max;
|
||||
return filter_var($value, FILTER_VALIDATE_INT, array('options' => $options)) !== false;
|
||||
}
|
||||
|
||||
public static function isUnixTimestamp($value) {
|
||||
return self::isInteger($value, 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace WordfenceLS;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class Utility_Serialization {
|
||||
|
||||
public static function unserialize($data, $options = array(), $validator = null) {
|
||||
static $serializedFalse;
|
||||
if ($serializedFalse === null)
|
||||
$serializedFalse = serialize(false);
|
||||
if ($data === $serializedFalse)
|
||||
return false;
|
||||
if (!is_serialized($data))
|
||||
throw new RuntimeException('Input data is not serialized');
|
||||
if (version_compare(PHP_VERSION, '5.6', '<=')) {
|
||||
$unserialized = @unserialize($data);
|
||||
}
|
||||
else {
|
||||
$unserialized = @unserialize($data, $options);
|
||||
}
|
||||
if ($unserialized === false)
|
||||
throw new RuntimeException('Deserialization failed');
|
||||
if ($validator !== null && !$validator($unserialized))
|
||||
throw new RuntimeException('Validation of unserialized data failed');
|
||||
return $unserialized;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user