]> git.mxchange.org Git - friendica.git/blobdiff - addon/statusnet/statusnet.php
infrastructure for personalised @ tags (no UI/settings form yet), allow own comments...
[friendica.git] / addon / statusnet / statusnet.php
index 25aad028d6b4ee9cb73f6cd586820371e91e445c..f1b35d6c04ff4248d797490919c0d6ff22b400d2 100644 (file)
@@ -1,11 +1,16 @@
 <?php
-
+/**
+ * Name: StatusNet Connector
+ * Version: 1.0.2
+ * Author: Tobias Diekershoff <https://diekershoff.homeunix.net/friendika/profile/tobias>
+ */
 /*   StatusNet Plugin for Friendika
  *
  *   Author: Tobias Diekershoff
  *           tobias.diekershoff@gmx.net
  *
- *   License:3-clause BSD license (same as Friendika)
+ *   License:3-clause BSD license
  *
  *   Configuration:
  *     To activate the plugin itself add it to the $a->config['system']['addon']
@@ -46,6 +51,57 @@ class StatusNetOAuth extends TwitterOAuth {
         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 friendika
+   */
+  function http($url, $method, $postfields = NULL) {
+    $this->http_info = array();
+    $ci = curl_init();
+    /* Curl settings */
+    $prx = get_config('system','proxy');
+    logger('Proxy SN: '.$prx);
+    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;
+  }
 }
 
 function statusnet_install() {
@@ -54,7 +110,6 @@ function statusnet_install() {
        register_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
        register_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
        register_hook('jot_networks',    'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
-
        logger("installed statusnet");
 }
 
@@ -85,6 +140,9 @@ function statusnet_jot_nets(&$a,&$b) {
 function statusnet_settings_post ($a,$post) {
        if(! local_user())
            return;
+       // don't check statusnet settings if statusnet submit button is not clicked
+       if (!x($_POST,'statusnet-submit')) return;
+       
        if (isset($_POST['statusnet-disconnect'])) {
             /***
              * if the statusnet-disconnect checkbox is set, clear the statusnet configuration
@@ -148,28 +206,28 @@ function statusnet_settings_post ($a,$post) {
                 goaway($a->get_baseurl().'/settings/addon');
             } else {
                if (isset($_POST['statusnet-pin'])) {
-                   //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
-                   logger('got a StatusNet security code');
+                       //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
+                       logger('got a StatusNet security code');
                     $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
-                   $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
-                   $csecret = get_pconfig(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 Twitter 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
-                   set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
-                   set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
+                                       $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
+                                       $csecret = get_pconfig(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 Twitter 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
+                                       set_pconfig(local_user(),'statusnet', 'oauthtoken',  $token['oauth_token']);
+                                       set_pconfig(local_user(),'statusnet', 'oauthsecret', $token['oauth_token_secret']);
                     set_pconfig(local_user(),'statusnet', 'post', 1);
                     //  reload the Addon Settings page, if we don't do it see Bug #42
                     goaway($a->get_baseurl().'/settings/addon');
-               } else {
-                   //  if no PIN is supplied in the POST variables, the user has changed the setting
-                   //  to post a tweet for every new __public__ posting to the wall
-                   set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
-                   set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
-                       notice( t('StatusNet settings updated.') . EOL);
+                               } else {
+                                       //  if no PIN is supplied in the POST variables, the user has changed the setting
+                                       //  to post a tweet for every new __public__ posting to the wall
+                                       set_pconfig(local_user(),'statusnet','post',intval($_POST['statusnet-enable']));
+                                       set_pconfig(local_user(),'statusnet','post_by_default',intval($_POST['statusnet-default']));
+                                       info( t('StatusNet settings updated.') . EOL);
                }}}}
 }
 function statusnet_settings(&$a,&$s) {
@@ -207,13 +265,13 @@ function statusnet_settings(&$a,&$s) {
              */
             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 .= '<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 \x28see below\x29.") .'</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="submit" class="settings-submit" value="' . t('Submit') . '" /></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 Friendika 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 Friendika installation at your favorited StatusNet installation.') .'</p>';
@@ -227,7 +285,7 @@ function statusnet_settings(&$a,&$s) {
             $s .= '<label id="statusnet-baseapi-label" for="statusnet-baseapi">'. t("Base API Path \x28remember the trailing /\x29") .'</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="submit" class="settings-submit" value="' . t('Submit') . '" /></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
@@ -252,15 +310,15 @@ function statusnet_settings(&$a,&$s) {
                        $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="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="submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
+                       $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
@@ -269,7 +327,7 @@ function statusnet_settings(&$a,&$s) {
                        $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 will be posted to the associated StatusNet account.') .'</p>';
+                       $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>';
                        $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 . '/>';
@@ -282,7 +340,7 @@ function statusnet_settings(&$a,&$s) {
                         $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="submit" class="settings-submit" value="' . t('Submit') . '" /></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>';
@@ -297,12 +355,19 @@ function statusnet_post_hook(&$a,&$b) {
 
         logger('StatusNet post invoked');
 
-       if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (!$b['parent']) ) {
+       if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) {
+
+               // mike 2-9-11 there was a restriction to only allow this for top level posts
+               // now relaxed so should allow one's own comments to be forwarded through the connector as well. 
 
-                load_pconfig(local_user(), 'statusnet');
+               // Status.Net is not considered a private network
+               if($b['prvnets'])
+                       return;
+
+               load_pconfig(local_user(), 'statusnet');
             
-                $api     = get_pconfig(local_user(), 'statusnet', 'baseapi');
-                $ckey    = get_pconfig(local_user(), 'statusnet', 'consumerkey'  );
+               $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' );
@@ -311,6 +376,9 @@ function statusnet_post_hook(&$a,&$b) {
 
                        $statusnet_post = get_pconfig(local_user(),'statusnet','post');
                        $statusnet_enable = (($statusnet_post && x($_POST,'statusnet_enable')) ? intval($_POST['statusnet_enable']) : 0);
+                       // if API is used, default to the chosen settings
+                       if($_POST['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default')))
+                               $statusnet_enable = 1;
 
                        if($statusnet_enable && $statusnet_post) {
                                require_once('include/bbcode.php');     
@@ -345,3 +413,66 @@ function statusnet_post_hook(&$a,&$b) {
     }
 }
 
+function statusnet_plugin_admin_post(&$a){
+       
+       $sites = array();
+       
+       foreach($_POST['sitename'] as $id=>$sitename){
+               $sitename=trim($sitename);
+               $apiurl=trim($_POST['apiurl'][$id]);
+               $secret=trim($_POST['secret'][$id]);
+               $key=trim($_POST['key'][$id]);
+               if ($sitename!="" &&
+                       $apiurl!="" &&
+                       $secret!="" &&
+                       $key!="" &&
+                       !x($_POST['delete'][$id])){
+                               
+                               $sites[] = Array(
+                                       'sitename' => $sitename,
+                                       'apiurl' => $apiurl,
+                                       'consumersecret' => $secret,
+                                       'consumerkey' => $key
+                               );
+               }
+       }
+       
+       $sites = set_config('statusnet','sites', $sites);
+       
+}
+
+function statusnet_plugin_admin(&$a, &$o){
+
+       $sites = get_config('statusnet','sites');
+       $sitesform=array();
+       if (is_array($sites)){
+               foreach($sites as $id=>$s){
+                       $sitesform[] = Array(
+                               'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""),
+                               'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], ""),
+                               'secret' => Array("secret[$id]", "Secret", $s['consumersecret'], ""),
+                               'key' => Array("key[$id]", "Key", $s['consumerkey'], ""),
+                               'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"),
+                       );
+               }
+       }
+       /* empty form to add new site */
+       $id++;
+       $sitesform[] = Array(
+               'sitename' => Array("sitename[$id]", t("Site name"), "", ""),
+               'apiurl' => Array("apiurl[$id]", t("API URL"), "", ""),
+               'secret' => Array("secret[$id]", t("Consumer Secret"), "", ""),
+               'key' => Array("key[$id]", t("Consumer Key"), "", ""),
+       );
+
+       
+       $t = file_get_contents( dirname(__file__). "/admin.tpl" );
+       $o = replace_macros($t, array(
+               '$submit' => t('Submit'),
+                                                       
+               '$sites' => $sitesform,
+               
+       ));
+       
+       
+}