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;
17 use Friendica\Util\Network;
19 require_once 'mod/contacts.php';
21 function dirfind_init(App $a) {
24 notice(L10n::t('Permission denied.') . EOL );
28 if (! x($a->page,'aside')) {
29 $a->page['aside'] = '';
32 $a->page['aside'] .= Widget::findPeople();
34 $a->page['aside'] .= Widget::follow();
37 function dirfind_content(App $a, $prefix = "") {
40 $discover_user = false;
42 $local = Config::get('system','poco_local_search');
44 $search = $prefix.notags(trim($_REQUEST['search']));
46 if (strpos($search,'@') === 0) {
47 $search = substr($search,1);
48 $header = L10n::t('People Search - %s', $search);
49 if ((valid_email($search) && Network::isEmailDomainValid($search)) ||
50 (substr(normalise_link($search), 0, 7) == "http://")) {
51 $user_data = Probe::uri($search);
52 $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
56 if (strpos($search,'!') === 0) {
57 $search = substr($search,1);
59 $header = L10n::t('Forum Search - %s', $search);
70 $j->page = $a->pager['page'];
72 $objresult = new stdClass();
74 $objresult->name = $user_data["name"];
75 $objresult->addr = $user_data["addr"];
76 $objresult->url = $user_data["url"];
77 $objresult->photo = $user_data["photo"];
78 $objresult->tags = "";
79 $objresult->network = $user_data["network"];
81 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
82 $objresult->cid = $contact["cid"];
84 $j->results[] = $objresult;
86 // Add the contact to the global contacts if it isn't already in our system
87 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
88 GContact::update($user_data);
93 $extra_sql = " AND `community`";
98 $startrec = (($a->pager['page']) * $perpage) - $perpage;
100 if (Config::get('system','diaspora_enabled')) {
101 $diaspora = NETWORK_DIASPORA;
103 $diaspora = NETWORK_DFRN;
106 if (!Config::get('system','ostatus_disabled')) {
107 $ostatus = NETWORK_OSTATUS;
109 $ostatus = NETWORK_DFRN;
112 $search2 = "%".$search."%";
114 /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
115 $count = q("SELECT count(*) AS `total` FROM `gcontact`
116 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
117 AND `contact`.`network` = `gcontact`.`network`
118 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
119 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
120 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
121 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
122 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
123 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
124 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
125 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
126 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
127 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
129 $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
131 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
132 AND `contact`.`network` = `gcontact`.`network`
133 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
134 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
135 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
136 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
137 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
138 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
139 GROUP BY `gcontact`.`nurl`
140 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
141 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
142 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
143 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
144 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
145 intval($startrec), intval($perpage));
147 $j->total = $count[0]["total"];
148 $j->items_page = $perpage;
149 $j->page = $a->pager['page'];
150 foreach ($results AS $result) {
151 if (PortableContact::alternateOStatusUrl($result["url"])) {
155 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
157 if ($result["name"] == "") {
158 $urlparts = parse_url($result["url"]);
159 $result["name"] = end(explode("/", $urlparts["path"]));
162 $objresult = new stdClass();
163 $objresult->cid = $result["cid"];
164 $objresult->name = $result["name"];
165 $objresult->addr = $result["addr"];
166 $objresult->url = $result["url"];
167 $objresult->photo = $result["photo"];
168 $objresult->tags = $result["keywords"];
169 $objresult->network = $result["network"];
171 $j->results[] = $objresult;
174 // Add found profiles from the global directory to the local directory
175 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
178 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
180 if(strlen(Config::get('system','directory')))
181 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
183 $j = json_decode($x);
187 $a->set_pager_total($j->total);
188 $a->set_pager_itemspage($j->items_page);
191 if (count($j->results)) {
195 foreach ($j->results as $jj) {
199 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
201 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
203 // If We already know this contact then don't show the "connect" button
207 $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
210 $photo_menu = Contact::photoMenu($contact[0]);
211 $details = _contact_detail_for_template($contact[0]);
212 $alt_text = $details['alt_text'];
217 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
218 $conntxt = L10n::t('Connect');
220 'profile' => [L10n::t("View Profile"), Profile::zrl($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' => Profile::zrl($jj->url),
230 'itemurl' => $itemurl,
231 'name' => htmlentities($jj->name),
232 'thumb' => proxy_url($jj->photo, false, PROXY_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);