]> git.mxchange.org Git - friendica.git/blob - mod/subthread.php
Merge pull request #4297 from hoergen/develop
[friendica.git] / mod / subthread.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Addon;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
7
8 require_once('include/security.php');
9 require_once('include/bbcode.php');
10 require_once('include/items.php');
11
12 function subthread_content(App $a) {
13
14         if(! local_user() && ! remote_user()) {
15                 return;
16         }
17
18         $activity = ACTIVITY_FOLLOW;
19
20         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
21
22         $r = q("SELECT * FROM `item` WHERE `parent` = '%s' OR `parent-uri` = '%s' and parent = id LIMIT 1",
23                 dbesc($item_id),
24                 dbesc($item_id)
25         );
26
27         if(! $item_id || (! DBM::is_result($r))) {
28                 logger('subthread: no item ' . $item_id);
29                 return;
30         }
31
32         $item = $r[0];
33
34         $owner_uid = $item['uid'];
35
36         if(! can_write_wall($owner_uid)) {
37                 return;
38         }
39
40         $remote_owner = null;
41
42         if(! $item['wall']) {
43                 // The top level post may have been written by somebody on another system
44                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
45                         intval($item['contact-id']),
46                         intval($item['uid'])
47                 );
48                 if (! DBM::is_result($r)) {
49                         return;
50                 }
51                 if (! $r[0]['self']) {
52                         $remote_owner = $r[0];
53                 }
54         }
55
56         // this represents the post owner on this system.
57
58         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
60                 intval($owner_uid)
61         );
62         if (DBM::is_result($r))
63                 $owner = $r[0];
64
65         if (! $owner) {
66                 logger('like: no owner');
67                 return;
68         }
69
70         if (! $remote_owner)
71                 $remote_owner = $owner;
72
73
74         // This represents the person posting
75
76         if ((local_user()) && (local_user() == $owner_uid)) {
77                 $contact = $owner;
78         } else {
79                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
80                         intval($_SESSION['visitor_id']),
81                         intval($owner_uid)
82                 );
83                 if (DBM::is_result($r))
84                         $contact = $r[0];
85         }
86         if (! $contact) {
87                 return;
88         }
89
90         $uri = item_new_uri($a->get_hostname(),$owner_uid);
91
92         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
93         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
94         $link = xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
95         $body = $item['body'];
96
97         $obj = <<< EOT
98
99         <object>
100                 <type>$objtype</type>
101                 <local>1</local>
102                 <id>{$item['uri']}</id>
103                 <link>$link</link>
104                 <title></title>
105                 <content>$body</content>
106         </object>
107 EOT;
108         $bodyverb = t('%1$s is following %2$s\'s %3$s');
109
110         if (! isset($bodyverb)) {
111                 return;
112         }
113
114         $arr = [];
115
116         $arr['guid'] = get_guid(32);
117         $arr['uri'] = $uri;
118         $arr['uid'] = $owner_uid;
119         $arr['contact-id'] = $contact['id'];
120         $arr['type'] = 'activity';
121         $arr['wall'] = $item['wall'];
122         $arr['origin'] = 1;
123         $arr['gravity'] = GRAVITY_LIKE;
124         $arr['parent'] = $item['id'];
125         $arr['parent-uri'] = $item['uri'];
126         $arr['thr-parent'] = $item['uri'];
127         $arr['owner-name'] = $remote_owner['name'];
128         $arr['owner-link'] = $remote_owner['url'];
129         $arr['owner-avatar'] = $remote_owner['thumb'];
130         $arr['author-name'] = $contact['name'];
131         $arr['author-link'] = $contact['url'];
132         $arr['author-avatar'] = $contact['thumb'];
133
134         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
135         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
136         $plink = '[url=' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
137         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
138
139         $arr['verb'] = $activity;
140         $arr['object-type'] = $objtype;
141         $arr['object'] = $obj;
142         $arr['allow_cid'] = $item['allow_cid'];
143         $arr['allow_gid'] = $item['allow_gid'];
144         $arr['deny_cid'] = $item['deny_cid'];
145         $arr['deny_gid'] = $item['deny_gid'];
146         $arr['visible'] = 1;
147         $arr['unseen'] = 1;
148
149         $post_id = item_store($arr);
150
151         if (! $item['visible']) {
152                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
153                         intval($item['id']),
154                         intval($owner_uid)
155                 );
156         }
157
158         $arr['id'] = $post_id;
159
160         Addon::callHooks('post_local_end', $arr);
161
162         killme();
163
164 }
165
166