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 // TRANS: Exception thrown if the Blacklist plugin configuration is incorrect.
130 // TRANS: %1$s is a configuration section, %2$s is a configuration setting.
131 throw new Exception(sprintf(_m('Unknown data type for config %1$s + %2$s.'),$section, $setting));
136 * Hook registration to prevent blacklisted homepages or nicknames
138 * Throws an exception if there's a blacklisted homepage or nickname.
140 * @param Action $action Action being called (usually register)
142 * @return boolean hook value
144 function onStartRegisterUser(&$user, &$profile)
146 $homepage = strtolower($profile->homepage);
148 if (!empty($homepage)) {
149 if (!$this->_checkUrl($homepage)) {
150 // TRANS: Validation failure for URL. %s is the URL.
151 $msg = sprintf(_m("You may not register with homepage \"%s\"."),
153 throw new ClientException($msg);
157 $nickname = strtolower($profile->nickname);
159 if (!empty($nickname)) {
160 if (!$this->_checkNickname($nickname)) {
161 // TRANS: Validation failure for nickname. %s is the nickname.
162 $msg = sprintf(_m("You may not register with nickname \"%s\"."),
164 throw new ClientException($msg);
172 * Hook profile update to prevent blacklisted homepages or nicknames
174 * Throws an exception if there's a blacklisted homepage or nickname.
176 * @param Action $action Action being called (usually register)
178 * @return boolean hook value
180 function onStartProfileSaveForm($action)
182 $homepage = strtolower($action->trimmed('homepage'));
184 if (!empty($homepage)) {
185 if (!$this->_checkUrl($homepage)) {
186 // TRANS: Validation failure for URL. %s is the URL.
187 $msg = sprintf(_m("You may not use homepage \"%s\"."),
189 throw new ClientException($msg);
193 $nickname = strtolower($action->trimmed('nickname'));
195 if (!empty($nickname)) {
196 if (!$this->_checkNickname($nickname)) {
197 // TRANS: Validation failure for nickname. %s is the nickname.
198 $msg = sprintf(_m("You may not use nickname \"%s\"."),
200 throw new ClientException($msg);
208 * Hook notice save to prevent blacklisted urls
210 * Throws an exception if there's a blacklisted url in the content.
212 * @param Notice &$notice Notice being saved
214 * @return boolean hook value
216 function onStartNoticeSave(&$notice)
218 common_replace_urls_callback($notice->content,
219 array($this, 'checkNoticeUrl'));
224 * Helper callback for notice save
226 * Throws an exception if there's a blacklisted url in the content.
228 * @param string $url URL in the notice content
230 * @return boolean hook value
232 function checkNoticeUrl($url)
234 // It comes in special'd, so we unspecial it
235 // before comparing against patterns
237 $url = htmlspecialchars_decode($url);
239 if (!$this->_checkUrl($url)) {
240 // TRANS: Validation failure for URL. %s is the URL.
241 $msg = sprintf(_m("You may not use URL \"%s\" in notices."),
243 throw new ClientException($msg);
250 * Helper for checking URLs
252 * Checks an URL against our patterns for a match.
254 * @param string $url URL to check
256 * @return boolean true means it's OK, false means it's bad
258 private function _checkUrl($url)
260 $patterns = $this->_getUrlPatterns();
262 foreach ($patterns as $pattern) {
263 if ($pattern != '' && preg_match("/$pattern/", $url)) {
272 * Helper for checking nicknames
274 * Checks a nickname against our patterns for a match.
276 * @param string $nickname nickname to check
278 * @return boolean true means it's OK, false means it's bad
280 private function _checkNickname($nickname)
282 $patterns = $this->_getNicknamePatterns();
284 foreach ($patterns as $pattern) {
285 if ($pattern != '' && preg_match("/$pattern/", $nickname)) {
294 * Add our actions to the URL router
296 * @param Net_URL_Mapper $m URL mapper for this hit
298 * @return boolean hook return
300 function onRouterInitialized($m)
302 $m->connect('panel/blacklist', array('action' => 'blacklistadminpanel'));
307 * Auto-load our classes if called
309 * @param string $cls Class to load
311 * @return boolean hook return
313 function onAutoload($cls)
315 switch (strtolower($cls))
317 case 'nickname_blacklist':
318 case 'homepage_blacklist':
319 include_once INSTALLDIR.'/plugins/Blacklist/'.ucfirst($cls).'.php';
321 case 'blacklistadminpanelaction':
322 $base = strtolower(mb_substr($cls, 0, -6));
323 include_once INSTALLDIR.'/plugins/Blacklist/'.$base.'.php';
331 * Plugin version data
333 * @param array &$versions array of version blocks
335 * @return boolean hook value
337 function onPluginVersion(&$versions)
339 $versions[] = array('name' => 'Blacklist',
340 'version' => self::VERSION,
341 'author' => 'Evan Prodromou',
343 'http://status.net/wiki/Plugin:Blacklist',
345 // TRANS: Plugin description.
346 _m('Keeps a blacklist of forbidden nickname '.
347 'and URL patterns.'));
352 * Determines if our admin panel can be shown
354 * @param string $name name of the admin panel
355 * @param boolean &$isOK result
357 * @return boolean hook value
359 function onAdminPanelCheck($name, &$isOK)
361 if ($name == 'blacklist') {
362 $isOK = $this->canAdmin;
370 * Add our tab to the admin panel
372 * @param Widget $nav Admin panel nav
374 * @return boolean hook value
376 function onEndAdminPanelNav($nav)
378 if (AdminPanelAction::canAdmin('blacklist')) {
380 $action_name = $nav->action->trimmed('action');
382 $nav->out->menuItem(common_local_url('blacklistadminpanel'),
383 // TRANS: Menu item in admin panel.
384 _m('MENU','Blacklist'),
385 // TRANS: Tooltip for menu item in admin panel.
386 _m('TOOLTIP','Blacklist configuration.'),
387 $action_name == 'blacklistadminpanel',
388 'nav_blacklist_admin_panel');
394 function onEndDeleteUserForm($action, $user)
396 $cur = common_current_user();
398 if (empty($cur) || !$cur->hasRight(Right::CONFIGURESITE)) {
402 $profile = $user->getProfile();
404 if (empty($profile)) {
408 $action->elementStart('ul', 'form_data');
409 $action->elementStart('li');
410 $this->checkboxAndText($action,
412 // TRANS: Checkbox label in the blacklist user form.
413 _m('Add this nickname pattern to blacklist'),
414 'blacklistnicknamepattern',
415 $this->patternizeNickname($user->nickname));
416 $action->elementEnd('li');
418 if (!empty($profile->homepage)) {
419 $action->elementStart('li');
420 $this->checkboxAndText($action,
422 // TRANS: Checkbox label in the blacklist user form.
423 _m('Add this homepage pattern to blacklist'),
424 'blacklisthomepagepattern',
425 $this->patternizeHomepage($profile->homepage));
426 $action->elementEnd('li');
429 $action->elementEnd('ul');
432 function onEndDeleteUser($action, $user)
434 if ($action->boolean('blacklisthomepage')) {
435 $pattern = $action->trimmed('blacklisthomepagepattern');
436 Homepage_blacklist::ensurePattern($pattern);
439 if ($action->boolean('blacklistnickname')) {
440 $pattern = $action->trimmed('blacklistnicknamepattern');
441 Nickname_blacklist::ensurePattern($pattern);
447 function checkboxAndText($action, $checkID, $label, $textID, $value)
449 $action->element('input', array('name' => $checkID,
450 'type' => 'checkbox',
451 'class' => 'checkbox',
456 $action->element('label', array('class' => 'checkbox',
462 $action->element('input', array('name' => $textID,
468 function patternizeNickname($nickname)
473 function patternizeHomepage($homepage)
475 $hostname = parse_url($homepage, PHP_URL_HOST);
479 function onStartHandleFeedEntry($activity)
481 return $this->_checkActivity($activity);
484 function onStartHandleSalmon($activity)
486 return $this->_checkActivity($activity);
489 function _checkActivity($activity)
491 $actor = $activity->actor;
497 $homepage = strtolower($actor->link);
499 if (!empty($homepage)) {
500 if (!$this->_checkUrl($homepage)) {
501 // TRANS: Exception thrown trying to post a notice while having set a blocked homepage URL. %s is the blocked URL.
502 $msg = sprintf(_m("Users from \"%s\" are blocked."),
504 throw new ClientException($msg);
508 $nickname = strtolower($actor->poco->preferredUsername);
510 if (!empty($nickname)) {
511 if (!$this->_checkNickname($nickname)) {
512 // TRANS: Exception thrown trying to post a notice while having a blocked nickname. %s is the blocked nickname.
513 $msg = sprintf(_m("Notices from nickname \"%s\" disallowed."),
515 throw new ClientException($msg);
523 * Check URLs and homepages for blacklisted users.
525 function onStartSubscribe($subscriber, $other)
527 foreach (array($other->profileurl, $other->homepage) as $url) {
533 $url = strtolower($url);
535 if (!$this->_checkUrl($url)) {
536 // TRANS: Client exception thrown trying to subscribe to a person with a blocked homepage or site URL. %s is the blocked URL.
537 $msg = sprintf(_m("Users from \"%s\" are blocked."),
539 throw new ClientException($msg);
543 $nickname = $other->nickname;
545 if (!empty($nickname)) {
546 if (!$this->_checkNickname($nickname)) {
547 // TRANS: Client exception thrown trying to subscribe to a person with a blocked nickname. %s is the blocked nickname.
548 $msg = sprintf(_m("Cannot subscribe to nickname \"%s\"."),
550 throw new ClientException($msg);