3 * @file mod/dirfind.php
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Widget;
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Model\Contact;
12 use Friendica\Model\GContact;
13 use Friendica\Model\Profile;
14 use Friendica\Network\Probe;
15 use Friendica\Protocol\PortableContact;
17 require_once 'mod/contacts.php';
19 function dirfind_init(App $a) {
22 notice( t('Permission denied.') . EOL );
26 if (! x($a->page,'aside')) {
27 $a->page['aside'] = '';
30 $a->page['aside'] .= Widget::findPeople();
32 $a->page['aside'] .= Widget::follow();
35 function dirfind_content(App $a, $prefix = "") {
38 $discover_user = false;
40 $local = Config::get('system','poco_local_search');
42 $search = $prefix.notags(trim($_REQUEST['search']));
44 if (strpos($search,'@') === 0) {
45 $search = substr($search,1);
46 $header = sprintf( t('People Search - %s'), $search);
47 if ((valid_email($search) && validate_email($search)) ||
48 (substr(normalise_link($search), 0, 7) == "http://")) {
49 $user_data = Probe::uri($search);
50 $discover_user = (in_array($user_data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA]));
54 if (strpos($search,'!') === 0) {
55 $search = substr($search,1);
57 $header = sprintf( t('Forum Search - %s'), $search);
68 $j->page = $a->pager['page'];
70 $objresult = new stdClass();
72 $objresult->name = $user_data["name"];
73 $objresult->addr = $user_data["addr"];
74 $objresult->url = $user_data["url"];
75 $objresult->photo = $user_data["photo"];
76 $objresult->tags = "";
77 $objresult->network = $user_data["network"];
79 $contact = Contact::getDetailsByURL($user_data["url"], local_user());
80 $objresult->cid = $contact["cid"];
82 $j->results[] = $objresult;
84 // Add the contact to the global contacts if it isn't already in our system
85 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
86 GContact::update($user_data);
91 $extra_sql = " AND `community`";
96 $startrec = (($a->pager['page']) * $perpage) - $perpage;
98 if (Config::get('system','diaspora_enabled')) {
99 $diaspora = NETWORK_DIASPORA;
101 $diaspora = NETWORK_DFRN;
104 if (!Config::get('system','ostatus_disabled')) {
105 $ostatus = NETWORK_OSTATUS;
107 $ostatus = NETWORK_DFRN;
110 $search2 = "%".$search."%";
112 /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
113 $count = q("SELECT count(*) AS `total` FROM `gcontact`
114 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
115 AND `contact`.`network` = `gcontact`.`network`
116 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
117 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
118 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
119 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
120 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
121 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
122 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
123 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
124 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
125 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
127 $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
129 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
130 AND `contact`.`network` = `gcontact`.`network`
131 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
132 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
133 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
134 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
135 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
136 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
137 GROUP BY `gcontact`.`nurl`
138 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
139 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
140 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
141 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
142 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
143 intval($startrec), intval($perpage));
145 $j->total = $count[0]["total"];
146 $j->items_page = $perpage;
147 $j->page = $a->pager['page'];
148 foreach ($results AS $result) {
149 if (PortableContact::alternateOStatusUrl($result["url"])) {
153 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
155 if ($result["name"] == "") {
156 $urlparts = parse_url($result["url"]);
157 $result["name"] = end(explode("/", $urlparts["path"]));
160 $objresult = new stdClass();
161 $objresult->cid = $result["cid"];
162 $objresult->name = $result["name"];
163 $objresult->addr = $result["addr"];
164 $objresult->url = $result["url"];
165 $objresult->photo = $result["photo"];
166 $objresult->tags = $result["keywords"];
167 $objresult->network = $result["network"];
169 $j->results[] = $objresult;
172 // Add found profiles from the global directory to the local directory
173 Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
176 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
178 if(strlen(Config::get('system','directory')))
179 $x = fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
181 $j = json_decode($x);
185 $a->set_pager_total($j->total);
186 $a->set_pager_itemspage($j->items_page);
189 if (count($j->results)) {
193 foreach ($j->results as $jj) {
197 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
199 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
201 // If We already know this contact then don't show the "connect" button
205 $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
208 $photo_menu = Contact::photoMenu($contact[0]);
209 $details = _contact_detail_for_template($contact[0]);
210 $alt_text = $details['alt_text'];
215 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
216 $conntxt = t('Connect');
218 'profile' => [t("View Profile"), Profile::zrl($jj->url)],
219 'follow' => [t("Connect/Follow"), $connlnk]
223 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
226 'alt_text' => $alt_text,
227 'url' => Profile::zrl($jj->url),
228 'itemurl' => $itemurl,
229 'name' => htmlentities($jj->name),
230 'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
231 'img_hover' => $jj->tags,
232 'conntxt' => $conntxt,
233 'connlnk' => $connlnk,
234 'photo_menu' => $photo_menu,
235 'details' => $contact_details['location'],
236 'tags' => $contact_details['keywords'],
237 'about' => $contact_details['about'],
238 'account_type' => Contact::getAccountType($contact_details),
239 'network' => ContactSelector::networkToName($jj->network, $jj->url),
245 $tpl = get_markup_template('viewcontact_template.tpl');
247 $o .= replace_macros($tpl,[
249 '$contacts' => $entries,
250 '$paginate' => paginate($a),
254 info( t('No matches') . EOL);