From dc9d385931bc5cfe768800d3164f6f2c636fae31 Mon Sep 17 00:00:00 2001
From: madmatter23 <madmatter23@gmail.com>
Date: Tue, 8 May 2012 13:39:02 -0400
Subject: [PATCH] breaking fboauth_action_link_properties() into smaller units
 to permit creation of fboauth_request(), which can
 programatically intitiate fboauth actions

---
 fboauth.module |   69 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/fboauth.module b/fboauth.module
index ac5965b..75e7d4d 100644
--- a/fboauth.module
+++ b/fboauth.module
@@ -316,6 +316,51 @@ function fboauth_action_link_properties($action_name, $redirect = NULL, $app_id
   $app_id = isset($app_id) ? $app_id : variable_get('fboauth_id', '');
   $action = fboauth_action_load($action_name);
 
+  $permissions = fboauth_build_scope($action);
+  $query = fboauth_build_query($action, $redirect);
+
+  $return = array(
+    'query' => array(
+      'client_id' => $app_id,
+      'redirect_uri' => fboauth_action_url('fboauth/' . $action_name, array('absolute' => TRUE, 'query' => $query)),
+    ),
+    'href' => 'https://www.facebook.com/dialog/oauth',
+  );
+
+  if ($permissions) {
+    $return['query']['scope'] = implode(',', $permissions);
+  }
+  return $return;
+}
+
+/**
+ * Provides a mechanism for programmatically initiating an oauth request.
+ */
+function fboauth_request($action_name) {
+  $app_id = isset($app_id) ? $app_id : variable_get('fboauth_id', '');
+  $action = fboauth_action_load($action_name);
+
+  $permissions = fboauth_build_scope($action);
+  $query = fboauth_build_query($action, $redirect);
+
+  // The user will be redirected to fboauth/%fboauth_action with unique authorization code
+  // appended to URL. The associated callback will retrieve an access token and invoke the action.
+  $auth_query = array(
+    'client_id' => $app_id,
+    'redirect_uri' => fboauth_action_url('fboauth/' . $action_name, array('absolute' => TRUE, 'query' => $query)),
+  );
+  if ($permissions) {
+    $auth_query['scope'] = implode(',', $permissions);
+  }
+  $facebook_auth_url = url('https://www.facebook.com/dialog/oauth', array('query' => $auth_query));
+
+  drupal_goto($facebook_auth_url);
+}
+
+/**
+ * Build an array of permissions to determine the scope of the oauth request.
+ */
+function fboauth_build_scope($action) {
   // Determine the required permissions for this action.
   if (!empty($action['permissions'])) {
     $permissions = $action['permissions'];
@@ -329,28 +374,24 @@ function fboauth_action_link_properties($action_name, $redirect = NULL, $app_id
     $permissions = array();
   }
 
+  return $permissions;
+}
+
+/**
+ * Build the $_GET parameters that will be appended to the redirect_uri.
+ * Currently, this supports setting a destination parameter.
+ */
+function fboauth_build_query($action, $redirect = NULL) {
   // Build the return query string.
   $query = array();
   if (isset($redirect)) {
     $query['destination'] = $redirect;
-  }
+  } 
   elseif (!empty($_GET['destination'])) {
     $query['destination'] = $_GET['destination'];
   }
 
-  $return = array(
-    'query' => array(
-      'client_id' => $app_id,
-      'redirect_uri' => fboauth_action_url('fboauth/' . $action_name, array('absolute' => TRUE, 'query' => $query)),
-    ),
-    'href' => 'https://www.facebook.com/dialog/oauth',
-  );
-
-  if ($permissions) {
-    $return['query']['scope'] = implode(',', $permissions);
-  }
-
-  return $return;
+  return $query;
 }
 
 /**
-- 
1.7.5.4

