]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Confirm_address.php
Added type-hint for StartShowEntryForms hook
[quix0rs-gnu-social.git] / classes / Confirm_address.php
1 <?php
2 /**
3  * Table Definition for confirm_address
4  */
5
6 class Confirm_address extends Managed_DataObject
7 {
8     public $__table = 'confirm_address';                 // table name
9     public $code;                            // varchar(32)  primary_key not_null
10     public $user_id;                         // int(4)   not_null
11     public $address;                         // varchar(191)   not_null   not 255 because utf8mb4 takes more space
12     public $address_extra;                   // varchar(191)   not_null   not 255 because utf8mb4 takes more space
13     public $address_type;                    // varchar(8)   not_null
14     public $claimed;                         // datetime()
15     public $sent;                            // datetime()
16     public $modified;                        // datetime()   not_null default_CURRENT_TIMESTAMP
17
18     public static function schemaDef()
19     {
20         return array(
21             'fields' => array(
22                 'code' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'good random code'),
23                 'user_id' => array('type' => 'int', 'default' => 0, 'description' => 'user who requested confirmation'),
24                 'address' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'address (email, xmpp, SMS, etc.)'),
25                 'address_extra' => array('type' => 'varchar', 'length' => 191, 'description' => 'carrier ID, for SMS'),
26                 'address_type' => array('type' => 'varchar', 'length' => 8, 'not null' => true, 'description' => 'address type ("email", "xmpp", "sms")'),
27                 'claimed' => array('type' => 'datetime', 'description' => 'date this was claimed for queueing'),
28                 'sent' => array('type' => 'datetime', 'description' => 'date this was sent for queueing'),
29                 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
30             ),
31             'primary key' => array('code'),
32             'foreign keys' => array(
33                 'confirm_address_user_id_fkey' => array('user', array('user_id' => 'id')),
34             ),
35         );
36     }
37
38     static function getByAddress($address, $addressType)
39     {
40         $ca = new Confirm_address();
41
42         $ca->address      = $address;
43         $ca->address_type = $addressType;
44
45         if (!$ca->find(true)) {
46             throw new NoResultException($ca);
47         }
48
49         return $ca;
50     }
51
52     static function saveNew($user, $address, $addressType, $extra=null)
53     {
54         $ca = new Confirm_address();
55
56         if (!empty($user)) {
57             $ca->user_id = $user->id;
58         }
59
60         $ca->address       = $address;
61         $ca->address_type  = $addressType;
62         $ca->address_extra = $extra;
63         $ca->code          = common_confirmation_code(64);
64
65         $ca->insert();
66
67         return $ca;
68     }
69
70     public function getAddress()
71     {
72         return $this->address;
73     }
74
75     public function getAddressType()
76     {
77         return $this->address_type;
78     }
79
80     public function getCode()
81     {
82         return $this->code;
83     }
84
85     public function getProfile()
86     {
87         return Profile::getByID($this->user_id);
88     }
89
90     public function getUrl()
91     {
92         return common_local_url('confirmaddress', array('code' => $this->code));
93     }
94
95     /**
96      * Supply arguments in $args. Currently known args:
97      *  headers     Array with headers (only used for email)
98      *  nickname    How we great the user (defaults to nickname, but can be any string)
99      *  sitename    Name we sign the email with (defaults to sitename, but can be any string)
100      *  url         The confirmation address URL.
101      */
102     public function sendConfirmation(array $args=array())
103     {
104         common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type));
105
106         $defaults = [
107                      'headers'  => array(),
108                      'nickname' => $this->getProfile()->getNickname(),
109                      'sitename' => common_config('site', 'name'),
110                      'url'      => $this->getUrl(),
111                     ];
112         foreach (array_keys($defaults) as $key) {
113             if (!isset($args[$key])) {
114                 $args[$key] = $defaults[$key];
115             }
116         }
117
118         switch ($this->getAddressType()) {
119         case 'email':
120             $this->sendEmailConfirmation($args);
121             break;
122         default:
123             throw ServerException('Unable to handle confirm_address address type: '._ve($this->address_type));
124         }
125     }
126
127     public function sendEmailConfirmation(array $args=array())
128     {
129         // TRANS: Subject for address confirmation email.
130         $subject = _('Email address confirmation');
131
132         // TRANS: Body for address confirmation email.
133         // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename,
134         // TRANS: %3$s is the URL to confirm at.
135         $body = sprintf(_("Hey, %1\$s.\n\n".
136                           "Someone just entered this email address on %2\$s.\n\n" .
137                           "If it was you, and you want to confirm your entry, ".
138                           "use the URL below:\n\n\t%3\$s\n\n" .
139                           "If not, just ignore this message.\n\n".
140                           "Thanks for your time, \n%2\$s\n"),
141                         $args['nickname'],
142                         $args['sitename'],
143                         $args['url']);
144
145         require_once(INSTALLDIR . '/lib/mail.php');
146         return mail_to_user($this->getProfile()->getUser(), $subject, $body, $args['headers'], $this->getAddress());
147     }
148
149     public function delete($useWhere=false)
150     {
151         $result = parent::delete($useWhere);
152
153         if ($result === false) {
154             common_log_db_error($confirm, 'DELETE', __FILE__);
155             // TRANS: Server error displayed when an address confirmation code deletion from the
156             // TRANS: database fails in the contact address confirmation action.
157             throw new ServerException(_('Could not delete address confirmation.'));
158         }
159         return $result;
160     }
161 }