3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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;
26 function __construct($service_url)
28 $this->service_url = $service_url;
31 function shorten($url)
33 if ($this->is_long($url)) return $this->shorten_imp($url);
37 protected function shorten_imp($url) {
41 private function is_long($url) {
42 return strlen($url) >= 30;
45 protected function http_post($data) {
47 curl_setopt($ch, CURLOPT_URL, $this->service_url);
48 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49 curl_setopt($ch, CURLOPT_POST, 1);
50 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
51 $response = curl_exec($ch);
52 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
54 if (($code < 200) || ($code >= 400)) return false;
58 protected function http_get($url) {
59 $encoded_url = urlencode($url);
60 return file_get_contents("{$this->service_url}$encoded_url");
63 protected function tidy($response) {
64 $response = str_replace(' ', ' ', $response);
65 $config = array('output-xhtml' => true);
67 $tidy->parseString($response, $config, 'utf8');
73 class LilUrl extends ShortUrlApi
75 function __construct()
77 parent::__construct('http://ur1.ca/');
80 protected function shorten_imp($url) {
81 $data['longurl'] = $url;
82 $response = $this->http_post($data);
83 if (!$response) return $url;
84 $y = @simplexml_load_string($response);
85 if (!isset($y->body)) return $url;
86 $x = $y->body->p[0]->a->attributes();
87 if (isset($x['href'])) return $x['href'];
93 class PtitUrl extends ShortUrlApi
95 function __construct()
97 parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
100 protected function shorten_imp($url) {
101 $response = $this->http_get($url);
102 if (!$response) return $url;
103 $response = $this->tidy($response);
104 $y = @simplexml_load_string($response);
105 if (!isset($y->body)) return $url;
106 $xml = $y->body->center->table->tr->td->pre->a->attributes();
107 if (isset($xml['href'])) return $xml['href'];
112 class TightUrl extends ShortUrlApi
114 function __construct()
116 parent::__construct('http://2tu.us/?save=y&url=');
119 protected function shorten_imp($url) {
120 $response = $this->http_get($url);
121 if (!$response) return $url;
122 $response = $this->tidy($response);
123 $y = @simplexml_load_string($response);
124 if (!isset($y->body)) return $url;
125 $xml = $y->body->p[0]->code[0]->a->attributes();
126 if (isset($xml['href'])) return $xml['href'];