]> git.mxchange.org Git - friendica.git/blob - mod/item.php
4ebb1e978157edb0d6b06822f033a9cd72539904
[friendica.git] / mod / item.php
1 <?php
2
3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status, 
5 // local comments, and remote coments - that are posted on this site 
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of 
8 // information. 
9
10 function item_post(&$a) {
11
12         if((! local_user()) && (! remote_user()))
13                 return;
14
15         require_once('include/security.php');
16
17         $uid = $_SESSION['uid'];
18
19
20         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
21
22         $parent_item = null;
23
24         if($parent) {
25                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
26                         intval($parent)
27                 );
28                 if(! count($r)) {
29                         notice( t('Unable to locate original post.') . EOL);
30                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
31                 }
32                 $parent_item = $r[0];
33         }
34
35         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
36
37
38         if(! can_write_wall($a,$profile_uid)) {
39                 notice( t('Permission denied.') . EOL) ;
40                 return;
41         }
42
43         $user = null;
44
45         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
46                 intval($profile_uid)
47         );
48         if(count($r))
49                 $user = $r[0];
50         
51
52         $str_group_allow   = perms2str($_POST['group_allow']);
53         $str_contact_allow = perms2str($_POST['contact_allow']);
54         $str_group_deny    = perms2str($_POST['group_deny']);
55         $str_contact_deny  = perms2str($_POST['contact_deny']);
56
57         $title             = notags(trim($_POST['title']));
58         $body              = escape_tags(trim($_POST['body']));
59         $location          = notags(trim($_POST['location']));
60         $coord             = notags(trim($_POST['coord']));
61         $verb              = notags(trim($_POST['verb']));
62
63         if(! strlen($body)) {
64                 notice( t('Empty post discarded.') . EOL );
65                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
66
67         }
68
69         // get contact info for poster
70
71         $author = null;
72
73         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
74                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
75                         intval($_SESSION['uid'])
76                 );
77         }
78         else {
79                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
80                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
81                                 intval($_SESSION['visitor_id'])
82                         );
83                 }
84         }
85
86         if(count($r)) {
87                 $author = $r[0];
88                 $contact_id = $author['id'];
89         }
90
91         // get contact info for owner
92         
93         if($profile_uid == $_SESSION['uid']) {
94                 $contact_record = $author;
95         }
96         else {
97                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
98                         intval($profile_uid)
99                 );
100                 if(count($r))
101                         $contact_record = $r[0];
102         }
103
104         $post_type = notags(trim($_POST['type']));
105
106         if($post_type === 'net-comment') {
107                 if($parent_item !== null) {
108                         if($parent_item['type'] === 'remote') {
109                                 $post_type = 'remote-comment';
110                         } 
111                         else {          
112                                 $post_type = 'wall-comment';
113                         }
114                 }
115         }
116
117         $wall = 0;
118         if($post_type === 'wall' || $post_type === 'wall-comment')
119                 $wall = 1;
120
121         if(! strlen($verb))
122                 $verb = ACTIVITY_POST ;
123
124         $gravity = (($parent) ? 6 : 0 );
125  
126         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
127
128         $uri = item_new_uri($a->get_hostname(),$profile_uid);
129
130         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
131                 `author-name`, `author-link`, `author-avatar`, `created`,
132                 `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
133                 VALUES( %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
134                 intval($profile_uid),
135                 dbesc($post_type),
136                 intval($wall),
137                 intval($gravity),
138                 intval($contact_id),
139                 dbesc($contact_record['name']),
140                 dbesc($contact_record['url']),
141                 dbesc($contact_record['thumb']),
142                 dbesc($author['name']),
143                 dbesc($author['url']),
144                 dbesc($author['thumb']),
145                 dbesc(datetime_convert()),
146                 dbesc(datetime_convert()),
147                 dbesc(datetime_convert()),
148                 dbesc($uri),
149                 dbesc($title),
150                 dbesc($body),
151                 dbesc($location),
152                 dbesc($coord),
153                 dbesc($verb),
154                 dbesc($str_contact_allow),
155                 dbesc($str_group_allow),
156                 dbesc($str_contact_deny),
157                 dbesc($str_group_deny)
158         );
159         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
160                 dbesc($uri));
161         if(count($r)) {
162                 $post_id = $r[0]['id'];
163
164                 if($parent) {
165
166                         // This item is the last leaf and gets the comment box, clear any ancestors
167                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
168                                 dbesc(datetime_convert()),
169                                 intval($parent)
170                         );
171
172                         // Inherit ACL's from the parent item.
173                         // TODO merge with subsequent UPDATE operation and save a db write 
174
175                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
176                                 WHERE `id` = %d LIMIT 1",
177                                 dbesc($parent_item['allow_cid']),
178                                 dbesc($parent_item['allow_gid']),
179                                 dbesc($parent_item['deny_cid']),
180                                 dbesc($parent_item['deny_gid']),
181                                 intval($post_id)
182                         );
183
184                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
185                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
186                                 require_once('bbcode.php');
187                                 $from = $author['name'];
188                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
189                                 $email_tpl = replace_macros($tpl, array(
190                                         '$sitename' => $a->config['sitename'],
191                                         '$siteurl' =>  $a->get_baseurl(),
192                                         '$username' => $user['username'],
193                                         '$email' => $user['email'],
194                                         '$from' => $from,
195                                         '$body' => strip_tags(bbcode($body))
196                                 ));
197
198                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
199                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
200                         }
201                 }
202                 else {
203                         $parent = $post_id;
204
205                         // let me know if somebody did a wall-to-wall post on my profile
206
207                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
208                                 require_once('bbcode.php');
209                                 $from = $author['name'];
210                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
211                                 $email_tpl = replace_macros($tpl, array(
212                                         '$sitename' => $a->config['sitename'],
213                                         '$siteurl' =>  $a->get_baseurl(),
214                                         '$username' => $user['username'],
215                                         '$email' => $user['email'],
216                                         '$from' => $from,
217                                         '$body' => strip_tags(bbcode($body))
218                                 ));
219
220                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
221                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
222                         }
223                 }
224
225                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
226                         WHERE `id` = %d LIMIT 1",
227                         intval($parent),
228                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
229                         dbesc(datetime_convert()),
230                         intval($post_id)
231                 );
232
233                 // photo comments turn the corresponding item visible to the profile wall
234                 // This way we don't see every picture in your new photo album posted to your wall at once.
235                 // They will show up as people comment on them.
236
237                 if(! $parent_item['visible']) {
238                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
239                                 intval($parent_item['id'])
240                         );
241                 }
242         }
243
244         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
245         $proc_debug = get_config('system','proc_debug');
246
247         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" $proc_debug &",
248                 array(),$foo));
249
250         goaway($a->get_baseurl() . "/" . $_POST['return'] );
251         return; // NOTREACHED
252 }
253
254 function item_content(&$a) {
255
256         if((! local_user()) && (! remote_user()))
257                 return;
258
259         require_once('include/security.php');
260
261         $uid = $_SESSION['uid'];
262
263         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
264
265                 // locate item to be deleted
266
267                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
268                         intval($a->argv[2])
269                 );
270
271                 if(! count($r)) {
272                         notice( t('Item not found.') . EOL);
273                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
274                 }
275                 $item = $r[0];
276
277                 // check if logged in user is either the author or owner of this item
278
279                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
280
281                         // delete the item
282
283                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
284                                 dbesc(datetime_convert()),
285                                 dbesc(datetime_convert()),
286                                 intval($item['id'])
287                         );
288
289                         // If item is a link to a photo resource, nuke all the associated photos 
290                         // (visitors will not have photo resources)
291                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
292                         // generate a resource-id and therefore aren't intimately linked to the item. 
293
294                         if(strlen($item['resource-id'])) {
295                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
296                                         dbesc($item['resource-id']),
297                                         intval($item['uid'])
298                                 );
299                                 // ignore the result
300                         }
301
302                         // If it's the parent of a comment thread, kill all the kids
303
304                         if($item['uri'] == $item['parent-uri']) {
305                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
306                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
307                                         dbesc(datetime_convert()),
308                                         dbesc(datetime_convert()),
309                                         dbesc($item['parent-uri']),
310                                         intval($item['uid'])
311                                 );
312                                 // ignore the result
313                         }
314                         else {
315                                 // ensure that last-child is set in case the comment that had it just got wiped.
316                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
317                                         dbesc(datetime_convert()),
318                                         dbesc($item['parent-uri']),
319                                         intval($item['uid'])
320                                 );
321                                 // who is the last child now? 
322                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
323                                         dbesc($item['parent-uri']),
324                                         intval($item['uid'])
325                                 );
326                                 if(count($r)) {
327                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
328                                                 intval($r[0]['id'])
329                                         );
330                                 }       
331                         }
332                         $drop_id = intval($item['id']);
333                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
334                         $proc_debug = get_config('system','proc_debug');
335
336                         
337                         // send the notification upstream/downstream as the case may be
338
339                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug &",
340                                 array(), $foo));
341
342                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
343                         return; //NOTREACHED
344                 }
345                 else {
346                         notice( t('Permission denied.') . EOL);
347                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
348                         return; //NOTREACHED
349                 }
350         }
351 }