@ -1,50 +1,41 @@
<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see < http: / / www . gnu . org / licenses / > .
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* Restrict the email addresses in a domain to a select whitelist
*
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see < http: / / www . gnu . org / licenses / > .
*
* @category Cache
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou < evan @ status . net >
* @author Zach Copley < zach @ status . net >
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
defined('GNUSOCIAL') || die();
/**
* Restrict the email addresses to a domain whitelist
*
* @category General
* @package StatusNet
* @package GNUsocial
* @author Evan Prodromou < evan @ status . net >
* @author Zach Copley < zach @ status . net >
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class DomainWhitelistPlugin extends Plugin
{
@ -56,7 +47,8 @@ class DomainWhitelistPlugin extends Plugin
*
* @return String the absolute path
*/
protected function getPath() {
protected function getPath()
{
return preg_replace('/^' . preg_quote(INSTALLDIR, '/') . '\//', '', dirname(__FILE__));
}
@ -67,7 +59,8 @@ class DomainWhitelistPlugin extends Plugin
*
* @return boolean hook flag
*/
function onEndShowStatusNetScripts($action) {
public function onEndShowStatusNetScripts($action)
{
$name = $action->arg('action');
if ($name == 'invite') {
$action->script($this->getPath() . '/js/whitelistinvite.js');
@ -75,13 +68,13 @@ class DomainWhitelistPlugin extends Plugin
return true;
}
function onRequireValidatedEmailPlugin_Override($user, & $knownGood)
public function onRequireValidatedEmailPlugin_Override($user, & $knownGood)
{
$knownGood = (!empty($user->email) & & $this->matchesWhitelist($user->email));
return true;
}
function onEndValidateUserEmail($user, $email, & $valid)
public function onEndValidateUserEmail($user, $email, & $valid)
{
if ($valid) { // it's otherwise valid
if (!$this->matchesWhitelist($email)) {
@ -89,14 +82,18 @@ class DomainWhitelistPlugin extends Plugin
if (count($whitelist) == 1) {
// TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
// TRANS: %s is a whitelisted e-mail domain.
$message = sprintf(_m('Email address must be in this domain: %s.'),
$whitelist[0]);
$message = sprintf(
_m('Email address must be in this domain: %s.'),
$whitelist[0]
);
} else {
// TRANS: Client exception thrown when a given e-mailaddress is not in the domain whitelist.
// TRANS: %s are whitelisted e-mail domains separated by comma's (localisable).
$message = sprintf(_m('Email address must be in one of these domains: %s.'),
// TRANS: Separator for whitelisted domains.
implode(_m('SEPARATOR',', '), $whitelist));
$message = sprintf(
_m('Email address must be in one of these domains: %s.'),
// TRANS: Separator for whitelisted domains.
implode(_m('SEPARATOR', ', '), $whitelist)
);
}
throw new ClientException($message);
}
@ -104,7 +101,7 @@ class DomainWhitelistPlugin extends Plugin
return true;
}
function onStartAddEmailAddress($user, $email)
public function onStartAddEmailAddress($user, $email)
{
if (!$this->matchesWhitelist($email)) {
// TRANS: Exception thrown when an e-mail address does not match the site's domain whitelist.
@ -114,7 +111,7 @@ class DomainWhitelistPlugin extends Plugin
return true;
}
function onEndValidateEmailInvite($user, $email, & $valid)
public function onEndValidateEmailInvite($user, $email, & $valid)
{
if ($valid) {
$valid = $this->matchesWhitelist($email);
@ -123,7 +120,7 @@ class DomainWhitelistPlugin extends Plugin
return true;
}
function matchesWhitelist($email)
public function matchesWhitelist($email)
{
$whitelist = $this->getWhitelist();
@ -143,13 +140,13 @@ class DomainWhitelistPlugin extends Plugin
* @param string $email and email address
* @return string the domain
*/
function domainFromEmail($email)
public function domainFromEmail($email)
{
$parts = explode('@', $email);
return strtolower(trim($parts[1]));
}
function getWhitelist()
public function getWhitelist()
{
$whitelist = common_config('email', 'whitelist');
@ -169,7 +166,7 @@ class DomainWhitelistPlugin extends Plugin
* @param string $domain domain to check
* @return boolean whether to include the domain
*/
function userDomainFilter($domain)
public function userDomainFilter($domain)
{
$user = common_current_user();
$userDomain = $this->domainFromEmail($user->email);
@ -190,7 +187,7 @@ class DomainWhitelistPlugin extends Plugin
* @param array $whitelist whitelist of allowed email domains
* @return array an ordered or sorted version of the whitelist
*/
function sortWhitelist($whitelist)
public function sortWhitelist($whitelist)
{
$whitelist = array_unique($whitelist);
natcasesort($whitelist);
@ -223,7 +220,7 @@ class DomainWhitelistPlugin extends Plugin
* @param action $action the invite action
* @return boolean hook value
*/
function onStartShowInviteForm($action)
public function onStartShowInviteForm($action)
{
$this->showConfirmDialog($action);
$form = new WhitelistInviteForm($action, $this->getWhitelist());
@ -231,7 +228,7 @@ class DomainWhitelistPlugin extends Plugin
return false;
}
function showConfirmDialog($action)
public function showConfirmDialog($action)
{
// For JQuery UI modal dialog
$action->elementStart(
@ -252,21 +249,21 @@ class DomainWhitelistPlugin extends Plugin
* @param action & $action the invite action
* @return boolean hook value
*/
function onStartSendInvitations(& $action)
public function onStartSendInvitations(& $action)
{
$emails = array() ;
$usernames = $action->arg('username');
$domains = $action->arg('domain');
$emails = [] ;
$usernames = $action->arg('username');
$domains = $action->arg('domain');
for($i = 0; $i < count ( $ usernames ) ; $ i + + ) {
if (!empty($usernames[$i] )) {
$emails[] = $usernames[$i] . '@' . $domains[$i ] . "\n";
}
}
foreach ($usernames as $key => $username) {
if (!empty($username)) {
$emails[] = $username . '@' . $domains[$key ] . "\n";
}
}
$action->args['addresses'] = implode($emails);
$action->args['addresses'] = implode('', $emails);
return true;
return true;
}
public function onPluginVersion(array & $versions): bool