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