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