diff --git a/addon/twitter/lighter.png b/addon/twitter/lighter.png new file mode 100644 index 000000000..297bb0340 Binary files /dev/null and b/addon/twitter/lighter.png differ diff --git a/addon/twitter/slinky.php b/addon/twitter/slinky.php new file mode 100644 index 000000000..51432c3f5 --- /dev/null +++ b/addon/twitter/slinky.php @@ -0,0 +1,704 @@ +url_get() and ->url_post() +define( 'SLINKY_BODY', 1 ); // Default +define( 'SLINKY_HEADERS', 2 ); // Not implemented yet +define( 'SLINKY_FINAL_URL', 3 ); // Default for lengthening URLs + +// So that services may decide what to do with us +define( 'SLINKY_USER_AGENT', 'Slinky v1.0 +http://dentedreality.com.au/projects/slinky/' ); + +// How many seconds until remote requests should be cut? +define( 'SLINKY_TIMEOUT', 10 ); + +/** + * Slinky allows you to go back and forth between "long" and shortened URLs + * using popular URL shortening services. + * + * Slinky assumes you have cURL installed and working, and requires the JSON + * extension installed if you're working with a service that uses JSON. + * + * Slinky will ONLY work with PHP5+ + * + * It supports some of the more popular services, with easy extensibility for + * adding your own relatively easily. It defaults to using TinyURL + * for shortening URLs. If you want to use some of the other services, you need + * to set some configuration options before actually shortening/lengthening + * URLs. I'd strongly suggest that you cache results using memcached, a local + * DB or whatever to avoid having to hit APIs etc every time you encounter a + * URL. + * + * Slinky supports shortening, and auto-detection (for lengthening URLs) + * using these services: + * - Bit.ly + * - Tr.im + * - TinyURL + * - Is.Gd + * - Fon.gs + * - Micurl.com + * - ur1.ca + * - Ptiturl + * - Tighturl + * - 2tu.us + * - Snipr / Snipurl / Snurl.com / Sn.im + * + * + * To use Slinky: + * + * $slinky = new Slinky( 'http://dentedreality.com.au/' ); + * - Creates a new Slinky instance, will default to using TinyURL for ->short(); + * + * $slinky = new Slinky( 'http://dentedreality.com.au', new Slinky_Bitly() ); + * - Creates new Slinky, forces use of Bit.ly for ->short(); + * + * $slinky = new Slinky( 'http://dentedreality.com.au/' ); + * echo $slinky->short(); + * - echos the short version of http://dentedreality.com.au/ (default to TinyURL) + * + * $slinky = new Slinky( 'http://tinyurl.com/jw5sh' ); + * echo $slinky->long(); + * - echos out the long version of http://tinyurl.com/jw5sh (which should be http://dentedreality.com.au/) + * + * $slinky = new Slinky( 'http://dentedreality.com.au/' ); + * echo $slinky->long(); + * - Attempts to lengthen the URL, but will not auto-detect which service it is + * so it will just output the original URL. Useful for always attempting to + * lengthen any URL you come across (fails gracefully) + * + * $slinky = new Slinky( 'http://dentedreality.com.au/' ); + * $slinky->set_cascade( array( new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); + * echo $slinky->short(); + * - Uses the powerful cascade mode to make sure that we get a short URL from + * Tr.im, Is.Gd or TinyURL (in that order). + * + * See specific service class definitions below for examples of how to use them, + * as some services allow (or require) additional properties before you can use + * them (for authentication etc). + * + * To use a different service with Slinky, just create your own class and + * extend Slinky_Service(). Make sure you implement url_is_short(), url_is_long(), + * make_short() and make_long(). If you need to GET or POST a URL, you can use + * ->url_get() and ->url_post(), which your class will have inherited. +**/ +class Slinky { + var $url = false; + var $service = false; + var $cascade = false; + + function __construct( $url = false, $service = false ) { + $this->url = $url; + $this->service = $service; + } + + /** + * Specify which URL Service to use + * + * @param Slinky_Service $service Packaged or custom Service object + * @return void + */ + public function set_service( $service = false ) { + if ( is_object( $service ) ) { + $this->service = $service; + } + } + + /** + * If you pass an array of Slinky_Service objects to this method, they will + * be used in order to try to get a short URL, so if one fails, it will + * try the next and so on, until it gets a valid short URL, or it runs + * out of options. + * + * @param array $services List of Slinky_Service objects as an array + **/ + public function set_cascade( $services = false ) { + if ( !$services || !is_array( $services ) ) + return false; + + $this->cascade = $services; + } + + /** + * Guess the URL service to use from known domains of short URLs + * + * @param string $url + */ + public function set_service_from_url( $url = false ) { + if ( !$url ) + $url = $this->url; + + $host = parse_url( $url, PHP_URL_HOST ); + switch ( str_replace( 'www.', '', $host ) ) { + case 'bit.ly': + if ( class_exists( 'Slinky_Bitly' ) ) { + $this->service = new Slinky_Bitly(); + break; + } + case 'tr.im': + if ( class_exists( 'Slinky_Trim' ) ) { + $this->service = new Slinky_Trim(); + break; + } + case 'tinyurl.com': + if ( class_exists( 'Slinky_TinyURL' ) ) { + $this->service = new Slinky_TinyURL(); + break; + } + case 'is.gd': + if ( class_exists( 'Slinky_IsGd' ) ) { + $this->service = new Slinky_IsGd(); + break; + } + case 'fon.gs': + if ( class_exists( 'Slinky_Fongs' ) ) { + $this->service = new Slinky_Fongs(); + break; + } + case 'micurl.com': + if ( class_exists( 'Slinky_Micurl' ) ) { + $this->service = new Slinky_Micurl(); + break; + } + case 'ur1.ca': + if ( class_exists( 'Slinky_Ur1ca' ) ) { + $this->service = new Slinky_Ur1ca(); + break; + } + case 'ptiturl.com': + if ( class_exists( 'Slinky_PtitURL' ) ) { + $this->service = new Slinky_PtitURL(); + break; + } + case 'tighturl.com': + case '2tu.us': + if ( class_exists( 'Slinky_TightURL' ) ) { + $this->service = new Slinky_TightURL(); + break; + } + case 'snipr.com': + case 'snipurl.com': + case 'snurl.com': + case 'sn.im': + if ( class_exists( 'Slinky_Snipr' ) ) { + $this->service = new Slinky_Snipr(); + break; + } + default: + $this->service = new Slinky_Default(); + break; + } + } + + /** + * Take a long URL and make it short. Will avoid "re-shortening" a URL if it + * already seems to be short. + * + * @param string $url Optional URL to shorten, otherwise use $this->url + * @return The short version of the URL + */ + public function short( $url = false ) { + if ( $url ) + $this->url = $url; + + if ( !$this->service ) + $this->set_service( new Slinky_TinyURL() ); // Defaults to tinyurl because it doesn't require any configuration + + if ( !$this->cascade ) + $this->cascade = array( $this->service ); // Use a single service in cascade mode + + foreach ( $this->cascade as $service ) { + if ( $service->url_is_short( $this->url ) ) + return trim( $this->url ); // Identified as already short, using this service + + $response = trim( $service->make_short( $this->url ) ); + if ( $response && $this->url != $response ) + return trim( $response ); + } + + return $this->url; // If all else fails, just send back the URL we already know about + } + + /** + * Take a short URL and make it long ("resolve" it). + * + * @param string $url The short URL + * @return A long URL + */ + public function long( $url = false ) { + if ( $url ) + $this->url = $url; + + if ( !$this->service ) + $this->set_service_from_url(); + + if ( $this->service->url_is_long( $this->url ) ) + return trim( $this->url ); + + return trim( $this->service->make_long( $this->url ) ); + } +} + +/** + * Use this class to create a Service implementation for your own URL + * shortening service. Extend the class and customize methods to suit your + * service. Note that it is an "abstract" class, so there are certain methods + * which you *must* define. +**/ +abstract class Slinky_Service { + + /** + * Determine, based on the input URL, if it's already a short URL, from + * this particular service. e.g. a Bit.ly URL contains "bit.ly/" + **/ + abstract function url_is_short( $url ); + + /** + * Determine if this is a "long" URL (just means it hasn't been shortened) + * already. e.g. a no-Bit.ly URL would NOT contain "bit.ly/" + **/ + abstract function url_is_long( $url ); + + /** + * Handle taking the $url and converting it to a short URL via whatever + * means is provided at the remote service. + **/ + abstract function make_short( $url ); + + /** + * Return the long/expanded version of a URL via any API means available + * from this service. As a fallback, you might + * consider just following the URL and using SLINKY_FINAL_URL as the + * return method from a $this->url_get() call to find out. + * + * This one is optional for Services extending this class, if they don't + * then the following implementation will work on most services anyway. + **/ + public function make_long( $url ) { + return $this->url_get( $url, SLINKY_FINAL_URL ); + } + + /** + * Method for getting properties that you might need during the process + * of shortening/lengthening a URL (e.g. auth credentials) + **/ + public function get( $prop ) { + if ( empty( $this->$prop ) ) + return null; + + return $this->$prop; + } + + /** + * Method for setting properties that you might need during the process + * of shortening/lengthening a URL (e.g. auth credentials) + **/ + public function set( $prop, $val ) { + $this->$prop = $val; + } + + /** + * Internal helper for performing a GET request on a remote URL. + * + * @param string $url The URL to GET + * @param const $return The return method [ SLINKY_BODY | SLINKY_FINAL_URL | SLINKY_HEADERS ] + * @return Mixed, based on the $return var passed in. + **/ + protected function url_get( $url, $return = SLINKY_BODY ) { + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_USERAGENT, SLINKY_USER_AGENT ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // Don't stress about SSL validity + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // Return the response, don't output it + curl_setopt( $ch, CURLOPT_TIMEOUT, SLINKY_TIMEOUT ); // Limit how long we'll wait for a response + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // Allow following of redirections + $r = curl_exec( $ch ); + if ( curl_errno( $ch ) ) { + return false; + } + + // Return whatever we were asked for + if ( SLINKY_FINAL_URL == $return ) + return curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL ); + else if ( SLINKY_BODY == $return ) + return $r; + + return false; + } + + /** + * Internal helper for performing a POST request on a remote URL. + * + * @param string $url The URL to POST to + * @param array $payload Array containing name/value pairs of the parameters to POST + * @param const $return The return method [ SLINKY_BODY | SLINKY_FINAL_URL | SLINKY_HEADERS ] + * @return Mixed, based on the $return var passed in. + **/ + protected function url_post( $url, $payload = array(), $return = SLINKY_BODY ) { + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_POST, true ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, (array) $payload ); + curl_setopt( $ch, CURLOPT_USERAGENT, SLINKY_USER_AGENT ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // Don't stress about SSL validity + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // Return the response, don't output it + curl_setopt( $ch, CURLOPT_TIMEOUT, SLINKY_TIMEOUT ); // Limit how long we'll wait for a response + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // Allow following of redirections + $r = curl_exec( $ch ); + if ( curl_errno( $ch ) ) { + return false; + } + + // Return whatever we were asked for + if ( SLINKY_FINAL_URL == $return ) + return curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL ); + else if ( SLINKY_BODY == $return ) + return $r; + + return false; + } +} + +// This default service is used in cases when you try to do something based +// on auto-detection, but we can't detect anything. It will also resolve URLs +// to their "long" version by following all redirects. +class Slinky_Default extends Slinky_Service { + function url_is_short( $url ) { + return false; + } + + function url_is_long( $url ) { + return false; + } + + function make_short( $url ) { + return $url; + } +} + +// Implementation of TinyURL as a Slinky Service +class Slinky_TinyURL extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'tinyurl.com/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'tinyurl.com/' ); + } + + function make_short( $url ) { + return $this->url_get( 'http://tinyurl.com/api-create.php?url=' . urlencode( $url ) ); + } + + function make_long( $url ) { + $bits = parse_url( $url ); + $result = $this->url_get( 'http://tinyurl.com/preview.php?num=' . substr( $bits['path'], 1 ) ); + if ( preg_match('//is', $result, $matches ) ) + return $matches[1]; + else + return $url; + } +} + +// Implementation of Bit.ly as a Slinky Service +/* +To use Bit.ly, you MUST set your login and apiKey for the service first, e.g. + +$bitly = new Slinky_Bitly(); +$bitly->set( 'login', 'bitly_login' ); +$bitly->set( 'apiKey', 'bitly_apiKey' ); + +$slinky = new Slinky( $url, $bitly ); +echo $slinky->short(); + +You could also do this if the URL was already a bit.ly URL and you +were going to make it longer, since Bitly is supported for auto-detection: + +$slinky = new Slinky( $url ); +$slinky->set_service_from_url(); +$slinky->service->set( 'login', 'bitly_login' ); +$slinky->service->set( 'apiKey', 'bitly_apiKey' ); +echo $slinky->long(); + +*/ +class Slinky_Bitly extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'bit.ly/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'bit.ly/' ); + } + + function make_short( $url ) { + // Can't do anything unless these 2 properties are set first + if ( !$this->get( 'login' ) || !$this->get( 'apiKey' ) ) + return $url; + + $result = $this->url_post( 'http://api.bit.ly/shorten?version=2.0.1&format=json&login=' . $this->get( 'login' ) . '&apiKey=' . $this->get( 'apiKey' ) . '&longUrl=' . urlencode( $url ) ); + $result = json_decode( $result ); + if ( !$result->errorCode ) { + foreach ( $result->results as $detail ) { + return $detail->shortUrl; + } + } else { + return false; + } + } + + function make_long( $url ) { + // Can't do anything unless these 2 properties are set first + if ( !$this->get( 'login' ) || !$this->get( 'apiKey' ) ) + return $url; + + $result = $this->url_post( 'http://api.bit.ly/expand?version=2.0.1&format=json&login=' . $this->get( 'login' ) . '&apiKey=' . $this->get( 'apiKey' ) . '&shortUrl=' . urlencode( $url ) ); + $result = json_decode( $result ); + if ( !$result->errorCode ) { + foreach ( $result->results as $detail ) { + return $detail->longUrl; + } + } else { + return false; + } + } +} + +// Implementation of Tr.im as a Slinky Service +/* +When using Tr.im, you MAY optionally set your username and password to tie +URLs to your account, e.g. + +$trim = new Slinky_Trim(); +$trim->set( 'username', 'trim_username' ); +$trim->set( 'password', 'trim_password' ); + +$slinky = new Slinky( $url, $trim ); +echo $slinky->short(); + +You could also do this if the URL was already a tr.im URL and you +were going to make it longer, since Tr.im is supported for auto-detection: + +$slinky = new Slinky( $url ); +$slinky->set_service_from_url(); +echo $slinky->long(); + +*/ +class Slinky_Trim extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'tr.im/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'tr.im/' ); + } + + function make_short( $url ) { + $url = 'http://api.tr.im/api/trim_simple?url=' . urlencode( $url ); + + if ( $this->get( 'username' ) && $this->get( 'password' ) ) + $url .= '&username=' . urlencode( $this->get( 'username' ) ) . '&password=' . urlencode( $this->get( 'password' ) ); + + return $this->url_get( $url ); + } + + function make_long( $url ) { + $bits = parse_url( $url ); + $result = $this->url_get( 'http://api.tr.im/api/trim_destination.json?trimpath=' . substr( $bits['path'], 1 ) ); + $result = json_decode($result); + if ( 'OK' == $result->status->result ) + return $result->destination; + else + return $url; + } +} + +// Implementation of Is.Gd as a Slinky Service +class Slinky_IsGd extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'is.gd/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'is.gd/' ); + } + + function make_short( $url ) { + $response = $this->url_get( 'http://is.gd/api.php?longurl=' . urlencode( $url ) ); + if ( 'error' == substr( strtolower( $response ), 0, 5 ) ) + return false; + else + return $response; + } +} + +// Fon.gs +class Slinky_Fongs extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'fon.gs/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'fon.gs/' ); + } + + function make_short( $url ) { + $response = $this->url_get( 'http://fon.gs/create.php?url=' . urlencode( $url ) ); + if ( 'OK:' == substr( $response, 0, 3 ) ) + return str_replace( 'OK: ', '', $response ); + else + return $url; + } +} + +// Micu.rl +class Slinky_Micurl extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'micurl.com/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'micurl.com/' ); + } + + function make_short( $url ) { + $result = $this->url_get( 'http://micurl.com/api.php?url=' . urlencode( $url ) ); + if ( 1 != $result && 2 != $result ) + return 'http://micurl.com/' . $result; + else + return $url; + } +} + +// ur1.ca +class Slinky_Ur1ca extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'ur1.ca/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'ur1.ca/' ); + } + + function make_short( $url ) { + $result = $this->url_post( 'http://ur1.ca/', array( 'longurl' => $url ) ); + if ( preg_match( '/

Your ur1 is: /', $result, $matches ) ) + return $matches[1]; + else + return $url; + } +} + +// PtitURL.com +class Slinky_PtitURL extends Slinky_Service { + function url_is_short( $url ) { + return stristr( $url, 'ptiturl.com/' ); + } + + function url_is_long( $url ) { + return !stristr( $url, 'ptiturl.com/' ); + } + + function make_short( $url ) { + $result = $this->url_get( 'http://ptiturl.com/index.php?creer=oui&url=' . urlencode( $url ) ); + if ( preg_match( '/

]+)\'?>/', $result, $matches ) )
+			return $matches[1];
+		else
+			return $url;
+	}
+}
+
+// Tighturl.com
+class Slinky_TightURL extends Slinky_Service {
+	function url_is_short( $url ) {
+		return stristr( $url, 'tighturl.com/' )
+				|| stristr( $url, '2tu.us/' );
+	}
+	
+	function url_is_long( $url ) {
+		return !stristr( $url, 'tighturl.com/' )
+				&& !stristr( $url, '2tu.us/' );
+	}
+	
+	function make_short( $url ) {
+		$response = $this->url_get( 'http://tighturl.com/?save=y&url=' . urlencode( $url ) );
+		if ( preg_match( '/Your tight URL is: /', $response, $matches ) ) {
+			return $matches[1];
+		} else {
+			return $url;
+		}
+	}
+}
+
+// Snipr for Slinky
+/*
+To use Snipr, you MUST set your user_id and API (key) for the service first, e.g.
+
+$snipr = new Slinky_Snipr();
+$snipr->set( 'user_id', 'Snipr User ID' );
+$snipr->set( 'API', 'Snipr API Key' );
+
+$slinky = new Slinky( $url, $snipr );
+echo $slinky->short();
+
+NOTE: Snipr requires the SimpleXML extension to be installed for lengthening URLs
+*/
+class Slinky_Snipr extends Slinky_Service {
+	// Snipurl, Snurl, Snipr, Sn.im
+	function url_is_short( $url ) {
+		return stristr( $url, 'snipr.com/' ) || stristr( $url, 'snipurl.com/' ) || stristr( $url, 'snurl.com/' ) || stristr( $url, 'sn.im/' );
+	}
+	
+	function url_is_long( $url ) {
+		return !stristr( $url, 'snipr.com/' ) || !stristr( $url, 'snipurl.com/' ) || !stristr( $url, 'snurl.com/' ) || !stristr( $url, 'sn.im/' );
+	}
+	
+	function make_short( $url ) {
+		if ( !$this->get( 'user_id' ) || !$this->get( 'API' ) )
+			return $url;
+		
+		$response = $this->url_post( 'http://snipr.com/site/getsnip', array( 'sniplink' => urlencode( $url ), 'snipuser' => $this->get( 'user_id'), 'snipapi' => $this->get( 'API' ), 'snipformat' => 'simple' ) );
+		if ( 'ERROR' != substr( $response, 0, 5 ) )
+			return $response;
+		else
+			return $url;
+	}
+}
+
+// If you're testing things out, http://dentedreality.com.au/ should convert to:
+// - http://tinyurl.com/jw5sh
+// - http://bit.ly/hEkAD
+// - http://tr.im/sk1H
+// - http://is.gd/1yJ81
+// - http://fon.gs/tc1p8c
+// - http://micurl.com/qen3uub
+// - http://ur1.ca/7dcd
+// - http://ptiturl.com/?id=bac8fb
+// - http://tighturl.com/kgd
+// - http://snipr.com/nbbw3
+// 
+// $slinky = new Slinky( 'http://dentedreality.com.au/' );
+// echo $slinky->short();
diff --git a/addon/twitter/twitter.css b/addon/twitter/twitter.css
new file mode 100644
index 000000000..899cfd173
--- /dev/null
+++ b/addon/twitter/twitter.css
@@ -0,0 +1,41 @@
+
+
+#twitter-avatar {
+	float: left;
+	width: 48px;
+	height: 48px;
+	padding: 2px;
+}
+#twitter-info-block {
+	height: 52px;
+	vertical-align: middle;
+}
+#twitter-disconnect-label {
+	float: left;
+	width: 200px;
+	margin-bottom: 25px;
+}
+
+#twitter-disconnect {
+	float: left;
+}
+#twitter-enable-label {
+	float: left;
+	width: 200px;
+	margin-bottom: 5px;
+}
+
+#twitter-checkbox {
+	float: left;
+}
+#twitter-pin-label {
+	float: left;
+	width: 200px;
+	margin-bottom: 25px;
+}
+
+#twitter-pin {
+	float: left;
+}
+
+
diff --git a/addon/twitter/twitter.php b/addon/twitter/twitter.php
index 9fccefbee..7d4cf1514 100644
--- a/addon/twitter/twitter.php
+++ b/addon/twitter/twitter.php
@@ -1,16 +1,166 @@
 config['twitter']['consumerkey'] = 'your consumer_key here';
+ *     $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
+ *
+ *     To activate the plugin itself add it to the $a->config['system']['addon']
+ *     setting. After this, your user can configure their Twitter account settings
+ *     from "Settings -> Plugin Settings".
+ *
+ *     Requirements: PHP5, curl [Slinky library]
+ *
+ *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Twitter_Plugin
+ */
+
+/*   __TODO__
+ *
+ *   - deletion of the OAuth credentials does not work
+ *   - what about multimedia content?
+ *     so far we just strip HTML tags from the message
+ */
 
 function twitter_install() {
+	//  we need some hooks, for the configuration and for sending tweets
+	register_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
+	register_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
 	register_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
+	logger("installed twitter");
 }
 
 
 function twitter_uninstall() {
+	unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
+	unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
 	unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
 }
 
+function twitter_settings_post ($a,$post) {
+	if(! local_user())
+		return;
+	if (isset($_POST['twitter-disconnect'])) {
+		/***
+		 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
+		 * from the user configuration
+                 * TODO this does not work that way!?
+		 * TODO can we revoke the access tokens at Twitter and do we need to do so?
+		 */
+		del_pconfig( local_user(), 'twitter', 'consumerkey'  );
+		del_pconfig( local_user(), 'twitter', 'consumersecret' );
+		del_pconfig( local_user(), 'twitter', 'post' );
+	} else {
+	if (isset($_POST['twitter-pin'])) {
+		//  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
+		logger('got a Twitter PIN');
+		require_once('addon/twitter/twitteroauth.php');
+		$ckey    = get_config('twitter', 'consumerkey'  );
+		$csecret = get_config('twitter', '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 TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
+		$token   = $connection->getAccessToken( $_POST['twitter-pin'] );
+		//  ok, now that we have the Access Token, save them in the user config
+ 		set_pconfig(local_user(),'twitter', 'oauthtoken',  $token['oauth_token']);
+		set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
+                set_pconfig(local_user(),'twitter', 'post', 1);
+                //  reload the Addon Settings page, if we don't do it see Bug #42
+                header('Location: '.$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(),'twitter','post',intval($_POST['twitter']));
+	}}
+}
+function twitter_settings(&$a,&$s) {
+        if(! local_user())
+                return;
+        $a->page['htmlhead'] .= '' . "\r\n";
+	/***
+	 * 1) Check that we have global consumer key & secret
+	 * 2) If no OAuthtoken & stuff is present, generate button to get some
+	 * 3) Checkbox for "Send public notices (140 chars only)
+	 */
+	$ckey    = get_config('twitter', 'consumerkey' );
+	$csecret = get_config('twitter', 'consumersecret' );
+	$otoken  = get_pconfig(local_user(), 'twitter', 'oauthtoken'  );
+	$osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
+        $enabled = get_pconfig(local_user(), 'twitter', 'post');
+	$checked = (($enabled) ? ' checked="checked" ' : '');
+	$s .= '

'.t('Twitter Posting Settings').'

'; + if ( (!$ckey) && (!$csecret) ) { + /*** + * no global consumer keys + * display warning and skip personal config + */ + $s .= '

'.t('No consumer key pair for Twitter found. Please contact your site administrator.').'

'; + } 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 twitter... + * 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 Twitter. + */ + require_once('addon/twitter/twitteroauth.php'); + $connection = new TwitterOAuth($ckey, $csecret); + $request_token = $connection->getRequestToken(); + $token = $request_token['oauth_token']; + /*** + * make some nice form + */ + $s .= '

'.t('At this Friendika instance the Twitter plugin was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your public posts will be posted to Twitter.').'

'; + $s .= '
'.t('Log in with Twitter').'></a>';
+			$s .= '<div id='; + $s .= ''; + $s .= ''; + $s .= ''; + $s .= ''; + $s .= '
'; + $s .= '
'; + } else { + /*** + * we have an OAuth key / secret pair for the user + * so let's give a chance to disable the postings to Twitter + */ + require_once('addon/twitter/twitteroauth.php'); + $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret); + $details = $connection->get('account/verify_credentials'); + $s .= '
'; + $s .= '

'.t('If enabled all your public postings will be posted to the associated Twitter account as well.').'

'; + $s .= '
'; + $s .= ''; + $s .= ''; + $s .= '
'; + $s .= '
'; + $s .= ''; + $s .= ''; + $s .= '
'; + $s .= '
'; + } + } + $s .= '
'; +} function twitter_post_hook(&$a,&$b) { @@ -19,12 +169,14 @@ function twitter_post_hook(&$a,&$b) { * Post to Twitter */ + logger('twitter post invoked'); + if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) { load_pconfig(local_user(), 'twitter'); - $ckey = get_pconfig(local_user(), 'twitter', 'consumerkey' ); - $csecret = get_pconfig(local_user(), 'twitter', 'consumersecret' ); + $ckey = get_config('twitter', 'consumerkey' ); + $csecret = get_config('twitter', 'consumersecret' ); $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' ); $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' ); @@ -35,12 +187,33 @@ function twitter_post_hook(&$a,&$b) { if($twitter_post) { require_once('addon/twitter/twitteroauth.php'); require_once('include/bbcode.php'); - $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret); - $tweet->post('statuses/update', array('status' => bbcode($b['body']))); + $max_char = 140; // max. length for a tweet + $msg = strip_tags(bbcode($b['body'])); + if ( strlen($msg) > $max_char) { + $shortlink = ""; + require_once('addon/twitter/slinky.php'); + // post url = base url + /display/ + owner + post id + // we construct this from the Owner link and replace + // profile by display - this will cause an error when + // /profile/ is in the owner url twice but I don't + // think this will be very common... + $posturl = str_replace('/profile/','/display/',$b['owner-link']).'/'.$b['id']; + $slinky = new Slinky( $posturl ); + // setup a cascade of shortening services + // try to get a short link from these services + // in the order ur1.ca, trim, id.gd, tinyurl + $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) ); + $shortlink = $slinky->short(); + // the new message will be shortened such that "... $shortlink" + // will fit into the character limit + $msg = substr($msg, 0, $max_char-strlen($shortlink)-4); + $msg .= '... ' . $shortlink; + } + // and now tweet it :-) + $tweet->post('statuses/update', array('status' => $msg)); } } - } + } } -