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 2010 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();
51 private $_nicknamePatterns = array();
52 private $_urlPatterns = array();
56 $this->_nicknamePatterns = array_merge($this->nicknames,
57 $this->_configArray('blacklist', 'nicknames'));
59 $this->_urlPatterns = array_merge($this->urls,
60 $this->_configArray('blacklist', 'urls'));
63 function _configArray($section, $setting)
65 $config = common_config($section, $setting);
69 } else if (is_array($config)) {
71 } else if (is_string($config)) {
72 return explode("\t", $config);
74 throw new Exception("Unknown data type for config $section + $setting");
79 * Hook registration to prevent blacklisted homepages or nicknames
81 * Throws an exception if there's a blacklisted homepage or nickname.
83 * @param Action $action Action being called (usually register)
85 * @return boolean hook value
88 function onStartRegistrationTry($action)
90 $homepage = strtolower($action->trimmed('homepage'));
92 if (!empty($homepage)) {
93 if (!$this->_checkUrl($homepage)) {
94 $msg = sprintf(_m("You may not register with homepage '%s'"),
96 throw new ClientException($msg);
100 $nickname = strtolower($action->trimmed('nickname'));
102 if (!empty($nickname)) {
103 if (!$this->_checkNickname($nickname)) {
104 $msg = sprintf(_m("You may not register with nickname '%s'"),
106 throw new ClientException($msg);
114 * Hook profile update to prevent blacklisted homepages or nicknames
116 * Throws an exception if there's a blacklisted homepage or nickname.
118 * @param Action $action Action being called (usually register)
120 * @return boolean hook value
123 function onStartProfileSaveForm($action)
125 $homepage = strtolower($action->trimmed('homepage'));
127 if (!empty($homepage)) {
128 if (!$this->_checkUrl($homepage)) {
129 $msg = sprintf(_m("You may not use homepage '%s'"),
131 throw new ClientException($msg);
135 $nickname = strtolower($action->trimmed('nickname'));
137 if (!empty($nickname)) {
138 if (!$this->_checkNickname($nickname)) {
139 $msg = sprintf(_m("You may not use nickname '%s'"),
141 throw new ClientException($msg);
149 * Hook notice save to prevent blacklisted urls
151 * Throws an exception if there's a blacklisted url in the content.
153 * @param Notice &$notice Notice being saved
155 * @return boolean hook value
158 function onStartNoticeSave(&$notice)
160 common_replace_urls_callback($notice->content,
161 array($this, 'checkNoticeUrl'));
166 * Helper callback for notice save
168 * Throws an exception if there's a blacklisted url in the content.
170 * @param string $url URL in the notice content
172 * @return boolean hook value
175 function checkNoticeUrl($url)
177 // It comes in special'd, so we unspecial it
178 // before comparing against patterns
180 $url = htmlspecialchars_decode($url);
182 if (!$this->_checkUrl($url)) {
183 $msg = sprintf(_m("You may not use url '%s' in notices"),
185 throw new ClientException($msg);
192 * Helper for checking URLs
194 * Checks an URL against our patterns for a match.
196 * @param string $url URL to check
198 * @return boolean true means it's OK, false means it's bad
201 private function _checkUrl($url)
203 foreach ($this->_urlPatterns as $pattern) {
204 if (preg_match("/$pattern/", $url)) {
213 * Helper for checking nicknames
215 * Checks a nickname against our patterns for a match.
217 * @param string $nickname nickname to check
219 * @return boolean true means it's OK, false means it's bad
222 private function _checkNickname($nickname)
224 foreach ($this->_nicknamePatterns as $pattern) {
225 if (preg_match("/$pattern/", $nickname)) {
233 function onPluginVersion(&$versions)
235 $versions[] = array('name' => 'Blacklist',
236 'version' => self::VERSION,
237 'author' => 'Evan Prodromou',
238 'homepage' => 'http://status.net/wiki/Plugin:Blacklist',
240 _m('Keep a blacklist of forbidden nickname and URL patterns.'));