| 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("http://twitter.com/account/rate_limit_status.%s", $format); |
|---|
| 99 | return $this->APICall($api_call); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | function getPublicTimeline($format, $since_id = 0) { |
|---|
| 103 | $api_call = sprintf("http://twitter.com/statuses/public_timeline.%s", $format); |
|---|
| 104 | if ($since_id > 0) { |
|---|
| 105 | $api_call .= sprintf("?since_id=%d", $since_id); |
|---|
| 106 | } |
|---|
| 107 | return $this->APICall($api_call); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | function getFriendsTimeline($format, $id = NULL, $since = NULL) { |
|---|
| 111 | if ($id != NULL) { |
|---|
| 112 | $api_call = sprintf("http://twitter.com/statuses/friends_timeline/%s.%s", $id, $format); |
|---|
| 113 | } |
|---|
| 114 | else { |
|---|
| 115 | $api_call = sprintf("http://twitter.com/statuses/friends_timeline.%s", $format); |
|---|
| 116 | } |
|---|
| 117 | if ($since != NULL) { |
|---|
| 118 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 119 | } |
|---|
| 120 | return $this->APICall($api_call, true); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | function getUserTimeline($format, $id = NULL, $count = 20, $since = NULL) { |
|---|
| 124 | if ($id != NULL) { |
|---|
| 125 | $api_call = sprintf("http://twitter.com/statuses/user_timeline/%s.%s", $id, $format); |
|---|
| 126 | } |
|---|
| 127 | else { |
|---|
| 128 | $api_call = sprintf("http://twitter.com/statuses/user_timeline.%s", $format); |
|---|
| 129 | } |
|---|
| 130 | if ($count != 20) { |
|---|
| 131 | $api_call .= sprintf("?count=%d", $count); |
|---|
| 132 | } |
|---|
| 133 | if ($since != NULL) { |
|---|
| 134 | $api_call .= sprintf("%ssince=%s", (strpos($api_call, "?count=") === false) ? "?" : "&", urlencode($since)); |
|---|
| 135 | } |
|---|
| 136 | return $this->APICall($api_call, true); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | function showStatus($format, $id) { |
|---|
| 140 | $api_call = sprintf("http://twitter.com/statuses/show/%d.%s", $id, $format); |
|---|
| 141 | return $this->APICall($api_call); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | function updateStatus($format, $status) { |
|---|
| 145 | $status = urlencode(stripslashes(urldecode($status))); |
|---|
| 146 | $api_call = sprintf("http://twitter.com/statuses/update.%s?status=%s", $format, $status); |
|---|
| 147 | return $this->APICall($api_call, true, true); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | function getReplies($format, $page = 0) { |
|---|
| 151 | $api_call = sprintf("http://twitter.com/statuses/replies.%s", $format); |
|---|
| 152 | if ($page) { |
|---|
| 153 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 154 | } |
|---|
| 155 | return $this->APICall($api_call, true); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | function destroyStatus($format, $id) { |
|---|
| 159 | $api_call = sprintf("http://twitter.com/statuses/destroy/%d.%s", $id, $format); |
|---|
| 160 | return $this->APICall($api_call, true); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | function getFriends($format, $page = 1) { |
|---|
| 164 | // take care of the id parameter |
|---|
| 165 | $api_call = sprintf("http://twitter.com/statuses/friends.%s%s", $format, "?page=$page"); |
|---|
| 166 | return $this->APICall($api_call, true); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | function getFollowers($format, $page = 1) { |
|---|
| 170 | $api_call = sprintf("http://twitter.com/statuses/followers.%s%s", $format, "?page=$page"); |
|---|
| 171 | return $this->APICall($api_call, true); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | function getFeatured($format) { |
|---|
| 175 | $api_call = sprintf("http://twitter.com/statuses/featured.%s", $format); |
|---|
| 176 | return $this->APICall($api_call); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | function showUser($format, $id, $email = NULL) { |
|---|
| 180 | if ($email == NULL) { |
|---|
| 181 | $api_call = sprintf("http://twitter.com/users/show/%s.%s", $id, $format); |
|---|
| 182 | } |
|---|
| 183 | else { |
|---|
| 184 | $api_call = sprintf("http://twitter.com/users/show.xml?email=%s", $email); |
|---|
| 185 | } |
|---|
| 186 | return $this->APICall($api_call, true); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | function getMessages($format, $since = NULL, $since_id = 0, $page = 1) { |
|---|
| 190 | $api_call = sprintf("http://twitter.com/direct_messages.%s", $format); |
|---|
| 191 | if ($since != NULL) { |
|---|
| 192 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 193 | } |
|---|
| 194 | if ($since_id > 0) { |
|---|
| 195 | $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id); |
|---|
| 196 | } |
|---|
| 197 | if ($page > 1) { |
|---|
| 198 | $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page); |
|---|
| 199 | } |
|---|
| 200 | return $this->APICall($api_call, true); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | function getSentMessages($format, $since = NULL, $since_id = 0, $page = 1) { |
|---|
| 204 | $api_call = sprintf("http://twitter.com/direct_messages/sent.%s", $format); |
|---|
| 205 | if ($since != NULL) { |
|---|
| 206 | $api_call .= sprintf("?since=%s", urlencode($since)); |
|---|
| 207 | } |
|---|
| 208 | if ($since_id > 0) { |
|---|
| 209 | $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id); |
|---|
| 210 | } |
|---|
| 211 | if ($page > 1) { |
|---|
| 212 | $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page); |
|---|
| 213 | } |
|---|
| 214 | return $this->APICall($api_call, true); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | function newMessage($format, $user, $text) { |
|---|
| 218 | $text = urlencode(stripslashes(urldecode($text))); |
|---|
| 219 | $api_call = sprintf("http://twitter.com/direct_messages/new.%s?user=%s&text=%s", $format, $user, $text); |
|---|
| 220 | return $this->APICall($api_call, true, true); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | function destroyMessage($format, $id) { |
|---|
| 224 | $api_call = sprintf("http://twitter.com/direct_messages/destroy/%s.%s", $id, $format); |
|---|
| 225 | return $this->APICall($api_call, true); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | function createFriendship($format, $id) { |
|---|
| 229 | $api_call = sprintf("http://twitter.com/friendships/create/%d.%s", $id, $format); |
|---|
| 230 | return $this->APICall($api_call, true, true); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | function destroyFriendship($format, $id) { |
|---|
| 234 | $api_call = sprintf("http://twitter.com/friendships/destroy/%s.%s", $id, $format); |
|---|
| 235 | return $this->APICall($api_call, true, true); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | function verifyCredentials($format = NULL) { |
|---|
| 239 | $api_call = sprintf("http://twitter.com/account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL); |
|---|
| 240 | return $this->APICall($api_call, true); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | function endSession() { |
|---|
| 244 | $api_call = "http://twitter.com/account/end_session"; |
|---|
| 245 | return $this->APICall($api_call, true); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | function getArchive($format, $page = 1) { |
|---|
| 249 | $api_call = sprintf("http://twitter.com/account/archive.%s", $format); |
|---|
| 250 | if ($page > 1) { |
|---|
| 251 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 252 | } |
|---|
| 253 | return $this->APICall($api_call, true); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | function getFavorites($format, $id = NULL, $page = 1) { |
|---|
| 257 | if ($id == NULL) { |
|---|
| 258 | $api_call = sprintf("http://twitter.com/favourings.%s", $format); |
|---|
| 259 | } |
|---|
| 260 | else { |
|---|
| 261 | $api_call = sprintf("http://twitter.com/favourings/%s.%s", $id, $format); |
|---|
| 262 | } |
|---|
| 263 | if ($page > 1) { |
|---|
| 264 | $api_call .= sprintf("?page=%d", $page); |
|---|
| 265 | } |
|---|
| 266 | return $this->APICall($api_call, true); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | function createFavorite($format, $id) { |
|---|
| 270 | $api_call = sprintf("http://twitter.com/favourings/create/%d.%s", $id, $format); |
|---|
| 271 | return $this->APICall($api_call, true); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | function destroyFavorite($format, $id) { |
|---|
| 275 | $api_call = sprintf("http://twitter.com/favourings/destroy/%d.%s", $id, $format); |
|---|
| 276 | return $this->APICall($api_call, true); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | private function APICall($api_url, $require_credentials = false, $http_post = false) { |
|---|
| 280 | $curl_handle = curl_init(); |
|---|
| 281 | curl_setopt($curl_handle, CURLOPT_URL, $api_url); |
|---|
| 282 | curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:')); |
|---|
| 283 | |
|---|
| 284 | if ($require_credentials) { |
|---|
| 285 | curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials); |
|---|
| 286 | } |
|---|
| 287 | if ($http_post) { |
|---|
| 288 | curl_setopt($curl_handle, CURLOPT_POST, true); |
|---|
| 289 | } |
|---|
| 290 | curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE); |
|---|
| 291 | $twitter_data = curl_exec($curl_handle); |
|---|
| 292 | $this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE); |
|---|
| 293 | $this->last_api_call = $api_url; |
|---|
| 294 | curl_close($curl_handle); |
|---|
| 295 | return $twitter_data; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | function lastStatusCode() { |
|---|
| 299 | return $this->http_status; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | function lastAPICall() { |
|---|
| 303 | return $this->last_api_call; |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | ?> |
|---|