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