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