3 require_once('include/security.php');
4 require_once('include/bbcode.php');
5 require_once('include/items.php');
8 function like_content(&$a) {
10 if(! local_user() && ! remote_user()) {
14 $verb = notags(trim($_GET['verb']));
23 $activity = ACTIVITY_LIKE;
27 $activity = ACTIVITY_DISLIKE;
31 $activity = ACTIVITY_AGREE;
35 $activity = ACTIVITY_DISAGREE;
39 $activity = ACTIVITY_ABSTAIN;
43 $activity = ACTIVITY_ATTEND;
47 $activity = ACTIVITY_ATTENDNO;
51 $activity = ACTIVITY_ATTENDMAYBE;
59 $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
61 logger('like: verb ' . $verb . ' item ' . $item_id);
64 $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
69 if(! $item_id || (! count($r))) {
70 logger('like: no item ' . $item_id);
76 $owner_uid = $item['uid'];
78 if(! can_write_wall($a,$owner_uid)) {
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']),
93 $remote_owner = $r[0];
96 // this represents the post owner on this system.
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",
106 logger('like: no owner');
111 $remote_owner = $owner;
114 // This represents the person posting
116 if((local_user()) && (local_user() == $owner_uid)) {
120 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
121 intval($_SESSION['visitor_id']),
132 // See if we've been passed a return path to redirect to
133 $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
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'])
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'])
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'])
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);
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");
167 like_content_return($a->get_baseurl(), $return_path);
168 return; // NOTREACHED
171 $uri = item_new_uri($a->get_hostname(),$owner_uid);
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'];
183 <type>$objtype</type>
185 <id>{$item['uri']}</id>
188 <content>$body</content>
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');
208 if(! isset($bodyverb))
214 $arr['uid'] = $owner_uid;
215 $arr['contact-id'] = $contact['id'];
216 $arr['type'] = 'activity';
217 $arr['wall'] = $item['wall'];
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'];
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 );
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'];
244 $arr['last-child'] = 0;
246 $post_id = item_store($arr);
248 if(! $item['visible']) {
249 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
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);
259 $arr['id'] = $post_id;
261 call_hooks('post_local_end', $arr);
263 proc_run('php',"include/notifier.php","like","$post_id");
265 like_content_return($a->get_baseurl(), $return_path);
266 killme(); // NOTREACHED
267 // return; // NOTREACHED
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
274 function like_content_return($baseurl, $return_path) {
277 $rand = '_=' . time();
278 if(strpos($return_path, '?')) $rand = "&$rand";
279 else $rand = "?$rand";
281 goaway($baseurl . "/" . $return_path . $rand);
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
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.
297 $enabled = intval(get_config('system','diaspora_enabled'));
299 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
303 logger('mod_like: storing diaspora like retraction signature');
305 if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
306 $signed_text = $like_item['guid'] . ';' . 'Like';
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;
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'])
320 $contact_uid = $r['uid'];
321 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
326 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
329 if(! isset($authorsig))
332 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
333 intval($like_item['id']),
336 dbesc($diaspora_handle)
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
349 $enabled = intval(get_config('system','diaspora_enabled'));
351 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
355 logger('mod_like: storing diaspora like signature');
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;
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'])
370 $contact_uid = $r['uid'];
371 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
376 $contact_uprvkey = $r['prvkey'];
379 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
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'])
388 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
390 if(isset($contact_uprvkey))
391 $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
395 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
399 dbesc($diaspora_handle)