]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apifriendshipsshow.php
Merge branch '0.9.x' into refactor-api
[quix0rs-gnu-social.git] / actions / apifriendshipsshow.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show information about the relationship between two users
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * Outputs detailed information about the relationship between two users
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiFriendshipsShowAction extends ApiBareAuthAction
47 {
48
49     var $format = null;
50     var $user   = null;
51     var $source = null;
52     var $target = null;
53
54     /**
55      * Take arguments for running
56      *
57      * @param array $args $_REQUEST args
58      *
59      * @return boolean success flag
60      *
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         if ($this->requiresAuth()) {
68             if ($this->checkBasicAuthUser() == false) {
69                 return;
70             }
71         }
72
73         $this->format = $this->arg('format');
74
75         $source_id          = (int)$this->trimmed('source_id');
76         $source_screen_name = $this->trimmed('source_screen_name');
77         $target_id          = (int)$this->trimmed('target_id');
78         $target_screen_name = $this->trimmed('target_screen_name');
79     
80         if (!empty($source_id)) {
81             $this->source = User::staticGet($source_id);
82         } elseif (!empty($source_screen_name)) {
83             $this->source = User::staticGet('nickname', $source_screen_name);
84         } else {
85             $this->source = $this->auth_user;
86         }
87
88         if (!empty($target_id)) {
89             $this->target = User::staticGet($target_id);
90         } elseif (!empty($target_screen_name)) {
91             $this->target = User::staticGet('nickname', $target_screen_name);
92         }
93
94         return true;
95     }
96
97
98     /**
99      * Determines whether this API resource requires auth.  Overloaded to look
100      * return true in case source_id and source_screen_name are both empty
101      *
102      * @return boolean true or false
103      */
104        
105     function requiresAuth()
106     {
107         if (common_config('site', 'private')) {
108             return true;
109         }
110
111         $source_id          = $this->trimmed('source_id');
112         $source_screen_name = $this->trimmed('source_screen_name');
113
114         if (empty($source_id) && empty($source_screen_name)) {
115             return true;
116         }
117
118         return false;
119     }
120
121     /**
122      * Handle the request
123      *
124      * Check the format and show the user info
125      *
126      * @param array $args $_REQUEST data (unused)
127      *
128      * @return void
129      */
130
131     function handle($args)
132     {
133         parent::handle($args);
134
135         if (!in_array($this->format, array('xml', 'json'))) {
136             $this->clientError(_('API method not found!'), 404);
137             return;
138         }
139         
140         if (empty($this->source)) {
141             $this->clientError(
142                 _('Could not determine source user.'),
143                 404
144              );
145             return;
146         }
147               
148         if (empty($this->target)) {
149             $this->clientError(
150                 _('Could not find target user.'),
151                 404
152             );
153             return;
154         }
155         
156         $result = $this->twitter_relationship_array($this->source, $this->target);
157
158         switch ($this->format) {
159         case 'xml':
160             $this->init_document('xml');
161             $this->show_twitter_xml_relationship($result[relationship]);
162             $this->end_document('xml');
163             break;
164         case 'json':
165             $this->init_document('json');
166             print json_encode($result);
167             $this->end_document('json');
168             break;
169         default:
170             break;
171         }
172         
173     }
174
175 }