From 2c420cc5eba9cd89b9daf0e10c750a34672a4795 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 29 Oct 2010 23:38:00 +0000 Subject: [PATCH 01/17] New Start/EndHtmlElement events. Allows adding namespaces. --- EVENTS.txt | 8 ++++++++ lib/htmloutputter.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/EVENTS.txt b/EVENTS.txt index 8e730945a4..7d4fc6c162 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -365,6 +365,14 @@ GetValidDaemons: Just before determining which daemons to run HandleQueuedNotice: Handle a queued notice at queue time (or immediately if no queue) - &$notice: notice to handle +StartHtmlElement: Reight before outputting the HTML element - allows plugins to add namespaces +- $action: the current action +- &$attrs: attributes for the HTML element + +EndHtmlElement: Right after outputting the HTML element +- $action: the current action +- &$attrs: attributes for the HTML element + StartShowHeadElements: Right after the tag - $action: the current action diff --git a/lib/htmloutputter.php b/lib/htmloutputter.php index 42bff44908..b341d14958 100644 --- a/lib/htmloutputter.php +++ b/lib/htmloutputter.php @@ -119,9 +119,16 @@ class HTMLOutputter extends XMLOutputter $language = $this->getLanguage(); - $this->elementStart('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', - 'xml:lang' => $language, - 'lang' => $language)); + $attrs = array( + 'xmlns' => 'http://www.w3.org/1999/xhtml', + 'xml:lang' => $language, + 'lang' => $language + ); + + if (Event::handle('StartHtmlElement', array($this, &$attrs))) { + $this->elementStart('html', $attrs); + Event::handle('EndHtmlElement', array($this, &$attrs)); + } } function getLanguage() From 5738e0e4a9e569baf97522a01bb214b178fcb698 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sat, 30 Oct 2010 00:44:16 +0000 Subject: [PATCH 02/17] Beginnings of a new Facebook integration plugin --- plugins/FacebookSSO/FacebookSSOPlugin.php | 301 ++++++ plugins/FacebookSSO/extlib/facebook.php | 963 ++++++++++++++++++ .../FacebookSSO/extlib/fb_ca_chain_bundle.crt | 121 +++ plugins/FacebookSSO/facebookadminpanel.php | 212 ++++ plugins/FacebookSSO/facebooklogin.php | 82 ++ 5 files changed, 1679 insertions(+) create mode 100644 plugins/FacebookSSO/FacebookSSOPlugin.php create mode 100644 plugins/FacebookSSO/extlib/facebook.php create mode 100644 plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt create mode 100644 plugins/FacebookSSO/facebookadminpanel.php create mode 100644 plugins/FacebookSSO/facebooklogin.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php new file mode 100644 index 0000000000..f4790f7056 --- /dev/null +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -0,0 +1,301 @@ +. + * + * @category Pugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Main class for Facebook single-sign-on plugin + * + * + * Simple plugins can be implemented as a single module. Others are more complex + * and require additional modules; these should use their own directory, like + * 'local/plugins/{$name}/'. All files related to the plugin, including images, + * JavaScript, CSS, external libraries or PHP modules should go in the plugin + * directory. + * + * @category Plugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ +class FacebookSSOPlugin extends Plugin +{ + public $appId = null; // Facebook application ID + public $secret = null; // Facebook application secret + public $facebook = null; // Facebook application instance + public $dir = null; // Facebook SSO plugin dir + + /** + * Initializer for this plugin + * + * Plugins overload this method to do any initialization they need, + * like connecting to remote servers or creating paths or so on. + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function initialize() + { + common_debug("XXXXXXXXXXXX " . $this->appId); + // Check defaults and configuration for application ID and secret + if (empty($this->appId)) { + $this->appId = common_config('facebook', 'appid'); + } + + if (empty($this->secret)) { + $this->secret = common_config('facebook', 'secret'); + } + + if (empty($this->facebook)) { + common_debug('instantiating facebook obj'); + $this->facebook = new Facebook( + array( + 'appId' => $this->appId, + 'secret' => $this->secret, + 'cookie' => true + ) + ); + } + + common_debug("FACEBOOK = " . var_export($this->facebook, true)); + + return true; + } + + /** + * Cleanup for this plugin + * + * Plugins overload this method to do any cleanup they need, + * like disconnecting from remote servers or deleting temp files or so on. + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function cleanup() + { + return true; + } + + /** + * Load related modules when needed + * + * Most non-trivial plugins will require extra modules to do their work. Typically + * these include data classes, action classes, widget classes, or external libraries. + * + * This method receives a class name and loads the PHP file related to that class. By + * tradition, action classes typically have files named for the action, all lower-case. + * Data classes are in files with the data class name, initial letter capitalized. + * + * Note that this method will be called for *all* overloaded classes, not just ones + * in this plugin! So, make sure to return true by default to let other plugins, and + * the core code, get a chance. + * + * @param string $cls Name of the class to be loaded + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onAutoload($cls) + { + + $dir = dirname(__FILE__); + + //common_debug("class = " . $cls); + + switch ($cls) + { + case 'Facebook': + include_once $dir . '/extlib/facebook.php'; + return false; + case 'FacebookloginAction': + case 'FacebookadminpanelAction': + include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; + return false; + default: + return true; + } + + } + + /** + * Map URLs to actions + * + * This event handler lets the plugin map URLs on the site to actions (and + * thus an action handler class). Note that the action handler class for an + * action will be named 'FoobarAction', where action = 'foobar'. The class + * must be loaded in the onAutoload() method. + * + * @param Net_URL_Mapper $m path-to-action mapper + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onRouterInitialized($m) + { + // Always add the admin panel route + $m->connect('admin/facebook', array('action' => 'facebookadminpanel')); + + // Only add these routes if an application has been setup on + // Facebook for the plugin to use. + if ($this->hasApplication()) { + $m->connect('main/facebooklogin', array('action' => 'facebooklogin')); + } + + return true; + } + + /* + * Add a login tab for Facebook, but only if there's a Facebook + * application defined for the plugin to use. + * + * @param Action &action the current action + * + * @return void + */ + function onEndLoginGroupNav(&$action) + { + $action_name = $action->trimmed('action'); + + if ($this->hasApplication()) { + + $action->menuItem( + common_local_url('facebooklogin'), + _m('MENU', 'Facebook'), + // TRANS: Tooltip for menu item "Facebook". + _m('Login or register using Facebook'), + 'facebooklogin' === $action_name + ); + } + + return true; + } + + /** + * Add a Facebook tab to the admin panels + * + * @param Widget $nav Admin panel nav + * + * @return boolean hook value + */ + function onEndAdminPanelNav($nav) + { + if (AdminPanelAction::canAdmin('facebook')) { + + $action_name = $nav->action->trimmed('action'); + + $nav->out->menuItem( + common_local_url('facebookadminpanel'), + // TRANS: Menu item. + _m('MENU','Facebook'), + // TRANS: Tooltip for menu item "Facebook". + _m('Facebook integration configuration'), + $action_name == 'facebookadminpanel', + 'nav_facebook_admin_panel' + ); + } + + return true; + } + + /* + * Is there a Facebook application for the plugin to use? + */ + function hasApplication() + { + if (!empty($this->appId) && !empty($this->secret)) { + return true; + } else { + return false; + } + } + + function onStartShowHeader($action) + { + // output
as close to as possible + $action->element('div', array('id' => 'fb-root')); + + $session = $this->facebook->getSession(); + $dir = dirname(__FILE__); + + // XXX: minify this + $script = <<inlineScript( + sprintf($script, + json_encode($this->appId), + json_encode($this->session) + ) + ); + + return true; + } + + function onStartHtmlElement($action, $attrs) { + $attrs = array_merge($attrs, array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')); + return true; + } + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'Facebook Single-Sign-On', + 'version' => STATUSNET_VERSION, + 'author' => 'Zach Copley', + 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO', + 'rawdescription' => + _m('A plugin for single-sign-on with Facebook.')); + return true; + } +} diff --git a/plugins/FacebookSSO/extlib/facebook.php b/plugins/FacebookSSO/extlib/facebook.php new file mode 100644 index 0000000000..d2d2e866bf --- /dev/null +++ b/plugins/FacebookSSO/extlib/facebook.php @@ -0,0 +1,963 @@ + + */ +class FacebookApiException extends Exception +{ + /** + * The result from the API server that represents the exception information. + */ + protected $result; + + /** + * Make a new API Exception with the given result. + * + * @param Array $result the result from the API server + */ + public function __construct($result) { + $this->result = $result; + + $code = isset($result['error_code']) ? $result['error_code'] : 0; + + if (isset($result['error_description'])) { + // OAuth 2.0 Draft 10 style + $msg = $result['error_description']; + } else if (isset($result['error']) && is_array($result['error'])) { + // OAuth 2.0 Draft 00 style + $msg = $result['error']['message']; + } else if (isset($result['error_msg'])) { + // Rest server style + $msg = $result['error_msg']; + } else { + $msg = 'Unknown Error. Check getResult()'; + } + + parent::__construct($msg, $code); + } + + /** + * Return the associated result object returned by the API server. + * + * @returns Array the result from the API server + */ + public function getResult() { + return $this->result; + } + + /** + * Returns the associated type for the error. This will default to + * 'Exception' when a type is not available. + * + * @return String + */ + public function getType() { + if (isset($this->result['error'])) { + $error = $this->result['error']; + if (is_string($error)) { + // OAuth 2.0 Draft 10 style + return $error; + } else if (is_array($error)) { + // OAuth 2.0 Draft 00 style + if (isset($error['type'])) { + return $error['type']; + } + } + } + return 'Exception'; + } + + /** + * To make debugging easier. + * + * @returns String the string representation of the error + */ + public function __toString() { + $str = $this->getType() . ': '; + if ($this->code != 0) { + $str .= $this->code . ': '; + } + return $str . $this->message; + } +} + +/** + * Provides access to the Facebook Platform. + * + * @author Naitik Shah + */ +class Facebook +{ + /** + * Version. + */ + const VERSION = '2.1.2'; + + /** + * Default options for curl. + */ + public static $CURL_OPTS = array( + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 60, + CURLOPT_USERAGENT => 'facebook-php-2.0', + ); + + /** + * List of query parameters that get automatically dropped when rebuilding + * the current URL. + */ + protected static $DROP_QUERY_PARAMS = array( + 'session', + 'signed_request', + ); + + /** + * Maps aliases to Facebook domains. + */ + public static $DOMAIN_MAP = array( + 'api' => 'https://api.facebook.com/', + 'api_read' => 'https://api-read.facebook.com/', + 'graph' => 'https://graph.facebook.com/', + 'www' => 'https://www.facebook.com/', + ); + + /** + * The Application ID. + */ + protected $appId; + + /** + * The Application API Secret. + */ + protected $apiSecret; + + /** + * The active user session, if one is available. + */ + protected $session; + + /** + * The data from the signed_request token. + */ + protected $signedRequest; + + /** + * Indicates that we already loaded the session as best as we could. + */ + protected $sessionLoaded = false; + + /** + * Indicates if Cookie support should be enabled. + */ + protected $cookieSupport = false; + + /** + * Base domain for the Cookie. + */ + protected $baseDomain = ''; + + /** + * Indicates if the CURL based @ syntax for file uploads is enabled. + */ + protected $fileUploadSupport = false; + + /** + * Initialize a Facebook Application. + * + * The configuration: + * - appId: the application ID + * - secret: the application secret + * - cookie: (optional) boolean true to enable cookie support + * - domain: (optional) domain for the cookie + * - fileUpload: (optional) boolean indicating if file uploads are enabled + * + * @param Array $config the application configuration + */ + public function __construct($config) { + $this->setAppId($config['appId']); + $this->setApiSecret($config['secret']); + if (isset($config['cookie'])) { + $this->setCookieSupport($config['cookie']); + } + if (isset($config['domain'])) { + $this->setBaseDomain($config['domain']); + } + if (isset($config['fileUpload'])) { + $this->setFileUploadSupport($config['fileUpload']); + } + } + + /** + * Set the Application ID. + * + * @param String $appId the Application ID + */ + public function setAppId($appId) { + $this->appId = $appId; + return $this; + } + + /** + * Get the Application ID. + * + * @return String the Application ID + */ + public function getAppId() { + return $this->appId; + } + + /** + * Set the API Secret. + * + * @param String $appId the API Secret + */ + public function setApiSecret($apiSecret) { + $this->apiSecret = $apiSecret; + return $this; + } + + /** + * Get the API Secret. + * + * @return String the API Secret + */ + public function getApiSecret() { + return $this->apiSecret; + } + + /** + * Set the Cookie Support status. + * + * @param Boolean $cookieSupport the Cookie Support status + */ + public function setCookieSupport($cookieSupport) { + $this->cookieSupport = $cookieSupport; + return $this; + } + + /** + * Get the Cookie Support status. + * + * @return Boolean the Cookie Support status + */ + public function useCookieSupport() { + return $this->cookieSupport; + } + + /** + * Set the base domain for the Cookie. + * + * @param String $domain the base domain + */ + public function setBaseDomain($domain) { + $this->baseDomain = $domain; + return $this; + } + + /** + * Get the base domain for the Cookie. + * + * @return String the base domain + */ + public function getBaseDomain() { + return $this->baseDomain; + } + + /** + * Set the file upload support status. + * + * @param String $domain the base domain + */ + public function setFileUploadSupport($fileUploadSupport) { + $this->fileUploadSupport = $fileUploadSupport; + return $this; + } + + /** + * Get the file upload support status. + * + * @return String the base domain + */ + public function useFileUploadSupport() { + return $this->fileUploadSupport; + } + + /** + * Get the data from a signed_request token + * + * @return String the base domain + */ + public function getSignedRequest() { + if (!$this->signedRequest) { + if (isset($_REQUEST['signed_request'])) { + $this->signedRequest = $this->parseSignedRequest( + $_REQUEST['signed_request']); + } + } + return $this->signedRequest; + } + + /** + * Set the Session. + * + * @param Array $session the session + * @param Boolean $write_cookie indicate if a cookie should be written. this + * value is ignored if cookie support has been disabled. + */ + public function setSession($session=null, $write_cookie=true) { + $session = $this->validateSessionObject($session); + $this->sessionLoaded = true; + $this->session = $session; + if ($write_cookie) { + $this->setCookieFromSession($session); + } + return $this; + } + + /** + * Get the session object. This will automatically look for a signed session + * sent via the signed_request, Cookie or Query Parameters if needed. + * + * @return Array the session + */ + public function getSession() { + if (!$this->sessionLoaded) { + $session = null; + $write_cookie = true; + + // try loading session from signed_request in $_REQUEST + $signedRequest = $this->getSignedRequest(); + if ($signedRequest) { + // sig is good, use the signedRequest + $session = $this->createSessionFromSignedRequest($signedRequest); + } + + // try loading session from $_REQUEST + if (!$session && isset($_REQUEST['session'])) { + $session = json_decode( + get_magic_quotes_gpc() + ? stripslashes($_REQUEST['session']) + : $_REQUEST['session'], + true + ); + $session = $this->validateSessionObject($session); + } + + // try loading session from cookie if necessary + if (!$session && $this->useCookieSupport()) { + $cookieName = $this->getSessionCookieName(); + if (isset($_COOKIE[$cookieName])) { + $session = array(); + parse_str(trim( + get_magic_quotes_gpc() + ? stripslashes($_COOKIE[$cookieName]) + : $_COOKIE[$cookieName], + '"' + ), $session); + $session = $this->validateSessionObject($session); + // write only if we need to delete a invalid session cookie + $write_cookie = empty($session); + } + } + + $this->setSession($session, $write_cookie); + } + + return $this->session; + } + + /** + * Get the UID from the session. + * + * @return String the UID if available + */ + public function getUser() { + $session = $this->getSession(); + return $session ? $session['uid'] : null; + } + + /** + * Gets a OAuth access token. + * + * @return String the access token + */ + public function getAccessToken() { + $session = $this->getSession(); + // either user session signed, or app signed + if ($session) { + return $session['access_token']; + } else { + return $this->getAppId() .'|'. $this->getApiSecret(); + } + } + + /** + * Get a Login URL for use with redirects. By default, full page redirect is + * assumed. If you are using the generated URL with a window.open() call in + * JavaScript, you can pass in display=popup as part of the $params. + * + * The parameters: + * - next: the url to go to after a successful login + * - cancel_url: the url to go to after the user cancels + * - req_perms: comma separated list of requested extended perms + * - display: can be "page" (default, full page) or "popup" + * + * @param Array $params provide custom parameters + * @return String the URL for the login flow + */ + public function getLoginUrl($params=array()) { + $currentUrl = $this->getCurrentUrl(); + return $this->getUrl( + 'www', + 'login.php', + array_merge(array( + 'api_key' => $this->getAppId(), + 'cancel_url' => $currentUrl, + 'display' => 'page', + 'fbconnect' => 1, + 'next' => $currentUrl, + 'return_session' => 1, + 'session_version' => 3, + 'v' => '1.0', + ), $params) + ); + } + + /** + * Get a Logout URL suitable for use with redirects. + * + * The parameters: + * - next: the url to go to after a successful logout + * + * @param Array $params provide custom parameters + * @return String the URL for the logout flow + */ + public function getLogoutUrl($params=array()) { + return $this->getUrl( + 'www', + 'logout.php', + array_merge(array( + 'next' => $this->getCurrentUrl(), + 'access_token' => $this->getAccessToken(), + ), $params) + ); + } + + /** + * Get a login status URL to fetch the status from facebook. + * + * The parameters: + * - ok_session: the URL to go to if a session is found + * - no_session: the URL to go to if the user is not connected + * - no_user: the URL to go to if the user is not signed into facebook + * + * @param Array $params provide custom parameters + * @return String the URL for the logout flow + */ + public function getLoginStatusUrl($params=array()) { + return $this->getUrl( + 'www', + 'extern/login_status.php', + array_merge(array( + 'api_key' => $this->getAppId(), + 'no_session' => $this->getCurrentUrl(), + 'no_user' => $this->getCurrentUrl(), + 'ok_session' => $this->getCurrentUrl(), + 'session_version' => 3, + ), $params) + ); + } + + /** + * Make an API call. + * + * @param Array $params the API call parameters + * @return the decoded response + */ + public function api(/* polymorphic */) { + $args = func_get_args(); + if (is_array($args[0])) { + return $this->_restserver($args[0]); + } else { + return call_user_func_array(array($this, '_graph'), $args); + } + } + + /** + * Invoke the old restserver.php endpoint. + * + * @param Array $params method call object + * @return the decoded response object + * @throws FacebookApiException + */ + protected function _restserver($params) { + // generic application level parameters + $params['api_key'] = $this->getAppId(); + $params['format'] = 'json-strings'; + + $result = json_decode($this->_oauthRequest( + $this->getApiUrl($params['method']), + $params + ), true); + + // results are returned, errors are thrown + if (is_array($result) && isset($result['error_code'])) { + throw new FacebookApiException($result); + } + return $result; + } + + /** + * Invoke the Graph API. + * + * @param String $path the path (required) + * @param String $method the http method (default 'GET') + * @param Array $params the query/post data + * @return the decoded response object + * @throws FacebookApiException + */ + protected function _graph($path, $method='GET', $params=array()) { + if (is_array($method) && empty($params)) { + $params = $method; + $method = 'GET'; + } + $params['method'] = $method; // method override as we always do a POST + + $result = json_decode($this->_oauthRequest( + $this->getUrl('graph', $path), + $params + ), true); + + // results are returned, errors are thrown + if (is_array($result) && isset($result['error'])) { + $e = new FacebookApiException($result); + switch ($e->getType()) { + // OAuth 2.0 Draft 00 style + case 'OAuthException': + // OAuth 2.0 Draft 10 style + case 'invalid_token': + $this->setSession(null); + } + throw $e; + } + return $result; + } + + /** + * Make a OAuth Request + * + * @param String $path the path (required) + * @param Array $params the query/post data + * @return the decoded response object + * @throws FacebookApiException + */ + protected function _oauthRequest($url, $params) { + if (!isset($params['access_token'])) { + $params['access_token'] = $this->getAccessToken(); + } + + // json_encode all params values that are not strings + foreach ($params as $key => $value) { + if (!is_string($value)) { + $params[$key] = json_encode($value); + } + } + return $this->makeRequest($url, $params); + } + + /** + * Makes an HTTP request. This method can be overriden by subclasses if + * developers want to do fancier things or use something other than curl to + * make the request. + * + * @param String $url the URL to make the request to + * @param Array $params the parameters to use for the POST body + * @param CurlHandler $ch optional initialized curl handle + * @return String the response text + */ + protected function makeRequest($url, $params, $ch=null) { + if (!$ch) { + $ch = curl_init(); + } + + $opts = self::$CURL_OPTS; + if ($this->useFileUploadSupport()) { + $opts[CURLOPT_POSTFIELDS] = $params; + } else { + $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&'); + } + $opts[CURLOPT_URL] = $url; + + // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait + // for 2 seconds if the server does not support this header. + if (isset($opts[CURLOPT_HTTPHEADER])) { + $existing_headers = $opts[CURLOPT_HTTPHEADER]; + $existing_headers[] = 'Expect:'; + $opts[CURLOPT_HTTPHEADER] = $existing_headers; + } else { + $opts[CURLOPT_HTTPHEADER] = array('Expect:'); + } + + curl_setopt_array($ch, $opts); + $result = curl_exec($ch); + + if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT + self::errorLog('Invalid or no certificate authority found, using bundled information'); + curl_setopt($ch, CURLOPT_CAINFO, + dirname(__FILE__) . '/fb_ca_chain_bundle.crt'); + $result = curl_exec($ch); + } + + if ($result === false) { + $e = new FacebookApiException(array( + 'error_code' => curl_errno($ch), + 'error' => array( + 'message' => curl_error($ch), + 'type' => 'CurlException', + ), + )); + curl_close($ch); + throw $e; + } + curl_close($ch); + return $result; + } + + /** + * The name of the Cookie that contains the session. + * + * @return String the cookie name + */ + protected function getSessionCookieName() { + return 'fbs_' . $this->getAppId(); + } + + /** + * Set a JS Cookie based on the _passed in_ session. It does not use the + * currently stored session -- you need to explicitly pass it in. + * + * @param Array $session the session to use for setting the cookie + */ + protected function setCookieFromSession($session=null) { + if (!$this->useCookieSupport()) { + return; + } + + $cookieName = $this->getSessionCookieName(); + $value = 'deleted'; + $expires = time() - 3600; + $domain = $this->getBaseDomain(); + if ($session) { + $value = '"' . http_build_query($session, null, '&') . '"'; + if (isset($session['base_domain'])) { + $domain = $session['base_domain']; + } + $expires = $session['expires']; + } + + // prepend dot if a domain is found + if ($domain) { + $domain = '.' . $domain; + } + + // if an existing cookie is not set, we dont need to delete it + if ($value == 'deleted' && empty($_COOKIE[$cookieName])) { + return; + } + + if (headers_sent()) { + self::errorLog('Could not set cookie. Headers already sent.'); + + // ignore for code coverage as we will never be able to setcookie in a CLI + // environment + // @codeCoverageIgnoreStart + } else { + setcookie($cookieName, $value, $expires, '/', $domain); + } + // @codeCoverageIgnoreEnd + } + + /** + * Validates a session_version=3 style session object. + * + * @param Array $session the session object + * @return Array the session object if it validates, null otherwise + */ + protected function validateSessionObject($session) { + // make sure some essential fields exist + if (is_array($session) && + isset($session['uid']) && + isset($session['access_token']) && + isset($session['sig'])) { + // validate the signature + $session_without_sig = $session; + unset($session_without_sig['sig']); + $expected_sig = self::generateSignature( + $session_without_sig, + $this->getApiSecret() + ); + if ($session['sig'] != $expected_sig) { + self::errorLog('Got invalid session signature in cookie.'); + $session = null; + } + // check expiry time + } else { + $session = null; + } + return $session; + } + + /** + * Returns something that looks like our JS session object from the + * signed token's data + * + * TODO: Nuke this once the login flow uses OAuth2 + * + * @param Array the output of getSignedRequest + * @return Array Something that will work as a session + */ + protected function createSessionFromSignedRequest($data) { + if (!isset($data['oauth_token'])) { + return null; + } + + $session = array( + 'uid' => $data['user_id'], + 'access_token' => $data['oauth_token'], + 'expires' => $data['expires'], + ); + + // put a real sig, so that validateSignature works + $session['sig'] = self::generateSignature( + $session, + $this->getApiSecret() + ); + + return $session; + } + + /** + * Parses a signed_request and validates the signature. + * Then saves it in $this->signed_data + * + * @param String A signed token + * @param Boolean Should we remove the parts of the payload that + * are used by the algorithm? + * @return Array the payload inside it or null if the sig is wrong + */ + protected function parseSignedRequest($signed_request) { + list($encoded_sig, $payload) = explode('.', $signed_request, 2); + + // decode the data + $sig = self::base64UrlDecode($encoded_sig); + $data = json_decode(self::base64UrlDecode($payload), true); + + if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { + self::errorLog('Unknown algorithm. Expected HMAC-SHA256'); + return null; + } + + // check sig + $expected_sig = hash_hmac('sha256', $payload, + $this->getApiSecret(), $raw = true); + if ($sig !== $expected_sig) { + self::errorLog('Bad Signed JSON signature!'); + return null; + } + + return $data; + } + + /** + * Build the URL for api given parameters. + * + * @param $method String the method name. + * @return String the URL for the given parameters + */ + protected function getApiUrl($method) { + static $READ_ONLY_CALLS = + array('admin.getallocation' => 1, + 'admin.getappproperties' => 1, + 'admin.getbannedusers' => 1, + 'admin.getlivestreamvialink' => 1, + 'admin.getmetrics' => 1, + 'admin.getrestrictioninfo' => 1, + 'application.getpublicinfo' => 1, + 'auth.getapppublickey' => 1, + 'auth.getsession' => 1, + 'auth.getsignedpublicsessiondata' => 1, + 'comments.get' => 1, + 'connect.getunconnectedfriendscount' => 1, + 'dashboard.getactivity' => 1, + 'dashboard.getcount' => 1, + 'dashboard.getglobalnews' => 1, + 'dashboard.getnews' => 1, + 'dashboard.multigetcount' => 1, + 'dashboard.multigetnews' => 1, + 'data.getcookies' => 1, + 'events.get' => 1, + 'events.getmembers' => 1, + 'fbml.getcustomtags' => 1, + 'feed.getappfriendstories' => 1, + 'feed.getregisteredtemplatebundlebyid' => 1, + 'feed.getregisteredtemplatebundles' => 1, + 'fql.multiquery' => 1, + 'fql.query' => 1, + 'friends.arefriends' => 1, + 'friends.get' => 1, + 'friends.getappusers' => 1, + 'friends.getlists' => 1, + 'friends.getmutualfriends' => 1, + 'gifts.get' => 1, + 'groups.get' => 1, + 'groups.getmembers' => 1, + 'intl.gettranslations' => 1, + 'links.get' => 1, + 'notes.get' => 1, + 'notifications.get' => 1, + 'pages.getinfo' => 1, + 'pages.isadmin' => 1, + 'pages.isappadded' => 1, + 'pages.isfan' => 1, + 'permissions.checkavailableapiaccess' => 1, + 'permissions.checkgrantedapiaccess' => 1, + 'photos.get' => 1, + 'photos.getalbums' => 1, + 'photos.gettags' => 1, + 'profile.getinfo' => 1, + 'profile.getinfooptions' => 1, + 'stream.get' => 1, + 'stream.getcomments' => 1, + 'stream.getfilters' => 1, + 'users.getinfo' => 1, + 'users.getloggedinuser' => 1, + 'users.getstandardinfo' => 1, + 'users.hasapppermission' => 1, + 'users.isappuser' => 1, + 'users.isverified' => 1, + 'video.getuploadlimits' => 1); + $name = 'api'; + if (isset($READ_ONLY_CALLS[strtolower($method)])) { + $name = 'api_read'; + } + return self::getUrl($name, 'restserver.php'); + } + + /** + * Build the URL for given domain alias, path and parameters. + * + * @param $name String the name of the domain + * @param $path String optional path (without a leading slash) + * @param $params Array optional query parameters + * @return String the URL for the given parameters + */ + protected function getUrl($name, $path='', $params=array()) { + $url = self::$DOMAIN_MAP[$name]; + if ($path) { + if ($path[0] === '/') { + $path = substr($path, 1); + } + $url .= $path; + } + if ($params) { + $url .= '?' . http_build_query($params, null, '&'); + } + return $url; + } + + /** + * Returns the Current URL, stripping it of known FB parameters that should + * not persist. + * + * @return String the current URL + */ + protected function getCurrentUrl() { + $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' + ? 'https://' + : 'http://'; + $currentUrl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + $parts = parse_url($currentUrl); + + // drop known fb params + $query = ''; + if (!empty($parts['query'])) { + $params = array(); + parse_str($parts['query'], $params); + foreach(self::$DROP_QUERY_PARAMS as $key) { + unset($params[$key]); + } + if (!empty($params)) { + $query = '?' . http_build_query($params, null, '&'); + } + } + + // use port if non default + $port = + isset($parts['port']) && + (($protocol === 'http://' && $parts['port'] !== 80) || + ($protocol === 'https://' && $parts['port'] !== 443)) + ? ':' . $parts['port'] : ''; + + // rebuild + return $protocol . $parts['host'] . $port . $parts['path'] . $query; + } + + /** + * Generate a signature for the given params and secret. + * + * @param Array $params the parameters to sign + * @param String $secret the secret to sign with + * @return String the generated signature + */ + protected static function generateSignature($params, $secret) { + // work with sorted data + ksort($params); + + // generate the base string + $base_string = ''; + foreach($params as $key => $value) { + $base_string .= $key . '=' . $value; + } + $base_string .= $secret; + + return md5($base_string); + } + + /** + * Prints to the error log if you aren't in command line mode. + * + * @param String log message + */ + protected static function errorLog($msg) { + // disable error log if we are running in a CLI environment + // @codeCoverageIgnoreStart + if (php_sapi_name() != 'cli') { + error_log($msg); + } + // uncomment this if you want to see the errors on the page + // print 'error_log: '.$msg."\n"; + // @codeCoverageIgnoreEnd + } + + /** + * Base64 encoding that doesn't need to be urlencode()ed. + * Exactly the same as base64_encode except it uses + * - instead of + + * _ instead of / + * + * @param String base64UrlEncodeded string + */ + protected static function base64UrlDecode($input) { + return base64_decode(strtr($input, '-_', '+/')); + } +} diff --git a/plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt b/plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt new file mode 100644 index 0000000000..b92d7190e9 --- /dev/null +++ b/plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt @@ -0,0 +1,121 @@ +-----BEGIN CERTIFICATE----- +MIIFgjCCBGqgAwIBAgIQDKKbZcnESGaLDuEaVk6fQjANBgkqhkiG9w0BAQUFADBm +MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 +d3cuZGlnaWNlcnQuY29tMSUwIwYDVQQDExxEaWdpQ2VydCBIaWdoIEFzc3VyYW5j +ZSBDQS0zMB4XDTEwMDExMzAwMDAwMFoXDTEzMDQxMTIzNTk1OVowaDELMAkGA1UE +BhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExEjAQBgNVBAcTCVBhbG8gQWx0bzEX +MBUGA1UEChMORmFjZWJvb2ssIEluYy4xFzAVBgNVBAMUDiouZmFjZWJvb2suY29t +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9rzj7QIuLM3sdHu1HcI1VcR3g +b5FExKNV646agxSle1aQ/sJev1mh/u91ynwqd2BQmM0brZ1Hc3QrfYyAaiGGgEkp +xbhezyfeYhAyO0TKAYxPnm2cTjB5HICzk6xEIwFbA7SBJ2fSyW1CFhYZyo3tIBjj +19VjKyBfpRaPkzLmRwIDAQABo4ICrDCCAqgwHwYDVR0jBBgwFoAUUOpzidsp+xCP +nuUBINTeeZlIg/cwHQYDVR0OBBYEFPp+tsFBozkjrHlEnZ9J4cFj2eM0MA4GA1Ud +DwEB/wQEAwIFoDAMBgNVHRMBAf8EAjAAMF8GA1UdHwRYMFYwKaAnoCWGI2h0dHA6 +Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9jYTMtZmIuY3JsMCmgJ6AlhiNodHRwOi8vY3Js +NC5kaWdpY2VydC5jb20vY2EzLWZiLmNybDCCAcYGA1UdIASCAb0wggG5MIIBtQYL +YIZIAYb9bAEDAAEwggGkMDoGCCsGAQUFBwIBFi5odHRwOi8vd3d3LmRpZ2ljZXJ0 +LmNvbS9zc2wtY3BzLXJlcG9zaXRvcnkuaHRtMIIBZAYIKwYBBQUHAgIwggFWHoIB +UgBBAG4AeQAgAHUAcwBlACAAbwBmACAAdABoAGkAcwAgAEMAZQByAHQAaQBmAGkA +YwBhAHQAZQAgAGMAbwBuAHMAdABpAHQAdQB0AGUAcwAgAGEAYwBjAGUAcAB0AGEA +bgBjAGUAIABvAGYAIAB0AGgAZQAgAEQAaQBnAGkAQwBlAHIAdAAgAEMAUAAvAEMA +UABTACAAYQBuAGQAIAB0AGgAZQAgAFIAZQBsAHkAaQBuAGcAIABQAGEAcgB0AHkA +IABBAGcAcgBlAGUAbQBlAG4AdAAgAHcAaABpAGMAaAAgAGwAaQBtAGkAdAAgAGwA +aQBhAGIAaQBsAGkAdAB5ACAAYQBuAGQAIABhAHIAZQAgAGkAbgBjAG8AcgBwAG8A +cgBhAHQAZQBkACAAaABlAHIAZQBpAG4AIABiAHkAIAByAGUAZgBlAHIAZQBuAGMA +ZQAuMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjANBgkqhkiG9w0BAQUF +AAOCAQEACOkTIdxMy11+CKrbGNLBSg5xHaTvu/v1wbyn3dO/mf68pPfJnX6ShPYy +4XM4Vk0x4uaFaU4wAGke+nCKGi5dyg0Esg7nemLNKEJaFAJZ9enxZm334lSCeARy +wlDtxULGOFRyGIZZPmbV2eNq5xdU/g3IuBEhL722mTpAye9FU/J8Wsnw54/gANyO +Gzkewigua8ip8Lbs9Cht399yAfbfhUP1DrAm/xEcnHrzPr3cdCtOyJaM6SRPpRqH +ITK5Nc06tat9lXVosSinT3KqydzxBYua9gCFFiR3x3DgZfvXkC6KDdUlDrNcJUub +a1BHnLLP4mxTHL6faAXYd05IxNn/IA== +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIGVTCCBT2gAwIBAgIQCFH5WYFBRcq94CTiEsnCDjANBgkqhkiG9w0BAQUFADBs +MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 +d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j +ZSBFViBSb290IENBMB4XDTA3MDQwMzAwMDAwMFoXDTIyMDQwMzAwMDAwMFowZjEL +MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3 +LmRpZ2ljZXJ0LmNvbTElMCMGA1UEAxMcRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug +Q0EtMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL9hCikQH17+NDdR +CPge+yLtYb4LDXBMUGMmdRW5QYiXtvCgFbsIYOBC6AUpEIc2iihlqO8xB3RtNpcv +KEZmBMcqeSZ6mdWOw21PoF6tvD2Rwll7XjZswFPPAAgyPhBkWBATaccM7pxCUQD5 +BUTuJM56H+2MEb0SqPMV9Bx6MWkBG6fmXcCabH4JnudSREoQOiPkm7YDr6ictFuf +1EutkozOtREqqjcYjbTCuNhcBoz4/yO9NV7UfD5+gw6RlgWYw7If48hl66l7XaAs +zPw82W3tzPpLQ4zJ1LilYRyyQLYoEt+5+F/+07LJ7z20Hkt8HEyZNp496+ynaF4d +32duXvsCAwEAAaOCAvcwggLzMA4GA1UdDwEB/wQEAwIBhjCCAcYGA1UdIASCAb0w +ggG5MIIBtQYLYIZIAYb9bAEDAAIwggGkMDoGCCsGAQUFBwIBFi5odHRwOi8vd3d3 +LmRpZ2ljZXJ0LmNvbS9zc2wtY3BzLXJlcG9zaXRvcnkuaHRtMIIBZAYIKwYBBQUH +AgIwggFWHoIBUgBBAG4AeQAgAHUAcwBlACAAbwBmACAAdABoAGkAcwAgAEMAZQBy +AHQAaQBmAGkAYwBhAHQAZQAgAGMAbwBuAHMAdABpAHQAdQB0AGUAcwAgAGEAYwBj +AGUAcAB0AGEAbgBjAGUAIABvAGYAIAB0AGgAZQAgAEQAaQBnAGkAQwBlAHIAdAAg +AEMAUAAvAEMAUABTACAAYQBuAGQAIAB0AGgAZQAgAFIAZQBsAHkAaQBuAGcAIABQ +AGEAcgB0AHkAIABBAGcAcgBlAGUAbQBlAG4AdAAgAHcAaABpAGMAaAAgAGwAaQBt +AGkAdAAgAGwAaQBhAGIAaQBsAGkAdAB5ACAAYQBuAGQAIABhAHIAZQAgAGkAbgBj +AG8AcgBwAG8AcgBhAHQAZQBkACAAaABlAHIAZQBpAG4AIABiAHkAIAByAGUAZgBl +AHIAZQBuAGMAZQAuMA8GA1UdEwEB/wQFMAMBAf8wNAYIKwYBBQUHAQEEKDAmMCQG +CCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5jb20wgY8GA1UdHwSBhzCB +hDBAoD6gPIY6aHR0cDovL2NybDMuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0SGlnaEFz +c3VyYW5jZUVWUm9vdENBLmNybDBAoD6gPIY6aHR0cDovL2NybDQuZGlnaWNlcnQu +Y29tL0RpZ2lDZXJ0SGlnaEFzc3VyYW5jZUVWUm9vdENBLmNybDAfBgNVHSMEGDAW +gBSxPsNpA/i/RwHUmCYaCALvY2QrwzAdBgNVHQ4EFgQUUOpzidsp+xCPnuUBINTe +eZlIg/cwDQYJKoZIhvcNAQEFBQADggEBAF1PhPGoiNOjsrycbeUpSXfh59bcqdg1 +rslx3OXb3J0kIZCmz7cBHJvUV5eR13UWpRLXuT0uiT05aYrWNTf58SHEW0CtWakv +XzoAKUMncQPkvTAyVab+hA4LmzgZLEN8rEO/dTHlIxxFVbdpCJG1z9fVsV7un5Tk +1nq5GMO41lJjHBC6iy9tXcwFOPRWBW3vnuzoYTYMFEuFFFoMg08iXFnLjIpx2vrF +EIRYzwfu45DC9fkpx1ojcflZtGQriLCnNseaIGHr+k61rmsb5OPs4tk8QUmoIKRU +9ZKNu8BVIASm2LAXFszj0Mi0PeXZhMbT9m5teMl5Q+h6N/9cNUm/ocU= +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIEQjCCA6ugAwIBAgIEQoclDjANBgkqhkiG9w0BAQUFADCBwzELMAkGA1UEBhMC +VVMxFDASBgNVBAoTC0VudHJ1c3QubmV0MTswOQYDVQQLEzJ3d3cuZW50cnVzdC5u +ZXQvQ1BTIGluY29ycC4gYnkgcmVmLiAobGltaXRzIGxpYWIuKTElMCMGA1UECxMc +KGMpIDE5OTkgRW50cnVzdC5uZXQgTGltaXRlZDE6MDgGA1UEAxMxRW50cnVzdC5u +ZXQgU2VjdXJlIFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw0wNjEy +MjIxNTI3MjdaFw0xNDA3MjIxNTU3MjdaMGwxCzAJBgNVBAYTAlVTMRUwEwYDVQQK +EwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xKzApBgNV +BAMTIkRpZ2lDZXJ0IEhpZ2ggQXNzdXJhbmNlIEVWIFJvb3QgQ0EwggEiMA0GCSqG +SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDGzOVz5vvUu+UtLTKm3+WBP8nNJUm2cSrD +1ZQ0Z6IKHLBfaaZAscS3so/QmKSpQVk609yU1jzbdDikSsxNJYL3SqVTEjju80lt +cZF+Y7arpl/DpIT4T2JRvvjF7Ns4kuMG5QiRDMQoQVX7y1qJFX5x6DW/TXIJPb46 +OFBbdzEbjbPHJEWap6xtABRaBLe6E+tRCphBQSJOZWGHgUFQpnlcid4ZSlfVLuZd +HFMsfpjNGgYWpGhz0DQEE1yhcdNafFXbXmThN4cwVgTlEbQpgBLxeTmIogIRfCdm +t4i3ePLKCqg4qwpkwr9mXZWEwaElHoddGlALIBLMQbtuC1E4uEvLAgMBAAGjggET +MIIBDzASBgNVHRMBAf8ECDAGAQH/AgEBMCcGA1UdJQQgMB4GCCsGAQUFBwMBBggr +BgEFBQcDAgYIKwYBBQUHAwQwMwYIKwYBBQUHAQEEJzAlMCMGCCsGAQUFBzABhhdo +dHRwOi8vb2NzcC5lbnRydXN0Lm5ldDAzBgNVHR8ELDAqMCigJqAkhiJodHRwOi8v +Y3JsLmVudHJ1c3QubmV0L3NlcnZlcjEuY3JsMB0GA1UdDgQWBBSxPsNpA/i/RwHU +mCYaCALvY2QrwzALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAU8BdiE1U9s/8KAGv7 +UISX8+1i0BowGQYJKoZIhvZ9B0EABAwwChsEVjcuMQMCAIEwDQYJKoZIhvcNAQEF +BQADgYEAUuVY7HCc/9EvhaYzC1rAIo348LtGIiMduEl5Xa24G8tmJnDioD2GU06r +1kjLX/ktCdpdBgXadbjtdrZXTP59uN0AXlsdaTiFufsqVLPvkp5yMnqnuI3E2o6p +NpAkoQSbB6kUCNnXcW26valgOjDLZFOnr241QiwdBAJAAE/rRa8= +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIE2DCCBEGgAwIBAgIEN0rSQzANBgkqhkiG9w0BAQUFADCBwzELMAkGA1UEBhMC +VVMxFDASBgNVBAoTC0VudHJ1c3QubmV0MTswOQYDVQQLEzJ3d3cuZW50cnVzdC5u +ZXQvQ1BTIGluY29ycC4gYnkgcmVmLiAobGltaXRzIGxpYWIuKTElMCMGA1UECxMc +KGMpIDE5OTkgRW50cnVzdC5uZXQgTGltaXRlZDE6MDgGA1UEAxMxRW50cnVzdC5u +ZXQgU2VjdXJlIFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05OTA1 +MjUxNjA5NDBaFw0xOTA1MjUxNjM5NDBaMIHDMQswCQYDVQQGEwJVUzEUMBIGA1UE +ChMLRW50cnVzdC5uZXQxOzA5BgNVBAsTMnd3dy5lbnRydXN0Lm5ldC9DUFMgaW5j +b3JwLiBieSByZWYuIChsaW1pdHMgbGlhYi4pMSUwIwYDVQQLExwoYykgMTk5OSBF +bnRydXN0Lm5ldCBMaW1pdGVkMTowOAYDVQQDEzFFbnRydXN0Lm5ldCBTZWN1cmUg +U2VydmVyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIGdMA0GCSqGSIb3DQEBAQUA +A4GLADCBhwKBgQDNKIM0VBuJ8w+vN5Ex/68xYMmo6LIQaO2f55M28Qpku0f1BBc/ +I0dNxScZgSYMVHINiC3ZH5oSn7yzcdOAGT9HZnuMNSjSuQrfJNqc1lB5gXpa0zf3 +wkrYKZImZNHkmGw6AIr1NJtl+O3jEP/9uElY3KDegjlrgbEWGWG5VLbmQwIBA6OC +AdcwggHTMBEGCWCGSAGG+EIBAQQEAwIABzCCARkGA1UdHwSCARAwggEMMIHeoIHb +oIHYpIHVMIHSMQswCQYDVQQGEwJVUzEUMBIGA1UEChMLRW50cnVzdC5uZXQxOzA5 +BgNVBAsTMnd3dy5lbnRydXN0Lm5ldC9DUFMgaW5jb3JwLiBieSByZWYuIChsaW1p +dHMgbGlhYi4pMSUwIwYDVQQLExwoYykgMTk5OSBFbnRydXN0Lm5ldCBMaW1pdGVk +MTowOAYDVQQDEzFFbnRydXN0Lm5ldCBTZWN1cmUgU2VydmVyIENlcnRpZmljYXRp +b24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMCmgJ6AlhiNodHRwOi8vd3d3LmVu +dHJ1c3QubmV0L0NSTC9uZXQxLmNybDArBgNVHRAEJDAigA8xOTk5MDUyNTE2MDk0 +MFqBDzIwMTkwNTI1MTYwOTQwWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAU8Bdi +E1U9s/8KAGv7UISX8+1i0BowHQYDVR0OBBYEFPAXYhNVPbP/CgBr+1CEl/PtYtAa +MAwGA1UdEwQFMAMBAf8wGQYJKoZIhvZ9B0EABAwwChsEVjQuMAMCBJAwDQYJKoZI +hvcNAQEFBQADgYEAkNwwAvpkdMKnCqV8IY00F6j7Rw7/JXyNEwr75Ji174z4xRAN +95K+8cPV1ZVqBLssziY2ZcgxxufuP+NXdYR6Ee9GTxj005i7qIcyunL2POI9n9cd +2cNgQ4xYDiKWL2KjLB+6rQXvqzJ4h6BUcxm1XAX5Uj5tLUUL9wqT6u0G+bI= +-----END CERTIFICATE----- diff --git a/plugins/FacebookSSO/facebookadminpanel.php b/plugins/FacebookSSO/facebookadminpanel.php new file mode 100644 index 0000000000..b76b035cd0 --- /dev/null +++ b/plugins/FacebookSSO/facebookadminpanel.php @@ -0,0 +1,212 @@ +. + * + * @category Settings + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Administer global Facebook integration settings + * + * @category Admin + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ +class FacebookadminpanelAction extends AdminPanelAction +{ + /** + * Returns the page title + * + * @return string page title + */ + function title() + { + return _m('Facebook'); + } + + /** + * Instructions for using this form. + * + * @return string instructions + */ + function getInstructions() + { + return _m('Facebook integration settings'); + } + + /** + * Show the Facebook admin panel form + * + * @return void + */ + function showForm() + { + $form = new FacebookAdminPanelForm($this); + $form->show(); + return; + } + + /** + * Save settings from the form + * + * @return void + */ + function saveSettings() + { + static $settings = array( + 'facebook' => array('appid', 'secret'), + ); + + $values = array(); + + foreach ($settings as $section => $parts) { + foreach ($parts as $setting) { + $values[$section][$setting] + = $this->trimmed($setting); + } + } + + // This throws an exception on validation errors + $this->validate($values); + + // assert(all values are valid); + + $config = new Config(); + + $config->query('BEGIN'); + + foreach ($settings as $section => $parts) { + foreach ($parts as $setting) { + Config::save($section, $setting, $values[$section][$setting]); + } + } + + $config->query('COMMIT'); + + return; + } + + function validate(&$values) + { + // appId and secret (can't be too long) + + if (mb_strlen($values['facebook']['appid']) > 255) { + $this->clientError( + _m("Invalid Facebook ID. Max length is 255 characters.") + ); + } + + if (mb_strlen($values['facebook']['secret']) > 255) { + $this->clientError( + _m("Invalid Facebook secret. Max length is 255 characters.") + ); + } + } +} + +class FacebookAdminPanelForm extends AdminForm +{ + /** + * ID of the form + * + * @return int ID of the form + */ + function id() + { + return 'facebookadminpanel'; + } + + /** + * class of the form + * + * @return string class of the form + */ + function formClass() + { + return 'form_settings'; + } + + /** + * Action of the form + * + * @return string URL of the action + */ + function action() + { + return common_local_url('facebookadminpanel'); + } + + /** + * Data elements of the form + * + * @return void + */ + function formData() + { + $this->out->elementStart( + 'fieldset', + array('id' => 'settings_facebook-application') + ); + $this->out->element('legend', null, _m('Facebook application settings')); + $this->out->elementStart('ul', 'form_data'); + + $this->li(); + $this->input( + 'appid', + _m('Application ID'), + _m('ID of your Facebook application'), + 'facebook' + ); + $this->unli(); + + $this->li(); + $this->input( + 'secret', + _m('Secret'), + _m('Application secret'), + 'facebook' + ); + $this->unli(); + + $this->out->elementEnd('ul'); + $this->out->elementEnd('fieldset'); + } + + /** + * Action elements + * + * @return void + */ + function formActions() + { + $this->out->submit('submit', _m('Save'), 'submit', null, _m('Save Facebook settings')); + } +} diff --git a/plugins/FacebookSSO/facebooklogin.php b/plugins/FacebookSSO/facebooklogin.php new file mode 100644 index 0000000000..9ea687d5d9 --- /dev/null +++ b/plugins/FacebookSSO/facebooklogin.php @@ -0,0 +1,82 @@ +. + * + * @category Pugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +class FacebookloginAction extends Action +{ + function handle($args) + { + parent::handle($args); + + if (common_is_real_login()) { + $this->clientError(_m('Already logged in.')); + } + + $this->showPage(); + } + + function getInstructions() + { + // TRANS: Instructions. + return _m('Login with your Facebook Account'); + } + + function showPageNotice() + { + $instr = $this->getInstructions(); + $output = common_markup_to_html($instr); + $this->elementStart('div', 'instructions'); + $this->raw($output); + $this->elementEnd('div'); + } + + function title() + { + // TRANS: Page title. + return _m('Login with Facebook'); + } + + function showContent() { + + $this->elementStart('fieldset'); + $this->element('fb:login-button'); + $this->elementEnd('fieldset'); + } + + function showLocalNav() + { + $nav = new LoginGroupNav($this); + $nav->show(); + } +} + From b54afa0cbc305df4177bb4b081bbc00b002409fd Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 1 Nov 2010 23:50:45 +0000 Subject: [PATCH 03/17] Facebook SSO - add ability to register a new user or connect to an existing local account --- plugins/FacebookSSO/facebookregister.php | 548 +++++++++++++++++++++++ 1 file changed, 548 insertions(+) create mode 100644 plugins/FacebookSSO/facebookregister.php diff --git a/plugins/FacebookSSO/facebookregister.php b/plugins/FacebookSSO/facebookregister.php new file mode 100644 index 0000000000..e21deff880 --- /dev/null +++ b/plugins/FacebookSSO/facebookregister.php @@ -0,0 +1,548 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +class FacebookregisterAction extends Action +{ + + private $facebook = null; // Facebook client + private $fbuid = null; // Facebook user ID + private $fbuser = null; // Facebook user object (JSON) + + function prepare($args) { + + parent::prepare($args); + + $this->facebook = new Facebook( + array( + 'appId' => common_config('facebook', 'appid'), + 'secret' => common_config('facebook', 'secret'), + 'cookie' => true, + ) + ); + + // Check for a Facebook user session + + $session = $this->facebook->getSession(); + $me = null; + + if ($session) { + try { + $this->fbuid = $this->facebook->getUser(); + $this->fbuser = $this->facebook->api('/me'); + } catch (FacebookApiException $e) { + common_log(LOG_ERROR, $e, __FILE__); + } + } + + if (!empty($this->fbuser)) { + + // OKAY, all is well... proceed to register + + common_debug("Found a valid Facebook user.", __FILE__); + } else { + + // This shouldn't happen in the regular course of things + + list($proxy, $ip) = common_client_ip(); + + common_log( + LOG_WARNING, + sprintf( + 'Failed Facebook authentication attempt, proxy = %s, ip = %s.', + $proxy, + $ip + ), + __FILE__ + ); + + $this->clientError( + _m('You must be logged into Facebook to register a local account using Facebook.') + ); + } + + return true; + } + + function handle($args) + { + parent::handle($args); + + if (common_is_real_login()) { + + // User is already logged in, are her accounts already linked? + + $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE); + + if (!empty($flink)) { + + // User already has a linked Facebook account and shouldn't be here! + + common_debug( + sprintf( + 'There\'s already a local user %d linked with Facebook user %s.', + $flink->user_id, + $this->fbuid + ) + ); + + $this->clientError( + _m('There is already a local account linked with that Facebook account.') + ); + + } else { + + // Possibly reconnect an existing account + + $this->connectUser(); + } + + } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { + + $token = $this->trimmed('token'); + + if (!$token || $token != common_session_token()) { + $this->showForm( + _m('There was a problem with your session token. Try again, please.')); + return; + } + + if ($this->arg('create')) { + + if (!$this->boolean('license')) { + $this->showForm( + _m('You can\'t register if you don\'t agree to the license.'), + $this->trimmed('newname') + ); + return; + } + + // We has a valid Facebook session and the Facebook user has + // agreed to the SN license, so create a new user + $this->createNewUser(); + + } else if ($this->arg('connect')) { + + $this->connectNewUser(); + + } else { + + $this->showForm( + _m('An unknown error has occured.'), + $this->trimmed('newname') + ); + } + } else { + + $this->tryLogin(); + } + } + + function showPageNotice() + { + if ($this->error) { + + $this->element('div', array('class' => 'error'), $this->error); + + } else { + + $this->element( + 'div', 'instructions', + // TRANS: %s is the site name. + sprintf( + _m('This is the first time you\'ve logged into %s so we must connect your Facebook to a local account. You can either create a new local account, or connect with an existing local account.'), + common_config('site', 'name') + ) + ); + } + } + + function title() + { + // TRANS: Page title. + return _m('Facebook Setup'); + } + + function showForm($error=null, $username=null) + { + $this->error = $error; + $this->username = $username; + + $this->showPage(); + } + + function showPage() + { + parent::showPage(); + } + + /** + * @fixme much of this duplicates core code, which is very fragile. + * Should probably be replaced with an extensible mini version of + * the core registration form. + */ + function showContent() + { + if (!empty($this->message_text)) { + $this->element('p', null, $this->message); + return; + } + + $this->elementStart('form', array('method' => 'post', + 'id' => 'form_settings_facebook_connect', + 'class' => 'form_settings', + 'action' => common_local_url('facebookregister'))); + $this->elementStart('fieldset', array('id' => 'settings_facebook_connect_options')); + // TRANS: Legend. + $this->element('legend', null, _m('Connection options')); + $this->elementStart('ul', 'form_data'); + $this->elementStart('li'); + $this->element('input', array('type' => 'checkbox', + 'id' => 'license', + 'class' => 'checkbox', + 'name' => 'license', + 'value' => 'true')); + $this->elementStart('label', array('class' => 'checkbox', 'for' => 'license')); + // TRANS: %s is the name of the license used by the user for their status updates. + $message = _m('My text and files are available under %s ' . + 'except this private data: password, ' . + 'email address, IM address, and phone number.'); + $link = '' . + htmlspecialchars(common_config('license', 'title')) . + ''; + $this->raw(sprintf(htmlspecialchars($message), $link)); + $this->elementEnd('label'); + $this->elementEnd('li'); + $this->elementEnd('ul'); + + $this->elementStart('fieldset'); + $this->hidden('token', common_session_token()); + $this->element('legend', null, + // TRANS: Legend. + _m('Create new account')); + $this->element('p', null, + _m('Create a new user with this nickname.')); + $this->elementStart('ul', 'form_data'); + $this->elementStart('li'); + // TRANS: Field label. + $this->input('newname', _m('New nickname'), + ($this->username) ? $this->username : '', + _m('1-64 lowercase letters or numbers, no punctuation or spaces')); + $this->elementEnd('li'); + $this->elementEnd('ul'); + // TRANS: Submit button. + $this->submit('create', _m('BUTTON','Create')); + $this->elementEnd('fieldset'); + + $this->elementStart('fieldset'); + // TRANS: Legend. + $this->element('legend', null, + _m('Connect existing account')); + $this->element('p', null, + _m('If you already have an account, login with your username and password to connect it to your Facebook.')); + $this->elementStart('ul', 'form_data'); + $this->elementStart('li'); + // TRANS: Field label. + $this->input('nickname', _m('Existing nickname')); + $this->elementEnd('li'); + $this->elementStart('li'); + $this->password('password', _m('Password')); + $this->elementEnd('li'); + $this->elementEnd('ul'); + // TRANS: Submit button. + $this->submit('connect', _m('BUTTON','Connect')); + $this->elementEnd('fieldset'); + + $this->elementEnd('fieldset'); + $this->elementEnd('form'); + } + + function message($msg) + { + $this->message_text = $msg; + $this->showPage(); + } + + function createNewUser() + { + if (common_config('site', 'closed')) { + // TRANS: Client error trying to register with registrations not allowed. + $this->clientError(_m('Registration not allowed.')); + return; + } + + $invite = null; + + if (common_config('site', 'inviteonly')) { + $code = $_SESSION['invitecode']; + if (empty($code)) { + // TRANS: Client error trying to register with registrations 'invite only'. + $this->clientError(_m('Registration not allowed.')); + return; + } + + $invite = Invitation::staticGet($code); + + if (empty($invite)) { + // TRANS: Client error trying to register with an invalid invitation code. + $this->clientError(_m('Not a valid invitation code.')); + return; + } + } + + $nickname = $this->trimmed('newname'); + + if (!Validate::string($nickname, array('min_length' => 1, + 'max_length' => 64, + 'format' => NICKNAME_FMT))) { + $this->showForm(_m('Nickname must have only lowercase letters and numbers and no spaces.')); + return; + } + + if (!User::allowed_nickname($nickname)) { + $this->showForm(_m('Nickname not allowed.')); + return; + } + + if (User::staticGet('nickname', $nickname)) { + $this->showForm(_m('Nickname already in use. Try another one.')); + return; + } + + $args = array( + 'nickname' => $nickname, + 'fullname' => $this->fbuser['firstname'] . ' ' . $this->fbuser['lastname'], + // XXX: Figure out how to get email + 'homepage' => $this->fbuser['link'], + 'bio' => $this->fbuser['about'], + 'location' => $this->fbuser['location']['name'] + ); + + if (!empty($invite)) { + $args['code'] = $invite->code; + } + + $user = User::register($args); + + $result = $this->flinkUser($user->id, $this->fbuid); + + if (!$result) { + $this->serverError(_m('Error connecting user to Facebook.')); + return; + } + + common_set_user($user); + common_real_login(true); + + common_log( + LOG_INFO, + sprintf( + 'Registered new user %d from Facebook user %s', + $user->id, + $this->fbuid + ), + __FILE__ + ); + + common_redirect( + common_local_url( + 'showstream', + array('nickname' => $user->nickname) + ), + 303 + ); + } + + function connectNewUser() + { + $nickname = $this->trimmed('nickname'); + $password = $this->trimmed('password'); + + if (!common_check_user($nickname, $password)) { + $this->showForm(_m('Invalid username or password.')); + return; + } + + $user = User::staticGet('nickname', $nickname); + + if (!empty($user)) { + common_debug('Facebook Connect Plugin - ' . + "Legit user to connect to Facebook: $nickname"); + } + + $result = $this->flinkUser($user->id, $this->fbuid); + + if (!$result) { + $this->serverError(_m('Error connecting user to Facebook.')); + return; + } + + common_debug('Facebook Connnect Plugin - ' . + "Connected Facebook user $this->fbuid to local user $user->id"); + + common_set_user($user); + common_real_login(true); + + $this->goHome($user->nickname); + } + + function connectUser() + { + $user = common_current_user(); + + $result = $this->flinkUser($user->id, $this->fbuid); + + if (empty($result)) { + $this->serverError(_m('Error connecting user to Facebook.')); + return; + } + + common_debug('Facebook Connect Plugin - ' . + "Connected Facebook user $this->fbuid to local user $user->id"); + + // Return to Facebook connection settings tab + common_redirect(common_local_url('FBConnectSettings'), 303); + } + + function tryLogin() + { + common_debug('Facebook Connect Plugin - ' . + "Trying login for Facebook user $this->fbuid."); + + $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_CONNECT_SERVICE); + + if (!empty($flink)) { + $user = $flink->getUser(); + + if (!empty($user)) { + + common_debug('Facebook Connect Plugin - ' . + "Logged in Facebook user $flink->foreign_id as user $user->id ($user->nickname)"); + + common_set_user($user); + common_real_login(true); + $this->goHome($user->nickname); + } + + } else { + + common_debug('Facebook Connect Plugin - ' . + "No flink found for fbuid: $this->fbuid - new user"); + + $this->showForm(null, $this->bestNewNickname()); + } + } + + function goHome($nickname) + { + $url = common_get_returnto(); + if ($url) { + // We don't have to return to it again + common_set_returnto(null); + } else { + $url = common_local_url('all', + array('nickname' => + $nickname)); + } + + common_redirect($url, 303); + } + + function flinkUser($user_id, $fbuid) + { + $flink = new Foreign_link(); + $flink->user_id = $user_id; + $flink->foreign_id = $fbuid; + $flink->service = FACEBOOK_SERVICE; + + // Pull the access token from the Facebook cookies + $flink->credentials = $this->facebook->getAccessToken(); + + $flink->created = common_sql_now(); + + $flink_id = $flink->insert(); + + return $flink_id; + } + + function bestNewNickname() + { + if (!empty($this->fbuser['name'])) { + $nickname = $this->nicknamize($this->fbuser['name']); + if ($this->isNewNickname($nickname)) { + return $nickname; + } + } + + // Try the full name + + $fullname = trim($this->fbuser['firstname'] . + ' ' . $this->fbuser['lastname']); + + if (!empty($fullname)) { + $fullname = $this->nicknamize($fullname); + if ($this->isNewNickname($fullname)) { + return $fullname; + } + } + + return null; + } + + /** + * Given a string, try to make it work as a nickname + */ + function nicknamize($str) + { + $str = preg_replace('/\W/', '', $str); + return strtolower($str); + } + + function isNewNickname($str) + { + if (!Validate::string($str, array('min_length' => 1, + 'max_length' => 64, + 'format' => NICKNAME_FMT))) { + return false; + } + if (!User::allowed_nickname($str)) { + return false; + } + if (User::staticGet('nickname', $str)) { + return false; + } + return true; + } + +} From 5ccc548bbcdd62e1b6aba3cd79372b4ace12d16b Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 2 Nov 2010 01:41:31 +0000 Subject: [PATCH 04/17] Facebook SSO - new settings page --- plugins/FacebookSSO/FacebookSSOPlugin.php | 44 +++- plugins/FacebookSSO/facebooklogin.php | 91 +++++++- plugins/FacebookSSO/facebooksettings.php | 264 ++++++++++++++++++++++ 3 files changed, 396 insertions(+), 3 deletions(-) create mode 100644 plugins/FacebookSSO/facebooksettings.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index f4790f7056..fca0275af0 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -32,6 +32,8 @@ if (!defined('STATUSNET')) { exit(1); } +define("FACEBOOK_SERVICE", 2); + /** * Main class for Facebook single-sign-on plugin * @@ -136,7 +138,9 @@ class FacebookSSOPlugin extends Plugin include_once $dir . '/extlib/facebook.php'; return false; case 'FacebookloginAction': + case 'FacebookregisterAction': case 'FacebookadminpanelAction': + case 'FacebooksettingsAction': include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; default: @@ -165,7 +169,21 @@ class FacebookSSOPlugin extends Plugin // Only add these routes if an application has been setup on // Facebook for the plugin to use. if ($this->hasApplication()) { - $m->connect('main/facebooklogin', array('action' => 'facebooklogin')); + + $m->connect( + 'main/facebooklogin', + array('action' => 'facebooklogin') + ); + $m->connect( + 'main/facebookregister', + array('action' => 'facebookregister') + ); + + $m->connect( + 'settings/facebook', + array('action' => 'facebooksettings') + ); + } return true; @@ -224,6 +242,30 @@ class FacebookSSOPlugin extends Plugin return true; } + /* + * Add a tab for managing Facebook Connect settings + * + * @param Action &action the current action + * + * @return void + */ + function onEndConnectSettingsNav(&$action) + { + if ($this->hasApplication()) { + $action_name = $action->trimmed('action'); + + $action->menuItem(common_local_url('facebooksettings'), + // @todo CHECKME: Should be 'Facebook Connect'? + // TRANS: Menu item tab. + _m('MENU','Facebook'), + // TRANS: Tooltip for menu item "Facebook". + _m('Facebook Connect Settings'), + $action_name === 'facebooksettings'); + } + + return true; + } + /* * Is there a Facebook application for the plugin to use? */ diff --git a/plugins/FacebookSSO/facebooklogin.php b/plugins/FacebookSSO/facebooklogin.php index 9ea687d5d9..fce481fc03 100644 --- a/plugins/FacebookSSO/facebooklogin.php +++ b/plugins/FacebookSSO/facebooklogin.php @@ -39,12 +39,91 @@ class FacebookloginAction extends Action parent::handle($args); if (common_is_real_login()) { + $this->clientError(_m('Already logged in.')); + + } else { + + $facebook = new Facebook( + array( + 'appId' => common_config('facebook', 'appid'), + 'secret' => common_config('facebook', 'secret'), + 'cookie' => true, + ) + ); + + $session = $facebook->getSession(); + $me = null; + + if ($session) { + try { + $fbuid = $facebook->getUser(); + $fbuser = $facebook->api('/me'); + } catch (FacebookApiException $e) { + common_log(LOG_ERROR, $e); + } + } + + if (!empty($fbuser)) { + common_debug("Found a valid Facebook user", __FILE__); + + // Check to see if we have a foreign link already + $flink = Foreign_link::getByForeignId($fbuid, FACEBOOK_SERVICE); + + if (empty($flink)) { + + // See if the user would like to register a new local + // account + common_redirect( + common_local_url('facebookregister'), + 303 + ); + + } else { + + // Log our user in! + $user = $flink->getUser(); + + if (!empty($user)) { + + common_debug( + sprintf( + 'Logged in Facebook user $s as user %d (%s)', + $this->fbuid, + $user->id, + $user->nickname + ), + __FILE__ + ); + + common_set_user($user); + common_real_login(true); + $this->goHome($user->nickname); + } + } + + } } - + $this->showPage(); } + function goHome($nickname) + { + $url = common_get_returnto(); + if ($url) { + // We don't have to return to it again + common_set_returnto(null); + } else { + $url = common_local_url( + 'all', + array('nickname' => $nickname) + ); + } + + common_redirect($url, 303); + } + function getInstructions() { // TRANS: Instructions. @@ -69,7 +148,15 @@ class FacebookloginAction extends Action function showContent() { $this->elementStart('fieldset'); - $this->element('fb:login-button'); + + $attrs = array( + 'show-faces' => 'true', + 'width' => '100', + 'max-rows' => '2', + 'perms' => 'user_location,user_website,offline_access,publish_stream' + ); + + $this->element('fb:login-button', $attrs); $this->elementEnd('fieldset'); } diff --git a/plugins/FacebookSSO/facebooksettings.php b/plugins/FacebookSSO/facebooksettings.php new file mode 100644 index 0000000000..e511810369 --- /dev/null +++ b/plugins/FacebookSSO/facebooksettings.php @@ -0,0 +1,264 @@ +. + * + * @category Settings + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Settings for Facebook + * + * @category Settings + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + * + * @see SettingsAction + */ + +class FacebooksettingsAction extends ConnectSettingsAction +{ + private $facebook; + private $flink; + private $user; + + function prepare($args) + { + parent::prepare($args); + + $this->facebook = new Facebook( + array( + 'appId' => common_config('facebook', 'appid'), + 'secret' => common_config('facebook', 'secret'), + 'cookie' => true, + ) + ); + + $this->user = common_current_user(); + $this->flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE); + + return true; + } + + function handlePost($args) + { + // CSRF protection + + $token = $this->trimmed('token'); + if (!$token || $token != common_session_token()) { + $this->showForm( + _m('There was a problem with your session token. Try again, please.') + ); + return; + } + + if ($this->arg('save')) { + $this->saveSettings(); + } else if ($this->arg('disconnect')) { + $this->disconnect(); + } + } + + function title() + { + // TRANS: Page title for Facebook settings. + return _m('Facebook settings'); + } + + /** + * Instructions for use + * + * @return instructions for use + */ + + function getInstructions() + { + return _('Facebook settings'); + } + + function showContent() + { + + if (empty($this->flink)) { + + $this->element( + 'p', + 'instructions', + _m('There is no Facebook user connected to this account.') + ); + + $attrs = array( + 'show-faces' => 'true', + 'perms' => 'user_location,user_website,offline_access,publish_stream' + ); + + $this->element('fb:login-button', $attrs); + + + } else { + + $this->elementStart( + 'form', + array( + 'method' => 'post', + 'id' => 'form_settings_facebook', + 'class' => 'form_settings', + 'action' => common_local_url('facebooksettings') + ) + ); + + $this->hidden('token', common_session_token()); + + $this->element('p', 'form_note', _m('Connected Facebook user')); + + $this->elementStart('p', array('class' => 'facebook-user-display')); + + $this->elementStart( + 'fb:profile-pic', + array('uid' => $this->flink->foreign_id, + 'size' => 'small', + 'linked' => 'true', + 'facebook-logo' => 'true') + ); + $this->elementEnd('fb:profile-pic'); + + $this->elementStart( + 'fb:name', + array('uid' => $this->flink->foreign_id, 'useyou' => 'false') + ); + + $this->elementEnd('fb:name'); + + $this->elementEnd('p'); + + $this->elementStart('ul', 'form_data'); + + $this->elementStart('li'); + + $this->checkbox( + 'noticesync', + _m('Publish my notices to Facebook.'), + ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true + ); + + $this->elementEnd('li'); + + $this->elementStart('li'); + + $this->checkbox( + 'replysync', + _m('Send "@" replies to Facebook.'), + ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true + ); + + $this->elementEnd('li'); + + $this->elementStart('li'); + + // TRANS: Submit button to save synchronisation settings. + $this->submit('save', _m('BUTTON','Save')); + + $this->elementEnd('li'); + + $this->elementEnd('ul'); + + $this->elementStart('fieldset'); + + // TRANS: Legend. + $this->element('legend', null, _m('Disconnect my account from Facebook')); + + if (empty($this->user->password)) { + + $this->elementStart('p', array('class' => 'form_guide')); + // @todo FIXME: Bad i18n. Patchwork message in three parts. + // TRANS: Followed by a link containing text "set a password". + $this->text(_m('Disconnecting your Faceboook ' . + 'would make it impossible to log in! Please ')); + $this->element('a', + array('href' => common_local_url('passwordsettings')), + // TRANS: Preceded by "Please " and followed by " first." + _m('set a password')); + // TRANS: Preceded by "Please set a password". + $this->text(_m(' first.')); + $this->elementEnd('p'); + } else { + + $note = 'Keep your %s account but disconnect from Facebook. ' . + 'You\'ll use your %s password to log in.'; + + $site = common_config('site', 'name'); + + $this->element('p', 'instructions', + sprintf($note, $site, $site)); + + // TRANS: Submit button. + $this->submit('disconnect', _m('BUTTON','Disconnect')); + } + + $this->elementEnd('fieldset'); + + $this->elementEnd('form'); + } + } + + function saveSettings() + { + + $noticesync = $this->boolean('noticesync'); + $replysync = $this->boolean('replysync'); + + $original = clone($this->flink); + $this->flink->set_flags($noticesync, false, $replysync, false); + $result = $this->flink->update($original); + + if ($result === false) { + $this->showForm(_m('There was a problem saving your sync preferences.')); + } else { + // TRANS: Confirmation that synchronisation settings have been saved into the system. + $this->showForm(_m('Sync preferences saved.'), true); + } + } + + function disconnect() + { + $flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE); + $result = $flink->delete(); + + if ($result === false) { + common_log_db_error($user, 'DELETE', __FILE__); + $this->serverError(_m('Couldn\'t delete link to Facebook.')); + return; + } + + $this->showForm(_m('You have disconnected from Facebook.'), true); + + } +} + From 764a297383ad8160b3e4d645d8953ef46a541b09 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 2 Nov 2010 23:13:20 +0000 Subject: [PATCH 05/17] Output filename in log msg if one is supplied --- lib/util.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/util.php b/lib/util.php index d50fa20814..47e52c9152 100644 --- a/lib/util.php +++ b/lib/util.php @@ -1499,6 +1499,7 @@ function common_request_id() function common_log($priority, $msg, $filename=null) { if(Event::handle('StartLog', array(&$priority, &$msg, &$filename))){ + $msg = (empty($filename)) ? $msg : basename($filename) . ' - ' . $msg; $msg = '[' . common_request_id() . '] ' . $msg; $logfile = common_config('site', 'logfile'); if ($logfile) { From 5ea04611450645e3b6b8c36627243e699be5f367 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 2 Nov 2010 23:14:45 +0000 Subject: [PATCH 06/17] Facebook SSO - Log the user out of Facebook when s/he logs out of StatusNet --- plugins/FacebookSSO/FacebookSSOPlugin.php | 89 ++++++++++++++++++----- plugins/FacebookSSO/facebooklogin.php | 18 +++-- 2 files changed, 81 insertions(+), 26 deletions(-) diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index fca0275af0..da109e9c47 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -68,7 +68,6 @@ class FacebookSSOPlugin extends Plugin */ function initialize() { - common_debug("XXXXXXXXXXXX " . $this->appId); // Check defaults and configuration for application ID and secret if (empty($this->appId)) { $this->appId = common_config('facebook', 'appid'); @@ -79,7 +78,6 @@ class FacebookSSOPlugin extends Plugin } if (empty($this->facebook)) { - common_debug('instantiating facebook obj'); $this->facebook = new Facebook( array( 'appId' => $this->appId, @@ -89,8 +87,6 @@ class FacebookSSOPlugin extends Plugin ); } - common_debug("FACEBOOK = " . var_export($this->facebook, true)); - return true; } @@ -243,7 +239,7 @@ class FacebookSSOPlugin extends Plugin } /* - * Add a tab for managing Facebook Connect settings + * Add a tab for user-level Facebook settings * * @param Action &action the current action * @@ -254,13 +250,14 @@ class FacebookSSOPlugin extends Plugin if ($this->hasApplication()) { $action_name = $action->trimmed('action'); - $action->menuItem(common_local_url('facebooksettings'), - // @todo CHECKME: Should be 'Facebook Connect'? - // TRANS: Menu item tab. - _m('MENU','Facebook'), - // TRANS: Tooltip for menu item "Facebook". - _m('Facebook Connect Settings'), - $action_name === 'facebooksettings'); + $action->menuItem( + common_local_url('facebooksettings'), + // TRANS: Menu item tab. + _m('MENU','Facebook'), + // TRANS: Tooltip for menu item "Facebook". + _m('Facebook settings'), + $action_name === 'facebooksettings' + ); } return true; @@ -325,19 +322,75 @@ ENDOFSCRIPT; return true; } + /* + * Log the user out of Facebook, per the Facebook authentication guide + * + * @param Action action the action + */ + function onEndLogout($action) + { + $session = $this->facebook->getSession(); + $fbuser = null; + $fbuid = null; + + if ($session) { + try { + $fbuid = $this->facebook->getUser(); + $fbuser = $this->facebook->api('/me'); + } catch (FacebookApiException $e) { + common_log(LOG_ERROR, $e, __FILE__); + } + } + + if (!empty($fbuser)) { + + $logoutUrl = $this->facebook->getLogoutUrl( + array('next' => common_local_url('public')) + ); + + common_log( + LOG_INFO, + sprintf( + "Logging user out of Facebook (fbuid = %s)", + $fbuid + ), + __FILE__ + ); + + common_redirect($logoutUrl, 303); + } + } + + /* + * Add fbml namespace so Facebook's JavaScript SDK can parse and render + * XFBML tags (e.g: ) + * + * @param Action $action current action + * @param array $attrs array of attributes for the HTML tag + * + * @return nothing + */ function onStartHtmlElement($action, $attrs) { $attrs = array_merge($attrs, array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')); return true; } + /* + * Add version info for this plugin + * + * @param array &$versions plugin version descriptions + */ function onPluginVersion(&$versions) { - $versions[] = array('name' => 'Facebook Single-Sign-On', - 'version' => STATUSNET_VERSION, - 'author' => 'Zach Copley', - 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO', - 'rawdescription' => - _m('A plugin for single-sign-on with Facebook.')); + $versions[] = array( + 'name' => 'Facebook Single-Sign-On', + 'version' => STATUSNET_VERSION, + 'author' => 'Zach Copley', + 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO', + 'rawdescription' => + _m('A plugin for single-sign-on with Facebook.') + ); + return true; } } diff --git a/plugins/FacebookSSO/facebooklogin.php b/plugins/FacebookSSO/facebooklogin.php index fce481fc03..bb30be1af7 100644 --- a/plugins/FacebookSSO/facebooklogin.php +++ b/plugins/FacebookSSO/facebooklogin.php @@ -20,7 +20,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * - * @category Pugin + * @category Plugin * @package StatusNet * @author Zach Copley * @copyright 2010 StatusNet, Inc. @@ -34,6 +34,7 @@ if (!defined('STATUSNET')) { class FacebookloginAction extends Action { + function handle($args) { parent::handle($args); @@ -53,7 +54,7 @@ class FacebookloginAction extends Action ); $session = $facebook->getSession(); - $me = null; + $fbuser = null; if ($session) { try { @@ -86,10 +87,11 @@ class FacebookloginAction extends Action if (!empty($user)) { - common_debug( + common_log( + LOG_INFO, sprintf( - 'Logged in Facebook user $s as user %d (%s)', - $this->fbuid, + 'Logged in Facebook user %s as user %s (%s)', + $fbuid, $user->id, $user->nickname ), @@ -150,9 +152,9 @@ class FacebookloginAction extends Action $this->elementStart('fieldset'); $attrs = array( - 'show-faces' => 'true', - 'width' => '100', - 'max-rows' => '2', + //'show-faces' => 'true', + //'max-rows' => '4', + //'width' => '600', 'perms' => 'user_location,user_website,offline_access,publish_stream' ); From c0cce1891307ebceed448118b318bf8b527dc06e Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 4 Nov 2010 00:43:40 +0000 Subject: [PATCH 07/17] - Some reorganizing - Making the Facebook bridging work --- plugins/FacebookSSO/FacebookSSOPlugin.php | 164 +- .../{ => actions}/facebookadminpanel.php | 0 .../{ => actions}/facebooklogin.php | 0 .../{ => actions}/facebookregister.php | 0 .../{ => actions}/facebooksettings.php | 0 .../extlib/facebookapi_php5_restlib.php | 3702 +++++++++++++++++ .../extlib/jsonwrapper/JSON/JSON.php | 806 ++++ .../extlib/jsonwrapper/JSON/LICENSE | 21 + .../extlib/jsonwrapper/jsonwrapper.php | 6 + .../extlib/jsonwrapper/jsonwrapper_inner.php | 23 + 10 files changed, 4656 insertions(+), 66 deletions(-) rename plugins/FacebookSSO/{ => actions}/facebookadminpanel.php (100%) rename plugins/FacebookSSO/{ => actions}/facebooklogin.php (100%) rename plugins/FacebookSSO/{ => actions}/facebookregister.php (100%) rename plugins/FacebookSSO/{ => actions}/facebooksettings.php (100%) create mode 100644 plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php create mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php create mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE create mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php create mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index da109e9c47..b14ef0bade 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -54,6 +54,7 @@ define("FACEBOOK_SERVICE", 2); class FacebookSSOPlugin extends Plugin { public $appId = null; // Facebook application ID + public $apikey = null; // Facebook API key (for deprecated "Old REST API") public $secret = null; // Facebook application secret public $facebook = null; // Facebook application instance public $dir = null; // Facebook SSO plugin dir @@ -68,25 +69,12 @@ class FacebookSSOPlugin extends Plugin */ function initialize() { - // Check defaults and configuration for application ID and secret - if (empty($this->appId)) { - $this->appId = common_config('facebook', 'appid'); - } + $this->facebook = Facebookclient::getFacebook( + $this->appId, + $this->apikey, + $this->secret + ); - if (empty($this->secret)) { - $this->secret = common_config('facebook', 'secret'); - } - - if (empty($this->facebook)) { - $this->facebook = new Facebook( - array( - 'appId' => $this->appId, - 'secret' => $this->secret, - 'cookie' => true - ) - ); - } - return true; } @@ -130,14 +118,21 @@ class FacebookSSOPlugin extends Plugin switch ($cls) { - case 'Facebook': + case 'Facebook': // New JavaScript SDK include_once $dir . '/extlib/facebook.php'; return false; + case 'FacebookRestClient': // Old REST lib + include_once $dir . '/extlib/facebookapi_php5_restlib.php'; + return false; case 'FacebookloginAction': case 'FacebookregisterAction': case 'FacebookadminpanelAction': case 'FacebooksettingsAction': - include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; + include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; + return false; + case 'Facebookclient': + case 'Facebookqueuehandler': + include_once $dir . '/lib/' . strtolower($cls) . '.php'; return false; default: return true; @@ -145,6 +140,25 @@ class FacebookSSOPlugin extends Plugin } + /* + * Does this $action need the Facebook JavaScripts? + */ + function needsScripts($action) + { + static $needy = array( + 'FacebookloginAction', + 'FacebookregisterAction', + 'FacebookadminpanelAction', + 'FacebooksettingsAction' + ); + + if (in_array(get_class($action), $needy)) { + return true; + } else { + return false; + } + } + /** * Map URLs to actions * @@ -179,7 +193,7 @@ class FacebookSSOPlugin extends Plugin 'settings/facebook', array('action' => 'facebooksettings') ); - + } return true; @@ -268,24 +282,31 @@ class FacebookSSOPlugin extends Plugin */ function hasApplication() { - if (!empty($this->appId) && !empty($this->secret)) { - return true; - } else { - return false; + if (!empty($this->facebook)) { + + $appId = $this->facebook->getAppId(); + $secret = $this->facebook->getApiSecret(); + + if (!empty($appId) && !empty($secret)) { + return true; + } + } + + return false; } function onStartShowHeader($action) { - // output
as close to as possible - $action->element('div', array('id' => 'fb-root')); + if ($this->needsScripts($action)) { - $session = $this->facebook->getSession(); - $dir = dirname(__FILE__); + // output
as close to as possible + $action->element('div', array('id' => 'fb-root')); - // XXX: minify this - $script = <<inlineScript( - sprintf($script, - json_encode($this->appId), - json_encode($this->session) - ) - ); - + $action->inlineScript( + sprintf($script, + json_encode($this->facebook->getAppId()), + json_encode($this->facebook->getSession()) + ) + ); + } + return true; } @@ -329,35 +351,38 @@ ENDOFSCRIPT; */ function onEndLogout($action) { - $session = $this->facebook->getSession(); - $fbuser = null; - $fbuid = null; + if ($this->hasApplication()) { + $session = $this->facebook->getSession(); + $fbuser = null; + $fbuid = null; - if ($session) { - try { - $fbuid = $this->facebook->getUser(); - $fbuser = $this->facebook->api('/me'); - } catch (FacebookApiException $e) { - common_log(LOG_ERROR, $e, __FILE__); - } - } + if ($session) { + try { + $fbuid = $this->facebook->getUser(); + $fbuser = $this->facebook->api('/me'); + } catch (FacebookApiException $e) { + common_log(LOG_ERROR, $e, __FILE__); + } + } - if (!empty($fbuser)) { + if (!empty($fbuser)) { - $logoutUrl = $this->facebook->getLogoutUrl( - array('next' => common_local_url('public')) - ); + $logoutUrl = $this->facebook->getLogoutUrl( + array('next' => common_local_url('public')) + ); - common_log( - LOG_INFO, - sprintf( - "Logging user out of Facebook (fbuid = %s)", - $fbuid - ), - __FILE__ - ); + common_log( + LOG_INFO, + sprintf( + "Logging user out of Facebook (fbuid = %s)", + $fbuid + ), + __FILE__ + ); + common_debug("LOGOUT URL = $logoutUrl"); + common_redirect($logoutUrl, 303); + } - common_redirect($logoutUrl, 303); } } @@ -371,7 +396,14 @@ ENDOFSCRIPT; * @return nothing */ function onStartHtmlElement($action, $attrs) { - $attrs = array_merge($attrs, array('xmlns:fb' => 'http://www.facebook.com/2008/fbml')); + + if ($this->needsScripts($action)) { + $attrs = array_merge( + $attrs, + array('xmlns:fb' => 'http://www.facebook.com/2008/fbml') + ); + } + return true; } @@ -385,10 +417,10 @@ ENDOFSCRIPT; $versions[] = array( 'name' => 'Facebook Single-Sign-On', 'version' => STATUSNET_VERSION, - 'author' => 'Zach Copley', + 'author' => 'Craig Andrews, Zach Copley', 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO', 'rawdescription' => - _m('A plugin for single-sign-on with Facebook.') + _m('A plugin for integrating StatusNet with Facebook.') ); return true; diff --git a/plugins/FacebookSSO/facebookadminpanel.php b/plugins/FacebookSSO/actions/facebookadminpanel.php similarity index 100% rename from plugins/FacebookSSO/facebookadminpanel.php rename to plugins/FacebookSSO/actions/facebookadminpanel.php diff --git a/plugins/FacebookSSO/facebooklogin.php b/plugins/FacebookSSO/actions/facebooklogin.php similarity index 100% rename from plugins/FacebookSSO/facebooklogin.php rename to plugins/FacebookSSO/actions/facebooklogin.php diff --git a/plugins/FacebookSSO/facebookregister.php b/plugins/FacebookSSO/actions/facebookregister.php similarity index 100% rename from plugins/FacebookSSO/facebookregister.php rename to plugins/FacebookSSO/actions/facebookregister.php diff --git a/plugins/FacebookSSO/facebooksettings.php b/plugins/FacebookSSO/actions/facebooksettings.php similarity index 100% rename from plugins/FacebookSSO/facebooksettings.php rename to plugins/FacebookSSO/actions/facebooksettings.php diff --git a/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php b/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php new file mode 100644 index 0000000000..e249a326b2 --- /dev/null +++ b/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php @@ -0,0 +1,3702 @@ +secret = $secret; + $this->session_key = $session_key; + $this->api_key = $api_key; + $this->batch_mode = FacebookRestClient::BATCH_MODE_DEFAULT; + $this->last_call_id = 0; + $this->call_as_apikey = ''; + $this->use_curl_if_available = true; + $this->server_addr = + Facebook::get_facebook_url('api') . '/restserver.php'; + $this->photo_server_addr = + Facebook::get_facebook_url('api-photo') . '/restserver.php'; + + if (!empty($GLOBALS['facebook_config']['debug'])) { + $this->cur_id = 0; + ?> + +user = $uid; + } + + + /** + * Switch to use the session secret instead of the app secret, + * for desktop and unsecured environment + */ + public function use_session_secret($session_secret) { + $this->secret = $session_secret; + $this->using_session_secret = true; + } + + /** + * Normally, if the cURL library/PHP extension is available, it is used for + * HTTP transactions. This allows that behavior to be overridden, falling + * back to a vanilla-PHP implementation even if cURL is installed. + * + * @param $use_curl_if_available bool whether or not to use cURL if available + */ + public function set_use_curl_if_available($use_curl_if_available) { + $this->use_curl_if_available = $use_curl_if_available; + } + + /** + * Start a batch operation. + */ + public function begin_batch() { + if ($this->pending_batch()) { + $code = FacebookAPIErrorCodes::API_EC_BATCH_ALREADY_STARTED; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + $this->batch_queue = array(); + $this->pending_batch = true; + } + + /* + * End current batch operation + */ + public function end_batch() { + if (!$this->pending_batch()) { + $code = FacebookAPIErrorCodes::API_EC_BATCH_NOT_STARTED; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + $this->pending_batch = false; + + $this->execute_server_side_batch(); + $this->batch_queue = null; + } + + /** + * are we currently queueing up calls for a batch? + */ + public function pending_batch() { + return $this->pending_batch; + } + + private function execute_server_side_batch() { + $item_count = count($this->batch_queue); + $method_feed = array(); + foreach ($this->batch_queue as $batch_item) { + $method = $batch_item['m']; + $params = $batch_item['p']; + list($get, $post) = $this->finalize_params($method, $params); + $method_feed[] = $this->create_url_string(array_merge($post, $get)); + } + + $serial_only = + ($this->batch_mode == FacebookRestClient::BATCH_MODE_SERIAL_ONLY); + + $params = array('method_feed' => json_encode($method_feed), + 'serial_only' => $serial_only, + 'format' => $this->format); + $result = $this->call_method('facebook.batch.run', $params); + + if (is_array($result) && isset($result['error_code'])) { + throw new FacebookRestClientException($result['error_msg'], + $result['error_code']); + } + + for ($i = 0; $i < $item_count; $i++) { + $batch_item = $this->batch_queue[$i]; + $batch_item['p']['format'] = $this->format; + $batch_item_result = $this->convert_result($result[$i], + $batch_item['m'], + $batch_item['p']); + + if (is_array($batch_item_result) && + isset($batch_item_result['error_code'])) { + throw new FacebookRestClientException($batch_item_result['error_msg'], + $batch_item_result['error_code']); + } + $batch_item['r'] = $batch_item_result; + } + } + + public function begin_permissions_mode($permissions_apikey) { + $this->call_as_apikey = $permissions_apikey; + } + + public function end_permissions_mode() { + $this->call_as_apikey = ''; + } + + + /* + * If a page is loaded via HTTPS, then all images and static + * resources need to be printed with HTTPS urls to avoid + * mixed content warnings. If your page loads with an HTTPS + * url, then call set_use_ssl_resources to retrieve the correct + * urls. + */ + public function set_use_ssl_resources($is_ssl = true) { + $this->use_ssl_resources = $is_ssl; + } + + /** + * Returns public information for an application (as shown in the application + * directory) by either application ID, API key, or canvas page name. + * + * @param int $application_id (Optional) app id + * @param string $application_api_key (Optional) api key + * @param string $application_canvas_name (Optional) canvas name + * + * Exactly one argument must be specified, otherwise it is an error. + * + * @return array An array of public information about the application. + */ + public function application_getPublicInfo($application_id=null, + $application_api_key=null, + $application_canvas_name=null) { + return $this->call_method('facebook.application.getPublicInfo', + array('application_id' => $application_id, + 'application_api_key' => $application_api_key, + 'application_canvas_name' => $application_canvas_name)); + } + + /** + * Creates an authentication token to be used as part of the desktop login + * flow. For more information, please see + * http://wiki.developers.facebook.com/index.php/Auth.createToken. + * + * @return string An authentication token. + */ + public function auth_createToken() { + return $this->call_method('facebook.auth.createToken'); + } + + /** + * Returns the session information available after current user logs in. + * + * @param string $auth_token the token returned by auth_createToken or + * passed back to your callback_url. + * @param bool $generate_session_secret whether the session returned should + * include a session secret + * @param string $host_url the connect site URL for which the session is + * being generated. This parameter is optional, unless + * you want Facebook to determine which of several base domains + * to choose from. If this third argument isn't provided but + * there are several base domains, the first base domain is + * chosen. + * + * @return array An assoc array containing session_key, uid + */ + public function auth_getSession($auth_token, + $generate_session_secret = false, + $host_url = null) { + if (!$this->pending_batch()) { + $result = $this->call_method( + 'facebook.auth.getSession', + array('auth_token' => $auth_token, + 'generate_session_secret' => $generate_session_secret, + 'host_url' => $host_url)); + $this->session_key = $result['session_key']; + + if (!empty($result['secret']) && !$generate_session_secret) { + // desktop apps have a special secret + $this->secret = $result['secret']; + } + + return $result; + } + } + + /** + * Generates a session-specific secret. This is for integration with + * client-side API calls, such as the JS library. + * + * @return array A session secret for the current promoted session + * + * @error API_EC_PARAM_SESSION_KEY + * API_EC_PARAM_UNKNOWN + */ + public function auth_promoteSession() { + return $this->call_method('facebook.auth.promoteSession'); + } + + /** + * Expires the session that is currently being used. If this call is + * successful, no further calls to the API (which require a session) can be + * made until a valid session is created. + * + * @return bool true if session expiration was successful, false otherwise + */ + public function auth_expireSession() { + return $this->call_method('facebook.auth.expireSession'); + } + + /** + * Revokes the given extended permission that the user granted at some + * prior time (for instance, offline_access or email). If no user is + * provided, it will be revoked for the user of the current session. + * + * @param string $perm The permission to revoke + * @param int $uid The user for whom to revoke the permission. + */ + public function auth_revokeExtendedPermission($perm, $uid=null) { + return $this->call_method('facebook.auth.revokeExtendedPermission', + array('perm' => $perm, 'uid' => $uid)); + } + + /** + * Revokes the user's agreement to the Facebook Terms of Service for your + * application. If you call this method for one of your users, you will no + * longer be able to make API requests on their behalf until they again + * authorize your application. Use with care. Note that if this method is + * called without a user parameter, then it will revoke access for the + * current session's user. + * + * @param int $uid (Optional) User to revoke + * + * @return bool true if revocation succeeds, false otherwise + */ + public function auth_revokeAuthorization($uid=null) { + return $this->call_method('facebook.auth.revokeAuthorization', + array('uid' => $uid)); + } + + /** + * Get public key that is needed to verify digital signature + * an app may pass to other apps. The public key is only used by + * other apps for verification purposes. + * @param string API key of an app + * @return string The public key for the app. + */ + public function auth_getAppPublicKey($target_app_key) { + return $this->call_method('facebook.auth.getAppPublicKey', + array('target_app_key' => $target_app_key)); + } + + /** + * Get a structure that can be passed to another app + * as proof of session. The other app can verify it using public + * key of this app. + * + * @return signed public session data structure. + */ + public function auth_getSignedPublicSessionData() { + return $this->call_method('facebook.auth.getSignedPublicSessionData', + array()); + } + + /** + * Returns the number of unconnected friends that exist in this application. + * This number is determined based on the accounts registered through + * connect.registerUsers() (see below). + */ + public function connect_getUnconnectedFriendsCount() { + return $this->call_method('facebook.connect.getUnconnectedFriendsCount', + array()); + } + + /** + * This method is used to create an association between an external user + * account and a Facebook user account, as per Facebook Connect. + * + * This method takes an array of account data, including a required email_hash + * and optional account data. For each connected account, if the user exists, + * the information is added to the set of the user's connected accounts. + * If the user has already authorized the site, the connected account is added + * in the confirmed state. If the user has not yet authorized the site, the + * connected account is added in the pending state. + * + * This is designed to help Facebook Connect recognize when two Facebook + * friends are both members of a external site, but perhaps are not aware of + * it. The Connect dialog (see fb:connect-form) is used when friends can be + * identified through these email hashes. See the following url for details: + * + * http://wiki.developers.facebook.com/index.php/Connect.registerUsers + * + * @param mixed $accounts A (JSON-encoded) array of arrays, where each array + * has three properties: + * 'email_hash' (req) - public email hash of account + * 'account_id' (opt) - remote account id; + * 'account_url' (opt) - url to remote account; + * + * @return array The list of email hashes for the successfully registered + * accounts. + */ + public function connect_registerUsers($accounts) { + return $this->call_method('facebook.connect.registerUsers', + array('accounts' => $accounts)); + } + + /** + * Unregisters a set of accounts registered using connect.registerUsers. + * + * @param array $email_hashes The (JSON-encoded) list of email hashes to be + * unregistered. + * + * @return array The list of email hashes which have been successfully + * unregistered. + */ + public function connect_unregisterUsers($email_hashes) { + return $this->call_method('facebook.connect.unregisterUsers', + array('email_hashes' => $email_hashes)); + } + + /** + * Returns events according to the filters specified. + * + * @param int $uid (Optional) User associated with events. A null + * parameter will default to the session user. + * @param array/string $eids (Optional) Filter by these event + * ids. A null parameter will get all events for + * the user. (A csv list will work but is deprecated) + * @param int $start_time (Optional) Filter with this unix time as lower + * bound. A null or zero parameter indicates no + * lower bound. + * @param int $end_time (Optional) Filter with this UTC as upper bound. + * A null or zero parameter indicates no upper + * bound. + * @param string $rsvp_status (Optional) Only show events where the given uid + * has this rsvp status. This only works if you + * have specified a value for $uid. Values are as + * in events.getMembers. Null indicates to ignore + * rsvp status when filtering. + * + * @return array The events matching the query. + */ + public function &events_get($uid=null, + $eids=null, + $start_time=null, + $end_time=null, + $rsvp_status=null) { + return $this->call_method('facebook.events.get', + array('uid' => $uid, + 'eids' => $eids, + 'start_time' => $start_time, + 'end_time' => $end_time, + 'rsvp_status' => $rsvp_status)); + } + + /** + * Returns membership list data associated with an event. + * + * @param int $eid event id + * + * @return array An assoc array of four membership lists, with keys + * 'attending', 'unsure', 'declined', and 'not_replied' + */ + public function &events_getMembers($eid) { + return $this->call_method('facebook.events.getMembers', + array('eid' => $eid)); + } + + /** + * RSVPs the current user to this event. + * + * @param int $eid event id + * @param string $rsvp_status 'attending', 'unsure', or 'declined' + * + * @return bool true if successful + */ + public function &events_rsvp($eid, $rsvp_status) { + return $this->call_method('facebook.events.rsvp', + array( + 'eid' => $eid, + 'rsvp_status' => $rsvp_status)); + } + + /** + * Cancels an event. Only works for events where application is the admin. + * + * @param int $eid event id + * @param string $cancel_message (Optional) message to send to members of + * the event about why it is cancelled + * + * @return bool true if successful + */ + public function &events_cancel($eid, $cancel_message='') { + return $this->call_method('facebook.events.cancel', + array('eid' => $eid, + 'cancel_message' => $cancel_message)); + } + + /** + * Creates an event on behalf of the user is there is a session, otherwise on + * behalf of app. Successful creation guarantees app will be admin. + * + * @param assoc array $event_info json encoded event information + * @param string $file (Optional) filename of picture to set + * + * @return int event id + */ + public function events_create($event_info, $file = null) { + if ($file) { + return $this->call_upload_method('facebook.events.create', + array('event_info' => $event_info), + $file, + $this->photo_server_addr); + } else { + return $this->call_method('facebook.events.create', + array('event_info' => $event_info)); + } + } + + /** + * Invites users to an event. If a session user exists, the session user + * must have permissions to invite friends to the event and $uids must contain + * a list of friend ids. Otherwise, the event must have been + * created by the app and $uids must contain users of the app. + * This method requires the 'create_event' extended permission to + * invite people on behalf of a user. + * + * @param $eid the event id + * @param $uids an array of users to invite + * @param $personal_message a string containing the user's message + * (text only) + * + */ + public function events_invite($eid, $uids, $personal_message) { + return $this->call_method('facebook.events.invite', + array('eid' => $eid, + 'uids' => $uids, + 'personal_message' => $personal_message)); + } + + /** + * Edits an existing event. Only works for events where application is admin. + * + * @param int $eid event id + * @param assoc array $event_info json encoded event information + * @param string $file (Optional) filename of new picture to set + * + * @return bool true if successful + */ + public function events_edit($eid, $event_info, $file = null) { + if ($file) { + return $this->call_upload_method('facebook.events.edit', + array('eid' => $eid, 'event_info' => $event_info), + $file, + $this->photo_server_addr); + } else { + return $this->call_method('facebook.events.edit', + array('eid' => $eid, + 'event_info' => $event_info)); + } + } + + /** + * Fetches and re-caches the image stored at the given URL, for use in images + * published to non-canvas pages via the API (for example, to user profiles + * via profile.setFBML, or to News Feed via feed.publishUserAction). + * + * @param string $url The absolute URL from which to refresh the image. + * + * @return bool true on success + */ + public function &fbml_refreshImgSrc($url) { + return $this->call_method('facebook.fbml.refreshImgSrc', + array('url' => $url)); + } + + /** + * Fetches and re-caches the content stored at the given URL, for use in an + * fb:ref FBML tag. + * + * @param string $url The absolute URL from which to fetch content. This URL + * should be used in a fb:ref FBML tag. + * + * @return bool true on success + */ + public function &fbml_refreshRefUrl($url) { + return $this->call_method('facebook.fbml.refreshRefUrl', + array('url' => $url)); + } + + /** + * Associates a given "handle" with FBML markup so that the handle can be + * used within the fb:ref FBML tag. A handle is unique within an application + * and allows an application to publish identical FBML to many user profiles + * and do subsequent updates without having to republish FBML on behalf of + * each user. + * + * @param string $handle The handle to associate with the given FBML. + * @param string $fbml The FBML to associate with the given handle. + * + * @return bool true on success + */ + public function &fbml_setRefHandle($handle, $fbml) { + return $this->call_method('facebook.fbml.setRefHandle', + array('handle' => $handle, 'fbml' => $fbml)); + } + + /** + * Register custom tags for the application. Custom tags can be used + * to extend the set of tags available to applications in FBML + * markup. + * + * Before you call this function, + * make sure you read the full documentation at + * + * http://wiki.developers.facebook.com/index.php/Fbml.RegisterCustomTags + * + * IMPORTANT: This function overwrites the values of + * existing tags if the names match. Use this function with care because + * it may break the FBML of any application that is using the + * existing version of the tags. + * + * @param mixed $tags an array of tag objects (the full description is on the + * wiki page) + * + * @return int the number of tags that were registered + */ + public function &fbml_registerCustomTags($tags) { + $tags = json_encode($tags); + return $this->call_method('facebook.fbml.registerCustomTags', + array('tags' => $tags)); + } + + /** + * Get the custom tags for an application. If $app_id + * is not specified, the calling app's tags are returned. + * If $app_id is different from the id of the calling app, + * only the app's public tags are returned. + * The return value is an array of the same type as + * the $tags parameter of fbml_registerCustomTags(). + * + * @param int $app_id the application's id (optional) + * + * @return mixed an array containing the custom tag objects + */ + public function &fbml_getCustomTags($app_id = null) { + return $this->call_method('facebook.fbml.getCustomTags', + array('app_id' => $app_id)); + } + + + /** + * Delete custom tags the application has registered. If + * $tag_names is null, all the application's custom tags will be + * deleted. + * + * IMPORTANT: If your application has registered public tags + * that other applications may be using, don't delete those tags! + * Doing so can break the FBML ofapplications that are using them. + * + * @param array $tag_names the names of the tags to delete (optinal) + * @return bool true on success + */ + public function &fbml_deleteCustomTags($tag_names = null) { + return $this->call_method('facebook.fbml.deleteCustomTags', + array('tag_names' => json_encode($tag_names))); + } + + /** + * Gets the best translations for native strings submitted by an application + * for translation. If $locale is not specified, only native strings and their + * descriptions are returned. If $all is true, then unapproved translations + * are returned as well, otherwise only approved translations are returned. + * + * A mapping of locale codes -> language names is available at + * http://wiki.developers.facebook.com/index.php/Facebook_Locales + * + * @param string $locale the locale to get translations for, or 'all' for all + * locales, or 'en_US' for native strings + * @param bool $all whether to return all or only approved translations + * + * @return array (locale, array(native_strings, array('best translation + * available given enough votes or manual approval', approval + * status))) + * @error API_EC_PARAM + * @error API_EC_PARAM_BAD_LOCALE + */ + public function &intl_getTranslations($locale = 'en_US', $all = false) { + return $this->call_method('facebook.intl.getTranslations', + array('locale' => $locale, + 'all' => $all)); + } + + /** + * Lets you insert text strings in their native language into the Facebook + * Translations database so they can be translated. + * + * @param array $native_strings An array of maps, where each map has a 'text' + * field and a 'description' field. + * + * @return int Number of strings uploaded. + */ + public function &intl_uploadNativeStrings($native_strings) { + return $this->call_method('facebook.intl.uploadNativeStrings', + array('native_strings' => json_encode($native_strings))); + } + + /** + * This method is deprecated for calls made on behalf of users. This method + * works only for publishing stories on a Facebook Page that has installed + * your application. To publish stories to a user's profile, use + * feed.publishUserAction instead. + * + * For more details on this call, please visit the wiki page: + * + * http://wiki.developers.facebook.com/index.php/Feed.publishTemplatizedAction + */ + public function &feed_publishTemplatizedAction($title_template, + $title_data, + $body_template, + $body_data, + $body_general, + $image_1=null, + $image_1_link=null, + $image_2=null, + $image_2_link=null, + $image_3=null, + $image_3_link=null, + $image_4=null, + $image_4_link=null, + $target_ids='', + $page_actor_id=null) { + return $this->call_method('facebook.feed.publishTemplatizedAction', + array('title_template' => $title_template, + 'title_data' => $title_data, + 'body_template' => $body_template, + 'body_data' => $body_data, + 'body_general' => $body_general, + 'image_1' => $image_1, + 'image_1_link' => $image_1_link, + 'image_2' => $image_2, + 'image_2_link' => $image_2_link, + 'image_3' => $image_3, + 'image_3_link' => $image_3_link, + 'image_4' => $image_4, + 'image_4_link' => $image_4_link, + 'target_ids' => $target_ids, + 'page_actor_id' => $page_actor_id)); + } + + /** + * Registers a template bundle. Template bundles are somewhat involved, so + * it's recommended you check out the wiki for more details: + * + * http://wiki.developers.facebook.com/index.php/Feed.registerTemplateBundle + * + * @return string A template bundle id + */ + public function &feed_registerTemplateBundle($one_line_story_templates, + $short_story_templates = array(), + $full_story_template = null, + $action_links = array()) { + + $one_line_story_templates = json_encode($one_line_story_templates); + + if (!empty($short_story_templates)) { + $short_story_templates = json_encode($short_story_templates); + } + + if (isset($full_story_template)) { + $full_story_template = json_encode($full_story_template); + } + + if (isset($action_links)) { + $action_links = json_encode($action_links); + } + + return $this->call_method('facebook.feed.registerTemplateBundle', + array('one_line_story_templates' => $one_line_story_templates, + 'short_story_templates' => $short_story_templates, + 'full_story_template' => $full_story_template, + 'action_links' => $action_links)); + } + + /** + * Retrieves the full list of active template bundles registered by the + * requesting application. + * + * @return array An array of template bundles + */ + public function &feed_getRegisteredTemplateBundles() { + return $this->call_method('facebook.feed.getRegisteredTemplateBundles', + array()); + } + + /** + * Retrieves information about a specified template bundle previously + * registered by the requesting application. + * + * @param string $template_bundle_id The template bundle id + * + * @return array Template bundle + */ + public function &feed_getRegisteredTemplateBundleByID($template_bundle_id) { + return $this->call_method('facebook.feed.getRegisteredTemplateBundleByID', + array('template_bundle_id' => $template_bundle_id)); + } + + /** + * Deactivates a previously registered template bundle. + * + * @param string $template_bundle_id The template bundle id + * + * @return bool true on success + */ + public function &feed_deactivateTemplateBundleByID($template_bundle_id) { + return $this->call_method('facebook.feed.deactivateTemplateBundleByID', + array('template_bundle_id' => $template_bundle_id)); + } + + const STORY_SIZE_ONE_LINE = 1; + const STORY_SIZE_SHORT = 2; + const STORY_SIZE_FULL = 4; + + /** + * Publishes a story on behalf of the user owning the session, using the + * specified template bundle. This method requires an active session key in + * order to be called. + * + * The parameters to this method ($templata_data in particular) are somewhat + * involved. It's recommended you visit the wiki for details: + * + * http://wiki.developers.facebook.com/index.php/Feed.publishUserAction + * + * @param int $template_bundle_id A template bundle id previously registered + * @param array $template_data See wiki article for syntax + * @param array $target_ids (Optional) An array of friend uids of the + * user who shared in this action. + * @param string $body_general (Optional) Additional markup that extends + * the body of a short story. + * @param int $story_size (Optional) A story size (see above) + * @param string $user_message (Optional) A user message for a short + * story. + * + * @return bool true on success + */ + public function &feed_publishUserAction( + $template_bundle_id, $template_data, $target_ids='', $body_general='', + $story_size=FacebookRestClient::STORY_SIZE_ONE_LINE, + $user_message='') { + + if (is_array($template_data)) { + $template_data = json_encode($template_data); + } // allow client to either pass in JSON or an assoc that we JSON for them + + if (is_array($target_ids)) { + $target_ids = json_encode($target_ids); + $target_ids = trim($target_ids, "[]"); // we don't want square brackets + } + + return $this->call_method('facebook.feed.publishUserAction', + array('template_bundle_id' => $template_bundle_id, + 'template_data' => $template_data, + 'target_ids' => $target_ids, + 'body_general' => $body_general, + 'story_size' => $story_size, + 'user_message' => $user_message)); + } + + + /** + * Publish a post to the user's stream. + * + * @param $message the user's message + * @param $attachment the post's attachment (optional) + * @param $action links the post's action links (optional) + * @param $target_id the user on whose wall the post will be posted + * (optional) + * @param $uid the actor (defaults to session user) + * @return string the post id + */ + public function stream_publish( + $message, $attachment = null, $action_links = null, $target_id = null, + $uid = null) { + + return $this->call_method( + 'facebook.stream.publish', + array('message' => $message, + 'attachment' => $attachment, + 'action_links' => $action_links, + 'target_id' => $target_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Remove a post from the user's stream. + * Currently, you may only remove stories you application created. + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_remove($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.remove', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Add a comment to a stream post + * + * @param $post_id the post id + * @param $comment the comment text + * @param $uid the actor (defaults to session user) + * @return string the id of the created comment + */ + public function stream_addComment($post_id, $comment, $uid = null) { + return $this->call_method( + 'facebook.stream.addComment', + array('post_id' => $post_id, + 'comment' => $comment, + 'uid' => $this->get_uid($uid))); + } + + + /** + * Remove a comment from a stream post + * + * @param $comment_id the comment id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_removeComment($comment_id, $uid = null) { + return $this->call_method( + 'facebook.stream.removeComment', + array('comment_id' => $comment_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Add a like to a stream post + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_addLike($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.addLike', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Remove a like from a stream post + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_removeLike($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.removeLike', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * For the current user, retrieves stories generated by the user's friends + * while using this application. This can be used to easily create a + * "News Feed" like experience. + * + * @return array An array of feed story objects. + */ + public function &feed_getAppFriendStories() { + return $this->call_method('facebook.feed.getAppFriendStories'); + } + + /** + * Makes an FQL query. This is a generalized way of accessing all the data + * in the API, as an alternative to most of the other method calls. More + * info at http://wiki.developers.facebook.com/index.php/FQL + * + * @param string $query the query to evaluate + * + * @return array generalized array representing the results + */ + public function &fql_query($query) { + return $this->call_method('facebook.fql.query', + array('query' => $query)); + } + + /** + * Makes a set of FQL queries in parallel. This method takes a dictionary + * of FQL queries where the keys are names for the queries. Results from + * one query can be used within another query to fetch additional data. More + * info about FQL queries at http://wiki.developers.facebook.com/index.php/FQL + * + * @param string $queries JSON-encoded dictionary of queries to evaluate + * + * @return array generalized array representing the results + */ + public function &fql_multiquery($queries) { + return $this->call_method('facebook.fql.multiquery', + array('queries' => $queries)); + } + + /** + * Returns whether or not pairs of users are friends. + * Note that the Facebook friend relationship is symmetric. + * + * @param array/string $uids1 list of ids (id_1, id_2,...) + * of some length X (csv is deprecated) + * @param array/string $uids2 list of ids (id_A, id_B,...) + * of SAME length X (csv is deprecated) + * + * @return array An array with uid1, uid2, and bool if friends, e.g.: + * array(0 => array('uid1' => id_1, 'uid2' => id_A, 'are_friends' => 1), + * 1 => array('uid1' => id_2, 'uid2' => id_B, 'are_friends' => 0) + * ...) + * @error + * API_EC_PARAM_USER_ID_LIST + */ + public function &friends_areFriends($uids1, $uids2) { + return $this->call_method('facebook.friends.areFriends', + array('uids1' => $uids1, + 'uids2' => $uids2)); + } + + /** + * Returns the friends of the current session user. + * + * @param int $flid (Optional) Only return friends on this friend list. + * @param int $uid (Optional) Return friends for this user. + * + * @return array An array of friends + */ + public function &friends_get($flid=null, $uid = null) { + if (isset($this->friends_list)) { + return $this->friends_list; + } + $params = array(); + if (!$uid && isset($this->canvas_user)) { + $uid = $this->canvas_user; + } + if ($uid) { + $params['uid'] = $uid; + } + if ($flid) { + $params['flid'] = $flid; + } + return $this->call_method('facebook.friends.get', $params); + + } + + /** + * Returns the mutual friends between the target uid and a source uid or + * the current session user. + * + * @param int $target_uid Target uid for which mutual friends will be found. + * @param int $source_uid (optional) Source uid for which mutual friends will + * be found. If no source_uid is specified, + * source_id will default to the session + * user. + * @return array An array of friend uids + */ + public function &friends_getMutualFriends($target_uid, $source_uid = null) { + return $this->call_method('facebook.friends.getMutualFriends', + array("target_uid" => $target_uid, + "source_uid" => $source_uid)); + } + + /** + * Returns the set of friend lists for the current session user. + * + * @return array An array of friend list objects + */ + public function &friends_getLists() { + return $this->call_method('facebook.friends.getLists'); + } + + /** + * Returns the friends of the session user, who are also users + * of the calling application. + * + * @return array An array of friends also using the app + */ + public function &friends_getAppUsers() { + return $this->call_method('facebook.friends.getAppUsers'); + } + + /** + * Returns groups according to the filters specified. + * + * @param int $uid (Optional) User associated with groups. A null + * parameter will default to the session user. + * @param array/string $gids (Optional) Array of group ids to query. A null + * parameter will get all groups for the user. + * (csv is deprecated) + * + * @return array An array of group objects + */ + public function &groups_get($uid, $gids) { + return $this->call_method('facebook.groups.get', + array('uid' => $uid, + 'gids' => $gids)); + } + + /** + * Returns the membership list of a group. + * + * @param int $gid Group id + * + * @return array An array with four membership lists, with keys 'members', + * 'admins', 'officers', and 'not_replied' + */ + public function &groups_getMembers($gid) { + return $this->call_method('facebook.groups.getMembers', + array('gid' => $gid)); + } + + /** + * Returns cookies according to the filters specified. + * + * @param int $uid User for which the cookies are needed. + * @param string $name (Optional) A null parameter will get all cookies + * for the user. + * + * @return array Cookies! Nom nom nom nom nom. + */ + public function data_getCookies($uid, $name) { + return $this->call_method('facebook.data.getCookies', + array('uid' => $uid, + 'name' => $name)); + } + + /** + * Sets cookies according to the params specified. + * + * @param int $uid User for which the cookies are needed. + * @param string $name Name of the cookie + * @param string $value (Optional) if expires specified and is in the past + * @param int $expires (Optional) Expiry time + * @param string $path (Optional) Url path to associate with (default is /) + * + * @return bool true on success + */ + public function data_setCookie($uid, $name, $value, $expires, $path) { + return $this->call_method('facebook.data.setCookie', + array('uid' => $uid, + 'name' => $name, + 'value' => $value, + 'expires' => $expires, + 'path' => $path)); + } + + /** + * Retrieves links posted by the given user. + * + * @param int $uid The user whose links you wish to retrieve + * @param int $limit The maximimum number of links to retrieve + * @param array $link_ids (Optional) Array of specific link + * IDs to retrieve by this user + * + * @return array An array of links. + */ + public function &links_get($uid, $limit, $link_ids = null) { + return $this->call_method('links.get', + array('uid' => $uid, + 'limit' => $limit, + 'link_ids' => $link_ids)); + } + + /** + * Posts a link on Facebook. + * + * @param string $url URL/link you wish to post + * @param string $comment (Optional) A comment about this link + * @param int $uid (Optional) User ID that is posting this link; + * defaults to current session user + * + * @return bool + */ + public function &links_post($url, $comment='', $uid = null) { + return $this->call_method('links.post', + array('uid' => $uid, + 'url' => $url, + 'comment' => $comment)); + } + + /** + * Permissions API + */ + + /** + * Checks API-access granted by self to the specified application. + * + * @param string $permissions_apikey Other application key + * + * @return array API methods/namespaces which are allowed access + */ + public function permissions_checkGrantedApiAccess($permissions_apikey) { + return $this->call_method('facebook.permissions.checkGrantedApiAccess', + array('permissions_apikey' => $permissions_apikey)); + } + + /** + * Checks API-access granted to self by the specified application. + * + * @param string $permissions_apikey Other application key + * + * @return array API methods/namespaces which are allowed access + */ + public function permissions_checkAvailableApiAccess($permissions_apikey) { + return $this->call_method('facebook.permissions.checkAvailableApiAccess', + array('permissions_apikey' => $permissions_apikey)); + } + + /** + * Grant API-access to the specified methods/namespaces to the specified + * application. + * + * @param string $permissions_apikey Other application key + * @param array(string) $method_arr (Optional) API methods/namespaces + * allowed + * + * @return array API methods/namespaces which are allowed access + */ + public function permissions_grantApiAccess($permissions_apikey, $method_arr) { + return $this->call_method('facebook.permissions.grantApiAccess', + array('permissions_apikey' => $permissions_apikey, + 'method_arr' => $method_arr)); + } + + /** + * Revoke API-access granted to the specified application. + * + * @param string $permissions_apikey Other application key + * + * @return bool true on success + */ + public function permissions_revokeApiAccess($permissions_apikey) { + return $this->call_method('facebook.permissions.revokeApiAccess', + array('permissions_apikey' => $permissions_apikey)); + } + + /** + * Payments Order API + */ + + /** + * Set Payments properties for an app. + * + * @param properties a map from property names to values + * @return true on success + */ + public function payments_setProperties($properties) { + return $this->call_method ('facebook.payments.setProperties', + array('properties' => json_encode($properties))); + } + + public function payments_getOrderDetails($order_id) { + return json_decode($this->call_method( + 'facebook.payments.getOrderDetails', + array('order_id' => $order_id)), true); + } + + public function payments_updateOrder($order_id, $status, + $params) { + return $this->call_method('facebook.payments.updateOrder', + array('order_id' => $order_id, + 'status' => $status, + 'params' => json_encode($params))); + } + + public function payments_getOrders($status, $start_time, + $end_time, $test_mode=false) { + return json_decode($this->call_method('facebook.payments.getOrders', + array('status' => $status, + 'start_time' => $start_time, + 'end_time' => $end_time, + 'test_mode' => $test_mode)), true); + } + + /** + * Gifts API + */ + + /** + * Get Gifts associated with an app + * + * @return array of gifts + */ + public function gifts_get() { + return json_decode( + $this->call_method('facebook.gifts.get', + array()), + true + ); + } + + /* + * Update gifts stored by an app + * + * @param array containing gift_id => gift_data to be updated + * @return array containing gift_id => true/false indicating success + * in updating that gift + */ + public function gifts_update($update_array) { + return json_decode( + $this->call_method('facebook.gifts.update', + array('update_str' => json_encode($update_array)) + ), + true + ); + } + + + /** + * Creates a note with the specified title and content. + * + * @param string $title Title of the note. + * @param string $content Content of the note. + * @param int $uid (Optional) The user for whom you are creating a + * note; defaults to current session user + * + * @return int The ID of the note that was just created. + */ + public function ¬es_create($title, $content, $uid = null) { + return $this->call_method('notes.create', + array('uid' => $uid, + 'title' => $title, + 'content' => $content)); + } + + /** + * Deletes the specified note. + * + * @param int $note_id ID of the note you wish to delete + * @param int $uid (Optional) Owner of the note you wish to delete; + * defaults to current session user + * + * @return bool + */ + public function ¬es_delete($note_id, $uid = null) { + return $this->call_method('notes.delete', + array('uid' => $uid, + 'note_id' => $note_id)); + } + + /** + * Edits a note, replacing its title and contents with the title + * and contents specified. + * + * @param int $note_id ID of the note you wish to edit + * @param string $title Replacement title for the note + * @param string $content Replacement content for the note + * @param int $uid (Optional) Owner of the note you wish to edit; + * defaults to current session user + * + * @return bool + */ + public function ¬es_edit($note_id, $title, $content, $uid = null) { + return $this->call_method('notes.edit', + array('uid' => $uid, + 'note_id' => $note_id, + 'title' => $title, + 'content' => $content)); + } + + /** + * Retrieves all notes by a user. If note_ids are specified, + * retrieves only those specific notes by that user. + * + * @param int $uid User whose notes you wish to retrieve + * @param array $note_ids (Optional) List of specific note + * IDs by this user to retrieve + * + * @return array A list of all of the given user's notes, or an empty list + * if the viewer lacks permissions or if there are no visible + * notes. + */ + public function ¬es_get($uid, $note_ids = null) { + return $this->call_method('notes.get', + array('uid' => $uid, + 'note_ids' => $note_ids)); + } + + + /** + * Returns the outstanding notifications for the session user. + * + * @return array An assoc array of notification count objects for + * 'messages', 'pokes' and 'shares', a uid list of + * 'friend_requests', a gid list of 'group_invites', + * and an eid list of 'event_invites' + */ + public function ¬ifications_get() { + return $this->call_method('facebook.notifications.get'); + } + + /** + * Sends a notification to the specified users. + * + * @return A comma separated list of successful recipients + * @error + * API_EC_PARAM_USER_ID_LIST + */ + public function ¬ifications_send($to_ids, $notification, $type) { + return $this->call_method('facebook.notifications.send', + array('to_ids' => $to_ids, + 'notification' => $notification, + 'type' => $type)); + } + + /** + * Sends an email to the specified user of the application. + * + * @param array/string $recipients array of ids of the recipients (csv is deprecated) + * @param string $subject subject of the email + * @param string $text (plain text) body of the email + * @param string $fbml fbml markup for an html version of the email + * + * @return string A comma separated list of successful recipients + * @error + * API_EC_PARAM_USER_ID_LIST + */ + public function ¬ifications_sendEmail($recipients, + $subject, + $text, + $fbml) { + return $this->call_method('facebook.notifications.sendEmail', + array('recipients' => $recipients, + 'subject' => $subject, + 'text' => $text, + 'fbml' => $fbml)); + } + + /** + * Returns the requested info fields for the requested set of pages. + * + * @param array/string $page_ids an array of page ids (csv is deprecated) + * @param array/string $fields an array of strings describing the + * info fields desired (csv is deprecated) + * @param int $uid (Optional) limit results to pages of which this + * user is a fan. + * @param string type limits results to a particular type of page. + * + * @return array An array of pages + */ + public function &pages_getInfo($page_ids, $fields, $uid, $type) { + return $this->call_method('facebook.pages.getInfo', + array('page_ids' => $page_ids, + 'fields' => $fields, + 'uid' => $uid, + 'type' => $type)); + } + + /** + * Returns true if the given user is an admin for the passed page. + * + * @param int $page_id target page id + * @param int $uid (Optional) user id (defaults to the logged-in user) + * + * @return bool true on success + */ + public function &pages_isAdmin($page_id, $uid = null) { + return $this->call_method('facebook.pages.isAdmin', + array('page_id' => $page_id, + 'uid' => $uid)); + } + + /** + * Returns whether or not the given page has added the application. + * + * @param int $page_id target page id + * + * @return bool true on success + */ + public function &pages_isAppAdded($page_id) { + return $this->call_method('facebook.pages.isAppAdded', + array('page_id' => $page_id)); + } + + /** + * Returns true if logged in user is a fan for the passed page. + * + * @param int $page_id target page id + * @param int $uid user to compare. If empty, the logged in user. + * + * @return bool true on success + */ + public function &pages_isFan($page_id, $uid = null) { + return $this->call_method('facebook.pages.isFan', + array('page_id' => $page_id, + 'uid' => $uid)); + } + + /** + * Adds a tag with the given information to a photo. See the wiki for details: + * + * http://wiki.developers.facebook.com/index.php/Photos.addTag + * + * @param int $pid The ID of the photo to be tagged + * @param int $tag_uid The ID of the user being tagged. You must specify + * either the $tag_uid or the $tag_text parameter + * (unless $tags is specified). + * @param string $tag_text Some text identifying the person being tagged. + * You must specify either the $tag_uid or $tag_text + * parameter (unless $tags is specified). + * @param float $x The horizontal position of the tag, as a + * percentage from 0 to 100, from the left of the + * photo. + * @param float $y The vertical position of the tag, as a percentage + * from 0 to 100, from the top of the photo. + * @param array $tags (Optional) An array of maps, where each map + * can contain the tag_uid, tag_text, x, and y + * parameters defined above. If specified, the + * individual arguments are ignored. + * @param int $owner_uid (Optional) The user ID of the user whose photo + * you are tagging. If this parameter is not + * specified, then it defaults to the session user. + * + * @return bool true on success + */ + public function &photos_addTag($pid, + $tag_uid, + $tag_text, + $x, + $y, + $tags, + $owner_uid=0) { + return $this->call_method('facebook.photos.addTag', + array('pid' => $pid, + 'tag_uid' => $tag_uid, + 'tag_text' => $tag_text, + 'x' => $x, + 'y' => $y, + 'tags' => (is_array($tags)) ? json_encode($tags) : null, + 'owner_uid' => $this->get_uid($owner_uid))); + } + + /** + * Creates and returns a new album owned by the specified user or the current + * session user. + * + * @param string $name The name of the album. + * @param string $description (Optional) A description of the album. + * @param string $location (Optional) A description of the location. + * @param string $visible (Optional) A privacy setting for the album. + * One of 'friends', 'friends-of-friends', + * 'networks', or 'everyone'. Default 'everyone'. + * @param int $uid (Optional) User id for creating the album; if + * not specified, the session user is used. + * + * @return array An album object + */ + public function &photos_createAlbum($name, + $description='', + $location='', + $visible='', + $uid=0) { + return $this->call_method('facebook.photos.createAlbum', + array('name' => $name, + 'description' => $description, + 'location' => $location, + 'visible' => $visible, + 'uid' => $this->get_uid($uid))); + } + + /** + * Returns photos according to the filters specified. + * + * @param int $subj_id (Optional) Filter by uid of user tagged in the photos. + * @param int $aid (Optional) Filter by an album, as returned by + * photos_getAlbums. + * @param array/string $pids (Optional) Restrict to an array of pids + * (csv is deprecated) + * + * Note that at least one of these parameters needs to be specified, or an + * error is returned. + * + * @return array An array of photo objects. + */ + public function &photos_get($subj_id, $aid, $pids) { + return $this->call_method('facebook.photos.get', + array('subj_id' => $subj_id, 'aid' => $aid, 'pids' => $pids)); + } + + /** + * Returns the albums created by the given user. + * + * @param int $uid (Optional) The uid of the user whose albums you want. + * A null will return the albums of the session user. + * @param string $aids (Optional) An array of aids to restrict + * the query. (csv is deprecated) + * + * Note that at least one of the (uid, aids) parameters must be specified. + * + * @returns an array of album objects. + */ + public function &photos_getAlbums($uid, $aids) { + return $this->call_method('facebook.photos.getAlbums', + array('uid' => $uid, + 'aids' => $aids)); + } + + /** + * Returns the tags on all photos specified. + * + * @param string $pids A list of pids to query + * + * @return array An array of photo tag objects, which include pid, + * subject uid, and two floating-point numbers (xcoord, ycoord) + * for tag pixel location. + */ + public function &photos_getTags($pids) { + return $this->call_method('facebook.photos.getTags', + array('pids' => $pids)); + } + + /** + * Uploads a photo. + * + * @param string $file The location of the photo on the local filesystem. + * @param int $aid (Optional) The album into which to upload the + * photo. + * @param string $caption (Optional) A caption for the photo. + * @param int uid (Optional) The user ID of the user whose photo you + * are uploading + * + * @return array An array of user objects + */ + public function photos_upload($file, $aid=null, $caption=null, $uid=null) { + return $this->call_upload_method('facebook.photos.upload', + array('aid' => $aid, + 'caption' => $caption, + 'uid' => $uid), + $file); + } + + + /** + * Uploads a video. + * + * @param string $file The location of the video on the local filesystem. + * @param string $title (Optional) A title for the video. Titles over 65 characters in length will be truncated. + * @param string $description (Optional) A description for the video. + * + * @return array An array with the video's ID, title, description, and a link to view it on Facebook. + */ + public function video_upload($file, $title=null, $description=null) { + return $this->call_upload_method('facebook.video.upload', + array('title' => $title, + 'description' => $description), + $file, + Facebook::get_facebook_url('api-video') . '/restserver.php'); + } + + /** + * Returns an array with the video limitations imposed on the current session's + * associated user. Maximum length is measured in seconds; maximum size is + * measured in bytes. + * + * @return array Array with "length" and "size" keys + */ + public function &video_getUploadLimits() { + return $this->call_method('facebook.video.getUploadLimits'); + } + + /** + * Returns the requested info fields for the requested set of users. + * + * @param array/string $uids An array of user ids (csv is deprecated) + * @param array/string $fields An array of info field names desired (csv is deprecated) + * + * @return array An array of user objects + */ + public function &users_getInfo($uids, $fields) { + return $this->call_method('facebook.users.getInfo', + array('uids' => $uids, + 'fields' => $fields)); + } + + /** + * Returns the requested info fields for the requested set of users. A + * session key must not be specified. Only data about users that have + * authorized your application will be returned. + * + * Check the wiki for fields that can be queried through this API call. + * Data returned from here should not be used for rendering to application + * users, use users.getInfo instead, so that proper privacy rules will be + * applied. + * + * @param array/string $uids An array of user ids (csv is deprecated) + * @param array/string $fields An array of info field names desired (csv is deprecated) + * + * @return array An array of user objects + */ + public function &users_getStandardInfo($uids, $fields) { + return $this->call_method('facebook.users.getStandardInfo', + array('uids' => $uids, + 'fields' => $fields)); + } + + /** + * Returns the user corresponding to the current session object. + * + * @return integer User id + */ + public function &users_getLoggedInUser() { + return $this->call_method('facebook.users.getLoggedInUser'); + } + + /** + * Returns 1 if the user has the specified permission, 0 otherwise. + * http://wiki.developers.facebook.com/index.php/Users.hasAppPermission + * + * @return integer 1 or 0 + */ + public function &users_hasAppPermission($ext_perm, $uid=null) { + return $this->call_method('facebook.users.hasAppPermission', + array('ext_perm' => $ext_perm, 'uid' => $uid)); + } + + /** + * Returns whether or not the user corresponding to the current + * session object has the give the app basic authorization. + * + * @return boolean true if the user has authorized the app + */ + public function &users_isAppUser($uid=null) { + if ($uid === null && isset($this->is_user)) { + return $this->is_user; + } + + return $this->call_method('facebook.users.isAppUser', array('uid' => $uid)); + } + + /** + * Returns whether or not the user corresponding to the current + * session object is verified by Facebook. See the documentation + * for Users.isVerified for details. + * + * @return boolean true if the user is verified + */ + public function &users_isVerified() { + return $this->call_method('facebook.users.isVerified'); + } + + /** + * Sets the users' current status message. Message does NOT contain the + * word "is" , so make sure to include a verb. + * + * Example: setStatus("is loving the API!") + * will produce the status "Luke is loving the API!" + * + * @param string $status text-only message to set + * @param int $uid user to set for (defaults to the + * logged-in user) + * @param bool $clear whether or not to clear the status, + * instead of setting it + * @param bool $status_includes_verb if true, the word "is" will *not* be + * prepended to the status message + * + * @return boolean + */ + public function &users_setStatus($status, + $uid = null, + $clear = false, + $status_includes_verb = true) { + $args = array( + 'status' => $status, + 'uid' => $uid, + 'clear' => $clear, + 'status_includes_verb' => $status_includes_verb, + ); + return $this->call_method('facebook.users.setStatus', $args); + } + + /** + * Gets the comments for a particular xid. This is essentially a wrapper + * around the comment FQL table. + * + * @param string $xid external id associated with the comments + * + * @return array of comment objects + */ + public function &comments_get($xid) { + $args = array('xid' => $xid); + return $this->call_method('facebook.comments.get', $args); + } + + /** + * Add a comment to a particular xid on behalf of a user. If called + * without an app_secret (with session secret), this will only work + * for the session user. + * + * @param string $xid external id associated with the comments + * @param string $text text of the comment + * @param int $uid user adding the comment (def: session user) + * @param string $title optional title for the stream story + * @param string $url optional url for the stream story + * @param bool $publish_to_stream publish a feed story about this comment? + * a link will be generated to title/url in the story + * + * @return string comment_id associated with the comment + */ + public function &comments_add($xid, $text, $uid=0, $title='', $url='', + $publish_to_stream=false) { + $args = array( + 'xid' => $xid, + 'uid' => $this->get_uid($uid), + 'text' => $text, + 'title' => $title, + 'url' => $url, + 'publish_to_stream' => $publish_to_stream); + + return $this->call_method('facebook.comments.add', $args); + } + + /** + * Remove a particular comment. + * + * @param string $xid the external id associated with the comments + * @param string $comment_id id of the comment to remove (returned by + * comments.add and comments.get) + * + * @return boolean + */ + public function &comments_remove($xid, $comment_id) { + $args = array( + 'xid' => $xid, + 'comment_id' => $comment_id); + return $this->call_method('facebook.comments.remove', $args); + } + + /** + * Gets the stream on behalf of a user using a set of users. This + * call will return the latest $limit queries between $start_time + * and $end_time. + * + * @param int $viewer_id user making the call (def: session) + * @param array $source_ids users/pages to look at (def: all connections) + * @param int $start_time start time to look for stories (def: 1 day ago) + * @param int $end_time end time to look for stories (def: now) + * @param int $limit number of stories to attempt to fetch (def: 30) + * @param string $filter_key key returned by stream.getFilters to fetch + * @param array $metadata metadata to include with the return, allows + * requested metadata to be returned, such as + * profiles, albums, photo_tags + * + * @return array( + * 'posts' => array of posts, + * // if requested, the following data may be returned + * 'profiles' => array of profile metadata of users/pages in posts + * 'albums' => array of album metadata in posts + * 'photo_tags' => array of photo_tags for photos in posts + * ) + */ + public function &stream_get($viewer_id = null, + $source_ids = null, + $start_time = 0, + $end_time = 0, + $limit = 30, + $filter_key = '', + $exportable_only = false, + $metadata = null, + $post_ids = null) { + $args = array( + 'viewer_id' => $viewer_id, + 'source_ids' => $source_ids, + 'start_time' => $start_time, + 'end_time' => $end_time, + 'limit' => $limit, + 'filter_key' => $filter_key, + 'exportable_only' => $exportable_only, + 'metadata' => $metadata, + 'post_ids' => $post_ids); + return $this->call_method('facebook.stream.get', $args); + } + + /** + * Gets the filters (with relevant filter keys for stream.get) for a + * particular user. These filters are typical things like news feed, + * friend lists, networks. They can be used to filter the stream + * without complex queries to determine which ids belong in which groups. + * + * @param int $uid user to get filters for + * + * @return array of stream filter objects + */ + public function &stream_getFilters($uid = null) { + $args = array('uid' => $uid); + return $this->call_method('facebook.stream.getFilters', $args); + } + + /** + * Gets the full comments given a post_id from stream.get or the + * stream FQL table. Initially, only a set of preview comments are + * returned because some posts can have many comments. + * + * @param string $post_id id of the post to get comments for + * + * @return array of comment objects + */ + public function &stream_getComments($post_id) { + $args = array('post_id' => $post_id); + return $this->call_method('facebook.stream.getComments', $args); + } + + /** + * Sets the FBML for the profile of the user attached to this session. + * + * @param string $markup The FBML that describes the profile + * presence of this app for the user + * @param int $uid The user + * @param string $profile Profile FBML + * @param string $profile_action Profile action FBML (deprecated) + * @param string $mobile_profile Mobile profile FBML + * @param string $profile_main Main Tab profile FBML + * + * @return array A list of strings describing any compile errors for the + * submitted FBML + */ + public function profile_setFBML($markup, + $uid=null, + $profile='', + $profile_action='', + $mobile_profile='', + $profile_main='') { + return $this->call_method('facebook.profile.setFBML', + array('markup' => $markup, + 'uid' => $uid, + 'profile' => $profile, + 'profile_action' => $profile_action, + 'mobile_profile' => $mobile_profile, + 'profile_main' => $profile_main)); + } + + /** + * Gets the FBML for the profile box that is currently set for a user's + * profile (your application set the FBML previously by calling the + * profile.setFBML method). + * + * @param int $uid (Optional) User id to lookup; defaults to session. + * @param int $type (Optional) 1 for original style, 2 for profile_main boxes + * + * @return string The FBML + */ + public function &profile_getFBML($uid=null, $type=null) { + return $this->call_method('facebook.profile.getFBML', + array('uid' => $uid, + 'type' => $type)); + } + + /** + * Returns the specified user's application info section for the calling + * application. These info sections have either been set via a previous + * profile.setInfo call or by the user editing them directly. + * + * @param int $uid (Optional) User id to lookup; defaults to session. + * + * @return array Info fields for the current user. See wiki for structure: + * + * http://wiki.developers.facebook.com/index.php/Profile.getInfo + * + */ + public function &profile_getInfo($uid=null) { + return $this->call_method('facebook.profile.getInfo', + array('uid' => $uid)); + } + + /** + * Returns the options associated with the specified info field for an + * application info section. + * + * @param string $field The title of the field + * + * @return array An array of info options. + */ + public function &profile_getInfoOptions($field) { + return $this->call_method('facebook.profile.getInfoOptions', + array('field' => $field)); + } + + /** + * Configures an application info section that the specified user can install + * on the Info tab of her profile. For details on the structure of an info + * field, please see: + * + * http://wiki.developers.facebook.com/index.php/Profile.setInfo + * + * @param string $title Title / header of the info section + * @param int $type 1 for text-only, 5 for thumbnail views + * @param array $info_fields An array of info fields. See wiki for details. + * @param int $uid (Optional) + * + * @return bool true on success + */ + public function &profile_setInfo($title, $type, $info_fields, $uid=null) { + return $this->call_method('facebook.profile.setInfo', + array('uid' => $uid, + 'type' => $type, + 'title' => $title, + 'info_fields' => json_encode($info_fields))); + } + + /** + * Specifies the objects for a field for an application info section. These + * options populate the typeahead for a thumbnail. + * + * @param string $field The title of the field + * @param array $options An array of items for a thumbnail, including + * 'label', 'link', and optionally 'image', + * 'description' and 'sublabel' + * + * @return bool true on success + */ + public function profile_setInfoOptions($field, $options) { + return $this->call_method('facebook.profile.setInfoOptions', + array('field' => $field, + 'options' => json_encode($options))); + } + + ///////////////////////////////////////////////////////////////////////////// + // Data Store API + + /** + * Set a user preference. + * + * @param pref_id preference identifier (0-200) + * @param value preferece's value + * @param uid the user id (defaults to current session user) + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + * API_EC_PERMISSION_OTHER_USER + */ + public function &data_setUserPreference($pref_id, $value, $uid = null) { + return $this->call_method('facebook.data.setUserPreference', + array('pref_id' => $pref_id, + 'value' => $value, + 'uid' => $this->get_uid($uid))); + } + + /** + * Set a user's all preferences for this application. + * + * @param values preferece values in an associative arrays + * @param replace whether to replace all existing preferences or + * merge into them. + * @param uid the user id (defaults to current session user) + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + * API_EC_PERMISSION_OTHER_USER + */ + public function &data_setUserPreferences($values, + $replace = false, + $uid = null) { + return $this->call_method('facebook.data.setUserPreferences', + array('values' => json_encode($values), + 'replace' => $replace, + 'uid' => $this->get_uid($uid))); + } + + /** + * Get a user preference. + * + * @param pref_id preference identifier (0-200) + * @param uid the user id (defaults to current session user) + * @return preference's value + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + * API_EC_PERMISSION_OTHER_USER + */ + public function &data_getUserPreference($pref_id, $uid = null) { + return $this->call_method('facebook.data.getUserPreference', + array('pref_id' => $pref_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Get a user preference. + * + * @param uid the user id (defaults to current session user) + * @return preference values + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + * API_EC_PERMISSION_OTHER_USER + */ + public function &data_getUserPreferences($uid = null) { + return $this->call_method('facebook.data.getUserPreferences', + array('uid' => $this->get_uid($uid))); + } + + /** + * Create a new object type. + * + * @param name object type's name + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_createObjectType($name) { + return $this->call_method('facebook.data.createObjectType', + array('name' => $name)); + } + + /** + * Delete an object type. + * + * @param obj_type object type's name + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_dropObjectType($obj_type) { + return $this->call_method('facebook.data.dropObjectType', + array('obj_type' => $obj_type)); + } + + /** + * Rename an object type. + * + * @param obj_type object type's name + * @param new_name new object type's name + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_renameObjectType($obj_type, $new_name) { + return $this->call_method('facebook.data.renameObjectType', + array('obj_type' => $obj_type, + 'new_name' => $new_name)); + } + + /** + * Add a new property to an object type. + * + * @param obj_type object type's name + * @param prop_name name of the property to add + * @param prop_type 1: integer; 2: string; 3: text blob + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_defineObjectProperty($obj_type, + $prop_name, + $prop_type) { + return $this->call_method('facebook.data.defineObjectProperty', + array('obj_type' => $obj_type, + 'prop_name' => $prop_name, + 'prop_type' => $prop_type)); + } + + /** + * Remove a previously defined property from an object type. + * + * @param obj_type object type's name + * @param prop_name name of the property to remove + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_undefineObjectProperty($obj_type, $prop_name) { + return $this->call_method('facebook.data.undefineObjectProperty', + array('obj_type' => $obj_type, + 'prop_name' => $prop_name)); + } + + /** + * Rename a previously defined property of an object type. + * + * @param obj_type object type's name + * @param prop_name name of the property to rename + * @param new_name new name to use + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_renameObjectProperty($obj_type, $prop_name, + $new_name) { + return $this->call_method('facebook.data.renameObjectProperty', + array('obj_type' => $obj_type, + 'prop_name' => $prop_name, + 'new_name' => $new_name)); + } + + /** + * Retrieve a list of all object types that have defined for the application. + * + * @return a list of object type names + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getObjectTypes() { + return $this->call_method('facebook.data.getObjectTypes'); + } + + /** + * Get definitions of all properties of an object type. + * + * @param obj_type object type's name + * @return pairs of property name and property types + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getObjectType($obj_type) { + return $this->call_method('facebook.data.getObjectType', + array('obj_type' => $obj_type)); + } + + /** + * Create a new object. + * + * @param obj_type object type's name + * @param properties (optional) properties to set initially + * @return newly created object's id + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_createObject($obj_type, $properties = null) { + return $this->call_method('facebook.data.createObject', + array('obj_type' => $obj_type, + 'properties' => json_encode($properties))); + } + + /** + * Update an existing object. + * + * @param obj_id object's id + * @param properties new properties + * @param replace true for replacing existing properties; + * false for merging + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_updateObject($obj_id, $properties, $replace = false) { + return $this->call_method('facebook.data.updateObject', + array('obj_id' => $obj_id, + 'properties' => json_encode($properties), + 'replace' => $replace)); + } + + /** + * Delete an existing object. + * + * @param obj_id object's id + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_deleteObject($obj_id) { + return $this->call_method('facebook.data.deleteObject', + array('obj_id' => $obj_id)); + } + + /** + * Delete a list of objects. + * + * @param obj_ids objects to delete + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_deleteObjects($obj_ids) { + return $this->call_method('facebook.data.deleteObjects', + array('obj_ids' => json_encode($obj_ids))); + } + + /** + * Get a single property value of an object. + * + * @param obj_id object's id + * @param prop_name individual property's name + * @return individual property's value + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getObjectProperty($obj_id, $prop_name) { + return $this->call_method('facebook.data.getObjectProperty', + array('obj_id' => $obj_id, + 'prop_name' => $prop_name)); + } + + /** + * Get properties of an object. + * + * @param obj_id object's id + * @param prop_names (optional) properties to return; null for all. + * @return specified properties of an object + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getObject($obj_id, $prop_names = null) { + return $this->call_method('facebook.data.getObject', + array('obj_id' => $obj_id, + 'prop_names' => json_encode($prop_names))); + } + + /** + * Get properties of a list of objects. + * + * @param obj_ids object ids + * @param prop_names (optional) properties to return; null for all. + * @return specified properties of an object + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getObjects($obj_ids, $prop_names = null) { + return $this->call_method('facebook.data.getObjects', + array('obj_ids' => json_encode($obj_ids), + 'prop_names' => json_encode($prop_names))); + } + + /** + * Set a single property value of an object. + * + * @param obj_id object's id + * @param prop_name individual property's name + * @param prop_value new value to set + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_setObjectProperty($obj_id, $prop_name, + $prop_value) { + return $this->call_method('facebook.data.setObjectProperty', + array('obj_id' => $obj_id, + 'prop_name' => $prop_name, + 'prop_value' => $prop_value)); + } + + /** + * Read hash value by key. + * + * @param obj_type object type's name + * @param key hash key + * @param prop_name (optional) individual property's name + * @return hash value + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getHashValue($obj_type, $key, $prop_name = null) { + return $this->call_method('facebook.data.getHashValue', + array('obj_type' => $obj_type, + 'key' => $key, + 'prop_name' => $prop_name)); + } + + /** + * Write hash value by key. + * + * @param obj_type object type's name + * @param key hash key + * @param value hash value + * @param prop_name (optional) individual property's name + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_setHashValue($obj_type, + $key, + $value, + $prop_name = null) { + return $this->call_method('facebook.data.setHashValue', + array('obj_type' => $obj_type, + 'key' => $key, + 'value' => $value, + 'prop_name' => $prop_name)); + } + + /** + * Increase a hash value by specified increment atomically. + * + * @param obj_type object type's name + * @param key hash key + * @param prop_name individual property's name + * @param increment (optional) default is 1 + * @return incremented hash value + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_incHashValue($obj_type, + $key, + $prop_name, + $increment = 1) { + return $this->call_method('facebook.data.incHashValue', + array('obj_type' => $obj_type, + 'key' => $key, + 'prop_name' => $prop_name, + 'increment' => $increment)); + } + + /** + * Remove a hash key and its values. + * + * @param obj_type object type's name + * @param key hash key + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_removeHashKey($obj_type, $key) { + return $this->call_method('facebook.data.removeHashKey', + array('obj_type' => $obj_type, + 'key' => $key)); + } + + /** + * Remove hash keys and their values. + * + * @param obj_type object type's name + * @param keys hash keys + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_removeHashKeys($obj_type, $keys) { + return $this->call_method('facebook.data.removeHashKeys', + array('obj_type' => $obj_type, + 'keys' => json_encode($keys))); + } + + /** + * Define an object association. + * + * @param name name of this association + * @param assoc_type 1: one-way 2: two-way symmetric 3: two-way asymmetric + * @param assoc_info1 needed info about first object type + * @param assoc_info2 needed info about second object type + * @param inverse (optional) name of reverse association + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_defineAssociation($name, $assoc_type, $assoc_info1, + $assoc_info2, $inverse = null) { + return $this->call_method('facebook.data.defineAssociation', + array('name' => $name, + 'assoc_type' => $assoc_type, + 'assoc_info1' => json_encode($assoc_info1), + 'assoc_info2' => json_encode($assoc_info2), + 'inverse' => $inverse)); + } + + /** + * Undefine an object association. + * + * @param name name of this association + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_undefineAssociation($name) { + return $this->call_method('facebook.data.undefineAssociation', + array('name' => $name)); + } + + /** + * Rename an object association or aliases. + * + * @param name name of this association + * @param new_name (optional) new name of this association + * @param new_alias1 (optional) new alias for object type 1 + * @param new_alias2 (optional) new alias for object type 2 + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_ALREADY_EXISTS + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_renameAssociation($name, $new_name, $new_alias1 = null, + $new_alias2 = null) { + return $this->call_method('facebook.data.renameAssociation', + array('name' => $name, + 'new_name' => $new_name, + 'new_alias1' => $new_alias1, + 'new_alias2' => $new_alias2)); + } + + /** + * Get definition of an object association. + * + * @param name name of this association + * @return specified association + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociationDefinition($name) { + return $this->call_method('facebook.data.getAssociationDefinition', + array('name' => $name)); + } + + /** + * Get definition of all associations. + * + * @return all defined associations + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociationDefinitions() { + return $this->call_method('facebook.data.getAssociationDefinitions', + array()); + } + + /** + * Create or modify an association between two objects. + * + * @param name name of association + * @param obj_id1 id of first object + * @param obj_id2 id of second object + * @param data (optional) extra string data to store + * @param assoc_time (optional) extra time data; default to creation time + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_setAssociation($name, $obj_id1, $obj_id2, $data = null, + $assoc_time = null) { + return $this->call_method('facebook.data.setAssociation', + array('name' => $name, + 'obj_id1' => $obj_id1, + 'obj_id2' => $obj_id2, + 'data' => $data, + 'assoc_time' => $assoc_time)); + } + + /** + * Create or modify associations between objects. + * + * @param assocs associations to set + * @param name (optional) name of association + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_setAssociations($assocs, $name = null) { + return $this->call_method('facebook.data.setAssociations', + array('assocs' => json_encode($assocs), + 'name' => $name)); + } + + /** + * Remove an association between two objects. + * + * @param name name of association + * @param obj_id1 id of first object + * @param obj_id2 id of second object + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_removeAssociation($name, $obj_id1, $obj_id2) { + return $this->call_method('facebook.data.removeAssociation', + array('name' => $name, + 'obj_id1' => $obj_id1, + 'obj_id2' => $obj_id2)); + } + + /** + * Remove associations between objects by specifying pairs of object ids. + * + * @param assocs associations to remove + * @param name (optional) name of association + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_removeAssociations($assocs, $name = null) { + return $this->call_method('facebook.data.removeAssociations', + array('assocs' => json_encode($assocs), + 'name' => $name)); + } + + /** + * Remove associations between objects by specifying one object id. + * + * @param name name of association + * @param obj_id who's association to remove + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_removeAssociatedObjects($name, $obj_id) { + return $this->call_method('facebook.data.removeAssociatedObjects', + array('name' => $name, + 'obj_id' => $obj_id)); + } + + /** + * Retrieve a list of associated objects. + * + * @param name name of association + * @param obj_id who's association to retrieve + * @param no_data only return object ids + * @return associated objects + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociatedObjects($name, $obj_id, $no_data = true) { + return $this->call_method('facebook.data.getAssociatedObjects', + array('name' => $name, + 'obj_id' => $obj_id, + 'no_data' => $no_data)); + } + + /** + * Count associated objects. + * + * @param name name of association + * @param obj_id who's association to retrieve + * @return associated object's count + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociatedObjectCount($name, $obj_id) { + return $this->call_method('facebook.data.getAssociatedObjectCount', + array('name' => $name, + 'obj_id' => $obj_id)); + } + + /** + * Get a list of associated object counts. + * + * @param name name of association + * @param obj_ids whose association to retrieve + * @return associated object counts + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_DATA_OBJECT_NOT_FOUND + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_INVALID_OPERATION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociatedObjectCounts($name, $obj_ids) { + return $this->call_method('facebook.data.getAssociatedObjectCounts', + array('name' => $name, + 'obj_ids' => json_encode($obj_ids))); + } + + /** + * Find all associations between two objects. + * + * @param obj_id1 id of first object + * @param obj_id2 id of second object + * @param no_data only return association names without data + * @return all associations between objects + * @error + * API_EC_DATA_DATABASE_ERROR + * API_EC_PARAM + * API_EC_PERMISSION + * API_EC_DATA_QUOTA_EXCEEDED + * API_EC_DATA_UNKNOWN_ERROR + */ + public function &data_getAssociations($obj_id1, $obj_id2, $no_data = true) { + return $this->call_method('facebook.data.getAssociations', + array('obj_id1' => $obj_id1, + 'obj_id2' => $obj_id2, + 'no_data' => $no_data)); + } + + /** + * Get the properties that you have set for an app. + * + * @param properties List of properties names to fetch + * + * @return array A map from property name to value + */ + public function admin_getAppProperties($properties) { + return json_decode( + $this->call_method('facebook.admin.getAppProperties', + array('properties' => json_encode($properties))), true); + } + + /** + * Set properties for an app. + * + * @param properties A map from property names to values + * + * @return bool true on success + */ + public function admin_setAppProperties($properties) { + return $this->call_method('facebook.admin.setAppProperties', + array('properties' => json_encode($properties))); + } + + /** + * Sets href and text for a Live Stream Box xid's via link + * + * @param string $xid xid of the Live Stream + * @param string $via_href Href for the via link + * @param string $via_text Text for the via link + * + * @return boolWhether the set was successful + */ + public function admin_setLiveStreamViaLink($xid, $via_href, $via_text) { + return $this->call_method('facebook.admin.setLiveStreamViaLink', + array('xid' => $xid, + 'via_href' => $via_href, + 'via_text' => $via_text)); + } + + /** + * Gets href and text for a Live Stream Box xid's via link + * + * @param string $xid xid of the Live Stream + * + * @return Array Associative array with keys 'via_href' and 'via_text' + * False if there was an error. + */ + public function admin_getLiveStreamViaLink($xid) { + return $this->call_method('facebook.admin.getLiveStreamViaLink', + array('xid' => $xid)); + } + + /** + * Returns the allocation limit value for a specified integration point name + * Integration point names are defined in lib/api/karma/constants.php in the + * limit_map. + * + * @param string $integration_point_name Name of an integration point + * (see developer wiki for list). + * @param int $uid Specific user to check the limit. + * + * @return int Integration point allocation value + */ + public function &admin_getAllocation($integration_point_name, $uid=null) { + return $this->call_method('facebook.admin.getAllocation', + array('integration_point_name' => $integration_point_name, + 'uid' => $uid)); + } + + /** + * Returns values for the specified metrics for the current application, in + * the given time range. The metrics are collected for fixed-length periods, + * and the times represent midnight at the end of each period. + * + * @param start_time unix time for the start of the range + * @param end_time unix time for the end of the range + * @param period number of seconds in the desired period + * @param metrics list of metrics to look up + * + * @return array A map of the names and values for those metrics + */ + public function &admin_getMetrics($start_time, $end_time, $period, $metrics) { + return $this->call_method('admin.getMetrics', + array('start_time' => $start_time, + 'end_time' => $end_time, + 'period' => $period, + 'metrics' => json_encode($metrics))); + } + + /** + * Sets application restriction info. + * + * Applications can restrict themselves to only a limited user demographic + * based on users' age and/or location or based on static predefined types + * specified by facebook for specifying diff age restriction for diff + * locations. + * + * @param array $restriction_info The age restriction settings to set. + * + * @return bool true on success + */ + public function admin_setRestrictionInfo($restriction_info = null) { + $restriction_str = null; + if (!empty($restriction_info)) { + $restriction_str = json_encode($restriction_info); + } + return $this->call_method('admin.setRestrictionInfo', + array('restriction_str' => $restriction_str)); + } + + /** + * Gets application restriction info. + * + * Applications can restrict themselves to only a limited user demographic + * based on users' age and/or location or based on static predefined types + * specified by facebook for specifying diff age restriction for diff + * locations. + * + * @return array The age restriction settings for this application. + */ + public function admin_getRestrictionInfo() { + return json_decode( + $this->call_method('admin.getRestrictionInfo'), + true); + } + + + /** + * Bans a list of users from the app. Banned users can't + * access the app's canvas page and forums. + * + * @param array $uids an array of user ids + * @return bool true on success + */ + public function admin_banUsers($uids) { + return $this->call_method( + 'admin.banUsers', array('uids' => json_encode($uids))); + } + + /** + * Unban users that have been previously banned with + * admin_banUsers(). + * + * @param array $uids an array of user ids + * @return bool true on success + */ + public function admin_unbanUsers($uids) { + return $this->call_method( + 'admin.unbanUsers', array('uids' => json_encode($uids))); + } + + /** + * Gets the list of users that have been banned from the application. + * $uids is an optional parameter that filters the result with the list + * of provided user ids. If $uids is provided, + * only banned user ids that are contained in $uids are returned. + * + * @param array $uids an array of user ids to filter by + * @return bool true on success + */ + + public function admin_getBannedUsers($uids = null) { + return $this->call_method( + 'admin.getBannedUsers', + array('uids' => $uids ? json_encode($uids) : null)); + } + + + /* UTILITY FUNCTIONS */ + + /** + * Calls the specified normal POST method with the specified parameters. + * + * @param string $method Name of the Facebook method to invoke + * @param array $params A map of param names => param values + * + * @return mixed Result of method call; this returns a reference to support + * 'delayed returns' when in a batch context. + * See: http://wiki.developers.facebook.com/index.php/Using_batching_API + */ + public function &call_method($method, $params = array()) { + if ($this->format) { + $params['format'] = $this->format; + } + if (!$this->pending_batch()) { + if ($this->call_as_apikey) { + $params['call_as_apikey'] = $this->call_as_apikey; + } + $data = $this->post_request($method, $params); + $this->rawData = $data; + $result = $this->convert_result($data, $method, $params); + if (is_array($result) && isset($result['error_code'])) { + throw new FacebookRestClientException($result['error_msg'], + $result['error_code']); + } + } + else { + $result = null; + $batch_item = array('m' => $method, 'p' => $params, 'r' => & $result); + $this->batch_queue[] = $batch_item; + } + + return $result; + } + + protected function convert_result($data, $method, $params) { + $is_xml = (empty($params['format']) || + strtolower($params['format']) != 'json'); + return ($is_xml) ? $this->convert_xml_to_result($data, $method, $params) + : json_decode($data, true); + } + + /** + * Change the response format + * + * @param string $format The response format (json, xml) + */ + public function setFormat($format) { + $this->format = $format; + return $this; + } + + /** + * get the current response serialization format + * + * @return string 'xml', 'json', or null (which means 'xml') + */ + public function getFormat() { + return $this->format; + } + + /** + * Returns the raw JSON or XML output returned by the server in the most + * recent API call. + * + * @return string + */ + public function getRawData() { + return $this->rawData; + } + + /** + * Calls the specified file-upload POST method with the specified parameters + * + * @param string $method Name of the Facebook method to invoke + * @param array $params A map of param names => param values + * @param string $file A path to the file to upload (required) + * + * @return array A dictionary representing the response. + */ + public function call_upload_method($method, $params, $file, $server_addr = null) { + if (!$this->pending_batch()) { + if (!file_exists($file)) { + $code = + FacebookAPIErrorCodes::API_EC_PARAM; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + if ($this->format) { + $params['format'] = $this->format; + } + $data = $this->post_upload_request($method, + $params, + $file, + $server_addr); + $result = $this->convert_result($data, $method, $params); + + if (is_array($result) && isset($result['error_code'])) { + throw new FacebookRestClientException($result['error_msg'], + $result['error_code']); + } + } + else { + $code = + FacebookAPIErrorCodes::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + return $result; + } + + protected function convert_xml_to_result($xml, $method, $params) { + $sxml = simplexml_load_string($xml); + $result = self::convert_simplexml_to_array($sxml); + + if (!empty($GLOBALS['facebook_config']['debug'])) { + // output the raw xml and its corresponding php object, for debugging: + print '
'; + $this->cur_id++; + print $this->cur_id . ': Called ' . $method . ', show ' . + 'Params | '. + 'XML | '. + 'SXML | '. + 'PHP'; + print ''; + print ''; + print ''; + print ''; + print '
'; + } + return $result; + } + + protected function finalize_params($method, $params) { + list($get, $post) = $this->add_standard_params($method, $params); + // we need to do this before signing the params + $this->convert_array_values_to_json($post); + $post['sig'] = Facebook::generate_sig(array_merge($get, $post), + $this->secret); + return array($get, $post); + } + + private function convert_array_values_to_json(&$params) { + foreach ($params as $key => &$val) { + if (is_array($val)) { + $val = json_encode($val); + } + } + } + + /** + * Add the generally required params to our request. + * Params method, api_key, and v should be sent over as get. + */ + private function add_standard_params($method, $params) { + $post = $params; + $get = array(); + if ($this->call_as_apikey) { + $get['call_as_apikey'] = $this->call_as_apikey; + } + if ($this->using_session_secret) { + $get['ss'] = '1'; + } + + $get['method'] = $method; + $get['session_key'] = $this->session_key; + $get['api_key'] = $this->api_key; + $post['call_id'] = microtime(true); + if ($post['call_id'] <= $this->last_call_id) { + $post['call_id'] = $this->last_call_id + 0.001; + } + $this->last_call_id = $post['call_id']; + if (isset($post['v'])) { + $get['v'] = $post['v']; + unset($post['v']); + } else { + $get['v'] = '1.0'; + } + if (isset($this->use_ssl_resources)) { + $post['return_ssl_resources'] = (bool) $this->use_ssl_resources; + } + return array($get, $post); + } + + private function create_url_string($params) { + $post_params = array(); + foreach ($params as $key => &$val) { + $post_params[] = $key.'='.urlencode($val); + } + return implode('&', $post_params); + } + + private function run_multipart_http_transaction($method, $params, $file, $server_addr) { + + // the format of this message is specified in RFC1867/RFC1341. + // we add twenty pseudo-random digits to the end of the boundary string. + $boundary = '--------------------------FbMuLtIpArT' . + sprintf("%010d", mt_rand()) . + sprintf("%010d", mt_rand()); + $content_type = 'multipart/form-data; boundary=' . $boundary; + // within the message, we prepend two extra hyphens. + $delimiter = '--' . $boundary; + $close_delimiter = $delimiter . '--'; + $content_lines = array(); + foreach ($params as $key => &$val) { + $content_lines[] = $delimiter; + $content_lines[] = 'Content-Disposition: form-data; name="' . $key . '"'; + $content_lines[] = ''; + $content_lines[] = $val; + } + // now add the file data + $content_lines[] = $delimiter; + $content_lines[] = + 'Content-Disposition: form-data; filename="' . $file . '"'; + $content_lines[] = 'Content-Type: application/octet-stream'; + $content_lines[] = ''; + $content_lines[] = file_get_contents($file); + $content_lines[] = $close_delimiter; + $content_lines[] = ''; + $content = implode("\r\n", $content_lines); + return $this->run_http_post_transaction($content_type, $content, $server_addr); + } + + public function post_request($method, $params) { + list($get, $post) = $this->finalize_params($method, $params); + $post_string = $this->create_url_string($post); + $get_string = $this->create_url_string($get); + $url_with_get = $this->server_addr . '?' . $get_string; + if ($this->use_curl_if_available && function_exists('curl_init')) { + $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url_with_get); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, $useragent); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + $result = $this->curl_exec($ch); + curl_close($ch); + } else { + $content_type = 'application/x-www-form-urlencoded'; + $content = $post_string; + $result = $this->run_http_post_transaction($content_type, + $content, + $url_with_get); + } + return $result; + } + + /** + * execute a curl transaction -- this exists mostly so subclasses can add + * extra options and/or process the response, if they wish. + * + * @param resource $ch a curl handle + */ + protected function curl_exec($ch) { + $result = curl_exec($ch); + return $result; + } + + protected function post_upload_request($method, $params, $file, $server_addr = null) { + $server_addr = $server_addr ? $server_addr : $this->server_addr; + list($get, $post) = $this->finalize_params($method, $params); + $get_string = $this->create_url_string($get); + $url_with_get = $server_addr . '?' . $get_string; + if ($this->use_curl_if_available && function_exists('curl_init')) { + // prepending '@' causes cURL to upload the file; the key is ignored. + $post['_file'] = '@' . $file; + $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url_with_get); + // this has to come before the POSTFIELDS set! + curl_setopt($ch, CURLOPT_POST, 1); + // passing an array gets curl to use the multipart/form-data content type + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, $useragent); + $result = $this->curl_exec($ch); + curl_close($ch); + } else { + $result = $this->run_multipart_http_transaction($method, $post, + $file, $url_with_get); + } + return $result; + } + + private function run_http_post_transaction($content_type, $content, $server_addr) { + + $user_agent = 'Facebook API PHP5 Client 1.1 (non-curl) ' . phpversion(); + $content_length = strlen($content); + $context = + array('http' => + array('method' => 'POST', + 'user_agent' => $user_agent, + 'header' => 'Content-Type: ' . $content_type . "\r\n" . + 'Content-Length: ' . $content_length, + 'content' => $content)); + $context_id = stream_context_create($context); + $sock = fopen($server_addr, 'r', false, $context_id); + + $result = ''; + if ($sock) { + while (!feof($sock)) { + $result .= fgets($sock, 4096); + } + fclose($sock); + } + return $result; + } + + public static function convert_simplexml_to_array($sxml) { + $arr = array(); + if ($sxml) { + foreach ($sxml as $k => $v) { + if ($sxml['list']) { + $arr[] = self::convert_simplexml_to_array($v); + } else { + $arr[$k] = self::convert_simplexml_to_array($v); + } + } + } + if (sizeof($arr) > 0) { + return $arr; + } else { + return (string)$sxml; + } + } + + protected function get_uid($uid) { + return $uid ? $uid : $this->user; + } +} + + +class FacebookRestClientException extends Exception { +} + +// Supporting methods and values------ + +/** + * Error codes and descriptions for the Facebook API. + */ + +class FacebookAPIErrorCodes { + + const API_EC_SUCCESS = 0; + + /* + * GENERAL ERRORS + */ + const API_EC_UNKNOWN = 1; + const API_EC_SERVICE = 2; + const API_EC_METHOD = 3; + const API_EC_TOO_MANY_CALLS = 4; + const API_EC_BAD_IP = 5; + const API_EC_HOST_API = 6; + const API_EC_HOST_UP = 7; + const API_EC_SECURE = 8; + const API_EC_RATE = 9; + const API_EC_PERMISSION_DENIED = 10; + const API_EC_DEPRECATED = 11; + const API_EC_VERSION = 12; + const API_EC_INTERNAL_FQL_ERROR = 13; + const API_EC_HOST_PUP = 14; + const API_EC_SESSION_SECRET_NOT_ALLOWED = 15; + const API_EC_HOST_READONLY = 16; + + /* + * PARAMETER ERRORS + */ + const API_EC_PARAM = 100; + const API_EC_PARAM_API_KEY = 101; + const API_EC_PARAM_SESSION_KEY = 102; + const API_EC_PARAM_CALL_ID = 103; + const API_EC_PARAM_SIGNATURE = 104; + const API_EC_PARAM_TOO_MANY = 105; + const API_EC_PARAM_USER_ID = 110; + const API_EC_PARAM_USER_FIELD = 111; + const API_EC_PARAM_SOCIAL_FIELD = 112; + const API_EC_PARAM_EMAIL = 113; + const API_EC_PARAM_USER_ID_LIST = 114; + const API_EC_PARAM_FIELD_LIST = 115; + const API_EC_PARAM_ALBUM_ID = 120; + const API_EC_PARAM_PHOTO_ID = 121; + const API_EC_PARAM_FEED_PRIORITY = 130; + const API_EC_PARAM_CATEGORY = 140; + const API_EC_PARAM_SUBCATEGORY = 141; + const API_EC_PARAM_TITLE = 142; + const API_EC_PARAM_DESCRIPTION = 143; + const API_EC_PARAM_BAD_JSON = 144; + const API_EC_PARAM_BAD_EID = 150; + const API_EC_PARAM_UNKNOWN_CITY = 151; + const API_EC_PARAM_BAD_PAGE_TYPE = 152; + const API_EC_PARAM_BAD_LOCALE = 170; + const API_EC_PARAM_BLOCKED_NOTIFICATION = 180; + + /* + * USER PERMISSIONS ERRORS + */ + const API_EC_PERMISSION = 200; + const API_EC_PERMISSION_USER = 210; + const API_EC_PERMISSION_NO_DEVELOPERS = 211; + const API_EC_PERMISSION_OFFLINE_ACCESS = 212; + const API_EC_PERMISSION_ALBUM = 220; + const API_EC_PERMISSION_PHOTO = 221; + const API_EC_PERMISSION_MESSAGE = 230; + const API_EC_PERMISSION_OTHER_USER = 240; + const API_EC_PERMISSION_STATUS_UPDATE = 250; + const API_EC_PERMISSION_PHOTO_UPLOAD = 260; + const API_EC_PERMISSION_VIDEO_UPLOAD = 261; + const API_EC_PERMISSION_SMS = 270; + const API_EC_PERMISSION_CREATE_LISTING = 280; + const API_EC_PERMISSION_CREATE_NOTE = 281; + const API_EC_PERMISSION_SHARE_ITEM = 282; + const API_EC_PERMISSION_EVENT = 290; + const API_EC_PERMISSION_LARGE_FBML_TEMPLATE = 291; + const API_EC_PERMISSION_LIVEMESSAGE = 292; + const API_EC_PERMISSION_CREATE_EVENT = 296; + const API_EC_PERMISSION_RSVP_EVENT = 299; + + /* + * DATA EDIT ERRORS + */ + const API_EC_EDIT = 300; + const API_EC_EDIT_USER_DATA = 310; + const API_EC_EDIT_PHOTO = 320; + const API_EC_EDIT_ALBUM_SIZE = 321; + const API_EC_EDIT_PHOTO_TAG_SUBJECT = 322; + const API_EC_EDIT_PHOTO_TAG_PHOTO = 323; + const API_EC_EDIT_PHOTO_FILE = 324; + const API_EC_EDIT_PHOTO_PENDING_LIMIT = 325; + const API_EC_EDIT_PHOTO_TAG_LIMIT = 326; + const API_EC_EDIT_ALBUM_REORDER_PHOTO_NOT_IN_ALBUM = 327; + const API_EC_EDIT_ALBUM_REORDER_TOO_FEW_PHOTOS = 328; + + const API_EC_MALFORMED_MARKUP = 329; + const API_EC_EDIT_MARKUP = 330; + + const API_EC_EDIT_FEED_TOO_MANY_USER_CALLS = 340; + const API_EC_EDIT_FEED_TOO_MANY_USER_ACTION_CALLS = 341; + const API_EC_EDIT_FEED_TITLE_LINK = 342; + const API_EC_EDIT_FEED_TITLE_LENGTH = 343; + const API_EC_EDIT_FEED_TITLE_NAME = 344; + const API_EC_EDIT_FEED_TITLE_BLANK = 345; + const API_EC_EDIT_FEED_BODY_LENGTH = 346; + const API_EC_EDIT_FEED_PHOTO_SRC = 347; + const API_EC_EDIT_FEED_PHOTO_LINK = 348; + + const API_EC_EDIT_VIDEO_SIZE = 350; + const API_EC_EDIT_VIDEO_INVALID_FILE = 351; + const API_EC_EDIT_VIDEO_INVALID_TYPE = 352; + const API_EC_EDIT_VIDEO_FILE = 353; + + const API_EC_EDIT_FEED_TITLE_ARRAY = 360; + const API_EC_EDIT_FEED_TITLE_PARAMS = 361; + const API_EC_EDIT_FEED_BODY_ARRAY = 362; + const API_EC_EDIT_FEED_BODY_PARAMS = 363; + const API_EC_EDIT_FEED_PHOTO = 364; + const API_EC_EDIT_FEED_TEMPLATE = 365; + const API_EC_EDIT_FEED_TARGET = 366; + const API_EC_EDIT_FEED_MARKUP = 367; + + /** + * SESSION ERRORS + */ + const API_EC_SESSION_TIMED_OUT = 450; + const API_EC_SESSION_METHOD = 451; + const API_EC_SESSION_INVALID = 452; + const API_EC_SESSION_REQUIRED = 453; + const API_EC_SESSION_REQUIRED_FOR_SECRET = 454; + const API_EC_SESSION_CANNOT_USE_SESSION_SECRET = 455; + + + /** + * FQL ERRORS + */ + const FQL_EC_UNKNOWN_ERROR = 600; + const FQL_EC_PARSER = 601; // backwards compatibility + const FQL_EC_PARSER_ERROR = 601; + const FQL_EC_UNKNOWN_FIELD = 602; + const FQL_EC_UNKNOWN_TABLE = 603; + const FQL_EC_NOT_INDEXABLE = 604; // backwards compatibility + const FQL_EC_NO_INDEX = 604; + const FQL_EC_UNKNOWN_FUNCTION = 605; + const FQL_EC_INVALID_PARAM = 606; + const FQL_EC_INVALID_FIELD = 607; + const FQL_EC_INVALID_SESSION = 608; + const FQL_EC_UNSUPPORTED_APP_TYPE = 609; + const FQL_EC_SESSION_SECRET_NOT_ALLOWED = 610; + const FQL_EC_DEPRECATED_TABLE = 611; + const FQL_EC_EXTENDED_PERMISSION = 612; + const FQL_EC_RATE_LIMIT_EXCEEDED = 613; + const FQL_EC_UNRESOLVED_DEPENDENCY = 614; + const FQL_EC_INVALID_SEARCH = 615; + const FQL_EC_CONTAINS_ERROR = 616; + + const API_EC_REF_SET_FAILED = 700; + + /** + * DATA STORE API ERRORS + */ + const API_EC_DATA_UNKNOWN_ERROR = 800; + const API_EC_DATA_INVALID_OPERATION = 801; + const API_EC_DATA_QUOTA_EXCEEDED = 802; + const API_EC_DATA_OBJECT_NOT_FOUND = 803; + const API_EC_DATA_OBJECT_ALREADY_EXISTS = 804; + const API_EC_DATA_DATABASE_ERROR = 805; + const API_EC_DATA_CREATE_TEMPLATE_ERROR = 806; + const API_EC_DATA_TEMPLATE_EXISTS_ERROR = 807; + const API_EC_DATA_TEMPLATE_HANDLE_TOO_LONG = 808; + const API_EC_DATA_TEMPLATE_HANDLE_ALREADY_IN_USE = 809; + const API_EC_DATA_TOO_MANY_TEMPLATE_BUNDLES = 810; + const API_EC_DATA_MALFORMED_ACTION_LINK = 811; + const API_EC_DATA_TEMPLATE_USES_RESERVED_TOKEN = 812; + + /* + * APPLICATION INFO ERRORS + */ + const API_EC_NO_SUCH_APP = 900; + + /* + * BATCH ERRORS + */ + const API_EC_BATCH_TOO_MANY_ITEMS = 950; + const API_EC_BATCH_ALREADY_STARTED = 951; + const API_EC_BATCH_NOT_STARTED = 952; + const API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE = 953; + + /* + * EVENT API ERRORS + */ + const API_EC_EVENT_INVALID_TIME = 1000; + const API_EC_EVENT_NAME_LOCKED = 1001; + + /* + * INFO BOX ERRORS + */ + const API_EC_INFO_NO_INFORMATION = 1050; + const API_EC_INFO_SET_FAILED = 1051; + + /* + * LIVEMESSAGE API ERRORS + */ + const API_EC_LIVEMESSAGE_SEND_FAILED = 1100; + const API_EC_LIVEMESSAGE_EVENT_NAME_TOO_LONG = 1101; + const API_EC_LIVEMESSAGE_MESSAGE_TOO_LONG = 1102; + + /* + * PAYMENTS API ERRORS + */ + const API_EC_PAYMENTS_UNKNOWN = 1150; + const API_EC_PAYMENTS_APP_INVALID = 1151; + const API_EC_PAYMENTS_DATABASE = 1152; + const API_EC_PAYMENTS_PERMISSION_DENIED = 1153; + const API_EC_PAYMENTS_APP_NO_RESPONSE = 1154; + const API_EC_PAYMENTS_APP_ERROR_RESPONSE = 1155; + const API_EC_PAYMENTS_INVALID_ORDER = 1156; + const API_EC_PAYMENTS_INVALID_PARAM = 1157; + const API_EC_PAYMENTS_INVALID_OPERATION = 1158; + const API_EC_PAYMENTS_PAYMENT_FAILED = 1159; + const API_EC_PAYMENTS_DISABLED = 1160; + + /* + * CONNECT SESSION ERRORS + */ + const API_EC_CONNECT_FEED_DISABLED = 1300; + + /* + * Platform tag bundles errors + */ + const API_EC_TAG_BUNDLE_QUOTA = 1400; + + /* + * SHARE + */ + const API_EC_SHARE_BAD_URL = 1500; + + /* + * NOTES + */ + const API_EC_NOTE_CANNOT_MODIFY = 1600; + + /* + * COMMENTS + */ + const API_EC_COMMENTS_UNKNOWN = 1700; + const API_EC_COMMENTS_POST_TOO_LONG = 1701; + const API_EC_COMMENTS_DB_DOWN = 1702; + const API_EC_COMMENTS_INVALID_XID = 1703; + const API_EC_COMMENTS_INVALID_UID = 1704; + const API_EC_COMMENTS_INVALID_POST = 1705; + const API_EC_COMMENTS_INVALID_REMOVE = 1706; + + /* + * GIFTS + */ + const API_EC_GIFTS_UNKNOWN = 1900; + + /* + * APPLICATION MORATORIUM ERRORS + */ + const API_EC_DISABLED_ALL = 2000; + const API_EC_DISABLED_STATUS = 2001; + const API_EC_DISABLED_FEED_STORIES = 2002; + const API_EC_DISABLED_NOTIFICATIONS = 2003; + const API_EC_DISABLED_REQUESTS = 2004; + const API_EC_DISABLED_EMAIL = 2005; + + /** + * This array is no longer maintained; to view the description of an error + * code, please look at the message element of the API response or visit + * the developer wiki at http://wiki.developers.facebook.com/. + */ + public static $api_error_descriptions = array( + self::API_EC_SUCCESS => 'Success', + self::API_EC_UNKNOWN => 'An unknown error occurred', + self::API_EC_SERVICE => 'Service temporarily unavailable', + self::API_EC_METHOD => 'Unknown method', + self::API_EC_TOO_MANY_CALLS => 'Application request limit reached', + self::API_EC_BAD_IP => 'Unauthorized source IP address', + self::API_EC_PARAM => 'Invalid parameter', + self::API_EC_PARAM_API_KEY => 'Invalid API key', + self::API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid', + self::API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous', + self::API_EC_PARAM_SIGNATURE => 'Incorrect signature', + self::API_EC_PARAM_USER_ID => 'Invalid user id', + self::API_EC_PARAM_USER_FIELD => 'Invalid user info field', + self::API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field', + self::API_EC_PARAM_USER_ID_LIST => 'Invalid user id list', + self::API_EC_PARAM_FIELD_LIST => 'Invalid field list', + self::API_EC_PARAM_ALBUM_ID => 'Invalid album id', + self::API_EC_PARAM_BAD_EID => 'Invalid eid', + self::API_EC_PARAM_UNKNOWN_CITY => 'Unknown city', + self::API_EC_PERMISSION => 'Permissions error', + self::API_EC_PERMISSION_USER => 'User not visible', + self::API_EC_PERMISSION_NO_DEVELOPERS => 'Application has no developers', + self::API_EC_PERMISSION_ALBUM => 'Album not visible', + self::API_EC_PERMISSION_PHOTO => 'Photo not visible', + self::API_EC_PERMISSION_EVENT => 'Creating and modifying events required the extended permission create_event', + self::API_EC_PERMISSION_RSVP_EVENT => 'RSVPing to events required the extended permission rsvp_event', + self::API_EC_EDIT_ALBUM_SIZE => 'Album is full', + self::FQL_EC_PARSER => 'FQL: Parser Error', + self::FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field', + self::FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table', + self::FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable', + self::FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function', + self::FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in', + self::API_EC_DATA_UNKNOWN_ERROR => 'Unknown data store API error', + self::API_EC_DATA_INVALID_OPERATION => 'Invalid operation', + self::API_EC_DATA_QUOTA_EXCEEDED => 'Data store allowable quota was exceeded', + self::API_EC_DATA_OBJECT_NOT_FOUND => 'Specified object cannot be found', + self::API_EC_DATA_OBJECT_ALREADY_EXISTS => 'Specified object already exists', + self::API_EC_DATA_DATABASE_ERROR => 'A database error occurred. Please try again', + self::API_EC_BATCH_ALREADY_STARTED => 'begin_batch already called, please make sure to call end_batch first', + self::API_EC_BATCH_NOT_STARTED => 'end_batch called before begin_batch', + self::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE => 'This method is not allowed in batch mode' + ); +} diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php new file mode 100644 index 0000000000..0cddbddb41 --- /dev/null +++ b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php @@ -0,0 +1,806 @@ + + * @author Matt Knapp + * @author Brett Stimmerman + * @copyright 2005 Michal Migurski + * @version CVS: $Id: JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $ + * @license http://www.opensource.org/licenses/bsd-license.php + * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 + */ + +/** + * Marker constant for Services_JSON::decode(), used to flag stack state + */ +define('SERVICES_JSON_SLICE', 1); + +/** + * Marker constant for Services_JSON::decode(), used to flag stack state + */ +define('SERVICES_JSON_IN_STR', 2); + +/** + * Marker constant for Services_JSON::decode(), used to flag stack state + */ +define('SERVICES_JSON_IN_ARR', 3); + +/** + * Marker constant for Services_JSON::decode(), used to flag stack state + */ +define('SERVICES_JSON_IN_OBJ', 4); + +/** + * Marker constant for Services_JSON::decode(), used to flag stack state + */ +define('SERVICES_JSON_IN_CMT', 5); + +/** + * Behavior switch for Services_JSON::decode() + */ +define('SERVICES_JSON_LOOSE_TYPE', 16); + +/** + * Behavior switch for Services_JSON::decode() + */ +define('SERVICES_JSON_SUPPRESS_ERRORS', 32); + +/** + * Converts to and from JSON format. + * + * Brief example of use: + * + * + * // create a new instance of Services_JSON + * $json = new Services_JSON(); + * + * // convert a complexe value to JSON notation, and send it to the browser + * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); + * $output = $json->encode($value); + * + * print($output); + * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]] + * + * // accept incoming POST data, assumed to be in JSON notation + * $input = file_get_contents('php://input', 1000000); + * $value = $json->decode($input); + * + */ +class Services_JSON +{ + /** + * constructs a new JSON instance + * + * @param int $use object behavior flags; combine with boolean-OR + * + * possible values: + * - SERVICES_JSON_LOOSE_TYPE: loose typing. + * "{...}" syntax creates associative arrays + * instead of objects in decode(). + * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression. + * Values which can't be encoded (e.g. resources) + * appear as NULL instead of throwing errors. + * By default, a deeply-nested resource will + * bubble up with an error, so all return values + * from encode() should be checked with isError() + */ + function Services_JSON($use = 0) + { + $this->use = $use; + } + + /** + * convert a string from one UTF-16 char to one UTF-8 char + * + * Normally should be handled by mb_convert_encoding, but + * provides a slower PHP-only method for installations + * that lack the multibye string extension. + * + * @param string $utf16 UTF-16 character + * @return string UTF-8 character + * @access private + */ + function utf162utf8($utf16) + { + // oh please oh please oh please oh please oh please + if(function_exists('mb_convert_encoding')) { + return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); + } + + $bytes = (ord($utf16{0}) << 8) | ord($utf16{1}); + + switch(true) { + case ((0x7F & $bytes) == $bytes): + // this case should never be reached, because we are in ASCII range + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return chr(0x7F & $bytes); + + case (0x07FF & $bytes) == $bytes: + // return a 2-byte UTF-8 character + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return chr(0xC0 | (($bytes >> 6) & 0x1F)) + . chr(0x80 | ($bytes & 0x3F)); + + case (0xFFFF & $bytes) == $bytes: + // return a 3-byte UTF-8 character + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return chr(0xE0 | (($bytes >> 12) & 0x0F)) + . chr(0x80 | (($bytes >> 6) & 0x3F)) + . chr(0x80 | ($bytes & 0x3F)); + } + + // ignoring UTF-32 for now, sorry + return ''; + } + + /** + * convert a string from one UTF-8 char to one UTF-16 char + * + * Normally should be handled by mb_convert_encoding, but + * provides a slower PHP-only method for installations + * that lack the multibye string extension. + * + * @param string $utf8 UTF-8 character + * @return string UTF-16 character + * @access private + */ + function utf82utf16($utf8) + { + // oh please oh please oh please oh please oh please + if(function_exists('mb_convert_encoding')) { + return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); + } + + switch(strlen($utf8)) { + case 1: + // this case should never be reached, because we are in ASCII range + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return $utf8; + + case 2: + // return a UTF-16 character from a 2-byte UTF-8 char + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return chr(0x07 & (ord($utf8{0}) >> 2)) + . chr((0xC0 & (ord($utf8{0}) << 6)) + | (0x3F & ord($utf8{1}))); + + case 3: + // return a UTF-16 character from a 3-byte UTF-8 char + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + return chr((0xF0 & (ord($utf8{0}) << 4)) + | (0x0F & (ord($utf8{1}) >> 2))) + . chr((0xC0 & (ord($utf8{1}) << 6)) + | (0x7F & ord($utf8{2}))); + } + + // ignoring UTF-32 for now, sorry + return ''; + } + + /** + * encodes an arbitrary variable into JSON format + * + * @param mixed $var any number, boolean, string, array, or object to be encoded. + * see argument 1 to Services_JSON() above for array-parsing behavior. + * if var is a strng, note that encode() always expects it + * to be in ASCII or UTF-8 format! + * + * @return mixed JSON string representation of input var or an error if a problem occurs + * @access public + */ + function encode($var) + { + switch (gettype($var)) { + case 'boolean': + return $var ? 'true' : 'false'; + + case 'NULL': + return 'null'; + + case 'integer': + return (int) $var; + + case 'double': + case 'float': + return (float) $var; + + case 'string': + // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT + $ascii = ''; + $strlen_var = strlen($var); + + /* + * Iterate over every character in the string, + * escaping with a slash or encoding to UTF-8 where necessary + */ + for ($c = 0; $c < $strlen_var; ++$c) { + + $ord_var_c = ord($var{$c}); + + switch (true) { + case $ord_var_c == 0x08: + $ascii .= '\b'; + break; + case $ord_var_c == 0x09: + $ascii .= '\t'; + break; + case $ord_var_c == 0x0A: + $ascii .= '\n'; + break; + case $ord_var_c == 0x0C: + $ascii .= '\f'; + break; + case $ord_var_c == 0x0D: + $ascii .= '\r'; + break; + + case $ord_var_c == 0x22: + case $ord_var_c == 0x2F: + case $ord_var_c == 0x5C: + // double quote, slash, slosh + $ascii .= '\\'.$var{$c}; + break; + + case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): + // characters U-00000000 - U-0000007F (same as ASCII) + $ascii .= $var{$c}; + break; + + case (($ord_var_c & 0xE0) == 0xC0): + // characters U-00000080 - U-000007FF, mask 110XXXXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($var{$c + 1})); + $c += 1; + $utf16 = $this->utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xF0) == 0xE0): + // characters U-00000800 - U-0000FFFF, mask 1110XXXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, + ord($var{$c + 1}), + ord($var{$c + 2})); + $c += 2; + $utf16 = $this->utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xF8) == 0xF0): + // characters U-00010000 - U-001FFFFF, mask 11110XXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, + ord($var{$c + 1}), + ord($var{$c + 2}), + ord($var{$c + 3})); + $c += 3; + $utf16 = $this->utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xFC) == 0xF8): + // characters U-00200000 - U-03FFFFFF, mask 111110XX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, + ord($var{$c + 1}), + ord($var{$c + 2}), + ord($var{$c + 3}), + ord($var{$c + 4})); + $c += 4; + $utf16 = $this->utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xFE) == 0xFC): + // characters U-04000000 - U-7FFFFFFF, mask 1111110X + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, + ord($var{$c + 1}), + ord($var{$c + 2}), + ord($var{$c + 3}), + ord($var{$c + 4}), + ord($var{$c + 5})); + $c += 5; + $utf16 = $this->utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + } + } + + return '"'.$ascii.'"'; + + case 'array': + /* + * As per JSON spec if any array key is not an integer + * we must treat the the whole array as an object. We + * also try to catch a sparsely populated associative + * array with numeric keys here because some JS engines + * will create an array with empty indexes up to + * max_index which can cause memory issues and because + * the keys, which may be relevant, will be remapped + * otherwise. + * + * As per the ECMA and JSON specification an object may + * have any string as a property. Unfortunately due to + * a hole in the ECMA specification if the key is a + * ECMA reserved word or starts with a digit the + * parameter is only accessible using ECMAScript's + * bracket notation. + */ + + // treat as a JSON object + if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { + $properties = array_map(array($this, 'name_value'), + array_keys($var), + array_values($var)); + + foreach($properties as $property) { + if(Services_JSON::isError($property)) { + return $property; + } + } + + return '{' . join(',', $properties) . '}'; + } + + // treat it like a regular array + $elements = array_map(array($this, 'encode'), $var); + + foreach($elements as $element) { + if(Services_JSON::isError($element)) { + return $element; + } + } + + return '[' . join(',', $elements) . ']'; + + case 'object': + $vars = get_object_vars($var); + + $properties = array_map(array($this, 'name_value'), + array_keys($vars), + array_values($vars)); + + foreach($properties as $property) { + if(Services_JSON::isError($property)) { + return $property; + } + } + + return '{' . join(',', $properties) . '}'; + + default: + return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS) + ? 'null' + : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string"); + } + } + + /** + * array-walking function for use in generating JSON-formatted name-value pairs + * + * @param string $name name of key to use + * @param mixed $value reference to an array element to be encoded + * + * @return string JSON-formatted name-value pair, like '"name":value' + * @access private + */ + function name_value($name, $value) + { + $encoded_value = $this->encode($value); + + if(Services_JSON::isError($encoded_value)) { + return $encoded_value; + } + + return $this->encode(strval($name)) . ':' . $encoded_value; + } + + /** + * reduce a string by removing leading and trailing comments and whitespace + * + * @param $str string string value to strip of comments and whitespace + * + * @return string string value stripped of comments and whitespace + * @access private + */ + function reduce_string($str) + { + $str = preg_replace(array( + + // eliminate single line comments in '// ...' form + '#^\s*//(.+)$#m', + + // eliminate multi-line comments in '/* ... */' form, at start of string + '#^\s*/\*(.+)\*/#Us', + + // eliminate multi-line comments in '/* ... */' form, at end of string + '#/\*(.+)\*/\s*$#Us' + + ), '', $str); + + // eliminate extraneous space + return trim($str); + } + + /** + * decodes a JSON string into appropriate variable + * + * @param string $str JSON-formatted string + * + * @return mixed number, boolean, string, array, or object + * corresponding to given JSON input string. + * See argument 1 to Services_JSON() above for object-output behavior. + * Note that decode() always returns strings + * in ASCII or UTF-8 format! + * @access public + */ + function decode($str) + { + $str = $this->reduce_string($str); + + switch (strtolower($str)) { + case 'true': + return true; + + case 'false': + return false; + + case 'null': + return null; + + default: + $m = array(); + + if (is_numeric($str)) { + // Lookie-loo, it's a number + + // This would work on its own, but I'm trying to be + // good about returning integers where appropriate: + // return (float)$str; + + // Return float or int, as appropriate + return ((float)$str == (integer)$str) + ? (integer)$str + : (float)$str; + + } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { + // STRINGS RETURNED IN UTF-8 FORMAT + $delim = substr($str, 0, 1); + $chrs = substr($str, 1, -1); + $utf8 = ''; + $strlen_chrs = strlen($chrs); + + for ($c = 0; $c < $strlen_chrs; ++$c) { + + $substr_chrs_c_2 = substr($chrs, $c, 2); + $ord_chrs_c = ord($chrs{$c}); + + switch (true) { + case $substr_chrs_c_2 == '\b': + $utf8 .= chr(0x08); + ++$c; + break; + case $substr_chrs_c_2 == '\t': + $utf8 .= chr(0x09); + ++$c; + break; + case $substr_chrs_c_2 == '\n': + $utf8 .= chr(0x0A); + ++$c; + break; + case $substr_chrs_c_2 == '\f': + $utf8 .= chr(0x0C); + ++$c; + break; + case $substr_chrs_c_2 == '\r': + $utf8 .= chr(0x0D); + ++$c; + break; + + case $substr_chrs_c_2 == '\\"': + case $substr_chrs_c_2 == '\\\'': + case $substr_chrs_c_2 == '\\\\': + case $substr_chrs_c_2 == '\\/': + if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || + ($delim == "'" && $substr_chrs_c_2 != '\\"')) { + $utf8 .= $chrs{++$c}; + } + break; + + case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): + // single, escaped unicode character + $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) + . chr(hexdec(substr($chrs, ($c + 4), 2))); + $utf8 .= $this->utf162utf8($utf16); + $c += 5; + break; + + case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): + $utf8 .= $chrs{$c}; + break; + + case ($ord_chrs_c & 0xE0) == 0xC0: + // characters U-00000080 - U-000007FF, mask 110XXXXX + //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $utf8 .= substr($chrs, $c, 2); + ++$c; + break; + + case ($ord_chrs_c & 0xF0) == 0xE0: + // characters U-00000800 - U-0000FFFF, mask 1110XXXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $utf8 .= substr($chrs, $c, 3); + $c += 2; + break; + + case ($ord_chrs_c & 0xF8) == 0xF0: + // characters U-00010000 - U-001FFFFF, mask 11110XXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $utf8 .= substr($chrs, $c, 4); + $c += 3; + break; + + case ($ord_chrs_c & 0xFC) == 0xF8: + // characters U-00200000 - U-03FFFFFF, mask 111110XX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $utf8 .= substr($chrs, $c, 5); + $c += 4; + break; + + case ($ord_chrs_c & 0xFE) == 0xFC: + // characters U-04000000 - U-7FFFFFFF, mask 1111110X + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $utf8 .= substr($chrs, $c, 6); + $c += 5; + break; + + } + + } + + return $utf8; + + } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { + // array, or object notation + + if ($str{0} == '[') { + $stk = array(SERVICES_JSON_IN_ARR); + $arr = array(); + } else { + if ($this->use & SERVICES_JSON_LOOSE_TYPE) { + $stk = array(SERVICES_JSON_IN_OBJ); + $obj = array(); + } else { + $stk = array(SERVICES_JSON_IN_OBJ); + $obj = new stdClass(); + } + } + + array_push($stk, array('what' => SERVICES_JSON_SLICE, + 'where' => 0, + 'delim' => false)); + + $chrs = substr($str, 1, -1); + $chrs = $this->reduce_string($chrs); + + if ($chrs == '') { + if (reset($stk) == SERVICES_JSON_IN_ARR) { + return $arr; + + } else { + return $obj; + + } + } + + //print("\nparsing {$chrs}\n"); + + $strlen_chrs = strlen($chrs); + + for ($c = 0; $c <= $strlen_chrs; ++$c) { + + $top = end($stk); + $substr_chrs_c_2 = substr($chrs, $c, 2); + + if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { + // found a comma that is not inside a string, array, etc., + // OR we've reached the end of the character list + $slice = substr($chrs, $top['where'], ($c - $top['where'])); + array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); + //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); + + if (reset($stk) == SERVICES_JSON_IN_ARR) { + // we are in an array, so just push an element onto the stack + array_push($arr, $this->decode($slice)); + + } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { + // we are in an object, so figure + // out the property name and set an + // element in an associative array, + // for now + $parts = array(); + + if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { + // "name":value pair + $key = $this->decode($parts[1]); + $val = $this->decode($parts[2]); + + if ($this->use & SERVICES_JSON_LOOSE_TYPE) { + $obj[$key] = $val; + } else { + $obj->$key = $val; + } + } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { + // name:value pair, where name is unquoted + $key = $parts[1]; + $val = $this->decode($parts[2]); + + if ($this->use & SERVICES_JSON_LOOSE_TYPE) { + $obj[$key] = $val; + } else { + $obj->$key = $val; + } + } + + } + + } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { + // found a quote, and we are not inside a string + array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); + //print("Found start of string at {$c}\n"); + + } elseif (($chrs{$c} == $top['delim']) && + ($top['what'] == SERVICES_JSON_IN_STR) && + ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { + // found a quote, we're in a string, and it's not escaped + // we know that it's not escaped becase there is _not_ an + // odd number of backslashes at the end of the string so far + array_pop($stk); + //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); + + } elseif (($chrs{$c} == '[') && + in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { + // found a left-bracket, and we are in an array, object, or slice + array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); + //print("Found start of array at {$c}\n"); + + } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { + // found a right-bracket, and we're in an array + array_pop($stk); + //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); + + } elseif (($chrs{$c} == '{') && + in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { + // found a left-brace, and we are in an array, object, or slice + array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); + //print("Found start of object at {$c}\n"); + + } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { + // found a right-brace, and we're in an object + array_pop($stk); + //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); + + } elseif (($substr_chrs_c_2 == '/*') && + in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { + // found a comment start, and we are in an array, object, or slice + array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); + $c++; + //print("Found start of comment at {$c}\n"); + + } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { + // found a comment end, and we're in one now + array_pop($stk); + $c++; + + for ($i = $top['where']; $i <= $c; ++$i) + $chrs = substr_replace($chrs, ' ', $i, 1); + + //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); + + } + + } + + if (reset($stk) == SERVICES_JSON_IN_ARR) { + return $arr; + + } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { + return $obj; + + } + + } + } + } + + /** + * @todo Ultimately, this should just call PEAR::isError() + */ + function isError($data, $code = null) + { + if (class_exists('pear')) { + return PEAR::isError($data, $code); + } elseif (is_object($data) && (get_class($data) == 'services_json_error' || + is_subclass_of($data, 'services_json_error'))) { + return true; + } + + return false; + } +} + +if (class_exists('PEAR_Error')) { + + class Services_JSON_Error extends PEAR_Error + { + function Services_JSON_Error($message = 'unknown error', $code = null, + $mode = null, $options = null, $userinfo = null) + { + parent::PEAR_Error($message, $code, $mode, $options, $userinfo); + } + } + +} else { + + /** + * @todo Ultimately, this class shall be descended from PEAR_Error + */ + class Services_JSON_Error + { + function Services_JSON_Error($message = 'unknown error', $code = null, + $mode = null, $options = null, $userinfo = null) + { + + } + } + +} + +?> diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE new file mode 100644 index 0000000000..4ae6bef55d --- /dev/null +++ b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE @@ -0,0 +1,21 @@ +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN +NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php new file mode 100644 index 0000000000..29509debad --- /dev/null +++ b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php @@ -0,0 +1,6 @@ + diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php new file mode 100644 index 0000000000..36a3f28635 --- /dev/null +++ b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php @@ -0,0 +1,23 @@ +encode($arg); +} + +function json_decode($arg) +{ + global $services_json; + if (!isset($services_json)) { + $services_json = new Services_JSON(); + } + return $services_json->decode($arg); +} + +?> From 035081a803b005b8a2410c6936eac4027fda493c Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 5 Nov 2010 06:34:06 +0000 Subject: [PATCH 08/17] Much more reliable Facebook SSO --- plugins/FacebookSSO/FacebookSSOPlugin.php | 62 +- ...okregister.php => facebookfinishlogin.php} | 50 +- plugins/FacebookSSO/actions/facebooklogin.php | 125 ++-- plugins/FacebookSSO/images/login-button.png | Bin 0 -> 1661 bytes plugins/FacebookSSO/lib/facebookclient.php | 640 ++++++++++++++++++ .../FacebookSSO/lib/facebookqueuehandler.php | 63 ++ 6 files changed, 806 insertions(+), 134 deletions(-) rename plugins/FacebookSSO/actions/{facebookregister.php => facebookfinishlogin.php} (93%) create mode 100644 plugins/FacebookSSO/images/login-button.png create mode 100644 plugins/FacebookSSO/lib/facebookclient.php create mode 100644 plugins/FacebookSSO/lib/facebookqueuehandler.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index b14ef0bade..a726b2facc 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -125,7 +125,7 @@ class FacebookSSOPlugin extends Plugin include_once $dir . '/extlib/facebookapi_php5_restlib.php'; return false; case 'FacebookloginAction': - case 'FacebookregisterAction': + case 'FacebookfinishloginAction': case 'FacebookadminpanelAction': case 'FacebooksettingsAction': include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; @@ -146,15 +146,17 @@ class FacebookSSOPlugin extends Plugin function needsScripts($action) { static $needy = array( - 'FacebookloginAction', - 'FacebookregisterAction', + //'FacebookloginAction', + 'FacebookfinishloginAction', 'FacebookadminpanelAction', 'FacebooksettingsAction' ); if (in_array(get_class($action), $needy)) { + common_debug("needs scripts!"); return true; } else { + common_debug("doesn't need scripts!"); return false; } } @@ -185,8 +187,8 @@ class FacebookSSOPlugin extends Plugin array('action' => 'facebooklogin') ); $m->connect( - 'main/facebookregister', - array('action' => 'facebookregister') + 'main/facebookfinishlogin', + array('action' => 'facebookfinishlogin') ); $m->connect( @@ -297,51 +299,43 @@ class FacebookSSOPlugin extends Plugin } function onStartShowHeader($action) + { + // output
as close to as possible + $action->element('div', array('id' => 'fb-root')); + return true; + } + + function onEndShowScripts($action) { if ($this->needsScripts($action)) { - // output
as close to as possible - $action->element('div', array('id' => 'fb-root')); - - $dir = dirname(__FILE__); + $action->script('https://connect.facebook.net/en_US/all.js'); $script = <<inlineScript( sprintf($script, json_encode($this->facebook->getAppId()), - json_encode($this->facebook->getSession()) + json_encode($this->facebook->getSession()), + common_local_url('facebookfinishlogin') ) ); } - - return true; } /* diff --git a/plugins/FacebookSSO/actions/facebookregister.php b/plugins/FacebookSSO/actions/facebookfinishlogin.php similarity index 93% rename from plugins/FacebookSSO/actions/facebookregister.php rename to plugins/FacebookSSO/actions/facebookfinishlogin.php index e21deff880..16f7cff500 100644 --- a/plugins/FacebookSSO/actions/facebookregister.php +++ b/plugins/FacebookSSO/actions/facebookfinishlogin.php @@ -2,7 +2,7 @@ /** * StatusNet, the distributed open-source microblogging tool * - * Register a local user and connect it to a Facebook account + * Login or register a local user based on a Facebook user * * PHP version 5 * @@ -31,7 +31,7 @@ if (!defined('STATUSNET')) { exit(1); } -class FacebookregisterAction extends Action +class FacebookfinishloginAction extends Action { private $facebook = null; // Facebook client @@ -220,7 +220,7 @@ class FacebookregisterAction extends Action $this->elementStart('form', array('method' => 'post', 'id' => 'form_settings_facebook_connect', 'class' => 'form_settings', - 'action' => common_local_url('facebookregister'))); + 'action' => common_local_url('facebookfinishlogin'))); $this->elementStart('fieldset', array('id' => 'settings_facebook_connect_options')); // TRANS: Legend. $this->element('legend', null, _m('Connection options')); @@ -428,27 +428,46 @@ class FacebookregisterAction extends Action return; } - common_debug('Facebook Connect Plugin - ' . - "Connected Facebook user $this->fbuid to local user $user->id"); + common_debug( + sprintf( + 'Connected Facebook user %s to local user %d', + $this->fbuid, + $user->id + ), + __FILE__ + ); // Return to Facebook connection settings tab - common_redirect(common_local_url('FBConnectSettings'), 303); + common_redirect(common_local_url('facebookfinishlogin'), 303); } function tryLogin() { - common_debug('Facebook Connect Plugin - ' . - "Trying login for Facebook user $this->fbuid."); + common_debug( + sprintf( + 'Trying login for Facebook user %s', + $this->fbuid + ), + __FILE__ + ); - $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_CONNECT_SERVICE); + $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE); if (!empty($flink)) { $user = $flink->getUser(); if (!empty($user)) { - common_debug('Facebook Connect Plugin - ' . - "Logged in Facebook user $flink->foreign_id as user $user->id ($user->nickname)"); + common_log( + LOG_INFO, + sprintf( + 'Logged in Facebook user %s as user %d (%s)', + $this->fbuid, + $user->nickname, + $user->id + ), + __FILE__ + ); common_set_user($user); common_real_login(true); @@ -457,8 +476,13 @@ class FacebookregisterAction extends Action } else { - common_debug('Facebook Connect Plugin - ' . - "No flink found for fbuid: $this->fbuid - new user"); + common_debug( + sprintf( + 'No flink found for fbuid: %s - new user', + $this->fbuid + ), + __FILE__ + ); $this->showForm(null, $this->bestNewNickname()); } diff --git a/plugins/FacebookSSO/actions/facebooklogin.php b/plugins/FacebookSSO/actions/facebooklogin.php index bb30be1af7..08c237fe6e 100644 --- a/plugins/FacebookSSO/actions/facebooklogin.php +++ b/plugins/FacebookSSO/actions/facebooklogin.php @@ -40,90 +40,10 @@ class FacebookloginAction extends Action parent::handle($args); if (common_is_real_login()) { - $this->clientError(_m('Already logged in.')); - } else { - - $facebook = new Facebook( - array( - 'appId' => common_config('facebook', 'appid'), - 'secret' => common_config('facebook', 'secret'), - 'cookie' => true, - ) - ); - - $session = $facebook->getSession(); - $fbuser = null; - - if ($session) { - try { - $fbuid = $facebook->getUser(); - $fbuser = $facebook->api('/me'); - } catch (FacebookApiException $e) { - common_log(LOG_ERROR, $e); - } - } - - if (!empty($fbuser)) { - common_debug("Found a valid Facebook user", __FILE__); - - // Check to see if we have a foreign link already - $flink = Foreign_link::getByForeignId($fbuid, FACEBOOK_SERVICE); - - if (empty($flink)) { - - // See if the user would like to register a new local - // account - common_redirect( - common_local_url('facebookregister'), - 303 - ); - - } else { - - // Log our user in! - $user = $flink->getUser(); - - if (!empty($user)) { - - common_log( - LOG_INFO, - sprintf( - 'Logged in Facebook user %s as user %s (%s)', - $fbuid, - $user->id, - $user->nickname - ), - __FILE__ - ); - - common_set_user($user); - common_real_login(true); - $this->goHome($user->nickname); - } - } - - } + $this->showPage(); } - - $this->showPage(); - } - - function goHome($nickname) - { - $url = common_get_returnto(); - if ($url) { - // We don't have to return to it again - common_set_returnto(null); - } else { - $url = common_local_url( - 'all', - array('nickname' => $nickname) - ); - } - - common_redirect($url, 303); } function getInstructions() @@ -151,14 +71,45 @@ class FacebookloginAction extends Action $this->elementStart('fieldset'); - $attrs = array( - //'show-faces' => 'true', - //'max-rows' => '4', - //'width' => '600', - 'perms' => 'user_location,user_website,offline_access,publish_stream' + $facebook = Facebookclient::getFacebook(); + + // Degrade to plain link if JavaScript is not available + $this->elementStart( + 'a', + array( + 'href' => $facebook->getLoginUrl( + array( + 'next' => common_local_url('facebookfinishlogin'), + 'cancel' => common_local_url('facebooklogin') + ) + ), + 'id' => 'facebook_button' + ) ); - $this->element('fb:login-button', $attrs); + $attrs = array( + 'src' => common_path( + 'plugins/FacebookSSO/images/login-button.png', + true + ), + 'alt' => 'Login with Facebook', + 'title' => 'Login with Facebook' + ); + + $this->element('img', $attrs); + + $this->elementEnd('a'); + + /* + $this->element('div', array('id' => 'fb-root')); + $this->script( + sprintf( + 'http://connect.facebook.net/en_US/all.js#appId=%s&xfbml=1', + common_config('facebook', 'appid') + ) + ); + $this->element('fb:facepile', array('max-rows' => '2', 'width' =>'300')); + */ $this->elementEnd('fieldset'); } diff --git a/plugins/FacebookSSO/images/login-button.png b/plugins/FacebookSSO/images/login-button.png new file mode 100644 index 0000000000000000000000000000000000000000..4e7766bcad5c627ba0969f9df4a4adffb0eaf2c7 GIT binary patch literal 1661 zcmV-@27>vCP)~SM{{Fzm+-P{L-{bAc&f#u-uXKa6 zae%RMfwJ%M_RG-Xw7b=GgR;-n%u(;Eyvd`-6^KyZ)%FyD}+2^RS&Ze);=IQZkd#%{q>34>-<>>Hhdabp* z*8Tnd`1$*pq{oYwzqr5G^!54B*5$0U(fRuP=j!rpe6H;7^qQo|#LC{w(c zaDT9mn!#sxtV>*!YI?2c>+-I)(xtA=>+bY|kGrO?&bq+SSzeXyyr(2||Q z(%0sChqlbpV{@s&#@u9dse+HXNm`O(bE#x?s!3UrVRET}jk;lRsYzLpVR5J=G;fb7(31cF z17b-;K~#9!)sFXf3}F<8vFzQfNJtP2`8J4z*bu#Jh>&csL=T@Hy)Ds8L|@S&x``5X zjoy2k=)ITKTlD@PxRd$jOfbq%HlB0lyYIPk&wHL+IhouVMh2-hw<7@mQK7}=wMT#0#P|Z&T&E(*z zEQq+Ado!BAdp ztPm0sLNtJi8cWVhRQ^gWv{L1;Dpg6f>R~l%)*`j*)FqbidgUs~iuEHJkjNm?uu+sH zoJ3n=^r+U@Y;KahplO7ySwlja*NJV>vTl%}Rcrjwrfs{p_N-bNsMbMWUR9$EP{7<# z*#+R!DSzkCF3^>7-MYs^584xYQLcBNzR-_K^oIchVUWG+V9J?kS;LS7Jm7{78_o($ z@$sQ_#K=*x(R4J789CN(T;jm-20MkMA`@UDt7ZcmCQTke85(L+epH(Z)2Q7sJX% zyK+4y^~TLorxH(yaO*aN8%0*l1~%M*yP^!Lxl~Y14jHcRxpyC*2lj{kps?%rJbJ9P zed2OG#c}_c3wi!RUOr~k%J>0P%Tz%%Ib>vN@X~6q`M=8iSD@N!6@XQ8c=J{Z@6dN| z{~f5dPX*QF@cx7QLA8%6s3wQ^pWM$c)INW4KdAOKhmz|XdC@~$IWWJf00000NkvXX Hu0mjfITyzy literal 0 HcmV?d00001 diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookSSO/lib/facebookclient.php new file mode 100644 index 0000000000..a0549a1d01 --- /dev/null +++ b/plugins/FacebookSSO/lib/facebookclient.php @@ -0,0 +1,640 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @author Zach Copley + * @copyright 2009-2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Class for communication with Facebook + * + * @category Plugin + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ +class Facebookclient +{ + protected $facebook = null; // Facebook Graph client obj + protected $flink = null; // Foreign_link StatusNet -> Facebook + protected $notice = null; // The user's notice + protected $user = null; // Sender of the notice + protected $oldRestClient = null; // Old REST API client + + function __constructor($notice) + { + $this->facebook = self::getFacebook(); + $this->notice = $notice; + + $this->flink = Foreign_link::getByUserID( + $notice->profile_id, + FACEBOOK_SERVICE + ); + + $this->user = $this->flink->getUser(); + + $this->oldRestClient = self::getOldRestClient(); + } + + /* + * Get and instance of the old REST API client for sending notices from + * users with Facebook links that pre-exist the Graph API + */ + static function getOldRestClient() + { + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + + // If there's no app key and secret set in the local config, look + // for a global one + if (empty($apikey) || empty($secret)) { + $apikey = common_config('facebook', 'global_apikey'); + $secret = common_config('facebook', 'global_secret'); + } + + return new FacebookRestClient($apikey, $secret, null); + } + + /* + * Get an instance of the Facebook Graph SDK object + * + * @param string $appId Application + * @param string $secret Facebook API secret + * + * @return Facebook A Facebook SDK obj + */ + static function getFacebook($appId = null, $secret = null) + { + // Check defaults and configuration for application ID and secret + if (empty($appId)) { + $appId = common_config('facebook', 'appid'); + } + + if (empty($secret)) { + $secret = common_config('facebook', 'secret'); + } + + // If there's no app ID and secret set in the local config, look + // for a global one + if (empty($appId) || empty($secret)) { + $appId = common_config('facebook', 'global_appid'); + $secret = common_config('facebook', 'global_secret'); + } + + return new Facebook( + array( + 'appId' => $appId, + 'secret' => $secret, + 'cookie' => true + ) + ); + } + + /* + * Broadcast a notice to Facebook + * + * @param Notice $notice the notice to send + */ + static function facebookBroadcastNotice($notice) + { + $client = new Facebookclient($notice); + $client->sendNotice(); + } + + /* + * Should the notice go to Facebook? + */ + function isFacebookBound() { + + if (empty($this->flink)) { + common_log( + LOG_WARN, + sprintf( + "No Foreign_link to Facebook for the author of notice %d.", + $this->notice->id + ), + __FILE__ + ); + return false; + } + + // Avoid a loop + if ($this->notice->source == 'Facebook') { + common_log( + LOG_INFO, + sprintf( + 'Skipping notice %d because its source is Facebook.', + $this->notice->id + ), + __FILE__ + ); + return false; + } + + // If the user does not want to broadcast to Facebook, move along + if (!($this->flink->noticesync & FOREIGN_NOTICE_SEND == FOREIGN_NOTICE_SEND)) { + common_log( + LOG_INFO, + sprintf( + 'Skipping notice %d because user has FOREIGN_NOTICE_SEND bit off.', + $this->notice->id + ), + __FILE__ + ); + return false; + } + + // If it's not a reply, or if the user WANTS to send @-replies, + // then, yeah, it can go to Facebook. + if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $this->notice->content) || + ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) { + return true; + } + + return false; + } + + /* + * Determine whether we should send this notice using the Graph API or the + * old REST API and then dispatch + */ + function sendNotice() + { + // If there's nothing in the credentials field try to send via + // the Old Rest API + + if (empty($this->flink->credentials)) { + $this->sendOldRest(); + } else { + + // Otherwise we most likely have an access token + $this->sendGraph(); + } + } + + /* + * Send a notice to Facebook using the Graph API + */ + function sendGraph() + { + common_debug("Send notice via Graph API", __FILE__); + } + + /* + * Send a notice to Facebook using the deprecated Old REST API. We need this + * for backwards compatibility. Users who signed up for Facebook bridging + * using the old Facebook Canvas application do not have an OAuth 2.0 + * access token. + */ + function sendOldRest() + { + if (isFacebookBound()) { + + try { + + $canPublish = $this->checkPermission('publish_stream'); + $canUpdate = $this->checkPermission('status_update'); + + // Post to Facebook + if ($notice->hasAttachments() && $canPublish == 1) { + $this->restPublishStream(); + } elseif ($canUpdate == 1 || $canPublish == 1) { + $this->restStatusUpdate(); + } else { + + $msg = 'Not sending notice %d to Facebook because user %s ' + . '(%d), fbuid %s, does not have \'status_update\' ' + . 'or \'publish_stream\' permission.'; + + common_log( + LOG_WARNING, + sprintf( + $msg, + $this->notice->id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + } + + } catch (FacebookRestClientException $e) { + return $this->handleFacebookError($e); + } + } + + return true; + } + + /* + * Query Facebook to to see if a user has permission + * + * + * + * @param $permission the permission to check for - must be either + * public_stream or status_update + * + * @return boolean result + */ + function checkPermission($permission) + { + + if (!in_array($permission, array('publish_stream', 'status_update'))) { + throw new ServerExpception("No such permission!"); + } + + $fbuid = $this->flink->foreign_link; + + common_debug( + sprintf( + 'Checking for %s permission for user %s (%d), fbuid %s', + $permission, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + // NOTE: $this->oldRestClient->users_hasAppPermission() has been + // returning bogus results, so we're using FQL to check for + // permissions + + $fql = sprintf( + "SELECT %s FROM permissions WHERE uid = %s", + $permission, + $fbuid + ); + + $result = $this->oldRestClient->fql_query($fql); + + $hasPermission = 0; + + if (isset($result[0][$permission])) { + $canPublish = $result[0][$permission]; + } + + if ($hasPermission == 1) { + + common_debug( + sprintf( + '%s (%d), fbuid %s has %s permission', + $permission, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + return true; + + } else { + + $logMsg = '%s (%d), fbuid $fbuid does NOT have %s permission.' + . 'Facebook returned: %s'; + + common_debug( + sprintf( + $logMsg, + $this->user->nickname, + $this->user->id, + $permission, + $fbuid, + var_export($result, true) + ), + __FILE__ + ); + + return false; + + } + + } + + function handleFacebookError($e) + { + $fbuid = $this->flink->foreign_id; + $code = $e->getCode(); + $errmsg = $e->getMessage(); + + // XXX: Check for any others? + switch($code) { + case 100: // Invalid parameter + $msg = 'Facebook claims notice %d was posted with an invalid ' + . 'parameter (error code 100 - %s) Notice details: ' + . '[nickname=%s, user id=%d, fbuid=%d, content="%s"]. ' + . 'Dequeing.'; + common_log( + LOG_ERR, sprintf( + $msg, + $this->notice->id, + $errmsg, + $this->user->nickname, + $this->user->id, + $fbuid, + $this->notice->content + ), + __FILE__ + ); + return true; + break; + case 200: // Permissions error + case 250: // Updating status requires the extended permission status_update + $this->disconnect(); + return true; // dequeue + break; + case 341: // Feed action request limit reached + $msg = '%s (userid=%d, fbuid=%d) has exceeded his/her limit ' + . 'for posting notices to Facebook today. Dequeuing ' + . 'notice %d'; + common_log( + LOG_INFO, sprintf( + $msg, + $user->nickname, + $user->id, + $fbuid, + $this->notice->id + ), + __FILE__ + ); + // @fixme: We want to rety at a later time when the throttling has expired + // instead of just giving up. + return true; + break; + default: + $msg = 'Facebook returned an error we don\'t know how to deal with ' + . 'when posting notice %d. Error code: %d, error message: "%s"' + . ' Notice details: [nickname=%s, user id=%d, fbuid=%d, ' + . 'notice content="%s"]. Dequeing.'; + common_log( + LOG_ERR, sprintf( + $msg, + $this->notice->id, + $code, + $errmsg, + $this->user->nickname, + $this->user->id, + $fbuid, + $this->notice->content + ), + __FILE__ + ); + return true; // dequeue + break; + } + } + + function restStatusUpdate() + { + $fbuid = $this->flink->foreign_id; + + common_debug( + sprintf( + "Attempting to post notice %d as a status update for %s (%d), fbuid %s", + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + $result = $this->oldRestClient->users_setStatus( + $this->notice->content, + $fbuid, + false, + true + ); + + common_log( + LOG_INFO, + sprintf( + "Posted notice %s as a status update for %s (%d), fbuid %s", + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + } + + function restPublishStream() + { + $fbuid = $this->flink->foreign_id; + + common_debug( + sprintf( + 'Attempting to post notice %d as stream item with attachment for ' + . '%s (%d) fbuid %s', + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + $fbattachment = format_attachments($notice->attachments()); + + $this->oldRestClient->stream_publish( + $this->notice->content, + $fbattachment, + null, + null, + $fbuid + ); + + common_log( + LOG_INFO, + sprintf( + 'Posted notice %d as a stream item with attachment for %s ' + . '(%d), fbuid %s', + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + } + + function format_attachments($attachments) + { + $fbattachment = array(); + $fbattachment['media'] = array(); + + foreach($attachments as $attachment) + { + if($enclosure = $attachment->getEnclosure()){ + $fbmedia = get_fbmedia_for_attachment($enclosure); + }else{ + $fbmedia = get_fbmedia_for_attachment($attachment); + } + if($fbmedia){ + $fbattachment['media'][]=$fbmedia; + }else{ + $fbattachment['name'] = ($attachment->title ? + $attachment->title : $attachment->url); + $fbattachment['href'] = $attachment->url; + } + } + if(count($fbattachment['media'])>0){ + unset($fbattachment['name']); + unset($fbattachment['href']); + } + return $fbattachment; + } + + /** + * given an File objects, returns an associative array suitable for Facebook media + */ + function get_fbmedia_for_attachment($attachment) + { + $fbmedia = array(); + + if (strncmp($attachment->mimetype, 'image/', strlen('image/')) == 0) { + $fbmedia['type'] = 'image'; + $fbmedia['src'] = $attachment->url; + $fbmedia['href'] = $attachment->url; + } else if ($attachment->mimetype == 'audio/mpeg') { + $fbmedia['type'] = 'mp3'; + $fbmedia['src'] = $attachment->url; + }else if ($attachment->mimetype == 'application/x-shockwave-flash') { + $fbmedia['type'] = 'flash'; + + // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29 + // says that imgsrc is required... but we have no value to put in it + // $fbmedia['imgsrc']=''; + + $fbmedia['swfsrc'] = $attachment->url; + }else{ + return false; + } + return $fbmedia; + } + + function disconnect() + { + $fbuid = $this->flink->foreign_link; + + common_log( + LOG_INFO, + sprintf( + 'Removing Facebook link for %s (%d), fbuid %s', + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + $result = $flink->delete(); + + if (empty($result)) { + common_log( + LOG_ERR, + sprintf( + 'Could not remove Facebook link for %s (%d), fbuid %s', + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + common_log_db_error($flink, 'DELETE', __FILE__); + } + + // Notify the user that we are removing their Facebook link + + $result = $this->mailFacebookDisconnect(); + + if (!$result) { + + $msg = 'Unable to send email to notify %s (%d), fbuid %s ' + . 'about his/her Facebook link being removed.'; + + common_log( + LOG_WARNING, + sprintf( + $msg, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + } + } + + /** + * Send a mail message to notify a user that her Facebook link + * has been terminated. + * + * @return boolean success flag + */ + function mailFacebookDisconnect() + { + $profile = $user->getProfile(); + + $siteName = common_config('site', 'name'); + + common_switch_locale($user->language); + + $subject = sprintf( + _m('Your Facebook connection has been removed'), + $siteName + ); + + $msg = <<user->nickname, + $siteName + ); + + common_switch_locale(); + + return mail_to_user($this->user, $subject, $body); + } + +} diff --git a/plugins/FacebookSSO/lib/facebookqueuehandler.php b/plugins/FacebookSSO/lib/facebookqueuehandler.php new file mode 100644 index 0000000000..af96d35c49 --- /dev/null +++ b/plugins/FacebookSSO/lib/facebookqueuehandler.php @@ -0,0 +1,63 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php'; + +class FacebookQueueHandler extends QueueHandler +{ + function transport() + { + return 'facebook'; + } + + function handle($notice) + { + if ($this->_isLocal($notice)) { + return facebookBroadcastNotice($notice); + } + return true; + } + + /** + * Determine whether the notice was locally created + * + * @param Notice $notice the notice + * + * @return boolean locality + */ + function _isLocal($notice) + { + return ($notice->is_local == Notice::LOCAL_PUBLIC || + $notice->is_local == Notice::LOCAL_NONPUBLIC); + } +} From cd236efe127c3f696b8a78c5ff66a08b24230a4e Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 9 Nov 2010 00:56:53 +0000 Subject: [PATCH 09/17] - Still send notices to Facebook from existing Facebook app users - Turns out we don't need the old REST lib to use the old REST API (removed) --- plugins/FacebookSSO/FacebookSSOPlugin.php | 35 +- .../actions/facebookadminpanel.php | 4 +- .../extlib/facebookapi_php5_restlib.php | 3702 ----------------- .../extlib/jsonwrapper/JSON/JSON.php | 806 ---- .../extlib/jsonwrapper/JSON/LICENSE | 21 - .../extlib/jsonwrapper/jsonwrapper.php | 6 - .../extlib/jsonwrapper/jsonwrapper_inner.php | 23 - plugins/FacebookSSO/lib/facebookclient.php | 279 +- .../FacebookSSO/lib/facebookqueuehandler.php | 4 +- 9 files changed, 210 insertions(+), 4670 deletions(-) delete mode 100644 plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php delete mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php delete mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE delete mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php delete mode 100644 plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index a726b2facc..a094f2957f 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -131,7 +131,7 @@ class FacebookSSOPlugin extends Plugin include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; case 'Facebookclient': - case 'Facebookqueuehandler': + case 'FacebookQueueHandler': include_once $dir . '/lib/' . strtolower($cls) . '.php'; return false; default: @@ -146,7 +146,7 @@ class FacebookSSOPlugin extends Plugin function needsScripts($action) { static $needy = array( - //'FacebookloginAction', + 'FacebookloginAction', 'FacebookfinishloginAction', 'FacebookadminpanelAction', 'FacebooksettingsAction' @@ -401,6 +401,37 @@ ENDOFSCRIPT; return true; } + /** + * Add a Facebook queue item for each notice + * + * @param Notice $notice the notice + * @param array &$transports the list of transports (queues) + * + * @return boolean hook return + */ + function onStartEnqueueNotice($notice, &$transports) + { + if (self::hasApplication() && $notice->isLocal()) { + array_push($transports, 'facebook'); + } + return true; + } + + /** + * Register Facebook notice queue handler + * + * @param QueueManager $manager + * + * @return boolean hook return + */ + function onEndInitializeQueueManager($manager) + { + if (self::hasApplication()) { + $manager->connect('facebook', 'FacebookQueueHandler'); + } + return true; + } + /* * Add version info for this plugin * diff --git a/plugins/FacebookSSO/actions/facebookadminpanel.php b/plugins/FacebookSSO/actions/facebookadminpanel.php index b76b035cd0..61b5441848 100644 --- a/plugins/FacebookSSO/actions/facebookadminpanel.php +++ b/plugins/FacebookSSO/actions/facebookadminpanel.php @@ -82,7 +82,7 @@ class FacebookadminpanelAction extends AdminPanelAction function saveSettings() { static $settings = array( - 'facebook' => array('appid', 'secret'), + 'facebook' => array('appid', 'secret'), ); $values = array(); @@ -116,7 +116,7 @@ class FacebookadminpanelAction extends AdminPanelAction function validate(&$values) { - // appId and secret (can't be too long) + // appId, key and secret (can't be too long) if (mb_strlen($values['facebook']['appid']) > 255) { $this->clientError( diff --git a/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php b/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php deleted file mode 100644 index e249a326b2..0000000000 --- a/plugins/FacebookSSO/extlib/facebookapi_php5_restlib.php +++ /dev/null @@ -1,3702 +0,0 @@ -secret = $secret; - $this->session_key = $session_key; - $this->api_key = $api_key; - $this->batch_mode = FacebookRestClient::BATCH_MODE_DEFAULT; - $this->last_call_id = 0; - $this->call_as_apikey = ''; - $this->use_curl_if_available = true; - $this->server_addr = - Facebook::get_facebook_url('api') . '/restserver.php'; - $this->photo_server_addr = - Facebook::get_facebook_url('api-photo') . '/restserver.php'; - - if (!empty($GLOBALS['facebook_config']['debug'])) { - $this->cur_id = 0; - ?> - -user = $uid; - } - - - /** - * Switch to use the session secret instead of the app secret, - * for desktop and unsecured environment - */ - public function use_session_secret($session_secret) { - $this->secret = $session_secret; - $this->using_session_secret = true; - } - - /** - * Normally, if the cURL library/PHP extension is available, it is used for - * HTTP transactions. This allows that behavior to be overridden, falling - * back to a vanilla-PHP implementation even if cURL is installed. - * - * @param $use_curl_if_available bool whether or not to use cURL if available - */ - public function set_use_curl_if_available($use_curl_if_available) { - $this->use_curl_if_available = $use_curl_if_available; - } - - /** - * Start a batch operation. - */ - public function begin_batch() { - if ($this->pending_batch()) { - $code = FacebookAPIErrorCodes::API_EC_BATCH_ALREADY_STARTED; - $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; - throw new FacebookRestClientException($description, $code); - } - - $this->batch_queue = array(); - $this->pending_batch = true; - } - - /* - * End current batch operation - */ - public function end_batch() { - if (!$this->pending_batch()) { - $code = FacebookAPIErrorCodes::API_EC_BATCH_NOT_STARTED; - $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; - throw new FacebookRestClientException($description, $code); - } - - $this->pending_batch = false; - - $this->execute_server_side_batch(); - $this->batch_queue = null; - } - - /** - * are we currently queueing up calls for a batch? - */ - public function pending_batch() { - return $this->pending_batch; - } - - private function execute_server_side_batch() { - $item_count = count($this->batch_queue); - $method_feed = array(); - foreach ($this->batch_queue as $batch_item) { - $method = $batch_item['m']; - $params = $batch_item['p']; - list($get, $post) = $this->finalize_params($method, $params); - $method_feed[] = $this->create_url_string(array_merge($post, $get)); - } - - $serial_only = - ($this->batch_mode == FacebookRestClient::BATCH_MODE_SERIAL_ONLY); - - $params = array('method_feed' => json_encode($method_feed), - 'serial_only' => $serial_only, - 'format' => $this->format); - $result = $this->call_method('facebook.batch.run', $params); - - if (is_array($result) && isset($result['error_code'])) { - throw new FacebookRestClientException($result['error_msg'], - $result['error_code']); - } - - for ($i = 0; $i < $item_count; $i++) { - $batch_item = $this->batch_queue[$i]; - $batch_item['p']['format'] = $this->format; - $batch_item_result = $this->convert_result($result[$i], - $batch_item['m'], - $batch_item['p']); - - if (is_array($batch_item_result) && - isset($batch_item_result['error_code'])) { - throw new FacebookRestClientException($batch_item_result['error_msg'], - $batch_item_result['error_code']); - } - $batch_item['r'] = $batch_item_result; - } - } - - public function begin_permissions_mode($permissions_apikey) { - $this->call_as_apikey = $permissions_apikey; - } - - public function end_permissions_mode() { - $this->call_as_apikey = ''; - } - - - /* - * If a page is loaded via HTTPS, then all images and static - * resources need to be printed with HTTPS urls to avoid - * mixed content warnings. If your page loads with an HTTPS - * url, then call set_use_ssl_resources to retrieve the correct - * urls. - */ - public function set_use_ssl_resources($is_ssl = true) { - $this->use_ssl_resources = $is_ssl; - } - - /** - * Returns public information for an application (as shown in the application - * directory) by either application ID, API key, or canvas page name. - * - * @param int $application_id (Optional) app id - * @param string $application_api_key (Optional) api key - * @param string $application_canvas_name (Optional) canvas name - * - * Exactly one argument must be specified, otherwise it is an error. - * - * @return array An array of public information about the application. - */ - public function application_getPublicInfo($application_id=null, - $application_api_key=null, - $application_canvas_name=null) { - return $this->call_method('facebook.application.getPublicInfo', - array('application_id' => $application_id, - 'application_api_key' => $application_api_key, - 'application_canvas_name' => $application_canvas_name)); - } - - /** - * Creates an authentication token to be used as part of the desktop login - * flow. For more information, please see - * http://wiki.developers.facebook.com/index.php/Auth.createToken. - * - * @return string An authentication token. - */ - public function auth_createToken() { - return $this->call_method('facebook.auth.createToken'); - } - - /** - * Returns the session information available after current user logs in. - * - * @param string $auth_token the token returned by auth_createToken or - * passed back to your callback_url. - * @param bool $generate_session_secret whether the session returned should - * include a session secret - * @param string $host_url the connect site URL for which the session is - * being generated. This parameter is optional, unless - * you want Facebook to determine which of several base domains - * to choose from. If this third argument isn't provided but - * there are several base domains, the first base domain is - * chosen. - * - * @return array An assoc array containing session_key, uid - */ - public function auth_getSession($auth_token, - $generate_session_secret = false, - $host_url = null) { - if (!$this->pending_batch()) { - $result = $this->call_method( - 'facebook.auth.getSession', - array('auth_token' => $auth_token, - 'generate_session_secret' => $generate_session_secret, - 'host_url' => $host_url)); - $this->session_key = $result['session_key']; - - if (!empty($result['secret']) && !$generate_session_secret) { - // desktop apps have a special secret - $this->secret = $result['secret']; - } - - return $result; - } - } - - /** - * Generates a session-specific secret. This is for integration with - * client-side API calls, such as the JS library. - * - * @return array A session secret for the current promoted session - * - * @error API_EC_PARAM_SESSION_KEY - * API_EC_PARAM_UNKNOWN - */ - public function auth_promoteSession() { - return $this->call_method('facebook.auth.promoteSession'); - } - - /** - * Expires the session that is currently being used. If this call is - * successful, no further calls to the API (which require a session) can be - * made until a valid session is created. - * - * @return bool true if session expiration was successful, false otherwise - */ - public function auth_expireSession() { - return $this->call_method('facebook.auth.expireSession'); - } - - /** - * Revokes the given extended permission that the user granted at some - * prior time (for instance, offline_access or email). If no user is - * provided, it will be revoked for the user of the current session. - * - * @param string $perm The permission to revoke - * @param int $uid The user for whom to revoke the permission. - */ - public function auth_revokeExtendedPermission($perm, $uid=null) { - return $this->call_method('facebook.auth.revokeExtendedPermission', - array('perm' => $perm, 'uid' => $uid)); - } - - /** - * Revokes the user's agreement to the Facebook Terms of Service for your - * application. If you call this method for one of your users, you will no - * longer be able to make API requests on their behalf until they again - * authorize your application. Use with care. Note that if this method is - * called without a user parameter, then it will revoke access for the - * current session's user. - * - * @param int $uid (Optional) User to revoke - * - * @return bool true if revocation succeeds, false otherwise - */ - public function auth_revokeAuthorization($uid=null) { - return $this->call_method('facebook.auth.revokeAuthorization', - array('uid' => $uid)); - } - - /** - * Get public key that is needed to verify digital signature - * an app may pass to other apps. The public key is only used by - * other apps for verification purposes. - * @param string API key of an app - * @return string The public key for the app. - */ - public function auth_getAppPublicKey($target_app_key) { - return $this->call_method('facebook.auth.getAppPublicKey', - array('target_app_key' => $target_app_key)); - } - - /** - * Get a structure that can be passed to another app - * as proof of session. The other app can verify it using public - * key of this app. - * - * @return signed public session data structure. - */ - public function auth_getSignedPublicSessionData() { - return $this->call_method('facebook.auth.getSignedPublicSessionData', - array()); - } - - /** - * Returns the number of unconnected friends that exist in this application. - * This number is determined based on the accounts registered through - * connect.registerUsers() (see below). - */ - public function connect_getUnconnectedFriendsCount() { - return $this->call_method('facebook.connect.getUnconnectedFriendsCount', - array()); - } - - /** - * This method is used to create an association between an external user - * account and a Facebook user account, as per Facebook Connect. - * - * This method takes an array of account data, including a required email_hash - * and optional account data. For each connected account, if the user exists, - * the information is added to the set of the user's connected accounts. - * If the user has already authorized the site, the connected account is added - * in the confirmed state. If the user has not yet authorized the site, the - * connected account is added in the pending state. - * - * This is designed to help Facebook Connect recognize when two Facebook - * friends are both members of a external site, but perhaps are not aware of - * it. The Connect dialog (see fb:connect-form) is used when friends can be - * identified through these email hashes. See the following url for details: - * - * http://wiki.developers.facebook.com/index.php/Connect.registerUsers - * - * @param mixed $accounts A (JSON-encoded) array of arrays, where each array - * has three properties: - * 'email_hash' (req) - public email hash of account - * 'account_id' (opt) - remote account id; - * 'account_url' (opt) - url to remote account; - * - * @return array The list of email hashes for the successfully registered - * accounts. - */ - public function connect_registerUsers($accounts) { - return $this->call_method('facebook.connect.registerUsers', - array('accounts' => $accounts)); - } - - /** - * Unregisters a set of accounts registered using connect.registerUsers. - * - * @param array $email_hashes The (JSON-encoded) list of email hashes to be - * unregistered. - * - * @return array The list of email hashes which have been successfully - * unregistered. - */ - public function connect_unregisterUsers($email_hashes) { - return $this->call_method('facebook.connect.unregisterUsers', - array('email_hashes' => $email_hashes)); - } - - /** - * Returns events according to the filters specified. - * - * @param int $uid (Optional) User associated with events. A null - * parameter will default to the session user. - * @param array/string $eids (Optional) Filter by these event - * ids. A null parameter will get all events for - * the user. (A csv list will work but is deprecated) - * @param int $start_time (Optional) Filter with this unix time as lower - * bound. A null or zero parameter indicates no - * lower bound. - * @param int $end_time (Optional) Filter with this UTC as upper bound. - * A null or zero parameter indicates no upper - * bound. - * @param string $rsvp_status (Optional) Only show events where the given uid - * has this rsvp status. This only works if you - * have specified a value for $uid. Values are as - * in events.getMembers. Null indicates to ignore - * rsvp status when filtering. - * - * @return array The events matching the query. - */ - public function &events_get($uid=null, - $eids=null, - $start_time=null, - $end_time=null, - $rsvp_status=null) { - return $this->call_method('facebook.events.get', - array('uid' => $uid, - 'eids' => $eids, - 'start_time' => $start_time, - 'end_time' => $end_time, - 'rsvp_status' => $rsvp_status)); - } - - /** - * Returns membership list data associated with an event. - * - * @param int $eid event id - * - * @return array An assoc array of four membership lists, with keys - * 'attending', 'unsure', 'declined', and 'not_replied' - */ - public function &events_getMembers($eid) { - return $this->call_method('facebook.events.getMembers', - array('eid' => $eid)); - } - - /** - * RSVPs the current user to this event. - * - * @param int $eid event id - * @param string $rsvp_status 'attending', 'unsure', or 'declined' - * - * @return bool true if successful - */ - public function &events_rsvp($eid, $rsvp_status) { - return $this->call_method('facebook.events.rsvp', - array( - 'eid' => $eid, - 'rsvp_status' => $rsvp_status)); - } - - /** - * Cancels an event. Only works for events where application is the admin. - * - * @param int $eid event id - * @param string $cancel_message (Optional) message to send to members of - * the event about why it is cancelled - * - * @return bool true if successful - */ - public function &events_cancel($eid, $cancel_message='') { - return $this->call_method('facebook.events.cancel', - array('eid' => $eid, - 'cancel_message' => $cancel_message)); - } - - /** - * Creates an event on behalf of the user is there is a session, otherwise on - * behalf of app. Successful creation guarantees app will be admin. - * - * @param assoc array $event_info json encoded event information - * @param string $file (Optional) filename of picture to set - * - * @return int event id - */ - public function events_create($event_info, $file = null) { - if ($file) { - return $this->call_upload_method('facebook.events.create', - array('event_info' => $event_info), - $file, - $this->photo_server_addr); - } else { - return $this->call_method('facebook.events.create', - array('event_info' => $event_info)); - } - } - - /** - * Invites users to an event. If a session user exists, the session user - * must have permissions to invite friends to the event and $uids must contain - * a list of friend ids. Otherwise, the event must have been - * created by the app and $uids must contain users of the app. - * This method requires the 'create_event' extended permission to - * invite people on behalf of a user. - * - * @param $eid the event id - * @param $uids an array of users to invite - * @param $personal_message a string containing the user's message - * (text only) - * - */ - public function events_invite($eid, $uids, $personal_message) { - return $this->call_method('facebook.events.invite', - array('eid' => $eid, - 'uids' => $uids, - 'personal_message' => $personal_message)); - } - - /** - * Edits an existing event. Only works for events where application is admin. - * - * @param int $eid event id - * @param assoc array $event_info json encoded event information - * @param string $file (Optional) filename of new picture to set - * - * @return bool true if successful - */ - public function events_edit($eid, $event_info, $file = null) { - if ($file) { - return $this->call_upload_method('facebook.events.edit', - array('eid' => $eid, 'event_info' => $event_info), - $file, - $this->photo_server_addr); - } else { - return $this->call_method('facebook.events.edit', - array('eid' => $eid, - 'event_info' => $event_info)); - } - } - - /** - * Fetches and re-caches the image stored at the given URL, for use in images - * published to non-canvas pages via the API (for example, to user profiles - * via profile.setFBML, or to News Feed via feed.publishUserAction). - * - * @param string $url The absolute URL from which to refresh the image. - * - * @return bool true on success - */ - public function &fbml_refreshImgSrc($url) { - return $this->call_method('facebook.fbml.refreshImgSrc', - array('url' => $url)); - } - - /** - * Fetches and re-caches the content stored at the given URL, for use in an - * fb:ref FBML tag. - * - * @param string $url The absolute URL from which to fetch content. This URL - * should be used in a fb:ref FBML tag. - * - * @return bool true on success - */ - public function &fbml_refreshRefUrl($url) { - return $this->call_method('facebook.fbml.refreshRefUrl', - array('url' => $url)); - } - - /** - * Associates a given "handle" with FBML markup so that the handle can be - * used within the fb:ref FBML tag. A handle is unique within an application - * and allows an application to publish identical FBML to many user profiles - * and do subsequent updates without having to republish FBML on behalf of - * each user. - * - * @param string $handle The handle to associate with the given FBML. - * @param string $fbml The FBML to associate with the given handle. - * - * @return bool true on success - */ - public function &fbml_setRefHandle($handle, $fbml) { - return $this->call_method('facebook.fbml.setRefHandle', - array('handle' => $handle, 'fbml' => $fbml)); - } - - /** - * Register custom tags for the application. Custom tags can be used - * to extend the set of tags available to applications in FBML - * markup. - * - * Before you call this function, - * make sure you read the full documentation at - * - * http://wiki.developers.facebook.com/index.php/Fbml.RegisterCustomTags - * - * IMPORTANT: This function overwrites the values of - * existing tags if the names match. Use this function with care because - * it may break the FBML of any application that is using the - * existing version of the tags. - * - * @param mixed $tags an array of tag objects (the full description is on the - * wiki page) - * - * @return int the number of tags that were registered - */ - public function &fbml_registerCustomTags($tags) { - $tags = json_encode($tags); - return $this->call_method('facebook.fbml.registerCustomTags', - array('tags' => $tags)); - } - - /** - * Get the custom tags for an application. If $app_id - * is not specified, the calling app's tags are returned. - * If $app_id is different from the id of the calling app, - * only the app's public tags are returned. - * The return value is an array of the same type as - * the $tags parameter of fbml_registerCustomTags(). - * - * @param int $app_id the application's id (optional) - * - * @return mixed an array containing the custom tag objects - */ - public function &fbml_getCustomTags($app_id = null) { - return $this->call_method('facebook.fbml.getCustomTags', - array('app_id' => $app_id)); - } - - - /** - * Delete custom tags the application has registered. If - * $tag_names is null, all the application's custom tags will be - * deleted. - * - * IMPORTANT: If your application has registered public tags - * that other applications may be using, don't delete those tags! - * Doing so can break the FBML ofapplications that are using them. - * - * @param array $tag_names the names of the tags to delete (optinal) - * @return bool true on success - */ - public function &fbml_deleteCustomTags($tag_names = null) { - return $this->call_method('facebook.fbml.deleteCustomTags', - array('tag_names' => json_encode($tag_names))); - } - - /** - * Gets the best translations for native strings submitted by an application - * for translation. If $locale is not specified, only native strings and their - * descriptions are returned. If $all is true, then unapproved translations - * are returned as well, otherwise only approved translations are returned. - * - * A mapping of locale codes -> language names is available at - * http://wiki.developers.facebook.com/index.php/Facebook_Locales - * - * @param string $locale the locale to get translations for, or 'all' for all - * locales, or 'en_US' for native strings - * @param bool $all whether to return all or only approved translations - * - * @return array (locale, array(native_strings, array('best translation - * available given enough votes or manual approval', approval - * status))) - * @error API_EC_PARAM - * @error API_EC_PARAM_BAD_LOCALE - */ - public function &intl_getTranslations($locale = 'en_US', $all = false) { - return $this->call_method('facebook.intl.getTranslations', - array('locale' => $locale, - 'all' => $all)); - } - - /** - * Lets you insert text strings in their native language into the Facebook - * Translations database so they can be translated. - * - * @param array $native_strings An array of maps, where each map has a 'text' - * field and a 'description' field. - * - * @return int Number of strings uploaded. - */ - public function &intl_uploadNativeStrings($native_strings) { - return $this->call_method('facebook.intl.uploadNativeStrings', - array('native_strings' => json_encode($native_strings))); - } - - /** - * This method is deprecated for calls made on behalf of users. This method - * works only for publishing stories on a Facebook Page that has installed - * your application. To publish stories to a user's profile, use - * feed.publishUserAction instead. - * - * For more details on this call, please visit the wiki page: - * - * http://wiki.developers.facebook.com/index.php/Feed.publishTemplatizedAction - */ - public function &feed_publishTemplatizedAction($title_template, - $title_data, - $body_template, - $body_data, - $body_general, - $image_1=null, - $image_1_link=null, - $image_2=null, - $image_2_link=null, - $image_3=null, - $image_3_link=null, - $image_4=null, - $image_4_link=null, - $target_ids='', - $page_actor_id=null) { - return $this->call_method('facebook.feed.publishTemplatizedAction', - array('title_template' => $title_template, - 'title_data' => $title_data, - 'body_template' => $body_template, - 'body_data' => $body_data, - 'body_general' => $body_general, - 'image_1' => $image_1, - 'image_1_link' => $image_1_link, - 'image_2' => $image_2, - 'image_2_link' => $image_2_link, - 'image_3' => $image_3, - 'image_3_link' => $image_3_link, - 'image_4' => $image_4, - 'image_4_link' => $image_4_link, - 'target_ids' => $target_ids, - 'page_actor_id' => $page_actor_id)); - } - - /** - * Registers a template bundle. Template bundles are somewhat involved, so - * it's recommended you check out the wiki for more details: - * - * http://wiki.developers.facebook.com/index.php/Feed.registerTemplateBundle - * - * @return string A template bundle id - */ - public function &feed_registerTemplateBundle($one_line_story_templates, - $short_story_templates = array(), - $full_story_template = null, - $action_links = array()) { - - $one_line_story_templates = json_encode($one_line_story_templates); - - if (!empty($short_story_templates)) { - $short_story_templates = json_encode($short_story_templates); - } - - if (isset($full_story_template)) { - $full_story_template = json_encode($full_story_template); - } - - if (isset($action_links)) { - $action_links = json_encode($action_links); - } - - return $this->call_method('facebook.feed.registerTemplateBundle', - array('one_line_story_templates' => $one_line_story_templates, - 'short_story_templates' => $short_story_templates, - 'full_story_template' => $full_story_template, - 'action_links' => $action_links)); - } - - /** - * Retrieves the full list of active template bundles registered by the - * requesting application. - * - * @return array An array of template bundles - */ - public function &feed_getRegisteredTemplateBundles() { - return $this->call_method('facebook.feed.getRegisteredTemplateBundles', - array()); - } - - /** - * Retrieves information about a specified template bundle previously - * registered by the requesting application. - * - * @param string $template_bundle_id The template bundle id - * - * @return array Template bundle - */ - public function &feed_getRegisteredTemplateBundleByID($template_bundle_id) { - return $this->call_method('facebook.feed.getRegisteredTemplateBundleByID', - array('template_bundle_id' => $template_bundle_id)); - } - - /** - * Deactivates a previously registered template bundle. - * - * @param string $template_bundle_id The template bundle id - * - * @return bool true on success - */ - public function &feed_deactivateTemplateBundleByID($template_bundle_id) { - return $this->call_method('facebook.feed.deactivateTemplateBundleByID', - array('template_bundle_id' => $template_bundle_id)); - } - - const STORY_SIZE_ONE_LINE = 1; - const STORY_SIZE_SHORT = 2; - const STORY_SIZE_FULL = 4; - - /** - * Publishes a story on behalf of the user owning the session, using the - * specified template bundle. This method requires an active session key in - * order to be called. - * - * The parameters to this method ($templata_data in particular) are somewhat - * involved. It's recommended you visit the wiki for details: - * - * http://wiki.developers.facebook.com/index.php/Feed.publishUserAction - * - * @param int $template_bundle_id A template bundle id previously registered - * @param array $template_data See wiki article for syntax - * @param array $target_ids (Optional) An array of friend uids of the - * user who shared in this action. - * @param string $body_general (Optional) Additional markup that extends - * the body of a short story. - * @param int $story_size (Optional) A story size (see above) - * @param string $user_message (Optional) A user message for a short - * story. - * - * @return bool true on success - */ - public function &feed_publishUserAction( - $template_bundle_id, $template_data, $target_ids='', $body_general='', - $story_size=FacebookRestClient::STORY_SIZE_ONE_LINE, - $user_message='') { - - if (is_array($template_data)) { - $template_data = json_encode($template_data); - } // allow client to either pass in JSON or an assoc that we JSON for them - - if (is_array($target_ids)) { - $target_ids = json_encode($target_ids); - $target_ids = trim($target_ids, "[]"); // we don't want square brackets - } - - return $this->call_method('facebook.feed.publishUserAction', - array('template_bundle_id' => $template_bundle_id, - 'template_data' => $template_data, - 'target_ids' => $target_ids, - 'body_general' => $body_general, - 'story_size' => $story_size, - 'user_message' => $user_message)); - } - - - /** - * Publish a post to the user's stream. - * - * @param $message the user's message - * @param $attachment the post's attachment (optional) - * @param $action links the post's action links (optional) - * @param $target_id the user on whose wall the post will be posted - * (optional) - * @param $uid the actor (defaults to session user) - * @return string the post id - */ - public function stream_publish( - $message, $attachment = null, $action_links = null, $target_id = null, - $uid = null) { - - return $this->call_method( - 'facebook.stream.publish', - array('message' => $message, - 'attachment' => $attachment, - 'action_links' => $action_links, - 'target_id' => $target_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * Remove a post from the user's stream. - * Currently, you may only remove stories you application created. - * - * @param $post_id the post id - * @param $uid the actor (defaults to session user) - * @return bool - */ - public function stream_remove($post_id, $uid = null) { - return $this->call_method( - 'facebook.stream.remove', - array('post_id' => $post_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * Add a comment to a stream post - * - * @param $post_id the post id - * @param $comment the comment text - * @param $uid the actor (defaults to session user) - * @return string the id of the created comment - */ - public function stream_addComment($post_id, $comment, $uid = null) { - return $this->call_method( - 'facebook.stream.addComment', - array('post_id' => $post_id, - 'comment' => $comment, - 'uid' => $this->get_uid($uid))); - } - - - /** - * Remove a comment from a stream post - * - * @param $comment_id the comment id - * @param $uid the actor (defaults to session user) - * @return bool - */ - public function stream_removeComment($comment_id, $uid = null) { - return $this->call_method( - 'facebook.stream.removeComment', - array('comment_id' => $comment_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * Add a like to a stream post - * - * @param $post_id the post id - * @param $uid the actor (defaults to session user) - * @return bool - */ - public function stream_addLike($post_id, $uid = null) { - return $this->call_method( - 'facebook.stream.addLike', - array('post_id' => $post_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * Remove a like from a stream post - * - * @param $post_id the post id - * @param $uid the actor (defaults to session user) - * @return bool - */ - public function stream_removeLike($post_id, $uid = null) { - return $this->call_method( - 'facebook.stream.removeLike', - array('post_id' => $post_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * For the current user, retrieves stories generated by the user's friends - * while using this application. This can be used to easily create a - * "News Feed" like experience. - * - * @return array An array of feed story objects. - */ - public function &feed_getAppFriendStories() { - return $this->call_method('facebook.feed.getAppFriendStories'); - } - - /** - * Makes an FQL query. This is a generalized way of accessing all the data - * in the API, as an alternative to most of the other method calls. More - * info at http://wiki.developers.facebook.com/index.php/FQL - * - * @param string $query the query to evaluate - * - * @return array generalized array representing the results - */ - public function &fql_query($query) { - return $this->call_method('facebook.fql.query', - array('query' => $query)); - } - - /** - * Makes a set of FQL queries in parallel. This method takes a dictionary - * of FQL queries where the keys are names for the queries. Results from - * one query can be used within another query to fetch additional data. More - * info about FQL queries at http://wiki.developers.facebook.com/index.php/FQL - * - * @param string $queries JSON-encoded dictionary of queries to evaluate - * - * @return array generalized array representing the results - */ - public function &fql_multiquery($queries) { - return $this->call_method('facebook.fql.multiquery', - array('queries' => $queries)); - } - - /** - * Returns whether or not pairs of users are friends. - * Note that the Facebook friend relationship is symmetric. - * - * @param array/string $uids1 list of ids (id_1, id_2,...) - * of some length X (csv is deprecated) - * @param array/string $uids2 list of ids (id_A, id_B,...) - * of SAME length X (csv is deprecated) - * - * @return array An array with uid1, uid2, and bool if friends, e.g.: - * array(0 => array('uid1' => id_1, 'uid2' => id_A, 'are_friends' => 1), - * 1 => array('uid1' => id_2, 'uid2' => id_B, 'are_friends' => 0) - * ...) - * @error - * API_EC_PARAM_USER_ID_LIST - */ - public function &friends_areFriends($uids1, $uids2) { - return $this->call_method('facebook.friends.areFriends', - array('uids1' => $uids1, - 'uids2' => $uids2)); - } - - /** - * Returns the friends of the current session user. - * - * @param int $flid (Optional) Only return friends on this friend list. - * @param int $uid (Optional) Return friends for this user. - * - * @return array An array of friends - */ - public function &friends_get($flid=null, $uid = null) { - if (isset($this->friends_list)) { - return $this->friends_list; - } - $params = array(); - if (!$uid && isset($this->canvas_user)) { - $uid = $this->canvas_user; - } - if ($uid) { - $params['uid'] = $uid; - } - if ($flid) { - $params['flid'] = $flid; - } - return $this->call_method('facebook.friends.get', $params); - - } - - /** - * Returns the mutual friends between the target uid and a source uid or - * the current session user. - * - * @param int $target_uid Target uid for which mutual friends will be found. - * @param int $source_uid (optional) Source uid for which mutual friends will - * be found. If no source_uid is specified, - * source_id will default to the session - * user. - * @return array An array of friend uids - */ - public function &friends_getMutualFriends($target_uid, $source_uid = null) { - return $this->call_method('facebook.friends.getMutualFriends', - array("target_uid" => $target_uid, - "source_uid" => $source_uid)); - } - - /** - * Returns the set of friend lists for the current session user. - * - * @return array An array of friend list objects - */ - public function &friends_getLists() { - return $this->call_method('facebook.friends.getLists'); - } - - /** - * Returns the friends of the session user, who are also users - * of the calling application. - * - * @return array An array of friends also using the app - */ - public function &friends_getAppUsers() { - return $this->call_method('facebook.friends.getAppUsers'); - } - - /** - * Returns groups according to the filters specified. - * - * @param int $uid (Optional) User associated with groups. A null - * parameter will default to the session user. - * @param array/string $gids (Optional) Array of group ids to query. A null - * parameter will get all groups for the user. - * (csv is deprecated) - * - * @return array An array of group objects - */ - public function &groups_get($uid, $gids) { - return $this->call_method('facebook.groups.get', - array('uid' => $uid, - 'gids' => $gids)); - } - - /** - * Returns the membership list of a group. - * - * @param int $gid Group id - * - * @return array An array with four membership lists, with keys 'members', - * 'admins', 'officers', and 'not_replied' - */ - public function &groups_getMembers($gid) { - return $this->call_method('facebook.groups.getMembers', - array('gid' => $gid)); - } - - /** - * Returns cookies according to the filters specified. - * - * @param int $uid User for which the cookies are needed. - * @param string $name (Optional) A null parameter will get all cookies - * for the user. - * - * @return array Cookies! Nom nom nom nom nom. - */ - public function data_getCookies($uid, $name) { - return $this->call_method('facebook.data.getCookies', - array('uid' => $uid, - 'name' => $name)); - } - - /** - * Sets cookies according to the params specified. - * - * @param int $uid User for which the cookies are needed. - * @param string $name Name of the cookie - * @param string $value (Optional) if expires specified and is in the past - * @param int $expires (Optional) Expiry time - * @param string $path (Optional) Url path to associate with (default is /) - * - * @return bool true on success - */ - public function data_setCookie($uid, $name, $value, $expires, $path) { - return $this->call_method('facebook.data.setCookie', - array('uid' => $uid, - 'name' => $name, - 'value' => $value, - 'expires' => $expires, - 'path' => $path)); - } - - /** - * Retrieves links posted by the given user. - * - * @param int $uid The user whose links you wish to retrieve - * @param int $limit The maximimum number of links to retrieve - * @param array $link_ids (Optional) Array of specific link - * IDs to retrieve by this user - * - * @return array An array of links. - */ - public function &links_get($uid, $limit, $link_ids = null) { - return $this->call_method('links.get', - array('uid' => $uid, - 'limit' => $limit, - 'link_ids' => $link_ids)); - } - - /** - * Posts a link on Facebook. - * - * @param string $url URL/link you wish to post - * @param string $comment (Optional) A comment about this link - * @param int $uid (Optional) User ID that is posting this link; - * defaults to current session user - * - * @return bool - */ - public function &links_post($url, $comment='', $uid = null) { - return $this->call_method('links.post', - array('uid' => $uid, - 'url' => $url, - 'comment' => $comment)); - } - - /** - * Permissions API - */ - - /** - * Checks API-access granted by self to the specified application. - * - * @param string $permissions_apikey Other application key - * - * @return array API methods/namespaces which are allowed access - */ - public function permissions_checkGrantedApiAccess($permissions_apikey) { - return $this->call_method('facebook.permissions.checkGrantedApiAccess', - array('permissions_apikey' => $permissions_apikey)); - } - - /** - * Checks API-access granted to self by the specified application. - * - * @param string $permissions_apikey Other application key - * - * @return array API methods/namespaces which are allowed access - */ - public function permissions_checkAvailableApiAccess($permissions_apikey) { - return $this->call_method('facebook.permissions.checkAvailableApiAccess', - array('permissions_apikey' => $permissions_apikey)); - } - - /** - * Grant API-access to the specified methods/namespaces to the specified - * application. - * - * @param string $permissions_apikey Other application key - * @param array(string) $method_arr (Optional) API methods/namespaces - * allowed - * - * @return array API methods/namespaces which are allowed access - */ - public function permissions_grantApiAccess($permissions_apikey, $method_arr) { - return $this->call_method('facebook.permissions.grantApiAccess', - array('permissions_apikey' => $permissions_apikey, - 'method_arr' => $method_arr)); - } - - /** - * Revoke API-access granted to the specified application. - * - * @param string $permissions_apikey Other application key - * - * @return bool true on success - */ - public function permissions_revokeApiAccess($permissions_apikey) { - return $this->call_method('facebook.permissions.revokeApiAccess', - array('permissions_apikey' => $permissions_apikey)); - } - - /** - * Payments Order API - */ - - /** - * Set Payments properties for an app. - * - * @param properties a map from property names to values - * @return true on success - */ - public function payments_setProperties($properties) { - return $this->call_method ('facebook.payments.setProperties', - array('properties' => json_encode($properties))); - } - - public function payments_getOrderDetails($order_id) { - return json_decode($this->call_method( - 'facebook.payments.getOrderDetails', - array('order_id' => $order_id)), true); - } - - public function payments_updateOrder($order_id, $status, - $params) { - return $this->call_method('facebook.payments.updateOrder', - array('order_id' => $order_id, - 'status' => $status, - 'params' => json_encode($params))); - } - - public function payments_getOrders($status, $start_time, - $end_time, $test_mode=false) { - return json_decode($this->call_method('facebook.payments.getOrders', - array('status' => $status, - 'start_time' => $start_time, - 'end_time' => $end_time, - 'test_mode' => $test_mode)), true); - } - - /** - * Gifts API - */ - - /** - * Get Gifts associated with an app - * - * @return array of gifts - */ - public function gifts_get() { - return json_decode( - $this->call_method('facebook.gifts.get', - array()), - true - ); - } - - /* - * Update gifts stored by an app - * - * @param array containing gift_id => gift_data to be updated - * @return array containing gift_id => true/false indicating success - * in updating that gift - */ - public function gifts_update($update_array) { - return json_decode( - $this->call_method('facebook.gifts.update', - array('update_str' => json_encode($update_array)) - ), - true - ); - } - - - /** - * Creates a note with the specified title and content. - * - * @param string $title Title of the note. - * @param string $content Content of the note. - * @param int $uid (Optional) The user for whom you are creating a - * note; defaults to current session user - * - * @return int The ID of the note that was just created. - */ - public function ¬es_create($title, $content, $uid = null) { - return $this->call_method('notes.create', - array('uid' => $uid, - 'title' => $title, - 'content' => $content)); - } - - /** - * Deletes the specified note. - * - * @param int $note_id ID of the note you wish to delete - * @param int $uid (Optional) Owner of the note you wish to delete; - * defaults to current session user - * - * @return bool - */ - public function ¬es_delete($note_id, $uid = null) { - return $this->call_method('notes.delete', - array('uid' => $uid, - 'note_id' => $note_id)); - } - - /** - * Edits a note, replacing its title and contents with the title - * and contents specified. - * - * @param int $note_id ID of the note you wish to edit - * @param string $title Replacement title for the note - * @param string $content Replacement content for the note - * @param int $uid (Optional) Owner of the note you wish to edit; - * defaults to current session user - * - * @return bool - */ - public function ¬es_edit($note_id, $title, $content, $uid = null) { - return $this->call_method('notes.edit', - array('uid' => $uid, - 'note_id' => $note_id, - 'title' => $title, - 'content' => $content)); - } - - /** - * Retrieves all notes by a user. If note_ids are specified, - * retrieves only those specific notes by that user. - * - * @param int $uid User whose notes you wish to retrieve - * @param array $note_ids (Optional) List of specific note - * IDs by this user to retrieve - * - * @return array A list of all of the given user's notes, or an empty list - * if the viewer lacks permissions or if there are no visible - * notes. - */ - public function ¬es_get($uid, $note_ids = null) { - return $this->call_method('notes.get', - array('uid' => $uid, - 'note_ids' => $note_ids)); - } - - - /** - * Returns the outstanding notifications for the session user. - * - * @return array An assoc array of notification count objects for - * 'messages', 'pokes' and 'shares', a uid list of - * 'friend_requests', a gid list of 'group_invites', - * and an eid list of 'event_invites' - */ - public function ¬ifications_get() { - return $this->call_method('facebook.notifications.get'); - } - - /** - * Sends a notification to the specified users. - * - * @return A comma separated list of successful recipients - * @error - * API_EC_PARAM_USER_ID_LIST - */ - public function ¬ifications_send($to_ids, $notification, $type) { - return $this->call_method('facebook.notifications.send', - array('to_ids' => $to_ids, - 'notification' => $notification, - 'type' => $type)); - } - - /** - * Sends an email to the specified user of the application. - * - * @param array/string $recipients array of ids of the recipients (csv is deprecated) - * @param string $subject subject of the email - * @param string $text (plain text) body of the email - * @param string $fbml fbml markup for an html version of the email - * - * @return string A comma separated list of successful recipients - * @error - * API_EC_PARAM_USER_ID_LIST - */ - public function ¬ifications_sendEmail($recipients, - $subject, - $text, - $fbml) { - return $this->call_method('facebook.notifications.sendEmail', - array('recipients' => $recipients, - 'subject' => $subject, - 'text' => $text, - 'fbml' => $fbml)); - } - - /** - * Returns the requested info fields for the requested set of pages. - * - * @param array/string $page_ids an array of page ids (csv is deprecated) - * @param array/string $fields an array of strings describing the - * info fields desired (csv is deprecated) - * @param int $uid (Optional) limit results to pages of which this - * user is a fan. - * @param string type limits results to a particular type of page. - * - * @return array An array of pages - */ - public function &pages_getInfo($page_ids, $fields, $uid, $type) { - return $this->call_method('facebook.pages.getInfo', - array('page_ids' => $page_ids, - 'fields' => $fields, - 'uid' => $uid, - 'type' => $type)); - } - - /** - * Returns true if the given user is an admin for the passed page. - * - * @param int $page_id target page id - * @param int $uid (Optional) user id (defaults to the logged-in user) - * - * @return bool true on success - */ - public function &pages_isAdmin($page_id, $uid = null) { - return $this->call_method('facebook.pages.isAdmin', - array('page_id' => $page_id, - 'uid' => $uid)); - } - - /** - * Returns whether or not the given page has added the application. - * - * @param int $page_id target page id - * - * @return bool true on success - */ - public function &pages_isAppAdded($page_id) { - return $this->call_method('facebook.pages.isAppAdded', - array('page_id' => $page_id)); - } - - /** - * Returns true if logged in user is a fan for the passed page. - * - * @param int $page_id target page id - * @param int $uid user to compare. If empty, the logged in user. - * - * @return bool true on success - */ - public function &pages_isFan($page_id, $uid = null) { - return $this->call_method('facebook.pages.isFan', - array('page_id' => $page_id, - 'uid' => $uid)); - } - - /** - * Adds a tag with the given information to a photo. See the wiki for details: - * - * http://wiki.developers.facebook.com/index.php/Photos.addTag - * - * @param int $pid The ID of the photo to be tagged - * @param int $tag_uid The ID of the user being tagged. You must specify - * either the $tag_uid or the $tag_text parameter - * (unless $tags is specified). - * @param string $tag_text Some text identifying the person being tagged. - * You must specify either the $tag_uid or $tag_text - * parameter (unless $tags is specified). - * @param float $x The horizontal position of the tag, as a - * percentage from 0 to 100, from the left of the - * photo. - * @param float $y The vertical position of the tag, as a percentage - * from 0 to 100, from the top of the photo. - * @param array $tags (Optional) An array of maps, where each map - * can contain the tag_uid, tag_text, x, and y - * parameters defined above. If specified, the - * individual arguments are ignored. - * @param int $owner_uid (Optional) The user ID of the user whose photo - * you are tagging. If this parameter is not - * specified, then it defaults to the session user. - * - * @return bool true on success - */ - public function &photos_addTag($pid, - $tag_uid, - $tag_text, - $x, - $y, - $tags, - $owner_uid=0) { - return $this->call_method('facebook.photos.addTag', - array('pid' => $pid, - 'tag_uid' => $tag_uid, - 'tag_text' => $tag_text, - 'x' => $x, - 'y' => $y, - 'tags' => (is_array($tags)) ? json_encode($tags) : null, - 'owner_uid' => $this->get_uid($owner_uid))); - } - - /** - * Creates and returns a new album owned by the specified user or the current - * session user. - * - * @param string $name The name of the album. - * @param string $description (Optional) A description of the album. - * @param string $location (Optional) A description of the location. - * @param string $visible (Optional) A privacy setting for the album. - * One of 'friends', 'friends-of-friends', - * 'networks', or 'everyone'. Default 'everyone'. - * @param int $uid (Optional) User id for creating the album; if - * not specified, the session user is used. - * - * @return array An album object - */ - public function &photos_createAlbum($name, - $description='', - $location='', - $visible='', - $uid=0) { - return $this->call_method('facebook.photos.createAlbum', - array('name' => $name, - 'description' => $description, - 'location' => $location, - 'visible' => $visible, - 'uid' => $this->get_uid($uid))); - } - - /** - * Returns photos according to the filters specified. - * - * @param int $subj_id (Optional) Filter by uid of user tagged in the photos. - * @param int $aid (Optional) Filter by an album, as returned by - * photos_getAlbums. - * @param array/string $pids (Optional) Restrict to an array of pids - * (csv is deprecated) - * - * Note that at least one of these parameters needs to be specified, or an - * error is returned. - * - * @return array An array of photo objects. - */ - public function &photos_get($subj_id, $aid, $pids) { - return $this->call_method('facebook.photos.get', - array('subj_id' => $subj_id, 'aid' => $aid, 'pids' => $pids)); - } - - /** - * Returns the albums created by the given user. - * - * @param int $uid (Optional) The uid of the user whose albums you want. - * A null will return the albums of the session user. - * @param string $aids (Optional) An array of aids to restrict - * the query. (csv is deprecated) - * - * Note that at least one of the (uid, aids) parameters must be specified. - * - * @returns an array of album objects. - */ - public function &photos_getAlbums($uid, $aids) { - return $this->call_method('facebook.photos.getAlbums', - array('uid' => $uid, - 'aids' => $aids)); - } - - /** - * Returns the tags on all photos specified. - * - * @param string $pids A list of pids to query - * - * @return array An array of photo tag objects, which include pid, - * subject uid, and two floating-point numbers (xcoord, ycoord) - * for tag pixel location. - */ - public function &photos_getTags($pids) { - return $this->call_method('facebook.photos.getTags', - array('pids' => $pids)); - } - - /** - * Uploads a photo. - * - * @param string $file The location of the photo on the local filesystem. - * @param int $aid (Optional) The album into which to upload the - * photo. - * @param string $caption (Optional) A caption for the photo. - * @param int uid (Optional) The user ID of the user whose photo you - * are uploading - * - * @return array An array of user objects - */ - public function photos_upload($file, $aid=null, $caption=null, $uid=null) { - return $this->call_upload_method('facebook.photos.upload', - array('aid' => $aid, - 'caption' => $caption, - 'uid' => $uid), - $file); - } - - - /** - * Uploads a video. - * - * @param string $file The location of the video on the local filesystem. - * @param string $title (Optional) A title for the video. Titles over 65 characters in length will be truncated. - * @param string $description (Optional) A description for the video. - * - * @return array An array with the video's ID, title, description, and a link to view it on Facebook. - */ - public function video_upload($file, $title=null, $description=null) { - return $this->call_upload_method('facebook.video.upload', - array('title' => $title, - 'description' => $description), - $file, - Facebook::get_facebook_url('api-video') . '/restserver.php'); - } - - /** - * Returns an array with the video limitations imposed on the current session's - * associated user. Maximum length is measured in seconds; maximum size is - * measured in bytes. - * - * @return array Array with "length" and "size" keys - */ - public function &video_getUploadLimits() { - return $this->call_method('facebook.video.getUploadLimits'); - } - - /** - * Returns the requested info fields for the requested set of users. - * - * @param array/string $uids An array of user ids (csv is deprecated) - * @param array/string $fields An array of info field names desired (csv is deprecated) - * - * @return array An array of user objects - */ - public function &users_getInfo($uids, $fields) { - return $this->call_method('facebook.users.getInfo', - array('uids' => $uids, - 'fields' => $fields)); - } - - /** - * Returns the requested info fields for the requested set of users. A - * session key must not be specified. Only data about users that have - * authorized your application will be returned. - * - * Check the wiki for fields that can be queried through this API call. - * Data returned from here should not be used for rendering to application - * users, use users.getInfo instead, so that proper privacy rules will be - * applied. - * - * @param array/string $uids An array of user ids (csv is deprecated) - * @param array/string $fields An array of info field names desired (csv is deprecated) - * - * @return array An array of user objects - */ - public function &users_getStandardInfo($uids, $fields) { - return $this->call_method('facebook.users.getStandardInfo', - array('uids' => $uids, - 'fields' => $fields)); - } - - /** - * Returns the user corresponding to the current session object. - * - * @return integer User id - */ - public function &users_getLoggedInUser() { - return $this->call_method('facebook.users.getLoggedInUser'); - } - - /** - * Returns 1 if the user has the specified permission, 0 otherwise. - * http://wiki.developers.facebook.com/index.php/Users.hasAppPermission - * - * @return integer 1 or 0 - */ - public function &users_hasAppPermission($ext_perm, $uid=null) { - return $this->call_method('facebook.users.hasAppPermission', - array('ext_perm' => $ext_perm, 'uid' => $uid)); - } - - /** - * Returns whether or not the user corresponding to the current - * session object has the give the app basic authorization. - * - * @return boolean true if the user has authorized the app - */ - public function &users_isAppUser($uid=null) { - if ($uid === null && isset($this->is_user)) { - return $this->is_user; - } - - return $this->call_method('facebook.users.isAppUser', array('uid' => $uid)); - } - - /** - * Returns whether or not the user corresponding to the current - * session object is verified by Facebook. See the documentation - * for Users.isVerified for details. - * - * @return boolean true if the user is verified - */ - public function &users_isVerified() { - return $this->call_method('facebook.users.isVerified'); - } - - /** - * Sets the users' current status message. Message does NOT contain the - * word "is" , so make sure to include a verb. - * - * Example: setStatus("is loving the API!") - * will produce the status "Luke is loving the API!" - * - * @param string $status text-only message to set - * @param int $uid user to set for (defaults to the - * logged-in user) - * @param bool $clear whether or not to clear the status, - * instead of setting it - * @param bool $status_includes_verb if true, the word "is" will *not* be - * prepended to the status message - * - * @return boolean - */ - public function &users_setStatus($status, - $uid = null, - $clear = false, - $status_includes_verb = true) { - $args = array( - 'status' => $status, - 'uid' => $uid, - 'clear' => $clear, - 'status_includes_verb' => $status_includes_verb, - ); - return $this->call_method('facebook.users.setStatus', $args); - } - - /** - * Gets the comments for a particular xid. This is essentially a wrapper - * around the comment FQL table. - * - * @param string $xid external id associated with the comments - * - * @return array of comment objects - */ - public function &comments_get($xid) { - $args = array('xid' => $xid); - return $this->call_method('facebook.comments.get', $args); - } - - /** - * Add a comment to a particular xid on behalf of a user. If called - * without an app_secret (with session secret), this will only work - * for the session user. - * - * @param string $xid external id associated with the comments - * @param string $text text of the comment - * @param int $uid user adding the comment (def: session user) - * @param string $title optional title for the stream story - * @param string $url optional url for the stream story - * @param bool $publish_to_stream publish a feed story about this comment? - * a link will be generated to title/url in the story - * - * @return string comment_id associated with the comment - */ - public function &comments_add($xid, $text, $uid=0, $title='', $url='', - $publish_to_stream=false) { - $args = array( - 'xid' => $xid, - 'uid' => $this->get_uid($uid), - 'text' => $text, - 'title' => $title, - 'url' => $url, - 'publish_to_stream' => $publish_to_stream); - - return $this->call_method('facebook.comments.add', $args); - } - - /** - * Remove a particular comment. - * - * @param string $xid the external id associated with the comments - * @param string $comment_id id of the comment to remove (returned by - * comments.add and comments.get) - * - * @return boolean - */ - public function &comments_remove($xid, $comment_id) { - $args = array( - 'xid' => $xid, - 'comment_id' => $comment_id); - return $this->call_method('facebook.comments.remove', $args); - } - - /** - * Gets the stream on behalf of a user using a set of users. This - * call will return the latest $limit queries between $start_time - * and $end_time. - * - * @param int $viewer_id user making the call (def: session) - * @param array $source_ids users/pages to look at (def: all connections) - * @param int $start_time start time to look for stories (def: 1 day ago) - * @param int $end_time end time to look for stories (def: now) - * @param int $limit number of stories to attempt to fetch (def: 30) - * @param string $filter_key key returned by stream.getFilters to fetch - * @param array $metadata metadata to include with the return, allows - * requested metadata to be returned, such as - * profiles, albums, photo_tags - * - * @return array( - * 'posts' => array of posts, - * // if requested, the following data may be returned - * 'profiles' => array of profile metadata of users/pages in posts - * 'albums' => array of album metadata in posts - * 'photo_tags' => array of photo_tags for photos in posts - * ) - */ - public function &stream_get($viewer_id = null, - $source_ids = null, - $start_time = 0, - $end_time = 0, - $limit = 30, - $filter_key = '', - $exportable_only = false, - $metadata = null, - $post_ids = null) { - $args = array( - 'viewer_id' => $viewer_id, - 'source_ids' => $source_ids, - 'start_time' => $start_time, - 'end_time' => $end_time, - 'limit' => $limit, - 'filter_key' => $filter_key, - 'exportable_only' => $exportable_only, - 'metadata' => $metadata, - 'post_ids' => $post_ids); - return $this->call_method('facebook.stream.get', $args); - } - - /** - * Gets the filters (with relevant filter keys for stream.get) for a - * particular user. These filters are typical things like news feed, - * friend lists, networks. They can be used to filter the stream - * without complex queries to determine which ids belong in which groups. - * - * @param int $uid user to get filters for - * - * @return array of stream filter objects - */ - public function &stream_getFilters($uid = null) { - $args = array('uid' => $uid); - return $this->call_method('facebook.stream.getFilters', $args); - } - - /** - * Gets the full comments given a post_id from stream.get or the - * stream FQL table. Initially, only a set of preview comments are - * returned because some posts can have many comments. - * - * @param string $post_id id of the post to get comments for - * - * @return array of comment objects - */ - public function &stream_getComments($post_id) { - $args = array('post_id' => $post_id); - return $this->call_method('facebook.stream.getComments', $args); - } - - /** - * Sets the FBML for the profile of the user attached to this session. - * - * @param string $markup The FBML that describes the profile - * presence of this app for the user - * @param int $uid The user - * @param string $profile Profile FBML - * @param string $profile_action Profile action FBML (deprecated) - * @param string $mobile_profile Mobile profile FBML - * @param string $profile_main Main Tab profile FBML - * - * @return array A list of strings describing any compile errors for the - * submitted FBML - */ - public function profile_setFBML($markup, - $uid=null, - $profile='', - $profile_action='', - $mobile_profile='', - $profile_main='') { - return $this->call_method('facebook.profile.setFBML', - array('markup' => $markup, - 'uid' => $uid, - 'profile' => $profile, - 'profile_action' => $profile_action, - 'mobile_profile' => $mobile_profile, - 'profile_main' => $profile_main)); - } - - /** - * Gets the FBML for the profile box that is currently set for a user's - * profile (your application set the FBML previously by calling the - * profile.setFBML method). - * - * @param int $uid (Optional) User id to lookup; defaults to session. - * @param int $type (Optional) 1 for original style, 2 for profile_main boxes - * - * @return string The FBML - */ - public function &profile_getFBML($uid=null, $type=null) { - return $this->call_method('facebook.profile.getFBML', - array('uid' => $uid, - 'type' => $type)); - } - - /** - * Returns the specified user's application info section for the calling - * application. These info sections have either been set via a previous - * profile.setInfo call or by the user editing them directly. - * - * @param int $uid (Optional) User id to lookup; defaults to session. - * - * @return array Info fields for the current user. See wiki for structure: - * - * http://wiki.developers.facebook.com/index.php/Profile.getInfo - * - */ - public function &profile_getInfo($uid=null) { - return $this->call_method('facebook.profile.getInfo', - array('uid' => $uid)); - } - - /** - * Returns the options associated with the specified info field for an - * application info section. - * - * @param string $field The title of the field - * - * @return array An array of info options. - */ - public function &profile_getInfoOptions($field) { - return $this->call_method('facebook.profile.getInfoOptions', - array('field' => $field)); - } - - /** - * Configures an application info section that the specified user can install - * on the Info tab of her profile. For details on the structure of an info - * field, please see: - * - * http://wiki.developers.facebook.com/index.php/Profile.setInfo - * - * @param string $title Title / header of the info section - * @param int $type 1 for text-only, 5 for thumbnail views - * @param array $info_fields An array of info fields. See wiki for details. - * @param int $uid (Optional) - * - * @return bool true on success - */ - public function &profile_setInfo($title, $type, $info_fields, $uid=null) { - return $this->call_method('facebook.profile.setInfo', - array('uid' => $uid, - 'type' => $type, - 'title' => $title, - 'info_fields' => json_encode($info_fields))); - } - - /** - * Specifies the objects for a field for an application info section. These - * options populate the typeahead for a thumbnail. - * - * @param string $field The title of the field - * @param array $options An array of items for a thumbnail, including - * 'label', 'link', and optionally 'image', - * 'description' and 'sublabel' - * - * @return bool true on success - */ - public function profile_setInfoOptions($field, $options) { - return $this->call_method('facebook.profile.setInfoOptions', - array('field' => $field, - 'options' => json_encode($options))); - } - - ///////////////////////////////////////////////////////////////////////////// - // Data Store API - - /** - * Set a user preference. - * - * @param pref_id preference identifier (0-200) - * @param value preferece's value - * @param uid the user id (defaults to current session user) - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - * API_EC_PERMISSION_OTHER_USER - */ - public function &data_setUserPreference($pref_id, $value, $uid = null) { - return $this->call_method('facebook.data.setUserPreference', - array('pref_id' => $pref_id, - 'value' => $value, - 'uid' => $this->get_uid($uid))); - } - - /** - * Set a user's all preferences for this application. - * - * @param values preferece values in an associative arrays - * @param replace whether to replace all existing preferences or - * merge into them. - * @param uid the user id (defaults to current session user) - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - * API_EC_PERMISSION_OTHER_USER - */ - public function &data_setUserPreferences($values, - $replace = false, - $uid = null) { - return $this->call_method('facebook.data.setUserPreferences', - array('values' => json_encode($values), - 'replace' => $replace, - 'uid' => $this->get_uid($uid))); - } - - /** - * Get a user preference. - * - * @param pref_id preference identifier (0-200) - * @param uid the user id (defaults to current session user) - * @return preference's value - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - * API_EC_PERMISSION_OTHER_USER - */ - public function &data_getUserPreference($pref_id, $uid = null) { - return $this->call_method('facebook.data.getUserPreference', - array('pref_id' => $pref_id, - 'uid' => $this->get_uid($uid))); - } - - /** - * Get a user preference. - * - * @param uid the user id (defaults to current session user) - * @return preference values - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - * API_EC_PERMISSION_OTHER_USER - */ - public function &data_getUserPreferences($uid = null) { - return $this->call_method('facebook.data.getUserPreferences', - array('uid' => $this->get_uid($uid))); - } - - /** - * Create a new object type. - * - * @param name object type's name - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_createObjectType($name) { - return $this->call_method('facebook.data.createObjectType', - array('name' => $name)); - } - - /** - * Delete an object type. - * - * @param obj_type object type's name - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_dropObjectType($obj_type) { - return $this->call_method('facebook.data.dropObjectType', - array('obj_type' => $obj_type)); - } - - /** - * Rename an object type. - * - * @param obj_type object type's name - * @param new_name new object type's name - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_renameObjectType($obj_type, $new_name) { - return $this->call_method('facebook.data.renameObjectType', - array('obj_type' => $obj_type, - 'new_name' => $new_name)); - } - - /** - * Add a new property to an object type. - * - * @param obj_type object type's name - * @param prop_name name of the property to add - * @param prop_type 1: integer; 2: string; 3: text blob - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_defineObjectProperty($obj_type, - $prop_name, - $prop_type) { - return $this->call_method('facebook.data.defineObjectProperty', - array('obj_type' => $obj_type, - 'prop_name' => $prop_name, - 'prop_type' => $prop_type)); - } - - /** - * Remove a previously defined property from an object type. - * - * @param obj_type object type's name - * @param prop_name name of the property to remove - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_undefineObjectProperty($obj_type, $prop_name) { - return $this->call_method('facebook.data.undefineObjectProperty', - array('obj_type' => $obj_type, - 'prop_name' => $prop_name)); - } - - /** - * Rename a previously defined property of an object type. - * - * @param obj_type object type's name - * @param prop_name name of the property to rename - * @param new_name new name to use - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_renameObjectProperty($obj_type, $prop_name, - $new_name) { - return $this->call_method('facebook.data.renameObjectProperty', - array('obj_type' => $obj_type, - 'prop_name' => $prop_name, - 'new_name' => $new_name)); - } - - /** - * Retrieve a list of all object types that have defined for the application. - * - * @return a list of object type names - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getObjectTypes() { - return $this->call_method('facebook.data.getObjectTypes'); - } - - /** - * Get definitions of all properties of an object type. - * - * @param obj_type object type's name - * @return pairs of property name and property types - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getObjectType($obj_type) { - return $this->call_method('facebook.data.getObjectType', - array('obj_type' => $obj_type)); - } - - /** - * Create a new object. - * - * @param obj_type object type's name - * @param properties (optional) properties to set initially - * @return newly created object's id - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_createObject($obj_type, $properties = null) { - return $this->call_method('facebook.data.createObject', - array('obj_type' => $obj_type, - 'properties' => json_encode($properties))); - } - - /** - * Update an existing object. - * - * @param obj_id object's id - * @param properties new properties - * @param replace true for replacing existing properties; - * false for merging - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_updateObject($obj_id, $properties, $replace = false) { - return $this->call_method('facebook.data.updateObject', - array('obj_id' => $obj_id, - 'properties' => json_encode($properties), - 'replace' => $replace)); - } - - /** - * Delete an existing object. - * - * @param obj_id object's id - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_deleteObject($obj_id) { - return $this->call_method('facebook.data.deleteObject', - array('obj_id' => $obj_id)); - } - - /** - * Delete a list of objects. - * - * @param obj_ids objects to delete - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_deleteObjects($obj_ids) { - return $this->call_method('facebook.data.deleteObjects', - array('obj_ids' => json_encode($obj_ids))); - } - - /** - * Get a single property value of an object. - * - * @param obj_id object's id - * @param prop_name individual property's name - * @return individual property's value - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getObjectProperty($obj_id, $prop_name) { - return $this->call_method('facebook.data.getObjectProperty', - array('obj_id' => $obj_id, - 'prop_name' => $prop_name)); - } - - /** - * Get properties of an object. - * - * @param obj_id object's id - * @param prop_names (optional) properties to return; null for all. - * @return specified properties of an object - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getObject($obj_id, $prop_names = null) { - return $this->call_method('facebook.data.getObject', - array('obj_id' => $obj_id, - 'prop_names' => json_encode($prop_names))); - } - - /** - * Get properties of a list of objects. - * - * @param obj_ids object ids - * @param prop_names (optional) properties to return; null for all. - * @return specified properties of an object - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getObjects($obj_ids, $prop_names = null) { - return $this->call_method('facebook.data.getObjects', - array('obj_ids' => json_encode($obj_ids), - 'prop_names' => json_encode($prop_names))); - } - - /** - * Set a single property value of an object. - * - * @param obj_id object's id - * @param prop_name individual property's name - * @param prop_value new value to set - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_setObjectProperty($obj_id, $prop_name, - $prop_value) { - return $this->call_method('facebook.data.setObjectProperty', - array('obj_id' => $obj_id, - 'prop_name' => $prop_name, - 'prop_value' => $prop_value)); - } - - /** - * Read hash value by key. - * - * @param obj_type object type's name - * @param key hash key - * @param prop_name (optional) individual property's name - * @return hash value - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getHashValue($obj_type, $key, $prop_name = null) { - return $this->call_method('facebook.data.getHashValue', - array('obj_type' => $obj_type, - 'key' => $key, - 'prop_name' => $prop_name)); - } - - /** - * Write hash value by key. - * - * @param obj_type object type's name - * @param key hash key - * @param value hash value - * @param prop_name (optional) individual property's name - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_setHashValue($obj_type, - $key, - $value, - $prop_name = null) { - return $this->call_method('facebook.data.setHashValue', - array('obj_type' => $obj_type, - 'key' => $key, - 'value' => $value, - 'prop_name' => $prop_name)); - } - - /** - * Increase a hash value by specified increment atomically. - * - * @param obj_type object type's name - * @param key hash key - * @param prop_name individual property's name - * @param increment (optional) default is 1 - * @return incremented hash value - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_incHashValue($obj_type, - $key, - $prop_name, - $increment = 1) { - return $this->call_method('facebook.data.incHashValue', - array('obj_type' => $obj_type, - 'key' => $key, - 'prop_name' => $prop_name, - 'increment' => $increment)); - } - - /** - * Remove a hash key and its values. - * - * @param obj_type object type's name - * @param key hash key - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_removeHashKey($obj_type, $key) { - return $this->call_method('facebook.data.removeHashKey', - array('obj_type' => $obj_type, - 'key' => $key)); - } - - /** - * Remove hash keys and their values. - * - * @param obj_type object type's name - * @param keys hash keys - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_removeHashKeys($obj_type, $keys) { - return $this->call_method('facebook.data.removeHashKeys', - array('obj_type' => $obj_type, - 'keys' => json_encode($keys))); - } - - /** - * Define an object association. - * - * @param name name of this association - * @param assoc_type 1: one-way 2: two-way symmetric 3: two-way asymmetric - * @param assoc_info1 needed info about first object type - * @param assoc_info2 needed info about second object type - * @param inverse (optional) name of reverse association - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_defineAssociation($name, $assoc_type, $assoc_info1, - $assoc_info2, $inverse = null) { - return $this->call_method('facebook.data.defineAssociation', - array('name' => $name, - 'assoc_type' => $assoc_type, - 'assoc_info1' => json_encode($assoc_info1), - 'assoc_info2' => json_encode($assoc_info2), - 'inverse' => $inverse)); - } - - /** - * Undefine an object association. - * - * @param name name of this association - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_undefineAssociation($name) { - return $this->call_method('facebook.data.undefineAssociation', - array('name' => $name)); - } - - /** - * Rename an object association or aliases. - * - * @param name name of this association - * @param new_name (optional) new name of this association - * @param new_alias1 (optional) new alias for object type 1 - * @param new_alias2 (optional) new alias for object type 2 - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_ALREADY_EXISTS - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_renameAssociation($name, $new_name, $new_alias1 = null, - $new_alias2 = null) { - return $this->call_method('facebook.data.renameAssociation', - array('name' => $name, - 'new_name' => $new_name, - 'new_alias1' => $new_alias1, - 'new_alias2' => $new_alias2)); - } - - /** - * Get definition of an object association. - * - * @param name name of this association - * @return specified association - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociationDefinition($name) { - return $this->call_method('facebook.data.getAssociationDefinition', - array('name' => $name)); - } - - /** - * Get definition of all associations. - * - * @return all defined associations - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociationDefinitions() { - return $this->call_method('facebook.data.getAssociationDefinitions', - array()); - } - - /** - * Create or modify an association between two objects. - * - * @param name name of association - * @param obj_id1 id of first object - * @param obj_id2 id of second object - * @param data (optional) extra string data to store - * @param assoc_time (optional) extra time data; default to creation time - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_setAssociation($name, $obj_id1, $obj_id2, $data = null, - $assoc_time = null) { - return $this->call_method('facebook.data.setAssociation', - array('name' => $name, - 'obj_id1' => $obj_id1, - 'obj_id2' => $obj_id2, - 'data' => $data, - 'assoc_time' => $assoc_time)); - } - - /** - * Create or modify associations between objects. - * - * @param assocs associations to set - * @param name (optional) name of association - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_setAssociations($assocs, $name = null) { - return $this->call_method('facebook.data.setAssociations', - array('assocs' => json_encode($assocs), - 'name' => $name)); - } - - /** - * Remove an association between two objects. - * - * @param name name of association - * @param obj_id1 id of first object - * @param obj_id2 id of second object - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_removeAssociation($name, $obj_id1, $obj_id2) { - return $this->call_method('facebook.data.removeAssociation', - array('name' => $name, - 'obj_id1' => $obj_id1, - 'obj_id2' => $obj_id2)); - } - - /** - * Remove associations between objects by specifying pairs of object ids. - * - * @param assocs associations to remove - * @param name (optional) name of association - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_removeAssociations($assocs, $name = null) { - return $this->call_method('facebook.data.removeAssociations', - array('assocs' => json_encode($assocs), - 'name' => $name)); - } - - /** - * Remove associations between objects by specifying one object id. - * - * @param name name of association - * @param obj_id who's association to remove - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_removeAssociatedObjects($name, $obj_id) { - return $this->call_method('facebook.data.removeAssociatedObjects', - array('name' => $name, - 'obj_id' => $obj_id)); - } - - /** - * Retrieve a list of associated objects. - * - * @param name name of association - * @param obj_id who's association to retrieve - * @param no_data only return object ids - * @return associated objects - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociatedObjects($name, $obj_id, $no_data = true) { - return $this->call_method('facebook.data.getAssociatedObjects', - array('name' => $name, - 'obj_id' => $obj_id, - 'no_data' => $no_data)); - } - - /** - * Count associated objects. - * - * @param name name of association - * @param obj_id who's association to retrieve - * @return associated object's count - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociatedObjectCount($name, $obj_id) { - return $this->call_method('facebook.data.getAssociatedObjectCount', - array('name' => $name, - 'obj_id' => $obj_id)); - } - - /** - * Get a list of associated object counts. - * - * @param name name of association - * @param obj_ids whose association to retrieve - * @return associated object counts - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_DATA_OBJECT_NOT_FOUND - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_INVALID_OPERATION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociatedObjectCounts($name, $obj_ids) { - return $this->call_method('facebook.data.getAssociatedObjectCounts', - array('name' => $name, - 'obj_ids' => json_encode($obj_ids))); - } - - /** - * Find all associations between two objects. - * - * @param obj_id1 id of first object - * @param obj_id2 id of second object - * @param no_data only return association names without data - * @return all associations between objects - * @error - * API_EC_DATA_DATABASE_ERROR - * API_EC_PARAM - * API_EC_PERMISSION - * API_EC_DATA_QUOTA_EXCEEDED - * API_EC_DATA_UNKNOWN_ERROR - */ - public function &data_getAssociations($obj_id1, $obj_id2, $no_data = true) { - return $this->call_method('facebook.data.getAssociations', - array('obj_id1' => $obj_id1, - 'obj_id2' => $obj_id2, - 'no_data' => $no_data)); - } - - /** - * Get the properties that you have set for an app. - * - * @param properties List of properties names to fetch - * - * @return array A map from property name to value - */ - public function admin_getAppProperties($properties) { - return json_decode( - $this->call_method('facebook.admin.getAppProperties', - array('properties' => json_encode($properties))), true); - } - - /** - * Set properties for an app. - * - * @param properties A map from property names to values - * - * @return bool true on success - */ - public function admin_setAppProperties($properties) { - return $this->call_method('facebook.admin.setAppProperties', - array('properties' => json_encode($properties))); - } - - /** - * Sets href and text for a Live Stream Box xid's via link - * - * @param string $xid xid of the Live Stream - * @param string $via_href Href for the via link - * @param string $via_text Text for the via link - * - * @return boolWhether the set was successful - */ - public function admin_setLiveStreamViaLink($xid, $via_href, $via_text) { - return $this->call_method('facebook.admin.setLiveStreamViaLink', - array('xid' => $xid, - 'via_href' => $via_href, - 'via_text' => $via_text)); - } - - /** - * Gets href and text for a Live Stream Box xid's via link - * - * @param string $xid xid of the Live Stream - * - * @return Array Associative array with keys 'via_href' and 'via_text' - * False if there was an error. - */ - public function admin_getLiveStreamViaLink($xid) { - return $this->call_method('facebook.admin.getLiveStreamViaLink', - array('xid' => $xid)); - } - - /** - * Returns the allocation limit value for a specified integration point name - * Integration point names are defined in lib/api/karma/constants.php in the - * limit_map. - * - * @param string $integration_point_name Name of an integration point - * (see developer wiki for list). - * @param int $uid Specific user to check the limit. - * - * @return int Integration point allocation value - */ - public function &admin_getAllocation($integration_point_name, $uid=null) { - return $this->call_method('facebook.admin.getAllocation', - array('integration_point_name' => $integration_point_name, - 'uid' => $uid)); - } - - /** - * Returns values for the specified metrics for the current application, in - * the given time range. The metrics are collected for fixed-length periods, - * and the times represent midnight at the end of each period. - * - * @param start_time unix time for the start of the range - * @param end_time unix time for the end of the range - * @param period number of seconds in the desired period - * @param metrics list of metrics to look up - * - * @return array A map of the names and values for those metrics - */ - public function &admin_getMetrics($start_time, $end_time, $period, $metrics) { - return $this->call_method('admin.getMetrics', - array('start_time' => $start_time, - 'end_time' => $end_time, - 'period' => $period, - 'metrics' => json_encode($metrics))); - } - - /** - * Sets application restriction info. - * - * Applications can restrict themselves to only a limited user demographic - * based on users' age and/or location or based on static predefined types - * specified by facebook for specifying diff age restriction for diff - * locations. - * - * @param array $restriction_info The age restriction settings to set. - * - * @return bool true on success - */ - public function admin_setRestrictionInfo($restriction_info = null) { - $restriction_str = null; - if (!empty($restriction_info)) { - $restriction_str = json_encode($restriction_info); - } - return $this->call_method('admin.setRestrictionInfo', - array('restriction_str' => $restriction_str)); - } - - /** - * Gets application restriction info. - * - * Applications can restrict themselves to only a limited user demographic - * based on users' age and/or location or based on static predefined types - * specified by facebook for specifying diff age restriction for diff - * locations. - * - * @return array The age restriction settings for this application. - */ - public function admin_getRestrictionInfo() { - return json_decode( - $this->call_method('admin.getRestrictionInfo'), - true); - } - - - /** - * Bans a list of users from the app. Banned users can't - * access the app's canvas page and forums. - * - * @param array $uids an array of user ids - * @return bool true on success - */ - public function admin_banUsers($uids) { - return $this->call_method( - 'admin.banUsers', array('uids' => json_encode($uids))); - } - - /** - * Unban users that have been previously banned with - * admin_banUsers(). - * - * @param array $uids an array of user ids - * @return bool true on success - */ - public function admin_unbanUsers($uids) { - return $this->call_method( - 'admin.unbanUsers', array('uids' => json_encode($uids))); - } - - /** - * Gets the list of users that have been banned from the application. - * $uids is an optional parameter that filters the result with the list - * of provided user ids. If $uids is provided, - * only banned user ids that are contained in $uids are returned. - * - * @param array $uids an array of user ids to filter by - * @return bool true on success - */ - - public function admin_getBannedUsers($uids = null) { - return $this->call_method( - 'admin.getBannedUsers', - array('uids' => $uids ? json_encode($uids) : null)); - } - - - /* UTILITY FUNCTIONS */ - - /** - * Calls the specified normal POST method with the specified parameters. - * - * @param string $method Name of the Facebook method to invoke - * @param array $params A map of param names => param values - * - * @return mixed Result of method call; this returns a reference to support - * 'delayed returns' when in a batch context. - * See: http://wiki.developers.facebook.com/index.php/Using_batching_API - */ - public function &call_method($method, $params = array()) { - if ($this->format) { - $params['format'] = $this->format; - } - if (!$this->pending_batch()) { - if ($this->call_as_apikey) { - $params['call_as_apikey'] = $this->call_as_apikey; - } - $data = $this->post_request($method, $params); - $this->rawData = $data; - $result = $this->convert_result($data, $method, $params); - if (is_array($result) && isset($result['error_code'])) { - throw new FacebookRestClientException($result['error_msg'], - $result['error_code']); - } - } - else { - $result = null; - $batch_item = array('m' => $method, 'p' => $params, 'r' => & $result); - $this->batch_queue[] = $batch_item; - } - - return $result; - } - - protected function convert_result($data, $method, $params) { - $is_xml = (empty($params['format']) || - strtolower($params['format']) != 'json'); - return ($is_xml) ? $this->convert_xml_to_result($data, $method, $params) - : json_decode($data, true); - } - - /** - * Change the response format - * - * @param string $format The response format (json, xml) - */ - public function setFormat($format) { - $this->format = $format; - return $this; - } - - /** - * get the current response serialization format - * - * @return string 'xml', 'json', or null (which means 'xml') - */ - public function getFormat() { - return $this->format; - } - - /** - * Returns the raw JSON or XML output returned by the server in the most - * recent API call. - * - * @return string - */ - public function getRawData() { - return $this->rawData; - } - - /** - * Calls the specified file-upload POST method with the specified parameters - * - * @param string $method Name of the Facebook method to invoke - * @param array $params A map of param names => param values - * @param string $file A path to the file to upload (required) - * - * @return array A dictionary representing the response. - */ - public function call_upload_method($method, $params, $file, $server_addr = null) { - if (!$this->pending_batch()) { - if (!file_exists($file)) { - $code = - FacebookAPIErrorCodes::API_EC_PARAM; - $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; - throw new FacebookRestClientException($description, $code); - } - - if ($this->format) { - $params['format'] = $this->format; - } - $data = $this->post_upload_request($method, - $params, - $file, - $server_addr); - $result = $this->convert_result($data, $method, $params); - - if (is_array($result) && isset($result['error_code'])) { - throw new FacebookRestClientException($result['error_msg'], - $result['error_code']); - } - } - else { - $code = - FacebookAPIErrorCodes::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE; - $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; - throw new FacebookRestClientException($description, $code); - } - - return $result; - } - - protected function convert_xml_to_result($xml, $method, $params) { - $sxml = simplexml_load_string($xml); - $result = self::convert_simplexml_to_array($sxml); - - if (!empty($GLOBALS['facebook_config']['debug'])) { - // output the raw xml and its corresponding php object, for debugging: - print '
'; - $this->cur_id++; - print $this->cur_id . ': Called ' . $method . ', show ' . - 'Params | '. - 'XML | '. - 'SXML | '. - 'PHP'; - print ''; - print ''; - print ''; - print ''; - print '
'; - } - return $result; - } - - protected function finalize_params($method, $params) { - list($get, $post) = $this->add_standard_params($method, $params); - // we need to do this before signing the params - $this->convert_array_values_to_json($post); - $post['sig'] = Facebook::generate_sig(array_merge($get, $post), - $this->secret); - return array($get, $post); - } - - private function convert_array_values_to_json(&$params) { - foreach ($params as $key => &$val) { - if (is_array($val)) { - $val = json_encode($val); - } - } - } - - /** - * Add the generally required params to our request. - * Params method, api_key, and v should be sent over as get. - */ - private function add_standard_params($method, $params) { - $post = $params; - $get = array(); - if ($this->call_as_apikey) { - $get['call_as_apikey'] = $this->call_as_apikey; - } - if ($this->using_session_secret) { - $get['ss'] = '1'; - } - - $get['method'] = $method; - $get['session_key'] = $this->session_key; - $get['api_key'] = $this->api_key; - $post['call_id'] = microtime(true); - if ($post['call_id'] <= $this->last_call_id) { - $post['call_id'] = $this->last_call_id + 0.001; - } - $this->last_call_id = $post['call_id']; - if (isset($post['v'])) { - $get['v'] = $post['v']; - unset($post['v']); - } else { - $get['v'] = '1.0'; - } - if (isset($this->use_ssl_resources)) { - $post['return_ssl_resources'] = (bool) $this->use_ssl_resources; - } - return array($get, $post); - } - - private function create_url_string($params) { - $post_params = array(); - foreach ($params as $key => &$val) { - $post_params[] = $key.'='.urlencode($val); - } - return implode('&', $post_params); - } - - private function run_multipart_http_transaction($method, $params, $file, $server_addr) { - - // the format of this message is specified in RFC1867/RFC1341. - // we add twenty pseudo-random digits to the end of the boundary string. - $boundary = '--------------------------FbMuLtIpArT' . - sprintf("%010d", mt_rand()) . - sprintf("%010d", mt_rand()); - $content_type = 'multipart/form-data; boundary=' . $boundary; - // within the message, we prepend two extra hyphens. - $delimiter = '--' . $boundary; - $close_delimiter = $delimiter . '--'; - $content_lines = array(); - foreach ($params as $key => &$val) { - $content_lines[] = $delimiter; - $content_lines[] = 'Content-Disposition: form-data; name="' . $key . '"'; - $content_lines[] = ''; - $content_lines[] = $val; - } - // now add the file data - $content_lines[] = $delimiter; - $content_lines[] = - 'Content-Disposition: form-data; filename="' . $file . '"'; - $content_lines[] = 'Content-Type: application/octet-stream'; - $content_lines[] = ''; - $content_lines[] = file_get_contents($file); - $content_lines[] = $close_delimiter; - $content_lines[] = ''; - $content = implode("\r\n", $content_lines); - return $this->run_http_post_transaction($content_type, $content, $server_addr); - } - - public function post_request($method, $params) { - list($get, $post) = $this->finalize_params($method, $params); - $post_string = $this->create_url_string($post); - $get_string = $this->create_url_string($get); - $url_with_get = $this->server_addr . '?' . $get_string; - if ($this->use_curl_if_available && function_exists('curl_init')) { - $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url_with_get); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $useragent); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); - curl_setopt($ch, CURLOPT_TIMEOUT, 30); - $result = $this->curl_exec($ch); - curl_close($ch); - } else { - $content_type = 'application/x-www-form-urlencoded'; - $content = $post_string; - $result = $this->run_http_post_transaction($content_type, - $content, - $url_with_get); - } - return $result; - } - - /** - * execute a curl transaction -- this exists mostly so subclasses can add - * extra options and/or process the response, if they wish. - * - * @param resource $ch a curl handle - */ - protected function curl_exec($ch) { - $result = curl_exec($ch); - return $result; - } - - protected function post_upload_request($method, $params, $file, $server_addr = null) { - $server_addr = $server_addr ? $server_addr : $this->server_addr; - list($get, $post) = $this->finalize_params($method, $params); - $get_string = $this->create_url_string($get); - $url_with_get = $server_addr . '?' . $get_string; - if ($this->use_curl_if_available && function_exists('curl_init')) { - // prepending '@' causes cURL to upload the file; the key is ignored. - $post['_file'] = '@' . $file; - $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url_with_get); - // this has to come before the POSTFIELDS set! - curl_setopt($ch, CURLOPT_POST, 1); - // passing an array gets curl to use the multipart/form-data content type - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, $useragent); - $result = $this->curl_exec($ch); - curl_close($ch); - } else { - $result = $this->run_multipart_http_transaction($method, $post, - $file, $url_with_get); - } - return $result; - } - - private function run_http_post_transaction($content_type, $content, $server_addr) { - - $user_agent = 'Facebook API PHP5 Client 1.1 (non-curl) ' . phpversion(); - $content_length = strlen($content); - $context = - array('http' => - array('method' => 'POST', - 'user_agent' => $user_agent, - 'header' => 'Content-Type: ' . $content_type . "\r\n" . - 'Content-Length: ' . $content_length, - 'content' => $content)); - $context_id = stream_context_create($context); - $sock = fopen($server_addr, 'r', false, $context_id); - - $result = ''; - if ($sock) { - while (!feof($sock)) { - $result .= fgets($sock, 4096); - } - fclose($sock); - } - return $result; - } - - public static function convert_simplexml_to_array($sxml) { - $arr = array(); - if ($sxml) { - foreach ($sxml as $k => $v) { - if ($sxml['list']) { - $arr[] = self::convert_simplexml_to_array($v); - } else { - $arr[$k] = self::convert_simplexml_to_array($v); - } - } - } - if (sizeof($arr) > 0) { - return $arr; - } else { - return (string)$sxml; - } - } - - protected function get_uid($uid) { - return $uid ? $uid : $this->user; - } -} - - -class FacebookRestClientException extends Exception { -} - -// Supporting methods and values------ - -/** - * Error codes and descriptions for the Facebook API. - */ - -class FacebookAPIErrorCodes { - - const API_EC_SUCCESS = 0; - - /* - * GENERAL ERRORS - */ - const API_EC_UNKNOWN = 1; - const API_EC_SERVICE = 2; - const API_EC_METHOD = 3; - const API_EC_TOO_MANY_CALLS = 4; - const API_EC_BAD_IP = 5; - const API_EC_HOST_API = 6; - const API_EC_HOST_UP = 7; - const API_EC_SECURE = 8; - const API_EC_RATE = 9; - const API_EC_PERMISSION_DENIED = 10; - const API_EC_DEPRECATED = 11; - const API_EC_VERSION = 12; - const API_EC_INTERNAL_FQL_ERROR = 13; - const API_EC_HOST_PUP = 14; - const API_EC_SESSION_SECRET_NOT_ALLOWED = 15; - const API_EC_HOST_READONLY = 16; - - /* - * PARAMETER ERRORS - */ - const API_EC_PARAM = 100; - const API_EC_PARAM_API_KEY = 101; - const API_EC_PARAM_SESSION_KEY = 102; - const API_EC_PARAM_CALL_ID = 103; - const API_EC_PARAM_SIGNATURE = 104; - const API_EC_PARAM_TOO_MANY = 105; - const API_EC_PARAM_USER_ID = 110; - const API_EC_PARAM_USER_FIELD = 111; - const API_EC_PARAM_SOCIAL_FIELD = 112; - const API_EC_PARAM_EMAIL = 113; - const API_EC_PARAM_USER_ID_LIST = 114; - const API_EC_PARAM_FIELD_LIST = 115; - const API_EC_PARAM_ALBUM_ID = 120; - const API_EC_PARAM_PHOTO_ID = 121; - const API_EC_PARAM_FEED_PRIORITY = 130; - const API_EC_PARAM_CATEGORY = 140; - const API_EC_PARAM_SUBCATEGORY = 141; - const API_EC_PARAM_TITLE = 142; - const API_EC_PARAM_DESCRIPTION = 143; - const API_EC_PARAM_BAD_JSON = 144; - const API_EC_PARAM_BAD_EID = 150; - const API_EC_PARAM_UNKNOWN_CITY = 151; - const API_EC_PARAM_BAD_PAGE_TYPE = 152; - const API_EC_PARAM_BAD_LOCALE = 170; - const API_EC_PARAM_BLOCKED_NOTIFICATION = 180; - - /* - * USER PERMISSIONS ERRORS - */ - const API_EC_PERMISSION = 200; - const API_EC_PERMISSION_USER = 210; - const API_EC_PERMISSION_NO_DEVELOPERS = 211; - const API_EC_PERMISSION_OFFLINE_ACCESS = 212; - const API_EC_PERMISSION_ALBUM = 220; - const API_EC_PERMISSION_PHOTO = 221; - const API_EC_PERMISSION_MESSAGE = 230; - const API_EC_PERMISSION_OTHER_USER = 240; - const API_EC_PERMISSION_STATUS_UPDATE = 250; - const API_EC_PERMISSION_PHOTO_UPLOAD = 260; - const API_EC_PERMISSION_VIDEO_UPLOAD = 261; - const API_EC_PERMISSION_SMS = 270; - const API_EC_PERMISSION_CREATE_LISTING = 280; - const API_EC_PERMISSION_CREATE_NOTE = 281; - const API_EC_PERMISSION_SHARE_ITEM = 282; - const API_EC_PERMISSION_EVENT = 290; - const API_EC_PERMISSION_LARGE_FBML_TEMPLATE = 291; - const API_EC_PERMISSION_LIVEMESSAGE = 292; - const API_EC_PERMISSION_CREATE_EVENT = 296; - const API_EC_PERMISSION_RSVP_EVENT = 299; - - /* - * DATA EDIT ERRORS - */ - const API_EC_EDIT = 300; - const API_EC_EDIT_USER_DATA = 310; - const API_EC_EDIT_PHOTO = 320; - const API_EC_EDIT_ALBUM_SIZE = 321; - const API_EC_EDIT_PHOTO_TAG_SUBJECT = 322; - const API_EC_EDIT_PHOTO_TAG_PHOTO = 323; - const API_EC_EDIT_PHOTO_FILE = 324; - const API_EC_EDIT_PHOTO_PENDING_LIMIT = 325; - const API_EC_EDIT_PHOTO_TAG_LIMIT = 326; - const API_EC_EDIT_ALBUM_REORDER_PHOTO_NOT_IN_ALBUM = 327; - const API_EC_EDIT_ALBUM_REORDER_TOO_FEW_PHOTOS = 328; - - const API_EC_MALFORMED_MARKUP = 329; - const API_EC_EDIT_MARKUP = 330; - - const API_EC_EDIT_FEED_TOO_MANY_USER_CALLS = 340; - const API_EC_EDIT_FEED_TOO_MANY_USER_ACTION_CALLS = 341; - const API_EC_EDIT_FEED_TITLE_LINK = 342; - const API_EC_EDIT_FEED_TITLE_LENGTH = 343; - const API_EC_EDIT_FEED_TITLE_NAME = 344; - const API_EC_EDIT_FEED_TITLE_BLANK = 345; - const API_EC_EDIT_FEED_BODY_LENGTH = 346; - const API_EC_EDIT_FEED_PHOTO_SRC = 347; - const API_EC_EDIT_FEED_PHOTO_LINK = 348; - - const API_EC_EDIT_VIDEO_SIZE = 350; - const API_EC_EDIT_VIDEO_INVALID_FILE = 351; - const API_EC_EDIT_VIDEO_INVALID_TYPE = 352; - const API_EC_EDIT_VIDEO_FILE = 353; - - const API_EC_EDIT_FEED_TITLE_ARRAY = 360; - const API_EC_EDIT_FEED_TITLE_PARAMS = 361; - const API_EC_EDIT_FEED_BODY_ARRAY = 362; - const API_EC_EDIT_FEED_BODY_PARAMS = 363; - const API_EC_EDIT_FEED_PHOTO = 364; - const API_EC_EDIT_FEED_TEMPLATE = 365; - const API_EC_EDIT_FEED_TARGET = 366; - const API_EC_EDIT_FEED_MARKUP = 367; - - /** - * SESSION ERRORS - */ - const API_EC_SESSION_TIMED_OUT = 450; - const API_EC_SESSION_METHOD = 451; - const API_EC_SESSION_INVALID = 452; - const API_EC_SESSION_REQUIRED = 453; - const API_EC_SESSION_REQUIRED_FOR_SECRET = 454; - const API_EC_SESSION_CANNOT_USE_SESSION_SECRET = 455; - - - /** - * FQL ERRORS - */ - const FQL_EC_UNKNOWN_ERROR = 600; - const FQL_EC_PARSER = 601; // backwards compatibility - const FQL_EC_PARSER_ERROR = 601; - const FQL_EC_UNKNOWN_FIELD = 602; - const FQL_EC_UNKNOWN_TABLE = 603; - const FQL_EC_NOT_INDEXABLE = 604; // backwards compatibility - const FQL_EC_NO_INDEX = 604; - const FQL_EC_UNKNOWN_FUNCTION = 605; - const FQL_EC_INVALID_PARAM = 606; - const FQL_EC_INVALID_FIELD = 607; - const FQL_EC_INVALID_SESSION = 608; - const FQL_EC_UNSUPPORTED_APP_TYPE = 609; - const FQL_EC_SESSION_SECRET_NOT_ALLOWED = 610; - const FQL_EC_DEPRECATED_TABLE = 611; - const FQL_EC_EXTENDED_PERMISSION = 612; - const FQL_EC_RATE_LIMIT_EXCEEDED = 613; - const FQL_EC_UNRESOLVED_DEPENDENCY = 614; - const FQL_EC_INVALID_SEARCH = 615; - const FQL_EC_CONTAINS_ERROR = 616; - - const API_EC_REF_SET_FAILED = 700; - - /** - * DATA STORE API ERRORS - */ - const API_EC_DATA_UNKNOWN_ERROR = 800; - const API_EC_DATA_INVALID_OPERATION = 801; - const API_EC_DATA_QUOTA_EXCEEDED = 802; - const API_EC_DATA_OBJECT_NOT_FOUND = 803; - const API_EC_DATA_OBJECT_ALREADY_EXISTS = 804; - const API_EC_DATA_DATABASE_ERROR = 805; - const API_EC_DATA_CREATE_TEMPLATE_ERROR = 806; - const API_EC_DATA_TEMPLATE_EXISTS_ERROR = 807; - const API_EC_DATA_TEMPLATE_HANDLE_TOO_LONG = 808; - const API_EC_DATA_TEMPLATE_HANDLE_ALREADY_IN_USE = 809; - const API_EC_DATA_TOO_MANY_TEMPLATE_BUNDLES = 810; - const API_EC_DATA_MALFORMED_ACTION_LINK = 811; - const API_EC_DATA_TEMPLATE_USES_RESERVED_TOKEN = 812; - - /* - * APPLICATION INFO ERRORS - */ - const API_EC_NO_SUCH_APP = 900; - - /* - * BATCH ERRORS - */ - const API_EC_BATCH_TOO_MANY_ITEMS = 950; - const API_EC_BATCH_ALREADY_STARTED = 951; - const API_EC_BATCH_NOT_STARTED = 952; - const API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE = 953; - - /* - * EVENT API ERRORS - */ - const API_EC_EVENT_INVALID_TIME = 1000; - const API_EC_EVENT_NAME_LOCKED = 1001; - - /* - * INFO BOX ERRORS - */ - const API_EC_INFO_NO_INFORMATION = 1050; - const API_EC_INFO_SET_FAILED = 1051; - - /* - * LIVEMESSAGE API ERRORS - */ - const API_EC_LIVEMESSAGE_SEND_FAILED = 1100; - const API_EC_LIVEMESSAGE_EVENT_NAME_TOO_LONG = 1101; - const API_EC_LIVEMESSAGE_MESSAGE_TOO_LONG = 1102; - - /* - * PAYMENTS API ERRORS - */ - const API_EC_PAYMENTS_UNKNOWN = 1150; - const API_EC_PAYMENTS_APP_INVALID = 1151; - const API_EC_PAYMENTS_DATABASE = 1152; - const API_EC_PAYMENTS_PERMISSION_DENIED = 1153; - const API_EC_PAYMENTS_APP_NO_RESPONSE = 1154; - const API_EC_PAYMENTS_APP_ERROR_RESPONSE = 1155; - const API_EC_PAYMENTS_INVALID_ORDER = 1156; - const API_EC_PAYMENTS_INVALID_PARAM = 1157; - const API_EC_PAYMENTS_INVALID_OPERATION = 1158; - const API_EC_PAYMENTS_PAYMENT_FAILED = 1159; - const API_EC_PAYMENTS_DISABLED = 1160; - - /* - * CONNECT SESSION ERRORS - */ - const API_EC_CONNECT_FEED_DISABLED = 1300; - - /* - * Platform tag bundles errors - */ - const API_EC_TAG_BUNDLE_QUOTA = 1400; - - /* - * SHARE - */ - const API_EC_SHARE_BAD_URL = 1500; - - /* - * NOTES - */ - const API_EC_NOTE_CANNOT_MODIFY = 1600; - - /* - * COMMENTS - */ - const API_EC_COMMENTS_UNKNOWN = 1700; - const API_EC_COMMENTS_POST_TOO_LONG = 1701; - const API_EC_COMMENTS_DB_DOWN = 1702; - const API_EC_COMMENTS_INVALID_XID = 1703; - const API_EC_COMMENTS_INVALID_UID = 1704; - const API_EC_COMMENTS_INVALID_POST = 1705; - const API_EC_COMMENTS_INVALID_REMOVE = 1706; - - /* - * GIFTS - */ - const API_EC_GIFTS_UNKNOWN = 1900; - - /* - * APPLICATION MORATORIUM ERRORS - */ - const API_EC_DISABLED_ALL = 2000; - const API_EC_DISABLED_STATUS = 2001; - const API_EC_DISABLED_FEED_STORIES = 2002; - const API_EC_DISABLED_NOTIFICATIONS = 2003; - const API_EC_DISABLED_REQUESTS = 2004; - const API_EC_DISABLED_EMAIL = 2005; - - /** - * This array is no longer maintained; to view the description of an error - * code, please look at the message element of the API response or visit - * the developer wiki at http://wiki.developers.facebook.com/. - */ - public static $api_error_descriptions = array( - self::API_EC_SUCCESS => 'Success', - self::API_EC_UNKNOWN => 'An unknown error occurred', - self::API_EC_SERVICE => 'Service temporarily unavailable', - self::API_EC_METHOD => 'Unknown method', - self::API_EC_TOO_MANY_CALLS => 'Application request limit reached', - self::API_EC_BAD_IP => 'Unauthorized source IP address', - self::API_EC_PARAM => 'Invalid parameter', - self::API_EC_PARAM_API_KEY => 'Invalid API key', - self::API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid', - self::API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous', - self::API_EC_PARAM_SIGNATURE => 'Incorrect signature', - self::API_EC_PARAM_USER_ID => 'Invalid user id', - self::API_EC_PARAM_USER_FIELD => 'Invalid user info field', - self::API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field', - self::API_EC_PARAM_USER_ID_LIST => 'Invalid user id list', - self::API_EC_PARAM_FIELD_LIST => 'Invalid field list', - self::API_EC_PARAM_ALBUM_ID => 'Invalid album id', - self::API_EC_PARAM_BAD_EID => 'Invalid eid', - self::API_EC_PARAM_UNKNOWN_CITY => 'Unknown city', - self::API_EC_PERMISSION => 'Permissions error', - self::API_EC_PERMISSION_USER => 'User not visible', - self::API_EC_PERMISSION_NO_DEVELOPERS => 'Application has no developers', - self::API_EC_PERMISSION_ALBUM => 'Album not visible', - self::API_EC_PERMISSION_PHOTO => 'Photo not visible', - self::API_EC_PERMISSION_EVENT => 'Creating and modifying events required the extended permission create_event', - self::API_EC_PERMISSION_RSVP_EVENT => 'RSVPing to events required the extended permission rsvp_event', - self::API_EC_EDIT_ALBUM_SIZE => 'Album is full', - self::FQL_EC_PARSER => 'FQL: Parser Error', - self::FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field', - self::FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table', - self::FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable', - self::FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function', - self::FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in', - self::API_EC_DATA_UNKNOWN_ERROR => 'Unknown data store API error', - self::API_EC_DATA_INVALID_OPERATION => 'Invalid operation', - self::API_EC_DATA_QUOTA_EXCEEDED => 'Data store allowable quota was exceeded', - self::API_EC_DATA_OBJECT_NOT_FOUND => 'Specified object cannot be found', - self::API_EC_DATA_OBJECT_ALREADY_EXISTS => 'Specified object already exists', - self::API_EC_DATA_DATABASE_ERROR => 'A database error occurred. Please try again', - self::API_EC_BATCH_ALREADY_STARTED => 'begin_batch already called, please make sure to call end_batch first', - self::API_EC_BATCH_NOT_STARTED => 'end_batch called before begin_batch', - self::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE => 'This method is not allowed in batch mode' - ); -} diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php deleted file mode 100644 index 0cddbddb41..0000000000 --- a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/JSON.php +++ /dev/null @@ -1,806 +0,0 @@ - - * @author Matt Knapp - * @author Brett Stimmerman - * @copyright 2005 Michal Migurski - * @version CVS: $Id: JSON.php,v 1.31 2006/06/28 05:54:17 migurski Exp $ - * @license http://www.opensource.org/licenses/bsd-license.php - * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 - */ - -/** - * Marker constant for Services_JSON::decode(), used to flag stack state - */ -define('SERVICES_JSON_SLICE', 1); - -/** - * Marker constant for Services_JSON::decode(), used to flag stack state - */ -define('SERVICES_JSON_IN_STR', 2); - -/** - * Marker constant for Services_JSON::decode(), used to flag stack state - */ -define('SERVICES_JSON_IN_ARR', 3); - -/** - * Marker constant for Services_JSON::decode(), used to flag stack state - */ -define('SERVICES_JSON_IN_OBJ', 4); - -/** - * Marker constant for Services_JSON::decode(), used to flag stack state - */ -define('SERVICES_JSON_IN_CMT', 5); - -/** - * Behavior switch for Services_JSON::decode() - */ -define('SERVICES_JSON_LOOSE_TYPE', 16); - -/** - * Behavior switch for Services_JSON::decode() - */ -define('SERVICES_JSON_SUPPRESS_ERRORS', 32); - -/** - * Converts to and from JSON format. - * - * Brief example of use: - * - * - * // create a new instance of Services_JSON - * $json = new Services_JSON(); - * - * // convert a complexe value to JSON notation, and send it to the browser - * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4))); - * $output = $json->encode($value); - * - * print($output); - * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]] - * - * // accept incoming POST data, assumed to be in JSON notation - * $input = file_get_contents('php://input', 1000000); - * $value = $json->decode($input); - * - */ -class Services_JSON -{ - /** - * constructs a new JSON instance - * - * @param int $use object behavior flags; combine with boolean-OR - * - * possible values: - * - SERVICES_JSON_LOOSE_TYPE: loose typing. - * "{...}" syntax creates associative arrays - * instead of objects in decode(). - * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression. - * Values which can't be encoded (e.g. resources) - * appear as NULL instead of throwing errors. - * By default, a deeply-nested resource will - * bubble up with an error, so all return values - * from encode() should be checked with isError() - */ - function Services_JSON($use = 0) - { - $this->use = $use; - } - - /** - * convert a string from one UTF-16 char to one UTF-8 char - * - * Normally should be handled by mb_convert_encoding, but - * provides a slower PHP-only method for installations - * that lack the multibye string extension. - * - * @param string $utf16 UTF-16 character - * @return string UTF-8 character - * @access private - */ - function utf162utf8($utf16) - { - // oh please oh please oh please oh please oh please - if(function_exists('mb_convert_encoding')) { - return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); - } - - $bytes = (ord($utf16{0}) << 8) | ord($utf16{1}); - - switch(true) { - case ((0x7F & $bytes) == $bytes): - // this case should never be reached, because we are in ASCII range - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return chr(0x7F & $bytes); - - case (0x07FF & $bytes) == $bytes: - // return a 2-byte UTF-8 character - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return chr(0xC0 | (($bytes >> 6) & 0x1F)) - . chr(0x80 | ($bytes & 0x3F)); - - case (0xFFFF & $bytes) == $bytes: - // return a 3-byte UTF-8 character - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return chr(0xE0 | (($bytes >> 12) & 0x0F)) - . chr(0x80 | (($bytes >> 6) & 0x3F)) - . chr(0x80 | ($bytes & 0x3F)); - } - - // ignoring UTF-32 for now, sorry - return ''; - } - - /** - * convert a string from one UTF-8 char to one UTF-16 char - * - * Normally should be handled by mb_convert_encoding, but - * provides a slower PHP-only method for installations - * that lack the multibye string extension. - * - * @param string $utf8 UTF-8 character - * @return string UTF-16 character - * @access private - */ - function utf82utf16($utf8) - { - // oh please oh please oh please oh please oh please - if(function_exists('mb_convert_encoding')) { - return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); - } - - switch(strlen($utf8)) { - case 1: - // this case should never be reached, because we are in ASCII range - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return $utf8; - - case 2: - // return a UTF-16 character from a 2-byte UTF-8 char - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return chr(0x07 & (ord($utf8{0}) >> 2)) - . chr((0xC0 & (ord($utf8{0}) << 6)) - | (0x3F & ord($utf8{1}))); - - case 3: - // return a UTF-16 character from a 3-byte UTF-8 char - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - return chr((0xF0 & (ord($utf8{0}) << 4)) - | (0x0F & (ord($utf8{1}) >> 2))) - . chr((0xC0 & (ord($utf8{1}) << 6)) - | (0x7F & ord($utf8{2}))); - } - - // ignoring UTF-32 for now, sorry - return ''; - } - - /** - * encodes an arbitrary variable into JSON format - * - * @param mixed $var any number, boolean, string, array, or object to be encoded. - * see argument 1 to Services_JSON() above for array-parsing behavior. - * if var is a strng, note that encode() always expects it - * to be in ASCII or UTF-8 format! - * - * @return mixed JSON string representation of input var or an error if a problem occurs - * @access public - */ - function encode($var) - { - switch (gettype($var)) { - case 'boolean': - return $var ? 'true' : 'false'; - - case 'NULL': - return 'null'; - - case 'integer': - return (int) $var; - - case 'double': - case 'float': - return (float) $var; - - case 'string': - // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT - $ascii = ''; - $strlen_var = strlen($var); - - /* - * Iterate over every character in the string, - * escaping with a slash or encoding to UTF-8 where necessary - */ - for ($c = 0; $c < $strlen_var; ++$c) { - - $ord_var_c = ord($var{$c}); - - switch (true) { - case $ord_var_c == 0x08: - $ascii .= '\b'; - break; - case $ord_var_c == 0x09: - $ascii .= '\t'; - break; - case $ord_var_c == 0x0A: - $ascii .= '\n'; - break; - case $ord_var_c == 0x0C: - $ascii .= '\f'; - break; - case $ord_var_c == 0x0D: - $ascii .= '\r'; - break; - - case $ord_var_c == 0x22: - case $ord_var_c == 0x2F: - case $ord_var_c == 0x5C: - // double quote, slash, slosh - $ascii .= '\\'.$var{$c}; - break; - - case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): - // characters U-00000000 - U-0000007F (same as ASCII) - $ascii .= $var{$c}; - break; - - case (($ord_var_c & 0xE0) == 0xC0): - // characters U-00000080 - U-000007FF, mask 110XXXXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($var{$c + 1})); - $c += 1; - $utf16 = $this->utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xF0) == 0xE0): - // characters U-00000800 - U-0000FFFF, mask 1110XXXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, - ord($var{$c + 1}), - ord($var{$c + 2})); - $c += 2; - $utf16 = $this->utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xF8) == 0xF0): - // characters U-00010000 - U-001FFFFF, mask 11110XXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, - ord($var{$c + 1}), - ord($var{$c + 2}), - ord($var{$c + 3})); - $c += 3; - $utf16 = $this->utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xFC) == 0xF8): - // characters U-00200000 - U-03FFFFFF, mask 111110XX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, - ord($var{$c + 1}), - ord($var{$c + 2}), - ord($var{$c + 3}), - ord($var{$c + 4})); - $c += 4; - $utf16 = $this->utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xFE) == 0xFC): - // characters U-04000000 - U-7FFFFFFF, mask 1111110X - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, - ord($var{$c + 1}), - ord($var{$c + 2}), - ord($var{$c + 3}), - ord($var{$c + 4}), - ord($var{$c + 5})); - $c += 5; - $utf16 = $this->utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - } - } - - return '"'.$ascii.'"'; - - case 'array': - /* - * As per JSON spec if any array key is not an integer - * we must treat the the whole array as an object. We - * also try to catch a sparsely populated associative - * array with numeric keys here because some JS engines - * will create an array with empty indexes up to - * max_index which can cause memory issues and because - * the keys, which may be relevant, will be remapped - * otherwise. - * - * As per the ECMA and JSON specification an object may - * have any string as a property. Unfortunately due to - * a hole in the ECMA specification if the key is a - * ECMA reserved word or starts with a digit the - * parameter is only accessible using ECMAScript's - * bracket notation. - */ - - // treat as a JSON object - if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { - $properties = array_map(array($this, 'name_value'), - array_keys($var), - array_values($var)); - - foreach($properties as $property) { - if(Services_JSON::isError($property)) { - return $property; - } - } - - return '{' . join(',', $properties) . '}'; - } - - // treat it like a regular array - $elements = array_map(array($this, 'encode'), $var); - - foreach($elements as $element) { - if(Services_JSON::isError($element)) { - return $element; - } - } - - return '[' . join(',', $elements) . ']'; - - case 'object': - $vars = get_object_vars($var); - - $properties = array_map(array($this, 'name_value'), - array_keys($vars), - array_values($vars)); - - foreach($properties as $property) { - if(Services_JSON::isError($property)) { - return $property; - } - } - - return '{' . join(',', $properties) . '}'; - - default: - return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS) - ? 'null' - : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string"); - } - } - - /** - * array-walking function for use in generating JSON-formatted name-value pairs - * - * @param string $name name of key to use - * @param mixed $value reference to an array element to be encoded - * - * @return string JSON-formatted name-value pair, like '"name":value' - * @access private - */ - function name_value($name, $value) - { - $encoded_value = $this->encode($value); - - if(Services_JSON::isError($encoded_value)) { - return $encoded_value; - } - - return $this->encode(strval($name)) . ':' . $encoded_value; - } - - /** - * reduce a string by removing leading and trailing comments and whitespace - * - * @param $str string string value to strip of comments and whitespace - * - * @return string string value stripped of comments and whitespace - * @access private - */ - function reduce_string($str) - { - $str = preg_replace(array( - - // eliminate single line comments in '// ...' form - '#^\s*//(.+)$#m', - - // eliminate multi-line comments in '/* ... */' form, at start of string - '#^\s*/\*(.+)\*/#Us', - - // eliminate multi-line comments in '/* ... */' form, at end of string - '#/\*(.+)\*/\s*$#Us' - - ), '', $str); - - // eliminate extraneous space - return trim($str); - } - - /** - * decodes a JSON string into appropriate variable - * - * @param string $str JSON-formatted string - * - * @return mixed number, boolean, string, array, or object - * corresponding to given JSON input string. - * See argument 1 to Services_JSON() above for object-output behavior. - * Note that decode() always returns strings - * in ASCII or UTF-8 format! - * @access public - */ - function decode($str) - { - $str = $this->reduce_string($str); - - switch (strtolower($str)) { - case 'true': - return true; - - case 'false': - return false; - - case 'null': - return null; - - default: - $m = array(); - - if (is_numeric($str)) { - // Lookie-loo, it's a number - - // This would work on its own, but I'm trying to be - // good about returning integers where appropriate: - // return (float)$str; - - // Return float or int, as appropriate - return ((float)$str == (integer)$str) - ? (integer)$str - : (float)$str; - - } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { - // STRINGS RETURNED IN UTF-8 FORMAT - $delim = substr($str, 0, 1); - $chrs = substr($str, 1, -1); - $utf8 = ''; - $strlen_chrs = strlen($chrs); - - for ($c = 0; $c < $strlen_chrs; ++$c) { - - $substr_chrs_c_2 = substr($chrs, $c, 2); - $ord_chrs_c = ord($chrs{$c}); - - switch (true) { - case $substr_chrs_c_2 == '\b': - $utf8 .= chr(0x08); - ++$c; - break; - case $substr_chrs_c_2 == '\t': - $utf8 .= chr(0x09); - ++$c; - break; - case $substr_chrs_c_2 == '\n': - $utf8 .= chr(0x0A); - ++$c; - break; - case $substr_chrs_c_2 == '\f': - $utf8 .= chr(0x0C); - ++$c; - break; - case $substr_chrs_c_2 == '\r': - $utf8 .= chr(0x0D); - ++$c; - break; - - case $substr_chrs_c_2 == '\\"': - case $substr_chrs_c_2 == '\\\'': - case $substr_chrs_c_2 == '\\\\': - case $substr_chrs_c_2 == '\\/': - if (($delim == '"' && $substr_chrs_c_2 != '\\\'') || - ($delim == "'" && $substr_chrs_c_2 != '\\"')) { - $utf8 .= $chrs{++$c}; - } - break; - - case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): - // single, escaped unicode character - $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) - . chr(hexdec(substr($chrs, ($c + 4), 2))); - $utf8 .= $this->utf162utf8($utf16); - $c += 5; - break; - - case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F): - $utf8 .= $chrs{$c}; - break; - - case ($ord_chrs_c & 0xE0) == 0xC0: - // characters U-00000080 - U-000007FF, mask 110XXXXX - //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $utf8 .= substr($chrs, $c, 2); - ++$c; - break; - - case ($ord_chrs_c & 0xF0) == 0xE0: - // characters U-00000800 - U-0000FFFF, mask 1110XXXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $utf8 .= substr($chrs, $c, 3); - $c += 2; - break; - - case ($ord_chrs_c & 0xF8) == 0xF0: - // characters U-00010000 - U-001FFFFF, mask 11110XXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $utf8 .= substr($chrs, $c, 4); - $c += 3; - break; - - case ($ord_chrs_c & 0xFC) == 0xF8: - // characters U-00200000 - U-03FFFFFF, mask 111110XX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $utf8 .= substr($chrs, $c, 5); - $c += 4; - break; - - case ($ord_chrs_c & 0xFE) == 0xFC: - // characters U-04000000 - U-7FFFFFFF, mask 1111110X - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $utf8 .= substr($chrs, $c, 6); - $c += 5; - break; - - } - - } - - return $utf8; - - } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) { - // array, or object notation - - if ($str{0} == '[') { - $stk = array(SERVICES_JSON_IN_ARR); - $arr = array(); - } else { - if ($this->use & SERVICES_JSON_LOOSE_TYPE) { - $stk = array(SERVICES_JSON_IN_OBJ); - $obj = array(); - } else { - $stk = array(SERVICES_JSON_IN_OBJ); - $obj = new stdClass(); - } - } - - array_push($stk, array('what' => SERVICES_JSON_SLICE, - 'where' => 0, - 'delim' => false)); - - $chrs = substr($str, 1, -1); - $chrs = $this->reduce_string($chrs); - - if ($chrs == '') { - if (reset($stk) == SERVICES_JSON_IN_ARR) { - return $arr; - - } else { - return $obj; - - } - } - - //print("\nparsing {$chrs}\n"); - - $strlen_chrs = strlen($chrs); - - for ($c = 0; $c <= $strlen_chrs; ++$c) { - - $top = end($stk); - $substr_chrs_c_2 = substr($chrs, $c, 2); - - if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { - // found a comma that is not inside a string, array, etc., - // OR we've reached the end of the character list - $slice = substr($chrs, $top['where'], ($c - $top['where'])); - array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); - //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); - - if (reset($stk) == SERVICES_JSON_IN_ARR) { - // we are in an array, so just push an element onto the stack - array_push($arr, $this->decode($slice)); - - } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { - // we are in an object, so figure - // out the property name and set an - // element in an associative array, - // for now - $parts = array(); - - if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { - // "name":value pair - $key = $this->decode($parts[1]); - $val = $this->decode($parts[2]); - - if ($this->use & SERVICES_JSON_LOOSE_TYPE) { - $obj[$key] = $val; - } else { - $obj->$key = $val; - } - } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { - // name:value pair, where name is unquoted - $key = $parts[1]; - $val = $this->decode($parts[2]); - - if ($this->use & SERVICES_JSON_LOOSE_TYPE) { - $obj[$key] = $val; - } else { - $obj->$key = $val; - } - } - - } - - } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) { - // found a quote, and we are not inside a string - array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c})); - //print("Found start of string at {$c}\n"); - - } elseif (($chrs{$c} == $top['delim']) && - ($top['what'] == SERVICES_JSON_IN_STR) && - ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { - // found a quote, we're in a string, and it's not escaped - // we know that it's not escaped becase there is _not_ an - // odd number of backslashes at the end of the string so far - array_pop($stk); - //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); - - } elseif (($chrs{$c} == '[') && - in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { - // found a left-bracket, and we are in an array, object, or slice - array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false)); - //print("Found start of array at {$c}\n"); - - } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) { - // found a right-bracket, and we're in an array - array_pop($stk); - //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); - - } elseif (($chrs{$c} == '{') && - in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { - // found a left-brace, and we are in an array, object, or slice - array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false)); - //print("Found start of object at {$c}\n"); - - } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) { - // found a right-brace, and we're in an object - array_pop($stk); - //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); - - } elseif (($substr_chrs_c_2 == '/*') && - in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) { - // found a comment start, and we are in an array, object, or slice - array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false)); - $c++; - //print("Found start of comment at {$c}\n"); - - } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) { - // found a comment end, and we're in one now - array_pop($stk); - $c++; - - for ($i = $top['where']; $i <= $c; ++$i) - $chrs = substr_replace($chrs, ' ', $i, 1); - - //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); - - } - - } - - if (reset($stk) == SERVICES_JSON_IN_ARR) { - return $arr; - - } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) { - return $obj; - - } - - } - } - } - - /** - * @todo Ultimately, this should just call PEAR::isError() - */ - function isError($data, $code = null) - { - if (class_exists('pear')) { - return PEAR::isError($data, $code); - } elseif (is_object($data) && (get_class($data) == 'services_json_error' || - is_subclass_of($data, 'services_json_error'))) { - return true; - } - - return false; - } -} - -if (class_exists('PEAR_Error')) { - - class Services_JSON_Error extends PEAR_Error - { - function Services_JSON_Error($message = 'unknown error', $code = null, - $mode = null, $options = null, $userinfo = null) - { - parent::PEAR_Error($message, $code, $mode, $options, $userinfo); - } - } - -} else { - - /** - * @todo Ultimately, this class shall be descended from PEAR_Error - */ - class Services_JSON_Error - { - function Services_JSON_Error($message = 'unknown error', $code = null, - $mode = null, $options = null, $userinfo = null) - { - - } - } - -} - -?> diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE b/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE deleted file mode 100644 index 4ae6bef55d..0000000000 --- a/plugins/FacebookSSO/extlib/jsonwrapper/JSON/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - -Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED -WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN -NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php deleted file mode 100644 index 29509debad..0000000000 --- a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper.php +++ /dev/null @@ -1,6 +0,0 @@ - diff --git a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php b/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php deleted file mode 100644 index 36a3f28635..0000000000 --- a/plugins/FacebookSSO/extlib/jsonwrapper/jsonwrapper_inner.php +++ /dev/null @@ -1,23 +0,0 @@ -encode($arg); -} - -function json_decode($arg) -{ - global $services_json; - if (!isset($services_json)) { - $services_json = new Services_JSON(); - } - return $services_json->decode($arg); -} - -?> diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookSSO/lib/facebookclient.php index a0549a1d01..cf45c9ed95 100644 --- a/plugins/FacebookSSO/lib/facebookclient.php +++ b/plugins/FacebookSSO/lib/facebookclient.php @@ -47,9 +47,9 @@ class Facebookclient protected $flink = null; // Foreign_link StatusNet -> Facebook protected $notice = null; // The user's notice protected $user = null; // Sender of the notice - protected $oldRestClient = null; // Old REST API client + //protected $oldRestClient = null; // Old REST API client - function __constructor($notice) + function __construct($notice) { $this->facebook = self::getFacebook(); $this->notice = $notice; @@ -58,29 +58,8 @@ class Facebookclient $notice->profile_id, FACEBOOK_SERVICE ); - + $this->user = $this->flink->getUser(); - - $this->oldRestClient = self::getOldRestClient(); - } - - /* - * Get and instance of the old REST API client for sending notices from - * users with Facebook links that pre-exist the Graph API - */ - static function getOldRestClient() - { - $apikey = common_config('facebook', 'apikey'); - $secret = common_config('facebook', 'secret'); - - // If there's no app key and secret set in the local config, look - // for a global one - if (empty($apikey) || empty($secret)) { - $apikey = common_config('facebook', 'global_apikey'); - $secret = common_config('facebook', 'global_secret'); - } - - return new FacebookRestClient($apikey, $secret, null); } /* @@ -125,8 +104,9 @@ class Facebookclient */ static function facebookBroadcastNotice($notice) { + common_debug('Facebook broadcast'); $client = new Facebookclient($notice); - $client->sendNotice(); + return $client->sendNotice(); } /* @@ -191,12 +171,24 @@ class Facebookclient // If there's nothing in the credentials field try to send via // the Old Rest API - if (empty($this->flink->credentials)) { - $this->sendOldRest(); - } else { + if ($this->isFacebookBound()) { + common_debug("notice is facebook bound", __FILE__); + if (empty($this->flink->credentials)) { + $this->sendOldRest(); + } else { - // Otherwise we most likely have an access token - $this->sendGraph(); + // Otherwise we most likely have an access token + $this->sendGraph(); + } + + } else { + common_debug( + sprintf( + "Skipping notice %d - not bound for Facebook", + $this->notice->id, + __FILE__ + ) + ); } } @@ -205,7 +197,55 @@ class Facebookclient */ function sendGraph() { - common_debug("Send notice via Graph API", __FILE__); + try { + + $fbuid = $this->flink->foreign_id; + + common_debug( + sprintf( + "Attempting use Graph API to post notice %d as a stream item for %s (%d), fbuid %s", + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + $params = array( + 'access_token' => $this->flink->credentials, + 'message' => $this->notice->content + ); + + $attachments = $this->notice->attachments(); + + if (!empty($attachments)) { + + // We can only send one attachment with the Graph API + + $first = array_shift($attachments); + + if (substr($first->mimetype, 0, 6) == 'image/' + || in_array( + $first->mimetype, + array('application/x-shockwave-flash', 'audio/mpeg' ))) { + + $params['picture'] = $first->url; + $params['caption'] = 'Click for full size'; + $params['source'] = $first->url; + } + + } + + $result = $this->facebook->api( + sprintf('/%s/feed', $fbuid), 'post', $params + ); + + } catch (FacebookApiException $e) { + return $this->handleFacebookError($e); + } + + return true; } /* @@ -216,40 +256,40 @@ class Facebookclient */ function sendOldRest() { - if (isFacebookBound()) { + try { - try { + $canPublish = $this->checkPermission('publish_stream'); + $canUpdate = $this->checkPermission('status_update'); - $canPublish = $this->checkPermission('publish_stream'); - $canUpdate = $this->checkPermission('status_update'); + // We prefer to use stream.publish, because it can handle + // attachments and returns the ID of the published item - // Post to Facebook - if ($notice->hasAttachments() && $canPublish == 1) { - $this->restPublishStream(); - } elseif ($canUpdate == 1 || $canPublish == 1) { - $this->restStatusUpdate(); - } else { + if ($canPublish == 1) { + $this->restPublishStream(); + } else if ($canUpdate == 1) { + // as a last resort we can just update the user's "status" + $this->restStatusUpdate(); + } else { - $msg = 'Not sending notice %d to Facebook because user %s ' - . '(%d), fbuid %s, does not have \'status_update\' ' - . 'or \'publish_stream\' permission.'; + $msg = 'Not sending notice %d to Facebook because user %s ' + . '(%d), fbuid %s, does not have \'status_update\' ' + . 'or \'publish_stream\' permission.'; - common_log( - LOG_WARNING, - sprintf( - $msg, - $this->notice->id, - $this->user->nickname, - $this->user->id, - $this->flink->foreign_id - ), - __FILE__ - ); - } - - } catch (FacebookRestClientException $e) { - return $this->handleFacebookError($e); + common_log( + LOG_WARNING, + sprintf( + $msg, + $this->notice->id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); } + + } catch (FacebookApiException $e) { + return $this->handleFacebookError($e); } return true; @@ -267,12 +307,11 @@ class Facebookclient */ function checkPermission($permission) { - if (!in_array($permission, array('publish_stream', 'status_update'))) { - throw new ServerExpception("No such permission!"); + throw new ServerException("No such permission!"); } - $fbuid = $this->flink->foreign_link; + $fbuid = $this->flink->foreign_id; common_debug( sprintf( @@ -285,24 +324,14 @@ class Facebookclient __FILE__ ); - // NOTE: $this->oldRestClient->users_hasAppPermission() has been - // returning bogus results, so we're using FQL to check for - // permissions - - $fql = sprintf( - "SELECT %s FROM permissions WHERE uid = %s", - $permission, - $fbuid + $hasPermission = $this->facebook->api( + array( + 'method' => 'users.hasAppPermission', + 'ext_perm' => $permission, + 'uid' => $fbuid + ) ); - $result = $this->oldRestClient->fql_query($fql); - - $hasPermission = 0; - - if (isset($result[0][$permission])) { - $canPublish = $result[0][$permission]; - } - if ($hasPermission == 1) { common_debug( @@ -338,14 +367,27 @@ class Facebookclient return false; } - } + /* + * Handle a Facebook API Exception + * + * @param FacebookApiException $e the exception + * + */ function handleFacebookError($e) { $fbuid = $this->flink->foreign_id; - $code = $e->getCode(); $errmsg = $e->getMessage(); + $code = $e->getCode(); + + // The Facebook PHP SDK seems to always set the code attribute + // of the Exception to 0; they put the real error code it in + // the message. Gar! + if ($code == 0) { + preg_match('/^\(#(?\d+)\)/', $errmsg, $matches); + $code = $matches['code']; + } // XXX: Check for any others? switch($code) { @@ -414,6 +456,14 @@ class Facebookclient } } + /* + * Publish a notice to Facebook as a status update + * + * This is the least preferable way to send a notice to Facebook because + * it doesn't support attachments and the API method doesn't return + * the ID of the post on Facebook. + * + */ function restStatusUpdate() { $fbuid = $this->flink->foreign_id; @@ -429,11 +479,13 @@ class Facebookclient __FILE__ ); - $result = $this->oldRestClient->users_setStatus( - $this->notice->content, - $fbuid, - false, - true + $result = $this->facebook->api( + array( + 'method' => 'users.setStatus', + 'status' => $this->notice->content, + 'status_includes_verb' => true, + 'uid' => $fbuid + ) ); common_log( @@ -447,16 +499,19 @@ class Facebookclient ), __FILE__ ); + } + /* + * Publish a notice to a Facebook user's stream using the old REST API + */ function restPublishStream() { $fbuid = $this->flink->foreign_id; common_debug( sprintf( - 'Attempting to post notice %d as stream item with attachment for ' - . '%s (%d) fbuid %s', + 'Attempting to post notice %d as stream item for %s (%d) fbuid %s', $this->notice->id, $this->user->nickname, $this->user->id, @@ -465,42 +520,52 @@ class Facebookclient __FILE__ ); - $fbattachment = format_attachments($notice->attachments()); + $fbattachment = $this->formatAttachments(); - $this->oldRestClient->stream_publish( - $this->notice->content, - $fbattachment, - null, - null, - $fbuid + $result = $this->facebook->api( + array( + 'method' => 'stream.publish', + 'message' => $this->notice->content, + 'attachment' => $fbattachment, + 'uid' => $fbuid + ) ); common_log( LOG_INFO, sprintf( - 'Posted notice %d as a stream item with attachment for %s ' - . '(%d), fbuid %s', + 'Posted notice %d as a %s for %s (%d), fbuid %s', $this->notice->id, + empty($fbattachment) ? 'stream item' : 'stream item with attachment', $this->user->nickname, $this->user->id, $fbuid ), __FILE__ ); - + } - function format_attachments($attachments) + /* + * Format attachments for the old REST API stream.publish method + * + * Note: Old REST API supports multiple attachments per post + * + */ + function formatAttachments() { + + $attachments = $this->notice->attachments(); + $fbattachment = array(); $fbattachment['media'] = array(); foreach($attachments as $attachment) { if($enclosure = $attachment->getEnclosure()){ - $fbmedia = get_fbmedia_for_attachment($enclosure); + $fbmedia = $this->getFacebookMedia($enclosure); }else{ - $fbmedia = get_fbmedia_for_attachment($attachment); + $fbmedia = $this->getFacebookMedia($attachment); } if($fbmedia){ $fbattachment['media'][]=$fbmedia; @@ -518,9 +583,9 @@ class Facebookclient } /** - * given an File objects, returns an associative array suitable for Facebook media + * given a File objects, returns an associative array suitable for Facebook media */ - function get_fbmedia_for_attachment($attachment) + function getFacebookMedia($attachment) { $fbmedia = array(); @@ -545,9 +610,13 @@ class Facebookclient return $fbmedia; } + /* + * Disconnect a user from Facebook by deleting his Foreign_link. + * Notifies the user his account has been disconnected by email. + */ function disconnect() { - $fbuid = $this->flink->foreign_link; + $fbuid = $this->flink->foreign_id; common_log( LOG_INFO, @@ -560,7 +629,7 @@ class Facebookclient __FILE__ ); - $result = $flink->delete(); + $result = $this->flink->delete(); if (empty($result)) { common_log( @@ -631,7 +700,7 @@ BODY; $this->user->nickname, $siteName ); - + common_switch_locale(); return mail_to_user($this->user, $subject, $body); diff --git a/plugins/FacebookSSO/lib/facebookqueuehandler.php b/plugins/FacebookSSO/lib/facebookqueuehandler.php index af96d35c49..1e82ff01b1 100644 --- a/plugins/FacebookSSO/lib/facebookqueuehandler.php +++ b/plugins/FacebookSSO/lib/facebookqueuehandler.php @@ -31,8 +31,6 @@ if (!defined('STATUSNET')) { exit(1); } -require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php'; - class FacebookQueueHandler extends QueueHandler { function transport() @@ -43,7 +41,7 @@ class FacebookQueueHandler extends QueueHandler function handle($notice) { if ($this->_isLocal($notice)) { - return facebookBroadcastNotice($notice); + return Facebookclient::facebookBroadcastNotice($notice); } return true; } From 17ae690d5937b9a9ac3c7cd8a37d461960ce4964 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 9 Nov 2010 23:14:50 +0000 Subject: [PATCH 10/17] Make a richer StatusNet profile from a user's Facebook profile --- plugins/FacebookSSO/FacebookSSOPlugin.php | 102 +++++++++--------- .../actions/facebookfinishlogin.php | 80 ++++++++++++-- plugins/FacebookSSO/lib/facebookclient.php | 4 +- 3 files changed, 124 insertions(+), 62 deletions(-) diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index a094f2957f..32b6a29106 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -3,7 +3,8 @@ * StatusNet - the distributed open-source microblogging tool * Copyright (C) 2010, StatusNet, Inc. * - * A plugin for single-sign-in (SSO) with Facebook + * A plugin for integrating Facebook with StatusNet. Includes single-sign-on + * and publishing notices to Facebook using Facebook's Graph API. * * PHP version 5 * @@ -35,14 +36,7 @@ if (!defined('STATUSNET')) { define("FACEBOOK_SERVICE", 2); /** - * Main class for Facebook single-sign-on plugin - * - * - * Simple plugins can be implemented as a single module. Others are more complex - * and require additional modules; these should use their own directory, like - * 'local/plugins/{$name}/'. All files related to the plugin, including images, - * JavaScript, CSS, external libraries or PHP modules should go in the plugin - * directory. + * Main class for Facebook plugin * * @category Plugin * @package StatusNet @@ -62,8 +56,7 @@ class FacebookSSOPlugin extends Plugin /** * Initializer for this plugin * - * Plugins overload this method to do any initialization they need, - * like connecting to remote servers or creating paths or so on. + * Gets an instance of the Facebook API client object * * @return boolean hook value; true means continue processing, false means stop. */ @@ -78,33 +71,9 @@ class FacebookSSOPlugin extends Plugin return true; } - /** - * Cleanup for this plugin - * - * Plugins overload this method to do any cleanup they need, - * like disconnecting from remote servers or deleting temp files or so on. - * - * @return boolean hook value; true means continue processing, false means stop. - */ - function cleanup() - { - return true; - } - /** * Load related modules when needed * - * Most non-trivial plugins will require extra modules to do their work. Typically - * these include data classes, action classes, widget classes, or external libraries. - * - * This method receives a class name and loads the PHP file related to that class. By - * tradition, action classes typically have files named for the action, all lower-case. - * Data classes are in files with the data class name, initial letter capitalized. - * - * Note that this method will be called for *all* overloaded classes, not just ones - * in this plugin! So, make sure to return true by default to let other plugins, and - * the core code, get a chance. - * * @param string $cls Name of the class to be loaded * * @return boolean hook value; true means continue processing, false means stop. @@ -118,12 +87,9 @@ class FacebookSSOPlugin extends Plugin switch ($cls) { - case 'Facebook': // New JavaScript SDK + case 'Facebook': // Facebook PHP SDK include_once $dir . '/extlib/facebook.php'; return false; - case 'FacebookRestClient': // Old REST lib - include_once $dir . '/extlib/facebookapi_php5_restlib.php'; - return false; case 'FacebookloginAction': case 'FacebookfinishloginAction': case 'FacebookadminpanelAction': @@ -153,10 +119,8 @@ class FacebookSSOPlugin extends Plugin ); if (in_array(get_class($action), $needy)) { - common_debug("needs scripts!"); return true; } else { - common_debug("doesn't need scripts!"); return false; } } @@ -164,11 +128,6 @@ class FacebookSSOPlugin extends Plugin /** * Map URLs to actions * - * This event handler lets the plugin map URLs on the site to actions (and - * thus an action handler class). Note that the action handler class for an - * action will be named 'FoobarAction', where action = 'foobar'. The class - * must be loaded in the onAutoload() method. - * * @param Net_URL_Mapper $m path-to-action mapper * * @return boolean hook value; true means continue processing, false means stop. @@ -281,6 +240,11 @@ class FacebookSSOPlugin extends Plugin /* * Is there a Facebook application for the plugin to use? + * + * Checks to see if a Facebook application ID and secret + * have been configured and a valid Facebook API client + * object exists. + * */ function hasApplication() { @@ -298,6 +262,12 @@ class FacebookSSOPlugin extends Plugin return false; } + /* + * Output a Facebook div for the Facebook JavaSsript SDK to use + * + * @param Action $action the current action + * + */ function onStartShowHeader($action) { // output
as close to as possible @@ -305,6 +275,12 @@ class FacebookSSOPlugin extends Plugin return true; } + /* + * Load the Facebook JavaScript SDK on pages that need them. + * + * @param Action $action the current action + * + */ function onEndShowScripts($action) { if ($this->needsScripts($action)) { @@ -324,7 +300,7 @@ $('#facebook_button').bind('click', function(event) { } else { // NOP (user cancelled login) } - }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website'}); + }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'}); }); ENDOFSCRIPT; @@ -341,7 +317,7 @@ ENDOFSCRIPT; /* * Log the user out of Facebook, per the Facebook authentication guide * - * @param Action action the action + * @param Action action the current action */ function onEndLogout($action) { @@ -381,10 +357,10 @@ ENDOFSCRIPT; } /* - * Add fbml namespace so Facebook's JavaScript SDK can parse and render - * XFBML tags (e.g: ) + * Add fbml namespace to our HTML, so Facebook's JavaScript SDK can parse + * and render XFBML tags * - * @param Action $action current action + * @param Action $action the current action * @param array $attrs array of attributes for the HTML tag * * @return nothing @@ -432,6 +408,30 @@ ENDOFSCRIPT; return true; } + /* + * Use SSL for Facebook stuff + * + * @param string $action name + * @param boolean $ssl outval to force SSL + * @return mixed hook return value + */ + function onSensitiveAction($action, &$ssl) + { + $sensitive = array( + 'facebookadminpanel', + 'facebooksettings', + 'facebooklogin', + 'facebookfinishlogin' + ); + + if (in_array($action, $sensitive)) { + $ssl = true; + return false; + } else { + return true; + } + } + /* * Add version info for this plugin * diff --git a/plugins/FacebookSSO/actions/facebookfinishlogin.php b/plugins/FacebookSSO/actions/facebookfinishlogin.php index 16f7cff500..e61f351547 100644 --- a/plugins/FacebookSSO/actions/facebookfinishlogin.php +++ b/plugins/FacebookSSO/actions/facebookfinishlogin.php @@ -33,7 +33,6 @@ if (!defined('STATUSNET')) { class FacebookfinishloginAction extends Action { - private $facebook = null; // Facebook client private $fbuid = null; // Facebook user ID private $fbuser = null; // Facebook user object (JSON) @@ -341,12 +340,14 @@ class FacebookfinishloginAction extends Action } $args = array( - 'nickname' => $nickname, - 'fullname' => $this->fbuser['firstname'] . ' ' . $this->fbuser['lastname'], - // XXX: Figure out how to get email - 'homepage' => $this->fbuser['link'], - 'bio' => $this->fbuser['about'], - 'location' => $this->fbuser['location']['name'] + 'nickname' => $nickname, + 'fullname' => $this->fbuser['first_name'] + . ' ' . $this->fbuser['last_name'], + 'email' => $this->fbuser['email'], + 'email_confirmed' => true, + 'homepage' => $this->fbuser['website'], + 'bio' => $this->fbuser['about'], + 'location' => $this->fbuser['location']['name'] ); if (!empty($invite)) { @@ -362,6 +363,8 @@ class FacebookfinishloginAction extends Action return; } + $this->setAvatar($user); + common_set_user($user); common_real_login(true); @@ -384,6 +387,68 @@ class FacebookfinishloginAction extends Action ); } + /* + * Attempt to download the user's Facebook picture and create a + * StatusNet avatar for the new user. + */ + function setAvatar($user) + { + $picUrl = sprintf( + 'http://graph.facebook.com/%s/picture?type=large', + $this->fbuid + ); + + // fetch the picture from Facebook + $client = new HTTPClient(); + + common_debug("status = $status - " . $finalUrl , __FILE__); + + // fetch the actual picture + $response = $client->get($picUrl); + + if ($response->isOk()) { + + $finalUrl = $client->getUrl(); + $filename = 'facebook-' . substr(strrchr($finalUrl, '/'), 1 ); + + common_debug("Filename = " . $filename, __FILE__); + + $ok = file_put_contents( + Avatar::path($filename), + $response->getBody() + ); + + if (!$ok) { + common_log( + LOG_WARNING, + sprintf( + 'Couldn\'t save Facebook avatar %s', + $tmp + ), + __FILE__ + ); + + } else { + + $profile = $user->getProfile(); + + if ($profile->setOriginal($filename)) { + common_log( + LOG_INFO, + sprintf( + 'Saved avatar for %s (%d) from Facebook profile %s, filename = %s', + $user->nickname, + $user->id, + $this->fbuid, + $picture + ), + __FILE__ + ); + } + } + } + } + function connectNewUser() { $nickname = $this->trimmed('nickname'); @@ -437,7 +502,6 @@ class FacebookfinishloginAction extends Action __FILE__ ); - // Return to Facebook connection settings tab common_redirect(common_local_url('facebookfinishlogin'), 303); } diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookSSO/lib/facebookclient.php index cf45c9ed95..6753243ed0 100644 --- a/plugins/FacebookSSO/lib/facebookclient.php +++ b/plugins/FacebookSSO/lib/facebookclient.php @@ -47,7 +47,6 @@ class Facebookclient protected $flink = null; // Foreign_link StatusNet -> Facebook protected $notice = null; // The user's notice protected $user = null; // Sender of the notice - //protected $oldRestClient = null; // Old REST API client function __construct($notice) { @@ -382,7 +381,7 @@ class Facebookclient $code = $e->getCode(); // The Facebook PHP SDK seems to always set the code attribute - // of the Exception to 0; they put the real error code it in + // of the Exception to 0; they put the real error code in // the message. Gar! if ($code == 0) { preg_match('/^\(#(?\d+)\)/', $errmsg, $matches); @@ -554,7 +553,6 @@ class Facebookclient */ function formatAttachments() { - $attachments = $this->notice->attachments(); $fbattachment = array(); From 3c921f38de55922b1de3a331826e01cb876898a2 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 10 Nov 2010 01:18:06 +0000 Subject: [PATCH 11/17] Add an action to handle deauthorization callbacks from Facebook --- plugins/FacebookSSO/FacebookSSOPlugin.php | 6 +- .../actions/facebookdeauthorize.php | 214 ++++++++++++++++++ plugins/FacebookSSO/lib/facebookclient.php | 28 ++- 3 files changed, 235 insertions(+), 13 deletions(-) create mode 100644 plugins/FacebookSSO/actions/facebookdeauthorize.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookSSOPlugin.php index 32b6a29106..19d61211d8 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookSSOPlugin.php @@ -94,6 +94,7 @@ class FacebookSSOPlugin extends Plugin case 'FacebookfinishloginAction': case 'FacebookadminpanelAction': case 'FacebooksettingsAction': + case 'FacebookdeauthorizeAction': include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; case 'Facebookclient': @@ -149,11 +150,14 @@ class FacebookSSOPlugin extends Plugin 'main/facebookfinishlogin', array('action' => 'facebookfinishlogin') ); - $m->connect( 'settings/facebook', array('action' => 'facebooksettings') ); + $m->connect( + 'facebook/deauthorize', + array('action' => 'facebookdeauthorize') + ); } diff --git a/plugins/FacebookSSO/actions/facebookdeauthorize.php b/plugins/FacebookSSO/actions/facebookdeauthorize.php new file mode 100644 index 0000000000..fb4afa13bc --- /dev/null +++ b/plugins/FacebookSSO/actions/facebookdeauthorize.php @@ -0,0 +1,214 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/* + * Action class for handling deauthorize callbacks from Facebook. If the user + * doesn't have a password let her know she'll need to contact the site + * admin to get back into her account (if possible). + */ +class FacebookdeauthorizeAction extends Action +{ + private $facebook; + + /** + * For initializing members of the class. + * + * @param array $args misc. arguments + * + * @return boolean true + */ + function prepare($args) + { + $this->facebook = Facebookclient::getFacebook(); + + return true; + } + + /** + * Handler method + * + * @param array $args is ignored since it's now passed in in prepare() + */ + function handle($args) + { + parent::handle($args); + + $data = $this->facebook->getSignedRequest(); + + if (isset($data['user_id'])) { + + $fbuid = $data['user_id']; + + $flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE); + $user = $flink->getUser(); + + // Remove the link to Facebook + $result = $flink->delete(); + + if (!$result) { + common_log_db_error($flink, 'DELETE', __FILE__); + common_log( + LOG_WARNING, + sprintf( + 'Unable to delete Facebook foreign link ' + . 'for %s (%d), fbuid %s', + $user->nickname, + $user->id, + $fbuid + ), + __FILE__ + ); + return; + } + + common_log( + LOG_INFO, + sprintf( + 'Facebook callback: %s (%d), fbuid %s has deauthorized ' + . 'the Facebook application.', + $user->nickname, + $user->id, + $fbuid + ), + __FILE__ + ); + + // Warn the user about being locked out of their account + // if we can. + if (empty($user->password) && !empty($user->email)) { + $this->emailWarn($user); + } else { + common_log( + LOG_WARNING, + sprintf( + '%s (%d), fbuid $s has deauthorized his/her Facebook ' + . 'connection but hasn\'t set a password so s/he ' + . 'is locked out.', + $user->nickname, + $user->id, + $fbuid + ), + __FILE__ + ); + } + + } else { + if (!empty($data)) { + common_log( + LOG_WARNING, + sprintf( + 'Facebook called the deauthorize callback ' + . ' but didn\'t provide a user ID.' + ), + __FILE__ + ); + } else { + // It probably wasn't Facebook that hit this action, + // so redirect to the login page + common_redirect(common_local_url('login'), 303); + } + } + } + + /* + * Send the user an email warning that their account has been + * disconnected and he/she has no way to login and must contact + * the site administrator for help. + * + * @param User $user the deauthorizing user + * + */ + function emailWarn($user) + { + $profile = $user->getProfile(); + + $siteName = common_config('site', 'name'); + $siteEmail = common_config('site', 'email'); + + if (empty($siteEmail)) { + common_log( + LOG_WARNING, + "No site email address configured. Please set one." + ); + } + + common_switch_locale($user->language); + + $subject = _m('Contact the %s administrator to retrieve your account'); + + $msg = <<nickname, + $siteName, + $siteEmail + ); + + common_switch_locale(); + + if (mail_to_user($user, $subject, $body)) { + common_log( + LOG_INFO, + sprintf( + 'Sent account lockout warning to %s (%d)', + $user->nickname, + $user->id + ), + __FILE__ + ); + } else { + common_log( + LOG_WARNING, + sprintf( + 'Unable to send account lockout warning to %s (%d)', + $user->nickname, + $user->id + ), + __FILE__ + ); + } + } + +} \ No newline at end of file diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookSSO/lib/facebookclient.php index 6753243ed0..cf00b55e3a 100644 --- a/plugins/FacebookSSO/lib/facebookclient.php +++ b/plugins/FacebookSSO/lib/facebookclient.php @@ -673,25 +673,29 @@ class Facebookclient */ function mailFacebookDisconnect() { - $profile = $user->getProfile(); + $profile = $this->user->getProfile(); $siteName = common_config('site', 'name'); - common_switch_locale($user->language); + common_switch_locale($this->user->language); - $subject = sprintf( - _m('Your Facebook connection has been removed'), - $siteName - ); + $subject = _m('Your Facebook connection has been removed'); $msg = << Date: Tue, 16 Nov 2010 02:30:08 +0000 Subject: [PATCH 12/17] - Map notices to Facebook stream items - rename plugin FacebookBridgePlugin - delete/like/unlike notices across the bridge --- ...SSOPlugin.php => FacebookBridgePlugin.php} | 74 +++- .../actions/facebookdeauthorize.php | 6 +- .../actions/facebookfinishlogin.php | 190 ++++++---- plugins/FacebookSSO/actions/facebooklogin.php | 2 +- .../FacebookSSO/classes/Notice_to_item.php | 190 ++++++++++ plugins/FacebookSSO/lib/facebookclient.php | 351 +++++++++++++++++- 6 files changed, 716 insertions(+), 97 deletions(-) rename plugins/FacebookSSO/{FacebookSSOPlugin.php => FacebookBridgePlugin.php} (86%) create mode 100644 plugins/FacebookSSO/classes/Notice_to_item.php diff --git a/plugins/FacebookSSO/FacebookSSOPlugin.php b/plugins/FacebookSSO/FacebookBridgePlugin.php similarity index 86% rename from plugins/FacebookSSO/FacebookSSOPlugin.php rename to plugins/FacebookSSO/FacebookBridgePlugin.php index 19d61211d8..c30ea15440 100644 --- a/plugins/FacebookSSO/FacebookSSOPlugin.php +++ b/plugins/FacebookSSO/FacebookBridgePlugin.php @@ -45,10 +45,9 @@ define("FACEBOOK_SERVICE", 2); * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @link http://status.net/ */ -class FacebookSSOPlugin extends Plugin +class FacebookBridgePlugin extends Plugin { public $appId = null; // Facebook application ID - public $apikey = null; // Facebook API key (for deprecated "Old REST API") public $secret = null; // Facebook application secret public $facebook = null; // Facebook application instance public $dir = null; // Facebook SSO plugin dir @@ -64,7 +63,6 @@ class FacebookSSOPlugin extends Plugin { $this->facebook = Facebookclient::getFacebook( $this->appId, - $this->apikey, $this->secret ); @@ -101,12 +99,32 @@ class FacebookSSOPlugin extends Plugin case 'FacebookQueueHandler': include_once $dir . '/lib/' . strtolower($cls) . '.php'; return false; + case 'Notice_to_item': + include_once $dir . '/classes/' . $cls . '.php'; + return false; default: return true; } } + /** + * Database schema setup + * + * We maintain a table mapping StatusNet notices to Facebook items + * + * @see Schema + * @see ColumnDef + * + * @return boolean hook value; true means continue processing, false means stop. + */ + function onCheckSchema() + { + $schema = Schema::get(); + $schema->ensureTable('notice_to_item', Notice_to_item::schemaDef()); + return true; + } + /* * Does this $action need the Facebook JavaScripts? */ @@ -436,6 +454,54 @@ ENDOFSCRIPT; } } + /** + * If a notice gets deleted, remove the Notice_to_item mapping and + * delete the item on Facebook + * + * @param User $user The user doing the deleting + * @param Notice $notice The notice getting deleted + * + * @return boolean hook value + */ + function onStartDeleteOwnNotice(User $user, Notice $notice) + { + $client = new Facebookclient($notice); + $client->streamRemove(); + + return true; + } + + /** + * Notify remote users when their notices get favorited. + * + * @param Profile or User $profile of local user doing the faving + * @param Notice $notice being favored + * @return hook return value + */ + function onEndFavorNotice(Profile $profile, Notice $notice) + { + $client = new Facebookclient($notice); + $client->like(); + + return true; + } + + /** + * Notify remote users when their notices get de-favorited. + * + * @param Profile $profile Profile person doing the de-faving + * @param Notice $notice Notice being favored + * + * @return hook return value + */ + function onEndDisfavorNotice(Profile $profile, Notice $notice) + { + $client = new Facebookclient($notice); + $client->unLike(); + + return true; + } + /* * Add version info for this plugin * @@ -447,7 +513,7 @@ ENDOFSCRIPT; 'name' => 'Facebook Single-Sign-On', 'version' => STATUSNET_VERSION, 'author' => 'Craig Andrews, Zach Copley', - 'homepage' => 'http://status.net/wiki/Plugin:FacebookSSO', + 'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge', 'rawdescription' => _m('A plugin for integrating StatusNet with Facebook.') ); diff --git a/plugins/FacebookSSO/actions/facebookdeauthorize.php b/plugins/FacebookSSO/actions/facebookdeauthorize.php index fb4afa13bc..cb816fc54a 100644 --- a/plugins/FacebookSSO/actions/facebookdeauthorize.php +++ b/plugins/FacebookSSO/actions/facebookdeauthorize.php @@ -112,7 +112,7 @@ class FacebookdeauthorizeAction extends Action common_log( LOG_WARNING, sprintf( - '%s (%d), fbuid $s has deauthorized his/her Facebook ' + '%s (%d), fbuid %d has deauthorized his/her Facebook ' . 'connection but hasn\'t set a password so s/he ' . 'is locked out.', $user->nickname, @@ -135,8 +135,8 @@ class FacebookdeauthorizeAction extends Action ); } else { // It probably wasn't Facebook that hit this action, - // so redirect to the login page - common_redirect(common_local_url('login'), 303); + // so redirect to the public timeline + common_redirect(common_local_url('public'), 303); } } } diff --git a/plugins/FacebookSSO/actions/facebookfinishlogin.php b/plugins/FacebookSSO/actions/facebookfinishlogin.php index e61f351547..2174c5ad4a 100644 --- a/plugins/FacebookSSO/actions/facebookfinishlogin.php +++ b/plugins/FacebookSSO/actions/facebookfinishlogin.php @@ -97,7 +97,7 @@ class FacebookfinishloginAction extends Action parent::handle($args); if (common_is_real_login()) { - + // User is already logged in, are her accounts already linked? $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE); @@ -121,48 +121,52 @@ class FacebookfinishloginAction extends Action } else { // Possibly reconnect an existing account - + $this->connectUser(); } } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { + $this->handlePost(); + } else { + $this->tryLogin(); + } + } - $token = $this->trimmed('token'); + function handlePost() + { + $token = $this->trimmed('token'); - if (!$token || $token != common_session_token()) { + if (!$token || $token != common_session_token()) { + $this->showForm( + _m('There was a problem with your session token. Try again, please.') + ); + return; + } + + if ($this->arg('create')) { + + if (!$this->boolean('license')) { $this->showForm( - _m('There was a problem with your session token. Try again, please.')); + _m('You can\'t register if you don\'t agree to the license.'), + $this->trimmed('newname') + ); return; } - if ($this->arg('create')) { + // We has a valid Facebook session and the Facebook user has + // agreed to the SN license, so create a new user + $this->createNewUser(); - if (!$this->boolean('license')) { - $this->showForm( - _m('You can\'t register if you don\'t agree to the license.'), - $this->trimmed('newname') - ); - return; - } + } else if ($this->arg('connect')) { - // We has a valid Facebook session and the Facebook user has - // agreed to the SN license, so create a new user - $this->createNewUser(); + $this->connectNewUser(); - } else if ($this->arg('connect')) { - - $this->connectNewUser(); - - } else { - - $this->showForm( - _m('An unknown error has occured.'), - $this->trimmed('newname') - ); - } } else { - $this->tryLogin(); + $this->showForm( + _m('An unknown error has occured.'), + $this->trimmed('newname') + ); } } @@ -173,7 +177,7 @@ class FacebookfinishloginAction extends Action $this->element('div', array('class' => 'error'), $this->error); } else { - + $this->element( 'div', 'instructions', // TRANS: %s is the site name. @@ -343,19 +347,23 @@ class FacebookfinishloginAction extends Action 'nickname' => $nickname, 'fullname' => $this->fbuser['first_name'] . ' ' . $this->fbuser['last_name'], - 'email' => $this->fbuser['email'], - 'email_confirmed' => true, 'homepage' => $this->fbuser['website'], 'bio' => $this->fbuser['about'], 'location' => $this->fbuser['location']['name'] ); + // It's possible that the email address is already in our + // DB. It's a unique key, so we need to check + if ($this->isNewEmail($this->fbuser['email'])) { + $args['email'] = $this->fbuser['email']; + $args['email_confirmed'] = true; + } + if (!empty($invite)) { $args['code'] = $invite->code; } - $user = User::register($args); - + $user = User::register($args); $result = $this->flinkUser($user->id, $this->fbuid); if (!$result) { @@ -363,6 +371,9 @@ class FacebookfinishloginAction extends Action return; } + // Add a Foreign_user record + Facebookclient::addFacebookUser($this->fbuser); + $this->setAvatar($user); common_set_user($user); @@ -371,20 +382,16 @@ class FacebookfinishloginAction extends Action common_log( LOG_INFO, sprintf( - 'Registered new user %d from Facebook user %s', + 'Registered new user %s (%d) from Facebook user %s, (fbuid %d)', + $user->nickname, $user->id, + $this->fbuser['name'], $this->fbuid ), __FILE__ ); - common_redirect( - common_local_url( - 'showstream', - array('nickname' => $user->nickname) - ), - 303 - ); + $this->goHome($user->nickname); } /* @@ -401,17 +408,19 @@ class FacebookfinishloginAction extends Action // fetch the picture from Facebook $client = new HTTPClient(); - common_debug("status = $status - " . $finalUrl , __FILE__); - // fetch the actual picture $response = $client->get($picUrl); if ($response->isOk()) { $finalUrl = $client->getUrl(); - $filename = 'facebook-' . substr(strrchr($finalUrl, '/'), 1 ); - common_debug("Filename = " . $filename, __FILE__); + // Make sure the filename is unique becuase it's possible for a user + // to deauthorize our app, and then come back in as a new user but + // have the same Facebook picture (avatar URLs have a unique index + // and their URLs are based on the filenames). + $filename = 'facebook-' . common_good_rand(4) . '-' + . substr(strrchr($finalUrl, '/'), 1); $ok = file_put_contents( Avatar::path($filename), @@ -430,17 +439,20 @@ class FacebookfinishloginAction extends Action } else { + // save it as an avatar $profile = $user->getProfile(); if ($profile->setOriginal($filename)) { common_log( LOG_INFO, sprintf( - 'Saved avatar for %s (%d) from Facebook profile %s, filename = %s', + 'Saved avatar for %s (%d) from Facebook picture for ' + . '%s (fbuid %d), filename = %s', $user->nickname, $user->id, + $this->fbuser['name'], $this->fbuid, - $picture + $filename ), __FILE__ ); @@ -462,19 +474,17 @@ class FacebookfinishloginAction extends Action $user = User::staticGet('nickname', $nickname); if (!empty($user)) { - common_debug('Facebook Connect Plugin - ' . - "Legit user to connect to Facebook: $nickname"); + common_debug( + sprintf( + 'Found a legit user to connect to Facebook: %s (%d)', + $user->nickname, + $user->id + ), + __FILE__ + ); } - $result = $this->flinkUser($user->id, $this->fbuid); - - if (!$result) { - $this->serverError(_m('Error connecting user to Facebook.')); - return; - } - - common_debug('Facebook Connnect Plugin - ' . - "Connected Facebook user $this->fbuid to local user $user->id"); + $this->tryLinkUser($user); common_set_user($user); common_real_login(true); @@ -485,7 +495,12 @@ class FacebookfinishloginAction extends Action function connectUser() { $user = common_current_user(); + $this->tryLinkUser($user); + common_redirect(common_local_url('facebookfinishlogin'), 303); + } + function tryLinkUser($user) + { $result = $this->flinkUser($user->id, $this->fbuid); if (empty($result)) { @@ -495,14 +510,14 @@ class FacebookfinishloginAction extends Action common_debug( sprintf( - 'Connected Facebook user %s to local user %d', + 'Connected Facebook user %s (fbuid %d) to local user %s (%d)', + $this->fbuser['name'], $this->fbuid, + $user->nickname, $user->id ), __FILE__ ); - - common_redirect(common_local_url('facebookfinishlogin'), 303); } function tryLogin() @@ -573,7 +588,7 @@ class FacebookfinishloginAction extends Action $flink->user_id = $user_id; $flink->foreign_id = $fbuid; $flink->service = FACEBOOK_SERVICE; - + // Pull the access token from the Facebook cookies $flink->credentials = $this->facebook->getAccessToken(); @@ -595,8 +610,8 @@ class FacebookfinishloginAction extends Action // Try the full name - $fullname = trim($this->fbuser['firstname'] . - ' ' . $this->fbuser['lastname']); + $fullname = trim($this->fbuser['first_name'] . + ' ' . $this->fbuser['last_name']); if (!empty($fullname)) { $fullname = $this->nicknamize($fullname); @@ -617,20 +632,57 @@ class FacebookfinishloginAction extends Action return strtolower($str); } - function isNewNickname($str) - { - if (!Validate::string($str, array('min_length' => 1, - 'max_length' => 64, - 'format' => NICKNAME_FMT))) { + /* + * Is the desired nickname already taken? + * + * @return boolean result + */ + function isNewNickname($str) + { + if ( + !Validate::string( + $str, + array( + 'min_length' => 1, + 'max_length' => 64, + 'format' => NICKNAME_FMT + ) + ) + ) { return false; } + if (!User::allowed_nickname($str)) { return false; } + if (User::staticGet('nickname', $str)) { return false; } + return true; } + /* + * Do we already have a user record with this email? + * (emails have to be unique but they can change) + * + * @param string $email the email address to check + * + * @return boolean result + */ + function isNewEmail($email) + { + // we shouldn't have to validate the format + $result = User::staticGet('email', $email); + + if (empty($result)) { + common_debug("XXXXXXXXXXXXXXXXXX We've never seen this email before!!!"); + return true; + } + common_debug("XXXXXXXXXXXXXXXXXX dupe email address!!!!"); + + return false; + } + } diff --git a/plugins/FacebookSSO/actions/facebooklogin.php b/plugins/FacebookSSO/actions/facebooklogin.php index 08c237fe6e..9a230b7241 100644 --- a/plugins/FacebookSSO/actions/facebooklogin.php +++ b/plugins/FacebookSSO/actions/facebooklogin.php @@ -89,7 +89,7 @@ class FacebookloginAction extends Action $attrs = array( 'src' => common_path( - 'plugins/FacebookSSO/images/login-button.png', + 'plugins/FacebookBridge/images/login-button.png', true ), 'alt' => 'Login with Facebook', diff --git a/plugins/FacebookSSO/classes/Notice_to_item.php b/plugins/FacebookSSO/classes/Notice_to_item.php new file mode 100644 index 0000000000..a6a8030342 --- /dev/null +++ b/plugins/FacebookSSO/classes/Notice_to_item.php @@ -0,0 +1,190 @@ + + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://status.net/ + * + * StatusNet - the distributed open-source microblogging tool + * Copyright (C) 2010, StatusNet, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; + +/** + * Data class for mapping notices to Facebook stream items + * + * Note that notice_id is unique only within a single database; if you + * want to share this data for some reason, get the notice's URI and use + * that instead, since it's universally unique. + * + * @category Action + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://status.net/ + * + * @see DB_DataObject + */ + +class Notice_to_item extends Memcached_DataObject +{ + public $__table = 'notice_to_item'; // table name + public $notice_id; // int(4) primary_key not_null + public $item_id; // varchar(255) not null + public $created; // datetime + + /** + * Get an instance by key + * + * This is a utility method to get a single instance with a given key value. + * + * @param string $k Key to use to lookup + * @param mixed $v Value to lookup + * + * @return Notice_to_item object found, or null for no hits + * + */ + + function staticGet($k, $v=null) + { + return Memcached_DataObject::staticGet('Notice_to_item', $k, $v); + } + + /** + * return table definition for DB_DataObject + * + * DB_DataObject needs to know something about the table to manipulate + * instances. This method provides all the DB_DataObject needs to know. + * + * @return array array of column definitions + */ + + function table() + { + return array( + 'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL, + 'item_id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL, + 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL + ); + } + + static function schemaDef() + { + return array( + new ColumnDef('notice_id', 'integer', null, false, 'PRI'), + new ColumnDef('item_id', 'varchar', 255, false, 'UNI'), + new ColumnDef('created', 'datetime', null, false) + ); + } + + /** + * return key definitions for DB_DataObject + * + * DB_DataObject needs to know about keys that the table has, since it + * won't appear in StatusNet's own keys list. In most cases, this will + * simply reference your keyTypes() function. + * + * @return array list of key field names + */ + + function keys() + { + return array_keys($this->keyTypes()); + } + + /** + * return key definitions for Memcached_DataObject + * + * Our caching system uses the same key definitions, but uses a different + * method to get them. This key information is used to store and clear + * cached data, so be sure to list any key that will be used for static + * lookups. + * + * @return array associative array of key definitions, field name to type: + * 'K' for primary key: for compound keys, add an entry for each component; + * 'U' for unique keys: compound keys are not well supported here. + */ + + function keyTypes() + { + return array('notice_id' => 'K', 'item_id' => 'U'); + } + + /** + * Magic formula for non-autoincrementing integer primary keys + * + * If a table has a single integer column as its primary key, DB_DataObject + * assumes that the column is auto-incrementing and makes a sequence table + * to do this incrementation. Since we don't need this for our class, we + * overload this method and return the magic formula that DB_DataObject needs. + * + * @return array magic three-false array that stops auto-incrementing. + */ + + function sequenceKey() + { + return array(false, false, false); + } + + /** + * Save a mapping between a notice and a Facebook item + * + * @param integer $notice_id ID of the notice in StatusNet + * @param integer $item_id ID of the stream item on Facebook + * + * @return Notice_to_item new object for this value + */ + + static function saveNew($notice_id, $item_id) + { + $n2i = Notice_to_item::staticGet('notice_id', $notice_id); + + if (!empty($n2i)) { + return $n2i; + } + + $n2i = Notice_to_item::staticGet('item_id', $item_id); + + if (!empty($n2i)) { + return $n2i; + } + + common_debug( + "Mapping notice {$notice_id} to Facebook item {$item_id}", + __FILE__ + ); + + $n2i = new Notice_to_item(); + + $n2i->notice_id = $notice_id; + $n2i->item_id = $item_id; + $n2i->created = common_sql_now(); + + $n2i->insert(); + + return $n2i; + } +} diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookSSO/lib/facebookclient.php index cf00b55e3a..33edf5c6b1 100644 --- a/plugins/FacebookSSO/lib/facebookclient.php +++ b/plugins/FacebookSSO/lib/facebookclient.php @@ -173,11 +173,11 @@ class Facebookclient if ($this->isFacebookBound()) { common_debug("notice is facebook bound", __FILE__); if (empty($this->flink->credentials)) { - $this->sendOldRest(); + return $this->sendOldRest(); } else { // Otherwise we most likely have an access token - $this->sendGraph(); + return $this->sendGraph(); } } else { @@ -213,6 +213,7 @@ class Facebookclient $params = array( 'access_token' => $this->flink->credentials, + // XXX: Need to worrry about length of the message? 'message' => $this->notice->content ); @@ -220,7 +221,7 @@ class Facebookclient if (!empty($attachments)) { - // We can only send one attachment with the Graph API + // We can only send one attachment with the Graph API :( $first = array_shift($attachments); @@ -240,6 +241,21 @@ class Facebookclient sprintf('/%s/feed', $fbuid), 'post', $params ); + // Save a mapping + Notice_to_item::saveNew($this->notice->id, $result['id']); + + common_log( + LOG_INFO, + sprintf( + "Posted notice %d as a stream item for %s (%d), fbuid %s", + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + } catch (FacebookApiException $e) { return $this->handleFacebookError($e); } @@ -481,24 +497,42 @@ class Facebookclient $result = $this->facebook->api( array( 'method' => 'users.setStatus', - 'status' => $this->notice->content, + 'status' => $this->formatMessage(), 'status_includes_verb' => true, 'uid' => $fbuid ) ); - common_log( - LOG_INFO, - sprintf( - "Posted notice %s as a status update for %s (%d), fbuid %s", + if ($result == 1) { // 1 is success + + common_log( + LOG_INFO, + sprintf( + "Posted notice %s as a status update for %s (%d), fbuid %s", + $this->notice->id, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + // There is no item ID returned for status update so we can't + // save a Notice_to_item mapping + + } else { + + $msg = sprintf( + "Error posting notice %s as a status update for %s (%d), fbuid %s - error code: %s", $this->notice->id, $this->user->nickname, $this->user->id, - $fbuid - ), - __FILE__ - ); + $fbuid, + $result // will contain 0, or an error + ); + throw new FacebookApiException($msg, $result); + } } /* @@ -524,25 +558,66 @@ class Facebookclient $result = $this->facebook->api( array( 'method' => 'stream.publish', - 'message' => $this->notice->content, + 'message' => $this->formatMessage(), 'attachment' => $fbattachment, 'uid' => $fbuid ) ); - common_log( - LOG_INFO, - sprintf( - 'Posted notice %d as a %s for %s (%d), fbuid %s', + if (!empty($result)) { // result will contain the item ID + + // Save a mapping + Notice_to_item::saveNew($this->notice->id, $result); + + common_log( + LOG_INFO, + sprintf( + 'Posted notice %d as a %s for %s (%d), fbuid %s', + $this->notice->id, + empty($fbattachment) ? 'stream item' : 'stream item with attachment', + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + + } else { + + $msg = sprintf( + 'Could not post notice %d as a %s for %s (%d), fbuid %s - error code: %s', $this->notice->id, empty($fbattachment) ? 'stream item' : 'stream item with attachment', $this->user->nickname, $this->user->id, + $result, // result will contain an error code $fbuid - ), - __FILE__ - ); + ); + throw new FacebookApiException($msg, $result); + } + } + + /* + * Format the text message of a stream item so it's appropriate for + * sending to Facebook. If the notice is too long, truncate it, and + * add a linkback to the original notice at the end. + * + * @return String $txt the formated message + */ + function formatMessage() + { + // Start with the plaintext source of this notice... + $txt = $this->notice->content; + + // Facebook has a 420-char hardcoded max. + if (mb_strlen($statustxt) > 420) { + $noticeUrl = common_shorten_url($this->notice->uri); + $urlLen = mb_strlen($noticeUrl); + $txt = mb_substr($statustxt, 0, 420 - ($urlLen + 3)) . ' … ' . $noticeUrl; + } + + return $txt; } /* @@ -708,4 +783,240 @@ BODY; return mail_to_user($this->user, $subject, $body); } + /* + * Check to see if we have a mapping to a copy of this notice + * on Facebook + * + * @param Notice $notice the notice to check + * + * @return mixed null if it can't find one, or the id of the Facebook + * stream item + */ + static function facebookStatusId($notice) + { + $n2i = Notice_to_item::staticGet('notice_id', $notice->id); + + if (empty($n2i)) { + return null; + } else { + return $n2i->item_id; + } + } + + /* + * Save a Foreign_user record of a Facebook user + * + * @param object $fbuser a Facebook Graph API user obj + * See: http://developers.facebook.com/docs/reference/api/user + * @return mixed $result Id or key + * + */ + static function addFacebookUser($fbuser) + { + // remove any existing, possibly outdated, record + $luser = Foreign_user::getForeignUser($fbuser['id'], FACEBOOK_SERVICE); + + if (!empty($luser)) { + + $result = $luser->delete(); + + if ($result != false) { + common_log( + LOG_INFO, + sprintf( + 'Removed old Facebook user: %s, fbuid %d', + $fbuid['name'], + $fbuid['id'] + ), + __FILE__ + ); + } + } + + $fuser = new Foreign_user(); + + $fuser->nickname = $fbuser['name']; + $fuser->uri = $fbuser['link']; + $fuser->id = $fbuser['id']; + $fuser->service = FACEBOOK_SERVICE; + $fuser->created = common_sql_now(); + + $result = $fuser->insert(); + + if (empty($result)) { + common_log( + LOG_WARNING, + sprintf( + 'Failed to add new Facebook user: %s, fbuid %d', + $fbuser['name'], + $fbuser['id'] + ), + __FILE__ + ); + + common_log_db_error($fuser, 'INSERT', __FILE__); + } else { + common_log( + LOG_INFO, + sprintf( + 'Added new Facebook user: %s, fbuid %d', + $fbuser['name'], + $fbuser['id'] + ), + __FILE__ + ); + } + + return $result; + } + + /* + * Remove an item from a Facebook user's feed if we have a mapping + * for it. + */ + function streamRemove() + { + $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); + + if (!empty($this->flink) && !empty($n2i)) { + + $result = $this->facebook->api( + array( + 'method' => 'stream.remove', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) + ); + + if (!empty($result) && result == true) { + + common_log( + LOG_INFO, + sprintf( + 'Deleted Facebook item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + + $n2i->delete(); + + } else { + + common_log( + LOG_WARNING, + sprintf( + 'Could not deleted Facebook item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + } + } + } + + /* + * Like an item in a Facebook user's feed if we have a mapping + * for it. + */ + function like() + { + $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); + + if (!empty($this->flink) && !empty($n2i)) { + + $result = $this->facebook->api( + array( + 'method' => 'stream.addlike', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) + ); + + if (!empty($result) && result == true) { + + common_log( + LOG_INFO, + sprintf( + 'Added like for item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + + } else { + + common_log( + LOG_WARNING, + sprintf( + 'Could not like Facebook item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + } + } + } + + /* + * Unlike an item in a Facebook user's feed if we have a mapping + * for it. + */ + function unLike() + { + $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id); + + if (!empty($this->flink) && !empty($n2i)) { + + $result = $this->facebook->api( + array( + 'method' => 'stream.removeLike', + 'post_id' => $n2i->item_id, + 'uid' => $this->flink->foreign_id + ) + ); + + if (!empty($result) && result == true) { + + common_log( + LOG_INFO, + sprintf( + 'Removed like for item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + + } else { + + common_log( + LOG_WARNING, + sprintf( + 'Could not remove like for Facebook item: %s for %s (%d), fbuid %d', + $n2i->item_id, + $this->user->nickname, + $this->user->id, + $this->flink->foreign_id + ), + __FILE__ + ); + } + } + } + } From 4f63b5cff613f02ffed7de7a47027d65d723dbd4 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 16 Nov 2010 02:33:17 +0000 Subject: [PATCH 13/17] FacebookSSO -> FacebookBridge --- .../FacebookBridgePlugin.php | 0 .../actions/facebookadminpanel.php | 0 .../actions/facebookdeauthorize.php | 0 .../actions/facebookfinishlogin.php | 0 .../actions/facebooklogin.php | 0 .../actions/facebooksettings.php | 0 .../classes/Notice_to_item.php | 0 .../extlib/facebook.php | 0 .../extlib/fb_ca_chain_bundle.crt | 0 .../images/login-button.png | Bin .../lib/facebookclient.php | 0 .../lib/facebookqueuehandler.php | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename plugins/{FacebookSSO => FacebookBridge}/FacebookBridgePlugin.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/actions/facebookadminpanel.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/actions/facebookdeauthorize.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/actions/facebookfinishlogin.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/actions/facebooklogin.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/actions/facebooksettings.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/classes/Notice_to_item.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/extlib/facebook.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/extlib/fb_ca_chain_bundle.crt (100%) rename plugins/{FacebookSSO => FacebookBridge}/images/login-button.png (100%) rename plugins/{FacebookSSO => FacebookBridge}/lib/facebookclient.php (100%) rename plugins/{FacebookSSO => FacebookBridge}/lib/facebookqueuehandler.php (100%) diff --git a/plugins/FacebookSSO/FacebookBridgePlugin.php b/plugins/FacebookBridge/FacebookBridgePlugin.php similarity index 100% rename from plugins/FacebookSSO/FacebookBridgePlugin.php rename to plugins/FacebookBridge/FacebookBridgePlugin.php diff --git a/plugins/FacebookSSO/actions/facebookadminpanel.php b/plugins/FacebookBridge/actions/facebookadminpanel.php similarity index 100% rename from plugins/FacebookSSO/actions/facebookadminpanel.php rename to plugins/FacebookBridge/actions/facebookadminpanel.php diff --git a/plugins/FacebookSSO/actions/facebookdeauthorize.php b/plugins/FacebookBridge/actions/facebookdeauthorize.php similarity index 100% rename from plugins/FacebookSSO/actions/facebookdeauthorize.php rename to plugins/FacebookBridge/actions/facebookdeauthorize.php diff --git a/plugins/FacebookSSO/actions/facebookfinishlogin.php b/plugins/FacebookBridge/actions/facebookfinishlogin.php similarity index 100% rename from plugins/FacebookSSO/actions/facebookfinishlogin.php rename to plugins/FacebookBridge/actions/facebookfinishlogin.php diff --git a/plugins/FacebookSSO/actions/facebooklogin.php b/plugins/FacebookBridge/actions/facebooklogin.php similarity index 100% rename from plugins/FacebookSSO/actions/facebooklogin.php rename to plugins/FacebookBridge/actions/facebooklogin.php diff --git a/plugins/FacebookSSO/actions/facebooksettings.php b/plugins/FacebookBridge/actions/facebooksettings.php similarity index 100% rename from plugins/FacebookSSO/actions/facebooksettings.php rename to plugins/FacebookBridge/actions/facebooksettings.php diff --git a/plugins/FacebookSSO/classes/Notice_to_item.php b/plugins/FacebookBridge/classes/Notice_to_item.php similarity index 100% rename from plugins/FacebookSSO/classes/Notice_to_item.php rename to plugins/FacebookBridge/classes/Notice_to_item.php diff --git a/plugins/FacebookSSO/extlib/facebook.php b/plugins/FacebookBridge/extlib/facebook.php similarity index 100% rename from plugins/FacebookSSO/extlib/facebook.php rename to plugins/FacebookBridge/extlib/facebook.php diff --git a/plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt b/plugins/FacebookBridge/extlib/fb_ca_chain_bundle.crt similarity index 100% rename from plugins/FacebookSSO/extlib/fb_ca_chain_bundle.crt rename to plugins/FacebookBridge/extlib/fb_ca_chain_bundle.crt diff --git a/plugins/FacebookSSO/images/login-button.png b/plugins/FacebookBridge/images/login-button.png similarity index 100% rename from plugins/FacebookSSO/images/login-button.png rename to plugins/FacebookBridge/images/login-button.png diff --git a/plugins/FacebookSSO/lib/facebookclient.php b/plugins/FacebookBridge/lib/facebookclient.php similarity index 100% rename from plugins/FacebookSSO/lib/facebookclient.php rename to plugins/FacebookBridge/lib/facebookclient.php diff --git a/plugins/FacebookSSO/lib/facebookqueuehandler.php b/plugins/FacebookBridge/lib/facebookqueuehandler.php similarity index 100% rename from plugins/FacebookSSO/lib/facebookqueuehandler.php rename to plugins/FacebookBridge/lib/facebookqueuehandler.php From 0b573e0d2b5c15e296d2520ba87cbb6a80f3837d Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 15 Nov 2010 18:48:22 -0800 Subject: [PATCH 14/17] Store the current user in the CurrentUserDesignAction --- lib/currentuserdesignaction.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/currentuserdesignaction.php b/lib/currentuserdesignaction.php index 490f87d13c..7cd892022d 100644 --- a/lib/currentuserdesignaction.php +++ b/lib/currentuserdesignaction.php @@ -22,7 +22,7 @@ * @category Action * @package StatusNet * @author Evan Prodromou - * @copyright 2009 StatusNet, Inc. + * @copyright 2009-2010 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ @@ -40,12 +40,31 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { * @category Action * @package StatusNet * @author Evan Prodromou + * @author Zach Copley * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ * */ class CurrentUserDesignAction extends Action { + + protected $cur = null; // The current user + + /** + * For initializing members of the class. Set a the + * current user here. + * + * @param array $argarray misc. arguments + * + * @return boolean true + */ + function prepare($argarray) + { + parent::prepare($argarray); + + $this->cur = common_current_user(); + } + /** * A design for this action * @@ -55,9 +74,7 @@ class CurrentUserDesignAction extends Action */ function getDesign() { - $cur = common_current_user(); - - if (!empty($cur)) { + if (!empty($this->cur)) { $design = $cur->getDesign(); @@ -68,4 +85,5 @@ class CurrentUserDesignAction extends Action return parent::getDesign(); } + } From 64a29bd401bb7f38e174a6529c062dd3c20bfe5b Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 16 Nov 2010 06:10:49 +0000 Subject: [PATCH 15/17] Fix syntax error --- lib/currentuserdesignaction.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/currentuserdesignaction.php b/lib/currentuserdesignaction.php index 7cd892022d..012d30ad6f 100644 --- a/lib/currentuserdesignaction.php +++ b/lib/currentuserdesignaction.php @@ -63,6 +63,8 @@ class CurrentUserDesignAction extends Action parent::prepare($argarray); $this->cur = common_current_user(); + + return true; } /** @@ -76,7 +78,7 @@ class CurrentUserDesignAction extends Action { if (!empty($this->cur)) { - $design = $cur->getDesign(); + $design = $this->cur->getDesign(); if (!empty($design)) { return $design; From 2c68703923e94a27376622d96a1d5481807bfbf6 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 17 Nov 2010 21:53:56 +0000 Subject: [PATCH 16/17] Facebook: Gracefully handle disconnection --- lib/currentuserdesignaction.php | 5 + .../FacebookBridge/FacebookBridgePlugin.php | 36 ++-- .../actions/facebookdeauthorize.php | 76 +------- .../actions/facebooksettings.php | 167 +++++++++--------- plugins/FacebookBridge/lib/facebookclient.php | 136 ++++++++++++-- 5 files changed, 239 insertions(+), 181 deletions(-) diff --git a/lib/currentuserdesignaction.php b/lib/currentuserdesignaction.php index 012d30ad6f..e84c777685 100644 --- a/lib/currentuserdesignaction.php +++ b/lib/currentuserdesignaction.php @@ -88,4 +88,9 @@ class CurrentUserDesignAction extends Action return parent::getDesign(); } + function getCurrentUser() + { + return $this->cur; + } } + diff --git a/plugins/FacebookBridge/FacebookBridgePlugin.php b/plugins/FacebookBridge/FacebookBridgePlugin.php index c30ea15440..93427d5bdc 100644 --- a/plugins/FacebookBridge/FacebookBridgePlugin.php +++ b/plugins/FacebookBridge/FacebookBridgePlugin.php @@ -236,7 +236,8 @@ class FacebookBridgePlugin extends Plugin } /* - * Add a tab for user-level Facebook settings + * Add a tab for user-level Facebook settings if the user + * has a link to Facebook * * @param Action &action the current action * @@ -247,17 +248,32 @@ class FacebookBridgePlugin extends Plugin if ($this->hasApplication()) { $action_name = $action->trimmed('action'); - $action->menuItem( - common_local_url('facebooksettings'), - // TRANS: Menu item tab. - _m('MENU','Facebook'), - // TRANS: Tooltip for menu item "Facebook". - _m('Facebook settings'), - $action_name === 'facebooksettings' - ); + // CurrentUserDesignAction stores the current user in $cur + $user = $action->getCurrentUser(); + + $flink = null; + + if (!empty($user)) { + $flink = Foreign_link::getByUserID( + $user->id, + FACEBOOK_SERVICE + ); + } + + if (!empty($flink)) { + + $action->menuItem( + common_local_url('facebooksettings'), + // TRANS: Menu item tab. + _m('MENU','Facebook'), + // TRANS: Tooltip for menu item "Facebook". + _m('Facebook settings'), + $action_name === 'facebooksettings' + ); + + } } - return true; } /* diff --git a/plugins/FacebookBridge/actions/facebookdeauthorize.php b/plugins/FacebookBridge/actions/facebookdeauthorize.php index cb816fc54a..6813ccf1d2 100644 --- a/plugins/FacebookBridge/actions/facebookdeauthorize.php +++ b/plugins/FacebookBridge/actions/facebookdeauthorize.php @@ -82,7 +82,7 @@ class FacebookdeauthorizeAction extends Action LOG_WARNING, sprintf( 'Unable to delete Facebook foreign link ' - . 'for %s (%d), fbuid %s', + . 'for %s (%d), fbuid %d', $user->nickname, $user->id, $fbuid @@ -95,7 +95,7 @@ class FacebookdeauthorizeAction extends Action common_log( LOG_INFO, sprintf( - 'Facebook callback: %s (%d), fbuid %s has deauthorized ' + 'Facebook callback: %s (%d), fbuid %d has deauthorized ' . 'the Facebook application.', $user->nickname, $user->id, @@ -107,7 +107,7 @@ class FacebookdeauthorizeAction extends Action // Warn the user about being locked out of their account // if we can. if (empty($user->password) && !empty($user->email)) { - $this->emailWarn($user); + Facebookclient::emailWarn($user); } else { common_log( LOG_WARNING, @@ -141,74 +141,4 @@ class FacebookdeauthorizeAction extends Action } } - /* - * Send the user an email warning that their account has been - * disconnected and he/she has no way to login and must contact - * the site administrator for help. - * - * @param User $user the deauthorizing user - * - */ - function emailWarn($user) - { - $profile = $user->getProfile(); - - $siteName = common_config('site', 'name'); - $siteEmail = common_config('site', 'email'); - - if (empty($siteEmail)) { - common_log( - LOG_WARNING, - "No site email address configured. Please set one." - ); - } - - common_switch_locale($user->language); - - $subject = _m('Contact the %s administrator to retrieve your account'); - - $msg = <<nickname, - $siteName, - $siteEmail - ); - - common_switch_locale(); - - if (mail_to_user($user, $subject, $body)) { - common_log( - LOG_INFO, - sprintf( - 'Sent account lockout warning to %s (%d)', - $user->nickname, - $user->id - ), - __FILE__ - ); - } else { - common_log( - LOG_WARNING, - sprintf( - 'Unable to send account lockout warning to %s (%d)', - $user->nickname, - $user->id - ), - __FILE__ - ); - } - } - } \ No newline at end of file diff --git a/plugins/FacebookBridge/actions/facebooksettings.php b/plugins/FacebookBridge/actions/facebooksettings.php index e511810369..b9fa7ba2af 100644 --- a/plugins/FacebookBridge/actions/facebooksettings.php +++ b/plugins/FacebookBridge/actions/facebooksettings.php @@ -2,7 +2,7 @@ /** * StatusNet, the distributed open-source microblogging tool * - * Settings for Facebook + * Edit user settings for Facebook * * PHP version 5 * @@ -26,13 +26,12 @@ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ - if (!defined('STATUSNET')) { exit(1); } /** - * Settings for Facebook + * Edit user settings for Facebook * * @category Settings * @package StatusNet @@ -42,15 +41,20 @@ if (!defined('STATUSNET')) { * * @see SettingsAction */ +class FacebooksettingsAction extends ConnectSettingsAction { -class FacebooksettingsAction extends ConnectSettingsAction -{ - private $facebook; + private $facebook; // Facebook PHP-SDK client obj private $flink; private $user; - - function prepare($args) - { + + /** + * For initializing members of the class. + * + * @param array $argarray misc. arguments + * + * @return boolean true + */ + function prepare($args) { parent::prepare($args); $this->facebook = new Facebook( @@ -62,13 +66,19 @@ class FacebooksettingsAction extends ConnectSettingsAction ); $this->user = common_current_user(); - $this->flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE); + + $this->flink = Foreign_link::getByUserID( + $this->user->id, + FACEBOOK_SERVICE + ); return true; } - function handlePost($args) - { + /* + * Check the sessions token and dispatch + */ + function handlePost($args) { // CSRF protection $token = $this->trimmed('token'); @@ -86,50 +96,40 @@ class FacebooksettingsAction extends ConnectSettingsAction } } - function title() - { + /** + * Returns the page title + * + * @return string page title + */ + function title() { // TRANS: Page title for Facebook settings. return _m('Facebook settings'); } - + /** * Instructions for use * * @return instructions for use */ - - function getInstructions() - { + function getInstructions() { return _('Facebook settings'); } - function showContent() - { + /* + * Show the settings form if he/she has a link to Facebook + * + * @return void + */ + function showContent() { - if (empty($this->flink)) { - - $this->element( - 'p', - 'instructions', - _m('There is no Facebook user connected to this account.') - ); - - $attrs = array( - 'show-faces' => 'true', - 'perms' => 'user_location,user_website,offline_access,publish_stream' - ); - - $this->element('fb:login-button', $attrs); - - - } else { + if (!empty($this->flink)) { $this->elementStart( 'form', array( 'method' => 'post', - 'id' => 'form_settings_facebook', - 'class' => 'form_settings', + 'id' => 'form_settings_facebook', + 'class' => 'form_settings', 'action' => common_local_url('facebooksettings') ) ); @@ -140,22 +140,21 @@ class FacebooksettingsAction extends ConnectSettingsAction $this->elementStart('p', array('class' => 'facebook-user-display')); - $this->elementStart( + $this->element( 'fb:profile-pic', - array('uid' => $this->flink->foreign_id, - 'size' => 'small', - 'linked' => 'true', - 'facebook-logo' => 'true') + array( + 'uid' => $this->flink->foreign_id, + 'size' => 'small', + 'linked' => 'true', + 'facebook-logo' => 'true' + ) ); - $this->elementEnd('fb:profile-pic'); - $this->elementStart( + $this->element( 'fb:name', array('uid' => $this->flink->foreign_id, 'useyou' => 'false') ); - $this->elementEnd('fb:name'); - $this->elementEnd('p'); $this->elementStart('ul', 'form_data'); @@ -173,9 +172,9 @@ class FacebooksettingsAction extends ConnectSettingsAction $this->elementStart('li'); $this->checkbox( - 'replysync', - _m('Send "@" replies to Facebook.'), - ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true + 'replysync', + _m('Send "@" replies to Facebook.'), + ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true ); $this->elementEnd('li'); @@ -183,10 +182,10 @@ class FacebooksettingsAction extends ConnectSettingsAction $this->elementStart('li'); // TRANS: Submit button to save synchronisation settings. - $this->submit('save', _m('BUTTON','Save')); + $this->submit('save', _m('BUTTON', 'Save')); $this->elementEnd('li'); - + $this->elementEnd('ul'); $this->elementStart('fieldset'); @@ -197,39 +196,44 @@ class FacebooksettingsAction extends ConnectSettingsAction if (empty($this->user->password)) { $this->elementStart('p', array('class' => 'form_guide')); - // @todo FIXME: Bad i18n. Patchwork message in three parts. - // TRANS: Followed by a link containing text "set a password". - $this->text(_m('Disconnecting your Faceboook ' . - 'would make it impossible to log in! Please ')); - $this->element('a', - array('href' => common_local_url('passwordsettings')), - // TRANS: Preceded by "Please " and followed by " first." - _m('set a password')); - // TRANS: Preceded by "Please set a password". - $this->text(_m(' first.')); + + $msg = sprintf( + _m( + 'Disconnecting your Faceboook would make it impossible to ' + . 'log in! Please [set a password](%s) first.' + ), + common_local_url('passwordsettings') + ); + + $this->raw(common_markup_to_html($msg)); $this->elementEnd('p'); + } else { - $note = 'Keep your %s account but disconnect from Facebook. ' . - 'You\'ll use your %s password to log in.'; - - $site = common_config('site', 'name'); - - $this->element('p', 'instructions', - sprintf($note, $site, $site)); + $msg = sprintf( + _m( + 'Keep your %1$s account but disconnect from Facebook. ' . + 'You\'ll use your 1%$s password to log in.' + ), + common_config('site', 'name') + ); // TRANS: Submit button. - $this->submit('disconnect', _m('BUTTON','Disconnect')); + $this->submit('disconnect', _m('BUTTON', 'Disconnect')); } $this->elementEnd('fieldset'); $this->elementEnd('form'); - } + } } - function saveSettings() - { + /* + * Save the user's Facebook settings + * + * @return void + */ + function saveSettings() { $noticesync = $this->boolean('noticesync'); $replysync = $this->boolean('replysync'); @@ -246,10 +250,14 @@ class FacebooksettingsAction extends ConnectSettingsAction } } - function disconnect() - { - $flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE); - $result = $flink->delete(); + /* + * Disconnect the user's Facebook account - deletes the Foreign_link + * and shows the user a success message if all goes well. + */ + function disconnect() { + + $result = $this->flink->delete(); + $this->flink = null; if ($result === false) { common_log_db_error($user, 'DELETE', __FILE__); @@ -258,7 +266,6 @@ class FacebooksettingsAction extends ConnectSettingsAction } $this->showForm(_m('You have disconnected from Facebook.'), true); - } -} +} diff --git a/plugins/FacebookBridge/lib/facebookclient.php b/plugins/FacebookBridge/lib/facebookclient.php index 33edf5c6b1..575208cacc 100644 --- a/plugins/FacebookBridge/lib/facebookclient.php +++ b/plugins/FacebookBridge/lib/facebookclient.php @@ -202,7 +202,7 @@ class Facebookclient common_debug( sprintf( - "Attempting use Graph API to post notice %d as a stream item for %s (%d), fbuid %s", + "Attempting use Graph API to post notice %d as a stream item for %s (%d), fbuid %d", $this->notice->id, $this->user->nickname, $this->user->id, @@ -247,7 +247,7 @@ class Facebookclient common_log( LOG_INFO, sprintf( - "Posted notice %d as a stream item for %s (%d), fbuid %s", + "Posted notice %d as a stream item for %s (%d), fbuid %d", $this->notice->id, $this->user->nickname, $this->user->id, @@ -287,7 +287,7 @@ class Facebookclient } else { $msg = 'Not sending notice %d to Facebook because user %s ' - . '(%d), fbuid %s, does not have \'status_update\' ' + . '(%d), fbuid %d, does not have \'status_update\' ' . 'or \'publish_stream\' permission.'; common_log( @@ -330,7 +330,7 @@ class Facebookclient common_debug( sprintf( - 'Checking for %s permission for user %s (%d), fbuid %s', + 'Checking for %s permission for user %s (%d), fbuid %d', $permission, $this->user->nickname, $this->user->id, @@ -351,7 +351,7 @@ class Facebookclient common_debug( sprintf( - '%s (%d), fbuid %s has %s permission', + '%s (%d), fbuid %d has %s permission', $permission, $this->user->nickname, $this->user->id, @@ -425,6 +425,12 @@ class Facebookclient ); return true; break; + + // @fixme: Facebook returns these 2xx permission errors sometimes + // FOR NO GOOD REASON AT ALL! It would be better to retry a few times + // over an extended period of time to instead of immediately + // disconnecting. + case 200: // Permissions error case 250: // Updating status requires the extended permission status_update $this->disconnect(); @@ -485,7 +491,7 @@ class Facebookclient common_debug( sprintf( - "Attempting to post notice %d as a status update for %s (%d), fbuid %s", + "Attempting to post notice %d as a status update for %s (%d), fbuid %d", $this->notice->id, $this->user->nickname, $this->user->id, @@ -508,7 +514,7 @@ class Facebookclient common_log( LOG_INFO, sprintf( - "Posted notice %s as a status update for %s (%d), fbuid %s", + "Posted notice %s as a status update for %s (%d), fbuid %d", $this->notice->id, $this->user->nickname, $this->user->id, @@ -523,7 +529,7 @@ class Facebookclient } else { $msg = sprintf( - "Error posting notice %s as a status update for %s (%d), fbuid %s - error code: %s", + "Error posting notice %s as a status update for %s (%d), fbuid %d - error code: %s", $this->notice->id, $this->user->nickname, $this->user->id, @@ -544,7 +550,7 @@ class Facebookclient common_debug( sprintf( - 'Attempting to post notice %d as stream item for %s (%d) fbuid %s', + 'Attempting to post notice %d as stream item for %s (%d) fbuid %d', $this->notice->id, $this->user->nickname, $this->user->id, @@ -572,7 +578,7 @@ class Facebookclient common_log( LOG_INFO, sprintf( - 'Posted notice %d as a %s for %s (%d), fbuid %s', + 'Posted notice %d as a %s for %s (%d), fbuid %d', $this->notice->id, empty($fbattachment) ? 'stream item' : 'stream item with attachment', $this->user->nickname, @@ -585,7 +591,7 @@ class Facebookclient } else { $msg = sprintf( - 'Could not post notice %d as a %s for %s (%d), fbuid %s - error code: %s', + 'Could not post notice %d as a %s for %s (%d), fbuid %d - error code: %s', $this->notice->id, empty($fbattachment) ? 'stream item' : 'stream item with attachment', $this->user->nickname, @@ -694,7 +700,7 @@ class Facebookclient common_log( LOG_INFO, sprintf( - 'Removing Facebook link for %s (%d), fbuid %s', + 'Removing Facebook link for %s (%d), fbuid %d', $this->user->nickname, $this->user->id, $fbuid @@ -708,7 +714,7 @@ class Facebookclient common_log( LOG_ERR, sprintf( - 'Could not remove Facebook link for %s (%d), fbuid %s', + 'Could not remove Facebook link for %s (%d), fbuid %d', $this->user->nickname, $this->user->id, $fbuid @@ -719,13 +725,31 @@ class Facebookclient } // Notify the user that we are removing their Facebook link + if (!empty($this->user->email)) { + $result = $this->mailFacebookDisconnect(); - $result = $this->mailFacebookDisconnect(); + if (!$result) { - if (!$result) { + $msg = 'Unable to send email to notify %s (%d), fbuid %d ' + . 'about his/her Facebook link being removed.'; - $msg = 'Unable to send email to notify %s (%d), fbuid %s ' - . 'about his/her Facebook link being removed.'; + common_log( + LOG_WARNING, + sprintf( + $msg, + $this->user->nickname, + $this->user->id, + $fbuid + ), + __FILE__ + ); + } + + } else { + + $msg = 'Unable to send email to notify %s (%d), fbuid %d ' + . 'about his/her Facebook link being removed because the ' + . 'user has not set an email address.'; common_log( LOG_WARNING, @@ -780,7 +804,83 @@ BODY; common_switch_locale(); - return mail_to_user($this->user, $subject, $body); + $result = mail_to_user($this->user, $subject, $body); + + if (empty($this->user->password)) { + $result = self::emailWarn($this->user); + } + + return $result; + } + + /* + * Send the user an email warning that their account has been + * disconnected and he/she has no way to login and must contact + * the site administrator for help. + * + * @param User $user the deauthorizing user + * + */ + static function emailWarn($user) + { + $profile = $user->getProfile(); + + $siteName = common_config('site', 'name'); + $siteEmail = common_config('site', 'email'); + + if (empty($siteEmail)) { + common_log( + LOG_WARNING, + "No site email address configured. Please set one." + ); + } + + common_switch_locale($user->language); + + $subject = _m('Contact the %s administrator to retrieve your account'); + + $msg = <<nickname, + $siteName, + $siteEmail + ); + + common_switch_locale(); + + if (mail_to_user($user, $subject, $body)) { + common_log( + LOG_INFO, + sprintf( + 'Sent account lockout warning to %s (%d)', + $user->nickname, + $user->id + ), + __FILE__ + ); + } else { + common_log( + LOG_WARNING, + sprintf( + 'Unable to send account lockout warning to %s (%d)', + $user->nickname, + $user->id + ), + __FILE__ + ); + } } /* From 163f18b8acd59e7343da4bdde9d0a5f5bd762308 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 17 Nov 2010 22:15:30 +0000 Subject: [PATCH 17/17] Remove dumb debugging statement --- plugins/FacebookBridge/lib/facebookclient.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/FacebookBridge/lib/facebookclient.php b/plugins/FacebookBridge/lib/facebookclient.php index 575208cacc..907c7537e9 100644 --- a/plugins/FacebookBridge/lib/facebookclient.php +++ b/plugins/FacebookBridge/lib/facebookclient.php @@ -103,7 +103,6 @@ class Facebookclient */ static function facebookBroadcastNotice($notice) { - common_debug('Facebook broadcast'); $client = new Facebookclient($notice); return $client->sendNotice(); }