Dotclear

source: plugins/kUtRL/inc/services/class.local.service.php @ 2190

Revision 2190, 5.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_RC_PATH')){return;}
14
15class localKutrlService extends kutrlServices
16{
17     public $id = 'local';
18     public $name = 'kUtRL';
19     public $home = 'http://kutrl.fr';
20
21     public function __construct($core,$limit_to_blog=true)
22     {
23          parent::__construct($core,$limit_to_blog);
24
25          $protocols = (string) $this->s->kutrl_srv_local_protocols;
26          $this->allowed_protocols = empty($protocols) ? array() : explode(',',$protocols);
27          $this->allow_customized_hash = true;
28
29          $this->url_base = $core->blog->url.$core->url->getBase('kutrl').'/';
30          $this->url_min_length = strlen($this->url_base) + 2;
31     }
32
33     public function saveSettings()
34     {
35          $this->s->put('kutrl_srv_local_protocols',$_POST['kutrl_srv_local_protocols']);
36          $this->s->put('kutrl_srv_local_public',isset($_POST['kutrl_srv_local_public']));
37          $this->s->put('kutrl_srv_local_css',$_POST['kutrl_srv_local_css']);
38     }
39
40     public function settingsForm()
41     {
42          echo
43          '<p>'.
44          __('This service use your own Blog to shorten and serve URL.').'<br />'.
45          sprintf(__('This means that with this service short links start with "%s".'),$this->url_base).
46          '</p>'.
47          '<p>'.
48          __("You can use Dotclear's plugin called myUrlHandlers to change short links prefix on your blog.");
49
50          if (preg_match('/index\.php/',$this->url_base))
51          {
52               echo 
53               '<p>'.
54               __("We recommand that you use a rewrite engine in order to remove 'index.php' from your blog's URL.").
55               '<br /><a href="http://fr.dotclear.org/documentation/2.0/usage/blog-parameters">'.
56               __("You can find more about this on the Dotclear's documentation.").
57               '</a></p>';
58          }
59          echo 
60          '</p>'.
61
62         '<p><label class="classic">'.
63          __('Allowed protocols:').'<br />'.
64         form::field(array('kutrl_srv_local_protocols'),50,255,$this->s->kutrl_srv_local_protocols).
65          '</label></p>'.
66
67         '<p class="form-note">'.
68         __('Use comma seperated list like: "http:,https:,ftp:"').
69         '</p>'.
70
71          '<p><label class="classic">'.
72          form::checkbox(array('kutrl_srv_local_public'),'1',$this->s->kutrl_srv_local_public).' '.
73          __('Enable public page for visitors to shorten links').
74          '</label></p>'.
75
76          '<p class="area" id="style-area"><label for="_style">'.__('CSS:').'</label>'.
77          form::textarea('kutrl_srv_local_css',50,3,html::escapeHTML($this->s->kutrl_srv_local_css),'',2).
78          '</p>'.
79          '<p class="form-note">'.__('You can add here special cascading style sheet. Body of page has class "dc-kutrl" and widgets have class "shortenkutrlwidget" and "rankkutrlwidget".').'</p>';
80     }
81
82     public function testService()
83     {
84          if (!empty($this->allowed_protocols))
85          {
86               return true;
87          }
88          else {
89               $this->error->add(__('Service is not well configured.'));
90               return false;
91          }
92     }
93
94     public function createHash($url,$hash=null)
95     {
96          # Create response object
97          $rs = new ArrayObject();
98          $rs->type = 'local';
99          $rs->url = $url;
100
101          # Normal link
102          if ($hash === null)
103          {
104               $type = 'localnormal';
105               $rs->hash = $this->next($this->last('localnormal'));
106          }
107
108          # Mixed custom link
109          elseif (preg_match('/^([A-Za-z0-9]{2,})!!$/',$hash,$m))
110          {
111               $type = 'localmix';
112               $rs->hash = $m[1].$this->next(-1,$m[1]);
113          }
114
115          # Custom link
116          elseif (preg_match('/^[A-Za-z0-9.-_]{2,}$/',$hash))
117          {
118               if (false !== $this->log->select(null,$hash,null,'local'))
119               {
120                    $this->error->add(__('Custom short link is already taken.'));
121                    return false;
122               }
123               $type = 'localcustom';
124               $rs->hash = $hash;
125          }
126
127          # Wrong char in custom hash
128          else
129          {
130               $this->error->add(__('Custom short link is not valid.'));
131               return false;
132          }
133
134          # Save link
135          try {
136               $this->log->insert($rs->url,$rs->hash,$type,$rs->type);
137               return $rs;
138          }
139          catch (Exception $e)
140          {
141               $this->error->add(__('Failed to save link.'));
142          }
143          return false;
144     }
145
146     protected function last($type)
147     {
148          return 
149          false === ($rs = $this->log->select(null,null,$type,'local')) ?
150          -1 : $rs->hash;
151     }
152
153     protected function next($last_id,$prefix='')
154     {
155          if ($last_id == -1)
156          {
157               $next_id = 0;
158          }
159          else
160          {
161               for($x = 1; $x <= strlen($last_id); $x++)
162               {
163                    $pos = strlen($last_id) - $x;
164
165                    if ($last_id[$pos] != 'z')
166                    {
167                         $next_id = $this->increment($last_id,$pos);
168                         break;
169                    }
170               }
171
172               if (!isset($next_id))
173               {
174                    $next_id = $this->append($last_id);
175               }
176          }
177
178          return 
179          false === $this->log->select(null,$prefix.$next_id,null,'local') ?
180          $next_id : $this->next($next_id,$prefix);
181     }
182
183     protected function append($id)
184     {
185          $id = str_split($id);
186          for ($x = 0; $x < count($id); $x++)
187          {
188               $id[$x] = 0;
189          }
190          return implode($id).'0';
191     }
192
193     protected function increment($id,$pos)
194     {
195          $id = str_split($id);
196          $char = $id[$pos];
197
198          if (is_numeric($char))
199          {
200               $new_char = $char < 9 ? $char + 1 : 'a';
201          }
202          else
203          {
204               $new_char = chr(ord($char) + 1);
205          }
206          $id[$pos] = $new_char;
207         
208          if ($pos != (count($id) - 1))
209          {
210               for ($x = ($pos + 1); $x < count($id); $x++)
211               {
212                    $id[$x] = 0;
213               }
214          }
215
216          return implode($id);
217     }
218
219     public function getUrl($hash)
220     {
221          if (false === ($rs = $this->log->select(null,$hash,null,'local')))
222          {
223               return false;
224          }
225          $this->log->counter($rs->id,'up');
226          return $rs->url;
227     }
228
229     public function deleteUrl($url)
230     {
231          if (false === ($rs = $this->log->select($url,null,null,'local')))
232          {
233               return false;
234          }
235          $this->log->delete($rs->id);
236          return true;
237     }
238}
239?>
Note: See TracBrowser for help on using the repository browser.

Sites map