123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class BitlyUrlPlugin extends UrlShortenerPlugin
- {
- public $shortenerName = 'bit.ly';
- public $serviceUrl = 'http://api.bit.ly/v3/shorten?longUrl=%s';
- public $login;
- public $apiKey;
- function onInitializePlugin(){
- parent::onInitializePlugin();
- if(!isset($this->serviceUrl)){
-
- throw new Exception(_m('You must specify a serviceUrl for bit.ly URL shortening.'));
- }
- }
-
- function onGetUrlShorteners(&$shorteners)
- {
- if ($this->getLogin() && $this->getApiKey()) {
- return parent::onGetUrlShorteners($shorteners);
- }
- return true;
- }
-
- protected function shorten($url) {
- common_log(LOG_INFO, "bit.ly call for $url");
- $response = $this->query($url);
- common_log(LOG_INFO, "bit.ly answer for $url is ".$response->getBody());
- return $this->decode($url, $response);
- }
-
- protected function getLogin()
- {
- $login = common_config('bitly', 'default_login');
- if (!$login) {
- $login = $this->login;
- }
- return $login;
- }
-
- protected function getApiKey()
- {
- $key = common_config('bitly', 'default_apikey');
- if (!$key) {
- $key = $this->apiKey;
- }
- return $key;
- }
-
- protected function query($url)
- {
-
- $params = http_build_query(array(
- 'login' => $this->getLogin(),
- 'apiKey' => $this->getApiKey()), '', '&');
- $serviceUrl = sprintf($this->serviceUrl, urlencode($url)) . '&' . $params;
- $request = HTTPClient::start();
- return $request->get($serviceUrl);
- }
-
- protected function decode($url, $response)
- {
- $msg = "bit.ly returned unknown response with unknown message for $url";
- if ($response->isOk()) {
- $body = $response->getBody();
- common_log(LOG_INFO, $body);
- $json = json_decode($body, true);
- if ($json['status_code'] == 200) {
- if (isset($json['data']['url'])) {
- common_log(LOG_INFO, "bit.ly returned ".$json['data']['url']." as short URL for $url");
- return $json['data']['url'];
- }
- $msg = "bit.ly returned ".$json['status_code']." response, but didn't find expected URL $url in $body";
- }else{
- $msg = "bit.ly returned ".$json['status_code']." response with ".$json['status_txt']." for $url";
- }
- }
- common_log(LOG_ERR, $msg);
- return null;
- }
- function onPluginVersion(&$versions)
- {
- $versions[] = array('name' => sprintf('BitlyUrl (%s)', $this->shortenerName),
- 'version' => GNUSOCIAL_VERSION,
- 'author' => 'Craig Andrews, Brion Vibber',
- 'homepage' => 'http://status.net/wiki/Plugin:BitlyUrl',
- 'rawdescription' =>
-
- sprintf(_m('Uses <a href="http://%1$s/">%1$s</a> URL-shortener service.'),
- $this->shortenerName));
- return true;
- }
-
- public function onRouterInitialized(URLMapper $m)
- {
- $m->connect('panel/bitly',
- array('action' => 'bitlyadminpanel'));
- return true;
- }
-
- function onAdminPanelCheck($name, &$isOK)
- {
- if ($name == 'bitly') {
- $isOK = true;
- return false;
- }
- return true;
- }
-
- function onEndAdminPanelNav($nav)
- {
- if (AdminPanelAction::canAdmin('bitly')) {
- $action_name = $nav->action->trimmed('action');
- $nav->out->menuItem(common_local_url('bitlyadminpanel'),
-
- _m('bit.ly'),
-
- _m('bit.ly URL shortening.'),
- $action_name == 'bitlyadminpanel',
- 'nav_bitly_admin_panel');
- }
- return true;
- }
-
- function onBitlyDefaultCredentials(&$login, &$apiKey)
- {
- $login = $this->login;
- $apiKey = $this->apiKey;
- return false;
- }
- }
|