]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/accountmover.php
move account-moving classes to their own libraries
[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
49 {
50     private $_user    = null;
51     private $_profile = null;
52     private $_remote  = null;
53     private $_sink    = null;
54     
55     function __construct($user, $remote, $password)
56     {
57         $this->_user    = $user;
58         $this->_profile = $user->getProfile();
59
60         $oprofile = Ostatus_profile::ensureProfileURI($remote);
61
62         if (empty($oprofile)) {
63             throw new Exception("Can't locate account {$remote}");
64         }
65
66         $this->_remote = $oprofile->localProfile();
67
68         list($svcDocUrl, $username) = self::getServiceDocument($remote);
69
70         $this->_sink = new ActivitySink($svcDocUrl, $username, $password);
71     }
72
73     static function getServiceDocument($remote)
74     {
75         $discovery = new Discovery();
76
77         $xrd = $discovery->lookup($remote);
78
79         if (empty($xrd)) {
80             throw new Exception("Can't find XRD for $remote");
81         } 
82
83         $svcDocUrl = null;
84         $username  = null;
85
86         foreach ($xrd->links as $link) {
87             if ($link['rel'] == 'http://apinamespace.org/atom' &&
88                 $link['type'] == 'application/atomsvc+xml') {
89                 $svcDocUrl = $link['href'];
90                 if (!empty($link['property'])) {
91                     foreach ($link['property'] as $property) {
92                         if ($property['type'] == 'http://apinamespace.org/atom/username') {
93                             $username = $property['value'];
94                             break;
95                         }
96                     }
97                 }
98                 break;
99             }
100         }
101
102         if (empty($svcDocUrl)) {
103             throw new Exception("No AtomPub API service for $remote.");
104         }
105
106         return array($svcDocUrl, $username);
107     }
108
109     function move()
110     {
111         $stream = new UserActivityStream($this->_user);
112
113         $acts = array_reverse($stream->activities);
114
115         // Reverse activities to run in correct chron order
116
117         foreach ($acts as $act) {
118             $this->_moveActivity($act);
119         }
120     }
121
122     private function _moveActivity($act)
123     {
124         switch ($act->verb) {
125         case ActivityVerb::FAVORITE:
126             // push it, then delete local
127             $this->_sink->postActivity($act);
128             $notice = Notice::staticGet('uri', $act->objects[0]->id);
129             if (!empty($notice)) {
130                 $fave = Fave::pkeyGet(array('user_id' => $this->_user->id,
131                                             'notice_id' => $notice->id));
132                 $fave->delete();
133             }
134             break;
135         case ActivityVerb::POST:
136             // XXX: send a reshare, not a post
137             common_log(LOG_INFO, "Pushing notice {$act->objects[0]->id} to {$this->_remote->getURI()}");
138             $this->_sink->postActivity($act);
139             $notice = Notice::staticGet('uri', $act->objects[0]->id);
140             if (!empty($notice)) {
141                 $notice->delete();
142             }
143             break;
144         case ActivityVerb::JOIN:
145             $this->_sink->postActivity($act);
146             $group = User_group::staticGet('uri', $act->objects[0]->id);
147             if (!empty($group)) {
148                 Group_member::leave($group->id, $this->_user->id);
149             }
150             break;
151         case ActivityVerb::FOLLOW:
152             if ($act->actor->id == $this->_user->uri) {
153                 $this->_sink->postActivity($act);
154                 $other = Profile::fromURI($act->objects[0]->id);
155                 if (!empty($other)) {
156                     Subscription::cancel($this->_profile, $other);
157                 }
158             } else {
159                 $otherUser = User::staticGet('uri', $act->actor->id);
160                 if (!empty($otherUser)) {
161                     $otherProfile = $otherUser->getProfile();
162                     Subscription::start($otherProfile, $this->_remote);
163                     Subscription::cancel($otherProfile, $this->_user->getProfile());
164                 } else {
165                     // It's a remote subscription. Do something here!
166                 }
167             }
168             break;
169         }
170     }
171 }