]> git.mxchange.org Git - friendica.git/blob - mod/like.php
Merge pull request #323 from fermionic/add-relayable-retraction-support-for-diaspora
[friendica.git] / mod / like.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 like_content(&$a) {
9
10         if(! local_user() && ! remote_user()) {
11                 return;
12         }
13
14         $verb = notags(trim($_GET['verb']));
15
16         if(! $verb)
17                 $verb = 'like';
18
19
20         switch($verb) {
21                 case 'like':
22                 case 'unlike':
23                         $activity = ACTIVITY_LIKE;
24                         break;
25                 case 'dislike':
26                 case 'undislike':
27                         $activity = ACTIVITY_DISLIKE;
28                         break;
29                 default:
30                         return;
31                         break;
32         }
33
34
35         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
36
37         logger('like: verb ' . $verb . ' item ' . $item_id);
38
39
40         $r = q("SELECT * FROM `item` WHERE ( `id` = '%s' OR `uri` = '%s') AND `id` = `parent` LIMIT 1",
41                 dbesc($item_id),
42                 dbesc($item_id)
43         );
44
45         if(! $item_id || (! count($r))) {
46                 logger('like: no item ' . $item_id);
47                 return;
48         }
49
50         $item = $r[0];
51
52         $owner_uid = $item['uid'];
53
54         if(! can_write_wall($a,$owner_uid)) {
55                 return;
56         }
57
58         $remote_owner = null;
59
60         if(! $item['wall']) {
61                 // The top level post may have been written by somebody on another system
62                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
63                         intval($item['contact-id']),
64                         intval($item['uid'])
65                 );
66                 if(! count($r))
67                         return;
68                 if(! $r[0]['self'])
69                         $remote_owner = $r[0];
70         }
71
72         // this represents the post owner on this system. 
73
74         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
75                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
76                 intval($owner_uid)
77         );
78         if(count($r))
79                 $owner = $r[0];
80
81         if(! $owner) {
82                 logger('like: no owner');
83                 return;
84         }
85
86         if(! $remote_owner)
87                 $remote_owner = $owner;
88
89
90         // This represents the person posting
91
92         if((local_user()) && (local_user() == $owner_uid)) {
93                 $contact = $owner;
94         }
95         else {
96                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
97                         intval($_SESSION['visitor_id']),
98                         intval($owner_uid)
99                 );
100                 if(count($r))
101                         $contact = $r[0];
102         }
103         if(! $contact) {
104                 return;
105         }
106
107
108         $r = q("SELECT * FROM `item` WHERE `verb` = '%s' AND `deleted` = 0 
109                 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s') LIMIT 1",
110                 dbesc($activity),
111                 intval($contact['id']),
112                 dbesc($item_id),
113                 dbesc($item_id)
114         );
115         if(count($r)) {
116                 $like_item = $r[0];
117
118                 // Already voted, undo it
119                 $r = q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
120                         dbesc(datetime_convert()),
121                         intval($like_item['id'])
122                 );
123
124                 // Clean up the `sign` table
125                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
126                         intval($like_item['id'])
127                 );
128
129                 // Save the author information for the unlike in case we need to relay to Diaspora
130                 // Note that we can only create a signature for a user of the local server. We don't have
131                 // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
132                 // means we are the relay, and for relayable_retractions, Diaspora
133                 // only checks the parent_author_signature if it doesn't have to relay further
134                 //
135                 // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
136                 // likes on photos, so don't bother.
137
138                 if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
139                         $signed_text = $like_item['guid'] . ';' . 'Like';
140
141                         if( $contact['network'] === NETWORK_DIASPORA)
142                                 $diaspora_handle = $contact['addr'];
143                         else { // Only works for NETWORK_DFRN
144                                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
145                                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
146                                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
147                                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
148
149                                 // Get contact's private key if he's a user of the local Friendica server
150                                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
151                                         dbesc($contact['url'])
152                                 );
153
154                                 if( $r) {
155                                         $contact_uid = $r['uid'];
156                                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
157                                                 intval($contact_uid)
158                                         );
159
160                                         if( $r)
161                                                 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
162                                 }
163                         }
164
165                         if(! isset($authorsig))
166                                 $authorsig = '';
167
168                         q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
169                                 intval($like_item['id']),
170                                 dbesc($signed_text),
171                                 dbesc($authorsig),
172                                 dbesc($diaspora_handle)
173                         );
174                 }
175
176
177 //              proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
178                 $like_item_id = $like_item['id'];
179                 proc_run('php',"include/notifier.php","like","$like_item_id");
180                 return;
181         }
182
183         $uri = item_new_uri($a->get_hostname(),$owner_uid);
184
185         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
186         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
187         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
188         $body = $item['body'];
189
190         $obj = <<< EOT
191
192         <object>
193                 <type>$objtype</type>
194                 <local>1</local>
195                 <id>{$item['uri']}</id>
196                 <link>$link</link>
197                 <title></title>
198                 <content>$body</content>
199         </object>
200 EOT;
201         if($verb === 'like')
202                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
203         if($verb === 'dislike')
204                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
205
206         if(! isset($bodyverb))
207                         return; 
208
209         $arr = array();
210
211         $arr['uri'] = $uri;
212         $arr['uid'] = $owner_uid;
213         $arr['contact-id'] = $contact['id'];
214         $arr['type'] = 'activity';
215         $arr['wall'] = $item['wall'];
216         $arr['origin'] = 1;
217         $arr['gravity'] = GRAVITY_LIKE;
218         $arr['parent'] = $item['id'];
219         $arr['parent-uri'] = $item['uri'];
220         $arr['owner-name'] = $remote_owner['name'];
221         $arr['owner-link'] = $remote_owner['url'];
222         $arr['owner-avatar'] = $remote_owner['thumb'];
223         $arr['author-name'] = $contact['name'];
224         $arr['author-link'] = $contact['url'];
225         $arr['author-avatar'] = $contact['thumb'];
226         
227         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
228         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
229         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
230         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
231
232         $arr['verb'] = $activity;
233         $arr['object-type'] = $objtype;
234         $arr['object'] = $obj;
235         $arr['allow_cid'] = $item['allow_cid'];
236         $arr['allow_gid'] = $item['allow_gid'];
237         $arr['deny_cid'] = $item['deny_cid'];
238         $arr['deny_gid'] = $item['deny_gid'];
239         $arr['visible'] = 1;
240         $arr['unseen'] = 1;
241         $arr['last-child'] = 0;
242
243         $post_id = item_store($arr);    
244
245         if(! $item['visible']) {
246                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
247                         intval($item['id']),
248                         intval($owner_uid)
249                 );
250         }                       
251
252
253         // Save the author information for the like in case we need to relay to Diaspora
254         // Note that we can only create a signature for a user of the local server. We don't have
255         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
256         // means we are the relay, and for relayable_retractions, Diaspora
257         // only checks the parent_author_signature if it doesn't have to relay further
258
259         if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
260                 if( $contact['network'] === NETWORK_DIASPORA)
261                         $diaspora_handle = $contact['addr'];
262                 else { // Only works for NETWORK_DFRN
263                         $contact_baseurl_start = strpos($contact['url'],'://') + 3;
264                         $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
265                         $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
266                         $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
267
268                         // Get contact's private key if he's a user of the local Friendica server
269                         $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
270                                 dbesc($contact['url'])
271                         );
272
273                         if( $r) {
274                                 $contact_uid = $r['uid'];
275                                 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
276                                         intval($contact_uid)
277                                 );
278
279                                 if( $r)
280                                         $contact_uprvkey = $r['prvkey'];
281                         }
282                 }
283
284                 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
285                         intval($post_id)
286                 );
287                 if( $r) {
288                         $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
289                                 intval($r[0]['parent']),
290                                 intval($r[0]['parent'])
291                         );
292                         if( $p) {
293                                 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
294
295                                 if(isset($contact_uprvkey))
296                                         $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
297                                 else
298                                         $authorsig = '';
299
300                                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
301                                         intval($post_id),
302                                         dbesc($signed_text),
303                                         dbesc($authorsig),
304                                         dbesc($diaspora_handle)
305                                 );
306                         }
307                 }
308         }
309
310
311         $arr['id'] = $post_id;
312
313         call_hooks('post_local_end', $arr);
314
315         proc_run('php',"include/notifier.php","like","$post_id");
316
317         killme();
318 //      return; // NOTREACHED
319 }