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 | if (!defined('DC_CONTEXT_ADMIN')){return;} |
---|
14 | |
---|
15 | require_once dirname(__FILE__).'/_widgets.php'; |
---|
16 | |
---|
17 | # Plugin menu |
---|
18 | $_menu['Plugins']->addItem( |
---|
19 | __('kUtRL'), |
---|
20 | 'plugin.php?p=kUtRL','index.php?pf=kUtRL/icon.png', |
---|
21 | preg_match('/plugin.php\?p=kUtRL(&.*)?$/',$_SERVER['REQUEST_URI']), |
---|
22 | $core->auth->check('admin',$core->blog->id) |
---|
23 | ); |
---|
24 | |
---|
25 | # Admin behaviors |
---|
26 | if ($core->blog->settings->kutrl_active) |
---|
27 | { |
---|
28 | $core->addBehavior('adminPostFormSidebar',array('adminKutrl','adminPostFormSidebar')); |
---|
29 | $core->addBehavior('adminAfterPostUpdate',array('adminKutrl','adminAfterPostUpdate')); // update existing short url |
---|
30 | $core->addBehavior('adminAfterPostUpdate',array('adminKutrl','adminAfterPostCreate')); // create new short url |
---|
31 | $core->addBehavior('adminAfterPostCreate',array('adminKutrl','adminAfterPostCreate')); |
---|
32 | $core->addBehavior('adminBeforePostDelete',array('adminKutrl','adminBeforePostDelete')); |
---|
33 | $core->addBehavior('adminPostsActionsCombo',array('adminKutrl','adminPostsActionsCombo')); |
---|
34 | $core->addBehavior('adminPostsActions',array('adminKutrl','adminPostsActions')); |
---|
35 | } |
---|
36 | |
---|
37 | # Import/export |
---|
38 | if ($core->blog->settings->kutrl_extend_importexport) |
---|
39 | { |
---|
40 | $core->addBehavior('exportFull',array('backupKutrl','exportFull')); |
---|
41 | $core->addBehavior('exportSingle',array('backupKutrl','exportSingle')); |
---|
42 | $core->addBehavior('importInit',array('backupKutrl','importInit')); |
---|
43 | $core->addBehavior('importSingle',array('backupKutrl','importSingle')); |
---|
44 | $core->addBehavior('importFull',array('backupKutrl','importFull')); |
---|
45 | } |
---|
46 | |
---|
47 | # Admin behaviors class |
---|
48 | class adminKutrl |
---|
49 | { |
---|
50 | public static function adminPostFormSidebar($post) |
---|
51 | { |
---|
52 | global $core; |
---|
53 | |
---|
54 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
55 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
56 | $_limit_to_blog = (boolean) $core->blog->settings->kutrl_limit_to_blog; |
---|
57 | |
---|
58 | if (!$_active || !$_admin) return; |
---|
59 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
60 | |
---|
61 | try |
---|
62 | { |
---|
63 | $kut = new $core->kutrlServices[$_admin]($core,$_limit_to_blog); |
---|
64 | } |
---|
65 | catch (Exception $e) |
---|
66 | { |
---|
67 | return; |
---|
68 | } |
---|
69 | |
---|
70 | if ($post) |
---|
71 | { |
---|
72 | $post_url = $post->getURL(); |
---|
73 | $rs = $kut->isKnowUrl($post_url); |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | $post_url = ''; |
---|
78 | $rs = false; |
---|
79 | } |
---|
80 | |
---|
81 | echo |
---|
82 | '<h3 class="clear">'.__('Short link').'</h3>'. |
---|
83 | form::hidden(array('kutrl_old_post_url'),$post_url); |
---|
84 | |
---|
85 | if (!$rs) |
---|
86 | { |
---|
87 | if (empty($_POST['kutrl_old_post_url']) && $core->blog->settings->kutrl_admin_entry_default) |
---|
88 | { |
---|
89 | $chk = true; |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | $chk = !empty($_POST['kutrl_create']); |
---|
94 | } |
---|
95 | echo |
---|
96 | '<p><label class="classic">'.form::checkbox('kutrl_create',1,$chk,'',3).' '. |
---|
97 | __('Create short link').'</label></p>'; |
---|
98 | |
---|
99 | if ($kut->allow_customized_hash) |
---|
100 | { |
---|
101 | echo |
---|
102 | '<p class="classic">'. |
---|
103 | '<label for="custom">'.__('Custom short link:').'</label>'. |
---|
104 | form::field('kutrl_create_custom',32,32,''). |
---|
105 | '</p>'; |
---|
106 | } |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | $count = $rs->counter; |
---|
111 | if ($count == 0) |
---|
112 | { |
---|
113 | $title = __('never followed'); |
---|
114 | } |
---|
115 | elseif ($count == 1) |
---|
116 | { |
---|
117 | $title = __('followed one time'); |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | $title = sprintf(__('followed %s times'),$count); |
---|
122 | } |
---|
123 | $href = $kut->url_base.$rs->hash; |
---|
124 | |
---|
125 | echo |
---|
126 | '<p><label class="classic">'.form::checkbox('kutrl_delete',1,!empty($_POST['kutrl_delete']),'',3).' '. |
---|
127 | __('delete short link').'</label></p>'. |
---|
128 | '<p><a href="'.$href.'" '.'title="'.$title.'">'.$href.'</a></p>'; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | public static function adminAfterPostUpdate($cur,$post_id) |
---|
133 | { |
---|
134 | global $core; |
---|
135 | |
---|
136 | # Create: see adminAfterPostCreate |
---|
137 | if (!empty($_POST['kutrl_create'])) return; |
---|
138 | |
---|
139 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
140 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
141 | $_limit_to_blog = (boolean) $core->blog->settings->kutrl_limit_to_blog; |
---|
142 | |
---|
143 | if (!$_active || !$_admin) return; |
---|
144 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
145 | |
---|
146 | try |
---|
147 | { |
---|
148 | $kut = new $core->kutrlServices[$_admin]($core,$_limit_to_blog); |
---|
149 | } |
---|
150 | catch (Exception $e) |
---|
151 | { |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | if (empty($_POST['kutrl_old_post_url'])) return; |
---|
156 | |
---|
157 | $old_post_url = $_POST['kutrl_old_post_url']; |
---|
158 | |
---|
159 | if (!($rs = $kut->isKnowUrl($old_post_url))) return; |
---|
160 | |
---|
161 | $rs = $core->blog->getPosts(array('post_id'=>$post_id)); |
---|
162 | |
---|
163 | if ($rs->isEmpty()) return; |
---|
164 | |
---|
165 | $new_post_url = $rs->getURL(); |
---|
166 | |
---|
167 | # Delete |
---|
168 | if (!empty($_POST['kutrl_delete'])) |
---|
169 | { |
---|
170 | $kut->remove($old_post_url); |
---|
171 | } |
---|
172 | # Update |
---|
173 | else |
---|
174 | { |
---|
175 | if ($old_post_url == $new_post_url) return; |
---|
176 | |
---|
177 | $kut->remove($old_post_url); |
---|
178 | |
---|
179 | $kut->hash($new_post_url,$custom); // better to update (not yet implemented) |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | public static function adminAfterPostCreate($cur,$post_id) |
---|
184 | { |
---|
185 | global $core; |
---|
186 | |
---|
187 | if (empty($_POST['kutrl_create'])) return; |
---|
188 | |
---|
189 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
190 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
191 | $_limit_to_blog = (boolean) $core->blog->settings->kutrl_limit_to_blog; |
---|
192 | |
---|
193 | if (!$_active || !$_admin) return; |
---|
194 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
195 | |
---|
196 | try |
---|
197 | { |
---|
198 | $kut = new $core->kutrlServices[$_admin]($core,$_limit_to_blog); |
---|
199 | } |
---|
200 | catch (Exception $e) |
---|
201 | { |
---|
202 | return; |
---|
203 | } |
---|
204 | |
---|
205 | $rs = $core->blog->getPosts(array('post_id'=>$post_id)); |
---|
206 | |
---|
207 | if ($rs->isEmpty()) return; |
---|
208 | |
---|
209 | $custom = !empty($_POST['kutrl_create_custom']) && $kut->allow_customized_hash ? |
---|
210 | $_POST['kutrl_create_custom'] : null; |
---|
211 | |
---|
212 | $kut->hash($rs->getURL(),$custom); |
---|
213 | } |
---|
214 | |
---|
215 | public static function adminBeforePostDelete($post_id) |
---|
216 | { |
---|
217 | global $core; |
---|
218 | |
---|
219 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
220 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
221 | $_limit_to_blog = (boolean) $core->blog->settings->kutrl_limit_to_blog; |
---|
222 | |
---|
223 | if (!$_active || !$_admin) return; |
---|
224 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
225 | |
---|
226 | try |
---|
227 | { |
---|
228 | $kut = new $core->kutrlServices[$_admin]($core,$_limit_to_blog); |
---|
229 | } |
---|
230 | catch (Exception $e) |
---|
231 | { |
---|
232 | return; |
---|
233 | } |
---|
234 | |
---|
235 | $rs = $core->blog->getPosts(array('post_id'=>$post_id)); |
---|
236 | |
---|
237 | if ($rs->isEmpty()) return; |
---|
238 | |
---|
239 | $kut->remove($rs->getURL()); |
---|
240 | } |
---|
241 | |
---|
242 | public static function adminPostsActionsCombo($args) |
---|
243 | { |
---|
244 | global $core; |
---|
245 | |
---|
246 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
247 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
248 | $_auth_check = $core->auth->check('admin',$core->blog->id); |
---|
249 | |
---|
250 | if (!$_active || !$_admin || !$_auth_check) return; |
---|
251 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
252 | |
---|
253 | $args[0][__('create short link')] = 'kutrl_create'; |
---|
254 | $args[0][__('delete short link')] = 'kutrl_delete'; |
---|
255 | } |
---|
256 | |
---|
257 | public static function adminPostsActions($core,$posts,$action,$redir) |
---|
258 | { |
---|
259 | if ($action != 'kutrl_create' |
---|
260 | && $action != 'kutrl_delete') return; |
---|
261 | |
---|
262 | |
---|
263 | $_active = (boolean) $core->blog->settings->kutrl_active; |
---|
264 | $_admin = (string) $core->blog->settings->kutrl_admin_service; |
---|
265 | $_limit_to_blog = (boolean) $core->blog->settings->kutrl_limit_to_blog; |
---|
266 | |
---|
267 | if (!$_active || !$_admin) return; |
---|
268 | if (!isset($core->kutrlServices[$_admin])) return; |
---|
269 | |
---|
270 | try |
---|
271 | { |
---|
272 | $kut = new $core->kutrlServices[$_admin]($core,$_limit_to_blog); |
---|
273 | } |
---|
274 | catch (Exception $e) |
---|
275 | { |
---|
276 | return; |
---|
277 | } |
---|
278 | |
---|
279 | while ($posts->fetch()) |
---|
280 | { |
---|
281 | $url = $posts->getURL(); |
---|
282 | |
---|
283 | if ($action == 'kutrl_create') |
---|
284 | { |
---|
285 | $kut->hash($url); |
---|
286 | } |
---|
287 | |
---|
288 | if ($action == 'kutrl_delete') |
---|
289 | { |
---|
290 | $kut->remove($url); |
---|
291 | } |
---|
292 | } |
---|
293 | $core->blog->triggerBlog(); |
---|
294 | http::redirect($redir.'&done=1'); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | # Import/export behaviors for Import/export plugin |
---|
299 | class backupKutrl |
---|
300 | { |
---|
301 | public static function exportSingle($core,$exp,$blog_id) |
---|
302 | { |
---|
303 | $exp->export('kutrl', |
---|
304 | 'SELECT kut_id, blog_id, kut_service, kut_type, '. |
---|
305 | 'kut_hash, kut_url, kut_dt, kut_password, kut_counter '. |
---|
306 | 'FROM '.$core->prefix.'kutrl '. |
---|
307 | "WHERE blog_id = '".$blog_id."' " |
---|
308 | ); |
---|
309 | } |
---|
310 | |
---|
311 | public static function exportFull($core,$exp) |
---|
312 | { |
---|
313 | $exp->exportTable('kutrl'); |
---|
314 | } |
---|
315 | |
---|
316 | public static function importInit($bk,$core) |
---|
317 | { |
---|
318 | $bk->cur_kutrl = $core->con->openCursor($core->prefix.'kutrl'); |
---|
319 | $bk->kutrl = new kutrlLog($core); |
---|
320 | } |
---|
321 | |
---|
322 | public static function importSingle($line,$bk,$core) |
---|
323 | { |
---|
324 | if ($line->__name == 'kutrl') |
---|
325 | { |
---|
326 | # Do nothing if str/type exists ! |
---|
327 | if (false === $bk->kutrl->select($line->kut_url,$line->kut_hash,$line->kut_type,$line->kut_service)) |
---|
328 | { |
---|
329 | $bk->kutrl->insert($line->kut_url,$line->kut_hash,$line->kut_type,$line->kut_service); |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | public static function importFull($line,$bk,$core) |
---|
335 | { |
---|
336 | if ($line->__name == 'kutrl') |
---|
337 | { |
---|
338 | $bk->cur_kutrl->clean(); |
---|
339 | |
---|
340 | $bk->cur_kutrl->kut_id = (integer) $line->kut_id; |
---|
341 | $bk->cur_kutrl->blog_id = (string) $line->blog_id; |
---|
342 | $bk->cur_kutrl->kut_service = (string) $line->kut_service; |
---|
343 | $bk->cur_kutrl->kut_type = (string) $line->kut_type; |
---|
344 | $bk->cur_kutrl->kut_hash = (string) $line->kut_hash; |
---|
345 | $bk->cur_kutrl->kut_url = (string) $line->kut_url; |
---|
346 | $bk->cur_kutrl->kut_dt = (string) $line->miniurl_dt; |
---|
347 | $bk->cur_kutrl->kut_counter = (integer) $line->kut_counter; |
---|
348 | $bk->cur_kutrl->kut_password = (string) $line->kut_password; |
---|
349 | |
---|
350 | $bk->cur_kutrl->insert(); |
---|
351 | } |
---|
352 | } |
---|
353 | } |
---|
354 | ?> |
---|