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