3 * Table Definition for confirm_address
6 class Confirm_address extends Managed_DataObject
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; // timestamp() not_null default_CURRENT_TIMESTAMP
18 public static function schemaDef()
22 'code' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'good random code'),
23 'user_id' => array('type' => 'int', 'not null' => true, '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' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
31 'primary key' => array('code'),
32 'foreign keys' => array(
33 'confirm_address_user_id_fkey' => array('user', array('user_id' => 'id')),
38 static function getByAddress($address, $addressType)
40 $ca = new Confirm_address();
42 $ca->address = $address;
43 $ca->address_type = $addressType;
45 if (!$ca->find(true)) {
46 throw new NoResultException($ca);
52 static function saveNew($user, $address, $addressType, $extra=null)
54 $ca = new Confirm_address();
57 $ca->user_id = $user->id;
60 $ca->address = $address;
61 $ca->address_type = $addressType;
62 $ca->address_extra = $extra;
63 $ca->code = common_confirmation_code(64);
70 public function getAddress()
72 return $this->address;
75 public function getAddressType()
77 return $this->address_type;
80 public function getCode()
85 public function getProfile()
87 return Profile::getByID($this->user_id);
90 public function getUrl()
92 return common_local_url('confirmaddress', array('code' => $this->code));
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.
102 public function sendConfirmation(array $args=array())
104 common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type));
107 'headers' => array(),
108 'nickname' => $this->getProfile()->getNickname(),
109 'sitename' => common_config('site', 'name'),
110 'url' => $this->getUrl(),
112 foreach (array_keys($defaults) as $key) {
113 if (!isset($args[$key])) {
114 $args[$key] = $defaults[$key];
118 switch ($this->getAddressType()) {
120 $this->sendEmailConfirmation($args);
123 throw ServerException('Unable to handle confirm_address address type: '._ve($this->address_type));
127 public function sendEmailConfirmation(array $args=array())
129 // TRANS: Subject for address confirmation email.
130 $subject = _('Email address confirmation');
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"),
145 require_once(INSTALLDIR . '/lib/mail.php');
146 return mail_to_user($this->getProfile()->getUser(), $subject, $body, $args['headers'], $this->getAddress());
149 public function delete($useWhere=false)
151 $result = parent::delete($useWhere);
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.'));