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