Overview

Packages

  • None
  • Service
    • Facebook

Classes

  • Horde_Service_Facebook_Auth
  • Horde_Service_Facebook_Base
  • Horde_Service_Facebook_BatchRequest
  • Horde_Service_Facebook_ErrorCodes
  • Horde_Service_Facebook_Events
  • Horde_Service_Facebook_Exception
  • Horde_Service_Facebook_Fql
  • Horde_Service_Facebook_Friends
  • Horde_Service_Facebook_Groups
  • Horde_Service_Facebook_Links
  • Horde_Service_Facebook_Notes
  • Horde_Service_Facebook_Notifications
  • Horde_Service_Facebook_Photos
  • Horde_Service_Facebook_Request
  • Horde_Service_Facebook_Streams
  • Horde_Service_Facebook_Translation
  • Horde_Service_Facebook_UploadRequest
  • Horde_Service_Facebook_Users
  • Horde_Service_Facebook_Videos
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Horde_Service_Facebook_Auth:: wrap functionality associated with
  4:  * authenticating to Facebook.
  5:  *
  6:  * For now, only provide methods for authenticating that make sense from
  7:  * within a Horde context.
  8:  *
  9:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
 10:  *
 11:  * @author Michael J. Rubinsky <mrubinsk@horde.org>
 12:  * @category Horde
 13:  * @package Service_Facebook
 14:  */
 15: class Horde_Service_Facebook_Auth extends Horde_Service_Facebook_Base
 16: {
 17:     /**
 18:      * Cache the current auth_token.
 19:      *
 20:      * @var string
 21:      */
 22:     protected $_sessionKey;
 23: 
 24:     /** User Data Perms **/
 25:     const EXTEND_PERMS_USER_ABOUT = 'user_about_me';
 26:     const EXTEND_PERMS_USER_BIRTHDAY = 'user_birthday';
 27:     const EXTEND_PERMS_USER_EVENTS = 'user_events';
 28:     const EXTEND_PERMS_USER_HOMETOWN = 'user_hometown';
 29:     const EXTEND_PERMS_USER_LOCATION = 'user_location';
 30:     const EXTEND_PERMS_USER_PHOTOS = 'user_photos';
 31: 
 32:     /** Friends Data **/
 33:     const EXTEND_PERMS_FRIENDS_ABOUT = 'friends_about_me';
 34:     const EXTEND_PERMS_FRIENDS_BIRTHDAY = 'friends_birthday';
 35:     const EXTEND_PERMS_FRIENDS_HOMETOWN = 'friends_hometown';
 36:     const EXTEND_PERMS_FRIENDS_LOCATION = 'friends_location';
 37:     const EXTEND_PERMS_FRIENDS_PHOTOS = 'friends_photos';
 38: 
 39:     /** Misc **/
 40:     const EXTEND_PERMS_OFFLINE = 'offline_access';
 41:     const EXTEND_PERMS_PUBLISHSTREAM = 'publish_stream';
 42:     const EXTEND_PERMS_READSTREAM = 'read_stream';
 43: 
 44:     /**
 45:      * Get the URL for the user to authenticate the application and authorize
 46:      * various extender permissions/
 47:      *
 48:      * @param string $callback  The callback url. FB will redirect back to here.
 49:      * @param array $perms      An array of FB permissions to request.
 50:      *
 51:      * @return string  The URL.
 52:      */
 53:     public function getOAuthUrl($callback, array $perms = array())
 54:     {
 55:         return $this->_facebook->getFacebookUrl()
 56:             . '/dialog/oauth?client_id=' . $this->_facebook->appId
 57:             . '&redirect_uri=' . urlencode($callback) . '&scope=' . implode(',', $perms);
 58:     }
 59: 
 60:     /**
 61:      * Returns the URL to obtain the auth_token from FB after getOAuthUrl
 62:      * redirects back to your callback URL.
 63:      *
 64:      * @param string $code      The code returned by FB after the OAuth2 dialog
 65:      * @param string $callback  The callback url. Required in order to
 66:      *                          authenticate via OAuth2.
 67:      *
 68:      * @return string  The URL.
 69:      */
 70:     public function getAuthTokenUrl($code, $callback)
 71:     {
 72:         return $this->_facebook->getFacebookUrl('graph')
 73:             . '/oauth/access_token?client_id=' . $this->_facebook->appId
 74:             . '&redirect_uri=' . urlencode($callback) . '&client_secret=' . $this->_facebook->secret
 75:             . '&code=' . $code;
 76:     }
 77: 
 78:     /**
 79:      * Obtain the current access_token. Either returns the currently set token
 80:      * or, if a OAuth2 code is provided, sends a GET request to FB requesting
 81:      * the access_token.
 82:      *
 83:      * @param string $code      The code returned from FB's OAuth dialog.
 84:      * @param string $callback  If provided, used as the callback URL required
 85:      *                          during the final steps in the OAuth2 process.
 86:      *
 87:      * @return string  The access_token
 88:      * @throws Horde_Service_Facebook_Exception
 89:      */
 90:     public function getSessionKey($code = null, $callback = '')
 91:     {
 92:         if (!empty($code)) {
 93:             try {
 94:                 $result = $this->_http->request(
 95:                     'GET', $this->getAuthTokenUrl($code, $callback));
 96:             } catch (Horde_Http_Exception $e) {
 97:                 throw new Horde_Service_Facebook_Exception($e);
 98:             }
 99: 
100:             if ($result->code !== 200) {
101:                 throw new Horde_Service_Facebook_Exception('Unable to contact Facebook', $result->code);
102:             }
103:             parse_str($result->getBody(), $vars);
104:             $this->_sessionKey = $vars['access_token'];
105:         }
106: 
107:         return $this->_sessionKey;
108:     }
109: 
110:     /**
111:      * Sets an existing access_token for this session.
112:      *
113:      * @param string $sessionKey  The FB OAuth2 access_token
114:      */
115:     public function setSession($sessionKey)
116:     {
117:         $this->_sessionKey = $sessionKey;
118:     }
119: 
120:     /**
121:      * Revoke a previously authorizied extended permission
122:      *
123:      * @param string $perm  The extended permission to remove.
124:      *
125:      * @return unknown_type
126:      */
127:     public function revokeExtendedPermission($perm)
128:     {
129:         // Session key is *required*
130:         if (!$skey = $this->getSessionKey()) {
131:             throw new Horde_Service_Facebook_Exception(
132:                 'session_key is required',
133:                 Horde_Service_Facebook_ErrorCodes::API_EC_SESSION_REQUIRED);
134:         }
135: 
136:         return $this->_facebook->callMethod(
137:             'Auth.revokeExtendedPermission', array('perm' => $perm));
138: 
139:     }
140: 
141:     /**
142:      * Returns the user corresponding to the current session object.
143:      *
144:      * @throws Horde_Service_Facebook_Exception
145:      * @return string User id
146:      */
147:     public function &getLoggedInUser()
148:     {
149:         if (empty($this->_sessionKey)) {
150:             throw new Horde_Service_Facebook_Exception(
151:                 'users.getLoggedInUser requires a session_key',
152:                 Horde_Service_Facebook_ErrorCodes::API_EC_PARAM_SESSION_KEY);
153:         }
154: 
155:         $user = (string)$this->_facebook->callMethod(
156:             'facebook.users.getLoggedInUser',
157:             array('session_key' => $this->_sessionKey));
158: 
159:         return $user;
160:     }
161: 
162: }
163: 
API documentation generated by ApiGen