Dotclear

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

Revision 1949, 5.0 KB checked in by JcDenis, 14 years ago (diff)

kUtRL 0.1:

  • First lab release
Line 
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
13if (!defined('DC_RC_PATH')){return;}
14
15class localKutrlService extends kutrlServices
16{
17     public $id = 'local';
18     public $name = 'My blog';
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) $core->blog->settings->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->setNameSpace('kUtRL');
36          $this->s->put('kutrl_srv_local_protocols',$_POST['kutrl_srv_local_protocols']);
37          $this->s->put('kutrl_srv_local_public',isset($_POST['kutrl_srv_local_public']));
38          $this->s->put('kutrl_srv_local_css',$_POST['kutrl_srv_local_css']);
39          $this->s->setNameSpace('system');
40     }
41
42     public function settingsForm()
43     {
44          echo
45          '<p>'.
46          __('This service use your own Blog to shorten and serve URL.').'<br />'.
47          sprintf(__('This means that with this service short links start with "%s".'),$this->url_base).
48          '</p>'.
49          '<p>'.
50          __("You can use Dotclear's plugin called myUrlHandlers to change short links prefix on your blog.");
51
52          if (preg_match('/index\.php/',$this->url_base))
53          {
54               echo 
55               '<p>'.
56               __("We recommand that you use a rewrite engine in order to remove 'index.php' from your blog's URL.").
57               '<br /><a href="http://fr.dotclear.org/documentation/2.0/usage/blog-parameters">'.
58               __("You can find more about this on the Dotclear's documentation.").
59               '</a></p>';
60          }
61          echo 
62          '</p>'.
63
64         '<p><label class="classic">'.
65          __('Allowed protocols:').'<br />'.
66         form::field(array('kutrl_srv_local_protocols'),50,255,$this->s->kutrl_srv_local_protocols).
67          '</label></p>'.
68
69         '<p class="form-note">'.
70         __('Use comma seperated list like: "http:,https:,ftp:"').
71         '</p>'.
72
73          '<p><label class="classic">'.
74          form::checkbox(array('kutrl_srv_local_public'),'1',$this->s->kutrl_srv_local_public).' '.
75          __('Enable public page for visitors to shorten links').
76          '</label></p>'.
77
78          '<p class="area" id="style-area"><label for="_style">'.__('CSS:').'</label>'.
79          form::textarea('kutrl_srv_local_css',50,3,html::escapeHTML($this->s->kutrl_srv_local_css),'',2).
80          '</p>'.
81          '<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>';
82     }
83
84     public function testService()
85     {
86          return !empty($this->allowed_protocols);
87     }
88
89     public function createHash($url,$hash=null)
90     {
91          $rs = new ArrayObject();
92          $rs->type = 'local';
93          $rs->url = $url;
94
95          if ($hash === null)
96          {
97               $type = 'localnormal';
98               $rs->hash = $this->next($this->last());
99          }
100          else
101          {
102               $type = 'localcustom';
103               $rs->hash = $hash;
104          }
105
106          $this->log->insert($rs->url,$rs->hash,$type,$rs->type);
107          return $rs;
108     }
109
110     protected function last()
111     {
112          return 
113          false === ($rs = $this->log->select(null,null,'localnormal','local')) ?
114          -1 : $rs->hash;
115     }
116
117     protected function next($last_id)
118     {
119          if ($last_id == -1)
120          {
121               $next_id = 0;
122          }
123          else
124          {
125               for($x = 1; $x <= strlen($last_id); $x++)
126               {
127                    $pos = strlen($last_id) - $x;
128
129                    if ($last_id[$pos] != 'z')
130                    {
131                         $next_id = $this->increment($last_id,$pos);
132                         break;
133                    }
134               }
135
136               if (!isset($next_id))
137               {
138                    $next_id = $this->append($last_id);
139               }
140          }
141
142          return 
143          false === ($rs = $this->log->select(null,$next_id,'localnormal','local')) ?
144          $next_id : $this->next($next_id);
145     }
146
147     protected function append($id)
148     {
149          for ($x = 0; $x < strlen($id); $x++)
150          {
151               $id[$x] = 0;
152          }
153          $id .= 0;
154
155          return $id;
156     }
157
158     protected function increment($id,$pos)
159     {
160          $char = $id[$pos];
161
162          if (is_numeric($char))
163          {
164               $new_char = $char < 9 ? $char + 1 : 'a';
165          }
166          else
167          {
168               $new_char = chr(ord($char) + 1);
169          }
170          $id[$pos] = $new_char;
171         
172          if ($pos != (strlen($id) - 1))
173          {
174               for ($x = ($pos + 1); $x < strlen($id); $x++)
175               {
176                    $id[$x] = 0;
177               }
178          }
179
180          return $id;
181     }
182
183     public function getUrl($hash)
184     {
185          if (false === ($rs = $this->log->select(null,$hash,null,'local')))
186          {
187               return false;
188          }
189          $this->log->counter($rs->id,'up');
190          return $rs->url;
191     }
192
193     public function deleteUrl($url)
194     {
195          if (false === ($rs = $this->log->select($url,null,null,'local')))
196          {
197               return false;
198          }
199          $this->log->delete($rs->id);
200          return true;
201     }
202}
203?>
Note: See TracBrowser for help on using the repository browser.

Sites map