]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
new version of the ShaShape iconset
[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         $discover_user = false;
29
30         $local = get_config('system','poco_local_search');
31
32         $search = $prefix.notags(trim($_REQUEST['search']));
33
34         if(strpos($search,'@') === 0) {
35                 $search = substr($search,1);
36                 if ((valid_email($search) AND validate_email($search)) OR
37                         (substr(normalise_link($search), 0, 7) == "http://")) {
38                         $user_data = probe_url($search);
39                         $discover_user = (in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)));
40                 }
41         }
42
43         if(strpos($search,'!') === 0) {
44                 $search = substr($search,1);
45                 $community = true;
46         }
47
48         $o = '';
49
50         if($search) {
51
52                 if ($discover_user) {
53                         $j = new stdClass();
54                         $j->total = 1;
55                         $j->items_page = 1;
56                         $j->page = $a->pager['page'];
57
58                         $objresult = new stdClass();
59                         $objresult->cid = 0;
60                         $objresult->name = $user_data["name"];
61                         $objresult->addr = $user_data["addr"];
62                         $objresult->url = $user_data["url"];
63                         $objresult->photo = $user_data["photo"];
64                         $objresult->tags = "";
65                         $objresult->network = $user_data["network"];
66
67                         $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
68                                         dbesc(normalise_link($user_data["url"])), intval(local_user()));
69                         if ($contact)
70                                 $objresult->cid = $contact[0]["id"];
71
72
73                         $j->results[] = $objresult;
74
75                         poco_check($user_data["url"], $user_data["name"], $user_data["network"], $user_data["photo"],
76                                 "", "", "", "", "", datetime_convert(), 0);
77                 } elseif ($local) {
78
79                         if ($community)
80                                 $extra_sql = " AND `community`";
81                         else
82                                 $extra_sql = "";
83
84                         $perpage = 80;
85                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
86
87                         if (get_config('system','diaspora_enabled'))
88                                 $diaspora = NETWORK_DIASPORA;
89                         else
90                                 $diaspora = NETWORK_DFRN;
91
92                         if (!get_config('system','ostatus_disabled'))
93                                 $ostatus = NETWORK_OSTATUS;
94                         else
95                                 $ostatus = NETWORK_DFRN;
96
97                         $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND
98                                         (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR
99                                                 `about` REGEXP '%s' OR `keywords` REGEXP '%s')".$extra_sql,
100                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
101                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
102                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)));
103
104                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
105                                         FROM `gcontact`
106                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
107                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
108                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
109                                         WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND
110                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
111                                         (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR
112                                                 `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') $extra_sql
113                                                 GROUP BY `gcontact`.`nurl`
114                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
115                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
116                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
117                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
118                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)),
119                                         intval($startrec), intval($perpage));
120                         $j = new stdClass();
121                         $j->total = $count[0]["total"];
122                         $j->items_page = $perpage;
123                         $j->page = $a->pager['page'];
124                         foreach ($results AS $result) {
125                                 if (poco_alternate_ostatus_url($result["url"]))
126                                          continue;
127
128                                 if ($result["name"] == "") {
129                                         $urlparts = parse_url($result["url"]);
130                                         $result["name"] = end(explode("/", $urlparts["path"]));
131                                 }
132
133                                 $objresult = new stdClass();
134                                 $objresult->cid = $result["cid"];
135                                 $objresult->name = $result["name"];
136                                 $objresult->addr = $result["addr"];
137                                 $objresult->url = $result["url"];
138                                 $objresult->photo = $result["photo"];
139                                 $objresult->tags = $result["keywords"];
140                                 $objresult->network = $result["network"];
141
142                                 $j->results[] = $objresult;
143                         }
144
145                         // Add found profiles from the global directory to the local directory
146                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
147                 } else {
148
149                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
150
151                         if(strlen(get_config('system','directory')))
152                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
153
154                         $j = json_decode($x);
155                 }
156
157                 if($j->total) {
158                         $a->set_pager_total($j->total);
159                         $a->set_pager_itemspage($j->items_page);
160                 }
161
162                 if(count($j->results)) {
163
164                         $id = 0;
165
166                         foreach($j->results as $jj) {
167
168                                 $alt_text = "";
169
170                                 $contact_details = get_contact_details_by_url($jj->url, local_user());
171
172                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
173
174                                 // If We already know this contact then don't show the "connect" button
175                                 if ($jj->cid > 0) {
176                                         $connlnk = "";
177                                         $conntxt = "";
178                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
179                                                         intval($jj->cid));
180                                         if ($contact) {
181                                                 $photo_menu = contact_photo_menu($contact[0]);
182                                                 $details = _contact_detail_for_template($contact[0]);
183                                                 $alt_text = $details['alt_text'];
184                                         } else
185                                                 $photo_menu = array();
186                                 } else {
187                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
188                                         $conntxt = t('Connect');
189                                         $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
190                                         $photo_menu[] = array(t("Connect/Follow"), $connlnk);
191                                 }
192
193                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
194
195                                 $entry = array(
196                                         'alt_text' => $alt_text,
197                                         'url' => zrl($jj->url),
198                                         'itemurl' => $itemurl,
199                                         'name' => htmlentities($jj->name),
200                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
201                                         'img_hover' => $jj->tags,
202                                         'conntxt' => $conntxt,
203                                         'connlnk' => $connlnk,
204                                         'photo_menu' => $photo_menu,
205                                         'details'       => $contact_details['location'],
206                                         'tags'          => $contact_details['keywords'],
207                                         'about'         => $contact_details['about'],
208                                         'network' => network_to_name($jj->network, $jj->url),
209                                         'id' => ++$id,
210                                 );
211                                 $entries[] = $entry;
212                         }
213
214                 $tpl = get_markup_template('viewcontact_template.tpl');
215
216                 $o .= replace_macros($tpl,array(
217                         'title' => sprintf( t('People Search - %s'), $search),
218                         '$contacts' => $entries,
219                         '$paginate' => paginate($a),
220                 ));
221
222                 }
223                 else {
224                         info( t('No matches') . EOL);
225                 }
226
227         }
228
229         return $o;
230 }