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