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