]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
normalise_link calls
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @file src/Content/Widget.php
4  */
5 namespace Friendica\Content;
6
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\FileTag;
19 use Friendica\Model\GContact;
20 use Friendica\Model\Profile;
21 use Friendica\Util\Strings;
22 use Friendica\Util\XML;
23
24 require_once 'boot.php';
25 require_once 'include/dba.php';
26
27 class Widget
28 {
29         /**
30          * Return the follow widget
31          *
32          * @param string $value optional, default empty
33          */
34         public static function follow($value = "")
35         {
36                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('follow.tpl'), array(
37                         '$connect' => L10n::t('Add New Contact'),
38                         '$desc' => L10n::t('Enter address or web location'),
39                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
40                         '$value' => $value,
41                         '$follow' => L10n::t('Connect')
42                 ));
43         }
44
45         /**
46          * Return Find People widget
47          */
48         public static function findPeople()
49         {
50                 $a = get_app();
51                 $global_dir = Config::get('system', 'directory');
52
53                 if (Config::get('system', 'invitation_only')) {
54                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
55                         if ($x || is_site_admin()) {
56                                 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
57                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
58                                         . '</div>';
59                         }
60                 }
61
62                 $nv = [];
63                 $nv['findpeople'] = L10n::t('Find People');
64                 $nv['desc'] = L10n::t('Enter name or interest');
65                 $nv['label'] = L10n::t('Connect/Follow');
66                 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
67                 $nv['findthem'] = L10n::t('Find');
68                 $nv['suggest'] = L10n::t('Friend Suggestions');
69                 $nv['similar'] = L10n::t('Similar Interests');
70                 $nv['random'] = L10n::t('Random Profile');
71                 $nv['inv'] = L10n::t('Invite Friends');
72                 $nv['directory'] = L10n::t('Global Directory');
73                 $nv['global_dir'] = $global_dir;
74                 $nv['local_directory'] = L10n::t('Local Directory');
75
76                 $aside = [];
77                 $aside['$nv'] = $nv;
78
79                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('peoplefind.tpl'), $aside);
80         }
81
82         /**
83          * Return unavailable networks
84          */
85         public static function unavailableNetworks()
86         {
87                 // Always hide content from these networks
88                 $networks = ['face', 'apdn'];
89
90                 if (!Addon::isEnabled("statusnet")) {
91                         $networks[] = Protocol::STATUSNET;
92                 }
93
94                 if (!Addon::isEnabled("pumpio")) {
95                         $networks[] = Protocol::PUMPIO;
96                 }
97
98                 if (!Addon::isEnabled("twitter")) {
99                         $networks[] = Protocol::TWITTER;
100                 }
101
102                 if (Config::get("system", "ostatus_disabled")) {
103                         $networks[] = Protocol::OSTATUS;
104                 }
105
106                 if (!Config::get("system", "diaspora_enabled")) {
107                         $networks[] = Protocol::DIASPORA;
108                 }
109
110                 if (!Addon::isEnabled("pnut")) {
111                         $networks[] = Protocol::PNUT;
112                 }
113
114                 if (!sizeof($networks)) {
115                         return "";
116                 }
117
118                 $network_filter = implode("','", $networks);
119
120                 $network_filter = "AND `network` NOT IN ('$network_filter')";
121
122                 return $network_filter;
123         }
124
125         /**
126          * Return networks widget
127          *
128          * @param string $baseurl  baseurl
129          * @param string $selected optional, default empty
130          */
131         public static function networks($baseurl, $selected = '')
132         {
133                 if (!local_user()) {
134                         return '';
135                 }
136
137                 if (!Feature::isEnabled(local_user(), 'networks')) {
138                         return '';
139                 }
140
141                 $extra_sql = self::unavailableNetworks();
142
143                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
144                         local_user()
145                 );
146
147                 $nets = array();
148                 while ($rr = DBA::fetch($r)) {
149                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
150                 }
151                 DBA::close($r);
152
153                 if (count($nets) < 2) {
154                         return '';
155                 }
156
157                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
158                         '$title' => L10n::t('Networks'),
159                         '$desc' => '',
160                         '$sel_all' => (($selected == '') ? 'selected' : ''),
161                         '$all' => L10n::t('All Networks'),
162                         '$nets' => $nets,
163                         '$base' => $baseurl,
164                 ));
165         }
166
167         /**
168          * Return file as widget
169          *
170          * @param string $baseurl  baseurl
171          * @param string $selected optional, default empty
172          */
173         public static function fileAs($baseurl, $selected = '')
174         {
175                 if (!local_user()) {
176                         return '';
177                 }
178
179                 if (!Feature::isEnabled(local_user(), 'filing')) {
180                         return '';
181                 }
182
183                 $saved = PConfig::get(local_user(), 'system', 'filetags');
184                 if (!strlen($saved)) {
185                         return;
186                 }
187
188                 $matches = false;
189                 $terms = array();
190                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
191                 if ($cnt) {
192                         foreach ($matches as $mtch)
193                         {
194                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
195                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
196                         }
197                 }
198
199                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
200                         '$title' => L10n::t('Saved Folders'),
201                         '$desc' => '',
202                         '$sel_all' => (($selected == '') ? 'selected' : ''),
203                         '$all' => L10n::t('Everything'),
204                         '$terms' => $terms,
205                         '$base' => $baseurl,
206                 ));
207         }
208
209         /**
210          * Return categories widget
211          *
212          * @param string $baseurl  baseurl
213          * @param string $selected optional, default empty
214          */
215         public static function categories($baseurl, $selected = '')
216         {
217                 $a = get_app();
218
219                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
220                         return '';
221                 }
222
223                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
224                 if (!strlen($saved)) {
225                         return;
226                 }
227
228                 $matches = false;
229                 $terms = array();
230                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
231
232                 if ($cnt) {
233                         foreach ($matches as $mtch) {
234                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
235                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
236                         }
237                 }
238
239                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
240                         '$title' => L10n::t('Categories'),
241                         '$desc' => '',
242                         '$sel_all' => (($selected == '') ? 'selected' : ''),
243                         '$all' => L10n::t('Everything'),
244                         '$terms' => $terms,
245                         '$base' => $baseurl,
246                 ));
247         }
248
249         /**
250          * Return common friends visitor widget
251          *
252          * @param string $profile_uid uid
253          */
254         public static function commonFriendsVisitor($profile_uid)
255         {
256                 if (local_user() == $profile_uid) {
257                         return;
258                 }
259
260                 $cid = $zcid = 0;
261
262                 if (!empty($_SESSION['remote'])) {
263                         foreach ($_SESSION['remote'] as $visitor) {
264                                 if ($visitor['uid'] == $profile_uid) {
265                                         $cid = $visitor['cid'];
266                                         break;
267                                 }
268                         }
269                 }
270
271                 if (!$cid) {
272                         if (Profile::getMyURL()) {
273                                 $contact = DBA::selectFirst('contact', ['id'],
274                                                 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
275                                 if (DBA::isResult($contact)) {
276                                         $cid = $contact['id'];
277                                 } else {
278                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
279                                         if (DBA::isResult($gcontact)) {
280                                                 $zcid = $gcontact['id'];
281                                         }
282                                 }
283                         }
284                 }
285
286                 if ($cid == 0 && $zcid == 0) {
287                         return;
288                 }
289
290                 if ($cid) {
291                         $t = GContact::countCommonFriends($profile_uid, $cid);
292                 } else {
293                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
294                 }
295
296                 if (!$t) {
297                         return;
298                 }
299
300                 if ($cid) {
301                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
302                 } else {
303                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
304                 }
305
306                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('remote_friends_common.tpl'), array(
307                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
308                         '$base' => System::baseUrl(),
309                         '$uid' => $profile_uid,
310                         '$cid' => (($cid) ? $cid : '0'),
311                         '$linkmore' => (($t > 5) ? 'true' : ''),
312                         '$more' => L10n::t('show more'),
313                         '$items' => $r)
314                 );
315         }
316
317         /**
318          * Insert a tag cloud widget for the present profile.
319          *
320          * @brief Insert a tag cloud widget for the present profile.
321          * @param int     $limit Max number of displayed tags.
322          * @return string HTML formatted output.
323          */
324         public static function tagCloud($limit = 50)
325         {
326                 $a = get_app();
327
328                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
329                         return '';
330                 }
331
332                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
333                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
334
335                         if (!$owner_id) {
336                                 return '';
337                         }
338                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
339                 }
340
341                 return '';
342         }
343 }