]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/BlacklistPlugin.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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     public $nicknames = array();
47     public $urls      = array();
48
49     /**
50      * Hook registration to prevent blacklisted homepages or nicknames
51      *
52      * Throws an exception if there's a blacklisted homepage or nickname.
53      *
54      * @param Action $action Action being called (usually register)
55      *
56      * @return boolean hook value
57      */
58
59     function onStartRegistrationTry($action)
60     {
61         $homepage = strtolower($action->trimmed('homepage'));
62
63         if (!empty($homepage)) {
64             if (!$this->_checkUrl($homepage)) {
65                 $msg = sprintf(_m("You may not register with homepage '%s'"),
66                                $homepage);
67                 throw new ClientException($msg);
68             }
69         }
70
71         $nickname = strtolower($action->trimmed('nickname'));
72
73         if (!empty($nickname)) {
74             if (!$this->_checkNickname($nickname)) {
75                 $msg = sprintf(_m("You may not register with nickname '%s'"),
76                                $nickname);
77                 throw new ClientException($msg);
78             }
79         }
80
81         return true;
82     }
83
84     /**
85      * Hook profile update to prevent blacklisted homepages or nicknames
86      *
87      * Throws an exception if there's a blacklisted homepage or nickname.
88      *
89      * @param Action $action Action being called (usually register)
90      *
91      * @return boolean hook value
92      */
93
94     function onStartProfileSaveForm($action)
95     {
96         $homepage = strtolower($action->trimmed('homepage'));
97
98         if (!empty($homepage)) {
99             if (!$this->_checkUrl($homepage)) {
100                 $msg = sprintf(_m("You may not use homepage '%s'"),
101                                $homepage);
102                 throw new ClientException($msg);
103             }
104         }
105
106         $nickname = strtolower($action->trimmed('nickname'));
107
108         if (!empty($nickname)) {
109             if (!$this->_checkNickname($nickname)) {
110                 $msg = sprintf(_m("You may not use nickname '%s'"),
111                                $nickname);
112                 throw new ClientException($msg);
113             }
114         }
115
116         return true;
117     }
118
119     /**
120      * Hook notice save to prevent blacklisted urls
121      *
122      * Throws an exception if there's a blacklisted url in the content.
123      *
124      * @param Notice &$notice Notice being saved
125      *
126      * @return boolean hook value
127      */
128
129     function onStartNoticeSave(&$notice)
130     {
131         common_replace_urls_callback($notice->content,
132                                      array($this, 'checkNoticeUrl'));
133         return true;
134     }
135
136     /**
137      * Helper callback for notice save
138      *
139      * Throws an exception if there's a blacklisted url in the content.
140      *
141      * @param string $url URL in the notice content
142      *
143      * @return boolean hook value
144      */
145
146     function checkNoticeUrl($url)
147     {
148         // It comes in special'd, so we unspecial it
149         // before comparing against patterns
150
151         $url = htmlspecialchars_decode($url);
152
153         if (!$this->_checkUrl($url)) {
154             $msg = sprintf(_m("You may not use url '%s' in notices"),
155                            $url);
156             throw new ClientException($msg);
157         }
158
159         return $url;
160     }
161
162     /**
163      * Helper for checking URLs
164      *
165      * Checks an URL against our patterns for a match.
166      *
167      * @param string $url URL to check
168      *
169      * @return boolean true means it's OK, false means it's bad
170      */
171
172     private function _checkUrl($url)
173     {
174         foreach ($this->urls as $pattern) {
175             if (preg_match("/$pattern/", $url)) {
176                 return false;
177             }
178         }
179
180         return true;
181     }
182
183     /**
184      * Helper for checking nicknames
185      *
186      * Checks a nickname against our patterns for a match.
187      *
188      * @param string $nickname nickname to check
189      *
190      * @return boolean true means it's OK, false means it's bad
191      */
192
193     private function _checkNickname($nickname)
194     {
195         foreach ($this->nicknames as $pattern) {
196             if (preg_match("/$pattern/", $nickname)) {
197                 return false;
198             }
199         }
200
201         return true;
202     }
203 }