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