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 public $nicknames = array();
47 public $urls = array();
50 * Hook registration to prevent blacklisted homepages or nicknames
52 * Throws an exception if there's a blacklisted homepage or nickname.
54 * @param Action $action Action being called (usually register)
56 * @return boolean hook value
59 function onStartRegistrationTry($action)
61 $homepage = strtolower($action->trimmed('homepage'));
63 if (!empty($homepage)) {
64 if (!$this->_checkUrl($homepage)) {
65 $msg = sprintf(_m("You may not register with homepage '%s'"),
67 throw new ClientException($msg);
71 $nickname = strtolower($action->trimmed('nickname'));
73 if (!empty($nickname)) {
74 if (!$this->_checkNickname($nickname)) {
75 $msg = sprintf(_m("You may not register with nickname '%s'"),
77 throw new ClientException($msg);
85 * Hook profile update to prevent blacklisted homepages or nicknames
87 * Throws an exception if there's a blacklisted homepage or nickname.
89 * @param Action $action Action being called (usually register)
91 * @return boolean hook value
94 function onStartProfileSaveForm($action)
96 $homepage = strtolower($action->trimmed('homepage'));
98 if (!empty($homepage)) {
99 if (!$this->_checkUrl($homepage)) {
100 $msg = sprintf(_m("You may not use homepage '%s'"),
102 throw new ClientException($msg);
106 $nickname = strtolower($action->trimmed('nickname'));
108 if (!empty($nickname)) {
109 if (!$this->_checkNickname($nickname)) {
110 $msg = sprintf(_m("You may not use nickname '%s'"),
112 throw new ClientException($msg);
120 * Hook notice save to prevent blacklisted urls
122 * Throws an exception if there's a blacklisted url in the content.
124 * @param Notice &$notice Notice being saved
126 * @return boolean hook value
129 function onStartNoticeSave(&$notice)
131 common_replace_urls_callback($notice->content,
132 array($this, 'checkNoticeUrl'));
137 * Helper callback for notice save
139 * Throws an exception if there's a blacklisted url in the content.
141 * @param string $url URL in the notice content
143 * @return boolean hook value
146 function checkNoticeUrl($url)
148 // It comes in special'd, so we unspecial it
149 // before comparing against patterns
151 $url = htmlspecialchars_decode($url);
153 if (!$this->_checkUrl($url)) {
154 $msg = sprintf(_m("You may not use url '%s' in notices"),
156 throw new ClientException($msg);
163 * Helper for checking URLs
165 * Checks an URL against our patterns for a match.
167 * @param string $url URL to check
169 * @return boolean true means it's OK, false means it's bad
172 private function _checkUrl($url)
174 foreach ($this->urls as $pattern) {
175 if (preg_match("/$pattern/", $url)) {
184 * Helper for checking nicknames
186 * Checks a nickname against our patterns for a match.
188 * @param string $nickname nickname to check
190 * @return boolean true means it's OK, false means it's bad
193 private function _checkNickname($nickname)
195 foreach ($this->nicknames as $pattern) {
196 if (preg_match("/$pattern/", $nickname)) {