Dotclear

source: plugins/kUtRL/_admin.php @ 2190

Revision 2190, 9.8 KB checked in by JcDenis, 14 years ago (diff)

kUtRL 0.3:

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

Sites map