<?php get_header() ?>

	<?php global $bp; ?>
	<?php locate_template( array( 'leftsidebar.php' ), true ) ?>

	<div id="content">

		<div id="iron-encabezado-container">
			<h2 class="iron-encabezado-seccion"><?php echo __('Mi perfil','buddypress'); ?></h2>
			<ul class="iron-encabezado-ul">
			<li class="iron-encabezado-li"><a <?php if ($bp->current_action=="edit") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/edit"><?php _e ('Editar perfil','buddypress'); ?></a></li>
			<li class="iron-encabezado-li">&middot;&nbsp;<a <?php if ($bp->current_action=="change-avatar") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>profile/change-avatar"><?php _e ('Cambiar avatar','buddypress'); ?></a></li>
			<li class="iron-encabezado-li">&middot;&nbsp;<a <?php if ($bp->current_action=="general") echo 'class="selected"'; ?> href="<?php echo bp_loggedin_user_domain(); ?>settings"><?php _e ('Tu cuenta','buddypress'); ?></a></li>
			</ul>
		</div>

		<div class="padder">

		<div id="content-top"></div>

			<div id="content-middle">

				<div id="content-middle">

				<?php do_action( 'bp_before_member_plugin_template' ) ?>


				<div id="item-body">

					<div class="profile">

						<?php do_action( 'bp_before_member_body' ) ?>

						<?php do_action( 'bp_template_content' ) ?>

						<?php global $bp; ?>

						<?php if ($bp->current_component == 'settings' && $bp->current_action != 'notifications' && $bp->current_action != 'delete-account') : ?>

						<?php /*
						<div class="item-list-tabs no-ajax" id="subnav">
							<ul>
								<?php bp_get_options_nav() ?>

								<?php do_action( 'bp_member_plugin_options_nav' ) ?>
							</ul>
						</div><!-- .item-list-tabs -->
						*/ ?>

						<?php //do_action( 'bp_template_title' ) ?>

						<?php
			            $accounts = array();

			            $fb_uid =  get_user_meta(get_current_user_id(), 'facebook_uid', true);

			            if (is_numeric($fb_uid)) {

			                $accounts["facebook"] = "http://graph.facebook.com/".$fb_uid."/picture?type=large";
			                $accounts["fbid"] = $fb_uid;
			            }

			            $twUsername = get_user_meta(get_current_user_id(), 'twc_name');

			            if ($twUsername) {

			                $base = "http://api.twitter.com/1/users/profile_image/%%username%%?size=bigger";
			                $accounts["twitter"] = str_replace('%%username%%', $twUsername[0] ,$base);
			                $accounts["twcid"] = array_shift(get_user_meta(get_current_user_id(), 'twcid'));
			            }

                        require_once(dirname(__FILE__)."/../../../../plugins/irontec-kronet-connect/utilities.php");
                        $blowfish = new Blowfish();
                        global $wpdb;

                        $inactive = array();

                        foreach($_COOKIE as $key => $value) {

                            if (preg_match("/kc[0-9*]/", $key)) {

                                $key = str_replace("kc","",$key);

                                if (!is_numeric($key)) {

                                    continue;
                                }

                                $query = "Select * from `".$wpdb->prefix."kconnect_satellites` where id = ".$key;
                                $site = array_shift($wpdb->get_results($query));

                                $blowfish->key = $site->Key;

                                if (! get_user_meta( get_current_user_id(), "asoc".$site->id, true)) {

                                    $inactive[] = $site;
                                    continue;
                                }

                                list($uid, $hash) = explode("_", $blowfish->decrypt(base64_decode($value)), 2);
                                $postdata = http_build_query(
                                    array(
                                        'hash' => base64_encode($blowfish->encrypt($hash)),
                                        'uid' => base64_encode($blowfish->encrypt($uid))
                                    )
                                );

                                $opts = array('http' =>
                                    array(
                                        'method'  => 'POST',
                                        'header'  => 'Content-type: application/x-www-form-urlencoded',
                                        'content' => $postdata
                                    )
                                );

                                $context  = stream_context_create($opts);
                                $domain = trim($site->domain."wp-content/plugins/kronet-connect/fetch.php");
                                $response = trim(file_get_contents($domain, false, $context));

                                if ("ok_" == substr($response,0,3)) {

                                    $response = $blowfish->decrypt(base64_decode(substr($response, 3)));
                                    $user = unserialize($response);

                                    if ($user->avatar == "") {

                                        $user->avatar = BP_PLUGIN_URL . '/bp-core/images/mystery-man.jpg';

                                    }

                                    $accounts[$key] = array(

                                        "image" => $user->avatar,
                                        "name" => ucfirst($site->siteName),
                                        "key" => $key,
                                    );
                                }
                            }
                        }

			            /************************************************************
			            ******************* Cuentas por vincular ********************
			            *************************************************************/

			            if ( (!isset($accounts["twitter"]) or !isset($accounts["facebook"])) && function_e) {
							if (function_exists('custom_link_fb_account_button'))
				                echo "<div class='vincular-cuentas'> <label>".__('Vincular mis cuentas de otras redes sociales','buddypress')."</label>";
			            }

			            if (! isset($accounts["twitter"])) {
							if (function_exists('custom_link_fb_account_button')) {
				                $redirect_to = home_url()."/index.php?twc_oauth_start=true&amp;a=undefined&amp;twcver=2&amp;loc=".home_url();
				            	?>
				                <a class="fancybox-tconnect"
				                href="<?echo wp_logout_url()."&redirect_to=".urlencode($redirect_to); ?>">
				                    <img style="margin: 0pt;" alt="Signin with Twitter" src="<?echo home_url();?>/wp-content/plugins/twitconnect/images/sign-in-with-twitter-d.png">
				                </a>
				            <?php } ?>

			            <?php
			            }
						
			            if (function_exists('custom_link_fb_account_button')) :
				            if (! isset($accounts["facebook"])) {
	
				                echo custom_link_fb_account_button("float:left;");
				            }
				        endif;

			            foreach ($inactive as $asociacion) {
                        ?>
                            <a style="float:left;" href='<?php echo wp_logout_url()."&redirect_to=".urlencode(home_url()."?kc=".$asociacion->id); ?>' >
                            <?php
                            if ($asociacion->logo != "") {
                            ?>
                                <img src="<?php echo $asociacion->logo ?>" />
                            <?php
                            } else {

                                echo $asociacion->siteName;

                            } //end if
                            ?>
                            </a>
		                <?php
			            }

			            if ( !isset($accounts["twitter"]) or !isset($accounts["facebook"]) ) {
							if (function_exists('custom_link_fb_account_button'))
			                	echo "</div><br style='clear:both;' />";
			            }

			            /************************************************************
			            ********************* Cuentas vinculadas ********************
			            ************************************************************/
			            if ( count($accounts) > 0) {

			                echo "<div class='vincular-cuentas'> <br style='clear:both;' /><label style='text-align:left;'>".__('Redes sociales vinculadas','buddypress')."</label>";
			            }

			            if (isset($accounts["twitter"])) {
			            ?>
			                <div style='float:left;text-align:center;width:100px;'>
			                    Twitter <br />
			                    <img height="50px" src="<?php echo $accounts["twitter"]; ?>" /><br />

			                    <a href="<?php echo home_url(); ?>/oauth?twcid=<?php echo $accounts["twcid"]; ?>&action=unlink">
			                        <input style="margin:auto;" type="button" value="<?php _e("desvincular", "buddypress"); ?>" />
			                    </a>
			                </div>

			            <?php
			            unset($accounts["twitter"]);
			            }

			            if (isset($accounts["facebook"])) {
			            ?>
			                <div style='float:left;text-align:center;width:100px;'>
			                    Facebook <br />
			                    <img height="50px" src="<?php echo $accounts["facebook"]; ?>" /> <br />

			                    <a href="<?php echo home_url(); ?>/oauth?fbid=<?php echo $accounts["fbid"]; ?>&action=unlink">
			                        <input style="margin:auto;" type="button" value="<?php _e("desvincular", "buddypress"); ?>" />
			                    </a>
			                </div>
			            <?php
			            unset($accounts["facebook"]);
			            }

			            foreach($accounts as $key => $val) {
			            ?>
                            <div style='float:left;text-align:center;width:100px;'>
                                <?php echo $val["name"]; ?> <br />
                                <img height="50px" src="<?php echo $val["image"]; ?>" /> <br />

                                <a href="<?php echo home_url(); ?>/oauth?kc=<?php echo $val["key"]; ?>&action=unlink">
                                    <input style="margin:auto;" type="button" value="<?php _e("desvincular", "buddypress"); ?>" />
                                </a>
                            </div>
			            <?php
			            }

			            if ( count($accounts) > 0) {

			                echo "<br style='clear:both;' /></div>";
			            }
			            ?>

			            <?php
			            // Los usuarios normales pueden eliminar sus cuentas
			            if ( !current_user_can ('manage-options') ) {
			            	echo "<div class='vincular-cuentas'> <label>".__('Eliminar mi cuenta de Kronikoen Sarea definitivamente','buddypress')."</label>"; ?>

			            	<a id="eliminar-cuenta" style="line-height: 30px;" href="<?php echo bp_loggedin_user_domain() . 'settings/delete-account'; ?>"><?php _e ('Delete Account','buddypress'); ?></a>

			            	</div>
			            <?php } ?>

			            <?php endif; ?>

						<?php do_action( 'bp_after_member_body' ) ?>

					</div><!-- .profile -->

				</div><!-- #item-body -->

				<?php do_action( 'bp_after_member_plugin_template' ) ?>

				</div> <!-- content-middle-add -->

			</div> <!-- content-middle -->

		<div id="content-bottom"></div>

		</div><!-- .padder -->
	</div><!-- #content -->

	<?php locate_template( array( 'sidebar.php' ), true ) ?>

<?php get_footer() ?>