]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/Shorturl_api.php
take an argument for fixup_utf8
[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 {
24     protected $service_url;
25
26     function __construct($service_url)
27     {
28         $this->service_url = $service_url;
29     }
30
31     function shorten($url)
32     {
33         if ($this->is_long($url)) return $this->shorten_imp($url);
34         return $url;
35     }
36
37     protected function shorten_imp($url) {
38         return "To Override";
39     }
40
41     private function is_long($url) {
42         return strlen($url) >= 30;
43     }
44
45     protected function http_post($data) {
46         $ch = curl_init();
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);
53         curl_close($ch);
54         if (($code < 200) || ($code >= 400)) return false;
55         return $response;
56     }
57
58     protected function http_get($url) {
59         $encoded_url = urlencode($url);
60         return file_get_contents("{$this->service_url}$encoded_url");
61     }
62
63     protected function tidy($response) {
64         $response = str_replace('&nbsp;', ' ', $response);
65         $config = array('output-xhtml' => true);
66         $tidy = new tidy;
67         $tidy->parseString($response, $config, 'utf8');
68         $tidy->cleanRepair();
69         return (string)$tidy;
70     }
71 }
72
73 class LilUrl extends ShortUrlApi
74 {
75     function __construct()
76     {
77         parent::__construct('http://ur1.ca/');
78     }
79
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'];
88         return $url;
89     }
90 }
91
92
93 class PtitUrl extends ShortUrlApi
94 {
95     function __construct()
96     {
97         parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
98     }
99
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'];
108         return $url;
109     }
110 }
111
112 class TightUrl extends ShortUrlApi
113 {
114     function __construct()
115     {
116         parent::__construct('http://2tu.us/?save=y&url=');
117     }
118
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'];
127         return $url;
128     }
129 }
130