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;
17 use Friendica\Network\Probe;
18 use Friendica\Protocol\PortableContact;
19 use Friendica\Util\Network;
20 use Friendica\Util\Proxy as ProxyUtils;
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(defaults($_REQUEST, 'search', '')));
50 if (strpos($search,'@') === 0) {
51 $search = substr($search,1);
52 $header = L10n::t('People Search - %s', $search);
53 if ((valid_email($search) && Network::isEmailDomainValid($search)) ||
54 (substr(normalise_link($search), 0, 7) == "http://")) {
55 $user_data = Probe::uri($search);
56 $discover_user = (in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]));
60 if (strpos($search,'!') === 0) {
61 $search = substr($search,1);
63 $header = L10n::t('Forum Search - %s', $search);
74 $j->page = $a->pager['page'];
76 $objresult = new stdClass();
78 $objresult->name = $user_data["name"];
79 $objresult->addr = $user_data["addr"];
80 $objresult->url = $user_data["url"];
81 $objresult->photo = $user_data["photo"];
82 $objresult->tags = "";
83 $objresult->network = $user_data["network"];
85 $contact = Model\Contact::getDetailsByURL($user_data["url"], local_user());
86 $objresult->cid = $contact["cid"];
87 $objresult->pcid = $contact["zid"];
89 $j->results[] = $objresult;
91 // Add the contact to the global contacts if it isn't already in our system
92 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
93 Model\GContact::update($user_data);
98 $extra_sql = " AND `community`";
103 $startrec = (($a->pager['page']) * $perpage) - $perpage;
105 if (Config::get('system','diaspora_enabled')) {
106 $diaspora = Protocol::DIASPORA;
108 $diaspora = Protocol::DFRN;
111 if (!Config::get('system','ostatus_disabled')) {
112 $ostatus = Protocol::OSTATUS;
114 $ostatus = Protocol::DFRN;
117 $search2 = "%".$search."%";
119 /// @TODO These 2 SELECTs are not checked on validity with DBA::isResult()
120 $count = q("SELECT count(*) AS `total` FROM `gcontact`
121 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
122 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
123 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
124 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
125 DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
126 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
127 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)));
129 $results = q("SELECT `nurl`
131 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
132 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
133 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
134 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
136 ORDER BY `updated` DESC LIMIT %d, %d",
137 DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
138 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
139 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
140 intval($startrec), intval($perpage));
142 $j->total = $count[0]["total"];
143 $j->items_page = $perpage;
144 $j->page = $a->pager['page'];
145 foreach ($results AS $result) {
146 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
150 $urlparts = parse_url($result["nurl"]);
152 // Ignore results that look strange.
153 // For historic reasons the gcontact table does contain some garbage.
154 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
158 $result = Model\Contact::getDetailsByURL($result["nurl"], local_user());
160 if ($result["name"] == "") {
161 $result["name"] = end(explode("/", $urlparts["path"]));
164 $objresult = new stdClass();
165 $objresult->cid = $result["cid"];
166 $objresult->pcid = $result["zid"];
167 $objresult->name = $result["name"];
168 $objresult->addr = $result["addr"];
169 $objresult->url = $result["url"];
170 $objresult->photo = $result["photo"];
171 $objresult->tags = $result["keywords"];
172 $objresult->network = $result["network"];
174 $j->results[] = $objresult;
177 // Add found profiles from the global directory to the local directory
178 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
181 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
183 if(strlen(Config::get('system','directory')))
184 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
186 $j = json_decode($x);
190 $a->setPagerTotal($j->total);
191 $a->setPagerItemsPage($j->items_page);
194 if (!empty($j->results)) {
198 foreach ($j->results as $jj) {
202 $contact_details = Model\Contact::getDetailsByURL($jj->url, local_user());
204 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
206 // If We already know this contact then don't show the "connect" button
210 $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
211 if (DBA::isResult($contact)) {
212 $photo_menu = Model\Contact::photoMenu($contact);
213 $details = Module\Contact::getContactTemplateVars($contact);
214 $alt_text = $details['alt_text'];
219 $connlnk = System::baseUrl().'/follow/?url='.(!empty($jj->connect) ? $jj->connect : $jj->url);
220 $conntxt = L10n::t('Connect');
222 $contact = DBA::selectFirst('contact', [], ['id' => $jj->pcid]);
223 if (DBA::isResult($contact)) {
224 $photo_menu = Model\Contact::photoMenu($contact);
229 $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($jj->url)];
230 $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connlnk];
233 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
236 'alt_text' => $alt_text,
237 'url' => Model\Contact::magicLink($jj->url),
238 'itemurl' => $itemurl,
239 'name' => htmlentities($jj->name),
240 'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
241 'img_hover' => $jj->tags,
242 'conntxt' => $conntxt,
243 'connlnk' => $connlnk,
244 'photo_menu' => $photo_menu,
245 'details' => $contact_details['location'],
246 'tags' => $contact_details['keywords'],
247 'about' => $contact_details['about'],
248 'account_type' => Model\Contact::getAccountType($contact_details),
249 'network' => ContactSelector::networkToName($jj->network, $jj->url),
255 $tpl = get_markup_template('viewcontact_template.tpl');
257 $o .= replace_macros($tpl,[
259 '$contacts' => $entries,
260 '$paginate' => paginate($a),
264 info(L10n::t('No matches') . EOL);