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