3 * @file mod/dirfind.php
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;
21 require_once 'mod/contacts.php';
23 function dirfind_init(App $a) {
26 notice(L10n::t('Permission denied.') . EOL );
30 if (! x($a->page,'aside')) {
31 $a->page['aside'] = '';
34 $a->page['aside'] .= Widget::findPeople();
36 $a->page['aside'] .= Widget::follow();
39 function dirfind_content(App $a, $prefix = "") {
42 $discover_user = false;
44 $local = Config::get('system','poco_local_search');
46 $search = $prefix.notags(trim($_REQUEST['search']));
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]));
58 if (strpos($search,'!') === 0) {
59 $search = substr($search,1);
61 $header = L10n::t('Forum Search - %s', $search);
72 $j->page = $a->pager['page'];
74 $objresult = new stdClass();
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"];
83 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
84 $objresult->cid = $contact["cid"];
86 $j->results[] = $objresult;
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);
95 $extra_sql = " AND `community`";
100 $startrec = (($a->pager['page']) * $perpage) - $perpage;
102 if (Config::get('system','diaspora_enabled')) {
103 $diaspora = NETWORK_DIASPORA;
105 $diaspora = NETWORK_DFRN;
108 if (!Config::get('system','ostatus_disabled')) {
109 $ostatus = NETWORK_OSTATUS;
111 $ostatus = NETWORK_DFRN;
114 $search2 = "%".$search."%";
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)));
126 $results = q("SELECT `nurl`
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
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));
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"])) {
147 $urlparts = parse_url($result["nurl"]);
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'])) {
155 $result = Contact::getDetailsByURL($result["nurl"], local_user());
157 if ($result["name"] == "") {
158 $result["name"] = end(explode("/", $urlparts["path"]));
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"];
170 $j->results[] = $objresult;
173 // Add found profiles from the global directory to the local directory
174 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
177 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
179 if(strlen(Config::get('system','directory')))
180 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
182 $j = json_decode($x);
186 $a->set_pager_total($j->total);
187 $a->set_pager_itemspage($j->items_page);
190 if (!empty($j->results)) {
194 foreach ($j->results as $jj) {
198 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
200 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
202 // If We already know this contact then don't show the "connect" button
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'];
215 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
216 $conntxt = L10n::t('Connect');
218 'profile' => [L10n::t("View Profile"), Contact::magicLink($jj->url)],
219 'follow' => [L10n::t("Connect/Follow"), $connlnk]
223 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
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),
245 $tpl = get_markup_template('viewcontact_template.tpl');
247 $o .= replace_macros($tpl,[
249 '$contacts' => $entries,
250 '$paginate' => paginate($a),
254 info(L10n::t('No matches') . EOL);