]> git.mxchange.org Git - friendica.git/blob - mod/profile.php
1b37d31bc0be39d288a3f17dd97cf6c2b281d3f6
[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 = 0;
61         if((local_user()) && ($a->argc > 2) && ($a->argv[2] == 'view')) {
62                 $which = $a->user['nickname'];
63                 $profile = $a->argv[1];         
64         }
65         profile_load($a,$which,$profile);
66         $a->page['htmlhead'] .= '<link rel="alternate" type="application/atom+xml" href="' . $a->get_baseurl() . '/dfrn_poll/' . $which .'" />';
67
68         $a->page['htmlhead'] .= "<meta name=\"dfrn-template\" content=\"" . $a->get_baseurl() . "/profile/%s" . "\" />\r\n";
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                 $_SESSION['profile_uid'] = $a->profile['uid'];
89         }
90
91         if(remote_user()) {
92                 $contact_id = $_SESSION['visitor_id'];
93                 $groups = init_groups_visitor($contact_id);
94                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
95                         intval($contact_id),
96                         intval($a->profile['uid'])
97                 );
98                 if(count($r))
99                         $contact = $r[0];
100         }
101         if(local_user()) {
102                 $contact_id = $_SESSION['cid'];
103                 $contact = $a->contact;
104         }
105
106         if($update) {
107                 // Ensure we've got a profile owner if updating.
108                 $a->profile['profile_uid'] = $_SESSION['profile_uid'];
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                         '$phototab' => $a->get_baseurl() . '/photos/' . $a->profile['nickname']
120                 ));
121
122
123                 if($tab == 'profile') {
124                         require_once('view/profile_advanced.php');
125                         return $o;
126                 }
127
128                 if(can_write_wall($a,$a->profile['profile_uid'])) {
129                         $tpl = file_get_contents('view/jot-header.tpl');
130         
131                         $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
132                         require_once('view/acl_selectors.php');
133
134                         $tpl = file_get_contents("view/jot.tpl");
135                         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']))))
136                                 $lockstate = 'lock';
137                         else
138                                 $lockstate = 'unlock';
139                         $o .= replace_macros($tpl,array(
140                                 '$baseurl' => $a->get_baseurl(),
141                                 '$return_path' => $a->cmd,
142                                 '$visitor' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? 'block' : 'none'),
143                                 '$lockstate' => $lockstate,
144                                 '$acl' => (($_SESSION['uid'] == $a->profile['profile_uid']) ? populate_acl($a->user) : ''),
145                                 '$profile_uid' => $a->profile['profile_uid']
146                         ));
147                 }
148
149                 if($tab == 'posts' && (! $a->pager['start']))
150                         $o .= '<div id="live-profile"></div>' . "\r\n";
151         }
152
153         // TODO alter registration and settings and profile to update contact table when names and  photos change.  
154
155         // default permissions - anonymous user
156
157         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
158
159         // Profile owner - everything is visible
160
161         if(local_user() && ($_SESSION['uid'] == $a->profile['uid'])) {
162                 $sql_extra = ''; 
163                 
164                 // Oh - while we're here... reset the Unseen messages
165
166                 $r = q("UPDATE `item` SET `unseen` = 0 
167                         WHERE `type` != 'remote' AND `unseen` = 1 AND `uid` = %d",
168                         intval($_SESSION['uid'])
169                 );
170
171         }
172
173         // authenticated visitor - here lie dragons
174         elseif(remote_user()) {
175                 $gs = '<<>>'; // should be impossible to match
176                 if(count($groups)) {
177                         foreach($groups as $g)
178                                 $gs .= '|<' . intval($g) . '>';
179                 } 
180                 $sql_extra = sprintf(
181                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
182                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
183                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
184                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
185
186                         intval($_SESSION['visitor_id']),
187                         intval($_SESSION['visitor_id']),
188                         dbesc($gs),
189                         dbesc($gs)
190                 );
191         }
192
193         $r = q("SELECT COUNT(*) AS `total`
194                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
195                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
196                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
197                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `type` != 'remote')
198                 $sql_extra ",
199                 intval($a->profile['uid'])
200
201         );
202
203         if(count($r))
204                 $a->set_pager_total($r[0]['total']);
205
206         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
207                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
208                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
209                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
210                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
211                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
212                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
213                 AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` AND `type` != 'remote')
214                 $sql_extra
215                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
216                 intval($a->profile['uid']),
217                 intval($a->pager['start']),
218                 intval($a->pager['itemspage'])
219
220         );
221
222
223         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
224
225         $tpl = file_get_contents('view/wall_item.tpl');
226
227         if($update)
228                 $return_url = $_SESSION['return_url'];
229         else
230                 $return_url = $_SESSION['return_url'] = $a->cmd;
231
232         if(count($r)) {
233                 foreach($r as $item) {
234                         $comment = '';
235                         $template = $tpl;
236                         
237                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
238                         
239
240
241                         if(can_write_wall($a,$a->profile['uid'])) {
242                                 if($item['last-child']) {
243                                         $comment = replace_macros($cmnt_tpl,array(
244                                                 '$return_path' => $_SESSION['return_url'],
245                                                 '$type' => 'wall-comment',
246                                                 '$id' => $item['item_id'],
247                                                 '$parent' => $item['parent'],
248                                                 '$profile_uid' =>  $a->profile['uid'],
249                                                 '$mylink' => $contact['url'],
250                                                 '$mytitle' => t('Me'),
251                                                 '$myphoto' => $contact['thumb'],
252                                                 '$ww' => ''
253                                         ));
254                                 }
255                         }
256
257
258                         $profile_url = $item['url'];
259
260                         // 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,
261                         // I can go directly to their profile as an authenticated guest.
262
263                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) && (strlen($item['dfrn-id'])) && (! $item['self'] ))
264                                 $profile_url = $redirect_url;
265
266                         // FIXME tryng to solve the mishmash of profile photos. 
267
268                 //      $photo = (($item['self']) ? $a->profile['photo'] : $item['photo']);
269                 //      $thumb = (($item['self']) ? $a->profile['thumb'] : $item['thumb']);
270         
271
272                         // We received this post via a remote feed. It's either a wall-to-wall or a remote comment. The author is
273                         // known to us and is reflected in the contact-id for this item. We can use the contact url or redirect rather than 
274                         // use the link in the feed. This is different than on the network page where we may not know the author.
275  
276                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
277                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
278                         $profile_link = $profile_url;
279
280                         $drop = '';
281
282                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
283                                 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
284
285
286
287
288                         $o .= replace_macros($template,array(
289                                 '$id' => $item['item_id'],
290                                 '$profile_url' => $profile_link,
291                                 '$name' => $profile_name,
292                                 '$thumb' => $profile_avatar,
293                                 '$title' => $item['title'],
294                                 '$body' => bbcode($item['body']),
295                                 '$ago' => relative_date($item['created']),
296                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
297                                 '$drop' => $drop,
298                                 '$comment' => $comment
299                         ));
300                         
301                 }
302         }
303
304         if($update) {
305                 return $o;
306         }
307                 
308         $o .= paginate($a);
309
310         return $o;
311 }