2021-08-25 23:06:22 +00:00
< ? php
2021-09-02 12:32:26 +00:00
/**
* @ brief kUtRL , a plugin for Dotclear 2
2021-11-06 15:43:02 +00:00
*
2021-09-02 12:32:26 +00:00
* @ package Dotclear
* @ subpackage Plugin
2021-11-06 15:43:02 +00:00
*
2021-09-02 12:32:26 +00:00
* @ author Jean - Christian Denis and contributors
2021-11-06 15:43:02 +00:00
*
2021-09-02 12:32:26 +00:00
* @ copyright Jean - Christian Denis
* @ copyright GPL - 2.0 https :// www . gnu . org / licenses / gpl - 2.0 . html
*/
2021-08-27 23:52:03 +00:00
if ( ! defined ( 'DC_RC_PATH' )) {
return null ;
}
2021-08-25 23:06:22 +00:00
class localKutrlService extends kutrlService
{
2021-08-27 23:52:03 +00:00
protected $config = [
2022-12-22 14:37:09 +00:00
'id' => 'local' ,
'name' => 'kUtRL' ,
'home' => 'https://github.com/JcDenis/kUtRL' ,
2021-09-02 12:32:26 +00:00
2022-11-20 16:15:36 +00:00
'allow_custom_hash' => true ,
2021-08-27 23:52:03 +00:00
];
2021-09-02 12:32:26 +00:00
2021-08-27 23:52:03 +00:00
protected function init ()
{
2022-12-22 14:37:09 +00:00
$protocols = ( string ) $this -> settings -> get ( 'kutrl_srv_local_protocols' );
2021-08-27 23:52:03 +00:00
$this -> config [ 'allow_protocols' ] = empty ( $protocols ) ? [] : explode ( ',' , $protocols );
2022-11-20 16:15:36 +00:00
$this -> config [ 'url_base' ] = dcCore :: app () -> blog -> url . dcCore :: app () -> url -> getBase ( 'kutrl' ) . '/' ;
2021-08-27 23:52:03 +00:00
$this -> config [ 'url_min_len' ] = strlen ( $this -> url_base ) + 2 ;
}
2021-09-02 12:32:26 +00:00
2021-08-27 23:52:03 +00:00
public function saveSettings ()
{
$this -> settings -> put ( 'kutrl_srv_local_protocols' , $_POST [ 'kutrl_srv_local_protocols' ], 'string' );
$this -> settings -> put ( 'kutrl_srv_local_public' , isset ( $_POST [ 'kutrl_srv_local_public' ]), 'boolean' );
$this -> settings -> put ( 'kutrl_srv_local_css' , $_POST [ 'kutrl_srv_local_css' ], 'string' );
$this -> settings -> put ( 'kutrl_srv_local_404_active' , isset ( $_POST [ 'kutrl_srv_local_404_active' ]), 'boolean' );
}
public function settingsForm ()
{
echo
2021-11-06 15:43:02 +00:00
'<div class="two-cols"><div class="col">' .
'<p><strong>' . __ ( 'Settings:' ) . '</strong></p>' .
'<p><label class="classic">' .
__ ( 'Allowed protocols:' ) . '<br />' .
2022-12-22 14:37:09 +00:00
form :: field ([ 'kutrl_srv_local_protocols' ], 50 , 255 , $this -> settings -> get ( 'kutrl_srv_local_protocols' )) .
2021-11-06 15:43:02 +00:00
'</label></p>' .
'<p class="form-note">' .
__ ( 'Use comma seperated list like: "http:,https:,ftp:"' ) .
'</p>' .
'<p><label class="classic">' .
2022-12-22 14:37:09 +00:00
form :: checkbox ([ 'kutrl_srv_local_public' ], '1' , $this -> settings -> get ( 'kutrl_srv_local_public' )) . ' ' .
2021-11-06 15:43:02 +00:00
__ ( 'Enable public page for visitors to shorten links' ) .
'</label></p>' .
'<p class="area" id="style-area"><label for="_style">' . __ ( 'CSS:' ) . '</label>' .
2022-12-22 14:37:09 +00:00
form :: textarea ( 'kutrl_srv_local_css' , 50 , 3 , html :: escapeHTML ( $this -> settings -> get ( 'kutrl_srv_local_css' )), '' , '2' ) .
2021-11-06 15:43:02 +00:00
'</p>' .
'<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>' .
'<p><label class="classic">' .
2022-12-22 14:37:09 +00:00
form :: checkbox ([ 'kutrl_srv_local_404_active' ], '1' , $this -> settings -> get ( 'kutrl_srv_local_404_active' )) . ' ' .
2021-11-06 15:43:02 +00:00
__ ( 'Enable special 404 error public page for unknow urls' ) .
'</label></p>' .
'<p class="form-note">' . __ ( 'If this is not activated, the default 404 page of the theme will be display.' ) . '</p>' .
'</div><div class="col">' .
'<p><strong>' . __ ( 'Note:' ) . '</strong></p>' .
'<p>' .
__ ( 'This service use your own Blog to shorten and serve URL.' ) . '<br />' .
sprintf ( __ ( 'This means that with this service short links start with "%s".' ), $this -> url_base ) .
'</p>' .
'<p>' .
2021-08-27 23:52:03 +00:00
__ ( " You can use Dotclear's plugin called myUrlHandlers to change short links prefix on your blog. " );
if ( preg_match ( '/index\.php/' , $this -> url_base )) {
2021-11-06 15:43:02 +00:00
echo
'<p>' .
__ ( " We recommand that you use a rewrite engine in order to remove 'index.php' from your blog's URL. " ) .
'<br /><a href="http://fr.dotclear.org/documentation/2.0/usage/blog-parameters">' .
__ ( " You can find more about this on the Dotclear's documentation. " ) .
2021-08-27 23:52:03 +00:00
'</a></p>' ;
}
2021-11-06 15:43:02 +00:00
echo
'</p>' .
'<p>' . __ ( 'There are two templates delivered with kUtRL, if you do not use default theme, you may adapt them to yours.' ) . '<br />' .
__ ( 'Files are in plugin directory /default-templates, just copy them into your theme and edit them.' ) . '</p>' .
2021-09-02 12:32:26 +00:00
2021-08-28 16:27:58 +00:00
'</div></div><br class="clear"/>' ;
2021-08-27 23:52:03 +00:00
}
public function testService ()
{
$ap = $this -> allow_protocols ;
if ( ! empty ( $ap )) {
return true ;
}
2021-11-06 15:43:02 +00:00
$this -> error -> add ( __ ( 'Service is not well configured.' ));
return false ;
2021-08-27 23:52:03 +00:00
}
public function createHash ( $url , $hash = null )
{
# Create response object
2021-11-06 15:43:02 +00:00
$rs = new ArrayObject ();
2021-08-27 23:52:03 +00:00
$rs -> type = 'local' ;
2021-10-26 21:03:38 +00:00
$rs -> url = $url ;
2021-08-27 23:52:03 +00:00
# Normal link
if ( $hash === null ) {
2021-11-06 15:43:02 +00:00
$type = 'localnormal' ;
2021-08-27 23:52:03 +00:00
$rs -> hash = $this -> next ( $this -> last ( 'localnormal' ));
# Mixed custom link
} elseif ( preg_match ( '/^([A-Za-z0-9]{2,})\!\!$/' , $hash , $m )) {
2021-11-06 15:43:02 +00:00
$type = 'localmix' ;
2021-08-27 23:52:03 +00:00
$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.' ));
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return false ;
}
2021-11-06 15:43:02 +00:00
$type = 'localcustom' ;
2021-08-27 23:52:03 +00:00
$rs -> hash = $hash ;
# Wrong char in custom hash
} else {
$this -> error -> add ( __ ( 'Custom short link is not valid.' ));
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return false ;
}
# Save link
try {
$this -> log -> insert ( $rs -> url , $rs -> hash , $type , $rs -> type );
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return $rs ;
} catch ( Exception $e ) {
$this -> error -> add ( __ ( 'Failed to save link.' ));
}
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return false ;
}
protected function last ( $type )
{
2021-10-26 21:03:38 +00:00
return false === ( $rs = $this -> log -> select ( null , null , $type , 'local' )) ?
2021-08-27 23:52:03 +00:00
- 1 : $rs -> hash ;
}
protected function next ( $last_id , $prefix = '' )
{
if ( $last_id == - 1 ) {
$next_id = 0 ;
} else {
2021-11-06 15:43:02 +00:00
for ( $x = 1 ; $x <= strlen ( $last_id ); $x ++ ) {
2021-08-27 23:52:03 +00:00
$pos = strlen ( $last_id ) - $x ;
if ( $last_id [ $pos ] != 'z' ) {
$next_id = $this -> increment ( $last_id , $pos );
2021-11-06 15:43:02 +00:00
2021-08-27 23:52:03 +00:00
break ;
}
}
if ( ! isset ( $next_id )) {
$next_id = $this -> append ( $last_id );
}
}
2021-10-26 21:03:38 +00:00
2021-11-06 15:43:02 +00:00
return false === $this -> log -> select ( null , $prefix . $next_id , null , 'local' ) ?
2021-10-26 21:03:38 +00:00
$next_id : $this -> next ( $next_id , $prefix );
2021-08-27 23:52:03 +00:00
}
2021-09-02 12:32:26 +00:00
2021-08-27 23:52:03 +00:00
protected function append ( $id )
{
$id = str_split ( $id );
for ( $x = 0 ; $x < count ( $id ); $x ++ ) {
$id [ $x ] = 0 ;
}
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return implode ( $id ) . '0' ;
}
protected function increment ( $id , $pos )
{
2021-11-06 15:43:02 +00:00
$id = str_split ( $id );
2021-08-27 23:52:03 +00:00
$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 ;
}
}
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return implode ( $id );
}
2021-09-02 12:32:26 +00:00
2021-08-27 23:52:03 +00:00
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' );
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
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 , '' );
}
2021-10-26 21:03:38 +00:00
2021-08-27 23:52:03 +00:00
return true ;
}
2021-11-06 15:43:02 +00:00
}