]> git.mxchange.org Git - friendica.git/blob - src/Model/Profile.php
Many t() calls
[friendica.git] / src / Model / Profile.php
1 <?php
2 /**
3  * @file src/Model/Profile.php
4  */
5
6 namespace Friendica\Model;
7
8 use Friendica\App;
9 use Friendica\Content\Feature;
10 use Friendica\Content\ForumManager;
11 use Friendica\Core\Addon;
12 use Friendica\Core\Cache;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Core\PConfig;
16 use Friendica\Core\System;
17 use Friendica\Core\Worker;
18 use Friendica\Database\DBM;
19 use Friendica\Model\Contact;
20 use Friendica\Protocol\Diaspora;
21 use dba;
22
23 require_once 'include/dba.php';
24 require_once 'include/bbcode.php';
25 require_once 'mod/proxy.php';
26
27 class Profile
28 {
29         /**
30          * @brief Returns a formatted location string from the given profile array
31          *
32          * @param array $profile Profile array (Generated from the "profile" table)
33          *
34          * @return string Location string
35          */
36         public static function formatLocation(array $profile)
37         {
38                 $location = '';
39
40                 if ($profile['locality']) {
41                         $location .= $profile['locality'];
42                 }
43
44                 if ($profile['region'] && ($profile['locality'] != $profile['region'])) {
45                         if ($location) {
46                                 $location .= ', ';
47                         }
48
49                         $location .= $profile['region'];
50                 }
51
52                 if ($profile['country-name']) {
53                         if ($location) {
54                                 $location .= ', ';
55                         }
56
57                         $location .= $profile['country-name'];
58                 }
59
60                 return $location;
61         }
62
63         /**
64          *
65          * Loads a profile into the page sidebar.
66          *
67          * The function requires a writeable copy of the main App structure, and the nickname
68          * of a registered local account.
69          *
70          * If the viewer is an authenticated remote viewer, the profile displayed is the
71          * one that has been configured for his/her viewing in the Contact manager.
72          * Passing a non-zero profile ID can also allow a preview of a selected profile
73          * by the owner.
74          *
75          * Profile information is placed in the App structure for later retrieval.
76          * Honours the owner's chosen theme for display.
77          *
78          * @attention Should only be run in the _init() functions of a module. That ensures that
79          *      the theme is chosen before the _init() function of a theme is run, which will usually
80          *      load a lot of theme-specific content
81          *
82          * @brief Loads a profile into the page sidebar.
83          * @param object  $a            App
84          * @param string  $nickname     string
85          * @param int     $profile      int
86          * @param array   $profiledata  array
87          * @param boolean $show_connect Show connect link
88          */
89         public static function load(App $a, $nickname, $profile = 0, $profiledata = [], $show_connect = true)
90         {
91                 $user = dba::selectFirst('user', ['uid'], ['nickname' => $nickname]);
92
93                 if (!$user && !count($user) && !count($profiledata)) {
94                         logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
95                         notice(L10n::t('Requested account is not available.') . EOL);
96                         $a->error = 404;
97                         return;
98                 }
99
100                 if (!x($a->page, 'aside')) {
101                         $a->page['aside'] = '';
102                 }
103
104                 if ($profiledata) {
105                         $a->page['aside'] .= self::sidebar($profiledata, true, $show_connect);
106
107                         if (!DBM::is_result($user)) {
108                                 return;
109                         }
110                 }
111
112                 $pdata = self::getByNickname($nickname, $user[0]['uid'], $profile);
113
114                 if (empty($pdata) && empty($profiledata)) {
115                         logger('profile error: ' . $a->query_string, LOGGER_DEBUG);
116                         notice(L10n::t('Requested profile is not available.') . EOL);
117                         $a->error = 404;
118                         return;
119                 }
120
121                 // fetch user tags if this isn't the default profile
122
123                 if (!$pdata['is-default']) {
124                         $x = q(
125                                 "SELECT `pub_keywords` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
126                                 intval($pdata['profile_uid'])
127                         );
128                         if ($x && count($x)) {
129                                 $pdata['pub_keywords'] = $x[0]['pub_keywords'];
130                         }
131                 }
132
133                 $a->profile = $pdata;
134                 $a->profile_uid = $pdata['profile_uid'];
135
136                 $a->profile['mobile-theme'] = PConfig::get($a->profile['profile_uid'], 'system', 'mobile_theme');
137                 $a->profile['network'] = NETWORK_DFRN;
138
139                 $a->page['title'] = $a->profile['name'] . ' @ ' . $a->config['sitename'];
140
141                 if (!$profiledata && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
142                         $_SESSION['theme'] = $a->profile['theme'];
143                 }
144
145                 $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
146
147                 /*
148                 * load/reload current theme info
149                 */
150
151                 $a->set_template_engine(); // reset the template engine to the default in case the user's theme doesn't specify one
152
153                 $theme_info_file = 'view/theme/' . current_theme() . '/theme.php';
154                 if (file_exists($theme_info_file)) {
155                         require_once $theme_info_file;
156                 }
157
158                 if (!x($a->page, 'aside')) {
159                         $a->page['aside'] = '';
160                 }
161
162                 if (local_user() && local_user() == $a->profile['uid'] && $profiledata) {
163                         $a->page['aside'] .= replace_macros(
164                                 get_markup_template('profile_edlink.tpl'),
165                                 [
166                                         '$editprofile' => t('Edit profile'),
167                                         '$profid' => $a->profile['id']
168                                 ]
169                         );
170                 }
171
172                 $block = ((Config::get('system', 'block_public') && !local_user() && !remote_user()) ? true : false);
173
174                 /**
175                  * @todo
176                  * By now, the contact block isn't shown, when a different profile is given
177                  * But: When this profile was on the same server, then we could display the contacts
178                  */
179                 if (!$profiledata) {
180                         $a->page['aside'] .= self::sidebar($a->profile, $block, $show_connect);
181                 }
182
183                 return;
184         }
185
186         /**
187          * Get all profile data of a local user
188          *
189          * If the viewer is an authenticated remote viewer, the profile displayed is the
190          * one that has been configured for his/her viewing in the Contact manager.
191          * Passing a non-zero profile ID can also allow a preview of a selected profile
192          * by the owner
193          *
194          * Includes all available profile data
195          *
196          * @brief Get all profile data of a local user
197          * @param string $nickname nick
198          * @param int    $uid      uid
199          * @param int    $profile_id  ID of the profile
200          * @return array
201          */
202         public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
203         {
204                 if (remote_user() && count($_SESSION['remote'])) {
205                         foreach ($_SESSION['remote'] as $visitor) {
206                                 if ($visitor['uid'] == $uid) {
207                                         $contact = dba::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
208                                         if (DBM::is_result($contact)) {
209                                                 $profile_id = $contact['profile-id'];
210                                         }
211                                         break;
212                                 }
213                         }
214                 }
215
216                 $profile = null;
217
218                 if ($profile_id) {
219                         $profile = dba::fetch_first(
220                                 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` AS `contact_photo`,
221                                         `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
222                                         `profile`.`uid` AS `profile_uid`, `profile`.*,
223                                         `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
224                                 FROM `profile`
225                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
226                                 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
227                                 WHERE `user`.`nickname` = ? AND `profile`.`id` = ? LIMIT 1",
228                                 $nickname,
229                                 intval($profile_id)
230                         );
231                 }
232                 if (!DBM::is_result($profile)) {
233                         $profile = dba::fetch_first(
234                                 "SELECT `contact`.`id` AS `contact_id`, `contact`.`photo` as `contact_photo`,
235                                         `contact`.`thumb` AS `contact_thumb`, `contact`.`micro` AS `contact_micro`,
236                                         `profile`.`uid` AS `profile_uid`, `profile`.*,
237                                         `contact`.`avatar-date` AS picdate, `contact`.`addr`, `contact`.`url`, `user`.*
238                                 FROM `profile`
239                                 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` AND `contact`.`self`
240                                 INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
241                                 WHERE `user`.`nickname` = ? AND `profile`.`is-default` LIMIT 1",
242                                 $nickname
243                         );
244                 }
245
246                 return $profile;
247         }
248
249         /**
250          * Formats a profile for display in the sidebar.
251          *
252          * It is very difficult to templatise the HTML completely
253          * because of all the conditional logic.
254          *
255          * @brief Formats a profile for display in the sidebar.
256          * @param array $profile
257          * @param int $block
258          * @param boolean $show_connect Show connect link
259          *
260          * @return HTML string suitable for sidebar inclusion
261          *
262          * @note Returns empty string if passed $profile is wrong type or not populated
263          *
264          * @hooks 'profile_sidebar_enter'
265          *      array $profile - profile data
266          * @hooks 'profile_sidebar'
267          *      array $arr
268          */
269         private static function sidebar($profile, $block = 0, $show_connect = true)
270         {
271                 $a = get_app();
272
273                 $o = '';
274                 $location = false;
275
276                 // This function can also use contact information in $profile
277                 $is_contact = x($profile, 'cid');
278
279                 if (!is_array($profile) && !count($profile)) {
280                         return $o;
281                 }
282
283                 $profile['picdate'] = urlencode(defaults($profile, 'picdate', ''));
284
285                 if (($profile['network'] != '') && ($profile['network'] != NETWORK_DFRN)) {
286                         $profile['network_name'] = format_network_name($profile['network'], $profile['url']);
287                 } else {
288                         $profile['network_name'] = '';
289                 }
290
291                 Addon::callHooks('profile_sidebar_enter', $profile);
292
293
294                 // don't show connect link to yourself
295                 $connect = $profile['uid'] != local_user() ? t('Connect') : false;
296
297                 // don't show connect link to authenticated visitors either
298                 if (remote_user() && count($_SESSION['remote'])) {
299                         foreach ($_SESSION['remote'] as $visitor) {
300                                 if ($visitor['uid'] == $profile['uid']) {
301                                         $connect = false;
302                                         break;
303                                 }
304                         }
305                 }
306
307                 if (!$show_connect) {
308                         $connect = false;
309                 }
310
311                 // Is the local user already connected to that user?
312                 if ($connect && local_user()) {
313                         if (isset($profile['url'])) {
314                                 $profile_url = normalise_link($profile['url']);
315                         } else {
316                                 $profile_url = normalise_link(System::baseUrl() . '/profile/' . $profile['nickname']);
317                         }
318
319                         if (dba::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
320                                 $connect = false;
321                         }
322                 }
323
324                 if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect'])) {
325                         $connect = false;
326                 }
327
328                 $remoteconnect = null;
329                 if (isset($profile['remoteconnect'])) {
330                         $remoteconnect = $profile['remoteconnect'];
331                 }
332
333                 if ($connect && ($profile['network'] == NETWORK_DFRN) && !isset($remoteconnect)) {
334                         $subscribe_feed = t('Atom feed');
335                 } else {
336                         $subscribe_feed = false;
337                 }
338
339                 if (remote_user() || (self::getMyURL() && x($profile, 'unkmail') && ($profile['uid'] != local_user()))) {
340                         $wallmessage = t('Message');
341                         $wallmessage_link = 'wallmessage/' . $profile['nickname'];
342
343                         if (remote_user()) {
344                                 $r = q(
345                                         "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
346                                         intval($profile['uid']),
347                                         intval(remote_user()),
348                                         intval(CONTACT_IS_FRIEND)
349                                 );
350                         } else {
351                                 $r = q(
352                                         "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
353                                         intval($profile['uid']),
354                                         dbesc(normalise_link(self::getMyURL())),
355                                         intval(CONTACT_IS_FRIEND)
356                                 );
357                         }
358                         if ($r) {
359                                 $remote_url = $r[0]['url'];
360                                 $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
361                                 $wallmessage_link = $message_path . base64_encode($profile['addr']);
362                         }
363                 } else {
364                         $wallmessage = false;
365                         $wallmessage_link = false;
366                 }
367
368                 // show edit profile to yourself
369                 if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
370                         $profile['edit'] = [System::baseUrl() . '/profiles', t('Profiles'), '', t('Manage/edit profiles')];
371                         $r = q(
372                                 "SELECT * FROM `profile` WHERE `uid` = %d",
373                                 local_user()
374                         );
375
376                         $profile['menu'] = [
377                                 'chg_photo' => t('Change profile photo'),
378                                 'cr_new' => t('Create New Profile'),
379                                 'entries' => [],
380                         ];
381
382                         if (DBM::is_result($r)) {
383                                 foreach ($r as $rr) {
384                                         $profile['menu']['entries'][] = [
385                                                 'photo' => $rr['thumb'],
386                                                 'id' => $rr['id'],
387                                                 'alt' => t('Profile Image'),
388                                                 'profile_name' => $rr['profile-name'],
389                                                 'isdefault' => $rr['is-default'],
390                                                 'visibile_to_everybody' => t('visible to everybody'),
391                                                 'edit_visibility' => t('Edit visibility'),
392                                         ];
393                                 }
394                         }
395                 }
396                 if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
397                         $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], t('Edit profile'), '', t('Edit profile')];
398                         $profile['menu'] = [
399                                 'chg_photo' => t('Change profile photo'),
400                                 'cr_new' => null,
401                                 'entries' => [],
402                         ];
403                 }
404
405                 // Fetch the account type
406                 $account_type = Contact::getAccountType($profile);
407
408                 if (x($profile, 'address')
409                         || x($profile, 'location')
410                         || x($profile, 'locality')
411                         || x($profile, 'region')
412                         || x($profile, 'postal-code')
413                         || x($profile, 'country-name')
414                 ) {
415                         $location = t('Location:');
416                 }
417
418                 $gender   = x($profile, 'gender')   ? t('Gender:')   : false;
419                 $marital  = x($profile, 'marital')  ? t('Status:')   : false;
420                 $homepage = x($profile, 'homepage') ? t('Homepage:') : false;
421                 $about    = x($profile, 'about')    ? t('About:')    : false;
422                 $xmpp     = x($profile, 'xmpp')     ? t('XMPP:')     : false;
423
424                 if ((x($profile, 'hidewall') || $block) && !local_user() && !remote_user()) {
425                         $location = $gender = $marital = $homepage = $about = false;
426                 }
427
428                 $split_name = Diaspora::splitName($profile['name']);
429                 $firstname = $split_name['first'];
430                 $lastname = $split_name['last'];
431
432                 if (x($profile, 'guid')) {
433                         $diaspora = [
434                                 'guid' => $profile['guid'],
435                                 'podloc' => System::baseUrl(),
436                                 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
437                                 'nickname' => $profile['nickname'],
438                                 'fullname' => $profile['name'],
439                                 'firstname' => $firstname,
440                                 'lastname' => $lastname,
441                                 'photo300' => $profile['contact_photo'],
442                                 'photo100' => $profile['contact_thumb'],
443                                 'photo50' => $profile['contact_micro'],
444                         ];
445                 } else {
446                         $diaspora = false;
447                 }
448
449                 $contact_block = '';
450                 $updated = '';
451                 $contacts = 0;
452                 if (!$block) {
453                         $contact_block = contact_block();
454
455                         if (is_array($a->profile) && !$a->profile['hide-friends']) {
456                                 $r = q(
457                                         "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
458                                         intval($a->profile['uid'])
459                                 );
460                                 if (DBM::is_result($r)) {
461                                         $updated = date('c', strtotime($r[0]['updated']));
462                                 }
463
464                                 $r = q(
465                                         "SELECT COUNT(*) AS `total` FROM `contact`
466                                         WHERE `uid` = %d
467                                                 AND NOT `self` AND NOT `blocked` AND NOT `pending`
468                                                 AND NOT `hidden` AND NOT `archive`
469                                                 AND `network` IN ('%s', '%s', '%s', '')",
470                                         intval($profile['uid']),
471                                         dbesc(NETWORK_DFRN),
472                                         dbesc(NETWORK_DIASPORA),
473                                         dbesc(NETWORK_OSTATUS)
474                                 );
475                                 if (DBM::is_result($r)) {
476                                         $contacts = intval($r[0]['total']);
477                                 }
478                         }
479                 }
480
481                 $p = [];
482                 foreach ($profile as $k => $v) {
483                         $k = str_replace('-', '_', $k);
484                         $p[$k] = $v;
485                 }
486
487                 if (isset($p['about'])) {
488                         $p['about'] = bbcode($p['about']);
489                 }
490
491                 if (isset($p['address'])) {
492                         $p['address'] = bbcode($p['address']);
493                 } else {
494                         $p['address'] = bbcode($p['location']);
495                 }
496
497                 if (isset($p['photo'])) {
498                         $p['photo'] = proxy_url($p['photo'], false, PROXY_SIZE_SMALL);
499                 }
500
501                 $tpl = get_markup_template('profile_vcard.tpl');
502                 $o .= replace_macros($tpl, [
503                         '$profile' => $p,
504                         '$xmpp' => $xmpp,
505                         '$connect' => $connect,
506                         '$remoteconnect' => $remoteconnect,
507                         '$subscribe_feed' => $subscribe_feed,
508                         '$wallmessage' => $wallmessage,
509                         '$wallmessage_link' => $wallmessage_link,
510                         '$account_type' => $account_type,
511                         '$location' => $location,
512                         '$gender' => $gender,
513                         '$marital' => $marital,
514                         '$homepage' => $homepage,
515                         '$about' => $about,
516                         '$network' => t('Network:'),
517                         '$contacts' => $contacts,
518                         '$updated' => $updated,
519                         '$diaspora' => $diaspora,
520                         '$contact_block' => $contact_block,
521                 ]);
522
523                 $arr = ['profile' => &$profile, 'entry' => &$o];
524
525                 Addon::callHooks('profile_sidebar', $arr);
526
527                 return $o;
528         }
529
530         public static function getBirthdays()
531         {
532                 $a = get_app();
533                 $o = '';
534
535                 if (!local_user() || $a->is_mobile || $a->is_tablet) {
536                         return $o;
537                 }
538
539                 /*
540                 * $mobile_detect = new Mobile_Detect();
541                 * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
542                 *               if ($is_mobile)
543                 *                       return $o;
544                 */
545
546                 $bd_format = t('g A l F d'); // 8 AM Friday January 18
547                 $bd_short = t('F d');
548
549                 $cachekey = 'get_birthdays:' . local_user();
550                 $r = Cache::get($cachekey);
551                 if (is_null($r)) {
552                         $s = dba::p(
553                                 "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
554                                 INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
555                                 WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
556                                 ORDER BY `start` ASC ",
557                                 local_user(),
558                                 datetime_convert('UTC', 'UTC', 'now + 6 days'),
559                                 datetime_convert('UTC', 'UTC', 'now')
560                         );
561                         if (DBM::is_result($s)) {
562                                 $r = dba::inArray($s);
563                                 Cache::set($cachekey, $r, CACHE_HOUR);
564                         }
565                 }
566                 if (DBM::is_result($r)) {
567                         $total = 0;
568                         $now = strtotime('now');
569                         $cids = [];
570
571                         $istoday = false;
572                         foreach ($r as $rr) {
573                                 if (strlen($rr['name'])) {
574                                         $total ++;
575                                 }
576                                 if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
577                                         $istoday = true;
578                                 }
579                         }
580                         $classtoday = $istoday ? ' birthday-today ' : '';
581                         if ($total) {
582                                 foreach ($r as &$rr) {
583                                         if (!strlen($rr['name'])) {
584                                                 continue;
585                                         }
586
587                                         // avoid duplicates
588
589                                         if (in_array($rr['cid'], $cids)) {
590                                                 continue;
591                                         }
592                                         $cids[] = $rr['cid'];
593
594                                         $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
595                                         $url = $rr['url'];
596                                         if ($rr['network'] === NETWORK_DFRN) {
597                                                 $url = System::baseUrl() . '/redir/' . $rr['cid'];
598                                         }
599
600                                         $rr['link'] = $url;
601                                         $rr['title'] = $rr['name'];
602                                         $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . t('[today]') : '');
603                                         $rr['startime'] = null;
604                                         $rr['today'] = $today;
605                                 }
606                         }
607                 }
608                 $tpl = get_markup_template('birthdays_reminder.tpl');
609                 return replace_macros($tpl, [
610                         '$baseurl' => System::baseUrl(),
611                         '$classtoday' => $classtoday,
612                         '$count' => $total,
613                         '$event_reminders' => t('Birthday Reminders'),
614                         '$event_title' => t('Birthdays this week:'),
615                         '$events' => $r,
616                         '$lbr' => '{', // raw brackets mess up if/endif macro processing
617                         '$rbr' => '}'
618                 ]);
619         }
620
621         public static function getEvents()
622         {
623                 require_once 'include/bbcode.php';
624
625                 $a = get_app();
626
627                 if (!local_user() || $a->is_mobile || $a->is_tablet) {
628                         return $o;
629                 }
630
631                 /*
632                 *       $mobile_detect = new Mobile_Detect();
633                 *               $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
634                 *               if ($is_mobile)
635                 *                       return $o;
636                 */
637
638                 $bd_format = t('g A l F d'); // 8 AM Friday January 18
639                 $classtoday = '';
640
641                 $s = dba::p(
642                         "SELECT `event`.* FROM `event`
643                         WHERE `event`.`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?
644                         ORDER BY `start` ASC ",
645                         local_user(),
646                         datetime_convert('UTC', 'UTC', 'now + 7 days'),
647                         datetime_convert('UTC', 'UTC', 'now - 1 days')
648                 );
649
650                 $r = [];
651
652                 if (DBM::is_result($s)) {
653                         $istoday = false;
654
655                         while ($rr = dba::fetch($s)) {
656                                 if (strlen($rr['name'])) {
657                                         $total ++;
658                                 }
659
660                                 $strt = datetime_convert('UTC', $rr['convert'] ? $a->timezone : 'UTC', $rr['start'], 'Y-m-d');
661                                 if ($strt === datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) {
662                                         $istoday = true;
663                                 }
664
665                                 $title = strip_tags(html_entity_decode(bbcode($rr['summary']), ENT_QUOTES, 'UTF-8'));
666
667                                 if (strlen($title) > 35) {
668                                         $title = substr($title, 0, 32) . '... ';
669                                 }
670
671                                 $description = substr(strip_tags(bbcode($rr['desc'])), 0, 32) . '... ';
672                                 if (!$description) {
673                                         $description = t('[No description]');
674                                 }
675
676                                 $strt = datetime_convert('UTC', $rr['convert'] ? $a->timezone : 'UTC', $rr['start']);
677
678                                 if (substr($strt, 0, 10) < datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) {
679                                         continue;
680                                 }
681
682                                 $today = ((substr($strt, 0, 10) === datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) ? true : false);
683
684                                 $rr['title'] = $title;
685                                 $rr['description'] = $description;
686                                 $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
687                                 $rr['startime'] = $strt;
688                                 $rr['today'] = $today;
689
690                                 $r[] = $rr;
691                         }
692                         dba::close($s);
693                         $classtoday = (($istoday) ? 'event-today' : '');
694                 }
695                 $tpl = get_markup_template('events_reminder.tpl');
696                 return replace_macros($tpl, [
697                         '$baseurl' => System::baseUrl(),
698                         '$classtoday' => $classtoday,
699                         '$count' => count($r),
700                         '$event_reminders' => t('Event Reminders'),
701                         '$event_title' => t('Events this week:'),
702                         '$events' => $r,
703                 ]);
704         }
705
706         public static function getAdvanced(App $a)
707         {
708                 $o = '';
709                 $uid = $a->profile['uid'];
710
711                 $o .= replace_macros(
712                         get_markup_template('section_title.tpl'),
713                         ['$title' => t('Profile')]
714                 );
715
716                 if ($a->profile['name']) {
717                         $tpl = get_markup_template('profile_advanced.tpl');
718
719                         $profile = [];
720
721                         $profile['fullname'] = [t('Full Name:'), $a->profile['name']];
722
723                         if ($a->profile['gender']) {
724                                 $profile['gender'] = [t('Gender:'), $a->profile['gender']];
725                         }
726
727                         if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
728                                 $year_bd_format = t('j F, Y');
729                                 $short_bd_format = t('j F');
730
731                                 $val = intval($a->profile['dob']) ?
732                                         day_translate(datetime_convert('UTC', 'UTC', $a->profile['dob'] . ' 00:00 +00:00', $year_bd_format))
733                                         : day_translate(datetime_convert('UTC', 'UTC', '2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format));
734
735                                 $profile['birthday'] = [t('Birthday:'), $val];
736                         }
737
738                         if (!empty($a->profile['dob'])
739                                 && $a->profile['dob'] > '0001-01-01'
740                                 && $age = age($a->profile['dob'], $a->profile['timezone'], '')
741                         ) {
742                                 $profile['age'] = [t('Age:'), $age];
743                         }
744
745                         if ($a->profile['marital']) {
746                                 $profile['marital'] = [t('Status:'), $a->profile['marital']];
747                         }
748
749                         /// @TODO Maybe use x() here, plus below?
750                         if ($a->profile['with']) {
751                                 $profile['marital']['with'] = $a->profile['with'];
752                         }
753
754                         if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= NULL_DATE) {
755                                 $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
756                         }
757
758                         if ($a->profile['sexual']) {
759                                 $profile['sexual'] = [t('Sexual Preference:'), $a->profile['sexual']];
760                         }
761
762                         if ($a->profile['homepage']) {
763                                 $profile['homepage'] = [t('Homepage:'), linkify($a->profile['homepage'])];
764                         }
765
766                         if ($a->profile['hometown']) {
767                                 $profile['hometown'] = [t('Hometown:'), linkify($a->profile['hometown'])];
768                         }
769
770                         if ($a->profile['pub_keywords']) {
771                                 $profile['pub_keywords'] = [t('Tags:'), $a->profile['pub_keywords']];
772                         }
773
774                         if ($a->profile['politic']) {
775                                 $profile['politic'] = [t('Political Views:'), $a->profile['politic']];
776                         }
777
778                         if ($a->profile['religion']) {
779                                 $profile['religion'] = [t('Religion:'), $a->profile['religion']];
780                         }
781
782                         if ($txt = prepare_text($a->profile['about'])) {
783                                 $profile['about'] = [t('About:'), $txt];
784                         }
785
786                         if ($txt = prepare_text($a->profile['interest'])) {
787                                 $profile['interest'] = [t('Hobbies/Interests:'), $txt];
788                         }
789
790                         if ($txt = prepare_text($a->profile['likes'])) {
791                                 $profile['likes'] = [t('Likes:'), $txt];
792                         }
793
794                         if ($txt = prepare_text($a->profile['dislikes'])) {
795                                 $profile['dislikes'] = [t('Dislikes:'), $txt];
796                         }
797
798                         if ($txt = prepare_text($a->profile['contact'])) {
799                                 $profile['contact'] = [t('Contact information and Social Networks:'), $txt];
800                         }
801
802                         if ($txt = prepare_text($a->profile['music'])) {
803                                 $profile['music'] = [t('Musical interests:'), $txt];
804                         }
805
806                         if ($txt = prepare_text($a->profile['book'])) {
807                                 $profile['book'] = [t('Books, literature:'), $txt];
808                         }
809
810                         if ($txt = prepare_text($a->profile['tv'])) {
811                                 $profile['tv'] = [t('Television:'), $txt];
812                         }
813
814                         if ($txt = prepare_text($a->profile['film'])) {
815                                 $profile['film'] = [t('Film/dance/culture/entertainment:'), $txt];
816                         }
817
818                         if ($txt = prepare_text($a->profile['romance'])) {
819                                 $profile['romance'] = [t('Love/Romance:'), $txt];
820                         }
821
822                         if ($txt = prepare_text($a->profile['work'])) {
823                                 $profile['work'] = [t('Work/employment:'), $txt];
824                         }
825
826                         if ($txt = prepare_text($a->profile['education'])) {
827                                 $profile['education'] = [t('School/education:'), $txt];
828                         }
829
830                         //show subcribed forum if it is enabled in the usersettings
831                         if (Feature::isEnabled($uid, 'forumlist_profile')) {
832                                 $profile['forumlist'] = [t('Forums:'), ForumManager::profileAdvanced($uid)];
833                         }
834
835                         if ($a->profile['uid'] == local_user()) {
836                                 $profile['edit'] = [System::baseUrl() . '/profiles/' . $a->profile['id'], t('Edit profile'), '', t('Edit profile')];
837                         }
838
839                         return replace_macros($tpl, [
840                                 '$title' => t('Profile'),
841                                 '$basic' => t('Basic'),
842                                 '$advanced' => t('Advanced'),
843                                 '$profile' => $profile
844                         ]);
845                 }
846
847                 return '';
848         }
849
850         public static function getTabs($a, $is_owner = false, $nickname = null)
851         {
852                 if (is_null($nickname)) {
853                         $nickname = $a->user['nickname'];
854                 }
855
856                 $tab = false;
857                 if (x($_GET, 'tab')) {
858                         $tab = notags(trim($_GET['tab']));
859                 }
860
861                 $url = System::baseUrl() . '/profile/' . $nickname;
862
863                 $tabs = [
864                         [
865                                 'label' => t('Status'),
866                                 'url'   => $url,
867                                 'sel'   => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
868                                 'title' => t('Status Messages and Posts'),
869                                 'id'    => 'status-tab',
870                                 'accesskey' => 'm',
871                         ],
872                         [
873                                 'label' => t('Profile'),
874                                 'url'   => $url . '/?tab=profile',
875                                 'sel'   => $tab == 'profile' ? 'active' : '',
876                                 'title' => t('Profile Details'),
877                                 'id'    => 'profile-tab',
878                                 'accesskey' => 'r',
879                         ],
880                         [
881                                 'label' => t('Photos'),
882                                 'url'   => System::baseUrl() . '/photos/' . $nickname,
883                                 'sel'   => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
884                                 'title' => t('Photo Albums'),
885                                 'id'    => 'photo-tab',
886                                 'accesskey' => 'h',
887                         ],
888                         [
889                                 'label' => t('Videos'),
890                                 'url'   => System::baseUrl() . '/videos/' . $nickname,
891                                 'sel'   => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
892                                 'title' => t('Videos'),
893                                 'id'    => 'video-tab',
894                                 'accesskey' => 'v',
895                         ],
896                 ];
897
898                 // the calendar link for the full featured events calendar
899                 if ($is_owner && $a->theme_events_in_profile) {
900                         $tabs[] = [
901                                 'label' => t('Events'),
902                                 'url'   => System::baseUrl() . '/events',
903                                 'sel'   => !$tab && $a->argv[0] == 'events' ? 'active' : '',
904                                 'title' => t('Events and Calendar'),
905                                 'id'    => 'events-tab',
906                                 'accesskey' => 'e',
907                         ];
908                         // if the user is not the owner of the calendar we only show a calendar
909                         // with the public events of the calendar owner
910                 } elseif (!$is_owner) {
911                         $tabs[] = [
912                                 'label' => t('Events'),
913                                 'url'   => System::baseUrl() . '/cal/' . $nickname,
914                                 'sel'   => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
915                                 'title' => t('Events and Calendar'),
916                                 'id'    => 'events-tab',
917                                 'accesskey' => 'e',
918                         ];
919                 }
920
921                 if ($is_owner) {
922                         $tabs[] = [
923                                 'label' => t('Personal Notes'),
924                                 'url'   => System::baseUrl() . '/notes',
925                                 'sel'   => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
926                                 'title' => t('Only You Can See This'),
927                                 'id'    => 'notes-tab',
928                                 'accesskey' => 't',
929                         ];
930                 }
931
932                 if ((!$is_owner) && ((count($a->profile)) || (!$a->profile['hide-friends']))) {
933                         $tabs[] = [
934                                 'label' => t('Contacts'),
935                                 'url'   => System::baseUrl() . '/viewcontacts/' . $nickname,
936                                 'sel'   => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
937                                 'title' => t('Contacts'),
938                                 'id'    => 'viewcontacts-tab',
939                                 'accesskey' => 'k',
940                         ];
941                 }
942
943                 $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
944                 Addon::callHooks('profile_tabs', $arr);
945
946                 $tpl = get_markup_template('common_tabs.tpl');
947
948                 return replace_macros($tpl, ['$tabs' => $arr['tabs']]);
949         }
950
951         /**
952          * Retrieves the my_url session variable
953          *
954          * @return string
955          */
956         public static function getMyURL()
957         {
958                 if (x($_SESSION, 'my_url')) {
959                         return $_SESSION['my_url'];
960                 }
961                 return null;
962         }
963
964         public static function zrlInit(App $a)
965         {
966                 $my_url = self::getMyURL();
967                 $my_url = validate_url($my_url);
968                 if ($my_url) {
969                         // Is it a DDoS attempt?
970                         // The check fetches the cached value from gprobe to reduce the load for this system
971                         $urlparts = parse_url($my_url);
972
973                         $result = Cache::get('gprobe:' . $urlparts['host']);
974                         if ((!is_null($result)) && (in_array($result['network'], [NETWORK_FEED, NETWORK_PHANTOM]))) {
975                                 logger('DDoS attempt detected for ' . $urlparts['host'] . ' by ' . $_SERVER['REMOTE_ADDR'] . '. server data: ' . print_r($_SERVER, true), LOGGER_DEBUG);
976                                 return;
977                         }
978
979                         Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
980                         $arr = ['zrl' => $my_url, 'url' => $a->cmd];
981                         Addon::callHooks('zrl_init', $arr);
982                 }
983         }
984
985         public static function zrl($s, $force = false)
986         {
987                 if (!strlen($s)) {
988                         return $s;
989                 }
990                 if ((!strpos($s, '/profile/')) && (!$force)) {
991                         return $s;
992                 }
993                 if ($force && substr($s, -1, 1) !== '/') {
994                         $s = $s . '/';
995                 }
996                 $achar = strpos($s, '?') ? '&' : '?';
997                 $mine = self::getMyURL();
998                 if ($mine && !link_compare($mine, $s)) {
999                         return $s . $achar . 'zrl=' . urlencode($mine);
1000                 }
1001                 return $s;
1002         }
1003
1004         /**
1005          * Get the user ID of the page owner.
1006          *
1007          * Used from within PCSS themes to set theme parameters. If there's a
1008          * puid request variable, that is the "page owner" and normally their theme
1009          * settings take precedence; unless a local user sets the "always_my_theme"
1010          * system pconfig, which means they don't want to see anybody else's theme
1011          * settings except their own while on this site.
1012          *
1013          * @brief Get the user ID of the page owner
1014          * @return int user ID
1015          *
1016          * @note Returns local_user instead of user ID if "always_my_theme"
1017          *      is set to true
1018          */
1019         public static function getThemeUid()
1020         {
1021                 $uid = ((!empty($_REQUEST['puid'])) ? intval($_REQUEST['puid']) : 0);
1022                 if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
1023                         return local_user();
1024                 }
1025
1026                 return $uid;
1027         }
1028 }