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