]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Poco now shows "about" and the location as well.
[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 * FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
87                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
88                         dbesc(NETWORK_DFRN),
89                         dbesc(NETWORK_DIASPORA),
90                         dbesc(NETWORK_OSTATUS),
91                         dbesc(NETWORK_STATUSNET),
92                         intval($startIndex),
93                         intval($itemsPerPage)
94                 );
95         }
96         else {
97                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
98                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
99                         intval($user['uid']),
100                         dbesc(NETWORK_DFRN),
101                         dbesc(NETWORK_DIASPORA),
102                         dbesc(NETWORK_OSTATUS),
103                         dbesc(NETWORK_STATUSNET),
104                         intval($startIndex),
105                         intval($itemsPerPage)
106                 );
107         }
108         $ret = array();
109         if(x($_GET,'sorted'))
110                 $ret['sorted'] = 'false';
111         if(x($_GET,'filtered'))
112                 $ret['filtered'] = 'false';
113         if(x($_GET,'updatedSince'))
114                 $ret['updateSince'] = 'false';
115
116         $ret['startIndex']   = (string) $startIndex;
117         $ret['itemsPerPage'] = (string) $itemsPerPage;
118         $ret['totalResults'] = (string) $totalResults;
119         $ret['entry']        = array();
120
121
122         $fields_ret = array(
123                 'id' => false,
124                 'displayName' => false,
125                 'urls' => false,
126                 'updated' => false,
127                 'preferredUsername' => false,
128                 'photos' => false,
129                 'aboutMe' => false,
130                 'currentLocation' => false,
131                 'network' => false
132         );
133
134         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
135                 foreach($fields_ret as $k => $v)
136                         $fields_ret[$k] = true;
137         else {
138                 $fields_req = explode(',',$_GET['fields']);
139                 foreach($fields_req as $f)
140                         $fields_ret[trim($f)] = true;
141         }
142
143         if(is_array($r)) {
144                 if(count($r)) {
145                         foreach($r as $rr) {
146                                 $entry = array();
147                                 if($fields_ret['id'])
148                                         $entry['id'] = $rr['id'];
149                                 if($fields_ret['displayName'])
150                                         $entry['displayName'] = $rr['name'];
151                                 if($fields_ret['aboutMe'])
152                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
153                                 if($fields_ret['currentLocation'])
154                                         $entry['currentLocation'] = $rr['location'];
155                                 if($fields_ret['urls']) {
156                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
157                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
158                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
159                                 }
160                                 if($fields_ret['preferredUsername'])
161                                         $entry['preferredUsername'] = $rr['nick'];
162                                 if($fields_ret['updated']) {
163                                         $entry['updated'] = $rr['success_update'];
164
165                                         if ($rr['name-date'] > $entry['updated'])
166                                                 $entry['updated'] = $rr['name-date'];
167
168                                         if ($rr['uri-date'] > $entry['updated'])
169                                                 $entry['updated'] = $rr['uri-date'];
170
171                                         if ($rr['avatar-date'] > $entry['updated'])
172                                                 $entry['updated'] = $rr['avatar-date'];
173
174                                         $entry['updated'] = date("c", strtotime($entry['updated']));
175                                 }
176                                 if($fields_ret['photos'])
177                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
178                                 if($fields_ret['network']) {
179                                         $entry['network'] = $rr['network'];
180                                         if ($entry['network'] == NETWORK_STATUSNET)
181                                                 $entry['network'] = NETWORK_OSTATUS;
182                                         if (($entry['network'] == "") AND ($rr['self']))
183                                                 $entry['network'] = NETWORK_DFRN;
184                                 }
185                                 $ret['entry'][] = $entry;
186                         }
187                 }
188                 else
189                         $ret['entry'][] = array();
190         }
191         else
192                 http_status_exit(500);
193
194         if($format === 'xml') {
195                 header('Content-type: text/xml');
196                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
197                 killme();
198         }
199         if($format === 'json') {
200                 header('Content-type: application/json');
201                 echo json_encode($ret);
202                 killme();
203         }
204         else
205                 http_status_exit(500);
206
207
208 }