Confirm_address.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(4) not_null
  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', 'default' => 0, '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. );
  49. }
  50. public static function getByAddress($address, $addressType)
  51. {
  52. $ca = new Confirm_address();
  53. $ca->address = $address;
  54. $ca->address_type = $addressType;
  55. if (!$ca->find(true)) {
  56. throw new NoResultException($ca);
  57. }
  58. return $ca;
  59. }
  60. public static function saveNew($user, $address, $addressType, $extra = null)
  61. {
  62. $ca = new Confirm_address();
  63. if (!empty($user)) {
  64. $ca->user_id = $user->id;
  65. }
  66. $ca->address = $address;
  67. $ca->address_type = $addressType;
  68. $ca->address_extra = $extra;
  69. $ca->code = common_confirmation_code(64);
  70. $ca->insert();
  71. return $ca;
  72. }
  73. public function getAddress()
  74. {
  75. return $this->address;
  76. }
  77. public function getAddressType()
  78. {
  79. return $this->address_type;
  80. }
  81. public function getCode()
  82. {
  83. return $this->code;
  84. }
  85. public function getProfile()
  86. {
  87. return Profile::getByID($this->user_id);
  88. }
  89. public function getUrl()
  90. {
  91. return common_local_url('confirmaddress', array('code' => $this->code));
  92. }
  93. /**
  94. * Supply arguments in $args. Currently known args:
  95. * headers Array with headers (only used for email)
  96. * nickname How we great the user (defaults to nickname, but can be any string)
  97. * sitename Name we sign the email with (defaults to sitename, but can be any string)
  98. * url The confirmation address URL.
  99. */
  100. public function sendConfirmation(array $args = [])
  101. {
  102. common_debug('Sending confirmation URL for user '._ve($this->user_id).' using '._ve($this->address_type));
  103. $defaults = [
  104. 'headers' => [],
  105. 'nickname' => $this->getProfile()->getNickname(),
  106. 'sitename' => common_config('site', 'name'),
  107. 'url' => $this->getUrl(),
  108. ];
  109. foreach (array_keys($defaults) as $key) {
  110. if (!isset($args[$key])) {
  111. $args[$key] = $defaults[$key];
  112. }
  113. }
  114. switch ($this->getAddressType()) {
  115. case 'email':
  116. $this->sendEmailConfirmation($args);
  117. break;
  118. default:
  119. throw ServerException('Unable to handle confirm_address address type: '._ve($this->address_type));
  120. }
  121. }
  122. public function sendEmailConfirmation(array $args = [])
  123. {
  124. // TRANS: Subject for address confirmation email.
  125. $subject = _('Email address confirmation');
  126. // TRANS: Body for address confirmation email.
  127. // TRANS: %1$s is the addressed user's nickname, %2$s is the StatusNet sitename,
  128. // TRANS: %3$s is the URL to confirm at.
  129. $body = sprintf(
  130. _("Hey, %1\$s.\n\n" .
  131. "Someone just entered this email address on %2\$s.\n\n" .
  132. "If it was you, and you want to confirm your entry, ".
  133. "use the URL below:\n\n\t%3\$s\n\n" .
  134. "If not, just ignore this message.\n\n".
  135. "Thanks for your time, \n%2\$s\n"),
  136. $args['nickname'],
  137. $args['sitename'],
  138. $args['url']
  139. );
  140. require_once INSTALLDIR . '/lib/util/mail.php';
  141. return mail_to_user($this->getProfile()->getUser(), $subject, $body, $args['headers'], $this->getAddress());
  142. }
  143. public function delete($useWhere = false)
  144. {
  145. $result = parent::delete($useWhere);
  146. if ($result === false) {
  147. common_log_db_error($confirm, 'DELETE', __FILE__);
  148. // TRANS: Server error displayed when an address confirmation code deletion from the
  149. // TRANS: database fails in the contact address confirmation action.
  150. throw new ServerException(_('Could not delete address confirmation.'));
  151. }
  152. return $result;
  153. }
  154. }