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