]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Move mail_confirm_address out of mail.php
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Mar 2016 16:27:40 +0000 (17:27 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Mar 2016 16:27:40 +0000 (17:27 +0100)
actions/emailsettings.php
classes/Confirm_address.php
classes/User.php
lib/mail.php
plugins/EmailRegistration/EmailRegistrationPlugin.php
plugins/EmailRegistration/scripts/cancelemailregistration.php
plugins/RequireValidatedEmail/scripts/registerbyemail.php

index 7384b3630d3a080debf6379148297aa4bf4b7ccb..b5933fdb6515c7db7444cfeeae281b572511e691 100644 (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));
         }
index 9bb56cef9c2d93fbd963ac99dedf830cead0ebac..f8b5b21124c9f03edaba9406e7a618eff6a7b813 100644 (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);
index 26225916f1e70cee2a0f261a245b475d737210fb..c9c61d3aed40606267eb5e6268b624b91e39a1c4 100644 (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) {
index 42a756ac5dc79b5f11ac0084dee1b05950df0e33..428f87638308928d3baa473196077caa420dbd14 100644 (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
  *
index 56e022435e93d8d1e688fa93a59d2043b171bfd3..378cb6acd1b8a3a7769aa192b0f81a3b042273ca 100644 (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');
         }
 
index d834aade600abdb032570d35fb9dc1f9b7bf5b36..b926ed9a62458cbe90af15cc2a6b3683b89ca634 100755 (executable)
@@ -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()}";
 }
index 4d2000ab0fe714903549880f040c1f97dc77cad0..c7b4a2af9e65a766f557e143d32d6a88851d9d78 100755 (executable)
@@ -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]);