function fb_admin_token_generate_process in Drupal for Facebook 7.4
1 string reference to 'fb_admin_token_generate_process'
- fb_element_info in ./
fb.module - Implements hook_element_info().
File
- ./
fb.admin.inc, line 1524
Code
function fb_admin_token_generate_process(&$element, &$form_state, &$form) {
// Defaults
$element = $element + array(
'#scope' => array(),
'#options' => array(),
);
$element['#tree'] = TRUE;
// Disable core validation, because options are not regenerated during submit. Avoids "illegal choice".
unset($element['#needs_validation']);
// Known apps can be used to generate new tokens.
$all_apps = fb_admin_all_apps();
$scope = implode(',', $element['#scope']);
$new_token_options = $element['#options'] + array(
-1 => t('Do not save new token'),
);
// TODO: keep track or learn which apps can support local auth.
// or now we assume secret or app token means local.
foreach ($all_apps as $app_data) {
if (!empty($app_data['secret'])) {
// App supports server-side auth.
$auth_url = fb_server_auth_url(array(
'fba' => $app_data['fba'],
'scope' => $scope,
));
}
elseif (!empty($app_data['data']['access_token'])) {
// App supports client-side auth.
$auth_url = fb_client_auth_url(array(
'fba' => $app_data['fba'],
'scope' => $scope,
));
}
else {
// Assume remote auth works.
// @todo find some way to know whether remote auth will actually work.
$auth_url = fb_remote_auth_url($app_data + array(
'scope' => $scope,
));
}
$generate_links[] = array(
'title' => t('Generate new token via %application', array(
'%application' => $app_data['title'],
)),
'href' => $auth_url,
'html' => TRUE,
);
// Detect if the user has generated a new token for the current app.
// Skip when form is submitted.
if (empty($_POST) && ($token = fb_auth_get_token($app_data))) {
if (empty($new_token_options[$token])) {
try {
// TODO: consolodate graph api, use batch.
$me = fb_graph('me', $token);
$app = fb_graph('app', $token);
$new_token_options[$token] = t('%user_name via the %app_name application', array(
'%user_name' => fb_get_name($me),
'%app_name' => fb_get_name($app),
));
drupal_set_message(t('Generated a new access token, but not yet saved. Remember to press the submit button.'), 'warning');
$default_token = $token;
} catch (exception $e) {
fb_log_exception($e, t('Failed to save new access token for %application.', array(
'%application' => $app['title'],
)), $token);
drupal_set_message(t('Failed to validate new token. You may <a target=_blank href=!token_debug_url>debug the token on Facebook</a>.', array(
'!token_debug_url' => 'https://developers.facebook.com/tools/debug/access_token?q=' . $token,
)), 'error');
}
try {
// Only tokens with manage_pages will successfully query me/accounts.
$accounts = fb_graph('me/accounts', $token);
// TODO: support pagination if not all accounts returned.
foreach ($accounts['data'] as $account_data) {
if (!empty($account_data['access_token'])) {
$new_token_options[$account_data['access_token']] = t('%account_name (%account_type) via %app_name', array(
'%account_name' => fb_get_name($account_data),
'%account_type' => $account_data['category'],
'%app_name' => !empty($app) ? fb_get_name($app) : t('(could not determine application)'),
));
}
}
} catch (Exception $e) {
// Probably we just don't have permission to get accounts.
}
}
}
}
// End application loop.
if (count($new_token_options) > 1) {
$element['fb_admin_token_generate_new'] = array(
'#type' => 'radios',
//'#title' => t('Select Access Token'),
'#options' => $new_token_options,
'#default_value' => $default_token,
);
}
if (!empty($generate_links)) {
$element['fb_admin_token_generate_links'] = array(
'#theme' => 'links',
'#links' => $generate_links,
);
}
return $element;
}