diff options
| author | lsces <lester@lsces.co.uk> | 2025-08-27 15:33:41 +0100 |
|---|---|---|
| committer | lsces <lester@lsces.co.uk> | 2025-08-27 15:33:41 +0100 |
| commit | ce99c524f21f62f146999702406716622e7cec3e (patch) | |
| tree | 576f35b960311c8a72575cb0267ea6e024d376bc | |
| parent | f07304f760323c088712e8efd82547c692cc421b (diff) | |
| download | themes-ce99c524f21f62f146999702406716622e7cec3e.tar.gz themes-ce99c524f21f62f146999702406716622e7cec3e.tar.bz2 themes-ce99c524f21f62f146999702406716622e7cec3e.zip | |
Add Smarty5 style block functions
| -rwxr-xr-x | smartyplugins/AddLinkTicket.php | 42 | ||||
| -rwxr-xr-x | smartyplugins/BlockBitModule.php | 63 | ||||
| -rwxr-xr-x | smartyplugins/BlockBox.php | 60 | ||||
| -rwxr-xr-x | smartyplugins/BlockForm.php | 122 | ||||
| -rwxr-xr-x | smartyplugins/BlockFormInput.php | 67 | ||||
| -rwxr-xr-x | smartyplugins/BlockJstab.php | 57 | ||||
| -rwxr-xr-x | smartyplugins/BlockJstabs.php | 62 | ||||
| -rwxr-xr-x | smartyplugins/BlockLegend.php | 42 | ||||
| -rwxr-xr-x | smartyplugins/BlockNavbar.php | 41 | ||||
| -rwxr-xr-x | smartyplugins/BlockRepeat.php | 49 | ||||
| -rwxr-xr-x | smartyplugins/BlockSortLinks.php | 47 | ||||
| -rwxr-xr-x | smartyplugins/BlockTr.php | 34 |
12 files changed, 686 insertions, 0 deletions
diff --git a/smartyplugins/AddLinkTicket.php b/smartyplugins/AddLinkTicket.php new file mode 100755 index 0000000..8a9a4d2 --- /dev/null +++ b/smartyplugins/AddLinkTicket.php @@ -0,0 +1,42 @@ +<?php +/** + * smarty_prefilter_add_link_ticket This will insert a ticket on all template URL's that have GET parameters. + * + * @param string $pTplSource source of template + * @return string ammended template source + */ + + namespace Bitweaver\Plugins; + use Smarty\Compile\Modifier\ModifierCompilerInterface; + use Smarty\Compiler\Template; + +class AddLinkTicket implements ModifierCompilerInterface { + public function __construct() { + // Initialization code + } + + public function compile($params, Template $template) { + global $gBitUser; + + $source = $params[0]; + + if( is_object( $gBitUser ) && $gBitUser->isRegistered() ) { + // $from = '#href="(.*PKG_URL.*php)\?(.*)&(.*)"#i'; + // $to = 'href="\\1?\\2&tk={$gBitUser->mTicket}&\\3"'; + // $pTplSource = preg_replace( $from, $to, $pTplSource ); + $from = '#<form([^>]*)>#i'; + // div tag is for stupid XHTML compliance. + $to = '<form\\1><div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>'; + $pTplSource = preg_replace( $from, $to, $source ); + if( strpos( $pTplSource[0], '{form}' )) { + $source = str_replace( '{form}', '{form}<div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>', $pTplSource ); + } elseif( strpos( $pTplSource[0], '{form ' ) ) { + $from = '#\{form(\}| [^\}]*)\}#i'; + $to = '{form\\1}<div style="display:inline"><input type="hidden" name="tk" value="{$gBitUser->mTicket}" /></div>'; + $source = preg_replace( $from, $to, $pTplSource ); + } + } + + return $source; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockBitModule.php b/smartyplugins/BlockBitModule.php new file mode 100755 index 0000000..0771ee3 --- /dev/null +++ b/smartyplugins/BlockBitModule.php @@ -0,0 +1,63 @@ +<?php +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ +// $Header$ +/** + * \brief Smarty {bitmodule}{/bitmodule} block handler + * + * To make a module it is enough to place something like following + * into corresponding mod-name.tpl file: + * \code + * {bitmodule name="module_name" title="Module title"} + * <!-- module Smarty/HTML code here --> + * {/bitmodule} + * \endcode + * + * This block may (can) use 2 Smarty templates: + * 1) module.tpl = usual template to generate module look-n-feel + * 2) module-error.tpl = to generate diagnostic error message about + * incorrect {bitmodule} parameters + +\Note +error was used only in case the name was not there. +I fixed that error case. -- mose + + */ + +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; +use Smarty\Exception; + +class BlockBitModule implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + if (is_null($content)) { + return ''; + } + global $gBitSmarty; + $moduleTag = !empty( $params['tag'] ) ? $params['tag'] : 'div'; + $gBitSmarty->assign( 'moduleTag', $moduleTag ); + if( empty( $content )) { + return ''; + } else { + $params['data'] = $content; + } + + if( !empty( $params['name'] ) ) { + $params['name'] = preg_replace( "/[^a-zA-Z0-9\\-\\_]/", "", $params['name'] ); + } + $gBitSmarty->assign( 'modInfo', $params ); + + $temp = $gBitSmarty->fetch('bitpackage:themes/module.tpl'); + return $temp; + + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockBox.php b/smartyplugins/BlockBox.php new file mode 100755 index 0000000..e826237 --- /dev/null +++ b/smartyplugins/BlockBox.php @@ -0,0 +1,60 @@ +<?php +namespace Bitweaver\Plugins; +use Bitweaver\KernelTools; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {box} block plugin + * + * Type: block + * Name: box + * Input: + * - title (optional) box title + * - class (optional) overrides the default class 'box' + * - biticon values (optional) see function.biticon.php for details + * - idiv (optional) name of class of div that surrounds icon (if not set, no div is created) + * @uses smarty_function_escape_special_chars() + * @todo somehow make the variable that is contained within $iselect global --> this will allow importing of outside variables not set in $_REQUEST + */ +class BlockBox implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + global $gBitSmarty; + + if( empty( $content )) { + return ''; + } + $atts = ''; + foreach( $params as $key => $val ) { + switch( $key ) { + case 'title': + $gBitSmarty->assign( $key, KernelTools::tra( $val ) ); + break; + case 'class': + case 'iclass': + case 'ipackage': + case 'iname': + case 'iexplain': + case 'idiv': + $gBitSmarty->assign( $key,$val ); + break; + default: + $atts .= $key.'="'.$val.'" '; + break; + } + } + $gBitSmarty->assign( 'content',$content ); + $gBitSmarty->assign( 'atts',$atts ); + return $gBitSmarty->fetch( 'bitpackage:kernel/box.tpl' ); + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockForm.php b/smartyplugins/BlockForm.php new file mode 100755 index 0000000..e79f4d7 --- /dev/null +++ b/smartyplugins/BlockForm.php @@ -0,0 +1,122 @@ +<?php +namespace Bitweaver\Plugins; +use Bitweaver\KernelTools; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {form} block plugin + * + * Type: block + * Name: form + * Input: + * - ipackage (optional) - package where we should direct the form after submission + * - ifile (optional) - file that is targetted + * - ianchor (optional) - move to anchor after submitting + * if neither are set, SCRIPT_NAME is used as url + * - legend if set, it will generate a fieldset using the input as legend + * @uses smarty_function_escape_special_chars() + * @todo somehow make the variable that is contained within $iselect global --> this will allow importing of outside variables not set in $_REQUEST + */ + + +class BlockForm implements BlockHandlerInterface { + + + public function handle( $pParams, $pContent, Template $template, &$repeat): string { + global $gBitSystem, $gSniffer; + + if( !empty($pContent) ) { + if ( $template ) { + if( !isset( $pParams['method'] ) ) { + $pParams['method'] = 'post'; + } + $atts = ''; + $url = $gBitSystem->isLive() && isset( $pParams['secure'] ) && $pParams['secure'] + // This is NEEDED to enforce HTTPS secure logins! + ? 'https://' . $_SERVER['HTTP_HOST'] : ''; + $onsubmit = ''; + + // services can add something to onsubmit + if( $template->getTemplateVars( 'serviceOnsubmit' ) ) { + $onsubmit .= $template->getTemplateVars( 'serviceOnsubmit' ).";"; + } + + foreach( $pParams as $key => $val ) { + switch( $key ) { + case 'ifile': + case 'ipackage': + if( $key == 'ipackage' ) { + $url = match ( $val ) { + 'root' => BIT_ROOT_URL . $pParams['ifile'], + default => constant( strtoupper( $val ) . '_PKG_URL' ) . $pParams['ifile'], + }; + } + break; + case 'legend': + if( !empty( $val ) ) { + $legend = '<legend>'.KernelTools::tra( $val ).'</legend>'; + } + break; + // this is needed for backwards compatibility since we sometimes pass in a url + case 'action': + if ( !empty( $val ) ) { + if( substr( $val, 0, 4 ) == 'http' ) { + if( isset( $pParams['secure'] ) && $pParams['secure'] && ( substr( $val, 0, 5 ) != 'https' )) { + $val = preg_replace( '/^http/', 'https', $val ); + } + $url = $val; + } else { + $url .= $val; + } + } + break; + case 'ianchor': + case 'secure': + break; + case 'onsubmit': + if( !empty( $val ) ) { + $onsubmit .= "$val;"; + } + break; + default: + if( !empty( $val ) ) { + $atts .= "$key=\"$val\" "; + } + break; + } + } + + if( empty( $url )) { + $url = $_SERVER['SCRIPT_NAME']; + } else if( $url == 'https://' . $_SERVER['HTTP_HOST'] ) { + $url .= $_SERVER['SCRIPT_NAME']; + } + + $onsub = !empty( $onsubmit ) ? " onsubmit=\"$onsubmit\"" : ''; + $ret = '<form action="'.$url.( !empty( $pParams['ianchor'] ) ? '#'.$pParams['ianchor'] : '' ).'" '.$atts.$onsub.'>'; + $ret .= isset( $legend ) ? "<fieldset>$legend" : ''; // adding the div makes it easier to be xhtml compliant + $ret .= $pContent; + $ret .= isset( $legend ) ? '</fieldset>' : ''; // close the open tags + $ret .= '</form>'; + return $ret; + } else { + global $gSmartyFormHorizontal; + // global var other plugin functions will pick up to add proper col-XX-YY styling for horizontal forms + $gSmartyFormHorizontal = !empty( $pParams['class'] ) && strpos( $pParams['class'], 'form-horizontal' ) !== false; + return ''; + } + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockFormInput.php b/smartyplugins/BlockFormInput.php new file mode 100755 index 0000000..a7889a3 --- /dev/null +++ b/smartyplugins/BlockFormInput.php @@ -0,0 +1,67 @@ +<?php +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {forminput} block plugin + * + * Type: block + * Name: forminput + */ +class BlockFormInput implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + global $gSmartyFormHorizontal; + + // defaults + $attr = ""; + $class = ''; + if( $gSmartyFormHorizontal ) { + $class = 'col-sm-8'; + } + + if( !empty( $params['class'] ) ){ + $class .= ' '.trim( $params['class'] ); + if( $gSmartyFormHorizontal && (strpos( $params['class'], 'submit' ) !== FALSE || strpos( $params['class'], 'offset' ) !== FALSE) ) { + $class .= ' col-sm-offset-4'; + } + } + + $labelStart = ''; + $labelEnd = ''; + + if( !empty( $params['label'] ) ){ + if( $gSmartyFormHorizontal ) { + $class .= ' col-sm-offset-4'; + } + $class .= ' '.trim( $params['label'] ); + $labelStart = '<label>'; + $labelEnd = '</label>'; + } + + if( !empty( $params['id'] ) ){ + $attr .= 'id="'.trim( $params['id'] ).'" '; + } + + if( !empty( $params['style'] ) ){ + $attr .= 'style="'.trim( $params['style'] ).'" '; + } + + if( $content ) { + return '<div class="'.$class.'" '.$attr.' >'.$labelStart.$content.$labelEnd.'</div>'; + } else { + return ''; + } + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockJstab.php b/smartyplugins/BlockJstab.php new file mode 100755 index 0000000..db28a54 --- /dev/null +++ b/smartyplugins/BlockJstab.php @@ -0,0 +1,57 @@ +<?php +namespace Bitweaver\Plugins; +use Bitweaver\BitBase; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; +use Bitweaver\KernelTools; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {jstab} block plugin + * + * Type: block + * Name: jstab + * Input: + * Abstract: Used to enclose a set of tabs + */ + + class BlockJstab implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + if( empty( $repeat ) ){ + global $jsTabLinks; + + $tClass = isset( $params['class'] ) ? ' class="'.$params['class'].'"' : ''; + $tStyle = isset( $params['style'] ) ? ' style="'.$params['style'].'"' : ''; + $tClick = isset( $params['onclick'] ) ? ' onclick="'.$params['onclick'].'"' : ''; + $tTitle = KernelTools::tra( isset( $params['title'] ) ? $params['title'] : 'No Title' ); + + $tabId = strtolower( isset( $params['id'] ) ? $params['id'] : 'tab'.preg_replace("/[^A-Za-z0-9]/", '', $tTitle) ); + + $tabString = '<li '.$tClick.' '.$tClass.' '.$tStyle.'><a href="#'.$tabId.'">' . $tTitle . '</a></li>'; + if( isset( $params['position'] ) ) { + array_splice( $jsTabLinks, $params['position'], 0, $tabString ); + } else { + $jsTabLinks[] = $tabString; + } + + $tabType = BitBase::getParameter( $params, 'tabtype', 'tab' ); + + $ret = '<div class="'.$tabType.'-pane" id="'.$tabId.'">'; + $ret .= $content; + $ret .= '</div>'; + + return $ret; + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockJstabs.php b/smartyplugins/BlockJstabs.php new file mode 100755 index 0000000..e9234aa --- /dev/null +++ b/smartyplugins/BlockJstabs.php @@ -0,0 +1,62 @@ +<?php +namespace Bitweaver\Plugins; +use Bitweaver\BitBase; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {jstabs} block plugin + * + * Type: block + * Name: jstabs + * Input: you can use {jstab tab=<tab number>} (staring with 0) to select a given tab + * or you can use the url to do so: page.php?jstab=<tab number> + * Abstract: Used to enclose a set of tabs + */ +class BlockJstabs implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + global $gBitSystem, $jsTabLinks; + if( $repeat ){ + $jsTabLinks = []; + } else { + extract( $params ); + + $tabId = !empty( $params['id'] ) ? $params['id'] : substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10); + + if( isset( $_REQUEST['jstab'] ) ) { + // make sure we aren't passed any evil shit + if( !isset( $tab ) && isset( $_REQUEST['jstab'] ) && preg_match( "!^\d+$!", $_REQUEST['jstab'] ) ) { + $tab = $_REQUEST['jstab']; + } + $setupJs = '$(\'#'.$tabId.' a[href="#profile"]\').tab(\'show\');'; + } else { + $setupJs = "$('#$tabId a:first').tab('show');"; + } + + $tabType = BitBase::getParameter( $params, 'tabtype', 'tab' ); + + $ret = '<ul class="nav nav-'.$tabType.'s" data-tab="'.$tabType.'" id="'.$tabId.'">'; + foreach( $jsTabLinks as $tabLink ) { + $ret .= $tabLink; + } + $ret .= '</ul><div class="tab-content">'.$content.'</div>'; + $ret .= '<script nonce="{$cspNonce}">/*<![CDATA[*/ $(\'#'.$tabId.' a\').click(function (e) { e.preventDefault(); $(this).tab(\'show\'); }); '.$setupJs .'/*]]>*/</script> '; + + $jsTabLinks = NULL; + + return $ret; + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +} diff --git a/smartyplugins/BlockLegend.php b/smartyplugins/BlockLegend.php new file mode 100755 index 0000000..d310b5e --- /dev/null +++ b/smartyplugins/BlockLegend.php @@ -0,0 +1,42 @@ +<?php +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; +use Bitweaver\KernelTools; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {form} block plugin + * + * Type: block + * Name: form + * Input: + * - legend (optional) - text that appears in the legend + */ +class BlockLegend implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + if( $content ) { + $attributes = ''; + $attributes .= !empty( $params['class'] ) ? ' class="'.$params['class'].'" ' : '' ; + $attributes .= !empty( $params['id'] ) ? ' id="'.$params['id'].'" ' : '' ; + $ret = '<fieldset '.$attributes.'>'; + if( !empty( $params['legend'] ) ) { + $ret .= '<legend>'.KernelTools::tra( $params['legend'] ).'</legend>'; + } + $ret .= $content; + $ret .= '</fieldset>'; + return $ret; + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockNavbar.php b/smartyplugins/BlockNavbar.php new file mode 100755 index 0000000..5fe2e4b --- /dev/null +++ b/smartyplugins/BlockNavbar.php @@ -0,0 +1,41 @@ +<?php +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty {navbar} block plugin + * + * Type: block + * Name: navbar + * Input: set of links that are used for navigation purposes + */ +class BlockNavbar implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + global $gBitSmarty; + + $links = $this->smarty_block_navbar_get_links( $content ); + $gBitSmarty->assign( 'links',$links ); + return $gBitSmarty->fetch( 'bitpackage:kernel/navbar.tpl' ); + } + + public function smarty_block_navbar_get_links( $content ) { + $links = []; + if( preg_match_all( "/<a.*?href=\".*?\">.*?<\/a>/i",$content,$res ) ) { + $res = $res[0]; + $links = array_unique( $res ); + } + return $links; + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockRepeat.php b/smartyplugins/BlockRepeat.php new file mode 100755 index 0000000..d318c21 --- /dev/null +++ b/smartyplugins/BlockRepeat.php @@ -0,0 +1,49 @@ +<?php +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; +use Smarty\Exception; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty plugin + * ------------------------------------------------------------- + * File: block.repeat.php + * Type: block + * Name: repeat + * Purpose: repeat a template block a given number of times + * Parameters: count [required] - number of times to repeat + * assign [optional] - variable to collect output + * Author: Scott Matthewman <scott@matthewman.net> + * ------------------------------------------------------------- + */ +class BlockRepeat implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + global $gBitSmarty; + if( !empty( $content ) ) { + $intCount = intval( $params['count'] ); + if( $intCount < 0 ) { + throw new Exception( + "block: negative 'count' parameter" ); + } + + $strRepeat = str_repeat( $content, $intCount ); + if( !empty( $params['assign'] ) ) { + $gBitSmarty->assign($params['assign'], $strRepeat ); + } else { + echo $strRepeat; + } + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +}
\ No newline at end of file diff --git a/smartyplugins/BlockSortLinks.php b/smartyplugins/BlockSortLinks.php new file mode 100755 index 0000000..9535212 --- /dev/null +++ b/smartyplugins/BlockSortLinks.php @@ -0,0 +1,47 @@ +<?php +namespace Bitweaver\Plugins; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty plugin + * ------------------------------------------------------------- + * File: block.sortlinks.php + * Type: block + * Name: sortlinks + * ------------------------------------------------------------- + */ +class BlockSortLinks implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + if ($content) { + $links = mb_split("\n",mb_strtolower($content) ); + $links2 = []; + foreach ($links as $value) { + $splitted=preg_split("/[<>]/",$value,-1,PREG_SPLIT_NO_EMPTY); + $links2[$splitted[2]]=$value; + } + + if( isset( $params['order'] ) && $params['order']=='reverse' ) { + krsort( $links2 ); + } else { + ksort($links2); + } + + foreach($links2 as $value) { + echo $value; + } + } + return ''; + } + + public function isCacheable(): bool { + return true; + } +} diff --git a/smartyplugins/BlockTr.php b/smartyplugins/BlockTr.php new file mode 100755 index 0000000..c8ced70 --- /dev/null +++ b/smartyplugins/BlockTr.php @@ -0,0 +1,34 @@ +<?php +namespace Bitweaver\Plugins; +use Bitweaver\KernelTools; +use Smarty\BlockHandler\BlockHandlerInterface; +use Smarty\Template; + +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + +/** + * Smarty plugin + * ------------------------------------------------------------- + * File: block.translate.php + * Type: block + * Name: translate + * Purpose: translate a block of text + * ------------------------------------------------------------- + */ +//global $lang; +//include_once('lang/language.php'); +class BlockTr implements BlockHandlerInterface { + + public function handle( $params, $content, Template $template, &$repeat): string { + echo KernelTools::tra( $content ); + return ''; + } + + public function isCacheable(): bool { + return true; + } +} |
