]> git.mxchange.org Git - friendica.git/blob - include/identity.php
Wall message can now redirect to your own page if you are connected.
[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)
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(count($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 `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* FROM `profile`
153                                 INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
154                                 WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d AND `contact`.`self` = 1 LIMIT 1",
155                                 dbesc($nickname),
156                                 intval($profile_int)
157                 );
158         }
159         if((!$r) && (!count($r))) {
160                 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `contact`.`addr`, `user`.* FROM `profile`
161                                 INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
162                                 WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 AND `contact`.`self` = 1 LIMIT 1",
163                                 dbesc($nickname)
164                 );
165         }
166
167         return $r[0];
168
169 }
170
171
172 /**
173  * @brief Formats a profile for display in the sidebar.
174  * 
175  * It is very difficult to templatise the HTML completely
176  * because of all the conditional logic.
177  * 
178  * @param array $profile
179  * @param int $block
180  * 
181  * @return HTML string stuitable for sidebar inclusion
182  * 
183  * @note Returns empty string if passed $profile is wrong type or not populated
184  * 
185  * @hooks 'profile_sidebar_enter'
186  *      array $profile - profile data
187  * @hooks 'profile_sidebar'
188  *      array $arr
189  */
190 function profile_sidebar($profile, $block = 0) {
191         $a = get_app();
192
193         $o = '';
194         $location = false;
195         $address = false;
196 //              $pdesc = true;
197
198         if((! is_array($profile)) && (! count($profile)))
199                 return $o;
200
201         $profile['picdate'] = urlencode($profile['picdate']);
202
203         if (($profile['network'] != "") AND ($profile['network'] != NETWORK_DFRN)) {
204                 $profile['network_name'] = format_network_name($profile['network'],$profile['url']);
205         } else
206                 $profile['network_name'] = "";
207
208         call_hooks('profile_sidebar_enter', $profile);
209
210
211         // don't show connect link to yourself
212         $connect = (($profile['uid'] != local_user()) ? t('Connect')  : False);
213
214         // don't show connect link to authenticated visitors either
215         if(remote_user() && count($_SESSION['remote'])) {
216                 foreach($_SESSION['remote'] as $visitor) {
217                         if($visitor['uid'] == $profile['uid']) {
218                                 $connect = false;
219                                 break;
220                         }
221                 }
222         }
223
224         // Is the local user already connected to that user?
225         if ($connect AND local_user()) {
226                 if (isset($profile["url"]))
227                         $profile_url = normalise_link($profile["url"]);
228                 else
229                         $profile_url = normalise_link($a->get_baseurl()."/profile/".$profile["nickname"]);
230
231                 $r = q("SELECT * FROM `contact` WHERE NOT `pending` AND `uid` = %d AND `nurl` = '%s'",
232                         local_user(), $profile_url);
233                 if (count($r))
234                         $connect = false;
235         }
236
237         if ($connect AND ($profile['network'] != NETWORK_DFRN) AND !isset($profile['remoteconnect']))
238                 $connect = false;
239
240         $remoteconnect = NULL;
241         if (isset($profile['remoteconnect']))
242                 $remoteconnect = $profile['remoteconnect'];
243
244         if ($connect AND ($profile['network'] == NETWORK_DFRN) AND !isset($remoteconnect))
245                 $subscribe_feed = t("Atom feed");
246         else
247                 $subscribe_feed = false;
248
249         if(get_my_url() && $profile['unkmail'] && ($profile['uid'] != local_user())) {
250                 $wallmessage = t('Message');
251
252                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
253                         intval($profile['uid']),
254                         dbesc(normalise_link(get_my_url())),
255                         intval(CONTACT_IS_FRIEND));
256                 if ($r) {
257                         $message_path = preg_replace("=(.*)/profile/(.*)=ism", "$1/message/new/", get_my_url());
258                         $wallmessage_link = $message_path.base64_encode(get_my_url());
259                 } else
260                         $wallmessage_link = "wallmessage/".$profile["nickname"];
261
262         } else {
263                 $wallmessage = false;
264                 $wallmessage_link = false;
265         }
266
267         // show edit profile to yourself
268         if ($profile['uid'] == local_user() && feature_enabled(local_user(),'multi_profiles')) {
269                 $profile['edit'] = array($a->get_baseurl(). '/profiles', t('Profiles'),"", t('Manage/edit profiles'));
270                 $r = q("SELECT * FROM `profile` WHERE `uid` = %d",
271                                 local_user());
272
273                 $profile['menu'] = array(
274                         'chg_photo' => t('Change profile photo'),
275                         'cr_new' => t('Create New Profile'),
276                         'entries' => array(),
277                 );
278
279                 if(count($r)) {
280
281                         foreach($r as $rr) {
282                                 $profile['menu']['entries'][] = array(
283                                         'photo' => $rr['thumb'],
284                                         'id' => $rr['id'],
285                                         'alt' => t('Profile Image'),
286                                         'profile_name' => $rr['profile-name'],
287                                         'isdefault' => $rr['is-default'],
288                                         'visibile_to_everybody' =>  t('visible to everybody'),
289                                         'edit_visibility' => t('Edit visibility'),
290
291                                 );
292                         }
293
294
295                 }
296         }
297         if ($profile['uid'] == local_user() && !feature_enabled(local_user(),'multi_profiles')) {
298                 $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$profile['id'], t('Edit profile'),"", t('Edit profile'));
299                 $profile['menu'] = array(
300                         'chg_photo' => t('Change profile photo'),
301                         'cr_new' => null,
302                         'entries' => array(),
303                 );
304         }
305
306         // check if profile is a forum
307         if((intval($profile['page-flags']) == PAGE_COMMUNITY)
308                         || (intval($profile['page-flags']) == PAGE_PRVGROUP)
309                         || (isset($profile['forum']) && intval($profile['forum']))
310                         || (isset($profile['prv']) && intval($profile['prv']))
311                         || (isset($profile['community']) && intval($profile['community'])))
312                 $account_type = t('Forum');
313         else
314                 $account_type = "";
315
316         if((x($profile,'address') == 1)
317                         || (x($profile,'location') == 1)
318                         || (x($profile,'locality') == 1)
319                         || (x($profile,'region') == 1)
320                         || (x($profile,'postal-code') == 1)
321                         || (x($profile,'country-name') == 1))
322                 $location = t('Location:');
323
324         $gender = ((x($profile,'gender') == 1) ? t('Gender:') : False);
325
326
327         $marital = ((x($profile,'marital') == 1) ?  t('Status:') : False);
328
329         $homepage = ((x($profile,'homepage') == 1) ?  t('Homepage:') : False);
330
331         $about = ((x($profile,'about') == 1) ?  t('About:') : False);
332
333         if(($profile['hidewall'] || $block) && (! local_user()) && (! remote_user())) {
334                 $location = $pdesc = $gender = $marital = $homepage = $about = False;
335         }
336
337         $firstname = ((strpos($profile['name'],' '))
338                         ? trim(substr($profile['name'],0,strpos($profile['name'],' '))) : $profile['name']);
339         $lastname = (($firstname === $profile['name']) ? '' : trim(substr($profile['name'],strlen($firstname))));
340
341         $diaspora = array(
342                 'guid' => $profile['guid'],
343                 'podloc' => $a->get_baseurl(),
344                 'searchable' => (($profile['publish'] && $profile['net-publish']) ? 'true' : 'false' ),
345                 'nickname' => $profile['nickname'],
346                 'fullname' => $profile['name'],
347                 'firstname' => $firstname,
348                 'lastname' => $lastname,
349                 'photo300' => $a->get_baseurl() . '/photo/custom/300/' . $profile['uid'] . '.jpg',
350                 'photo100' => $a->get_baseurl() . '/photo/custom/100/' . $profile['uid'] . '.jpg',
351                 'photo50' => $a->get_baseurl() . '/photo/custom/50/'  . $profile['uid'] . '.jpg',
352         );
353
354         if (!$block){
355                 $contact_block = contact_block();
356
357                 if(is_array($a->profile) AND !$a->profile['hide-friends']) {
358                         $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
359                                 intval($a->profile['uid']));
360                         if(count($r))
361                                 $updated =  date("c", strtotime($r[0]['updated']));
362
363                         $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
364                                         AND `network` IN ('%s', '%s', '%s', '')",
365                                 intval($profile['uid']),
366                                 dbesc(NETWORK_DFRN),
367                                 dbesc(NETWORK_DIASPORA),
368                                 dbesc(NETWORK_OSTATUS)
369                         );
370                         if(count($r))
371                                 $contacts = intval($r[0]['total']);
372                 }
373         }
374
375         $p = array();
376         foreach($profile as $k => $v) {
377                 $k = str_replace('-','_',$k);
378                 $p[$k] = $v;
379         }
380
381         if (isset($p["about"]))
382                 $p["about"] = bbcode($p["about"]);
383
384         if (isset($p["address"]))
385                 $p["address"] = bbcode($p["address"]);
386         else
387                 $p["address"] = bbcode($p["location"]);
388
389         if (isset($p["photo"]))
390                 $p["photo"] = proxy_url($p["photo"], false, PROXY_SIZE_SMALL);
391
392         if($a->theme['template_engine'] === 'internal')
393                 $location = template_escape($location);
394
395         $tpl = get_markup_template('profile_vcard.tpl');
396         $o .= replace_macros($tpl, array(
397                 '$profile' => $p,
398                 '$connect'  => $connect,
399                 '$remoteconnect'  => $remoteconnect,
400                 '$subscribe_feed' => $subscribe_feed,
401                 '$wallmessage' => $wallmessage,
402                 '$wallmessage_link' => $wallmessage_link,
403                 '$account_type' => $account_type,
404                 '$location' => $location,
405                 '$gender'   => $gender,
406 //                      '$pdesc'        => $pdesc,
407                 '$marital'  => $marital,
408                 '$homepage' => $homepage,
409                 '$about' => $about,
410                 '$network' =>  t('Network:'),
411                 '$contacts' => $contacts,
412                 '$updated' => $updated,
413                 '$diaspora' => $diaspora,
414                 '$contact_block' => $contact_block,
415         ));
416
417         $arr = array('profile' => &$profile, 'entry' => &$o);
418
419         call_hooks('profile_sidebar', $arr);
420
421         return $o;
422 }
423
424
425 function get_birthdays() {
426
427         $a = get_app();
428         $o = '';
429
430         if(! local_user() || $a->is_mobile || $a->is_tablet)
431                 return $o;
432
433 //              $mobile_detect = new Mobile_Detect();
434 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
435
436 //              if($is_mobile)
437 //                      return $o;
438
439         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
440         $bd_short = t('F d');
441
442         $r = q("SELECT `event`.*, `event`.`id` AS `eid`, `contact`.* FROM `event`
443                         INNER JOIN `contact` ON `contact`.`id` = `event`.`cid`
444                         WHERE `event`.`uid` = %d AND `type` = 'birthday' AND `start` < '%s' AND `finish` > '%s'
445                         ORDER BY `start` ASC ",
446                         intval(local_user()),
447                         dbesc(datetime_convert('UTC','UTC','now + 6 days')),
448                         dbesc(datetime_convert('UTC','UTC','now'))
449         );
450
451         if($r && count($r)) {
452                 $total = 0;
453                 $now = strtotime('now');
454                 $cids = array();
455
456                 $istoday = false;
457                 foreach($r as $rr) {
458                         if(strlen($rr['name']))
459                                 $total ++;
460                         if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now))
461                                 $istoday = true;
462                 }
463                 $classtoday = $istoday ? ' birthday-today ' : '';
464                 if($total) {
465                         foreach($r as &$rr) {
466                                 if(! strlen($rr['name']))
467                                         continue;
468
469                                 // avoid duplicates
470
471                                 if(in_array($rr['cid'],$cids))
472                                         continue;
473                                 $cids[] = $rr['cid'];
474
475                                 $today = (((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) ? true : false);
476                                 $sparkle = '';
477                                 $url = $rr['url'];
478                                 if($rr['network'] === NETWORK_DFRN) {
479                                         $sparkle = " sparkle";
480                                         $url = $a->get_baseurl() . '/redir/'  . $rr['cid'];
481                                 }
482
483                                 $rr['link'] = $url;
484                                 $rr['title'] = $rr['name'];
485                                 $rr['date'] = day_translate(datetime_convert('UTC', $a->timezone, $rr['start'], $rr['adjust'] ? $bd_format : $bd_short)) . (($today) ?  ' ' . t('[today]') : '');
486                                 $rr['startime'] = Null;
487                                 $rr['today'] = $today;
488
489                         }
490                 }
491         }
492         $tpl = get_markup_template("birthdays_reminder.tpl");
493         return replace_macros($tpl, array(
494                 '$baseurl' => $a->get_baseurl(),
495                 '$classtoday' => $classtoday,
496                 '$count' => $total,
497                 '$event_reminders' => t('Birthday Reminders'),
498                 '$event_title' => t('Birthdays this week:'),
499                 '$events' => $r,
500                 '$lbr' => '{',  // raw brackets mess up if/endif macro processing
501                 '$rbr' => '}'
502
503         ));
504 }
505
506
507 function get_events() {
508
509         require_once('include/bbcode.php');
510
511         $a = get_app();
512
513         if(! local_user() || $a->is_mobile || $a->is_tablet)
514                 return $o;
515
516
517 //              $mobile_detect = new Mobile_Detect();
518 //              $is_mobile = $mobile_detect->isMobile() || $mobile_detect->isTablet();
519
520 //              if($is_mobile)
521 //                      return $o;
522
523         $bd_format = t('g A l F d') ; // 8 AM Friday January 18
524         $bd_short = t('F d');
525
526         $r = q("SELECT `event`.* FROM `event`
527                         WHERE `event`.`uid` = %d AND `type` != 'birthday' AND `start` < '%s' AND `start` >= '%s'
528                         ORDER BY `start` ASC ",
529                         intval(local_user()),
530                         dbesc(datetime_convert('UTC','UTC','now + 7 days')),
531                         dbesc(datetime_convert('UTC','UTC','now - 1 days'))
532         );
533
534         if($r && count($r)) {
535                 $now = strtotime('now');
536                 $istoday = false;
537                 foreach($r as $rr) {
538                         if(strlen($rr['name']))
539                                 $total ++;
540
541                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start'],'Y-m-d');
542                         if($strt === datetime_convert('UTC',$a->timezone,'now','Y-m-d'))
543                                 $istoday = true;
544                 }
545                 $classtoday = (($istoday) ? 'event-today' : '');
546
547                 $skip = 0;
548
549                 foreach($r as &$rr) {
550                         $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
551
552                         if(strlen($title) > 35)
553                                 $title = substr($title,0,32) . '... ';
554
555                         $description = substr(strip_tags(bbcode($rr['desc'])),0,32) . '... ';
556                         if(! $description)
557                                 $description = t('[No description]');
558
559                         $strt = datetime_convert('UTC',$rr['convert'] ? $a->timezone : 'UTC',$rr['start']);
560
561                         if(substr($strt,0,10) < datetime_convert('UTC',$a->timezone,'now','Y-m-d')) {
562                                 $skip++;
563                                 continue;
564                         }
565
566                         $today = ((substr($strt,0,10) === datetime_convert('UTC',$a->timezone,'now','Y-m-d')) ? true : false);
567
568                         $rr['title'] = $title;
569                         $rr['description'] = $desciption;
570                         $rr['date'] = day_translate(datetime_convert('UTC', $rr['adjust'] ? $a->timezone : 'UTC', $rr['start'], $bd_format)) . (($today) ?  ' ' . t('[today]') : '');
571                         $rr['startime'] = $strt;
572                         $rr['today'] = $today;
573                 }
574         }
575
576         $tpl = get_markup_template("events_reminder.tpl");
577         return replace_macros($tpl, array(
578                 '$baseurl' => $a->get_baseurl(),
579                 '$classtoday' => $classtoday,
580                 '$count' => count($r) - $skip,
581                 '$event_reminders' => t('Event Reminders'),
582                 '$event_title' => t('Events this week:'),
583                 '$events' => $r,
584         ));
585 }
586
587 function advanced_profile(&$a) {
588
589         $o = '';
590         $uid = $a->profile['uid'];
591
592         $o .= replace_macros(get_markup_template('section_title.tpl'),array(
593                 '$title' => t('Profile')
594         ));
595
596         if($a->profile['name']) {
597
598                 $tpl = get_markup_template('profile_advanced.tpl');
599
600                 $profile = array();
601
602                 $profile['fullname'] = array( t('Full Name:'), $a->profile['name'] ) ;
603
604                 if($a->profile['gender']) $profile['gender'] = array( t('Gender:'),  $a->profile['gender'] );
605
606
607                 if(($a->profile['dob']) && ($a->profile['dob'] != '0000-00-00')) {
608
609                         $year_bd_format = t('j F, Y');
610                         $short_bd_format = t('j F');
611
612
613                         $val = ((intval($a->profile['dob']))
614                                 ? day_translate(datetime_convert('UTC','UTC',$a->profile['dob'] . ' 00:00 +00:00',$year_bd_format))
615                                 : day_translate(datetime_convert('UTC','UTC','2001-' . substr($a->profile['dob'],5) . ' 00:00 +00:00',$short_bd_format)));
616
617                         $profile['birthday'] = array( t('Birthday:'), $val);
618
619                 }
620
621                 if($age = age($a->profile['dob'],$a->profile['timezone'],''))  $profile['age'] = array( t('Age:'), $age );
622
623
624                 if($a->profile['marital']) $profile['marital'] = array( t('Status:'), $a->profile['marital']);
625
626
627                 if($a->profile['with']) $profile['marital']['with'] = $a->profile['with'];
628
629                 if(strlen($a->profile['howlong']) && $a->profile['howlong'] !== '0000-00-00 00:00:00') {
630                                 $profile['howlong'] = relative_date($a->profile['howlong'], t('for %1$d %2$s'));
631                 }
632
633                 if($a->profile['sexual']) $profile['sexual'] = array( t('Sexual Preference:'), $a->profile['sexual'] );
634
635                 if($a->profile['homepage']) $profile['homepage'] = array( t('Homepage:'), linkify($a->profile['homepage']) );
636
637                 if($a->profile['hometown']) $profile['hometown'] = array( t('Hometown:'), linkify($a->profile['hometown']) );
638
639                 if($a->profile['pub_keywords']) $profile['pub_keywords'] = array( t('Tags:'), $a->profile['pub_keywords']);
640
641                 if($a->profile['politic']) $profile['politic'] = array( t('Political Views:'), $a->profile['politic']);
642
643                 if($a->profile['religion']) $profile['religion'] = array( t('Religion:'), $a->profile['religion']);
644
645                 if($txt = prepare_text($a->profile['about'])) $profile['about'] = array( t('About:'), $txt );
646
647                 if($txt = prepare_text($a->profile['interest'])) $profile['interest'] = array( t('Hobbies/Interests:'), $txt);
648
649                 if($txt = prepare_text($a->profile['likes'])) $profile['likes'] = array( t('Likes:'), $txt);
650
651                 if($txt = prepare_text($a->profile['dislikes'])) $profile['dislikes'] = array( t('Dislikes:'), $txt);
652
653
654                 if($txt = prepare_text($a->profile['contact'])) $profile['contact'] = array( t('Contact information and Social Networks:'), $txt);
655
656                 if($txt = prepare_text($a->profile['music'])) $profile['music'] = array( t('Musical interests:'), $txt);
657
658                 if($txt = prepare_text($a->profile['book'])) $profile['book'] = array( t('Books, literature:'), $txt);
659
660                 if($txt = prepare_text($a->profile['tv'])) $profile['tv'] = array( t('Television:'), $txt);
661
662                 if($txt = prepare_text($a->profile['film'])) $profile['film'] = array( t('Film/dance/culture/entertainment:'), $txt);
663
664                 if($txt = prepare_text($a->profile['romance'])) $profile['romance'] = array( t('Love/Romance:'), $txt);
665
666                 if($txt = prepare_text($a->profile['work'])) $profile['work'] = array( t('Work/employment:'), $txt);
667
668                 if($txt = prepare_text($a->profile['education'])) $profile['education'] = array( t('School/education:'), $txt );
669         
670                 //show subcribed forum if it is enabled in the usersettings
671                 if (feature_enabled($uid,'forumlist_profile')) {
672                         $profile['forumlist'] = array( t('Forums:'), ForumManager::profile_advanced($uid));
673                 }
674
675                 if ($a->profile['uid'] == local_user())
676                         $profile['edit'] = array($a->get_baseurl(). '/profiles/'.$a->profile['id'], t('Edit profile'),"", t('Edit profile'));
677
678                 return replace_macros($tpl, array(
679                         '$title' => t('Profile'),
680                         '$profile' => $profile
681                 ));
682         }
683
684         return '';
685 }
686
687 function profile_tabs($a, $is_owner=False, $nickname=Null){
688         //echo "<pre>"; var_dump($a->user); killme();
689
690         if (is_null($nickname))
691                 $nickname  = $a->user['nickname'];
692
693         if(x($_GET,'tab'))
694                 $tab = notags(trim($_GET['tab']));
695
696         $url = $a->get_baseurl() . '/profile/' . $nickname;
697
698         $tabs = array(
699                 array(
700                         'label'=>t('Status'),
701                         'url' => $url,
702                         'sel' => ((!isset($tab)&&$a->argv[0]=='profile')?'active':''),
703                         'title' => t('Status Messages and Posts'),
704                         'id' => 'status-tab',
705                         'accesskey' => 'm',
706                 ),
707                 array(
708                         'label' => t('Profile'),
709                         'url'   => $url.'/?tab=profile',
710                         'sel'   => ((isset($tab) && $tab=='profile')?'active':''),
711                         'title' => t('Profile Details'),
712                         'id' => 'profile-tab',
713                         'accesskey' => 'r',
714                 ),
715                 array(
716                         'label' => t('Photos'),
717                         'url'   => $a->get_baseurl() . '/photos/' . $nickname,
718                         'sel'   => ((!isset($tab)&&$a->argv[0]=='photos')?'active':''),
719                         'title' => t('Photo Albums'),
720                         'id' => 'photo-tab',
721                         'accesskey' => 'h',
722                 ),
723                 array(
724                         'label' => t('Videos'),
725                         'url'   => $a->get_baseurl() . '/videos/' . $nickname,
726                         'sel'   => ((!isset($tab)&&$a->argv[0]=='videos')?'active':''),
727                         'title' => t('Videos'),
728                         'id' => 'video-tab',
729                         'accesskey' => 'v',
730                 ),
731         );
732
733         if ($is_owner){
734                 if ($a->theme_events_in_profile)
735                         $tabs[] = array(
736                                 'label' => t('Events'),
737                                 'url'   => $a->get_baseurl() . '/events',
738                                 'sel'   =>((!isset($tab)&&$a->argv[0]=='events')?'active':''),
739                                 'title' => t('Events and Calendar'),
740                                 'id' => 'events-tab',
741                                 'accesskey' => 'e',
742                         );
743                 $tabs[] = array(
744                         'label' => t('Personal Notes'),
745                         'url'   => $a->get_baseurl() . '/notes',
746                         'sel'   =>((!isset($tab)&&$a->argv[0]=='notes')?'active':''),
747                         'title' => t('Only You Can See This'),
748                         'id' => 'notes-tab',
749                         'accesskey' => 't',
750                 );
751         }
752
753         if ((! $is_owner) && ((count($a->profile)) || (! $a->profile['hide-friends']))) {
754                 $tabs[] = array(
755                         'label' => t('Contacts'),
756                         'url'   => $a->get_baseurl() . '/viewcontacts/' . $nickname,
757                         'sel'   => ((!isset($tab)&&$a->argv[0]=='viewcontacts')?'active':''),
758                         'title' => t('Contacts'),
759                         'id' => 'viewcontacts-tab',
760                         'accesskey' => 'k',
761                 );
762         }
763
764         $arr = array('is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => (($tab) ? $tab : false), 'tabs' => $tabs);
765         call_hooks('profile_tabs', $arr);
766
767         $tpl = get_markup_template('common_tabs.tpl');
768
769         return replace_macros($tpl,array('$tabs' => $arr['tabs']));
770 }
771
772 function get_my_url() {
773         if(x($_SESSION,'my_url'))
774                 return $_SESSION['my_url'];
775         return false;
776 }
777
778 function zrl_init(&$a) {
779         $tmp_str = get_my_url();
780         if(validate_url($tmp_str)) {
781
782                 // Is it a DDoS attempt?
783                 // The check fetches the cached value from gprobe to reduce the load for this system
784                 $urlparts = parse_url($tmp_str);
785
786                 $result = Cache::get("gprobe:".$urlparts["host"]);
787                 if (!is_null($result)) {
788                         $result = unserialize($result);
789                         if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
790                                 logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
791                                 return;
792                         }
793                 }
794
795                 proc_run('php','include/gprobe.php',bin2hex($tmp_str));
796                 $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
797                 call_hooks('zrl_init',$arr);
798         }
799 }
800
801 function zrl($s,$force = false) {
802         if(! strlen($s))
803                 return $s;
804         if((! strpos($s,'/profile/')) && (! $force))
805                 return $s;
806         if($force && substr($s,-1,1) !== '/')
807                 $s = $s . '/';
808         $achar = strpos($s,'?') ? '&' : '?';
809         $mine = get_my_url();
810         if($mine and ! link_compare($mine,$s))
811                 return $s . $achar . 'zrl=' . urlencode($mine);
812         return $s;
813 }
814
815 /**
816  * @brief Get the user ID of the page owner
817  *
818  * Used from within PCSS themes to set theme parameters. If there's a
819  * puid request variable, that is the "page owner" and normally their theme
820  * settings take precedence; unless a local user sets the "always_my_theme"
821  * system pconfig, which means they don't want to see anybody else's theme
822  * settings except their own while on this site.
823  *
824  * @return int user ID
825  * 
826  * @note Returns local_user instead of user ID if "always_my_theme"
827  *      is set to true
828  */
829 function get_theme_uid() {
830         $uid = (($_REQUEST['puid']) ? intval($_REQUEST['puid']) : 0);
831         if(local_user()) {
832                 if((get_pconfig(local_user(),'system','always_my_theme')) || (! $uid))
833                         return local_user();
834         }
835
836         return $uid;
837 }