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