From a5cf4f9483df355f9ff45799a8d592dd31a23afe Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 6 May 2017 11:13:33 +0200 Subject: [PATCH] REL-0.9 Updating intranet class files --- .devilbox/www/include/lib/Docker.php | 122 ------------ .devilbox/www/include/lib/Httpd.php | 131 +++++++++++++ .devilbox/www/include/lib/Logger.php | 12 ++ .devilbox/www/include/lib/Memcd.php | 185 ++++++++++++++++++ .devilbox/www/include/lib/Mysql.php | 144 +++++++------- .../include/lib/{Postgres.php => Pgsql.php} | 125 +++++------- .devilbox/www/include/lib/Php.php | 158 +++++++++++++++ .devilbox/www/include/lib/Redis.php | 107 +++------- .devilbox/www/include/lib/_Base.php | 168 ++++++++++++++++ .devilbox/www/include/lib/_iBase.php | 22 +++ 10 files changed, 832 insertions(+), 342 deletions(-) create mode 100644 .devilbox/www/include/lib/Httpd.php create mode 100644 .devilbox/www/include/lib/Memcd.php rename .devilbox/www/include/lib/{Postgres.php => Pgsql.php} (77%) create mode 100644 .devilbox/www/include/lib/Php.php create mode 100644 .devilbox/www/include/lib/_Base.php create mode 100644 .devilbox/www/include/lib/_iBase.php diff --git a/.devilbox/www/include/lib/Docker.php b/.devilbox/www/include/lib/Docker.php index 76a5a167..3df5db97 100644 --- a/.devilbox/www/include/lib/Docker.php +++ b/.devilbox/www/include/lib/Docker.php @@ -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 . '
(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'; - } - } diff --git a/.devilbox/www/include/lib/Httpd.php b/.devilbox/www/include/lib/Httpd.php new file mode 100644 index 00000000..fbae1c82 --- /dev/null +++ b/.devilbox/www/include/lib/Httpd.php @@ -0,0 +1,131 @@ +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; + } +} diff --git a/.devilbox/www/include/lib/Logger.php b/.devilbox/www/include/lib/Logger.php index b8281e7e..34cea0ab 100644 --- a/.devilbox/www/include/lib/Logger.php +++ b/.devilbox/www/include/lib/Logger.php @@ -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; + } } diff --git a/.devilbox/www/include/lib/Memcd.php b/.devilbox/www/include/lib/Memcd.php new file mode 100644 index 00000000..77233321 --- /dev/null +++ b/.devilbox/www/include/lib/Memcd.php @@ -0,0 +1,185 @@ +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]; + } +} diff --git a/.devilbox/www/include/lib/Mysql.php b/.devilbox/www/include/lib/Mysql.php index 81ef2762..edc8b482 100644 --- a/.devilbox/www/include/lib/Mysql.php +++ b/.devilbox/www/include/lib/Mysql.php @@ -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; } } diff --git a/.devilbox/www/include/lib/Postgres.php b/.devilbox/www/include/lib/Pgsql.php similarity index 77% rename from .devilbox/www/include/lib/Postgres.php rename to .devilbox/www/include/lib/Pgsql.php index 7897f17c..fd00117f 100644 --- a/.devilbox/www/include/lib/Postgres.php +++ b/.devilbox/www/include/lib/Pgsql.php @@ -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; } } diff --git a/.devilbox/www/include/lib/Php.php b/.devilbox/www/include/lib/Php.php new file mode 100644 index 00000000..b8a5535f --- /dev/null +++ b/.devilbox/www/include/lib/Php.php @@ -0,0 +1,158 @@ +_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; + } +} diff --git a/.devilbox/www/include/lib/Redis.php b/.devilbox/www/include/lib/Redis.php index f78cfd8b..46732603 100644 --- a/.devilbox/www/include/lib/Redis.php +++ b/.devilbox/www/include/lib/Redis.php @@ -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']; } } diff --git a/.devilbox/www/include/lib/_Base.php b/.devilbox/www/include/lib/_Base.php new file mode 100644 index 00000000..dd098a0e --- /dev/null +++ b/.devilbox/www/include/lib/_Base.php @@ -0,0 +1,168 @@ +_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; + } +} diff --git a/.devilbox/www/include/lib/_iBase.php b/.devilbox/www/include/lib/_iBase.php new file mode 100644 index 00000000..dba2d76a --- /dev/null +++ b/.devilbox/www/include/lib/_iBase.php @@ -0,0 +1,22 @@ +