Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/Twitter.php
Go to the documentation of this file.
00001 <?php
00026 require_once 'Zend/Rest/Client.php';
00027 
00031 require_once 'Zend/Rest/Client/Result.php';
00032 
00036 require_once 'Zend/Oauth/Consumer.php';
00037 
00045 class Zend_Service_Twitter extends Zend_Rest_Client
00046 {
00047 
00056     const STATUS_MAX_CHARACTERS = 246;
00057     
00061     const OAUTH_BASE_URI = 'http://twitter.com/oauth';
00062     
00066     protected $_cookieJar;
00067     
00073     protected $_dateFormat = 'D, d M Y H:i:s T';
00074     
00080     protected $_username;
00081     
00087     protected $_methodType;
00088     
00094     protected $_oauthConsumer = null;
00095     
00101     protected $_methodTypes = array(
00102         'status',
00103         'user',
00104         'directMessage',
00105         'friendship',
00106         'account',
00107         'favorite',
00108         'block'
00109     );
00110     
00116     protected $_options = array();
00117 
00123     protected $_localHttpClient = null;
00124 
00131     public function __construct(array $options = null, Zend_Oauth_Consumer $consumer = null)
00132     {
00133         $this->setUri('http://api.twitter.com');
00134         if (!is_array($options)) $options = array();
00135         $options['siteUrl'] = self::OAUTH_BASE_URI;
00136         if ($options instanceof Zend_Config) {
00137             $options = $options->toArray();
00138         }
00139         $this->_options = $options;
00140         if (isset($options['username'])) {
00141             $this->setUsername($options['username']);
00142         }
00143         if (isset($options['accessToken'])
00144         && $options['accessToken'] instanceof Zend_Oauth_Token_Access) {
00145             $this->setLocalHttpClient($options['accessToken']->getHttpClient($options));
00146         } else {
00147             $this->setLocalHttpClient(clone self::getHttpClient());
00148             if (is_null($consumer)) {
00149                 $this->_oauthConsumer = new Zend_Oauth_Consumer($options);
00150             } else {
00151                 $this->_oauthConsumer = $consumer;
00152             }
00153         }
00154     }
00155 
00163     public function setLocalHttpClient(Zend_Http_Client $client)
00164     {
00165         $this->_localHttpClient = $client;
00166         $this->_localHttpClient->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
00167         return $this;
00168     }
00169     
00176     public function getLocalHttpClient()
00177     {
00178         return $this->_localHttpClient;
00179     }
00180     
00186     public function isAuthorised()
00187     {
00188         if ($this->getLocalHttpClient() instanceof Zend_Oauth_Client) {
00189             return true;
00190         }
00191         return false;
00192     }
00193 
00199     public function getUsername()
00200     {
00201         return $this->_username;
00202     }
00203 
00210     public function setUsername($value)
00211     {
00212         $this->_username = $value;
00213         return $this;
00214     }
00215 
00223     public function __get($type)
00224     {
00225         if (!in_array($type, $this->_methodTypes)) {
00226             include_once 'Zend/Service/Twitter/Exception.php';
00227             throw new Zend_Service_Twitter_Exception(
00228                 'Invalid method type "' . $type . '"'
00229             );
00230         }
00231         $this->_methodType = $type;
00232         return $this;
00233     }
00234 
00243     public function __call($method, $params)
00244     {
00245         if (method_exists($this->_oauthConsumer, $method)) {
00246             $return = call_user_func_array(array($this->_oauthConsumer, $method), $params);
00247             if ($return instanceof Zend_Oauth_Token_Access) {
00248                 $this->setLocalHttpClient($return->getHttpClient($this->_options));
00249             }
00250             return $return;
00251         }
00252         if (empty($this->_methodType)) {
00253             include_once 'Zend/Service/Twitter/Exception.php';
00254             throw new Zend_Service_Twitter_Exception(
00255                 'Invalid method "' . $method . '"'
00256             );
00257         }
00258         $test = $this->_methodType . ucfirst($method);
00259         if (!method_exists($this, $test)) {
00260             include_once 'Zend/Service/Twitter/Exception.php';
00261             throw new Zend_Service_Twitter_Exception(
00262                 'Invalid method "' . $test . '"'
00263             );
00264         }
00265 
00266         return call_user_func_array(array($this, $test), $params);
00267     }
00268 
00274     protected function _init()
00275     {
00276         if (!$this->isAuthorised() && $this->getUsername() !== null) {
00277             require_once 'Zend/Service/Twitter/Exception.php';
00278             throw new Zend_Service_Twitter_Exception(
00279                 'Twitter session is unauthorised. You need to initialize '
00280                 . 'Zend_Service_Twitter with an OAuth Access Token or use '
00281                 . 'its OAuth functionality to obtain an Access Token before '
00282                 . 'attempting any API actions that require authorisation'
00283             );
00284         }
00285         $client = $this->_localHttpClient;
00286         $client->resetParameters();
00287         if (null == $this->_cookieJar) {
00288             $client->setCookieJar();
00289             $this->_cookieJar = $client->getCookieJar();
00290         } else {
00291             $client->setCookieJar($this->_cookieJar);
00292         }
00293     }
00294 
00302     protected function _setDate($value)
00303     {
00304         if (is_int($value)) {
00305             $date = date($this->_dateFormat, $value);
00306         } else {
00307             $date = date($this->_dateFormat, strtotime($value));
00308         }
00309         $this->_localHttpClient->setHeaders('If-Modified-Since', $date);
00310     }
00311 
00318     public function statusPublicTimeline()
00319     {
00320         $this->_init();
00321         $path = '/1/statuses/public_timeline.xml';
00322         $response = $this->_get($path);
00323         return new Zend_Rest_Client_Result($response->getBody());
00324     }
00325 
00339     public function statusFriendsTimeline(array $params = array())
00340     {
00341         $this->_init();
00342         $path = '/1/statuses/friends_timeline';
00343         $_params = array();
00344         foreach ($params as $key => $value) {
00345             switch (strtolower($key)) {
00346                 case 'count':
00347                     $count = (int) $value;
00348                     if (0 >= $count) {
00349                         $count = 1;
00350                     } elseif (200 < $count) {
00351                         $count = 200;
00352                     }
00353                     $_params['count'] = (int) $count;
00354                     break;
00355                 case 'since_id':
00356                     $_params['since_id'] = $this->_validInteger($value);
00357                     break;
00358                 case 'page':
00359                     $_params['page'] = (int) $value;
00360                     break;
00361                 default:
00362                     break;
00363             }
00364         }
00365         $path .= '.xml';
00366         $response = $this->_get($path, $_params);
00367         return new Zend_Rest_Client_Result($response->getBody());
00368     }
00369 
00385     public function statusUserTimeline(array $params = array())
00386     {
00387         $this->_init();
00388         $path = '/1/statuses/user_timeline';
00389         $_params = array();
00390         foreach ($params as $key => $value) {
00391             switch (strtolower($key)) {
00392                 case 'id':
00393                     $path .= '/' . $value;
00394                     break;
00395                 case 'page':
00396                     $_params['page'] = (int) $value;
00397                     break;
00398                 case 'count':
00399                     $count = (int) $value;
00400                     if (0 >= $count) {
00401                         $count = 1;
00402                     } elseif (200 < $count) {
00403                         $count = 200;
00404                     }
00405                     $_params['count'] = $count;
00406                     break;
00407                 case 'user_id':
00408                     $_params['user_id'] = $this->_validInteger($value);
00409                     break;
00410                 case 'screen_name':
00411                     $_params['screen_name'] = $this->_validateScreenName($value);
00412                     break;
00413                 case 'since_id':
00414                     $_params['since_id'] = $this->_validInteger($value);
00415                     break;
00416                 case 'max_id':
00417                     $_params['max_id'] = $this->_validInteger($value);
00418                     break;
00419                 default:
00420                     break;
00421             }
00422         }
00423         $path .= '.xml';
00424         $response = $this->_get($path, $_params);
00425         return new Zend_Rest_Client_Result($response->getBody());
00426     }
00427 
00435     public function statusShow($id)
00436     {
00437         $this->_init();
00438         $path = '/1/statuses/show/' . $this->_validInteger($id) . '.xml';
00439         $response = $this->_get($path);
00440         return new Zend_Rest_Client_Result($response->getBody());
00441     }
00442 
00452     public function statusUpdate($status, $inReplyToStatusId = null)
00453     {
00454         $this->_init();
00455         $path = '/1/statuses/update.xml';
00456         $len = iconv_strlen(htmlspecialchars($status, ENT_QUOTES, 'UTF-8'), 'UTF-8');
00457         if ($len > self::STATUS_MAX_CHARACTERS) {
00458             include_once 'Zend/Service/Twitter/Exception.php';
00459             throw new Zend_Service_Twitter_Exception(
00460                 'Status must be no more than '
00461                 . self::STATUS_MAX_CHARACTERS
00462                 . ' characters in length'
00463             );
00464         } elseif (0 == $len) {
00465             include_once 'Zend/Service/Twitter/Exception.php';
00466             throw new Zend_Service_Twitter_Exception(
00467                 'Status must contain at least one character'
00468             );
00469         }
00470         $data = array('status' => $status);
00471         if (is_numeric($inReplyToStatusId) && !empty($inReplyToStatusId)) {
00472             $data['in_reply_to_status_id'] = $inReplyToStatusId;
00473         }
00474         $response = $this->_post($path, $data);
00475         return new Zend_Rest_Client_Result($response->getBody());
00476     }
00477 
00488     public function statusReplies(array $params = array())
00489     {
00490         $this->_init();
00491         $path = '/1/statuses/mentions.xml';
00492         $_params = array();
00493         foreach ($params as $key => $value) {
00494             switch (strtolower($key)) {
00495                 case 'since_id':
00496                     $_params['since_id'] = $this->_validInteger($value);
00497                     break;
00498                 case 'page':
00499                     $_params['page'] = (int) $value;
00500                     break;
00501                 default:
00502                     break;
00503             }
00504         }
00505         $response = $this->_get($path, $_params);
00506         return new Zend_Rest_Client_Result($response->getBody());
00507     }
00508 
00516     public function statusDestroy($id)
00517     {
00518         $this->_init();
00519         $path = '/1/statuses/destroy/' . $this->_validInteger($id) . '.xml';
00520         $response = $this->_post($path);
00521         return new Zend_Rest_Client_Result($response->getBody());
00522     }
00523 
00531     public function userFriends(array $params = array())
00532     {
00533         $this->_init();
00534         $path = '/1/statuses/friends';
00535         $_params = array();
00536 
00537         foreach ($params as $key => $value) {
00538             switch (strtolower($key)) {
00539                 case 'id':
00540                     $path .= '/' . $value;
00541                     break;
00542                 case 'page':
00543                     $_params['page'] = (int) $value;
00544                     break;
00545                 default:
00546                     break;
00547             }
00548         }
00549         $path .= '.xml';
00550 
00551         $response = $this->_get($path, $_params);
00552         return new Zend_Rest_Client_Result($response->getBody());
00553     }
00554 
00562     public function userFollowers($lite = false)
00563     {
00564         $this->_init();
00565         $path = '/1/statuses/followers.xml';
00566         if ($lite) {
00567             $this->lite = 'true';
00568         }
00569         $response = $this->_get($path);
00570         return new Zend_Rest_Client_Result($response->getBody());
00571     }
00572 
00580     public function userShow($id)
00581     {
00582         $this->_init();
00583         $path = '/1/users/show.xml';
00584         $response = $this->_get($path, array('id'=>$id));
00585         return new Zend_Rest_Client_Result($response->getBody());
00586     }
00587 
00599     public function directMessageMessages(array $params = array())
00600     {
00601         $this->_init();
00602         $path = '/1/direct_messages.xml';
00603         $_params = array();
00604         foreach ($params as $key => $value) {
00605             switch (strtolower($key)) {
00606                 case 'since_id':
00607                     $_params['since_id'] = $this->_validInteger($value);
00608                     break;
00609                 case 'page':
00610                     $_params['page'] = (int) $value;
00611                     break;
00612                 default:
00613                     break;
00614             }
00615         }
00616         $response = $this->_get($path, $_params);
00617         return new Zend_Rest_Client_Result($response->getBody());
00618     }
00619 
00631     public function directMessageSent(array $params = array())
00632     {
00633         $this->_init();
00634         $path = '/1/direct_messages/sent.xml';
00635         $_params = array();
00636         foreach ($params as $key => $value) {
00637             switch (strtolower($key)) {
00638                 case 'since_id':
00639                     $_params['since_id'] = $this->_validInteger($value);
00640                     break;
00641                 case 'page':
00642                     $_params['page'] = (int) $value;
00643                     break;
00644                 default:
00645                     break;
00646             }
00647         }
00648         $response = $this->_get($path, $_params);
00649         return new Zend_Rest_Client_Result($response->getBody());
00650     }
00651 
00661     public function directMessageNew($user, $text)
00662     {
00663         $this->_init();
00664         $path = '/1/direct_messages/new.xml';
00665         $len = iconv_strlen($text, 'UTF-8');
00666         if (0 == $len) {
00667             throw new Zend_Service_Twitter_Exception(
00668                 'Direct message must contain at least one character'
00669             );
00670         } elseif (140 < $len) {
00671             throw new Zend_Service_Twitter_Exception(
00672                 'Direct message must contain no more than 140 characters'
00673             );
00674         }
00675         $data = array('user' => $user, 'text' => $text);
00676         $response = $this->_post($path, $data);
00677         return new Zend_Rest_Client_Result($response->getBody());
00678     }
00679 
00687     public function directMessageDestroy($id)
00688     {
00689         $this->_init();
00690         $path = '/1/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
00691         $response = $this->_post($path);
00692         return new Zend_Rest_Client_Result($response->getBody());
00693     }
00694 
00702     public function friendshipCreate($id)
00703     {
00704         $this->_init();
00705         $path = '/1/friendships/create/' . $id . '.xml';
00706         $response = $this->_post($path);
00707         return new Zend_Rest_Client_Result($response->getBody());
00708     }
00709 
00717     public function friendshipDestroy($id)
00718     {
00719         $this->_init();
00720         $path = '/1/friendships/destroy/' . $id . '.xml';
00721         $response = $this->_post($path);
00722         return new Zend_Rest_Client_Result($response->getBody());
00723     }
00724 
00732     public function friendshipExists($id)
00733     {
00734         $this->_init();
00735         $path = '/1/friendships/exists.xml';
00736         $data = array('user_a' => $this->getUsername(), 'user_b' => $id);
00737         $response = $this->_get($path, $data);
00738         return new Zend_Rest_Client_Result($response->getBody());
00739     }
00740 
00747     public function accountVerifyCredentials()
00748     {
00749         $this->_init();
00750         $response = $this->_get('/1/account/verify_credentials.xml');
00751         return new Zend_Rest_Client_Result($response->getBody());
00752     }
00753 
00760     public function accountEndSession()
00761     {
00762         $this->_init();
00763         $this->_get('/1/account/end_session');
00764         return true;
00765     }
00766 
00773     public function accountRateLimitStatus()
00774     {
00775         $this->_init();
00776         $response = $this->_get('/1/account/rate_limit_status.xml');
00777         return new Zend_Rest_Client_Result($response->getBody());
00778     }
00779 
00791     public function favoriteFavorites(array $params = array())
00792     {
00793         $this->_init();
00794         $path = '/1/favorites';
00795         $_params = array();
00796         foreach ($params as $key => $value) {
00797             switch (strtolower($key)) {
00798                 case 'id':
00799                     $path .= '/' . $this->_validInteger($value);
00800                     break;
00801                 case 'page':
00802                     $_params['page'] = (int) $value;
00803                     break;
00804                 default:
00805                     break;
00806             }
00807         }
00808         $path .= '.xml';
00809         $response = $this->_get($path, $_params);
00810         return new Zend_Rest_Client_Result($response->getBody());
00811     }
00812 
00820     public function favoriteCreate($id)
00821     {
00822         $this->_init();
00823         $path = '/1/favorites/create/' . $this->_validInteger($id) . '.xml';
00824         $response = $this->_post($path);
00825         return new Zend_Rest_Client_Result($response->getBody());
00826     }
00827 
00835     public function favoriteDestroy($id)
00836     {
00837         $this->_init();
00838         $path = '/1/favorites/destroy/' . $this->_validInteger($id) . '.xml';
00839         $response = $this->_post($path);
00840         return new Zend_Rest_Client_Result($response->getBody());
00841     }
00842 
00850     public function blockCreate($id)
00851     {
00852         $this->_init();
00853         $path = '/1/blocks/create/' . $id . '.xml';
00854         $response = $this->_post($path);
00855         return new Zend_Rest_Client_Result($response->getBody());
00856     }
00857 
00864     public function blockDestroy($id)
00865     {
00866         $this->_init();
00867         $path = '/1/blocks/destroy/' . $id . '.xml';
00868         $response = $this->_post($path);
00869         return new Zend_Rest_Client_Result($response->getBody());
00870     }
00871 
00879     public function blockExists($id, $returnResult = false)
00880     {
00881         $this->_init();
00882         $path = '/1/blocks/exists/' . $id . '.xml';
00883         $response = $this->_get($path);
00884 
00885         $cr = new Zend_Rest_Client_Result($response->getBody());
00886 
00887         if ($returnResult === true)
00888             return $cr;
00889 
00890         if (!empty($cr->request)) {
00891             return false;
00892         }
00893 
00894         return true;
00895     }
00896 
00904     public function blockBlocking($page = 1, $returnUserIds = false)
00905     {
00906         $this->_init();
00907         $path = '/1/blocks/blocking';
00908         if ($returnUserIds === true) {
00909             $path .= '/ids';
00910         }
00911         $path .= '.xml';
00912         $response = $this->_get($path, array('page' => $page));
00913         return new Zend_Rest_Client_Result($response->getBody());
00914     }
00915 
00922     protected function _validInteger($int)
00923     {
00924         if (preg_match("/(\d+)/", $int)) {
00925             return $int;
00926         }
00927         return 0;
00928     }
00929 
00937     protected function _validateScreenName($name)
00938     {
00939         if (!preg_match('/^[a-zA-Z0-9_]{0,20}$/', $name)) {
00940             require_once 'Zend/Service/Twitter/Exception.php';
00941             throw new Zend_Service_Twitter_Exception(
00942                 'Screen name, "' . $name
00943                 . '" should only contain alphanumeric characters and'
00944                 . ' underscores, and not exceed 15 characters.');
00945         }
00946         return $name;
00947     }
00948 
00956     protected function _prepare($path)
00957     {
00958         // Get the URI object and configure it
00959         if (!$this->_uri instanceof Zend_Uri_Http) {
00960             require_once 'Zend/Rest/Client/Exception.php';
00961             throw new Zend_Rest_Client_Exception(
00962                 'URI object must be set before performing call'
00963             );
00964         }
00965 
00966         $uri = $this->_uri->getUri();
00967 
00968         if ($path[0] != '/' && $uri[strlen($uri) - 1] != '/') {
00969             $path = '/' . $path;
00970         }
00971 
00972         $this->_uri->setPath($path);
00973 
00979         $this->_localHttpClient->resetParameters()->setUri((string) $this->_uri);
00980     }
00981 
00990     protected function _get($path, array $query = null)
00991     {
00992         $this->_prepare($path);
00993         $this->_localHttpClient->setParameterGet($query);
00994         return $this->_localHttpClient->request(Zend_Http_Client::GET);
00995     }
00996 
01005     protected function _post($path, $data = null)
01006     {
01007         $this->_prepare($path);
01008         return $this->_performPost(Zend_Http_Client::POST, $data);
01009     }
01010 
01022     protected function _performPost($method, $data = null)
01023     {
01024         $client = $this->_localHttpClient;
01025         if (is_string($data)) {
01026             $client->setRawData($data);
01027         } elseif (is_array($data) || is_object($data)) {
01028             $client->setParameterPost((array) $data);
01029         }
01030         return $client->request($method);
01031     }
01032 
01033 }
 All Data Structures Namespaces Files Functions Variables Enumerations