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://bit.ly/api?method=shorten&version=2.0.1&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 throw new Exception(_m("You must specify a serviceUrl for bit.ly shortening."));
51 * Add bit.ly to the list of available URL shorteners if it's configured,
52 * otherwise leave it out.
54 * @param array $shorteners
55 * @return boolean hook return value
57 function onGetUrlShorteners(&$shorteners)
59 if ($this->getLogin() && $this->getApiKey()) {
60 return parent::onGetUrlShorteners($shorteners);
68 * @return string shortened version of the url, or null if URL shortening failed
70 protected function shorten($url) {
71 $response = $this->query($url);
72 if ($this->isOk($url, $response)) {
73 return $this->decode($url, $response->getBody());
80 * Get the user's or site-wide default bit.ly login name.
84 protected function getLogin()
86 $login = common_config('bitly', 'default_login');
88 $login = $this->login;
94 * Get the user's or site-wide default bit.ly API key.
98 protected function getApiKey()
100 $key = common_config('bitly', 'default_apikey');
102 $key = $this->apiKey;
108 * Inject API key into query before sending out...
111 * @return HTTPResponse
113 protected function query($url)
115 // http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/shorten
116 $params = http_build_query(array(
117 'login' => $this->getLogin(),
118 'apiKey' => $this->getApiKey()), '', '&');
119 $serviceUrl = sprintf($this->serviceUrl, $url) . '&' . $params;
121 $request = HTTPClient::start();
122 return $request->get($serviceUrl);
126 * JSON decode for API result
128 protected function decode($url, $body)
130 $json = json_decode($body, true);
131 return $json['results'][$url]['shortUrl'];
135 * JSON decode for API result
137 protected function isOk($url, $response)
141 if ($response->isOk()) {
142 $body = $response->getBody();
143 common_log(LOG_INFO, $body);
144 $json = json_decode($body, true);
145 if ($json['statusCode'] == 'OK') {
146 $data = $json['results'][$url];
147 if (isset($data['shortUrl'])) {
149 } else if (isset($data['statusCode']) && $data['statusCode'] == 'ERROR') {
150 $code = $data['errorCode'];
151 $msg = $data['errorMessage'];
153 } else if ($json['statusCode'] == 'ERROR') {
154 $code = $json['errorCode'];
155 $msg = $json['errorMessage'];
157 common_log(LOG_ERR, "bit.ly returned error $code $msg for $url");
162 function onPluginVersion(&$versions)
164 $versions[] = array('name' => sprintf('BitlyUrl (%s)', $this->shortenerName),
165 'version' => STATUSNET_VERSION,
166 'author' => 'Craig Andrews, Brion Vibber',
167 'homepage' => 'http://status.net/wiki/Plugin:BitlyUrl',
169 sprintf(_m('Uses <a href="http://%1$s/">%1$s</a> URL-shortener service.'),
170 $this->shortenerName));
176 * Hook for RouterInitialized event.
178 * @param Net_URL_Mapper $m path-to-action mapper
179 * @return boolean hook return
181 function onRouterInitialized($m)
183 $m->connect('admin/bitly',
184 array('action' => 'bitlyadminpanel'));
189 * If the plugin's installed, this should be accessible to admins.
191 function onAdminPanelCheck($name, &$isOK)
193 if ($name == 'bitly') {
202 * Add the bit.ly admin panel to the list...
204 function onEndAdminPanelNav($nav)
206 if (AdminPanelAction::canAdmin('bitly')) {
207 $action_name = $nav->action->trimmed('action');
209 $nav->out->menuItem(common_local_url('bitlyadminpanel'),
211 _m('bit.ly URL shortening'),
212 $action_name == 'bitlyadminpanel',
213 'nav_bitly_admin_panel');
220 * Automatically load the actions and libraries used by the plugin
222 * @param Class $cls the class
224 * @return boolean hook return
227 function onAutoload($cls)
229 $base = dirname(__FILE__);
230 $lower = strtolower($cls);
232 case 'bitlyadminpanelaction':
233 require_once "$base/$lower.php";
241 * Internal hook point to check the default global credentials so
242 * the admin form knows if we have a fallback or not.
244 * @param string $login
245 * @param string $apiKey
246 * @return boolean hook return value
248 function onBitlyDefaultCredentials(&$login, &$apiKey)
250 $login = $this->login;
251 $apiKey = $this->apiKey;