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