<?php
error_reporting(E_ALL);

$error_msg = false;

if (!$this->curl_installed()) {
    $error_msg = "This plugin requires the php_curl library to be installed.";
}


if (isset($_POST["formatsettingsformsent"])) {
	
	if(isset($_GET['oauth_token']))
	{
		$_GET['oauth_token'] = false;
	}
    $this->tweetformat = $_POST["tweetformat"];
    $this->options['tweet_format'] = $this->tweetformat;

    $this->bitly_login = trim($_POST['bitly_login']);
    $this->options['bitly_login'] = $this->bitly_login;

    $this->bitly_apikey = trim($_POST['bitly_apikey']);
    $this->options['bitly_apikey'] = $this->bitly_apikey;

    $this->save_options();
}


if (isset($_GET["oauth_token"]) && $_GET["oauth_token"] != false) {
    $twitterapi = new TwitterAPI();
    $twitterapi->consumer_key = $this->consumer_key;
    $twitterapi->consumer_secret = $this->consumer_secret;


    $this->options['oauth_token'] = $_GET['oauth_token'];
    $this->save_options();

    $twitterapi->set_token_pair($this->options['oauth_token'], $this->options['oauth_token_secret']);


    try {
        //Get an Access Token and save it
        $twitterapi->get_access_token();
        //echo "got access token!: " . $twitterapi->oauth_token;
        $this->options['oauth_token'] = $twitterapi->oauth_token;

        $this->oauth_token = $twitterapi->oauth_token;

        $this->options['oauth_token_secret'] = $twitterapi->oauth_token_secret;
        $this->oauth_token_secret = $twitterapi->oauth_token_secret;

        //Set Authenticated to true
        $this->options['authenticated'] = true;
        $this->authenticated = true;

        $this->save_options();


        echo "<h2>Authorized!</h2>";

    }
    catch (exception $e) {
        $error_msg = "Could not get access token.";
    }


}


$consumer_key = $this->options['consumer_key'];
$consumer_secret = $this->options['consumer_secret'];


$wpurl = get_bloginfo("wpurl");


?>

<style>

.note
{
	padding: 30px;
	margin: 20px;
	border-style: solid;
	border-width: 1px;
	border-color: #000;
	font-size: 1em;
	font-weight: bold; 
}

.note-table th
{
	vertical-align:top;
	text-align: left;
}
.note-table td
{
	vertical-align:top;
	text-align: left;
}

.error_msg
{
	margin: 20px;
	padding: 20px;
	border-style: solid;
	border-width: 2px;
	background-color: #FFCCCC;
	font-weight: bold;
}

</style>



<div class="wrap">

<div id="icon-options-general" class="icon32"></div>

<h2>Tweet On Post Settings</h2>

<?php

if($error_msg)
{
	?>
	<div class="error_msg">ERROR: <?php echo $error_msg; ?></div>
	<?php
}

?>

<?php



?>

<form method="POST">

<input type="hidden" name="tweetonpost_auth_formsent" value="true" />


<div class="note">

<p>This plug requires you to register a new application on Twitter.  You can do this at <a target="_new" href="http://twitter.com/oauth_clients">http://twitter.com/oauth_clients</a>.  When you register a new application, use the following settings:</p>


<table class="note-table">

<tr>
<th>Application Name:</th>
<td>Use whatever name you would like to show as your tweets origin.</td>
</tr>

<tr>
<th>Application Website:</th>
<td>Must be the same domain as your Wordpress install.</td>
</tr>

<tr>
<th>Application Type:</th>
<td>Browser</td>
</tr>

<tr>
<th>Callback URL:</th>
<td><?php echo $wpurl; ?>/wp-admin/options-general.php?page=TweetOnPost</td>
</tr>

<tr>
<th>Default Access type:</th>
<td>Read &amp; Write</td>
</tr>

</table>

</div>
<br />
<table class="form-table">
<tr>
	<th>Twitter Consumer Key</th>
	<td><input type="text" size="100" name="consumer_key" value="<?php echo $consumer_key; ?>" /></td>
</tr>
<tr>
	<th>Twitter Consumer Secret</th>
	<td><input type="text" size="100" name="consumer_secret" value="<?php echo $consumer_secret; ?>" /></td>
</tr>

</table>
<?php 
if($this->authenticated) //if
{
?>
<input type="submit" class="button-primary" value="Re-Authenticate Twitter" />
<?php
} else { //else if
?>
<input type="submit" class="button-primary" value="Authenticate Twitter" />
<?php } //end if ?> 

</form>




<?php
//start if
if ($this->authenticated) {
?>

<div style="height:30px;"></div>
<form method="POST">
<input type="hidden" name="formatsettingsformsent" value="true" />



<table class="form-table">

<tr>
<th>Tweet Format:</th>
<td><textarea name="tweetformat" ><?php echo $this->tweetformat; ?></textarea></td>
</tr>

<tr>
<th>Bit.ly Login:</th>
<td><input type="text" name="bitly_login" value="<?php echo $this->bitly_login; ?>" /></td>
</tr>
<tr>
<th>Bit.ly API Key:</th>
<td><input type="text" name="bitly_apikey" value="<?php echo $this->
bitly_apikey; ?> " /></td>
</tr>
</table>

<input class="button-primary" type="submit" value="Save Settings" />
</form>


<?php
}//end if
?>


</div>