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