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