Beispiel #1
0
function get_atom_elements($feed, $item, $contact = array())
{
    require_once 'library/HTMLPurifier.auto.php';
    require_once 'include/html2bbcode.php';
    $best_photo = array();
    $res = array();
    $author = $item->get_author();
    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));
    if (isset($contact["network"]) and $contact["network"] == NETWORK_FEED and strstr($res['plink'], ".app.net/")) {
        logger("get_atom_elements: detected app.net posting: " . print_r($res, true), LOGGER_DEBUG);
        $res['title'] = "";
        $res['body'] = nl2br($res['body']);
    }
    // removing the content of the title if its identically to the body
    // This helps with auto generated titles e.g. from tumblr
    if (title_is_body($res["title"], $res["body"])) {
        $res['title'] = "";
    }
    if ($res['plink']) {
        $base_url = implode('/', array_slice(explode('/', $res['plink']), 0, 3));
    } else {
        $base_url = '';
    }
    // look for a photo. We should check media size and find the best one,
    // but for now let's just find any author photo
    // Additionally we look for an alternate author link. On OStatus this one is the one we want.
    $authorlinks = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
    if (is_array($authorlinks)) {
        foreach ($authorlinks as $link) {
            $linkdata = array_shift($link["attribs"]);
            if ($linkdata["rel"] == "alternate") {
                $res["author-link"] = $linkdata["href"];
            }
        }
    }
    $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
    if ($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
        $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        foreach ($base as $link) {
            if ($link['attribs']['']['rel'] === 'alternate') {
                $res['author-link'] = unxmlify($link['attribs']['']['href']);
            }
            if (!x($res, 'author-avatar') || !$res['author-avatar']) {
                if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                    $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
                }
            }
        }
    }
    $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
    if ($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'], ACTIVITY_OBJ_PERSON)) {
        $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        if ($base && count($base)) {
            foreach ($base as $link) {
                if ($link['attribs']['']['rel'] === 'alternate' && !$res['author-link']) {
                    $res['author-link'] = unxmlify($link['attribs']['']['href']);
                }
                if (!x($res, 'author-avatar') || !$res['author-avatar']) {
                    if ($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo') {
                        $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
                    }
                }
            }
        }
    }
    // No photo/profile-link on the item - look at the feed level
    if (!x($res, 'author-link') || !x($res, 'author-avatar')) {
        $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
        if ($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
            foreach ($base as $link) {
                if ($link['attribs']['']['rel'] === 'alternate' && !$res['author-link']) {
                    $res['author-link'] = unxmlify($link['attribs']['']['href']);
                }
                if (!$res['author-avatar']) {
                    if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                        $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
                    }
                }
            }
        }
        $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
        if ($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'], ACTIVITY_OBJ_PERSON)) {
            $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
            if ($base && count($base)) {
                foreach ($base as $link) {
                    if ($link['attribs']['']['rel'] === 'alternate' && !$res['author-link']) {
                        $res['author-link'] = unxmlify($link['attribs']['']['href']);
                    }
                    if (!x($res, 'author-avatar')) {
                        if ($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo') {
                            $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
                        }
                    }
                }
            }
        }
    }
    $apps = $item->get_item_tags(NAMESPACE_STATUSNET, 'notice_info');
    if ($apps && $apps[0]['attribs']['']['source']) {
        $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
        if ($res['app'] === 'web') {
            $res['app'] = 'OStatus';
        }
    }
    // base64 encoded json structure representing Diaspora signature
    $dsig = $item->get_item_tags(NAMESPACE_DFRN, 'diaspora_signature');
    if ($dsig) {
        $res['dsprsig'] = unxmlify($dsig[0]['data']);
    }
    $dguid = $item->get_item_tags(NAMESPACE_DFRN, 'diaspora_guid');
    if ($dguid) {
        $res['guid'] = unxmlify($dguid[0]['data']);
    }
    $bm = $item->get_item_tags(NAMESPACE_DFRN, 'bookmark');
    if ($bm) {
        $res['bookmark'] = unxmlify($bm[0]['data']) === 'true' ? 1 : 0;
    }
    /**
     * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
     */
    $have_real_body = false;
    $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
    if ($rawenv) {
        $have_real_body = true;
        $res['body'] = $rawenv[0]['data'];
        $res['body'] = str_replace(array(' ', "\t", "\r", "\n"), array('', '', '', ''), $res['body']);
        // make sure nobody is trying to sneak some html tags by us
        $res['body'] = notags(base64url_decode($res['body']));
    }
    $res['body'] = limit_body_size($res['body']);
    // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
    // the content type. Our own network only emits text normally, though it might have been converted to
    // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
    // have to assume it is all html and needs to be purified.
    // It doesn't matter all that much security wise - because before this content is used anywhere, we are
    // going to escape any tags we find regardless, but this lets us import a limited subset of html from
    // 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'] = reltoabs($res['body'], $base_url);
        $res['body'] = html2bb_video($res['body']);
        $res['body'] = oembed_html2bbcode($res['body']);
        $config = HTMLPurifier_Config::createDefault();
        $config->set('Cache.DefinitionImpl', null);
        // we shouldn't need a whitelist, because the bbcode converter
        // will strip out any unsupported tags.
        $purifier = new HTMLPurifier($config);
        $res['body'] = $purifier->purify($res['body']);
        $res['body'] = @html2bbcode($res['body']);
    } elseif (!$have_real_body) {
        // it's not one of our messages and it has no tags
        // so it's probably just text. We'll escape it just to be safe.
        $res['body'] = escape_tags($res['body']);
    }
    // this tag is obsolete but we keep it for really old sites
    $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
    if ($allow && $allow[0]['data'] == 1) {
        $res['last-child'] = 1;
    } else {
        $res['last-child'] = 0;
    }
    $private = $item->get_item_tags(NAMESPACE_DFRN, 'private');
    if ($private && intval($private[0]['data']) > 0) {
        $res['private'] = intval($private[0]['data']);
    } else {
        $res['private'] = 0;
    }
    $extid = $item->get_item_tags(NAMESPACE_DFRN, 'extid');
    if ($extid && $extid[0]['data']) {
        $res['extid'] = $extid[0]['data'];
    }
    $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
    if ($rawlocation) {
        $res['location'] = unxmlify($rawlocation[0]['data']);
    }
    $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'published');
    if ($rawcreated) {
        $res['created'] = unxmlify($rawcreated[0]['data']);
    }
    $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated');
    if ($rawedited) {
        $res['edited'] = unxmlify($rawedited[0]['data']);
    }
    if (x($res, 'edited') && !x($res, 'created')) {
        $res['created'] = $res['edited'];
    }
    if (!$res['created']) {
        $res['created'] = $item->get_date('c');
    }
    if (!$res['edited']) {
        $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']);
    } elseif ($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']) {
        $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
    }
    if ($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']) {
        $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
    } elseif ($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']) {
        $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
    }
    if ($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
        $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        foreach ($base as $link) {
            if (!x($res, 'owner-avatar') || !$res['owner-avatar']) {
                if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                    $res['owner-avatar'] = unxmlify($link['attribs']['']['href']);
                }
            }
        }
    }
    $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS, 'point');
    if ($rawgeo) {
        $res['coord'] = unxmlify($rawgeo[0]['data']);
    }
    if ($contact["network"] == NETWORK_FEED) {
        $res['verb'] = ACTIVITY_POST;
        $res['object-type'] = ACTIVITY_OBJ_NOTE;
    }
    $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
    // select between supported verbs
    if ($rawverb) {
        $res['verb'] = unxmlify($rawverb[0]['data']);
    }
    // translate OStatus unfollow to activity streams if it happened to get selected
    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(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_link()))));
            $title = str_replace(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_title()))));
            $type = str_replace(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_type()))));
            if (strpos($type, ';')) {
                $type = substr($type, 0, strpos($type, ';'));
            }
            if (!$link || strpos($link, 'http') !== 0) {
                continue;
            }
            if (!$title) {
                $title = ' ';
            }
            if (!$type) {
                $type = 'application/octet-stream';
            }
            $att_arr[] = '[attach]href="' . $link . '" length="' . $len . '" type="' . $type . '" title="' . $title . '"[/attach]';
        }
        $res['attach'] = implode(',', $att_arr);
    }
    $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
    if ($rawobj) {
        $res['object'] = '<object>' . "\n";
        $child = $rawobj[0]['child'];
        if ($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
            $res['object-type'] = $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'];
            $res['object'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data']) {
            $res['object'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $res['object'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data']) {
            $res['object'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
            $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
            if (!$body) {
                $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
            }
            // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
            $res['object'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
            if (strpos($body, '<') !== false || strpos($body, '>') !== false) {
                $body = html2bb_video($body);
                $config = HTMLPurifier_Config::createDefault();
                $config->set('Cache.DefinitionImpl', null);
                $purifier = new HTMLPurifier($config);
                $body = $purifier->purify($body);
                $body = html2bbcode($body);
            }
            $res['object'] .= '<content>' . $body . '</content>' . "\n";
        }
        $res['object'] .= '</object>' . "\n";
    }
    $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
    if ($rawobj) {
        $res['target'] = '<target>' . "\n";
        $child = $rawobj[0]['child'];
        if ($child[NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
            $res['target'] .= '<type>' . $child[NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data']) {
            $res['target'] .= '<id>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $res['target'] .= '<link>' . encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) . '</link>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data']) {
            $res['target'] .= '<title>' . $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'data') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
            $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
            if (!$body) {
                $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
            }
            // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
            $res['target'] .= '<orig>' . xmlify($body) . '</orig>' . "\n";
            if (strpos($body, '<') !== false || strpos($body, '>') !== false) {
                $body = html2bb_video($body);
                $config = HTMLPurifier_Config::createDefault();
                $config->set('Cache.DefinitionImpl', null);
                $purifier = new HTMLPurifier($config);
                $body = $purifier->purify($body);
                $body = html2bbcode($body);
            }
            $res['target'] .= '<content>' . $body . '</content>' . "\n";
        }
        $res['target'] .= '</target>' . "\n";
    }
    // This is some experimental stuff. By now retweets are shown with "RT:"
    // But: There is data so that the message could be shown similar to native retweets
    // There is some better way to parse this array - but it didn't worked for me.
    $child = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["entry"][0]["child"]["http://activitystrea.ms/spec/1.0/"][object][0]["child"];
    if (is_array($child)) {
        logger('get_atom_elements: Looking for status.net repeated message');
        $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
        $orig_id = ostatus_convert_href($child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"]);
        $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
        $uri = $author["uri"][0]["data"];
        $name = $author["name"][0]["data"];
        $avatar = @array_shift($author["link"][2]["attribs"]);
        $avatar = $avatar["href"];
        if ($name != "" and $uri != "" and $avatar != "" and $message != "") {
            logger('get_atom_elements: fixing sender of repeated message. ' . $orig_id, LOGGER_DEBUG);
            if (!intval(get_config('system', 'wall-to-wall_share'))) {
                $prefix = share_header($name, $uri, $avatar, "", "", $orig_link);
                $res["body"] = $prefix . html2bbcode($message) . "[/share]";
            } else {
                $res["owner-name"] = $res["author-name"];
                $res["owner-link"] = $res["author-link"];
                $res["owner-avatar"] = $res["author-avatar"];
                $res["author-name"] = $name;
                $res["author-link"] = $uri;
                $res["author-avatar"] = $avatar;
                $res["body"] = html2bbcode($message);
            }
        }
    }
    if (isset($contact["network"]) and $contact["network"] == NETWORK_FEED and $contact['fetch_further_information']) {
        $preview = "";
        // Handle enclosures and treat them as preview picture
        if (isset($attach)) {
            foreach ($attach as $attachment) {
                if ($attachment->type == "image/jpeg") {
                    $preview = $attachment->link;
                }
            }
        }
        $res["body"] = $res["title"] . add_page_info($res['plink'], false, $preview, $contact['fetch_further_information'] == 2, $contact['ffi_keyword_blacklist']);
        $res["tag"] = add_page_keywords($res['plink'], false, $preview, $contact['fetch_further_information'] == 2, $contact['ffi_keyword_blacklist']);
        $res["title"] = "";
        $res["object-type"] = ACTIVITY_OBJ_BOOKMARK;
        unset($res["attach"]);
    } elseif (isset($contact["network"]) and $contact["network"] == NETWORK_OSTATUS) {
        $res["body"] = add_page_info_to_body($res["body"]);
    } elseif (isset($contact["network"]) and $contact["network"] == NETWORK_FEED and strstr($res['plink'], ".app.net/")) {
        $res["body"] = add_page_info_to_body($res["body"]);
    }
    $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
    call_hooks('parse_atom', $arr);
    return $res;
}
Beispiel #2
0
/**
 * @param object $feed
 * @param array $item
 * @param[out] array $author
 * @return multitype:multitype: string NULL number Ambigous <NULL, string, number> Ambigous <mixed, string> Ambigous <multitype:multitype:string Ambigous <NULL, string>  , multitype:multitype:string unknown  > multitype:NULL unknown
 */
function get_atom_elements($feed, $item, &$author)
{
    //$best_photo = array();
    $res = array();
    $found_author = $item->get_author();
    if ($found_author) {
        $author['author_name'] = unxmlify($found_author->get_name());
        $author['author_link'] = unxmlify($found_author->get_link());
        $author['author_is_feed'] = false;
    } else {
        $author['author_name'] = unxmlify($feed->get_title());
        $author['author_link'] = unxmlify($feed->get_permalink());
        $author['author_is_feed'] = true;
    }
    if (substr($author['author_link'], -1, 1) == '/') {
        $author['author_link'] = substr($author['author_link'], 0, -1);
    }
    $res['mid'] = base64url_encode(unxmlify($item->get_id()));
    $res['title'] = unxmlify($item->get_title());
    $res['body'] = unxmlify($item->get_content());
    $res['plink'] = unxmlify($item->get_link(0));
    $res['item_flags'] = ITEM_RSS;
    // removing the content of the title if its identically to the body
    // This helps with auto generated titles e.g. from tumblr
    if (title_is_body($res["title"], $res["body"])) {
        $res['title'] = "";
    }
    if ($res['plink']) {
        $base_url = implode('/', array_slice(explode('/', $res['plink']), 0, 3));
    } else {
        $base_url = '';
    }
    // look for a photo. We should check media size and find the best one,
    // but for now let's just find any author photo
    $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
    if ($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
        $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        foreach ($base as $link) {
            if (!x($author, 'author_photo') || !$author['author_photo']) {
                if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                    $author['author_photo'] = unxmlify($link['attribs']['']['href']);
                }
            }
        }
    }
    $rawactor = $item->get_item_tags(NAMESPACE_ACTIVITY, 'actor');
    if ($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['obj_type'][0]['data'], ACTIVITY_OBJ_PERSON)) {
        $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        if ($base && count($base)) {
            foreach ($base as $link) {
                if ($link['attribs']['']['rel'] === 'alternate' && !$res['author_link']) {
                    $author['author_link'] = unxmlify($link['attribs']['']['href']);
                }
                if (!x($author, 'author_photo') || !$author['author_photo']) {
                    if ($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo') {
                        $author['author_photo'] = unxmlify($link['attribs']['']['href']);
                    }
                }
            }
        }
    }
    // check for a yahoo media element (github etc.)
    if (!$author['author_photo']) {
        $rawmedia = $item->get_item_tags(NAMESPACE_YMEDIA, 'thumbnail');
        if ($rawmedia && $rawmedia[0]['attribs']['']['url']) {
            $author['author_photo'] = strip_tags(unxmlify($rawmedia[0]['attribs']['']['url']));
        }
    }
    // No photo/profile-link on the item - look at the feed level
    if (!x($author, 'author_link') || !x($author, 'author_photo')) {
        $rawauthor = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
        if ($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
            foreach ($base as $link) {
                if ($link['attribs']['']['rel'] === 'alternate' && !$author['author_link']) {
                    $author['author_link'] = unxmlify($link['attribs']['']['href']);
                    $author['author_is_feed'] = true;
                }
                if (!$author['author_photo']) {
                    if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                        $author['author_photo'] = unxmlify($link['attribs']['']['href']);
                    }
                }
            }
        }
        $rawactor = $feed->get_feed_tags(NAMESPACE_ACTIVITY, 'subject');
        if ($rawactor && activity_match($rawactor[0]['child'][NAMESPACE_ACTIVITY]['obj_type'][0]['data'], ACTIVITY_OBJ_PERSON)) {
            $base = $rawactor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
            if ($base && count($base)) {
                foreach ($base as $link) {
                    if ($link['attribs']['']['rel'] === 'alternate' && !$res['author_link']) {
                        $author['author_link'] = unxmlify($link['attribs']['']['href']);
                    }
                    if (!x($author, 'author_photo')) {
                        if ($link['attribs']['']['rel'] === 'avatar' || $link['attribs']['']['rel'] === 'photo') {
                            $author['author_photo'] = unxmlify($link['attribs']['']['href']);
                        }
                    }
                }
            }
        }
    }
    $apps = $item->get_item_tags(NAMESPACE_STATUSNET, 'notice_info');
    if ($apps && $apps[0]['attribs']['']['source']) {
        $res['app'] = strip_tags(unxmlify($apps[0]['attribs']['']['source']));
    }
    /*
     * If there's a copy of the body content which is guaranteed to have survived mangling in transit, use it.
     */
    $have_real_body = false;
    $rawenv = $item->get_item_tags(NAMESPACE_DFRN, 'env');
    if ($rawenv) {
        $have_real_body = true;
        $res['body'] = $rawenv[0]['data'];
        $res['body'] = str_replace(array(' ', "\t", "\r", "\n"), array('', '', '', ''), $res['body']);
        // make sure nobody is trying to sneak some html tags by us
        $res['body'] = notags(base64url_decode($res['body']));
        // We could probably turn these old Friendica bbcode bookmarks into bookmark tags but we'd have to
        // create a term table item for them. For now just make sure they stay as links.
        $res['body'] = preg_replace('/\\[bookmark(.*?)\\](.*?)\\[\\/bookmark\\]/', '[url$1]$2[/url]', $res['body']);
    }
    $res['body'] = limit_body_size($res['body']);
    // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust
    // the content type. Our own network only emits text normally, though it might have been converted to
    // html if we used a pubsubhubbub transport. But if we see even one html tag in our text, we will
    // have to assume it is all html and needs to be purified.
    // It doesn't matter all that much security wise - because before this content is used anywhere, we are
    // going to escape any tags we find regardless, but this lets us import a limited subset of html from
    // 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'] = reltoabs($res['body'], $base_url);
        $res['body'] = html2bb_video($res['body']);
        $res['body'] = oembed_html2bbcode($res['body']);
        $res['body'] = purify_html($res['body']);
        $res['body'] = @html2bbcode($res['body']);
    } elseif (!$have_real_body) {
        // it's not one of our messages and it has no tags
        // so it's probably just text. We'll escape it just to be safe.
        $res['body'] = escape_tags($res['body']);
    }
    if ($res['plink'] && $res['title']) {
        $res['body'] = '#^[url=' . $res['plink'] . ']' . $res['title'] . '[/url]' . "\n\n" . $res['body'];
        $terms = array();
        $terms[] = array('otype' => TERM_OBJ_POST, 'type' => TERM_BOOKMARK, 'url' => $res['plink'], 'term' => $res['title']);
    } elseif ($res['plink']) {
        $res['body'] = '#^[url]' . $res['plink'] . '[/url]' . "\n\n" . $res['body'];
        $terms = array();
        $terms[] = array('otype' => TERM_OBJ_POST, 'type' => TERM_BOOKMARK, 'url' => $res['plink'], 'term' => $res['plink']);
    }
    $private = $item->get_item_tags(NAMESPACE_DFRN, 'private');
    if ($private && intval($private[0]['data']) > 0) {
        $res['item_private'] = intval($private[0]['data']) ? 1 : 0;
    } else {
        $res['item_private'] = 0;
    }
    $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
    if ($rawlocation) {
        $res['location'] = unxmlify($rawlocation[0]['data']);
    }
    $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'published');
    if ($rawcreated) {
        $res['created'] = unxmlify($rawcreated[0]['data']);
    }
    $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'updated');
    if ($rawedited) {
        $res['edited'] = unxmlify($rawedited[0]['data']);
    }
    if (x($res, 'edited') && !x($res, 'created')) {
        $res['created'] = $res['edited'];
    }
    if (!$res['created']) {
        $res['created'] = $item->get_date('c');
    }
    if (!$res['edited']) {
        $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();
    }
    $res['created'] = datetime_convert('UTC', 'UTC', $res['created']);
    $res['edited'] = datetime_convert('UTC', 'UTC', $res['edited']);
    $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
    if (!$rawowner) {
        $rawowner = $item->get_item_tags(NAMESPACE_ZOT, 'owner');
    }
    if ($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']) {
        $author['owner_name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
    } elseif ($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']) {
        $author['owner_name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
    }
    if ($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']) {
        $author['owner_link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
    } elseif ($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']) {
        $author['owner_link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
    }
    if ($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
        $base = $rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
        foreach ($base as $link) {
            if (!x($author, 'owner_photo') || !$author['owner_photo']) {
                if ($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar') {
                    $author['owner_photo'] = unxmlify($link['attribs']['']['href']);
                }
            }
        }
    }
    $rawgeo = $item->get_item_tags(NAMESPACE_GEORSS, 'point');
    if ($rawgeo) {
        $res['coord'] = unxmlify($rawgeo[0]['data']);
    }
    $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
    // select between supported verbs
    if ($rawverb) {
        $res['verb'] = unxmlify($rawverb[0]['data']);
    }
    // translate OStatus unfollow to activity streams if it happened to get selected
    if (x($res, 'verb') && $res['verb'] === 'http://ostatus.org/schema/1.0/unfollow') {
        $res['verb'] = ACTIVITY_UNFOLLOW;
    }
    $cats = $item->get_categories();
    if ($cats) {
        if (is_null($terms)) {
            $terms = array();
        }
        foreach ($cats as $cat) {
            $term = $cat->get_term();
            if (!$term) {
                $term = $cat->get_label();
            }
            $scheme = $cat->get_scheme();
            $termurl = '';
            if ($scheme && $term && stristr($scheme, 'X-DFRN:')) {
                $termtype = substr($scheme, 7, 1) === '#' ? TERM_HASHTAG : TERM_MENTION;
                $termurl = unxmlify(substr($scheme, 9));
            } else {
                $termtype = TERM_CATEGORY;
            }
            $termterm = notags(trim(unxmlify($term)));
            if ($termterm) {
                $terms[] = array('otype' => TERM_OBJ_POST, 'type' => $termtype, 'url' => $termurl, 'term' => $termterm);
            }
        }
    }
    if (!is_null($terms)) {
        $res['term'] = $terms;
    }
    $attach = $item->get_enclosures();
    if ($attach) {
        $res['attach'] = array();
        foreach ($attach as $att) {
            $len = intval($att->get_length());
            $link = str_replace(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_link()))));
            $title = str_replace(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_title()))));
            $type = str_replace(array(',', '"'), array('%2D', '%22'), notags(trim(unxmlify($att->get_type()))));
            if (strpos($type, ';')) {
                $type = substr($type, 0, strpos($type, ';'));
            }
            if (!$link || strpos($link, 'http') !== 0) {
                continue;
            }
            if (!$title) {
                $title = ' ';
            }
            if (!$type) {
                $type = 'application/octet-stream';
            }
            $res['attach'][] = array('href' => $link, 'length' => $len, 'type' => $type, 'title' => $title);
        }
    }
    $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
    if ($rawobj) {
        $obj = array();
        $child = $rawobj[0]['child'];
        if ($child[NAMESPACE_ACTIVITY]['obj_type'][0]['data']) {
            $res['obj_type'] = $child[NAMESPACE_ACTIVITY]['obj_type'][0]['data'];
            $obj['type'] = $child[NAMESPACE_ACTIVITY]['obj_type'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data']) {
            $obj['id'] = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $obj['link'] = encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']);
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data']) {
            $obj['title'] = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
            $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
            if (!$body) {
                $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
            }
            // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
            $obj['orig'] = xmlify($body);
            if (strpos($body, '<') !== false || strpos($body, '>') !== false) {
                $body = purify_html($body);
                $body = html2bbcode($body);
            }
            $obj['content'] = $body;
        }
        $res['object'] = $obj;
    }
    $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'target');
    if ($rawobj) {
        $obj = array();
        $child = $rawobj[0]['child'];
        if ($child[NAMESPACE_ACTIVITY]['obj_type'][0]['data']) {
            $res['tgt_type'] = $child[NAMESPACE_ACTIVITY]['obj_type'][0]['data'];
            $obj['type'] = $child[NAMESPACE_ACTIVITY]['obj_type'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'id') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data']) {
            $obj['id'] = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'link') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
            $obj['link'] = encode_rel_links($child[SIMPLEPIE_NAMESPACE_ATOM_10]['link']);
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'title') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data']) {
            $obj['title'] = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'];
        }
        if (x($child[SIMPLEPIE_NAMESPACE_ATOM_10], 'content') && $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
            $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
            if (!$body) {
                $body = $child[SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
            }
            // preserve a copy of the original body content in case we later need to parse out any microformat information, e.g. events
            $obj['orig'] = xmlify($body);
            if (strpos($body, '<') !== false || strpos($body, '>') !== false) {
                $body = purify_html($body);
                $body = html2bbcode($body);
            }
            $obj['content'] = $body;
        }
        $res['target'] = $obj;
    }
    $res['public_policy'] = 'specific';
    $res['comment_policy'] = 'none';
    $arr = array('feed' => $feed, 'item' => $item, 'result' => $res);
    call_hooks('parse_atom', $arr);
    logger('get_atom_elements: author: ' . print_r($author, true), LOGGER_DATA);
    logger('get_atom_elements: ' . print_r($res, true), LOGGER_DATA);
    return $res;
}
Beispiel #3
0
function onepoll_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    require_once 'library/simplepie/simplepie.inc';
    require_once 'include/items.php';
    require_once 'include/Contact.php';
    require_once 'include/email.php';
    require_once 'include/socgraph.php';
    require_once 'include/pidfile.php';
    require_once 'include/queue_fn.php';
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    logger('onepoll: start');
    $manual_id = 0;
    $generation = 0;
    $hub_update = false;
    $force = false;
    $restart = false;
    if ($argc > 1 && intval($argv[1])) {
        $contact_id = intval($argv[1]);
    }
    if (!$contact_id) {
        logger('onepoll: no contact');
        return;
    }
    $lockpath = get_lockpath();
    if ($lockpath != '') {
        $pidfile = new pidfile($lockpath, 'onepoll' . $contact_id);
        if ($pidfile->is_already_running()) {
            logger("onepoll: Already running for contact " . $contact_id);
            if ($pidfile->running_time() > 9 * 60) {
                $pidfile->kill();
                logger("killed stale process");
            }
            exit;
        }
    }
    $d = datetime_convert();
    // Only poll from those with suitable relationships,
    // and which have a polling address and ignore Diaspora since
    // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
    $contacts = q("SELECT `contact`.* FROM `contact`\n\t\tWHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''\n\t\tAND NOT `network` IN ( '%s', '%s', '%s' )\n\t\tAND `contact`.`id` = %d\n\t\tAND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0\n\t\tAND `contact`.`archive` = 0 LIMIT 1", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_FACEBOOK), dbesc(NETWORK_PUMPIO), intval($contact_id));
    if (!count($contacts)) {
        return;
    }
    $contact = $contacts[0];
    $xml = false;
    $t = $contact['last-update'];
    if ($contact['subhub']) {
        $poll_interval = get_config('system', 'pushpoll_frequency');
        $contact['priority'] = $poll_interval !== false ? intval($poll_interval) : 3;
        $hub_update = false;
        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
            $hub_update = true;
        }
    } else {
        $hub_update = false;
    }
    $importer_uid = $contact['uid'];
    $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `contact`.`uid` = `user`.`uid` WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval($importer_uid));
    if (!count($r)) {
        return;
    }
    $importer = $r[0];
    logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
    $last_update = $contact['last-update'] === '0000-00-00 00:00:00' ? datetime_convert('UTC', 'UTC', 'now - 7 days', ATOM_TIME) : datetime_convert('UTC', 'UTC', $contact['last-update'], ATOM_TIME);
    if ($contact['network'] === NETWORK_DFRN) {
        $idtosend = $orig_id = $contact['dfrn-id'] ? $contact['dfrn-id'] : $contact['issued-id'];
        if (intval($contact['duplex']) && $contact['dfrn-id']) {
            $idtosend = '0:' . $orig_id;
        }
        if (intval($contact['duplex']) && $contact['issued-id']) {
            $idtosend = '1:' . $orig_id;
        }
        // they have permission to write to us. We already filtered this in the contact query.
        $perm = 'rw';
        // But this may be our first communication, so set the writable flag if it isn't set already.
        if (!intval($contact['writable'])) {
            q("update contact set writable = 1 where id = %d", intval($contact['id']));
        }
        $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=data&last_update=' . $last_update . '&perm=' . $perm;
        $handshake_xml = fetch_url($url);
        $html_code = $a->get_curl_code();
        logger('onepoll: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
        if (!strlen($handshake_xml) || $html_code >= 400 || !$html_code) {
            logger("poller: {$url} appears to be dead - marking for death ");
            // dead connection - might be a transient event, or this might
            // mean the software was uninstalled or the domain expired.
            // Will keep trying for one month.
            mark_for_death($contact);
            // set the last-update so we don't keep polling
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        if (!strstr($handshake_xml, '<')) {
            logger('poller: response from ' . $url . ' did not contain XML.');
            mark_for_death($contact);
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        $res = parse_xml_string($handshake_xml);
        if (intval($res->status) == 1) {
            logger("poller: {$url} replied status 1 - marking for death ");
            // we may not be friends anymore. Will keep trying for one month.
            // set the last-update so we don't keep polling
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            mark_for_death($contact);
        } else {
            if ($contact['term-date'] != '0000-00-00 00:00:00') {
                logger("poller: {$url} back from the dead - removing mark for death");
                unmark_for_death($contact);
            }
        }
        if (intval($res->status) != 0 || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
            return;
        }
        if ((double) $res->dfrn_version > 2.21 && $contact['poco'] == '') {
            q("update contact set poco = '%s' where id = %d", dbesc(str_replace('/profile/', '/poco/', $contact['url'])), intval($contact['id']));
        }
        $postvars = array();
        $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
        $challenge = hex2bin((string) $res->challenge);
        $final_dfrn_id = '';
        if ($contact['duplex'] && strlen($contact['prvkey'])) {
            openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
            openssl_private_decrypt($challenge, $postvars['challenge'], $contact['prvkey']);
        } else {
            openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
            openssl_public_decrypt($challenge, $postvars['challenge'], $contact['pubkey']);
        }
        $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
        if (strpos($final_dfrn_id, ':') == 1) {
            $final_dfrn_id = substr($final_dfrn_id, 2);
        }
        if ($final_dfrn_id != $orig_id) {
            logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
            // did not decode properly - cannot trust this site
            return;
        }
        $postvars['dfrn_id'] = $idtosend;
        $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
        $postvars['perm'] = 'rw';
        $xml = post_url($contact['poll'], $postvars);
    } elseif ($contact['network'] === NETWORK_OSTATUS || $contact['network'] === NETWORK_DIASPORA || $contact['network'] === NETWORK_FEED) {
        // Upgrading DB fields from an older Friendica version
        // Will only do this once per notify-enabled OStatus contact
        // or if relationship changes
        $stat_writeable = $contact['notify'] && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND) ? 1 : 0;
        if ($contact['network'] === NETWORK_OSTATUS && get_pconfig($importer_uid, 'system', 'ostatus_autofriend')) {
            $stat_writeable = 1;
        }
        if ($stat_writeable != $contact['writable']) {
            q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d", intval($stat_writeable), intval($contact['id']));
        }
        // Are we allowed to import from this person?
        if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
            return;
        }
        $xml = fetch_url($contact['poll']);
    } elseif ($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) {
        logger("Mail: Fetching", LOGGER_DEBUG);
        $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
        if ($mail_disabled) {
            return;
        }
        logger("Mail: Enabled", LOGGER_DEBUG);
        $mbox = null;
        $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer_uid));
        $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1", intval($importer_uid));
        if (count($x) && count($mailconf)) {
            $mailbox = construct_mailbox_name($mailconf[0]);
            $password = '';
            openssl_private_decrypt(hex2bin($mailconf[0]['pass']), $password, $x[0]['prvkey']);
            $mbox = email_connect($mailbox, $mailconf[0]['user'], $password);
            unset($password);
            logger("Mail: Connect to " . $mailconf[0]['user']);
            if ($mbox) {
                q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d", dbesc(datetime_convert()), intval($mailconf[0]['id']), intval($importer_uid));
                logger("Mail: Connected to " . $mailconf[0]['user']);
            } else {
                logger("Mail: Connection error " . $mailconf[0]['user'] . " " . print_r(imap_errors()));
            }
        }
        if ($mbox) {
            $msgs = email_poll($mbox, $contact['addr']);
            if (count($msgs)) {
                logger("Mail: Parsing " . count($msgs) . " mails for " . $mailconf[0]['user'], LOGGER_DEBUG);
                $metas = email_msg_meta($mbox, implode(',', $msgs));
                if (count($metas) != count($msgs)) {
                    logger("onepoll: for " . $mailconf[0]['user'] . " there are " . count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG);
                } else {
                    $msgs = array_combine($msgs, $metas);
                    foreach ($msgs as $msg_uid => $meta) {
                        logger("Mail: Parsing mail " . $msg_uid, LOGGER_DATA);
                        $datarray = array();
                        $datarray['verb'] = ACTIVITY_POST;
                        $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
                        //					$meta = email_msg_meta($mbox,$msg_uid);
                        //					$headers = email_msg_headers($mbox,$msg_uid);
                        $datarray['uri'] = msgid2iri(trim($meta->message_id, '<>'));
                        // Have we seen it before?
                        $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($importer_uid), dbesc($datarray['uri']));
                        if (count($r)) {
                            logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . " UID: " . $importer_uid . " URI: " . $datarray['uri'], LOGGER_DEBUG);
                            // Only delete when mails aren't automatically moved or deleted
                            if ($mailconf[0]['action'] != 1 and $mailconf[0]['action'] != 3) {
                                if ($meta->deleted && !$r[0]['deleted']) {
                                    q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($r[0]['id']));
                                }
                            }
                            switch ($mailconf[0]['action']) {
                                case 0:
                                    logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . ". Doing nothing.", LOGGER_DEBUG);
                                    break;
                                case 1:
                                    logger("Mail: Deleting " . $msg_uid . " for " . $mailconf[0]['user']);
                                    imap_delete($mbox, $msg_uid, FT_UID);
                                    break;
                                case 2:
                                    logger("Mail: Mark as seen " . $msg_uid . " for " . $mailconf[0]['user']);
                                    imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                    break;
                                case 3:
                                    logger("Mail: Moving " . $msg_uid . " to " . $mailconf[0]['movetofolder'] . " for " . $mailconf[0]['user']);
                                    imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                    if ($mailconf[0]['movetofolder'] != "") {
                                        imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
                                    }
                                    break;
                            }
                            continue;
                        }
                        // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
                        //					$raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
                        $raw_refs = property_exists($meta, 'references') ? str_replace("\t", '', $meta->references) : '';
                        if (!trim($raw_refs)) {
                            $raw_refs = property_exists($meta, 'in_reply_to') ? str_replace("\t", '', $meta->in_reply_to) : '';
                        }
                        $raw_refs = trim($raw_refs);
                        // Don't allow a blank reference in $refs_arr
                        if ($raw_refs) {
                            $refs_arr = explode(' ', $raw_refs);
                            if (count($refs_arr)) {
                                for ($x = 0; $x < count($refs_arr); $x++) {
                                    $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<', '>', ' '), array('', '', ''), dbesc($refs_arr[$x]))) . "'";
                                }
                            }
                            $qstr = implode(',', $refs_arr);
                            $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( {$qstr} ) AND `uid` = %d LIMIT 1", intval($importer_uid));
                            if (count($r)) {
                                $datarray['parent-uri'] = $r[0]['parent-uri'];
                            }
                            // Set the parent as the top-level item
                            //							$datarray['parent-uri'] = $r[0]['uri'];
                        }
                        // Decoding the header
                        $subject = imap_mime_header_decode($meta->subject);
                        $datarray['title'] = "";
                        foreach ($subject as $subpart) {
                            if ($subpart->charset != "default") {
                                $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
                            } else {
                                $datarray['title'] .= $subpart->text;
                            }
                        }
                        $datarray['title'] = notags(trim($datarray['title']));
                        //$datarray['title'] = notags(trim($meta->subject));
                        $datarray['created'] = datetime_convert('UTC', 'UTC', $meta->date);
                        // Is it a reply?
                        $reply = (substr(strtolower($datarray['title']), 0, 3) == "re:" or substr(strtolower($datarray['title']), 0, 3) == "re-" or $raw_refs != "");
                        // Remove Reply-signs in the subject
                        $datarray['title'] = RemoveReply($datarray['title']);
                        // If it seems to be a reply but a header couldn't be found take the last message with matching subject
                        if (!x($datarray, 'parent-uri') and $reply) {
                            $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d ORDER BY `created` DESC LIMIT 1", dbesc(protect_sprintf($datarray['title'])), intval($importer_uid));
                            if (count($r)) {
                                $datarray['parent-uri'] = $r[0]['parent-uri'];
                            }
                        }
                        if (!x($datarray, 'parent-uri')) {
                            $datarray['parent-uri'] = $datarray['uri'];
                        }
                        $r = email_get_msg($mbox, $msg_uid, $reply);
                        if (!$r) {
                            logger("Mail: can't fetch msg " . $msg_uid . " for " . $mailconf[0]['user']);
                            continue;
                        }
                        $datarray['body'] = escape_tags($r['body']);
                        $datarray['body'] = limit_body_size($datarray['body']);
                        logger("Mail: Importing " . $msg_uid . " for " . $mailconf[0]['user']);
                        // some mailing lists have the original author as 'from' - add this sender info to msg body.
                        // todo: adding a gravatar for the original author would be cool
                        if (!stristr($meta->from, $contact['addr'])) {
                            $from = imap_mime_header_decode($meta->from);
                            $fromdecoded = "";
                            foreach ($from as $frompart) {
                                if ($frompart->charset != "default") {
                                    $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
                                } else {
                                    $fromdecoded .= $frompart->text;
                                }
                            }
                            $fromarr = imap_rfc822_parse_adrlist($fromdecoded, $a->get_hostname());
                            $frommail = $fromarr[0]->mailbox . "@" . $fromarr[0]->host;
                            if (isset($fromarr[0]->personal)) {
                                $fromname = $fromarr[0]->personal;
                            } else {
                                $fromname = $frommail;
                            }
                            //$datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body'];
                            $datarray['author-name'] = $fromname;
                            $datarray['author-link'] = "mailto:" . $frommail;
                            $datarray['author-avatar'] = $contact['photo'];
                            $datarray['owner-name'] = $contact['name'];
                            $datarray['owner-link'] = "mailto:" . $contact['addr'];
                            $datarray['owner-avatar'] = $contact['photo'];
                        } else {
                            $datarray['author-name'] = $contact['name'];
                            $datarray['author-link'] = 'mailbox';
                            $datarray['author-avatar'] = $contact['photo'];
                        }
                        $datarray['uid'] = $importer_uid;
                        $datarray['contact-id'] = $contact['id'];
                        if ($datarray['parent-uri'] === $datarray['uri']) {
                            $datarray['private'] = 1;
                        }
                        if ($contact['network'] === NETWORK_MAIL && !get_pconfig($importer_uid, 'system', 'allow_public_email_replies')) {
                            $datarray['private'] = 1;
                            $datarray['allow_cid'] = '<' . $contact['id'] . '>';
                        }
                        $stored_item = item_store($datarray);
                        q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($datarray['parent-uri']), intval($importer_uid));
                        q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", intval($stored_item));
                        switch ($mailconf[0]['action']) {
                            case 0:
                                logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . ". Doing nothing.", LOGGER_DEBUG);
                                break;
                            case 1:
                                logger("Mail: Deleting " . $msg_uid . " for " . $mailconf[0]['user']);
                                imap_delete($mbox, $msg_uid, FT_UID);
                                break;
                            case 2:
                                logger("Mail: Mark as seen " . $msg_uid . " for " . $mailconf[0]['user']);
                                imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                break;
                            case 3:
                                logger("Mail: Moving " . $msg_uid . " to " . $mailconf[0]['movetofolder'] . " for " . $mailconf[0]['user']);
                                imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                if ($mailconf[0]['movetofolder'] != "") {
                                    imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
                                }
                                break;
                        }
                    }
                }
            } else {
                logger("Mail: no mails for " . $mailconf[0]['user']);
            }
            logger("Mail: closing connection for " . $mailconf[0]['user']);
            imap_close($mbox);
        }
    } elseif ($contact['network'] === NETWORK_FACEBOOK) {
        // This is picked up by the Facebook plugin on a cron hook.
        // Ignored here.
    } elseif ($contact['network'] === NETWORK_PUMPIO) {
        // This is picked up by the pump.io plugin on a cron hook.
        // Ignored here.
    }
    if ($xml) {
        logger('poller: received xml : ' . $xml, LOGGER_DATA);
        if (!strstr($xml, '<')) {
            logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        consume_feed($xml, $importer, $contact, $hub, 1, 1);
        // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
        consume_feed($xml, $importer, $contact, $hub, 1, 2);
        $hubmode = 'subscribe';
        if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) {
            $hubmode = 'unsubscribe';
        }
        if (($contact['network'] === NETWORK_OSTATUS || $contact['network'] == NETWORK_FEED) && !$contact['hub-verify']) {
            $hub_update = true;
        }
        if (strlen($hub) && $hub_update && ($contact['rel'] != CONTACT_IS_FOLLOWER || $contact['network'] == NETWORK_FEED)) {
            logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : '******'name']);
            $hubs = explode(',', $hub);
            if (count($hubs)) {
                foreach ($hubs as $h) {
                    $h = trim($h);
                    if (!strlen($h)) {
                        continue;
                    }
                    subscribe_to_hub($h, $importer, $contact, $hubmode);
                }
            }
        }
    }
    $updated = datetime_convert();
    $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d", dbesc($updated), dbesc($updated), intval($contact['id']));
    // load current friends if possible.
    if ($contact['poco']) {
        $r = q("SELECT count(*) as total from glink\n\t\t\twhere `cid` = %d and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY", intval($contact['id']));
    }
    if (count($r)) {
        if (!$r[0]['total']) {
            poco_load($contact['id'], $importer_uid, 0, $contact['poco']);
        }
    }
    return;
}