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