示例#1
0
function fbpost_fetchwall($a, $uid)
{
    require_once "include/oembed.php";
    require_once "include/network.php";
    require_once "include/items.php";
    require_once "mod/item.php";
    require_once "include/bbcode.php";
    $access_token = get_pconfig($uid, 'facebook', 'access_token');
    $post_to_page = get_pconfig($uid, 'facebook', 'post_to_page');
    $mirror_page = get_pconfig($uid, 'facebook', 'mirror_page');
    $lastcreated = get_pconfig($uid, 'facebook', 'last_created');
    if ((int) $post_to_page == 0) {
        $post_to_page = "me";
    }
    if ((int) $mirror_page != 0) {
        $post_to_page = $mirror_page;
    }
    $url = "https://graph.facebook.com/" . $post_to_page . "/feed?access_token=" . $access_token;
    $first_time = $lastcreated == "";
    if ($lastcreated != "") {
        $url .= "&since=" . urlencode($lastcreated);
    }
    $feed = fetch_url($url);
    $data = json_decode($feed);
    if (!is_array($data->data)) {
        return;
    }
    $items = array_reverse($data->data);
    foreach ($items as $item) {
        if ($item->created_time > $lastcreated) {
            $lastcreated = $item->created_time;
        }
        if ($first_time) {
            continue;
        }
        if ($item->application->id == get_config('facebook', 'appid')) {
            continue;
        }
        if (isset($item->privacy) && $item->privacy->value !== 'EVERYONE' && (int) $mirror_page == 0) {
            continue;
        } elseif (isset($item->privacy) && $item->privacy->value !== 'EVERYONE' && $item->privacy->value !== '') {
            continue;
        } elseif (!isset($item->privacy)) {
            continue;
        }
        if ($post_to_page != $item->from->id and (int) $post_to_page != 0) {
            continue;
        }
        if (!strstr($item->id, $item->from->id . "_") and isset($item->to) and (int) $post_to_page == 0) {
            continue;
        }
        $_SESSION["authenticated"] = true;
        $_SESSION["uid"] = $uid;
        unset($_REQUEST);
        $_REQUEST["type"] = "wall";
        $_REQUEST["api_source"] = true;
        $_REQUEST["profile_uid"] = $uid;
        //$_REQUEST["source"] = "Facebook";
        $_REQUEST["source"] = $item->application->name;
        $_REQUEST["extid"] = NETWORK_FACEBOOK;
        $_REQUEST["title"] = "";
        $_REQUEST["body"] = isset($item->message) ? escape_tags($item->message) : '';
        $pagedata = array();
        $content = "";
        $pagedata["type"] = "";
        if (isset($item->name) and isset($item->link)) {
            $item->link = original_url($item->link);
            $oembed_data = oembed_fetch_url($item->link);
            $pagedata["type"] = $oembed_data->type;
            $pagedata["url"] = $item->link;
            $pagedata["title"] = $item->name;
            $content = "[bookmark=" . $item->link . "]" . $item->name . "[/bookmark]";
            // If a link is not only attached but also added in the body, look if it can be removed in the body.
            $removedlink = trim(str_replace($item->link, "", $_REQUEST["body"]));
            if ($removedlink == "" or strstr($_REQUEST["body"], $removedlink)) {
                $_REQUEST["body"] = $removedlink;
            }
        } elseif (isset($item->name)) {
            $content .= "[b]" . $item->name . "[/b]";
        }
        $pagedata["text"] = "";
        if (isset($item->description) and $item->type != "photo") {
            $pagedata["text"] = $item->description;
        }
        if (isset($item->caption) and $item->type == "photo") {
            $pagedata["text"] = $item->caption;
        }
        // Only import the picture when the message is no video
        // oembed display a picture of the video as well
        //if ($item->type != "video") {
        //if (($item->type != "video") and ($item->type != "photo")) {
        if ($pagedata["type"] == "" or $pagedata["type"] == "link") {
            $pagedata["type"] = $item->type;
            if (isset($item->picture)) {
                $pagedata["images"][0]["src"] = $item->picture;
            }
            if ($pagedata["type"] == "photo" and isset($item->object_id)) {
                logger('fbpost_fetchwall: fetching fbid ' . $item->object_id, LOGGER_DEBUG);
                $url = "https://graph.facebook.com/" . $item->object_id . "?access_token=" . $access_token;
                $feed = fetch_url($url);
                $data = json_decode($feed);
                if (isset($data->images)) {
                    $pagedata["images"][0]["src"] = $data->images[0]->source;
                    logger('got fbid image from images for ' . $item->object_id, LOGGER_DEBUG);
                } elseif (isset($data->source)) {
                    $pagedata["images"][0]["src"] = $data->source;
                    logger('got fbid image from source for ' . $item->object_id, LOGGER_DEBUG);
                } elseif (isset($data->picture)) {
                    $pagedata["images"][0]["src"] = $data->picture;
                    logger('got fbid image from picture for ' . $item->object_id, LOGGER_DEBUG);
                }
            }
            if (trim($_REQUEST["body"] . $content . $pagedata["text"]) == '') {
                logger('facebook: empty body 1 ' . $item->id . ' ' . print_r($item, true));
                continue;
            }
            $pagedata["images"][0]["src"] = fbpost_cleanpicture($pagedata["images"][0]["src"]);
            if ($pagedata["images"][0]["src"] != "" && isset($item->link)) {
                $item->link = original_url($item->link);
                $pagedata["url"] = $item->link;
                $content .= "\n" . '[url=' . $item->link . '][img]' . $pagedata["images"][0]["src"] . '[/img][/url]';
            } else {
                if ($pagedata["images"][0]["src"] != "") {
                    $content .= "\n" . '[img]' . $pagedata["images"][0]["src"] . '[/img]';
                }
                // if just a link, it may be a wall photo - check
                if (isset($item->link)) {
                    $content .= fbpost_get_photo($uid, $item->link);
                }
            }
        }
        if (trim($_REQUEST["body"] . $content . $pagedata["text"]) == '') {
            logger('facebook: empty body 2 ' . $item->id . ' ' . print_r($item, true));
            continue;
        }
        if ($pagedata["type"] != "") {
            $_REQUEST["body"] .= add_page_info_data($pagedata);
        } else {
            if ($content) {
                $_REQUEST["body"] .= "\n" . trim($content);
            }
            if ($pagedata["text"]) {
                $_REQUEST["body"] .= "\n[quote]" . $pagedata["text"] . "[/quote]";
            }
            $_REQUEST["body"] = trim($_REQUEST["body"]);
        }
        if (isset($item->place)) {
            if ($item->place->name or $item->place->location->street or $item->place->location->city or $item->place->location->country) {
                $_REQUEST["location"] = '';
                if ($item->place->name) {
                    $_REQUEST["location"] .= $item->place->name;
                }
                if ($item->place->location->street) {
                    $_REQUEST["location"] .= " " . $item->place->location->street;
                }
                if ($item->place->location->city) {
                    $_REQUEST["location"] .= " " . $item->place->location->city;
                }
                if ($item->place->location->country) {
                    $_REQUEST["location"] .= " " . $item->place->location->country;
                }
                $_REQUEST["location"] = trim($_REQUEST["location"]);
            }
            if ($item->place->location->latitude and $item->place->location->longitude) {
                $_REQUEST["coord"] = substr($item->place->location->latitude, 0, 8) . ' ' . substr($item->place->location->longitude, 0, 8);
            }
        }
        if (trim($_REQUEST["body"]) == '') {
            logger('facebook: empty body 3 ' . $item->id . ' ' . print_r($item, true));
            continue;
        }
        if (trim(strip_tags(bbcode($_REQUEST["body"], false, false))) == '') {
            logger('facebook: empty body 4 ' . $item->id . ' ' . print_r($item, true));
            continue;
        }
        //print_r($_REQUEST);
        logger('facebook: posting for user ' . $uid);
        item_post($a);
    }
    set_pconfig($uid, 'facebook', 'last_created', $lastcreated);
}
示例#2
0
function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $create_user)
{
    $access_token = get_pconfig($uid, 'facebook', 'access_token');
    require_once "include/oembed.php";
    require_once "include/network.php";
    require_once "include/items.php";
    // check if it was already imported
    $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($uid), dbesc('fb::' . $post->post_id));
    if (count($r)) {
        return;
    }
    $postarray = array();
    $postarray['gravity'] = 0;
    $postarray['uid'] = $uid;
    $postarray['wall'] = 0;
    $postarray['verb'] = ACTIVITY_POST;
    $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
    // default value - is maybe changed later when media is attached
    $postarray['network'] = dbesc(NETWORK_FACEBOOK);
    $postarray['uri'] = "fb::" . $post->post_id;
    $postarray['thr-parent'] = $postarray['uri'];
    $postarray['parent-uri'] = $postarray['uri'];
    $postarray['plink'] = $post->permalink;
    $postarray['author-name'] = $contacts[$post->actor_id]->name;
    $postarray['author-link'] = $contacts[$post->actor_id]->url;
    $postarray['author-avatar'] = $contacts[$post->actor_id]->pic_square;
    $postarray['owner-name'] = $contacts[$post->source_id]->name;
    $postarray['owner-link'] = $contacts[$post->source_id]->url;
    $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square;
    $contact_id = 0;
    if ($post->parent_post_id != "" and $post->actor_id == $post->source_id) {
        $pos = strpos($post->parent_post_id, "_");
        if ($pos != 0) {
            $user_id = substr($post->parent_post_id, 0, $pos);
            $userdata = fbsync_fetchuser($a, $uid, $user_id);
            $contact_id = $userdata["contact-id"];
            $postarray['contact-id'] = $contact_id;
            if (array_key_exists("name", $userdata) and $userdata["name"] != "" and !link_compare($userdata["link"], $postarray['author-link'])) {
                $postarray['owner-name'] = $userdata["name"];
                $postarray['owner-link'] = $userdata["link"];
                $postarray['owner-avatar'] = $userdata["avatar"];
                if (!intval(get_config('system', 'wall-to-wall_share'))) {
                    $prebody = "[share author='" . $postarray['author-name'] . "' profile='" . $postarray['author-link'] . "' avatar='" . $postarray['author-avatar'] . "']";
                    $postarray['author-name'] = $postarray['owner-name'];
                    $postarray['author-link'] = $postarray['owner-link'];
                    $postarray['author-avatar'] = $postarray['owner-avatar'];
                }
            }
        }
    }
    if ($contact_id <= 0) {
        if ($post->actor_id != $post->source_id) {
            // Testing if we know the source or the actor
            $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], false);
            if ($contact_id == 0 and array_key_exists($post->actor_id, $contacts)) {
                $contact_id = fbsync_fetch_contact($uid, $contacts[$post->actor_id], false);
            }
            // If we don't know anyone, we guess we should know the source. Could be the wrong decision
            if ($contact_id == 0) {
                $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user);
            }
        } else {
            $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user);
        }
        if ($contact_id == -1) {
            logger('fbsync_createpost: Contact is blocked. Post not imported ' . print_r($post, true), LOGGER_DEBUG);
            return;
        } elseif ($contact_id <= 0 and !$create_user) {
            logger('fbsync_createpost: No matching contact found. Post not imported ' . print_r($post, true), LOGGER_DEBUG);
            return;
        } elseif ($contact_id == 0) {
            // This case should never happen
            logger('fbsync_createpost: No matching contact found. Using own id. (Should never happen) ' . print_r($post, true), LOGGER_DEBUG);
            $contact_id = $self[0]["id"];
        }
        $postarray['contact-id'] = $contact_id;
    }
    $postarray["body"] = isset($post->message) ? escape_tags($post->message) : '';
    $msgdata = fbsync_convertmsg($a, $postarray["body"]);
    $postarray["body"] = $msgdata["body"];
    $postarray["tag"] = $msgdata["tags"];
    // Change the object type when an attachment is present
    if (isset($post->attachment->fb_object_type)) {
        logger('fb_object_type: ' . $post->attachment->fb_object_type . " " . print_r($post->attachment, true), LOGGER_DEBUG);
    }
    switch ($post->attachment->fb_object_type) {
        case 'photo':
            $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
            // photo is deprecated: http://activitystrea.ms/head/activity-schema.html#image
            break;
        case 'video':
            $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
            break;
        case '':
            //$postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
            break;
        default:
            logger('Unknown object type ' . $post->attachment->fb_object_type, LOGGER_DEBUG);
            break;
    }
    $pagedata = array();
    $content = "";
    $pagedata["type"] = "";
    if (isset($post->attachment->name) and isset($post->attachment->href)) {
        $post->attachment->href = original_url($post->attachment->href);
        $oembed_data = oembed_fetch_url($post->attachment->href);
        $pagedata["type"] = $oembed_data->type;
        if ($pagedata["type"] == "rich") {
            $pagedata["type"] = "link";
        }
        $pagedata["url"] = $post->attachment->href;
        $pagedata["title"] = $post->attachment->name;
        $content = "[bookmark=" . $post->attachment->href . "]" . $post->attachment->name . "[/bookmark]";
        // If a link is not only attached but also added in the body, look if it can be removed in the body.
        $removedlink = trim(str_replace($post->attachment->href, "", $postarray["body"]));
        if ($removedlink == "" or strstr($postarray["body"], $removedlink)) {
            $postarray["body"] = $removedlink;
        }
    } elseif (isset($post->attachment->name) and $post->attachment->name != "") {
        $content = "[b]" . $post->attachment->name . "[/b]";
    }
    $pagedata["text"] = "";
    if (isset($post->attachment->description) and $post->attachment->fb_object_type != "photo") {
        $pagedata["text"] = $post->attachment->description;
    }
    if (isset($post->attachment->caption) and $post->attachment->fb_object_type == "photo") {
        $pagedata["text"] = $post->attachment->caption;
    }
    if ($pagedata["text"] . $post->attachment->href . $content . $postarray["body"] == "") {
        return;
    }
    if (isset($post->attachment->media) and ($pagedata["type"] == "" or $pagedata["type"] == "link")) {
        foreach ($post->attachment->media as $media) {
            if (isset($media->type)) {
                $pagedata["type"] = $media->type;
            }
            if (isset($media->src)) {
                $pagedata["images"][0]["src"] = $media->src;
            }
            if (isset($media->photo)) {
                if (isset($media->photo->images) and count($media->photo->images) > 1) {
                    $pagedata["images"][0]["src"] = $media->photo->images[1]->src;
                }
                if (isset($media->photo->fbid)) {
                    logger('fbsync_createpost: fetching fbid ' . $media->photo->fbid, LOGGER_DEBUG);
                    $url = "https://graph.facebook.com/" . $media->photo->fbid . "?access_token=" . $access_token;
                    $feed = fetch_url($url);
                    $data = json_decode($feed);
                    if (isset($data->images)) {
                        $pagedata["images"][0]["src"] = $data->images[0]->source;
                        logger('fbsync_createpost: got fbid ' . $media->photo->fbid . ' image ' . $pagedata["images"][0]["src"], LOGGER_DEBUG);
                    } else {
                        logger('fbsync_createpost: error fetching fbid ' . $media->photo->fbid . ' ' . print_r($data, true), LOGGER_DEBUG);
                    }
                }
            }
            $pagedata["images"][0]["src"] = fbpost_cleanpicture($pagedata["images"][0]["src"]);
            if (isset($media->href) and $pagedata["images"][0]["src"] != "" and $media->href != "") {
                $media->href = original_url($media->href);
                $pagedata["url"] = $media->href;
                $content .= "\n" . '[url=' . $media->href . '][img]' . $pagedata["images"][0]["src"] . '[/img][/url]';
            } else {
                if ($pagedata["images"][0]["src"] != "") {
                    $content .= "\n" . '[img]' . $pagedata["images"][0]["src"] . '[/img]';
                }
                // if just a link, it may be a wall photo - check
                if (isset($post->link)) {
                    $content .= fbpost_get_photo($media->href);
                }
            }
        }
    }
    if ($pagedata["type"] != "") {
        if ($pagedata["type"] == "link") {
            $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK;
        }
        $postarray["body"] .= add_page_info_data($pagedata);
    } else {
        if ($content) {
            $postarray["body"] .= "\n" . trim($content);
        }
        if ($pagedata["text"]) {
            $postarray["body"] .= "\n[quote]" . trim($pagedata["text"]) . "[/quote]";
        }
        $postarray["body"] = trim($postarray["body"]);
    }
    if (trim($postarray["body"]) == "") {
        return;
    }
    if ($prebody != "") {
        $postarray["body"] = $prebody . $postarray["body"] . "[/share]";
    }
    $postarray['created'] = datetime_convert('UTC', 'UTC', date("c", $post->created_time));
    $postarray['edited'] = datetime_convert('UTC', 'UTC', date("c", $post->updated_time));
    $postarray['app'] = $applications[$post->app_id]->display_name;
    if ($postarray['app'] == "") {
        $postarray['app'] = "Facebook";
    }
    if (isset($post->privacy) && $post->privacy->value !== '') {
        $postarray['private'] = 1;
        $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
    }
    /*
    $postarray["location"] = $post->place->name;
    postarray["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
    */
    //$types = array(46, 80, 237, 247, 308);
    //if (!in_array($post->type, $types))
    //	$postarray["body"] = "Type: ".$post->type."\n".$postarray["body"];
    //print_r($post);
    //print_r($postarray);
    $item = item_store($postarray);
    logger('fbsync_createpost: User ' . $self[0]["nick"] . ' posted feed item ' . $item, LOGGER_DEBUG);
}