]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
lrdd link on profile page (for salmon m/e), remove dfrn-template, qualify some contac...
[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         if(! $update) {
122                 if(x($_GET,'tab'))
123                         $tab = notags(trim($_GET['tab']));
124
125                 $tpl = load_view_file('view/profile_tabs.tpl');
126
127                 $o .= replace_macros($tpl,array(
128                         '$url' => $a->get_baseurl() . '/' . $a->cmd,
129                         '$phototab' => $a->get_baseurl() . '/photos/' . $a->profile['nickname']
130                 ));
131
132
133                 if($tab === 'profile') {
134                         $lang = get_config('system','language');
135                         if($lang && file_exists("view/$lang/profile_advanced.php"))
136                                 require_once("view/$lang/profile_advanced.php");
137                         else
138                                 require_once('view/profile_advanced.php');
139                         return $o;
140                 }
141
142                 $celeb = ((($a->profile['page-flags'] == PAGE_SOAPBOX) || ($a->profile['page-flags'] == PAGE_COMMUNITY)) ? true : false);
143                 if(can_write_wall($a,$a->profile['profile_uid'])) {
144                         $tpl = load_view_file('view/jot-header.tpl');
145         
146                         $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
147                         require_once('view/acl_selectors.php');
148
149                         $tpl = load_view_file("view/jot.tpl");
150                         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']))))
151                                 $lockstate = 'lock';
152                         else
153                                 $lockstate = 'unlock';
154                         $o .= replace_macros($tpl,array(
155                                 '$baseurl' => $a->get_baseurl(),
156                                 '$defloc' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? $a->user['default-location'] : ''),
157                                 '$return_path' => $a->cmd,
158                                 '$visitor' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? 'block' : 'none'),
159                                 '$lockstate' => $lockstate,
160                                 '$bang' => '',
161                                 '$acl' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? populate_acl($a->user, $celeb) : ''),
162                                 '$profile_uid' => $a->profile['profile_uid']
163                         ));
164                 }
165
166                 // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
167                 // because browser prefetching might change it on us. We have to deliver it with the page.
168
169                 if($tab === 'posts' && (! $a->pager['start'])) {
170                         $o .= '<div id="live-profile"></div>' . "\r\n";
171                         $o .= "<script> var profile_uid = " . $a->profile['profile_uid'] . "; </script>\r\n";
172                 }
173
174         }
175
176         // TODO alter registration and settings and profile to update contact table when names and  photos change.  
177
178         // default permissions - anonymous user
179
180         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
181
182
183
184
185         // Profile owner - everything is visible
186
187         if(local_user() && ($_SESSION['uid'] == $a->profile['profile_uid'])) {
188                 $sql_extra = ''; 
189                 
190                 // Oh - while we're here... reset the Unseen messages
191
192                 $r = q("UPDATE `item` SET `unseen` = 0 
193                         WHERE `type` != 'remote' AND `unseen` = 1 AND `uid` = %d",
194                         intval($_SESSION['uid'])
195                 );
196
197         }
198
199         // authenticated visitor - here lie dragons
200         // If $remotecontact is true, we know that not only is this a remotely authenticated
201         // person, but that it is *our* contact, which is important in multi-user mode.
202
203         elseif($remote_contact) {
204                 $gs = '<<>>'; // should be impossible to match
205                 if(count($groups)) {
206                         foreach($groups as $g)
207                                 $gs .= '|<' . intval($g) . '>';
208                 } 
209                 $sql_extra = sprintf(
210                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
211                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
212                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
213                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
214
215                         intval($_SESSION['visitor_id']),
216                         intval($_SESSION['visitor_id']),
217                         dbesc($gs),
218                         dbesc($gs)
219                 );
220         }
221
222         $r = q("SELECT COUNT(*) AS `total`
223                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
224                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
225                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
226                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
227                 $sql_extra ",
228                 intval($a->profile['profile_uid'])
229
230         );
231
232         if(count($r))
233                 $a->set_pager_total($r[0]['total']);
234
235         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
236                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, `contact`.`rel`, 
237                 `contact`.`thumb`, `contact`.`self`, 
238                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
239                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
240                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
241                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
242                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `wall` = 1 )
243                 $sql_extra
244                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC LIMIT %d ,%d ",
245                 intval($a->profile['profile_uid']),
246                 intval($a->pager['start']),
247                 intval($a->pager['itemspage'])
248
249         );
250
251
252         $cmnt_tpl = load_view_file('view/comment_item.tpl');
253
254         $like_tpl = load_view_file('view/like.tpl');
255
256         $tpl = load_view_file('view/wall_item.tpl');
257
258         $droptpl = load_view_file('view/wall_item_drop.tpl');
259         $fakedrop = load_view_file('view/wall_fake_drop.tpl');
260
261         if($update)
262                 $return_url = $_SESSION['return_url'];
263         else
264                 $return_url = $_SESSION['return_url'] = $a->cmd;
265
266         $alike = array();
267         $dlike = array();
268
269         if(count($r)) {
270
271                 foreach($r as $item) {
272
273                         $sparkle = '';
274                         
275                         if(($item['verb'] == ACTIVITY_LIKE) && ($item['id'] != $item['parent'])) {
276                                 $url = $item['url'];
277                                 if(($item['network'] === 'dfrn') && (! $item['self'])) {
278                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
279                                         $sparkle = ' class="sparkle" ';
280                                 }
281                                 if(! is_array($alike[$item['parent'] . '-l']))
282                                         $alike[$item['parent'] . '-l'] = array();
283                                 $alike[$item['parent']] ++;
284                                 $alike[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
285                         }
286                         if(($item['verb'] == ACTIVITY_DISLIKE) && ($item['id'] != $item['parent'])) {
287                                 $url = $item['url'];
288                                 if(($item['network'] === 'dfrn') && (! $item['self'])) { 
289                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
290                                         $sparkle = ' class="sparkle" ';
291                                 }
292                                 if(! is_array($dlike[$item['parent'] . '-l']))
293                                         $dlike[$item['parent'] . '-l'] = array();
294                                 $dlike[$item['parent']] ++;
295                                 $dlike[$item['parent'] . '-l'][] = '<a href="'. $url . '"'. $sparkle .'>' . $item['name'] . '</a>';
296                         }
297                 }
298
299                 foreach($r as $item) {
300
301                         $sparkle = '';          
302                         $comment = '';
303                         $likebuttons = '';
304
305                         $template = $tpl;
306                         
307                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
308
309                         if((($item['verb'] == ACTIVITY_LIKE) || ($item['verb'] == ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) 
310                                 continue;
311
312                         $lock = (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
313                                 || strlen($item['deny_cid']) || strlen($item['deny_gid']))
314                                 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
315                                 : '<div class="wall-item-lock"></div>');
316
317                         if(can_write_wall($a,$a->profile['profile_uid'])) {
318                                 if($item['id'] == $item['parent']) {
319                                         $likebuttons = replace_macros($like_tpl,array('$id' => $item['id']));
320                                 }
321                                 if($item['last-child']) {
322                                         $comment = replace_macros($cmnt_tpl,array(
323                                                 '$return_path' => $_SESSION['return_url'],
324                                                 '$type' => 'wall-comment',
325                                                 '$id' => $item['item_id'],
326                                                 '$parent' => $item['parent'],
327                                                 '$profile_uid' =>  $a->profile['profile_uid'],
328                                                 '$mylink' => $contact['url'],
329                                                 '$mytitle' => t('This is you'),
330                                                 '$myphoto' => $contact['thumb'],
331                                                 '$ww' => ''
332                                         ));
333                                 }
334                         }
335
336
337                         $profile_url = $item['url'];
338
339                         // 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,
340                         // I can go directly to their profile as an authenticated guest.
341
342                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) 
343                                 && ($item['network'] === 'dfrn') && (! $item['self'] )) {
344                                 $profile_url = $redirect_url;
345                                 $sparkle = ' sparkle';
346                         }
347                         else
348                                 $sparkle = '';
349
350                         // We received this post via a remote feed. It's either a wall-to-wall or a remote comment. The author is
351                         // known to us and is reflected in the contact-id for this item. We can use the contact url or redirect rather than 
352                         // use the link in the feed. This is different than on the network page where we may not know the author.
353  
354                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
355                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
356                         $profile_link = $profile_url;
357
358                         $drop = '';
359                         $dropping = false;
360
361                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
362                                 $dropping = true;
363
364                         $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id']));
365
366
367                         $like    = (($alike[$item['id']]) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
368                         $dislike = (($dlike[$item['id']]) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
369
370
371                         $o .= replace_macros($template,array(
372                                 '$id' => $item['item_id'],
373                                 '$profile_url' => $profile_link,
374                                 '$name' => $profile_name,
375                                 '$thumb' => $profile_avatar,
376                                 '$sparkle' => $sparkle,
377                                 '$title' => $item['title'],
378                                 '$body' => bbcode($item['body']),
379                                 '$ago' => relative_date($item['created']),
380                                 '$lock' => $lock,
381                                 '$location' => (($item['location']) ? '<a target="map" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : ''),
382                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
383                                 '$drop' => $drop,
384                                 '$like' => $like,
385                                 '$vote' => $likebuttons,
386                                 '$dislike' => $dislike,
387                                 '$comment' => $comment
388                         ));
389                         
390                 }
391         }
392
393         if($update) {
394                 return $o;
395         }
396                 
397         $o .= paginate($a);
398
399         return $o;
400 }