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