require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
-
-function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
-
-
- $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
- $public_feed = (($dfrn_id) ? false : true);
- $starred = false; // not yet implemented, possible security issues
- $converse = false;
-
- if($public_feed && $a->argc > 2) {
- for($x = 2; $x < $a->argc; $x++) {
- if($a->argv[$x] == 'converse')
- $converse = true;
- if($a->argv[$x] == 'starred')
- $starred = true;
- if($a->argv[$x] === 'category' && $a->argc > ($x + 1) && strlen($a->argv[$x+1]))
- $category = $a->argv[$x+1];
- }
- }
-
-
-
- // default permissions - anonymous user
-
- $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' ";
-
- $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
- FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
- WHERE `contact`.`self` = 1 AND `user`.`nickname` = '%s' LIMIT 1",
- dbesc($owner_nick)
- );
-
- if(! count($r))
- killme();
-
- $owner = $r[0];
- $owner_id = $owner['user_uid'];
- $owner_nick = $owner['nickname'];
-
- $birthday = feed_birthday($owner_id,$owner['timezone']);
-
- $sql_post_table = "";
- $visibility = "";
-
- if(! $public_feed) {
-
- $sql_extra = '';
- switch($direction) {
- case (-1):
- $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
- $my_id = $dfrn_id;
- break;
- case 0:
- $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
- $my_id = '1:' . $dfrn_id;
- break;
- case 1:
- $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
- $my_id = '0:' . $dfrn_id;
- break;
- default:
- return false;
- break; // NOTREACHED
- }
-
- $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
- intval($owner_id)
- );
-
- if(! count($r))
- killme();
-
- $contact = $r[0];
- require_once('include/security.php');
- $groups = init_groups_visitor($contact['id']);
-
- if(count($groups)) {
- for($x = 0; $x < count($groups); $x ++)
- $groups[$x] = '<' . intval($groups[$x]) . '>' ;
- $gs = implode('|', $groups);
- }
- else
- $gs = '<<>>' ; // Impossible to match
-
- $sql_extra = sprintf("
- AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
- AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
- AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
- AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s')
- ",
- intval($contact['id']),
- intval($contact['id']),
- dbesc($gs),
- dbesc($gs)
- );
- }
-
- if($public_feed)
- $sort = 'DESC';
- else
- $sort = 'ASC';
-
- $date_field = "`changed`";
- $sql_order = "`item`.`parent` ".$sort.", `item`.`created` ASC";
-
- if(! strlen($last_update))
- $last_update = 'now -30 days';
-
- if(isset($category)) {
- $sql_post_table = sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
- dbesc(protect_sprintf($category)), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), intval($owner_id));
- //$sql_extra .= file_tag_file_query('item',$category,'category');
- }
-
- if($public_feed) {
- if(! $converse)
- $sql_extra .= " AND `contact`.`self` = 1 ";
- }
-
- $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
-
- // AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
- // dbesc($check_date),
-
- $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`,
- `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
- `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
- `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
- `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`,
- `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
- FROM `item` $sql_post_table
- INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
- AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
- LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
- WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`parent` != 0
- AND ((`item`.`wall` = 1) $visibility) AND `item`.$date_field > '%s'
- $sql_extra
- ORDER BY $sql_order LIMIT 0, 300",
- intval($owner_id),
- dbesc($check_date),
- dbesc($sort)
- );
-
- // Will check further below if this actually returned results.
- // We will provide an empty feed if that is the case.
-
- $items = $r;
-
- $feed_template = get_markup_template(($dfrn_id) ? 'atom_feed_dfrn.tpl' : 'atom_feed.tpl');
-
- $atom = '';
-
- $hubxml = feed_hublinks();
-
- $salmon = feed_salmonlinks($owner_nick);
-
- $alternatelink = $owner['url'];
-
- if(isset($category))
- $alternatelink .= "/category/".$category;
-
- $atom .= replace_macros($feed_template, array(
- '$version' => xmlify(FRIENDICA_VERSION),
- '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
- '$feed_title' => xmlify($owner['name']),
- '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 'now' , ATOM_TIME)) ,
- '$hub' => $hubxml,
- '$salmon' => $salmon,
- '$alternatelink' => xmlify($alternatelink),
- '$name' => xmlify($owner['name']),
- '$profile_page' => xmlify($owner['url']),
- '$photo' => xmlify($owner['photo']),
- '$thumb' => xmlify($owner['thumb']),
- '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
- '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , ATOM_TIME)) ,
- '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , ATOM_TIME)) ,
- '$birthday' => ((strlen($birthday)) ? '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>' : ''),
- '$community' => (($owner['page-flags'] == PAGE_COMMUNITY) ? '<dfrn:community>1</dfrn:community>' : '')
- ));
-
- call_hooks('atom_feed', $atom);
-
- if(! count($items)) {
-
- call_hooks('atom_feed_end', $atom);
-
- $atom .= '</feed>' . "\r\n";
- return $atom;
- }
-
- foreach($items as $item) {
-
- // prevent private email from leaking.
- if($item['network'] === NETWORK_MAIL)
- continue;
-
- // public feeds get html, our own nodes use bbcode
-
- if($public_feed) {
- $type = 'html';
- // catch any email that's in a public conversation and make sure it doesn't leak
- if($item['private'])
- continue;
- }
- else {
- $type = 'text';
- }
-
- $atom .= atom_entry($item,$type,null,$owner,true);
- }
-
- call_hooks('atom_feed_end', $atom);
-
- $atom .= '</feed>' . "\r\n";
-
- return $atom;
-}
-
-
function construct_verb($item) {
if($item['verb'])
return $item['verb'];
return ACTIVITY_POST;
}
-function construct_activity_object($item) {
-
- if($item['object']) {
- $o = '<as:object>' . "\r\n";
- $r = parse_xml_string($item['object'],false);
-
-
- if(! $r)
- return '';
- if($r->type)
- $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
- if($r->id)
- $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
- if($r->title)
- $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
- if($r->link) {
- if(substr($r->link,0,1) === '<') {
- // patch up some facebook "like" activity objects that got stored incorrectly
- // for a couple of months prior to 9-Jun-2011 and generated bad XML.
- // we can probably remove this hack here and in the following function in a few months time.
- if(strstr($r->link,'&') && (! strstr($r->link,'&')))
- $r->link = str_replace('&','&', $r->link);
- $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
- $o .= $r->link;
- }
- else
- $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
- }
- if($r->content)
- $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
- $o .= '</as:object>' . "\r\n";
- return $o;
- }
-
- return '';
-}
-
-function construct_activity_target($item) {
-
- if($item['target']) {
- $o = '<as:target>' . "\r\n";
- $r = parse_xml_string($item['target'],false);
- if(! $r)
- return '';
- if($r->type)
- $o .= '<as:object-type>' . xmlify($r->type) . '</as:object-type>' . "\r\n";
- if($r->id)
- $o .= '<id>' . xmlify($r->id) . '</id>' . "\r\n";
- if($r->title)
- $o .= '<title>' . xmlify($r->title) . '</title>' . "\r\n";
- if($r->link) {
- if(substr($r->link,0,1) === '<') {
- if(strstr($r->link,'&') && (! strstr($r->link,'&')))
- $r->link = str_replace('&','&', $r->link);
- $r->link = preg_replace('/\<link(.*?)\"\>/','<link$1"/>',$r->link);
- $o .= $r->link;
- }
- else
- $o .= '<link rel="alternate" type="text/html" href="' . xmlify($r->link) . '" />' . "\r\n";
- }
- if($r->content)
- $o .= '<content type="html" >' . xmlify(bbcode($r->content)) . '</content>' . "\r\n";
- $o .= '</as:target>' . "\r\n";
- return $o;
- }
-
- return '';
-}
-
/* limit_body_size()
*
* The purpose of this function is to apply system message length limits to
return($title == $body);
}
-
-
function get_atom_elements($feed, $item, $contact = array()) {
require_once('library/HTMLPurifier.auto.php');
}
}
-
function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
$a = get_app();
}
-
-function atom_author($tag,$name,$uri,$h,$w,$photo,$geo) {
- $o = '';
- if(! $tag)
- return $o;
- $name = xmlify($name);
- $uri = xmlify($uri);
- $h = intval($h);
- $w = intval($w);
- $photo = xmlify($photo);
-
-
- $o .= "<$tag>\r\n";
- $o .= "\t<name>$name</name>\r\n";
- $o .= "\t<uri>$uri</uri>\r\n";
- $o .= "\t".'<link rel="photo" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
- $o .= "\t".'<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
-
- if ($tag == "author") {
-
- if($geo)
- $o .= '<georss:point>'.xmlify($geo).'</georss:point>'."\r\n";
-
- $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
- `profile`.`name`, `profile`.`pub_keywords`, `profile`.`about`,
- `profile`.`homepage`,`contact`.`nick` FROM `profile`
- INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
- INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
- WHERE `profile`.`is-default` AND `contact`.`self` AND
- NOT `user`.`hidewall` AND `contact`.`nurl`='%s'",
- dbesc(normalise_link($uri)));
- if ($r) {
- $location = '';
- if($r[0]['locality'])
- $location .= $r[0]['locality'];
- if($r[0]['region']) {
- if($location)
- $location .= ', ';
- $location .= $r[0]['region'];
- }
- if($r[0]['country-name']) {
- if($location)
- $location .= ', ';
- $location .= $r[0]['country-name'];
- }
-
- $o .= "\t<poco:preferredUsername>".xmlify($r[0]["nick"])."</poco:preferredUsername>\r\n";
- $o .= "\t<poco:displayName>".xmlify($r[0]["name"])."</poco:displayName>\r\n";
- $o .= "\t<poco:note>".xmlify(bbcode($r[0]["about"]))."</poco:note>\r\n";
- $o .= "\t<poco:address>\r\n";
- $o .= "\t\t<poco:formatted>".xmlify($location)."</poco:formatted>\r\n";
- $o .= "\t</poco:address>\r\n";
- $o .= "\t<poco:urls>\r\n";
- $o .= "\t<poco:type>homepage</poco:type>\r\n";
- $o .= "\t\t<poco:value>".xmlify($r[0]["homepage"])."</poco:value>\r\n";
- $o .= "\t\t<poco:primary>true</poco:primary>\r\n";
- $o .= "\t</poco:urls>\r\n";
- }
- }
-
- call_hooks('atom_author', $o);
-
- $o .= "</$tag>\r\n";
- return $o;
-}
-
-function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
-
- $a = get_app();
-
- if(! $item['parent'])
- return;
-
- if($item['deleted'])
- return '<at:deleted-entry ref="' . xmlify($item['uri']) . '" when="' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '" />' . "\r\n";
-
-
- if($item['allow_cid'] || $item['allow_gid'] || $item['deny_cid'] || $item['deny_gid'])
- $body = fix_private_photos($item['body'],$owner['uid'],$item,$cid);
- else
- $body = $item['body'];
-
-
- $o = "\r\n\r\n<entry>\r\n";
-
- if(is_array($author))
- $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb'], $item['coord']);
- else
- $o .= atom_author('author',(($item['author-name']) ? $item['author-name'] : $item['name']),(($item['author-link']) ? $item['author-link'] : $item['url']),80,80,(($item['author-avatar']) ? $item['author-avatar'] : $item['thumb']), $item['coord']);
- if(strlen($item['owner-name']))
- $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar'], $item['coord']);
-
- if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
- $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
- $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
- $o .= '<thr:in-reply-to ref="'.xmlify($parent_item).'" type="text/html" href="'.xmlify($a->get_baseurl().'/display/'.$parent[0]['guid']).'" />'."\r\n";
- }
-
- $htmlbody = $body;
-
- if ($item['title'] != "")
- $htmlbody = "[b]".$item['title']."[/b]\n\n".$htmlbody;
-
- $htmlbody = bbcode($htmlbody, false, false, 7);
-
- $o .= '<id>' . xmlify($item['uri']) . '</id>' . "\r\n";
- $o .= '<title>' . xmlify($item['title']) . '</title>' . "\r\n";
- $o .= '<published>' . xmlify(datetime_convert('UTC','UTC',$item['created'] . '+00:00',ATOM_TIME)) . '</published>' . "\r\n";
- $o .= '<updated>' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '</updated>' . "\r\n";
- $o .= '<dfrn:env>' . base64url_encode($body, true) . '</dfrn:env>' . "\r\n";
- $o .= '<content type="' . $type . '" >' . xmlify((($type === 'html') ? $htmlbody : $body)) . '</content>' . "\r\n";
- $o .= '<link rel="alternate" type="text/html" href="'.xmlify($a->get_baseurl().'/display/'.$item['guid']).'" />'."\r\n";
-
- $o .= '<status_net notice_id="'.$item['id'].'"></status_net>'."\r\n";
-
- if($comment)
- $o .= '<dfrn:comment-allow>' . intval($item['last-child']) . '</dfrn:comment-allow>' . "\r\n";
-
- if($item['location']) {
- $o .= '<dfrn:location>' . xmlify($item['location']) . '</dfrn:location>' . "\r\n";
- $o .= '<poco:address><poco:formatted>' . xmlify($item['location']) . '</poco:formatted></poco:address>' . "\r\n";
- }
-
- if($item['coord'])
- $o .= '<georss:point>' . xmlify($item['coord']) . '</georss:point>' . "\r\n";
-
- if(($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid']))
- $o .= '<dfrn:private>' . (($item['private']) ? $item['private'] : 1) . '</dfrn:private>' . "\r\n";
-
- if($item['extid'])
- $o .= '<dfrn:extid>' . xmlify($item['extid']) . '</dfrn:extid>' . "\r\n";
- if($item['bookmark'])
- $o .= '<dfrn:bookmark>true</dfrn:bookmark>' . "\r\n";
-
- if($item['app'])
- $o .= '<statusnet:notice_info local_id="' . $item['id'] . '" source="' . xmlify($item['app']) . '" ></statusnet:notice_info>' . "\r\n";
-
- if($item['guid'])
- $o .= '<dfrn:diaspora_guid>' . $item['guid'] . '</dfrn:diaspora_guid>' . "\r\n";
-
- if($item['signed_text']) {
- $sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
- $o .= '<dfrn:diaspora_signature>' . xmlify($sign) . '</dfrn:diaspora_signature>' . "\r\n";
- }
-
- $verb = construct_verb($item);
- $o .= '<as:verb>' . xmlify($verb) . '</as:verb>' . "\r\n";
- $actobj = construct_activity_object($item);
- if(strlen($actobj))
- $o .= $actobj;
- $actarg = construct_activity_target($item);
- if(strlen($actarg))
- $o .= $actarg;
-
- $tags = item_getfeedtags($item);
- if(count($tags)) {
- foreach($tags as $t)
- if (($type != 'html') OR ($t[0] != "@"))
- $o .= '<category scheme="X-DFRN:' . xmlify($t[0]) . ':' . xmlify($t[1]) . '" term="' . xmlify($t[2]) . '" />' . "\r\n";
- }
-
- /// @TODO
- /// To support these elements, the API needs to be enhanced
- /// $o .= '<link rel="ostatus:conversation" href="'.xmlify($a->get_baseurl().'/display/'.$owner['nickname'].'/'.$item['parent']).'"/>'."\r\n";
- /// $o .= "\t".'<link rel="self" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
- /// $o .= "\t".'<link rel="edit" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
-
- // Deactivated since it was meant only for OStatus
- //$o .= item_get_attachment($item);
-
- $o .= item_getfeedattach($item);
-
- $mentioned = get_mentions($item);
- if($mentioned)
- $o .= $mentioned;
-
- call_hooks('atom_entry', $o);
-
- $o .= '</entry>' . "\r\n";
-
- return $o;
-}
-
function fix_private_photos($s, $uid, $item = null, $cid = 0) {
if(get_config('system','disable_embedded'))
return($new_body);
}
-
function has_permissions($obj) {
if(($obj['allow_cid'] != '') || ($obj['allow_gid'] != '') || ($obj['deny_cid'] != '') || ($obj['deny_gid'] != ''))
return true;
return $ret;
}
-function item_get_attachment($item) {
- $o = "";
- $siteinfo = get_attached_data($item["body"]);
-
- switch($siteinfo["type"]) {
- case 'link':
- $o = '<link rel="enclosure" href="'.xmlify($siteinfo["url"]).'" type="text/html; charset=UTF-8" length="" title="'.xmlify($siteinfo["title"]).'"/>'."\r\n";
- break;
- case 'photo':
- $imgdata = get_photo_info($siteinfo["image"]);
- $o = '<link rel="enclosure" href="'.xmlify($siteinfo["image"]).'" type="'.$imgdata["mime"].'" length="'.$imgdata["size"].'"/>'."\r\n";
- break;
- case 'video':
- $o = '<link rel="enclosure" href="'.xmlify($siteinfo["url"]).'" type="text/html; charset=UTF-8" length="" title="'.xmlify($siteinfo["title"]).'"/>'."\r\n";
- break;
- default:
- break;
- }
-
- return $o;
-}
-
-function item_getfeedattach($item) {
- $ret = '';
- $arr = explode('[/attach],',$item['attach']);
- if(count($arr)) {
- foreach($arr as $r) {
- $matches = false;
- $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches);
- if($cnt) {
- $ret .= '<link rel="enclosure" href="' . xmlify($matches[1]) . '" type="' . xmlify($matches[3]) . '" ';
- if(intval($matches[2]))
- $ret .= 'length="' . intval($matches[2]) . '" ';
- if($matches[4] !== ' ')
- $ret .= 'title="' . xmlify(trim($matches[4])) . '" ';
- $ret .= ' />' . "\r\n";
- }
- }
- }
- return $ret;
-}
-
-
-
function item_expire($uid, $days, $network = "", $force = false) {
if((! $uid) || ($days < 1))