gnu-social/plugins/Irc/extlib/phergie/Phergie/Plugin/Acl.php

187 lines
6.0 KiB
PHP
Raw Normal View History

2010-06-16 01:55:39 +01:00
<?php
/**
2010-08-03 17:42:02 +01:00
* Phergie
2010-06-16 01:55:39 +01:00
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
2010-08-03 17:42:02 +01:00
* @category Phergie
2010-06-16 01:55:39 +01:00
* @package Phergie_Plugin_Acl
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Acl
*/
/**
2010-08-03 17:42:02 +01:00
* Provides an access control system to limit reponses to events based on
2010-06-16 01:55:39 +01:00
* the users who originate them.
*
2010-08-03 17:42:02 +01:00
* Configuration settings:
* acl.whitelist - mapping of user hostmask patterns (optionally by host) to
* plugins and methods where those plugins and methods will
* only be accessible to those users (i.e. and inaccessible
* to other users)
* acl.blacklist - mapping of user hostmasks (optionally by host) to plugins
* and methods where where those plugins and methods will be
* inaccessible to those users but accessible to other users
* acl.ops - TRUE to automatically give access to whitelisted plugins
* and methods to users with ops for events they initiate in
* channels where they have ops
*
* The whitelist and blacklist settings are formatted like so:
* <code>
* 'acl.whitelist' => array(
* 'hostname1' => array(
* 'pattern1' => array(
* 'plugins' => array(
* 'ShortPluginName'
* ),
* 'methods' => array(
* 'methodName'
* )
* ),
* )
* ),
* </code>
*
* The hostname array dimension is optional; if not used, rules will be
* applied across all connections. The pattern is a user hostmask pattern
* where asterisks (*) are used for wildcards. Plugins and methods do not
* need to be set to empty arrays if they are not used; simply exclude them.
*
* @category Phergie
2010-06-16 01:55:39 +01:00
* @package Phergie_Plugin_Acl
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Acl
2010-08-03 17:42:02 +01:00
* @uses Phergie_Plugin_UserInfo pear.phergie.org
2010-06-16 01:55:39 +01:00
*/
class Phergie_Plugin_Acl extends Phergie_Plugin_Abstract
{
/**
* Checks for permission settings and removes the plugin if none are set.
*
* @return void
*/
public function onLoad()
{
2010-08-03 17:42:02 +01:00
$this->plugins->getPlugin('UserInfo');
2010-06-16 01:55:39 +01:00
if (!$this->getConfig('acl.blacklist')
&& !$this->getConfig('acl.whitelist')
) {
$this->plugins->removePlugin($this);
}
}
/**
2010-08-03 17:42:02 +01:00
* Applies a set of rules to a plugin handler iterator.
*
* @param Phergie_Plugin_Iterator $iterator Iterator to receive rules
* @param array $rules Associate array containing
* either a 'plugins' key pointing to an array containing plugin
* short names to filter, a 'methods' key pointing to an array
* containing method names to filter, or both
*
* @return void
*/
protected function applyRules(Phergie_Plugin_Iterator $iterator, array $rules)
{
if (!empty($rules['plugins'])) {
$iterator->addPluginFilter($rules['plugins']);
}
if (!empty($rules['methods'])) {
$iterator->addMethodFilter($rules['methods']);
}
}
/**
* Checks permission settings and short-circuits event processing for
2010-06-16 01:55:39 +01:00
* blacklisted users.
*
2010-08-03 17:42:02 +01:00
* @return void
2010-06-16 01:55:39 +01:00
*/
public function preEvent()
{
// Ignore server responses
if ($this->event instanceof Phergie_Event_Response) {
2010-08-03 17:42:02 +01:00
return;
2010-06-16 01:55:39 +01:00
}
// Ignore server-initiated events
if (!$this->event->isFromUser()) {
2010-08-03 17:42:02 +01:00
return;
2010-06-16 01:55:39 +01:00
}
2010-08-03 17:42:02 +01:00
// Get the iterator used to filter plugins when processing events
$iterator = $this->plugins->getIterator();
// Get configuration setting values
$whitelist = $this->getConfig('acl.whitelist', array());
$blacklist = $this->getConfig('acl.blacklist', array());
$ops = $this->getConfig('acl.ops', false);
2010-06-16 01:55:39 +01:00
2010-08-03 17:42:02 +01:00
// Support host-specific lists
2010-06-16 01:55:39 +01:00
$host = $this->connection->getHost();
2010-08-03 17:42:02 +01:00
foreach (array('whitelist', 'blacklist') as $var) {
foreach ($$var as $pattern => $rules) {
$regex = '/^' . str_replace('*', '.*', $pattern) . '$/i';
if (preg_match($regex, $host)) {
${$var} = ${$var}[$pattern];
break;
}
}
2010-06-16 01:55:39 +01:00
}
2010-08-03 17:42:02 +01:00
// Get information on the user initiating the current event
2010-06-16 01:55:39 +01:00
$hostmask = $this->event->getHostmask();
2010-08-03 17:42:02 +01:00
$isOp = $ops
&& $this->event->isInChannel()
&& $this->plugins->userInfo->isOp(
$this->event->getNick(),
$this->event->getSource()
);
// Filter whitelisted commands if the user is not on the whitelist
if (!$isOp) {
$whitelisted = false;
foreach ($whitelist as $pattern => $rules) {
if ($hostmask->matches($pattern)) {
$whitelisted = true;
}
}
if (!$whitelisted) {
foreach ($whitelist as $pattern => $rules) {
$this->applyRules($iterator, $rules);
}
}
}
// Filter blacklisted commands if the user is on the blacklist
$blacklisted = false;
foreach ($blacklist as $pattern => $rules) {
2010-06-16 01:55:39 +01:00
if ($hostmask->matches($pattern)) {
2010-08-03 17:42:02 +01:00
$this->applyRules($iterator, $rules);
break;
2010-06-16 01:55:39 +01:00
}
}
2010-08-03 17:42:02 +01:00
}
2010-06-16 01:55:39 +01:00
2010-08-03 17:42:02 +01:00
/**
* Clears filters on the plugin handler iterator.
*
* @return void
*/
public function postDispatch()
{
$this->plugins->getIterator()->clearFilters();
2010-06-16 01:55:39 +01:00
}
}