<?php
ini_set('session.cookie_domain', '.wowdoyou.com');
session_start();
require 'includes/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '132880206758943',
  'secret' => '16d42b51af2920ff3c03f9dbf7c0d5e2',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
    $friends = $facebook->api('/me/friends');
    $photo = $facebook->api('/me/picture');
 } catch (FacebookApiException $e) {
    error_log($e);
  }
}
//$contents= file_get_contents("http://graph.facebook.com/$uid/picture?type=large");
//$im = imagecreatefromstring($contents);
//$fullpath = "images/hahaper_pro.jpg";
//if ($im !== false) {
//	if(file_exists($fullpath)){
//		unlink($fullpath);
//	}
//    imagejpeg($im, $fullpath, 100);
//    imagedestroy($im);
//}
//else {
//    echo 'An error occurred.';
//}
//echo "<img src=\"$fullpath\"/>";

////Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
//function save_image($img,$fullpath){
//	$ch = curl_init ($img);
//	curl_setopt($ch, CURLOPT_HEADER, 0);
//	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//	curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
//	$rawdata=curl_exec($ch);
//	curl_close ($ch);
//	if(file_exists($fullpath)){
//		unlink($fullpath);
//	}
//	$fp = fopen($fullpath,'x');
//	fwrite($fp, $rawdata);
//	fclose($fp);
//}
//save_image ("http://profile.ak.fbcdn.net/profile-ak-snc4/v22943/903/30/n682672444_6629.jpg", "images/hahaper_pro.jpg");
$contents= file_get_contents("http://graph.facebook.com/$uid/picture?type=large");
//$base64_data = base64_encode($contents);
//echo "<img src=\"data:image/jpg;base64,{$base64_data}\"/>";
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
<body>
<script language="javascript">

function Validate_FrmLogin() {
   if (document.FrmLogin.WUsername.value.length < 5)
   {
     alert("Username limit to 5 characters and above.");
     document.FrmLogin.WUsername.focus();
     return (false);
   }

   if (document.FrmLogin.WUsername.value.length > 50)
   {
     alert("Username limit to 50 characters and below.");
  	 document.FrmLogin.WUsername.focus();
     return (false);
   }

   if ((document.FrmLogin.WPassword.value.length < 6) || (document.FrmLogin.WPassword.value.length > 20))
   {
     alert("Password limit between 6-20 characters.");
     document.FrmLogin.WPassword.focus();
     return (false);
   }
   return (true);
 }
</script>
<table width="241" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="14" valign="bottom"><img src="images/box_01.gif" width="14" height="15" /></td>
    <td width="212" valign="bottom" style="background-image:url(images/box_02.gif); background-repeat:repeat-x; background-position:bottom">&nbsp;</td>
    <td width="15" valign="bottom"><img src="images/box_04.gif" width="15" height="15" /></td>
  </tr>
  <tr>
    <td style="background-image:url(images/box_05.gif); background-repeat:repeat-y">&nbsp;</td>
    <td class="white_back"><form action="complete_login.php" method="post" name="FrmLogin" target="_self" id="FrmLogin" style="margin:0px; padding:0px" onSubmit="return Validate_FrmLogin()" requirelogin=1>
                                        <table width="100%" border="0" cellpadding="5" cellspacing="0" class="white_back">
                                          <tr>
                                            <td colspan="2" class="purple_14">Anyone can join !</td>
                                          </tr>
                                          <tr>
                                            <td width="10%">Username&nbsp;:</td>
                                            <td width="90%"><input type="text" name="WUsername" tabindex="1" style="font-size:11px; width:120px" value="<?=$HTTP_COOKIE_VARS[WUsername_c]?>" /></td>
                                          </tr>
                                          <tr>
                                            <td>Password&nbsp;:</td>
                                            <td><input name="WPassword" type="password" tabindex="2" size="15" style="font-size:11px; width:120px" /></td>
                                          </tr>
                                          <tr>
                                            <td colspan="2">
                                            
                                            <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                                              <tr>
                                                <td class="grey_12"><input name="save" id="save" type="checkbox" value="save"<?php if(isset($HTTP_COOKIE_VARS[WUsername_c])) echo ' checked' ?> /><label for="save" id="label_save">Remember Me</label>                                            </td>
                                                <td align="right"><input name="submit" type="submit" id="submit" value="Login" /></td>
                                              </tr>
                                            </table></td>
                                          </tr>
                                          <tr>
                                            <td colspan="2" align="center" class="red_12"><a href="reset.php" target="_self">Forgotten your password?</a></td>
                                          </tr>
        </table>
                                      </form></td>
    <td style="background-image:url(images/box_07.gif); background-repeat:repeat-y">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top"><img src="images/box_10.gif" width="14" height="12" /></td>
    <td valign="top" style="background-image:url(images/box_11.gif); background-repeat:repeat-x; background-position:top">&nbsp;</td>
    <td valign="top"><img src="images/box_13.gif" width="15" height="12" /></td>
  </tr>
</table>
       <fb:login-button autologoutlink="false" perms="email,read_stream,read_friendlists,publish_stream,offline_access"></fb:login-button>
     <fb:login-button autologoutlink="true" perms="email,read_stream,read_friendlists,publish_stream,offline_access"></fb:login-button>
    <?php if ($me): ?>
    <pre><?php print_r($session); ?></pre>
    <pre><?php print_r($me); ?></pre>
	 <?php echo $me['email']; ?>
	 <?php echo $session['access_token']; ?>
	 <?php echo $session['uid']; ?>
	     <?php endif ?>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true // parse XFBML
        });

		// whenever the user logs in, we refresh the page
        FB.Event.subscribe('auth.login', function() {
          window.location.reload();
        });
      };

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
</body>
</html>
