3 * @file mod/dirfind.php
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Widget;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Model\Profile;
15 use Friendica\Network\Probe;
16 use Friendica\Protocol\PortableContact;
18 require_once 'mod/contacts.php';
20 function dirfind_init(App $a) {
23 notice(L10n::t('Permission denied.') . EOL );
27 if (! x($a->page,'aside')) {
28 $a->page['aside'] = '';
31 $a->page['aside'] .= Widget::findPeople();
33 $a->page['aside'] .= Widget::follow();
36 function dirfind_content(App $a, $prefix = "") {
39 $discover_user = false;
41 $local = Config::get('system','poco_local_search');
43 $search = $prefix.notags(trim($_REQUEST['search']));
45 if (strpos($search,'@') === 0) {
46 $search = substr($search,1);
47 $header = sprintf(L10n::t('People Search - %s'), $search);
48 if ((valid_email($search) && validate_email($search)) ||
49 (substr(normalise_link($search), 0, 7) == "http://")) {
50 $user_data = Probe::uri($search);
51 $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
55 if (strpos($search,'!') === 0) {
56 $search = substr($search,1);
58 $header = sprintf(L10n::t('Forum Search - %s'), $search);
69 $j->page = $a->pager['page'];
71 $objresult = new stdClass();
73 $objresult->name = $user_data["name"];
74 $objresult->addr = $user_data["addr"];
75 $objresult->url = $user_data["url"];
76 $objresult->photo = $user_data["photo"];
77 $objresult->tags = "";
78 $objresult->network = $user_data["network"];
80 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
81 $objresult->cid = $contact["cid"];
83 $j->results[] = $objresult;
85 // Add the contact to the global contacts if it isn't already in our system
86 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
87 GContact::update($user_data);
92 $extra_sql = " AND `community`";
97 $startrec = (($a->pager['page']) * $perpage) - $perpage;
99 if (Config::get('system','diaspora_enabled')) {
100 $diaspora = NETWORK_DIASPORA;
102 $diaspora = NETWORK_DFRN;
105 if (!Config::get('system','ostatus_disabled')) {
106 $ostatus = NETWORK_OSTATUS;
108 $ostatus = NETWORK_DFRN;
111 $search2 = "%".$search."%";
113 /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
114 $count = q("SELECT count(*) AS `total` FROM `gcontact`
115 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
116 AND `contact`.`network` = `gcontact`.`network`
117 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
118 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
119 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
120 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
121 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
122 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
123 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
124 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
125 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
126 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
128 $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
130 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
131 AND `contact`.`network` = `gcontact`.`network`
132 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
133 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
134 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
135 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
136 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
137 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
138 GROUP BY `gcontact`.`nurl`
139 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
140 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
141 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
142 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
143 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
144 intval($startrec), intval($perpage));
146 $j->total = $count[0]["total"];
147 $j->items_page = $perpage;
148 $j->page = $a->pager['page'];
149 foreach ($results AS $result) {
150 if (PortableContact::alternateOStatusUrl($result["url"])) {
154 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
156 if ($result["name"] == "") {
157 $urlparts = parse_url($result["url"]);
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 = fetch_url(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 (count($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 = q("SELECT * FROM `contact` WHERE `id` = %d",
209 $photo_menu = Contact::photoMenu($contact[0]);
210 $details = _contact_detail_for_template($contact[0]);
211 $alt_text = $details['alt_text'];
216 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
217 $conntxt = L10n::t('Connect');
219 'profile' => [t("View Profile"), Profile::zrl($jj->url)],
220 'follow' => [t("Connect/Follow"), $connlnk]
224 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
227 'alt_text' => $alt_text,
228 'url' => Profile::zrl($jj->url),
229 'itemurl' => $itemurl,
230 'name' => htmlentities($jj->name),
231 'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
232 'img_hover' => $jj->tags,
233 'conntxt' => $conntxt,
234 'connlnk' => $connlnk,
235 'photo_menu' => $photo_menu,
236 'details' => $contact_details['location'],
237 'tags' => $contact_details['keywords'],
238 'about' => $contact_details['about'],
239 'account_type' => Contact::getAccountType($contact_details),
240 'network' => ContactSelector::networkToName($jj->network, $jj->url),
246 $tpl = get_markup_template('viewcontact_template.tpl');
248 $o .= replace_macros($tpl,[
250 '$contacts' => $entries,
251 '$paginate' => paginate($a),
255 info(L10n::t('No matches') . EOL);