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