]> git.mxchange.org Git - friendica.git/blob - mod/like.php
make event confirmation work
[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
136         $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0
137                 AND `contact-id` = %d AND `uid` = %d
138                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
139                 dbesc($activity), intval($contact['id']), intval($owner_uid),
140                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
141         );
142
143         if(count($r)) {
144                 $like_item = $r[0];
145
146                 // Already voted, undo it
147                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
148                         dbesc(datetime_convert()),
149                         intval($like_item['id'])
150                 );
151
152
153                 // Clean up the Diaspora signatures for this like
154                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
155                 // if it had been enabled in the past
156                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
157                         intval($like_item['id'])
158                 );
159
160                 // Save the author information for the unlike in case we need to relay to Diaspora
161                 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
162
163 //              proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
164                 $like_item_id = $like_item['id'];
165                 proc_run('php',"include/notifier.php","like","$like_item_id");
166
167                 like_content_return($a->get_baseurl(), $return_path);
168                 return; // NOTREACHED
169         }
170
171         $uri = item_new_uri($a->get_hostname(),$owner_uid);
172
173         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
174         if($item['resource-type'] === 'event')
175                 $post_type = t('event');
176         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
177         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
178         $body = $item['body'];
179
180         $obj = <<< EOT
181
182         <object>
183                 <type>$objtype</type>
184                 <local>1</local>
185                 <id>{$item['uri']}</id>
186                 <link>$link</link>
187                 <title></title>
188                 <content>$body</content>
189         </object>
190 EOT;
191         if($verb === 'like')
192                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
193         if($verb === 'dislike')
194                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
195         if($verb === 'agree')
196                 $bodyverb = t('%1$s agrees with %2$s\'s %3$s');
197         if($verb === 'disagree')
198                 $bodyverb = t('%1$s doesn\'t agree with %2$s\'s %3$s');
199         if($verb === 'abstain')
200                 $bodyverb = t('%1$s abstains from a decision on %2$s\'s %3$s');
201         if($verb === 'attendyes')
202                 $bodyverb = t('%1$s is attending %2$s\'s %3$s');
203         if($verb === 'attendno')
204                 $bodyverb = t('%1$s is not attending %2$s\'s %3$s');
205         if($verb === 'attendmaybe')
206                 $bodyverb = t('%1$s may attend %2$s\'s %3$s');
207
208         if(! isset($bodyverb))
209                         return; 
210
211         $arr = array();
212
213         $arr['uri'] = $uri;
214         $arr['uid'] = $owner_uid;
215         $arr['contact-id'] = $contact['id'];
216         $arr['type'] = 'activity';
217         $arr['wall'] = $item['wall'];
218         $arr['origin'] = 1;
219         $arr['gravity'] = GRAVITY_LIKE;
220         $arr['parent'] = $item['id'];
221         $arr['parent-uri'] = $item['uri'];
222         $arr['thr-parent'] = $item['uri'];
223         $arr['owner-name'] = $remote_owner['name'];
224         $arr['owner-link'] = $remote_owner['url'];
225         $arr['owner-avatar'] = $remote_owner['thumb'];
226         $arr['author-name'] = $contact['name'];
227         $arr['author-link'] = $contact['url'];
228         $arr['author-avatar'] = $contact['thumb'];
229         
230         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
231         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
232         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
233         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
234
235         $arr['verb'] = $activity;
236         $arr['object-type'] = $objtype;
237         $arr['object'] = $obj;
238         $arr['allow_cid'] = $item['allow_cid'];
239         $arr['allow_gid'] = $item['allow_gid'];
240         $arr['deny_cid'] = $item['deny_cid'];
241         $arr['deny_gid'] = $item['deny_gid'];
242         $arr['visible'] = 1;
243         $arr['unseen'] = 1;
244         $arr['last-child'] = 0;
245
246         $post_id = item_store($arr);
247
248         if(! $item['visible']) {
249                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
250                         intval($item['id']),
251                         intval($owner_uid)
252                 );
253         }
254
255
256         // Save the author information for the like in case we need to relay to Diaspora
257         store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
258
259         $arr['id'] = $post_id;
260
261         call_hooks('post_local_end', $arr);
262
263         proc_run('php',"include/notifier.php","like","$post_id");
264
265         like_content_return($a->get_baseurl(), $return_path);
266         killme(); // NOTREACHED
267 //      return; // NOTREACHED
268 }
269
270
271 // Decide how to return. If we were called with a 'return' argument,
272 // then redirect back to the calling page. If not, just quietly end
273
274 function like_content_return($baseurl, $return_path) {
275
276         if($return_path) {
277                 $rand = '_=' . time();
278                 if(strpos($return_path, '?')) $rand = "&$rand";
279                 else $rand = "?$rand";
280
281                 goaway($baseurl . "/" . $return_path . $rand);
282         }
283
284         killme();
285 }
286
287
288 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
289         // Note that we can only create a signature for a user of the local server. We don't have
290         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
291         // means we are the relay, and for relayable_retractions, Diaspora
292         // only checks the parent_author_signature if it doesn't have to relay further
293         //
294         // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
295         // likes on photos, so don't bother.
296
297         $enabled = intval(get_config('system','diaspora_enabled'));
298         if(! $enabled) {
299                 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
300                 return;
301         }
302
303         logger('mod_like: storing diaspora like retraction signature');
304
305         if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
306                 $signed_text = $like_item['guid'] . ';' . 'Like';
307
308                 // Only works for NETWORK_DFRN
309                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
310                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
311                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
312                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
313
314                 // Get contact's private key if he's a user of the local Friendica server
315                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
316                         dbesc($contact['url'])
317                 );
318
319                 if( $r) {
320                         $contact_uid = $r['uid'];
321                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
322                                 intval($contact_uid)
323                         );
324
325                         if( $r)
326                                 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
327                 }
328
329                 if(! isset($authorsig))
330                         $authorsig = '';
331
332                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
333                         intval($like_item['id']),
334                         dbesc($signed_text),
335                         dbesc($authorsig),
336                         dbesc($diaspora_handle)
337                 );
338         }
339
340         return;
341 }
342
343 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
344         // Note that we can only create a signature for a user of the local server. We don't have
345         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
346         // means we are the relay, and for relayable_retractions, Diaspora
347         // only checks the parent_author_signature if it doesn't have to relay further
348
349         $enabled = intval(get_config('system','diaspora_enabled'));
350         if(! $enabled) {
351                 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
352                 return;
353         }
354
355         logger('mod_like: storing diaspora like signature');
356
357         if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
358                 // Only works for NETWORK_DFRN
359                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
360                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
361                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
362                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
363
364                 // Get contact's private key if he's a user of the local Friendica server
365                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
366                         dbesc($contact['url'])
367                 );
368
369                 if( $r) {
370                         $contact_uid = $r['uid'];
371                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
372                                 intval($contact_uid)
373                         );
374
375                         if( $r)
376                                 $contact_uprvkey = $r['prvkey'];
377                 }
378
379                 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
380                         intval($post_id)
381                 );
382                 if( $r) {
383                         $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
384                                 intval($r[0]['parent']),
385                                 intval($r[0]['parent'])
386                         );
387                         if( $p) {
388                                 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
389
390                                 if(isset($contact_uprvkey))
391                                         $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
392                                 else
393                                         $authorsig = '';
394
395                                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
396                                         intval($post_id),
397                                         dbesc($signed_text),
398                                         dbesc($authorsig),
399                                         dbesc($diaspora_handle)
400                                 );
401                         }
402                 }
403         }
404
405         return;
406 }