]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Merge remote-tracking branch 'upstream/develop' into 1501-global-contacts
[friendica.git] / mod / poco.php
1 <?php
2
3 function poco_init(&$a) {
4         require_once("include/bbcode.php");
5
6         $system_mode = false;
7
8         if(intval(get_config('system','block_public')))
9                 http_status_exit(401);
10
11
12         if($a->argc > 1) {
13                 $user = notags(trim($a->argv[1]));
14         }
15         if(! x($user)) {
16                 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
17                 if(! count($c))
18                         http_status_exit(401);
19                 $system_mode = true;
20         }
21
22         $format = (($_GET['format']) ? $_GET['format'] : 'json');
23
24         $justme = false;
25
26         if($a->argc > 2 && $a->argv[2] === '@me')
27                 $justme = true;
28         if($a->argc > 3 && $a->argv[3] === '@all')
29                 $justme = false;
30         if($a->argc > 3 && $a->argv[3] === '@self')
31                 $justme = true;
32         if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
33                 $cid = intval($a->argv[4]);
34
35
36         if(! $system_mode) {
37                 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
38                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
39                         dbesc($user)
40                 );
41                 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
42                         http_status_exit(404);
43
44                 $user = $r[0];
45         }
46
47         if($justme)
48                 $sql_extra = " AND `contact`.`self` = 1 ";
49         else
50                 $sql_extra = " AND `contact`.`self` = 0 ";
51
52         if($cid)
53                 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
54
55         if($system_mode) {
56                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
57                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
58                         dbesc(NETWORK_DFRN),
59                         dbesc(NETWORK_DIASPORA),
60                         dbesc(NETWORK_OSTATUS),
61                         dbesc(NETWORK_STATUSNET)
62                         );
63         }
64         else {
65                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
66                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
67                         intval($user['uid']),
68                         dbesc(NETWORK_DFRN),
69                         dbesc(NETWORK_DIASPORA),
70                         dbesc(NETWORK_OSTATUS),
71                         dbesc(NETWORK_STATUSNET)
72                 );
73         }
74         if(count($r))
75                 $totalResults = intval($r[0]['total']);
76         else
77                 $totalResults = 0;
78
79         $startIndex = intval($_GET['startIndex']);
80         if(! $startIndex)
81                 $startIndex = 0;
82         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
83
84
85         if($system_mode) {
86                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation` FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
87                         WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
88                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
89                         dbesc(NETWORK_DFRN),
90                         dbesc(NETWORK_DIASPORA),
91                         dbesc(NETWORK_OSTATUS),
92                         dbesc(NETWORK_STATUSNET),
93                         intval($startIndex),
94                         intval($itemsPerPage)
95                 );
96         }
97         else {
98                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
99                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
100                         intval($user['uid']),
101                         dbesc(NETWORK_DFRN),
102                         dbesc(NETWORK_DIASPORA),
103                         dbesc(NETWORK_OSTATUS),
104                         dbesc(NETWORK_STATUSNET),
105                         intval($startIndex),
106                         intval($itemsPerPage)
107                 );
108         }
109         $ret = array();
110         if(x($_GET,'sorted'))
111                 $ret['sorted'] = 'false';
112         if(x($_GET,'filtered'))
113                 $ret['filtered'] = 'false';
114         if(x($_GET,'updatedSince'))
115                 $ret['updateSince'] = 'false';
116
117         $ret['startIndex']   = (string) $startIndex;
118         $ret['itemsPerPage'] = (string) $itemsPerPage;
119         $ret['totalResults'] = (string) $totalResults;
120         $ret['entry']        = array();
121
122
123         $fields_ret = array(
124                 'id' => false,
125                 'displayName' => false,
126                 'urls' => false,
127                 'updated' => false,
128                 'preferredUsername' => false,
129                 'photos' => false,
130                 'aboutMe' => false,
131                 'currentLocation' => false,
132                 'network' => false
133         );
134
135         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
136                 foreach($fields_ret as $k => $v)
137                         $fields_ret[$k] = true;
138         else {
139                 $fields_req = explode(',',$_GET['fields']);
140                 foreach($fields_req as $f)
141                         $fields_ret[trim($f)] = true;
142         }
143
144         if(is_array($r)) {
145                 if(count($r)) {
146                         foreach($r as $rr) {
147                                 if (($rr['about'] == "") AND isset($rr['pabout']))
148                                         $rr['about'] = $rr['pabout'];
149
150                                 if (($rr['location'] == "") AND isset($rr['plocation']))
151                                         $rr['location'] = $rr['plocation'];
152
153                                 $entry = array();
154                                 if($fields_ret['id'])
155                                         $entry['id'] = $rr['id'];
156                                 if($fields_ret['displayName'])
157                                         $entry['displayName'] = $rr['name'];
158                                 if($fields_ret['aboutMe'])
159                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
160                                 if($fields_ret['currentLocation'])
161                                         $entry['currentLocation'] = $rr['location'];
162                                 if($fields_ret['urls']) {
163                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
164                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
165                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
166                                 }
167                                 if($fields_ret['preferredUsername'])
168                                         $entry['preferredUsername'] = $rr['nick'];
169                                 if($fields_ret['updated']) {
170                                         $entry['updated'] = $rr['success_update'];
171
172                                         if ($rr['name-date'] > $entry['updated'])
173                                                 $entry['updated'] = $rr['name-date'];
174
175                                         if ($rr['uri-date'] > $entry['updated'])
176                                                 $entry['updated'] = $rr['uri-date'];
177
178                                         if ($rr['avatar-date'] > $entry['updated'])
179                                                 $entry['updated'] = $rr['avatar-date'];
180
181                                         $entry['updated'] = date("c", strtotime($entry['updated']));
182                                 }
183                                 if($fields_ret['photos'])
184                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
185                                 if($fields_ret['network']) {
186                                         $entry['network'] = $rr['network'];
187                                         if ($entry['network'] == NETWORK_STATUSNET)
188                                                 $entry['network'] = NETWORK_OSTATUS;
189                                         if (($entry['network'] == "") AND ($rr['self']))
190                                                 $entry['network'] = NETWORK_DFRN;
191                                 }
192                                 $ret['entry'][] = $entry;
193                         }
194                 }
195                 else
196                         $ret['entry'][] = array();
197         }
198         else
199                 http_status_exit(500);
200
201         if($format === 'xml') {
202                 header('Content-type: text/xml');
203                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
204                 killme();
205         }
206         if($format === 'json') {
207                 header('Content-type: application/json');
208                 echo json_encode($ret);
209                 killme();
210         }
211         else
212                 http_status_exit(500);
213
214
215 }