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