]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/BlacklistPlugin.php
Mapstraction PluginVersion
[quix0rs-gnu-social.git] / plugins / Blacklist / BlacklistPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to prevent use of nicknames or URLs on a blacklist
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Action
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin to prevent use of nicknames or URLs on a blacklist
36  *
37  * @category Plugin
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class BlacklistPlugin extends Plugin
45 {
46     const VERSION = STATUSNET_VERSION;
47
48     public $nicknames = array();
49     public $urls      = array();
50
51     /**
52      * Hook registration to prevent blacklisted homepages or nicknames
53      *
54      * Throws an exception if there's a blacklisted homepage or nickname.
55      *
56      * @param Action $action Action being called (usually register)
57      *
58      * @return boolean hook value
59      */
60
61     function onStartRegistrationTry($action)
62     {
63         $homepage = strtolower($action->trimmed('homepage'));
64
65         if (!empty($homepage)) {
66             if (!$this->_checkUrl($homepage)) {
67                 $msg = sprintf(_m("You may not register with homepage '%s'"),
68                                $homepage);
69                 throw new ClientException($msg);
70             }
71         }
72
73         $nickname = strtolower($action->trimmed('nickname'));
74
75         if (!empty($nickname)) {
76             if (!$this->_checkNickname($nickname)) {
77                 $msg = sprintf(_m("You may not register with nickname '%s'"),
78                                $nickname);
79                 throw new ClientException($msg);
80             }
81         }
82
83         return true;
84     }
85
86     /**
87      * Hook profile update to prevent blacklisted homepages or nicknames
88      *
89      * Throws an exception if there's a blacklisted homepage or nickname.
90      *
91      * @param Action $action Action being called (usually register)
92      *
93      * @return boolean hook value
94      */
95
96     function onStartProfileSaveForm($action)
97     {
98         $homepage = strtolower($action->trimmed('homepage'));
99
100         if (!empty($homepage)) {
101             if (!$this->_checkUrl($homepage)) {
102                 $msg = sprintf(_m("You may not use homepage '%s'"),
103                                $homepage);
104                 throw new ClientException($msg);
105             }
106         }
107
108         $nickname = strtolower($action->trimmed('nickname'));
109
110         if (!empty($nickname)) {
111             if (!$this->_checkNickname($nickname)) {
112                 $msg = sprintf(_m("You may not use nickname '%s'"),
113                                $nickname);
114                 throw new ClientException($msg);
115             }
116         }
117
118         return true;
119     }
120
121     /**
122      * Hook notice save to prevent blacklisted urls
123      *
124      * Throws an exception if there's a blacklisted url in the content.
125      *
126      * @param Notice &$notice Notice being saved
127      *
128      * @return boolean hook value
129      */
130
131     function onStartNoticeSave(&$notice)
132     {
133         common_replace_urls_callback($notice->content,
134                                      array($this, 'checkNoticeUrl'));
135         return true;
136     }
137
138     /**
139      * Helper callback for notice save
140      *
141      * Throws an exception if there's a blacklisted url in the content.
142      *
143      * @param string $url URL in the notice content
144      *
145      * @return boolean hook value
146      */
147
148     function checkNoticeUrl($url)
149     {
150         // It comes in special'd, so we unspecial it
151         // before comparing against patterns
152
153         $url = htmlspecialchars_decode($url);
154
155         if (!$this->_checkUrl($url)) {
156             $msg = sprintf(_m("You may not use url '%s' in notices"),
157                            $url);
158             throw new ClientException($msg);
159         }
160
161         return $url;
162     }
163
164     /**
165      * Helper for checking URLs
166      *
167      * Checks an URL against our patterns for a match.
168      *
169      * @param string $url URL to check
170      *
171      * @return boolean true means it's OK, false means it's bad
172      */
173
174     private function _checkUrl($url)
175     {
176         foreach ($this->urls as $pattern) {
177             if (preg_match("/$pattern/", $url)) {
178                 return false;
179             }
180         }
181
182         return true;
183     }
184
185     /**
186      * Helper for checking nicknames
187      *
188      * Checks a nickname against our patterns for a match.
189      *
190      * @param string $nickname nickname to check
191      *
192      * @return boolean true means it's OK, false means it's bad
193      */
194
195     private function _checkNickname($nickname)
196     {
197         foreach ($this->nicknames as $pattern) {
198             if (preg_match("/$pattern/", $nickname)) {
199                 return false;
200             }
201         }
202
203         return true;
204     }
205
206     function onPluginVersion(&$versions)
207     {
208         $versions[] = array('name' => 'Blacklist',
209                             'version' => self::VERSION,
210                             'author' => 'Evan Prodromou',
211                             'homepage' => 'http://status.net/wiki/Plugin:Blacklist',
212                             'description' =>
213                             _m('Keep a blacklist of forbidden nickname and URL patterns.'));
214         return true;
215     }
216 }