Files
.github
admin
install
pages_template
qrcode
system
autoload
PEAR2
Cache
Console
Net
RouterOS
Client.php
Communicator.php
DataFlowException.php
Exception.php
InvalidArgumentException.php
LengthException.php
Message.php
NotSupportedException.php
ParserException.php
Query.php
Registry.php
Request.php
Response.php
ResponseCollection.php
RouterErrorException.php
Script.php
SocketException.php
UnexpectedValueException.php
Util.php
Transmitter
Autoload.php
Admin.php
App.php
Balance.php
File.php
Hookers.php
Http.php
Lang.php
Log.php
Message.php
Mikrotik.php
Package.php
Paginator.php
Password.php
Radius.php
Timezone.php
User.php
Validator.php
index.html
cache
controllers
lan
paymentgateway
plugin
uploads
vendor
boot.php
composer.json
composer.lock
cron.php
cron_reminder.php
index.html
orm.php
updates.json
ui
.gitignore
CHANGELOG.md
LICENSE
README.md
composer.json
config.sample.php
favicon.ico
index.php
update.php
version.json
mitrobill/system/autoload/PEAR2/Net/RouterOS/Registry.php

290 lines
8.5 KiB
PHP
Raw Normal View History

2017-03-11 02:51:06 +07:00
<?php
/**
* RouterOS API client implementation.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* 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.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* PHP version 5
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @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
2023-10-05 16:55:44 +07:00
* @version 1.0.0b6
2017-03-11 02:51:06 +07:00
* @link http://pear2.php.net/PEAR2_Net_RouterOS
*/
/**
* The namespace declaration.
*/
namespace PEAR2\Net\RouterOS;
/**
* Uses shared memory to keep responses in.
*/
use PEAR2\Cache\SHM;
/**
* A RouterOS registry.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Provides functionality for managing the request/response flow. Particularly
* useful in persistent connections.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Note that this class is not meant to be called directly.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @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 Registry
{
/**
2023-10-05 16:55:44 +07:00
* The storage.
*
* @var SHM
2017-03-11 02:51:06 +07:00
*/
protected $shm;
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-05 16:55:44 +07:00
* ID of request. Populated at first instance in request.
*
* @var int
2017-03-11 02:51:06 +07:00
*/
protected static $requestId = -1;
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-05 16:55:44 +07:00
* ID to be given to next instance, after incrementing it.
*
* @var int
2017-03-11 02:51:06 +07:00
*/
protected static $instanceIdSeed = -1;
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-05 16:55:44 +07:00
* ID of instance within the request.
*
* @var int
2017-03-11 02:51:06 +07:00
*/
protected $instanceId;
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Creates a registry.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param string $uri An URI to bind the registry to.
*/
public function __construct($uri)
{
$this->shm = SHM::factory(__CLASS__ . ' ' . $uri);
if (-1 === self::$requestId) {
self::$requestId = $this->shm->add('requestId', 0)
? 0 : $this->shm->inc('requestId');
}
$this->instanceId = ++self::$instanceIdSeed;
$this->shm->add('responseBuffer_' . $this->getOwnershipTag(), array());
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Parses a tag.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Parses a tag to reveal the ownership part of it, and the original tag.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param string $tag The tag (as received) to parse.
2023-10-05 16:55:44 +07:00
*
* @return array<int,string|null> An array with
* the first member being the ownership tag, and
* the second one being the original tag.
2017-03-11 02:51:06 +07:00
*/
public static function parseTag($tag)
{
if (null === $tag) {
return array(null, null);
}
$result = explode('__', $tag, 2);
$result[0] .= '__';
if ('' === $result[1]) {
$result[1] = null;
}
return $result;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Checks if this instance is the tagless mode owner.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return bool TRUE if this instance is the tagless mode owner, FALSE
* otherwise.
*/
public function isTaglessModeOwner()
{
$this->shm->lock('taglessModeOwner');
$result = $this->shm->exists('taglessModeOwner')
&& $this->getOwnershipTag() === $this->shm->get('taglessModeOwner');
$this->shm->unlock('taglessModeOwner');
return $result;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Sets the "tagless mode" setting.
2023-10-05 16:55:44 +07:00
*
* While in tagless mode, this instance will claim ownership of any
2017-03-11 02:51:06 +07:00
* responses without a tag. While not in this mode, any requests without a
* tag will be given to all instances.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Regardless of mode, if the type of the response is
* {@link Response::TYPE_FATAL}, it will be given to all instances.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param bool $taglessMode TRUE to claim tagless ownership, FALSE to
* release such ownership, if taken.
2023-10-05 16:55:44 +07:00
*
* @return bool TRUE on success, FALSE on failure.
2017-03-11 02:51:06 +07:00
*/
public function setTaglessMode($taglessMode)
{
return $taglessMode
? ($this->shm->lock('taglessMode')
&& $this->shm->lock('taglessModeOwner')
&& $this->shm->add('taglessModeOwner', $this->getOwnershipTag())
&& $this->shm->unlock('taglessModeOwner'))
: ($this->isTaglessModeOwner()
&& $this->shm->lock('taglessModeOwner')
&& $this->shm->delete('taglessModeOwner')
&& $this->shm->unlock('taglessModeOwner')
&& $this->shm->unlock('taglessMode'));
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Get the ownership tag for this instance.
2023-10-05 16:55:44 +07:00
*
* @return string The ownership tag for this registry instance.
2017-03-11 02:51:06 +07:00
*/
public function getOwnershipTag()
{
return self::$requestId . '_' . $this->instanceId . '__';
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Add a response to the registry.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param Response $response The response to add. The caller of this
* function is responsible for ensuring that the ownership tag and the
* original tag are separated, so that only the original one remains in
* the response.
* @param string $ownershipTag The ownership tag that the response had.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return bool TRUE if the request was added to its buffer, FALSE if
* this instance owns the response, and therefore doesn't need to add
* the response to its buffer.
*/
public function add(Response $response, $ownershipTag)
{
if ($this->getOwnershipTag() === $ownershipTag
|| ($this->isTaglessModeOwner()
&& $response->getType() !== Response::TYPE_FATAL)
) {
return false;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
if (null === $ownershipTag) {
$this->shm->lock('taglessModeOwner');
if ($this->shm->exists('taglessModeOwner')
&& $response->getType() !== Response::TYPE_FATAL
) {
$ownershipTag = $this->shm->get('taglessModeOwner');
$this->shm->unlock('taglessModeOwner');
} else {
$this->shm->unlock('taglessModeOwner');
foreach ($this->shm->getIterator(
'/^(responseBuffer\_)/',
true
) as $targetBufferName) {
$this->_add($response, $targetBufferName);
}
return true;
}
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
$this->_add($response, 'responseBuffer_' . $ownershipTag);
return true;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Adds a response to a buffer.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param Response $response The response to add.
* @param string $targetBufferName The name of the buffer to add the
* response to.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return void
*/
private function _add(Response $response, $targetBufferName)
{
if ($this->shm->lock($targetBufferName)) {
$targetBuffer = $this->shm->get($targetBufferName);
$targetBuffer[] = $response;
$this->shm->set($targetBufferName, $targetBuffer);
$this->shm->unlock($targetBufferName);
}
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Gets the next response from this instance's buffer.
2023-10-05 16:55:44 +07:00
*
* @return Response|null The next response, or NULL if there isn't one.
2017-03-11 02:51:06 +07:00
*/
public function getNextResponse()
{
$response = null;
$targetBufferName = 'responseBuffer_' . $this->getOwnershipTag();
if ($this->shm->exists($targetBufferName)
&& $this->shm->lock($targetBufferName)
) {
$targetBuffer = $this->shm->get($targetBufferName);
if (!empty($targetBuffer)) {
$response = array_shift($targetBuffer);
$this->shm->set($targetBufferName, $targetBuffer);
}
$this->shm->unlock($targetBufferName);
}
return $response;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Closes the registry.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Closes the registry, meaning that all buffers are cleared.
2023-10-05 16:55:44 +07:00
*
* @return void
2017-03-11 02:51:06 +07:00
*/
public function close()
{
self::$requestId = -1;
self::$instanceIdSeed = -1;
$this->shm->clear();
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Removes a buffer.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param string $targetBufferName The buffer to remove.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return void
*/
private function _close($targetBufferName)
{
if ($this->shm->lock($targetBufferName)) {
$this->shm->delete($targetBufferName);
$this->shm->unlock($targetBufferName);
}
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-05 16:55:44 +07:00
* Removes this instance's buffer.
2017-03-11 02:51:06 +07:00
*/
public function __destruct()
{
$this->_close('responseBuffer_' . $this->getOwnershipTag());
}
}