3 * StatusNet, the distributed open-source microblogging tool
5 * Superclass for plugins that do URL shortening
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 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26 * @link http://status.net/
29 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 * Superclass for plugins that do URL shortening
38 * @author Craig Andrews <candrews@integralblue.com>
39 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40 * @link http://status.net/
43 abstract class UrlShortenerPlugin extends Plugin
45 public $shortenerName;
46 public $freeService = false;
48 // Url Shortener plugins should implement some (or all)
52 * Make an URL shorter.
54 * @param string $url URL to shorten
56 * @return string shortened version of the url, or null on failure
59 protected abstract function shorten($url);
62 * Utility to get the data at an URL
64 * @param string $url URL to fetch
66 * @return string response body
68 * @todo rename to code-standard camelCase httpGet()
71 protected function http_get($url)
73 $request = HTTPClient::start();
74 $response = $request->get($url);
75 return $response->getBody();
79 * Utility to post a request and get a response URL
81 * @param string $url URL to fetch
82 * @param array $data post parameters
84 * @return string response body
86 * @todo rename to code-standard httpPost()
89 protected function http_post($url, $data)
91 $request = HTTPClient::start();
92 $response = $request->post($url, null, $data);
93 return $response->getBody();
99 * Called when all plugins have been initialized
101 * @return boolean hook value
104 function onInitializePlugin()
106 if (!isset($this->shortenerName)) {
107 throw new Exception("must specify a shortenerName");
113 * Called when a showing the URL shortener drop-down box
115 * Properties of the shortening service currently only
116 * include whether it's a free service.
118 * @param array &$shorteners array mapping shortener name to properties
120 * @return boolean hook value
123 function onGetUrlShorteners(&$shorteners)
125 $shorteners[$this->shortenerName] =
126 array('freeService' => $this->freeService);
131 * Called to shorten an URL
133 * @param string $url URL to shorten
134 * @param string $shortenerName Shortening service. Don't handle if it's
136 * @param string &$shortenedUrl URL after shortening; out param.
138 * @return boolean hook value
141 function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
143 if ($shortenerName == $this->shortenerName) {
144 $result = $this->shorten($url);
145 if (isset($result) && $result != null && $result !== false) {
146 $shortenedUrl = $result;
148 __CLASS__ . ": $this->shortenerName ".
149 "shortened $url to $shortenedUrl");