]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Don't export contacts with empty network
[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 AND `network` IN ('%s', '%s', '%s', '%s')
73                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
74                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
75                         dbesc(NETWORK_DFRN),
76                         dbesc(NETWORK_DIASPORA),
77                         dbesc(NETWORK_OSTATUS),
78                         dbesc(NETWORK_STATUSNET)
79                         );
80         } else {
81                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
82                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
83                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
84                         intval($user['uid']),
85                         dbesc(NETWORK_DFRN),
86                         dbesc(NETWORK_DIASPORA),
87                         dbesc(NETWORK_OSTATUS),
88                         dbesc(NETWORK_STATUSNET)
89                 );
90         }
91         if(count($r))
92                 $totalResults = intval($r[0]['total']);
93         else
94                 $totalResults = 0;
95
96         $startIndex = intval($_GET['startIndex']);
97         if(! $startIndex)
98                 $startIndex = 0;
99         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
100
101
102         if ($global) {
103                 //$r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND ((`last_contact` >= `last_failure`) OR (`updated` > `last_failure`)) LIMIT %d, %d",
104                 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s', '%1', '%1') and `updated` > `last_failure` LIMIT %d, %d",
105                         dbesc($update_limit),
106                         dbesc(NETWORK_DFRN),
107                         dbesc(NETWORK_DIASPORA),
108                         dbesc(NETWORK_OSTATUS),
109                         intval($startIndex),
110                         intval($itemsPerPage)
111                 );
112         } elseif($system_mode) {
113                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
114                         `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
115                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
116                         WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s') AND `profile`.`is-default`
117                         AND ((`contact`.`success_update` >= `contact`.`failure_update`) OR (`contact`.`last-item` >= `contact`.`failure_update`))
118                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
119                         dbesc(NETWORK_DFRN),
120                         dbesc(NETWORK_DIASPORA),
121                         dbesc(NETWORK_OSTATUS),
122                         dbesc(NETWORK_STATUSNET),
123                         intval($startIndex),
124                         intval($itemsPerPage)
125                 );
126         } else {
127                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
128                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
129                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
130                         intval($user['uid']),
131                         dbesc(NETWORK_DFRN),
132                         dbesc(NETWORK_DIASPORA),
133                         dbesc(NETWORK_OSTATUS),
134                         dbesc(NETWORK_STATUSNET),
135                         intval($startIndex),
136                         intval($itemsPerPage)
137                 );
138         }
139
140         $ret = array();
141         if(x($_GET,'sorted'))
142                 $ret['sorted'] = false;
143         if(x($_GET,'filtered'))
144                 $ret['filtered'] = false;
145         if(x($_GET,'updatedSince') AND !$global)
146                 $ret['updatedSince'] = false;
147
148         $ret['startIndex']   = (int) $startIndex;
149         $ret['itemsPerPage'] = (int) $itemsPerPage;
150         $ret['totalResults'] = (int) $totalResults;
151         $ret['entry']        = array();
152
153
154         $fields_ret = array(
155                 'id' => false,
156                 'displayName' => false,
157                 'urls' => false,
158                 'updated' => false,
159                 'preferredUsername' => false,
160                 'photos' => false,
161                 'aboutMe' => false,
162                 'currentLocation' => false,
163                 'network' => false,
164                 'gender' => false,
165                 'tags' => false,
166                 'address' => false,
167                 'generation' => false
168         );
169
170         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
171                 foreach($fields_ret as $k => $v)
172                         $fields_ret[$k] = true;
173         else {
174                 $fields_req = explode(',',$_GET['fields']);
175                 foreach($fields_req as $f)
176                         $fields_ret[trim($f)] = true;
177         }
178
179         if(is_array($r)) {
180                 if(count($r)) {
181                         foreach($r as $rr) {
182                                 if (!isset($rr['generation'])) {
183                                         if ($global)
184                                                 $rr['generation'] = 3;
185                                         elseif ($system_mode)
186                                                 $rr['generation'] = 1;
187                                         else
188                                                 $rr['generation'] = 2;
189                                 }
190
191                                 if (($rr['about'] == "") AND isset($rr['pabout']))
192                                         $rr['about'] = $rr['pabout'];
193
194                                 if ($rr['location'] == "") {
195                                         if (isset($rr['plocation']))
196                                                 $rr['location'] = $rr['plocation'];
197
198                                         if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
199                                                 if ($rr['location'] != "")
200                                                         $rr['location'] .= ", ";
201
202                                                 $rr['location'] .= $rr['pregion'];
203                                         }
204
205                                         if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
206                                                 if ($rr['location'] != "")
207                                                         $rr['location'] .= ", ";
208
209                                                 $rr['location'] .= $rr['pcountry'];
210                                         }
211                                 }
212
213                                 if (($rr['gender'] == "") AND isset($rr['pgender']))
214                                         $rr['gender'] = $rr['pgender'];
215
216                                 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
217                                         $rr['keywords'] = $rr['pub_keywords'];
218
219                                 $entry = array();
220                                 if($fields_ret['id'])
221                                         $entry['id'] = (int)$rr['id'];
222                                 if($fields_ret['displayName'])
223                                         $entry['displayName'] = $rr['name'];
224                                 if($fields_ret['aboutMe'])
225                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
226                                 if($fields_ret['currentLocation'])
227                                         $entry['currentLocation'] = $rr['location'];
228                                 if($fields_ret['gender'])
229                                         $entry['gender'] = $rr['gender'];
230                                 if($fields_ret['generation'])
231                                         $entry['generation'] = (int)$rr['generation'];
232                                 if($fields_ret['urls']) {
233                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
234                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
235                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
236                                 }
237                                 if($fields_ret['preferredUsername'])
238                                         $entry['preferredUsername'] = $rr['nick'];
239                                 if($fields_ret['updated']) {
240                                         if (!$global) {
241                                                 $entry['updated'] = $rr['success_update'];
242
243                                                 if ($rr['name-date'] > $entry['updated'])
244                                                         $entry['updated'] = $rr['name-date'];
245
246                                                 if ($rr['uri-date'] > $entry['updated'])
247                                                         $entry['updated'] = $rr['uri-date'];
248
249                                                 if ($rr['avatar-date'] > $entry['updated'])
250                                                         $entry['updated'] = $rr['avatar-date'];
251                                         } else
252                                                 $entry['updated'] = $rr['updated'];
253
254                                         $entry['updated'] = date("c", strtotime($entry['updated']));
255                                 }
256                                 if($fields_ret['photos'])
257                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
258                                 if($fields_ret['network']) {
259                                         $entry['network'] = $rr['network'];
260                                         if ($entry['network'] == NETWORK_STATUSNET)
261                                                 $entry['network'] = NETWORK_OSTATUS;
262                                         if (($entry['network'] == "") AND ($rr['self']))
263                                                 $entry['network'] = NETWORK_DFRN;
264                                 }
265                                 if($fields_ret['tags']) {
266                                         $tags = str_replace(","," ",$rr['keywords']);
267                                         $tags = explode(" ", $tags);
268
269                                         $cleaned = array();
270                                         foreach ($tags as $tag) {
271                                                 $tag = trim(strtolower($tag));
272                                                 if ($tag != "")
273                                                         $cleaned[] = $tag;
274                                         }
275
276                                         $entry['tags'] = array($cleaned);
277                                 }
278                                 if($fields_ret['address']) {
279                                         $entry['address'] = array();
280
281                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
282                                         //if (isset($rr['paddress']))
283                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
284
285                                         if (isset($rr['plocation']))
286                                                  $entry['address']['locality'] = $rr['plocation'];
287
288                                         if (isset($rr['pregion']))
289                                                  $entry['address']['region'] = $rr['pregion'];
290
291                                         // See above
292                                         //if (isset($rr['ppostalcode']))
293                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
294
295                                         if (isset($rr['pcountry']))
296                                                  $entry['address']['country'] = $rr['pcountry'];
297                                 }
298
299                                 $ret['entry'][] = $entry;
300                         }
301                 }
302                 else
303                         $ret['entry'][] = array();
304         }
305         else
306                 http_status_exit(500);
307
308         if($format === 'xml') {
309                 header('Content-type: text/xml');
310                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
311                 killme();
312         }
313         if($format === 'json') {
314                 header('Content-type: application/json');
315                 echo json_encode($ret);
316                 killme();
317         }
318         else
319                 http_status_exit(500);
320
321
322 }