| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file facebook_apps_helpers.inc Helper functions for the facebook_apps module.
|
| 4 |
**/
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Removes gunk from the end of a URL and strips it down to the base path.
|
| 8 |
*
|
| 9 |
* eg: http://www.ex.com/foo#?abc=123 --> http://www.ex.com/foo
|
| 10 |
**/
|
| 11 |
function _clean_url($url) {
|
| 12 |
if (substr($url, -1) == '#') {
|
| 13 |
$url = substr($url, 0, -1);
|
| 14 |
}
|
| 15 |
return $url;
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Translate internal Drupal URLs to external FB canvas URLs
|
| 20 |
*
|
| 21 |
* @param $url
|
| 22 |
* The URL to translate. Should be a full path URL path.
|
| 23 |
* @param $fb_conf
|
| 24 |
* Use the $fb_conf param to override the default global $fb_conf
|
| 25 |
**/
|
| 26 |
function _fb_translate_url($url, $fb_conf = NULL) {
|
| 27 |
if (!$fb_conf) {
|
| 28 |
global $fb_conf;
|
| 29 |
}
|
| 30 |
|
| 31 |
// Replace the callback path with the apps path
|
| 32 |
return strtr($url, array($fb_conf['callback_url'] => $fb_conf['app_url']));
|
| 33 |
}
|
| 34 |
function _fb_translate_text($text, $fb_conf = NULL) {
|
| 35 |
if (!$fb_conf) {
|
| 36 |
global $fb_conf;
|
| 37 |
}
|
| 38 |
|
| 39 |
return preg_replace('/' . preg_quote($fb_conf['callback_url']) . '/', $fb_conf['app_url'], $text);
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Helper function to get the full Facebook canvas URL for an internal
|
| 44 |
* Drupal path.
|
| 45 |
*/
|
| 46 |
function facebook_apps_url($path) {
|
| 47 |
global $fb_conf;
|
| 48 |
return $fb_conf['app_url'] . '/' . $path;
|
| 49 |
// return variable_get('facebook_canvas_url', '') . url($path);
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Get's local friends for a local user based on their Facebook friendships.
|
| 54 |
*
|
| 55 |
* @param $user
|
| 56 |
* A local user
|
| 57 |
* @return (array) $friends
|
| 58 |
* An array of local users that are friends with $user on Facebook.
|
| 59 |
**/
|
| 60 |
function facebook_apps_get_local_friends($user = NULL) {
|
| 61 |
if ($user === NULL) {
|
| 62 |
global $user;
|
| 63 |
} else if (is_numeric($user)) {
|
| 64 |
$user = user_load(array('uid' => $user));
|
| 65 |
}
|
| 66 |
|
| 67 |
$facebook =& facebook_get_facebook_object();
|
| 68 |
|
| 69 |
// TODO: This only returns relationships when we have a valid Facebook Apps session to work with, this should probably have a fail-over that uses FQL for times when we don't have that session.
|
| 70 |
$fb_friends = $facebook->api_client->friends_get();
|
| 71 |
|
| 72 |
if (count($fb_friends)) {
|
| 73 |
// TODO: This query might run into problems if the user has A LOT of friends, especially if the database has a BIG authmap table.
|
| 74 |
$result = db_query(
|
| 75 |
"SELECT uid, authname
|
| 76 |
FROM {authmap}
|
| 77 |
WHERE module = 'facebook' AND authname IN (%s)",
|
| 78 |
implode(', ', $fb_friends)
|
| 79 |
);
|
| 80 |
|
| 81 |
while ($row = db_fetch_object($result)) {
|
| 82 |
$friends[$row->authname] = $row->uid;
|
| 83 |
}
|
| 84 |
|
| 85 |
return $friends;
|
| 86 |
}
|
| 87 |
|
| 88 |
return FALSE;
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Same as drupal_goto except uses the proper $facebook->redirect() function.
|
| 93 |
*
|
| 94 |
* @param (string) $path
|
| 95 |
* A path inside your facebook application.
|
| 96 |
* @param (array) $query
|
| 97 |
* A query array.
|
| 98 |
**/
|
| 99 |
function facebook_goto($path, $query = NULL) {
|
| 100 |
// if (isset($_REQUEST['destination'])) {
|
| 101 |
// extract(parse_url(urldecode($_REQUEST['destination'])));
|
| 102 |
// }
|
| 103 |
// else if (isset($_REQUEST['edit']['destination'])) {
|
| 104 |
// extract(parse_url(urldecode($_REQUEST['edit']['destination'])));
|
| 105 |
// }
|
| 106 |
|
| 107 |
global $fb_conf;
|
| 108 |
$url = $fb_conf['app_url'] . '/' . $path . ($query ? '?' . http_build_query($query) : '');
|
| 109 |
|
| 110 |
// Before the redirect, allow modules to react to the end of the page request.
|
| 111 |
module_invoke_all('exit', $url);
|
| 112 |
|
| 113 |
$facebook =& facebook_get_facebook_object();
|
| 114 |
$facebook->redirect($url);
|
| 115 |
exit();
|
| 116 |
}
|
| 117 |
|
| 118 |
function facebook_apps_get_app_path() {
|
| 119 |
global $fb_conf;
|
| 120 |
|
| 121 |
// Using parse_url ensures we get rid of the query string and fragment.
|
| 122 |
$current = parse_url('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
|
| 123 |
|
| 124 |
return strtr(
|
| 125 |
$current['scheme'] . '://' . $current['host'] . $current['path'],
|
| 126 |
array($fb_conf['callback_url'] . '/' => '')
|
| 127 |
);
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Get a list of facebook albums as a simple array. Works great as '#options'
|
| 132 |
* in a form array.
|
| 133 |
**/
|
| 134 |
function facebook_apps_get_albums() {
|
| 135 |
$facebook =& facebook_get_facebook_object();
|
| 136 |
$fb_albums = $facebook->api_client->photos_getAlbums($facebook->user);
|
| 137 |
|
| 138 |
foreach ($fb_albums as $album) {
|
| 139 |
// Need to store the aid as a string, it overflow's the limits of an integer.
|
| 140 |
$albums[(string) $album['aid']] = check_plain($album['name']);
|
| 141 |
}
|
| 142 |
|
| 143 |
return (array) $albums;
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Wrapper function to post to a user's minifeed. If we are posting to another user's feed
|
| 148 |
* we have to first have his infinite session key.
|
| 149 |
*/
|
| 150 |
// function facebook_post_to_minifeed($feed_title, $feed_body, $img, $link, $drupal_uid = NULL) {
|
| 151 |
// global $user;
|
| 152 |
// if (isset($drupal_uid) && $drupal_uid != $user->uid) {
|
| 153 |
// // If we are trying to post to a diff user's minifeed, need inf sesh key.
|
| 154 |
// if (!($fbuser_session_key = facebook_apps_get_session_key($drupal_uid))) {
|
| 155 |
// watchdog('Facebook', t('Could not retreive infinite session key for Drupal user %uid when trying to post to mini-feed.', array('%uid' => $drupal_uid)), WATCHDOG_WARNING);
|
| 156 |
// return FALSE;
|
| 157 |
// }
|
| 158 |
//
|
| 159 |
// require_once './'. drupal_get_path('module', 'facebook') .'/facebookapi_php5/facebook.php';
|
| 160 |
//
|
| 161 |
// extract(facebook_get_conf());
|
| 162 |
// $facebook = new Facebook($facebook_apikey, $facebook_secretkey);
|
| 163 |
// $facebook->api_client->session_key = $fbuser_session_key;
|
| 164 |
// }
|
| 165 |
// else {
|
| 166 |
// // Just use the current FB session.
|
| 167 |
// $facebook =& facebook_get_facebook_object();
|
| 168 |
// }
|
| 169 |
//
|
| 170 |
// try {
|
| 171 |
// $result = $facebook->api_client->feed_publishActionOfUser($feed_title, $feed_body, $img, $link);
|
| 172 |
// }
|
| 173 |
// catch (FacebookRestClientException $fb_e) {
|
| 174 |
// error_handler($fb_e->getCode(), $fb_e->getMessage(), $fb_e->getFile(), $fb_e->getLine());
|
| 175 |
// watchdog('Facebook', t("Exception thrown when posting %fbml to user's mini-feed.", array('%fbml' => $feed_title)), WATCHDOG_ERROR);
|
| 176 |
// }
|
| 177 |
//
|
| 178 |
// if (is_array($result) && $result) {
|
| 179 |
// $status = (boolean)$result[0];
|
| 180 |
// if ($status) {
|
| 181 |
// watchdog('Facebook', t("Successfully posted %fbml to user's mini-feed.", array('%fbml' => $feed_title)));
|
| 182 |
// }
|
| 183 |
// else {
|
| 184 |
// watchdog('Facebook', t("Could not post %fbml to user's mini-feed, may have hit limit for this user.", array('%fbml' => $feed_title)), WATCHDOG_WARNING);
|
| 185 |
// }
|
| 186 |
// return $status;
|
| 187 |
// }
|
| 188 |
// return FALSE;
|
| 189 |
// }
|
| 190 |
|
| 191 |
// function &facebook_set_user() {
|
| 192 |
// $facebook =& facebook_get_facebook_object();
|
| 193 |
// $facebook->set_user($_SESSION['facebook_user'], $_SESSION['facebook_session_key']);
|
| 194 |
// $fbuser = $facebook->require_login();
|
| 195 |
// if (!$fbuser) {
|
| 196 |
// // TODO: Return some error here.
|
| 197 |
// }
|
| 198 |
// return $facebook;
|
| 199 |
// }
|