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