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