Confirm_address.php 6.0 KB

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