3
* This file implements the Twitter plugin.
5
* For the most recent and complete Plugin API documentation
6
* see {@link Plugin} in ../evocore/_plugin.class.php.
8
* This plugin will post to your twitter account when you have added a post to your blog.
10
* @todo (legacy): use OAuth -- http://www.jaisenmathai.com/blog/2009/03/31/how-to-quickly-integrate-with-twitters-oauth-api-using-php/
11
* @todo (legacy): Tblue> Do not use cURL, or at least do not depend on it! We could
12
* clone/modify {@link fetch_remote_page()} to be able to do
3
* This file implements the Twitter plugin for Quam Plures
5
* READ THE README! That's where all the answers to all your questions are!
7
* Some folk think licensing matters. I guess maybe it does, but so does a 45
8
* caliber semi-automatic pistol. Just sayin' Anyway let's talk licensing.
9
* This plugin is based on and built on three different programs, all of which
10
* have some sort of licensing crap in them.
12
* -----------------------------------------------------------------------------
14
* First, this exact file is built on the corpse of a dead plugin with the same
15
* name. It claimed copyright by 2 persons and 1 entity, and claimed release
16
* under a GPL2 license. Were the contributions of each person and party
17
* identified? No. Did it say exactly who decided it was GPL2? No. Near as I can
18
* tell it's all a bunch of crap designed to make sure a lawyer could charge a
19
* fortune of hourly time just reading this rubbish, but whatever: here for your
20
* entertainment is the original tidbits from the original version of this file:
14
22
* This file is part of the Quam Plures project - {@link http://quamplures.net/}
16
24
* @copyright (c) 2009 by the Quam Plures developers - {@link http://quamplures.net/}
17
25
* @copyright (c)2009 by Francois PLANQUE - {@link http://fplanque.net/}
18
26
* @copyright (c)2007 by Lee Turner - {@link http://leeturner.org/}.
20
* @license GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
23
* @author fplanque: Francois PLANQUE.
28
* @license GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
30
* -----------------------------------------------------------------------------
32
* Next up is a version of this file found by downloading b2evolution's future
33
* stuff. Stuff that may or may not ever see a public release but guess what:
34
* don't put it on the internet if you don't want someone downloading it!
35
* That file actually was the basis for this file even though that file failed
36
* in some very fundamental ways. The connection to twitter was not done in
37
* accordance with both oauth and twitter's expectations. I fixed that because
38
* I care about quality code. Anyway that file claims 2 authors, and a GPL2
39
* license. Here, again for your entertainment, is the lines that lawyers get
41
* @copyright (c)2009 by Francois PLANQUE - {@link http://fplanque.net/}
42
* @copyright (c)2007 by Lee Turner - {@link http://leeturner.org/}.
44
* @license GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
46
* -----------------------------------------------------------------------------
48
* Finally, we come to a file that I actually liked. I used none of their
49
* code, but most certainly drew strongly from what that author did.
50
* Have you ever heard of Wordpress? It's another open source blog app that
51
* seems to be somewhat popular. Anyway some nice folks wrote a plugin for
52
* that app called tweetable
53
* http://www.webmaster-source.com/tweetable-twitter-plugin-wordpress/
54
* It was cool because it connected to twitter the right way, but it wasn't
55
* geared towards multiple blogs and bloggers so there was only one thing I
56
* could get from it. Anyway since we're pretending licensing crap matters,
57
* and noticing that their downloadable package includes ANOTHER copy of a
58
* GPL license, I've included their file here as tweetable.GPL.txt.
59
* Lots of bytes + no value added = pure bloat but so what! It's all about the lawyers...
61
* -----------------------------------------------------------------------------
63
* What, you may ask, have I done for this code?
64
* 1) I made a plugin that works in Quam Plures based on bits and pieces of
65
* three different open source plugins.
66
* 2) I made it connect to twitter the way ouath and twitter expect (no storing
68
* 3) I wrote the steps that walk the installation owner through registering
70
* 4) I made it work with multiple bloggers on multiple blogs in one
72
* 5) I made it capable of showing your twitter status in 3 locations instead
74
* 7) I skipped number six.
75
* 8) I made the callback file recognize the different sources one might
76
* authorize the registered app so it could return the blogger to the right
78
* 9) I wrote all this stupid crap.
79
* 10) I wrote the songs that make the whole world sing.
81
* NEW LICENSING REQUIREMENTS!
82
* Given that bits and pieces of source material that have been identified above
83
* are licensed under varying licensing schemes, and noting that nobody actually
84
* cares about it anyway except for lawyers but I don't care about lawyers, I
85
* hereby decree that this file (noting any restrictions that a normal human
86
* might magically think the GPL licenses associated with the original concepts
87
* that became this plugin might contain) hereby release unto the world this
88
* creative work to do with as you see fit including modify, adapt, build on,
89
* use as a basis for your own creative work, use to make money, or use to
90
* impress cute members of the gender of your choice for the purpose of wild
91
* yet emotionally unsatisfying gratuitous sex HOWEVER you must always keep this
92
* license notice intact in it's entirety.
94
* IF you fail to keep this licence notice intact in it's entirety then I swear
95
* with words yet unwritten by Gods yet unborn that I shall lay upon your seed
96
* a curse for all eternity that their operating systems will reboot randomly,
97
* that the porn they download is not what they thought it was after waiting
98
* forever due to seeds with crappy connections, that their apps will interfere
99
* with efficient texting, that no one shall read their tweets, and that they
100
* shall suffer great emotional harm from these undesirable conditions.
102
* Will you be able to sleep at night if you bring such a strong curse upon your
103
* heir's heir's heir's heads? If so then hey that's cool. Have a nice day :)
105
* I'm EdB {{http://wonderwinds.com/}} and I approve this message
25
107
* @package plugins
26
108
* @subpackage Twitter
28
111
if( !defined('QP_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
32
116
* @package plugins
33
117
* @subpackage Twitter
35
119
class twitter_plugin extends Plugin
37
var $author = 'Lee Turner';
38
var $author_url = 'http://www.leeturner.org/';
122
var $author_url = 'http://wonderwinds.com/';
39
123
var $code = 'twitter';
40
124
var $group = 'pingers';
125
var $help_url = 'http://1912webworks.com/plugins.php';
41
126
var $number_of_installs = 1;
42
127
var $priority = 50;
43
128
var $version = '0.1';
47
131
* @see Plugin::PluginInit()
49
133
function PluginInit( & $params )
51
135
$this->name = $this->T_('Twitter');
52
$this->short_desc = $this->T_('Posts to your Twitter account when you post to your blog');
53
$this->long_desc = $this->T_('Posts to your Twitter account to update Twitter.com with (configurable) details of your blog post.');
136
$this->short_desc = $this->T_('Tweet when you post :)');
137
$this->long_desc = $this->T_('User ID#1 must first register your installation as an application. After that each user will be able to trust the application on their profile page or the write page. When a post is made it will tweet in your name whatever you set up for the message parameter. The default is $twitcerpt$, which will create an excerpt and link that total 140 characters.');
55
139
$this->ping_service_name = 'twitter.com';
56
$this->ping_service_note = $this->T_('Update your twitter account with details about the new post.');
140
$this->ping_service_note = $this->T_('Update the blogger\'s twitter account with each new post.');
61
* Check if the plugin can be enabled:
145
* @see Plugin::BeforeEnable()
65
147
function BeforeEnable()
68
if( empty($this->code) )
150
if( !extension_loaded( 'curl') )
70
return $this->T_('The twitter plugin needs a non-empty code.');
152
return $this->T_('The twitter plugin requires curl extension.');
160
* @see Plugin::GetDbLayout()
162
function GetDbLayout()
165
"CREATE TABLE IF NOT EXISTS ".$this->get_sql_table( 'twitter' )." (
166
t_key VARCHAR(32) NOT NULL default '',
167
t_secret VARCHAR(52) NOT NULL default '',
168
t_number VARCHAR(10) NOT NULL default '',
176
* @see Plugin::AdminAfterMenuInit()
178
function AdminAfterMenuInit()
180
$this->register_menu_entry( $this->T_('Twitter Keys') );
185
* @see Plugin::AdminTabAction()
187
function AdminTabAction( $params )
191
global $current_User;
193
// this is adding keys
194
if( param( $this->get_class_id( 'what' ) ) == 'add' )
196
$key = param( $this->get_class_id('key') );
197
$secret = param( $this->get_class_id('secret') );
198
$number = param( $this->get_class_id('number') );
200
if( $key == '' || $secret == '' )
202
$this->msg( $this->T_('The "key" and "secret" fields are required!'), 'error' );
206
$query = "INSERT INTO ".$this->get_sql_table('twitter')." SET t_key = '".$DB->escape( $key )."', t_secret = '".$DB->escape( $secret )."', t_number = '".$DB->escape( $number )."'";
207
if( $DB->query( $query ) )
209
$this->msg( $this->T_('Your application keys have been added to your database!'), 'success' );
213
$this->msg( $this->T_('This is bad: we tried to add the keys to your database but failed :('), 'error' );
217
// this is dropping keys
218
if( param( $this->get_class_id( 'what' ) ) == 'reset' )
220
$DB->query( "TRUNCATE TABLE ".$this->get_sql_table('twitter') );
223
// if the keys are stored we're okay
224
$this->keys_check = false;
225
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
226
if( $results = $DB->get_results( $query ) )
228
$this->keys_check = true;
235
* @see Plugin::AdminTabPayload()
237
function AdminTabPayload( $params )
241
global $current_User;
243
if( $this->keys_check )
244
{ // we have registration keys
245
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
246
if( $app_data = $DB->get_row( $query, ARRAY_A ) )
247
{ // get the user's twitter authentication status
248
$user_id = $current_User->ID;
249
$status = $this->get_twitter_link( $user_id, 'status' );
250
$Form = & new Form( NULL, 'twitter_keys_a', 'post', 'compact' );
251
$Form->begin_form( 'fform', $this->T_('Have you authorized this app to tweet in your name, or do you want/need to?') );
252
$Form->hidden_ctrl();
253
$Form->hiddens_by_key( get_memorized() );
254
$Form->begin_fieldset( '' );
255
$Form->info_field( 'Your twitter status', $status );
256
$Form->end_fieldset( 'foo' );
259
if( $current_User->ID == '1' )
260
{ // user ID#1 can drop the keys if desired, and gets a link to the app details page
261
$Form = & new Form( NULL, 'twitter_keys_b', 'post', 'compact' );
262
$Form->begin_form( 'fform', $this->T_('Application Info') );
263
$Form->hidden_ctrl();
264
$Form->hiddens_by_key( get_memorized() );
265
$Form->begin_fieldset( '' );
266
$Form->info_field( 'opens new window', '<a href="http://dev.twitter.com/apps/'.$app_data['t_number'].'" target="_blank">'.$this->T_('Application Info (link launches new window)').'</a>' );
267
$Form->end_fieldset( 'foo' );
270
$Form = & new Form( NULL, 'twitter_keys_c', 'post', 'compact' );
271
$Form->begin_form( 'fform', $this->T_('Drop Application Keys') );
272
$Form->hidden_ctrl();
273
$Form->hiddens_by_key( get_memorized() );
274
$Form->hidden( $this->get_class_id().'_what', 'reset' );
275
$Form->info_field( 'Warning', 'If you do this all of your bloggers will have to authenticate again.' );
276
$Form->button_input( array( 'value' => $this->T_('Wipe out my keys so I can do it again (WARNING: THIS CAN NOT BE UNDONE!)') ) );
284
elseif( $current_User->ID == '1' )
285
{ // no keys so walk through adding them
292
$default_blog_on_index = $Settings->get('default_blog_ID');
293
$default_Blog = & $BlogCache->get_by_ID( $default_blog_on_index, false );
294
$callback = $this->get_plugin_url( true ).'_callback.php';
296
echo '<p>'.$this->T_('Twitter uses the OAuth protocol to authenticate and access Twitter accounts.').' '
297
.$this->T_('That means you have to register your blog as an application with Twitter.').' '
298
.$this->T_('Why? <strong>Because twitter says so</strong>. The link below will open a new window, but first...').'</p>';
300
echo '<h3>'.$this->T_('...some things to know before you register:').'</h3>';
301
echo '<ol style="list-style:decimal; margin: 0 1ex 1ex 2ex;line-height:1.3em;">';
302
echo '<li>'.$this->T_('<strong>Application Icon</strong> is not required and will be shown to your bloggers when they decide to trust your app or not.').'</li>';
303
echo '<li>'.$this->T_('<strong>Application Name</strong> (<em class="red">required</em>) must be "unique to twitter". Maybe your base URL minus the http part and trailing slash?');
304
echo '<br /><code style="font-size:1.2em;background-color:white;margin-left:4ex;padding:2px;">'.substr( $basedomain.$basesubpath, 0, -1 ).'</code></li>';
305
echo '<li>'.$this->T_('<strong>Application Description</strong> (<em class="red">required</em>). Maybe the tagline or short description from your primary blog?');
306
echo '<ul style="margin: 0 1ex 1ex 2ex;line-height:1.3em;">';
307
echo '<li><strong>'.$this->T_('TAGLINE').':</strong><code style="font-size:1.2em;background-color:white;margin-left:1ex;padding:2px;">'.$default_Blog->get( 'tagline' ).'</code></li>';
308
echo '<li><strong>'.$this->T_('SHORT DESCRIPTION').':</strong><code style="font-size:1.2em;background-color:white;margin-left:1ex;padding:2px;">'.$default_Blog->get( 'description' ).'</code></li>';
310
echo '<li>'.$this->T_('<strong>Application Website</strong> (<em class="red">required</em>) should be the same as your $app_baseurl value:');
311
echo '<br /><code style="font-size:1.2em;background-color:white;margin-left:4ex;padding:2px;">'.$app_baseurl.'</code></li>';
312
echo '<li>'.$this->T_('<strong>Organization</strong> is not required but should be because they assume it exists. Your bloggers will be asked to trust "application name by organization". Maybe your preferred name? Or a business name?</li>');
313
echo '<li>'.$this->T_('<strong>Website</strong> is not required and hey didn\'t we just do this in step 4?');
314
echo '<br /><code style="font-size:1.2em;background-color:white;margin-left:4ex;padding:2px;">'.$app_baseurl.'</code></li>';
315
echo '<li>'.$this->T_('<strong>Application Type</strong> should be and needs to be <strong>Browser.</strong></li>');
316
echo '<li>'.$this->T_('<strong>Callback URL</strong> is where twitter will come back to after one of your bloggers posts (and tweets) something.');
317
echo '<br /><code style="font-size:1.2em;background-color:white;margin-left:4ex;padding:2px;">'.$callback.'</code></li>';
318
echo '<li>'.$this->T_('<strong>Default Access type</strong> needs to be <strong>Read & Write.</strong> but won\'t be so make sure you set it correctly.</li>');
319
echo '<li>'.$this->T_('Do their stupid captcha thing, press the "Save" button, then you can copy/paste some info back to this form.</li>');
322
echo '<h4 style="text-align:center"><a href="http://twitter.com/oauth_clients/new" target="_blank">[_Register your installation as a new Application_]</a></h4>';
324
$Form = & new Form( NULL, 'twitter_keys', 'post', 'compact' );
325
$Form->begin_form( 'fform', $this->T_('Add twitter keys') );
326
$Form->hidden_ctrl();
327
$Form->hiddens_by_key( get_memorized() );
328
$Form->hidden( $this->get_class_id().'_what', 'add' );
329
$Form->text_input( $this->get_class_id().'_key', '', '32', $this->T_('Consumer key'), $this->T_('<em class="red">required</em>: copy/paste from twitter'), array( 'maxlength' => '32' ) );
330
$Form->text_input( $this->get_class_id().'_secret', '', '52', $this->T_('Consumer secret'), $this->T_('<em class="red">required</em>: copy/paste from twitter'), array( 'maxlength' => '52' ) );
331
$Form->text_input( $this->get_class_id().'_number', '', '52', $this->T_('Application ID#'), $this->T_('<em class="green">optional</em>: find this in the URL if registration is successful'), array( 'maxlength' => '52' ) );
332
$Form->button_input( array( 'value' => $this->T_('Roll them bones') ) );
337
{ // other users get nothing
345
* @see Plugin::GetDefaultSettings()
347
function GetDefaultSettings( & $params )
350
// if the keys are stored we don't need settings
351
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
352
if( $results = $DB->get_results( $query ) )
357
$default_settings = array(
358
'twitter_contact' => array(
359
'label' => $this->T_('foo'),
361
'info' => $this->T_('1) Hit the "Tools -> Twitter Keys" page first to register your installation.<br />2) Each blogger will have to approve this application on their profile page.'),
364
return $default_settings;
369
* @see Plugin::GetDefaultUserSettings()
371
function GetDefaultUserSettings( & $params )
373
global $current_User;
379
$user_id = $current_User->ID;
382
{ // initialize info only once, when needs to display the link (user_id is set)
383
$info = $this->get_twitter_link( $user_id );
384
$user_settings = array(
385
'twitter_contact' => array(
386
'label' => $this->T_('Twitter account status'),
390
'twitter_msg_format' => array(
391
'label' => $this->T_('Message format'),
395
'defaultvalue' => $this->T_('$twitcerpt$'),
396
'note' => $this->T_('$twitcerpt$ is an excerpt + URL that totals 140 characters'),
399
return $user_settings;
79
405
* @see Plugin::ItemSendPing()
81
407
function ItemSendPing( & $params )
86
$item_Blog = $params['Item']->get_Blog();
88
// Try to get twitter account for Blog:
89
$username = $this->get_coll_setting( 'twitter_username', $item_Blog );
90
$password = $this->get_coll_setting( 'twitter_password', $item_Blog );
91
if( empty($username) || empty($password) )
92
{ // Not found, fallback to Trying to get twitter account for User:
93
$username = $this->UserSettings->get( 'twitter_username' );
94
$password = $this->UserSettings->get( 'twitter_password' );
95
if( empty($username) || empty($password) )
96
{ // Still no twitter account found:
97
$params['xmlrpcresp'] = $this->T_('You must configure a twitter username/password before you can post to twitter.');
101
{ // Get additional params from User Setttings:
102
$msg = $this->UserSettings->get( 'twitter_msg_format' );
410
// if the keys are not stored we get out
411
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
412
if( ! $results = $DB->get_results( $query ) )
418
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
419
if( $app_data = $DB->get_row( $query, ARRAY_A ) )
421
$consumer_key = $app_data['t_key'];
422
$consumer_secret = $app_data['t_secret'];
425
// get twitter account info
426
$oauth_token = $this->UserSettings->get( 'twitter_token' );
427
$oauth_token_secret = $this->UserSettings->get( 'twitter_secret' );
428
if( empty($oauth_token) || empty($oauth_token_secret) )
429
{ // no twitter account :(
430
$params['xmlrpcresp'] = $this->T_('You must configure a twitter username/password before you can post to twitter.');
106
{ // Get additional params from Blog Setttings:
107
$msg = $this->get_coll_setting( 'twitter_msg_format', $item_Blog );
434
{ // get the message format
435
$msg = $this->UserSettings->get( 'twitter_msg_format' );
438
// converitate the $message$ bits
110
439
$title = $params['Item']->dget('title', 'xml');
111
440
$excerpt = $params['Item']->dget('excerpt', 'xml');
112
441
$url = $params['Item']->get_permanent_url();
442
$short_url = $params['Item']->Blog->gen_blogurl().'?p='.$params['Item']->ID;
443
$clip_to = 140 - ( evo_strlen( $short_url ) + 4 );
444
$twitcerpt = trim( evo_substr( $excerpt, 0, $clip_to ) ).'... '.$short_url;
114
446
$msg = str_replace( '$title$', $title, $msg );
115
447
$msg = str_replace( '$excerpt$', $excerpt, $msg );
116
448
$msg = str_replace( '$url$', $url, $msg );
118
if( extension_loaded( 'curl' ) )
120
$session = curl_init();
121
curl_setopt( $session, CURLOPT_URL, 'http://twitter.com/statuses/update.xml' );
122
curl_setopt( $session, CURLOPT_POSTFIELDS, 'status='.urlencode($msg));
123
curl_setopt( $session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
124
curl_setopt( $session, CURLOPT_HEADER, false );
125
curl_setopt( $session, CURLOPT_CONNECTTIMEOUT, 5);
126
curl_setopt( $session, CURLOPT_USERPWD, $username.':'.$password );
127
curl_setopt( $session, CURLOPT_RETURNTRANSFER, 1 );
128
curl_setopt( $session, CURLOPT_POST, 1);
129
$result = curl_exec ( $session ); // will be an XML message
130
curl_close( $session );
133
{ // fallback to fsockopen
134
$host = 'twitter.com';
135
$url = '/statuses/update.xml?status='.urlencode( $msg );
137
$header = 'POST '.$url.' HTTP/1.0'."\r\n";
138
$header .= 'Host: '.$host."\r\n";
139
$header .= 'Authorization: Basic '.base64_encode( $username.':'.$password )."\r\n";
140
$header .= 'Connection: Close'."\r\n\r\n";
141
if( $fp = fsockopen($host, 80) )
143
fputs($fp, $header );
145
while( !feof( $fp ) )
147
$result .= fgets( $fp, 1024 );
153
$params['xmlrpcresp'] = $this->T_('No ping method available, please contact your host about using CURL or fsockopen');
449
$msg = str_replace( '$twitcerpt$', $twitcerpt, $msg );
451
require_once 'twitteroauth/twitteroauth.php';
452
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret );
454
$result = $connection->post('statuses/update', array( 'status' => $msg ));
157
456
if( empty($result) )
161
elseif( preg_match( '�<error>(.*)</error>�', $result, $matches ) )
163
$params['xmlrpcresp'] = $matches[1];
167
$params['xmlrpcresp'] = $this->T_('Posted to account: @').$username;
461
$account = $connection->get('account/verify_credentials');
463
$params['xmlrpcresp'] = $this->T_('Posted to account: @').$account->screen_name;
173
* @see Plugin::GetDefaultUserSettings()
470
* @see Plugin::AdminDisplayFieldsetSidebar()
175
function GetDefaultUserSettings( & $params )
472
function AdminDisplayFieldsetSidebar( & $params )
178
'twitter_username' => array(
179
'label' => $this->T_('Twitter username'),
182
'twitter_password' => array(
183
'label' => $this->T_('Twitter password'),
184
'type' => 'password',
186
'twitter_msg_format' => array(
187
'label' => $this->T_('Message format'),
191
'defaultvalue' => $this->T_('Just posted $title$ $url$ #b2p'),
192
'note' => $this->T_('$title$, $excerpt$ and $url$ will be replaced appropriately.'),
474
// use cat_ID to get blog_ID to get pingers...
476
$main_cat_ID = $params['Item']->main_cat_ID;
477
$query = "SELECT cat_blog_ID FROM T_categories WHERE cat_ID = '{$main_cat_ID}'";
478
$this_blog_id = $DB->get_var( $query );
479
$query = "SELECT cset_value FROM T_coll_settings WHERE cset_coll_ID ='{$this_blog_id}' AND cset_name = 'ping_plugins'";
480
$ping_string = $DB->get_var( $query );
481
$ping_plugins = array_unique( explode( ',', $ping_string ) );
483
// get out if this blog doesn't have this pinger
484
if( ! in_array( $this->code, $ping_plugins ) )
489
// get the user's twitter authentication status
490
global $current_User;
491
$user_id = $current_User->ID;
492
$status = $this->get_twitter_link( $user_id, 'status' );
494
$params['Form']->begin_fieldset( $this->T_('twitter status') );
495
$params['Form']->info_field( '', $status );
496
$params['Form']->end_fieldset( 'foo' );
199
* Define here default collection/blog settings that are to be made available in the backoffice.
201
* @todo (legacy): ideally we'd want a warning if the twitter ping is not enabled
203
* @return array See {@link Plugin::GetDefaultSettings()}.
502
* Get link to twitter oAuth
205
function get_coll_setting_definitions( & $params )
504
function get_twitter_link( $user_id, $give = 'info' )
208
'twitter_username' => array(
209
'label' => $this->T_('Twitter username'),
212
'twitter_password' => array(
213
'label' => $this->T_('Twitter password'),
214
'type' => 'password',
216
'twitter_msg_format' => array(
217
'label' => $this->T_('Message format'),
221
'defaultvalue' => $this->T_('Just posted $title$ $url$ #b2p'),
222
'note' => $this->T_('$title$, $excerpt$ and $url$ will be replaced appropriately.'),
507
// if the keys are not stored we get out
508
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
509
if( ! $results = $DB->get_results( $query ) )
511
if( $give == 'info' )
513
return $this->T_('This installation has not registered as an application with twitter yet.<br />Tell the admin to get off his/her lazy ass and register so you can tweet when you post.');
515
elseif( $give == 'status' )
517
return $this->T_('This installation is not registered!');
522
$query = "SELECT * FROM ".$this->get_sql_table('twitter');
523
if( $app_data = $DB->get_row( $query, ARRAY_A ) )
525
$consumer_key = $app_data['t_key'];
526
$consumer_secret = $app_data['t_secret'];
529
require_once 'twitteroauth/twitteroauth.php';
532
// get and set UserSettings
533
if( empty ( $this->UserSettings ) )
535
if( $give == 'info' )
541
$oauth_token = $this->UserSettings->get( 'twitter_token', $user_id );
542
if( !empty( $oauth_token ) )
543
{ // user has already approved app, get token secret
544
$oauth_token_secret = $this->UserSettings->get( 'twitter_secret', $user_id );
545
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret );
546
// get linked user account
547
$account = $connection->get('account/verify_credentials');
549
// if they turn off approval via twitter forget everything
550
if( isset( $account->error ) )
553
$Session->delete( 'oauth_token' );
554
$Session->delete( 'oauth_token_secret' );
559
$result = $this->T_('Linked to').': @'.$account->screen_name;
564
// create new connection
565
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
567
// set callback params
568
$callback = $this->get_plugin_url( true ).'_callback.php';
570
$req_token = $connection->getRequestToken( $callback );
572
$token = $req_token['oauth_token'];
574
/* Save temporary credentials to session. */
576
$Session->delete( 'oauth_token' );
577
$Session->delete( 'oauth_token_secret' );
578
$Session->set( 'user_id', $user_id );
579
$Session->set( 'plugin_id', $this->ID );
580
$Session->set( 'oauth_token', $req_token['oauth_token'] );
581
$Session->set( 'oauth_token_secret', $req_token['oauth_token_secret'] );
582
$Session->set( 'consumer_key', $consumer_key );
583
$Session->set( 'consumer_secret', $consumer_secret );
585
// info so that we can return to the write page
587
if( $give == 'status' )
589
$this_action = isset($_GET['action']) ? $_GET['action'] : '';
590
if( $this_action == 'edit' )
592
$redir = 'ctrl=items&action=edit&p='.$_GET['p'];
594
elseif( $this_action == 'new' )
596
$redir = 'ctrl=items&action=new&blog='.$_GET['blog'];
600
$Session->set( 'redir', $redir );
604
if( empty( $result ) )
605
{ // wasn't linked to twitter
606
$result = '<a href="'.$connection->getAuthorizeURL( $req_token, false ).'" title="'.$this->T_('sign in with twitter').'"><img src="'.$this->get_plugin_url( true ).'twitter_button.png" width="151" height="24" alt="'.T_('sign in with twitter').'" /></a>';
609
{ // was linked so show it with an option to switch on user profile page
610
if( $give == 'info' )
612
$result = $result.' <a href='.$connection->getAuthorizeURL( $req_token, false ).'>'.$this->T_('Link to another account').'</a>';