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 require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
38 class BitlyUrlPlugin extends UrlShortenerPlugin
40 public $shortenerName = 'bit.ly';
41 public $serviceUrl = 'http://bit.ly/api?method=shorten&version=2.0.1&longUrl=%s';
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."));
53 * @return string shortened version of the url, or null if URL shortening failed
55 protected function shorten($url) {
56 $response = $this->query($url);
57 if ($this->isOk($url, $response)) {
58 return $this->decode($url, $response->getBody());
65 * Get the user's or site-wide default bit.ly login name.
69 protected function getLogin()
71 return common_config('bitly', 'default_login');
75 * Get the user's or site-wide default bit.ly API key.
79 protected function getApiKey()
81 return common_config('bitly', 'default_apikey');
85 * Inject API key into query before sending out...
88 * @return HTTPResponse
90 protected function query($url)
92 // http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/shorten
93 $params = http_build_query(array(
94 'login' => $this->getLogin(),
95 'apiKey' => $this->getApiKey()), '', '&');
96 $serviceUrl = sprintf($this->serviceUrl, $url) . '&' . $params;
98 $request = HTTPClient::start();
99 return $request->get($serviceUrl);
103 * JSON decode for API result
105 protected function decode($url, $body)
107 $json = json_decode($body, true);
108 return $json['results'][$url]['shortUrl'];
112 * JSON decode for API result
114 protected function isOk($url, $response)
118 if ($response->isOk()) {
119 $body = $response->getBody();
120 common_log(LOG_INFO, $body);
121 $json = json_decode($body, true);
122 if ($json['statusCode'] == 'OK') {
123 $data = $json['results'][$url];
124 if (isset($data['shortUrl'])) {
126 } else if (isset($data['statusCode']) && $data['statusCode'] == 'ERROR') {
127 $code = $data['errorCode'];
128 $msg = $data['errorMessage'];
130 } else if ($json['statusCode'] == 'ERROR') {
131 $code = $json['errorCode'];
132 $msg = $json['errorMessage'];
134 common_log(LOG_ERR, "bit.ly returned error $code $msg for $url");
139 function onPluginVersion(&$versions)
141 $versions[] = array('name' => sprintf('BitlyUrl (%s)', $this->shortenerName),
142 'version' => STATUSNET_VERSION,
143 'author' => 'Craig Andrews, Brion Vibber',
144 'homepage' => 'http://status.net/wiki/Plugin:BitlyUrl',
146 sprintf(_m('Uses <a href="http://%1$s/">%1$s</a> URL-shortener service.'),
147 $this->shortenerName));
153 * Hook for RouterInitialized event.
155 * @param Net_URL_Mapper $m path-to-action mapper
156 * @return boolean hook return
158 function onRouterInitialized($m)
160 $m->connect('admin/bitly',
161 array('action' => 'bitlyadminpanel'));
166 * If the plugin's installed, this should be accessible to admins.
168 function onAdminPanelCheck($name, &$isOK)
170 if ($name == 'bitly') {
179 * Add the bit.ly admin panel to the list...
181 function onEndAdminPanelNav($nav)
183 if (AdminPanelAction::canAdmin('bitly')) {
184 $action_name = $nav->action->trimmed('action');
186 $nav->out->menuItem(common_local_url('bitlyadminpanel'),
188 _m('bit.ly URL shortening'),
189 $action_name == 'bitlyadminpanel',
190 'nav_bitly_admin_panel');
197 * Automatically load the actions and libraries used by the plugin
199 * @param Class $cls the class
201 * @return boolean hook return
204 function onAutoload($cls)
206 $base = dirname(__FILE__);
207 $lower = strtolower($cls);
209 case 'bitlyadminpanelaction':
210 require_once "$base/$lower.php";