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\Protocol;
12 use Friendica\Core\System;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
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 use Friendica\Util\Proxy as ProxyUtils;
22 require_once 'mod/contacts.php';
24 function dirfind_init(App $a) {
27 notice(L10n::t('Permission denied.') . EOL );
31 if (! x($a->page,'aside')) {
32 $a->page['aside'] = '';
35 $a->page['aside'] .= Widget::findPeople();
37 $a->page['aside'] .= Widget::follow();
40 function dirfind_content(App $a, $prefix = "") {
43 $discover_user = false;
45 $local = Config::get('system','poco_local_search');
47 $search = $prefix.notags(trim(defaults($_REQUEST, 'search', '')));
51 if (strpos($search,'@') === 0) {
52 $search = substr($search,1);
53 $header = L10n::t('People Search - %s', $search);
54 if ((valid_email($search) && Network::isEmailDomainValid($search)) ||
55 (substr(normalise_link($search), 0, 7) == "http://")) {
56 $user_data = Probe::uri($search);
57 $discover_user = (in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]));
61 if (strpos($search,'!') === 0) {
62 $search = substr($search,1);
64 $header = L10n::t('Forum Search - %s', $search);
75 $j->page = $a->pager['page'];
77 $objresult = new stdClass();
79 $objresult->name = $user_data["name"];
80 $objresult->addr = $user_data["addr"];
81 $objresult->url = $user_data["url"];
82 $objresult->photo = $user_data["photo"];
83 $objresult->tags = "";
84 $objresult->network = $user_data["network"];
86 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
87 $objresult->cid = $contact["cid"];
88 $objresult->pcid = $contact["zid"];
90 $j->results[] = $objresult;
92 // Add the contact to the global contacts if it isn't already in our system
93 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
94 GContact::update($user_data);
99 $extra_sql = " AND `community`";
104 $startrec = (($a->pager['page']) * $perpage) - $perpage;
106 if (Config::get('system','diaspora_enabled')) {
107 $diaspora = Protocol::DIASPORA;
109 $diaspora = Protocol::DFRN;
112 if (!Config::get('system','ostatus_disabled')) {
113 $ostatus = Protocol::OSTATUS;
115 $ostatus = Protocol::DFRN;
118 $search2 = "%".$search."%";
120 /// @TODO These 2 SELECTs are not checked on validity with DBA::isResult()
121 $count = q("SELECT count(*) AS `total` FROM `gcontact`
122 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
123 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
124 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
125 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
126 DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
127 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
128 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)));
130 $results = q("SELECT `nurl`
132 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
133 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
134 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
135 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
137 ORDER BY `updated` DESC LIMIT %d, %d",
138 DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
139 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
140 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
141 intval($startrec), intval($perpage));
143 $j->total = $count[0]["total"];
144 $j->items_page = $perpage;
145 $j->page = $a->pager['page'];
146 foreach ($results AS $result) {
147 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
151 $urlparts = parse_url($result["nurl"]);
153 // Ignore results that look strange.
154 // For historic reasons the gcontact table does contain some garbage.
155 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
159 $result = Contact::getDetailsByURL($result["nurl"], local_user());
161 if ($result["name"] == "") {
162 $result["name"] = end(explode("/", $urlparts["path"]));
165 $objresult = new stdClass();
166 $objresult->cid = $result["cid"];
167 $objresult->pcid = $result["zid"];
168 $objresult->name = $result["name"];
169 $objresult->addr = $result["addr"];
170 $objresult->url = $result["url"];
171 $objresult->photo = $result["photo"];
172 $objresult->tags = $result["keywords"];
173 $objresult->network = $result["network"];
175 $j->results[] = $objresult;
178 // Add found profiles from the global directory to the local directory
179 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
182 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
184 if(strlen(Config::get('system','directory')))
185 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
187 $j = json_decode($x);
191 $a->set_pager_total($j->total);
192 $a->set_pager_itemspage($j->items_page);
195 if (!empty($j->results)) {
199 foreach ($j->results as $jj) {
203 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
205 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
207 // If We already know this contact then don't show the "connect" button
211 $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
212 if (DBA::isResult($contact)) {
213 $photo_menu = Contact::photoMenu($contact);
214 $details = _contact_detail_for_template($contact);
215 $alt_text = $details['alt_text'];
220 $connlnk = System::baseUrl().'/follow/?url='.(!empty($jj->connect) ? $jj->connect : $jj->url);
221 $conntxt = L10n::t('Connect');
223 $contact = DBA::selectFirst('contact', [], ['id' => $jj->pcid]);
224 if (DBA::isResult($contact)) {
225 $photo_menu = Contact::photoMenu($contact);
230 $photo_menu['profile'] = [L10n::t("View Profile"), Contact::magicLink($jj->url)];
231 $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connlnk];
234 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
237 'alt_text' => $alt_text,
238 'url' => Contact::magicLink($jj->url),
239 'itemurl' => $itemurl,
240 'name' => htmlentities($jj->name),
241 'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
242 'img_hover' => $jj->tags,
243 'conntxt' => $conntxt,
244 'connlnk' => $connlnk,
245 'photo_menu' => $photo_menu,
246 'details' => $contact_details['location'],
247 'tags' => $contact_details['keywords'],
248 'about' => $contact_details['about'],
249 'account_type' => Contact::getAccountType($contact_details),
250 'network' => ContactSelector::networkToName($jj->network, $jj->url),
256 $tpl = get_markup_template('viewcontact_template.tpl');
258 $o .= replace_macros($tpl,[
260 '$contacts' => $entries,
261 '$paginate' => paginate($a),
265 info(L10n::t('No matches') . EOL);