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;
19 use Friendica\Util\Proxy as ProxyUtils;
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']));
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"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_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 = Contact::getDetailsByURL($user_data["url"], local_user());
86 $objresult->cid = $contact["cid"];
88 $j->results[] = $objresult;
90 // Add the contact to the global contacts if it isn't already in our system
91 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
92 GContact::update($user_data);
97 $extra_sql = " AND `community`";
102 $startrec = (($a->pager['page']) * $perpage) - $perpage;
104 if (Config::get('system','diaspora_enabled')) {
105 $diaspora = NETWORK_DIASPORA;
107 $diaspora = NETWORK_DFRN;
110 if (!Config::get('system','ostatus_disabled')) {
111 $ostatus = NETWORK_OSTATUS;
113 $ostatus = NETWORK_DFRN;
116 $search2 = "%".$search."%";
118 /// @TODO These 2 SELECTs are not checked on validity with DBA::isResult()
119 $count = q("SELECT count(*) AS `total` FROM `gcontact`
120 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
121 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
122 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
123 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
124 DBA::escape(NETWORK_DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
125 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
126 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)));
128 $results = q("SELECT `nurl`
130 WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
131 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
132 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
133 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
135 ORDER BY `updated` DESC LIMIT %d, %d",
136 DBA::escape(NETWORK_DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
137 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
138 DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
139 intval($startrec), intval($perpage));
141 $j->total = $count[0]["total"];
142 $j->items_page = $perpage;
143 $j->page = $a->pager['page'];
144 foreach ($results AS $result) {
145 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
149 $urlparts = parse_url($result["nurl"]);
151 // Ignore results that look strange.
152 // For historic reasons the gcontact table does contain some garbage.
153 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
157 $result = Contact::getDetailsByURL($result["nurl"], local_user());
159 if ($result["name"] == "") {
160 $result["name"] = end(explode("/", $urlparts["path"]));
163 $objresult = new stdClass();
164 $objresult->cid = $result["cid"];
165 $objresult->name = $result["name"];
166 $objresult->addr = $result["addr"];
167 $objresult->url = $result["url"];
168 $objresult->photo = $result["photo"];
169 $objresult->tags = $result["keywords"];
170 $objresult->network = $result["network"];
172 $j->results[] = $objresult;
175 // Add found profiles from the global directory to the local directory
176 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
179 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
181 if(strlen(Config::get('system','directory')))
182 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
184 $j = json_decode($x);
188 $a->set_pager_total($j->total);
189 $a->set_pager_itemspage($j->items_page);
192 if (!empty($j->results)) {
196 foreach ($j->results as $jj) {
200 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
202 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
204 // If We already know this contact then don't show the "connect" button
208 $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
209 if (DBA::isResult($contact)) {
210 $photo_menu = Contact::photoMenu($contact);
211 $details = _contact_detail_for_template($contact);
212 $alt_text = $details['alt_text'];
217 $connlnk = System::baseUrl().'/follow/?url='.(!empty($jj->connect) ? $jj->connect : $jj->url);
218 $conntxt = L10n::t('Connect');
220 'profile' => [L10n::t("View Profile"), Contact::magicLink($jj->url)],
221 'follow' => [L10n::t("Connect/Follow"), $connlnk]
225 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
228 'alt_text' => $alt_text,
229 'url' => Contact::magicLink($jj->url),
230 'itemurl' => $itemurl,
231 'name' => htmlentities($jj->name),
232 'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
233 'img_hover' => $jj->tags,
234 'conntxt' => $conntxt,
235 'connlnk' => $connlnk,
236 'photo_menu' => $photo_menu,
237 'details' => $contact_details['location'],
238 'tags' => $contact_details['keywords'],
239 'about' => $contact_details['about'],
240 'account_type' => Contact::getAccountType($contact_details),
241 'network' => ContactSelector::networkToName($jj->network, $jj->url),
247 $tpl = get_markup_template('viewcontact_template.tpl');
249 $o .= replace_macros($tpl,[
251 '$contacts' => $entries,
252 '$paginate' => paginate($a),
256 info(L10n::t('No matches') . EOL);