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

Revision 11, 2.1 KB (checked in by Seldaek, 5 years ago)

fixes #7
* bugfix in AdminCommand?
* added some comments in DNSLookup

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 = null;
31
32    public function isAdmin(Phergie_Event_Request $event)
33    {
34        if ($this->_admins === null) {
35            $this->initialize();
36        }
37        $mask = $event->getNick() .'!'. $event->getUsername() .'@'. $event->getHost();
38        // Try to match mask against admin masks
39        foreach ($this->_admins as $admin) {
40            if (preg_match('{^'.$admin.'$}', $mask)) {
41                return true;
42            }
43        }
44        return false;
45    }
46
47    protected function initialize()
48    {
49        $admins = explode(',', $this->getIni('AdminCommand.admins'));
50        $this->_admins = array();
51        foreach ($admins as $admin) {
52            // Find out which chars are present in the config mask and exclude them from the regex match
53            $excluded = '';
54            if (strpos($admin, '!')!==false) {
55                $excluded .= '!';
56            }
57            if (strpos($admin, '@')!==false) {
58                $excluded .= '@';
59            }
60
61            // Escape regex meta characters
62            $admin = str_replace(
63                array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '+', '{', '}'),
64                array('\\\\', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\+', '\\{', '\\}'),
65                $admin
66            );
67            // Replace * so that they match correctly in a regex
68            $this->_admins[] = str_replace('*', ($excluded === '' ? '.*' : '[^'.$excluded.']*'), $admin);
69        }
70    }
71}
Note: See TracBrowser for help on using the browser.