diff --git a/actions/emailsettings.php b/actions/emailsettings.php index 7384b3630d..b5933fdb65 100644 --- a/actions/emailsettings.php +++ b/actions/emailsettings.php @@ -369,8 +369,7 @@ class EmailsettingsAction extends SettingsAction throw new ServerException(_('Could not insert confirmation code.')); } - common_debug('Sending confirmation address for user '.$user->getID().' to email '.$email); - mail_confirm_address($user, $confirm->code, $user->getNickname(), $email); + $confirm->sendConfirmation(); Event::handle('EndAddEmailAddress', array($user, $email)); } diff --git a/classes/Confirm_address.php b/classes/Confirm_address.php index 9bb56cef9c..f8b5b21124 100644 --- a/classes/Confirm_address.php +++ b/classes/Confirm_address.php @@ -35,18 +35,18 @@ class Confirm_address extends Managed_DataObject ); } - static function getAddress($address, $addressType) + static function getByAddress($address, $addressType) { $ca = new Confirm_address(); $ca->address = $address; $ca->address_type = $addressType; - if ($ca->find(true)) { - return $ca; + if (!$ca->find(true)) { + throw new NoResultException($ca); } - return null; + return $ca; } static function saveNew($user, $address, $addressType, $extra=null) @@ -67,6 +67,85 @@ class Confirm_address extends Managed_DataObject return $ca; } + public function getAddress() + { + return $this->address; + } + + public function getAddressType() + { + return $this->address_type; + } + + public function getCode() + { + return $this->code; + } + + public function getProfile() + { + return Profile::getByID($this->user_id); + } + + public function getUrl() + { + return common_local_url('confirmaddress', array('code' => $this->code)); + } + + /** + * Supply arguments in $args. Currently known args: + * headers Array with headers (only used for email) + * nickname How we great the user (defaults to nickname, but can be any string) + * sitename Name we sign the email with (defaults to sitename, but can be any string) + * url The confirmation address URL. + */ + public function sendConfirmation(array $args=array()) + { + common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type)); + + $defaults = [ + 'headers' => array(), + 'nickname' => $this->getProfile()->getNickname(), + 'sitename' => common_config('site', 'name'), + 'url' => $this->getUrl(), + ]; + foreach (array_keys($defaults) as $key) { + if (!isset($args[$key])) { + $args[$key] = $defaults[$key]; + } + } + + switch ($this->getAddressType()) { + case 'email': + $this->sendEmailConfirmation($args); + break; + default: + throw ServerException('Unable to handle confirm_address address type: '._ve($this->address_type)); + } + } + + public function sendEmailConfirmation(array $args=array()) + { + // TRANS: Subject for address confirmation email. + $subject = _('Email address confirmation'); + + // TRANS: Body for address confirmation email. + // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename, + // TRANS: %3$s is the URL to confirm at. + $body = sprintf(_("Hey, %1\$s.\n\n". + "Someone just entered this email address on %2\$s.\n\n" . + "If it was you, and you want to confirm your entry, ". + "use the URL below:\n\n\t%3\$s\n\n" . + "If not, just ignore this message.\n\n". + "Thanks for your time, \n%2\$s\n"), + $args['nickname'], + $args['sitename'], + $args['url']); + + require_once(INSTALLDIR . '/lib/mail.php'); + return mail_to_user($this->getProfile()->getUser(), $subject, $body, $args['headers'], $this->getAddress()); + } + public function delete($useWhere=false) { $result = parent::delete($useWhere); diff --git a/classes/User.php b/classes/User.php index 26225916f1..c9c61d3aed 100644 --- a/classes/User.php +++ b/classes/User.php @@ -384,8 +384,7 @@ class User extends Managed_DataObject if (!empty($email) && empty($user->email)) { try { - require_once(INSTALLDIR . '/lib/mail.php'); - mail_confirm_address($user, $confirm->code, $profile->getNickname(), $email); + $confirm->sendConfirmation(); } catch (EmailException $e) { common_log(LOG_ERR, "Could not send user registration email for user id=={$profile->getID()}: {$e->getMessage()}"); if (!$accept_email_fail) { diff --git a/lib/mail.php b/lib/mail.php index 42a756ac5d..428f876383 100644 --- a/lib/mail.php +++ b/lib/mail.php @@ -144,7 +144,7 @@ function mail_notify_from() * * @return boolean success flag */ -function mail_to_user(&$user, $subject, $body, $headers=array(), $address=null) +function mail_to_user($user, $subject, $body, $headers=array(), $address=null) { if (!$address) { $address = $user->email; @@ -161,45 +161,6 @@ function mail_to_user(&$user, $subject, $body, $headers=array(), $address=null) return mail_send($recipients, $headers, $body); } -/** - * Send an email to confirm a user's control of an email address - * - * @param User $user User claiming the email address - * @param string $code Confirmation code - * @param string $nickname Nickname of user - * @param string $address email address to confirm - * - * @see common_confirmation_code() - * - * @return success flag - */ -function mail_confirm_address($user, $code, $nickname, $address, $url=null) -{ - if (empty($url)) { - $url = common_local_url('confirmaddress', array('code' => $code)); - } - - // TRANS: Subject for address confirmation email. - $subject = _('Email address confirmation'); - - // TRANS: Body for address confirmation email. - // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename, - // TRANS: %3$s is the URL to confirm at. - $body = sprintf(_("Hey, %1\$s.\n\n". - "Someone just entered this email address on %2\$s.\n\n" . - "If it was you, and you want to confirm your entry, ". - "use the URL below:\n\n\t%3\$s\n\n" . - "If not, just ignore this message.\n\n". - "Thanks for your time, \n%2\$s\n"), - $nickname, - common_config('site', 'name'), - $url); - - $headers = array(); - - return mail_to_user($user, $subject, $body, $headers, $address); -} - /** * notify a user of subscription by another user * diff --git a/plugins/EmailRegistration/EmailRegistrationPlugin.php b/plugins/EmailRegistration/EmailRegistrationPlugin.php index 56e022435e..378cb6acd1 100644 --- a/plugins/EmailRegistration/EmailRegistrationPlugin.php +++ b/plugins/EmailRegistration/EmailRegistrationPlugin.php @@ -106,9 +106,9 @@ class EmailRegistrationPlugin extends Plugin throw new ClientException(_m('Not a valid email address.')); } - $confirm = Confirm_address::getAddress($email, self::CONFIRMTYPE); - - if (empty($confirm)) { + try { + $confirm = Confirm_address::getByAddress($email, self::CONFIRMTYPE); + } catch (NoResultException $e) { $confirm = Confirm_address::saveNew(null, $email, 'register'); } diff --git a/plugins/EmailRegistration/scripts/cancelemailregistration.php b/plugins/EmailRegistration/scripts/cancelemailregistration.php index d834aade60..b926ed9a62 100755 --- a/plugins/EmailRegistration/scripts/cancelemailregistration.php +++ b/plugins/EmailRegistration/scripts/cancelemailregistration.php @@ -41,15 +41,15 @@ if (count($args) == 0) { $email = $args[0]; -$confirm = Confirm_address::getAddress($email, EmailRegistrationPlugin::CONFIRMTYPE); +try { + $confirm = Confirm_address::getByAddress($email, EmailRegistrationPlugin::CONFIRMTYPE); -if (!empty($confirm)) { if (have_option('d', 'dryrun')) { print "[Dry run mode] Deleted confirmation code {$confirm->code} for {$confirm->address}.\n"; } else { $confirm->delete(); print "Deleted confirmation code {$confirm->code} for {$confirm->address}.\n"; } -} else { - print "Couldn't find an email registration code for {$email}.\n"; +} catch (NoResultException $e) { + print "Exception thrown for {$email}: {$e->getMessage()}"; } diff --git a/plugins/RequireValidatedEmail/scripts/registerbyemail.php b/plugins/RequireValidatedEmail/scripts/registerbyemail.php index 4d2000ab0f..c7b4a2af9e 100755 --- a/plugins/RequireValidatedEmail/scripts/registerbyemail.php +++ b/plugins/RequireValidatedEmail/scripts/registerbyemail.php @@ -73,8 +73,4 @@ $url = common_local_url('confirmfirstemail', print "$url\n"; -mail_confirm_address($user, - $confirm->code, - $user->nickname, - $email, - $url); +$confirm->sendConfirmation(['url'=>$url]);