123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- class RequireValidatedEmailPlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
-
- public $grandfatherCutoff = null;
-
- public $trustedOpenIDs = array();
-
- public $disallowLogin = false;
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('main/confirmfirst/:code',
- array('action' => 'confirmfirstemail'));
- return true;
- }
-
- public function onStartNoticeSave(Notice $notice)
- {
- $author = $notice->getProfile();
- if (!$author->isLocal()) {
-
- return true;
- }
- $user = $author->getUser();
- if (!$this->validated($user)) {
-
- $msg = _m('You must validate your email address before posting.');
- throw new ClientException($msg);
- }
- return true;
- }
-
- 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)
- {
-
-
- $knownGood = !empty($user->email) ||
- $this->grandfathered($user) ||
- $this->hasTrustedOpenID($user);
-
-
-
- Event::handle('RequireValidatedEmailPlugin_Override',
- array($user, &$knownGood));
- return $knownGood;
- }
-
- protected function grandfathered(User $user)
- {
- if ($this->grandfatherCutoff) {
- $created = strtotime($user->created . " GMT");
- $cutoff = strtotime($this->grandfatherCutoff);
- if ($created < $cutoff) {
- return true;
- }
- }
- return false;
- }
-
- 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;
- }
-
- function onPluginVersion(array &$versions)
- {
- $versions[] =
- array('name' => 'Require Validated Email',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Craig Andrews, '.
- 'Evan Prodromou, '.
- 'Brion Vibber',
- 'homepage' =>
- 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/RequireValidatedEmail',
- 'rawdescription' =>
-
- _m('Disables posting without a validated email address.'));
- return true;
- }
-
- 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;
- }
-
- 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;
- }
- function onLoginAction($action, &$login)
- {
- if ($action == 'confirmfirstemail') {
- $login = true;
- return false;
- }
- return true;
- }
- }
|