]> git.mxchange.org Git - friendica.git/blob - mod/item.php
better intro text
[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`, `changed`, `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', '%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                 dbesc(datetime_convert()),
158                 dbesc(datetime_convert()),
159                 dbesc(datetime_convert()),
160                 dbesc($uri),
161                 dbesc($title),
162                 dbesc($body),
163                 dbesc($location),
164                 dbesc($str_contact_allow),
165                 dbesc($str_group_allow),
166                 dbesc($str_contact_deny),
167                 dbesc($str_group_deny)
168         );
169         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
170                 dbesc($uri));
171         if(count($r)) {
172                 $post_id = $r[0]['id'];
173
174                 if($parent) {
175
176                         // This item is the last leaf and gets the comment box, clear any ancestors
177                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
178                                 dbesc(datetime_convert()),
179                                 intval($parent)
180                         );
181
182                         // Inherit ACL's from the parent item.
183                         // TODO merge with subsequent UPDATE operation and save a db write 
184
185                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
186                                 WHERE `id` = %d LIMIT 1",
187                                 dbesc($parent_item['allow_cid']),
188                                 dbesc($parent_item['allow_gid']),
189                                 dbesc($parent_item['deny_cid']),
190                                 dbesc($parent_item['deny_gid']),
191                                 intval($post_id)
192                         );
193
194                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
195                                 require_once('bbcode.php');
196                                 $from = $author['name'];
197                                 $tpl = file_get_contents('view/cmnt_received_eml.tpl');                 
198                                 $email_tpl = replace_macros($tpl, array(
199                                         '$sitename' => $a->config['sitename'],
200                                         '$siteurl' =>  $a->get_baseurl(),
201                                         '$username' => $user['username'],
202                                         '$email' => $user['email'],
203                                         '$from' => $from,
204                                         '$body' => strip_tags(bbcode($body))
205                                 ));
206
207                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
208                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
209                         }
210                 }
211                 else {
212                         $parent = $post_id;
213
214                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
215                                 require_once('bbcode.php');
216                                 $from = $author['name'];
217                                 $tpl = file_get_contents('view/wall_received_eml.tpl');                 
218                                 $email_tpl = replace_macros($tpl, array(
219                                         '$sitename' => $a->config['sitename'],
220                                         '$siteurl' =>  $a->get_baseurl(),
221                                         '$username' => $user['username'],
222                                         '$email' => $user['email'],
223                                         '$from' => $from,
224                                         '$body' => strip_tags(bbcode($body))
225                                 ));
226
227                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
228                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
229                         }
230                 }
231
232                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
233                         WHERE `id` = %d LIMIT 1",
234                         intval($parent),
235                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
236                         dbesc(datetime_convert()),
237                         intval($post_id)
238                 );
239                 // photo comments turn the corresponding item visible to the profile wall
240                 if(! $parent_item['visible']) {
241                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
242                                 intval($parent_item['id'])
243                         );
244                 }
245         }
246
247         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
248
249         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
250                 array(),$foo));
251
252         goaway($a->get_baseurl() . "/" . $_POST['return'] );
253         return; // NOTREACHED
254 }
255
256 function item_content(&$a) {
257
258         if((! local_user()) && (! remote_user()))
259                 return;
260
261         require_once('include/security.php');
262
263         $uid = $_SESSION['uid'];
264
265         if(($a->argc == 3) && ($a->argv[1] == 'drop') && intval($a->argv[2])) {
266
267                 // locate item to be deleted
268
269                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
270                         intval($a->argv[2])
271                 );
272
273                 if(! count($r)) {
274                         notice( t('Item not found.') . EOL);
275                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
276                 }
277                 $item = $r[0];
278
279                 // check if logged in user is either the author or owner of this item
280
281                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
282
283                         // delete the item
284
285                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
286                                 dbesc(datetime_convert()),
287                                 dbesc(datetime_convert()),
288                                 intval($item['id'])
289                         );
290
291                         // If item is a link to a photo resource, nuke all the associated photos 
292                         // (visitors will not have photo resources)
293                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
294                         // generate a resource-id and therefore aren't intimately linked to the item. 
295
296                         if(strlen($item['resource-id'])) {
297                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
298                                         dbesc($item['resource-id']),
299                                         intval($item['uid'])
300                                 );
301                                 // ignore the result
302                         }
303
304                         // If it's the parent of a comment thread, kill all the kids
305
306                         if($item['uri'] == $item['parent-uri']) {
307                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
308                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
309                                         dbesc(datetime_convert()),
310                                         dbesc(datetime_convert()),
311                                         dbesc($item['parent-uri']),
312                                         intval($item['uid'])
313                                 );
314                                 // ignore the result
315                         }
316
317                         $drop_id = intval($item['id']);
318                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
319                         
320                         // send the notification upstream/downstream as the case may be
321
322                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
323                                 array(), $foo));
324
325                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
326                         return; //NOTREACHED
327                 }
328                 else {
329                         notice( t('Permission denied.') . EOL);
330                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
331                         return; //NOTREACHED
332                 }
333         }
334 }