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