]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/Shorturl_api.php
HTML output in RSS 2.0 and Atom
[quix0rs-gnu-social.git] / lib / Shorturl_api.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class ShortUrlApi {
23     protected $service_url;
24
25     function __construct($service_url) {
26         $this->service_url = $service_url;
27     }
28
29     function shorten($url) {
30         if ($this->is_long($url)) return $this->shorten_imp($url);
31         return $url;
32     }
33
34     protected function shorten_imp($url) {
35         return "To Override";
36     }
37
38     private function is_long($url) {
39         return strlen($url) > 20;
40     }
41
42     protected function http_post($data) {
43         $ch = curl_init();
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);
50         curl_close($ch);
51         if (($code < 200) || ($code >= 400)) return false;
52         return $response;
53     }
54
55     protected function http_get($url) {
56         $encoded_url = urlencode($url);
57         return file_get_contents("{$this->service_url}$encoded_url");
58     }
59
60     protected function tidy($response) {
61         $response = str_replace('&nbsp;', ' ', $response);
62         $config = array('output-xhtml' => true);
63         $tidy = new tidy;
64         $tidy->parseString($response, $config, 'utf8');
65         $tidy->cleanRepair();
66         return (string)$tidy;
67     }
68 }
69
70 class LilUrl extends ShortUrlApi {
71     function __construct() {
72         parent::__construct('http://ur1.ca/');
73     }
74
75     protected function shorten_imp($url) {
76         $data['longurl'] = $url;
77         $response = $this->http_post($data);
78         if (!$response) return $url;
79         $x = simplexml_load_string($response)->body->p[0]->a->attributes();
80         if (isset($x['href'])) return $x['href'];
81         return $url;
82     }
83 }
84
85
86 class PtitUrl extends ShortUrlApi {
87     function __construct() {
88         parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
89     }
90
91     protected function shorten_imp($url) {
92         $response = $this->http_get($url);
93         if (!$response) return $url;
94         $response = $this->tidy($response);
95         $xml = simplexml_load_string($response)->body->center->table->tr->td->pre->a->attributes();
96         if (isset($xml['href'])) return $xml['href'];
97         return $url;
98     }
99 }
100
101 class TightUrl extends ShortUrlApi {
102     function __construct() {
103         parent::__construct('http://2tu.us/?save=y&url=');
104     }
105
106     protected function shorten_imp($url) {
107         $response = $this->http_get($url);
108         if (!$response) return $url;
109         $response = $this->tidy($response);
110         $xml = simplexml_load_string($response)->body->p[0]->code[0]->a->attributes();
111         if (isset($xml['href'])) return $xml['href'];
112         return $url;
113     }
114 }
115
116