예제 #1
0
function api_format_items($r, $user_info, $filter_user = false)
{
    $a = get_app();
    $ret = array();
    foreach ($r as $item) {
        api_share_as_retweet($item);
        localize_item($item);
        $status_user = api_item_get_user($a, $item);
        // Look if the posts are matching if they should be filtered by user id
        if ($filter_user and $status_user["id"] != $user_info["id"]) {
            continue;
        }
        if ($item['thr-parent'] != $item['uri']) {
            $r = q("SELECT id FROM item WHERE uid=%d AND uri='%s' LIMIT 1", intval(api_user()), dbesc($item['thr-parent']));
            if ($r) {
                $in_reply_to_status_id = intval($r[0]['id']);
            } else {
                $in_reply_to_status_id = intval($item['parent']);
            }
            $in_reply_to_status_id_str = (string) intval($item['parent']);
            $in_reply_to_screen_name = NULL;
            $in_reply_to_user_id = NULL;
            $in_reply_to_user_id_str = NULL;
            $r = q("SELECT `author-link` FROM item WHERE uid=%d AND id=%d LIMIT 1", intval(api_user()), intval($in_reply_to_status_id));
            if ($r) {
                $r = q("SELECT * FROM unique_contacts WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link'])));
                if ($r) {
                    if ($r[0]['nick'] == "") {
                        $r[0]['nick'] = api_get_nick($r[0]["url"]);
                    }
                    $in_reply_to_screen_name = $r[0]['nick'] ? $r[0]['nick'] : $r[0]['name'];
                    $in_reply_to_user_id = intval($r[0]['id']);
                    $in_reply_to_user_id_str = (string) intval($r[0]['id']);
                }
            }
        } else {
            $in_reply_to_screen_name = NULL;
            $in_reply_to_user_id = NULL;
            $in_reply_to_status_id = NULL;
            $in_reply_to_user_id_str = NULL;
            $in_reply_to_status_id_str = NULL;
        }
        $converted = api_convert_item($item);
        $status = array('text' => $converted["text"], 'truncated' => False, 'created_at' => api_date($item['created']), 'in_reply_to_status_id' => $in_reply_to_status_id, 'in_reply_to_status_id_str' => $in_reply_to_status_id_str, 'source' => $item['app'] ? $item['app'] : 'web', 'id' => intval($item['id']), 'id_str' => (string) intval($item['id']), 'in_reply_to_user_id' => $in_reply_to_user_id, 'in_reply_to_user_id_str' => $in_reply_to_user_id_str, 'in_reply_to_screen_name' => $in_reply_to_screen_name, 'geo' => NULL, 'favorited' => $item['starred'] ? true : false, 'user' => $status_user, 'statusnet_html' => $converted["html"], 'statusnet_conversation_id' => $item['parent']);
        if (count($converted["attachments"]) > 0) {
            $status["attachments"] = $converted["attachments"];
        }
        if (count($converted["entities"]) > 0) {
            $status["entities"] = $converted["entities"];
        }
        if ($item['item_network'] != "" and $status["source"] == 'web') {
            $status["source"] = network_to_name($item['item_network'], $user_info['url']);
        } else {
            if ($item['item_network'] != "" and network_to_name($item['item_network'], $user_info['url']) != $status["source"]) {
                $status["source"] = trim($status["source"] . ' (' . network_to_name($item['item_network'], $user_info['url']) . ')');
            }
        }
        // Retweets are only valid for top postings
        // It doesn't work reliable with the link if its a feed
        $IsRetweet = $item['owner-link'] != $item['author-link'];
        if ($IsRetweet) {
            $IsRetweet = ($item['owner-name'] != $item['author-name'] or $item['owner-avatar'] != $item['author-avatar']);
        }
        if ($IsRetweet and $item["id"] == $item["parent"]) {
            $retweeted_status = $status;
            $retweeted_status["user"] = api_get_user($a, $item["author-link"]);
            $status["retweeted_status"] = $retweeted_status;
        }
        // "uid" and "self" are only needed for some internal stuff, so remove it from here
        unset($status["user"]["uid"]);
        unset($status["user"]["self"]);
        if ($item["coord"] != "") {
            $coords = explode(' ', $item["coord"]);
            if (count($coords) == 2) {
                $status["geo"] = array('type' => 'Point', 'coordinates' => array((double) $coords[0], (double) $coords[1]));
            }
        }
        $ret[] = $status;
    }
    return $ret;
}
예제 #2
0
function diaspora_reshare($importer, $xml, $msg)
{
    logger('diaspora_reshare: init: ' . print_r($xml, true));
    $a = get_app();
    $guid = notags(unxmlify($xml->guid));
    $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
    if ($diaspora_handle != $msg['author']) {
        logger('diaspora_post: Potential forgery. Message handle is not the same as envelope sender.');
        return 202;
    }
    $contact = diaspora_get_contact_by_handle($importer['uid'], $diaspora_handle);
    if (!$contact) {
        return;
    }
    if (!diaspora_post_allow($importer, $contact, false)) {
        logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml, true));
        return 202;
    }
    $message_id = $diaspora_handle . ':' . $guid;
    $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1", intval($importer['uid']), dbesc($guid));
    if (count($r)) {
        logger('diaspora_reshare: message exists: ' . $guid);
        return;
    }
    $orig_author = notags(unxmlify($xml->root_diaspora_id));
    $orig_guid = notags(unxmlify($xml->root_guid));
    $orig_url = $a->get_baseurl() . "/display/" . $orig_guid;
    $create_original_post = false;
    // Do we already have this item?
    $r = q("SELECT `body`, `tag`, `app`, `created`, `plink`, `object`, `object-type`, `uri` FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1", dbesc($orig_guid), dbesc(NETWORK_DIASPORA));
    if (count($r)) {
        logger('reshared message ' . $orig_guid . " reshared by " . $guid . ' already exists on system.');
        // Maybe it is already a reshared item?
        // Then refetch the content, since there can be many side effects with reshared posts from other networks or reshares from reshares
        require_once 'include/api.php';
        if (api_share_as_retweet($r[0])) {
            $r = array();
        } else {
            $body = $r[0]["body"];
            $str_tags = $r[0]["tag"];
            $app = $r[0]["app"];
            $orig_created = $r[0]["created"];
            $orig_plink = $r[0]["plink"];
            $orig_uri = $r[0]["uri"];
            $object = $r[0]["object"];
            $objecttype = $r[0]["object-type"];
        }
    }
    if (!count($r)) {
        $body = "";
        $str_tags = "";
        $app = "";
        $server = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1);
        logger('1st try: reshared message ' . $orig_guid . " reshared by " . $guid . ' will be fetched from original server: ' . $server);
        $item = diaspora_fetch_message($orig_guid, $server);
        if (!$item) {
            $server = 'https://' . substr($diaspora_handle, strpos($diaspora_handle, '@') + 1);
            logger('2nd try: reshared message ' . $orig_guid . " reshared by " . $guid . " will be fetched from sharer's server: " . $server);
            $item = diaspora_fetch_message($orig_guid, $server);
        }
        if (!$item) {
            $server = 'http://' . substr($orig_author, strpos($orig_author, '@') + 1);
            logger('3rd try: reshared message ' . $orig_guid . " reshared by " . $guid . ' will be fetched from original server: ' . $server);
            $item = diaspora_fetch_message($orig_guid, $server);
        }
        if (!$item) {
            $server = 'http://' . substr($diaspora_handle, strpos($diaspora_handle, '@') + 1);
            logger('4th try: reshared message ' . $orig_guid . " reshared by " . $guid . " will be fetched from sharer's server: " . $server);
            $item = diaspora_fetch_message($orig_guid, $server);
        }
        if ($item) {
            $body = $item["body"];
            $str_tags = $item["tag"];
            $app = $item["app"];
            $orig_created = $item["created"];
            $orig_author = $item["author"];
            $orig_guid = $item["guid"];
            $orig_plink = diaspora_plink($orig_author, $orig_guid);
            $orig_uri = $orig_author . ':' . $orig_guid;
            $create_original_post = $body != "";
            $object = $item["object"];
            $objecttype = $item["object-type"];
        }
    }
    $plink = diaspora_plink($diaspora_handle, $guid);
    $person = find_diaspora_person_by_handle($orig_author);
    $created = unxmlify($xml->created_at);
    $private = unxmlify($xml->public) == 'false' ? 1 : 0;
    $datarray = array();
    $datarray['uid'] = $importer['uid'];
    $datarray['contact-id'] = $contact['id'];
    $datarray['wall'] = 0;
    $datarray['network'] = NETWORK_DIASPORA;
    $datarray['guid'] = $guid;
    $datarray['uri'] = $datarray['parent-uri'] = $message_id;
    $datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert('UTC', 'UTC', $created);
    $datarray['private'] = $private;
    $datarray['parent'] = 0;
    $datarray['plink'] = $plink;
    $datarray['owner-name'] = $contact['name'];
    $datarray['owner-link'] = $contact['url'];
    $datarray['owner-avatar'] = x($contact, 'thumb') ? $contact['thumb'] : $contact['photo'];
    if (!intval(get_config('system', 'wall-to-wall_share'))) {
        $prefix = share_header($person['name'], $person['url'], x($person, 'thumb') ? $person['thumb'] : $person['photo'], $orig_guid, $orig_created, $orig_url);
        $datarray['author-name'] = $contact['name'];
        $datarray['author-link'] = $contact['url'];
        $datarray['author-avatar'] = $contact['thumb'];
        $datarray['body'] = $prefix . $body . "[/share]";
    } else {
        // Let reshared messages look like wall-to-wall posts
        $datarray['author-name'] = $person['name'];
        $datarray['author-link'] = $person['url'];
        $datarray['author-avatar'] = x($person, 'thumb') ? $person['thumb'] : $person['photo'];
        $datarray['body'] = $body;
    }
    $datarray["object"] = json_encode($xml);
    $datarray['object-type'] = $objecttype;
    $datarray['tag'] = $str_tags;
    $datarray['app'] = $app;
    // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible. (testing)
    $datarray['visible'] = strlen($body) ? 1 : 0;
    // Store the original item of a reshare
    if ($create_original_post) {
        require_once "include/Contact.php";
        $datarray2 = $datarray;
        $datarray2['uid'] = 0;
        $datarray2['contact-id'] = get_contact($person['url'], 0);
        $datarray2['guid'] = $orig_guid;
        $datarray2['uri'] = $datarray2['parent-uri'] = $orig_uri;
        $datarray2['changed'] = $datarray2['created'] = $datarray2['edited'] = $datarray2['commented'] = $datarray2['received'] = datetime_convert('UTC', 'UTC', $orig_created);
        $datarray2['parent'] = 0;
        $datarray2['plink'] = $orig_plink;
        $datarray2['author-name'] = $person['name'];
        $datarray2['author-link'] = $person['url'];
        $datarray2['author-avatar'] = x($person, 'thumb') ? $person['thumb'] : $person['photo'];
        $datarray2['owner-name'] = $datarray2['author-name'];
        $datarray2['owner-link'] = $datarray2['author-link'];
        $datarray2['owner-avatar'] = $datarray2['author-avatar'];
        $datarray2['body'] = $body;
        $datarray2["object"] = $object;
        DiasporaFetchGuid($datarray2);
        $message_id = item_store($datarray2);
        logger("Store original item " . $orig_guid . " under message id " . $message_id);
    }
    DiasporaFetchGuid($datarray);
    $message_id = item_store($datarray);
    return;
}
예제 #3
0
파일: api.php 프로젝트: jzacman/friendica
function api_format_items($r, $user_info, $filter_user = false)
{
    $a = get_app();
    $ret = array();
    foreach ($r as $item) {
        api_share_as_retweet($a, api_user(), $item);
        localize_item($item);
        $status_user = api_item_get_user($a, $item);
        // Look if the posts are matching if they should be filtered by user id
        if ($filter_user and $status_user["id"] != $user_info["id"]) {
            continue;
        }
        if ($item['thr-parent'] != $item['uri']) {
            $r = q("SELECT id FROM item WHERE uid=%d AND uri='%s' LIMIT 1", intval(api_user()), dbesc($item['thr-parent']));
            if ($r) {
                $in_reply_to_status_id = intval($r[0]['id']);
            } else {
                $in_reply_to_status_id = intval($item['parent']);
            }
            $in_reply_to_status_id_str = (string) intval($item['parent']);
            $in_reply_to_screen_name = NULL;
            $in_reply_to_user_id = NULL;
            $in_reply_to_user_id_str = NULL;
            $r = q("SELECT `author-link` FROM item WHERE uid=%d AND id=%d LIMIT 1", intval(api_user()), intval($in_reply_to_status_id));
            if ($r) {
                $r = q("SELECT * FROM unique_contacts WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link'])));
                if ($r) {
                    if ($r[0]['nick'] == "") {
                        $r[0]['nick'] = api_get_nick($r[0]["url"]);
                    }
                    $in_reply_to_screen_name = $r[0]['nick'] ? $r[0]['nick'] : $r[0]['name'];
                    $in_reply_to_user_id = intval($r[0]['id']);
                    $in_reply_to_user_id_str = (string) intval($r[0]['id']);
                }
            }
        } else {
            $in_reply_to_screen_name = NULL;
            $in_reply_to_user_id = NULL;
            $in_reply_to_status_id = NULL;
            $in_reply_to_user_id_str = NULL;
            $in_reply_to_status_id_str = NULL;
        }
        // Workaround for ostatus messages where the title is identically to the body
        //$statusbody = trim(html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 5, true), 0));
        $html = bbcode(api_clean_plain_items($item['body']), false, false, 2, true);
        $statusbody = trim(html2plain($html, 0));
        $statustitle = trim($item['title']);
        if ($statustitle != '' and strpos($statusbody, $statustitle) !== false) {
            $statustext = trim($statusbody);
        } else {
            $statustext = trim($statustitle . "\n\n" . $statusbody);
        }
        if ($item["network"] == NETWORK_FEED and strlen($statustext) > 1000) {
            $statustext = substr($statustext, 0, 1000) . "... \n" . $item["plink"];
        }
        $status = array('text' => $statustext, 'truncated' => False, 'created_at' => api_date($item['created']), 'in_reply_to_status_id' => $in_reply_to_status_id, 'in_reply_to_status_id_str' => $in_reply_to_status_id_str, 'source' => $item['app'] ? $item['app'] : 'web', 'id' => intval($item['id']), 'id_str' => (string) intval($item['id']), 'in_reply_to_user_id' => $in_reply_to_user_id, 'in_reply_to_user_id_str' => $in_reply_to_user_id_str, 'in_reply_to_screen_name' => $in_reply_to_screen_name, 'geo' => NULL, 'favorited' => $item['starred'] ? true : false, 'user' => $status_user, 'statusnet_html' => trim(bbcode($item['body'], false, false)), 'statusnet_conversation_id' => $item['parent']);
        if ($item['title'] != "") {
            $status['statusnet_html'] = "<h4>" . bbcode($item['title']) . "</h4>\n" . $status['statusnet_html'];
        }
        $entities = api_get_entitities($status['text'], $item['body']);
        if (count($entities) > 0) {
            $status['entities'] = $entities;
        }
        if ($item['item_network'] != "" and $status["source"] == 'web') {
            $status["source"] = network_to_name($item['item_network']);
        } else {
            if ($item['item_network'] != "" and network_to_name($item['item_network']) != $status["source"]) {
                $status["source"] = trim($status["source"] . ' (' . network_to_name($item['item_network']) . ')');
            }
        }
        // Retweets are only valid for top postings
        // It doesn't work reliable with the link if its a feed
        $IsRetweet = $item['owner-link'] != $item['author-link'];
        if ($IsRetweet) {
            $IsRetweet = ($item['owner-name'] != $item['author-name'] or $item['owner-avatar'] != $item['author-avatar']);
        }
        if ($IsRetweet and $item["id"] == $item["parent"]) {
            $retweeted_status = $status;
            $retweeted_status["user"] = api_get_user($a, $item["author-link"]);
            $status["retweeted_status"] = $retweeted_status;
        }
        // "uid" and "self" are only needed for some internal stuff, so remove it from here
        unset($status["user"]["uid"]);
        unset($status["user"]["self"]);
        // 'geo' => array('type' => 'Point',
        //                   'coordinates' => array((float) $notice->lat,
        //                                          (float) $notice->lon));
        $ret[] = $status;
    }
    return $ret;
}