]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/accountmover.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into testing
[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
48 class AccountMover extends QueueHandler
49 {
50     function transport()
51     {
52         return 'acctmove';
53     }
54
55     function handle($object)
56     {
57         list($user, $remote, $password) = $object;
58
59         $remote = Discovery::normalize($remote);
60
61         $oprofile = Ostatus_profile::ensureProfileURI($remote);
62
63         if (empty($oprofile)) {
64             throw new Exception("Can't locate account {$remote}");
65         }
66
67         list($svcDocUrl, $username) = self::getServiceDocument($remote);
68
69         $sink = new ActivitySink($svcDocUrl, $username, $password);
70
71         $this->log(LOG_INFO, 
72                    "Moving user {$user->nickname} ".
73                    "to {$remote}.");
74
75         $stream = new UserActivityStream($user);
76
77         // Reverse activities to run in correct chron order
78
79         $acts = array_reverse($stream->activities);
80
81         $this->log(LOG_INFO,
82                    "Got ".count($acts)." activities ".
83                    "for {$user->nickname}.");
84
85         $qm = QueueManager::get();
86
87         foreach ($acts as $act) {
88             $qm->enqueue(array($act, $sink, $user->uri, $remote), 'actmove');
89         }
90
91         $this->log(LOG_INFO,
92                    "Finished moving user {$user->nickname} ".
93                    "to {$remote}.");
94     }
95
96     static function getServiceDocument($remote)
97     {
98         $discovery = new Discovery();
99
100         $xrd = $discovery->lookup($remote);
101
102         if (empty($xrd)) {
103             throw new Exception("Can't find XRD for $remote");
104         } 
105
106         $svcDocUrl = null;
107         $username  = null;
108
109         foreach ($xrd->links as $link) {
110             if ($link['rel'] == 'http://apinamespace.org/atom' &&
111                 $link['type'] == 'application/atomsvc+xml') {
112                 $svcDocUrl = $link['href'];
113                 if (!empty($link['property'])) {
114                     foreach ($link['property'] as $property) {
115                         if ($property['type'] == 'http://apinamespace.org/atom/username') {
116                             $username = $property['value'];
117                             break;
118                         }
119                     }
120                 }
121                 break;
122             }
123         }
124
125         if (empty($svcDocUrl)) {
126             throw new Exception("No AtomPub API service for $remote.");
127         }
128
129         return array($svcDocUrl, $username);
130     }
131
132     /**
133      * Log some data
134      * 
135      * Add a header for our class so we know who did it.
136      *
137      * @param int    $level   Log level, like LOG_ERR or LOG_INFO
138      * @param string $message Message to log
139      *
140      * @return void
141      */
142
143     protected function log($level, $message)
144     {
145         common_log($level, "AccountMover: " . $message);
146     }
147 }