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