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