2008-11-07 19:38:31 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2009-08-25 23:14:12 +01:00
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
2009-08-25 23:12:20 +01:00
|
|
|
* Copyright (C) 2008, 2009, StatusNet, Inc.
|
2008-11-07 19:38:31 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-08-26 15:41:36 +01:00
|
|
|
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
|
2008-11-07 19:38:31 +00:00
|
|
|
|
2009-09-03 19:58:50 +01:00
|
|
|
abstract class ShortUrlApi
|
2008-12-23 19:49:23 +00:00
|
|
|
{
|
2008-11-07 19:38:31 +00:00
|
|
|
protected $service_url;
|
2009-05-13 19:27:32 +01:00
|
|
|
protected $long_limit = 27;
|
2008-11-07 19:38:31 +00:00
|
|
|
|
2008-12-23 19:33:23 +00:00
|
|
|
function __construct($service_url)
|
|
|
|
{
|
2008-11-07 19:38:31 +00:00
|
|
|
$this->service_url = $service_url;
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:33:23 +00:00
|
|
|
function shorten($url)
|
|
|
|
{
|
2008-11-07 19:38:31 +00:00
|
|
|
if ($this->is_long($url)) return $this->shorten_imp($url);
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2009-09-03 19:58:50 +01:00
|
|
|
protected abstract function shorten_imp($url);
|
2008-11-07 19:38:31 +00:00
|
|
|
|
2009-09-03 19:58:50 +01:00
|
|
|
protected function is_long($url) {
|
2009-06-11 14:07:41 +01:00
|
|
|
return strlen($url) >= common_config('site', 'shorturllength');
|
2008-11-07 19:38:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 19:29:20 +00:00
|
|
|
protected function http_post($data)
|
|
|
|
{
|
|
|
|
$request = HTTPClient::start();
|
|
|
|
$response = $request->post($this->service_url, null, $data);
|
|
|
|
return $response->getBody();
|
2008-11-07 19:38:31 +00:00
|
|
|
}
|
|
|
|
|
2009-10-28 19:29:20 +00:00
|
|
|
protected function http_get($url)
|
|
|
|
{
|
|
|
|
$request = HTTPClient::start();
|
|
|
|
$response = $request->get($this->service_url . urlencode($url));
|
|
|
|
return $response->getBody();
|
2008-11-07 19:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tidy($response) {
|
|
|
|
$response = str_replace(' ', ' ', $response);
|
|
|
|
$config = array('output-xhtml' => true);
|
|
|
|
$tidy = new tidy;
|
|
|
|
$tidy->parseString($response, $config, 'utf8');
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
return (string)$tidy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|