]> git.mxchange.org Git - friendica.git/blob - mod/display.php
like, dislike, activity streams, etc.
[friendica.git] / mod / display.php
1 <?php
2
3
4 function display_content(&$a) {
5
6         require_once('mod/profile.php');
7         profile_init($a);
8
9         $item_id = (($a->argc > 2) ? intval($a->argv[2]) : 0);
10
11         if(! $item_id) {
12                 $a->error = 404;
13                 notice( t('Item not found.') . EOL);
14                 return;
15         }
16
17         require_once("include/bbcode.php");
18         require_once('include/security.php');
19
20
21         $groups = array();
22
23         $tab = 'posts';
24
25
26         $contact = null;
27         $remote_contact = false;
28
29         if(remote_user()) {
30                 $contact_id = $_SESSION['visitor_id'];
31                 $groups = init_groups_visitor($contact_id);
32                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
33                         intval($contact_id),
34                         intval($a->profile['uid'])
35                 );
36                 if(count($r)) {
37                         $contact = $r[0];
38                         $remote_contact = true;
39                 }
40         }
41
42         if(! $remote_contact) {
43                 if(local_user()) {
44                         $contact_id = $_SESSION['cid'];
45                         $contact = $a->contact;
46                 }
47         }
48
49
50         $sql_extra = "
51                 AND `allow_cid` = '' 
52                 AND `allow_gid` = '' 
53                 AND `deny_cid`  = '' 
54                 AND `deny_gid`  = '' 
55         ";
56
57
58         // Profile owner - everything is visible
59
60         if(local_user() && (get_uid() == $a->profile['uid'])) {
61                 $sql_extra = '';                
62         }
63
64         // authenticated visitor - here lie dragons
65         // If $remotecontact is true, we know that not only is this a remotely authenticated
66         // person, but that it is *our* contact, which is important in multi-user mode.
67
68         elseif($remote_contact) {
69                 $gs = '<<>>'; // should be impossible to match
70                 if(count($groups)) {
71                         foreach($groups as $g)
72                                 $gs .= '|<' . intval($g) . '>';
73                 } 
74                 $sql_extra = sprintf(
75                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
76                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
77                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
78                           AND ( `deny_gid`  = '' OR  NOT `deny_gid` REGEXP '%s') ",
79
80                         intval($_SESSION['visitor_id']),
81                         intval($_SESSION['visitor_id']),
82                         dbesc($gs),
83                         dbesc($gs)
84                 );
85         }
86
87         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
88                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
89                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
90                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
91                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
92                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
93                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
94                 AND `item`.`parent` = ( SELECT `parent` FROM `item` WHERE ( `id` = '%s' OR `uri` = '%s' ))
95                 $sql_extra
96                 ORDER BY `parent` DESC, `gravity` ASC, `id` ASC ",
97                 intval($a->profile['uid']),
98                 dbesc($item_id),
99                 dbesc($item_id)
100         );
101
102
103         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
104         $like_tpl = file_get_contents('view/like.tpl');
105         $tpl = file_get_contents('view/wall_item.tpl');
106         $wallwall = file_get_contents('view/wallwall_item.tpl');
107
108         $return_url = $_SESSION['return_url'] = $a->cmd;
109
110         $alike = array();
111         $dlike = array();
112
113         if(count($r)) {
114
115                 foreach($r as $item) {
116
117                         if(($item['verb'] == ACTIVITY_LIKE) && ($item['id'] != $item['parent'])) {
118                                 $url = $item['url'];
119                                 if(($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'])) 
120                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
121                                 if(! is_array($alike[$item['parent'] . '-l']))
122                                         $alike[$item['parent'] . '-l'] = array();
123                                 $alike[$item['parent']] ++;
124                                 $alike[$item['parent'] . '-l'][] = '<a href="'. $url . '">' . $item['name'] . '</a>';
125                         }
126                         if(($item['verb'] == ACTIVITY_DISLIKE) && ($item['id'] != $item['parent'])) {
127                                 $url = $item['url'];
128                                 if(($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'])) 
129                                         $url = $a->get_baseurl() . '/redir/' . $item['contact-id'];
130                                 if(! is_array($dlike[$item['parent'] . '-l']))
131                                         $dlike[$item['parent'] . '-l'] = array();
132                                 $dlike[$item['parent']] ++;
133                                 $dlike[$item['parent'] . '-l'][] = '<a href="'. $url . '">' . $item['name'] . '</a>';
134                         }
135                 }
136
137
138
139                 foreach($r as $item) {
140                         $comment = '';
141                         $template = $tpl;
142                         
143                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
144                         
145                         if((($item['verb'] == ACTIVITY_LIKE) || ($item['verb'] == ACTIVITY_DISLIKE)) && ($item['id'] != $item['parent'])) 
146                                 continue;
147
148                         if(can_write_wall($a,$a->profile['uid'])) {
149                                 if($item['last-child']) {
150                                         $comment = replace_macros($cmnt_tpl,array(
151                                                 '$return_path' => $_SESSION['return_url'],
152                                                 '$type' => 'wall-comment',
153                                                 '$id' => $item['item_id'],
154                                                 '$parent' => $item['parent'],
155                                                 '$profile_uid' =>  $a->profile['uid'],
156                                                 '$mylink' => $contact['url'],
157                                                 '$mytitle' => t('Me'),
158                                                 '$myphoto' => $contact['thumb'],
159                                                 '$ww' => ''
160                                         ));
161                                 }
162                         }
163
164
165                         $profile_url = $item['url'];
166
167
168                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
169
170
171                         // Top-level wall post not written by the wall owner (wall-to-wall)
172                         // First figure out who owns it. 
173
174                         if(($item['parent'] == $item['item_id']) && (! $item['self'])) {
175                                 
176                                 if($item['type'] == 'wall') {
177                                         // I do. Put me on the left of the wall-to-wall notice.
178                                         $owner_url = $a->contact['url'];
179                                         $owner_photo = $a->contact['thumb'];
180                                         $owner_name = $a->contact['name'];
181                                         $template = $wallwall;
182                                         $commentww = 'ww';      
183                                 }
184                                 if($item['type'] == 'remote' && ($item['owner-link'] != $item['author-link'])) {
185                                         // Could be anybody. 
186                                         $owner_url = $item['owner-link'];
187                                         $owner_photo = $item['owner-avatar'];
188                                         $owner_name = $item['owner-name'];
189                                         $template = $wallwall;
190                                         $commentww = 'ww';
191                                         // If it is our contact, use a friendly redirect link
192                                         if(($item['owner-link'] == $item['url']) && ($item['rel'] == DIRECTION_IN || $item['rel'] == DIRECTION_BOTH))
193                                                 $owner_url = $redirect_url;
194                                                 $owner_url = $redirect_url;
195
196                                 }
197                         }
198
199                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
200                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
201                         $profile_link = $profile_url;
202
203                         $drop = '';
204
205                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == get_uid()))
206                                 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
207
208                         $like    = (($alike[$item['id']]) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
209                         $dislike = (($dlike[$item['id']]) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
210
211                         $likebuttons = '';
212                         if($item['id'] == $item['parent']) {
213                                 $likebuttons = replace_macros($like_tpl,array('$id' => $item['id']));
214                         }
215
216
217
218                         $o .= replace_macros($template,array(
219                                 '$id' => $item['item_id'],
220                                 '$profile_url' => $profile_link,
221                                 '$name' => $profile_name,
222                                 '$thumb' => $profile_avatar,
223                                 '$title' => $item['title'],
224                                 '$body' => bbcode($item['body']),
225                                 '$ago' => relative_date($item['created']),
226                                 '$location' => (($item['location']) ? '<a target="map" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : ''),
227                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
228                                 '$owner_url' => $owner_url,
229                                 '$owner_photo' => $owner_photo,
230                                 '$owner_name' => $owner_name,
231                                 '$drop' => $drop,
232                                 '$vote' => $likebuttons,
233                                 '$like' => $like,
234                                 '$dislike' => $dislike,
235                                 '$comment' => $comment
236                         ));
237
238                 }
239         }
240         else {
241                 $r = q("SELECT `id` FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
242                         dbesc($item_id),
243                         dbesc($item_id)
244                 );
245                 if(count($r)) {
246                         if($r[0]['deleted']) {
247                                 notice( t('Item has been removed.') . EOL );
248                         }
249                         else {  
250                                 notice( t('Permission denied.') . EOL ); 
251                         }
252                 }
253                 else {
254                         notice( t('Item not found.') . EOL );
255                 }
256
257         }
258         return $o;
259 }
260