3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to use bit.ly URL shortening services.
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Brion Vibber <brion@status.net>
26 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
27 * @copyright 2010 StatusNet, Inc http://status.net/
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET')) {
36 class BitlyUrlPlugin extends UrlShortenerPlugin
38 public $shortenerName = 'bit.ly';
39 public $serviceUrl = 'http://api.bit.ly/v3/shorten?longUrl=%s';
40 public $login; // To set a site-default when admins or users don't override it.
43 function onInitializePlugin(){
44 parent::onInitializePlugin();
45 if(!isset($this->serviceUrl)){
46 // TRANS: Exception thrown when bit.ly URL shortening plugin was configured incorrectly.
47 throw new Exception(_m('You must specify a serviceUrl for bit.ly URL shortening.'));
52 * Add bit.ly to the list of available URL shorteners if it's configured,
53 * otherwise leave it out.
55 * @param array $shorteners
56 * @return boolean hook return value
58 function onGetUrlShorteners(&$shorteners)
60 if ($this->getLogin() && $this->getApiKey()) {
61 return parent::onGetUrlShorteners($shorteners);
69 * @return string shortened version of the url, or null if URL shortening failed
71 protected function shorten($url) {
72 common_log(LOG_INFO, "bit.ly call for $url");
73 $response = $this->query($url);
74 common_log(LOG_INFO, "bit.ly answer for $url is ".$response->getBody());
75 return $this->decode($url, $response);
79 * Get the user's or site-wide default bit.ly login name.
83 protected function getLogin()
85 $login = common_config('bitly', 'default_login');
87 $login = $this->login;
93 * Get the user's or site-wide default bit.ly API key.
97 protected function getApiKey()
99 $key = common_config('bitly', 'default_apikey');
101 $key = $this->apiKey;
107 * Inject API key into query before sending out...
110 * @return HTTPResponse
112 protected function query($url)
114 // http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/shorten
115 $params = http_build_query(array(
116 'login' => $this->getLogin(),
117 'apiKey' => $this->getApiKey()), '', '&');
118 $serviceUrl = sprintf($this->serviceUrl, urlencode($url)) . '&' . $params;
120 $request = HTTPClient::start();
121 return $request->get($serviceUrl);
125 * JSON decode for API result
127 protected function decode($url, $response)
129 $msg = "bit.ly returned unknown response with unknown message for $url";
130 if ($response->isOk()) {
131 $body = $response->getBody();
132 common_log(LOG_INFO, $body);
133 $json = json_decode($body, true);
134 if ($json['status_code'] == 200) {
135 if (isset($json['data']['url'])) {
136 common_log(LOG_INFO, "bit.ly returned ".$json['data']['url']." as short URL for $url");
137 return $json['data']['url'];
139 $msg = "bit.ly returned ".$json['status_code']." response, but didn't find expected URL $url in $body";
141 $msg = "bit.ly returned ".$json['status_code']." response with ".$json['status_txt']." for $url";
144 common_log(LOG_ERR, $msg);
148 function onPluginVersion(array &$versions)
150 $versions[] = array('name' => sprintf('BitlyUrl (%s)', $this->shortenerName),
151 'version' => GNUSOCIAL_VERSION,
152 'author' => 'Craig Andrews, Brion Vibber',
153 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/BitlyUrl',
155 // TRANS: Plugin description. %1$s is the URL shortening service base URL (for example "bit.ly").
156 sprintf(_m('Uses <a href="http://%1$s/">%1$s</a> URL-shortener service.'),
157 $this->shortenerName));
163 * Hook for RouterInitialized event.
165 * @param URLMapper $m path-to-action mapper
166 * @return boolean hook return
168 public function onRouterInitialized(URLMapper $m)
170 $m->connect('panel/bitly',
171 array('action' => 'bitlyadminpanel'));
176 * If the plugin's installed, this should be accessible to admins.
178 function onAdminPanelCheck($name, &$isOK)
180 if ($name == 'bitly') {
189 * Add the bit.ly admin panel to the list...
191 function onEndAdminPanelNav($nav)
193 if (AdminPanelAction::canAdmin('bitly')) {
194 $action_name = $nav->action->trimmed('action');
196 $nav->out->menuItem(common_local_url('bitlyadminpanel'),
197 // TRANS: Menu item in administration menus for bit.ly URL shortening settings.
199 // TRANS: Title for menu item in administration menus for bit.ly URL shortening settings.
200 _m('bit.ly URL shortening.'),
201 $action_name == 'bitlyadminpanel',
202 'nav_bitly_admin_panel');
209 * Internal hook point to check the default global credentials so
210 * the admin form knows if we have a fallback or not.
212 * @param string $login
213 * @param string $apiKey
214 * @return boolean hook return value
216 function onBitlyDefaultCredentials(&$login, &$apiKey)
218 $login = $this->login;
219 $apiKey = $this->apiKey;