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