]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Twitter/twitter.class.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Twitter / twitter.class.php
1 <?php
2 /**
3  * Sean's Simple Twitter Library
4  *
5  * Probably a little more or a little less than you need.
6  *
7  * Copyright 2008, Sean Coates
8  * Usage of the works is permitted provided that this instrument is retained
9  * with the works, so that any entity that uses the works is notified of this
10  * instrument.
11  * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
12  * ( Fair License - http://www.opensource.org/licenses/fair.php )
13  * Short license: do whatever you like with this.
14  *
15  * komode: le=unix language=php codepage=utf8 tab=4 notabs indent=4
16  */
17 class Twitter {
18
19     /**
20      * Base URL for Twitter API
21      *
22      * Do not specify user/password in URL
23      */
24     protected $baseUrl = 'http://twitter.com/';
25     
26     /**
27      * Full base URL (includes user/pass)
28      *
29      * (created in Init)
30      */
31     protected $baseUrlFull = null;
32     
33     /**
34      * Twitter API user
35      */
36     protected $user;
37     
38     /**
39      * Twitter API password
40      */
41     protected $pass;
42     
43     /**
44      * Constructor; sets up configuration.
45      * 
46      * @param string $user Twitter user name; null for limited read-only access
47      * @param string $pass Twitter password; null for limited read-only access
48      */
49     public function __construct($user=null, $pass=null) {
50         $this->baseUrlFull = $this->baseUrl;
51         if (null !== $user) {
52             // user is defined, so use it in the URL
53             $this->user = $user;
54             $this->pass = $pass;
55             $parsed = parse_url($this->baseUrl);
56             $this->baseUrlFull = $parsed['scheme'] . '://' . $this->user . ':' .
57                 $this->pass . '@' . $parsed['host'];
58             // port (optional)
59             if (isset($parsed['port']) && is_numeric($parsed['port'])) {
60                 $this->baseUrlFull .= ':' . $parsed['port'];
61             }
62             // append path (default: /)
63             if (isset($parsed['path'])) {
64                 $this->baseUrlFull .= $parsed['path'];
65             } else {
66                 $this->baseUrlFull .= '/';
67             }
68         }
69     }
70
71     /**
72      * Fetches a tweet by its number/id
73      *
74      * @param int $num the tweet id/number
75      * @return string (null on failure)
76      */
77     public function getTweetByNum($num) {
78         if (!is_numeric($num)) {
79             return;
80         }
81         $tweet = json_decode(file_get_contents($this->getUrlStatus($num)));
82         return $tweet;
83     }
84
85     /**
86      * Reads [last] tweet from user
87      *
88      * @param string $tweeter the tweeter username
89      * @param int $num this many tweets ago (1 = current tweet)
90      * @return string (false on failure)
91      */
92     public function getLastTweet($tweeter, $num = 1)
93     {
94         $source = json_decode(file_get_contents($this->getUrlUserTimeline($tweeter)));
95         if ($num > count($source)) {
96             return false;
97         }
98         $tweet = $source[$num - 1];
99         if (!isset($tweet->user->screen_name) || !$tweet->user->screen_name) {
100             return false;
101         }
102         return $tweet;
103     }
104     
105     /**
106      * fetches mentions for a user
107      */
108     public function getMentions($sinceId=null, $count=20) {
109         return json_decode(file_get_contents($this->getUrlMentions($sinceId, $count)));
110     }
111     
112     /**
113      * Fetches followers for a user
114      */
115     public function getFollowers($cursor=-1) {
116         return json_decode(file_get_contents($this->getUrlFollowers($cursor)));
117     }
118     
119     /**
120      * Follow a userid
121      */
122     public function follow($userId) {
123         $params = array(
124             'http' => array(
125                 'method' => 'POST',
126                 'content' => array(),
127                 'header' => 'Content-type: application/x-www-form-urlencoded',
128             )
129         );
130         $ctx = stream_context_create($params);
131         $fp = fopen($this->getUrlFollow($userId), 'rb', false, $ctx);
132         if (!$fp) {
133             return false;
134         }
135         $response = stream_get_contents($fp);
136         if ($response === false) {
137             return false;
138         }
139         $response = json_decode($response);
140         return $response;
141     }
142     
143     /**
144      * fetches DMs for a user
145      */
146     public function getDMs($sinceId=null, $count=20, $page=1) {
147         return json_decode(file_get_contents($this->getUrlDMs($sinceId, $count, $page)));
148     }
149     
150     /**
151      * Send DM
152      */
153     public function sendDM($screenName, $text) {
154         $data = http_build_query(array('screen_name'=>$screenName, 'text'=>$text));
155         $params = array(
156             'http' => array(
157                 'method' => 'POST',
158                 'content' => $data,
159                 'header' => 'Content-type: application/x-www-form-urlencoded',
160             )
161         );
162         $ctx = stream_context_create($params);
163         $fp = fopen($this->getUrlSendDM(), 'rb', false, $ctx);
164         if (!$fp) {
165             return false;
166         }
167         $response = stream_get_contents($fp);
168         if ($response === false) {
169             return false;
170         }
171         $response = json_decode($response);
172         return $response;
173     }
174
175     /**
176      * Sends a tweet
177      *
178      * @param string $txt the tweet text to send
179      * @return string URL of tweet (or false on failure)
180      */
181     public function sendTweet($txt, $limit=true) {
182         if ($limit) {
183             $txt = substr($txt, 0, 140); // twitter message size limit
184         }
185         $data = 'status=' . urlencode($txt);
186         $params = array(
187             'http' => array(
188                 'method' => 'POST',
189                 'content' => $data,
190                 'header' => 'Content-type: application/x-www-form-urlencoded',
191             )
192         );
193         $ctx = stream_context_create($params);
194         $fp = fopen($this->getUrlTweetPost(), 'rb', false, $ctx);
195         if (!$fp) {
196             return false;
197         }
198         $response = stream_get_contents($fp);
199         if ($response === false) {
200             return false;
201         }
202         $response = json_decode($response);
203         return $response;
204     }
205     
206     /**
207      * Returns the base API URL
208      */
209     protected function getUrlApi() {
210         return $this->baseUrlFull;
211     }
212     
213     /**
214      * Returns the status URL
215      *
216      * @param int $num the tweet number
217      */
218     protected function getUrlStatus($num) {
219         return $this->getUrlApi() . 'statuses/show/'. urlencode($num) .'.json';
220     }
221     
222     /**
223      * Returns the user timeline URL
224      */
225     protected function getUrlUserTimeline($user) {
226         return $this->getUrlApi() . 'statuses/user_timeline/'. urlencode($user) .'.json';
227     }
228     
229     /**
230      * Returns the tweet posting URL
231      */
232     protected function getUrlTweetPost() {
233         return $this->getUrlApi() . 'statuses/update.json';
234     }
235     
236     /**
237      * Output URL: status
238      */
239     public function getUrlOutputStatus(StdClass $tweet) {
240         return $this->baseUrl . urlencode($tweet->user->screen_name) . '/statuses/' . urlencode($tweet->id);
241     }
242     
243     /**
244      * Return mentions URL
245      */
246     public function getUrlMentions($sinceId=null, $count=20) {
247         $url = $this->baseUrlFull . 'statuses/mentions.json?count=' . urlencode($count);
248         if ($sinceId !== null) {
249             $url .= '&since_id=' . urlencode($sinceId);
250         }
251         return $url;
252     }
253     
254     /**
255      * Returns the followers URL
256      */
257     public function getUrlFollowers($cursor=-1) {
258         return $this->baseUrlFull . 'statuses/followers.json?cursor=' . ((int)$cursor);
259     }
260     
261     /**
262      * Returns the follow-user URL
263      */
264     public function getUrlFollow($userid) {
265         return $this->baseUrlFull . 'friendships/create/' . ((int) $userid) . '.json';
266     }
267     
268     /**
269      * Returns the get DMs URL
270      */
271     public function getUrlDMs($sinceId=null, $count=20, $page=1) {
272         $url = $this->baseUrlFull . 'direct_messages.json?';
273         if ($sinceId !== null) {
274             $url .= 'since_id=' . urlencode($sinceId);
275         }
276         $url .= "&page={$page}";
277         $url .= "&count={$count}";
278         return $url;
279     }
280
281     /**
282      * Returns the send DM URL
283      */
284     public function getURLSendDM() {
285         return $this->baseUrlFull . 'direct_messages/new.json';
286     }
287 }