]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Confirm_address.php
Added type-hint for StartShowNoticeFormData hook
[quix0rs-gnu-social.git] / classes / Confirm_address.php
index 0ed7796ad40b8458de85c6538d166ad4ee38b13d..f0748b888fe5a21779b5547d313370617350d13f 100644 (file)
@@ -2,38 +2,31 @@
 /**
  * Table Definition for confirm_address
  */
-require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Confirm_address extends Managed_DataObject 
+class Confirm_address extends Managed_DataObject
 {
-    ###START_AUTOCODE
-    /* the code below is auto generated do not remove the above tag */
-
     public $__table = 'confirm_address';                 // table name
     public $code;                            // varchar(32)  primary_key not_null
     public $user_id;                         // int(4)   not_null
-    public $address;                         // varchar(255)   not_null
-    public $address_extra;                   // varchar(255)   not_null
+    public $address;                         // varchar(191)   not_null   not 255 because utf8mb4 takes more space
+    public $address_extra;                   // varchar(191)   not_null   not 255 because utf8mb4 takes more space
     public $address_type;                    // varchar(8)   not_null
-    public $claimed;                         // datetime()  
-    public $sent;                            // datetime()  
-    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
-
-    /* the code above is auto generated do not remove the tag below */
-    ###END_AUTOCODE
+    public $claimed;                         // datetime()
+    public $sent;                            // datetime()
+    public $modified;                        // datetime()   not_null default_CURRENT_TIMESTAMP
 
     public static function schemaDef()
     {
         return array(
             'fields' => array(
                 'code' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'good random code'),
-                'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user who requested confirmation'),
-                'address' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'address (email, xmpp, SMS, etc.)'),
-                'address_extra' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'carrier ID, for SMS'),
+                'user_id' => array('type' => 'int', 'default' => 0, 'description' => 'user who requested confirmation'),
+                'address' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'address (email, xmpp, SMS, etc.)'),
+                'address_extra' => array('type' => 'varchar', 'length' => 191, 'description' => 'carrier ID, for SMS'),
                 'address_type' => array('type' => 'varchar', 'length' => 8, 'not null' => true, 'description' => 'address type ("email", "xmpp", "sms")'),
                 'claimed' => array('type' => 'datetime', 'description' => 'date this was claimed for queueing'),
                 'sent' => array('type' => 'datetime', 'description' => 'date this was sent for queueing'),
-                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
+                'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
             ),
             'primary key' => array('code'),
             'foreign keys' => array(
@@ -42,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)
@@ -73,4 +66,96 @@ 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);
+
+        if ($result === false) {
+            common_log_db_error($confirm, 'DELETE', __FILE__);
+            // TRANS: Server error displayed when an address confirmation code deletion from the
+            // TRANS: database fails in the contact address confirmation action.
+            throw new ServerException(_('Could not delete address confirmation.'));
+        }
+        return $result;
+    }
 }