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