forked from kevinowino869/mitrobill
update PEAR
This commit is contained in:
parent
1861358415
commit
071df91de0
@ -1,8 +1,73 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Standard Autoloader for PEAR2
|
||||||
|
*
|
||||||
|
* PEAR2_Autoload is the standard method of class loading for development and
|
||||||
|
* low-volume web sites using PEAR2 packages.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category PEAR2
|
||||||
|
* @package PEAR2_Autoload
|
||||||
|
* @author Gregory Beaver <cellog@php.net>
|
||||||
|
* @author Brett Bieber <saltybeagle@php.net>
|
||||||
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
||||||
|
* @version 0.3.0
|
||||||
|
* @link http://pear2.php.net/PEAR2_Autoload
|
||||||
|
*/
|
||||||
namespace PEAR2;
|
namespace PEAR2;
|
||||||
|
|
||||||
if (!class_exists('\PEAR2\Autoload', false)) {
|
if (!class_exists('\PEAR2\Autoload', false)) {
|
||||||
|
/**
|
||||||
|
* Standard Autoloader for PEAR2
|
||||||
|
*
|
||||||
|
* PEAR2_Autoload is the standard method of class loading for development
|
||||||
|
* and low-volume web sites using PEAR2 packages.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category PEAR2
|
||||||
|
* @package PEAR2_Autoload
|
||||||
|
* @author Gregory Beaver <cellog@php.net>
|
||||||
|
* @author Brett Bieber <saltybeagle@php.net>
|
||||||
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD
|
||||||
|
* New BSDLicense
|
||||||
|
* @link http://pear2.php.net/PEAR2_Autoload
|
||||||
|
*/
|
||||||
class Autoload
|
class Autoload
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Used at {@link initialize()} to specify that the load function, path
|
||||||
|
* and map should be appended to the respective lists.
|
||||||
|
*/
|
||||||
|
const APPEND = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used at {@link initialize()} to specify that the load function should
|
||||||
|
* be prepended on the autoload stack, instead of being appended.
|
||||||
|
*/
|
||||||
|
const PREPEND_LOAD = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used at {@link initialize()} to specify that the path should be
|
||||||
|
* prepended on the list of paths, instead of being appended.
|
||||||
|
*/
|
||||||
|
const PREPEND_PATH = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used at {@link initialize()} to specify that the map should be
|
||||||
|
* prepended on the list of maps, instead of being appended.
|
||||||
|
*/
|
||||||
|
const PREPEND_MAP = 4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used at {@link initialize()} to specify that the load function, path
|
||||||
|
* and map should be prepended on their respective lists, instead of
|
||||||
|
* being appended.
|
||||||
|
*/
|
||||||
|
const PREPEND = 7;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the autoload class has been spl_autoload_register-ed
|
* Whether the autoload class has been spl_autoload_register-ed
|
||||||
*
|
*
|
||||||
@ -45,75 +110,113 @@ if (!class_exists('\PEAR2\Autoload', false)) {
|
|||||||
*/
|
*/
|
||||||
protected static $unmapped = array();
|
protected static $unmapped = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array of functions to be checked in exception traces.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $checkFunctions = array(
|
||||||
|
'class_exists', 'interface_exists'
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the PEAR2 autoloader
|
* Initialize the PEAR2 autoloader
|
||||||
*
|
*
|
||||||
* @param string $path Directory path to register
|
* @param string $path Directory path(s) to register.
|
||||||
|
* @param string $mapfile Path to a mapping file to register.
|
||||||
|
* @param int $flags A bitmaks with options for the autoloader.
|
||||||
|
* See the PREPEND(_*) constants for details.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
static function initialize($path, $mapfile = null)
|
public static function initialize(
|
||||||
{
|
$path,
|
||||||
self::register();
|
$mapfile = null,
|
||||||
self::addPath($path);
|
$flags = self::APPEND
|
||||||
self::addMap($mapfile);
|
) {
|
||||||
|
self::register(0 !== $flags & self::PREPEND_LOAD);
|
||||||
|
self::addPath($path, 0 !== ($flags & self::PREPEND_PATH));
|
||||||
|
self::addMap($mapfile, 0 !== ($flags & self::PREPEND_MAP));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the PEAR2 autoload class with spl_autoload_register
|
* Register the PEAR2 autoload class with spl_autoload_register
|
||||||
*
|
*
|
||||||
|
* @param bool $prepend Whether to prepend the load function to the
|
||||||
|
* autoload stack instead of appending it.
|
||||||
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function register()
|
protected static function register($prepend = false)
|
||||||
{
|
{
|
||||||
if (!self::$registered) {
|
if (!self::$registered) {
|
||||||
// set up __autoload
|
// set up __autoload
|
||||||
$autoload = spl_autoload_functions();
|
$autoload = spl_autoload_functions();
|
||||||
spl_autoload_register('PEAR2\Autoload::load');
|
spl_autoload_register('PEAR2\Autoload::load', true, $prepend);
|
||||||
if (function_exists('__autoload') && ($autoload === false)) {
|
if (function_exists('__autoload') && ($autoload === false)) {
|
||||||
// __autoload() was being used, but now would be ignored, add
|
// __autoload() was being used, but now would be ignored,
|
||||||
// it to the autoload stack
|
// add it to the autoload stack
|
||||||
spl_autoload_register('__autoload');
|
spl_autoload_register('__autoload');
|
||||||
}
|
}
|
||||||
|
if (function_exists('trait_exists')) {
|
||||||
|
self::$checkFunctions[] = 'trait_exists';
|
||||||
|
}
|
||||||
|
self::$registered = true;
|
||||||
}
|
}
|
||||||
self::$registered = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a path
|
* Add a path
|
||||||
*
|
*
|
||||||
* @param string $path The directory to add to the set of PEAR2 paths
|
* @param string $paths The folder(s) to add to the set of paths.
|
||||||
|
* @param bool $prepend Whether to prepend the path to the list of
|
||||||
|
* paths instead of appending it.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function addPath($path)
|
protected static function addPath($paths, $prepend = false)
|
||||||
{
|
{
|
||||||
if (!in_array($path, self::$paths)) {
|
foreach (explode(PATH_SEPARATOR, $paths) as $path) {
|
||||||
self::$paths[] = $path;
|
if (!in_array($path, self::$paths)) {
|
||||||
|
if ($prepend) {
|
||||||
|
self::$paths = array_merge(array($path), self::$paths);
|
||||||
|
} else {
|
||||||
|
self::$paths[] = $path;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a classname-to-file map
|
* Add a classname-to-file map
|
||||||
*
|
*
|
||||||
* @param string $mapfile The filename of the classmap
|
* @param string $mapfile The filename of the classmap.
|
||||||
|
* @param bool $prepend Whether to prepend the map to the list of maps
|
||||||
|
* instead of appending it.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function addMap($mapfile)
|
protected static function addMap($mapfile, $prepend = false)
|
||||||
{
|
{
|
||||||
if (! in_array($mapfile, self::$maps)) {
|
if (!in_array($mapfile, self::$maps)) {
|
||||||
|
|
||||||
// keep track of specific map file loaded in this
|
// keep track of specific map file loaded in this
|
||||||
// instance so we can update it if necessary
|
// instance so we can update it if necessary
|
||||||
self::$mapfile = $mapfile;
|
self::$mapfile = $mapfile;
|
||||||
|
|
||||||
if (file_exists($mapfile)) {
|
if (is_file($mapfile)) {
|
||||||
$map = include $mapfile;
|
$map = include $mapfile;
|
||||||
if (is_array($map)) {
|
if (is_array($map)) {
|
||||||
// mapfile contains a valid map, so we'll keep it
|
// mapfile contains a valid map, so we'll keep it
|
||||||
self::$maps[] = $mapfile;
|
if ($prepend) {
|
||||||
self::$map = array_merge(self::$map, $map);
|
self::$maps = array_merge(
|
||||||
|
array($mapfile),
|
||||||
|
self::$maps
|
||||||
|
);
|
||||||
|
self::$map = array_merge($map, self::$map);
|
||||||
|
} else {
|
||||||
|
self::$maps[] = $mapfile;
|
||||||
|
self::$map = array_merge(self::$map, $map);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,13 +249,13 @@ if (!class_exists('\PEAR2\Autoload', false)) {
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
static function load($class)
|
public static function load($class)
|
||||||
{
|
{
|
||||||
// need to check if there's a current map file specified ALSO.
|
// need to check if there's a current map file specified ALSO.
|
||||||
// this could be the first time writing it.
|
// this could be the first time writing it.
|
||||||
$mapped = self::isMapped($class);
|
$mapped = self::isMapped($class);
|
||||||
if ($mapped) {
|
if ($mapped && is_file(self::$map[$class])) {
|
||||||
require self::$map[$class];
|
include self::$map[$class];
|
||||||
if (!self::loadSuccessful($class)) {
|
if (!self::loadSuccessful($class)) {
|
||||||
// record this failure & keep going, we may still find it
|
// record this failure & keep going, we may still find it
|
||||||
self::$unmapped[] = $class;
|
self::$unmapped[] = $class;
|
||||||
@ -161,34 +264,59 @@ if (!class_exists('\PEAR2\Autoload', false)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$file = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
|
$file = '';
|
||||||
|
$className = $class;
|
||||||
|
if (false !== $lastNsPos = strrpos($class, '\\')) {
|
||||||
|
$namespace = substr($class, 0, $lastNsPos);
|
||||||
|
$className = substr($class, $lastNsPos + 1);
|
||||||
|
$file = str_replace(
|
||||||
|
'\\',
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
$namespace
|
||||||
|
) . DIRECTORY_SEPARATOR;
|
||||||
|
}
|
||||||
|
$file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
|
||||||
foreach (self::$paths as $path) {
|
foreach (self::$paths as $path) {
|
||||||
if (file_exists($path . DIRECTORY_SEPARATOR . $file)) {
|
if (is_file($path . DIRECTORY_SEPARATOR . $file)) {
|
||||||
require $path . DIRECTORY_SEPARATOR . $file;
|
include $path . DIRECTORY_SEPARATOR . $file;
|
||||||
if (!self::loadSuccessful($class)) {
|
if (!self::loadSuccessful($class)) {
|
||||||
throw new \Exception('Class ' . $class . ' was not present in ' .
|
if (count(spl_autoload_functions()) > 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
throw new \Exception(
|
||||||
|
'Class ' . $class . ' was not present in ' .
|
||||||
$path . DIRECTORY_SEPARATOR . $file .
|
$path . DIRECTORY_SEPARATOR . $file .
|
||||||
'") [PEAR2_Autoload-0.2.4]');
|
'") [PEAR2_Autoload-@PACKAGE_VERSION@]'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_array($class, self::$unmapped)) {
|
if (in_array($class, self::$unmapped)) {
|
||||||
self::updateMap($class, $path . DIRECTORY_SEPARATOR . $file);
|
self::updateMap(
|
||||||
|
$class,
|
||||||
|
$path . DIRECTORY_SEPARATOR . $file
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (count(spl_autoload_functions()) > 1) {
|
||||||
$e = new \Exception('Class ' . $class . ' could not be loaded from ' .
|
|
||||||
$file . ', file does not exist (registered paths="' .
|
|
||||||
implode(PATH_SEPARATOR, self::$paths) .
|
|
||||||
'") [PEAR2_Autoload-0.2.4]');
|
|
||||||
$trace = $e->getTrace();
|
|
||||||
if (isset($trace[2]) && isset($trace[2]['function']) &&
|
|
||||||
in_array($trace[2]['function'], array('class_exists', 'interface_exists'))) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($trace[1]) && isset($trace[1]['function']) &&
|
$e = new \Exception(
|
||||||
in_array($trace[1]['function'], array('class_exists', 'interface_exists'))) {
|
'Class ' . $class . ' could not be loaded from ' .
|
||||||
|
$file . ', file does not exist (registered paths="' .
|
||||||
|
implode(PATH_SEPARATOR, self::$paths) .
|
||||||
|
'") [PEAR2_Autoload-@PACKAGE_VERSION@]'
|
||||||
|
);
|
||||||
|
$trace = $e->getTrace();
|
||||||
|
if (isset($trace[2]) && isset($trace[2]['function'])
|
||||||
|
&& in_array($trace[2]['function'], self::$checkFunctions)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (isset($trace[1]) && isset($trace[1]['function'])
|
||||||
|
&& in_array($trace[1]['function'], self::$checkFunctions)
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
throw $e;
|
throw $e;
|
||||||
@ -197,30 +325,35 @@ if (!class_exists('\PEAR2\Autoload', false)) {
|
|||||||
/**
|
/**
|
||||||
* Check if the requested class was loaded from the specified path
|
* Check if the requested class was loaded from the specified path
|
||||||
*
|
*
|
||||||
|
* @param string $class The name of the class to check.
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected static function loadSuccessful($class)
|
protected static function loadSuccessful($class)
|
||||||
{
|
{
|
||||||
if (!class_exists($class, false) && !interface_exists($class, false)) {
|
return class_exists($class, false)
|
||||||
return false;
|
|| interface_exists($class, false)
|
||||||
}
|
|| (in_array('trait_exists', self::$checkFunctions, true)
|
||||||
return true;
|
&& trait_exists($class, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If possible, update the classmap file with newly-discovered
|
* If possible, update the classmap file with newly-discovered
|
||||||
* mapping.
|
* mapping.
|
||||||
*
|
*
|
||||||
* @param string $class Class name discovered
|
* @param string $class Class name discovered
|
||||||
*
|
|
||||||
* @param string $origin File where class was found
|
* @param string $origin File where class was found
|
||||||
*
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function updateMap($class, $origin)
|
protected static function updateMap($class, $origin)
|
||||||
{
|
{
|
||||||
if (is_writable(self::$mapfile) || is_writable(dirname(self::$mapfile))) {
|
if (is_writable(self::$mapfile)
|
||||||
|
|| is_writable(dirname(self::$mapfile))
|
||||||
|
) {
|
||||||
self::$map[$class] = $origin;
|
self::$map[$class] = $origin;
|
||||||
file_put_contents(self::$mapfile,
|
file_put_contents(
|
||||||
|
self::$mapfile,
|
||||||
'<'."?php\n"
|
'<'."?php\n"
|
||||||
. "// PEAR2\Autoload auto-generated classmap\n"
|
. "// PEAR2\Autoload auto-generated classmap\n"
|
||||||
. "return " . var_export(self::$map, true) . ';',
|
. "return " . var_export(self::$map, true) . ';',
|
||||||
@ -230,11 +363,11 @@ if (!class_exists('\PEAR2\Autoload', false)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the array of paths PEAR2 autoload has registered
|
* Return the array of paths PEAR2 autoload has registered
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
static function getPaths()
|
public static function getPaths()
|
||||||
{
|
{
|
||||||
return self::$paths;
|
return self::$paths;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -50,14 +51,16 @@ use PEAR2\Cache\SHM\InvalidArgumentException;
|
|||||||
abstract class SHM implements IteratorAggregate
|
abstract class SHM implements IteratorAggregate
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array An array of adapter names that meet their requirements.
|
* An array of adapter names that meet their requirements.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static $_adapters = array();
|
private static $_adapters = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new shared memory storage.
|
* Creates a new shared memory storage.
|
||||||
*
|
*
|
||||||
* Estabilishes a separate persistent storage. Adapter is automatically
|
* Establishes a separate persistent storage. Adapter is automatically
|
||||||
* chosen based on the available extensions.
|
* chosen based on the available extensions.
|
||||||
*
|
*
|
||||||
* @param string $persistentId The ID for the storage.
|
* @param string $persistentId The ID for the storage.
|
||||||
@ -207,7 +210,7 @@ abstract class SHM implements IteratorAggregate
|
|||||||
/**
|
/**
|
||||||
* Creates a new shared memory storage.
|
* Creates a new shared memory storage.
|
||||||
*
|
*
|
||||||
* Estabilishes a separate persistent storage.
|
* Establishes a separate persistent storage.
|
||||||
*
|
*
|
||||||
* @param string $persistentId The ID for the storage. The storage will be
|
* @param string $persistentId The ID for the storage. The storage will be
|
||||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||||
@ -220,7 +223,7 @@ abstract class SHM implements IteratorAggregate
|
|||||||
*
|
*
|
||||||
* @param string $key Name of the key to obtain. Note that $key may
|
* @param string $key Name of the key to obtain. Note that $key may
|
||||||
* repeat for each distinct $persistentId.
|
* repeat for each distinct $persistentId.
|
||||||
* @param double $timeout If the lock can't be immediatly obtained, the
|
* @param double $timeout If the lock can't be immediately obtained, the
|
||||||
* script will block for at most the specified amount of seconds.
|
* script will block for at most the specified amount of seconds.
|
||||||
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||||
* to NULL makes it block without a time limit.
|
* to NULL makes it block without a time limit.
|
||||||
@ -368,4 +371,5 @@ abstract class SHM implements IteratorAggregate
|
|||||||
|
|
||||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Placebo');
|
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Placebo');
|
||||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Wincache');
|
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\Wincache');
|
||||||
|
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\APCu');
|
||||||
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\APC');
|
SHM::registerAdapter('\\' . __NAMESPACE__ . '\SHM\Adapter\APC');
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -42,7 +43,9 @@ use ArrayObject;
|
|||||||
class APC extends SHM
|
class APC extends SHM
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string ID of the current storage.
|
* ID of the current storage.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $persistentId;
|
protected $persistentId;
|
||||||
|
|
||||||
@ -53,20 +56,25 @@ class APC extends SHM
|
|||||||
* (as a value) specifying the number of instances in the current request.
|
* (as a value) specifying the number of instances in the current request.
|
||||||
* Used as an attempt to ensure implicit lock releases even on errors in the
|
* Used as an attempt to ensure implicit lock releases even on errors in the
|
||||||
* critical sections, since APC doesn't have an actual locking function.
|
* critical sections, since APC doesn't have an actual locking function.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $requestInstances = array();
|
protected static $requestInstances = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Array of lock names (as values) for each persistent ID (as
|
* Array of lock names for each persistent ID.
|
||||||
* key) obtained during the current request.
|
*
|
||||||
|
* Array of lock names (as values) for each persistent ID (as key) obtained
|
||||||
|
* during the current request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $locksBackup = array();
|
protected static $locksBackup = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new shared memory storage.
|
* Creates a new shared memory storage.
|
||||||
*
|
*
|
||||||
* Estabilishes a separate persistent storage.
|
* Establishes a separate persistent storage.
|
||||||
*
|
*
|
||||||
* @param string $persistentId The ID for the storage. The storage will be
|
* @param string $persistentId The ID for the storage. The storage will be
|
||||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||||
@ -96,7 +104,7 @@ class APC extends SHM
|
|||||||
public static function isMeetingRequirements()
|
public static function isMeetingRequirements()
|
||||||
{
|
{
|
||||||
return extension_loaded('apc')
|
return extension_loaded('apc')
|
||||||
&& version_compare(phpversion('apc'), '3.0.13', '>=')
|
&& version_compare(phpversion('apc'), '3.1.1', '>=')
|
||||||
&& ini_get('apc.enabled')
|
&& ini_get('apc.enabled')
|
||||||
&& ('cli' !== PHP_SAPI || ini_get('apc.enable_cli'));
|
&& ('cli' !== PHP_SAPI || ini_get('apc.enable_cli'));
|
||||||
}
|
}
|
||||||
@ -116,6 +124,7 @@ class APC extends SHM
|
|||||||
* shutdown.
|
* shutdown.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public static function releaseLocks($internalPersistentId, $isAtShutdown)
|
public static function releaseLocks($internalPersistentId, $isAtShutdown)
|
||||||
@ -144,7 +153,7 @@ class APC extends SHM
|
|||||||
*
|
*
|
||||||
* @param string $key Name of the key to obtain. Note that $key may
|
* @param string $key Name of the key to obtain. Note that $key may
|
||||||
* repeat for each distinct $persistentId.
|
* repeat for each distinct $persistentId.
|
||||||
* @param double $timeout If the lock can't be immediatly obtained, the
|
* @param double $timeout If the lock can't be immediately obtained, the
|
||||||
* script will block for at most the specified amount of seconds.
|
* script will block for at most the specified amount of seconds.
|
||||||
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||||
* to NULL makes it block without a time limit.
|
* to NULL makes it block without a time limit.
|
||||||
@ -178,11 +187,13 @@ class APC extends SHM
|
|||||||
$lock = $this->persistentId . 'l ' . $key;
|
$lock = $this->persistentId . 'l ' . $key;
|
||||||
$success = apc_delete($lock);
|
$success = apc_delete($lock);
|
||||||
if ($success) {
|
if ($success) {
|
||||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
unset(
|
||||||
$key,
|
static::$locksBackup[$this->persistentId][array_search(
|
||||||
static::$locksBackup[$this->persistentId],
|
$key,
|
||||||
true
|
static::$locksBackup[$this->persistentId],
|
||||||
)]);
|
true
|
||||||
|
)]
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
416
system/autoload/PEAR2/Cache/SHM/Adapter/APCu.php
Normal file
416
system/autoload/PEAR2/Cache/SHM/Adapter/APCu.php
Normal file
@ -0,0 +1,416 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
|
*
|
||||||
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Caching
|
||||||
|
* @package PEAR2_Cache_SHM
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @copyright 2011 Vasil Rangelov
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @version 0.2.0
|
||||||
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* The namespace declaration.
|
||||||
|
*/
|
||||||
|
namespace PEAR2\Cache\SHM\Adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws exceptions from this namespace, and extends from this class.
|
||||||
|
*/
|
||||||
|
use PEAR2\Cache\SHM;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link APC::getIterator()} returns this object.
|
||||||
|
*/
|
||||||
|
use ArrayObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared memory adapter for the APC extension.
|
||||||
|
*
|
||||||
|
* @category Caching
|
||||||
|
* @package PEAR2_Cache_SHM
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
|
*/
|
||||||
|
class APCu extends SHM
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ID of the current storage.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $persistentId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of persistent IDs.
|
||||||
|
*
|
||||||
|
* A list of persistent IDs within the current request (as keys) with an int
|
||||||
|
* (as a value) specifying the number of instances in the current request.
|
||||||
|
* Used as an attempt to ensure implicit lock releases even on errors in the
|
||||||
|
* critical sections, since APC doesn't have an actual locking function.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $requestInstances = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Array of lock names for each persistent ID.
|
||||||
|
*
|
||||||
|
* Array of lock names (as values) for each persistent ID (as key) obtained
|
||||||
|
* during the current request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $locksBackup = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new shared memory storage.
|
||||||
|
*
|
||||||
|
* Establishes a separate persistent storage.
|
||||||
|
*
|
||||||
|
* @param string $persistentId The ID for the storage. The storage will be
|
||||||
|
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||||
|
* are namespaced by this ID.
|
||||||
|
*/
|
||||||
|
public function __construct($persistentId)
|
||||||
|
{
|
||||||
|
$this->persistentId = __CLASS__ . ' ' . $persistentId;
|
||||||
|
if (isset(static::$requestInstances[$this->persistentId])) {
|
||||||
|
static::$requestInstances[$this->persistentId]++;
|
||||||
|
} else {
|
||||||
|
static::$requestInstances[$this->persistentId] = 1;
|
||||||
|
static::$locksBackup[$this->persistentId] = array();
|
||||||
|
}
|
||||||
|
register_shutdown_function(
|
||||||
|
get_called_class() . '::releaseLocks',
|
||||||
|
$this->persistentId,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the adapter meets its requirements.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public static function isMeetingRequirements()
|
||||||
|
{
|
||||||
|
return extension_loaded('apcu')
|
||||||
|
&& version_compare(phpversion('apcu'), '5.0.0', '>=')
|
||||||
|
&& ini_get('apc.enabled')
|
||||||
|
&& ('cli' !== PHP_SAPI || ini_get('apc.enable_cli'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases all locks in a storage.
|
||||||
|
*
|
||||||
|
* This function is not meant to be used directly. It is implicitly called
|
||||||
|
* by the the destructor and as a shutdown function when the request ends.
|
||||||
|
* One of these calls ends up releasing any unreleased locks obtained
|
||||||
|
* during the request. A lock is also implicitly released as soon as there
|
||||||
|
* are no objects left in the current request using the same persistent ID.
|
||||||
|
*
|
||||||
|
* @param string $internalPersistentId The internal persistent ID, the locks
|
||||||
|
* of which are being released.
|
||||||
|
* @param bool $isAtShutdown Whether the function was executed at
|
||||||
|
* shutdown.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public static function releaseLocks($internalPersistentId, $isAtShutdown)
|
||||||
|
{
|
||||||
|
$hasInstances = 0 !== static::$requestInstances[$internalPersistentId];
|
||||||
|
if ($isAtShutdown === $hasInstances) {
|
||||||
|
foreach (static::$locksBackup[$internalPersistentId] as $key) {
|
||||||
|
apcu_delete($internalPersistentId . 'l ' . $key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases any locks obtained by this instance as soon as there are no more
|
||||||
|
* references to the object's persistent ID.
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
static::$requestInstances[$this->persistentId]--;
|
||||||
|
static::releaseLocks($this->persistentId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a named lock.
|
||||||
|
*
|
||||||
|
* @param string $key Name of the key to obtain. Note that $key may
|
||||||
|
* repeat for each distinct $persistentId.
|
||||||
|
* @param double $timeout If the lock can't be immediately obtained, the
|
||||||
|
* script will block for at most the specified amount of seconds.
|
||||||
|
* Setting this to 0 makes lock obtaining non blocking, and setting it
|
||||||
|
* to NULL makes it block without a time limit.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function lock($key, $timeout = null)
|
||||||
|
{
|
||||||
|
$lock = $this->persistentId . 'l ' . $key;
|
||||||
|
$hasTimeout = $timeout !== null;
|
||||||
|
$start = microtime(true);
|
||||||
|
while (!apcu_add($lock, 1)) {
|
||||||
|
if ($hasTimeout && (microtime(true) - $start) > $timeout) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static::$locksBackup[$this->persistentId] = $key;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases a named lock.
|
||||||
|
*
|
||||||
|
* @param string $key Name of the key to release. Note that $key may
|
||||||
|
* repeat for each distinct $persistentId.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function unlock($key)
|
||||||
|
{
|
||||||
|
$lock = $this->persistentId . 'l ' . $key;
|
||||||
|
$success = apcu_delete($lock);
|
||||||
|
if ($success) {
|
||||||
|
unset(
|
||||||
|
static::$locksBackup[$this->persistentId][array_search(
|
||||||
|
$key,
|
||||||
|
static::$locksBackup[$this->persistentId],
|
||||||
|
true
|
||||||
|
)]
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a specified key is in the storage.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to check.
|
||||||
|
*
|
||||||
|
* @return bool TRUE if the key is in the storage, FALSE otherwise.
|
||||||
|
*/
|
||||||
|
public function exists($key)
|
||||||
|
{
|
||||||
|
return apcu_exists($this->persistentId . 'd ' . $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a value to the shared memory storage.
|
||||||
|
*
|
||||||
|
* Adds a value to the storage if it doesn't exist, or fails if it does.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to associate the value with.
|
||||||
|
* @param mixed $value Value for the specified key.
|
||||||
|
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||||
|
* time limit.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function add($key, $value, $ttl = 0)
|
||||||
|
{
|
||||||
|
return apcu_add($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in the shared memory storage.
|
||||||
|
*
|
||||||
|
* Adds a value to the storage if it doesn't exist, overwrites it otherwise.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to associate the value with.
|
||||||
|
* @param mixed $value Value for the specified key.
|
||||||
|
* @param int $ttl Seconds to store the value. If set to 0 indicates no
|
||||||
|
* time limit.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function set($key, $value, $ttl = 0)
|
||||||
|
{
|
||||||
|
return apcu_store($this->persistentId . 'd ' . $key, $value, $ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value from the shared memory storage.
|
||||||
|
*
|
||||||
|
* Gets the current value, or throws an exception if it's not stored.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to get the value of.
|
||||||
|
*
|
||||||
|
* @return mixed The current value of the specified key.
|
||||||
|
*/
|
||||||
|
public function get($key)
|
||||||
|
{
|
||||||
|
$fullKey = $this->persistentId . 'd ' . $key;
|
||||||
|
if (apcu_exists($fullKey)) {
|
||||||
|
$value = apcu_fetch($fullKey, $success);
|
||||||
|
if (!$success) {
|
||||||
|
throw new SHM\InvalidArgumentException(
|
||||||
|
'Unable to fetch key. ' .
|
||||||
|
'Key has either just now expired or (if no TTL was set) ' .
|
||||||
|
'is possibly in a race condition with another request.',
|
||||||
|
100
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
throw new SHM\InvalidArgumentException('No such key in cache', 101);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a value from the shared memory storage.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to delete.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function delete($key)
|
||||||
|
{
|
||||||
|
return apcu_delete($this->persistentId . 'd ' . $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increases a value from the shared memory storage.
|
||||||
|
*
|
||||||
|
* Increases a value from the shared memory storage. Unlike a plain
|
||||||
|
* set($key, get($key)+$step) combination, this function also implicitly
|
||||||
|
* performs locking.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to increase.
|
||||||
|
* @param int $step Value to increase the key by.
|
||||||
|
*
|
||||||
|
* @return int The new value.
|
||||||
|
*/
|
||||||
|
public function inc($key, $step = 1)
|
||||||
|
{
|
||||||
|
$newValue = apcu_inc(
|
||||||
|
$this->persistentId . 'd ' . $key,
|
||||||
|
(int) $step,
|
||||||
|
$success
|
||||||
|
);
|
||||||
|
if (!$success) {
|
||||||
|
throw new SHM\InvalidArgumentException(
|
||||||
|
'Unable to increase the value. Are you sure the value is int?',
|
||||||
|
102
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $newValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decreases a value from the shared memory storage.
|
||||||
|
*
|
||||||
|
* Decreases a value from the shared memory storage. Unlike a plain
|
||||||
|
* set($key, get($key)-$step) combination, this function also implicitly
|
||||||
|
* performs locking.
|
||||||
|
*
|
||||||
|
* @param string $key Name of key to decrease.
|
||||||
|
* @param int $step Value to decrease the key by.
|
||||||
|
*
|
||||||
|
* @return int The new value.
|
||||||
|
*/
|
||||||
|
public function dec($key, $step = 1)
|
||||||
|
{
|
||||||
|
$newValue = apcu_dec(
|
||||||
|
$this->persistentId . 'd ' . $key,
|
||||||
|
(int) $step,
|
||||||
|
$success
|
||||||
|
);
|
||||||
|
if (!$success) {
|
||||||
|
throw new SHM\InvalidArgumentException(
|
||||||
|
'Unable to decrease the value. Are you sure the value is int?',
|
||||||
|
103
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $newValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a new value if a key has a certain value.
|
||||||
|
*
|
||||||
|
* Sets a new value if a key has a certain value. This function only works
|
||||||
|
* when $old and $new are longs.
|
||||||
|
*
|
||||||
|
* @param string $key Key of the value to compare and set.
|
||||||
|
* @param int $old The value to compare the key against.
|
||||||
|
* @param int $new The value to set the key to.
|
||||||
|
*
|
||||||
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
*/
|
||||||
|
public function cas($key, $old, $new)
|
||||||
|
{
|
||||||
|
return apcu_cas($this->persistentId . 'd ' . $key, $old, $new);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the persistent storage.
|
||||||
|
*
|
||||||
|
* Clears the persistent storage, i.e. removes all keys. Locks are left
|
||||||
|
* intact.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
foreach (new APCIterator(
|
||||||
|
'user',
|
||||||
|
'/^' . preg_quote($this->persistentId, '/') . 'd /',
|
||||||
|
APC_ITER_KEY,
|
||||||
|
100,
|
||||||
|
APC_LIST_ACTIVE
|
||||||
|
) as $key) {
|
||||||
|
apcu_delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve an external iterator
|
||||||
|
*
|
||||||
|
* Returns an external iterator.
|
||||||
|
*
|
||||||
|
* @param string|null $filter A PCRE regular expression.
|
||||||
|
* Only matching keys will be iterated over.
|
||||||
|
* Setting this to NULL matches all keys of this instance.
|
||||||
|
* @param bool $keysOnly Whether to return only the keys,
|
||||||
|
* or return both the keys and values.
|
||||||
|
*
|
||||||
|
* @return ArrayObject An array with all matching keys as array keys,
|
||||||
|
* and values as array values. If $keysOnly is TRUE, the array keys are
|
||||||
|
* numeric, and the array values are key names.
|
||||||
|
*/
|
||||||
|
public function getIterator($filter = null, $keysOnly = false)
|
||||||
|
{
|
||||||
|
$result = array();
|
||||||
|
foreach (new APCUIterator(
|
||||||
|
'/^' . preg_quote($this->persistentId, '/') . 'd /',
|
||||||
|
APC_ITER_KEY,
|
||||||
|
100,
|
||||||
|
APC_LIST_ACTIVE
|
||||||
|
) as $key) {
|
||||||
|
$localKey = strstr($key, $this->persistentId . 'd ');
|
||||||
|
if (null === $filter || preg_match($filter, $localKey)) {
|
||||||
|
if ($keysOnly) {
|
||||||
|
$result[] = $localKey;
|
||||||
|
} else {
|
||||||
|
$result[$localKey] = apcu_fetch($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ArrayObject($result);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -31,7 +32,7 @@ use PEAR2\Cache\SHM;
|
|||||||
use ArrayObject;
|
use ArrayObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This adapter is not truly persistent. It is intended to emulate persistency
|
* This adapter is not truly persistent. It is intended to emulate persistence
|
||||||
* in non persistent environments, so that upper level applications can use a
|
* in non persistent environments, so that upper level applications can use a
|
||||||
* single code path for persistent and non persistent code.
|
* single code path for persistent and non persistent code.
|
||||||
*
|
*
|
||||||
@ -44,7 +45,9 @@ use ArrayObject;
|
|||||||
class Placebo extends SHM
|
class Placebo extends SHM
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string ID of the current storage.
|
* ID of the current storage.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $persistentId;
|
protected $persistentId;
|
||||||
|
|
||||||
@ -54,13 +57,18 @@ class Placebo extends SHM
|
|||||||
* A list of persistent IDs within the current request (as keys) with an int
|
* A list of persistent IDs within the current request (as keys) with an int
|
||||||
* (as a value) specifying the number of instances in the current request.
|
* (as a value) specifying the number of instances in the current request.
|
||||||
* Used as an attempt to ensure implicit lock releases on destruction.
|
* Used as an attempt to ensure implicit lock releases on destruction.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $requestInstances = array();
|
protected static $requestInstances = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Array of lock names (as values) for each persistent ID (as
|
* Array of lock names for each persistent ID.
|
||||||
|
*
|
||||||
|
* Array of lock names (as values) for each persistent ID (as
|
||||||
* key) obtained during the current request.
|
* key) obtained during the current request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $locksBackup = array();
|
protected static $locksBackup = array();
|
||||||
|
|
||||||
@ -71,6 +79,7 @@ class Placebo extends SHM
|
|||||||
* Each such array has data keys as its keys, and an array as a value.
|
* Each such array has data keys as its keys, and an array as a value.
|
||||||
* Each such array has as its elements the value, the timeout and the time
|
* Each such array has as its elements the value, the timeout and the time
|
||||||
* the data was set.
|
* the data was set.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $data = array();
|
protected static $data = array();
|
||||||
@ -78,7 +87,7 @@ class Placebo extends SHM
|
|||||||
/**
|
/**
|
||||||
* Creates a new shared memory storage.
|
* Creates a new shared memory storage.
|
||||||
*
|
*
|
||||||
* Estabilishes a separate persistent storage.
|
* Establishes a separate persistent storage.
|
||||||
*
|
*
|
||||||
* @param string $persistentId The ID for the storage. The storage will be
|
* @param string $persistentId The ID for the storage. The storage will be
|
||||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||||
@ -147,11 +156,13 @@ class Placebo extends SHM
|
|||||||
if (!in_array($key, static::$locksBackup[$this->persistentId], true)) {
|
if (!in_array($key, static::$locksBackup[$this->persistentId], true)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
unset(
|
||||||
$key,
|
static::$locksBackup[$this->persistentId][array_search(
|
||||||
static::$locksBackup[$this->persistentId],
|
$key,
|
||||||
true
|
static::$locksBackup[$this->persistentId],
|
||||||
)]);
|
true
|
||||||
|
)]
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -42,7 +43,9 @@ use ArrayObject;
|
|||||||
class Wincache extends SHM
|
class Wincache extends SHM
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string ID of the current storage.
|
* ID of the current storage.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $persistentId;
|
protected $persistentId;
|
||||||
|
|
||||||
@ -52,19 +55,22 @@ class Wincache extends SHM
|
|||||||
* A list of persistent IDs within the current request (as keys) with an int
|
* A list of persistent IDs within the current request (as keys) with an int
|
||||||
* (as a value) specifying the number of instances in the current request.
|
* (as a value) specifying the number of instances in the current request.
|
||||||
* Used as an attempt to ensure implicit lock releases on destruction.
|
* Used as an attempt to ensure implicit lock releases on destruction.
|
||||||
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $requestInstances = array();
|
protected static $requestInstances = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Array of lock names obtained during the current request.
|
* Array of lock names obtained during the current request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $locksBackup = array();
|
protected static $locksBackup = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new shared memory storage.
|
* Creates a new shared memory storage.
|
||||||
*
|
*
|
||||||
* Estabilishes a separate persistent storage.
|
* Establishes a separate persistent storage.
|
||||||
*
|
*
|
||||||
* @param string $persistentId The ID for the storage. The storage will be
|
* @param string $persistentId The ID for the storage. The storage will be
|
||||||
* reused if it exists, or created if it doesn't exist. Data and locks
|
* reused if it exists, or created if it doesn't exist. Data and locks
|
||||||
@ -93,6 +99,7 @@ class Wincache extends SHM
|
|||||||
* @param string $name The lock name to encode.
|
* @param string $name The lock name to encode.
|
||||||
*
|
*
|
||||||
* @return string The encoded name.
|
* @return string The encoded name.
|
||||||
|
*
|
||||||
* @link http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx
|
* @link http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx
|
||||||
*/
|
*/
|
||||||
protected static function encodeLockName($name)
|
protected static function encodeLockName($name)
|
||||||
@ -135,7 +142,7 @@ class Wincache extends SHM
|
|||||||
* @param string $key Name of the key to obtain. Note that $key may
|
* @param string $key Name of the key to obtain. Note that $key may
|
||||||
* repeat for each distinct $persistentId.
|
* repeat for each distinct $persistentId.
|
||||||
* @param double $timeout Ignored with WinCache. Script will always block if
|
* @param double $timeout Ignored with WinCache. Script will always block if
|
||||||
* the lock can't be immediatly obtained.
|
* the lock can't be immediately obtained.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
*/
|
*/
|
||||||
@ -164,11 +171,13 @@ class Wincache extends SHM
|
|||||||
$this->persistentId . static::encodeLockName($key)
|
$this->persistentId . static::encodeLockName($key)
|
||||||
);
|
);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
unset(static::$locksBackup[$this->persistentId][array_search(
|
unset(
|
||||||
$key,
|
static::$locksBackup[$this->persistentId][array_search(
|
||||||
static::$locksBackup[$this->persistentId],
|
$key,
|
||||||
true
|
static::$locksBackup[$this->persistentId],
|
||||||
)]);
|
true
|
||||||
|
)]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ~~summary~~
|
* Wrapper for shared memory and locking functionality across different extensions.
|
||||||
|
|
||||||
*
|
*
|
||||||
* ~~description~~
|
* Allows you to share data across requests as long as the PHP process is running. One of APC or WinCache is required to accomplish this, with other extensions being potentially pluggable as adapters.
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 0.1.3
|
* @version 0.2.0
|
||||||
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
* @link http://pear2.php.net/PEAR2_Cache_SHM
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -59,6 +59,7 @@ class CommandLine
|
|||||||
* Error messages.
|
* Error messages.
|
||||||
*
|
*
|
||||||
* @var array $errors Error messages
|
* @var array $errors Error messages
|
||||||
|
*
|
||||||
* @todo move this to PEAR2\Console\CommandLine\MessageProvider
|
* @todo move this to PEAR2\Console\CommandLine\MessageProvider
|
||||||
*/
|
*/
|
||||||
public static $errors = array(
|
public static $errors = array(
|
||||||
@ -130,7 +131,7 @@ class CommandLine
|
|||||||
/**
|
/**
|
||||||
* The command line parser renderer instance.
|
* The command line parser renderer instance.
|
||||||
*
|
*
|
||||||
* @var object that implements PEAR2\Console\CommandLine\Renderer interface
|
* @var PEAR2\Console\CommandLine\Renderer a renderer
|
||||||
*/
|
*/
|
||||||
public $renderer = false;
|
public $renderer = false;
|
||||||
|
|
||||||
@ -144,7 +145,7 @@ class CommandLine
|
|||||||
/**
|
/**
|
||||||
* The command line message provider instance.
|
* The command line message provider instance.
|
||||||
*
|
*
|
||||||
* @var PEAR2\Console\CommandLine\MessageProvider A message provider instance
|
* @var PEAR2\Console\CommandLine\MessageProvider A message provider
|
||||||
*/
|
*/
|
||||||
public $message_provider = false;
|
public $message_provider = false;
|
||||||
|
|
||||||
@ -191,6 +192,7 @@ class CommandLine
|
|||||||
* convenience.
|
* convenience.
|
||||||
*
|
*
|
||||||
* @var PEAR2\Console\CommandLine The parent instance
|
* @var PEAR2\Console\CommandLine The parent instance
|
||||||
|
*
|
||||||
* @todo move CommandLine::parent to CommandLine\Command
|
* @todo move CommandLine::parent to CommandLine\Command
|
||||||
*/
|
*/
|
||||||
public $parent = false;
|
public $parent = false;
|
||||||
@ -272,7 +274,7 @@ class CommandLine
|
|||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
* @see PEAR2\Console\CommandLine\MessageProvider_Default
|
* @see PEAR2\Console\CommandLine\MessageProvider\DefaultProvider
|
||||||
*/
|
*/
|
||||||
public $messages = array();
|
public $messages = array();
|
||||||
|
|
||||||
@ -286,6 +288,9 @@ class CommandLine
|
|||||||
*/
|
*/
|
||||||
private $_dispatchLater = array();
|
private $_dispatchLater = array();
|
||||||
|
|
||||||
|
private $_lastopt = false;
|
||||||
|
private $_stopflag = false;
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// __construct() {{{
|
// __construct() {{{
|
||||||
|
|
||||||
@ -345,7 +350,7 @@ class CommandLine
|
|||||||
// set default instances
|
// set default instances
|
||||||
$this->renderer = new CommandLine\Renderer_Default($this);
|
$this->renderer = new CommandLine\Renderer_Default($this);
|
||||||
$this->outputter = new CommandLine\Outputter_Default();
|
$this->outputter = new CommandLine\Outputter_Default();
|
||||||
$this->message_provider = new CommandLine\MessageProvider_Default();
|
$this->message_provider = new CommandLine\MessageProvider\DefaultProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
@ -481,6 +486,7 @@ class CommandLine
|
|||||||
* @param array $params An array containing the argument attributes
|
* @param array $params An array containing the argument attributes
|
||||||
*
|
*
|
||||||
* @return PEAR2\Console\CommandLine\Argument the added argument
|
* @return PEAR2\Console\CommandLine\Argument the added argument
|
||||||
|
*
|
||||||
* @see PEAR2\Console\CommandLine\Argument
|
* @see PEAR2\Console\CommandLine\Argument
|
||||||
*/
|
*/
|
||||||
public function addArgument($name, $params = array())
|
public function addArgument($name, $params = array())
|
||||||
@ -842,6 +848,7 @@ class CommandLine
|
|||||||
* @param array $params An array of search=>replaces entries
|
* @param array $params An array of search=>replaces entries
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
*
|
||||||
* @todo remove Console::triggerError() and use exceptions only
|
* @todo remove Console::triggerError() and use exceptions only
|
||||||
*/
|
*/
|
||||||
public static function triggerError($msgId, $level, $params=array())
|
public static function triggerError($msgId, $level, $params=array())
|
||||||
@ -912,7 +919,7 @@ class CommandLine
|
|||||||
// Check if an invalid subcommand was specified. If there are
|
// Check if an invalid subcommand was specified. If there are
|
||||||
// subcommands and no arguments, but an argument was provided, it is
|
// subcommands and no arguments, but an argument was provided, it is
|
||||||
// an invalid subcommand.
|
// an invalid subcommand.
|
||||||
if ( count($this->commands) > 0
|
if (count($this->commands) > 0
|
||||||
&& count($this->args) === 0
|
&& count($this->args) === 0
|
||||||
&& count($args) > 0
|
&& count($args) > 0
|
||||||
) {
|
) {
|
||||||
@ -925,7 +932,7 @@ class CommandLine
|
|||||||
}
|
}
|
||||||
// if subcommand_required is set to true we must check that we have a
|
// if subcommand_required is set to true we must check that we have a
|
||||||
// subcommand.
|
// subcommand.
|
||||||
if ( count($this->commands)
|
if (count($this->commands)
|
||||||
&& $this->subcommand_required
|
&& $this->subcommand_required
|
||||||
&& !$result->command_name
|
&& !$result->command_name
|
||||||
) {
|
) {
|
||||||
@ -989,23 +996,21 @@ class CommandLine
|
|||||||
*/
|
*/
|
||||||
protected function parseToken($token, $result, &$args, $argc)
|
protected function parseToken($token, $result, &$args, $argc)
|
||||||
{
|
{
|
||||||
static $lastopt = false;
|
|
||||||
static $stopflag = false;
|
|
||||||
$last = $argc === 0;
|
$last = $argc === 0;
|
||||||
if (!$stopflag && $lastopt) {
|
if (!$this->_stopflag && $this->_lastopt) {
|
||||||
if (substr($token, 0, 1) == '-') {
|
if (substr($token, 0, 1) == '-') {
|
||||||
if ($lastopt->argument_optional) {
|
if ($this->_lastopt->argument_optional) {
|
||||||
$this->_dispatchAction($lastopt, '', $result);
|
$this->_dispatchAction($this->_lastopt, '', $result);
|
||||||
if ($lastopt->action != 'StoreArray') {
|
if ($this->_lastopt->action != 'StoreArray') {
|
||||||
$lastopt = false;
|
$this->_lastopt = false;
|
||||||
}
|
}
|
||||||
} else if (isset($result->options[$lastopt->name])) {
|
} else if (isset($result->options[$this->_lastopt->name])) {
|
||||||
// case of an option that expect a list of args
|
// case of an option that expect a list of args
|
||||||
$lastopt = false;
|
$this->_lastopt = false;
|
||||||
} else {
|
} else {
|
||||||
throw CommandLine\Exception::factory(
|
throw CommandLine\Exception::factory(
|
||||||
'OPTION_VALUE_REQUIRED',
|
'OPTION_VALUE_REQUIRED',
|
||||||
array('name' => $lastopt->name),
|
array('name' => $this->_lastopt->name),
|
||||||
$this,
|
$this,
|
||||||
$this->messages
|
$this->messages
|
||||||
);
|
);
|
||||||
@ -1015,8 +1020,8 @@ class CommandLine
|
|||||||
// is to consider that if there's already an element in the
|
// is to consider that if there's already an element in the
|
||||||
// array, and the commandline expects one or more args, we
|
// array, and the commandline expects one or more args, we
|
||||||
// leave last tokens to arguments
|
// leave last tokens to arguments
|
||||||
if ($lastopt->action == 'StoreArray'
|
if ($this->_lastopt->action == 'StoreArray'
|
||||||
&& !empty($result->options[$lastopt->name])
|
&& !empty($result->options[$this->_lastopt->name])
|
||||||
&& count($this->args) > ($argc + count($args))
|
&& count($this->args) > ($argc + count($args))
|
||||||
) {
|
) {
|
||||||
if (!is_null($token)) {
|
if (!is_null($token)) {
|
||||||
@ -1024,22 +1029,22 @@ class CommandLine
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!is_null($token) || $lastopt->action == 'Password') {
|
if (!is_null($token) || $this->_lastopt->action == 'Password') {
|
||||||
$this->_dispatchAction($lastopt, $token, $result);
|
$this->_dispatchAction($this->_lastopt, $token, $result);
|
||||||
}
|
}
|
||||||
if ($lastopt->action != 'StoreArray') {
|
if ($this->_lastopt->action != 'StoreArray') {
|
||||||
$lastopt = false;
|
$this->_lastopt = false;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$stopflag && substr($token, 0, 2) == '--') {
|
if (!$this->_stopflag && substr($token, 0, 2) == '--') {
|
||||||
// a long option
|
// a long option
|
||||||
$optkv = explode('=', $token, 2);
|
$optkv = explode('=', $token, 2);
|
||||||
if (trim($optkv[0]) == '--') {
|
if (trim($optkv[0]) == '--') {
|
||||||
// the special argument "--" forces in all cases the end of
|
// the special argument "--" forces in all cases the end of
|
||||||
// option scanning.
|
// option scanning.
|
||||||
$stopflag = true;
|
$this->_stopflag = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$opt = $this->findOption($optkv[0]);
|
$opt = $this->findOption($optkv[0]);
|
||||||
@ -1072,14 +1077,14 @@ class CommandLine
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
// we will have a value next time
|
// we will have a value next time
|
||||||
$lastopt = $opt;
|
$this->_lastopt = $opt;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($opt->action == 'StoreArray') {
|
if ($opt->action == 'StoreArray') {
|
||||||
$lastopt = $opt;
|
$this->_lastopt = $opt;
|
||||||
}
|
}
|
||||||
$this->_dispatchAction($opt, $value, $result);
|
$this->_dispatchAction($opt, $value, $result);
|
||||||
} else if (!$stopflag && substr($token, 0, 1) == '-') {
|
} else if (!$this->_stopflag && substr($token, 0, 1) == '-') {
|
||||||
// a short option
|
// a short option
|
||||||
$optname = substr($token, 0, 2);
|
$optname = substr($token, 0, 2);
|
||||||
if ($optname == '-') {
|
if ($optname == '-') {
|
||||||
@ -1100,7 +1105,7 @@ class CommandLine
|
|||||||
// in short: handle -f<value> and -f <value>
|
// in short: handle -f<value> and -f <value>
|
||||||
$next = substr($token, 2, 1);
|
$next = substr($token, 2, 1);
|
||||||
// check if we must wait for a value
|
// check if we must wait for a value
|
||||||
if ($next === false) {
|
if (!$next) {
|
||||||
if ($opt->expectsArgument()) {
|
if ($opt->expectsArgument()) {
|
||||||
if ($last && !$opt->argument_optional) {
|
if ($last && !$opt->argument_optional) {
|
||||||
throw CommandLine\Exception::factory(
|
throw CommandLine\Exception::factory(
|
||||||
@ -1111,7 +1116,7 @@ class CommandLine
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
// we will have a value next time
|
// we will have a value next time
|
||||||
$lastopt = $opt;
|
$this->_lastopt = $opt;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$value = false;
|
$value = false;
|
||||||
@ -1136,7 +1141,7 @@ class CommandLine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($opt->action == 'StoreArray') {
|
if ($opt->action == 'StoreArray') {
|
||||||
$lastopt = $opt;
|
$this->_lastopt = $opt;
|
||||||
}
|
}
|
||||||
$value = substr($token, 2);
|
$value = substr($token, 2);
|
||||||
}
|
}
|
||||||
@ -1145,8 +1150,8 @@ class CommandLine
|
|||||||
// We have an argument.
|
// We have an argument.
|
||||||
// if we are in POSIX compliant mode, we must set the stop flag to
|
// if we are in POSIX compliant mode, we must set the stop flag to
|
||||||
// true in order to stop option parsing.
|
// true in order to stop option parsing.
|
||||||
if (!$stopflag && $this->force_posix) {
|
if (!$this->_stopflag && $this->force_posix) {
|
||||||
$stopflag = true;
|
$this->_stopflag = true;
|
||||||
}
|
}
|
||||||
if (!is_null($token)) {
|
if (!is_null($token)) {
|
||||||
$args[] = $token;
|
$args[] = $token;
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
namespace PEAR2\Console\CommandLine;
|
namespace PEAR2\Console\CommandLine;
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
namespace PEAR2\Console\CommandLine\Action;
|
namespace PEAR2\Console\CommandLine\Action;
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
* @version CVS: $Id: List.php,v 1.2 2009/02/27 08:03:17 izi Exp $
|
* @version CVS: $Id: List.php,v 1.2 2009/02/27 08:03:17 izi Exp $
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -62,6 +63,7 @@ class Password extends CommandLine\Action
|
|||||||
* Prompts the password to the user without echoing it.
|
* Prompts the password to the user without echoing it.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
|
*
|
||||||
* @todo not echo-ing the password does not work on windows is there a way
|
* @todo not echo-ing the password does not work on windows is there a way
|
||||||
* to make this work ?
|
* to make this work ?
|
||||||
*/
|
*/
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -65,6 +66,7 @@ class Argument extends Element
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
* @throws PEAR2\Console\CommandLine\Exception
|
* @throws PEAR2\Console\CommandLine\Exception
|
||||||
|
*
|
||||||
* @todo use exceptions
|
* @todo use exceptions
|
||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
* @version CVS: $Id: CustomMessageProvider.php 282427 2009-06-19 10:22:48Z izi $
|
* @version CVS: $Id: CustomMessageProvider.php 282427 2009-06-19 10:22:48Z izi $
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 1.1.0
|
* @since File available since release 1.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -56,8 +57,9 @@ interface CustomMessageProvider
|
|||||||
* indexes are message codes.
|
* indexes are message codes.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
|
*
|
||||||
* @see PEAR2\Console\CommandLine_MessageProvider
|
* @see PEAR2\Console\CommandLine_MessageProvider
|
||||||
* @see PEAR2\Console\CommandLine_MessageProvider_Default
|
* @see PEAR2\Console\CommandLine_MessageProvider\DefaultProvider
|
||||||
*/
|
*/
|
||||||
public function getWithCustomMessages(
|
public function getWithCustomMessages(
|
||||||
$code, $vars = array(), $messages = array()
|
$code, $vars = array(), $messages = array()
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -92,7 +93,7 @@ abstract class Element
|
|||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
* @see PEAR2\Console\CommandLine_MessageProvider_Default
|
* @see PEAR2\Console\CommandLine_MessageProvider\DefaultProvider
|
||||||
*/
|
*/
|
||||||
public $messages = array();
|
public $messages = array();
|
||||||
|
|
||||||
@ -124,6 +125,7 @@ abstract class Element
|
|||||||
* Returns the string representation of the element.
|
* Returns the string representation of the element.
|
||||||
*
|
*
|
||||||
* @return string The string representation of the element
|
* @return string The string representation of the element
|
||||||
|
*
|
||||||
* @todo use __toString() instead
|
* @todo use __toString() instead
|
||||||
*/
|
*/
|
||||||
public function toString()
|
public function toString()
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -49,7 +50,8 @@ interface MessageProvider
|
|||||||
* @param array $vars An array of template variables
|
* @param array $vars An array of template variables
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
* @see PEAR2\Console\CommandLine_MessageProvider_Default
|
*
|
||||||
|
* @see PEAR2\Console\CommandLine\MessageProvider\DefaultProvider
|
||||||
*/
|
*/
|
||||||
public function get($code, $vars=array());
|
public function get($code, $vars=array());
|
||||||
|
|
||||||
|
@ -16,13 +16,17 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace PEAR2\Console\CommandLine;
|
namespace PEAR2\Console\CommandLine\MessageProvider;
|
||||||
|
|
||||||
|
use PEAR2\Console\CommandLine\MessageProvider;
|
||||||
|
use PEAR2\Console\CommandLine\CustomMessageProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lightweight class that manages messages used by PEAR2\Console\CommandLine package,
|
* Lightweight class that manages messages used by PEAR2\Console\CommandLine package,
|
||||||
@ -37,7 +41,7 @@ namespace PEAR2\Console\CommandLine;
|
|||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since Class available since release 0.1.0
|
* @since Class available since release 0.1.0
|
||||||
*/
|
*/
|
||||||
class MessageProvider_Default
|
class DefaultProvider
|
||||||
implements MessageProvider,
|
implements MessageProvider,
|
||||||
CustomMessageProvider
|
CustomMessageProvider
|
||||||
{
|
{
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -173,6 +174,7 @@ class Option extends Element
|
|||||||
* @param string $delim Delimiter to use between short and long option
|
* @param string $delim Delimiter to use between short and long option
|
||||||
*
|
*
|
||||||
* @return string The string representation of the option
|
* @return string The string representation of the option
|
||||||
|
*
|
||||||
* @todo use __toString() instead
|
* @todo use __toString() instead
|
||||||
*/
|
*/
|
||||||
public function toString($delim = ", ")
|
public function toString($delim = ", ")
|
||||||
@ -269,6 +271,7 @@ class Option extends Element
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
* @throws PEAR2\Console\CommandLine\Exception
|
* @throws PEAR2\Console\CommandLine\Exception
|
||||||
|
*
|
||||||
* @todo use exceptions instead
|
* @todo use exceptions instead
|
||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
*/
|
*/
|
||||||
|
@ -16,9 +16,10 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -16,15 +16,19 @@
|
|||||||
* @author David JEAN LOUIS <izimobil@gmail.com>
|
* @author David JEAN LOUIS <izimobil@gmail.com>
|
||||||
* @copyright 2007-2009 David JEAN LOUIS
|
* @copyright 2007-2009 David JEAN LOUIS
|
||||||
* @license http://opensource.org/licenses/mit-license.php MIT License
|
* @license http://opensource.org/licenses/mit-license.php MIT License
|
||||||
* @version 0.2.1
|
* @version 0.2.3
|
||||||
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
* @link http://pear2.php.net/PEAR2_Console_CommandLine
|
||||||
* @since File available since release 0.1.0
|
* @since File available since release 0.1.0
|
||||||
|
*
|
||||||
* @filesource
|
* @filesource
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace PEAR2\Console\CommandLine;
|
namespace PEAR2\Console\CommandLine;
|
||||||
|
|
||||||
use PEAR2\Console\CommandLine;
|
use PEAR2\Console\CommandLine;
|
||||||
|
use DOMDocument;
|
||||||
|
use DOMNode;
|
||||||
|
use Phar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser for command line xml definitions.
|
* Parser for command line xml definitions.
|
||||||
@ -58,7 +62,7 @@ class XmlParser
|
|||||||
array('{$file}' => $xmlfile)
|
array('{$file}' => $xmlfile)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$doc = new \DomDocument();
|
$doc = new DOMDocument();
|
||||||
$doc->load($xmlfile);
|
$doc->load($xmlfile);
|
||||||
self::validate($doc);
|
self::validate($doc);
|
||||||
$nodes = $doc->getElementsByTagName('command');
|
$nodes = $doc->getElementsByTagName('command');
|
||||||
@ -79,7 +83,7 @@ class XmlParser
|
|||||||
*/
|
*/
|
||||||
public static function parseString($xmlstr)
|
public static function parseString($xmlstr)
|
||||||
{
|
{
|
||||||
$doc = new \DomDocument();
|
$doc = new DOMDocument();
|
||||||
$doc->loadXml($xmlstr);
|
$doc->loadXml($xmlstr);
|
||||||
self::validate($doc);
|
self::validate($doc);
|
||||||
$nodes = $doc->getElementsByTagName('command');
|
$nodes = $doc->getElementsByTagName('command');
|
||||||
@ -93,27 +97,48 @@ class XmlParser
|
|||||||
/**
|
/**
|
||||||
* Validates the xml definition using Relax NG.
|
* Validates the xml definition using Relax NG.
|
||||||
*
|
*
|
||||||
* @param DomDocument $doc The document to validate
|
* @param DOMDocument $doc The document to validate
|
||||||
*
|
*
|
||||||
* @return boolean Whether the xml data is valid or not.
|
* @return boolean Whether the xml data is valid or not.
|
||||||
* @throws PEAR2\Console\CommandLine\Exception
|
* @throws PEAR2\Console\CommandLine\Exception
|
||||||
|
*
|
||||||
* @todo use exceptions only
|
* @todo use exceptions only
|
||||||
*/
|
*/
|
||||||
public static function validate($doc)
|
public static function validate(DOMDocument $doc)
|
||||||
{
|
{
|
||||||
$rngfile = __DIR__
|
$paths = array();
|
||||||
. '/../../../../data/pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng';
|
if (!class_exists('Phar', false) || !Phar::running()) {
|
||||||
if (!is_file($rngfile)) {
|
// Pyrus
|
||||||
$rngfile = __DIR__ . '/../../../../data/xmlschema.rng';
|
$paths[]
|
||||||
|
= 'D:\Vasko\WEB\PHP\_shared\PEAR2\data/pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng';
|
||||||
|
// PEAR
|
||||||
|
$pearDataDirEnv = getenv('PHP_PEAR_DATA_DIR');
|
||||||
|
if ($pearDataDirEnv) {
|
||||||
|
$paths[] = $pearDataDirEnv .
|
||||||
|
'/PEAR2_Console_CommandLine/xmlschema.rng';
|
||||||
|
}
|
||||||
|
$paths[] = 'D:\Vasko\WEB\PHP\_shared\PEAR2\data/PEAR2_Console_CommandLine/xmlschema.rng';
|
||||||
}
|
}
|
||||||
if (!is_readable($rngfile)) {
|
$pkgData = __DIR__ . '/../../../../data/';
|
||||||
CommandLine::triggerError(
|
// PHAR dep
|
||||||
'invalid_xml_file',
|
$paths[] = $pkgData .
|
||||||
E_USER_ERROR,
|
'pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng';
|
||||||
array('{$file}' => $rngfile)
|
$paths[] = $pkgData . 'PEAR2_Console_CommandLine/xmlschema.rng';
|
||||||
);
|
$paths[] = $pkgData . 'pear2/console_commandline/xmlschema.rng';
|
||||||
|
// Git/Composer
|
||||||
|
$paths[] = $pkgData . 'xmlschema.rng';
|
||||||
|
$paths[] = 'xmlschema.rng';
|
||||||
|
|
||||||
|
foreach ($paths as $path) {
|
||||||
|
if (is_readable($path)) {
|
||||||
|
return $doc->relaxNGValidate($path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $doc->relaxNGValidate($rngfile);
|
CommandLine::triggerError(
|
||||||
|
'invalid_xml_file',
|
||||||
|
E_USER_ERROR,
|
||||||
|
array('{$file}' => $path)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
@ -124,12 +149,13 @@ class XmlParser
|
|||||||
* constructed PEAR2\Console\CommandLine or PEAR2\Console\CommandLine_Command
|
* constructed PEAR2\Console\CommandLine or PEAR2\Console\CommandLine_Command
|
||||||
* instance.
|
* instance.
|
||||||
*
|
*
|
||||||
* @param DomDocumentNode $node The node to parse
|
* @param DOMNode $node The node to parse
|
||||||
* @param bool $isRootNode Whether it is a root node or not
|
* @param bool $isRootNode Whether it is a root node or not
|
||||||
*
|
*
|
||||||
* @return mixed PEAR2\Console\CommandLine or PEAR2\Console\CommandLine_Command
|
* @return CommandLine|CommandLine\Command An instance of CommandLine for
|
||||||
|
* root node, CommandLine\Command otherwise.
|
||||||
*/
|
*/
|
||||||
private static function _parseCommandNode($node, $isRootNode = false)
|
private static function _parseCommandNode(DOMNode $node, $isRootNode = false)
|
||||||
{
|
{
|
||||||
if ($isRootNode) {
|
if ($isRootNode) {
|
||||||
$obj = new CommandLine();
|
$obj = new CommandLine();
|
||||||
@ -184,11 +210,11 @@ class XmlParser
|
|||||||
* Parses an option node and returns the constructed
|
* Parses an option node and returns the constructed
|
||||||
* PEAR2\Console\CommandLine_Option instance.
|
* PEAR2\Console\CommandLine_Option instance.
|
||||||
*
|
*
|
||||||
* @param DomDocumentNode $node The node to parse
|
* @param DOMNode $node The node to parse
|
||||||
*
|
*
|
||||||
* @return PEAR2\Console\CommandLine\Option The built option
|
* @return PEAR2\Console\CommandLine\Option The built option
|
||||||
*/
|
*/
|
||||||
private static function _parseOptionNode($node)
|
private static function _parseOptionNode(DOMNode $node)
|
||||||
{
|
{
|
||||||
$obj = new CommandLine\Option($node->getAttribute('name'));
|
$obj = new CommandLine\Option($node->getAttribute('name'));
|
||||||
foreach ($node->childNodes as $cNode) {
|
foreach ($node->childNodes as $cNode) {
|
||||||
@ -224,11 +250,11 @@ class XmlParser
|
|||||||
* Parses an argument node and returns the constructed
|
* Parses an argument node and returns the constructed
|
||||||
* PEAR2\Console\CommandLine_Argument instance.
|
* PEAR2\Console\CommandLine_Argument instance.
|
||||||
*
|
*
|
||||||
* @param DomDocumentNode $node The node to parse
|
* @param DOMNode $node The node to parse
|
||||||
*
|
*
|
||||||
* @return PEAR2\Console\CommandLine\Argument The built argument
|
* @return PEAR2\Console\CommandLine\Argument The built argument
|
||||||
*/
|
*/
|
||||||
private static function _parseArgumentNode($node)
|
private static function _parseArgumentNode(DOMNode $node)
|
||||||
{
|
{
|
||||||
$obj = new CommandLine\Argument($node->getAttribute('name'));
|
$obj = new CommandLine\Argument($node->getAttribute('name'));
|
||||||
foreach ($node->childNodes as $cNode) {
|
foreach ($node->childNodes as $cNode) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -64,36 +65,52 @@ class Client
|
|||||||
const FILTER_ALL = 3;
|
const FILTER_ALL = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Communicator The communicator for this client.
|
* The communicator for this client.
|
||||||
|
*
|
||||||
|
* @var Communicator
|
||||||
*/
|
*/
|
||||||
protected $com;
|
protected $com;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int The number of currently pending requests.
|
* The number of currently pending requests.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $pendingRequestsCount = 0;
|
protected $pendingRequestsCount = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array of responses that have not yet been extracted or
|
* An array of responses that have not yet been extracted
|
||||||
* passed to a callback. Key is the tag of the request, and the value
|
* or passed to a callback.
|
||||||
* is an array of associated responses.
|
*
|
||||||
|
* Key is the tag of the request, and the value is an array of
|
||||||
|
* associated responses.
|
||||||
|
*
|
||||||
|
* @var array<string,Response[]>
|
||||||
*/
|
*/
|
||||||
protected $responseBuffer = array();
|
protected $responseBuffer = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array of callbacks to be executed as responses come.
|
* An array of callbacks to be executed as responses come.
|
||||||
* Key is the tag of the request, and the value is the callback for it.
|
*
|
||||||
|
* Key is the tag of the request, and the value is the callback for it.
|
||||||
|
*
|
||||||
|
* @var array<string,callback>
|
||||||
*/
|
*/
|
||||||
protected $callbacks = array();
|
protected $callbacks = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Registry A registry for the operations. Particularly helpful at
|
* A registry for the operations.
|
||||||
* persistent connections.
|
*
|
||||||
|
* Particularly helpful at persistent connections.
|
||||||
|
*
|
||||||
|
* @var Registry
|
||||||
*/
|
*/
|
||||||
protected $registry = null;
|
protected $registry = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool Whether to stream future responses.
|
* Whether to stream future responses.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $_streamingResponses = false;
|
private $_streamingResponses = false;
|
||||||
|
|
||||||
@ -103,24 +120,25 @@ class Client
|
|||||||
* Creates a new instance of a RouterOS API client with the specified
|
* Creates a new instance of a RouterOS API client with the specified
|
||||||
* settings.
|
* settings.
|
||||||
*
|
*
|
||||||
* @param string $host Hostname (IP or domain) of the RouterOS server.
|
* @param string $host Hostname (IP or domain) of RouterOS.
|
||||||
* @param string $username The RouterOS username.
|
* @param string $username The RouterOS username.
|
||||||
* @param string $password The RouterOS password.
|
* @param string $password The RouterOS password.
|
||||||
* @param int|null $port The port on which the RouterOS server provides
|
* @param int|null $port The port on which the RouterOS host
|
||||||
* the API service. You can also specify NULL, in which case the port
|
* provides the API service. You can also specify NULL, in which case
|
||||||
* will automatically be chosen between 8728 and 8729, depending on the
|
* the port will automatically be chosen between 8728 and 8729,
|
||||||
* value of $crypto.
|
* depending on the value of $crypto.
|
||||||
* @param bool $persist Whether or not the connection should be a
|
* @param bool $persist Whether or not the connection should be a
|
||||||
* persistent one.
|
* persistent one.
|
||||||
* @param float $timeout The timeout for the connection.
|
* @param double|null $timeout The timeout for the connection.
|
||||||
* @param string $crypto The encryption for this connection. Must be one
|
* @param string $crypto The encryption for this connection.
|
||||||
* of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off
|
* Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_*
|
||||||
* by default. RouterOS currently supports only TLS, but the setting is
|
* constants. Off by default. RouterOS currently supports only TLS, but
|
||||||
* provided in this fashion for forward compatibility's sake. And for
|
* the setting is provided in this fashion for forward compatibility's
|
||||||
* the sake of simplicity, if you specify an encryption, don't specify a
|
* sake. And for the sake of simplicity, if you specify an encryption,
|
||||||
* context and your default context uses the value "DEFAULT" for
|
* don't specify a context and your default context uses the value
|
||||||
* ciphers, "ADH" will be automatically added to the list of ciphers.
|
* "DEFAULT" for ciphers, "ADH" will be automatically added to the list
|
||||||
* @param resource $context A context for the socket.
|
* of ciphers.
|
||||||
|
* @param resource|null $context A context for the socket.
|
||||||
*
|
*
|
||||||
* @see sendSync()
|
* @see sendSync()
|
||||||
* @see sendAsync()
|
* @see sendAsync()
|
||||||
@ -131,15 +149,10 @@ class Client
|
|||||||
$password = '',
|
$password = '',
|
||||||
$port = 8728,
|
$port = 8728,
|
||||||
$persist = false,
|
$persist = false,
|
||||||
$timeout = 10,
|
$timeout = null,
|
||||||
$crypto = N::CRYPTO_OFF,
|
$crypto = N::CRYPTO_OFF,
|
||||||
$context = null
|
$context = null
|
||||||
) {
|
) {
|
||||||
if(strpos($host,":")>-1){
|
|
||||||
$part = explode(":",$host);
|
|
||||||
$host = $part[0];
|
|
||||||
$port = $part[1];
|
|
||||||
}
|
|
||||||
$this->com = new Communicator(
|
$this->com = new Communicator(
|
||||||
$host,
|
$host,
|
||||||
$port,
|
$port,
|
||||||
@ -183,7 +196,7 @@ class Client
|
|||||||
* the class is invoked and its returned value is returned by this function.
|
* the class is invoked and its returned value is returned by this function.
|
||||||
*
|
*
|
||||||
* @param mixed $arg Value can be either a {@link Request} to send, which
|
* @param mixed $arg Value can be either a {@link Request} to send, which
|
||||||
* would be sent asynchoniously if it has a tag, and synchroniously if
|
* would be sent asynchronously if it has a tag, and synchronously if
|
||||||
* not, a number to loop with or NULL to complete all pending requests.
|
* not, a number to loop with or NULL to complete all pending requests.
|
||||||
* Any other value is converted to string and treated as the tag of a
|
* Any other value is converted to string and treated as the tag of a
|
||||||
* request to complete.
|
* request to complete.
|
||||||
@ -210,7 +223,7 @@ class Client
|
|||||||
* @param string $username The RouterOS username.
|
* @param string $username The RouterOS username.
|
||||||
* @param string $password The RouterOS password.
|
* @param string $password The RouterOS password.
|
||||||
* @param int|null $timeout The time to wait for each response. NULL
|
* @param int|null $timeout The time to wait for each response. NULL
|
||||||
* waits indefinetly.
|
* waits indefinitely.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
*/
|
*/
|
||||||
@ -263,7 +276,7 @@ class Client
|
|||||||
* @param string $password The RouterOS password. Potentially parsed
|
* @param string $password The RouterOS password. Potentially parsed
|
||||||
* already by iconv.
|
* already by iconv.
|
||||||
* @param int|null $timeout The time to wait for each response. NULL
|
* @param int|null $timeout The time to wait for each response. NULL
|
||||||
* waits indefinetly.
|
* waits indefinitely.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
*/
|
*/
|
||||||
@ -274,15 +287,31 @@ class Client
|
|||||||
$timeout = null
|
$timeout = null
|
||||||
) {
|
) {
|
||||||
$request = new Request('/login');
|
$request = new Request('/login');
|
||||||
// Update Mikrotik Versi terbaru
|
|
||||||
// sayangnya ini ngga aman, bagusnya di setup ke port SSL
|
|
||||||
$request->setArgument('name', $username);
|
|
||||||
$request->setArgument('password', $password);
|
|
||||||
$request->send($com);
|
$request->send($com);
|
||||||
$response = new Response($com, false, $timeout);
|
$response = new Response($com, false, $timeout);
|
||||||
return $response->getType() === Response::TYPE_FINAL;
|
$request->setArgument('name', $username);
|
||||||
null === $response->getProperty('ret');
|
$request->setArgument('password', $password);
|
||||||
}
|
// $request->setArgument(
|
||||||
|
// 'response',
|
||||||
|
// '00' . md5(
|
||||||
|
// chr(0) . $password
|
||||||
|
// . pack('H*', $response->getProperty('ret'))
|
||||||
|
// )
|
||||||
|
// );
|
||||||
|
$request->verify($com)->send($com);
|
||||||
|
|
||||||
|
$response = new Response($com, false, $timeout);
|
||||||
|
if ($response->getType() === Response::TYPE_FINAL) {
|
||||||
|
return null === $response->getProperty('ret');
|
||||||
|
} else {
|
||||||
|
while ($response->getType() !== Response::TYPE_FINAL
|
||||||
|
&& $response->getType() !== Response::TYPE_FATAL
|
||||||
|
) {
|
||||||
|
$response = new Response($com, false, $timeout);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the charset(s) for this connection.
|
* Sets the charset(s) for this connection.
|
||||||
@ -307,6 +336,7 @@ class Client
|
|||||||
* @return string|array The old charset. If $charsetType is
|
* @return string|array The old charset. If $charsetType is
|
||||||
* {@link Communicator::CHARSET_ALL}, the old values will be returned as
|
* {@link Communicator::CHARSET_ALL}, the old values will be returned as
|
||||||
* an array with the types as keys, and charsets as values.
|
* an array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see Communicator::setDefaultCharset()
|
* @see Communicator::setDefaultCharset()
|
||||||
*/
|
*/
|
||||||
public function setCharset(
|
public function setCharset(
|
||||||
@ -326,6 +356,7 @@ class Client
|
|||||||
* @return string|array The current charset. If $charsetType is
|
* @return string|array The current charset. If $charsetType is
|
||||||
* {@link Communicator::CHARSET_ALL}, the current values will be
|
* {@link Communicator::CHARSET_ALL}, the current values will be
|
||||||
* returned as an array with the types as keys, and charsets as values.
|
* returned as an array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see setCharset()
|
* @see setCharset()
|
||||||
*/
|
*/
|
||||||
public function getCharset($charsetType)
|
public function getCharset($charsetType)
|
||||||
@ -336,16 +367,18 @@ class Client
|
|||||||
/**
|
/**
|
||||||
* Sends a request and waits for responses.
|
* Sends a request and waits for responses.
|
||||||
*
|
*
|
||||||
* @param Request $request The request to send.
|
* @param Request $request The request to send.
|
||||||
* @param callback $callback Optional. A function that is to be executed
|
* @param callback|null $callback Optional. A function that is to be
|
||||||
* when new responses for this request are available. The callback takes
|
* executed when new responses for this request are available.
|
||||||
* two parameters. The {@link Response} object as the first, and the
|
* The callback takes two parameters. The {@link Response} object as
|
||||||
* {@link Client} object as the second one. If the function returns
|
* the first, and the {@link Client} object as the second one. If the
|
||||||
* TRUE, the request is canceled. Note that the callback may be executed
|
* callback returns TRUE, the request is canceled. Note that the
|
||||||
* one last time after that with a response that notifies about the
|
* callback may be executed at least two times after that. Once with a
|
||||||
* canceling.
|
* {@link Response::TYPE_ERROR} response that notifies about the
|
||||||
|
* canceling, plus the {@link Response::TYPE_FINAL} response.
|
||||||
*
|
*
|
||||||
* @return $this The client object.
|
* @return $this The client object.
|
||||||
|
*
|
||||||
* @see completeRequest()
|
* @see completeRequest()
|
||||||
* @see loop()
|
* @see loop()
|
||||||
* @see cancelRequest()
|
* @see cancelRequest()
|
||||||
@ -392,10 +425,11 @@ class Client
|
|||||||
* pending request and/or has responses that are not yet extracted.
|
* pending request and/or has responses that are not yet extracted.
|
||||||
*
|
*
|
||||||
* @param string $tag The tag of the request to look for.
|
* @param string $tag The tag of the request to look for.
|
||||||
* @param int $filter One of the FILTER_* consntants. Limits the search
|
* @param int $filter One of the FILTER_* constants. Limits the search
|
||||||
* to the specified places.
|
* to the specified places.
|
||||||
*
|
*
|
||||||
* @return bool TRUE if the request is active, FALSE otherwise.
|
* @return bool TRUE if the request is active, FALSE otherwise.
|
||||||
|
*
|
||||||
* @see getPendingRequestsCount()
|
* @see getPendingRequestsCount()
|
||||||
* @see completeRequest()
|
* @see completeRequest()
|
||||||
*/
|
*/
|
||||||
@ -417,6 +451,7 @@ class Client
|
|||||||
* @param Request $request The request to send.
|
* @param Request $request The request to send.
|
||||||
*
|
*
|
||||||
* @return ResponseCollection The received responses as a collection.
|
* @return ResponseCollection The received responses as a collection.
|
||||||
|
*
|
||||||
* @see sendAsync()
|
* @see sendAsync()
|
||||||
* @see close()
|
* @see close()
|
||||||
*/
|
*/
|
||||||
@ -437,8 +472,8 @@ class Client
|
|||||||
* Starts an event loop for the RouterOS callbacks and finishes when a
|
* Starts an event loop for the RouterOS callbacks and finishes when a
|
||||||
* specified request is completed.
|
* specified request is completed.
|
||||||
*
|
*
|
||||||
* @param string $tag The tag of the request to complete. Setting NULL
|
* @param string|null $tag The tag of the request to complete.
|
||||||
* completes all requests.
|
* Setting NULL completes all requests.
|
||||||
*
|
*
|
||||||
* @return ResponseCollection A collection of {@link Response} objects that
|
* @return ResponseCollection A collection of {@link Response} objects that
|
||||||
* haven't been passed to a callback function or previously extracted
|
* haven't been passed to a callback function or previously extracted
|
||||||
@ -481,11 +516,13 @@ class Client
|
|||||||
* Gets all new responses for a request that haven't been passed to a
|
* Gets all new responses for a request that haven't been passed to a
|
||||||
* callback and clears the buffer from them.
|
* callback and clears the buffer from them.
|
||||||
*
|
*
|
||||||
* @param string $tag The tag of the request to extract new responses for.
|
* @param string|null $tag The tag of the request to extract
|
||||||
|
* new responses for.
|
||||||
* Specifying NULL with extract new responses for all requests.
|
* Specifying NULL with extract new responses for all requests.
|
||||||
*
|
*
|
||||||
* @return ResponseCollection A collection of {@link Response} objects for
|
* @return ResponseCollection A collection of {@link Response} objects for
|
||||||
* the specified request.
|
* the specified request.
|
||||||
|
*
|
||||||
* @see loop()
|
* @see loop()
|
||||||
*/
|
*/
|
||||||
public function extractNewResponses($tag = null)
|
public function extractNewResponses($tag = null)
|
||||||
@ -526,12 +563,13 @@ class Client
|
|||||||
* are no more pending requests or when a specified timeout has passed
|
* are no more pending requests or when a specified timeout has passed
|
||||||
* (whichever comes first).
|
* (whichever comes first).
|
||||||
*
|
*
|
||||||
* @param int $sTimeout Timeout for the loop. If NULL, there is no time
|
* @param int|null $sTimeout Timeout for the loop.
|
||||||
* limit.
|
* If NULL, there is no time limit.
|
||||||
* @param int $usTimeout Microseconds to add to the time limit.
|
* @param int $usTimeout Microseconds to add to the time limit.
|
||||||
*
|
*
|
||||||
* @return bool TRUE when there are any more pending requests, FALSE
|
* @return bool TRUE when there are any more pending requests, FALSE
|
||||||
* otherwise.
|
* otherwise.
|
||||||
|
*
|
||||||
* @see extractNewResponses()
|
* @see extractNewResponses()
|
||||||
* @see getPendingRequestsCount()
|
* @see getPendingRequestsCount()
|
||||||
*/
|
*/
|
||||||
@ -581,6 +619,7 @@ class Client
|
|||||||
* Gets the number of pending requests.
|
* Gets the number of pending requests.
|
||||||
*
|
*
|
||||||
* @return int The number of pending requests.
|
* @return int The number of pending requests.
|
||||||
|
*
|
||||||
* @see isRequestActive()
|
* @see isRequestActive()
|
||||||
*/
|
*/
|
||||||
public function getPendingRequestsCount()
|
public function getPendingRequestsCount()
|
||||||
@ -592,15 +631,16 @@ class Client
|
|||||||
* Cancels a request.
|
* Cancels a request.
|
||||||
*
|
*
|
||||||
* Cancels an active request. Using this function in favor of a plain call
|
* Cancels an active request. Using this function in favor of a plain call
|
||||||
* to the "/cancel" command is highly reccomended, as it also updates the
|
* to the "/cancel" command is highly recommended, as it also updates the
|
||||||
* counter of pending requests properly. Note that canceling a request also
|
* counter of pending requests properly. Note that canceling a request also
|
||||||
* removes any responses for it that were not previously extracted with
|
* removes any responses for it that were not previously extracted with
|
||||||
* {@link static::extractNewResponses()}.
|
* {@link static::extractNewResponses()}.
|
||||||
*
|
*
|
||||||
* @param string $tag Tag of the request to cancel. Setting NULL will cancel
|
* @param string|null $tag Tag of the request to cancel.
|
||||||
* all requests.
|
* Setting NULL will cancel all requests.
|
||||||
*
|
*
|
||||||
* @return $this The client object.
|
* @return $this The client object.
|
||||||
|
*
|
||||||
* @see sendAsync()
|
* @see sendAsync()
|
||||||
* @see close()
|
* @see close()
|
||||||
*/
|
*/
|
||||||
@ -671,6 +711,7 @@ class Client
|
|||||||
* @param bool $streamingResponses Whether to stream future responses.
|
* @param bool $streamingResponses Whether to stream future responses.
|
||||||
*
|
*
|
||||||
* @return bool The previous value of the setting.
|
* @return bool The previous value of the setting.
|
||||||
|
*
|
||||||
* @see isStreamingResponses()
|
* @see isStreamingResponses()
|
||||||
*/
|
*/
|
||||||
public function setStreamingResponses($streamingResponses)
|
public function setStreamingResponses($streamingResponses)
|
||||||
@ -686,6 +727,7 @@ class Client
|
|||||||
* Gets whether future responses are streamed.
|
* Gets whether future responses are streamed.
|
||||||
*
|
*
|
||||||
* @return bool The value of the setting.
|
* @return bool The value of the setting.
|
||||||
|
*
|
||||||
* @see setStreamingResponses()
|
* @see setStreamingResponses()
|
||||||
*/
|
*/
|
||||||
public function isStreamingResponses()
|
public function isStreamingResponses()
|
||||||
@ -758,12 +800,13 @@ class Client
|
|||||||
* @param Request $request The request to send.
|
* @param Request $request The request to send.
|
||||||
*
|
*
|
||||||
* @return $this The client object.
|
* @return $this The client object.
|
||||||
|
*
|
||||||
* @see sendSync()
|
* @see sendSync()
|
||||||
* @see sendAsync()
|
* @see sendAsync()
|
||||||
*/
|
*/
|
||||||
protected function send(Request $request)
|
protected function send(Request $request)
|
||||||
{
|
{
|
||||||
$request->send($this->com, $this->registry);
|
$request->verify($this->com)->send($this->com, $this->registry);
|
||||||
$this->pendingRequestsCount++;
|
$this->pendingRequestsCount++;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -774,9 +817,10 @@ class Client
|
|||||||
* Dispatches the next response in queue, i.e. it executes the associated
|
* Dispatches the next response in queue, i.e. it executes the associated
|
||||||
* callback if there is one, or places the response in the response buffer.
|
* callback if there is one, or places the response in the response buffer.
|
||||||
*
|
*
|
||||||
* @param int $sTimeout If a response is not immediatly available, wait
|
* @param int|null $sTimeout If a response is not immediately available,
|
||||||
* this many seconds. If NULL, wait indefinetly.
|
* wait this many seconds.
|
||||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
* If NULL, wait indefinitely.
|
||||||
|
* @param int $usTimeout Microseconds to add to the waiting time.
|
||||||
*
|
*
|
||||||
* @throws SocketException When there's no response within the time limit.
|
* @throws SocketException When there's no response within the time limit.
|
||||||
* @return Response The dispatched response.
|
* @return Response The dispatched response.
|
||||||
@ -805,7 +849,14 @@ class Client
|
|||||||
if ('' != $tag) {
|
if ('' != $tag) {
|
||||||
if ($this->isRequestActive($tag, self::FILTER_CALLBACK)) {
|
if ($this->isRequestActive($tag, self::FILTER_CALLBACK)) {
|
||||||
if ($this->callbacks[$tag]($response, $this)) {
|
if ($this->callbacks[$tag]($response, $this)) {
|
||||||
$this->cancelRequest($tag);
|
try {
|
||||||
|
$this->cancelRequest($tag);
|
||||||
|
} catch (DataFlowException $e) {
|
||||||
|
if ($e->getCode() !== DataFlowException::CODE_UNKNOWN_REQUEST
|
||||||
|
) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
} elseif ($isLastForRequest) {
|
} elseif ($isLastForRequest) {
|
||||||
unset($this->callbacks[$tag]);
|
unset($this->callbacks[$tag]);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -29,7 +30,7 @@ use PEAR2\Net\Transmitter as T;
|
|||||||
* A RouterOS communicator.
|
* A RouterOS communicator.
|
||||||
*
|
*
|
||||||
* Implementation of the RouterOS API protocol. Unlike the other classes in this
|
* Implementation of the RouterOS API protocol. Unlike the other classes in this
|
||||||
* package, this class doesn't provide any conviniences beyond the low level
|
* package, this class doesn't provide any conveniences beyond the low level
|
||||||
* implementation details (automatic word length encoding/decoding, charset
|
* implementation details (automatic word length encoding/decoding, charset
|
||||||
* translation and data integrity), and because of that, its direct usage is
|
* translation and data integrity), and because of that, its direct usage is
|
||||||
* strongly discouraged.
|
* strongly discouraged.
|
||||||
@ -67,8 +68,11 @@ class Communicator
|
|||||||
const CHARSET_LOCAL = 1;
|
const CHARSET_LOCAL = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with the default charset types as keys, and the
|
* An array with the default charset.
|
||||||
* default charsets as values.
|
*
|
||||||
|
* Charset types as keys, and the default charsets as values.
|
||||||
|
*
|
||||||
|
* @var array<string,string|null>
|
||||||
*/
|
*/
|
||||||
protected static $defaultCharsets = array(
|
protected static $defaultCharsets = array(
|
||||||
self::CHARSET_REMOTE => null,
|
self::CHARSET_REMOTE => null,
|
||||||
@ -76,37 +80,43 @@ class Communicator
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with the current charset types as keys, and the
|
* An array with the current charset.
|
||||||
* current charsets as values.
|
*
|
||||||
|
* Charset types as keys, and the current charsets as values.
|
||||||
|
*
|
||||||
|
* @var array<string,string|null>
|
||||||
*/
|
*/
|
||||||
protected $charsets = array();
|
protected $charsets = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var T\TcpClient The transmitter for the connection.
|
* The transmitter for the connection.
|
||||||
|
*
|
||||||
|
* @var T\TcpClient
|
||||||
*/
|
*/
|
||||||
protected $trans;
|
protected $trans;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new connection with the specified options.
|
* Creates a new connection with the specified options.
|
||||||
*
|
*
|
||||||
* @param string $host Hostname (IP or domain) of the RouterOS server.
|
* @param string $host Hostname (IP or domain) of RouterOS.
|
||||||
* @param int|null $port The port on which the RouterOS server provides
|
* @param int|null $port The port on which the RouterOS host
|
||||||
* the API service. You can also specify NULL, in which case the port
|
* provides the API service. You can also specify NULL, in which case
|
||||||
* will automatically be chosen between 8728 and 8729, depending on the
|
* the port will automatically be chosen between 8728 and 8729,
|
||||||
* value of $crypto.
|
* depending on the value of $crypto.
|
||||||
* @param bool $persist Whether or not the connection should be a
|
* @param bool $persist Whether or not the connection should be a
|
||||||
* persistent one.
|
* persistent one.
|
||||||
* @param float $timeout The timeout for the connection.
|
* @param double|null $timeout The timeout for the connection.
|
||||||
* @param string $key A string that uniquely identifies the
|
* @param string $key A string that uniquely identifies the
|
||||||
* connection.
|
* connection.
|
||||||
* @param string $crypto The encryption for this connection. Must be one
|
* @param string $crypto The encryption for this connection.
|
||||||
* of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_* constants. Off
|
* Must be one of the PEAR2\Net\Transmitter\NetworkStream::CRYPTO_*
|
||||||
* by default. RouterOS currently supports only TLS, but the setting is
|
* constants. Off by default. RouterOS currently supports only TLS, but
|
||||||
* provided in this fashion for forward compatibility's sake. And for
|
* the setting is provided in this fashion for forward compatibility's
|
||||||
* the sake of simplicity, if you specify an encryption, don't specify a
|
* sake. And for the sake of simplicity, if you specify an encryption,
|
||||||
* context and your default context uses the value "DEFAULT" for
|
* don't specify a context and your default context uses the value
|
||||||
* ciphers, "ADH" will be automatically added to the list of ciphers.
|
* "DEFAULT" for ciphers, "ADH" will be automatically added to the list
|
||||||
* @param resource $context A context for the socket.
|
* of ciphers.
|
||||||
|
* @param resource|null $context A context for the socket.
|
||||||
*
|
*
|
||||||
* @see sendWord()
|
* @see sendWord()
|
||||||
*/
|
*/
|
||||||
@ -126,7 +136,16 @@ class Communicator
|
|||||||
if (!isset($opts['ssl']['ciphers'])
|
if (!isset($opts['ssl']['ciphers'])
|
||||||
|| 'DEFAULT' === $opts['ssl']['ciphers']
|
|| 'DEFAULT' === $opts['ssl']['ciphers']
|
||||||
) {
|
) {
|
||||||
stream_context_set_option($context, 'ssl', 'ciphers', 'ADH');
|
stream_context_set_option(
|
||||||
|
$context,
|
||||||
|
array(
|
||||||
|
'ssl' => array(
|
||||||
|
'ciphers' => 'ADH',
|
||||||
|
'verify_peer' => false,
|
||||||
|
'verify_peer_name' => false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
@ -166,11 +185,11 @@ class Communicator
|
|||||||
* function. Depending on the argument given, one of the other functions in
|
* function. Depending on the argument given, one of the other functions in
|
||||||
* the class is invoked and its returned value is returned by this function.
|
* the class is invoked and its returned value is returned by this function.
|
||||||
*
|
*
|
||||||
* @param string $string A string of the word to send, or NULL to get the
|
* @param string|null $string A string of the word to send, or NULL to get
|
||||||
* next word as a string.
|
* the next word as a string.
|
||||||
*
|
*
|
||||||
* @return int|string If a string is provided, returns the number of bytes
|
* @return int|string If a string is provided, returns the number of bytes
|
||||||
* sent, otherwise retuns the next word as a string.
|
* sent, otherwise returns the next word as a string.
|
||||||
*/
|
*/
|
||||||
public function __invoke($string = null)
|
public function __invoke($string = null)
|
||||||
{
|
{
|
||||||
@ -218,8 +237,11 @@ class Communicator
|
|||||||
);
|
);
|
||||||
|
|
||||||
flock($stream, LOCK_SH);
|
flock($stream, LOCK_SH);
|
||||||
while (!feof($stream)) {
|
$reader = new T\Stream($stream, false);
|
||||||
$bytes += stream_copy_to_stream($stream, $result, 0xFFFFF);
|
$writer = new T\Stream($result, false);
|
||||||
|
$chunkSize = $reader->getChunk(T\Stream::DIRECTION_RECEIVE);
|
||||||
|
while ($reader->isAvailable() && $reader->isDataAwaiting()) {
|
||||||
|
$bytes += $writer->send(fread($stream, $chunkSize));
|
||||||
}
|
}
|
||||||
fseek($stream, -$bytes, SEEK_CUR);
|
fseek($stream, -$bytes, SEEK_CUR);
|
||||||
flock($stream, LOCK_UN);
|
flock($stream, LOCK_UN);
|
||||||
@ -243,6 +265,7 @@ class Communicator
|
|||||||
* @return string|array The old charset. If $charsetType is
|
* @return string|array The old charset. If $charsetType is
|
||||||
* {@link self::CHARSET_ALL}, the old values will be returned as an
|
* {@link self::CHARSET_ALL}, the old values will be returned as an
|
||||||
* array with the types as keys, and charsets as values.
|
* array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see setCharset()
|
* @see setCharset()
|
||||||
*/
|
*/
|
||||||
public static function setDefaultCharset(
|
public static function setDefaultCharset(
|
||||||
@ -274,6 +297,7 @@ class Communicator
|
|||||||
* @return string|array The current charset. If $charsetType is
|
* @return string|array The current charset. If $charsetType is
|
||||||
* {@link self::CHARSET_ALL}, the current values will be returned as an
|
* {@link self::CHARSET_ALL}, the current values will be returned as an
|
||||||
* array with the types as keys, and charsets as values.
|
* array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see setDefaultCharset()
|
* @see setDefaultCharset()
|
||||||
*/
|
*/
|
||||||
public static function getDefaultCharset($charsetType)
|
public static function getDefaultCharset($charsetType)
|
||||||
@ -310,7 +334,7 @@ class Communicator
|
|||||||
* used for all future words. When sending, {@link self::CHARSET_LOCAL} is
|
* used for all future words. When sending, {@link self::CHARSET_LOCAL} is
|
||||||
* converted to {@link self::CHARSET_REMOTE}, and when receiving,
|
* converted to {@link self::CHARSET_REMOTE}, and when receiving,
|
||||||
* {@link self::CHARSET_REMOTE} is converted to {@link self::CHARSET_LOCAL}.
|
* {@link self::CHARSET_REMOTE} is converted to {@link self::CHARSET_LOCAL}.
|
||||||
* Setting NULL to either charset will disable charset convertion, and data
|
* Setting NULL to either charset will disable charset conversion, and data
|
||||||
* will be both sent and received "as is".
|
* will be both sent and received "as is".
|
||||||
*
|
*
|
||||||
* @param mixed $charset The charset to set. If $charsetType is
|
* @param mixed $charset The charset to set. If $charsetType is
|
||||||
@ -324,6 +348,7 @@ class Communicator
|
|||||||
* @return string|array The old charset. If $charsetType is
|
* @return string|array The old charset. If $charsetType is
|
||||||
* {@link self::CHARSET_ALL}, the old values will be returned as an
|
* {@link self::CHARSET_ALL}, the old values will be returned as an
|
||||||
* array with the types as keys, and charsets as values.
|
* array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see setDefaultCharset()
|
* @see setDefaultCharset()
|
||||||
*/
|
*/
|
||||||
public function setCharset($charset, $charsetType = self::CHARSET_ALL)
|
public function setCharset($charset, $charsetType = self::CHARSET_ALL)
|
||||||
@ -353,6 +378,7 @@ class Communicator
|
|||||||
* @return string|array The current charset. If $charsetType is
|
* @return string|array The current charset. If $charsetType is
|
||||||
* {@link self::CHARSET_ALL}, the current values will be returned as an
|
* {@link self::CHARSET_ALL}, the current values will be returned as an
|
||||||
* array with the types as keys, and charsets as values.
|
* array with the types as keys, and charsets as values.
|
||||||
|
*
|
||||||
* @see getDefaultCharset()
|
* @see getDefaultCharset()
|
||||||
* @see setCharset()
|
* @see setCharset()
|
||||||
*/
|
*/
|
||||||
@ -380,6 +406,7 @@ class Communicator
|
|||||||
* @param string $word The word to send.
|
* @param string $word The word to send.
|
||||||
*
|
*
|
||||||
* @return int The number of bytes sent.
|
* @return int The number of bytes sent.
|
||||||
|
*
|
||||||
* @see sendWordFromStream()
|
* @see sendWordFromStream()
|
||||||
* @see getNextWord()
|
* @see getNextWord()
|
||||||
*/
|
*/
|
||||||
@ -417,6 +444,7 @@ class Communicator
|
|||||||
* @param resource $stream The seekable stream to send.
|
* @param resource $stream The seekable stream to send.
|
||||||
*
|
*
|
||||||
* @return int The number of bytes sent.
|
* @return int The number of bytes sent.
|
||||||
|
*
|
||||||
* @see sendWord()
|
* @see sendWord()
|
||||||
*/
|
*/
|
||||||
public function sendWordFromStream($prefix, $stream)
|
public function sendWordFromStream($prefix, $stream)
|
||||||
@ -465,7 +493,7 @@ class Communicator
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function verifyLengthSupport($length)
|
public static function verifyLengthSupport($length)
|
||||||
{
|
{
|
||||||
if ($length > 0xFFFFFFFF) {
|
if ($length > 0xFFFFFFFF) {
|
||||||
throw new LengthException(
|
throw new LengthException(
|
||||||
@ -478,7 +506,7 @@ class Communicator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes the length as requred by the RouterOS API.
|
* Encodes the length as required by the RouterOS API.
|
||||||
*
|
*
|
||||||
* @param int $length The length to encode.
|
* @param int $length The length to encode.
|
||||||
*
|
*
|
||||||
@ -524,6 +552,7 @@ class Communicator
|
|||||||
* length.
|
* length.
|
||||||
*
|
*
|
||||||
* @return string The word.
|
* @return string The word.
|
||||||
|
*
|
||||||
* @see close()
|
* @see close()
|
||||||
*/
|
*/
|
||||||
public function getNextWord()
|
public function getNextWord()
|
||||||
@ -562,6 +591,7 @@ class Communicator
|
|||||||
* length.
|
* length.
|
||||||
*
|
*
|
||||||
* @return resource The word, as a stream.
|
* @return resource The word, as a stream.
|
||||||
|
*
|
||||||
* @see close()
|
* @see close()
|
||||||
*/
|
*/
|
||||||
public function getNextWordAsStream()
|
public function getNextWordAsStream()
|
||||||
@ -596,15 +626,16 @@ class Communicator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes the lenght of the incoming message.
|
* Decodes the length of the incoming message.
|
||||||
*
|
*
|
||||||
* Decodes the lenght of the incoming message, as specified by the RouterOS
|
* Decodes the length of the incoming message, as specified by the RouterOS
|
||||||
* API.
|
* API.
|
||||||
*
|
*
|
||||||
* @param T\Stream $trans The transmitter from which to decode the length of
|
* @param T\Stream $trans The transmitter from which to decode the length of
|
||||||
* the incoming message.
|
* the incoming message.
|
||||||
*
|
*
|
||||||
* @return int The decoded length.
|
* @return int|double The decoded length.
|
||||||
|
* Is of type "double" only for values above "2 << 31".
|
||||||
*/
|
*/
|
||||||
public static function decodeLength(T\Stream $trans)
|
public static function decodeLength(T\Stream $trans)
|
||||||
{
|
{
|
||||||
@ -618,9 +649,9 @@ class Communicator
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes the lenght of the incoming message.
|
* Decodes the length of the incoming message.
|
||||||
*
|
*
|
||||||
* Decodes the lenght of the incoming message, as specified by the RouterOS
|
* Decodes the length of the incoming message, as specified by the RouterOS
|
||||||
* API.
|
* API.
|
||||||
*
|
*
|
||||||
* Difference with the non private function is that this one doesn't perform
|
* Difference with the non private function is that this one doesn't perform
|
||||||
@ -629,7 +660,8 @@ class Communicator
|
|||||||
* @param T\Stream $trans The transmitter from which to decode the length of
|
* @param T\Stream $trans The transmitter from which to decode the length of
|
||||||
* the incoming message.
|
* the incoming message.
|
||||||
*
|
*
|
||||||
* @return int The decoded length.
|
* @return int|double The decoded length.
|
||||||
|
* Is of type "double" only for values above "2 << 31".
|
||||||
*/
|
*/
|
||||||
private static function _decodeLength(T\Stream $trans)
|
private static function _decodeLength(T\Stream $trans)
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -25,6 +26,11 @@ namespace PEAR2\Net\RouterOS;
|
|||||||
*/
|
*/
|
||||||
use LengthException as L;
|
use LengthException as L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in $previous
|
||||||
|
*/
|
||||||
|
use Exception as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception thrown when there is a problem with a word's length.
|
* Exception thrown when there is a problem with a word's length.
|
||||||
*
|
*
|
||||||
@ -42,24 +48,25 @@ class LengthException extends L implements Exception
|
|||||||
const CODE_BEYOND_SHEME = 1301;
|
const CODE_BEYOND_SHEME = 1301;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The problematic length.
|
||||||
*
|
*
|
||||||
* @var mixed The problematic length.
|
* @var int|double|null
|
||||||
*/
|
*/
|
||||||
private $_length;
|
private $_length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new LengthException.
|
* Creates a new LengthException.
|
||||||
*
|
*
|
||||||
* @param string $message The Exception message to throw.
|
* @param string $message The Exception message to throw.
|
||||||
* @param int $code The Exception code.
|
* @param int $code The Exception code.
|
||||||
* @param \Exception $previous The previous exception used for the exception
|
* @param E|null $previous The previous exception used for the
|
||||||
* chaining.
|
* exception chaining.
|
||||||
* @param number $length The length.
|
* @param int|double|null $length The length.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$message,
|
$message,
|
||||||
$code = 0,
|
$code = 0,
|
||||||
$previous = null,
|
E $previous = null,
|
||||||
$length = null
|
$length = null
|
||||||
) {
|
) {
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
@ -69,7 +76,7 @@ class LengthException extends L implements Exception
|
|||||||
/**
|
/**
|
||||||
* Gets the length.
|
* Gets the length.
|
||||||
*
|
*
|
||||||
* @return number The length.
|
* @return int|double|null The length.
|
||||||
*/
|
*/
|
||||||
public function getLength()
|
public function getLength()
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -31,7 +32,7 @@ use Countable;
|
|||||||
use IteratorAggregate;
|
use IteratorAggregate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requred for IteratorAggregate::getIterator() to work properly with foreach.
|
* Required for IteratorAggregate::getIterator() to work properly with foreach.
|
||||||
*/
|
*/
|
||||||
use ArrayObject;
|
use ArrayObject;
|
||||||
|
|
||||||
@ -48,14 +49,19 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with message attributes. Each array key is the the
|
* An array with message attributes.
|
||||||
* name of an attribute, and the correspding array value is the value
|
*
|
||||||
* for that attribute.
|
* Each array key is the the name of an attribute,
|
||||||
|
* and the corresponding array value is the value for that attribute.
|
||||||
|
*
|
||||||
|
* @var array<string,string|resource>
|
||||||
*/
|
*/
|
||||||
protected $attributes = array();
|
protected $attributes = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string An optional tag to associate the message with.
|
* An optional tag to associate the message with.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_tag = null;
|
private $_tag = null;
|
||||||
|
|
||||||
@ -66,8 +72,8 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
* function. Depending on the argument given, one of the other functions in
|
* function. Depending on the argument given, one of the other functions in
|
||||||
* the class is invoked and its returned value is returned by this function.
|
* the class is invoked and its returned value is returned by this function.
|
||||||
*
|
*
|
||||||
* @param string $name The name of an attribute to get the value of, or NULL
|
* @param string|null $name The name of an attribute to get the value of,
|
||||||
* to get the tag.
|
* or NULL to get the tag.
|
||||||
*
|
*
|
||||||
* @return string|resource The value of the specified attribute,
|
* @return string|resource The value of the specified attribute,
|
||||||
* or the tag if NULL is provided.
|
* or the tag if NULL is provided.
|
||||||
@ -106,7 +112,7 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
*
|
*
|
||||||
* @param mixed $value The value to sanitize.
|
* @param mixed $value The value to sanitize.
|
||||||
*
|
*
|
||||||
* @return string The sanitized value.
|
* @return string|resource The sanitized value.
|
||||||
*/
|
*/
|
||||||
public static function sanitizeAttributeValue($value)
|
public static function sanitizeAttributeValue($value)
|
||||||
{
|
{
|
||||||
@ -121,6 +127,7 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
* Gets the tag that the message is associated with.
|
* Gets the tag that the message is associated with.
|
||||||
*
|
*
|
||||||
* @return string The current tag or NULL if there isn't a tag.
|
* @return string The current tag or NULL if there isn't a tag.
|
||||||
|
*
|
||||||
* @see setTag()
|
* @see setTag()
|
||||||
*/
|
*/
|
||||||
public function getTag()
|
public function getTag()
|
||||||
@ -137,6 +144,7 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
* @param string $tag The tag to set.
|
* @param string $tag The tag to set.
|
||||||
*
|
*
|
||||||
* @return $this The message object.
|
* @return $this The message object.
|
||||||
|
*
|
||||||
* @see getTag()
|
* @see getTag()
|
||||||
*/
|
*/
|
||||||
protected function setTag($tag)
|
protected function setTag($tag)
|
||||||
@ -152,6 +160,7 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
*
|
*
|
||||||
* @return string|resource|null The value of the specified attribute.
|
* @return string|resource|null The value of the specified attribute.
|
||||||
* Returns NULL if such an attribute is not set.
|
* Returns NULL if such an attribute is not set.
|
||||||
|
*
|
||||||
* @see setAttribute()
|
* @see setAttribute()
|
||||||
*/
|
*/
|
||||||
protected function getAttribute($name)
|
protected function getAttribute($name)
|
||||||
@ -168,6 +177,7 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
*
|
*
|
||||||
* @return ArrayObject An ArrayObject with the keys being argument names,
|
* @return ArrayObject An ArrayObject with the keys being argument names,
|
||||||
* and the array values being argument values.
|
* and the array values being argument values.
|
||||||
|
*
|
||||||
* @see getArgument()
|
* @see getArgument()
|
||||||
* @see setArgument()
|
* @see setArgument()
|
||||||
*/
|
*/
|
||||||
@ -177,24 +187,13 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Counts the number of arguments.
|
* Counts the number of attributes.
|
||||||
*
|
*
|
||||||
* @param int $mode The counter mode.
|
* @return int The number of attributes.
|
||||||
* Either COUNT_NORMAL or COUNT_RECURSIVE.
|
|
||||||
* When in normal mode, counts the number of arguments.
|
|
||||||
* When in recursive mode, counts the number of API words
|
|
||||||
* (including the empty word at the end).
|
|
||||||
*
|
|
||||||
* @return int The number of arguments/words.
|
|
||||||
*/
|
*/
|
||||||
public function count($mode = COUNT_NORMAL)
|
public function count()
|
||||||
{
|
{
|
||||||
$result = count($this->attributes);
|
return count($this->attributes);
|
||||||
if ($mode !== COUNT_NORMAL) {
|
|
||||||
$result += 2/*first+last word*/
|
|
||||||
+ (int)(null !== $this->getTag());
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -205,12 +204,13 @@ abstract class Message implements IteratorAggregate, Countable
|
|||||||
* seekable stream.
|
* seekable stream.
|
||||||
* Setting the value to NULL removes an argument of this name.
|
* Setting the value to NULL removes an argument of this name.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
*
|
*
|
||||||
* @return $this The message object.
|
* @return $this The message object.
|
||||||
|
*
|
||||||
* @see getArgument()
|
* @see getArgument()
|
||||||
*/
|
*/
|
||||||
protected function setAttribute($name, $value = '')
|
protected function setAttribute($name, $value = '')
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -40,24 +41,30 @@ class NotSupportedException extends E implements Exception
|
|||||||
|
|
||||||
const CODE_CONTROL_BYTE = 1601;
|
const CODE_CONTROL_BYTE = 1601;
|
||||||
|
|
||||||
|
const CODE_MENU_MISMATCH = 60000;
|
||||||
|
|
||||||
|
const CODE_ARG_PROHIBITED = 60001;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var mixed The unsuppported value.
|
* The unsupported value.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
private $_value;
|
private $_value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new NotSupportedException.
|
* Creates a new NotSupportedException.
|
||||||
*
|
*
|
||||||
* @param string $message The Exception message to throw.
|
* @param string $message The Exception message to throw.
|
||||||
* @param int $code The Exception code.
|
* @param int $code The Exception code.
|
||||||
* @param \Exception $previous The previous exception used for the exception
|
* @param E|null $previous The previous exception used for the exception
|
||||||
* chaining.
|
* chaining.
|
||||||
* @param mixed $value The unsupported value.
|
* @param mixed $value The unsupported value.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$message,
|
$message,
|
||||||
$code = 0,
|
$code = 0,
|
||||||
$previous = null,
|
E $previous = null,
|
||||||
$value = null
|
$value = null
|
||||||
) {
|
) {
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
|
43
system/autoload/PEAR2/Net/RouterOS/ParserException.php
Normal file
43
system/autoload/PEAR2/Net/RouterOS/ParserException.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
|
*
|
||||||
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @copyright 2011 Vasil Rangelov
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @version 1.0.0b6
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* The namespace declaration.
|
||||||
|
*/
|
||||||
|
namespace PEAR2\Net\RouterOS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base of this class.
|
||||||
|
*/
|
||||||
|
use DomainException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown when a value can't be parsed properly.
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
class ParserException extends DomainException implements Exception
|
||||||
|
{
|
||||||
|
const CODE_DATETIME = 1;
|
||||||
|
const CODE_DATEINTERVAL = 2;
|
||||||
|
const CODE_ARRAY = 3;
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -58,14 +59,18 @@ class Query
|
|||||||
const OP_LT = '<';
|
const OP_LT = '<';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the property is greather than a certain value.
|
* Checks if the property is greater than a certain value.
|
||||||
*/
|
*/
|
||||||
const OP_GT = '>';
|
const OP_GT = '>';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array of the words forming the query. Each value is an
|
* An array of the words forming the query.
|
||||||
* array with the first member being the predicate (operator and name),
|
*
|
||||||
* and the second member being the value for the predicate.
|
* Each value is an array with the first member being the predicate
|
||||||
|
* (operator and name), and the second member being the value
|
||||||
|
* for the predicate.
|
||||||
|
*
|
||||||
|
* @var array<string,string|null>[]
|
||||||
*/
|
*/
|
||||||
protected $words = array();
|
protected $words = array();
|
||||||
|
|
||||||
@ -73,7 +78,7 @@ class Query
|
|||||||
* This class is not to be instantiated normally, but by static methods
|
* This class is not to be instantiated normally, but by static methods
|
||||||
* instead. Use {@link static::where()} to create an instance of it.
|
* instead. Use {@link static::where()} to create an instance of it.
|
||||||
*/
|
*/
|
||||||
private function __construct()
|
protected function __construct()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -112,7 +117,7 @@ class Query
|
|||||||
* @param string|resource|null $value Value of the property as a string
|
* @param string|resource|null $value Value of the property as a string
|
||||||
* or seekable stream. Not required for existence tests.
|
* or seekable stream. Not required for existence tests.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
@ -148,7 +153,7 @@ class Query
|
|||||||
* @param string|resource|null $value Value of the property as a string
|
* @param string|resource|null $value Value of the property as a string
|
||||||
* or seekable stream. Not required for existence tests.
|
* or seekable stream. Not required for existence tests.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
@ -170,7 +175,7 @@ class Query
|
|||||||
* @param string|resource|null $value Value of the property as a string
|
* @param string|resource|null $value Value of the property as a string
|
||||||
* or seekable stream. Not required for existence tests.
|
* or seekable stream. Not required for existence tests.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
@ -239,6 +244,39 @@ class Query
|
|||||||
return $bytes;
|
return $bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the query.
|
||||||
|
*
|
||||||
|
* Verifies the query against a communicator, i.e. whether the query
|
||||||
|
* could successfully be sent (assuming the connection is still opened).
|
||||||
|
*
|
||||||
|
* @param Communicator $com The Communicator to check against.
|
||||||
|
*
|
||||||
|
* @return $this The query object itself.
|
||||||
|
*
|
||||||
|
* @throws LengthException If the resulting length of an API word is not
|
||||||
|
* supported.
|
||||||
|
*/
|
||||||
|
public function verify(Communicator $com)
|
||||||
|
{
|
||||||
|
foreach ($this->words as $queryWord) {
|
||||||
|
list($predicate, $value) = $queryWord;
|
||||||
|
if (null === $value) {
|
||||||
|
$com::verifyLengthSupport(strlen('?' . $predicate));
|
||||||
|
} elseif (is_string($value)) {
|
||||||
|
$com::verifyLengthSupport(
|
||||||
|
strlen('?' . $predicate . '=' . $value)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$com::verifyLengthSupport(
|
||||||
|
strlen('?' . $predicate . '=') +
|
||||||
|
$com::seekableStreamLength($value)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a condition.
|
* Adds a condition.
|
||||||
*
|
*
|
||||||
@ -246,7 +284,7 @@ class Query
|
|||||||
* @param string|resource|null $value Value of the property as a string
|
* @param string|resource|null $value Value of the property as a string
|
||||||
* or seekable stream. Not required for existence tests.
|
* or seekable stream. Not required for existence tests.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -42,22 +43,30 @@ use PEAR2\Cache\SHM;
|
|||||||
class Registry
|
class Registry
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var SHM The storage.
|
* The storage.
|
||||||
|
*
|
||||||
|
* @var SHM
|
||||||
*/
|
*/
|
||||||
protected $shm;
|
protected $shm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int ID of request. Populated at first instance in request.
|
* ID of request. Populated at first instance in request.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected static $requestId = -1;
|
protected static $requestId = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int ID to be given to next instance, after incrementing it.
|
* ID to be given to next instance, after incrementing it.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected static $instanceIdSeed = -1;
|
protected static $instanceIdSeed = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int ID of instance within the request.
|
* ID of instance within the request.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $instanceId;
|
protected $instanceId;
|
||||||
|
|
||||||
@ -84,7 +93,8 @@ class Registry
|
|||||||
*
|
*
|
||||||
* @param string $tag The tag (as received) to parse.
|
* @param string $tag The tag (as received) to parse.
|
||||||
*
|
*
|
||||||
* @return array An array with the first member being the ownership tag, and
|
* @return array<int,string|null> An array with
|
||||||
|
* the first member being the ownership tag, and
|
||||||
* the second one being the original tag.
|
* the second one being the original tag.
|
||||||
*/
|
*/
|
||||||
public static function parseTag($tag)
|
public static function parseTag($tag)
|
||||||
@ -118,7 +128,7 @@ class Registry
|
|||||||
/**
|
/**
|
||||||
* Sets the "tagless mode" setting.
|
* Sets the "tagless mode" setting.
|
||||||
*
|
*
|
||||||
* While in tagless mode, this instance will claim owhership of any
|
* While in tagless mode, this instance will claim ownership of any
|
||||||
* responses without a tag. While not in this mode, any requests without a
|
* responses without a tag. While not in this mode, any requests without a
|
||||||
* tag will be given to all instances.
|
* tag will be given to all instances.
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -38,22 +39,26 @@ class Request extends Message
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The command to be executed.
|
* The command to be executed.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_command;
|
private $_command;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Query A query for the command.
|
* A query for the command.
|
||||||
|
*
|
||||||
|
* @var Query
|
||||||
*/
|
*/
|
||||||
private $_query;
|
private $_query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a request to send to RouterOS.
|
* Creates a request to send to RouterOS.
|
||||||
*
|
*
|
||||||
* @param string $command The command to send. Can also contain arguments
|
* @param string $command The command to send.
|
||||||
* expressed in a shell-like syntax.
|
* Can also contain arguments expressed in a shell-like syntax.
|
||||||
* @param Query $query A query to associate with the request.
|
* @param Query|null $query A query to associate with the request.
|
||||||
* @param string $tag The tag for the request.
|
* @param string|null $tag The tag for the request.
|
||||||
*
|
*
|
||||||
* @see setCommand()
|
* @see setCommand()
|
||||||
* @see setArgument()
|
* @see setArgument()
|
||||||
@ -116,6 +121,7 @@ class Request extends Message
|
|||||||
* @param string $command The command to send.
|
* @param string $command The command to send.
|
||||||
*
|
*
|
||||||
* @return $this The request object.
|
* @return $this The request object.
|
||||||
|
*
|
||||||
* @see getCommand()
|
* @see getCommand()
|
||||||
* @see setArgument()
|
* @see setArgument()
|
||||||
*/
|
*/
|
||||||
@ -165,6 +171,7 @@ class Request extends Message
|
|||||||
* Gets the command that will be send to RouterOS in its API syntax.
|
* Gets the command that will be send to RouterOS in its API syntax.
|
||||||
*
|
*
|
||||||
* @return string The command to send.
|
* @return string The command to send.
|
||||||
|
*
|
||||||
* @see setCommand()
|
* @see setCommand()
|
||||||
*/
|
*/
|
||||||
public function getCommand()
|
public function getCommand()
|
||||||
@ -175,10 +182,11 @@ class Request extends Message
|
|||||||
/**
|
/**
|
||||||
* Sets the query to send with the command.
|
* Sets the query to send with the command.
|
||||||
*
|
*
|
||||||
* @param Query $query The query to be set. Setting NULL will remove the
|
* @param Query|null $query The query to be set.
|
||||||
* currently associated query.
|
* Setting NULL will remove the currently associated query.
|
||||||
*
|
*
|
||||||
* @return $this The request object.
|
* @return $this The request object.
|
||||||
|
*
|
||||||
* @see getQuery()
|
* @see getQuery()
|
||||||
*/
|
*/
|
||||||
public function setQuery(Query $query = null)
|
public function setQuery(Query $query = null)
|
||||||
@ -190,7 +198,8 @@ class Request extends Message
|
|||||||
/**
|
/**
|
||||||
* Gets the currently associated query
|
* Gets the currently associated query
|
||||||
*
|
*
|
||||||
* @return Query The currently associated query.
|
* @return Query|null The currently associated query.
|
||||||
|
*
|
||||||
* @see setQuery()
|
* @see setQuery()
|
||||||
*/
|
*/
|
||||||
public function getQuery()
|
public function getQuery()
|
||||||
@ -204,9 +213,10 @@ class Request extends Message
|
|||||||
* Sets the tag to associate the request with. Setting NULL erases the
|
* Sets the tag to associate the request with. Setting NULL erases the
|
||||||
* currently set tag.
|
* currently set tag.
|
||||||
*
|
*
|
||||||
* @param string $tag The tag to set.
|
* @param string|null $tag The tag to set.
|
||||||
*
|
*
|
||||||
* @return $this The request object.
|
* @return $this The request object.
|
||||||
|
*
|
||||||
* @see getTag()
|
* @see getTag()
|
||||||
*/
|
*/
|
||||||
public function setTag($tag)
|
public function setTag($tag)
|
||||||
@ -222,12 +232,13 @@ class Request extends Message
|
|||||||
* seekable stream.
|
* seekable stream.
|
||||||
* Setting the value to NULL removes an argument of this name.
|
* Setting the value to NULL removes an argument of this name.
|
||||||
* If a seekable stream is provided, it is sent from its current
|
* If a seekable stream is provided, it is sent from its current
|
||||||
* posistion to its end, and the pointer is seeked back to its current
|
* position to its end, and the pointer is seeked back to its current
|
||||||
* position after sending.
|
* position after sending.
|
||||||
* Non seekable streams, as well as all other types, are casted to a
|
* Non seekable streams, as well as all other types, are casted to a
|
||||||
* string.
|
* string.
|
||||||
*
|
*
|
||||||
* @return $this The request object.
|
* @return $this The request object.
|
||||||
|
*
|
||||||
* @see getArgument()
|
* @see getArgument()
|
||||||
*/
|
*/
|
||||||
public function setArgument($name, $value = '')
|
public function setArgument($name, $value = '')
|
||||||
@ -242,6 +253,7 @@ class Request extends Message
|
|||||||
*
|
*
|
||||||
* @return string|resource|null The value of the specified argument.
|
* @return string|resource|null The value of the specified argument.
|
||||||
* Returns NULL if such an argument is not set.
|
* Returns NULL if such an argument is not set.
|
||||||
|
*
|
||||||
* @see setAttribute()
|
* @see setAttribute()
|
||||||
*/
|
*/
|
||||||
public function getArgument($name)
|
public function getArgument($name)
|
||||||
@ -262,10 +274,11 @@ class Request extends Message
|
|||||||
/**
|
/**
|
||||||
* Sends a request over a communicator.
|
* Sends a request over a communicator.
|
||||||
*
|
*
|
||||||
* @param Communicator $com The communicator to send the request over.
|
* @param Communicator $com The communicator to send the request over.
|
||||||
* @param Registry $reg An optional registry to sync the request with.
|
* @param Registry|null $reg An optional registry to sync the request with.
|
||||||
*
|
*
|
||||||
* @return int The number of bytes sent.
|
* @return int The number of bytes sent.
|
||||||
|
*
|
||||||
* @see Client::sendSync()
|
* @see Client::sendSync()
|
||||||
* @see Client::sendAsync()
|
* @see Client::sendAsync()
|
||||||
*/
|
*/
|
||||||
@ -298,6 +311,7 @@ class Request extends Message
|
|||||||
* @param Communicator $com The communicator to send the request over.
|
* @param Communicator $com The communicator to send the request over.
|
||||||
*
|
*
|
||||||
* @return int The number of bytes sent.
|
* @return int The number of bytes sent.
|
||||||
|
*
|
||||||
* @see Client::sendSync()
|
* @see Client::sendSync()
|
||||||
* @see Client::sendAsync()
|
* @see Client::sendAsync()
|
||||||
*/
|
*/
|
||||||
@ -330,6 +344,40 @@ class Request extends Message
|
|||||||
return $bytes;
|
return $bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies the request.
|
||||||
|
*
|
||||||
|
* Verifies the request against a communicator, i.e. whether the request
|
||||||
|
* could successfully be sent (assuming the connection is still opened).
|
||||||
|
*
|
||||||
|
* @param Communicator $com The Communicator to check against.
|
||||||
|
*
|
||||||
|
* @return $this The request object itself.
|
||||||
|
*
|
||||||
|
* @throws LengthException If the resulting length of an API word is not
|
||||||
|
* supported.
|
||||||
|
*/
|
||||||
|
public function verify(Communicator $com)
|
||||||
|
{
|
||||||
|
$com::verifyLengthSupport(strlen($this->getCommand()));
|
||||||
|
$com::verifyLengthSupport(strlen('.tag=' . (string)$this->getTag()));
|
||||||
|
foreach ($this->attributes as $name => $value) {
|
||||||
|
if (is_string($value)) {
|
||||||
|
$com::verifyLengthSupport(strlen('=' . $name . '=' . $value));
|
||||||
|
} else {
|
||||||
|
$com::verifyLengthSupport(
|
||||||
|
strlen('=' . $name . '=') +
|
||||||
|
$com::seekableStreamLength($value)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$query = $this->getQuery();
|
||||||
|
if ($query instanceof Query) {
|
||||||
|
$query->verify($com);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the arguments of a command.
|
* Parses the arguments of a command.
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -64,26 +65,30 @@ class Response extends Message
|
|||||||
const TYPE_FATAL = '!fatal';
|
const TYPE_FATAL = '!fatal';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array of unrecognized words in network order.
|
* An array of unrecognized words in network order.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $unrecognizedWords = array();
|
protected $unrecognizedWords = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The response type.
|
* The response type.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $_type;
|
private $_type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts a new response from a communicator.
|
* Extracts a new response from a communicator.
|
||||||
*
|
*
|
||||||
* @param Communicator $com The communicator from which to extract
|
* @param Communicator $com The communicator from which to extract
|
||||||
* the new response.
|
* the new response.
|
||||||
* @param bool $asStream Whether to populate the argument values
|
* @param bool $asStream Whether to populate the argument values
|
||||||
* with streams instead of strings.
|
* with streams instead of strings.
|
||||||
* @param int $sTimeout If a response is not immediatly
|
* @param int $sTimeout If a response is not immediately
|
||||||
* available, wait this many seconds. If NULL, wait indefinetly.
|
* available, wait this many seconds. If NULL, wait indefinitely.
|
||||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
* @param int|null $usTimeout Microseconds to add to the waiting time.
|
||||||
* @param Registry $reg An optional registry to sync the
|
* @param Registry|null $reg An optional registry to sync the
|
||||||
* response with.
|
* response with.
|
||||||
*
|
*
|
||||||
* @see getType()
|
* @see getType()
|
||||||
@ -150,11 +155,11 @@ class Response extends Message
|
|||||||
* the new response.
|
* the new response.
|
||||||
* @param bool $asStream Whether to populate the argument values
|
* @param bool $asStream Whether to populate the argument values
|
||||||
* with streams instead of strings.
|
* with streams instead of strings.
|
||||||
* @param int $sTimeout If a response is not immediatly
|
* @param int $sTimeout If a response is not immediately
|
||||||
* available, wait this many seconds. If NULL, wait indefinetly.
|
* available, wait this many seconds. If NULL, wait indefinitely.
|
||||||
* Note that if an empty sentence is received, the timeout will be
|
* Note that if an empty sentence is received, the timeout will be
|
||||||
* reset for another sentence receiving.
|
* reset for another sentence receiving.
|
||||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
* @param int|null $usTimeout Microseconds to add to the waiting time.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@ -234,6 +239,7 @@ class Response extends Message
|
|||||||
* @param string $type The new response type.
|
* @param string $type The new response type.
|
||||||
*
|
*
|
||||||
* @return $this The response object.
|
* @return $this The response object.
|
||||||
|
*
|
||||||
* @see getType()
|
* @see getType()
|
||||||
*/
|
*/
|
||||||
protected function setType($type)
|
protected function setType($type)
|
||||||
@ -259,6 +265,7 @@ class Response extends Message
|
|||||||
* Gets the response type.
|
* Gets the response type.
|
||||||
*
|
*
|
||||||
* @return string The response type.
|
* @return string The response type.
|
||||||
|
*
|
||||||
* @see setType()
|
* @see setType()
|
||||||
*/
|
*/
|
||||||
public function getType()
|
public function getType()
|
||||||
@ -273,7 +280,8 @@ class Response extends Message
|
|||||||
*
|
*
|
||||||
* @return string|resource|null The value of the specified argument.
|
* @return string|resource|null The value of the specified argument.
|
||||||
* Returns NULL if such an argument is not set.
|
* Returns NULL if such an argument is not set.
|
||||||
* @deprecated 1.0.0b5 Use {@link static::getProperty()} instead.
|
*
|
||||||
|
* @deprecated 1.0.0b5 Use {@link static::getProperty()} instead.
|
||||||
* This method will be removed upon final release, and is currently
|
* This method will be removed upon final release, and is currently
|
||||||
* left standing merely because it can't be easily search&replaced in
|
* left standing merely because it can't be easily search&replaced in
|
||||||
* existing code, due to the fact the name "getArgument()" is shared
|
* existing code, due to the fact the name "getArgument()" is shared
|
||||||
@ -307,29 +315,10 @@ class Response extends Message
|
|||||||
/**
|
/**
|
||||||
* Gets a list of unrecognized words.
|
* Gets a list of unrecognized words.
|
||||||
*
|
*
|
||||||
* @return array The list of unrecognized words.
|
* @return string[] The list of unrecognized words.
|
||||||
*/
|
*/
|
||||||
public function getUnrecognizedWords()
|
public function getUnrecognizedWords()
|
||||||
{
|
{
|
||||||
return $this->unrecognizedWords;
|
return $this->unrecognizedWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Counts the number of arguments or words.
|
|
||||||
*
|
|
||||||
* @param int $mode The counter mode.
|
|
||||||
* Either COUNT_NORMAL or COUNT_RECURSIVE.
|
|
||||||
* When in normal mode, counts the number of arguments.
|
|
||||||
* When in recursive mode, counts the number of API words.
|
|
||||||
*
|
|
||||||
* @return int The number of arguments/words.
|
|
||||||
*/
|
|
||||||
public function count($mode = COUNT_NORMAL)
|
|
||||||
{
|
|
||||||
$result = parent::count($mode);
|
|
||||||
if ($mode !== COUNT_NORMAL) {
|
|
||||||
$result += count($this->unrecognizedWords);
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -61,59 +62,86 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with all {@link Response} objects.
|
* An array with all {@link Response} objects.
|
||||||
|
*
|
||||||
|
* An array with all Response objects.
|
||||||
|
*
|
||||||
|
* @var Response[]
|
||||||
*/
|
*/
|
||||||
protected $responses = array();
|
protected $responses = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with each {@link Response} object's type.
|
* An array with each Response object's type.
|
||||||
|
*
|
||||||
|
* An array with each {@link Response} object's type.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $responseTypes = array();
|
protected $responseTypes = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with each {@link Response} object's tag.
|
* An array with each Response object's tag.
|
||||||
|
*
|
||||||
|
* An array with each {@link Response} object's tag.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
protected $responseTags = array();
|
protected $responseTags = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with positions of responses, based on an property
|
* An array with positions of responses, based on an property name.
|
||||||
* name. The name of each property is the array key, and the array value
|
*
|
||||||
* is another array where the key is the value for that property, and
|
* The name of each property is the array key, and the array value
|
||||||
* the value is the posistion of the response. For performance reasons,
|
* is another array where the key is the value for that property, and
|
||||||
* each key is built only when {@link static::setIndex()} is called with
|
* the value is the position of the response. For performance reasons,
|
||||||
* that property, and remains available for the lifetime of this
|
* each key is built only when {@link static::setIndex()} is called with
|
||||||
* collection.
|
* that property, and remains available for the lifetime of this collection.
|
||||||
|
*
|
||||||
|
* @var array<string,array<string,int>>
|
||||||
*/
|
*/
|
||||||
protected $responsesIndex = array();
|
protected $responsesIndex = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with all distinct properties across all
|
* An array with all distinct properties.
|
||||||
* {@link Response} objects. Created at the first call of
|
*
|
||||||
* {@link static::getPropertyMap()}.
|
* An array with all distinct properties across all {@link Response}
|
||||||
|
* objects. Created at the first call of {@link static::getPropertyMap()}.
|
||||||
|
*
|
||||||
|
* @var array<string,int[]>
|
||||||
*/
|
*/
|
||||||
protected $propertyMap = null;
|
protected $propertyMap = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int A pointer, as required by SeekableIterator.
|
* A pointer, as required by SeekableIterator.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $position = 0;
|
protected $position = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string|null Name of property to use as index. NULL when disabled.
|
* Name of property to use as index
|
||||||
|
*
|
||||||
|
* NULL when disabled.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
protected $index = null;
|
protected $index = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Criterias used by {@link compare()} to determine the order
|
* Compare criteria.
|
||||||
* between two respones. See {@link orderBy()} for a detailed
|
*
|
||||||
* description of this array's format.
|
* Used by {@link static::compare()} to determine the order between
|
||||||
|
* two responses. See {@link static::orderBy()} for a detailed description
|
||||||
|
* of this array's format.
|
||||||
|
*
|
||||||
|
* @var string[]|array<string,null|int|array<int|callable>>
|
||||||
*/
|
*/
|
||||||
protected $compareBy = array();
|
protected $compareBy = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new collection.
|
* Creates a new collection.
|
||||||
*
|
*
|
||||||
* @param array $responses An array of responses, in network order.
|
* @param Response[] $responses An array of responses, in network order.
|
||||||
*/
|
*/
|
||||||
public function __construct(array $responses)
|
public function __construct(array $responses)
|
||||||
{
|
{
|
||||||
@ -137,7 +165,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* @param int|string|null $offset The offset of the response to seek to.
|
* @param int|string|null $offset The offset of the response to seek to.
|
||||||
* If the offset is negative, seek to that relative to the end.
|
* If the offset is negative, seek to that relative to the end.
|
||||||
* If the collection is indexed, you can also supply a value to seek to.
|
* If the collection is indexed, you can also supply a value to seek to.
|
||||||
* Setting NULL will get the current response's interator.
|
* Setting NULL will get the current response's iterator.
|
||||||
*
|
*
|
||||||
* @return Response|ArrayObject The {@link Response} at the specified
|
* @return Response|ArrayObject The {@link Response} at the specified
|
||||||
* offset, the current response's iterator (which is an ArrayObject)
|
* offset, the current response's iterator (which is an ArrayObject)
|
||||||
@ -158,7 +186,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* that accept a position will then also be able to search values of
|
* that accept a position will then also be able to search values of
|
||||||
* that property for a matching value.
|
* that property for a matching value.
|
||||||
* Specifying NULL will disable such lookups (as is by default).
|
* Specifying NULL will disable such lookups (as is by default).
|
||||||
* Note that in case this value occures multiple times within the
|
* Note that in case this value occurs multiple times within the
|
||||||
* collection, only the last matching response will be accessible by
|
* collection, only the last matching response will be accessible by
|
||||||
* that value.
|
* that value.
|
||||||
*
|
*
|
||||||
@ -198,7 +226,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* @param bool $useIndex Whether to use the index values as keys for the
|
* @param bool $useIndex Whether to use the index values as keys for the
|
||||||
* resulting array.
|
* resulting array.
|
||||||
*
|
*
|
||||||
* @return array An array with all responses, in network order.
|
* @return Response[] An array with all responses, in network order.
|
||||||
*/
|
*/
|
||||||
public function toArray($useIndex = false)
|
public function toArray($useIndex = false)
|
||||||
{
|
{
|
||||||
@ -215,26 +243,13 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Counts the responses/words in the collection.
|
* Counts the responses in the collection.
|
||||||
*
|
|
||||||
* @param int $mode The counter mode.
|
|
||||||
* Either COUNT_NORMAL or COUNT_RECURSIVE.
|
|
||||||
* When in normal mode, counts the number of responses.
|
|
||||||
* When in recursive mode, counts the total number of API words.
|
|
||||||
*
|
*
|
||||||
* @return int The number of responses in the collection.
|
* @return int The number of responses in the collection.
|
||||||
*/
|
*/
|
||||||
public function count($mode = COUNT_NORMAL)
|
public function count()
|
||||||
{
|
{
|
||||||
if ($mode !== COUNT_NORMAL) {
|
return count($this->responses);
|
||||||
$result = 0;
|
|
||||||
foreach ($this->responses as $response) {
|
|
||||||
$result += $response->count($mode);
|
|
||||||
}
|
|
||||||
return $result;
|
|
||||||
} else {
|
|
||||||
return count($this->responses);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -280,6 +295,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* @param Response $value N/A
|
* @param Response $value N/A
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
*
|
||||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||||
*/
|
*/
|
||||||
public function offsetSet($offset, $value)
|
public function offsetSet($offset, $value)
|
||||||
@ -296,6 +312,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* @param int|string $offset N/A
|
* @param int|string $offset N/A
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
*
|
||||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||||
*/
|
*/
|
||||||
public function offsetUnset($offset)
|
public function offsetUnset($offset)
|
||||||
@ -306,8 +323,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Resets the pointer to 0, and returns the first response.
|
* Resets the pointer to 0, and returns the first response.
|
||||||
*
|
*
|
||||||
* @return Response The first response in the collection, or FALSE if the
|
* @return Response|false The first response in the collection,
|
||||||
* collection is empty.
|
* or FALSE if the collection is empty.
|
||||||
*/
|
*/
|
||||||
public function rewind()
|
public function rewind()
|
||||||
{
|
{
|
||||||
@ -321,8 +338,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* indexed, you can also supply a value to move the pointer to.
|
* indexed, you can also supply a value to move the pointer to.
|
||||||
* A non-existent index will move the pointer to "-1".
|
* A non-existent index will move the pointer to "-1".
|
||||||
*
|
*
|
||||||
* @return Response The {@link Response} at the specified position, or FALSE
|
* @return Response|false The {@link Response} at the specified position,
|
||||||
* if the specified position is not valid.
|
* or FALSE if the specified position is not valid.
|
||||||
*/
|
*/
|
||||||
public function seek($position)
|
public function seek($position)
|
||||||
{
|
{
|
||||||
@ -339,8 +356,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Moves the pointer forward by 1, and gets the next response.
|
* Moves the pointer forward by 1, and gets the next response.
|
||||||
*
|
*
|
||||||
* @return Response The next {@link Response} object, or FALSE if the
|
* @return Response|false The next {@link Response} object,
|
||||||
* position is not valid.
|
* or FALSE if the position is not valid.
|
||||||
*/
|
*/
|
||||||
public function next()
|
public function next()
|
||||||
{
|
{
|
||||||
@ -351,8 +368,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Gets the response at the current pointer position.
|
* Gets the response at the current pointer position.
|
||||||
*
|
*
|
||||||
* @return Response The response at the current pointer position, or FALSE
|
* @return Response|false The response at the current pointer position,
|
||||||
* if the position is not valid.
|
* or FALSE if the position is not valid.
|
||||||
*/
|
*/
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
@ -362,8 +379,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Moves the pointer backwards by 1, and gets the previous response.
|
* Moves the pointer backwards by 1, and gets the previous response.
|
||||||
*
|
*
|
||||||
* @return Response The next {@link Response} object, or FALSE if the
|
* @return Response|false The next {@link Response} object,
|
||||||
* position is not valid.
|
* or FALSE if the position is not valid.
|
||||||
*/
|
*/
|
||||||
public function prev()
|
public function prev()
|
||||||
{
|
{
|
||||||
@ -375,8 +392,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* Moves the pointer to the last valid position, and returns the last
|
* Moves the pointer to the last valid position, and returns the last
|
||||||
* response.
|
* response.
|
||||||
*
|
*
|
||||||
* @return Response The last response in the collection, or FALSE if the
|
* @return Response|false The last response in the collection,
|
||||||
* collection is empty.
|
* or FALSE if the collection is empty.
|
||||||
*/
|
*/
|
||||||
public function end()
|
public function end()
|
||||||
{
|
{
|
||||||
@ -387,8 +404,9 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Gets the key at the current pointer position.
|
* Gets the key at the current pointer position.
|
||||||
*
|
*
|
||||||
* @return int The key at the current pointer position, i.e. the pointer
|
* @return int|false The key at the current pointer position,
|
||||||
* position itself, or FALSE if the position is not valid.
|
* i.e. the pointer position itself, or FALSE if the position
|
||||||
|
* is not valid.
|
||||||
*/
|
*/
|
||||||
public function key()
|
public function key()
|
||||||
{
|
{
|
||||||
@ -410,8 +428,9 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
*
|
*
|
||||||
* Gets all distinct property names across all responses.
|
* Gets all distinct property names across all responses.
|
||||||
*
|
*
|
||||||
* @return array An array with all distinct property names as keys, and the
|
* @return array<string,int[]> An array with
|
||||||
* indexes at which they occur as values.
|
* all distinct property names as keys, and
|
||||||
|
* the indexes at which they occur as values.
|
||||||
*/
|
*/
|
||||||
public function getPropertyMap()
|
public function getPropertyMap()
|
||||||
{
|
{
|
||||||
@ -469,7 +488,8 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
/**
|
/**
|
||||||
* Order resones by criteria.
|
* Order resones by criteria.
|
||||||
*
|
*
|
||||||
* @param mixed[] $criteria The criteria to order respones by. It takes the
|
* @param string[]|array<string,null|int|array<int|callable>> $criteria The
|
||||||
|
* criteria to order responses by. It takes the
|
||||||
* form of an array where each key is the name of the property to use
|
* form of an array where each key is the name of the property to use
|
||||||
* as (N+1)th sorting key. The value of each member can be either NULL
|
* as (N+1)th sorting key. The value of each member can be either NULL
|
||||||
* (for that property, sort normally in ascending order), a single sort
|
* (for that property, sort normally in ascending order), a single sort
|
||||||
@ -479,7 +499,7 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
* array functions) or a callback.
|
* array functions) or a callback.
|
||||||
* If a callback is provided, it must accept two arguments
|
* If a callback is provided, it must accept two arguments
|
||||||
* (the two values to be compared), and return -1, 0 or 1 if the first
|
* (the two values to be compared), and return -1, 0 or 1 if the first
|
||||||
* value is respectively less than, equal to or greather than the second
|
* value is respectively less than, equal to or greater than the second
|
||||||
* one.
|
* one.
|
||||||
* Each key of $criteria can also be numeric, in which case the
|
* Each key of $criteria can also be numeric, in which case the
|
||||||
* value is the name of the property, and sorting is done normally in
|
* value is the name of the property, and sorting is done normally in
|
||||||
@ -517,15 +537,15 @@ class ResponseCollection implements ArrayAccess, SeekableIterator, Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares two respones.
|
* Compares two responses.
|
||||||
*
|
*
|
||||||
* Compares two respones, based on criteria defined in
|
* Compares two responses, based on criteria defined in
|
||||||
* {@link static::$compareBy}.
|
* {@link static::$compareBy}.
|
||||||
*
|
*
|
||||||
* @param Response $itemA The response to compare.
|
* @param Response $itemA The response to compare.
|
||||||
* @param Response $itemB The response to compare $a against.
|
* @param Response $itemB The response to compare $a against.
|
||||||
*
|
*
|
||||||
* @return int Returns 0 if the two respones are equal according to every
|
* @return int Returns 0 if the two responses are equal according to every
|
||||||
* criteria specified, -1 if $a should be placed before $b, and 1 if $b
|
* criteria specified, -1 if $a should be placed before $b, and 1 if $b
|
||||||
* should be placed before $a.
|
* should be placed before $a.
|
||||||
*/
|
*/
|
||||||
|
127
system/autoload/PEAR2/Net/RouterOS/RouterErrorException.php
Normal file
127
system/autoload/PEAR2/Net/RouterOS/RouterErrorException.php
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
|
*
|
||||||
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @copyright 2011 Vasil Rangelov
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @version 1.0.0b6
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* The namespace declaration.
|
||||||
|
*/
|
||||||
|
namespace PEAR2\Net\RouterOS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base of this class.
|
||||||
|
*/
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refered to in the constructor.
|
||||||
|
*/
|
||||||
|
use Exception as E;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown by higher level classes (Util, etc.) when the router
|
||||||
|
* returns an error.
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
class RouterErrorException extends RuntimeException implements Exception
|
||||||
|
{
|
||||||
|
const CODE_ITEM_ERROR = 0x100000;
|
||||||
|
const CODE_SCRIPT_ERROR = 0x200000;
|
||||||
|
const CODE_READ_ERROR = 0x010000;
|
||||||
|
const CODE_WRITE_ERROR = 0x020000;
|
||||||
|
const CODE_EXEC_ERROR = 0x040000;
|
||||||
|
|
||||||
|
const CODE_CACHE_ERROR = 0x100001;
|
||||||
|
const CODE_GET_ERROR = 0x110001;
|
||||||
|
const CODE_GETALL_ERROR = 0x110002;
|
||||||
|
const CODE_ADD_ERROR = 0x120001;
|
||||||
|
const CODE_SET_ERROR = 0x120002;
|
||||||
|
const CODE_REMOVE_ERROR = 0x120004;
|
||||||
|
const CODE_ENABLE_ERROR = 0x120012;
|
||||||
|
const CODE_DISABLE_ERROR = 0x120022;
|
||||||
|
const CODE_COMMENT_ERROR = 0x120042;
|
||||||
|
const CODE_UNSET_ERROR = 0x120082;
|
||||||
|
const CODE_MOVE_ERROR = 0x120107;
|
||||||
|
const CODE_SCRIPT_ADD_ERROR = 0x220001;
|
||||||
|
const CODE_SCRIPT_REMOVE_ERROR = 0x220004;
|
||||||
|
const CODE_SCRIPT_RUN_ERROR = 0x240001;
|
||||||
|
const CODE_SCRIPT_FILE_ERROR = 0x240003;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The complete response returned by the router.
|
||||||
|
*
|
||||||
|
* NULL when the router was not contacted at all.
|
||||||
|
*
|
||||||
|
* @var ResponseCollection|null
|
||||||
|
*/
|
||||||
|
private $_responses = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new RouterErrorException.
|
||||||
|
*
|
||||||
|
* @param string $message The Exception message to throw.
|
||||||
|
* @param int $code The Exception code.
|
||||||
|
* @param E|null $previous The previous exception used for
|
||||||
|
* the exception chaining.
|
||||||
|
* @param ResponseCollection|null $responses The complete set responses
|
||||||
|
* returned by the router.
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
$message,
|
||||||
|
$code = 0,
|
||||||
|
E $previous = null,
|
||||||
|
ResponseCollection $responses = null
|
||||||
|
) {
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
$this->_responses = $responses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the complete set responses returned by the router.
|
||||||
|
*
|
||||||
|
* @return ResponseCollection|null The complete set responses
|
||||||
|
* returned by the router.
|
||||||
|
*/
|
||||||
|
public function getResponses()
|
||||||
|
{
|
||||||
|
return $this->_responses;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
// String representation is not reliable in testing
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string representation of the exception.
|
||||||
|
*
|
||||||
|
* @return string The exception as a string.
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$result = parent::__toString();
|
||||||
|
if ($this->_responses instanceof ResponseCollection) {
|
||||||
|
$result .= "\nResponse collection:\n" .
|
||||||
|
print_r($this->_responses->toArray(), true);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
|
}
|
642
system/autoload/PEAR2/Net/RouterOS/Script.php
Normal file
642
system/autoload/PEAR2/Net/RouterOS/Script.php
Normal file
@ -0,0 +1,642 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
|
*
|
||||||
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @copyright 2011 Vasil Rangelov
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @version 1.0.0b6
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* The namespace declaration.
|
||||||
|
*/
|
||||||
|
namespace PEAR2\Net\RouterOS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Values at {@link Script::escapeValue()} can be casted from this type.
|
||||||
|
*/
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Values at {@link Script::escapeValue()} can be casted from this type.
|
||||||
|
*/
|
||||||
|
use DateInterval;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used at {@link Script::escapeValue()} to get the proper time.
|
||||||
|
*/
|
||||||
|
use DateTimeZone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to reliably write to streams at {@link Script::prepare()}.
|
||||||
|
*/
|
||||||
|
use PEAR2\Net\Transmitter\Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to catch DateTime and DateInterval exceptions at
|
||||||
|
* {@link Script::parseValue()}.
|
||||||
|
*/
|
||||||
|
use Exception as E;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scripting class.
|
||||||
|
*
|
||||||
|
* Provides functionality related to parsing and composing RouterOS scripts and
|
||||||
|
* values.
|
||||||
|
*
|
||||||
|
* @category Net
|
||||||
|
* @package PEAR2_Net_RouterOS
|
||||||
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
|
*/
|
||||||
|
class Script
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Parses a value from a RouterOS scripting context.
|
||||||
|
*
|
||||||
|
* Turns a value from RouterOS into an equivalent PHP value, based on
|
||||||
|
* determining the type in the same way RouterOS would determine it for a
|
||||||
|
* literal.
|
||||||
|
*
|
||||||
|
* This method is intended to be the very opposite of
|
||||||
|
* {@link static::escapeValue()}. That is, results from that method, if
|
||||||
|
* given to this method, should produce equivalent results.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed.
|
||||||
|
* Must be a literal of a value,
|
||||||
|
* e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
* @param DateTimeZone|null $timezone The timezone which any resulting
|
||||||
|
* DateTime object (either the main value, or values within an array)
|
||||||
|
* will use. Defaults to UTC.
|
||||||
|
*
|
||||||
|
* @return mixed Depending on RouterOS type detected:
|
||||||
|
* - "nil" (the string "[]") or "nothing" (empty string) - NULL.
|
||||||
|
* - "num" - int or double for large values.
|
||||||
|
* - "bool" - a boolean.
|
||||||
|
* - "array" - an array, with the keys and values processed recursively.
|
||||||
|
* - "time" - a {@link DateInterval} object.
|
||||||
|
* - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
|
||||||
|
* object with the specified date, at midnight.
|
||||||
|
* - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a
|
||||||
|
* DateTime object with the specified date and time.
|
||||||
|
* - "str" (a quoted string) - a string, with the contents escaped.
|
||||||
|
* - Unrecognized type - casted to a string, unmodified.
|
||||||
|
*/
|
||||||
|
public static function parseValue($value, DateTimeZone $timezone = null)
|
||||||
|
{
|
||||||
|
$value = static::parseValueToSimple($value);
|
||||||
|
if (!is_string($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return static::parseValueToArray($value, $timezone);
|
||||||
|
} catch (ParserException $e) {
|
||||||
|
try {
|
||||||
|
return static::parseValueToDateInterval($value);
|
||||||
|
} catch (ParserException $e) {
|
||||||
|
try {
|
||||||
|
return static::parseValueToDateTime($value, $timezone);
|
||||||
|
} catch (ParserException $e) {
|
||||||
|
return static::parseValueToString($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a RouterOS value into a PHP string.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed.
|
||||||
|
* Must be a literal of a value,
|
||||||
|
* e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
*
|
||||||
|
* @return string If a quoted string is provided, it would be parsed.
|
||||||
|
* Otherwise, the value is casted to a string, and returned unmodified.
|
||||||
|
*/
|
||||||
|
public static function parseValueToString($value)
|
||||||
|
{
|
||||||
|
$value = (string)$value;
|
||||||
|
if ('"' === $value[0] && '"' === $value[strlen($value) - 1]) {
|
||||||
|
return str_replace(
|
||||||
|
array('\"', '\\\\', "\\\n", "\\\r\n", "\\\r"),
|
||||||
|
array('"', '\\'),
|
||||||
|
substr($value, 1, -1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a RouterOS value into a PHP simple type.
|
||||||
|
*
|
||||||
|
* Parses a RouterOS value into a PHP simple type. "Simple" types being
|
||||||
|
* scalar types, plus NULL.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed. Must be a literal of a
|
||||||
|
* value, e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
*
|
||||||
|
* @return string|bool|int|double|null Depending on RouterOS type detected:
|
||||||
|
* - "nil" (the string "[]") or "nothing" (empty string) - NULL.
|
||||||
|
* - "num" - int or double for large values.
|
||||||
|
* - "bool" - a boolean.
|
||||||
|
* - Unrecognized type - casted to a string, unmodified.
|
||||||
|
*/
|
||||||
|
public static function parseValueToSimple($value)
|
||||||
|
{
|
||||||
|
$value = (string)$value;
|
||||||
|
|
||||||
|
if (in_array($value, array('', '[]'), true)) {
|
||||||
|
return null;
|
||||||
|
} elseif (in_array($value, array('true', 'false', 'yes', 'no'), true)) {
|
||||||
|
return $value === 'true' || $value === 'yes';
|
||||||
|
} elseif ($value === (string)($num = (int)$value)
|
||||||
|
|| $value === (string)($num = (double)$value)
|
||||||
|
) {
|
||||||
|
return $num;
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a RouterOS value into a PHP DateTime object
|
||||||
|
*
|
||||||
|
* Parses a RouterOS value into a PHP DateTime object.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed.
|
||||||
|
* Must be a literal of a value,
|
||||||
|
* e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
* @param DateTimeZone|null $timezone The timezone which the resulting
|
||||||
|
* DateTime object will use. Defaults to UTC.
|
||||||
|
*
|
||||||
|
* @return DateTime Depending on RouterOS type detected:
|
||||||
|
* - "date" (pseudo type; string in the form "M/j/Y") - a DateTime
|
||||||
|
* object with the specified date, at midnight UTC time (regardless
|
||||||
|
* of timezone provided).
|
||||||
|
* - "datetime" (pseudo type; string in the form "M/j/Y H:i:s") - a
|
||||||
|
* DateTime object with the specified date and time,
|
||||||
|
* with the specified timezone.
|
||||||
|
*
|
||||||
|
* @throws ParserException When the value is not of a recognized type.
|
||||||
|
*/
|
||||||
|
public static function parseValueToDateTime(
|
||||||
|
$value,
|
||||||
|
DateTimeZone $timezone = null
|
||||||
|
) {
|
||||||
|
$previous = null;
|
||||||
|
$value = (string)$value;
|
||||||
|
if ('' !== $value && preg_match(
|
||||||
|
'#^
|
||||||
|
(?<mon>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)
|
||||||
|
/
|
||||||
|
(?<day>\d\d?)
|
||||||
|
/
|
||||||
|
(?<year>\d{4})
|
||||||
|
(?:
|
||||||
|
\s+(?<time>\d{2}\:\d{2}:\d{2})
|
||||||
|
)?
|
||||||
|
$#uix',
|
||||||
|
$value,
|
||||||
|
$date
|
||||||
|
)) {
|
||||||
|
if (!isset($date['time'])) {
|
||||||
|
$date['time'] = '00:00:00';
|
||||||
|
$timezone = new DateTimeZone('UTC');
|
||||||
|
} elseif (null === $timezone) {
|
||||||
|
$timezone = new DateTimeZone('UTC');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new DateTime(
|
||||||
|
$date['year'] .
|
||||||
|
'-' . ucfirst($date['mon']) .
|
||||||
|
"-{$date['day']} {$date['time']}",
|
||||||
|
$timezone
|
||||||
|
);
|
||||||
|
} catch (E $e) {
|
||||||
|
$previous = $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new ParserException(
|
||||||
|
'The supplied value can not be converted to a DateTime',
|
||||||
|
ParserException::CODE_DATETIME,
|
||||||
|
$previous
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a RouterOS value into a PHP DateInterval.
|
||||||
|
*
|
||||||
|
* Parses a RouterOS value into a PHP DateInterval.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed. Must be a literal of a
|
||||||
|
* value, e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
*
|
||||||
|
* @return DateInterval The value as a DateInterval object.
|
||||||
|
*
|
||||||
|
* @throws ParserException When the value is not of a recognized type.
|
||||||
|
*/
|
||||||
|
public static function parseValueToDateInterval($value)
|
||||||
|
{
|
||||||
|
$value = (string)$value;
|
||||||
|
if ('' !== $value && preg_match(
|
||||||
|
'/^
|
||||||
|
(?:(\d+)w)?
|
||||||
|
(?:(\d+)d)?
|
||||||
|
(?:(\d+)(?:\:|h))?
|
||||||
|
(?|
|
||||||
|
(\d+)\:
|
||||||
|
(\d*(?:\.\d{1,9})?)
|
||||||
|
|
|
||||||
|
(?:(\d+)m)?
|
||||||
|
(?:(\d+|\d*\.\d{1,9})s)?
|
||||||
|
(?:((?5))ms)?
|
||||||
|
(?:((?5))us)?
|
||||||
|
(?:((?5))ns)?
|
||||||
|
)
|
||||||
|
$/x',
|
||||||
|
$value,
|
||||||
|
$time
|
||||||
|
)) {
|
||||||
|
$days = isset($time[2]) ? (int)$time[2] : 0;
|
||||||
|
if (isset($time[1])) {
|
||||||
|
$days += 7 * (int)$time[1];
|
||||||
|
}
|
||||||
|
if (empty($time[3])) {
|
||||||
|
$time[3] = 0;
|
||||||
|
}
|
||||||
|
if (empty($time[4])) {
|
||||||
|
$time[4] = 0;
|
||||||
|
}
|
||||||
|
if (empty($time[5])) {
|
||||||
|
$time[5] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$subsecondTime = 0.0;
|
||||||
|
//@codeCoverageIgnoreStart
|
||||||
|
// No PHP version currently supports sub-second DateIntervals,
|
||||||
|
// meaning this section is untestable, since no version constraints
|
||||||
|
// can be specified for test inputs.
|
||||||
|
// All inputs currently use integer seconds only, making this
|
||||||
|
// section unreachable during tests.
|
||||||
|
// Nevertheless, this section exists right now, in order to provide
|
||||||
|
// such support as soon as PHP has it.
|
||||||
|
if (!empty($time[6])) {
|
||||||
|
$subsecondTime += ((double)$time[6]) / 1000;
|
||||||
|
}
|
||||||
|
if (!empty($time[7])) {
|
||||||
|
$subsecondTime += ((double)$time[7]) / 1000000;
|
||||||
|
}
|
||||||
|
if (!empty($time[8])) {
|
||||||
|
$subsecondTime += ((double)$time[8]) / 1000000000;
|
||||||
|
}
|
||||||
|
//@codeCoverageIgnoreEnd
|
||||||
|
|
||||||
|
$secondsSpec = $time[5] + $subsecondTime;
|
||||||
|
try {
|
||||||
|
return new DateInterval(
|
||||||
|
"P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S"
|
||||||
|
);
|
||||||
|
//@codeCoverageIgnoreStart
|
||||||
|
// See previous ignored section's note.
|
||||||
|
//
|
||||||
|
// This section is added for backwards compatibility with current
|
||||||
|
// PHP versions, when in the future sub-second support is added.
|
||||||
|
// In that event, the test inputs for older versions will be
|
||||||
|
// expected to get a rounded up result of the sub-second data.
|
||||||
|
} catch (E $e) {
|
||||||
|
$secondsSpec = (int)round($secondsSpec);
|
||||||
|
return new DateInterval(
|
||||||
|
"P{$days}DT{$time[3]}H{$time[4]}M{$secondsSpec}S"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
//@codeCoverageIgnoreEnd
|
||||||
|
}
|
||||||
|
throw new ParserException(
|
||||||
|
'The supplied value can not be converted to DateInterval',
|
||||||
|
ParserException::CODE_DATEINTERVAL
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a RouterOS value into a PHP array.
|
||||||
|
*
|
||||||
|
* Parses a RouterOS value into a PHP array.
|
||||||
|
*
|
||||||
|
* @param string $value The value to be parsed.
|
||||||
|
* Must be a literal of a value,
|
||||||
|
* e.g. what {@link static::escapeValue()} will give you.
|
||||||
|
* @param DateTimeZone|null $timezone The timezone which any resulting
|
||||||
|
* DateTime object within the array will use. Defaults to UTC.
|
||||||
|
*
|
||||||
|
* @return array An array, with the keys and values processed recursively,
|
||||||
|
* the keys with {@link static::parseValueToSimple()},
|
||||||
|
* and the values with {@link static::parseValue()}.
|
||||||
|
*
|
||||||
|
* @throws ParserException When the value is not of a recognized type.
|
||||||
|
*/
|
||||||
|
public static function parseValueToArray(
|
||||||
|
$value,
|
||||||
|
DateTimeZone $timezone = null
|
||||||
|
) {
|
||||||
|
$value = (string)$value;
|
||||||
|
if ('{' === $value[0] && '}' === $value[strlen($value) - 1]) {
|
||||||
|
$value = substr($value, 1, -1);
|
||||||
|
if ('' === $value) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
$parsedValue = preg_split(
|
||||||
|
'/
|
||||||
|
(\"(?:\\\\\\\\|\\\\"|[^"])*\")
|
||||||
|
|
|
||||||
|
(\{[^{}]*(?2)?\})
|
||||||
|
|
|
||||||
|
([^;=]+)
|
||||||
|
/sx',
|
||||||
|
$value,
|
||||||
|
null,
|
||||||
|
PREG_SPLIT_DELIM_CAPTURE
|
||||||
|
);
|
||||||
|
$result = array();
|
||||||
|
$newVal = null;
|
||||||
|
$newKey = null;
|
||||||
|
for ($i = 0, $l = count($parsedValue); $i < $l; ++$i) {
|
||||||
|
switch ($parsedValue[$i]) {
|
||||||
|
case '':
|
||||||
|
break;
|
||||||
|
case ';':
|
||||||
|
if (null === $newKey) {
|
||||||
|
$result[] = $newVal;
|
||||||
|
} else {
|
||||||
|
$result[$newKey] = $newVal;
|
||||||
|
}
|
||||||
|
$newKey = $newVal = null;
|
||||||
|
break;
|
||||||
|
case '=':
|
||||||
|
$newKey = static::parseValueToSimple($parsedValue[$i - 1]);
|
||||||
|
$newVal = static::parseValue($parsedValue[++$i], $timezone);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$newVal = static::parseValue($parsedValue[$i], $timezone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null === $newKey) {
|
||||||
|
$result[] = $newVal;
|
||||||
|
} else {
|
||||||
|
$result[$newKey] = $newVal;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
throw new ParserException(
|
||||||
|
'The supplied value can not be converted to an array',
|
||||||
|
ParserException::CODE_ARRAY
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepares a script.
|
||||||
|
*
|
||||||
|
* Prepares a script for eventual execution by prepending parameters as
|
||||||
|
* variables to it.
|
||||||
|
*
|
||||||
|
* This is particularly useful when you're creating scripts that you don't
|
||||||
|
* want to execute right now (as with {@link Util::exec()}, but instead
|
||||||
|
* you want to store it for later execution, perhaps by supplying it to
|
||||||
|
* "/system scheduler".
|
||||||
|
*
|
||||||
|
* @param string|resource $source The source of the script,
|
||||||
|
* as a string or stream. If a stream is provided, reading starts from
|
||||||
|
* the current position to the end of the stream, and the pointer stays
|
||||||
|
* at the end after reading is done.
|
||||||
|
* @param array<string|int,mixed> $params An array of parameters to make
|
||||||
|
* available in the script as local variables.
|
||||||
|
* Variable names are array keys, and variable values are array values.
|
||||||
|
* Array values are automatically processed with
|
||||||
|
* {@link static::escapeValue()}. Streams are also supported, and are
|
||||||
|
* processed in chunks, each with
|
||||||
|
* {@link static::escapeString()} with all bytes being escaped.
|
||||||
|
* Processing starts from the current position to the end of the stream,
|
||||||
|
* and the stream's pointer is left untouched after the reading is done.
|
||||||
|
* Variables with a value of type "nothing" can be declared with a
|
||||||
|
* numeric array key and the variable name as the array value
|
||||||
|
* (that is casted to a string).
|
||||||
|
*
|
||||||
|
* @return resource A new PHP temporary stream with the script as contents,
|
||||||
|
* with the pointer back at the start.
|
||||||
|
*
|
||||||
|
* @see static::append()
|
||||||
|
*/
|
||||||
|
public static function prepare(
|
||||||
|
$source,
|
||||||
|
array $params = array()
|
||||||
|
) {
|
||||||
|
$resultStream = fopen('php://temp', 'r+b');
|
||||||
|
static::append($resultStream, $source, $params);
|
||||||
|
rewind($resultStream);
|
||||||
|
return $resultStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends a script.
|
||||||
|
*
|
||||||
|
* Appends a script to an existing stream.
|
||||||
|
*
|
||||||
|
* @param resource $stream An existing stream to write the
|
||||||
|
* resulting script to.
|
||||||
|
* @param string|resource $source The source of the script,
|
||||||
|
* as a string or stream. If a stream is provided, reading starts from
|
||||||
|
* the current position to the end of the stream, and the pointer stays
|
||||||
|
* at the end after reading is done.
|
||||||
|
* @param array<string|int,mixed> $params An array of parameters to make
|
||||||
|
* available in the script as local variables.
|
||||||
|
* Variable names are array keys, and variable values are array values.
|
||||||
|
* Array values are automatically processed with
|
||||||
|
* {@link static::escapeValue()}. Streams are also supported, and are
|
||||||
|
* processed in chunks, each with
|
||||||
|
* {@link static::escapeString()} with all bytes being escaped.
|
||||||
|
* Processing starts from the current position to the end of the stream,
|
||||||
|
* and the stream's pointer is left untouched after the reading is done.
|
||||||
|
* Variables with a value of type "nothing" can be declared with a
|
||||||
|
* numeric array key and the variable name as the array value
|
||||||
|
* (that is casted to a string).
|
||||||
|
*
|
||||||
|
* @return int The number of bytes written to $stream is returned,
|
||||||
|
* and the pointer remains where it was after the write
|
||||||
|
* (i.e. it is not seeked back, even if seeking is supported).
|
||||||
|
*/
|
||||||
|
public static function append(
|
||||||
|
$stream,
|
||||||
|
$source,
|
||||||
|
array $params = array()
|
||||||
|
) {
|
||||||
|
$writer = new Stream($stream, false);
|
||||||
|
$bytes = 0;
|
||||||
|
|
||||||
|
foreach ($params as $pname => $pvalue) {
|
||||||
|
if (is_int($pname)) {
|
||||||
|
$pvalue = static::escapeString((string)$pvalue);
|
||||||
|
$bytes += $writer->send(":local \"{$pvalue}\";\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$pname = static::escapeString($pname);
|
||||||
|
$bytes += $writer->send(":local \"{$pname}\" ");
|
||||||
|
if (Stream::isStream($pvalue)) {
|
||||||
|
$reader = new Stream($pvalue, false);
|
||||||
|
$chunkSize = $reader->getChunk(Stream::DIRECTION_RECEIVE);
|
||||||
|
$bytes += $writer->send('"');
|
||||||
|
while ($reader->isAvailable() && $reader->isDataAwaiting()) {
|
||||||
|
$bytes += $writer->send(
|
||||||
|
static::escapeString(fread($pvalue, $chunkSize), true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$bytes += $writer->send("\";\n");
|
||||||
|
} else {
|
||||||
|
$bytes += $writer->send(static::escapeValue($pvalue) . ";\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bytes += $writer->send($source);
|
||||||
|
return $bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escapes a value for a RouterOS scripting context.
|
||||||
|
*
|
||||||
|
* Turns any native PHP value into an equivalent whole value that can be
|
||||||
|
* inserted as part of a RouterOS script.
|
||||||
|
*
|
||||||
|
* DateInterval objects will be casted to RouterOS' "time" type.
|
||||||
|
*
|
||||||
|
* DateTime objects will be casted to a string following the "M/d/Y H:i:s"
|
||||||
|
* format. If the time is exactly midnight (including microseconds), and
|
||||||
|
* the timezone is UTC, the string will include only the "M/d/Y" date.
|
||||||
|
*
|
||||||
|
* Unrecognized types (i.e. resources and other objects) are casted to
|
||||||
|
* strings, and those strings are then escaped.
|
||||||
|
*
|
||||||
|
* @param mixed $value The value to be escaped.
|
||||||
|
*
|
||||||
|
* @return string A string representation that can be directly inserted in a
|
||||||
|
* script as a whole value.
|
||||||
|
*/
|
||||||
|
public static function escapeValue($value)
|
||||||
|
{
|
||||||
|
switch(gettype($value)) {
|
||||||
|
case 'NULL':
|
||||||
|
$value = '[]';
|
||||||
|
break;
|
||||||
|
case 'integer':
|
||||||
|
$value = (string)$value;
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
$value = $value ? 'true' : 'false';
|
||||||
|
break;
|
||||||
|
case 'array':
|
||||||
|
if (0 === count($value)) {
|
||||||
|
$value = '({})';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$result = '';
|
||||||
|
foreach ($value as $key => $val) {
|
||||||
|
$result .= ';';
|
||||||
|
if (!is_int($key)) {
|
||||||
|
$result .= static::escapeValue($key) . '=';
|
||||||
|
}
|
||||||
|
$result .= static::escapeValue($val);
|
||||||
|
}
|
||||||
|
$value = '{' . substr($result, 1) . '}';
|
||||||
|
break;
|
||||||
|
case 'object':
|
||||||
|
if ($value instanceof DateTime) {
|
||||||
|
$usec = $value->format('u');
|
||||||
|
$usec = '000000' === $usec ? '' : '.' . $usec;
|
||||||
|
$value = '00:00:00.000000 UTC' === $value->format('H:i:s.u e')
|
||||||
|
? $value->format('M/d/Y')
|
||||||
|
: $value->format('M/d/Y H:i:s') . $usec;
|
||||||
|
}
|
||||||
|
if ($value instanceof DateInterval) {
|
||||||
|
if (false === $value->days || $value->days < 0) {
|
||||||
|
$value = $value->format('%r%dd%H:%I:%S');
|
||||||
|
} else {
|
||||||
|
$value = $value->format('%r%ad%H:%I:%S');
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//break; intentionally omitted
|
||||||
|
default:
|
||||||
|
$value = '"' . static::escapeString((string)$value) . '"';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escapes a string for a RouterOS scripting context.
|
||||||
|
*
|
||||||
|
* Escapes a string for a RouterOS scripting context. The value can then be
|
||||||
|
* surrounded with quotes at a RouterOS script (or concatenated onto a
|
||||||
|
* larger string first), and you can be sure there won't be any code
|
||||||
|
* injections coming from it.
|
||||||
|
*
|
||||||
|
* By default, for the sake of brevity of the output, ASCII alphanumeric
|
||||||
|
* characters and underscores are left untouched. And for the sake of
|
||||||
|
* character conversion, bytes above 0x7F are also left untouched.
|
||||||
|
*
|
||||||
|
* @param string $value Value to be escaped.
|
||||||
|
* @param bool $full Whether to escape all bytes in the string, including
|
||||||
|
* ASCII alphanumeric characters, underscores and bytes above 0x7F.
|
||||||
|
*
|
||||||
|
* @return string The escaped value.
|
||||||
|
*
|
||||||
|
* @internal Why leave ONLY those ASCII characters and not also others?
|
||||||
|
* Because those can't in any way be mistaken for language constructs,
|
||||||
|
* unlike many other "safe inside strings, but not outside" ASCII
|
||||||
|
* characters, like ",", ".", "+", "-", "~", etc.
|
||||||
|
*/
|
||||||
|
public static function escapeString($value, $full = false)
|
||||||
|
{
|
||||||
|
if ($full) {
|
||||||
|
return self::_escapeCharacters(array($value));
|
||||||
|
}
|
||||||
|
return preg_replace_callback(
|
||||||
|
'/[^\\_A-Za-z0-9\\x80-\\xFF]+/S',
|
||||||
|
array(__CLASS__, '_escapeCharacters'),
|
||||||
|
$value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escapes a character for a RouterOS scripting context.
|
||||||
|
*
|
||||||
|
* Escapes a character for a RouterOS scripting context.
|
||||||
|
* Intended to only be called by {@link self::escapeString()} for the
|
||||||
|
* matching strings.
|
||||||
|
*
|
||||||
|
* @param array $chars The matches array, expected to contain exactly one
|
||||||
|
* member, in which is the whole string to be escaped.
|
||||||
|
*
|
||||||
|
* @return string The escaped characters.
|
||||||
|
*/
|
||||||
|
private static function _escapeCharacters(array $chars)
|
||||||
|
{
|
||||||
|
$result = '';
|
||||||
|
for ($i = 0, $l = strlen($chars[0]); $i < $l; ++$i) {
|
||||||
|
$result .= '\\' . str_pad(
|
||||||
|
strtoupper(dechex(ord($chars[0][$i]))),
|
||||||
|
2,
|
||||||
|
'0',
|
||||||
|
STR_PAD_LEFT
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* RouterOS API client implementation.
|
* RouterOS API client implementation.
|
||||||
|
|
||||||
*
|
*
|
||||||
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
* RouterOS is the flag product of the company MikroTik and is a powerful router software. One of its many abilities is to allow control over it via an API. This package provides a client for that API, in turn allowing you to use PHP to control RouterOS hosts.
|
||||||
*
|
*
|
||||||
@ -12,7 +13,7 @@
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0b5
|
* @version 1.0.0b6
|
||||||
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
* @link http://pear2.php.net/PEAR2_Net_RouterOS
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -20,8 +21,16 @@
|
|||||||
*/
|
*/
|
||||||
namespace PEAR2\Net\RouterOS;
|
namespace PEAR2\Net\RouterOS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base for this exception.
|
||||||
|
*/
|
||||||
use UnexpectedValueException as U;
|
use UnexpectedValueException as U;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in $previous.
|
||||||
|
*/
|
||||||
|
use Exception as E;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception thrown when encountering an invalid value in a function argument.
|
* Exception thrown when encountering an invalid value in a function argument.
|
||||||
*
|
*
|
||||||
@ -38,23 +47,25 @@ class UnexpectedValueException extends U implements Exception
|
|||||||
const CODE_RESPONSE_TYPE_UNKNOWN = 50100;
|
const CODE_RESPONSE_TYPE_UNKNOWN = 50100;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var mixed The unexpected value.
|
* The unexpected value.
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
private $_value;
|
private $_value;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new UnexpectedValueException.
|
* Creates a new UnexpectedValueException.
|
||||||
*
|
*
|
||||||
* @param string $message The Exception message to throw.
|
* @param string $message The Exception message to throw.
|
||||||
* @param int $code The Exception code.
|
* @param int $code The Exception code.
|
||||||
* @param \Exception $previous The previous exception used for the exception
|
* @param E|null $previous The previous exception used for the exception
|
||||||
* chaining.
|
* chaining.
|
||||||
* @param mixed $value The unexpected value.
|
* @param mixed $value The unexpected value.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$message,
|
$message,
|
||||||
$code = 0,
|
$code = 0,
|
||||||
$previous = null,
|
E $previous = null,
|
||||||
$value = null
|
$value = null
|
||||||
) {
|
) {
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -37,12 +38,16 @@ namespace PEAR2\Net\Transmitter;
|
|||||||
class FilterCollection implements \SeekableIterator, \Countable
|
class FilterCollection implements \SeekableIterator, \Countable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array The filter collection itself.
|
* The filter collection itself.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $filters = array();
|
protected $filters = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int A pointer, as required by SeekableIterator.
|
* A pointer, as required by SeekableIterator.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $position = 0;
|
protected $position = 0;
|
||||||
|
|
||||||
@ -177,7 +182,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
|||||||
/**
|
/**
|
||||||
* Gets the filter name at the current pointer position.
|
* Gets the filter name at the current pointer position.
|
||||||
*
|
*
|
||||||
* @return string The name of the filter at the current position.
|
* @return string|false The name of the filter at the current position.
|
||||||
*/
|
*/
|
||||||
public function key()
|
public function key()
|
||||||
{
|
{
|
||||||
@ -187,8 +192,8 @@ class FilterCollection implements \SeekableIterator, \Countable
|
|||||||
/**
|
/**
|
||||||
* Gets the filter parameters at the current pointer position.
|
* Gets the filter parameters at the current pointer position.
|
||||||
*
|
*
|
||||||
* @return array An array of parameters for the filter at the current
|
* @return array|false An array of parameters for the filter at the current
|
||||||
* position.
|
* position, or FALSE if the position is not valid.
|
||||||
*/
|
*/
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -25,7 +26,7 @@ namespace PEAR2\Net\Transmitter;
|
|||||||
/**
|
/**
|
||||||
* A network transmitter.
|
* A network transmitter.
|
||||||
*
|
*
|
||||||
* This is a convinience wrapper for network streams. Used to ensure data
|
* This is a convenience wrapper for network streams. Used to ensure data
|
||||||
* integrity.
|
* integrity.
|
||||||
*
|
*
|
||||||
* @category Net
|
* @category Net
|
||||||
@ -64,14 +65,19 @@ abstract class NetworkStream extends Stream
|
|||||||
const CRYPTO_TLS = 'TLS';
|
const CRYPTO_TLS = 'TLS';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The type of stream. Can be either "_CLIENT" or "_SERVER".
|
* The type of stream. Can be either "_CLIENT" or "_SERVER".
|
||||||
* Used to complement the encryption type. Must be set by child classes
|
*
|
||||||
* for {@link setCrypto()} to work properly.
|
* Used to complement the encryption type. Must be set by child classes
|
||||||
|
* for {@link setCrypto()} to work properly.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $streamType = '';
|
protected $streamType = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The current cryptography setting.
|
* The current cryptography setting.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $crypto = '';
|
protected $crypto = '';
|
||||||
|
|
||||||
@ -128,7 +134,7 @@ abstract class NetworkStream extends Stream
|
|||||||
*/
|
*/
|
||||||
public function isAvailable()
|
public function isAvailable()
|
||||||
{
|
{
|
||||||
if (parent::isStream($this->stream)) {
|
if ($this->isStream($this->stream)) {
|
||||||
if ($this->isBlocking && feof($this->stream)) {
|
if ($this->isBlocking && feof($this->stream)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -141,8 +147,8 @@ abstract class NetworkStream extends Stream
|
|||||||
/**
|
/**
|
||||||
* Sets the size of a stream's buffer.
|
* Sets the size of a stream's buffer.
|
||||||
*
|
*
|
||||||
* @param int $size The desired size of the buffer, in bytes.
|
* @param int $size The desired size of the buffer, in bytes.
|
||||||
* @param string $direction The buffer of which direction to set. Valid
|
* @param int $direction The buffer of which direction to set. Valid
|
||||||
* values are the DIRECTION_* constants.
|
* values are the DIRECTION_* constants.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
@ -163,7 +169,7 @@ abstract class NetworkStream extends Stream
|
|||||||
*
|
*
|
||||||
* Shutdowns (partially or not) a full-duplex connection.
|
* Shutdowns (partially or not) a full-duplex connection.
|
||||||
*
|
*
|
||||||
* @param string $direction The direction for which to disable further
|
* @param int $direction The direction for which to disable further
|
||||||
* communications.
|
* communications.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -40,12 +41,16 @@ class SocketException extends StreamException
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int The system level error code.
|
* The system level error code.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $errorNo;
|
protected $errorNo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The system level error message.
|
* The system level error message.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $errorStr;
|
protected $errorStr;
|
||||||
|
|
||||||
@ -62,7 +67,7 @@ class SocketException extends StreamException
|
|||||||
* successfully before the failure.
|
* successfully before the failure.
|
||||||
* On failure when receiving, this is a string/stream holding
|
* On failure when receiving, this is a string/stream holding
|
||||||
* the contents received successfully before the failure.
|
* the contents received successfully before the failure.
|
||||||
* NULL if the failure occured before the operation started.
|
* NULL if the failure occurred before the operation started.
|
||||||
* @param int $errorNo The system level error number.
|
* @param int $errorNo The system level error number.
|
||||||
* @param string $errorStr The system level
|
* @param string $errorStr The system level
|
||||||
* error message.
|
* error message.
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -27,7 +28,7 @@ use Exception as E;
|
|||||||
/**
|
/**
|
||||||
* A stream transmitter.
|
* A stream transmitter.
|
||||||
*
|
*
|
||||||
* This is a convinience wrapper for stream functionality. Used to ensure data
|
* This is a convenience wrapper for stream functionality. Used to ensure data
|
||||||
* integrity. Designed for TCP sockets, but it has intentionally been made to
|
* integrity. Designed for TCP sockets, but it has intentionally been made to
|
||||||
* accept any stream.
|
* accept any stream.
|
||||||
*
|
*
|
||||||
@ -57,32 +58,44 @@ class Stream
|
|||||||
const DIRECTION_ALL = 3;
|
const DIRECTION_ALL = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var resource The stream to wrap around.
|
* The stream to wrap around.
|
||||||
|
*
|
||||||
|
* @var resource
|
||||||
*/
|
*/
|
||||||
protected $stream;
|
protected $stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool Whether to automaticaly close the stream on
|
* Whether to automatically close the stream on object destruction if
|
||||||
* object destruction if it's not a persistent one. Setting this to
|
* it's not a persistent one.
|
||||||
* FALSE may be useful if you're only using this class "part time",
|
*
|
||||||
* while setting it to TRUE might be useful if you're doing some
|
* Setting this to FALSE may be useful if you're only using this class
|
||||||
* "on offs".
|
* "part time", while setting it to TRUE might be useful if you're doing
|
||||||
|
* some "one offs".
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $autoClose = false;
|
protected $autoClose = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool A flag that tells whether or not the stream is persistent.
|
* A flag that tells whether or not the stream is persistent.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $persist;
|
protected $persist;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool Whether the wrapped stream is in blocking mode or not.
|
* Whether the wrapped stream is in blocking mode or not.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $isBlocking = true;
|
protected $isBlocking = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An associative array with the chunk size of each direction.
|
* An associative array with the chunk size of each direction.
|
||||||
* Key is the direction, value is the size in bytes as integer.
|
*
|
||||||
|
* Key is the direction, value is the size in bytes as integer.
|
||||||
|
*
|
||||||
|
* @var array<int,int>
|
||||||
*/
|
*/
|
||||||
protected $chunkSize = array(
|
protected $chunkSize = array(
|
||||||
self::DIRECTION_SEND => 0xFFFFF, self::DIRECTION_RECEIVE => 0xFFFFF
|
self::DIRECTION_SEND => 0xFFFFF, self::DIRECTION_RECEIVE => 0xFFFFF
|
||||||
@ -92,7 +105,7 @@ class Stream
|
|||||||
* Wraps around the specified stream.
|
* Wraps around the specified stream.
|
||||||
*
|
*
|
||||||
* @param resource $stream The stream to wrap around.
|
* @param resource $stream The stream to wrap around.
|
||||||
* @param bool $autoClose Whether to automaticaly close the stream on
|
* @param bool $autoClose Whether to automatically close the stream on
|
||||||
* object destruction if it's not a persistent one. Setting this to
|
* object destruction if it's not a persistent one. Setting this to
|
||||||
* FALSE may be useful if you're only using this class "part time",
|
* FALSE may be useful if you're only using this class "part time",
|
||||||
* while setting it to TRUE might be useful if you're doing some
|
* while setting it to TRUE might be useful if you're doing some
|
||||||
@ -122,7 +135,9 @@ class Stream
|
|||||||
* @param string $message Message raised by PHP.
|
* @param string $message Message raised by PHP.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
|
*
|
||||||
* @throws SocketException That's how the error is handled.
|
* @throws SocketException That's how the error is handled.
|
||||||
|
*
|
||||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||||
*/
|
*/
|
||||||
protected function handleError($level, $message)
|
protected function handleError($level, $message)
|
||||||
@ -211,8 +226,8 @@ class Stream
|
|||||||
/**
|
/**
|
||||||
* Sets the size of a stream's buffer.
|
* Sets the size of a stream's buffer.
|
||||||
*
|
*
|
||||||
* @param int $size The desired size of the buffer, in bytes.
|
* @param int $size The desired size of the buffer, in bytes.
|
||||||
* @param string $direction The buffer of which direction to set. Valid
|
* @param int $direction The buffer of which direction to set. Valid
|
||||||
* values are the DIRECTION_* constants.
|
* values are the DIRECTION_* constants.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
@ -238,8 +253,8 @@ class Stream
|
|||||||
* consumption, data is sent/received in chunks. This function
|
* consumption, data is sent/received in chunks. This function
|
||||||
* allows you to set the size of each chunk. The default is 0xFFFFF.
|
* allows you to set the size of each chunk. The default is 0xFFFFF.
|
||||||
*
|
*
|
||||||
* @param int $size The desired size of the chunk, in bytes.
|
* @param int $size The desired size of the chunk, in bytes.
|
||||||
* @param string $direction The chunk of which direction to set. Valid
|
* @param int $direction The chunk of which direction to set. Valid
|
||||||
* values are the DIRECTION_* constants.
|
* values are the DIRECTION_* constants.
|
||||||
*
|
*
|
||||||
* @return bool TRUE on success, FALSE on failure.
|
* @return bool TRUE on success, FALSE on failure.
|
||||||
@ -266,10 +281,10 @@ class Stream
|
|||||||
/**
|
/**
|
||||||
* Gets the size of the chunk.
|
* Gets the size of the chunk.
|
||||||
*
|
*
|
||||||
* @param string $direction The chunk of which direction to get. Valid
|
* @param int $direction The chunk of which direction to get. Valid
|
||||||
* values are the DIRECTION_* constants.
|
* values are the DIRECTION_* constants.
|
||||||
*
|
*
|
||||||
* @return int|array|false The chunk size in bytes,
|
* @return int|array<int,int>|false The chunk size in bytes,
|
||||||
* or an array of chunk sizes with the directions as keys.
|
* or an array of chunk sizes with the directions as keys.
|
||||||
* FALSE on invalid direction.
|
* FALSE on invalid direction.
|
||||||
*/
|
*/
|
||||||
@ -318,21 +333,22 @@ class Stream
|
|||||||
) {
|
) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$bytesNow = @fwrite(
|
$contentsToSend = fread($contents, $chunkSize);
|
||||||
$this->stream,
|
if ('' != $contentsToSend) {
|
||||||
fread($contents, $chunkSize)
|
$bytesNow = @fwrite(
|
||||||
);
|
$this->stream,
|
||||||
if (0 != $bytesNow) {
|
$contentsToSend
|
||||||
$bytes += $bytesNow;
|
|
||||||
} elseif ($this->isBlocking || false === $bytesNow) {
|
|
||||||
throw $this->createException(
|
|
||||||
'Failed while sending stream.',
|
|
||||||
2,
|
|
||||||
null,
|
|
||||||
$bytes
|
|
||||||
);
|
);
|
||||||
} else {
|
if (0 != $bytesNow) {
|
||||||
usleep(300000);
|
$bytes += $bytesNow;
|
||||||
|
} elseif ($this->isBlocking || false === $bytesNow) {
|
||||||
|
throw $this->createException(
|
||||||
|
'Failed while sending stream.',
|
||||||
|
2,
|
||||||
|
null,
|
||||||
|
$bytes
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$this->isAcceptingData(null);
|
$this->isAcceptingData(null);
|
||||||
}
|
}
|
||||||
@ -364,8 +380,6 @@ class Stream
|
|||||||
null,
|
null,
|
||||||
$bytes
|
$bytes
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
usleep(300000);
|
|
||||||
}
|
}
|
||||||
$this->isAcceptingData(null);
|
$this->isAcceptingData(null);
|
||||||
}
|
}
|
||||||
@ -432,10 +446,10 @@ class Stream
|
|||||||
$result = fopen('php://temp', 'r+b');
|
$result = fopen('php://temp', 'r+b');
|
||||||
$appliedFilters = array();
|
$appliedFilters = array();
|
||||||
if (null !== $filters) {
|
if (null !== $filters) {
|
||||||
foreach ($filters as $filtername => $params) {
|
foreach ($filters as $filterName => $params) {
|
||||||
$appliedFilters[] = stream_filter_append(
|
$appliedFilters[] = stream_filter_append(
|
||||||
$result,
|
$result,
|
||||||
$filtername,
|
$filterName,
|
||||||
STREAM_FILTER_WRITE,
|
STREAM_FILTER_WRITE,
|
||||||
$params
|
$params
|
||||||
);
|
);
|
||||||
@ -491,12 +505,13 @@ class Stream
|
|||||||
/**
|
/**
|
||||||
* Checks whether there is data to be read from the wrapped stream.
|
* Checks whether there is data to be read from the wrapped stream.
|
||||||
*
|
*
|
||||||
* @param int|null $sTimeout If theere isn't data awaiting currently,
|
* @param int|null $sTimeout If there isn't data awaiting currently,
|
||||||
* wait for it this many seconds for data to arrive. If NULL is
|
* wait for it this many seconds for data to arrive. If NULL is
|
||||||
* specified, wait indefinetly for that.
|
* specified, wait indefinitely for that.
|
||||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
* @param int $usTimeout Microseconds to add to the waiting time.
|
||||||
*
|
*
|
||||||
* @return bool TRUE if there is data to be read, FALSE otherwise.
|
* @return bool TRUE if there is data to be read, FALSE otherwise.
|
||||||
|
*
|
||||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||||
*/
|
*/
|
||||||
public function isDataAwaiting($sTimeout = 0, $usTimeout = 0)
|
public function isDataAwaiting($sTimeout = 0, $usTimeout = 0)
|
||||||
@ -525,11 +540,12 @@ class Stream
|
|||||||
*
|
*
|
||||||
* @param int|null $sTimeout If the stream isn't currently accepting data,
|
* @param int|null $sTimeout If the stream isn't currently accepting data,
|
||||||
* wait for it this many seconds to start accepting data. If NULL is
|
* wait for it this many seconds to start accepting data. If NULL is
|
||||||
* specified, wait indefinetly for that.
|
* specified, wait indefinitely for that.
|
||||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
* @param int $usTimeout Microseconds to add to the waiting time.
|
||||||
*
|
*
|
||||||
* @return bool TRUE if the wrapped stream would not block on a write, FALSE
|
* @return bool TRUE if the wrapped stream would not block on a write,
|
||||||
* otherwise.
|
* FALSE otherwise.
|
||||||
|
*
|
||||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||||
*/
|
*/
|
||||||
public function isAcceptingData($sTimeout = 0, $usTimeout = 0)
|
public function isAcceptingData($sTimeout = 0, $usTimeout = 0)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -44,12 +45,15 @@ use Exception as E;
|
|||||||
class StreamException extends RuntimeException implements Exception
|
class StreamException extends RuntimeException implements Exception
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var int|string|resource|null The fragment up until the point of failure.
|
* The fragment up until the point of failure.
|
||||||
* On failure with sending, this is the number of bytes sent
|
*
|
||||||
* successfully before the failure.
|
* On failure with sending, this is the number of bytes sent successfully
|
||||||
* On failure when receiving, this is a string/stream holding
|
* before the failure.
|
||||||
* the contents received successfully before the failure.
|
* On failure when receiving, this is a string/stream holding the contents
|
||||||
* NULL if the failure occured before the operation started.
|
* received successfully before the failure.
|
||||||
|
* NULL if the failure occurred before the operation started.
|
||||||
|
*
|
||||||
|
* @var int|string|resource|null
|
||||||
*/
|
*/
|
||||||
protected $fragment = null;
|
protected $fragment = null;
|
||||||
|
|
||||||
@ -66,7 +70,7 @@ class StreamException extends RuntimeException implements Exception
|
|||||||
* successfully before the failure.
|
* successfully before the failure.
|
||||||
* On failure when receiving, this is a string/stream holding
|
* On failure when receiving, this is a string/stream holding
|
||||||
* the contents received successfully before the failure.
|
* the contents received successfully before the failure.
|
||||||
* NULL if the failure occured before the operation started.
|
* NULL if the failure occurred before the operation started.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
$message,
|
$message,
|
||||||
@ -87,7 +91,7 @@ class StreamException extends RuntimeException implements Exception
|
|||||||
* successfully before the failure.
|
* successfully before the failure.
|
||||||
* On failure when receiving, this is a string/stream holding
|
* On failure when receiving, this is a string/stream holding
|
||||||
* the contents received successfully before the failure.
|
* the contents received successfully before the failure.
|
||||||
* NULL if the failure occured before the operation started.
|
* NULL if the failure occurred before the operation started.
|
||||||
*/
|
*/
|
||||||
public function getFragment()
|
public function getFragment()
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -36,7 +37,7 @@ use Exception as E;
|
|||||||
/**
|
/**
|
||||||
* A socket transmitter.
|
* A socket transmitter.
|
||||||
*
|
*
|
||||||
* This is a convinience wrapper for socket functionality. Used to ensure data
|
* This is a convenience wrapper for socket functionality. Used to ensure data
|
||||||
* integrity.
|
* integrity.
|
||||||
*
|
*
|
||||||
* @category Net
|
* @category Net
|
||||||
@ -49,27 +50,41 @@ class TcpClient extends NetworkStream
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int The error code of the last error on the socket.
|
* The error code of the last error on the socket.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $errorNo = 0;
|
protected $errorNo = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The error message of the last error on the socket.
|
* The error message of the last error on the socket.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $errorStr = null;
|
protected $errorStr = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var SHM Persistent connection handler. Remains NULL for non-persistent
|
* Persistent connection handler.
|
||||||
* connections.
|
*
|
||||||
|
* Remains NULL for non-persistent connections.
|
||||||
|
*
|
||||||
|
* @var SHM
|
||||||
*/
|
*/
|
||||||
protected $shmHandler = null;
|
protected $shmHandler = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array An array with all connections from this PHP request (as keys)
|
* An array with all connections from this PHP request (as keys)
|
||||||
* and their lock state (as a value).
|
* and their lock state (as a value).
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $lockState = array();
|
protected static $lockState = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mappings from a protocol name to an URI scheme.
|
||||||
|
*
|
||||||
|
* @var array<string,string>
|
||||||
|
*/
|
||||||
protected static $cryptoScheme = array(
|
protected static $cryptoScheme = array(
|
||||||
parent::CRYPTO_OFF => 'tcp',
|
parent::CRYPTO_OFF => 'tcp',
|
||||||
parent::CRYPTO_SSL2 => 'sslv2',
|
parent::CRYPTO_SSL2 => 'sslv2',
|
||||||
@ -79,7 +94,9 @@ class TcpClient extends NetworkStream
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The URI of this connection.
|
* The URI of this connection.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $uri;
|
protected $uri;
|
||||||
|
|
||||||
@ -92,12 +109,12 @@ class TcpClient extends NetworkStream
|
|||||||
* persistent one.
|
* persistent one.
|
||||||
* @param float $timeout The timeout for the connection.
|
* @param float $timeout The timeout for the connection.
|
||||||
* @param string $key A string that uniquely identifies the
|
* @param string $key A string that uniquely identifies the
|
||||||
* connection.
|
* connection. Ignored for non-persistent connections.
|
||||||
* @param string $crypto Encryption setting. Must be one of the
|
* @param string $crypto Encryption setting. Must be one of the
|
||||||
* NetworkStream::CRYPTO_* constants. By default, encryption is
|
* NetworkStream::CRYPTO_* constants. By default, encryption is
|
||||||
* disabled. If the setting has an associated scheme for it, it will be
|
* disabled. If the setting has an associated scheme for it, it will be
|
||||||
* used, and if not, the setting will be adjusted right after the
|
* used, and if not, the setting will be adjusted right after the
|
||||||
* connection is estabilished.
|
* connection is established.
|
||||||
* @param resource $context A context for the socket.
|
* @param resource $context A context for the socket.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -114,16 +131,10 @@ class TcpClient extends NetworkStream
|
|||||||
if (strpos($host, ':') !== false) {
|
if (strpos($host, ':') !== false) {
|
||||||
$host = "[{$host}]";
|
$host = "[{$host}]";
|
||||||
}
|
}
|
||||||
$flags = STREAM_CLIENT_CONNECT;
|
|
||||||
if ($persist) {
|
|
||||||
$flags |= STREAM_CLIENT_PERSISTENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
$timeout
|
$timeout
|
||||||
= null == $timeout ? ini_get('default_socket_timeout') : $timeout;
|
= null == $timeout ? ini_get('default_socket_timeout') : $timeout;
|
||||||
|
|
||||||
$key = rawurlencode($key);
|
|
||||||
|
|
||||||
if (null === $context) {
|
if (null === $context) {
|
||||||
$context = stream_context_get_default();
|
$context = stream_context_get_default();
|
||||||
} elseif ((!is_resource($context))
|
} elseif ((!is_resource($context))
|
||||||
@ -133,7 +144,14 @@ class TcpClient extends NetworkStream
|
|||||||
}
|
}
|
||||||
$hasCryptoScheme = array_key_exists($crypto, static::$cryptoScheme);
|
$hasCryptoScheme = array_key_exists($crypto, static::$cryptoScheme);
|
||||||
$scheme = $hasCryptoScheme ? static::$cryptoScheme[$crypto] : 'tcp';
|
$scheme = $hasCryptoScheme ? static::$cryptoScheme[$crypto] : 'tcp';
|
||||||
$this->uri = "{$scheme}://{$host}:{$port}/{$key}";
|
$flags = STREAM_CLIENT_CONNECT;
|
||||||
|
if ($persist) {
|
||||||
|
$flags |= STREAM_CLIENT_PERSISTENT;
|
||||||
|
$key = rawurlencode($key);
|
||||||
|
$this->uri = "{$scheme}://{$host}:{$port}/{$key}";
|
||||||
|
} else {
|
||||||
|
$this->uri = "{$scheme}://{$host}:{$port}";
|
||||||
|
}
|
||||||
set_error_handler(array($this, 'handleError'));
|
set_error_handler(array($this, 'handleError'));
|
||||||
try {
|
try {
|
||||||
parent::__construct(
|
parent::__construct(
|
||||||
@ -168,7 +186,9 @@ class TcpClient extends NetworkStream
|
|||||||
} elseif (parent::CRYPTO_OFF !== $crypto) {
|
} elseif (parent::CRYPTO_OFF !== $crypto) {
|
||||||
$this->setCrypto($crypto);
|
$this->setCrypto($crypto);
|
||||||
}
|
}
|
||||||
$this->setIsBlocking(parent::CRYPTO_OFF === $crypto);
|
if (parent::CRYPTO_OFF !== $crypto) {
|
||||||
|
$this->setIsBlocking(false);
|
||||||
|
}
|
||||||
|
|
||||||
if ($persist) {
|
if ($persist) {
|
||||||
$this->shmHandler = SHM::factory(
|
$this->shmHandler = SHM::factory(
|
||||||
@ -218,15 +238,16 @@ class TcpClient extends NetworkStream
|
|||||||
* Locks transmission in one or more directions. Useful when dealing with
|
* Locks transmission in one or more directions. Useful when dealing with
|
||||||
* persistent connections. Note that every send/receive call implicitly
|
* persistent connections. Note that every send/receive call implicitly
|
||||||
* calls this function and then restores it to the previous state. You only
|
* calls this function and then restores it to the previous state. You only
|
||||||
* need to call this function if you need to do an uninterrputed sequence of
|
* need to call this function if you need to do an uninterrupted sequence of
|
||||||
* such calls.
|
* such calls.
|
||||||
*
|
*
|
||||||
* @param int $direction The direction(s) to have locked. Acceptable values
|
* @param int $direction The direction(s) to have locked. Acceptable values
|
||||||
* are the DIRECTION_* constants. If a lock for a direction can't be
|
* are the DIRECTION_* constants. If a lock for a direction can't be
|
||||||
* obtained immediatly, the function will block until one is aquired.
|
* obtained immediately, the function will block until one is acquired.
|
||||||
* Note that if you specify {@link DIRECTION_ALL}, the sending lock will
|
* Note that if you specify {@link static::DIRECTION_ALL},
|
||||||
* be obtained before the receiving one, and if obtaining the receiving
|
* the sending lock will be obtained before the receiving one,
|
||||||
* lock afterwards fails, the sending lock will be released too.
|
* and if obtaining the receiving lock afterwards fails,
|
||||||
|
* the sending lock will be released too.
|
||||||
* @param bool $replace Whether to replace all locks with the specified
|
* @param bool $replace Whether to replace all locks with the specified
|
||||||
* ones. Setting this to FALSE will make the function only obtain the
|
* ones. Setting this to FALSE will make the function only obtain the
|
||||||
* locks which are not already obtained.
|
* locks which are not already obtained.
|
||||||
@ -309,6 +330,7 @@ class TcpClient extends NetworkStream
|
|||||||
* the string/stream will be sent to its end.
|
* the string/stream will be sent to its end.
|
||||||
*
|
*
|
||||||
* @return int The number of bytes sent.
|
* @return int The number of bytes sent.
|
||||||
|
* @throws E
|
||||||
*/
|
*/
|
||||||
public function send($contents, $offset = null, $length = null)
|
public function send($contents, $offset = null, $length = null)
|
||||||
{
|
{
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for network stream functionality.
|
* Wrapper for network stream functionality.
|
||||||
|
|
||||||
*
|
*
|
||||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
|||||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||||
* @copyright 2011 Vasil Rangelov
|
* @copyright 2011 Vasil Rangelov
|
||||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||||
* @version 1.0.0a5
|
* @version 1.0.0b2
|
||||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
@ -27,7 +28,7 @@ use Exception as E;
|
|||||||
/**
|
/**
|
||||||
* A transmitter for connections to a socket server.
|
* A transmitter for connections to a socket server.
|
||||||
*
|
*
|
||||||
* This is a convinience wrapper for functionality of socket server connections.
|
* This is a convenience wrapper for functionality of socket server connections.
|
||||||
* Used to ensure data integrity. Server handling is not part of the class in
|
* Used to ensure data integrity. Server handling is not part of the class in
|
||||||
* order to allow its usage as part of various server implementations (e.g. fork
|
* order to allow its usage as part of various server implementations (e.g. fork
|
||||||
* and/or sequential).
|
* and/or sequential).
|
||||||
@ -42,21 +43,26 @@ class TcpServerConnection extends NetworkStream
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string The IP address of the connected client.
|
* The IP address of the connected client.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $peerIP;
|
protected $peerIP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int The port of the connected client.
|
* The port of the connected client.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $peerPort;
|
protected $peerPort;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new connection with the specified options.
|
* Creates a new connection with the specified options.
|
||||||
*
|
*
|
||||||
* @param resource $server A socket server, created with
|
* @param resource $server A socket server, created with
|
||||||
* {@link stream_socket_server()}.
|
* {@link stream_socket_server()}.
|
||||||
* @param float $timeout The timeout for the connection.
|
* @param float|null $timeout The timeout for the connection. Leaving this
|
||||||
|
* to NULL uses the default socket timeout.
|
||||||
*/
|
*/
|
||||||
public function __construct($server, $timeout = null)
|
public function __construct($server, $timeout = null)
|
||||||
{
|
{
|
||||||
@ -71,15 +77,15 @@ class TcpServerConnection extends NetworkStream
|
|||||||
set_error_handler(array($this, 'handleError'));
|
set_error_handler(array($this, 'handleError'));
|
||||||
try {
|
try {
|
||||||
parent::__construct(
|
parent::__construct(
|
||||||
stream_socket_accept($server, $timeout, $peername)
|
stream_socket_accept($server, $timeout, $peerName)
|
||||||
);
|
);
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
$portString = strrchr($peername, ':');
|
$portString = strrchr($peerName, ':');
|
||||||
$this->peerPort = (int) substr($portString, 1);
|
$this->peerPort = (int) substr($portString, 1);
|
||||||
$ipString = substr(
|
$ipString = substr(
|
||||||
$peername,
|
$peerName,
|
||||||
0,
|
0,
|
||||||
strlen($peername) - strlen($portString)
|
strlen($peerName) - strlen($portString)
|
||||||
);
|
);
|
||||||
if (strpos($ipString, '[') === 0
|
if (strpos($ipString, '[') === 0
|
||||||
&& strpos(strrev($ipString), ']') === 0
|
&& strpos(strrev($ipString), ']') === 0
|
||||||
@ -127,7 +133,7 @@ class TcpServerConnection extends NetworkStream
|
|||||||
* @param E|null $previous Previous exception thrown, or NULL if there
|
* @param E|null $previous Previous exception thrown, or NULL if there
|
||||||
* is none.
|
* is none.
|
||||||
* @param string|null $fragment The fragment up until the point of failure.
|
* @param string|null $fragment The fragment up until the point of failure.
|
||||||
* NULL if the failure occured before the operation started.
|
* NULL if the failure occurred before the operation started.
|
||||||
*
|
*
|
||||||
* @return SocketException The exception to then be thrown.
|
* @return SocketException The exception to then be thrown.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user