| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * singleton wrapper for twitter class |
|---|
| 4 | */ |
|---|
| 5 | class TwitterUtil { |
|---|
| 6 | private static $uniqueInstance = NULL; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * |
|---|
| 10 | */ |
|---|
| 11 | protected function __construct() { |
|---|
| 12 | |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * |
|---|
| 17 | */ |
|---|
| 18 | private final function __clone() { |
|---|
| 19 | |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * |
|---|
| 24 | */ |
|---|
| 25 | public static function getInstance() { |
|---|
| 26 | if (self::$uniqueInstance === NULL) { |
|---|
| 27 | $email = TWITTER_EMAIL; |
|---|
| 28 | $pass = TWITTER_PASS; |
|---|
| 29 | if(empty($email) || empty($pass)) { |
|---|
| 30 | require_once(WCF_DIR.'lib/system/exception/PermissionDeniedException.class.php'); |
|---|
| 31 | throw new PermissionDeniedException("no login credentials."); |
|---|
| 32 | } |
|---|
| 33 | self::$uniqueInstance = new Twitter($email, $pass); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | return self::$uniqueInstance; |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Twitter interface class |
|---|
| 42 | * Nov 26 2007 Nick Beam |
|---|
| 43 | * Bugs, comments, questions: winkerbeam@gmail.com |
|---|
| 44 | * http://rbrw.net -- http://tinydinosaur.com |
|---|
| 45 | * |
|---|
| 46 | * This is a simple interface to the Twitter API. |
|---|
| 47 | * I've tried to keep as close as possible to the real API |
|---|
| 48 | * calls (some had to be changed due to ambiguity), but all |
|---|
| 49 | * of the arguments are as they are in the official docs. |
|---|
| 50 | * |
|---|
| 51 | * Usage: |
|---|
| 52 | * $twitter = new Twitter("username", "password"); |
|---|
| 53 | * $public_timeline_xml = $twitter->getPublicTimeline("xml"); |
|---|
| 54 | * |
|---|
| 55 | * Methods: |
|---|
| 56 | * getPublicTimeline($format [, $since_id]) |
|---|
| 57 | * getFriendsTimeline($format [, $id [, $since ]]) |
|---|
| 58 | * getUserTimeline($format [, $id [, $count [, $since ]]]) |
|---|
| 59 | * showStatus($format, $id) |
|---|
| 60 | * updateStatus($status) |
|---|
| 61 | * destroyStatus($format, $id) |
|---|
| 62 | * getReplies($format [, $page ]) |
|---|
| 63 | * getFriends($format [, $id ]) |
|---|
| 64 | * getFollowers($format [, $lite ]) |
|---|
| 65 | * getFeatured($format) |
|---|
| 66 | * showUser($format [, $id [, $email ]]) |
|---|
| 67 | * getMessages($format [, $since [, $since_id [, $page ]]]) |
|---|
| 68 | * getSentMessages($format [, $since [, $since_id [, $page ]]]) |
|---|
| 69 | * newMessage($format, $user, $text) |
|---|
| 70 | * destroyMessage($format, $id) |
|---|
| 71 | * createFriendship($format, $id) |
|---|
| 72 | * destroyFriendship($format, $id) |
|---|
| 73 | * verifyCredentials([$format]) |
|---|
| 74 | * endSession() |
|---|
| 75 | * getArchive($format [, $page ]) |
|---|
| 76 | * getFavorites($format [, $id [, $page ]]) |
|---|
| 77 | * createFavorite($format, $id) |
|---|
| 78 | * destroyFavorite($format, $id) |
|---|
| 79 | * lastStatusCode() |
|---|
| 80 | * lastAPICall() |
|---|
| 81 | */ |
|---|
| 82 | class Twitter { |
|---|
| 83 | /* Username:password format string */ |
|---|
| 84 | private $credentials; |
|---|
| 85 | |
|---|
| 86 | /* Contains the last HTTP status code returned */ |
|---|
| 87 | private $http_status; |
|---|
| 88 | |
|---|
| 89 | /* Contains the last API call */ |
|---|
| 90 | private $last_api_call; |
|---|
| 91 | |
|---|
| 92 | /* Twitter class constructor */ |
|---|
| 93 | function Twitter($username, $password) { |
|---|
| 94 | $this->credentials = sprintf("%s:%s", $username, $password); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function getRateLimit($format, $since_id = 0) { |
|---|
| 98 | $api_call = sprintf("account/rate_limit_status.%s", $format); |
|---|
| 99 | return $this->APICall($api_call); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | function getRateLimit($format, $since_id = 0) { |
|---|
| 103 | $api_call = sprintf("account/rate_limit_status.%s", $format); |
|---|
| 104 | return $this->APICall($api_call); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | function getMentions($format, $since_id = 0) { |
|---|
| 108 | $api_call = sprintf("statuses/mentions.%s", $format); |
|---|
| 109 | return $this->APICall($api_call); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | function getPublicTimeline($format, $since_id = 0) { |
|---|
| 113 | $api_call = sprintf("statuses/public_timeline.%s", $format); |
|---|
| 114 | if ($since_id > 0) { |
|---|
| 115 | $api_call .= sprintf("?since_id=%d", $since_id); |
|---|
| 116 | } |
|---|
| 117 | return $this->APICall($api_call); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | function getFriendsTimeline($format, $id = NULL, $since = NULL) { |
|---|
| 121 | if ($id != NULL) { |
|---|
| 122 | $api_call = sprintf("statuses/friends_timeline/%s.%s", $id, $format); |
|---|
| 123 | } |
|---|
| 124 | else { |
|---|
| 125 | $api_call = sprintf("statuses/friends_timeline.%s", $format); |
|---|
| 126 | } |
|---|
| 127 | if ($since != NULL) { |
|---|
| 128 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 129 | } |
|---|
| 130 | return $this->APICall($api_call, true); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | function getUserTimeline($format, $id = NULL, $count = 20, $since = NULL) { |
|---|
| 134 | if ($id != NULL) { |
|---|
| 135 | $api_call = sprintf("statuses/user_timeline/%s.%s", $id, $format); |
|---|
| 136 | } |
|---|
| 137 | else { |
|---|
| 138 | $api_call = sprintf("statuses/user_timeline.%s", $format); |
|---|
| 139 | } |
|---|
| 140 | if ($count != 20) { |
|---|
| 141 | $api_call .= sprintf("?count=%d", $count); |
|---|
| 142 | } |
|---|
| 143 | if ($since != NULL) { |
|---|
| 144 | $api_call .= sprintf("%ssince=%s", (strpos($api_call, "?count=") === false) ? "?" : "&", urlencode($since)); |
|---|
| 145 | } |
|---|
| 146 | return $this->APICall($api_call, true); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | function showStatus($format, $id) { |
|---|
| 150 | $api_call = sprintf("statuses/show/%d.%s", $id, $format); |
|---|
| 151 | return $this->APICall($api_call); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | function updateStatus($format, $status) { |
|---|
| 155 | $status = urlencode(stripslashes(urldecode($status))); |
|---|
| 156 | $api_call = sprintf("statuses/update.%s?status=%s", $format, $status); |
|---|
| 157 | return $this->APICall($api_call, true, true); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | function getReplies($format, $page = 0) { |
|---|
| 161 | $api_call = sprintf("statuses/replies.%s", $format); |
|---|
| 162 | if ($page) { |
|---|
| 163 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 164 | } |
|---|
| 165 | return $this->APICall($api_call, true); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | function destroyStatus($format, $id) { |
|---|
| 169 | $api_call = sprintf("statuses/destroy/%d.%s", $id, $format); |
|---|
| 170 | return $this->APICall($api_call, true); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | function getFriends($format, $page = 1) { |
|---|
| 174 | // take care of the id parameter |
|---|
| 175 | $api_call = sprintf("statuses/friends.%s%s", $format, "?page=$page"); |
|---|
| 176 | return $this->APICall($api_call, true); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | function getFollowers($format, $page = 1) { |
|---|
| 180 | $api_call = sprintf("statuses/followers.%s%s", $format, "?page=$page"); |
|---|
| 181 | return $this->APICall($api_call, true); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | function getFeatured($format) { |
|---|
| 185 | $api_call = sprintf("statuses/featured.%s", $format); |
|---|
| 186 | return $this->APICall($api_call); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | function showUser($format, $id, $email = NULL) { |
|---|
| 190 | if ($email == NULL) { |
|---|
| 191 | $api_call = sprintf("users/show/%s.%s", $id, $format); |
|---|
| 192 | } |
|---|
| 193 | else { |
|---|
| 194 | $api_call = sprintf("users/show.xml?email=%s", $email); |
|---|
| 195 | } |
|---|
| 196 | return $this->APICall($api_call, true); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | function getMessages($format, $since = NULL, $since_id = 0, $page = 1) { |
|---|
| 200 | $api_call = sprintf("direct_messages.%s", $format); |
|---|
| 201 | if ($since != NULL) { |
|---|
| 202 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 203 | } |
|---|
| 204 | if ($since_id > 0) { |
|---|
| 205 | $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id); |
|---|
| 206 | } |
|---|
| 207 | if ($page > 1) { |
|---|
| 208 | $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page); |
|---|
| 209 | } |
|---|
| 210 | return $this->APICall($api_call, true); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | function getSentMessages($format, $since = NULL, $since_id = 0, $page = 1) { |
|---|
| 214 | $api_call = sprintf("direct_messages/sent.%s", $format); |
|---|
| 215 | if ($since != NULL) { |
|---|
| 216 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 217 | } |
|---|
| 218 | if ($since_id > 0) { |
|---|
| 219 | $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id); |
|---|
| 220 | } |
|---|
| 221 | if ($page > 1) { |
|---|
| 222 | $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page); |
|---|
| 223 | } |
|---|
| 224 | return $this->APICall($api_call, true); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | function newMessage($format, $user, $text) { |
|---|
| 228 | $text = urlencode(stripslashes(urldecode($text))); |
|---|
| 229 | $api_call = sprintf("direct_messages/new.%s?user=%s&text=%s", $format, $user, $text); |
|---|
| 230 | return $this->APICall($api_call, true, true); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | function destroyMessage($format, $id) { |
|---|
| 234 | $api_call = sprintf("direct_messages/destroy/%s.%s", $id, $format); |
|---|
| 235 | return $this->APICall($api_call, true); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | function createFriendship($format, $id) { |
|---|
| 239 | $api_call = sprintf("friendships/create/%d.%s", $id, $format); |
|---|
| 240 | return $this->APICall($api_call, true, true); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | function destroyFriendship($format, $id) { |
|---|
| 244 | $api_call = sprintf("friendships/destroy/%s.%s", $id, $format); |
|---|
| 245 | return $this->APICall($api_call, true, true); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | function verifyCredentials($format = NULL) { |
|---|
| 249 | $api_call = sprintf("account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL); |
|---|
| 250 | return $this->APICall($api_call, true); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | function endSession() { |
|---|
| 254 | $api_call = "account/end_session"; |
|---|
| 255 | return $this->APICall($api_call, true); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | function getArchive($format, $page = 1) { |
|---|
| 259 | $api_call = sprintf("account/archive.%s", $format); |
|---|
| 260 | if ($page > 1) { |
|---|
| 261 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 262 | } |
|---|
| 263 | return $this->APICall($api_call, true); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | function getFavorites($format, $id = NULL, $page = 1) { |
|---|
| 267 | if ($id == NULL) { |
|---|
| 268 | $api_call = sprintf("favourings.%s", $format); |
|---|
| 269 | } |
|---|
| 270 | else { |
|---|
| 271 | $api_call = sprintf("favourings/%s.%s", $id, $format); |
|---|
| 272 | } |
|---|
| 273 | if ($page > 1) { |
|---|
| 274 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 275 | } |
|---|
| 276 | return $this->APICall($api_call, true); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | function createFavorite($format, $id) { |
|---|
| 280 | $api_call = sprintf("favourings/create/%d.%s", $id, $format); |
|---|
| 281 | return $this->APICall($api_call, true); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | function destroyFavorite($format, $id) { |
|---|
| 285 | $api_call = sprintf("favourings/destroy/%d.%s", $id, $format); |
|---|
| 286 | return $this->APICall($api_call, true); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | private function APICall($api_url, $require_credentials = false, $http_post = false) { |
|---|
| 290 | $api_url = 'http://api.twitter.com/1/'; |
|---|
| 291 | |
|---|
| 292 | $curl_handle = curl_init(); |
|---|
| 293 | curl_setopt($curl_handle, CURLOPT_URL, $api_url); |
|---|
| 294 | curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:')); |
|---|
| 295 | |
|---|
| 296 | if ($require_credentials) { |
|---|
| 297 | curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials); |
|---|
| 298 | } |
|---|
| 299 | if ($http_post) { |
|---|
| 300 | curl_setopt($curl_handle, CURLOPT_POST, true); |
|---|
| 301 | } |
|---|
| 302 | curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE); |
|---|
| 303 | $twitter_data = curl_exec($curl_handle); |
|---|
| 304 | $this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); |
|---|
| 305 | $this->last_api_call = $api_url; |
|---|
| 306 | curl_close($curl_handle); |
|---|
| 307 | return $twitter_data; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | function lastStatusCode() { |
|---|
| 311 | return $this->http_status; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | function lastAPICall() { |
|---|
| 315 | return $this->last_api_call; |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | ?> |
|---|