]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Merge pull request #2148 from annando/issue-1871
[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')) || (get_config('system','block_local_dir')))
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         $global = false;
26
27         if($a->argc > 1 && $a->argv[1] === '@global') {
28                 $global = true;
29                 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
30         }
31         if($a->argc > 2 && $a->argv[2] === '@me')
32                 $justme = true;
33         if($a->argc > 3 && $a->argv[3] === '@all')
34                 $justme = false;
35         if($a->argc > 3 && $a->argv[3] === '@self')
36                 $justme = true;
37         if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
38                 $cid = intval($a->argv[4]);
39
40
41         if(!$system_mode AND !$global) {
42                 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
43                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
44                         dbesc($user)
45                 );
46                 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
47                         http_status_exit(404);
48
49                 $user = $r[0];
50         }
51
52         if($justme)
53                 $sql_extra = " AND `contact`.`self` = 1 ";
54 //      else
55 //              $sql_extra = " AND `contact`.`self` = 0 ";
56
57         if($cid)
58                 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
59
60         if(x($_GET,'updatedSince'))
61                 $update_limit =  date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
62
63         if ($global) {
64                 //$r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))  AND `network` IN ('%s')",
65                 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure`  AND `network` IN ('%s', '%s', '%s')",
66                         dbesc($update_limit),
67                         dbesc(NETWORK_DFRN),
68                         dbesc(NETWORK_DIASPORA),
69                         dbesc(NETWORK_OSTATUS)
70                 );
71         } elseif($system_mode) {
72                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
73                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
74         } else {
75                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
76                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
77                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
78                         intval($user['uid']),
79                         dbesc(NETWORK_DFRN),
80                         dbesc(NETWORK_DIASPORA),
81                         dbesc(NETWORK_OSTATUS),
82                         dbesc(NETWORK_STATUSNET)
83                 );
84         }
85         if(count($r))
86                 $totalResults = intval($r[0]['total']);
87         else
88                 $totalResults = 0;
89
90         $startIndex = intval($_GET['startIndex']);
91         if(! $startIndex)
92                 $startIndex = 0;
93         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
94
95         if ($global) {
96                 logger("Start global query", LOGGER_DEBUG);
97                 //$r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND ((`last_contact` >= `last_failure`) OR (`updated` > `last_failure`)) LIMIT %d, %d",
98                 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
99                         ORDER BY `updated` DESC LIMIT %d, %d",
100                         dbesc($update_limit),
101                         dbesc(NETWORK_DFRN),
102                         dbesc(NETWORK_DIASPORA),
103                         dbesc(NETWORK_OSTATUS),
104                         intval($startIndex),
105                         intval($itemsPerPage)
106                 );
107         } elseif($system_mode) {
108                 logger("Start system mode query", LOGGER_DEBUG);
109                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
110                         `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
111                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
112                         WHERE `self` = 1 AND `profile`.`is-default`
113                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
114                         intval($startIndex),
115                         intval($itemsPerPage)
116                 );
117         } else {
118                 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
119                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
120                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
121                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
122                         intval($user['uid']),
123                         dbesc(NETWORK_DFRN),
124                         dbesc(NETWORK_DIASPORA),
125                         dbesc(NETWORK_OSTATUS),
126                         dbesc(NETWORK_STATUSNET),
127                         intval($startIndex),
128                         intval($itemsPerPage)
129                 );
130         }
131         logger("Query done", LOGGER_DEBUG);
132
133         $ret = array();
134         if(x($_GET,'sorted'))
135                 $ret['sorted'] = false;
136         if(x($_GET,'filtered'))
137                 $ret['filtered'] = false;
138         if(x($_GET,'updatedSince') AND !$global)
139                 $ret['updatedSince'] = false;
140
141         $ret['startIndex']   = (int) $startIndex;
142         $ret['itemsPerPage'] = (int) $itemsPerPage;
143         $ret['totalResults'] = (int) $totalResults;
144         $ret['entry']        = array();
145
146
147         $fields_ret = array(
148                 'id' => false,
149                 'displayName' => false,
150                 'urls' => false,
151                 'updated' => false,
152                 'preferredUsername' => false,
153                 'photos' => false,
154                 'aboutMe' => false,
155                 'currentLocation' => false,
156                 'network' => false,
157                 'gender' => false,
158                 'tags' => false,
159                 'address' => false,
160                 'generation' => false
161         );
162
163         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
164                 foreach($fields_ret as $k => $v)
165                         $fields_ret[$k] = true;
166         else {
167                 $fields_req = explode(',',$_GET['fields']);
168                 foreach($fields_req as $f)
169                         $fields_ret[trim($f)] = true;
170         }
171
172         if(is_array($r)) {
173                 if(count($r)) {
174                         foreach($r as $rr) {
175                                 if (!isset($rr['generation'])) {
176                                         if ($global)
177                                                 $rr['generation'] = 3;
178                                         elseif ($system_mode)
179                                                 $rr['generation'] = 1;
180                                         else
181                                                 $rr['generation'] = 2;
182                                 }
183
184                                 if (($rr['about'] == "") AND isset($rr['pabout']))
185                                         $rr['about'] = $rr['pabout'];
186
187                                 if ($rr['location'] == "") {
188                                         if (isset($rr['plocation']))
189                                                 $rr['location'] = $rr['plocation'];
190
191                                         if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
192                                                 if ($rr['location'] != "")
193                                                         $rr['location'] .= ", ";
194
195                                                 $rr['location'] .= $rr['pregion'];
196                                         }
197
198                                         if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
199                                                 if ($rr['location'] != "")
200                                                         $rr['location'] .= ", ";
201
202                                                 $rr['location'] .= $rr['pcountry'];
203                                         }
204                                 }
205
206                                 if (($rr['gender'] == "") AND isset($rr['pgender']))
207                                         $rr['gender'] = $rr['pgender'];
208
209                                 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
210                                         $rr['keywords'] = $rr['pub_keywords'];
211
212                                 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
213                                 if (is_null($about)) {
214                                         $about = bbcode($rr['about'], false, false);
215                                         Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
216                                 }
217
218                                 // Non connected persons can only see the keywords of a Diaspora account
219                                 if ($rr['network'] == NETWORK_DIASPORA) {
220                                         $rr['location'] = "";
221                                         $about = "";
222                                         $rr['gender'] = "";
223                                 }
224
225                                 $entry = array();
226                                 if($fields_ret['id'])
227                                         $entry['id'] = (int)$rr['id'];
228                                 if($fields_ret['displayName'])
229                                         $entry['displayName'] = $rr['name'];
230                                 if($fields_ret['aboutMe'])
231                                         $entry['aboutMe'] = $about;
232                                 if($fields_ret['currentLocation'])
233                                         $entry['currentLocation'] = $rr['location'];
234                                 if($fields_ret['gender'])
235                                         $entry['gender'] = $rr['gender'];
236                                 if($fields_ret['generation'])
237                                         $entry['generation'] = (int)$rr['generation'];
238                                 if($fields_ret['urls']) {
239                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
240                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
241                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
242                                 }
243                                 if($fields_ret['preferredUsername'])
244                                         $entry['preferredUsername'] = $rr['nick'];
245                                 if($fields_ret['updated']) {
246                                         if (!$global) {
247                                                 $entry['updated'] = $rr['success_update'];
248
249                                                 if ($rr['name-date'] > $entry['updated'])
250                                                         $entry['updated'] = $rr['name-date'];
251
252                                                 if ($rr['uri-date'] > $entry['updated'])
253                                                         $entry['updated'] = $rr['uri-date'];
254
255                                                 if ($rr['avatar-date'] > $entry['updated'])
256                                                         $entry['updated'] = $rr['avatar-date'];
257                                         } else
258                                                 $entry['updated'] = $rr['updated'];
259
260                                         $entry['updated'] = date("c", strtotime($entry['updated']));
261                                 }
262                                 if($fields_ret['photos'])
263                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
264                                 if($fields_ret['network']) {
265                                         $entry['network'] = $rr['network'];
266                                         if ($entry['network'] == NETWORK_STATUSNET)
267                                                 $entry['network'] = NETWORK_OSTATUS;
268                                         if (($entry['network'] == "") AND ($rr['self']))
269                                                 $entry['network'] = NETWORK_DFRN;
270                                 }
271                                 if($fields_ret['tags']) {
272                                         $tags = str_replace(","," ",$rr['keywords']);
273                                         $tags = explode(" ", $tags);
274
275                                         $cleaned = array();
276                                         foreach ($tags as $tag) {
277                                                 $tag = trim(strtolower($tag));
278                                                 if ($tag != "")
279                                                         $cleaned[] = $tag;
280                                         }
281
282                                         $entry['tags'] = array($cleaned);
283                                 }
284                                 if($fields_ret['address']) {
285                                         $entry['address'] = array();
286
287                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
288                                         //if (isset($rr['paddress']))
289                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
290
291                                         if (isset($rr['plocation']))
292                                                  $entry['address']['locality'] = $rr['plocation'];
293
294                                         if (isset($rr['pregion']))
295                                                  $entry['address']['region'] = $rr['pregion'];
296
297                                         // See above
298                                         //if (isset($rr['ppostalcode']))
299                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
300
301                                         if (isset($rr['pcountry']))
302                                                  $entry['address']['country'] = $rr['pcountry'];
303                                 }
304
305                                 $ret['entry'][] = $entry;
306                         }
307                 }
308                 else
309                         $ret['entry'][] = array();
310         }
311         else
312                 http_status_exit(500);
313
314         logger("End of poco", LOGGER_DEBUG);
315
316         if($format === 'xml') {
317                 header('Content-type: text/xml');
318                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
319                 killme();
320         }
321         if($format === 'json') {
322                 header('Content-type: application/json');
323                 echo json_encode($ret);
324                 killme();
325         }
326         else
327                 http_status_exit(500);
328
329
330 }