]> git.mxchange.org Git - friendica.git/blob - mod/like.php
rest of the consensus porting work and integration into friendica
[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                 case 'agree':
30                 case 'unagree':
31                         $activity = ACTIVITY_AGREE;
32                         break;
33                 case 'disagree':
34                 case 'undisagree':
35                         $activity = ACTIVITY_DISAGREE;
36                         break;
37                 case 'abstain':
38                 case 'unabstain':
39                         $activity = ACTIVITY_ABSTAIN;
40                         break;
41                 case 'attendyes':
42                 case 'unattendyes':
43                         $activity = ACTIVITY_ATTEND;
44                         break;
45                 case 'attendno':
46                 case 'unattendno':
47                         $activity = ACTIVITY_ATTENDNO;
48                         break;
49                 case 'attendmaybe':
50                 case 'unattendmaybe':
51                         $activity = ACTIVITY_ATTENDMAYBE;
52                         break;
53                 default:
54                         return;
55                         break;
56         }
57
58
59         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
60
61         logger('like: verb ' . $verb . ' item ' . $item_id);
62
63
64         $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
65                 dbesc($item_id),
66                 dbesc($item_id)
67         );
68
69         if(! $item_id || (! count($r))) {
70                 logger('like: no item ' . $item_id);
71                 return;
72         }
73
74         $item = $r[0];
75
76         $owner_uid = $item['uid'];
77
78         if(! can_write_wall($a,$owner_uid)) {
79                 return;
80         }
81
82         $remote_owner = null;
83
84         if(! $item['wall']) {
85                 // The top level post may have been written by somebody on another system
86                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
87                         intval($item['contact-id']),
88                         intval($item['uid'])
89                 );
90                 if(! count($r))
91                         return;
92                 if(! $r[0]['self'])
93                         $remote_owner = $r[0];
94         }
95
96         // this represents the post owner on this system. 
97
98         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
99                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
100                 intval($owner_uid)
101         );
102         if(count($r))
103                 $owner = $r[0];
104
105         if(! $owner) {
106                 logger('like: no owner');
107                 return;
108         }
109
110         if(! $remote_owner)
111                 $remote_owner = $owner;
112
113
114         // This represents the person posting
115
116         if((local_user()) && (local_user() == $owner_uid)) {
117                 $contact = $owner;
118         }
119         else {
120                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
121                         intval($_SESSION['visitor_id']),
122                         intval($owner_uid)
123                 );
124                 if(count($r))
125                         $contact = $r[0];
126         }
127         if(! $contact) {
128                 return;
129         }
130
131
132         // See if we've been passed a return path to redirect to
133         $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
134
135         $verbs = " '".dbesc($activity)."' ";
136
137         // event participation and consensus items are essentially radio toggles. If you make a subsequent choice,
138         // we need to eradicate your first choice. 
139         if($activity === ACTIVITY_ATTEND || $activity === ACTIVITY_ATTENDNO || $activity === ACTIVITY_ATTENDMAYBE) {
140                 $verbs = " '" . dbesc(ACTIVITY_ATTEND) . "','" . dbesc(ACTIVITY_ATTENDNO) . "','" . dbesc(ACTIVITY_ATTENDMAYBE) . "' ";
141         }
142         if($activity === ACTIVITY_AGREE || $activity === ACTIVITY_DISAGREE || $activity === ACTIVITY_ABSTAIN) {
143                 $verbs = " '" . dbesc(ACTIVITY_AGREE) . "','" . dbesc(ACTIVITY_DISAGREE) . "','" . dbesc(ACTIVITY_ABSTAIN) . "' ";
144         }
145
146         $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` IN ( $verbs ) AND `deleted` = 0
147                 AND `contact-id` = %d AND `uid` = %d
148                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
149                 intval($contact['id']), intval($owner_uid),
150                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
151         );
152
153         if(count($r)) {
154                 $like_item = $r[0];
155
156                 // Already voted, undo it
157                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
158                         dbesc(datetime_convert()),
159                         intval($like_item['id'])
160                 );
161
162
163                 // Clean up the Diaspora signatures for this like
164                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
165                 // if it had been enabled in the past
166                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
167                         intval($like_item['id'])
168                 );
169
170                 // Save the author information for the unlike in case we need to relay to Diaspora
171                 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
172
173 //              proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
174                 $like_item_id = $like_item['id'];
175                 proc_run('php',"include/notifier.php","like","$like_item_id");
176
177                 like_content_return($a->get_baseurl(), $return_path);
178                 return; // NOTREACHED
179         }
180
181         $uri = item_new_uri($a->get_hostname(),$owner_uid);
182
183         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
184         if($item['resource-type'] === 'event')
185                 $post_type = t('event');
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         if($verb === 'agree')
206                 $bodyverb = t('%1$s agrees with %2$s\'s %3$s');
207         if($verb === 'disagree')
208                 $bodyverb = t('%1$s doesn\'t agree with %2$s\'s %3$s');
209         if($verb === 'abstain')
210                 $bodyverb = t('%1$s abstains from a decision on %2$s\'s %3$s');
211         if($verb === 'attendyes')
212                 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
213         if($verb === 'attendno')
214                 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
215         if($verb === 'attendmaybe')
216                 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
217
218         if(! isset($bodyverb))
219                         return; 
220
221         $arr = array();
222
223         $arr['uri'] = $uri;
224         $arr['uid'] = $owner_uid;
225         $arr['contact-id'] = $contact['id'];
226         $arr['type'] = 'activity';
227         $arr['wall'] = $item['wall'];
228         $arr['origin'] = 1;
229         $arr['gravity'] = GRAVITY_LIKE;
230         $arr['parent'] = $item['id'];
231         $arr['parent-uri'] = $item['uri'];
232         $arr['thr-parent'] = $item['uri'];
233         $arr['owner-name'] = $remote_owner['name'];
234         $arr['owner-link'] = $remote_owner['url'];
235         $arr['owner-avatar'] = $remote_owner['thumb'];
236         $arr['author-name'] = $contact['name'];
237         $arr['author-link'] = $contact['url'];
238         $arr['author-avatar'] = $contact['thumb'];
239         
240         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
241         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
242         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
243         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
244
245         $arr['verb'] = $activity;
246         $arr['object-type'] = $objtype;
247         $arr['object'] = $obj;
248         $arr['allow_cid'] = $item['allow_cid'];
249         $arr['allow_gid'] = $item['allow_gid'];
250         $arr['deny_cid'] = $item['deny_cid'];
251         $arr['deny_gid'] = $item['deny_gid'];
252         $arr['visible'] = 1;
253         $arr['unseen'] = 1;
254         $arr['last-child'] = 0;
255
256         $post_id = item_store($arr);
257
258         if(! $item['visible']) {
259                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
260                         intval($item['id']),
261                         intval($owner_uid)
262                 );
263         }
264
265
266         // Save the author information for the like in case we need to relay to Diaspora
267         store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
268
269         $arr['id'] = $post_id;
270
271         call_hooks('post_local_end', $arr);
272
273         proc_run('php',"include/notifier.php","like","$post_id");
274
275         like_content_return($a->get_baseurl(), $return_path);
276         killme(); // NOTREACHED
277 //      return; // NOTREACHED
278 }
279
280
281 // Decide how to return. If we were called with a 'return' argument,
282 // then redirect back to the calling page. If not, just quietly end
283
284 function like_content_return($baseurl, $return_path) {
285
286         if($return_path) {
287                 $rand = '_=' . time();
288                 if(strpos($return_path, '?')) $rand = "&$rand";
289                 else $rand = "?$rand";
290
291                 goaway($baseurl . "/" . $return_path . $rand);
292         }
293
294         killme();
295 }
296
297
298 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
299         // Note that we can only create a signature for a user of the local server. We don't have
300         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
301         // means we are the relay, and for relayable_retractions, Diaspora
302         // only checks the parent_author_signature if it doesn't have to relay further
303         //
304         // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
305         // likes on photos, so don't bother.
306
307         $enabled = intval(get_config('system','diaspora_enabled'));
308         if(! $enabled) {
309                 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
310                 return;
311         }
312
313         logger('mod_like: storing diaspora like retraction signature');
314
315         if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
316                 $signed_text = $like_item['guid'] . ';' . 'Like';
317
318                 // Only works for NETWORK_DFRN
319                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
320                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
321                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
322                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
323
324                 // Get contact's private key if he's a user of the local Friendica server
325                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
326                         dbesc($contact['url'])
327                 );
328
329                 if( $r) {
330                         $contact_uid = $r['uid'];
331                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
332                                 intval($contact_uid)
333                         );
334
335                         if( $r)
336                                 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
337                 }
338
339                 if(! isset($authorsig))
340                         $authorsig = '';
341
342                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
343                         intval($like_item['id']),
344                         dbesc($signed_text),
345                         dbesc($authorsig),
346                         dbesc($diaspora_handle)
347                 );
348         }
349
350         return;
351 }
352
353 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
354         // Note that we can only create a signature for a user of the local server. We don't have
355         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
356         // means we are the relay, and for relayable_retractions, Diaspora
357         // only checks the parent_author_signature if it doesn't have to relay further
358
359         $enabled = intval(get_config('system','diaspora_enabled'));
360         if(! $enabled) {
361                 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
362                 return;
363         }
364
365         logger('mod_like: storing diaspora like signature');
366
367         if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
368                 // Only works for NETWORK_DFRN
369                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
370                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
371                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
372                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
373
374                 // Get contact's private key if he's a user of the local Friendica server
375                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
376                         dbesc($contact['url'])
377                 );
378
379                 if( $r) {
380                         $contact_uid = $r['uid'];
381                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
382                                 intval($contact_uid)
383                         );
384
385                         if( $r)
386                                 $contact_uprvkey = $r['prvkey'];
387                 }
388
389                 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
390                         intval($post_id)
391                 );
392                 if( $r) {
393                         $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
394                                 intval($r[0]['parent']),
395                                 intval($r[0]['parent'])
396                         );
397                         if( $p) {
398                                 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
399
400                                 if(isset($contact_uprvkey))
401                                         $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
402                                 else
403                                         $authorsig = '';
404
405                                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
406                                         intval($post_id),
407                                         dbesc($signed_text),
408                                         dbesc($authorsig),
409                                         dbesc($diaspora_handle)
410                                 );
411                         }
412                 }
413         }
414
415         return;
416 }