summaryrefslogtreecommitdiff
path: root/plugins/LogFilter/LogFilterPlugin.php
blob: e2153e959c8f6d3b4c0331bd691679afdc4f2969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Example to disable all debug messages and those containing 'About to push':
 * addPlugin('LogFilter', array(
 *    'priority' => array(LOG_DEBUG => false),
 *    'regex' => array('/About to push/' => false)
 * ));
 *
 * @todo add an admin panel
 *
 * @package LogFilterPlugin
 * @maintainer Brion Vibber <brion@status.net>
 */
class LogFilterPlugin extends Plugin
{
    public $default = true;     // Set to false to require opting things in
    public $priority = array(); // override by priority: array(LOG_ERR => true, LOG_DEBUG => false)
    public $regex = array();    // override by regex match of message: array('/twitter/i' => false)

    function onPluginVersion(&$versions)
    {
        $versions[] = array('name' => 'LogFilter',
                            'version' => STATUSNET_VERSION,
                            'author' => 'Brion Vibber',
                            'homepage' => 'http://status.net/wiki/Plugin:LogFilter',
                            'rawdescription' =>
                            _m('Provides server-side setting to filter log output by type or keyword.'));

        return true;
    }

    /**
     * Hook for the StartLog event in common_log().
     * If a message doesn't pass our filters, we'll abort it.
     *
     * @param string $priority
     * @param string $msg
     * @param string $filename
     * @return boolean hook result code
     */
    function onStartLog(&$priority, &$msg, &$filename)
    {
        if ($this->filter($priority, $msg)) {
            // Let it through
            return true;
        } else {
            // Abort -- this line will go to /dev/null :)
            return false;
        }
    }

    /**
     * Do the filtering...
     *
     * @param string $priority
     * @param string $msg
     * @return boolean true to let the log message be processed
     */
    function filter($priority, $msg)
    {
        $state = $this->default;
        if (array_key_exists($priority, $this->priority)) {
            $state = $this->priority[$priority];
        }
        foreach ($this->regex as $regex => $override) {
            if (preg_match($regex, $msg)) {
                $state = $override;
            }
        }
        return $state;
    }
}