]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Merge pull request #4323 from MrPetovan/bug/fix-removeme-auth
[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\System;
14 use Friendica\Database\DBM;
15 use Friendica\Model\GContact;
16 use Friendica\Model\Profile;
17
18 use dba;
19
20 require_once 'boot.php';
21
22 class Widget
23 {
24         /**
25          * Return the follow widget
26          *
27          * @param string $value optional, default empty
28          */
29         public static function follow($value = "")
30         {
31                 return replace_macros(get_markup_template('follow.tpl'), array(
32                         '$connect' => L10n::t('Add New Contact'),
33                         '$desc' => L10n::t('Enter address or web location'),
34                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
35                         '$value' => $value,
36                         '$follow' => L10n::t('Connect')
37                 ));
38         }
39
40         /**
41          * Return Find People widget
42          */
43         public static function findPeople()
44         {
45                 $a = get_app();
46                 $global_dir = Config::get('system', 'directory');
47
48                 if (Config::get('system', 'invitation_only')) {
49                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
50                         if ($x || is_site_admin()) {
51                                 $a->page['aside'] .= '<div class="side-link" id="side-invite-remain">'
52                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
53                                         . '</div>' . $inv;
54                         }
55                 }
56
57                 return replace_macros(get_markup_template('peoplefind.tpl'), array(
58                         '$findpeople' => L10n::t('Find People'),
59                         '$desc' => L10n::t('Enter name or interest'),
60                         '$label' => L10n::t('Connect/Follow'),
61                         '$hint' => L10n::t('Examples: Robert Morgenstein, Fishing'),
62                         '$findthem' => L10n::t('Find'),
63                         '$suggest' => L10n::t('Friend Suggestions'),
64                         '$similar' => L10n::t('Similar Interests'),
65                         '$random' => L10n::t('Random Profile'),
66                         '$inv' => L10n::t('Invite Friends'),
67                         '$directory' => L10n::t('View Global Directory'),
68                         '$global_dir' => $global_dir
69                 ));
70         }
71
72         /**
73          * Return unavailable networks
74          */
75         public static function unavailableNetworks()
76         {
77                 $networks = array();
78
79                 if (!Addon::isEnabled("appnet")) {
80                         $networks[] = NETWORK_APPNET;
81                 }
82
83                 if (!Addon::isEnabled("fbpost") && !Addon::isEnabled("facebook")) {
84                         $networks[] = NETWORK_FACEBOOK;
85                 }
86
87                 if (!Addon::isEnabled("statusnet")) {
88                         $networks[] = NETWORK_STATUSNET;
89                 }
90
91                 if (!Addon::isEnabled("pumpio")) {
92                         $networks[] = NETWORK_PUMPIO;
93                 }
94
95                 if (!Addon::isEnabled("twitter")) {
96                         $networks[] = NETWORK_TWITTER;
97                 }
98
99                 if (Config::get("system", "ostatus_disabled")) {
100                         $networks[] = NETWORK_OSTATUS;
101                 }
102
103                 if (!Config::get("system", "diaspora_enabled")) {
104                         $networks[] = NETWORK_DIASPORA;
105                 }
106
107                 if (!Addon::isEnabled("pnut")) {
108                         $networks[] = NETWORK_PNUT;
109                 }
110
111                 if (!sizeof($networks)) {
112                         return "";
113                 }
114
115                 $network_filter = implode("','", $networks);
116
117                 $network_filter = "AND `network` NOT IN ('$network_filter')";
118
119                 return $network_filter;
120         }
121
122         /**
123          * Return networks widget
124          *
125          * @param string $baseurl  baseurl
126          * @param string $selected optional, default empty
127          */
128         public static function networks($baseurl, $selected = '')
129         {
130                 if (!local_user()) {
131                         return '';
132                 }
133
134                 if (!Feature::isEnabled(local_user(), 'networks')) {
135                         return '';
136                 }
137
138                 $extra_sql = self::unavailableNetworks();
139
140                 $r = dba::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
141                         local_user()
142                 );
143
144                 $nets = array();
145                 while ($rr = dba::fetch($r)) {
146                         /// @TODO If 'network' is not there, this triggers an E_NOTICE
147                         if ($rr['network']) {
148                                 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
149                         }
150                 }
151                 dba::close($r);
152
153                 if (count($nets) < 2) {
154                         return '';
155                 }
156
157                 return replace_macros(get_markup_template('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                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
194                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
195                         }
196                 }
197
198                 return replace_macros(get_markup_template('fileas_widget.tpl'), array(
199                         '$title' => L10n::t('Saved Folders'),
200                         '$desc' => '',
201                         '$sel_all' => (($selected == '') ? 'selected' : ''),
202                         '$all' => L10n::t('Everything'),
203                         '$terms' => $terms,
204                         '$base' => $baseurl,
205                 ));
206         }
207
208         /**
209          * Return categories widget
210          *
211          * @param string $baseurl  baseurl
212          * @param string $selected optional, default empty
213          */
214         public static function categories($baseurl, $selected = '')
215         {
216                 $a = get_app();
217
218                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
219                         return '';
220                 }
221
222                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
223                 if (!strlen($saved)) {
224                         return;
225                 }
226
227                 $matches = false;
228                 $terms = array();
229                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
230
231                 if ($cnt) {
232                         foreach ($matches as $mtch) {
233                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
234                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
235                         }
236                 }
237
238                 return replace_macros(get_markup_template('categories_widget.tpl'), array(
239                         '$title' => L10n::t('Categories'),
240                         '$desc' => '',
241                         '$sel_all' => (($selected == '') ? 'selected' : ''),
242                         '$all' => L10n::t('Everything'),
243                         '$terms' => $terms,
244                         '$base' => $baseurl,
245                 ));
246         }
247
248         /**
249          * Return common friends visitor widget
250          *
251          * @param string $profile_uid uid
252          */
253         public static function commonFriendsVisitor($profile_uid)
254         {
255                 if (local_user() == $profile_uid) {
256                         return;
257                 }
258
259                 $cid = $zcid = 0;
260
261                 if (is_array($_SESSION['remote'])) {
262                         foreach ($_SESSION['remote'] as $visitor) {
263                                 if ($visitor['uid'] == $profile_uid) {
264                                         $cid = $visitor['cid'];
265                                         break;
266                                 }
267                         }
268                 }
269
270                 if (!$cid) {
271                         if (Profile::getMyURL()) {
272                                 $contact = dba::selectFirst('contact', ['id'],
273                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
274                                 if (DBM::is_result($contact)) {
275                                         $cid = $contact['id'];
276                                 } else {
277                                         $gcontact = dba::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
278                                         if (DBM::is_result($gcontact)) {
279                                                 $zcid = $gcontact['id'];
280                                         }
281                                 }
282                         }
283                 }
284
285                 if ($cid == 0 && $zcid == 0) {
286                         return;
287                 }
288
289                 if ($cid) {
290                         $t = GContact::countCommonFriends($profile_uid, $cid);
291                 } else {
292                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
293                 }
294
295                 if (!$t) {
296                         return;
297                 }
298
299                 if ($cid) {
300                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
301                 } else {
302                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
303                 }
304
305                 return replace_macros(get_markup_template('remote_friends_common.tpl'), array(
306                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
307                         '$base' => System::baseUrl(),
308                         '$uid' => $profile_uid,
309                         '$cid' => (($cid) ? $cid : '0'),
310                         '$linkmore' => (($t > 5) ? 'true' : ''),
311                         '$more' => L10n::t('show more'),
312                         '$items' => $r)
313                 );
314         }
315 }