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/
43 class BlacklistPlugin extends Plugin
45 const VERSION = STATUSNET_VERSION;
47 public $nicknames = array();
48 public $urls = array();
49 public $canAdmin = true;
51 function _getNicknamePatterns()
53 $confNicknames = $this->_configArray('blacklist', 'nicknames');
55 $dbNicknames = Nickname_blacklist::getPatterns();
57 return array_merge($this->nicknames,
62 function _getUrlPatterns()
64 $confURLs = $this->_configArray('blacklist', 'urls');
66 $dbURLs = Homepage_blacklist::getPatterns();
68 return array_merge($this->urls,
74 * Database schema setup
76 * @return boolean hook value
78 function onCheckSchema()
80 $schema = Schema::get();
82 // For storing blacklist patterns for nicknames
83 $schema->ensureTable('nickname_blacklist',
84 array(new ColumnDef('pattern',
89 new ColumnDef('created',
94 $schema->ensureTable('homepage_blacklist',
95 array(new ColumnDef('pattern',
100 new ColumnDef('created',
109 * Retrieve an array from configuration
111 * Carefully checks a section.
113 * @param string $section Configuration section
114 * @param string $setting Configuration setting
116 * @return array configuration values
118 function _configArray($section, $setting)
120 $config = common_config($section, $setting);
122 if (empty($config)) {
124 } else if (is_array($config)) {
126 } else if (is_string($config)) {
127 return explode("\r\n", $config);
129 throw new Exception("Unknown data type for config $section + $setting");
134 * Hook registration to prevent blacklisted homepages or nicknames
136 * Throws an exception if there's a blacklisted homepage or nickname.
138 * @param Action $action Action being called (usually register)
140 * @return boolean hook value
142 function onStartRegistrationTry($action)
144 $homepage = strtolower($action->trimmed('homepage'));
146 if (!empty($homepage)) {
147 if (!$this->_checkUrl($homepage)) {
148 $msg = sprintf(_m("You may not register with homepage '%s'."),
150 throw new ClientException($msg);
154 $nickname = strtolower($action->trimmed('nickname'));
156 if (!empty($nickname)) {
157 if (!$this->_checkNickname($nickname)) {
158 $msg = sprintf(_m("You may not register with nickname '%s'."),
160 throw new ClientException($msg);
168 * Hook profile update to prevent blacklisted homepages or nicknames
170 * Throws an exception if there's a blacklisted homepage or nickname.
172 * @param Action $action Action being called (usually register)
174 * @return boolean hook value
176 function onStartProfileSaveForm($action)
178 $homepage = strtolower($action->trimmed('homepage'));
180 if (!empty($homepage)) {
181 if (!$this->_checkUrl($homepage)) {
182 $msg = sprintf(_m("You may not use homepage '%s'."),
184 throw new ClientException($msg);
188 $nickname = strtolower($action->trimmed('nickname'));
190 if (!empty($nickname)) {
191 if (!$this->_checkNickname($nickname)) {
192 $msg = sprintf(_m("You may not use nickname '%s'."),
194 throw new ClientException($msg);
202 * Hook notice save to prevent blacklisted urls
204 * Throws an exception if there's a blacklisted url in the content.
206 * @param Notice &$notice Notice being saved
208 * @return boolean hook value
210 function onStartNoticeSave(&$notice)
212 common_replace_urls_callback($notice->content,
213 array($this, 'checkNoticeUrl'));
218 * Helper callback for notice save
220 * Throws an exception if there's a blacklisted url in the content.
222 * @param string $url URL in the notice content
224 * @return boolean hook value
226 function checkNoticeUrl($url)
228 // It comes in special'd, so we unspecial it
229 // before comparing against patterns
231 $url = htmlspecialchars_decode($url);
233 if (!$this->_checkUrl($url)) {
234 $msg = sprintf(_m("You may not use UTL \"%s\" in notices."),
236 throw new ClientException($msg);
243 * Helper for checking URLs
245 * Checks an URL against our patterns for a match.
247 * @param string $url URL to check
249 * @return boolean true means it's OK, false means it's bad
251 private function _checkUrl($url)
253 $patterns = $this->_getUrlPatterns();
255 foreach ($patterns as $pattern) {
256 if ($pattern != '' && preg_match("/$pattern/", $url)) {
265 * Helper for checking nicknames
267 * Checks a nickname against our patterns for a match.
269 * @param string $nickname nickname to check
271 * @return boolean true means it's OK, false means it's bad
273 private function _checkNickname($nickname)
275 $patterns = $this->_getNicknamePatterns();
277 foreach ($patterns as $pattern) {
278 if ($pattern != '' && preg_match("/$pattern/", $nickname)) {
287 * Add our actions to the URL router
289 * @param Net_URL_Mapper $m URL mapper for this hit
291 * @return boolean hook return
293 function onRouterInitialized($m)
295 $m->connect('admin/blacklist', array('action' => 'blacklistadminpanel'));
300 * Auto-load our classes if called
302 * @param string $cls Class to load
304 * @return boolean hook return
306 function onAutoload($cls)
308 switch (strtolower($cls))
310 case 'nickname_blacklist':
311 case 'homepage_blacklist':
312 include_once INSTALLDIR.'/plugins/Blacklist/'.ucfirst($cls).'.php';
314 case 'blacklistadminpanelaction':
315 $base = strtolower(mb_substr($cls, 0, -6));
316 include_once INSTALLDIR.'/plugins/Blacklist/'.$base.'.php';
324 * Plugin version data
326 * @param array &$versions array of version blocks
328 * @return boolean hook value
330 function onPluginVersion(&$versions)
332 $versions[] = array('name' => 'Blacklist',
333 'version' => self::VERSION,
334 'author' => 'Evan Prodromou',
336 'http://status.net/wiki/Plugin:Blacklist',
338 _m('Keeps a blacklist of forbidden nickname '.
339 'and URL patterns.'));
344 * Determines if our admin panel can be shown
346 * @param string $name name of the admin panel
347 * @param boolean &$isOK result
349 * @return boolean hook value
352 function onAdminPanelCheck($name, &$isOK)
354 if ($name == 'blacklist') {
355 $isOK = $this->canAdmin;
363 * Add our tab to the admin panel
365 * @param Widget $nav Admin panel nav
367 * @return boolean hook value
369 function onEndAdminPanelNav($nav)
371 if (AdminPanelAction::canAdmin('blacklist')) {
373 $action_name = $nav->action->trimmed('action');
375 $nav->out->menuItem(common_local_url('blacklistadminpanel'),
377 _m('Blacklist configuration'),
378 $action_name == 'blacklistadminpanel',
379 'nav_blacklist_admin_panel');
385 function onEndDeleteUserForm($action, $user)
387 $cur = common_current_user();
389 if (empty($cur) || !$cur->hasRight(Right::CONFIGURESITE)) {
393 $profile = $user->getProfile();
395 if (empty($profile)) {
399 $action->elementStart('ul', 'form_data');
400 $action->elementStart('li');
401 $this->checkboxAndText($action,
403 _m('Add this nickname pattern to blacklist'),
404 'blacklistnicknamepattern',
405 $this->patternizeNickname($user->nickname));
406 $action->elementEnd('li');
408 if (!empty($profile->homepage)) {
409 $action->elementStart('li');
410 $this->checkboxAndText($action,
412 _m('Add this homepage pattern to blacklist'),
413 'blacklisthomepagepattern',
414 $this->patternizeHomepage($profile->homepage));
415 $action->elementEnd('li');
418 $action->elementEnd('ul');
421 function onEndDeleteUser($action, $user)
423 if ($action->boolean('blacklisthomepage')) {
424 $pattern = $action->trimmed('blacklisthomepagepattern');
425 Homepage_blacklist::ensurePattern($pattern);
428 if ($action->boolean('blacklistnickname')) {
429 $pattern = $action->trimmed('blacklistnicknamepattern');
430 Nickname_blacklist::ensurePattern($pattern);
436 function checkboxAndText($action, $checkID, $label, $textID, $value)
438 $action->element('input', array('name' => $checkID,
439 'type' => 'checkbox',
440 'class' => 'checkbox',
445 $action->element('label', array('class' => 'checkbox',
451 $action->element('input', array('name' => $textID,
457 function patternizeNickname($nickname)
462 function patternizeHomepage($homepage)
464 $hostname = parse_url($homepage, PHP_URL_HOST);