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