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