Move mail_confirm_address out of mail.php

This commit is contained in:
Mikael Nordfeldth 2016-03-06 17:27:40 +01:00
parent 47ae21c08e
commit 6ec72b2978
7 changed files with 94 additions and 60 deletions

View File

@ -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));
}

View File

@ -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);

View File

@ -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) {

View File

@ -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
*

View File

@ -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');
}

View File

@ -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()}";
}

View File

@ -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]);