accountmover.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * A class for moving an account to a new server
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Account
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Moves an account from this server to another
  37. *
  38. * @category Account
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class AccountMover extends QueueHandler
  46. {
  47. public $widgetOpts;
  48. public $scoped;
  49. function transport()
  50. {
  51. return 'acctmove';
  52. }
  53. function handle($object) : bool
  54. {
  55. list($user, $remote, $password) = $object;
  56. $remote = Discovery::normalize($remote);
  57. $oprofile = Ostatus_profile::ensureProfileURI($remote);
  58. if (empty($oprofile)) {
  59. // TRANS: Exception thrown when an account could not be located when it should be moved.
  60. // TRANS: %s is the remote site.
  61. throw new Exception(sprintf(_("Cannot locate account %s."),$remote));
  62. }
  63. list($svcDocUrl, $username) = self::getServiceDocument($remote);
  64. $sink = new ActivitySink($svcDocUrl, $username, $password);
  65. $this->log(LOG_INFO,
  66. "Moving user {$user->nickname} ".
  67. "to {$remote}.");
  68. $stream = new UserActivityStream($user);
  69. // Reverse activities to run in correct chron order
  70. $acts = array_reverse($stream->activities);
  71. $this->log(LOG_INFO,
  72. "Got ".count($acts)." activities ".
  73. "for {$user->nickname}.");
  74. $qm = QueueManager::get();
  75. foreach ($acts as $act) {
  76. $qm->enqueue(array($act, $sink, $user->getUri(), $remote), 'actmove');
  77. }
  78. $this->log(LOG_INFO,
  79. "Finished moving user {$user->nickname} ".
  80. "to {$remote}.");
  81. }
  82. static function getServiceDocument($remote)
  83. {
  84. $discovery = new Discovery();
  85. $xrd = $discovery->lookup($remote);
  86. if (empty($xrd)) {
  87. // TRANS: Exception thrown when a service document could not be located account move.
  88. // TRANS: %s is the remote site.
  89. throw new Exception(sprintf(_("Cannot find XRD for %s."),$remote));
  90. }
  91. $svcDocUrl = null;
  92. $username = null;
  93. $link = $xrd->links->get('http://apinamespace.org/atom', 'application/atomsvc+xml');
  94. if (!is_null($link)) {
  95. $svcDocUrl = $link->href;
  96. if (isset($link['http://apinamespace.org/atom/username'])) {
  97. $username = $link['http://apinamespace.org/atom/username'];
  98. }
  99. }
  100. if (empty($svcDocUrl)) {
  101. // TRANS: Exception thrown when an account could not be located when it should be moved.
  102. // TRANS: %s is the remote site.
  103. throw new Exception(sprintf(_("No AtomPub API service for %s."),$remote));
  104. }
  105. return array($svcDocUrl, $username);
  106. }
  107. /**
  108. * Log some data
  109. *
  110. * Add a header for our class so we know who did it.
  111. *
  112. * @param int $level Log level, like LOG_ERR or LOG_INFO
  113. * @param string $message Message to log
  114. *
  115. * @return void
  116. */
  117. protected function log($level, $message)
  118. {
  119. common_log($level, "AccountMover: " . $message);
  120. }
  121. }