Confirm_address.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for confirm_address
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Confirm_address extends Managed_DataObject
  21. {
  22. public $__table = 'confirm_address'; // table name
  23. public $code; // varchar(32) primary_key not_null
  24. public $user_id; // int()
  25. public $address; // varchar(191) not_null not 255 because utf8mb4 takes more space
  26. public $address_extra; // varchar(191) not_null not 255 because utf8mb4 takes more space
  27. public $address_type; // varchar(8) not_null
  28. public $claimed; // datetime()
  29. public $sent; // datetime()
  30. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  31. public static function schemaDef()
  32. {
  33. return array(
  34. 'fields' => array(
  35. 'code' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'good random code'),
  36. 'user_id' => array('type' => 'int', 'description' => 'user who requested confirmation'),
  37. 'address' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'address (email, xmpp, SMS, etc.)'),
  38. 'address_extra' => array('type' => 'varchar', 'length' => 191, 'description' => 'carrier ID, for SMS'),
  39. 'address_type' => array('type' => 'varchar', 'length' => 8, 'not null' => true, 'description' => 'address type ("email", "xmpp", "sms")'),
  40. 'claimed' => array('type' => 'datetime', 'description' => 'date this was claimed for queueing'),
  41. 'sent' => array('type' => 'datetime', 'description' => 'date this was sent for queueing'),
  42. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  43. ),
  44. 'primary key' => array('code'),
  45. 'foreign keys' => array(
  46. 'confirm_address_user_id_fkey' => array('user', array('user_id' => 'id')),
  47. ),
  48. 'indexes' => array(
  49. 'confirm_address_user_id_idx' => array('user_id'),
  50. ),
  51. );
  52. }
  53. public static function getByAddress($address, $addressType)
  54. {
  55. $ca = new Confirm_address();
  56. $ca->address = $address;
  57. $ca->address_type = $addressType;
  58. if (!$ca->find(true)) {
  59. throw new NoResultException($ca);
  60. }
  61. return $ca;
  62. }
  63. public static function saveNew($user, $address, $addressType, $extra = null)
  64. {
  65. $ca = new Confirm_address();
  66. if (!empty($user)) {
  67. $ca->user_id = $user->id;
  68. }
  69. $ca->address = $address;
  70. $ca->address_type = $addressType;
  71. $ca->address_extra = $extra;
  72. $ca->code = common_confirmation_code(64);
  73. $ca->insert();
  74. return $ca;
  75. }
  76. public function getAddress()
  77. {
  78. return $this->address;
  79. }
  80. public function getAddressType()
  81. {
  82. return $this->address_type;
  83. }
  84. public function getCode()
  85. {
  86. return $this->code;
  87. }
  88. public function getProfile()
  89. {
  90. return Profile::getByID($this->user_id);
  91. }
  92. public function getUrl()
  93. {
  94. return common_local_url('confirmaddress', array('code' => $this->code));
  95. }
  96. /**
  97. * Supply arguments in $args. Currently known args:
  98. * headers Array with headers (only used for email)
  99. * nickname How we great the user (defaults to nickname, but can be any string)
  100. * sitename Name we sign the email with (defaults to sitename, but can be any string)
  101. * url The confirmation address URL.
  102. */
  103. public function sendConfirmation(array $args = [])
  104. {
  105. common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type));
  106. $defaults = [
  107. 'headers' => [],
  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. switch ($this->getAddressType()) {
  118. case 'email':
  119. $this->sendEmailConfirmation($args);
  120. break;
  121. default:
  122. throw ServerException('Unable to handle confirm_address address type: '._ve($this->address_type));
  123. }
  124. }
  125. public function sendEmailConfirmation(array $args = [])
  126. {
  127. // TRANS: Subject for address confirmation email.
  128. $subject = _('Email address confirmation');
  129. // TRANS: Body for address confirmation email.
  130. // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename,
  131. // TRANS: %3$s is the URL to confirm at.
  132. $body = sprintf(
  133. _("Hey, %1\$s.\n\n" .
  134. "Someone just entered this email address on %2\$s.\n\n" .
  135. "If it was you, and you want to confirm your entry, ".
  136. "use the URL below:\n\n\t%3\$s\n\n" .
  137. "If not, just ignore this message.\n\n".
  138. "Thanks for your time, \n%2\$s\n"),
  139. $args['nickname'],
  140. $args['sitename'],
  141. $args['url']
  142. );
  143. require_once INSTALLDIR . '/lib/util/mail.php';
  144. return mail_to_user($this->getProfile()->getUser(), $subject, $body, $args['headers'], $this->getAddress());
  145. }
  146. public function delete($useWhere = false)
  147. {
  148. $result = parent::delete($useWhere);
  149. if ($result === false) {
  150. common_log_db_error($confirm, 'DELETE', __FILE__);
  151. // TRANS: Server error displayed when an address confirmation code deletion from the
  152. // TRANS: database fails in the contact address confirmation action.
  153. throw new ServerException(_('Could not delete address confirmation.'));
  154. }
  155. return $result;
  156. }
  157. }