1<?php 2 3/** 4 * PHP5 interface for Facebook's REST API 5 * 6 * PHP version 5.1.0+ 7 * 8 * LICENSE: This source file is subject to the New BSD license that is 9 * available through the world-wide-web at the following URI: 10 * http://www.opensource.org/licenses/bsd-license.php. If you did not receive 11 * a copy of the New BSD License and are unable to obtain it through the web, 12 * please send a note to license@php.net so we can mail you a copy immediately. 13 * 14 * @category Services 15 * @package Services_Facebook 16 * @author Joe Stump <joe@joestump.net> 17 * @copyright 2007-2008 Joe Stump <joe@joestump.net> 18 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 19 * @version Release: 0.2.14 20 * @link http://pear.php.net/package/Services_Facebook 21 */ 22 23require_once 'Services/Facebook/Common.php'; 24require_once 'Services/Facebook/Exception.php'; 25 26/** 27 * Facebook Users Interface 28 * 29 * @category Services 30 * @package Services_Facebook 31 * @author Joe Stump <joe@joestump.net> 32 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 33 * @version Release: 0.2.14 34 * @link http://wiki.developers.facebook.com 35 */ 36class Services_Facebook_Users extends Services_Facebook_Common 37{ 38 /** 39 * Default user fields 40 * 41 * @var array $userFields List of allowed getInfo fields 42 */ 43 public $userFields = array( 44 'about_me', 'activities', 'affiliations', 'birthday', 'books', 45 'current_location', 'education_history', 'first_name', 'has_added_app', 46 'hometown_location', 'hs_info', 'interests', 'is_app_user', 47 'last_name', 'meeting_for', 'meeting_sex', 'movies', 'music', 'name', 48 'notes_count', 'pic', 'pic_small', 'pic_square', 'political', 49 'profile_update_time', 'quotes', 'relationship_status', 'religion', 50 'sex', 'significant_other_id', 'status', 'timezone', 'tv', 51 'wall_count', 'work_history' 52 ); 53 54 /** 55 * photoSizes 56 * 57 * @var array $photoSizes Supported photo sizes 58 * @see self::getPhoto 59 */ 60 protected $photoSizes = array('big', 'small', 'square'); 61 62 /** 63 * Has the current user added this application? 64 * 65 * @return boolean 66 */ 67 public function isAppAdded() 68 { 69 $result = $this->callMethod('users.isAppAdded', array( 70 'session_key' => $this->sessionKey 71 )); 72 73 return (intval((string)$result) == 1); 74 } 75 76 /** 77 * Is app user 78 * 79 * Uses the passed in user ID or session key to determine 80 * if the user is a user of the application. 81 * 82 * @param float $uid Facebook user ID 83 * 84 * @return bool 85 */ 86 public function isAppUser($uid = null) 87 { 88 $args = array(); 89 if ($uid !== null) { 90 $args['uid'] = $uid; 91 } elseif (!empty($this->sessionKey)) { 92 $args['session_key'] = $this->sessionKey; 93 } else { 94 throw new Services_Facebook_Exception('Users.isAppUser ' . 95 'requires a session key or uid, none provided'); 96 } 97 98 $result = $this->callMethod('users.isAppUser', $args); 99 return (intval((string)$result) == 1); 100 } 101 102 /** 103 * Set a user's status message 104 * 105 * Set $status to true to clear the status or a string to change the 106 * actual status message. 107 * 108 * @param mixed $status Set to true to clear status 109 * 110 * @return boolean True on success, false on failure 111 * @link http://wiki.developers.facebook.com/index.php/Users.setStatus 112 * @link http://wiki.developers.facebook.com/index.php/Extended_permission 113 */ 114 public function setStatus($status) 115 { 116 $args = array( 117 'session_key' => $this->sessionKey, 118 ); 119 120 if (is_bool($status) && $status === true) { 121 $args['clear'] = 'true'; 122 } else { 123 $args['status'] = $status; 124 } 125 126 $res = $this->callMethod('users.setStatus', $args); 127 return (intval((string)$res) == 1); 128 } 129 130 /** 131 * Get user info 132 * 133 * @param mixed $uids A single uid or array of uids 134 * @param array $fields List of fields to retrieve 135 * 136 * @return object SimpleXmlElement of result 137 * @link http://wiki.developers.facebook.com/index.php/Users.getInfo 138 */ 139 public function getInfo($uids, array $fields = array()) 140 { 141 if (is_array($uids)) { 142 $uids = implode(',', $uids); 143 } 144 145 if (!count($fields)) { 146 $fields = $this->userFields; 147 } 148 149 return $this->callMethod('users.getInfo', array( 150 'session_key' => $this->sessionKey, 151 'uids' => $uids, 152 'fields' => implode(',', $fields) 153 )); 154 } 155 156 /** 157 * Get the currently logged in uid 158 * 159 * Returns the Facebook uid of the person currently "logged in" as 160 * specified by $sessionKey. 161 * 162 * @return string The uid of the person logged in 163 * @see Services_Digg::$sessionKey 164 * @link http://wiki.developers.facebook.com/index.php/Users.getLoggedInUser 165 */ 166 public function getLoggedInUser() 167 { 168 $result = $this->callMethod('users.getLoggedInUser', array( 169 'session_key' => $this->sessionKey 170 )); 171 172 return (string)$result; 173 } 174 175 /** 176 * Has given extended permission 177 * 178 * @param string $perm Permission to check 179 * @param string $uid User's ID, optional if session key present 180 * 181 * @return boolean True if user has enabled extended permission 182 * @link http://wiki.developers.facebook.com/index.php/Users.hasAppPermission 183 */ 184 public function hasAppPermission($perm, $uid = null) 185 { 186 $valid = array( 187 'email', 'offline_access', 'status_update', 'photo_upload', 188 'create_listing', 'create_event', 'rsvp_event', 'sms' 189 ); 190 191 if (!in_array($perm, $valid)) { 192 throw new Services_Facebook_Exception( 193 'Invalid extended permission type supplied: ' . $perm 194 ); 195 } 196 197 $params = array( 198 'ext_perm' => $perm 199 ); 200 201 if ($uid !== null) { 202 $params['uid'] = $uid; 203 } elseif (!empty($this->sessionKey)) { 204 $params['session_key'] = $this->sessionKey; 205 } else { 206 throw new Services_Facebook_Exception('A UID or session key must be ' . 207 'given for hadAppPermission.'); 208 } 209 210 $result = $this->callMethod('users.hasAppPermission', $params); 211 212 return (intval((string)$result) == 1); 213 } 214 215 /** 216 * Get photo 217 * 218 * Get a photo given an user id. Allow different sizes. 219 * 220 * @param string $uid Id of the user you want to get a photo of 221 * @param string $size Size of the photo {@link self::photoSizes} 222 * 223 * @return mixed Photo data 224 */ 225 public function getPhoto($uid, $size = '') 226 { 227 $field = 'pic'; 228 if ($size !== '') { 229 if (!in_array($size, $this->photoSizes)) { 230 throw new Services_Facebook_Exception('Photo size "' . 231 $size . '" is not supported.'); 232 } 233 234 $field .= '_' . $size; 235 } 236 237 $url = (string) $this->getInfo($uid, array($field))->user->$field; 238 239 $ch = curl_init(); 240 curl_setopt($ch, CURLOPT_URL, $url); 241 curl_setopt($ch, CURLOPT_HEADER, false); 242 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 243 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 244 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Services_Facebook::$timeout); 245 $photo = curl_exec($ch); 246 247 if (curl_errno($ch)) { 248 throw new Services_Facebook_Exception( 249 curl_error($ch), 250 curl_errno($ch) 251 ); 252 } 253 curl_close($ch); 254 255 return $photo; 256 } 257} 258 259?> 260