X-Git-Url: https://git.mxchange.org/?p=friendica-addons.git;a=blobdiff_plain;f=statusnet%2Fstatusnet.php;h=16f68a8aa1f5b36f5cf190ebdcddb4b8278b9896;hp=9c44ef0236b68182a2e2e2bf48e31491d6dc6743;hb=39dd3dffe07efd69fa1ac6d0bd243c7fc0e3a66f;hpb=092221d4717bb40354d12ba201f9466a843514cc
diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php
index 9c44ef02..16f68a8a 100644
--- a/statusnet/statusnet.php
+++ b/statusnet/statusnet.php
@@ -32,115 +32,57 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
-
-
-/***
- * We have to alter the TwitterOAuth class a little bit to work with any GNU Social
- * installation abroad. Basically it's only make the API path variable and be happy.
- *
- * Thank you guys for the Twitter compatible API!
- */
-
define('STATUSNET_DEFAULT_POLL_INTERVAL', 5); // given in minutes
-require_once('library/twitteroauth.php');
-require_once('include/enotify.php');
-require_once("include/socgraph.php");
-
-class StatusNetOAuth extends TwitterOAuth {
- function get_maxlength() {
- $config = $this->get($this->host . 'statusnet/config.json');
- return $config->site->textlimit;
- }
- function accessTokenURL() { return $this->host.'oauth/access_token'; }
- function authenticateURL() { return $this->host.'oauth/authenticate'; }
- function authorizeURL() { return $this->host.'oauth/authorize'; }
- function requestTokenURL() { return $this->host.'oauth/request_token'; }
- function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
- parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
- $this->host = $apipath;
- }
- /**
- * Make an HTTP request
- *
- * @return API results
- *
- * Copied here from the twitteroauth library and complemented by applying the proxy settings of friendica
- */
- function http($url, $method, $postfields = NULL) {
- $this->http_info = array();
- $ci = curl_init();
- /* Curl settings */
- $prx = get_config('system','proxy');
- if(strlen($prx)) {
- curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1);
- curl_setopt($ci, CURLOPT_PROXY, $prx);
- $prxusr = get_config('system','proxyuser');
- if(strlen($prxusr))
- curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr);
- }
- curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
- curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
- curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
- curl_setopt($ci, CURLOPT_HEADER, FALSE);
-
- switch ($method) {
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, TRUE);
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- }
- break;
- case 'DELETE':
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- if (!empty($postfields)) {
- $url = "{$url}?{$postfields}";
- }
- }
-
- curl_setopt($ci, CURLOPT_URL, $url);
- $response = curl_exec($ci);
- $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
- $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
- $this->url = $url;
- curl_close ($ci);
- return $response;
- }
-}
+require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'statusnetoauth.php';
+require_once 'include/enotify.php';
+
+use Codebird\Codebird;
+use CodebirdSN\CodebirdSN;
+use Friendica\App;
+use Friendica\Content\OEmbed;
+use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\Plaintext;
+use Friendica\Core\Addon;
+use Friendica\Core\Config;
+use Friendica\Core\L10n;
+use Friendica\Core\PConfig;
+use Friendica\Model\GContact;
+use Friendica\Model\Group;
+use Friendica\Model\Item;
+use Friendica\Model\Photo;
+use Friendica\Model\User;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Network;
function statusnet_install() {
// we need some hooks, for the configuration and for sending tweets
- register_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
- register_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
- register_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
- register_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
- register_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
- register_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
- register_hook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body');
- register_hook('check_item_notification','addon/statusnet/statusnet.php', 'statusnet_check_item_notification');
+ Addon::registerHook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
+ Addon::registerHook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
+ Addon::registerHook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
+ Addon::registerHook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
+ Addon::registerHook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
+ Addon::registerHook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
+ Addon::registerHook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body');
+ Addon::registerHook('check_item_notification', 'addon/statusnet/statusnet.php', 'statusnet_check_item_notification');
logger("installed GNU Social");
}
-
-function statusnet_uninstall() {
- unregister_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
- unregister_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
- unregister_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
- unregister_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
- unregister_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
- unregister_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
- unregister_hook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body');
- unregister_hook('check_item_notification','addon/statusnet/statusnet.php', 'statusnet_check_item_notification');
+function statusnet_uninstall()
+{
+ Addon::unregisterHook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
+ Addon::unregisterHook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
+ Addon::unregisterHook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
+ Addon::unregisterHook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
+ Addon::unregisterHook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
+ Addon::unregisterHook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
+ Addon::unregisterHook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body');
+ Addon::unregisterHook('check_item_notification', 'addon/statusnet/statusnet.php', 'statusnet_check_item_notification');
// old setting - remove only
- unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
- unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
- unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
-
+ Addon::unregisterHook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
+ Addon::unregisterHook('addon_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
+ Addon::unregisterHook('addon_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
}
function statusnet_check_item_notification($a, &$notification_data) {
@@ -155,8 +97,8 @@ function statusnet_jot_nets(&$a,&$b) {
if(intval($statusnet_post) == 1) {
$statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default');
$selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : '');
- $b .= '
'
- . t('Post to GNU Social') . '
';
+ $b .= '
'
+ . L10n::t('Post to GNU Social') . '
';
}
}
@@ -184,24 +126,53 @@ function statusnet_settings_post ($a,$post) {
del_pconfig(local_user(), 'statusnet', 'create_user');
del_pconfig(local_user(), 'statusnet', 'own_id');
} else {
- if (isset($_POST['statusnet-preconf-apiurl'])) {
- /***
- * If the user used one of the preconfigured GNU Social server credentials
- * use them. All the data are available in the global config.
- * Check the API Url never the less and blame the admin if it's not working ^^
- */
- $globalsn = get_config('statusnet', 'sites');
- foreach ( $globalsn as $asn) {
- if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) {
- $apibase = $asn['apiurl'];
- $c = fetch_url( $apibase . 'statusnet/version.xml' );
+ if (isset($_POST['statusnet-preconf-apiurl'])) {
+ /* * *
+ * If the user used one of the preconfigured GNU Social server credentials
+ * use them. All the data are available in the global config.
+ * Check the API Url never the less and blame the admin if it's not working ^^
+ */
+ $globalsn = Config::get('statusnet', 'sites');
+ foreach ($globalsn as $asn) {
+ if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl']) {
+ $apibase = $asn['apiurl'];
+ $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
+ if (strlen($c) > 0) {
+ PConfig::set(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey']);
+ PConfig::set(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret']);
+ PConfig::set(local_user(), 'statusnet', 'baseapi', $asn['apiurl']);
+ //PConfig::set(local_user(), 'statusnet', 'application_name', $asn['applicationname'] );
+ } else {
+ notice(L10n::t('Please contact your site administrator. The provided API URL is not valid.') . EOL . $asn['apiurl'] . EOL);
+ }
+ }
+ }
+ goaway('settings/connectors');
+ } else {
+ if (isset($_POST['statusnet-consumersecret'])) {
+ // check if we can reach the API of the GNU Social server
+ // we'll check the API Version for that, if we don't get one we'll try to fix the path but will
+ // resign quickly after this one try to fix the path ;-)
+ $apibase = $_POST['statusnet-baseapi'];
+ $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
if (strlen($c) > 0) {
set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] );
set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] );
set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] );
//set_pconfig(local_user(), 'statusnet', 'application_name', $asn['applicationname'] );
} else {
- notice( t('Please contact your site administrator. The provided API URL is not valid.').EOL.$asn['apiurl'].EOL );
+ // the API path is not correct, maybe missing trailing / ?
+ $apibase = $apibase . '/';
+ $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
+ if (strlen($c) > 0) {
+ // ok the API path is now correct, let's save the settings
+ PConfig::set(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
+ PConfig::set(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
+ PConfig::set(local_user(), 'statusnet', 'baseapi', $apibase);
+ } else {
+ // still not the correct API base, let's do noting
+ notice(L10n::t('We could not contact the GNU Social API with the Path you entered.') . EOL);
+ }
}
}
}
@@ -229,8 +200,37 @@ function statusnet_settings_post ($a,$post) {
set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']);
set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase );
} else {
- // still not the correct API base, let's do noting
- notice( t('We could not contact the GNU Social API with the Path you entered.').EOL );
+ if (isset($_POST['statusnet-pin'])) {
+ // if the user supplied us with a PIN from GNU Social, let the magic of OAuth happen
+ $api = PConfig::get(local_user(), 'statusnet', 'baseapi');
+ $ckey = PConfig::get(local_user(), 'statusnet', 'consumerkey');
+ $csecret = PConfig::get(local_user(), 'statusnet', 'consumersecret');
+ // the token and secret for which the PIN was generated were hidden in the settings
+ // form as token and token2, we need a new connection to GNU Social using these token
+ // and secret to request a Access Token with the PIN
+ $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']);
+ $token = $connection->getAccessToken($_POST['statusnet-pin']);
+ // ok, now that we have the Access Token, save them in the user config
+ PConfig::set(local_user(), 'statusnet', 'oauthtoken', $token['oauth_token']);
+ PConfig::set(local_user(), 'statusnet', 'oauthsecret', $token['oauth_token_secret']);
+ PConfig::set(local_user(), 'statusnet', 'post', 1);
+ PConfig::set(local_user(), 'statusnet', 'post_taglinks', 1);
+ // reload the Addon Settings page, if we don't do it see Bug #42
+ goaway('settings/connectors');
+ } else {
+ // if no PIN is supplied in the POST variables, the user has changed the setting
+ // to post a dent for every new __public__ posting to the wall
+ PConfig::set(local_user(), 'statusnet', 'post', intval($_POST['statusnet-enable']));
+ PConfig::set(local_user(), 'statusnet', 'post_by_default', intval($_POST['statusnet-default']));
+ PConfig::set(local_user(), 'statusnet', 'mirror_posts', intval($_POST['statusnet-mirror']));
+ PConfig::set(local_user(), 'statusnet', 'import', intval($_POST['statusnet-import']));
+ PConfig::set(local_user(), 'statusnet', 'create_user', intval($_POST['statusnet-create_user']));
+
+ if (!intval($_POST['statusnet-mirror']))
+ PConfig::delete(local_user(), 'statusnet', 'lastid');
+
+ info(L10n::t('GNU Social settings updated.') . EOL);
+ }
}
}
goaway($a->get_baseurl().'/settings/connectors');
@@ -299,11 +299,11 @@ function statusnet_settings(&$a,&$s) {
$css = (($enabled) ? '' : '-disabled');
$s .= '';
- $s .= '
'. t('GNU Social Import/Export/Mirror').'
';
+ $s .= '
' . L10n::t('GNU Social Import/Export/Mirror') . '
';
$s .= '';
$s .= '
';
$s .= '';
- $s .= '
'. t('GNU Social Import/Export/Mirror').'
';
+ $s .= '
' . L10n::t('GNU Social Import/Export/Mirror') . '
';
$s .= '';
if ( (!$ckey) && (!$csecret) ) {
@@ -317,32 +317,32 @@ function statusnet_settings(&$a,&$s) {
* with a little explanation to the user as choice - otherwise
* ignore this option entirely.
*/
- if (! $globalsn == null) {
- $s .= '
' . t('Globally Available GNU Social OAuthKeys') . '
';
- $s .= '
'. t("There are preconfigured OAuth key pairs for some GNU Social servers available. If you are using one of them, please use these credentials. If not feel free to connect to any other GNU Social instance \x28see below\x29.") .'
';
+ if (!$globalsn == null) {
+ $s .= '
' . L10n::t('Globally Available GNU Social OAuthKeys') . '
';
+ $s .= '
' . L10n::t("There are preconfigured OAuth key pairs for some GNU Social servers available. If you are using one of them, please use these credentials. If not feel free to connect to any other GNU Social instance \x28see below\x29.") . '
'. t('No consumer key pair for GNU Social found. Register your Friendica Account as an desktop client on your GNU Social account, copy the consumer key pair here and enter the API base root. 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 GNU Social installation.') .'
';
+ $s .= '
' . L10n::t('Provide your own OAuth Credentials') . '
';
+ $s .= '
' . L10n::t('No consumer key pair for GNU Social found. Register your Friendica Account as an desktop client on your GNU Social account, copy the consumer key pair here and enter the API base root. 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 GNU Social installation.') . '
';
} else {
/***
@@ -361,22 +361,22 @@ function statusnet_settings(&$a,&$s) {
/***
* make some nice form
*/
- $s .= '
'. t('To connect to your GNU Social account click the button below to get a security code from GNU Social which you have to copy into the input box below and submit the form. Only your public posts will be posted to GNU Social.') .'
';
- $s .= '';
+ $s .= '
' . L10n::t('To connect to your GNU Social account click the button below to get a security code from GNU Social which you have to copy into the input box below and submit the form. Only your public posts will be posted to GNU Social.') . '
'. t('If enabled all your public postings can be posted to the associated GNU Social account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') .'
' . L10n::t('If enabled all your public postings can be posted to the associated GNU Social account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') . '
';
if ($a->user['hidewall']) {
- $s .= '
'. t('Note: Due your privacy settings (Hide your profile details from unknown viewers?) the link potentially included in public postings relayed to GNU Social will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') .'
';
+ $s .= '
' . L10n::t('Note: Due your privacy settings (Hide your profile details from unknown viewers?) the link potentially included in public postings relayed to GNU Social will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '