Created
May 21, 2010 18:57
-
-
Save johan--/409271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Temporary Workaround Concept for Facebook Bug 10005</title> | |
<!-- | |
Temporary workaround concept for Facebook Bug 10005 | |
http://bugs.developers.facebook.com/show_bug.cgi?id=10005 | |
From your friends at http://awe.sm/ | |
--> | |
</head> | |
<body> | |
<p id="fb_login_btn">Login to Facebook</p> | |
<form name="profile_form" id="profile_form" action="save-this.php" method="post" style="display: none;"> | |
<p id="profile_block"> | |
<label for="profiles">Profile:</label> | |
<select id="profiles" name="profiles"></select> | |
<span id="profile_add">Add Page</span> | |
</p> | |
<input id="credentials" name="credentials" value="" type="hidden"> | |
<input id="profile_name" name="profile_name" value="" type="hidden"> | |
<input type="submit" value="Save"> | |
</form> | |
<div id="fb-root"></div> | |
<script src="http://connect.facebook.net/en_US/all.js"></script> | |
<script type="text/javascript"> | |
var user_id; | |
var access_token; | |
var pages_to; | |
FB.init({appId: '***YOUR_APP_ID_HERE***', status: true, cookie: true, xfbml: true}); | |
document.getElementById('profile_form').onsubmit = function() { | |
assemble_credentials(); | |
return true; | |
} | |
document.getElementById('fb_login_btn').onclick = function() { | |
login_and_request_perms() | |
}; | |
document.getElementById('profile_add').onclick = function() { | |
login_and_request_perms(); | |
} | |
function login_and_request_perms() { | |
FB.login(function(response) { | |
// if user actually logged in... | |
if (response.session) { | |
user_id = response.session.uid; | |
access_token = response.session.access_token; | |
// Add the logged in user to the list of Facebook profiles | |
var name_query = FB.Data.query('select name, uid from user where uid={0}', user_id); | |
name_query.wait(function(rows) { | |
add_profile_select(user_id, rows[0].name, true, true); | |
}); | |
// Check if user is the admin of any Facebook Pages and if those pages are authorized | |
// wait a few seconds because Facebook can be slow to update permissions table | |
pages_to = setTimeout(function() { | |
var pages_query = FB.Data.query('SELECT name, page_id FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid="{0}")', user_id); | |
pages_query.wait(function(rows) { | |
// console.log(rows); | |
if (rows.length > 0) { | |
for (var i = 0; i < rows.length; i++) { | |
callback = { | |
page_id: rows[i].page_id, | |
name: rows[i].name, | |
cb: function() { | |
add_profile_select(this.page_id, this.name, false, false); | |
} | |
}; | |
verify_permissions(rows[i].page_id, callback); | |
} | |
} | |
}); | |
// Display profile selector, Hide Facebook button | |
document.getElementById("profile_form").style.display = "block"; | |
document.getElementById("fb_login_btn").style.display = "none"; | |
}, 2000); | |
} | |
}, {perms:'publish_stream', enable_profile_selector: true}); | |
} | |
function add_profile_select(value, title, selected, reset) { | |
// Adds options to select in form. reset=true prevents duplicates | |
var select_html = '<option value="'+ value +'"'; | |
if (selected == true) { | |
select_html += ' selected="selected"'; | |
} | |
select_html += '>'+ title +'</option>'; | |
if (reset == true) { | |
document.getElementById('profiles').innerHTML = select_html; | |
} else { | |
document.getElementById('profiles').innerHTML += select_html; | |
} | |
} | |
function assemble_credentials() { | |
// get value of selected profile for saving and assemble JSON string for db | |
var target_id = document.getElementById("profiles").options[ document.getElementById("profiles").selectedIndex ].value; | |
document.getElementById("credentials").value = '{"access_token":"'+ access_token +'", "target_id":"'+ target_id +'", "uid":"'+ user_id +'"}'; | |
document.getElementById("profile_name").value = document.getElementById("profiles").options[ document.getElementById("profiles").selectedIndex ].innerHTML; | |
} | |
function verify_permissions(profile_id, callback) { | |
// query to see if the profile_id has publish_stream permission | |
var permissions_query = FB.Data.query('SELECT status_update FROM permissions WHERE uid={0}', profile_id); | |
permissions_query.wait(function(rows) { | |
if ((rows.length == 0) || (rows[0].status_update == 0)) { | |
return false; | |
} else { | |
if (callback) { | |
callback.cb(); | |
} | |
return true; | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment