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\Model\Contact;
15 use Friendica\Model\GContact;
16 use Friendica\Network\Probe;
17 use Friendica\Protocol\PortableContact;
18 use Friendica\Util\Network;
20 require_once 'mod/contacts.php';
22 function dirfind_init(App $a) {
25 notice(L10n::t('Permission denied.') . EOL );
29 if (! x($a->page,'aside')) {
30 $a->page['aside'] = '';
33 $a->page['aside'] .= Widget::findPeople();
35 $a->page['aside'] .= Widget::follow();
38 function dirfind_content(App $a, $prefix = "") {
41 $discover_user = false;
43 $local = Config::get('system','poco_local_search');
45 $search = $prefix.notags(trim($_REQUEST['search']));
47 if (strpos($search,'@') === 0) {
48 $search = substr($search,1);
49 $header = L10n::t('People Search - %s', $search);
50 if ((valid_email($search) && Network::isEmailDomainValid($search)) ||
51 (substr(normalise_link($search), 0, 7) == "http://")) {
52 $user_data = Probe::uri($search);
53 $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
57 if (strpos($search,'!') === 0) {
58 $search = substr($search,1);
60 $header = L10n::t('Forum Search - %s', $search);
71 $j->page = $a->pager['page'];
73 $objresult = new stdClass();
75 $objresult->name = $user_data["name"];
76 $objresult->addr = $user_data["addr"];
77 $objresult->url = $user_data["url"];
78 $objresult->photo = $user_data["photo"];
79 $objresult->tags = "";
80 $objresult->network = $user_data["network"];
82 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
83 $objresult->cid = $contact["cid"];
85 $j->results[] = $objresult;
87 // Add the contact to the global contacts if it isn't already in our system
88 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
89 GContact::update($user_data);
94 $extra_sql = " AND `community`";
99 $startrec = (($a->pager['page']) * $perpage) - $perpage;
101 if (Config::get('system','diaspora_enabled')) {
102 $diaspora = NETWORK_DIASPORA;
104 $diaspora = NETWORK_DFRN;
107 if (!Config::get('system','ostatus_disabled')) {
108 $ostatus = NETWORK_OSTATUS;
110 $ostatus = NETWORK_DFRN;
113 $search2 = "%".$search."%";
115 /// @TODO These 2 SELECTs are not checked on validity with DBA::is_result()
116 $count = q("SELECT count(*) AS `total` FROM `gcontact`
117 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
118 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
119 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
120 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
121 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
122 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
123 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
125 $results = q("SELECT `nurl`
127 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
128 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
129 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
130 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
132 ORDER BY `updated` DESC LIMIT %d, %d",
133 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
134 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
135 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
136 intval($startrec), intval($perpage));
138 $j->total = $count[0]["total"];
139 $j->items_page = $perpage;
140 $j->page = $a->pager['page'];
141 foreach ($results AS $result) {
142 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
146 $urlparts = parse_url($result["nurl"]);
148 // Ignore results that look strange.
149 // For historic reasons the gcontact table does contain some garbage.
150 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
154 $result = Contact::getDetailsByURL($result["nurl"], local_user());
156 if ($result["name"] == "") {
157 $result["name"] = end(explode("/", $urlparts["path"]));
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"];
169 $j->results[] = $objresult;
172 // Add found profiles from the global directory to the local directory
173 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
176 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
178 if(strlen(Config::get('system','directory')))
179 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
181 $j = json_decode($x);
185 $a->set_pager_total($j->total);
186 $a->set_pager_itemspage($j->items_page);
189 if (!empty($j->results)) {
193 foreach ($j->results as $jj) {
197 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
199 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
201 // If We already know this contact then don't show the "connect" button
205 $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
206 if (DBA::is_result($contact)) {
207 $photo_menu = Contact::photoMenu($contact);
208 $details = _contact_detail_for_template($contact);
209 $alt_text = $details['alt_text'];
214 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
215 $conntxt = L10n::t('Connect');
217 'profile' => [L10n::t("View Profile"), Contact::magicLink($jj->url)],
218 'follow' => [L10n::t("Connect/Follow"), $connlnk]
222 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
225 'alt_text' => $alt_text,
226 'url' => Contact::magicLink($jj->url),
227 'itemurl' => $itemurl,
228 'name' => htmlentities($jj->name),
229 'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
230 'img_hover' => $jj->tags,
231 'conntxt' => $conntxt,
232 'connlnk' => $connlnk,
233 'photo_menu' => $photo_menu,
234 'details' => $contact_details['location'],
235 'tags' => $contact_details['keywords'],
236 'about' => $contact_details['about'],
237 'account_type' => Contact::getAccountType($contact_details),
238 'network' => ContactSelector::networkToName($jj->network, $jj->url),
244 $tpl = get_markup_template('viewcontact_template.tpl');
246 $o .= replace_macros($tpl,[
248 '$contacts' => $entries,
249 '$paginate' => paginate($a),
253 info(L10n::t('No matches') . EOL);