1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_Service_Facebook_Auth extends Horde_Service_Facebook_Base
16: {
17: 18: 19: 20: 21:
22: protected $_sessionKey;
23:
24:
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:
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:
40: const EXTEND_PERMS_OFFLINE = 'offline_access';
41: const EXTEND_PERMS_PUBLISHSTREAM = 'publish_stream';
42: const EXTEND_PERMS_READSTREAM = 'read_stream';
43:
44: 45: 46: 47: 48: 49: 50: 51: 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: 62: 63: 64: 65: 66: 67: 68: 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: 80: 81: 82: 83: 84: 85: 86: 87: 88: 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: 112: 113: 114:
115: public function setSession($sessionKey)
116: {
117: $this->_sessionKey = $sessionKey;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public function revokeExtendedPermission($perm)
128: {
129:
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: 143: 144: 145: 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: