3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to prevent use of nicknames or URLs on a blacklist
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
35 * Plugin to prevent use of nicknames or URLs on a blacklist
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/
44 class BlacklistPlugin extends Plugin
46 const VERSION = STATUSNET_VERSION;
48 public $nicknames = array();
49 public $urls = array();
52 * Hook registration to prevent blacklisted homepages or nicknames
54 * Throws an exception if there's a blacklisted homepage or nickname.
56 * @param Action $action Action being called (usually register)
58 * @return boolean hook value
61 function onStartRegistrationTry($action)
63 $homepage = strtolower($action->trimmed('homepage'));
65 if (!empty($homepage)) {
66 if (!$this->_checkUrl($homepage)) {
67 $msg = sprintf(_m("You may not register with homepage '%s'"),
69 throw new ClientException($msg);
73 $nickname = strtolower($action->trimmed('nickname'));
75 if (!empty($nickname)) {
76 if (!$this->_checkNickname($nickname)) {
77 $msg = sprintf(_m("You may not register with nickname '%s'"),
79 throw new ClientException($msg);
87 * Hook profile update to prevent blacklisted homepages or nicknames
89 * Throws an exception if there's a blacklisted homepage or nickname.
91 * @param Action $action Action being called (usually register)
93 * @return boolean hook value
96 function onStartProfileSaveForm($action)
98 $homepage = strtolower($action->trimmed('homepage'));
100 if (!empty($homepage)) {
101 if (!$this->_checkUrl($homepage)) {
102 $msg = sprintf(_m("You may not use homepage '%s'"),
104 throw new ClientException($msg);
108 $nickname = strtolower($action->trimmed('nickname'));
110 if (!empty($nickname)) {
111 if (!$this->_checkNickname($nickname)) {
112 $msg = sprintf(_m("You may not use nickname '%s'"),
114 throw new ClientException($msg);
122 * Hook notice save to prevent blacklisted urls
124 * Throws an exception if there's a blacklisted url in the content.
126 * @param Notice &$notice Notice being saved
128 * @return boolean hook value
131 function onStartNoticeSave(&$notice)
133 common_replace_urls_callback($notice->content,
134 array($this, 'checkNoticeUrl'));
139 * Helper callback for notice save
141 * Throws an exception if there's a blacklisted url in the content.
143 * @param string $url URL in the notice content
145 * @return boolean hook value
148 function checkNoticeUrl($url)
150 // It comes in special'd, so we unspecial it
151 // before comparing against patterns
153 $url = htmlspecialchars_decode($url);
155 if (!$this->_checkUrl($url)) {
156 $msg = sprintf(_m("You may not use url '%s' in notices"),
158 throw new ClientException($msg);
165 * Helper for checking URLs
167 * Checks an URL against our patterns for a match.
169 * @param string $url URL to check
171 * @return boolean true means it's OK, false means it's bad
174 private function _checkUrl($url)
176 foreach ($this->urls as $pattern) {
177 if (preg_match("/$pattern/", $url)) {
186 * Helper for checking nicknames
188 * Checks a nickname against our patterns for a match.
190 * @param string $nickname nickname to check
192 * @return boolean true means it's OK, false means it's bad
195 private function _checkNickname($nickname)
197 foreach ($this->nicknames as $pattern) {
198 if (preg_match("/$pattern/", $nickname)) {
206 function onPluginVersion(&$versions)
208 $versions[] = array('name' => 'Blacklist',
209 'version' => self::VERSION,
210 'author' => 'Evan Prodromou',
211 'homepage' => 'http://status.net/wiki/Plugin:Blacklist',
213 _m('Keep a blacklist of forbidden nickname and URL patterns.'));