]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LogFilter/LogFilterPlugin.php
Plugins didn't match lib/plugin.php onPluginVersion function definition
[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(array &$versions)
43     {
44         $versions[] = array('name' => 'LogFilter',
45                             'version' => GNUSOCIAL_VERSION,
46                             'author' => 'Brion Vibber',
47                             'homepage' => 'http://status.net/wiki/Plugin:LogFilter',
48                             'rawdescription' =>
49                             // TRANS: Plugin description.
50                             _m('Provides server-side setting to filter log output by type or keyword.'));
51
52         return true;
53     }
54
55     /**
56      * Hook for the StartLog event in common_log().
57      * If a message doesn't pass our filters, we'll abort it.
58      *
59      * @param string $priority
60      * @param string $msg
61      * @param string $filename
62      * @return boolean hook result code
63      */
64     function onStartLog(&$priority, &$msg, &$filename)
65     {
66         if ($this->filter($priority, $msg)) {
67             // Let it through
68             return true;
69         } else {
70             // Abort -- this line will go to /dev/null :)
71             return false;
72         }
73     }
74
75     /**
76      * Do the filtering...
77      *
78      * @param string $priority
79      * @param string $msg
80      * @return boolean true to let the log message be processed
81      */
82     function filter($priority, $msg)
83     {
84         $state = $this->default;
85         if (array_key_exists($priority, $this->priority)) {
86             $state = $this->priority[$priority];
87         }
88         foreach ($this->regex as $regex => $override) {
89             if (preg_match($regex, $msg)) {
90                 $state = $override;
91             }
92         }
93         return $state;
94     }
95 }