OSDN Git Service

embr
[embrj/master.git] / lib / twitter.php
1 <?php
2 if (get_magic_quotes_gpc()) {
3
4         $_REQUEST = array_map('stripslashes', $_REQUEST);
5         $_GET     = array_map('stripslashes', $_GET);
6         $_POST    = array_map('stripslashes', $_POST);
7         $_COOKIE  = array_map('stripslashes', $_COOKIE);
8
9 }
10 class twitter{
11         var $username='';
12         var $password='';
13         var $user_agent='API';
14         var $type='json';
15
16         var $headers=array('Expect:', 'X-Twitter-Client: ','X-Twitter-Client-Version: ','X-Twitter-Client-URL: ');
17         var $responseInfo=array();
18         var $suppress_response_code = false;
19         var $debug = false;
20         public $timeout = 5;
21         public $connecttimeout = 5;
22
23         function twitter($username = '', $password = '', $type = 'json')
24         {
25                 if ($username != '' && $password != '') {
26                         $this->username = $username;
27                         $this->password = $password;
28                         $this->type = $type;
29                 }
30         }
31
32         function veverify()
33         {
34                 $request = API_URL . '/account/verify_credentials.' . $this->type;
35                 return $this->objectify( $this->process($request, false, 0) );
36         }
37
38         /**** Get user theme ****/
39         function getTheme()
40         {
41                 $twit = $this->veverify();
42                 $theme = array( 'background-color' => $twit->profile_background_color, 'background-image-url' => $twit->profile_background_image_url, 'background-tile' => $twit->profile_background_tile == true ? 'repeat' : 'no-repeat', 'link-color' => $twit->profile_link_color, 'sidebar-border-color' => $twit->profile_sidebar_border_color, 'sidebar-fill-color' => $twit->profile_sidebar_fill_color, 'text-color' => $twit->profile_text_color);
43                 return $theme;
44         }
45
46
47         /**** Status Methods ****/
48
49         function showStatus( $id )
50         {
51                 if( !in_array( $this->type, array( 'xml','json' ) ) )
52                         return false;
53
54                 $request = API_URL . '/statuses/show/'.$id . '.' . $this->type;
55                 return $this->objectify( $this->process($request) );
56         }
57
58         function update( $status, $replying_to = false )
59         {
60                 if( !in_array( $this->type, array( 'xml','json' ) ) )
61                         return false;
62
63                 $args = array();
64                 if( $status )
65                         $args['status'] = $status;
66                 if( $replying_to )
67                         $args['in_reply_to_status_id'] = $replying_to;
68                 $args['source'] = 'API';
69
70                 $qs = '';
71                 if( !empty( $args ) )
72                         $qs = $this->_glue( $args );
73
74                 $request = API_URL . '/statuses/update.' . $this->type . $qs;
75
76                 return $this->objectify( $this->process($request, true) );
77         }
78
79         function deleteStatus( $id ) {
80                 if( !in_array( $this->type, array( 'xml','json' ) ) )
81                         return false;
82
83                 $request = API_URL . '/statuses/destroy/' . $id . '.' . $this->type;
84                 return $this->objectify( $this->process( $request, true ) );
85         }
86
87
88         /**** Timeline Methods ****/
89
90         function publicTimeline( $sinceid = false )
91         {
92                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
93                         return false;
94
95                 $qs='';
96                 if( $sinceid !== false )
97                         $qs = '?since_id=' . intval($sinceid);
98                 $request = API_URL . '/statuses/public_timeline.' . $this->type . $qs;
99
100                 return $this->objectify( $this->process($request) );
101         }
102
103         function userTimeline($page=false,$id=false,$count=false,$since_id=false)
104         {
105                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
106                         return false;
107
108                 $args = array();
109                 if( $id )
110                         $args['id'] = $id;
111                 if( $count )
112                         $args['count'] = (int) $count;
113                 if( $since_id )
114                         $args['since_id'] = (int) $since_id;
115                 if( $page )
116                         $args['page'] = (int) $page;
117
118                 $qs = '';
119                 if( !empty( $args ) )
120                         $qs = $this->_glue( $args );
121
122                 if( $id === false )
123                         $request = API_URL . '/statuses/user_timeline.' . $this->type . $qs;
124                 else
125                         $request = API_URL . '/statuses/user_timeline/' . rawurlencode($id) . '.' . $this->type . $qs;
126                 $response = $this->process($request);
127                 if($response == false){
128                         return 'protected';
129                 }
130                 return $this->objectify($response);
131
132         }
133
134         function friendsTimeline( $page = false, $since_id = false,  $count = false )
135         {
136                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
137                         return false;
138
139                 $args = array();
140                 if( $count )
141                         $args['count'] = $count;
142                 if( $since_id )
143                         $args['since_id'] = $since_id;
144                 if( $page )
145                         $args['page'] = (int) $page;
146
147                 $qs = '';
148                 if( !empty( $args ) )
149                         $qs = $this->_glue( $args );
150
151                 $request = API_URL . '/statuses/friends_timeline.' . $this->type . $qs;
152                 return $this->objectify( $this->process($request) );
153
154         }
155
156         function replies( $page = false, $since_id = false )
157         {
158                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
159                         return false;
160
161                 $args = array();
162                 if( $page )
163                         $args['page'] = (int) $page;
164                 if( $since_id )
165                         $args['since_id'] = $since_id;
166
167                 $qs = '';
168                 if( !empty( $args ) )
169                         $qs = $this->_glue( $args );
170
171                 $request = API_URL . '/statuses/mentions.' . $this->type . $qs;    
172
173                 return $this->objectify( $this->process($request) );
174
175         }
176
177
178         /**** Direct Message Methods ****/
179
180         function directMessages( $page = false, $since_id = false, $count = null )
181         {
182                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
183                         return false;
184
185                 $qs='?';
186                 $qsparams = array();
187                 if( $since_id )
188                         $qsparams['since_id'] = $since_id;
189                 if( $page )
190                         $qsparams['page'] = (int) $page;
191
192                 $request = API_URL . '/direct_messages.' . $this->type . '?' . $this->arrToStr($qsparams);
193
194                 return $this->objectify( $this->process($request) );
195         }
196
197         function sentDirectMessage( $page = false, $since = false, $since_id = false )
198         {
199                 if( !in_array( $this->type, array( 'xml','json' ) ) )
200                         return false;
201
202                 $qs = '?';
203                 $qsparams = array();
204                 if( $since !== false )
205                         $qsparams['since'] = rawurlencode($since);
206                 if( $since_id )
207                         $qsparams['since_id'] = (int) $since_id;
208                 if( $page )
209                         $qsparams['page'] = (int) $page;
210
211                 $request = API_URL . '/direct_messages/sent.' . $this->type  . '?' . $this->arrToStr($qsparams);
212                 return $this->objectify( $this->process($request) );
213         }
214
215         function sendDirectMessage( $user, $text )
216         {
217                 if( !in_array( $this->type, array( 'xml','json' ) ) )
218                         return false;
219
220                 $request = API_URL . '/direct_messages/new.' . $this->type;
221                 $postargs = 'user=' . rawurlencode($user) . '&text=' . rawurlencode($text);
222
223                 return $this->objectify( $this->process($request, $postargs) );
224         }
225
226         function deleteDirectMessage( $id )
227         {
228                 if( !in_array( $this->type, array( 'xml','json' ) ) )
229                         return false;
230
231                 $request = API_URL . '/direct_messages/destroy/' . $id . '.' . $this->type;
232                 return $this->objectify( $this->process( $request, true ) );
233         }
234
235
236         /**** User Methods ****/
237
238         function showUser( $id = false , $email = false, $user_id = false, $screen_name=false )
239         {
240                 if( !in_array( $this->type, array( 'xml','json' ) ) )
241                         return false;
242
243                 if (!$id) 
244                         $id = $this->username;
245                 if( $user_id ) :
246                         $qs = '?user_id=' . $user_id;
247                 elseif ( $screen_name ) :
248                         $qs = '?screen_name=' . (string) $screen_name;
249                 elseif ( $email ) :
250                         $qs = '?email=' . (string) $email;
251                 else :
252                         $qs = $id;
253
254 endif;
255
256 $request = API_URL . '/users/show/' . $qs . '.' . $this->type;
257
258 return $this->objectify( $this->process($request) );
259         }
260
261         function friends( $id = false, $page = false , $count = 30 )
262         {
263                 if( !in_array( $this->type, array( 'xml','json' ) ) )
264                         return false;
265
266                 $args = array();
267                 if( $id )
268                         $args['id'] = $id;
269                 if( $count )
270                         $args['count'] = (int) $count;
271                 $args['cursor'] = $page ? $page : -1; // ¸ü»»ÁËÅжϱí´ïʽ
272                 $qs = '';
273                 if( !empty( $args ) )
274                         $qs = $this->_glue( $args );
275
276                 $request = API_URL . ($id ?  "/statuses/friends/$id.$this->type" : "/statuses/friends.$this->type");
277                 $request .= $qs;
278                 return $this->objectify( $this->process($request) );
279         }
280
281         function relationship($target, $source = false){
282                 if($target == $this->username){
283                         return -1;
284                 }
285                 $args = array();
286                 $args['target_screen_name'] = $target;
287                 if($source){
288                         $args['source_screen_name'] = $source;
289                 }
290                 $request = API_TWITTER."/1/friendships/show.$this->type".$this->_glue($args);
291                 return $this->objectify($this->process($request));
292         }
293
294         function followers( $id = false, $page = false , $count = 30 )
295         {
296                 if( !in_array( $this->type, array( 'xml','json' ) ) )
297                         return false;
298
299                 $args = array();
300                 if( $id )
301                         $args['id'] = $id;
302                 if( $count )
303                         $args['count'] = (int) $count;
304                 $args['cursor'] = $page ? $page : -1; // ¸ü»»ÁËÅжϱí´ïʽ
305                 $qs = '';
306                 if( !empty( $args ) )
307                         $qs = $this->_glue( $args );
308
309                 $request = API_URL . ($id ?  "/statuses/followers/$id.$this->type" : "/statuses/followers.$this->type");
310                 $request .= $qs;
311
312                 return $this->objectify( $this->process($request) );
313         }
314
315         /****** Favorites ******/
316
317         function getFavorites( $page=false )
318         {
319                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
320                         return false;
321
322                 if( $page != false )
323                         $qs = '?page=' . $page;
324
325                 $request = API_URL . '/favorites.' . $this->type . $qs; 
326                 return $this->objectify( $this->process($request) );
327         }
328
329         function makeFavorite( $id )
330         {
331                 if( !in_array( $this->type, array( 'xml','json' ) ) )
332                         return false;
333
334                 $request = API_URL . '/favorites/create/' . $id . '.' . $this->type;
335                 return $this->objectify( $this->process($request, $args) );
336         }
337
338         function removeFavorite( $id )
339         {
340                 if( !in_array( $this->type, array( 'xml','json' ) ) )
341                         return false;
342
343                 $request = API_URL . '/favorites/destroy/' . $id . '.' . $this->type;
344                 return $this->objectify( $this->process($request, true) );      
345         }
346
347
348         /**** Friendship Methods ****/
349
350         function isFriend( $user_a, $user_b )
351         {
352                 if( !in_array( $this->type, array( 'xml','json' ) ) )
353                         return false;
354
355                 $args = array();
356                 $args['user_a'] = $user_a;
357                 $args['user_b'] = $user_b;
358                 $qs = $this->_glue( $args );
359
360                 $request = API_URL . '/friendships/exists.' . $this->type . $qs;
361                 return $this->objectify( $this->process($request) );
362         }
363
364         function followUser( $id, $notifications = false )
365         {
366                 if( !in_array( $this->type, array( 'xml','json' ) ) )
367                         return false;
368
369                 $request = API_URL . '/friendships/create/' . $id . '.' . $this->type;
370                 if( $notifications )
371                         $request .= '?follow=true';
372
373                 return $this->objectify( $this->process($request, true) );
374         }
375
376         function destroyUser( $id )
377         {
378                 if( !in_array( $this->type, array( 'xml','json' ) ) )
379                         return false;
380
381                 $request = API_URL . '/friendships/destroy/' . $id . '.' . $this->type;
382                 return $this->objectify( $this->process($request, true) );
383         }
384
385         /****** Block Methods ******/
386         function blockingList($page = false){
387                 $url = API_URL."/blocks/blocking.$this->type";
388                 if($page){
389                         $url .= "?page=$page";
390                 }
391                 return $this->objectify($this->process($url));
392         }
393
394         function blockingIDs(){
395                 $url = API_URL."blocks/blocking/ids.$this->type";
396                 return $this->objectify($this->process($url));
397         }
398
399         function blockUser($id)
400         {
401                 if( !in_array( $this->type, array( 'xml','json' ) ) )
402                         return false;
403                 $request = API_URL . '/blocks/create/' . $id . '.' . $this->type;
404                 $args = array();
405                 $arps['post_method'] = 1;
406                 $result = $this->curl_process($request, $args);
407                 if($result->error == 'Not found'){
408                         return false;
409                 }else{
410                         return $result;
411                 }
412         }
413
414         function unblockUser($id)
415         {
416                 if( !in_array( $this->type, array( 'xml','json' ) ) )
417                         return false;
418                 $request = API_URL . '/blocks/destroy/' . $id . '.' . $this->type;
419                 $args = array();
420                 $arps['post_method'] = 1;
421                 $result = $this->curl_process($request, $args);
422                 if($result->error == 'Not found'){
423                         return false;
424                 }else{
425                         return $result;
426                 }
427         }
428
429         function isBlocked($id)
430         {               
431                 $request = API_URL . '/blocks/exists/' . $id . '.' . $this->type;
432                 $result = $this->curl_process($request);
433                 if($result->error == 'Not found'){
434                         return "false";
435                 }else{
436                         return $result;
437                 }
438         }
439
440         /****** Trends ******/
441         function trends(){
442                 $url = "http://search.twitter.com/trends.$this->type";
443                 return $this->curl_process($url);
444         }
445
446         /* ---------- Spam ---------- */
447         function reportSpam($screen_name){
448                 $url = API_TWITTER."/1/report_spam.$this->type";
449                 $args = array();
450                 $args['screen_name'] = $screen_name;
451                 return $this->curl_process($url, $args);
452         }
453
454         /****** Social Graph ******/
455
456         function socialGraphFollowing( $id = false)
457         {
458                 if( !in_array( $this->type, array( 'xml','json' ) ) )
459                         return false;
460
461                 $request = API_URL . '/friends/ids';
462                 if( $id )
463                         $request .= '/' . (int) $id . '.' . $this->type;
464
465                 return $this->objectify( $this->process($request) );
466         }
467
468         function socialGraphFollowedBy( $id = false )
469         {
470                 if( !in_array( $this->type, array( 'xml','json' ) ) )
471                         return false;
472
473                 $request = API_URL . '/followers/ids';
474                 if( $id )
475                         $request .= '/' . (int) $id . '.' . $this->type;
476
477                 return $this->objectify( $this->process($request) );
478         }
479
480
481         /****** Account Methods ******/
482
483         function updateDevice( $device )
484         {
485                 if( !in_array( $this->type, array( 'xml','json' ) ) )
486                         return false;
487
488                 if( !in_array( $device, array('im','sms','none') ) )
489                         return false;
490
491                 $qs = '?device=' . $device;
492                 $request = API_URL . '/account/update_delivery_device.' . $this->type . $qs;
493                 return $this->objectify( $this->process( $request ) );
494         }
495
496         function updateAvatar( $file )
497         {
498                 if( !in_array( $this->type, array( 'xml','json' ) ) )
499                         return false;
500
501                 // Adding @ ensures the POST will be raw multipart data encoded. This MUST be a file, not a URL. Handle it outside of the class.
502                 $postdata = array( 'image' => "@$file");
503                 $request = API_URL . '/account/update_profile_image.' . $this->type;
504                 return $this->objectify( $this->process( $request, $postdata ) );
505         }
506
507         function updateBackground( $file )
508         {
509                 if( !in_array( $this->type, array( 'xml','json' ) ) )
510                         return false;
511
512                 // Adding @ ensures the POST will be raw multipart data encoded. This MUST be a file, not a URL. Handle it outside of the class.
513                 $postdata = array( 'image' => "@$file");
514                 $request = API_URL . '/account/update_profile_background_image.' . $this->type;
515                 return $this->objectify( $this->process( $request, $postdata ) );
516         }
517
518         function updateProfile( $fields = array() )
519         {
520                 if( !in_array( $this->type, array( 'xml','json' ) ) )
521                         return false;
522
523                 $postdata = array();
524                 foreach( $fields as $pk => $pv ) :
525                         switch( $pk ) 
526                         {
527                         case 'name' :
528                                 $postdata[$pk] = (string) substr( $pv, 0, 20 );
529                                 break;
530                         case 'email' :
531                                 if( preg_match( '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $pv ) )
532                                         $postdata[$pk] = (string) $pv;
533                                 break;
534                         case 'url' :
535                                 $postdata[$pk] = (string) substr( $pv, 0, 100 );
536                                 break;
537                         case 'location' :
538                                 $postdata[$pk] = (string) substr( $pv, 0, 30 );
539                                 break;
540                         case 'description' :
541                                 $postdata[$pk] = (string) substr( $pv, 0, 160 );
542                                 break;
543                         default :
544                                 break;
545                         }
546 endforeach;
547
548 $request = API_URL . '/account/update_profile.' . $this->type;
549 return $this->objectify( $this->process( $request, $postdata ) );
550         }
551
552         function updateColors( $colors = array() )
553         {
554                 if( !in_array( $this->type, array( 'xml','json' ) ) )
555                         return false;
556
557                 $postdata = array();
558                 foreach( $colors as $ck => $cv ) :
559                         if( preg_match('/^(?:(?:[a-f\d]{3}){1,2})$/i', $hex) ) :
560                                 $postdata[$ck] = (string) $cv;
561 endif;
562 endforeach;
563
564 $request = API_URL . '/account/update_profile_colors.' . $this->type;
565 return $this->objectify( $this->process( $request, $postdata ) );
566         }
567
568
569         /**** Search Method ****/
570
571         function search( $q = false, $page = false, $rpp = false)
572         {
573                 if( !$q )
574                         return false;
575                 $qs = '?q='.urlencode($q);
576                 if( $page )
577                         $qs .= '&page='.$page;
578                 if($rpp){
579                         $qs .= '&rpp='.$rpp;
580                 }
581                 $searchApiUrl = strpos(API_URL, "twitter.com") > 0 ? "http://search.twitter.com" : API_URL;
582                 $request = $searchApiUrl . '/search.' . $this->type . $qs;
583
584                 return $this->objectify( $this->process($request) );
585         }
586
587         /**** List Method ****/
588
589         function createdLists( $username = '', $cursor = false )
590         {
591                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
592                         return false;
593
594                 $args = array();
595                 if( $cursor )
596                         $args['cursor'] = $cursor;
597
598                 $qs = '';
599                 if( !empty( $args ) )
600                         $qs = $this->_glue( $args );
601
602                 $request = API_URL . '/' . $username . '/lists.' . $this->type . $qs;    
603
604                 return $this->objectify( $this->process($request) );
605
606         }
607
608         function followedLists( $username = '', $cursor = false )
609         {
610                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
611                         return false;
612
613                 $args = array();
614                 if( $cursor )
615                         $args['cursor'] = $cursor;
616
617                 $qs = '';
618                 if( !empty( $args ) )
619                         $qs = $this->_glue( $args );
620
621                 $request = API_URL . '/' . $username . '/lists/subscriptions.' . $this->type . $qs;    
622
623                 return $this->objectify( $this->process($request) );
624
625         }
626
627         function beAddedLists( $username = '', $cursor = false )
628         {
629                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
630                         return false;
631
632                 $args = array();
633                 if( $cursor )
634                         $args['cursor'] = $cursor;
635
636                 $qs = '';
637                 if( !empty( $args ) )
638                         $qs = $this->_glue( $args );
639
640                 $request = API_URL . '/' . $username . '/lists/memberships.' . $this->type . $qs;    
641
642                 return $this->objectify( $this->process($request) );
643
644         }
645
646         //id¸ñʽ£ºusername/listname Àý:bang590/temp ÏÂͬ
647         function listStatus( $id, $page = false, $since_id = false )
648         {
649                 if( !in_array( $this->type, array( 'xml','json','rss','atom' ) ) )
650                         return false;
651
652                 if (!$id) {
653                         return false;
654                 }
655
656                 $arr = explode('/', $id);
657                 if (count($arr) != 2) return false;
658                 $username = $arr[0];
659                 $listname = $arr[1];
660
661                 $args = array();
662                 if( $page )
663                         $args['page'] = (int) $page;
664                 if( $since_id )
665                         $args['since_id'] = $since_id;
666
667                 $qs = '';
668                 if( !empty( $args ) )
669                         $qs = $this->_glue( $args );
670
671                 $request = API_URL . "/$username/lists/$listname/statuses." . $this->type . $qs;    
672
673                 return $this->objectify( $this->process($request) );
674
675         }
676
677         function listInfo( $id ) {
678                 if( !in_array( $this->type, array( 'xml','json' ) ) )
679                         return false;
680
681                 if (!$id) {
682                         return false;
683                 }
684
685                 $arr = explode('/', $id);
686                 if (count($arr) != 2) return false;
687                 $username = $arr[0];
688                 $listname = $arr[1];
689
690                 $request = API_URL . "/$username/lists/$listname." . $this->type;    
691                 return $this->objectify( $this->process($request) );
692         }
693
694         function isFollowedList( $id )
695         {
696                 if( !in_array( $this->type, array( 'xml','json' ) ) )
697                         return false;
698
699                 $arr = explode('/', $id);
700                 if (count($arr) != 2) return false;
701                 $username = $arr[0];
702                 $listname = $arr[1];
703
704                 $request = API_URL . "/$username/$listname/subscribers/$this->username." . $this->type;
705                 return $this->objectify( $this->process($request) );
706         }
707
708
709         function listMembers( $id, $cursor = false )
710         {
711                 if( !in_array( $this->type, array( 'xml','json' ) ) )
712                         return false;
713
714                 $arr = explode('/', $id);
715                 if (count($arr) != 2) return false;
716                 $username = $arr[0];
717                 $listname = $arr[1];
718
719                 $args = array();
720                 if( $cursor )
721                         $args['cursor'] = $cursor;
722                 $qs = '';
723                 if( !empty( $args ) )
724                         $qs = $this->_glue( $args );
725
726                 $request = API_URL . "/1/$username/$listname/members." . $this->type . $qs;  
727                 return $this->objectify( $this->process($request) );
728         }
729
730         function listFollowers( $id, $cursor = false )
731         {
732                 if( !in_array( $this->type, array( 'xml','json' ) ) )
733                         return false;
734
735                 $arr = explode('/', $id);
736                 if (count($arr) != 2) return false;
737                 $username = $arr[0];
738                 $listname = $arr[1];
739
740                 $args = array();
741                 if( $cursor )
742                         $args['cursor'] = $cursor;
743                 $qs = '';
744                 if( !empty( $args ) )
745                         $qs = $this->_glue( $args );
746
747                 $request = API_URL . "/1/$username/$listname/subscribers." . $this->type . $qs;  
748                 return $this->objectify( $this->process($request) );
749         }
750
751         function followList( $id )
752         {
753                 if( !in_array( $this->type, array( 'xml','json' ) ) )
754                         return false;
755
756                 $arr = explode('/', $id);
757                 if (count($arr) != 2) return false;
758                 $username = $arr[0];
759                 $listname = $arr[1];
760
761                 $request = API_URL . "/1/$username/$listname/subscribers." . $this->type;  
762                 return $this->objectify( $this->process($request, true) );
763         }
764
765         function unfollowList( $id )
766         {
767                 if( !in_array( $this->type, array( 'xml','json' ) ) )
768                         return false;
769
770                 $arr = explode('/', $id);
771                 if (count($arr) != 2) return false;
772                 $username = $arr[0];
773                 $listname = $arr[1];
774
775                 $request = API_URL . "/1/$username/$listname/subscribers." . $this->type;  
776                 return $this->objectify( $this->process($request, "DELETE") );
777         }
778
779         function createList( $name, $description, $isProtect)
780         {
781                 if( !in_array( $this->type, array( 'xml','json' ) ) )
782                         return false;
783
784                 $mode = $isProtect ? "private" : "public";
785                 $args = array();
786                 if( $name )
787                         $args['name'] = $name;
788                 if( $description )
789                         $args['description'] = $description;
790                 if( $isProtect )
791                         $args['mode'] = $mode;
792                 $qs = '';
793                 if( !empty( $args ) )
794                         $qs = $this->_glue( $args );
795
796                 $request = API_URL . "/$this->username/lists." . $this->type . $qs; 
797
798                 return $this->objectify( $this->process($request, true) );
799         }
800
801         function editList( $prename, $name, $description, $isProtect)
802         {
803                 if( !in_array( $this->type, array( 'xml','json' ) ) )
804                         return false;
805
806                 $mode = $isProtect ? "private" : "public";
807                 $args = array();
808                 if( $name )
809                         $args['name'] = $name;
810                 if( $description )
811                         $args['description'] = $description;
812                 if( $isProtect )
813                         $args['mode'] = $mode;
814                 $qs = '';
815                 if( !empty( $args ) )
816                         $qs = $this->_glue( $args );
817
818                 $request = API_URL . "/$this->username/lists/$prename." . $this->type . $qs; 
819
820                 return $this->objectify( $this->process($request, true) );
821         }
822
823         function deleteList( $id)
824         {
825                 if( !in_array( $this->type, array( 'xml','json' ) ) )
826                         return false;
827
828                 $arr = explode('/', $id);
829                 if (count($arr) != 2) return false;
830                 $username = $arr[0];
831                 $listname = $arr[1];
832
833                 $request = API_URL . "/$username/lists/$listname." . $this->type; 
834
835                 return $this->objectify( $this->process($request, "DELETE") );
836         }
837
838
839         function deleteListMember( $id, $memberid )
840         {
841                 if( !in_array( $this->type, array( 'xml','json' ) ) )
842                         return false;
843
844                 $arr = explode('/', $id);
845                 if (count($arr) != 2) return false;
846                 $username = $arr[0];
847                 $listname = $arr[1];
848
849                 $args = array();
850                 if( $memberid )
851                         $args['id'] = $memberid;
852                 $qs = '';
853                 if( !empty( $args ) )
854                         $qs = $this->_glue( $args );
855
856                 $request = API_URL . "/$username/$listname/members." . $this->type . $qs;  
857
858                 return $this->objectify( $this->process($request, "DELETE") );
859         }
860
861         function addListMember( $listid, $memberid )
862         {
863                 if( !in_array( $this->type, array( 'xml','json' ) ) )
864                         return false;
865
866                 $args = array();
867                 if( $memberid )
868                         $args['id'] = $memberid;
869                 $qs = '';
870                 if( !empty( $args ) )
871                         $qs = $this->_glue( $args );
872
873                 $request = API_URL . "/$this->username/$listid/members." . $this->type . $qs;  
874                 return $this->objectify( $this->process($request, true) );
875         }
876
877         /* ---------- Retweet ---------- */
878         function getRetweeters($id, $count = false){
879                 $url = "http://api.twitter.com/1/statuses/retweets/$id.$this->type";
880                 if($count != false){
881                         $url .= "?count=$count";
882                 }
883                 return $this->objectify($this->process($url, $args));
884         }
885
886         function retweet($id){
887                 $url = "http://api.twitter.com/1/statuses/retweet/$id.$this->type";
888                 $args = array();
889                 $args['post_method'] = true;
890                 return $this->curl_process($url, $args);
891         }
892
893         function retweets($id, $count = false){
894                 $url = "http://api.twitter.com/1/statuses/retweets/id.$this->type";
895                 if($count){
896                         $url .= "?count=".($count > 100 ? 100 : $count);
897                 }
898                 return $this->curl_process($url);
899         }
900
901         function retweeted_by_me($page = false, $count = 20, $since_id = false, $max_id = false){
902                 $url = "http://api.twitter.com/1/statuses/retweeted_by_me.$this->type";
903                 $args = array();
904                 if($since_id){
905                         $args['since_id'] = $since_id;
906                 }
907                 if($max_id){
908                         $args['max_id'] = $max_id;
909                 }
910                 if($count){
911                         $args['count'] = $count;
912                 }
913                 if($page){
914                         $args['page'] = $page;
915                 }
916                 $url .= $this->_glue($args);
917                 return $this->curl_process($url);
918         }
919
920         function retweeted_to_me($page = false, $count = false, $since_id = false, $max_id = false){
921                 $url = "http://api.twitter.com/1/statuses/retweeted_to_me.$this->type";
922                 $args = array();
923                 if($since_id){
924                         $args['since_id'] = $since_id;
925                 }
926                 if($max_id){
927                         $args['max_id'] = $max_id;
928                 }
929                 if($count){
930                         $args['count'] = $count;
931                 }
932                 if($page){
933                         $args['page'] = $page;
934                 }
935                 $url .= $this->_glue($args);
936                 return $this->curl_process($url);
937         }
938
939         function retweets_of_me($page = false, $count = false, $since_id = false, $max_id = false){
940                 $url = "http://api.twitter.com/1/statuses/retweets_of_me.$this->type";
941                 $args = array();
942                 if($since_id){
943                         $args['since_id'] = $since_id;
944                 }
945                 if($max_id){
946                         $args['max_id'] = $max_id;
947                 }
948                 if($count){
949                         $args['count'] = $count;
950                 }
951                 if($page){
952                         $args['page'] = $page;
953                 }
954                 $url .= $this->_glue($args);
955                 return $this->curl_process($url);
956         }
957
958         /**** Twitese Method ****/
959
960         function rank( $page = false, $count = false )
961         {
962                 $args = array();
963                 if( $page )
964                         $args['page'] = $page;
965                 if( $count )
966                         $args['count'] = $count;
967                 $qs = $this->_glue( $args );
968
969                 $request = TWITESE_API_URL . '/rank.' . $this->type . $qs;
970
971                 return $this->objectify( $this->process($request) );
972         }
973
974         function browse( $page = false, $count = false )
975         {
976                 $args = array();
977                 if( $page )
978                         $args['page'] = $page;
979                 if( $count )
980                         $args['count'] = $count;
981                 $qs = $this->_glue( $args );
982
983                 $request = TWITESE_API_URL . '/browse.' . $this->type . $qs;
984
985                 return $this->objectify( $this->process($request) );
986         }
987         /**** API Rate Limit ****/
988         function ratelimit()
989         {
990                 if( !in_array( $this->type, array( 'xml','json' ) ) )
991                         return false;
992                 $request = API_URL . '/account/rate_limit_status.' . $this->type;
993                 return $this->objectify( $this->process($request) );
994         }
995
996         function ratelimit_status()
997         {
998                 return $this->ratelimit();
999         }
1000
1001         /**** Upload Photo ****/
1002         /****
1003                 function twitgooUpload( $image ) {
1004                                 $postdata = array( 'media' => "@$image", 'username' => $this->username, 'password' => $this->password);
1005                         $request = 'http://twitgoo.com/api/upload';
1006                         $this->type = 'xml';
1007                         return $this->objectify( $this->process( $request, $postdata ) );
1008                 }
1009         ****/   
1010         function imglyUpload( $image ) {
1011                 $postdata = array( 'media' => "@$image", 'username' => $this->username, 'password' => $this->password);
1012                 $request = 'http://img.ly/api/upload';
1013                 $this->type = 'xml';
1014                 $this->ssl_mode = 1;
1015                 return $this->objectify( $this->process( $request, $postdata ) );
1016         }
1017
1018         /****** Tests ******/
1019
1020         function twitterAvailable()
1021         {
1022                 if( !in_array( $this->type, array( 'xml','json' ) ) )
1023                         return false;
1024
1025                 $request = API_URL . '/help/test.' . $this->type;
1026                 if( $this->objectify( $this->process($request) ) == 'ok' )
1027                         return true;
1028
1029                 return false;
1030         }
1031
1032
1033         /**** request method ****/
1034         function process($url,$postargs=false,$ssl_mode=0)
1035         {
1036                 if ($this->debug) {
1037                         echo $url;
1038                 }
1039                 $url = ( $this->suppress_response_code ) ? $url . '&suppress_response_code=true' : $url;
1040                 $ch = curl_init($url);
1041                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
1042
1043                 if($postargs !== false)
1044                 {
1045                         curl_setopt ($ch, CURLOPT_POST, true);
1046                         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
1047                         if ($postargs === "DELETE") {
1048                                 curl_setopt ($ch, CURLOPT_POSTFIELDS, "_method=DELETE");
1049                         }
1050                 }
1051
1052
1053                 if($this->username !== false && $this->password !== false)
1054                         curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password );
1055
1056                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1057                 //                  curl_setopt($ch, CURLOPT_VERBOSE, 1);
1058                 curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
1059                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl_mode);
1060
1061                 $response = curl_exec($ch);
1062
1063                 $this->responseInfo=curl_getinfo($ch);
1064                 curl_close($ch);
1065                 if(intval($this->responseInfo['http_code'] ) == 200)
1066                         return $response;    
1067                 else
1068                         return false;
1069         }
1070
1071         function curl_process($url, $postargs = false)
1072         {
1073                 $url = ( $this->suppress_response_code ) ? $url . '&suppress_response_code=true' : $url;
1074                 $ch = curl_init($url);
1075                 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
1076
1077                 if($postargs !== false)
1078                 {
1079                         if(isset($postargs['post_method'])){
1080                                 curl_setopt ($ch, CURLOPT_POST, true);
1081                         }else{
1082                                 curl_setopt ($ch, CURLOPT_POST, true);
1083                                 curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
1084                                 if ($postargs === "DELETE") {
1085                                         curl_setopt ($ch, CURLOPT_POSTFIELDS, "_method=DELETE");
1086                                 }
1087                         }
1088                 }
1089
1090                 if($this->username !== false && $this->password !== false)
1091                         curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password );
1092
1093                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
1094                 curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
1095                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1096                 curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
1097                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
1098
1099                 $response = curl_exec($ch);
1100
1101                 $this->responseInfo = curl_getinfo($ch);
1102                 curl_close($ch);
1103
1104                 if($response === false){
1105                         return false;
1106                 }
1107                 if($this->type == 'xml'){
1108                         return @simplexml_load_string($response);
1109                 }else if($this->type == 'json'){
1110                         return @json_decode($response);
1111                 }
1112         }
1113
1114         // Ôö¼Ó $api_type ²ÎÊý
1115         function objectify( $data )
1116         {
1117                 if($date === false){
1118                         return;
1119                 }
1120                 if( $this->type ==  'json' ) {
1121                         $result = @json_decode( $data );
1122                         if ($this->debug) {
1123                                 echo '<pre>';
1124                                 print_r($result);
1125                                 echo '</pre>';
1126                         }
1127                         if (isset($result->error)) {
1128                                 if (substr_count($result->request, 'user_timeline') && $result->error == 'Not authorized') {
1129                                         return 'protected';
1130                                 }
1131                                 return false;
1132                         }
1133                         else return $result;
1134                 }else if( $this->type == 'xml' ){
1135                         if( function_exists('simplexml_load_string') ) {
1136                                 $obj = @simplexml_load_string( $data );
1137                         }
1138                         if ($this->debug) {
1139                                 echo '<pre>';
1140                                 print_r($obj);
1141                                 echo '</pre>';
1142                         }
1143                         if (isset($obj->error) || !$obj) return false;
1144                         else return $obj;
1145                 }
1146                 else
1147                         return false;
1148         }
1149
1150         function arrToStr($postargs) {
1151                 $post_str = '';
1152                 foreach ($postargs as $key=>$value) {
1153                         $post_str .= $key . "=" . $value . "&";
1154                 }
1155                 $post_str = substr($post_str, 0,-1);
1156                 return $post_str;
1157         }
1158
1159         function _glue( $array )
1160         {
1161                 $query_string = '';
1162                 foreach( $array as $key => $val ) :
1163                         $query_string .= $key . '=' . rawurlencode( $val ) . '&';
1164 endforeach;
1165
1166 return '?' . substr( $query_string, 0, strlen( $query_string )-1 );
1167         }
1168 }
1169 ?>