root/trunk/Phergie/Event/Handler/AdminCommand.php @ 16

Revision 16, 2.3 KB (checked in by Seldaek, 5 years ago)

* change in AdminCommand? to allow plugins extending it to have their own .admins setting

Line 
1<?php
2
3/**
4* @see Phergie_Event_Handler
5*/
6require_once 'Phergie/Event/Handler.php';
7
8/**
9 * Handles admin rights
10 *
11 * Users are defined in the ini file in the AdminCommand.admins entry
12 * The syntax is a double-quoted, coma-separated list of hostmasks, i.e. :
13 * <code>AdminCommand.admins = "John!Doe@*.aol.com,Bob!*"</code>
14 *
15 * Each hostmask can comprise a nick, a user and a host part, structured as such :
16 *   nick!user@host
17 *
18 * A wildcard can be used to match anything,
19 * for example those are valid (but different) matches for : John!Doe@35.43.2.aol.com
20 *
21 * <code>
22 *   John*
23 *   *Doe@*.aol.com
24 *   John!*@*
25 *   *!*@35.43.2.aol.com
26 * </code>
27 */
28abstract class Phergie_Event_Handler_AdminCommand extends Phergie_Event_Handler
29{
30    protected $_admins = array();
31
32    public function isAdmin(Phergie_Event_Request $event, $__class__ = __CLASS__)
33    {
34        $class = substr($__class__, 22);
35        if ($this->_admins[$class] === null) {
36            $this->initialize($class);
37        }
38        $mask = $event->getNick() .'!'. $event->getUsername() .'@'. $event->getHost();
39        // Try to match mask against admin masks
40        foreach ($this->_admins[$class] as $admin) {
41            if (preg_match('{^'.$admin.'$}', $mask)) {
42                return true;
43            }
44        }
45        return false;
46    }
47
48    protected function initialize($class)
49    {
50        $ini = $this->getIni($class.'.admins');
51        if(empty($ini))
52            $ini = $this->getIni('AdminCommand.admins');
53        $admins = explode(',', $ini);
54        $this->_admins = array();
55        foreach ($admins as $admin) {
56            // Find out which chars are present in the config mask and exclude them from the regex match
57            $excluded = '';
58            if (strpos($admin, '!')!==false) {
59                $excluded .= '!';
60            }
61            if (strpos($admin, '@')!==false) {
62                $excluded .= '@';
63            }
64
65            // Escape regex meta characters
66            $admin = str_replace(
67                array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '+', '{', '}'),
68                array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'),
69                $admin
70            );
71            // Replace * so that they match correctly in a regex
72            $this->_admins[] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $admin);
73        }
74    }
75}
Note: See TracBrowser for help on using the browser.