3 * @file mod/dirfind.php
6 use Friendica\Core\Config;
7 use Friendica\Core\System;
8 use Friendica\Core\Worker;
9 use Friendica\Model\GlobalContact;
10 use Friendica\Network\Probe;
11 use Friendica\Protocol\PortableContact;
13 require_once 'include/contact_widgets.php';
14 require_once 'include/Contact.php';
15 require_once 'include/contact_selectors.php';
16 require_once 'mod/contacts.php';
18 function dirfind_init(App $a) {
21 notice( t('Permission denied.') . EOL );
25 if (! x($a->page,'aside')) {
26 $a->page['aside'] = '';
29 $a->page['aside'] .= findpeople_widget();
31 $a->page['aside'] .= follow_widget();
34 function dirfind_content(App $a, $prefix = "") {
37 $discover_user = false;
39 $local = Config::get('system','poco_local_search');
41 $search = $prefix.notags(trim($_REQUEST['search']));
43 if (strpos($search,'@') === 0) {
44 $search = substr($search,1);
45 $header = sprintf( t('People Search - %s'), $search);
46 if ((valid_email($search) && validate_email($search)) ||
47 (substr(normalise_link($search), 0, 7) == "http://")) {
48 $user_data = Probe::uri($search);
49 $discover_user = (in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)));
53 if (strpos($search,'!') === 0) {
54 $search = substr($search,1);
56 $header = sprintf( t('Forum Search - %s'), $search);
67 $j->page = $a->pager['page'];
69 $objresult = new stdClass();
71 $objresult->name = $user_data["name"];
72 $objresult->addr = $user_data["addr"];
73 $objresult->url = $user_data["url"];
74 $objresult->photo = $user_data["photo"];
75 $objresult->tags = "";
76 $objresult->network = $user_data["network"];
78 $contact = get_contact_details_by_url($user_data["url"], local_user());
79 $objresult->cid = $contact["cid"];
81 $j->results[] = $objresult;
83 // Add the contact to the global contacts if it isn't already in our system
84 if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
85 GlobalContact::update($user_data);
90 $extra_sql = " AND `community`";
95 $startrec = (($a->pager['page']) * $perpage) - $perpage;
97 if (Config::get('system','diaspora_enabled')) {
98 $diaspora = NETWORK_DIASPORA;
100 $diaspora = NETWORK_DFRN;
103 if (!Config::get('system','ostatus_disabled')) {
104 $ostatus = NETWORK_OSTATUS;
106 $ostatus = NETWORK_DFRN;
109 $search2 = "%".$search."%";
111 /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
112 $count = q("SELECT count(*) AS `total` FROM `gcontact`
113 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
114 AND `contact`.`network` = `gcontact`.`network`
115 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
116 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
117 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
118 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
119 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
120 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
121 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
122 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
123 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
124 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
126 $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
128 LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
129 AND `contact`.`network` = `gcontact`.`network`
130 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
131 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
132 WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
133 ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
134 (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
135 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
136 GROUP BY `gcontact`.`nurl`
137 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
138 intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
139 dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
140 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
141 dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
142 intval($startrec), intval($perpage));
144 $j->total = $count[0]["total"];
145 $j->items_page = $perpage;
146 $j->page = $a->pager['page'];
147 foreach ($results AS $result) {
148 if (PortableContact::alternateOStatusUrl($result["url"])) {
152 $result = get_contact_details_by_url($result["url"], local_user(), $result);
154 if ($result["name"] == "") {
155 $urlparts = parse_url($result["url"]);
156 $result["name"] = end(explode("/", $urlparts["path"]));
159 $objresult = new stdClass();
160 $objresult->cid = $result["cid"];
161 $objresult->name = $result["name"];
162 $objresult->addr = $result["addr"];
163 $objresult->url = $result["url"];
164 $objresult->photo = $result["photo"];
165 $objresult->tags = $result["keywords"];
166 $objresult->network = $result["network"];
168 $j->results[] = $objresult;
171 // Add found profiles from the global directory to the local directory
172 Worker::add(PRIORITY_LOW, 'discover_poco', "dirsearch", urlencode($search));
175 $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
177 if(strlen(Config::get('system','directory')))
178 $x = fetch_url(get_server().'/lsearch?f=' . $p . '&search=' . urlencode($search));
180 $j = json_decode($x);
184 $a->set_pager_total($j->total);
185 $a->set_pager_itemspage($j->items_page);
188 if (count($j->results)) {
192 foreach ($j->results as $jj) {
196 $contact_details = get_contact_details_by_url($jj->url, local_user());
198 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
200 // If We already know this contact then don't show the "connect" button
204 $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
207 $photo_menu = contact_photo_menu($contact[0]);
208 $details = _contact_detail_for_template($contact[0]);
209 $alt_text = $details['alt_text'];
211 $photo_menu = array();
214 $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
215 $conntxt = t('Connect');
217 'profile' => array(t("View Profile"), zrl($jj->url)),
218 'follow' => array(t("Connect/Follow"), $connlnk)
222 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
225 'alt_text' => $alt_text,
226 'url' => zrl($jj->url),
227 'itemurl' => $itemurl,
228 'name' => htmlentities($jj->name),
229 'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
230 'img_hover' => $jj->tags,
231 'conntxt' => $conntxt,
232 'connlnk' => $connlnk,
233 'photo_menu' => $photo_menu,
234 'details' => $contact_details['location'],
235 'tags' => $contact_details['keywords'],
236 'about' => $contact_details['about'],
237 'account_type' => account_type($contact_details),
238 'network' => network_to_name($jj->network, $jj->url),
244 $tpl = get_markup_template('viewcontact_template.tpl');
246 $o .= replace_macros($tpl,array(
248 '$contacts' => $entries,
249 '$paginate' => paginate($a),
253 info( t('No matches') . EOL);