3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
24 protected $service_url;
25 protected $long_limit = 27;
27 function __construct($service_url)
29 $this->service_url = $service_url;
32 function shorten($url)
34 if ($this->is_long($url)) return $this->shorten_imp($url);
38 protected function shorten_imp($url) {
42 private function is_long($url) {
43 return strlen($url) >= common_config('site', 'shorturllength');
46 protected function http_post($data) {
48 curl_setopt($ch, CURLOPT_URL, $this->service_url);
49 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
50 curl_setopt($ch, CURLOPT_POST, 1);
51 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
52 $response = curl_exec($ch);
53 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
55 if (($code < 200) || ($code >= 400)) return false;
59 protected function http_get($url) {
60 $encoded_url = urlencode($url);
61 return file_get_contents("{$this->service_url}$encoded_url");
64 protected function tidy($response) {
65 $response = str_replace(' ', ' ', $response);
66 $config = array('output-xhtml' => true);
68 $tidy->parseString($response, $config, 'utf8');
74 class LilUrl extends ShortUrlApi
76 function __construct()
78 parent::__construct('http://ur1.ca/');
81 protected function shorten_imp($url) {
82 $data['longurl'] = $url;
83 $response = $this->http_post($data);
84 if (!$response) return $url;
85 $y = @simplexml_load_string($response);
86 if (!isset($y->body)) return $url;
87 $x = $y->body->p[0]->a->attributes();
88 if (isset($x['href'])) return $x['href'];
94 class PtitUrl extends ShortUrlApi
96 function __construct()
98 parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
101 protected function shorten_imp($url) {
102 $response = $this->http_get($url);
103 if (!$response) return $url;
104 $response = $this->tidy($response);
105 $y = @simplexml_load_string($response);
106 if (!isset($y->body)) return $url;
107 $xml = $y->body->center->table->tr->td->pre->a->attributes();
108 if (isset($xml['href'])) return $xml['href'];
113 class TightUrl extends ShortUrlApi
115 function __construct()
117 parent::__construct('http://2tu.us/?save=y&url=');
120 protected function shorten_imp($url) {
121 $response = $this->http_get($url);
122 if (!$response) return $url;
123 $response = $this->tidy($response);
124 $y = @simplexml_load_string($response);
125 if (!isset($y->body)) return $url;
126 $xml = $y->body->p[0]->code[0]->a->attributes();
127 if (isset($xml['href'])) return $xml['href'];