X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fitems.php;h=5969466813a0d1b406a8319c3802abfc4ef6a007;hb=b8479cc2f98bdd3b144cfda127c799ba4740fc83;hp=5d9eaaaf3fb0f1b738d029229bfc8523499b77c8;hpb=9f5d3000c74253a1284abda16006d74df84073fe;p=friendica.git diff --git a/include/items.php b/include/items.php index 5d9eaaaf3f..5969466813 100644 --- a/include/items.php +++ b/include/items.php @@ -180,7 +180,7 @@ function construct_activity_object($item) { if($item['object']) { $o = '' . "\r\n"; - $r = @simplexml_load_string($item['object']); + $r = parse_xml_string($item['object']); if($r->type) $o .= '' . xmlify($r->type) . '' . "\r\n"; if($r->id) @@ -206,7 +206,7 @@ function construct_activity_target($item) { if($item['target']) { $o = '' . "\r\n"; - $r = @simplexml_load_string($item['target']); + $r = parse_xml_string($item['target']); if($r->type) $o .= '' . xmlify($r->type) . '' . "\r\n"; if($r->id) @@ -241,12 +241,18 @@ function get_atom_elements($feed,$item) { $res = array(); $author = $item->get_author(); - $res['author-name'] = unxmlify($author->get_name()); - $res['author-link'] = unxmlify($author->get_link()); + if($author) { + $res['author-name'] = unxmlify($author->get_name()); + $res['author-link'] = unxmlify($author->get_link()); + } + else { + $res['author-name'] = unxmlify($feed->get_title()); + $res['author-link'] = unxmlify($feed->get_permalink()); + } $res['uri'] = unxmlify($item->get_id()); $res['title'] = unxmlify($item->get_title()); $res['body'] = unxmlify($item->get_content()); - + $res['plink'] = unxmlify($item->get_link(0)); // look for a photo. We should check media size and find the best one, // but for now let's just find any author photo @@ -343,14 +349,13 @@ function get_atom_elements($feed,$item) { // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining // html. - if((strpos($res['body'],'<') !== false) || (strpos($res['body'],'>') !== false)) { $res['body'] = preg_replace('#]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?#s', '[youtube]$1[/youtube]', $res['body']); $res['body'] = oembed_html2bbcode($res['body']); - + $config = HTMLPurifier_Config::createDefault(); $config->set('Cache.DefinitionImpl', null); @@ -363,7 +368,7 @@ function get_atom_elements($feed,$item) { $res['body'] = html2bbcode($res['body']); } - + $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow'); if($allow && $allow[0]['data'] == 1) $res['last-child'] = 1; @@ -401,6 +406,17 @@ function get_atom_elements($feed,$item) { $res['edited'] = $item->get_date('c'); + // Disallow time travelling posts + + $d1 = strtotime($res['created']); + $d2 = strtotime($res['edited']); + $d3 = strtotime('now'); + + if($d1 > $d3) + $res['created'] = datetime_convert(); + if($d2 > $d3) + $res['edited'] = datetime_convert(); + $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner'); if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']) $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']); @@ -440,7 +456,45 @@ function get_atom_elements($feed,$item) { if((x($res,'verb')) && ($res['verb'] === 'http://ostatus.org/schema/1.0/unfollow')) $res['verb'] = ACTIVITY_UNFOLLOW; - + + $cats = $item->get_categories(); + if($cats) { + $tag_arr = array(); + foreach($cats as $cat) { + $term = $cat->get_term(); + if(! $term) + $term = $cat->get_label(); + $scheme = $cat->get_scheme(); + if($scheme && $term && stristr($scheme,'X-DFRN:')) + $tag_arr[] = substr($scheme,7,1) . '[url=' . unxmlify(substr($scheme,9)) . ']' . unxmlify($term) . '[/url]'; + elseif($term) + $tag_arr[] = notags(trim($term)); + } + $res['tag'] = implode(',', $tag_arr); + } + + $attach = $item->get_enclosures(); + if($attach) { + $att_arr = array(); + foreach($attach as $att) { + $len = intval($att->get_length()); + $link = str_replace(',','%2D', notags(trim($att->get_link()))); + $title = str_replace(',','%2D',notags(trim($att->get_title()))); + $type = notags(trim($att->get_type())); + if((! $link) || (strpos($link,'http') !== 0)) + continue; + + if(! $title) + $title = ' '; + if(! $type) + $type = 'application/octet-stream'; + + // this isn't legal html - there is no size in an 'a' tag, remember to strip it before display + + $att_arr[] = '' . $title . ''; + } + $res['attach'] = implode(',', $att_arr); + } $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object'); @@ -591,12 +645,15 @@ function item_store($arr,$force_parent = false) { $arr['object'] = ((x($arr,'object')) ? trim($arr['object']) : ''); $arr['target-type'] = ((x($arr,'target-type')) ? notags(trim($arr['target-type'])) : ''); $arr['target'] = ((x($arr,'target')) ? trim($arr['target']) : ''); + $arr['plink'] = ((x($arr,'plink')) ? notags(trim($arr['plink'])) : ''); $arr['allow_cid'] = ((x($arr,'allow_cid')) ? trim($arr['allow_cid']) : ''); $arr['allow_gid'] = ((x($arr,'allow_gid')) ? trim($arr['allow_gid']) : ''); $arr['deny_cid'] = ((x($arr,'deny_cid')) ? trim($arr['deny_cid']) : ''); $arr['deny_gid'] = ((x($arr,'deny_gid')) ? trim($arr['deny_gid']) : ''); $arr['private'] = ((x($arr,'private')) ? intval($arr['private']) : 0 ); $arr['body'] = ((x($arr,'body')) ? trim($arr['body']) : ''); + $arr['tag'] = ((x($arr,'tag')) ? notags(trim($arr['tag'])) : ''); + $arr['attach'] = ((x($arr,'attach')) ? notags(trim($arr['attach'])) : ''); if($arr['parent-uri'] === $arr['uri']) { $parent_id = 0; @@ -608,6 +665,7 @@ function item_store($arr,$force_parent = false) { else { // find the parent and snarf the item id and ACL's + // and anything else we need to inherit $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($arr['parent-uri']), @@ -631,6 +689,7 @@ function item_store($arr,$force_parent = false) { $allow_gid = $r[0]['allow_gid']; $deny_cid = $r[0]['deny_cid']; $deny_gid = $r[0]['deny_gid']; + $arr['wall'] = $r[0]['wall']; } else { @@ -699,6 +758,18 @@ function item_store($arr,$force_parent = false) { intval($current_post) ); + /** + * If this is now the last-child, force all _other_ children of this parent to *not* be last-child + */ + + if($arr['last-child']) { + $r = q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d AND `id` != %d", + dbesc($arr['uri']), + intval($arr['uid']), + intval($current_post) + ); + } + return $current_post; } @@ -736,7 +807,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) { if(! $rino_enable) $rino = 0; - $url = $contact['notify'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : ''); + $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : ''); logger('dfrn_deliver: ' . $url); @@ -757,7 +828,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) { return 3; } - $res = simplexml_load_string($xml); + $res = parse_xml_string($xml); if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) return (($res->status) ? $res->status : 3); @@ -852,7 +923,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) { return 3; } - $res = simplexml_load_string($xml); + $res = parse_xml_string($xml); return $res->status; @@ -875,7 +946,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) { * */ -function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { +function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $secure_feed = false) { require_once('simplepie/simplepie.inc'); @@ -890,6 +961,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { if($feed->error()) logger('consume_feed: Error parsing XML: ' . $feed->error()); + $permalink = $feed->get_permalink(); // Check at the feed level for updated contact name and/or photo @@ -1031,25 +1103,18 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { } - // Now process the feed - if($feed->get_item_quantity()) { - - // in inverse date order - if ($datedir) - $items = array_reverse($feed->get_items()); - else - $items = $feed->get_items(); - foreach($items as $item) { + // process any deleted entries + $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry'); + if(is_array($del_entries) && count($del_entries)) { + foreach($del_entries as $dentry) { $deleted = false; - - $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry'); - if(isset($rawdelete[0]['attribs']['']['ref'])) { - $uri = $rawthread[0]['attribs']['']['ref']; + if(isset($dentry['attribs']['']['ref'])) { + $uri = $dentry['attribs']['']['ref']; $deleted = true; - if(isset($rawdelete[0]['attribs']['']['when'])) { - $when = $rawthread[0]['attribs']['']['when']; + if(isset($dentry['attribs']['']['when'])) { + $when = $dentry['attribs']['']['when']; $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s'); } else @@ -1063,6 +1128,10 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { ); if(count($r)) { $item = $r[0]; + + if(! $item['deleted']) + logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG); + if($item['uri'] == $item['parent-uri']) { $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '', `title` = '' @@ -1084,7 +1153,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { ); if($item['last-child']) { // ensure that last-child is set in case the comment that had it just got wiped. - $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", + q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", dbesc(datetime_convert()), dbesc($item['parent-uri']), intval($item['uid']) @@ -1103,9 +1172,24 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { } } } - continue; } + } + } + + // Now process the feed + + if($feed->get_item_quantity()) { + logger('consume_feed: feed item count = ' . $feed->get_item_quantity()); + + // in inverse date order + if ($datedir) + $items = array_reverse($feed->get_items()); + else + $items = $feed->get_items(); + + + foreach($items as $item) { $is_reply = false; $item_id = $item->get_id(); @@ -1115,19 +1199,32 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { $parent_uri = $rawthread[0]['attribs']['']['ref']; } - if(($is_reply) && is_array($contact)) { - + // Have we seen it? If not, import it. - $item_id = $item->get_id(); - - $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + $item_id = $item->get_id(); + $datarray = get_atom_elements($feed,$item); + + $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['uid']) ); - // FIXME update content if 'updated' changes + + // Update content if 'updated' changes + if(count($r)) { + if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) { + $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + dbesc($datarray['body']), + dbesc(datetime_convert('UTC','UTC',$datarray['edited'])), + dbesc($item_id), + intval($importer['uid']) + ); + } + + // update last-child if it changes + $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow'); if(($allow) && ($allow[0]['data'] != $r[0]['last-child'])) { $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d", @@ -1144,7 +1241,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { } continue; } - $datarray = get_atom_elements($feed,$item); + $force_parent = false; if($contact['network'] === 'stat') { $force_parent = true; @@ -1175,14 +1272,32 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { } else { + // Head post of a conversation. Have we seen it? If not, import it. - $item_id = $item->get_id(); - $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + $item_id = $item->get_id(); + + $datarray = get_atom_elements($feed,$item); + + $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['uid']) ); + + // Update content if 'updated' changes + if(count($r)) { + if((x($datarray,'edited') !== false) && (datetime_convert('UTC','UTC',$datarray['edited']) !== $r[0]['edited'])) { + $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + dbesc($datarray['body']), + dbesc(datetime_convert('UTC','UTC',$datarray['edited'])), + dbesc($item_id), + intval($importer['uid']) + ); + } + + // update last-child if it changes + $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow'); if($allow && $allow[0]['data'] != $r[0]['last-child']) { $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", @@ -1194,7 +1309,6 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { } continue; } - $datarray = get_atom_elements($feed,$item); if(activity_match($datarray['verb'],ACTIVITY_FOLLOW)) { logger('consume-feed: New follower'); @@ -1208,7 +1322,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0) { if(! is_array($contact)) return; - if($contact['network'] === 'stat') { + if($contact['network'] === 'stat' || stristr($permalink,'twitter.com')) { if(strlen($datarray['title'])) unset($datarray['title']); $datarray['last-child'] = 1; @@ -1405,7 +1519,7 @@ function atom_entry($item,$type,$author,$owner,$comment = false) { $o .= '' . xmlify(datetime_convert('UTC','UTC',$item['edited'] . '+00:00',ATOM_TIME)) . '' . "\r\n"; $o .= '' . base64url_encode($item['body'], true) . '' . "\r\n"; $o .= '' . xmlify(($type === 'html') ? bbcode($item['body']) : $item['body']) . '' . "\r\n"; - $o .= '' . "\r\n"; + $o .= '' . "\r\n"; if($comment) $o .= '' . intval($item['last-child']) . '' . "\r\n"; @@ -1429,6 +1543,15 @@ function atom_entry($item,$type,$author,$owner,$comment = false) { if(strlen($actarg)) $o .= $actarg; + $tags = item_getfeedtags($item); + if(count($tags)) { + foreach($tags as $t) { + $o .= '' . "\r\n"; + } + } + + $o .= item_getfeedattach($item); + $mentioned = get_mentions($item); if($mentioned) $o .= $mentioned; @@ -1439,4 +1562,93 @@ function atom_entry($item,$type,$author,$owner,$comment = false) { return $o; } + +function item_getfeedtags($item) { + $ret = array(); + $matches = false; + $cnt = preg_match_all('|\#\[url\=(.+?)\](.+?)\[\/url\]|',$item['tag'],$matches); + if($cnt) { + for($x = 0; $x < count($matches); $x ++) { + if($matches[1][$x]) + $ret[] = array('#',$matches[1][$x], $matches[2][$x]); + } + } + $matches = false; + $cnt = preg_match_all('|\@\[url\=(.+?)\](.+?)\[\/url\]|',$item['tag'],$matches); + if($cnt) { + for($x = 0; $x < count($matches); $x ++) { + if($matches[1][$x]) + $ret[] = array('#',$matches[1][$x], $matches[2][$x]); + } + } + return $ret; +} + +function item_getfeedattach($item) { + $ret = ''; + $arr = explode(',',$item['attach']); + if(count($arr)) { + foreach($arr as $r) { + $matches = false; + $cnt = preg_match('|\(.+?)|',$r,$matches); + if($cnt) { + $ret .= '