function statusnet_fetch_own_contact($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');
    $own_url = get_pconfig($uid, 'statusnet', 'own_url');
    $contact_id = 0;
    if ($own_url == "") {
        require_once 'library/twitteroauth.php';
        $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
        // Fetching user data
        $user = $connection->get('account/verify_credentials');
        set_pconfig($uid, 'statusnet', 'own_url', normalise_link($user->statusnet_profile_url));
        $contact_id = statusnet_fetch_contact($uid, $user, true);
    } else {
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", intval($uid), dbesc($own_url));
        if (count($r)) {
            $contact_id = $r[0]["id"];
        } else {
            del_pconfig($uid, 'statusnet', 'own_url');
        }
    }
    return $contact_id;
}
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);
}
function statusnet_settings(&$a, &$s)
{
    if (!local_user()) {
        return;
    }
    $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/statusnet/statusnet.css' . '" media="all" />' . "\r\n";
    /***
     * 1) Check that we have a base api url and a consumer key & secret
     * 2) If no OAuthtoken & stuff is present, generate button to get some
     *    allow the user to cancel the connection process at this step
     * 3) Checkbox for "Send public notices (respect size limitation)
     */
    $api = get_pconfig(local_user(), 'statusnet', 'baseapi');
    $ckey = get_pconfig(local_user(), 'statusnet', 'consumerkey');
    $csecret = get_pconfig(local_user(), 'statusnet', 'consumersecret');
    $otoken = get_pconfig(local_user(), 'statusnet', 'oauthtoken');
    $osecret = get_pconfig(local_user(), 'statusnet', 'oauthsecret');
    $enabled = get_pconfig(local_user(), 'statusnet', 'post');
    $checked = $enabled ? ' checked="checked" ' : '';
    $defenabled = get_pconfig(local_user(), 'statusnet', 'post_by_default');
    $defchecked = $defenabled ? ' checked="checked" ' : '';
    $linksenabled = get_pconfig(local_user(), 'statusnet', 'post_taglinks');
    $linkschecked = $linksenabled ? ' checked="checked" ' : '';
    $s .= '<div class="settings-block">';
    $s .= '<h3>' . t('StatusNet Posting Settings') . '</h3>';
    if (!$ckey && !$csecret) {
        /***
         * no consumer keys
         */
        $globalsn = get_config('statusnet', 'sites');
        /***
         * lets check if we have one or more globally configured StatusNet
         * server OAuth credentials in the configuration. If so offer them
         * with a little explanation to the user as choice - otherwise
         * ignore this option entirely.
         */
        if (!$globalsn == null) {
            $s .= '<h4>' . t('Globally Available StatusNet OAuthKeys') . '</h4>';
            $s .= '<p>' . t("There are preconfigured OAuth key pairs for some StatusNet servers available. If you are useing one of them, please use these credentials. If not feel free to connect to any other StatusNet instance (see below).") . '</p>';
            $s .= '<div id="statusnet-preconf-wrapper">';
            foreach ($globalsn as $asn) {
                $s .= '<input type="radio" name="statusnet-preconf-apiurl" value="' . $asn['apiurl'] . '">' . $asn['sitename'] . '<br />';
            }
            $s .= '<p></p><div class="clear"></div></div>';
            $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
        }
        $s .= '<h4>' . t('Provide your own OAuth Credentials') . '</h4>';
        $s .= '<p>' . t('No consumer key pair for StatusNet found. Register your Friendica Account as an desktop client on your StatusNet account, copy the consumer key pair here and enter the API base root.<br />Before you register your own OAuth key pair ask the administrator if there is already a key pair for this Friendica installation at your favorited StatusNet installation.') . '</p>';
        $s .= '<div id="statusnet-consumer-wrapper">';
        $s .= '<label id="statusnet-consumerkey-label" for="statusnet-consumerkey">' . t('OAuth Consumer Key') . '</label>';
        $s .= '<input id="statusnet-consumerkey" type="text" name="statusnet-consumerkey" size="35" /><br />';
        $s .= '<div class="clear"></div>';
        $s .= '<label id="statusnet-consumersecret-label" for="statusnet-consumersecret">' . t('OAuth Consumer Secret') . '</label>';
        $s .= '<input id="statusnet-consumersecret" type="text" name="statusnet-consumersecret" size="35" /><br />';
        $s .= '<div class="clear"></div>';
        $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">' . t("Base API Path (remember the trailing /)") . '</label>';
        $s .= '<input id="statusnet-baseapi" type="text" name="statusnet-baseapi" size="35" /><br />';
        $s .= '<p></p><div class="clear"></div></div>';
        $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
    } else {
        /***
         * ok we have a consumer key pair now look into the OAuth stuff
         */
        if (!$otoken && !$osecret) {
            /***
             * the user has not yet connected the account to statusnet
             * get a temporary OAuth key/secret pair and display a button with
             * which the user can request a PIN to connect the account to a
             * account at statusnet
             */
            $connection = new StatusNetOAuth($api, $ckey, $csecret);
            $request_token = $connection->getRequestToken('oob');
            $token = $request_token['oauth_token'];
            /***
             *  make some nice form
             */
            $s .= '<p>' . t('To connect to your StatusNet account click the button below to get a security code from StatusNet which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to StatusNet.') . '</p>';
            $s .= '<a href="' . $connection->getAuthorizeURL($token, False) . '" target="_statusnet"><img src="addon/statusnet/signinwithstatusnet.png" alt="' . t('Log in with StatusNet') . '"></a>';
            $s .= '<div id="statusnet-pin-wrapper">';
            $s .= '<label id="statusnet-pin-label" for="statusnet-pin">' . t('Copy the security code from StatusNet here') . '</label>';
            $s .= '<input id="statusnet-pin" type="text" name="statusnet-pin" />';
            $s .= '<input id="statusnet-token" type="hidden" name="statusnet-token" value="' . $token . '" />';
            $s .= '<input id="statusnet-token2" type="hidden" name="statusnet-token2" value="' . $request_token['oauth_token_secret'] . '" />';
            $s .= '</div><div class="clear"></div>';
            $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
            $s .= '<h4>' . t('Cancel Connection Process') . '</h4>';
            $s .= '<div id="statusnet-cancel-wrapper">';
            $s .= '<p>' . t('Current StatusNet API is') . ': ' . $api . '</p>';
            $s .= '<label id="statusnet-cancel-label" for="statusnet-cancel">' . t('Cancel StatusNet Connection') . '</label>';
            $s .= '<input id="statusnet-cancel" type="checkbox" name="statusnet-disconnect" value="1" />';
            $s .= '</div><div class="clear"></div>';
            $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
        } else {
            /***
             *  we have an OAuth key / secret pair for the user
             *  so let's give a chance to disable the postings to statusnet
             */
            $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret);
            $details = $connection->get('account/verify_credentials');
            $s .= '<div id="statusnet-info" ><img id="statusnet-avatar" src="' . $details->profile_image_url . '" /><p id="statusnet-info-block">' . t('Currently connected to: ') . '<a href="' . $details->statusnet_profile_url . '" target="_statusnet">' . $details->screen_name . '</a><br /><em>' . $details->description . '</em></p></div>';
            $s .= '<p>' . t('If enabled all your <strong>public</strong> postings can be posted to the associated StatusNet account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') . '</p>';
            if ($a->user['hidewall']) {
                $s .= '<p>' . t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to StatusNet will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
            }
            $s .= '<div id="statusnet-enable-wrapper">';
            $s .= '<label id="statusnet-enable-label" for="statusnet-checkbox">' . t('Allow posting to StatusNet') . '</label>';
            $s .= '<input id="statusnet-checkbox" type="checkbox" name="statusnet-enable" value="1" ' . $checked . '/>';
            $s .= '<div class="clear"></div>';
            $s .= '<label id="statusnet-default-label" for="statusnet-default">' . t('Send public postings to StatusNet by default') . '</label>';
            $s .= '<input id="statusnet-default" type="checkbox" name="statusnet-default" value="1" ' . $defchecked . '/>';
            $s .= '<div class="clear"></div>';
            $s .= '<label id="statusnet-sendtaglinks-label" for="statusnet-sendtaglinks">' . t('Send linked #-tags and @-names to StatusNet') . '</label>';
            $s .= '<input id="statusnet-sendtaglinks" type="checkbox" name="statusnet-sendtaglinks" value="1" ' . $linkschecked . '/>';
            $s .= '</div><div class="clear"></div>';
            $s .= '<div id="statusnet-disconnect-wrapper">';
            $s .= '<label id="statusnet-disconnect-label" for="statusnet-disconnect">' . t('Clear OAuth configuration') . '</label>';
            $s .= '<input id="statusnet-disconnect" type="checkbox" name="statusnet-disconnect" value="1" />';
            $s .= '</div><div class="clear"></div>';
            $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="statusnet-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
        }
    }
    $s .= '</div><div class="clear"></div></div>';
}