]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Use short form array syntax everywhere
[friendica.git] / mod / dirfind.php
1 <?php
2 /**
3  * @file mod/dirfind.php
4  */
5 use Friendica\App;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Widget;
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Model\Contact;
12 use Friendica\Model\GContact;
13 use Friendica\Model\Profile;
14 use Friendica\Network\Probe;
15 use Friendica\Protocol\PortableContact;
16
17 require_once 'mod/contacts.php';
18
19 function dirfind_init(App $a) {
20
21         if (! local_user()) {
22                 notice( t('Permission denied.') . EOL );
23                 return;
24         }
25
26         if (! x($a->page,'aside')) {
27                 $a->page['aside'] = '';
28         }
29
30         $a->page['aside'] .= Widget::findPeople();
31
32         $a->page['aside'] .= Widget::follow();
33 }
34
35 function dirfind_content(App $a, $prefix = "") {
36
37         $community = false;
38         $discover_user = false;
39
40         $local = Config::get('system','poco_local_search');
41
42         $search = $prefix.notags(trim($_REQUEST['search']));
43
44         if (strpos($search,'@') === 0) {
45                 $search = substr($search,1);
46                 $header = sprintf( t('People Search - %s'), $search);
47                 if ((valid_email($search) && validate_email($search)) ||
48                         (substr(normalise_link($search), 0, 7) == "http://")) {
49                         $user_data = Probe::uri($search);
50                         $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
51                 }
52         }
53
54         if (strpos($search,'!') === 0) {
55                 $search = substr($search,1);
56                 $community = true;
57                 $header = sprintf( t('Forum Search - %s'), $search);
58         }
59
60         $o = '';
61
62         if ($search) {
63
64                 if ($discover_user) {
65                         $j = new stdClass();
66                         $j->total = 1;
67                         $j->items_page = 1;
68                         $j->page = $a->pager['page'];
69
70                         $objresult = new stdClass();
71                         $objresult->cid = 0;
72                         $objresult->name = $user_data["name"];
73                         $objresult->addr = $user_data["addr"];
74                         $objresult->url = $user_data["url"];
75                         $objresult->photo = $user_data["photo"];
76                         $objresult->tags = "";
77                         $objresult->network = $user_data["network"];
78
79                         $contact = Contact::getDetailsByURL($user_data["url"], local_user());
80                         $objresult->cid = $contact["cid"];
81
82                         $j->results[] = $objresult;
83
84                         // Add the contact to the global contacts if it isn't already in our system
85                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
86                                 GContact::update($user_data);
87                         }
88                 } elseif ($local) {
89
90                         if ($community)
91                                 $extra_sql = " AND `community`";
92                         else
93                                 $extra_sql = "";
94
95                         $perpage = 80;
96                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
97
98                         if (Config::get('system','diaspora_enabled')) {
99                                 $diaspora = NETWORK_DIASPORA;
100                         } else {
101                                 $diaspora = NETWORK_DFRN;
102                         }
103
104                         if (!Config::get('system','ostatus_disabled')) {
105                                 $ostatus = NETWORK_OSTATUS;
106                         } else {
107                                 $ostatus = NETWORK_DFRN;
108                         }
109
110                         $search2 = "%".$search."%";
111
112                         /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
113                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
114                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
115                                                 AND `contact`.`network` = `gcontact`.`network`
116                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
117                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
118                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
119                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
120                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
121                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
122                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
123                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
124                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
125                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
126
127                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
128                                         FROM `gcontact`
129                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
130                                                 AND `contact`.`network` = `gcontact`.`network`
131                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
132                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
133                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
134                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
135                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
136                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
137                                                 GROUP BY `gcontact`.`nurl`
138                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
139                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
140                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
141                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
142                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
143                                         intval($startrec), intval($perpage));
144                         $j = new stdClass();
145                         $j->total = $count[0]["total"];
146                         $j->items_page = $perpage;
147                         $j->page = $a->pager['page'];
148                         foreach ($results AS $result) {
149                                 if (PortableContact::alternateOStatusUrl($result["url"])) {
150                                         continue;
151                                 }
152
153                                 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
154
155                                 if ($result["name"] == "") {
156                                         $urlparts = parse_url($result["url"]);
157                                         $result["name"] = end(explode("/", $urlparts["path"]));
158                                 }
159
160                                 $objresult = new stdClass();
161                                 $objresult->cid = $result["cid"];
162                                 $objresult->name = $result["name"];
163                                 $objresult->addr = $result["addr"];
164                                 $objresult->url = $result["url"];
165                                 $objresult->photo = $result["photo"];
166                                 $objresult->tags = $result["keywords"];
167                                 $objresult->network = $result["network"];
168
169                                 $j->results[] = $objresult;
170                         }
171
172                         // Add found profiles from the global directory to the local directory
173                         Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
174                 } else {
175
176                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
177
178                         if(strlen(Config::get('system','directory')))
179                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
180
181                         $j = json_decode($x);
182                 }
183
184                 if ($j->total) {
185                         $a->set_pager_total($j->total);
186                         $a->set_pager_itemspage($j->items_page);
187                 }
188
189                 if (count($j->results)) {
190
191                         $id = 0;
192
193                         foreach ($j->results as $jj) {
194
195                                 $alt_text = "";
196
197                                 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
198
199                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
200
201                                 // If We already know this contact then don't show the "connect" button
202                                 if ($jj->cid > 0) {
203                                         $connlnk = "";
204                                         $conntxt = "";
205                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
206                                                         intval($jj->cid));
207                                         if ($contact) {
208                                                 $photo_menu = Contact::photoMenu($contact[0]);
209                                                 $details = _contact_detail_for_template($contact[0]);
210                                                 $alt_text = $details['alt_text'];
211                                         } else {
212                                                 $photo_menu = [];
213                                         }
214                                 } else {
215                                         $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
216                                         $conntxt = t('Connect');
217                                         $photo_menu = [
218                                                 'profile' => [t("View Profile"), Profile::zrl($jj->url)],
219                                                 'follow' => [t("Connect/Follow"), $connlnk]
220                                         ];
221                                 }
222
223                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
224
225                                 $entry = [
226                                         'alt_text' => $alt_text,
227                                         'url' => Profile::zrl($jj->url),
228                                         'itemurl' => $itemurl,
229                                         'name' => htmlentities($jj->name),
230                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
231                                         'img_hover' => $jj->tags,
232                                         'conntxt' => $conntxt,
233                                         'connlnk' => $connlnk,
234                                         'photo_menu' => $photo_menu,
235                                         'details'       => $contact_details['location'],
236                                         'tags'          => $contact_details['keywords'],
237                                         'about'         => $contact_details['about'],
238                                         'account_type'  => Contact::getAccountType($contact_details),
239                                         'network' => ContactSelector::networkToName($jj->network, $jj->url),
240                                         'id' => ++$id,
241                                 ];
242                                 $entries[] = $entry;
243                         }
244
245                 $tpl = get_markup_template('viewcontact_template.tpl');
246
247                 $o .= replace_macros($tpl,[
248                         'title' => $header,
249                         '$contacts' => $entries,
250                         '$paginate' => paginate($a),
251                 ]);
252
253                 } else {
254                         info( t('No matches') . EOL);
255                 }
256
257         }
258
259         return $o;
260 }