1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of kUtRL, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | # This file contents parent class of shorten link services |
---|
14 | |
---|
15 | class kutrlServices |
---|
16 | { |
---|
17 | public $core; |
---|
18 | public $error; |
---|
19 | public $s; |
---|
20 | public $log; |
---|
21 | public $limit_to_blog; |
---|
22 | |
---|
23 | public $id = 'undefined'; |
---|
24 | public $name = 'undefined'; |
---|
25 | |
---|
26 | public $allow_customized_hash = false; |
---|
27 | public $allowed_protocols = array('http://'); |
---|
28 | public $url_base = ''; |
---|
29 | public $url_min_length = 0; |
---|
30 | |
---|
31 | public function __construct($core,$limit_to_blog=true) |
---|
32 | { |
---|
33 | $this->core = $core; |
---|
34 | $this->s = kutrlSettings($core); |
---|
35 | $this->log = new kutrlLog($core); |
---|
36 | $this->limit_to_blog = (boolean) $limit_to_blog; |
---|
37 | $this->error = new dcError(); |
---|
38 | $this->error->setHTMLFormat('%s',"%s\n"); |
---|
39 | } |
---|
40 | |
---|
41 | # Save settings from admin page |
---|
42 | public function saveSettings() |
---|
43 | { |
---|
44 | return null; |
---|
45 | } |
---|
46 | |
---|
47 | # Settings form for admin page |
---|
48 | public function settingsForm() |
---|
49 | { |
---|
50 | echo |
---|
51 | '<p class="form-note">'. |
---|
52 | __('There is nothing to configure for this service.'). |
---|
53 | '</p>'; |
---|
54 | } |
---|
55 | |
---|
56 | # Test if service is well configured |
---|
57 | public function testService() |
---|
58 | { |
---|
59 | return null; |
---|
60 | } |
---|
61 | |
---|
62 | # Test if an url is valid |
---|
63 | public function isValidUrl($url) |
---|
64 | { |
---|
65 | return (boolean) filter_var($url,FILTER_VALIDATE_URL); |
---|
66 | } |
---|
67 | |
---|
68 | # Test if an url contents know prefix |
---|
69 | public function isServiceUrl($url) |
---|
70 | { |
---|
71 | return strpos($url,$this->url_base) === 0; |
---|
72 | } |
---|
73 | |
---|
74 | # Test if an url is long enoutgh |
---|
75 | public function isLongerUrl($url) |
---|
76 | { |
---|
77 | return ($this->url_min_length >= $url); |
---|
78 | } |
---|
79 | |
---|
80 | # Test if an url protocol (eg: http://) is allowed |
---|
81 | public function isProtocolUrl($url) |
---|
82 | { |
---|
83 | foreach($this->allowed_protocols as $protocol) |
---|
84 | { |
---|
85 | if (empty($protocol)) continue; |
---|
86 | |
---|
87 | if (strpos($url,$protocol) === 0) return true; |
---|
88 | } |
---|
89 | return false; |
---|
90 | } |
---|
91 | |
---|
92 | # Test if an url is from current blog |
---|
93 | public function isBlogUrl($url) |
---|
94 | { |
---|
95 | $base = $this->core->blog->url; |
---|
96 | $url = substr($url,0,strlen($base)); |
---|
97 | |
---|
98 | return $url == $base; |
---|
99 | } |
---|
100 | |
---|
101 | # Test if an url is know |
---|
102 | public function isKnowUrl($url) |
---|
103 | { |
---|
104 | return $this->log->select($url,null,$this->id,'kutrl'); |
---|
105 | } |
---|
106 | |
---|
107 | # Test if an custom short url is know |
---|
108 | public function isKnowHash($hash) |
---|
109 | { |
---|
110 | return $this->log->select(null,$hash,$this->id,'kutrl'); |
---|
111 | } |
---|
112 | |
---|
113 | # Create hash from url |
---|
114 | public function hash($url,$hash=null) |
---|
115 | { |
---|
116 | $url = trim($this->core->con->escape($url)); |
---|
117 | if ('undefined' === $this->id) |
---|
118 | { |
---|
119 | return false; |
---|
120 | } |
---|
121 | if ($hash && !$this->allow_customized_hash) |
---|
122 | { |
---|
123 | return false; |
---|
124 | } |
---|
125 | if ($this->isServiceUrl($url)) |
---|
126 | { |
---|
127 | return false; |
---|
128 | } |
---|
129 | if (!$this->isLongerUrl($url)) |
---|
130 | { |
---|
131 | return false; |
---|
132 | } |
---|
133 | if ($this->limit_to_blog && !$this->isBlogUrl($url)) |
---|
134 | { |
---|
135 | return false; |
---|
136 | } |
---|
137 | if ($hash && false !== ($rs = $this->isKnowHash($hash))) |
---|
138 | { |
---|
139 | return false; |
---|
140 | } |
---|
141 | if (false === ($rs = $this->isKnowUrl($url))) |
---|
142 | { |
---|
143 | if (false === ($rs = $this->createHash($url,$hash))) |
---|
144 | { |
---|
145 | return false; |
---|
146 | } |
---|
147 | |
---|
148 | $this->log->insert($rs->url,$rs->hash,$rs->type,'kutrl'); |
---|
149 | $this->core->blog->triggerBlog(); |
---|
150 | |
---|
151 | |
---|
152 | # --BEHAVIOR-- kutrlAfterCreateShortUrl |
---|
153 | $this->core->callBehavior('kutrlAfterCreateShortUrl',$rs); |
---|
154 | |
---|
155 | |
---|
156 | } |
---|
157 | return $rs; |
---|
158 | } |
---|
159 | |
---|
160 | # Create a hash for a given url (and its custom hash) |
---|
161 | public function createHash($url,$hash=null) |
---|
162 | { |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | # Remove an url from list of know urls |
---|
167 | public function remove($url) |
---|
168 | { |
---|
169 | if (!($rs = $this->isKnowUrl($url))) return false; |
---|
170 | $this->deleteUrl($url); |
---|
171 | $this->log->delete($rs->id); |
---|
172 | return true; |
---|
173 | } |
---|
174 | |
---|
175 | # Delete url on service |
---|
176 | public function deleteUrl($url) |
---|
177 | { |
---|
178 | return null; |
---|
179 | } |
---|
180 | |
---|
181 | # Retrieve long url from hash |
---|
182 | public function getUrl($hash) |
---|
183 | { |
---|
184 | return false; |
---|
185 | } |
---|
186 | |
---|
187 | # Post request |
---|
188 | public static function post($server,$data,$verbose=true,$get=false) |
---|
189 | { |
---|
190 | $url = (string) $server; |
---|
191 | $client = netHttp::initClient($url,$url); |
---|
192 | $client->setUserAgent('kUtRL - http://kutrl.fr'); |
---|
193 | $client->setPersistReferers(false); |
---|
194 | |
---|
195 | if ($get) |
---|
196 | { |
---|
197 | $client->get($url,$data); |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | $client->post($url,$data); |
---|
202 | } |
---|
203 | |
---|
204 | if (!$verbose && $client->getStatus() != 200) |
---|
205 | { |
---|
206 | return false; |
---|
207 | } |
---|
208 | |
---|
209 | if ($verbose) |
---|
210 | { |
---|
211 | return $client->getContent(); |
---|
212 | } |
---|
213 | return true; |
---|
214 | } |
---|
215 | } |
---|
216 | ?> |
---|