]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
GlobalContact created
[friendica.git] / mod / dirfind.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\System;
6 use Friendica\Core\Worker;
7 use Friendica\Model\GlobalContact;
8 use Friendica\Network\Probe;
9
10 require_once 'include/contact_widgets.php';
11 require_once 'include/socgraph.php';
12 require_once 'include/Contact.php';
13 require_once 'include/contact_selectors.php';
14 require_once 'mod/contacts.php';
15
16 function dirfind_init(App $a) {
17
18         if (! local_user()) {
19                 notice( t('Permission denied.') . EOL );
20                 return;
21         }
22
23         if (! x($a->page,'aside')) {
24                 $a->page['aside'] = '';
25         }
26
27         $a->page['aside'] .= findpeople_widget();
28
29         $a->page['aside'] .= follow_widget();
30 }
31
32 function dirfind_content(App $a, $prefix = "") {
33
34         $community = false;
35         $discover_user = false;
36
37         $local = Config::get('system','poco_local_search');
38
39         $search = $prefix.notags(trim($_REQUEST['search']));
40
41         if (strpos($search,'@') === 0) {
42                 $search = substr($search,1);
43                 $header = sprintf( t('People Search - %s'), $search);
44                 if ((valid_email($search) && validate_email($search)) ||
45                         (substr(normalise_link($search), 0, 7) == "http://")) {
46                         $user_data = Probe::uri($search);
47                         $discover_user = (in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)));
48                 }
49         }
50
51         if (strpos($search,'!') === 0) {
52                 $search = substr($search,1);
53                 $community = true;
54                 $header = sprintf( t('Forum Search - %s'), $search);
55         }
56
57         $o = '';
58
59         if ($search) {
60
61                 if ($discover_user) {
62                         $j = new stdClass();
63                         $j->total = 1;
64                         $j->items_page = 1;
65                         $j->page = $a->pager['page'];
66
67                         $objresult = new stdClass();
68                         $objresult->cid = 0;
69                         $objresult->name = $user_data["name"];
70                         $objresult->addr = $user_data["addr"];
71                         $objresult->url = $user_data["url"];
72                         $objresult->photo = $user_data["photo"];
73                         $objresult->tags = "";
74                         $objresult->network = $user_data["network"];
75
76                         $contact = get_contact_details_by_url($user_data["url"], local_user());
77                         $objresult->cid = $contact["cid"];
78
79                         $j->results[] = $objresult;
80
81                         // Add the contact to the global contacts if it isn't already in our system
82                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
83                                 GlobalContact::update($user_data);
84                         }
85                 } elseif ($local) {
86
87                         if ($community)
88                                 $extra_sql = " AND `community`";
89                         else
90                                 $extra_sql = "";
91
92                         $perpage = 80;
93                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
94
95                         if (Config::get('system','diaspora_enabled')) {
96                                 $diaspora = NETWORK_DIASPORA;
97                         } else {
98                                 $diaspora = NETWORK_DFRN;
99                         }
100
101                         if (!Config::get('system','ostatus_disabled')) {
102                                 $ostatus = NETWORK_OSTATUS;
103                         } else {
104                                 $ostatus = NETWORK_DFRN;
105                         }
106
107                         $search2 = "%".$search."%";
108
109                         /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
110                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
111                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
112                                                 AND `contact`.`network` = `gcontact`.`network`
113                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
114                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
115                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
116                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
117                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
118                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
119                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
120                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
121                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
122                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
123
124                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
125                                         FROM `gcontact`
126                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
127                                                 AND `contact`.`network` = `gcontact`.`network`
128                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
129                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
130                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
131                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
132                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
133                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
134                                                 GROUP BY `gcontact`.`nurl`
135                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
136                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
137                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
138                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
139                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
140                                         intval($startrec), intval($perpage));
141                         $j = new stdClass();
142                         $j->total = $count[0]["total"];
143                         $j->items_page = $perpage;
144                         $j->page = $a->pager['page'];
145                         foreach ($results AS $result) {
146                                 if (poco_alternate_ostatus_url($result["url"])) {
147                                         continue;
148                                 }
149
150                                 $result = get_contact_details_by_url($result["url"], local_user(), $result);
151
152                                 if ($result["name"] == "") {
153                                         $urlparts = parse_url($result["url"]);
154                                         $result["name"] = end(explode("/", $urlparts["path"]));
155                                 }
156
157                                 $objresult = new stdClass();
158                                 $objresult->cid = $result["cid"];
159                                 $objresult->name = $result["name"];
160                                 $objresult->addr = $result["addr"];
161                                 $objresult->url = $result["url"];
162                                 $objresult->photo = $result["photo"];
163                                 $objresult->tags = $result["keywords"];
164                                 $objresult->network = $result["network"];
165
166                                 $j->results[] = $objresult;
167                         }
168
169                         // Add found profiles from the global directory to the local directory
170                         Worker::add(PRIORITY_LOW, 'discover_poco', "dirsearch", urlencode($search));
171                 } else {
172
173                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
174
175                         if(strlen(Config::get('system','directory')))
176                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
177
178                         $j = json_decode($x);
179                 }
180
181                 if ($j->total) {
182                         $a->set_pager_total($j->total);
183                         $a->set_pager_itemspage($j->items_page);
184                 }
185
186                 if (count($j->results)) {
187
188                         $id = 0;
189
190                         foreach ($j->results as $jj) {
191
192                                 $alt_text = "";
193
194                                 $contact_details = get_contact_details_by_url($jj->url, local_user());
195
196                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
197
198                                 // If We already know this contact then don't show the "connect" button
199                                 if ($jj->cid > 0) {
200                                         $connlnk = "";
201                                         $conntxt = "";
202                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
203                                                         intval($jj->cid));
204                                         if ($contact) {
205                                                 $photo_menu = contact_photo_menu($contact[0]);
206                                                 $details = _contact_detail_for_template($contact[0]);
207                                                 $alt_text = $details['alt_text'];
208                                         } else {
209                                                 $photo_menu = array();
210                                         }
211                                 } else {
212                                         $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
213                                         $conntxt = t('Connect');
214                                         $photo_menu = array(
215                                                 'profile' => array(t("View Profile"), zrl($jj->url)),
216                                                 'follow' => array(t("Connect/Follow"), $connlnk)
217                                         );
218                                 }
219
220                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
221
222                                 $entry = array(
223                                         'alt_text' => $alt_text,
224                                         'url' => zrl($jj->url),
225                                         'itemurl' => $itemurl,
226                                         'name' => htmlentities($jj->name),
227                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
228                                         'img_hover' => $jj->tags,
229                                         'conntxt' => $conntxt,
230                                         'connlnk' => $connlnk,
231                                         'photo_menu' => $photo_menu,
232                                         'details'       => $contact_details['location'],
233                                         'tags'          => $contact_details['keywords'],
234                                         'about'         => $contact_details['about'],
235                                         'account_type'  => account_type($contact_details),
236                                         'network' => network_to_name($jj->network, $jj->url),
237                                         'id' => ++$id,
238                                 );
239                                 $entries[] = $entry;
240                         }
241
242                 $tpl = get_markup_template('viewcontact_template.tpl');
243
244                 $o .= replace_macros($tpl,array(
245                         'title' => $header,
246                         '$contacts' => $entries,
247                         '$paginate' => paginate($a),
248                 ));
249
250                 } else {
251                         info( t('No matches') . EOL);
252                 }
253
254         }
255
256         return $o;
257 }