From d16b47f02a41843121c15150e8b9b06cbd048b1c Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sun, 27 Nov 2022 13:57:51 +0100 Subject: [PATCH] initial commit from alias 1.2.1 --- _admin.php | 90 +++++++++++++++++++++++++++++++ _define.php | 21 ++++++++ _install.php | 44 ++++++++++++++++ _prepend.php | 15 ++++++ _public.php | 51 ++++++++++++++++++ class.dc.aliases.php | 111 +++++++++++++++++++++++++++++++++++++++ icon.png | Bin 0 -> 677 bytes index.php | 91 ++++++++++++++++++++++++++++++++ locales/_pot/main.pot | 73 +++++++++++++++++++++++++ locales/en/help.html | 19 +++++++ locales/fr/help.html | 20 +++++++ locales/fr/main.lang.php | 30 +++++++++++ 12 files changed, 565 insertions(+) create mode 100644 _admin.php create mode 100644 _define.php create mode 100644 _install.php create mode 100644 _prepend.php create mode 100644 _public.php create mode 100644 class.dc.aliases.php create mode 100644 icon.png create mode 100644 index.php create mode 100644 locales/_pot/main.pot create mode 100644 locales/en/help.html create mode 100644 locales/fr/help.html create mode 100644 locales/fr/main.lang.php diff --git a/_admin.php b/_admin.php new file mode 100644 index 0000000..abf1af4 --- /dev/null +++ b/_admin.php @@ -0,0 +1,90 @@ +addBehavior('exportFull',array('aliasBehaviors','exportFull')); +$core->addBehavior('exportSingle',array('aliasBehaviors','exportSingle')); +$core->addBehavior('importInit',array('aliasBehaviors','importInit')); +$core->addBehavior('importFull',array('aliasBehaviors','importFull')); +$core->addBehavior('importSingle',array('aliasBehaviors','importSingle')); + +$_menu['Plugins']->addItem(__('Aliases'),'plugin.php?p=alias','index.php?pf=alias/icon.png', + preg_match('/plugin.php\?p=alias(&.*)?$/',$_SERVER['REQUEST_URI']), + $core->auth->check('admin',$core->blog->id)); + +if (!isset($__resources['help']['alias'])) { + $__resources['help']['alias'] = dirname(__FILE__).'/locales/en/help.html'; + + if (file_exists(dirname(__FILE__).'/locales/'.$_lang.'/help.html')) { + $__resources['help']['alias'] = dirname(__FILE__).'/locales/'.$_lang.'/help.html'; + } +} + +# Behaviors +class aliasBehaviors +{ + public static function exportFull($core,$exp) + { + $exp->exportTable('alias'); + } + + public static function exportSingle($core,$exp,$blog_id) + { + $exp->export('alias', + 'SELECT alias_url, alias_destination, alias_position '. + 'FROM '.$core->prefix.'alias A '. + "WHERE A.blog_id = '".$blog_id."'" + ); + } + + public static function importInit($bk,$core) + { + $bk->cur_alias = $core->con->openCursor($core->prefix.'alias'); + $bk->alias = new dcAliases($core); + $bk->aliases = $bk->alias->getAliases(); + } + + public static function importFull($line,$bk,$core) + { + if ($line->__name == 'alias') + { + $bk->cur_alias->clean(); + + $bk->cur_alias->blog_id = (string) $line->blog_id; + $bk->cur_alias->alias_url = (string) $line->alias_url; + $bk->cur_alias->alias_destination = (string) $line->alias_destination; + $bk->cur_alias->alias_position = (integer) $line->alias_position; + + $bk->cur_alias->insert(); + } + } + + public static function importSingle($line,$bk,$core) + { + if ($line->__name == 'alias') + { + $found = false; + foreach ($bk->aliases as $v) + { + if ($v['alias_url'] == $line->alias_url) { + $found = true; + } + } + if ($found) { + $bk->alias->deleteAlias($line->alias_url); + } + $bk->alias->createAlias($line->alias_url,$line->alias_destination,$line->alias_position); + } + } +} + +?> diff --git a/_define.php b/_define.php new file mode 100644 index 0000000..d6f3f05 --- /dev/null +++ b/_define.php @@ -0,0 +1,21 @@ +registerModule( + /* Name */ "alias", + /* Description*/ "Create aliases of your blog's URLs", + /* Author */ "Olivier Meunier and contributors", + /* Version */ '1.2.1', + /* Permissions */ 'admin' +); +?> diff --git a/_install.php b/_install.php new file mode 100644 index 0000000..b5c6591 --- /dev/null +++ b/_install.php @@ -0,0 +1,44 @@ +plugins->moduleInfo('alias','version'); + +if (version_compare($core->getVersion('alias'),$version,'>=')) { + return; +} + +/* Database schema +-------------------------------------------------------- */ +$s = new dbStruct($core->con,$core->prefix); + +$s->alias + ->blog_id('varchar',32,false) + ->alias_url('varchar',255,false) + ->alias_destination('varchar',255,false) + ->alias_position('smallint',0,false,1) + + ->primary('pk_alias','blog_id','alias_url') + + ->index('idx_alias_blog_id','btree','blog_id') + ->index('idx_alias_blog_id_alias_position','btree','blog_id','alias_position') + + ->reference('fk_alias_blog','blog_id','blog','blog_id','cascade','cascade') + ; + +# Schema installation +$si = new dbStruct($core->con,$core->prefix); +$changes = $si->synchronize($s); + +$core->setVersion('alias',$version); +return true; +?> \ No newline at end of file diff --git a/_prepend.php b/_prepend.php new file mode 100644 index 0000000..7fc495d --- /dev/null +++ b/_prepend.php @@ -0,0 +1,15 @@ + diff --git a/_public.php b/_public.php new file mode 100644 index 0000000..3ddf316 --- /dev/null +++ b/_public.php @@ -0,0 +1,51 @@ +url->register('alias','','^(.*)$',array('urlAlias','alias')); + +class urlAlias extends dcUrlHandlers +{ + public static function alias($args) + { + $o = new dcAliases($GLOBALS['core']); + $aliases = $o->getAliases(); + + foreach ($aliases as $v) + { + if (@preg_match('#^/.*/$#',$v['alias_url']) && @preg_match($v['alias_url'],$args)) { + self::callAliasHandler(preg_replace($v['alias_url'],$v['alias_destination'],$args)); + return; + } elseif ($v['alias_url'] == $args) { + self::callAliasHandler($v['alias_destination']); + return; + } + } + + self::callAliasHandler($args); + } + + public function callAliasHandler($part) + { + global $core; + $core->url->unregister('alias'); + $core->url->getArgs($part,$type,$args); + + global $core; + if (!$type) { + $core->url->callDefaultHandler($args); + } else { + $core->url->callHandler($type,$args); + } + } +} +?> diff --git a/class.dc.aliases.php b/class.dc.aliases.php new file mode 100644 index 0000000..87e0d6f --- /dev/null +++ b/class.dc.aliases.php @@ -0,0 +1,111 @@ +core =& $core; + } + + public function getAliases() + { + if (is_array($this->aliases)) { + return $this->aliases; + } + + $this->aliases = array(); + $sql = 'SELECT alias_url, alias_destination, alias_position '. + 'FROM '.$this->core->prefix.'alias '. + "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ". + 'ORDER BY alias_position ASC '; + $this->aliases = $this->core->con->select($sql)->rows(); + return $this->aliases; + } + + public function updateAliases($aliases) + { + usort($aliases,array($this,'sortCallback')); + foreach ($aliases as $v) { + if (!isset($v['alias_url']) || !isset($v['alias_destination'])) { + throw new Exception(__('Invalid aliases definitions')); + } + } + + $this->core->con->begin(); + try + { + $this->deleteAliases(); + foreach ($aliases as $k => $v) + { + if (!empty($v['alias_url']) && !empty($v['alias_destination'])) + { + $this->createAlias($v['alias_url'],$v['alias_destination'],$k+1); + } + } + + $this->core->con->commit(); + } + catch (Exception $e) + { + $this->core->con->rollback(); + throw $e; + } + } + + public function createAlias($url,$destination,$position) + { + if (!$url) { + throw new Exception(__('Alias URL is empty.')); + } + + if (!$destination) { + throw new Exception(__('Alias destination is empty.')); + } + + $cur = $this->core->con->openCursor($this->core->prefix.'alias'); + $cur->blog_id = (string) $this->core->blog->id; + $cur->alias_url = (string) $url; + $cur->alias_destination = (string) $destination; + $cur->alias_position = abs((integer) $position); + $cur->insert(); + } + + public function deleteAlias($url) + { + $this->core->con->execute( + 'DELETE FROM '.$this->core->prefix.'alias '. + "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ". + "AND alias_url = '".$this->core->con->escape($url)."' " + ); + } + + public function deleteAliases() + { + $this->core->con->execute( + 'DELETE FROM '.$this->core->prefix.'alias '. + "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' " + ); + } + + protected function sortCallback($a,$b) + { + if ($a['alias_position'] == $b['alias_position']) { + return 0; + } + return $a['alias_position'] < $b['alias_position'] ? -1 : 1; + } +} +?> diff --git a/icon.png b/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a66c66b60b8df3959f6328053ea40078ca905965 GIT binary patch literal 677 zcmV;W0$TlvP)ujiQR^Y-H3zSf`!|IgxiCH+ku4If`Qt9e%XG0*nNE1eYC)| zde?e+)_Ql=d3M!ybkuZi)ogFnY;(|abI@_Exvp-~Z*0dbVG7wVRUJ4ZXi@?ZDjyAFEBVSFgO>dycqxh0HR4mK~xx5V_?7ljM>?l86Y|# zJ|-F@z@BDmnhp|(Q&I|J0|}&>m?X0>FtA3+%Ljl3lJxXKy%`wY9Hk{ZK?2Mv*4o;B zkrB=^qH6w3AVV|~J(Ly93?wAXz2qSFaE8QMNQoN;x^lp+=Jyd3vFAZ{OSrJGCo-SM zMNrU*ff3FR3RdUi)3LOJt6=dp;^tPhVMfyAuBBzmfC3z>7;pdpCO#KG-{6pv00000 LNkvXXu0mjfp{OK; literal 0 HcmV?d00001 diff --git a/index.php b/index.php new file mode 100644 index 0000000..4c1000e --- /dev/null +++ b/index.php @@ -0,0 +1,91 @@ +getAliases(); + +# Update aliases +if (isset($_POST['a']) && is_array($_POST['a'])) +{ + try { + $o->updateAliases($_POST['a']); + http::redirect($p_url.'&up=1'); + } catch (Exception $e) { + $core->error->add($e->getMessage()); + } +} + +# New alias +if (isset($_POST['alias_url'])) +{ + try { + $o->createAlias($_POST['alias_url'],$_POST['alias_destination'],count($aliases)+1); + http::redirect($p_url.'&created=1'); + } catch (Exception $e) { + $core->error->add($e->getMessage()); + } +} +?> + + + <?php echo __('Aliases'); ?> + + + +'.html::escapeHTML($core->blog->name).' › '.__('Aliases').''. +'

'.__('Aliases list').'

'; + +if (empty($aliases)) +{ + echo '

'.__('No alias').'

'; +} +else +{ + echo + '
'. + ''. + ''. + ''. + ''. + ''; + + foreach ($aliases as $k => $v) + { + echo + ''. + ''. + ''. + ''. + ''; + } + + echo '
'.__('Alias URL').''.__('Alias destination').''.__('Alias position').'
'.form::field(array('a['.$k.'][alias_url]'),30,255,html::escapeHTML($v['alias_url'])).''.form::field(array('a['.$k.'][alias_destination]'),50,255,html::escapeHTML($v['alias_destination'])).''.form::field(array('a['.$k.'][alias_position]'),3,5,html::escapeHTML($v['alias_position'])).'
'. + '

'.__('To remove an alias, empty its URL or destination.').'

'. + '

'.$core->formNonce(). + '

'. + '
'; +} + +echo +'

'.__('New alias').'

'. +'
'. +'

'. +'

'. +'

'.$core->formNonce().'

'. +'
'; + +dcPage::helpBlock('alias'); +?> + + diff --git a/locales/_pot/main.pot b/locales/_pot/main.pot new file mode 100644 index 0000000..84da57f --- /dev/null +++ b/locales/_pot/main.pot @@ -0,0 +1,73 @@ +# SOME DESCRIPTIVE TITLE. +# This file is put in the public domain. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: Dotclear 2 alias module\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2008-08-07 12:39+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: plugins-test/alias//_admin.php:14 plugins-test/alias//index.php:41 +#: plugins-test/alias//index.php:47 +msgid "Aliases" +msgstr "" + +#: plugins-test/alias//class.dc.aliases.php:43 +msgid "Invalid aliases definitions" +msgstr "" + +#: plugins-test/alias//class.dc.aliases.php:71 +msgid "Alias URL is empty." +msgstr "" + +#: plugins-test/alias//class.dc.aliases.php:75 +msgid "Alias destination is empty." +msgstr "" + +#: plugins-test/alias//index.php:48 +msgid "Aliases list" +msgstr "" + +#: plugins-test/alias//index.php:52 +msgid "No alias" +msgstr "" + +#: plugins-test/alias//index.php:59 +msgid "Alias URL" +msgstr "" + +#: plugins-test/alias//index.php:60 +msgid "Alias destination" +msgstr "" + +#: plugins-test/alias//index.php:61 +msgid "Alias position" +msgstr "" + +#: plugins-test/alias//index.php:75 +msgid "To remove an alias, empty its URL or destination." +msgstr "" + +#: plugins-test/alias//index.php:77 +msgid "Update" +msgstr "" + +#: plugins-test/alias//index.php:82 +msgid "New alias" +msgstr "" + +#: plugins-test/alias//index.php:84 +msgid "Alias URL:" +msgstr "" + +#: plugins-test/alias//index.php:85 +msgid "Alias destination:" +msgstr "" diff --git a/locales/en/help.html b/locales/en/help.html new file mode 100644 index 0000000..4d9d6e7 --- /dev/null +++ b/locales/en/help.html @@ -0,0 +1,19 @@ +

Aliases allow you to map any URLs you create to any available resource of +your blog.

+ +
+
Alias URL
+
Alias URL is the URL you create, without your blog URL. An alias URL + named "aboutme" will be caught when someone call http://yourblog/aboutme + page.
+ +
Alias destination
+
Alias destination is the resource your alias is pointing to. If you + create an alias called "aboutme" and want it to serve your static page + "aboutme", alias destination will be "pages/aboutme".
+ +
Regular expressions
+
You can use regular expression by wrapping alias URL with "/"; + ie. "/^word\/(.+)?$/". You can use captures in destination; ie. + "tag/$1".
+
\ No newline at end of file diff --git a/locales/fr/help.html b/locales/fr/help.html new file mode 100644 index 0000000..053aa8c --- /dev/null +++ b/locales/fr/help.html @@ -0,0 +1,20 @@ +

Les alias vous permettent de définir n'importe quelle URL renvoyant le +contenu de n'importe quelle ressource disponible de votre blog.

+ +
+
URL de l'alias
+
L'URL de l'alias est l'URL que vous créez, sans l'URL du blog. Une URL + d'alias appelée "a-propos" sera reconnu quand quelqu'un appellera la page + http://votreblog/a-propos.
+ +
Destination de l'alias
+
La destination de l'alias est la ressource dont le contenu sera + renvoyé par votre alias. Si vous créez un alias "a-propos" et souhaitez + le faire pointer sur la page statique "a-propos", la destination de + l'alias sera "pages/a-propos".
+ +
Expressions rationnelles
+
Vous pouvez utiliser des expressions rationnelles en encadrant l'URL + de l'alias par "/", par exemple : "/^word\/(.+)?$/". Vous pouvez utiliser + les captures dans la destination, par exemple : "tag/$1".
+
\ No newline at end of file diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php new file mode 100644 index 0000000..79d87dd --- /dev/null +++ b/locales/fr/main.lang.php @@ -0,0 +1,30 @@ + \ No newline at end of file