]> git.mxchange.org Git - friendica.git/blob - mod/item.php
clean up some regex's for i18n, and eliminate old ereg patterns.
[friendica.git] / mod / item.php
1 <?php
2
3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status, 
5 // local comments, and remote coments - that are posted on this site 
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of 
8 // information. 
9
10 function item_post(&$a) {
11
12         if((! local_user()) && (! remote_user()))
13                 return;
14
15         require_once('include/security.php');
16
17         $uid = local_user();
18
19         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
20
21         $parent_item = null;
22
23         if($parent) {
24                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
25                         intval($parent)
26                 );
27                 if(! count($r)) {
28                         notice( t('Unable to locate original post.') . EOL);
29                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
30                 }
31                 $parent_item = $r[0];
32         }
33
34         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
35
36
37         if(! can_write_wall($a,$profile_uid)) {
38                 notice( t('Permission denied.') . EOL) ;
39                 return;
40         }
41
42         $user = null;
43
44         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
45                 intval($profile_uid)
46         );
47         if(count($r))
48                 $user = $r[0];
49         
50
51         $str_group_allow   = perms2str($_POST['group_allow']);
52         $str_contact_allow = perms2str($_POST['contact_allow']);
53         $str_group_deny    = perms2str($_POST['group_deny']);
54         $str_contact_deny  = perms2str($_POST['contact_deny']);
55
56         $title             = notags(trim($_POST['title']));
57         $body              = escape_tags(trim($_POST['body']));
58         $location          = notags(trim($_POST['location']));
59         $coord             = notags(trim($_POST['coord']));
60         $verb              = notags(trim($_POST['verb']));
61
62         if(! strlen($body)) {
63                 notice( t('Empty post discarded.') . EOL );
64                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
65
66         }
67
68         // get contact info for poster
69
70         $author = null;
71
72         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
73                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
74                         intval($_SESSION['uid'])
75                 );
76         }
77         else {
78                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
79                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
80                                 intval($_SESSION['visitor_id'])
81                         );
82                 }
83         }
84
85         if(count($r)) {
86                 $author = $r[0];
87                 $contact_id = $author['id'];
88         }
89
90         // get contact info for owner
91         
92         if($profile_uid == $_SESSION['uid']) {
93                 $contact_record = $author;
94         }
95         else {
96                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
97                         intval($profile_uid)
98                 );
99                 if(count($r))
100                         $contact_record = $r[0];
101         }
102
103         $post_type = notags(trim($_POST['type']));
104
105         if($post_type === 'net-comment') {
106                 if($parent_item !== null) {
107                         if($parent_item['type'] === 'remote') {
108                                 $post_type = 'remote-comment';
109                         } 
110                         else {          
111                                 $post_type = 'wall-comment';
112                         }
113                 }
114         }
115
116         $str_tags = '';
117         $inform   = '';
118
119         $tags = get_tags($body);
120
121
122         if(count($tags)) {
123                 foreach($tags as $tag) {
124                         if(strpos($tag,'#') === 0) {
125                                 $basetag = substr($tag,1);
126                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . urlencode($basetag) . ']' . $basetag . '[/url]',$body);
127                                 continue;
128                         }
129                         if(strpos($tag,'@') === 0) {
130                                 $name = substr($tag,1);
131                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
132                                         $newname = $name;
133                                         $links = @lrdd($name);
134                                         if(count($links)) {
135                                                 foreach($links as $link) {
136                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
137                                         $profile = $link['@attributes']['href'];
138                                                         if($link['@attributes']['rel'] === 'salmon') {
139                                                                 if(strlen($inform))
140                                                                         $inform .= ',';
141                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
142                                                         }
143                                                 }
144                                         }
145                                 }
146                                 else {
147                                         $newname = $name;
148                                         if(strstr($name,'_')) {
149                                                 $newname = str_replace('_',' ',$name);
150                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
151                                                         dbesc($newname),
152                                                         intval($profile_uid)
153                                                 );
154                                         }
155                                         else {
156                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
157                                                         dbesc($name),
158                                                         intval($profile_uid)
159                                                 );
160                                         }
161                                         if(count($r)) {
162                                                 $profile = $r[0]['url'];
163                                                 $newname = $r[0]['name'];
164                                                 if(strlen($inform))
165                                                         $inform .= ',';
166                                                 $inform .= 'cid:' . $r[0]['id'];
167                                         }
168                                 }
169                                 if($profile) {
170                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
171                                         if(strlen($str_tags))
172                                                 $str_tags .= ',';
173                                         $profile = str_replace(',','%2c',$profile);
174                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
175                                 }
176                         }
177                 }
178         }
179
180         $wall = 0;
181         if($post_type === 'wall' || $post_type === 'wall-comment')
182                 $wall = 1;
183
184         if(! strlen($verb))
185                 $verb = ACTIVITY_POST ;
186
187         $gravity = (($parent) ? 6 : 0 );
188  
189         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
190
191         $uri = item_new_uri($a->get_hostname(),$profile_uid);
192
193         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
194                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
195                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
196                 VALUES( %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
197                 intval($profile_uid),
198                 dbesc($post_type),
199                 intval($wall),
200                 intval($gravity),
201                 intval($contact_id),
202                 dbesc($contact_record['name']),
203                 dbesc($contact_record['url']),
204                 dbesc($contact_record['thumb']),
205                 dbesc($author['name']),
206                 dbesc($author['url']),
207                 dbesc($author['thumb']),
208                 dbesc(datetime_convert()),
209                 dbesc(datetime_convert()),
210                 dbesc(datetime_convert()),
211                 dbesc($uri),
212                 dbesc($title),
213                 dbesc($body),
214                 dbesc($location),
215                 dbesc($coord),
216                 dbesc($str_tags),
217                 dbesc($inform),
218                 dbesc($verb),
219                 dbesc($str_contact_allow),
220                 dbesc($str_group_allow),
221                 dbesc($str_contact_deny),
222                 dbesc($str_group_deny)
223         );
224         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
225                 dbesc($uri));
226         if(count($r)) {
227                 $post_id = $r[0]['id'];
228
229                 if($parent) {
230
231                         // This item is the last leaf and gets the comment box, clear any ancestors
232                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
233                                 dbesc(datetime_convert()),
234                                 intval($parent)
235                         );
236
237                         // Inherit ACL's from the parent item.
238                         // TODO merge with subsequent UPDATE operation and save a db write 
239
240                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
241                                 WHERE `id` = %d LIMIT 1",
242                                 dbesc($parent_item['allow_cid']),
243                                 dbesc($parent_item['allow_gid']),
244                                 dbesc($parent_item['deny_cid']),
245                                 dbesc($parent_item['deny_gid']),
246                                 intval($post_id)
247                         );
248
249                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
250                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
251                                 require_once('bbcode.php');
252                                 $from = $author['name'];
253                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
254                                 $email_tpl = replace_macros($tpl, array(
255                                         '$sitename' => $a->config['sitename'],
256                                         '$siteurl' =>  $a->get_baseurl(),
257                                         '$username' => $user['username'],
258                                         '$email' => $user['email'],
259                                         '$from' => $from,
260                                         '$display' => $a->get_baseurl() . '/display/' . $post_id,
261                                         '$body' => strip_tags(bbcode($body))
262                                 ));
263
264                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
265                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
266                         }
267                 }
268                 else {
269                         $parent = $post_id;
270
271                         // let me know if somebody did a wall-to-wall post on my profile
272
273                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
274                                 require_once('bbcode.php');
275                                 $from = $author['name'];
276                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
277                                 $email_tpl = replace_macros($tpl, array(
278                                         '$sitename' => $a->config['sitename'],
279                                         '$siteurl' =>  $a->get_baseurl(),
280                                         '$username' => $user['username'],
281                                         '$email' => $user['email'],
282                                         '$from' => $from,
283                                         '$display' => $a->get_baseurl() . '/display/' . $post_id,
284                                         '$body' => strip_tags(bbcode($body))
285                                 ));
286
287                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
288                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
289                         }
290                 }
291
292                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
293                         WHERE `id` = %d LIMIT 1",
294                         intval($parent),
295                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
296                         dbesc(datetime_convert()),
297                         intval($post_id)
298                 );
299
300                 // photo comments turn the corresponding item visible to the profile wall
301                 // This way we don't see every picture in your new photo album posted to your wall at once.
302                 // They will show up as people comment on them.
303
304                 if(! $parent_item['visible']) {
305                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
306                                 intval($parent_item['id'])
307                         );
308                 }
309         }
310
311         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
312
313         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
314                 array(),$foo));
315
316         goaway($a->get_baseurl() . "/" . $_POST['return'] );
317         return; // NOTREACHED
318 }
319
320 function item_content(&$a) {
321
322         if((! local_user()) && (! remote_user()))
323                 return;
324
325         require_once('include/security.php');
326
327         $uid = $_SESSION['uid'];
328
329         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
330
331                 // locate item to be deleted
332
333                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
334                         intval($a->argv[2])
335                 );
336
337                 if(! count($r)) {
338                         notice( t('Item not found.') . EOL);
339                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
340                 }
341                 $item = $r[0];
342
343                 // check if logged in user is either the author or owner of this item
344
345                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
346
347                         // delete the item
348
349                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
350                                 dbesc(datetime_convert()),
351                                 dbesc(datetime_convert()),
352                                 intval($item['id'])
353                         );
354
355                         // If item is a link to a photo resource, nuke all the associated photos 
356                         // (visitors will not have photo resources)
357                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
358                         // generate a resource-id and therefore aren't intimately linked to the item. 
359
360                         if(strlen($item['resource-id'])) {
361                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
362                                         dbesc($item['resource-id']),
363                                         intval($item['uid'])
364                                 );
365                                 // ignore the result
366                         }
367
368                         // If it's the parent of a comment thread, kill all the kids
369
370                         if($item['uri'] == $item['parent-uri']) {
371                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
372                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
373                                         dbesc(datetime_convert()),
374                                         dbesc(datetime_convert()),
375                                         dbesc($item['parent-uri']),
376                                         intval($item['uid'])
377                                 );
378                                 // ignore the result
379                         }
380                         else {
381                                 // ensure that last-child is set in case the comment that had it just got wiped.
382                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
383                                         dbesc(datetime_convert()),
384                                         dbesc($item['parent-uri']),
385                                         intval($item['uid'])
386                                 );
387                                 // who is the last child now? 
388                                 $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",
389                                         dbesc($item['parent-uri']),
390                                         intval($item['uid'])
391                                 );
392                                 if(count($r)) {
393                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
394                                                 intval($r[0]['id'])
395                                         );
396                                 }       
397                         }
398                         $drop_id = intval($item['id']);
399                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
400                         
401                         // send the notification upstream/downstream as the case may be
402
403                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
404                                 array(), $foo));
405
406                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
407                         return; //NOTREACHED
408                 }
409                 else {
410                         notice( t('Permission denied.') . EOL);
411                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
412                         //NOTREACHED
413                 }
414         }
415 }