]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
pass vars via javascript, not html. Also fix broken pause img on IE
[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'] .= '<link rel="alternate" type="application/atom+xml" href="' . $a->get_baseurl() . '/dfrn_poll/' . $which .'" />' . "\r\n" ;
65
66         $a->page['htmlhead'] .= '<meta name="dfrn-template" content="' . $a->get_baseurl() . "/profile/%s" . '" />' . "\r\n" ;
67         $a->page['htmlhead'] .= '<meta name="dfrn-global-visibility" content="' . (($a->profile['net-publish']) ? 'true' : 'false') . '" />' . "\r\n" ;
68   
69         
70         $dfrn_pages = array('request', 'confirm', 'notify', 'poll');
71         foreach($dfrn_pages as $dfrn)
72                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"".$a->get_baseurl()."/dfrn_{$dfrn}/{$which}\" />\r\n";
73
74 }
75
76
77 function profile_content(&$a, $update = 0) {
78
79
80         file_put_contents('uid.log',"{$_SERVER['QUERY_STRING']} ". session_id() . "\n", FILE_APPEND);
81
82
83         require_once("include/bbcode.php");
84         require_once('include/security.php');
85
86         $groups = array();
87
88         $tab = 'posts';
89
90
91         if($update) {
92                 // Ensure we've got a profile owner if updating.
93                 $a->profile['profile_uid'] = $update;
94         }
95         else {
96                 if($a->profile['profile_uid'] == get_uid())             
97                         $o .= '<script> $(document).ready(function() { $(\'#nav-home-link\').addClass(\'nav-selected\'); });</script>';
98         }
99
100         $contact = null;
101         $remote_contact = false;
102
103         if(remote_user()) {
104                 $contact_id = $_SESSION['visitor_id'];
105                 $groups = init_groups_visitor($contact_id);
106                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
107                         intval($contact_id),
108                         intval($a->profile['profile_uid'])
109                 );
110                 if(count($r)) {
111                         $contact = $r[0];
112                         $remote_contact = true;
113                 }
114         }
115
116         if(! $remote_contact) {
117                 if(local_user()) {
118                         $contact_id = $_SESSION['cid'];
119                         $contact = $a->contact;
120                 }
121         }
122
123         if(! $update) {
124                 if(x($_GET,'tab'))
125                         $tab = notags(trim($_GET['tab']));
126
127                 $tpl = file_get_contents('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                         require_once('view/profile_advanced.php');
137                         return $o;
138                 }
139
140                 if(can_write_wall($a,$a->profile['profile_uid'])) {
141                         $tpl = file_get_contents('view/jot-header.tpl');
142         
143                         $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
144                         require_once('view/acl_selectors.php');
145
146                         $tpl = file_get_contents("view/jot.tpl");
147                         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']))))
148                                 $lockstate = 'lock';
149                         else
150                                 $lockstate = 'unlock';
151                         $o .= replace_macros($tpl,array(
152                                 '$baseurl' => $a->get_baseurl(),
153                                 '$defloc' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? $a->user['default-location'] : ''),
154                                 '$return_path' => $a->cmd,
155                                 '$visitor' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? 'block' : 'none'),
156                                 '$lockstate' => $lockstate,
157                                 '$bang' => '',
158                                 '$acl' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? populate_acl($a->user) : ''),
159                                 '$profile_uid' => $a->profile['profile_uid']
160                         ));
161                 }
162
163                 // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
164                 // because browser prefetching might change it on us. We have to deliver it with the page.
165
166                 if($tab == 'posts' && (! $a->pager['start'])) {
167                         $o .= '<div id="live-profile"></div>' . "\r\n";
168                         $o .= "<script> var profile_uid = " . $a->profile['profile_uid'] . "; </script>\r\n";
169                 }
170
171         }
172
173         // TODO alter registration and settings and profile to update contact table when names and  photos change.  
174
175         // default permissions - anonymous user
176
177         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
178
179
180
181
182         // Profile owner - everything is visible
183
184         if(local_user() && ($_SESSION['uid'] == $a->profile['profile_uid'])) {
185                 $sql_extra = ''; 
186                 
187                 // Oh - while we're here... reset the Unseen messages
188
189                 $r = q("UPDATE `item` SET `unseen` = 0 
190                         WHERE `type` != 'remote' AND `unseen` = 1 AND `uid` = %d",
191                         intval($_SESSION['uid'])
192                 );
193
194         }
195
196         // authenticated visitor - here lie dragons
197         // If $remotecontact is true, we know that not only is this a remotely authenticated
198         // person, but that it is *our* contact, which is important in multi-user mode.
199
200         elseif($remote_contact) {
201                 $gs = '<<>>'; // should be impossible to match
202                 if(count($groups)) {
203                         foreach($groups as $g)
204                                 $gs .= '|<' . intval($g) . '>';
205                 } 
206                 $sql_extra = sprintf(
207                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
208                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
209                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
210                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
211
212                         intval($_SESSION['visitor_id']),
213                         intval($_SESSION['visitor_id']),
214                         dbesc($gs),
215                         dbesc($gs)
216                 );
217         }
218
219         $r = q("SELECT COUNT(*) AS `total`
220                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
221                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
222                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
223                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
224                 $sql_extra ",
225                 intval($a->profile['profile_uid'])
226
227         );
228
229         if(count($r))
230                 $a->set_pager_total($r[0]['total']);
231
232         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
233                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
234                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
235                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
236                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
237                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
238                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
239                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
240                 $sql_extra
241                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC LIMIT %d ,%d ",
242                 intval($a->profile['profile_uid']),
243                 intval($a->pager['start']),
244                 intval($a->pager['itemspage'])
245
246         );
247
248
249         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
250
251         $like_tpl = file_get_contents('view/like.tpl');
252
253         $tpl = file_get_contents('view/wall_item.tpl');
254
255         $droptpl = file_get_contents('view/wall_item_drop.tpl');
256         $fakedrop = file_get_contents('view/wall_fake_drop.tpl');
257
258         if($update)
259                 $return_url = $_SESSION['return_url'];
260         else
261                 $return_url = $_SESSION['return_url'] = $a->cmd;
262
263         $alike = array();
264         $dlike = array();
265
266         if(count($r)) {
267
268                 foreach($r as $item) {
269
270                         if(($item['verb'] == ACTIVITY_LIKE) && ($item['id'] != $item['parent'])) {
271                                 $url = $item['url'];
272                                 if(($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'])) 
273                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
274                                 if(! is_array($alike[$item['parent'] . '-l']))
275                                         $alike[$item['parent'] . '-l'] = array();
276                                 $alike[$item['parent']] ++;
277                                 $alike[$item['parent'] . '-l'][] = '<a href="'. $url . '">' . $item['name'] . '</a>';
278                         }
279                         if(($item['verb'] == ACTIVITY_DISLIKE) && ($item['id'] != $item['parent'])) {
280                                 $url = $item['url'];
281                                 if(($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'])) 
282                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
283                                 if(! is_array($dlike[$item['parent'] . '-l']))
284                                         $dlike[$item['parent'] . '-l'] = array();
285                                 $dlike[$item['parent']] ++;
286                                 $dlike[$item['parent'] . '-l'][] = '<a href="'. $url . '">' . $item['name'] . '</a>';
287                         }
288                 }
289
290                 foreach($r as $item) {
291
292                 
293                         $comment = '';
294                         $likebuttons = '';
295
296                         $template = $tpl;
297                         
298                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
299
300                         if((($item['verb'] == ACTIVITY_LIKE) || ($item['verb'] == ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) 
301                                 continue;
302
303                         if(can_write_wall($a,$a->profile['profile_uid'])) {
304                                 if($item['id'] == $item['parent']) {
305                                         $likebuttons = replace_macros($like_tpl,array('$id' => $item['id']));
306                                 }
307                                 if($item['last-child']) {
308                                         $comment = replace_macros($cmnt_tpl,array(
309                                                 '$return_path' => $_SESSION['return_url'],
310                                                 '$type' => 'wall-comment',
311                                                 '$id' => $item['item_id'],
312                                                 '$parent' => $item['parent'],
313                                                 '$profile_uid' =>  $a->profile['profile_uid'],
314                                                 '$mylink' => $contact['url'],
315                                                 '$mytitle' => t('Me'),
316                                                 '$myphoto' => $contact['thumb'],
317                                                 '$ww' => ''
318                                         ));
319                                 }
320                         }
321
322
323                         $profile_url = $item['url'];
324
325                         // 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,
326                         // I can go directly to their profile as an authenticated guest.
327
328                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) 
329                                 && ($item['rel'] == DIRECTION_IN || $item['rel'] == DIRECTION_BOTH) && (! $item['self'] ))
330                                 $profile_url = $redirect_url;
331         
332
333                         // We received this post via a remote feed. It's either a wall-to-wall or a remote comment. The author is
334                         // known to us and is reflected in the contact-id for this item. We can use the contact url or redirect rather than 
335                         // use the link in the feed. This is different than on the network page where we may not know the author.
336  
337                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
338                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
339                         $profile_link = $profile_url;
340
341                         $drop = '';
342                         $dropping = false;
343
344                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
345                                 $dropping = true;
346
347                         $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id']));
348
349
350                         $like    = (($alike[$item['id']]) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
351                         $dislike = (($dlike[$item['id']]) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
352
353
354                         $o .= replace_macros($template,array(
355                                 '$id' => $item['item_id'],
356                                 '$profile_url' => $profile_link,
357                                 '$name' => $profile_name,
358                                 '$thumb' => $profile_avatar,
359                                 '$title' => $item['title'],
360                                 '$body' => bbcode($item['body']),
361                                 '$ago' => relative_date($item['created']),
362                                 '$location' => (($item['location']) ? '<a target="map" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : ''),
363                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
364                                 '$drop' => $drop,
365                                 '$like' => $like,
366                                 '$vote' => $likebuttons,
367                                 '$dislike' => $dislike,
368                                 '$comment' => $comment
369                         ));
370                         
371                 }
372         }
373
374         if($update) {
375                 return $o;
376         }
377                 
378         $o .= paginate($a);
379
380         return $o;
381 }