]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Merge pull request #1990 from annando/1510-contact-reworked
[friendica.git] / mod / dirfind.php
1 <?php
2 require_once('include/contact_widgets.php');
3 require_once('include/socgraph.php');
4 require_once('include/Contact.php');
5 require_once('include/contact_selectors.php');
6 require_once('mod/contacts.php');
7
8 function dirfind_init(&$a) {
9
10         if(! local_user()) {
11                 notice( t('Permission denied.') . EOL );
12                 return;
13         }
14
15         if(! x($a->page,'aside'))
16                 $a->page['aside'] = '';
17
18         $a->page['aside'] .= findpeople_widget();
19
20         $a->page['aside'] .= follow_widget();
21 }
22
23
24
25 function dirfind_content(&$a, $prefix = "") {
26
27         $community = false;
28
29         $local = get_config('system','poco_local_search');
30
31         $search = $prefix.notags(trim($_REQUEST['search']));
32
33         if(strpos($search,'@') === 0)
34                 $search = substr($search,1);
35
36         if(strpos($search,'!') === 0) {
37                 $search = substr($search,1);
38                 $community = true;
39         }
40
41         $o = '';
42
43         if($search) {
44
45                 if ($local) {
46
47                         if ($community)
48                                 $extra_sql = " AND `community`";
49                         else
50                                 $extra_sql = "";
51
52                         $perpage = 80;
53                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
54
55                         $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND
56                                         (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR
57                                                 `about` REGEXP '%s' OR `keywords` REGEXP '%s')".$extra_sql,
58                                         dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
59                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
60                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)));
61
62                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network` , `gcontact`.`keywords`
63                                         FROM `gcontact`
64                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
65                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
66                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
67                                         WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND
68                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
69                                         (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR
70                                                 `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') $extra_sql
71                                                 GROUP BY `gcontact`.`nurl`
72                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
73                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
74                                         dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
75                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
76                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)),
77                                         intval($startrec), intval($perpage));
78                         $j = new stdClass();
79                         $j->total = $count[0]["total"];
80                         $j->items_page = $perpage;
81                         $j->page = $a->pager['page'];
82                         foreach ($results AS $result) {
83                                 if (poco_alternate_ostatus_url($result["url"]))
84                                          continue;
85
86                                 if ($result["name"] == "") {
87                                         $urlparts = parse_url($result["url"]);
88                                         $result["name"] = end(explode("/", $urlparts["path"]));
89                                 }
90
91                                 $objresult = new stdClass();
92                                 $objresult->cid = $result["cid"];
93                                 $objresult->name = $result["name"];
94                                 $objresult->url = $result["url"];
95                                 $objresult->photo = $result["photo"];
96                                 $objresult->tags = $result["keywords"];
97                                 $objresult->network = $result["network"];
98
99                                 $j->results[] = $objresult;
100                         }
101
102                         // Add found profiles from the global directory to the local directory
103                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
104                 } else {
105
106                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
107
108                         if(strlen(get_config('system','directory')))
109                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
110
111                         $j = json_decode($x);
112                 }
113
114                 if($j->total) {
115                         $a->set_pager_total($j->total);
116                         $a->set_pager_itemspage($j->items_page);
117                 }
118
119                 if(count($j->results)) {
120
121                         $id = 0;
122
123                         foreach($j->results as $jj) {
124
125                                 $alt_text = "";
126
127                                 $itemurl = $jj->url;
128
129                                 // If We already know this contact then don't show the "connect" button
130                                 if ($jj->cid > 0) {
131                                         $connlnk = "";
132                                         $conntxt = "";
133                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
134                                                         intval($jj->cid));
135                                         if ($contact) {
136                                                 $photo_menu = contact_photo_menu($contact[0]);
137                                                 $details = _contact_detail_for_template($contact[0]);
138                                                 $alt_text = $details['alt_text'];
139                                         } else
140                                                 $photo_menu = array();
141                                 } else {
142                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
143                                         $conntxt = t('Connect');
144                                         $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
145                                         $photo_menu[] = array(t("Connect/Follow"), $connlnk);
146                                 }
147
148                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
149
150                                 $entry = array(
151                                         'alt_text' => $alt_text,
152                                         'url' => zrl($jj->url),
153                                         'itemurl' => $itemurl,
154                                         'name' => htmlentities($jj->name),
155                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
156                                         'img_hover' => $jj->tags,
157                                         'conntxt' => $conntxt,
158                                         'connlnk' => $connlnk,
159                                         'photo_menu' => $photo_menu,
160                                         'network' => network_to_name($jj->network, $jj->url),
161                                         'id' => ++$id,
162                                 );
163                                 $entries[] = $entry;
164                         }
165
166                 $tpl = get_markup_template('viewcontact_template.tpl');
167
168                 $o .= replace_macros($tpl,array(
169                         'title' => sprintf( t('People Search - %s'), $search),
170                         '$contacts' => $entries,
171                         '$paginate' => paginate($a),
172                 ));
173
174                 }
175                 else {
176                         info( t('No matches') . EOL);
177                 }
178
179         }
180
181         return $o;
182 }