123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- defined('GNUSOCIAL') || die();
- class Confirm_address extends Managed_DataObject
- {
- public $__table = 'confirm_address';
- public $code;
- public $user_id;
- public $address;
- public $address_extra;
- public $address_type;
- public $claimed;
- public $sent;
- public $modified;
- 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', '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'),
- ),
- 'primary key' => array('code'),
- 'foreign keys' => array(
- 'confirm_address_user_id_fkey' => array('user', array('user_id' => 'id')),
- ),
- 'indexes' => array(
- 'confirm_address_user_id_idx' => array('user_id'),
- ),
- );
- }
- public static function getByAddress($address, $addressType)
- {
- $ca = new Confirm_address();
- $ca->address = $address;
- $ca->address_type = $addressType;
- if (!$ca->find(true)) {
- throw new NoResultException($ca);
- }
- return $ca;
- }
- public static function saveNew($user, $address, $addressType, $extra = null)
- {
- $ca = new Confirm_address();
- if (!empty($user)) {
- $ca->user_id = $user->id;
- }
- $ca->address = $address;
- $ca->address_type = $addressType;
- $ca->address_extra = $extra;
- $ca->code = common_confirmation_code(64);
- $ca->insert();
- 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));
- }
-
- public function sendConfirmation(array $args = [])
- {
- common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type));
- $defaults = [
- 'headers' => [],
- '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 = [])
- {
-
- $subject = _('Email address confirmation');
-
-
-
- $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/util/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__);
-
-
- throw new ServerException(_('Could not delete address confirmation.'));
- }
- return $result;
- }
- }
|