]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
fixup of some nits with live update and remote comment display on profile page
[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         if($profile) {
14                 $profile_int = intval($profile);
15                 $sql_which = " AND `profile`.`id` = $profile_int ";
16         }
17         else
18                 $sql_which = " AND `profile`.`is-default` = 1 "; 
19
20         $r = q("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `user`.* FROM `profile` 
21                 LEFT JOIN `user` ON `profile`.`uid` = `user`.`uid`
22                 WHERE `user`.`nickname` = '%s' $sql_which LIMIT 1",
23                 dbesc($username)
24         );
25
26         if(($r === false) || (! count($r))) {
27                 notice("No profile" . EOL );
28                 $a->error = 404;
29                 return;
30         }
31
32         $a->profile = $r[0];
33
34         $a->page['template'] = 'profile';
35
36         $a->page['title'] = $a->profile['name'];
37
38         return;
39 }}
40
41 function profile_init(&$a) {
42
43         if($a->argc > 1)
44                 $which = $a->argv[1];
45         else {
46                 notice("No profile" . EOL );
47                 $a->error = 404;
48                 return;
49         }
50
51         if((remote_user()) && ($a->argc > 2) && ($a->argv[2] == 'visit'))
52                 $_SESSION['is_visitor'] = 1;
53         else {
54                 unset($_SESSION['is_visitor']);
55                 unset($_SESSION['visitor_id']);
56                 if(! $_SESSION['uid'])
57                         unset($_SESSION['authenticated']);
58         }
59
60         profile_load($a,$which);
61         $a->page['htmlhead'] .= "<meta name=\"dfrn-template\" content=\"" . $a->get_baseurl() . "/profile/%s" . "\" />\r\n";
62         
63         $dfrn_pages = array('request', 'confirm', 'notify', 'poll');
64         foreach($dfrn_pages as $dfrn)
65                 $a->page['htmlhead'] .= "<link rel=\"dfrn-{$dfrn}\" href=\"".$a->get_baseurl()."/dfrn_{$dfrn}/{$which}\" />\r\n";
66
67 }
68
69
70 function profile_content(&$a, $update = false) {
71
72         require_once("include/bbcode.php");
73         require_once('include/security.php');
74
75         $groups = array();
76
77         $tab = 'posts';
78
79
80
81
82
83         if(remote_user()) {
84                 $contact_id = $_SESSION['visitor_id'];
85                 $groups = init_groups_visitor($contact_id);
86         }
87         if(local_user()) {
88                 $contact_id = $_SESSION['cid'];
89         }
90
91         if($update) {
92                 // Ensure we've got a profile owner if updating.
93                 if(remote_user()) {
94                         $r = q("SELECT `uid` FROM `contact` WHERE `id` = %d LIMIT 1",
95                                 intval($_SESSION['visitor_id'])
96                         );
97                         if(count($r))
98                                 $a->profile['uid'] = $r[0]['uid'];
99                         else
100                                 killme();
101                 }
102                 elseif(local_user()) {
103                         $a->profile['uid'] = $_SESSION['uid'];
104                 }
105                 else {
106                         killme();
107                         return; // NOTREACHED
108                 }
109         }
110
111         else {
112                 if(x($_GET,'tab'))
113                         $tab = notags(trim($_GET['tab']));
114
115                 $tpl = file_get_contents('view/profile_tabs.tpl');
116
117                 $o .= replace_macros($tpl,array(
118                         '$url' => $a->get_baseurl() . '/' . $a->cmd
119                 ));
120
121
122                 if($tab == 'profile') {
123                         require_once('view/profile_advanced.php');
124                         return $o;
125                 }
126
127                 if(can_write_wall($a,$a->profile['profile_uid'])) {
128                         $tpl = file_get_contents('view/jot-header.tpl');
129         
130                         $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
131                         require_once('view/acl_selectors.php');
132
133                         $tpl = file_get_contents("view/jot.tpl");
134
135                         $o .= replace_macros($tpl,array(
136                                 '$baseurl' => $a->get_baseurl(),
137                                 '$return_path' => $a->cmd,
138                                 '$visitor' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? 'block' : 'none'),
139                                 '$lockstate' => 'unlock',
140                                 '$acl' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? populate_acl() : ''),
141                                 '$profile_uid' => $a->profile['profile_uid']
142                         ));
143                 }
144
145                 if($tab == 'posts' && (! $a->pager['start']))
146                         $o .= '<div id="live-profile"></div>' . "\r\n";
147         }
148
149         // TODO alter registration and settings and profile to update contact table when names and  photos change.  
150
151         // default permissions - anonymous user
152
153         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
154
155         // Profile owner - everything is visible
156
157         if(local_user() && ($_SESSION['uid'] == $a->profile['uid'])) {
158                 $sql_extra = ''; 
159                 
160                 // Oh - while we're here... reset the Unseen messages
161
162                 $r = q("UPDATE `item` SET `unseen` = 0 
163                         WHERE `type` != 'remote' AND `unseen` = 1 AND `uid` = %d",
164                         intval($_SESSION['uid'])
165                 );
166
167         }
168
169         // authenticated visitor - here lie dragons
170         elseif(remote_user()) {
171                 $gs = '<<>>'; // should be impossible to match
172                 if(count($groups)) {
173                         foreach($groups as $g)
174                                 $gs .= '|<' . intval($g) . '>';
175                 } 
176                 $sql_extra = sprintf(
177                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
178                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
179                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
180                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
181
182                         intval($_SESSION['visitor_id']),
183                         intval($_SESSION['visitor_id']),
184                         dbesc($gs),
185                         dbesc($gs)
186                 );
187         }
188
189         $r = q("SELECT COUNT(*) AS `total`
190                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
191                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
192                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
193                 $sql_extra ",
194                 intval($a->profile['uid'])
195
196         );
197
198         if(count($r))
199                 $a->set_pager_total($r[0]['total']);
200
201
202         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
203                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
204                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
205                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
206                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
207                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
208                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
209                 $sql_extra
210                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
211                 intval($a->profile['uid']),
212                 intval($a->pager['start']),
213                 intval($a->pager['itemspage'])
214
215         );
216
217
218         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
219
220         $tpl = file_get_contents('view/wall_item.tpl');
221
222
223         if(count($r)) {
224                 foreach($r as $item) {
225                         $comment = '';
226                         $template = $tpl;
227                         
228                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
229                         
230                         if($update)
231                                 $return_url = $_SESSION['return_url'];
232                         else
233                                 $return_url = $_SESSION['return_url'] = $a->cmd;
234
235
236                         if(can_write_wall($a,$a->profile['uid'])) {
237                                 if($item['last-child']) {
238                                         $comment = replace_macros($cmnt_tpl,array(
239                                                 '$return_path' => $_SESSION['return_url'],
240                                                 '$type' => 'wall-comment',
241                                                 '$id' => $item['item_id'],
242                                                 '$parent' => $item['parent'],
243                                                 '$profile_uid' =>  $a->profile['uid'],
244                                                 '$ww' => ''
245                                         ));
246                                 }
247                         }
248
249
250                         $profile_url = $item['url'];
251
252                         // 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,
253                         // I can go directly to their profile as an authenticated guest.
254
255                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) && (strlen($item['dfrn-id'])) && (! $item['self'] ))
256                                 $profile_url = $redirect_url;
257
258                         // FIXME tryng to solve the mishmash of profile photos. 
259
260                 //      $photo = (($item['self']) ? $a->profile['photo'] : $item['photo']);
261                 //      $thumb = (($item['self']) ? $a->profile['thumb'] : $item['thumb']);
262         
263
264                         // We received this post via a remote feed. It's either a wall-to-wall or a remote comment. The author is
265                         // known to us and is reflected in the contact-id for this item. We can use the contact url or redirect rather than 
266                         // use the link in the feed. This is different than on the network page where we may not know the author.
267  
268                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
269                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
270                         $profile_link = $profile_url;
271
272                         $drop = '';
273
274                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
275                                 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
276
277
278
279
280                         $o .= replace_macros($template,array(
281                                 '$id' => $item['item_id'],
282                                 '$profile_url' => $profile_link,
283                                 '$name' => $profile_name,
284                                 '$thumb' => $profile_avatar,
285                                 '$body' => bbcode($item['body']),
286                                 '$ago' => relative_date($item['created']),
287                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
288                                 '$drop' => $drop,
289                                 '$comment' => $comment
290                         ));
291                 }
292         }
293
294         if($update) {
295                 return $o;
296         }
297                 
298         $o .= paginate($a);
299
300         return $o;
301 }