REL-0.9 Updating intranet class files

This commit is contained in:
cytopia 2017-05-06 11:13:33 +02:00
parent 2f57691e58
commit a5cf4f9483
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
10 changed files with 832 additions and 342 deletions

View File

@ -150,20 +150,6 @@ class Docker
*
*********************************************************************************/
/**
* Get PHP Version.
*
* @return string PHP version string
*/
public function PHP_version()
{
if (defined('HHVM_VERSION')) {
return 'HHVM ' .HHVM_VERSION . '<br/>(PHP '.str_replace('-hhvm', '', phpversion()).')';
//return 'PHP ' . phpversion() . '('. HHVM_VERSION . ')';
} else {
return 'PHP ' . phpversion() .' (' . php_sapi_name().')';
}
}
/**
* Read out PHP Server configuration by variable
@ -303,68 +289,6 @@ class Docker
}
}
/*********************************************************************************
*
* MySQL Docker functions
*
*********************************************************************************/
/**
* Get MySQL Version.
*
* @return string MySQL version string.
*/
public function MySQL_version()
{
$name = $this->MySQL_config('version_comment');
$version = $this->MySQL_config('version');
if (!$name && !$version) {
return 'Unknown MySQL version';
}
return $name . ' ' . $version;
}
/**
* Read out MySQL Server configuration by variable
*
* @param string|null $key Config key name
* @return string|mixed[]
*/
public function MySQL_config($key = null)
{
// Get all configs as array
if ($key === null) {
$callback = function ($row, &$data) {
$key = $row['Variable_name'];
$val = $row['Value'];
$data[$key] = $val;
};
$sql = 'SHOW VARIABLES;';
$configs = loadClass('Mysql')->select($sql, $callback);
return $configs ? $configs : array();
} else { // Get single config
$key = str_replace('-', '_', $key);
$callback = function ($row, &$data) use ($key) {
$data = isset($row['Value']) ? $row['Value'] : false;
};
$sql = 'SHOW VARIABLES WHERE Variable_Name = "'.$key.'";';
$val = loadClass('Mysql')->select($sql, $callback);
if (is_array($val) && $val) {
return array_values($val)[0];
} else {
return $val;
}
}
}
/*********************************************************************************
*
@ -372,32 +296,6 @@ class Docker
*
*********************************************************************************/
/**
* Get Postgres Version.
*
* @return string Postgres version string.
*/
public function Postgres_version()
{
$callback = function ($row, &$data) {
$data = $row['version'];
};
$version = loadClass('Postgres')->select('SELECT version();', $callback);
// Extract shorthand
preg_match('/\w+[[:space:]]*[.0-9]+/i', $version, $matches);
if (isset($matches[0])) {
return $matches[0];
}
// Unknown
if (!$version) {
return 'Unknown Postgres version';
}
return $version;
}
/**
* Read out PostgreSQL Server configuration by variable
@ -435,26 +333,6 @@ class Docker
/*********************************************************************************
*
* HTTPD Docker functions
*
*********************************************************************************/
/**
* Get HTTPD Version
*
* @return string HTTPD server version string.
*/
public function HTTPD_version()
{
preg_match('/\w+\/[.0-9]+/i', $_SERVER['SERVER_SOFTWARE'], $matches);
if (isset($matches[0])) {
return $matches[0];
} else {
return 'Unknown Webserver';
}
}

View File

@ -0,0 +1,131 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Httpd extends _Base implements _iBase
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
/**
* Httpd instance
* @var Httpd|null
*/
protected static $instance = null;
/**
* Singleton Instance getter.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
* @return object|null
*/
public static function getInstance($host = null, $user = null, $pass = null)
{
if (!isset(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
/**
* @overwrite
* @param string $hostname [description]
* @return boolean [description]
*/
public static function isAvailable($hostname)
{
// Always available, otherwise you would not see any browser output.
return true;
}
/**
* Connect to Httpd
*
* @param string $err Reference to error message
* @param string $host Redis hostname
* @return boolean
*/
public static function testConnection(&$err, $host, $user = '', $pass = '')
{
$err = false;
// Silence errors and try to connect
//error_reporting(0);
$url = 'http://'.$host.'/not-existing-page-which-returns-404.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
error_reporting(-1);
if ($http_code == 0) {
$err = 'Failed to connect to Httpd host on '.$host;
return false;
}
return true;
}
/*********************************************************************************
*
* Construct/Destructor
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct()
{
}
/**
* Destructor
*/
public function __destruct()
{
}
/*********************************************************************************
*
* Interface required functions
*
*********************************************************************************/
public function getName($default = 'Httpd')
{
$name = $this->egrep('/[a-zA-Z0-9]+/', $_SERVER['SERVER_SOFTWARE']);
if (!$name) {
loadClass('Logger')->error('Could not get Httpd name');
return $default;
}
return $name;
}
public function getVersion()
{
$version = $this->egrep('/[.0-9]+/', $_SERVER['SERVER_SOFTWARE']);
if (!$version) {
loadClass('Logger')->error('Could not get Httpd version');
return '';
}
return $version;
}
}

View File

@ -154,4 +154,16 @@ class Logger
}
return false;
}
public function countErrors()
{
$count = 0;
$handle = fopen($this->_logfile, 'r');
while (!feof($handle)) {
$line = fgets($handle, 4096);
$count = $count + substr_count($line, PHP_EOL);
}
fclose($handle);
return $count;
}
}

View File

@ -0,0 +1,185 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Memcd extends _Base implements _iBase
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
/**
* Memcached instance
* @var Memcached|null
*/
protected static $instance = null;
/**
* Singleton Instance getter.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
* @return object|null
*/
public static function getInstance($host, $user = null, $pass = null)
{
if (!isset(static::$instance)) {
static::$instance = new static($host);
}
// If current Memcached instance was unable to connect
if (!static::$instance) {
//loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
}
return static::$instance;
}
/**
* Connect to Memcached
*
* @param string $err Reference to error message
* @param string $host Memcached hostname
* @return boolean
*/
public static function testConnection(&$err, $host, $user = '', $pass = '')
{
$err = false;
// Silence errors and try to connect
//error_reporting(-1);
$memcd = new \Memcached();
$memcd->resetServerList();
if (!$memcd->addServer($host, 11211)) {
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$host;
error_reporting(-1);
return false;
}
$stats = $memcd->getStats();
if (!isset($stats[$host.':11211'])) {
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$host;
return false;
}
if (!isset($stats[$host.':11211']['pid'])) {
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$host;
return false;
}
if ($stats[$host.':11211']['pid'] < 1) {
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$host;
return false;
}
$memcd->quit();
return true;
}
/*********************************************************************************
*
* Private Variables
*
*********************************************************************************/
/**
* Memcached instance
* @var object|null
*/
private $_memcached = null;
/*********************************************************************************
*
* Construct/Destructor
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct($host)
{
$memcd = new \Memcached();
$memcd->resetServerList();
if (!$memcd->addServer($host, 11211)) {
$this->_connect_error = 'Failed to connect to Memcached host on '.$host;
$this->_connect_errno = 1;
//loadClass('Logger')->error($this->_connect_error);
} else {
$this->_memcached = $memcd;
}
}
/**
* Destructor
*/
public function __destruct()
{
if ($this->_memcached) {
$this->_memcached->quit();
}
}
/*********************************************************************************
*
* Memcached Select functions
*
*********************************************************************************/
/* public function getInfo()
{
if ($this->_memcached) {
return $this->_memcached->info('all');
} else {
return array();
}
}
public function getKeys()
{
if ($this->_memcached) {
return $this->_memcached->keys('*');
} else {
return array();
}
}*/
/*********************************************************************************
*
* Interface required functions
*
*********************************************************************************/
public function getName($default = 'Memcached')
{
return $default;
}
public function getVersion()
{
$info = $this->_memcached->getVersion();
$info = array_values($info);
if (!isset($info[0])) {
loadClass('Logger')->error('Could not get Memcached version');
return '';
}
return $info[0];
}
}

View File

@ -4,7 +4,7 @@ namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Mysql
class Mysql extends _Base implements _iBase
{
/*********************************************************************************
@ -27,15 +27,14 @@ class Mysql
* @param string $host Host
* @return object|null
*/
public static function getInstance($user = null, $pass = null, $host = null)
public static function getInstance($host, $user, $pass)
{
if (!isset(static::$instance)) {
static::$instance = new static($user, $pass, $host);
}
// If current MySQL instance was unable to connect
if ((static::$instance->getConnectError())) {
loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
//return null;
// loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
}
return static::$instance;
}
@ -49,7 +48,7 @@ class Mysql
* @param string $host MySQL hostname
* @return boolean
*/
public static function testConnection(&$err, $user, $pass, $host)
public static function testConnection(&$err, $host, $user, $pass)
{
$err = false;
@ -80,31 +79,6 @@ class Mysql
*/
private $_link = null;
/**
* Connection error string
* @var string
*/
private $_connect_error = '';
/**
* Connection error code
* @var integer
*/
private $_connect_errno = 0;
/**
* Error string
* @var string
*/
private $_error = '';
/**
* Error code
* @var integer
*/
private $_errno = 0;
/*********************************************************************************
@ -128,9 +102,9 @@ class Mysql
error_reporting(-1);
if (mysqli_connect_errno()) {
$this->_connect_error = 'Failed to connect: ' .mysqli_connect_error();
$this->_connect_errno = mysqli_connect_errno();
loadClass('Logger')->error($this->_connect_error);
$this->setConnectError('Failed to connect: ' .mysqli_connect_error());
$this->setConnectErrno(mysqli_connect_errno());
//loadClass('Logger')->error($this->_connect_error);
} else {
$this->_link = $link;
}
@ -162,14 +136,14 @@ class Mysql
public function select($query, $callback = null)
{
if (!$this->_link) {
loadClass('Logger')->error('MySQL error, link is no resource in select()');
loadClass('Logger')->error('MySQL error, link is no resource in select(): '.$query);
return false;
}
if (!($result = mysqli_query($this->_link, $query))) {
$this->_error = mysqli_error($this->_link);
$this->_errno = mysqli_errno($this->_link);
loadClass('Logger')->error($this->_error);
$this->setError(mysqli_error($this->_link));
$this->setErrno(mysqli_errno($this->_link));
loadClass('Logger')->error($this->getError());
return false;
}
@ -207,11 +181,7 @@ class Mysql
S.DEFAULT_COLLATION_NAME AS 'collation',
S.default_character_set_name AS 'charset'
FROM
information_schema.SCHEMATA AS S
WHERE
S.SCHEMA_NAME != 'mysql' AND
S.SCHEMA_NAME != 'performance_schema' AND
S.SCHEMA_NAME != 'information_schema'";
information_schema.SCHEMATA AS S;";
$databases = $this->select($sql, $callback);
@ -266,53 +236,89 @@ class Mysql
}
/**
* Read out MySQL Server configuration by variable
*
* @param string|null $key Config key name
* @return string|mixed[]
*/
public function getConfig($key = null)
{
// Get all configs as array
if ($key === null) {
$callback = function ($row, &$data) {
$key = $row['Variable_name'];
$val = $row['Value'];
$data[$key] = $val;
};
return $this->select('SHOW VARIABLES;');
} else { // Get single config
$key = str_replace('-', '_', $key);
$callback = function ($row, &$data) use ($key) {
$data = isset($row['Value']) ? $row['Value'] : false;
};
$sql = 'SHOW VARIABLES WHERE Variable_Name = "'.$key.'";';
$val = $this->select($sql, $callback);
if (is_array($val) && $val) {
return array_values($val)[0];
} else {
return $val;
}
}
}
/*********************************************************************************
*
* MySQL Error functions
* Interface required functions
*
*********************************************************************************/
/**
* Return connection error message.
* Get MySQL Name.
*
* @return string Error message
* @return string MySQL short name.
*/
public function getConnectError()
public function getName($default = 'MySQL')
{
return $this->_connect_error;
if (!static::isAvailable('mysql')) {
return $default;
}
$name = $this->egrep('/[a-zA-Z0-9]+/', $this->getConfig('version_comment'));
if (!$name) {
loadClass('Logger')->error('Could not get MySQL Name');
return $default;
}
return $name;
}
/**
* Return connection errno code.
*
* @return integer Error code
*/
public function getConnectErrno()
{
return $this->_connect_errno;
}
/**
* Return error message.
* Get MySQL Version.
*
* @return string Error message
* @return string MySQL version.
*/
public function getError()
public function getVersion()
{
return $this->_error;
}
if (!static::isAvailable('mysql')) {
return '';
}
/**
* Return errno code.
*
* @return integer Error code
*/
public function getErrno()
{
return $this->_errno;
$version = $this->egrep('/[.0-9]+/', $this->getConfig('version'));
if (!$version) {
loadClass('Logger')->error('Could not get MySQL version');
return '';
}
return $version;
}
}

View File

@ -4,7 +4,7 @@ namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Postgres
class Pgsql extends _Base implements _iBase
{
/*********************************************************************************
@ -27,15 +27,14 @@ class Postgres
* @param string $host Host
* @return object|null
*/
public static function getInstance($user = null, $pass = null, $host = null)
public static function getInstance($host = null, $user = null, $pass = null)
{
if (!isset(static::$instance)) {
static::$instance = new static($user, $pass, $host);
}
// If current Postgres instance was unable to connect
if ((static::$instance->getConnectError())) {
loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
//return null;
//loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
}
return static::$instance;
}
@ -49,7 +48,7 @@ class Postgres
* @param string $host Postgres hostname
* @return boolean
*/
public static function testConnection(&$err, $user, $pass, $host)
public static function testConnection(&$err, $host, $user, $pass)
{
$err = false;
@ -59,7 +58,7 @@ class Postgres
error_reporting(-1);
if (!$link || pg_connection_status($link) !== PGSQL_CONNECTION_OK) {
$err = 'Failed to connect: ' .pg_last_error($link);
$err = 'Failed to connect';
return false;
}
pg_close($link);
@ -80,31 +79,6 @@ class Postgres
*/
private $_link = null;
/**
* Connection error string
* @var string
*/
private $_connect_error = '';
/**
* Connection error code
* @var integer
*/
private $_connect_errno = 0;
/**
* Error string
* @var string
*/
private $_error = '';
/**
* Error code
* @var integer
*/
private $_errno = 0;
/*********************************************************************************
@ -128,15 +102,14 @@ class Postgres
if ($database !== null) {
$link = pg_connect('host='.$host.' dbname='.$database.' user='.$user.' password='.$pass);
} else {
// NOTE: using dbname=postgres prevents HHVM from segfaulting
$link = pg_connect('host='.$host.' user='.$user.' password='.$pass);
}
error_reporting(-1);
if (!$link || pg_connection_status($link) !== PGSQL_CONNECTION_OK) {
$this->_connect_error = 'Failed to connect to '.$user.'@'.$host;
$this->_connect_errno = 1;
loadClass('Logger')->error($this->_connect_error);
$this->setConnectError('Failed to connect to '.$user.'@'.$host);
$this->setConnectErrno(1);
//loadClass('Logger')->error($this->_connect_error);
} else {
$this->_link = $link;
}
@ -144,15 +117,13 @@ class Postgres
/**
* Destructor
*/
public function __destruct()
{
if ($this->_link) {
pg_close($this->_link);
@pg_close($this->_link);
}
}
@ -177,9 +148,9 @@ class Postgres
}
if (!($result = pg_query($this->_link, $query))) {
$this->_error = 'PostgreSQL - error on result: '.pg_result_error($result)."\n" . 'query:'."\n" . $query;
$this->_errno = 1;
loadClass('Logger')->error($this->_error);
$this->setError('PostgreSQL - error on result: '.pg_result_error($result)."\n" . 'query:'."\n" . $query);
$this->setErrno(1);
loadClass('Logger')->error($this->getError());
return false;
}
@ -225,7 +196,7 @@ class Postgres
// Get schemas for each database
foreach ($databases as $name => &$database) {
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('POSTGRES_PASSWORD'), $GLOBALS['POSTGRES_HOST_ADDR'], $name);
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('PGSQL_ROOT_PASSWORD'), $GLOBALS['PGSQL_HOST_ADDR'], $name);
$sql = "SELECT n.nspname AS schemas FROM pg_catalog.pg_namespace AS n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema';";
$callback = function ($row, &$data) {
@ -248,7 +219,7 @@ class Postgres
*/
public function getSchemaSize($database, $schema)
{
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('POSTGRES_PASSWORD'), $GLOBALS['POSTGRES_HOST_ADDR'], $database);
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('PGSQL_ROOT_PASSWORD'), $GLOBALS['PGSQL_HOST_ADDR'], $database);
$callback = function ($row, &$data) {
$data = $row['size'];
@ -277,7 +248,7 @@ class Postgres
*/
public function getTableCount($database, $schema)
{
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('POSTGRES_PASSWORD'), $GLOBALS['POSTGRES_HOST_ADDR'], $database);
$PSQL = new Postgres('postgres', loadClass('Docker')->getEnv('PGSQL_ROOT_PASSWORD'), $GLOBALS['PGSQL_HOST_ADDR'], $database);
$callback = function ($row, &$data) {
$data = $row['count'];
};
@ -297,53 +268,57 @@ class Postgres
}
/*********************************************************************************
*
* MySQL Error functions
* Interface required functions
*
*********************************************************************************/
/**
* Return connection error message.
* Get PgSQL Name.
*
* @return string Error message
* @return string PgSQL short name.
*/
public function getConnectError()
public function getName($default = 'PostgreSQL')
{
return $this->_connect_error;
if (!static::isAvailable('pgsql')) {
return $default;
}
$callback = function ($row, &$data) {
$data = $row['version'];
};
$name = $this->egrep('/[a-zA-Z0-9]*/', $this->select('SELECT version();', $callback));
if (!$name) {
loadClass('Logger')->error('Could not get PgSQL Name');
return $default;
}
return $name;
}
/**
* Return connection errno code.
* Get PgSQL Version.
*
* @return integer Error code
* @return string PgSQL version.
*/
public function getConnectErrno()
public function getVersion()
{
return $this->_connect_errno;
}
if (!static::isAvailable('pgsql')) {
return '';
}
/**
* Return error message.
*
* @return string Error message
*/
public function getError()
{
return $this->_error;
}
$callback = function ($row, &$data) {
$data = $row['version'];
};
/**
* Return errno code.
*
* @return integer Error code
*/
public function getErrno()
{
return $this->_errno;
$version = $this->egrep('/[.0-9]+/', $this->select('SELECT version();', $callback));
if (!$version) {
loadClass('Logger')->error('Could not get PgSQL version');
return '';
}
return $version;
}
}

View File

@ -0,0 +1,158 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Php extends _Base implements _iBase
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
/**
* Httpd instance
* @var Httpd|null
*/
protected static $instance = null;
/**
* Singleton Instance getter.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
* @return object|null
*/
public static function getInstance($host = null, $user = null, $pass = null)
{
if (!isset(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
/**
* @overwrite
* @param string $hostname [description]
* @return boolean [description]
*/
public static function isAvailable($hostname)
{
// Always available, otherwise you would not see any browser output.
return true;
}
/**
* Connect to PHP
*
* @param string $err Reference to error message
* @param string $host Redis hostname
* @return boolean
*/
public static function testConnection(&$err, $host, $user = '', $pass = '')
{
// Connection is always working, otherwise you would not see any browser output.
return true;
}
/*********************************************************************************
*
* Construct/Destructor
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct()
{
}
/**
* Destructor
*/
public function __destruct()
{
}
/*********************************************************************************
*
* PHP Select functions
*
*********************************************************************************/
public function getUid()
{
$output = null;
$return = $this->_exec('id', $output);
$uid = $this->egrep('/uid=[0-9]+/', isset($output[0]) ? $output[0] : '');
$uid = $this->egrep('/[0-9]+/', $uid);
return $uid;
}
public function getGid()
{
$output = null;
$return = $this->_exec('id', $output);
$uid = $this->egrep('/gid=[0-9]+/', isset($output[0]) ? $output[0] : '');
$uid = $this->egrep('/[0-9]+/', $uid);
return $uid;
}
/*********************************************************************************
*
* Interface required functions
*
*********************************************************************************/
public function getName($default = 'PHP')
{
if (defined('HHVM_VERSION')) {
return 'HHVM';
}
return $default;
}
public function getVersion()
{
if (defined('HHVM_VERSION')) {
return HHVM_VERSION . ' php-'.str_replace('-hhvm', '', phpversion());
} else {
return phpversion();
}
}
/*********************************************************************************
*
* Private functions
*
*********************************************************************************/
/**
* Executes shell commands on the PHP-FPM Host
*
* @param string $cmd Command
* @param string $output Reference to output
* @return integer
*/
private function _exec($cmd, &$output = '')
{
// Clean output
$output = '';
exec($cmd, $output, $exit_code);
return $exit_code;
}
}

View File

@ -4,7 +4,7 @@ namespace devilbox;
/**
* @requires devilbox::Logger
*/
class Redis
class Redis extends _Base implements _iBase
{
/*********************************************************************************
@ -27,15 +27,14 @@ class Redis
* @param string $host Host
* @return object|null
*/
public static function getInstance($host = null)
public static function getInstance($host, $user = null, $pass = null)
{
if (!isset(static::$instance)) {
static::$instance = new static($host);
}
// If current Redis instance was unable to connect
if (!static::$instance) {
loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
//return null;
//loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
}
return static::$instance;
}
@ -47,7 +46,7 @@ class Redis
* @param string $host Redis hostname
* @return boolean
*/
public static function testConnection(&$err, $host)
public static function testConnection(&$err, $host, $user = '', $pass = '')
{
$err = false;
@ -56,7 +55,7 @@ class Redis
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
$err = 'Failed to connect to Redis host on '.$host.': ' .$redis->getLastError();
$err = 'Failed to connect to Redis host on '.$host;
error_reporting(-1);
return false;
}
@ -79,30 +78,6 @@ class Redis
*/
private $_redis = null;
/**
* Connection error string
* @var string
*/
private $_connect_error = '';
/**
* Connection error code
* @var integer
*/
private $_connect_errno = 0;
/**
* Error string
* @var string
*/
private $_error = '';
/**
* Error code
* @var integer
*/
private $_errno = 0;
@ -126,9 +101,9 @@ class Redis
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
$this->_connect_error = 'Failed to connect to Redis host on '.$host.': ' .$redis->getLastError();
$this->_connect_errno = 1;
loadClass('Logger')->error($this->_connect_error);
$this->setConnectError('Failed to connect to Redis host on '.$host);
$this->setConnectErrno(1);
//loadClass('Logger')->error($this->_connect_error);
} else {
$this->_redis = $redis;
}
@ -145,72 +120,52 @@ class Redis
}
}
/*********************************************************************************
*
* Redis Select functions
*
*********************************************************************************/
public function getVersion()
{
$info = $this->_redis->info();
return 'Redis '.$info['redis_version'];
}
public function getInfo()
{
return $this->_redis->info('all');
if ($this->_redis) {
return $this->_redis->info('all');
} else {
return array();
}
}
public function getKeys()
{
return $this->_redis->keys('*');
if ($this->_redis) {
return $this->_redis->keys('*');
} else {
return array();
}
}
/*********************************************************************************
*
* MySQL Error functions
* Interface required functions
*
*********************************************************************************/
/**
* Return connection error message.
*
* @return string Error message
*/
public function getConnectError()
public function getName($default = 'Redis')
{
return $this->_connect_error;
return $default;
}
/**
* Return connection errno code.
*
* @return integer Error code
*/
public function getConnectErrno()
public function getVersion()
{
return $this->_connect_errno;
}
/**
* Return error message.
*
* @return string Error message
*/
public function getError()
{
return $this->_error;
}
/**
* Return errno code.
*
* @return integer Error code
*/
public function getErrno()
{
return $this->_errno;
$info = $this->getInfo();
if (!isset($info['redis_version'])) {
loadClass('Logger')->error('Could not get Redis version');
return '';
}
return $info['redis_version'];
}
}

View File

@ -0,0 +1,168 @@
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
class _Base
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
private static $_available = array();
private static $_hostname = array();
public static function isAvailable($hostname)
{
if (!isset(self::$_available[$hostname])) {
$ip = gethostbyname($hostname);
self::$_available[$hostname] = ($ip == $hostname) ? false : true;
}
return self::$_available[$hostname];
}
public static function getIpAddress($hostname)
{
if (!isset(self::$_hostname[$hostname])) {
self::$_hostname[$hostname] = gethostbyname($hostname);
}
return self::$_hostname[$hostname];
}
/*********************************************************************************
*
* Instance
*
*********************************************************************************/
/**
* Connection error string
* @var string
*/
private $_connect_error = '';
/**
* Connection error code
* @var integer
*/
private $_connect_errno = 0;
/**
* Error string
* @var string
*/
private $_error = '';
/**
* Error code
* @var integer
*/
private $_errno = 0;
/**
* Shorter version to regex select a string.
*
* @param string $regex Regex
* @param string $string String to look in to
* @return bool|string Returns false on error otherwise the string
*/
public function egrep($regex, $string)
{
$match = array();
$error = preg_match($regex, $string, $match);
if ($error === false) {
return false;
}
return isset($match[0]) ? $match[0] : false;
}
/**
* Set Connection Error Message.
*
* @param string $error Error Message
*/
public function setConnectError($error)
{
$this->_connect_error = $error;
}
/**
* Set Connection Error Code.
*
* @param integer $errno Error Code
*/
public function setConnectErrno($errno)
{
$this->_connect_erro = $errno;
}
/**
* Set Error Message.
*
* @param string $error Error message
*/
public function setError($error)
{
$this->_error = $error;
}
/**
* Set Error Code.
*
* @param integer $errno Error Code
*/
public function setErrno($errno)
{
$this->_erro = $errno;
}
/**
* Return connection error message.
*
* @return string Error message
*/
public function getConnectError()
{
return $this->_connect_error;
}
/**
* Return connection errno code.
*
* @return integer Error code
*/
public function getConnectErrno()
{
return $this->_connect_errno;
}
/**
* Return error message.
*
* @return string Error message
*/
public function getError()
{
return $this->_error;
}
/**
* Return errno code.
*
* @return integer Error code
*/
public function getErrno()
{
return $this->_errno;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace devilbox;
/**
* Interface
*/
interface _iBase
{
/**
* Get singleton instance
* @return Object
*/
public static function getInstance($host, $user, $pass);
public static function isAvailable($hostname);
public static function testConnection(&$err, $host, $user, $pass);
public static function getIpAddress($hostname);
public function getName($default);
public function getVersion();
}