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