]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Poco: Option to activate and deactivate detection of last activity.
[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` AND `network` IN ('%s')",
65                         dbesc($update_limit),
66                         dbesc(NETWORK_DFRN)
67                 );
68         } elseif($system_mode) {
69                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
70                         AND `success_update` >= `failure_update`
71                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
72                         dbesc(NETWORK_DFRN),
73                         dbesc(NETWORK_DIASPORA),
74                         dbesc(NETWORK_OSTATUS),
75                         dbesc(NETWORK_STATUSNET)
76                         );
77         } else {
78                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
79                         AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
80                         intval($user['uid']),
81                         dbesc(NETWORK_DFRN),
82                         dbesc(NETWORK_DIASPORA),
83                         dbesc(NETWORK_OSTATUS),
84                         dbesc(NETWORK_STATUSNET)
85                 );
86         }
87         if(count($r))
88                 $totalResults = intval($r[0]['total']);
89         else
90                 $totalResults = 0;
91
92         $startIndex = intval($_GET['startIndex']);
93         if(! $startIndex)
94                 $startIndex = 0;
95         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
96
97
98         if ($global) {
99                 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND `last_contact` >= `last_failure` LIMIT %d, %d",
100                         dbesc($update_limit),
101                         dbesc(NETWORK_DFRN),
102                         intval($startIndex),
103                         intval($itemsPerPage)
104                 );
105         } elseif($system_mode) {
106                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
107                         `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
108                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
109                         WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
110                         AND `contact`.`success_update` >= `contact`.`failure_update`
111                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
112                         dbesc(NETWORK_DFRN),
113                         dbesc(NETWORK_DIASPORA),
114                         dbesc(NETWORK_OSTATUS),
115                         dbesc(NETWORK_STATUSNET),
116                         intval($startIndex),
117                         intval($itemsPerPage)
118                 );
119         } else {
120                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
121                         AND `success_update` >= `failure_update` 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
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                                 $entry = array();
212                                 if($fields_ret['id'])
213                                         $entry['id'] = (int)$rr['id'];
214                                 if($fields_ret['displayName'])
215                                         $entry['displayName'] = $rr['name'];
216                                 if($fields_ret['aboutMe'])
217                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
218                                 if($fields_ret['currentLocation'])
219                                         $entry['currentLocation'] = $rr['location'];
220                                 if($fields_ret['gender'])
221                                         $entry['gender'] = $rr['gender'];
222                                 if($fields_ret['generation'])
223                                         $entry['generation'] = (int)$rr['generation'];
224                                 if($fields_ret['urls']) {
225                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
226                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
227                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
228                                 }
229                                 if($fields_ret['preferredUsername'])
230                                         $entry['preferredUsername'] = $rr['nick'];
231                                 if($fields_ret['updated']) {
232                                         if (!$global) {
233                                                 $entry['updated'] = $rr['success_update'];
234
235                                                 if ($rr['name-date'] > $entry['updated'])
236                                                         $entry['updated'] = $rr['name-date'];
237
238                                                 if ($rr['uri-date'] > $entry['updated'])
239                                                         $entry['updated'] = $rr['uri-date'];
240
241                                                 if ($rr['avatar-date'] > $entry['updated'])
242                                                         $entry['updated'] = $rr['avatar-date'];
243                                         } else
244                                                 $entry['updated'] = $rr['updated'];
245
246                                         $entry['updated'] = date("c", strtotime($entry['updated']));
247                                 }
248                                 if($fields_ret['photos'])
249                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
250                                 if($fields_ret['network']) {
251                                         $entry['network'] = $rr['network'];
252                                         if ($entry['network'] == NETWORK_STATUSNET)
253                                                 $entry['network'] = NETWORK_OSTATUS;
254                                         if (($entry['network'] == "") AND ($rr['self']))
255                                                 $entry['network'] = NETWORK_DFRN;
256                                 }
257                                 if($fields_ret['tags']) {
258                                         $tags = str_replace(","," ",$rr['keywords']);
259                                         $tags = explode(" ", $tags);
260
261                                         $cleaned = array();
262                                         foreach ($tags as $tag) {
263                                                 $tag = trim(strtolower($tag));
264                                                 if ($tag != "")
265                                                         $cleaned[] = $tag;
266                                         }
267
268                                         $entry['tags'] = array($cleaned);
269                                 }
270                                 if($fields_ret['address']) {
271                                         $entry['address'] = array();
272
273                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
274                                         //if (isset($rr['paddress']))
275                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
276
277                                         if (isset($rr['plocation']))
278                                                  $entry['address']['locality'] = $rr['plocation'];
279
280                                         if (isset($rr['pregion']))
281                                                  $entry['address']['region'] = $rr['pregion'];
282
283                                         // See above
284                                         //if (isset($rr['ppostalcode']))
285                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
286
287                                         if (isset($rr['pcountry']))
288                                                  $entry['address']['country'] = $rr['pcountry'];
289                                 }
290
291                                 $ret['entry'][] = $entry;
292                         }
293                 }
294                 else
295                         $ret['entry'][] = array();
296         }
297         else
298                 http_status_exit(500);
299
300         if($format === 'xml') {
301                 header('Content-type: text/xml');
302                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
303                 killme();
304         }
305         if($format === 'json') {
306                 header('Content-type: application/json');
307                 echo json_encode($ret);
308                 killme();
309         }
310         else
311                 http_status_exit(500);
312
313
314 }