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