2008-11-08 04:38:31 +09:00
|
|
|
<?php
|
|
|
|
/*
|
2009-08-26 07:14:12 +09:00
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
2009-08-26 07:12:20 +09:00
|
|
|
* Copyright (C) 2008, 2009, StatusNet, Inc.
|
2008-11-08 04:38:31 +09: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 23:41:36 +09:00
|
|
|
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
|
2008-11-08 04:38:31 +09:00
|
|
|
|
2009-09-04 03:58:50 +09:00
|
|
|
abstract class ShortUrlApi
|
2008-12-24 04:49:23 +09:00
|
|
|
{
|
2008-11-08 04:38:31 +09:00
|
|
|
protected $service_url;
|
2009-05-14 03:27:32 +09:00
|
|
|
protected $long_limit = 27;
|
2008-11-08 04:38:31 +09:00
|
|
|
|
2008-12-24 04:33:23 +09:00
|
|
|
function __construct($service_url)
|
|
|
|
{
|
2008-11-08 04:38:31 +09:00
|
|
|
$this->service_url = $service_url;
|
|
|
|
}
|
|
|
|
|
2008-12-24 04:33:23 +09:00
|
|
|
function shorten($url)
|
|
|
|
{
|
2008-11-08 04:38:31 +09:00
|
|
|
if ($this->is_long($url)) return $this->shorten_imp($url);
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2009-09-04 03:58:50 +09:00
|
|
|
protected abstract function shorten_imp($url);
|
2008-11-08 04:38:31 +09:00
|
|
|
|
2009-09-04 03:58:50 +09:00
|
|
|
protected function is_long($url) {
|
2009-06-11 22:07:41 +09:00
|
|
|
return strlen($url) >= common_config('site', 'shorturllength');
|
2008-11-08 04:38:31 +09:00
|
|
|
}
|
|
|
|
|
2009-10-29 04:29:20 +09:00
|
|
|
protected function http_post($data)
|
|
|
|
{
|
|
|
|
$request = HTTPClient::start();
|
|
|
|
$response = $request->post($this->service_url, null, $data);
|
|
|
|
return $response->getBody();
|
2008-11-08 04:38:31 +09:00
|
|
|
}
|
|
|
|
|
2009-10-29 04:29:20 +09:00
|
|
|
protected function http_get($url)
|
|
|
|
{
|
|
|
|
$request = HTTPClient::start();
|
|
|
|
$response = $request->get($this->service_url . urlencode($url));
|
|
|
|
return $response->getBody();
|
2008-11-08 04:38:31 +09: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|