Exemplo n.º 1
0
function statusnet_is_retweet($a, $uid, $body)
{
    $body = trim($body);
    // Skip if it isn't a pure repeated messages
    // Does it start with a share?
    if (strpos($body, "[share") > 0) {
        return false;
    }
    // Does it end with a share?
    if (strlen($body) > strrpos($body, "[/share]") + 8) {
        return false;
    }
    $attributes = preg_replace("/\\[share(.*?)\\]\\s?(.*?)\\s?\\[\\/share\\]\\s?/ism", "\$1", $body);
    // Skip if there is no shared message in there
    if ($body == $attributes) {
        return false;
    }
    $link = "";
    preg_match("/link='(.*?)'/ism", $attributes, $matches);
    if ($matches[1] != "") {
        $link = $matches[1];
    }
    preg_match('/link="(.*?)"/ism', $attributes, $matches);
    if ($matches[1] != "") {
        $link = $matches[1];
    }
    $ckey = get_pconfig($uid, 'statusnet', 'consumerkey');
    $csecret = get_pconfig($uid, 'statusnet', 'consumersecret');
    $api = get_pconfig($uid, 'statusnet', 'baseapi');
    $otoken = get_pconfig($uid, 'statusnet', 'oauthtoken');
    $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret');
    $hostname = preg_replace("=https?://([\\w\\.]*)/.*=ism", "\$1", $api);
    $id = preg_replace("=https?://" . $hostname . "/notice/(.*)=ism", "\$1", $link);
    if ($id == $link) {
        return false;
    }
    logger('statusnet_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, LOGGER_DEBUG);
    $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
    $result = $connection->post('statuses/retweet/' . $id);
    logger('statusnet_is_retweet: result ' . print_r($result, true), LOGGER_DEBUG);
    return isset($result->id);
}
Exemplo n.º 2
0
function statusnet_fetchtimeline($a, $uid)
{
    $ckey = get_pconfig($uid, 'statusnet', 'consumerkey');
    $csecret = get_pconfig($uid, 'statusnet', 'consumersecret');
    $api = get_pconfig($uid, 'statusnet', 'baseapi');
    $otoken = get_pconfig($uid, 'statusnet', 'oauthtoken');
    $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret');
    $lastid = get_pconfig($uid, 'statusnet', 'lastid');
    //  get the application name for the SN app
    //  1st try personal config, then system config and fallback to the
    //  hostname of the node if neither one is set.
    $application_name = get_pconfig($uid, 'statusnet', 'application_name');
    if ($application_name == "") {
        $application_name = get_config('statusnet', 'application_name');
    }
    if ($application_name == "") {
        $application_name = App::get_hostname();
    }
    $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
    $parameters = array("exclude_replies" => true, "trim_user" => true, "contributor_details" => false, "include_rts" => false);
    $first_time = $lastid == "";
    if ($lastid != "") {
        $parameters["since_id"] = $lastid;
    }
    $items = $connection->get('statuses/user_timeline', $parameters);
    if (!is_array($items)) {
        return;
    }
    $posts = array_reverse($items);
    if (count($posts)) {
        foreach ($posts as $post) {
            if ($post->id > $lastid) {
                $lastid = $post->id;
            }
            if ($first_time) {
                continue;
            }
            if (is_object($post->retweeted_status)) {
                continue;
            }
            if ($post->in_reply_to_status_id != "") {
                continue;
            }
            if (!strpos($post->source, $application_name)) {
                $_REQUEST["type"] = "wall";
                $_REQUEST["api_source"] = true;
                $_REQUEST["profile_uid"] = $uid;
                $_REQUEST["source"] = "StatusNet";
                //$_REQUEST["date"] = $post->created_at;
                $_REQUEST["body"] = $post->text;
                if (is_string($post->place->name)) {
                    $_REQUEST["location"] = $post->place->name;
                }
                if (is_string($post->place->full_name)) {
                    $_REQUEST["location"] = $post->place->full_name;
                }
                if (is_array($post->geo->coordinates)) {
                    $_REQUEST["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
                }
                if (is_array($post->coordinates->coordinates)) {
                    $_REQUEST["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
                }
                //print_r($_REQUEST);
                if ($_REQUEST["body"] != "") {
                    logger('statusnet: posting for user ' . $uid);
                    $mod = new Zotlabs\Module\Item();
                    $mod->post();
                }
            }
        }
    }
    set_pconfig($uid, 'statusnet', 'lastid', $lastid);
}
Exemplo n.º 3
0
function statusnet_post_hook(&$a, &$b)
{
    /**
     * Post to statusnet
     */
    if ($b['deleted'] || $b['private'] || $b['created'] !== $b['edited']) {
        return;
    }
    if (!strstr($b['postopts'], 'statusnet')) {
        return;
    }
    load_pconfig($b['uid'], 'statusnet');
    $api = get_pconfig($b['uid'], 'statusnet', 'baseapi');
    $ckey = get_pconfig($b['uid'], 'statusnet', 'consumerkey');
    $csecret = get_pconfig($b['uid'], 'statusnet', 'consumersecret');
    $otoken = get_pconfig($b['uid'], 'statusnet', 'oauthtoken');
    $osecret = get_pconfig($b['uid'], 'statusnet', 'oauthsecret');
    if ($ckey && $csecret && $otoken && $osecret) {
        require_once 'include/bbcode.php';
        $dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
        $max_char = $dent->get_maxlength();
        // max. length for a dent
        // we will only work with up to two times the length of the dent
        // we can later send to StatusNet. This way we can "gain" some
        // information during shortening of potential links but do not
        // shorten all the links in a 200000 character long essay.
        if (!$b['title'] == '') {
            $tmp = $b['title'] . ' : ' . $b['body'];
            //                    $tmp = substr($tmp, 0, 4*$max_char);
        } else {
            $tmp = $b['body'];
            // substr($b['body'], 0, 3*$max_char);
        }
        // if [url=bla][img]blub.png[/img][/url] get blub.png
        $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\]\\[img\\](\\w+.*?)\\[\\/img\\]\\[\\/url\\]/i', '$2', $tmp);
        // preserve links to images, videos and audios
        $tmp = preg_replace('/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism', '$3', $tmp);
        $tmp = preg_replace('/\\[\\/?img(\\s+.*?\\]|\\])/i', '', $tmp);
        $tmp = preg_replace('/\\[\\/?video(\\s+.*?\\]|\\])/i', '', $tmp);
        $tmp = preg_replace('/\\[\\/?youtube(\\s+.*?\\]|\\])/i', '', $tmp);
        $tmp = preg_replace('/\\[\\/?vimeo(\\s+.*?\\]|\\])/i', '', $tmp);
        $tmp = preg_replace('/\\[\\/?audio(\\s+.*?\\]|\\])/i', '', $tmp);
        $linksenabled = get_pconfig($b['uid'], 'statusnet', 'post_taglinks');
        // if a #tag is linked, don't send the [url] over to SN
        // that is, don't send if the option is not set in the
        // connector settings
        if ($linksenabled == '0') {
            // #-tags
            $tmp = preg_replace('/#\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '#$2', $tmp);
            // @-mentions
            $tmp = preg_replace('/@\\[url\\=(\\w+.*?)\\](\\w+.*?)\\[\\/url\\]/i', '@$2', $tmp);
        }
        // preserve links to webpages
        $tmp = preg_replace('/\\[url\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/url\\]/i', '$2 $1', $tmp);
        $tmp = preg_replace('/\\[bookmark\\=(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)\\](\\w+.*?)\\[\\/bookmark\\]/i', '$2 $1', $tmp);
        // find all http or https links in the body of the entry and
        // apply the shortener if the link is longer then 20 characters
        if (strlen($tmp) > $max_char && $max_char > 0) {
            preg_match_all('/(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\\$\\!\\+\\,]+)/i', $tmp, $allurls);
            foreach ($allurls as $url) {
                foreach ($url as $u) {
                    if (strlen($u) > 20) {
                        $sl = short_link($u);
                        $tmp = str_replace($u, $sl, $tmp);
                    }
                }
            }
        }
        // ok, all the links we want to send out are save, now strip
        // away the remaining bbcode
        $msg = strip_tags(bbcode($tmp));
        // quotes not working - let's try this
        $msg = html_entity_decode($msg);
        if (strlen($msg) > $max_char && $max_char > 0) {
            $shortlink = short_link($b['plink']);
            // the new message will be shortened such that "... $shortlink"
            // will fit into the character limit
            $msg = nl2br(substr($msg, 0, $max_char - strlen($shortlink) - 4));
            $msg = str_replace(array('<br>', '<br />'), ' ', $msg);
            $e = explode(' ', $msg);
            //  remove the last word from the cut down message to
            //  avoid sending cut words to the MicroBlog
            array_pop($e);
            $msg = implode(' ', $e);
            $msg .= '... ' . $shortlink;
        }
        // and now dent it :-)
        if (strlen($msg)) {
            $result = $dent->post('statuses/update', array('status' => $msg));
            logger('statusnet_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
            if ($result->error) {
                logger('Send to StatusNet failed: "' . $result->error . '"');
            }
        }
    }
}