<?php

/*
Plugin Name: Irontec - FB Connect
Plugin URI: http://www.irontec.com
Description: Facebook connect adapter throught oauth.
Version: 0.1
Author: Irontec
Author URI: http://www.irontec.com
License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
*/

// Define plugin version and plugin db version
define( 'IRON_FBCONNECT_VERSION', '0.1' );
define( 'IRON_FBCONNECT_DB_VERSION', '0.1' );
define( 'IRON_FBCONNECT_URL', WP_PLUGIN_URL . '/irontec-fb-connect' );

/**** API KEYS *****/
define( 'IRON_FBCONNECT_APPID' , get_option("fbc_api_id") );
define( 'IRON_FBCONNECT_APIKEY' , get_option("fbc_api_key") );
define( 'IRON_FBCONNECT_SECRET' , get_option("fbc_api_sec" ));

// Activation hook
register_activation_hook( __FILE__, 'iron_fbconnect_activation' );
// Deactivation hook
register_deactivation_hook( __FILE__, 'iron_fbconnect_deactivation' );

/*
Activation of the plugin.
*/
function iron_fbconnect_activation() {

    global $wpdb;
    $fields = array("fbc_api_id","fbc_api_key","fbc_api_sec");

    $q = "select count(*) as fexists from ".$wpdb->options." where option_name ='%s'";

    foreach ($fields as $field) {

        $query = sprintf($q, $field);
        $fexists = $wpdb->get_var($query);

        if ($fexists == "0") {

            update_option( $field, "" );
        }
    }

	return true;
}

/*
Deactivation of the plugin.
*/
function iron_fbconnect_deactivation() {

	return true;
}

/*
 * Add a Facebook Login button to the Buddypress sidebar login widget
 * NOTE: If you use this, you mustn't also use the built-in widget - just one or the other!
 */

function custom_fb_login_button()
{
  if ( !is_user_logged_in() )
  {
      ?>
      <a href="<?php echo home_url() ?>/wp-content/plugins/irontec-fb-connect/fetch_response.php">
        <img style="float: right;height: 18px;margin-right: 3px;" src="<?php echo home_url();?>/wp-content/plugins/irontec-fb-connect/image.png" />
      </a>
      <?php

        if (isset($_GET["LoginError"])) {
        ?>
            <span id="LoginWithAjax_Status" class="invalid" style="margin-left: 175px;margin-top: 25px;">
                <?php echo strip_tags($_GET["LoginError"]); ?>
            </span>
        <?php
        }
  }
}

function custom_link_fb_account_button($style="")
{
    ?>
    <a style="<?php echo $style; ?>" href='<?php echo wp_logout_url()."&redirect_to=".home_url() ?>/wp-content/plugins/irontec-fb-connect/fetch_response.php' >
        <img src="<?php echo home_url();?>/wp-content/plugins/irontec-fb-connect/image.png" />
    </a>
    <?php
}

/****************************************************
 ****************** Admin Page **********************
 ****************************************************/

/*
 * Tell WP about the Admin page
 */

function fbc_add_admin_page()
{
    add_options_page("Facebook connect Options", 'Facebook connect', 'administrator', "wp-irontec-fbc", 'fbc_admin_page');
}
add_action('admin_menu', 'fbc_add_admin_page');

/**
  * Link to Settings on Plugins page
  */
add_filter('plugin_action_links', 'fbc_add_plugin_links', 10, 2);
function fbc_add_plugin_links($links, $file)
{
    if( dirname(plugin_basename( __FILE__ )) == dirname($file) )
        $links[] = '<a href="options-general.php?page=' . "wp-irontec-fbc" .'">' . __('Settings','sitemap') . '</a>';
    return $links;
}

/*
 * Output the Admin page
 */
function fbc_admin_page()
{
    ?>
    <div class="wrap">
     <h2><?php echo __("Irontec Facebook connect Options","buddypress");?></h2>
    <?php

    if(version_compare('5', PHP_VERSION, ">"))
    {
        ?><div class="error"><p><?php echo __("Sorry,  Irontec facebook connect requires PHP5.", "buddypress"); ?></p></div><?php
        die();
    }

    //Update options
    if ( isset($_POST['fb_opts_updated']) ) {

        update_option("fbc_api_id" ,$_POST["api_id"]);
        update_option("fbc_api_key" ,$_POST["api_key"]);
        update_option("fbc_api_sec" ,$_POST["api_secret"]);
    }
    ?>

    <form name="formFacebook" method="post" action="">
        <input type="text" size="40" name="api_id" value="<?php echo get_option("fbc_api_id") ?>" /> API ID<br />
        <input type="text" size="40" name="api_key" value="<?php echo get_option("fbc_api_key") ?>" /> API Key<br />
        <input type="text" size="40" name="api_secret" value="<?php echo get_option("fbc_api_sec") ?>" /> API Secret
        <input type="hidden" name="fb_opts_updated" value="1" />
        <div class="submit"><input type="submit" name="Submit" value="Save" /></div>
    </form>
    <hr />

   </div><?php
}
