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;
35 $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
37 logger('like: verb ' . $verb . ' item ' . $item_id);
40 $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
45 if(! $item_id || (! count($r))) {
46 logger('like: no item ' . $item_id);
52 $owner_uid = $item['uid'];
54 if(! can_write_wall($a,$owner_uid)) {
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']),
69 $remote_owner = $r[0];
72 // this represents the post owner on this system.
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",
82 logger('like: no owner');
87 $remote_owner = $owner;
90 // This represents the person posting
92 if((local_user()) && (local_user() == $owner_uid)) {
96 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
97 intval($_SESSION['visitor_id']),
108 $r = q("SELECT * FROM `item` WHERE `verb` = '%s' AND `deleted` = 0
109 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
111 intval($contact['id']),
119 // Already voted, undo it
120 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
121 dbesc(datetime_convert()),
122 intval($like_item['id'])
126 // Clean up the Diaspora signatures for this like
127 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
128 // if it had been enabled in the past
129 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
130 intval($like_item['id'])
133 // Save the author information for the unlike in case we need to relay to Diaspora
134 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
137 // proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
138 $like_item_id = $like_item['id'];
139 proc_run('php',"include/notifier.php","like","$like_item_id");
143 $uri = item_new_uri($a->get_hostname(),$owner_uid);
145 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
146 $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
147 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
148 $body = $item['body'];
153 <type>$objtype</type>
155 <id>{$item['uri']}</id>
158 <content>$body</content>
162 $bodyverb = t('%1$s likes %2$s\'s %3$s');
163 if($verb === 'dislike')
164 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
166 if(! isset($bodyverb))
172 $arr['uid'] = $owner_uid;
173 $arr['contact-id'] = $contact['id'];
174 $arr['type'] = 'activity';
175 $arr['wall'] = $item['wall'];
177 $arr['gravity'] = GRAVITY_LIKE;
178 $arr['parent'] = $item['id'];
179 $arr['parent-uri'] = $item['uri'];
180 $arr['thr-parent'] = $item['uri'];
181 $arr['owner-name'] = $remote_owner['name'];
182 $arr['owner-link'] = $remote_owner['url'];
183 $arr['owner-avatar'] = $remote_owner['thumb'];
184 $arr['author-name'] = $contact['name'];
185 $arr['author-link'] = $contact['url'];
186 $arr['author-avatar'] = $contact['thumb'];
188 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
189 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
190 $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
191 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
193 $arr['verb'] = $activity;
194 $arr['object-type'] = $objtype;
195 $arr['object'] = $obj;
196 $arr['allow_cid'] = $item['allow_cid'];
197 $arr['allow_gid'] = $item['allow_gid'];
198 $arr['deny_cid'] = $item['deny_cid'];
199 $arr['deny_gid'] = $item['deny_gid'];
202 $arr['last-child'] = 0;
204 $post_id = item_store($arr);
206 if(! $item['visible']) {
207 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
214 // Save the author information for the like in case we need to relay to Diaspora
215 store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
218 $arr['id'] = $post_id;
220 call_hooks('post_local_end', $arr);
222 proc_run('php',"include/notifier.php","like","$post_id");
225 // return; // NOTREACHED
229 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
230 // Note that we can only create a signature for a user of the local server. We don't have
231 // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it
232 // means we are the relay, and for relayable_retractions, Diaspora
233 // only checks the parent_author_signature if it doesn't have to relay further
235 // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
236 // likes on photos, so don't bother.
238 $enabled = intval(get_config('system','diaspora_enabled'));
240 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
244 logger('mod_like: storing diaspora like retraction signature');
246 if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
247 $signed_text = $like_item['guid'] . ';' . 'Like';
249 // Only works for NETWORK_DFRN
250 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
251 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
252 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
253 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
255 // Get contact's private key if he's a user of the local Friendica server
256 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
257 dbesc($contact['url'])
261 $contact_uid = $r['uid'];
262 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
267 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
270 if(! isset($authorsig))
273 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
274 intval($like_item['id']),
277 dbesc($diaspora_handle)
284 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
285 // Note that we can only create a signature for a user of the local server. We don't have
286 // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it
287 // means we are the relay, and for relayable_retractions, Diaspora
288 // only checks the parent_author_signature if it doesn't have to relay further
290 $enabled = intval(get_config('system','diaspora_enabled'));
292 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
296 logger('mod_like: storing diaspora like signature');
298 if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
299 // Only works for NETWORK_DFRN
300 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
301 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
302 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
303 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
305 // Get contact's private key if he's a user of the local Friendica server
306 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
307 dbesc($contact['url'])
311 $contact_uid = $r['uid'];
312 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
317 $contact_uprvkey = $r['prvkey'];
320 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
324 $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
325 intval($r[0]['parent']),
326 intval($r[0]['parent'])
329 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
331 if(isset($contact_uprvkey))
332 $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
336 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
340 dbesc($diaspora_handle)