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); }
23 protected $service_url;
25 function __construct($service_url) {
26 $this->service_url = $service_url;
29 function shorten($url) {
30 if ($this->is_long($url)) return $this->shorten_imp($url);
34 protected function shorten_imp($url) {
38 private function is_long($url) {
39 return strlen($url) >= 30;
42 protected function http_post($data) {
44 curl_setopt($ch, CURLOPT_URL, $this->service_url);
45 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
46 curl_setopt($ch, CURLOPT_POST, 1);
47 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
48 $response = curl_exec($ch);
49 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
51 if (($code < 200) || ($code >= 400)) return false;
55 protected function http_get($url) {
56 $encoded_url = urlencode($url);
57 return file_get_contents("{$this->service_url}$encoded_url");
60 protected function tidy($response) {
61 $response = str_replace(' ', ' ', $response);
62 $config = array('output-xhtml' => true);
64 $tidy->parseString($response, $config, 'utf8');
70 class LilUrl extends ShortUrlApi {
71 function __construct() {
72 parent::__construct('http://ur1.ca/');
75 protected function shorten_imp($url) {
76 $data['longurl'] = $url;
77 $response = $this->http_post($data);
78 if (!$response) return $url;
79 $y = @simplexml_load_string($response);
80 if (!isset($y->body)) return $url;
81 $x = $y->body->p[0]->a->attributes();
82 if (isset($x['href'])) return $x['href'];
88 class PtitUrl extends ShortUrlApi {
89 function __construct() {
90 parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
93 protected function shorten_imp($url) {
94 $response = $this->http_get($url);
95 if (!$response) return $url;
96 $response = $this->tidy($response);
97 $y = @simplexml_load_string($response);
98 if (!isset($y->body)) return $url;
99 $xml = $y->body->center->table->tr->td->pre->a->attributes();
100 if (isset($xml['href'])) return $xml['href'];
105 class TightUrl extends ShortUrlApi {
106 function __construct() {
107 parent::__construct('http://2tu.us/?save=y&url=');
110 protected function shorten_imp($url) {
111 $response = $this->http_get($url);
112 if (!$response) return $url;
113 $response = $this->tidy($response);
114 $y = @simplexml_load_string($response);
115 if (!isset($y->body)) return $url;
116 $xml = $y->body->p[0]->code[0]->a->attributes();
117 if (isset($xml['href'])) return $xml['href'];