]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/accountmover.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / accountmover.php
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
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Moves an account from this server to another
39  *
40  * @category  Account
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class AccountMover extends QueueHandler
48 {
49     function transport()
50     {
51         return 'acctmove';
52     }
53
54     function handle($object)
55     {
56         list($user, $remote, $password) = $object;
57
58         $remote = Discovery::normalize($remote);
59
60         $oprofile = Ostatus_profile::ensureProfileURI($remote);
61
62         if (empty($oprofile)) {
63             // TRANS: Exception thrown when an account could not be located when it should be moved.
64             // TRANS: %s is the remote site.
65             throw new Exception(sprintf(_("Cannot locate account %s."),$remote));
66         }
67
68         list($svcDocUrl, $username) = self::getServiceDocument($remote);
69
70         $sink = new ActivitySink($svcDocUrl, $username, $password);
71
72         $this->log(LOG_INFO,
73                    "Moving user {$user->nickname} ".
74                    "to {$remote}.");
75
76         $stream = new UserActivityStream($user);
77
78         // Reverse activities to run in correct chron order
79
80         $acts = array_reverse($stream->activities);
81
82         $this->log(LOG_INFO,
83                    "Got ".count($acts)." activities ".
84                    "for {$user->nickname}.");
85
86         $qm = QueueManager::get();
87
88         foreach ($acts as $act) {
89             $qm->enqueue(array($act, $sink, $user->getUri(), $remote), 'actmove');
90         }
91
92         $this->log(LOG_INFO,
93                    "Finished moving user {$user->nickname} ".
94                    "to {$remote}.");
95     }
96
97     static function getServiceDocument($remote)
98     {
99         $discovery = new Discovery();
100
101         $xrd = $discovery->lookup($remote);
102
103         if (empty($xrd)) {
104             // TRANS: Exception thrown when a service document could not be located account move.
105             // TRANS: %s is the remote site.
106             throw new Exception(sprintf(_("Cannot find XRD for %s."),$remote));
107         }
108
109         $svcDocUrl = null;
110         $username  = null;
111
112         $link = $xrd->links->get('http://apinamespace.org/atom', 'application/atomsvc+xml');
113         if (!is_null($link)) {
114             $svcDocUrl = $link->href;
115             if (isset($link['http://apinamespace.org/atom/username'])) {
116                 $username = $link['http://apinamespace.org/atom/username'];
117                 break;
118             }
119         }
120
121         if (empty($svcDocUrl)) {
122             // TRANS: Exception thrown when an account could not be located when it should be moved.
123             // TRANS: %s is the remote site.
124             throw new Exception(sprintf(_("No AtomPub API service for %s."),$remote));
125         }
126
127         return array($svcDocUrl, $username);
128     }
129
130     /**
131      * Log some data
132      *
133      * Add a header for our class so we know who did it.
134      *
135      * @param int    $level   Log level, like LOG_ERR or LOG_INFO
136      * @param string $message Message to log
137      *
138      * @return void
139      */
140     protected function log($level, $message)
141     {
142         common_log($level, "AccountMover: " . $message);
143     }
144 }