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