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