'local', 'name' => 'kUtRL', 'home' => 'https://github.com/JcDenis/kUtRL', 'allow_custom_hash' => true, ]; protected function init() { $protocols = (string) $this->settings->get('srv_local_protocols'); $this->config['allow_protocols'] = empty($protocols) ? [] : explode(',', $protocols); $this->config['url_base'] = dcCore::app()->blog->url . dcCore::app()->url->getBase('kutrl') . '/'; $this->config['url_min_len'] = strlen($this->url_base) + 2; } public function saveSettings() { $this->settings->put('srv_local_protocols', $_POST['kutrl_srv_local_protocols'], 'string'); $this->settings->put('srv_local_public', isset($_POST['kutrl_srv_local_public']), 'boolean'); $this->settings->put('srv_local_css', $_POST['kutrl_srv_local_css'], 'string'); $this->settings->put('srv_local_404_active', isset($_POST['kutrl_srv_local_404_active']), 'boolean'); } public function settingsForm() { echo '
' . '

' . __('Settings:') . '

' . '

' . '

' . __('Use comma seperated list like: "http:,https:,ftp:"') . '

' . '

' . '

' . form::textarea('kutrl_srv_local_css', 50, 3, html::escapeHTML($this->settings->get('srv_local_css')), '', '2') . '

' . '

' . __('You can add here special cascading style sheet. Body of page has class "dc-kutrl" and widgets have class "shortenkutrlwidget" and "rankkutrlwidget".') . '

' . '

' . '

' . __('If this is not activated, the default 404 page of the theme will be display.') . '

' . '
' . '

' . __('Note:') . '

' . '

' . __('This service use your own Blog to shorten and serve URL.') . '
' . sprintf(__('This means that with this service short links start with "%s".'), $this->url_base) . '

' . '

' . __("You can use Dotclear's plugin called myUrlHandlers to change short links prefix on your blog."); if (preg_match('/index\.php/', $this->url_base)) { echo '

' . __("We recommand that you use a rewrite engine in order to remove 'index.php' from your blog's URL.") . '
' . __("You can find more about this on the Dotclear's documentation.") . '

'; } echo '

' . '

' . __('There are two templates delivered with kUtRL, if you do not use default theme, you may adapt them to yours.') . '
' . __('Files are in plugin directory /default-templates, just copy them into your theme and edit them.') . '

' . '

'; } public function testService() { $ap = $this->allow_protocols; if (!empty($ap)) { return true; } $this->error->add(__('Service is not well configured.')); return false; } public function createHash($url, $hash = null) { # Create response object $rs = new ArrayObject(); $rs->type = 'local'; $rs->url = $url; # Normal link if ($hash === null) { $type = 'localnormal'; $rs->hash = $this->next($this->last('localnormal')); # Mixed custom link } elseif (preg_match('/^([A-Za-z0-9]{2,})\!\!$/', $hash, $m)) { $type = 'localmix'; $rs->hash = $m[1] . $this->next(-1, $m[1]); # Custom link } elseif (preg_match('/^[A-Za-z0-9\.\-\_]{2,}$/', $hash)) { if (false !== $this->log->select(null, $hash, null, 'local')) { $this->error->add(__('Custom short link is already taken.')); return false; } $type = 'localcustom'; $rs->hash = $hash; # Wrong char in custom hash } else { $this->error->add(__('Custom short link is not valid.')); return false; } # Save link try { $this->log->insert($rs->url, $rs->hash, $type, $rs->type); return $rs; } catch (Exception $e) { $this->error->add(__('Failed to save link.')); } return false; } protected function last($type) { return false === ($rs = $this->log->select(null, null, $type, 'local')) ? -1 : $rs->hash; } protected function next($last_id, $prefix = '') { if ($last_id == -1) { $next_id = 0; } else { for ($x = 1; $x <= strlen($last_id); $x++) { $pos = strlen($last_id) - $x; if ($last_id[$pos] != 'z') { $next_id = $this->increment($last_id, $pos); break; } } if (!isset($next_id)) { $next_id = $this->append($last_id); } } return false === $this->log->select(null, $prefix . $next_id, null, 'local') ? $next_id : $this->next($next_id, $prefix); } protected function append($id) { $id = str_split($id); for ($x = 0; $x < count($id); $x++) { $id[$x] = 0; } return implode($id) . '0'; } protected function increment($id, $pos) { $id = str_split($id); $char = $id[$pos]; if (is_numeric($char)) { $new_char = $char < 9 ? $char + 1 : 'a'; } else { $new_char = chr(ord($char) + 1); } $id[$pos] = $new_char; if ($pos != (count($id) - 1)) { for ($x = ($pos + 1); $x < count($id); $x++) { $id[$x] = 0; } } return implode($id); } public function getUrl($hash) { if (false === ($rs = $this->log->select(null, $hash, null, 'local'))) { return false; } if (!$rs->url) { //previously removed url return false; } $this->log->counter($rs->id, 'up'); return $rs->url; } public function deleteUrl($url, $delete = false) { if (false === ($rs = $this->log->select($url, null, null, 'local'))) { return false; } if ($delete) { $this->log->delete($rs->id); } else { $this->log->clear($rs->id, ''); } return true; } }