]> git.mxchange.org Git - friendica.git/blob - include/identity.php
0c6224594234814b32cc32ec30447899fd507b20
[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::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
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
234         // This function can also use contact information in $profile
235         $is_contact = x($profile, 'cid');
236
237         if (!is_array($profile) && !count($profile)) {
238                 return $o;
239         }
240
241         $profile['picdate'] = urlencode(defaults($profile, 'picdate', ''));
242
243         if (($profile['network'] != '') && ($profile['network'] != NETWORK_DFRN)) {
244                 $profile['network_name'] = format_network_name($profile['network'], $profile['url']);
245         } else {
246                 $profile['network_name'] = '';
247         }
248
249         call_hooks('profile_sidebar_enter', $profile);
250
251
252         // don't show connect link to yourself
253         $connect = $profile['uid'] != local_user() ? t('Connect') : false;
254
255         // don't show connect link to authenticated visitors either
256         if (remote_user() && count($_SESSION['remote'])) {
257                 foreach ($_SESSION['remote'] as $visitor) {
258                         if ($visitor['uid'] == $profile['uid']) {
259                                 $connect = false;
260                                 break;
261                         }
262                 }
263         }
264
265         if (!$show_connect) {
266                 $connect = false;
267         }
268
269         // Is the local user already connected to that user?
270         if ($connect && local_user()) {
271                 if (isset($profile['url'])) {
272                         $profile_url = normalise_link($profile['url']);
273                 } else {
274                         $profile_url = normalise_link(System::baseUrl() . '/profile/' . $profile['nickname']);
275                 }
276
277                 if (dba::exists('contact', array('pending' => false, 'uid' => local_user(), 'nurl' => $profile_url))) {
278                         $connect = false;
279                 }
280         }
281
282         if ($connect && ($profile['network'] != NETWORK_DFRN) && !isset($profile['remoteconnect'])) {
283                 $connect = false;
284         }
285
286         $remoteconnect = null;
287         if (isset($profile['remoteconnect'])) {
288                 $remoteconnect = $profile['remoteconnect'];
289         }
290
291         if ($connect && ($profile['network'] == NETWORK_DFRN) && !isset($remoteconnect)) {
292                 $subscribe_feed = t('Atom feed');
293         } else {
294                 $subscribe_feed = false;
295         }
296
297         if (remote_user() || (get_my_url() && x($profile, 'unkmail') && ($profile['uid'] != local_user()))) {
298                 $wallmessage = t('Message');
299                 $wallmessage_link = 'wallmessage/' . $profile['nickname'];
300
301                 if (remote_user()) {
302                         $r = q(
303                                 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
304                                 intval($profile['uid']),
305                                 intval(remote_user()),
306                                 intval(CONTACT_IS_FRIEND)
307                         );
308                 } else {
309                         $r = q(
310                                 "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
311                                 intval($profile['uid']),
312                                 dbesc(normalise_link(get_my_url())),
313                                 intval(CONTACT_IS_FRIEND)
314                         );
315                 }
316                 if ($r) {
317                         $remote_url = $r[0]['url'];
318                         $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
319                         $wallmessage_link = $message_path . base64_encode($profile['addr']);
320                 }
321         } else {
322                 $wallmessage = false;
323                 $wallmessage_link = false;
324         }
325
326         // show edit profile to yourself
327         if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
328                 $profile['edit'] = array(System::baseUrl() . '/profiles', t('Profiles'), '', t('Manage/edit profiles'));
329                 $r = q(
330                         "SELECT * FROM `profile` WHERE `uid` = %d",
331                         local_user()
332                 );
333
334                 $profile['menu'] = array(
335                         'chg_photo' => t('Change profile photo'),
336                         'cr_new' => t('Create New Profile'),
337                         'entries' => array(),
338                 );
339
340                 if (DBM::is_result($r)) {
341                         foreach ($r as $rr) {
342                                 $profile['menu']['entries'][] = array(
343                                         'photo' => $rr['thumb'],
344                                         'id' => $rr['id'],
345                                         'alt' => t('Profile Image'),
346                                         'profile_name' => $rr['profile-name'],
347                                         'isdefault' => $rr['is-default'],
348                                         'visibile_to_everybody' => t('visible to everybody'),
349                                         'edit_visibility' => t('Edit visibility'),
350                                 );
351                         }
352                 }
353         }
354         if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
355                 $profile['edit'] = array(System::baseUrl() . '/profiles/' . $profile['id'], t('Edit profile'), '', t('Edit profile'));
356                 $profile['menu'] = array(
357                         'chg_photo' => t('Change profile photo'),
358                         'cr_new' => null,
359                         'entries' => array(),
360                 );
361         }
362
363         // Fetch the account type
364         $account_type = Contact::getAccountType($profile);
365
366         if (x($profile, 'address')
367                 || x($profile, 'location')
368                 || x($profile, 'locality')
369                 || x($profile, 'region')
370                 || x($profile, 'postal-code')
371                 || x($profile, 'country-name')
372         ) {
373                 $location = t('Location:');
374         }
375
376         $gender   = x($profile, 'gender')   ? t('Gender:')   : false;
377         $marital  = x($profile, 'marital')  ? t('Status:')   : false;
378         $homepage = x($profile, 'homepage') ? t('Homepage:') : false;
379         $about    = x($profile, 'about')    ? t('About:')    : false;
380         $xmpp     = x($profile, 'xmpp')     ? t('XMPP:')     : false;
381
382         if ((x($profile, 'hidewall') || $block) && !local_user() && !remote_user()) {
383                 $location = $gender = $marital = $homepage = $about = false;
384         }
385
386         $split_name = Diaspora::splitName($profile['name']);
387         $firstname = $split_name['first'];
388         $lastname = $split_name['last'];
389
390         if (x($profile, 'guid')) {
391                 $diaspora = array(
392                         'guid' => $profile['guid'],
393                         'podloc' => System::baseUrl(),
394                         'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
395                         'nickname' => $profile['nickname'],
396                         'fullname' => $profile['name'],
397                         'firstname' => $firstname,
398                         'lastname' => $lastname,
399                         'photo300' => $profile['contact_photo'],
400                         'photo100' => $profile['contact_thumb'],
401                         'photo50' => $profile['contact_micro'],
402                 );
403         } else {
404                 $diaspora = false;
405         }
406
407         $contact_block = '';
408         $updated = '';
409         $contacts = 0;
410         if (!$block) {
411                 $contact_block = contact_block();
412
413                 if (is_array($a->profile) && !$a->profile['hide-friends']) {
414                         $r = q(
415                                 "SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
416                                 intval($a->profile['uid'])
417                         );
418                         if (DBM::is_result($r)) {
419                                 $updated = date('c', strtotime($r[0]['updated']));
420                         }
421
422                         $r = q(
423                                 "SELECT COUNT(*) AS `total` FROM `contact`
424                                 WHERE `uid` = %d
425                                         AND NOT `self` AND NOT `blocked` AND NOT `pending`
426                                         AND NOT `hidden` AND NOT `archive`
427                                         AND `network` IN ('%s', '%s', '%s', '')",
428                                 intval($profile['uid']),
429                                 dbesc(NETWORK_DFRN),
430                                 dbesc(NETWORK_DIASPORA),
431                                 dbesc(NETWORK_OSTATUS)
432                         );
433                         if (DBM::is_result($r)) {
434                                 $contacts = intval($r[0]['total']);
435                         }
436                 }
437         }
438
439         $p = array();
440         foreach ($profile as $k => $v) {
441                 $k = str_replace('-', '_', $k);
442                 $p[$k] = $v;
443         }
444
445         if (isset($p['about'])) {
446                 $p['about'] = bbcode($p['about']);
447         }
448
449         if (isset($p['address'])) {
450                 $p['address'] = bbcode($p['address']);
451         } else {
452                 $p['address'] = bbcode($p['location']);
453         }
454
455         if (isset($p['photo'])) {
456                 $p['photo'] = proxy_url($p['photo'], false, PROXY_SIZE_SMALL);
457         }
458
459         $tpl = get_markup_template('profile_vcard.tpl');
460         $o .= replace_macros($tpl, array(
461                 '$profile' => $p,
462                 '$xmpp' => $xmpp,
463                 '$connect' => $connect,
464                 '$remoteconnect' => $remoteconnect,
465                 '$subscribe_feed' => $subscribe_feed,
466                 '$wallmessage' => $wallmessage,
467                 '$wallmessage_link' => $wallmessage_link,
468                 '$account_type' => $account_type,
469                 '$location' => $location,
470                 '$gender' => $gender,
471                 '$marital' => $marital,
472                 '$homepage' => $homepage,
473                 '$about' => $about,
474                 '$network' => t('Network:'),
475                 '$contacts' => $contacts,
476                 '$updated' => $updated,
477                 '$diaspora' => $diaspora,
478                 '$contact_block' => $contact_block,
479         ));
480
481         $arr = array('profile' => &$profile, 'entry' => &$o);
482
483         call_hooks('profile_sidebar', $arr);
484
485         return $o;
486 }
487
488 function get_birthdays()
489 {
490         $a = get_app();
491         $o = '';
492
493         if (!local_user() || $a->is_mobile || $a->is_tablet) {
494                 return $o;
495         }
496
497         /*
498          * $mobile_detect = new Mobile_Detect();
499          * $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
500          *              if ($is_mobile)
501          *                      return $o;
502          */
503
504         $bd_format = t('g A l F d'); // 8 AM Friday January 18
505         $bd_short = t('F d');
506
507         $cachekey = 'get_birthdays:' . local_user();
508         $r = Cache::get($cachekey);
509         if (is_null($r)) {
510                 $s = dba::p(
511                         "SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
512                         INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
513                         WHERE `event`.`uid` = ? AND `type` = 'birthday' AND `start` < ? AND `finish` > ?
514                         ORDER BY `start` ASC ",
515                         local_user(),
516                         datetime_convert('UTC', 'UTC', 'now + 6 days'),
517                         datetime_convert('UTC', 'UTC', 'now')
518                 );
519                 if (DBM::is_result($s)) {
520                         $r = dba::inArray($s);
521                         Cache::set($cachekey, $r, CACHE_HOUR);
522                 }
523         }
524         if (DBM::is_result($r)) {
525                 $total = 0;
526                 $now = strtotime('now');
527                 $cids = array();
528
529                 $istoday = false;
530                 foreach ($r as $rr) {
531                         if (strlen($rr['name'])) {
532                                 $total ++;
533                         }
534                         if ((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) {
535                                 $istoday = true;
536                         }
537                 }
538                 $classtoday = $istoday ? ' birthday-today ' : '';
539                 if ($total) {
540                         foreach ($r as &$rr) {
541                                 if (!strlen($rr['name'])) {
542                                         continue;
543                                 }
544
545                                 // avoid duplicates
546
547                                 if (in_array($rr['cid'], $cids)) {
548                                         continue;
549                                 }
550                                 $cids[] = $rr['cid'];
551
552                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
553                                 $url = $rr['url'];
554                                 if ($rr['network'] === NETWORK_DFRN) {
555                                         $url = System::baseUrl() . '/redir/' . $rr['cid'];
556                                 }
557
558                                 $rr['link'] = $url;
559                                 $rr['title'] = $rr['name'];
560                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ? ' ' . t('[today]') : '');
561                                 $rr['startime'] = null;
562                                 $rr['today'] = $today;
563                         }
564                 }
565         }
566         $tpl = get_markup_template('birthdays_reminder.tpl');
567         return replace_macros($tpl, array(
568                 '$baseurl' => System::baseUrl(),
569                 '$classtoday' => $classtoday,
570                 '$count' => $total,
571                 '$event_reminders' => t('Birthday Reminders'),
572                 '$event_title' => t('Birthdays this week:'),
573                 '$events' => $r,
574                 '$lbr' => '{', // raw brackets mess up if/endif macro processing
575                 '$rbr' => '}'
576         ));
577 }
578
579 function get_events()
580 {
581         require_once 'include/bbcode.php';
582
583         $a = get_app();
584
585         if (!local_user() || $a->is_mobile || $a->is_tablet) {
586                 return $o;
587         }
588
589         /*
590          *      $mobile_detect = new Mobile_Detect();
591          *              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
592          *              if ($is_mobile)
593          *                      return $o;
594          */
595
596         $bd_format = t('g A l F d'); // 8 AM Friday January 18
597         $classtoday = '';
598
599         $s = dba::p(
600                 "SELECT `event`.* FROM `event`
601                 WHERE `event`.`uid` = ? AND `type` != 'birthday' AND `start` < ? AND `start` >= ?
602                 ORDER BY `start` ASC ",
603                 local_user(),
604                 datetime_convert('UTC', 'UTC', 'now + 7 days'),
605                 datetime_convert('UTC', 'UTC', 'now - 1 days')
606         );
607
608         $r = array();
609
610         if (DBM::is_result($s)) {
611                 $istoday = false;
612
613                 while ($rr = dba::fetch($s)) {
614                         if (strlen($rr['name'])) {
615                                 $total ++;
616                         }
617
618                         $strt = datetime_convert('UTC', $rr['convert'] ? $a->timezone : 'UTC', $rr['start'], 'Y-m-d');
619                         if ($strt === datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) {
620                                 $istoday = true;
621                         }
622
623                         $title = strip_tags(html_entity_decode(bbcode($rr['summary']), ENT_QUOTES, 'UTF-8'));
624
625                         if (strlen($title) > 35) {
626                                 $title = substr($title, 0, 32) . '... ';
627                         }
628
629                         $description = substr(strip_tags(bbcode($rr['desc'])), 0, 32) . '... ';
630                         if (!$description) {
631                                 $description = t('[No description]');
632                         }
633
634                         $strt = datetime_convert('UTC', $rr['convert'] ? $a->timezone : 'UTC', $rr['start']);
635
636                         if (substr($strt, 0, 10) < datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) {
637                                 continue;
638                         }
639
640                         $today = ((substr($strt, 0, 10) === datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d')) ? true : false);
641
642                         $rr['title'] = $title;
643                         $rr['description'] = $description;
644                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
645                         $rr['startime'] = $strt;
646                         $rr['today'] = $today;
647
648                         $r[] = $rr;
649                 }
650                 dba::close($s);
651                 $classtoday = (($istoday) ? 'event-today' : '');
652         }
653         $tpl = get_markup_template('events_reminder.tpl');
654         return replace_macros($tpl, array(
655                 '$baseurl' => System::baseUrl(),
656                 '$classtoday' => $classtoday,
657                 '$count' => count($r),
658                 '$event_reminders' => t('Event Reminders'),
659                 '$event_title' => t('Events this week:'),
660                 '$events' => $r,
661         ));
662 }
663
664 function advanced_profile(App $a)
665 {
666         $o = '';
667         $uid = $a->profile['uid'];
668
669         $o .= replace_macros(
670                 get_markup_template('section_title.tpl'), array(
671                         '$title' => t('Profile')
672                 )
673         );
674
675         if ($a->profile['name']) {
676                 $tpl = get_markup_template('profile_advanced.tpl');
677
678                 $profile = array();
679
680                 $profile['fullname'] = array(t('Full Name:'), $a->profile['name']);
681
682                 if ($a->profile['gender']) {
683                         $profile['gender'] = array(t('Gender:'), $a->profile['gender']);
684                 }
685
686                 if (($a->profile['dob']) && ($a->profile['dob'] > '0001-01-01')) {
687                         $year_bd_format = t('j F, Y');
688                         $short_bd_format = t('j F');
689
690                         $val = intval($a->profile['dob']) ?
691                                 day_translate(datetime_convert('UTC', 'UTC', $a->profile['dob'] . ' 00:00 +00:00', $year_bd_format))
692                                 : day_translate(datetime_convert('UTC', 'UTC', '2001-' . substr($a->profile['dob'], 5) . ' 00:00 +00:00', $short_bd_format));
693
694                         $profile['birthday'] = array(t('Birthday:'), $val);
695                 }
696
697                 if (!empty($a->profile['dob'])
698                         && $a->profile['dob'] > '0001-01-01'
699                         && $age = age($a->profile['dob'], $a->profile['timezone'], '')
700                 ) {
701                         $profile['age'] = array(t('Age:'), $age);
702                 }
703
704                 if ($a->profile['marital']) {
705                         $profile['marital'] = array(t('Status:'), $a->profile['marital']);
706                 }
707
708                 /// @TODO Maybe use x() here, plus below?
709                 if ($a->profile['with']) {
710                         $profile['marital']['with'] = $a->profile['with'];
711                 }
712
713                 if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= NULL_DATE) {
714                         $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
715                 }
716
717                 if ($a->profile['sexual']) {
718                         $profile['sexual'] = array(t('Sexual Preference:'), $a->profile['sexual']);
719                 }
720
721                 if ($a->profile['homepage']) {
722                         $profile['homepage'] = array(t('Homepage:'), linkify($a->profile['homepage']));
723                 }
724
725                 if ($a->profile['hometown']) {
726                         $profile['hometown'] = array(t('Hometown:'), linkify($a->profile['hometown']));
727                 }
728
729                 if ($a->profile['pub_keywords']) {
730                         $profile['pub_keywords'] = array(t('Tags:'), $a->profile['pub_keywords']);
731                 }
732
733                 if ($a->profile['politic']) {
734                         $profile['politic'] = array(t('Political Views:'), $a->profile['politic']);
735                 }
736
737                 if ($a->profile['religion']) {
738                         $profile['religion'] = array(t('Religion:'), $a->profile['religion']);
739                 }
740
741                 if ($txt = prepare_text($a->profile['about'])) {
742                         $profile['about'] = array(t('About:'), $txt);
743                 }
744
745                 if ($txt = prepare_text($a->profile['interest'])) {
746                         $profile['interest'] = array(t('Hobbies/Interests:'), $txt);
747                 }
748
749                 if ($txt = prepare_text($a->profile['likes'])) {
750                         $profile['likes'] = array(t('Likes:'), $txt);
751                 }
752
753                 if ($txt = prepare_text($a->profile['dislikes'])) {
754                         $profile['dislikes'] = array(t('Dislikes:'), $txt);
755                 }
756
757                 if ($txt = prepare_text($a->profile['contact'])) {
758                         $profile['contact'] = array(t('Contact information and Social Networks:'), $txt);
759                 }
760
761                 if ($txt = prepare_text($a->profile['music'])) {
762                         $profile['music'] = array(t('Musical interests:'), $txt);
763                 }
764
765                 if ($txt = prepare_text($a->profile['book'])) {
766                         $profile['book'] = array(t('Books, literature:'), $txt);
767                 }
768
769                 if ($txt = prepare_text($a->profile['tv'])) {
770                         $profile['tv'] = array(t('Television:'), $txt);
771                 }
772
773                 if ($txt = prepare_text($a->profile['film'])) {
774                         $profile['film'] = array(t('Film/dance/culture/entertainment:'), $txt);
775                 }
776
777                 if ($txt = prepare_text($a->profile['romance'])) {
778                         $profile['romance'] = array(t('Love/Romance:'), $txt);
779                 }
780
781                 if ($txt = prepare_text($a->profile['work'])) {
782                         $profile['work'] = array(t('Work/employment:'), $txt);
783                 }
784
785                 if ($txt = prepare_text($a->profile['education'])) {
786                         $profile['education'] = array(t('School/education:'), $txt);
787                 }
788
789                 //show subcribed forum if it is enabled in the usersettings
790                 if (Feature::isEnabled($uid, 'forumlist_profile')) {
791                         $profile['forumlist'] = array(t('Forums:'), ForumManager::profileAdvanced($uid));
792                 }
793
794                 if ($a->profile['uid'] == local_user()) {
795                         $profile['edit'] = array(System::baseUrl() . '/profiles/' . $a->profile['id'], t('Edit profile'), '', t('Edit profile'));
796                 }
797
798                 return replace_macros($tpl, array(
799                         '$title' => t('Profile'),
800                         '$basic' => t('Basic'),
801                         '$advanced' => t('Advanced'),
802                         '$profile' => $profile
803                 ));
804         }
805
806         return '';
807 }
808
809 function profile_tabs($a, $is_owner = false, $nickname = null)
810 {
811         if (is_null($nickname)) {
812                 $nickname = $a->user['nickname'];
813         }
814
815         $tab = false;
816         if (x($_GET, 'tab')) {
817                 $tab = notags(trim($_GET['tab']));
818         }
819
820         $url = System::baseUrl() . '/profile/' . $nickname;
821
822         $tabs = array(
823                 array(
824                         'label' => t('Status'),
825                         'url'   => $url,
826                         'sel'   => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
827                         'title' => t('Status Messages and Posts'),
828                         'id'    => 'status-tab',
829                         'accesskey' => 'm',
830                 ),
831                 array(
832                         'label' => t('Profile'),
833                         'url'   => $url . '/?tab=profile',
834                         'sel'   => $tab == 'profile' ? 'active' : '',
835                         'title' => t('Profile Details'),
836                         'id'    => 'profile-tab',
837                         'accesskey' => 'r',
838                 ),
839                 array(
840                         'label' => t('Photos'),
841                         'url'   => System::baseUrl() . '/photos/' . $nickname,
842                         'sel'   => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
843                         'title' => t('Photo Albums'),
844                         'id'    => 'photo-tab',
845                         'accesskey' => 'h',
846                 ),
847                 array(
848                         'label' => t('Videos'),
849                         'url'   => System::baseUrl() . '/videos/' . $nickname,
850                         'sel'   => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
851                         'title' => t('Videos'),
852                         'id'    => 'video-tab',
853                         'accesskey' => 'v',
854                 ),
855         );
856
857         // the calendar link for the full featured events calendar
858         if ($is_owner && $a->theme_events_in_profile) {
859                 $tabs[] = array(
860                         'label' => t('Events'),
861                         'url'   => System::baseUrl() . '/events',
862                         'sel'   => !$tab && $a->argv[0] == 'events' ? 'active' : '',
863                         'title' => t('Events and Calendar'),
864                         'id'    => 'events-tab',
865                         'accesskey' => 'e',
866                 );
867                 // if the user is not the owner of the calendar we only show a calendar
868                 // with the public events of the calendar owner
869         } elseif (!$is_owner) {
870                 $tabs[] = array(
871                         'label' => t('Events'),
872                         'url'   => System::baseUrl() . '/cal/' . $nickname,
873                         'sel'   => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
874                         'title' => t('Events and Calendar'),
875                         'id'    => 'events-tab',
876                         'accesskey' => 'e',
877                 );
878         }
879
880         if ($is_owner) {
881                 $tabs[] = array(
882                         'label' => t('Personal Notes'),
883                         'url'   => System::baseUrl() . '/notes',
884                         'sel'   => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
885                         'title' => t('Only You Can See This'),
886                         'id'    => 'notes-tab',
887                         'accesskey' => 't',
888                 );
889         }
890
891         if ((!$is_owner) && ((count($a->profile)) || (!$a->profile['hide-friends']))) {
892                 $tabs[] = array(
893                         'label' => t('Contacts'),
894                         'url'   => System::baseUrl() . '/viewcontacts/' . $nickname,
895                         'sel'   => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
896                         'title' => t('Contacts'),
897                         'id'    => 'viewcontacts-tab',
898                         'accesskey' => 'k',
899                 );
900         }
901
902         $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs);
903         call_hooks('profile_tabs', $arr);
904
905         $tpl = get_markup_template('common_tabs.tpl');
906
907         return replace_macros($tpl, array('$tabs' => $arr['tabs']));
908 }
909
910 /**
911  * Retrieves the my_url session variable
912  *
913  * @return string
914  */
915 function get_my_url()
916 {
917         if (x($_SESSION, 'my_url')) {
918                 return $_SESSION['my_url'];
919         }
920         return null;
921 }
922
923 function zrl_init(App $a)
924 {
925         $my_url = get_my_url();
926         $my_url = validate_url($my_url);
927         if ($my_url) {
928                 // Is it a DDoS attempt?
929                 // The check fetches the cached value from gprobe to reduce the load for this system
930                 $urlparts = parse_url($my_url);
931
932                 $result = Cache::get('gprobe:' . $urlparts['host']);
933                 if ((!is_null($result)) && (in_array($result['network'], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
934                         logger('DDoS attempt detected for ' . $urlparts['host'] . ' by ' . $_SERVER['REMOTE_ADDR'] . '. server data: ' . print_r($_SERVER, true), LOGGER_DEBUG);
935                         return;
936                 }
937
938                 Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
939                 $arr = array('zrl' => $my_url, 'url' => $a->cmd);
940                 call_hooks('zrl_init', $arr);
941         }
942 }
943
944 function zrl($s, $force = false)
945 {
946         if (!strlen($s)) {
947                 return $s;
948         }
949         if ((!strpos($s, '/profile/')) && (!$force)) {
950                 return $s;
951         }
952         if ($force && substr($s, -1, 1) !== '/') {
953                 $s = $s . '/';
954         }
955         $achar = strpos($s, '?') ? '&' : '?';
956         $mine = get_my_url();
957         if ($mine && !link_compare($mine, $s)) {
958                 return $s . $achar . 'zrl=' . urlencode($mine);
959         }
960         return $s;
961 }
962
963 /**
964  * @brief Get the user ID of the page owner
965  *
966  * Used from within PCSS themes to set theme parameters. If there's a
967  * puid request variable, that is the "page owner" and normally their theme
968  * settings take precedence; unless a local user sets the "always_my_theme"
969  * system pconfig, which means they don't want to see anybody else's theme
970  * settings except their own while on this site.
971  *
972  * @return int user ID
973  *
974  * @note Returns local_user instead of user ID if "always_my_theme"
975  *      is set to true
976  */
977 function get_theme_uid()
978 {
979         $uid = ((!empty($_REQUEST['puid'])) ? intval($_REQUEST['puid']) : 0);
980         if ((local_user()) && ((PConfig::get(local_user(), 'system', 'always_my_theme')) || (!$uid))) {
981                 return local_user();
982         }
983
984         return $uid;
985 }