]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LogFilter/LogFilterPlugin.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / plugins / LogFilter / LogFilterPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Example to disable all debug messages and those containing 'About to push':
26  * addPlugin('LogFilter', array(
27  *    'priority' => array(LOG_DEBUG => false),
28  *    'regex' => array('/About to push/' => false)
29  * ));
30  *
31  * @todo add an admin panel
32  *
33  * @package LogFilterPlugin
34  * @maintainer Brion Vibber <brion@status.net>
35  */
36 class LogFilterPlugin extends Plugin
37 {
38     public $default = true;     // Set to false to require opting things in
39     public $priority = array(); // override by priority: array(LOG_ERR => true, LOG_DEBUG => false)
40     public $regex = array();    // override by regex match of message: array('/twitter/i' => false)
41
42     function onPluginVersion(&$versions)
43     {
44         $versions[] = array('name' => 'LogFilter',
45                             'version' => STATUSNET_VERSION,
46                             'author' => 'Brion Vibber',
47                             'homepage' => 'http://status.net/wiki/Plugin:LogFilter',
48                             'rawdescription' =>
49                             _m('Provides server-side setting to filter log output by type or keyword.'));
50
51         return true;
52     }
53
54     /**
55      * Hook for the StartLog event in common_log().
56      * If a message doesn't pass our filters, we'll abort it.
57      *
58      * @param string $priority
59      * @param string $msg
60      * @param string $filename
61      * @return boolean hook result code
62      */
63     function onStartLog(&$priority, &$msg, &$filename)
64     {
65         if ($this->filter($priority, $msg)) {
66             // Let it through
67             return true;
68         } else {
69             // Abort -- this line will go to /dev/null :)
70             return false;
71         }
72     }
73
74     /**
75      * Do the filtering...
76      *
77      * @param string $priority
78      * @param string $msg
79      * @return boolean true to let the log message be processed
80      */
81     function filter($priority, $msg)
82     {
83         $state = $this->default;
84         if (array_key_exists($priority, $this->priority)) {
85             $state = $this->priority[$priority];
86         }
87         foreach ($this->regex as $regex => $override) {
88             if (preg_match($regex, $msg)) {
89                 $state = $override;
90             }
91         }
92         return $state;
93     }
94 }