123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- defined('GNUSOCIAL') || die();
- class RequireValidatedEmailPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
-
- public $exemptBefore = null;
-
- public $grandfatherCutoff = null;
-
- public $trustedOpenIDs = [];
-
- public $disallowLogin = false;
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect(
- 'main/confirmfirst/:code',
- ['action' => 'confirmfirstemail']
- );
- return true;
- }
-
- public function onStartNoticeSave(Notice $notice)
- {
- $author = $notice->getProfile();
- if (!$author->isLocal()) {
-
- return true;
- }
- $user = $author->getUser();
- if ($user !== common_current_user()) {
-
- return true;
- }
- if (!$this->validated($user)) {
-
- $msg = _m('You must validate your email address before posting.');
- throw new ClientException($msg);
- }
- return true;
- }
-
- public function onStartRegisterUser(&$user, &$profile)
- {
- $email = $user->email;
- if (empty($email)) {
-
- throw new ClientException(_m('You must provide an email address to register.'));
- }
- return true;
- }
-
- protected function validated(User $user): bool
- {
-
-
- $knownGood = (
- !empty($user->email)
- || $this->exempted($user)
- || $this->hasTrustedOpenID($user)
- );
-
-
-
- Event::handle(
- 'RequireValidatedEmailPlugin_Override',
- [$user, &$knownGood]
- );
- return $knownGood;
- }
-
- protected function exempted(User $user): bool
- {
- $exempt_before = ($this->exemptBefore ?? $this->grandfatherCutoff);
- if (!empty($exempt_before)) {
- $utc_timezone = new DateTimeZone('UTC');
- $created_date = new DateTime($user->created, $utc_timezone);
- $exempt_date = new DateTime($exempt_before, $utc_timezone);
- if ($created_date < $exempt_date) {
- return true;
- }
- }
- return false;
- }
-
- public function hasTrustedOpenID(User $user)
- {
- if ($this->trustedOpenIDs && class_exists('User_openid')) {
- foreach ($this->trustedOpenIDs as $regex) {
- $oid = new User_openid();
- $oid->user_id = $user->id;
- $oid->find();
- while ($oid->fetch()) {
- if (preg_match($regex, $oid->canonical)) {
- return true;
- }
- }
- }
- }
- return false;
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] =
- array('name' => 'Require Validated Email',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Craig Andrews, '.
- 'Evan Prodromou, '.
- 'Brion Vibber',
- 'homepage' =>
- GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/RequireValidatedEmail',
- 'rawdescription' =>
-
- _m('Disables posting without a validated email address.'));
- return true;
- }
-
- public function onStartMakeEntryForm($tag, $action, &$form)
- {
- $user = common_current_user();
- if (!empty($user)) {
- if (!$this->validated($user)) {
- $action->element('div', array('class'=>'error'), _m('You must validate an email address before posting!'));
- }
- }
- return true;
- }
-
- public function onUserRightsCheck(Profile $profile, $right, &$result)
- {
- if ($right == Right::CREATEGROUP ||
- ($this->disallowLogin && ($right == Right::WEBLOGIN || $right == Right::API))) {
- $user = User::getKV('id', $profile->id);
- if ($user && !$this->validated($user)) {
- $result = false;
- return false;
- }
- }
- return true;
- }
- public function onLoginAction($action, &$login)
- {
- if ($action == 'confirmfirstemail') {
- $login = true;
- return false;
- }
- return true;
- }
- }
|