]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
ed26237b6861d508fdcc4226174827c872fb0c20
[friendica.git] / mod / profile.php
1 <?php
2
3 if(! function_exists('profile_load')) {
4 function profile_load(&$a, $username, $profile = 0) {
5
6         if(remote_user()) {
7                 $r = q("SELECT `profile-id` FROM `contact` WHERE `id` = %d LIMIT 1",
8                         intval($_SESSION['visitor_id']));
9                 if(count($r))
10                         $profile = $r[0]['profile-id'];
11         } 
12
13         $r = null;
14
15         if($profile) {
16                 $profile_int = intval($profile);
17                 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` 
18                         LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
19                         WHERE `user`.`nickname` = '%s' AND `profile`.`id` = %d LIMIT 1",
20                         dbesc($username),
21                         intval($profile_int)
22                 );
23         }
24         if(! count($r)) {       
25                 $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` 
26                         LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
27                         WHERE `user`.`nickname` = '%s' AND `profile`.`is-default` = 1 LIMIT 1",
28                         dbesc($username)
29                 );
30         }
31
32         if(($r === false) || (! count($r))) {
33                 notice( t('No profile') . EOL );
34                 $a->error = 404;
35                 return;
36         }
37
38         $a->profile = $r[0];
39
40         $a->page['template'] = 'profile';
41
42         $a->page['title'] = $a->profile['name'];
43         $_SESSION['theme'] = $a->profile['theme'];
44
45         return;
46 }}
47
48 function profile_init(&$a) {
49
50         if($a->argc > 1)
51                 $which = $a->argv[1];
52         else {
53                 notice( t('No profile') . EOL );
54                 $a->error = 404;
55                 return;
56         }
57
58         $profile = 0;
59         if((local_user()) && ($a->argc > 2) && ($a->argv[2] === 'view')) {
60                 $which = $a->user['nickname'];
61                 $profile = $a->argv[1];         
62         }
63         profile_load($a,$which,$profile);
64         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . (($a->profile['net-publish']) ? 'true' : 'false') . '" />' . "\r\n" ;
65         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $a->get_baseurl() . '/dfrn_poll/' . $which .'" />' . "\r\n" ;
66                 $uri = urlencode('acct:' . $a->user['nickname'] . '@' . $a->get_hostname() . (($a->path) ? '/' . $a->path : ''));
67                 $a->page['htmlhead'] .= '<link rel="lrdd" type="application/xrd+xml" href="' . $a->get_baseurl() . '/xrd/?uri=' . $uri . '" />' . "\r\n";
68
69   
70         
71         $dfrn_pages = array('request', 'confirm', 'notify', 'poll');
72         foreach($dfrn_pages as $dfrn)
73                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"".$a->get_baseurl()."/dfrn_{$dfrn}/{$which}\" />\r\n";
74
75 }
76
77
78 function profile_content(&$a, $update = 0) {
79
80
81         require_once("include/bbcode.php");
82         require_once('include/security.php');
83
84         $groups = array();
85
86         $tab = 'posts';
87
88
89         if($update) {
90                 // Ensure we've got a profile owner if updating.
91                 $a->profile['profile_uid'] = $update;
92         }
93         else {
94                 if($a->profile['profile_uid'] == local_user())          
95                         $o .= '<script> $(document).ready(function() { $(\'#nav-home-link\').addClass(\'nav-selected\'); });</script>';
96         }
97
98         $contact = null;
99         $remote_contact = false;
100
101         if(remote_user()) {
102                 $contact_id = $_SESSION['visitor_id'];
103                 $groups = init_groups_visitor($contact_id);
104                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
105                         intval($contact_id),
106                         intval($a->profile['profile_uid'])
107                 );
108                 if(count($r)) {
109                         $contact = $r[0];
110                         $remote_contact = true;
111                 }
112         }
113
114         if(! $remote_contact) {
115                 if(local_user()) {
116                         $contact_id = $_SESSION['cid'];
117                         $contact = $a->contact;
118                 }
119         }
120
121         $is_owner = ((local_user()) && (local_user() == $a->profile['profile_uid']) ? true : false);
122
123         if(! $update) {
124                 if(x($_GET,'tab'))
125                         $tab = notags(trim($_GET['tab']));
126
127                 $tpl = load_view_file('view/profile_tabs.tpl');
128
129                 $o .= replace_macros($tpl,array(
130                         '$url' => $a->get_baseurl() . '/' . $a->cmd,
131                         '$phototab' => $a->get_baseurl() . '/photos/' . $a->profile['nickname']
132                 ));
133
134
135                 if($tab === 'profile') {
136                         $lang = get_config('system','language');
137                         if($lang && file_exists("view/$lang/profile_advanced.php"))
138                                 require_once("view/$lang/profile_advanced.php");
139                         else
140                                 require_once('view/profile_advanced.php');
141                         return $o;
142                 }
143
144                 $celeb = ((($a->profile['page-flags'] == PAGE_SOAPBOX) || ($a->profile['page-flags'] == PAGE_COMMUNITY)) ? true : false);
145                 if(can_write_wall($a,$a->profile['profile_uid'])) {
146
147
148                         $geotag = (($is_owner && $a->profile['allow_location']) ? load_view_file('view/jot_geotag.tpl') : '');
149
150                         $tpl = load_view_file('view/jot-header.tpl');
151         
152                         $a->page['htmlhead'] .= replace_macros($tpl, array(
153                                 '$baseurl' => $a->get_baseurl(),
154                                 '$geotag'  => $geotag
155                         ));
156
157                         require_once('view/acl_selectors.php');
158
159                         $tpl = load_view_file("view/jot.tpl");
160                         if(is_array($a->user) && ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) || (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))
161                                 $lockstate = 'lock';
162                         else
163                                 $lockstate = 'unlock';
164                         $o .= replace_macros($tpl,array(
165                                 '$baseurl' => $a->get_baseurl(),
166                                 '$defloc' => (($is_owner) ? $a->user['default-location'] : ''),
167                                 '$return_path' => $a->cmd,
168                                 '$visitor' => (($is_owner) ? 'block' : 'none'),
169                                 '$lockstate' => $lockstate,
170                                 '$bang' => '',
171                                 '$acl' => (($is_owner) ? populate_acl($a->user, $celeb) : ''),
172                                 '$profile_uid' => $a->profile['profile_uid']
173                         ));
174                 }
175
176                 // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
177                 // because browser prefetching might change it on us. We have to deliver it with the page.
178
179                 if($tab === 'posts' && (! $a->pager['start'])) {
180                         $o .= '<div id="live-profile"></div>' . "\r\n";
181                         $o .= "<script> var profile_uid = " . $a->profile['profile_uid'] . "; </script>\r\n";
182                 }
183
184         }
185
186         // Construct permissions
187
188         // default permissions - anonymous user
189
190         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
191
192         // Profile owner - everything is visible
193
194         if(is_owner) {
195                 $sql_extra = ''; 
196                 
197                 // Oh - while we're here... reset the Unseen messages
198
199                 $r = q("UPDATE `item` SET `unseen` = 0 
200                         WHERE `type` != 'remote' AND `unseen` = 1 AND `uid` = %d",
201                         intval($_SESSION['uid'])
202                 );
203
204         }
205
206         // authenticated visitor - here lie dragons
207         // If $remotecontact is true, we know that not only is this a remotely authenticated
208         // person, but that it is *our* contact, which is important in multi-user mode.
209
210         elseif($remote_contact) {
211                 $gs = '<<>>'; // should be impossible to match
212                 if(count($groups)) {
213                         foreach($groups as $g)
214                                 $gs .= '|<' . intval($g) . '>';
215                 } 
216                 $sql_extra = sprintf(
217                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
218                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
219                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
220                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
221
222                         intval($_SESSION['visitor_id']),
223                         intval($_SESSION['visitor_id']),
224                         dbesc($gs),
225                         dbesc($gs)
226                 );
227         }
228
229         $r = q("SELECT COUNT(*) AS `total`
230                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
231                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
232                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
233                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
234                 $sql_extra ",
235                 intval($a->profile['profile_uid'])
236
237         );
238
239         if(count($r))
240                 $a->set_pager_total($r[0]['total']);
241
242         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
243                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, `contact`.`rel`, 
244                 `contact`.`thumb`, `contact`.`self`, 
245                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
246                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
247                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
248                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
249                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
250                 $sql_extra
251                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC LIMIT %d ,%d ",
252                 intval($a->profile['profile_uid']),
253                 intval($a->pager['start']),
254                 intval($a->pager['itemspage'])
255
256         );
257
258
259         $cmnt_tpl = load_view_file('view/comment_item.tpl');
260
261         $like_tpl = load_view_file('view/like.tpl');
262
263         $tpl = load_view_file('view/wall_item.tpl');
264
265         $droptpl = load_view_file('view/wall_item_drop.tpl');
266         $fakedrop = load_view_file('view/wall_fake_drop.tpl');
267
268         if($update)
269                 $return_url = $_SESSION['return_url'];
270         else
271                 $return_url = $_SESSION['return_url'] = $a->cmd;
272
273         $alike = array();
274         $dlike = array();
275
276         if(count($r)) {
277
278                 foreach($r as $item) {
279
280                         $sparkle = '';
281                         
282                         if(($item['verb'] == ACTIVITY_LIKE) && ($item['id'] != $item['parent'])) {
283                                 $url = $item['url'];
284                                 if(($item['network'] === 'dfrn') && (! $item['self'])) {
285                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
286                                         $sparkle = ' class="sparkle" ';
287                                 }
288                                 if(! is_array($alike[$item['parent'] . '-l']))
289                                         $alike[$item['parent'] . '-l'] = array();
290                                 $alike[$item['parent']] ++;
291                                 $alike[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
292                         }
293                         if(($item['verb'] == ACTIVITY_DISLIKE) && ($item['id'] != $item['parent'])) {
294                                 $url = $item['url'];
295                                 if(($item['network'] === 'dfrn') && (! $item['self'])) { 
296                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
297                                         $sparkle = ' class="sparkle" ';
298                                 }
299                                 if(! is_array($dlike[$item['parent'] . '-l']))
300                                         $dlike[$item['parent'] . '-l'] = array();
301                                 $dlike[$item['parent']] ++;
302                                 $dlike[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
303                         }
304                 }
305
306                 foreach($r as $item) {
307
308                         $sparkle = '';          
309                         $comment = '';
310                         $likebuttons = '';
311
312                         $template = $tpl;
313                         
314                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
315
316                         if((($item['verb'] == ACTIVITY_LIKE) || ($item['verb'] == ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) 
317                                 continue;
318
319                         $lock = (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
320                                 || strlen($item['deny_cid']) || strlen($item['deny_gid']))
321                                 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
322                                 : '<div class="wall-item-lock"></div>');
323
324                         if(can_write_wall($a,$a->profile['profile_uid'])) {
325                                 if($item['id'] == $item['parent']) {
326                                         $likebuttons = replace_macros($like_tpl,array('$id' => $item['id']));
327                                 }
328                                 if($item['last-child']) {
329                                         $comment = replace_macros($cmnt_tpl,array(
330                                                 '$return_path' => $_SESSION['return_url'],
331                                                 '$type' => 'wall-comment',
332                                                 '$id' => $item['item_id'],
333                                                 '$parent' => $item['parent'],
334                                                 '$profile_uid' =>  $a->profile['profile_uid'],
335                                                 '$mylink' => $contact['url'],
336                                                 '$mytitle' => t('This is you'),
337                                                 '$myphoto' => $contact['thumb'],
338                                                 '$ww' => ''
339                                         ));
340                                 }
341                         }
342
343
344                         $profile_url = $item['url'];
345
346                         // This is my profile but I'm not the author of this post/comment. If it's somebody that's a fan or mutual friend,
347                         // I can go directly to their profile as an authenticated guest.
348
349                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) 
350                                 && ($item['network'] === 'dfrn') && (! $item['self'] )) {
351                                 $profile_url = $redirect_url;
352                                 $sparkle = ' sparkle';
353                         }
354                         else
355                                 $sparkle = '';
356
357                         // We received this post via a remote feed. It's either a wall-to-wall or a remote comment. The author is
358                         // known to us and is reflected in the contact-id for this item. We can use the contact url or redirect rather than 
359                         // use the link in the feed. This is different than on the network page where we may not know the author.
360  
361                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
362                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
363                         $profile_link = $profile_url;
364
365                         $drop = '';
366                         $dropping = false;
367
368                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
369                                 $dropping = true;
370
371                         $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id']));
372
373
374                         $like    = (($alike[$item['id']]) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
375                         $dislike = (($dlike[$item['id']]) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
376                         $location = (($item['location']) ? '<a target="map" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
377                         $coord = (($item['coord']) ? '<a target="map" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
378                         if($coord) {
379                                 if($location)
380                                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
381                                 else
382                                         $location = '<span class="smalltext">' . $coord . '</span>';
383                         }
384
385                         $o .= replace_macros($template,array(
386                                 '$id' => $item['item_id'],
387                                 '$profile_url' => $profile_link,
388                                 '$name' => $profile_name,
389                                 '$thumb' => $profile_avatar,
390                                 '$sparkle' => $sparkle,
391                                 '$title' => $item['title'],
392                                 '$body' => bbcode($item['body']),
393                                 '$ago' => relative_date($item['created']),
394                                 '$lock' => $lock,
395                                 '$location' => $location, 
396                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
397                                 '$drop' => $drop,
398                                 '$like' => $like,
399                                 '$vote' => $likebuttons,
400                                 '$dislike' => $dislike,
401                                 '$comment' => $comment
402                         ));
403                         
404                 }
405         }
406
407         if($update) {
408                 return $o;
409         }
410                 
411         $o .= paginate($a);
412
413         return $o;
414 }