23 Commits

Author SHA1 Message Date
7d7fb7a186 PHPunit update to v12 2026-01-07 11:51:42 +09:00
8da6f903bc Release: v5.7.0 2026-01-07 10:12:47 +09:00
856a88d891 Update to Smarty 5.7.0
Fix tests for translations, where missing in the po/mo files
Add original and post translation for compare for outside and inside smarty translation

Update documentation and only keep the git update path, the others are deprecated

Update phpunit to v11 and update tests according to this, including updated test template data file

Add the tools folder to git ignore
2026-01-07 10:06:39 +09:00
2287af625b Update publish script with function calls 2025-07-15 14:12:54 +09:00
c5d151a08c Release: v5.4.3.1 2025-01-23 12:55:29 +09:00
00d64c54c2 Fix missing pos for checkboxes 2025-01-23 12:54:21 +09:00
7999a0aa46 Published: v5.4.3 2024-12-27 10:44:23 +09:00
e2151c7855 Merge branch 'Smarty5-Upgrade' 2024-12-27 10:42:04 +09:00
39bfcecd3f Add phpunit test to add all additions to smarty 2024-12-27 10:30:27 +09:00
7dba969e5d Add ingore for test template compile folder 2024-12-26 18:44:26 +09:00
fd369a7115 Smarty Update to v5.4.3 2024-12-26 17:59:29 +09:00
f7faa504f8 Update documentation and composer file with correct git repo Homepage
The correct place is at the EGRA github org, reference original in
the description.

Update the require part for the composer json file with this new information
2024-08-20 11:37:58 +09:00
39a579a78a Update publish script for Version 4 2024-08-20 11:14:48 +09:00
4ab8a6810c Deploy Script update
Fix all shellcheck problems
2024-08-20 11:11:25 +09:00
b390d0f67a Readme update for Smarty v5 update process 2024-08-20 11:05:36 +09:00
077b9f28ad Test plugins as loaded modifier, block t update
Update old block t data to match new block t class type

Test if we can load plugin parts, this is a side test for if we want
to not add them to the main Smarty load class

Remove old test source code, no longer needed
2024-08-20 10:56:59 +09:00
ea4162dfa6 Update Smarty: v5.4.0 2024-08-19 11:39:40 +09:00
d1fe561041 T Block: raise notice if target function is not callable
To catch if the L10n functions where not loaded
2024-08-09 18:47:00 +09:00
5bddc90972 Readme file update 2024-08-09 17:40:22 +09:00
2fc2807a6b Smarty v5 update test, initial code setup 2024-08-09 17:28:32 +09:00
b2c8fe2e1f Publish script update 2024-05-22 11:14:16 +09:00
888bda4c93 v4.5.2 Released, publish script update 2024-04-16 18:25:53 +09:00
b864e9dc7f fix publish script whitespace 2024-04-16 18:23:46 +09:00
399 changed files with 31896 additions and 28589 deletions

5
.gitignore vendored
View File

@@ -1,2 +1,5 @@
vendor
vendor/
tools/
composer.lock
.phpunit.cache/
tools-libs/

8
.phive/phars.xml Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^12.0.0" installed="12.5.4" location="./tools/phpunit" copy="false"/>
<phar name="phpcs" version="^4.0.0" installed="4.0.1" location="./tools/phpcs" copy="false"/>
<phar name="phpcbf" version="^4.0.0" installed="4.0.1" location="./tools/phpcbf" copy="false"/>
<phar name="phan" version="^5.5.2" installed="5.5.2" location="./tools/phan" copy="false"/>
<phar name="phpstan" version="^2.1.33" installed="2.1.33" location="./tools/phpstan" copy="false"/>
</phive>

2
.shellcheckrc Normal file
View File

@@ -0,0 +1,2 @@
shell=bash
external-sources=true

289
ReadMe.md
View File

@@ -1,6 +1,6 @@
# Composer package from Smarty Extended
This is an updated package for smarty\smarty
This is an updated package for [smarty\smarty](https://github.com/smarty-php/smarty/)
Adds:
@@ -20,27 +20,292 @@ composer config repositories.git.egplusww.jp.Composer composer https://git.egplu
Alternative setup composer local zip file repot:
`composer config repositories.composer.egplusww.jp composer http://composer.egplusww.jp`
> [!notice]
> Requires mbstring extension installed, will not use the symfony/polyfill-mbstring
## Install package
`composer require egrajp/smarty-extended:^4.3`
`composer require egrajp/smarty-extended:^5`
## How to update
1) update the original composer for ^4.3
2) copy over the src/sysplugins and all base files in src/
3) check either function.html_checkboxes.php and function.html_options.php have changed
4) copy src/plugins except the above two files, be sure to keep the block.t.php and function_popup*.php
5) Create new release version as official relase number
1) Alternative is to checkout master branch from git
1) Located in `Smarty/Smarty-git/src/`
2) Run `git pull smarty master`
2) Copy the `src/` folder as is over the `Smarty/Smarty-Extended/src` folder
3) From the `update/` folder copy
1) copy over the following into `src/BlockHandler/`:
1) T.php
2) copy over the following into `src/FunctionHandler`:
1) Popup.php
2) PopupInit.php
3) Upate the global `src/Extensions/DefaultExtension.php`:
1) `getFunctionHandler`: popup_init, popup
2) `getBlockHandler`: t
4) check either `src/FunctionHander/HtmlCheckboxes.php`, `src/FunctionHander/HtmlOptions.php` and `src/FunctionHander/HtmlBase.php` have changed
1) Update and leep the label/pos changes
4) Create new release version as official relase number
## Test
After any update run `tools/phpunit` to test the compare output, also a visual check can be done by accessing the `test/` folder
- Translations should work
- pos/label names for checkbox/options should work
- get var output should work
- plugin test load should work
### For intelephense users when phpunit was installed as phar
Intelephense cannot directly access the phar file, if phpunit was installed as a phar file (eg via phive) then the following commands must be used to setup the intelephense parser
In the base folder:
```sh
php -r "(new Phar('/path/to/phive/folder/.phive/phars/phpunit-X.Y.Z.phar'))->extractTo('tools-libs/phpunit');"
// Example, must point to the .phar file itself
php -r "(new Phar('/storage/home/clemens/.phive/phars/phpunit-11.5.46.phar'))->extractTo('tools-libs/phpunit');"
```
Then open the vscode settings and set for the Folder (if multiple folders are in the workspace) or the Workspace the following setting
```txt
"intelephense.environment.includePaths": [
"/tools-libs/phpunit/"
]
```
## Updated files (different from master)
### New
`src/plugins/block.t.php`
`src/plugins/function_popup.php`
`src/plugins/function_popup.init.php`
- `src/BlockHandler/T.php`
- `src/FunctionHandler/Popup.php`
- `src/FunctionHandler/PopupInit.php`
### Changed
`src/plugins/function.html_checkboxes.php`
`src/plugins/function.html_options.php`
- `src/FunctionHander/HtmlBase.php`
```diff
diff --git i/src/FunctionHandler/HtmlBase.php w/src/FunctionHandler/HtmlBase.php
index 99f8e6c..99a82a6 100644
--- i/src/FunctionHandler/HtmlBase.php
+++ w/src/FunctionHandler/HtmlBase.php
@@ -16,6 +16,7 @@ class HtmlBase extends Base {
* @param $labels
* @param $label_ids
* @param bool $escape
+ * @param $pos [default='']
*
* @return string
*/
@@ -30,7 +31,8 @@ class HtmlBase extends Base {
$separator,
$labels,
$label_ids,
- $escape = true
+ $escape = true,
+ $pos = ''
) {
$_output = '';
@@ -83,7 +85,7 @@ class HtmlBase extends Base {
}
$_output .= '<input type="' . $inputType . '" name="' . $name;
if ($ismultiselect) {
- $_output .= '[]';
+ $_output .= '[' . $pos . ']';
}
$_output .= '" value="' . $value . '"';
if ($labels && $label_ids) {
```
- `src/FunctionHander/HtmlCheckboxes.php`
```diff
--- Smarty/Smarty-git/src/FunctionHandler/HtmlCheckboxes.php 2024-04-16 18:06:25.299206501 +0900
+++ core_data/composer-packages/Smarty-Extended/src/FunctionHandler/HtmlCheckboxes.php 2024-07-26 11:48:23.698784159 +0900
@@ -24,6 +24,7 @@
* - checked (optional) - array default not set
* - separator (optional) - ie <br> or &nbsp;
* - output (optional) - the output next to each checkbox
+ * - pos (optional) - position entry into the [] for multi checkboxes
* - assign (optional) - assign the output as an array to this variable
* - escape (optional) - escape the content (not value), defaults to true
*
@@ -50,6 +51,7 @@
$labels = true;
$label_ids = false;
$output = null;
+ $pos = null;
$extra = '';
foreach ($params as $_key => $_val) {
switch ($_key) {
@@ -111,6 +113,9 @@
);
$options = (array)$_val;
break;
+ case 'pos':
+ $$_key = array_values((array)$_val);
+ break;
case 'strict':
case 'assign':
break;
@@ -145,6 +150,7 @@
$_html_result = [];
if (isset($options)) {
foreach ($options as $_key => $_val) {
+ $_pos = isset($pos[ $_key ]) ? $pos[ $_key ] : '';
$_html_result[] =
$this->getHtmlForInput(
'checkbox',
@@ -157,12 +163,14 @@
$separator,
$labels,
$label_ids,
- $_pos,
- $escape
+ $escape,
+ $_pos
);
}
} else {
foreach ($values as $_i => $_key) {
$_val = isset($output[$_i]) ? $output[$_i] : '';
+ $_pos = isset($pos[ $_i ]) ? $pos[ $_i ] : '';
$_html_result[] =
$this->getHtmlForInput(
'checkbox',
@@ -175,6 +183,7 @@
$separator,
$labels,
$label_ids,
- $_pos,
- $escape
+ $escape,
+ $_pos
);
}
```
- `src/FunctionHander/HtmlOptions.php`
```diff
--- Smarty/Smarty-git/src/FunctionHandler/HtmlOptions.php 2024-04-16 18:06:25.299206501 +0900
+++ core_data/composer-packages/Smarty-Extended/src/FunctionHandler/HtmlOptions.php 2024-07-26 11:51:13.287320709 +0900
@@ -17,6 +17,7 @@
* - selected (optional) - string default not set
* - output (required) - if not options supplied) - array
* - id (optional) - string default not set
+ * - label (optional) - label strinng to set
* - class (optional) - string default not set
*
* @author Monte Ohrt <monte at ohrt dot com>
@@ -40,6 +41,7 @@
$output = null;
$id = null;
$class = null;
+ $label = true;
$extra = '';
foreach ($params as $_key => $_val) {
switch ($_key) {
@@ -89,6 +91,11 @@
$selected = smarty_function_escape_special_chars((string)$_val);
}
break;
+ case 'label':
+ if ($_val == 'true' || $_val == 'false') {
+ $$_key = (string)$_val;
+ }
+ break;
case 'strict':
break;
case 'disabled':
@@ -124,12 +131,12 @@
$_idx = 0;
if (isset($options)) {
foreach ($options as $_key => $_val) {
- $_html_result .= $this->output($_key, $_val, $selected, $id, $class, $_idx);
+ $_html_result .= $this->output($_key, $_val, $selected, $id, $class, $label, $_idx);
}
} else {
foreach ($values as $_i => $_key) {
$_val = $output[$_i] ?? '';
- $_html_result .= $this->output($_key, $_val, $selected, $id, $class, $_idx);
+ $_html_result .= $this->output($_key, $_val, $selected, $id, $class, $label, $_idx);
}
}
if (!empty($name)) {
@@ -149,15 +156,20 @@
* @param $selected
* @param $id
* @param $class
+ * @param $label
* @param $idx
*
* @return string
*/
- private function output($key, $value, $selected, $id, $class, &$idx)
+ private function output($key, $value, $selected, $id, $class, $label, &$idx)
{
if (!is_array($value)) {
$_key = smarty_function_escape_special_chars($key);
- $_html_result = '<option value="' . $_key . '"';
+ $_html_result = '<option'
+ . ($label == 'true' ?
+ ' label="' . smarty_function_escape_special_chars($value) . '"' : ''
+ )
+ . ' value="' . $_key . '"';
if (is_array($selected)) {
if (isset($selected[ $_key ])) {
$_html_result .= ' selected="selected"';
@@ -192,6 +204,7 @@
$selected,
!empty($id) ? ($id . '-' . $idx) : null,
$class,
+ $label,
$_idx
);
$idx++;
@@ -209,11 +222,11 @@
*
* @return string
*/
- private function getHtmlForOptGroup($key, $values, $selected, $id, $class, &$idx)
+ private function getHtmlForOptGroup($key, $values, $selected, $id, $class, $label, &$idx)
{
$optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
foreach ($values as $key => $value) {
- $optgroup_html .= $this->output($key, $value, $selected, $id, $class, $idx);
+ $optgroup_html .= $this->output($key, $value, $selected, $id, $class, $label, $idx);
}
$optgroup_html .= "</optgroup>\n";
return $optgroup_html;
```
### Updated
- `src/Extensions/DefaultExtension.php`
```diff
--- Smarty/Smarty-git/src/Extension/DefaultExtension.php 2024-07-19 18:44:16.158700904 +0900
+++ core_data/composer-packages/Smarty-Extended/src/Extension/DefaultExtension.php 2024-07-26 17:38:18.257179379 +0900
@@ -94,6 +94,8 @@
case 'html_table': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\HtmlTable(); break;
case 'mailto': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Mailto(); break;
case 'math': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Math(); break;
+ case 'popup_init': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\PopupInit(); break;
+ case 'popup': $this->functionHandlers[$functionName] = new \Smarty\FunctionHandler\Popup(); break;
}
return $this->functionHandlers[$functionName] ?? null;
@@ -103,6 +105,7 @@
switch ($blockTagName) {
case 'textformat': $this->blockHandlers[$blockTagName] = new \Smarty\BlockHandler\TextFormat(); break;
+ case 't': $this->blockHandlers[$blockTagName] = new \Smarty\BlockHandler\T(); break;
}
return $this->blockHandlers[$blockTagName] ?? null;
```

View File

@@ -1,16 +1,19 @@
{
"name": "egrajp/smarty-extended",
"description": "Smarty, extended with gettext, checkbox/radio labels and index numbers",
"description": "Smarty, extended with gettext, checkbox/radio labels and index numbers. Based on the smarty project: https://github.com/smarty-php/smarty/",
"type": "library",
"keywords": [
"templating"
],
"homepage": "https://github.com/smarty-php/smarty/",
"homepage": "https://github.com/TBWA-EGPlus-Japan/Composer.smarty-extended",
"license": "LGPL-3.0",
"autoload": {
"classmap": [
"src/"
]
"files": [
"src/functions.php"
],
"psr-4" : {
"Smarty\\" : "src/"
}
},
"authors": [
{
@@ -19,5 +22,17 @@
}
],
"minimum-stability": "dev",
"require": {}
"repositories": {
"git.egplusww.jp.Composer": {
"type": "composer",
"url": "https://git.egplusww.jp/api/packages/Composer/composer"
}
},
"require": {
"php": "^7.4 || ^8.0",
"ext-mbstring": "*"
},
"require-dev": {
"egrajp/corelibs-composer-all": "^9"
}
}

14
phpunit.xml Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
bootstrap="test/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="deploy">
<directory>test</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -1 +1 @@
4.5.1
5.7.0

View File

@@ -1,89 +1,134 @@
#!/usr/bin/env bash
BASE_FOLDER=$(dirname $(readlink -f $0))"/";
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
PACKAGE_DOWNLOAD="${BASE_FOLDER}package-download/";
if [ ! -d "${PACKAGE_DOWNLOAD}" ]; then
mkdir "${PACKAGE_DOWNLOAD}";
mkdir "${PACKAGE_DOWNLOAD}";
fi;
VERSION=$(git tag --list | sort -V | tail -n1 | sed -e "s/^v//");
file_last_published="${BASE_FOLDER}last.published";
go_flag="$1";
function gitea_publish
{
_GITEA_PUBLISH="${1}"
_GITEA_UPLOAD_FILENAME="${2}"
_GITEA_URL_DL="${3}"
_GITEA_URL_PUSH="${4}"
_GITEA_USER="${5}"
_GITEA_TOKEN="${6}"
_PACKAGE_DOWNLOAD="${7}"
_VERSION="${8}"
_file_last_published="${9}"
if [ -z "${_GITEA_PUBLISH}" ]; then
return
fi;
if [ -n "${_GITEA_UPLOAD_FILENAME}" ] &&
[ -n "${_GITEA_URL_DL}" ] && [ -n "${_GITEA_URL_PUSH}" ] &&
[ -n "${_GITEA_USER}" ] && [ -n "${_GITEA_TOKEN}" ]; then
echo "> Publish ${_GITEA_UPLOAD_FILENAME} with ${_VERSION} to: ${_GITEA_URL_PUSH}";
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "> Download: ${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip";
curl -LJO \
--output-dir "${_PACKAGE_DOWNLOAD}" \
"${_GITEA_URL_DL}"/v"${_VERSION}".zip;
fi;
if [ ! -f "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" ]; then
echo "[!] Package file does not exist for version: ${_VERSION}";
else
response=$(curl --user "${_GITEA_USER}":"${_GITEA_TOKEN}" \
--upload-file "${_PACKAGE_DOWNLOAD}${_GITEA_UPLOAD_FILENAME}-v${_VERSION}.zip" \
"${_GITEA_URL_PUSH}"?version="${_VERSION}");
status=$(echo "${response}" | jq .errors[].status);
message=$(echo "${response}" | jq .errors[].message);
if [ -n "${status}" ]; then
echo "[!] Error ${status}: ${message}";
else
echo "> Publish completed";
fi;
echo "${_VERSION}" > "${_file_last_published}";
fi;
else
echo "[!] Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
fi;
}
function gitlab_publish
{
_GITLAB_PUBLISH="${1}";
_GITLAB_URL="${2}";
_GITLAB_DEPLOY_TOKEN="${3}";
_PACKAGE_DOWNLOAD="${4}"
_VERSION="${5}"
_file_last_published="${6}"
if [ -z "${_GITLAB_PUBLISH}" ]; then
return;
fi;
if [ -n "${_GITLAB_URL}" ] && [ -n "${_GITLAB_DEPLOY_TOKEN}" ]; then
curl --data tag=v"${_VERSION}" \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
curl --data branch=master \
--header "Deploy-Token: ${_GITLAB_DEPLOY_TOKEN}" \
"${_GITLAB_URL}";
echo "${_VERSION}" > "${_file_last_published}";
else
echo "[!] Missing GITLAB_URL or GITLAB_DEPLOY_TOKEN environment variable";
fi;
}
if [ -z "${VERSION}" ]; then
echo "Version must be set in the form x.y.z without any leading characters";
exit;
echo "[!] Version must be set in the form x.y.z without any leading characters";
exit;
fi;
# compare version, if different or newer, deploy
if [ -f "${file_last_published}" ]; then
LAST_PUBLISHED_VERSION=$(cat ${file_last_published});
if $(dpkg --compare-versions "${VERSION}" le "${LAST_PUBLISHED_VERSION}"); then
echo "git tag version ${VERSION} is not newer than previous published version ${LAST_PUBLISHED_VERSION}";
exit;
fi;
LAST_PUBLISHED_VERSION=$(cat "${file_last_published}");
if dpkg --compare-versions "${VERSION}" le "${LAST_PUBLISHED_VERSION}"; then
echo "[!] git tag version ${VERSION} is not newer than previous published version ${LAST_PUBLISHED_VERSION}";
fi;
fi;
# read in the .env.deploy file and we must have
# for gitea
# GITEA_PUBLISH: must be set with a value to trigger publish run
# GITEA_UPLOAD_FILENAME
# GITLAB_USER
# GITLAB_TOKEN
# GITLAB_URL
# GITEA_USER
# GITEA_DEPLOY_TOKEN
# GITEA_URL_DL
# GITEA_URL_PUSH
# for gitlab
# GITLAB_PUBLISH: must be set with a value to trigger publish run
# GITLAB_USER
# GITLAB_TOKEN
# GITLAB_URL
if [ ! -f "${BASE_FOLDER}.env.deploy" ]; then
echo "Deploy enviroment file .env.deploy is missing";
exit;
echo "[!] Deploy enviroment file .env.deploy is missing";
exit;
fi;
set -o allexport;
cd ${BASE_FOLDER};
cd "${BASE_FOLDER}" || exit
# shellcheck source=.env.deploy
source .env.deploy;
cd -;
cd - >/dev/null 2>&1 || exit;
set +o allexport;
if [ "${go_flag}" != "go" ]; then
echo "No go flag given";
echo "Would publish ${VERSION}";
echo "[END]";
exit;
echo "[!] No go flag given";
echo "> Would publish ${VERSION}";
echo "[END]";
exit;
fi;
echo "[START]";
# gitea
if [ ! -z "${GITEA_UPLOAD_FILENAME}" ] &&
[ ! -z "${GITEA_URL_DL}" ] && [ ! -z "${GITEA_URL_PUSH}" ] &&
[ ! -z "${GITEA_USER}" ] && [ ! -z "${GITEA_TOKEN}" ]; then
curl -LJO \
--output-dir "${PACKAGE_DOWNLOAD}" \
${GITEA_URL_DL}/v${VERSION}.zip;
# echo "curl -LJO \
# --output-dir "${PACKAGE_DOWNLOAD}" \
# ${GITEA_URL_DL}/v${VERSION}.zip;"
curl --user ${GITEA_USER}:${GITEA_TOKEN} \
--upload-file "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" \
${GITEA_URL_PUSH}?version=${VERSION};
# echo "curl --user ${GITEA_USER}:${GITEA_TOKEN} \
# --upload-file "${PACKAGE_DOWNLOAD}${GITEA_UPLOAD_FILENAME}-v${VERSION}.zip" \
# ${GITEA_URL_PUSH}?version=${VERSION};"
echo "${VERSION}" > "${file_last_published}";
else
echo "Missing either GITEA_UPLOAD_FILENAME, GITEA_URL_DL, GITEA_URL_PUSH, GITEA_USER or GITEA_TOKEN environment variable";
fi;
gitea_publish "${GITEA_PUBLISH}" "${GITEA_UPLOAD_FILENAME}" "${GITEA_URL_DL}" "${GITEA_URL_PUSH}" "${GITEA_USER}" "${GITEA_TOKEN}" "${PACKAGE_DOWNLOAD}" "${VERSION}" "${file_last_published}";
gitea_publish "${PR_GITEA_PUBLISH}" "${PR_GITEA_UPLOAD_FILENAME}" "${PR_GITEA_URL_DL}" "${PR_GITEA_URL_PUSH}" "${PR_GITEA_USER}" "${PR_GITEA_TOKEN}" "${PACKAGE_DOWNLOAD}" "${VERSION}" "${file_last_published}";
# gitlab
if [ ! -z "${GITLAB_URL}" ] && [ ! -z "${GITLAB_DEPLOY_TOKEN}" ]; then
curl --data tag=v${VERSION} \
--header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \
"${GITLAB_URL}";
curl --data branch=master \
--header "Deploy-Token: ${GITLAB_DEPLOY_TOKEN}" \
"${GITLAB_URL}";
echo "${VERSION}" > "${file_last_published}";
else
echo "Missing GITLAB_DEPLOY_TOKEN environment variable";
fi;
# gitlab_publish "${GITLAB_PUBLISH}" "${GITLAB_URL}" "${GITLAB_DEPLOY_TOKEN}" "${PACKAGE_DOWNLOAD}" "${VERSION}" "${file_last_published}";
echo "";
echo "[DONE]";

View File

@@ -1,111 +0,0 @@
<?php
/**
* Smarty Autoloader
*
* @package Smarty
*/
if (!defined('SMARTY_HELPER_FUNCTIONS_LOADED')) {
include __DIR__ . '/functions.php';
}
/**
* Smarty Autoloader
*
* @package Smarty
* @author Uwe Tews
* Usage:
* require_once '...path/Autoloader.php';
* Smarty_Autoloader::register();
* or
* include '...path/bootstrap.php';
*
* $smarty = new Smarty();
*/
class Smarty_Autoloader
{
/**
* Filepath to Smarty root
*
* @var string
*/
public static $SMARTY_DIR = null;
/**
* Filepath to Smarty internal plugins
*
* @var string
*/
public static $SMARTY_SYSPLUGINS_DIR = null;
/**
* Array with Smarty core classes and their filename
*
* @var array
*/
public static $rootClasses = array('smarty' => 'Smarty.class.php');
/**
* Registers Smarty_Autoloader backward compatible to older installations.
*
* @param bool $prepend Whether to prepend the autoloader or not.
*/
public static function registerBC($prepend = false)
{
/**
* register the class autoloader
*/
if (!defined('SMARTY_SPL_AUTOLOAD')) {
define('SMARTY_SPL_AUTOLOAD', 0);
}
if (SMARTY_SPL_AUTOLOAD
&& set_include_path(get_include_path() . PATH_SEPARATOR . SMARTY_SYSPLUGINS_DIR) !== false
) {
$registeredAutoLoadFunctions = spl_autoload_functions();
if (!isset($registeredAutoLoadFunctions[ 'spl_autoload' ])) {
spl_autoload_register();
}
} else {
self::register($prepend);
}
}
/**
* Registers Smarty_Autoloader as an SPL autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not.
*/
public static function register($prepend = false)
{
self::$SMARTY_DIR = defined('SMARTY_DIR') ? SMARTY_DIR : __DIR__ . DIRECTORY_SEPARATOR;
self::$SMARTY_SYSPLUGINS_DIR = defined('SMARTY_SYSPLUGINS_DIR') ? SMARTY_SYSPLUGINS_DIR :
self::$SMARTY_DIR . 'sysplugins' . DIRECTORY_SEPARATOR;
spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
}
/**
* Handles auto loading of classes.
*
* @param string $class A class name.
*/
public static function autoload($class)
{
if ($class[ 0 ] !== 'S' || strpos($class, 'Smarty') !== 0) {
return;
}
$_class = smarty_strtolower_ascii($class);
if (isset(self::$rootClasses[ $_class ])) {
$file = self::$SMARTY_DIR . self::$rootClasses[ $_class ];
if (is_file($file)) {
include $file;
}
} else {
$file = self::$SMARTY_SYSPLUGINS_DIR . $_class . '.php';
if (is_file($file)) {
include $file;
}
}
return;
}
}

19
src/BlockHandler/Base.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\BlockHandler;
use Smarty\Template;
abstract class Base implements BlockHandlerInterface {
/**
* @var bool
*/
protected $cacheable = true;
abstract public function handle($params, $content, Template $template, &$repeat);
public function isCacheable(): bool {
return $this->cacheable;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Smarty\BlockHandler;
use Smarty\Template;
interface BlockHandlerInterface {
public function handle($params, $content, Template $template, &$repeat);
public function isCacheable(): bool;
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\BlockHandler;
use Smarty\Template;
class BlockPluginWrapper extends Base {
private $callback;
public function __construct($callback, bool $cacheable = true) {
$this->callback = $callback;
$this->cacheable = $cacheable;
}
public function handle($params, $content, Template $template, &$repeat) {
return \call_user_func_array($this->callback, [$params, $content, &$template, &$repeat]);
}
}

238
src/BlockHandler/T.php Normal file
View File

@@ -0,0 +1,238 @@
<?php
namespace Smarty\BlockHandler;
// use Smarty\Smarty;
use Smarty\Template;
/**
* Smart {t}{/t} translation block plugin
* Type: block function
* Name: t
* Purpose: translate text with gettext l10n class
* Params:
*
* - escape - default html. else: javascript/js, url, no/off/false/'0'/0
* - plural - pluaral option for gettext
* - count - count option for gettext
* - domain - domain name option for gettext
* - context - context name option for gettext
* - 1, 2, n - position for replace %n
*
* Must have the following functions loaded and defined before hand
* d: domain
* p: context
* n: plural
* - _dnpgettext - domain, plural, context gettext call
* - _dngettext - domain, plural gettext call
* - _npgettext - plural, contexct gettext call
* - _ngettext - plural gettext call
* - _dpgettext - domain, context gettext call
* - _dgettext - domain gettext call
* - _pgettext - context gettext calls
* - _gettext - normal call
*
* @param array $params parameters
* @param string $content contents of the block
* @param Template $template template object
* @param boolean &$repeat repeat flag
*
* @return string content re-formatted
* @author Clemens Schwaighofer <gullevek at gullevek dot org>
* @throws \Smarty\Exception
*/
class T implements BlockHandlerInterface
{
/**
* Replaces arguments in a string with their values.
* Arguments are represented by % followed by their number.
*
* @param string $str Source string
* @param mixed mixed Arguments, can be passed in an array or through single variables.
* @return string Modified string
*/
private function smartyGettextStrArg($str/*, $varargs... */)
{
$tr = [];
$p = 0;
$nargs = func_num_args();
for ($i = 1; $i < $nargs; $i++) {
$arg = func_get_arg($i);
if (is_array($arg)) {
foreach ($arg as $aarg) {
$tr['%' . ++$p] = $aarg;
}
} else {
$tr['%' . ++$p] = $arg;
}
}
return strtr($str, $tr);
}
/**
* raise a notice for missing callable, indicates L10n functions where not loaded
*
* @param string $function
* @return void
*/
private function reportL10nNotInitialized(string $function): void
{
trigger_error("Missing " . $function . ", L10n::loadFunctions() called?", E_NOTICE);
}
/**
* main handler
*
* @param array $params
* @param string $content
* @param Template $template
* @param boolean $repeat
* @return void
*/
public function handle($params, $content, Template $template, &$repeat)
{
if (is_null($content)) {
return;
}
$escape = 'html';
$plural = null;
$count = null;
$domain = null;
$context = null;
foreach ($params as $_key => $_value) {
switch ($_key) {
// set escape mode, default html escape
case 'escape':
$escape = (string)$_value;
break;
// set plural parameters 'plural' and 'count'.
case 'plural':
$plural = $_value;
break;
// set count, only for plural, else ignored
case 'count':
$count = $_value;
break;
// get domain param
case 'domain':
$domain = (string)$_value;
break;
// get context param
case 'context':
$context = (string)$_value;
break;
}
}
// use plural if required parameters are set
if (isset($count) && isset($plural)) {
// plural calls
if (isset($domain) && isset($context)) {
if (is_callable('_dnpgettext')) {
$content = _dnpgettext($domain, $context, $content, $plural, $count);
} else {
$this->reportL10nNotInitialized('_dnpgettext');
}
} elseif (isset($domain)) {
if (is_callable('_dngettext')) {
$content = _dngettext($domain, $content, $plural, $count);
} else {
$this->reportL10nNotInitialized('_dngettext');
}
} elseif (isset($context)) {
if (is_callable('_npgettext')) {
$content = _npgettext($context, $content, $plural, $count);
} else {
$this->reportL10nNotInitialized('_npgettext');
}
} else {
if (is_callable('_ngettext')) {
$content = _ngettext($content, $plural, $count);
} else {
$this->reportL10nNotInitialized('_ngettext');
}
}
} else {
// use normal
if (isset($domain) && isset($context)) {
if (is_callable('_dpgettext')) {
$content = _dpgettext($domain, $context, $content);
} else {
$this->reportL10nNotInitialized('_dpgettext');
}
} elseif (isset($domain)) {
if (is_callable('_dgettext')) {
$content = _dgettext($domain, $content);
} else {
$this->reportL10nNotInitialized('_dgettext');
}
} elseif (isset($context)) {
if (is_callable('_pgettext')) {
$content = _pgettext($context, $content);
} else {
$this->reportL10nNotInitialized('_pgettext');
}
} else {
if (is_callable('_gettext')) {
$content = _gettext($content);
} else {
$this->reportL10nNotInitialized('_gettext');
}
}
}
// run strarg if there are parameters
if (count($params)) {
$content = $this->smartyGettextStrArg($content, $params);
}
switch ($escape) {
case 'html':
// default
$content = nl2br(htmlspecialchars($content));
break;
case 'javascript':
case 'js':
// javascript escape
$content = strtr(
$content,
[
'\\' => '\\\\',
"'" => "\\'",
'"' => '\\"',
"\r" => '\\r',
"\n" => '\\n',
'</' => '<\/'
]
);
break;
case 'url':
// url escape
$content = urlencode($content);
break;
// below is a list for explicit OFF
case 'no':
case 'off':
case 'false':
case '0':
case 0:
// explicit OFF
default:
break;
}
return $content;
}
public function isCacheable(): bool
{
return true;
}
}
// __END__

View File

@@ -0,0 +1,110 @@
<?php
namespace Smarty\BlockHandler;
use Smarty\Smarty;
use Smarty\Template;
/**
* Smarty {textformat}{/textformat} block plugin
* Type: block function
* Name: textformat
* Purpose: format text a certain way with preset styles
* or custom wrap/indent settings
* Params:
*
* - style - string (email)
* - indent - integer (0)
* - wrap - integer (80)
* - wrap_char - string ("\n")
* - indent_char - string (" ")
* - wrap_boundary - boolean (true)
*
* @param array $params parameters
* @param string $content contents of the block
* @param Template $template template object
* @param boolean &$repeat repeat flag
*
* @return string content re-formatted
* @author Monte Ohrt <monte at ohrt dot com>
* @throws \Smarty\Exception
*/
class TextFormat implements BlockHandlerInterface {
public function handle($params, $content, Template $template, &$repeat) {
if (is_null($content)) {
return;
}
$style = null;
$indent = 0;
$indent_first = 0;
$indent_char = ' ';
$wrap = 80;
$wrap_char = "\n";
$wrap_cut = false;
$assign = null;
foreach ($params as $_key => $_val) {
switch ($_key) {
case 'style':
case 'indent_char':
case 'wrap_char':
case 'assign':
$$_key = (string)$_val;
break;
case 'indent':
case 'indent_first':
case 'wrap':
$$_key = (int)$_val;
break;
case 'wrap_cut':
$$_key = (bool)$_val;
break;
default:
trigger_error("textformat: unknown attribute '{$_key}'");
}
}
if ($style === 'email') {
$wrap = 72;
}
// split into paragraphs
$_paragraphs = preg_split('![\r\n]{2}!', $content);
foreach ($_paragraphs as &$_paragraph) {
if (!$_paragraph) {
continue;
}
// convert mult. spaces & special chars to single space
$_paragraph =
preg_replace(
array(
'!\s+!' . Smarty::$_UTF8_MODIFIER,
'!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER
),
array(
' ',
''
),
$_paragraph
);
// indent first line
if ($indent_first > 0) {
$_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
}
// wordwrap sentences
$_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
// indent lines
if ($indent > 0) {
$_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
}
}
$_output = implode($wrap_char . $wrap_char, $_paragraphs);
if ($assign) {
$template->assign($assign, $_output);
} else {
return $_output;
}
}
public function isCacheable(): bool {
return true;
}
}

156
src/Cacheresource/Base.php Normal file
View File

@@ -0,0 +1,156 @@
<?php
namespace Smarty\Cacheresource;
use Smarty\Exception;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Cached;
/**
* Cache Handler API
* @author Rodney Rehm
*/
abstract class Base
{
/**
* populate Cached Object with metadata from Resource
*
* @param Cached $cached cached object
* @param Template $_template template object
*
* @return void
*/
abstract public function populate(Cached $cached, Template $_template);
/**
* populate Cached Object with timestamp and exists from Resource
*
* @param Cached $cached
*
* @return void
*/
abstract public function populateTimestamp(Cached $cached);
/**
* Read the cached template and process header
*
* @param Template $_template template object
* @param Cached|null $cached cached object
* @param boolean $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
abstract public function process(
Template $_template,
?Cached $cached = null,
$update = false
);
/**
* Write the rendered template output to cache
*
* @param Template $_template template object
* @param string $content content to cache
*
* @return boolean success
*/
abstract public function storeCachedContent(Template $_template, $content);
/**
* Read cached template from cache
*
* @param Template $_template template object
*
* @return string content
*/
abstract public function retrieveCachedContent(Template $_template);
/**
* Empty cache
*
* @param Smarty $smarty Smarty object
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
abstract public function clearAll(Smarty $smarty, $exp_time = null);
/**
* Empty cache for a specific template
*
* @param Smarty $smarty Smarty object
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
/**
* @param Smarty $smarty
* @param Cached $cached
*
* @return bool|null
*/
public function locked(Smarty $smarty, Cached $cached)
{
// theoretically locking_timeout should be checked against time_limit (max_execution_time)
$start = microtime(true);
$hadLock = null;
while ($this->hasLock($smarty, $cached)) {
$hadLock = true;
if (microtime(true) - $start > $smarty->locking_timeout) {
// abort waiting for lock release
return false;
}
sleep(1);
}
return $hadLock;
}
/**
* Check is cache is locked for this template
*
* @param Smarty $smarty
* @param Cached $cached
*
* @return bool
*/
public function hasLock(Smarty $smarty, Cached $cached)
{
// check if lock exists
return false;
}
/**
* Lock cache for this template
*
* @param Smarty $smarty
* @param Cached $cached
*
* @return bool
*/
public function acquireLock(Smarty $smarty, Cached $cached)
{
// create lock
return true;
}
/**
* Unlock cache for this template
*
* @param Smarty $smarty
* @param Cached $cached
*
* @return bool
*/
public function releaseLock(Smarty $smarty, Cached $cached)
{
// release lock
return true;
}
}

View File

@@ -1,19 +1,26 @@
<?php
namespace Smarty\Cacheresource;
/**
* Smarty Internal Plugin
*
* @package Smarty
* @subpackage Cacher
*/
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Cached;
/**
* Cache Handler API
*
* @package Smarty
* @subpackage Cacher
* @author Rodney Rehm
*/
abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
abstract class Custom extends Base
{
/**
* fetch cached content and its modification time from data source
@@ -73,20 +80,20 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
/**
* populate Cached Object with meta data from Resource
* populate Cached Object with metadata from Resource
*
* @param Smarty_Template_Cached $cached cached object
* @param Smarty_Internal_Template $_template template object
* @param \Smarty\Template\Cached $cached cached object
* @param Template $_template template object
*
* @return void
*/
public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
public function populate(\Smarty\Template\Cached $cached, Template $_template)
{
$_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
$_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
$path = $cached->source->uid . $_cache_id . $_compile_id;
$path = $cached->getSource()->uid . $_cache_id . $_compile_id;
$cached->filepath = sha1($path);
if ($_template->smarty->cache_locking) {
if ($_template->getSmarty()->cache_locking) {
$cached->lock_id = sha1('lock.' . $path);
}
$this->populateTimestamp($cached);
@@ -95,14 +102,14 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
/**
* populate Cached Object with timestamp and exists from Resource
*
* @param Smarty_Template_Cached $cached
* @param \Smarty\Template\Cached $cached
*
* @return void
*/
public function populateTimestamp(Smarty_Template_Cached $cached)
public function populateTimestamp(\Smarty\Template\Cached $cached)
{
$mtime =
$this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
$this->fetchTimestamp($cached->filepath, $cached->getSource()->name, $cached->cache_id, $cached->compile_id);
if ($mtime !== null) {
$cached->timestamp = $mtime;
$cached->exists = !!$cached->timestamp;
@@ -111,7 +118,7 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
$timestamp = null;
$this->fetch(
$cached->filepath,
$cached->source->name,
$cached->getSource()->name,
$cached->cache_id,
$cached->compile_id,
$cached->content,
@@ -121,29 +128,29 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
$cached->exists = !!$cached->timestamp;
}
/**
* Read the cached template and process the header
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
* @param Smarty_Template_Cached $cached cached object
* @param boolean $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
/**
* Read the cached template and process the header
*
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
* @param Cached|null $cached cached object
* @param boolean $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
public function process(
Smarty_Internal_Template $_smarty_tpl,
Smarty_Template_Cached $cached = null,
$update = false
Template $_smarty_tpl,
?\Smarty\Template\Cached $cached = null,
$update = false
) {
if (!$cached) {
$cached = $_smarty_tpl->cached;
$cached = $_smarty_tpl->getCached();
}
$content = $cached->content ? $cached->content : null;
$timestamp = $cached->timestamp ? $cached->timestamp : null;
if ($content === null || !$timestamp) {
$this->fetch(
$_smarty_tpl->cached->filepath,
$_smarty_tpl->source->name,
$_smarty_tpl->getCached()->filepath,
$_smarty_tpl->getSource()->name,
$_smarty_tpl->cache_id,
$_smarty_tpl->compile_id,
$content,
@@ -161,16 +168,16 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
/**
* Write the rendered template output to cache
*
* @param Smarty_Internal_Template $_template template object
* @param Template $_template template object
* @param string $content content to cache
*
* @return boolean success
*/
public function writeCachedContent(Smarty_Internal_Template $_template, $content)
public function storeCachedContent(Template $_template, $content)
{
return $this->save(
$_template->cached->filepath,
$_template->source->name,
$_template->getCached()->filepath,
$_template->getSource()->name,
$_template->cache_id,
$_template->compile_id,
$_template->cache_lifetime,
@@ -181,19 +188,18 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
/**
* Read cached template from cache
*
* @param Smarty_Internal_Template $_template template object
* @param Template $_template template object
*
* @return string|boolean content
*/
public function readCachedContent(Smarty_Internal_Template $_template)
public function retrieveCachedContent(Template $_template)
{
$content = $_template->cached->content ? $_template->cached->content : null;
$timestamp = null;
if ($content === null) {
$content = $_template->getCached()->content ?: null;
if ($content === null) {
$timestamp = null;
$this->fetch(
$_template->cached->filepath,
$_template->source->name,
$_template->getCached()->filepath,
$_template->getSource()->name,
$_template->cache_id,
$_template->compile_id,
$content,
@@ -206,15 +212,15 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
return false;
}
/**
* Empty cache
*
* @param Smarty $smarty Smarty object
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
public function clearAll(Smarty $smarty, $exp_time = null)
/**
* Empty cache
*
* @param \Smarty\Smarty $smarty Smarty object
* @param null $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
public function clearAll(\Smarty\Smarty $smarty, $exp_time = null)
{
return $this->delete(null, null, null, $exp_time);
}
@@ -222,20 +228,20 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
/**
* Empty cache for a specific template
*
* @param Smarty $smarty Smarty object
* @param \Smarty\Smarty $smarty Smarty object
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return int number of cache files deleted
* @throws \SmartyException
* @throws \Smarty\Exception
*/
public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
public function clear(\Smarty\Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
{
$cache_name = null;
if (isset($resource_name)) {
$source = Smarty_Template_Source::load(null, $smarty, $resource_name);
$source = \Smarty\Template\Source::load(null, $smarty, $resource_name);
if ($source->exists) {
$cache_name = $source->name;
} else {
@@ -245,18 +251,18 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
}
/**
* Check is cache is locked for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
*
* @return boolean true or false if cache is locked
*/
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
/**
* Check is cache is locked for this template
*
* @param Smarty $smarty Smarty object
* @param Cached $cached cached object
*
* @return boolean true or false if cache is locked
*/
public function hasLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
{
$id = $cached->lock_id;
$name = $cached->source->name . '.lock';
$name = $cached->getSource()->name . '.lock';
$mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
if ($mtime === null) {
$this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
@@ -267,31 +273,31 @@ abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
/**
* Lock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @param \Smarty\Smarty $smarty Smarty object
* @param \Smarty\Template\Cached $cached cached object
*
* @return bool|void
*/
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
public function acquireLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
{
$cached->is_locked = true;
$id = $cached->lock_id;
$name = $cached->source->name . '.lock';
$name = $cached->getSource()->name . '.lock';
$this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
}
/**
* Unlock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @param \Smarty\Smarty $smarty Smarty object
* @param \Smarty\Template\Cached $cached cached object
*
* @return bool|void
*/
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
public function releaseLock(\Smarty\Smarty $smarty, \Smarty\Template\Cached $cached)
{
$cached->is_locked = false;
$name = $cached->source->name . '.lock';
$name = $cached->getSource()->name . '.lock';
$this->delete($name, $cached->cache_id, $cached->compile_id, null);
}
}

338
src/Cacheresource/File.php Normal file
View File

@@ -0,0 +1,338 @@
<?php
namespace Smarty\Cacheresource;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Cached;
/**
* Smarty Internal Plugin CacheResource File
*
* @author Uwe Tews
* @author Rodney Rehm
*/
/**
* This class does contain all necessary methods for the HTML cache on file system
* Implements the file system as resource for the HTML cache Version using nocache inserts.
*/
class File extends Base
{
/**
* populate Cached Object with metadata from Resource
*
* @param Cached $cached cached object
* @param Template $_template template object
*
* @return void
*/
public function populate(Cached $cached, Template $_template)
{
$source = $_template->getSource();
$smarty = $_template->getSmarty();
$_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
$_filepath = $source->uid;
$cached->filepath = $smarty->getCacheDir();
if (isset($_template->cache_id)) {
$cached->filepath .= preg_replace(
array(
'![^\w|]+!',
'![|]+!'
),
array(
'_',
$_compile_dir_sep
),
$_template->cache_id
) . $_compile_dir_sep;
}
if (isset($_template->compile_id)) {
$cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
}
// if use_sub_dirs, break file into directories
if ($smarty->use_sub_dirs) {
$cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] .
$_filepath[ 3 ] .
DIRECTORY_SEPARATOR .
$_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
}
$cached->filepath .= $_filepath . '_' . $source->getBasename();
if ($smarty->cache_locking) {
$cached->lock_id = $cached->filepath . '.lock';
}
$cached->filepath .= '.php';
$cached->timestamp = $cached->exists = is_file($cached->filepath);
if ($cached->exists) {
$cached->timestamp = filemtime($cached->filepath);
}
}
/**
* populate Cached Object with timestamp and exists from Resource
*
* @param Cached $cached cached object
*
* @return void
*/
public function populateTimestamp(Cached $cached)
{
$cached->timestamp = $cached->exists = is_file($cached->filepath);
if ($cached->exists) {
$cached->timestamp = filemtime($cached->filepath);
}
}
/**
* Read the cached template and process its header
*
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
* @param Cached|null $cached cached object
* @param bool $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
public function process(
Template $_smarty_tpl,
?Cached $cached = null,
$update = false
) {
$_smarty_tpl->getCached()->setValid(false);
if ($update && defined('HHVM_VERSION')) {
eval('?>' . file_get_contents($_smarty_tpl->getCached()->filepath));
return true;
} else {
return @include $_smarty_tpl->getCached()->filepath;
}
}
/**
* Write the rendered template output to cache
*
* @param Template $_template template object
* @param string $content content to cache
*
* @return bool success
* @throws \Smarty\Exception
*/
public function storeCachedContent(Template $_template, $content)
{
if ($_template->getSmarty()->writeFile($_template->getCached()->filepath, $content) === true) {
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api'))) < 1
) {
opcache_invalidate($_template->getCached()->filepath, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->getCached()->filepath);
}
$cached = $_template->getCached();
$cached->timestamp = $cached->exists = is_file($cached->filepath);
if ($cached->exists) {
$cached->timestamp = filemtime($cached->filepath);
return true;
}
}
return false;
}
/**
* Read cached template from cache
*
* @param Template $_template template object
*
* @return string content
*/
public function retrieveCachedContent(Template $_template)
{
if (is_file($_template->getCached()->filepath)) {
return file_get_contents($_template->getCached()->filepath);
}
return false;
}
/**
* Empty cache
*
* @param Smarty $smarty
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
public function clearAll(Smarty $smarty, $exp_time = null)
{
return $this->clear($smarty, null, null, null, $exp_time);
}
/**
* Empty cache for a specific template
*
* @param Smarty $smarty
* @param string $resource_name template name
* @param string $cache_id cache id
* @param string $compile_id compile id
* @param integer $exp_time expiration time (number of seconds, not timestamp)
*
* @return integer number of cache files deleted
*/
public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
{
$_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
$_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
$_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
$_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
$_dir = $smarty->getCacheDir();
if ($_dir === '/') { //We should never want to delete this!
return 0;
}
$_dir_length = strlen($_dir);
if (isset($_cache_id)) {
$_cache_id_parts = explode('|', $_cache_id);
$_cache_id_parts_count = count($_cache_id_parts);
if ($smarty->use_sub_dirs) {
foreach ($_cache_id_parts as $id_part) {
$_dir .= $id_part . '/';
}
}
}
if (isset($resource_name)) {
$_save_stat = $smarty->caching;
$smarty->caching = \Smarty\Smarty::CACHING_LIFETIME_CURRENT;
$tpl = $smarty->doCreateTemplate($resource_name);
$smarty->caching = $_save_stat;
// remove from template cache
if ($tpl->getSource()->exists) {
$_resourcename_parts = basename(str_replace('^', '/', $tpl->getCached()->filepath));
} else {
return 0;
}
}
$_count = 0;
$_time = time();
if (file_exists($_dir)) {
$_cacheDirs = new RecursiveDirectoryIterator($_dir);
$_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_cache as $_file) {
if (substr(basename($_file->getPathname()), 0, 1) === '.') {
continue;
}
$_filepath = (string)$_file;
// directory ?
if ($_file->isDir()) {
if (!$_cache->isDot()) {
// delete folder if empty
@rmdir($_file->getPathname());
}
} else {
// delete only php files
if (substr($_filepath, -4) !== '.php') {
continue;
}
$_parts = explode($_dir_sep, str_replace('\\', '/', substr($_filepath, $_dir_length)));
$_parts_count = count($_parts);
// check name
if (isset($resource_name)) {
if ($_parts[ $_parts_count - 1 ] !== $_resourcename_parts) {
continue;
}
}
// check compile id
if (isset($_compile_id) && (!isset($_parts[ $_parts_count - 2 - $_compile_id_offset ])
|| $_parts[ $_parts_count - 2 - $_compile_id_offset ] !== $_compile_id)
) {
continue;
}
// check cache id
if (isset($_cache_id)) {
// count of cache id parts
$_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset :
$_parts_count - 1 - $_compile_id_offset;
if ($_parts_count < $_cache_id_parts_count) {
continue;
}
for ($i = 0; $i < $_cache_id_parts_count; $i++) {
if ($_parts[ $i ] !== $_cache_id_parts[ $i ]) {
continue 2;
}
}
}
if (is_file($_filepath)) {
// expired ?
if (isset($exp_time)) {
if ($exp_time < 0) {
preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_filepath), $match);
if ($_time < (filemtime($_filepath) + $match[ 1 ])) {
continue;
}
} else {
if ($_time - filemtime($_filepath) < $exp_time) {
continue;
}
}
}
$_count += @unlink($_filepath) ? 1 : 0;
if (function_exists('opcache_invalidate')
&& (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
) {
opcache_invalidate($_filepath, true);
} elseif (function_exists('apc_delete_file')) {
apc_delete_file($_filepath);
}
}
}
}
}
return $_count;
}
/**
* Check is cache is locked for this template
*
* @param Smarty $smarty Smarty object
* @param Cached $cached cached object
*
* @return boolean true or false if cache is locked
*/
public function hasLock(Smarty $smarty, Cached $cached)
{
clearstatcache(true, $cached->lock_id ?? '');
if (null !== $cached->lock_id && is_file($cached->lock_id)) {
$t = filemtime($cached->lock_id);
return $t && (time() - $t < $smarty->locking_timeout);
} else {
return false;
}
}
/**
* Lock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Cached $cached cached object
*
* @return void
*/
public function acquireLock(Smarty $smarty, Cached $cached)
{
$cached->is_locked = true;
touch($cached->lock_id);
}
/**
* Unlock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Cached $cached cached object
*
* @return void
*/
public function releaseLock(Smarty $smarty, Cached $cached)
{
$cached->is_locked = false;
@unlink($cached->lock_id);
}
}

View File

@@ -1,9 +1,16 @@
<?php
namespace Smarty\Cacheresource;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Cached;
/**
* Smarty Internal Plugin
*
* @package Smarty
* @subpackage Cacher
*/
/**
@@ -24,11 +31,11 @@
* cache groups: if your cache groups look somewhat like »a|b|$page|$items|$whatever«
* consider using »a|b|c|$page-$items-$whatever« instead.
*
* @package Smarty
* @subpackage Cacher
* @author Rodney Rehm
*/
abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
abstract class KeyValueStore extends Base
{
/**
* cache for contents
@@ -47,14 +54,14 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* populate Cached Object with meta data from Resource
*
* @param Smarty_Template_Cached $cached cached object
* @param Smarty_Internal_Template $_template template object
* @param Cached $cached cached object
* @param Template $_template template object
*
* @return void
*/
public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
public function populate(Cached $cached, Template $_template)
{
$cached->filepath = $_template->source->uid . '#' . $this->sanitize($cached->source->resource) . '#' .
$cached->filepath = $_template->getSource()->uid . '#' . $this->sanitize($cached->getSource()->resource) . '#' .
$this->sanitize($cached->cache_id) . '#' . $this->sanitize($cached->compile_id);
$this->populateTimestamp($cached);
}
@@ -62,20 +69,20 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* populate Cached Object with timestamp and exists from Resource
*
* @param Smarty_Template_Cached $cached cached object
* @param Cached $cached cached object
*
* @return void
*/
public function populateTimestamp(Smarty_Template_Cached $cached)
public function populateTimestamp(Cached $cached)
{
if (!$this->fetch(
$cached->filepath,
$cached->source->name,
$cached->getSource()->name,
$cached->cache_id,
$cached->compile_id,
$content,
$timestamp,
$cached->source->uid
$cached->getSource()->uid
)
) {
return;
@@ -85,34 +92,34 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$cached->exists = !!$cached->timestamp;
}
/**
* Read the cached template and process the header
*
* @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
* @param Smarty_Template_Cached $cached cached object
* @param boolean $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
/**
* Read the cached template and process the header
*
* @param Template $_smarty_tpl do not change variable name, is used by compiled template
* @param Cached|null $cached cached object
* @param boolean $update flag if called because cache update
*
* @return boolean true or false if the cached content does not exist
*/
public function process(
Smarty_Internal_Template $_smarty_tpl,
Smarty_Template_Cached $cached = null,
$update = false
Template $_smarty_tpl,
?Cached $cached = null,
$update = false
) {
if (!$cached) {
$cached = $_smarty_tpl->cached;
$cached = $_smarty_tpl->getCached();
}
$content = $cached->content ? $cached->content : null;
$timestamp = $cached->timestamp ? $cached->timestamp : null;
$content = $cached->content ?: null;
$timestamp = $cached->timestamp ?: null;
if ($content === null || !$timestamp) {
if (!$this->fetch(
$_smarty_tpl->cached->filepath,
$_smarty_tpl->source->name,
$_smarty_tpl->getCached()->filepath,
$_smarty_tpl->getSource()->name,
$_smarty_tpl->cache_id,
$_smarty_tpl->compile_id,
$content,
$timestamp,
$_smarty_tpl->source->uid
$_smarty_tpl->getSource()->uid
)
) {
return false;
@@ -128,37 +135,37 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
/**
* Write the rendered template output to cache
*
* @param Smarty_Internal_Template $_template template object
* @param Template $_template template object
* @param string $content content to cache
*
* @return boolean success
*/
public function writeCachedContent(Smarty_Internal_Template $_template, $content)
public function storeCachedContent(Template $_template, $content)
{
$this->addMetaTimestamp($content);
return $this->write(array($_template->cached->filepath => $content), $_template->cache_lifetime);
return $this->write(array($_template->getCached()->filepath => $content), $_template->cache_lifetime);
}
/**
* Read cached template from cache
*
* @param Smarty_Internal_Template $_template template object
* @param Template $_template template object
*
* @return string|false content
*/
public function readCachedContent(Smarty_Internal_Template $_template)
public function retrieveCachedContent(Template $_template)
{
$content = $_template->cached->content ? $_template->cached->content : null;
$content = $_template->getCached()->content ?: null;
$timestamp = null;
if ($content === null) {
if (!$this->fetch(
$_template->cached->filepath,
$_template->source->name,
$_template->getCached()->filepath,
$_template->getSource()->name,
$_template->cache_id,
$_template->compile_id,
$content,
$timestamp,
$_template->source->uid
$_template->getSource()->uid
)
) {
return false;
@@ -200,7 +207,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param integer $exp_time expiration time [being ignored]
*
* @return int number of cache files deleted [always -1]
* @throws \SmartyException
* @throws \Smarty\Exception
* @uses buildCachedFilepath() to generate the CacheID
* @uses invalidate() to mark CacheIDs parent chain as outdated
* @uses delete() to remove CacheID from cache
@@ -222,12 +229,12 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* @param string $resource_name template name
*
* @return string filepath of cache file
* @throws \SmartyException
* @throws \Smarty\Exception
*/
protected function getTemplateUid(Smarty $smarty, $resource_name)
{
if (isset($resource_name)) {
$source = Smarty_Template_Source::load(null, $smarty, $resource_name);
$source = \Smarty\Template\Source::load(null, $smarty, $resource_name);
if ($source->exists) {
return $source->uid;
}
@@ -380,11 +387,7 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
$compile_id = null,
$resource_uid = null
) {
// abort if there is no CacheID
if (false && !$cid) {
return 0;
}
// abort if there are no InvalidationKeys to check
// abort if there are no InvalidationKeys to check
if (!($_cid = $this->listInvalidationKeys($cid, $resource_name, $cache_id, $compile_id, $resource_uid))) {
return 0;
}
@@ -457,11 +460,11 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Check is cache is locked for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @param Cached $cached cached object
*
* @return boolean true or false if cache is locked
*/
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
public function hasLock(Smarty $smarty, Cached $cached)
{
$key = 'LOCK#' . $cached->filepath;
$data = $this->read(array($key));
@@ -472,11 +475,11 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Lock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @param Cached $cached cached object
*
* @return bool|void
*/
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
public function acquireLock(Smarty $smarty, Cached $cached)
{
$cached->is_locked = true;
$key = 'LOCK#' . $cached->filepath;
@@ -487,11 +490,11 @@ abstract class Smarty_CacheResource_KeyValueStore extends Smarty_CacheResource
* Unlock cache for this template
*
* @param Smarty $smarty Smarty object
* @param Smarty_Template_Cached $cached cached object
* @param Cached $cached cached object
*
* @return bool|void
* @return void
*/
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
public function releaseLock(Smarty $smarty, Cached $cached)
{
$cached->is_locked = false;
$key = 'LOCK#' . $cached->filepath;

View File

@@ -0,0 +1,144 @@
<?php
namespace Smarty\Compile;
/**
* This class handles compiling the attributes.
*/
class AttributeCompiler
{
/**
* Array of names of required attributes required by tag
*
* @var array
*/
protected $required_attributes = [];
/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected $optional_attributes = [];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];
/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = [];
public function __construct(
array $required_attributes = [],
array $optional_attributes = [],
array $shorttag_order = [],
array $option_flags = []
) {
$this->required_attributes = $required_attributes;
$this->optional_attributes = $optional_attributes;
$this->shorttag_order = $shorttag_order;
$this->option_flags = $option_flags;
}
/**
* This function checks if the attributes passed are valid
* The attributes passed for the tag to compile are checked against the list of required and
* optional attributes. Required attributes must be present. Optional attributes are check against
* the corresponding list. The keyword '_any' specifies that any attribute will be accepted
* as valid
*
* @param object $compiler compiler object
* @param array $attributes attributes applied to the tag
*
* @return array of mapped attributes for further processing
*/
public function getAttributes($compiler, $attributes)
{
$_indexed_attr = [];
$options = array_fill_keys($this->option_flags, true);
foreach ($attributes as $key => $mixed) {
// shorthand ?
if (!is_array($mixed)) {
// options flag ?
if (isset($options[trim($mixed, '\'"')])) {
$_indexed_attr[trim($mixed, '\'"')] = true;
// shorthand attribute ?
} elseif (isset($this->shorttag_order[$key])) {
$_indexed_attr[$this->shorttag_order[$key]] = $mixed;
} else {
// too many shorthands
$compiler->trigger_template_error('too many shorthand attributes', null, true);
}
// named attribute
} else {
foreach ($mixed as $k => $v) {
// options flag?
if (isset($options[$k])) {
if (is_bool($v)) {
$_indexed_attr[$k] = $v;
} else {
if (is_string($v)) {
$v = trim($v, '\'" ');
}
// Mapping array for boolean option value
static $optionMap = [1 => true, 0 => false, 'true' => true, 'false' => false];
if (isset($optionMap[$v])) {
$_indexed_attr[$k] = $optionMap[$v];
} else {
$compiler->trigger_template_error(
"illegal value '" . var_export($v, true) .
"' for options flag '{$k}'",
null,
true
);
}
}
// must be named attribute
} else {
$_indexed_attr[$k] = $v;
}
}
}
}
// check if all required attributes present
foreach ($this->required_attributes as $attr) {
if (!isset($_indexed_attr[$attr])) {
$compiler->trigger_template_error("missing '{$attr}' attribute", null, true);
}
}
// check for not allowed attributes
if ($this->optional_attributes !== ['_any']) {
$allowedAttributes = array_fill_keys(
array_merge(
$this->required_attributes,
$this->optional_attributes,
$this->option_flags
),
true
);
foreach ($_indexed_attr as $key => $dummy) {
if (!isset($allowedAttributes[$key]) && $key !== 0) {
$compiler->trigger_template_error("unexpected '{$key}' attribute", null, true);
}
}
}
// default 'false' for all options flags not set
foreach ($this->option_flags as $flag) {
if (!isset($_indexed_attr[$flag])) {
$_indexed_attr[$flag] = false;
}
}
return $_indexed_attr;
}
}

161
src/Compile/Base.php Normal file
View File

@@ -0,0 +1,161 @@
<?php
/**
* Smarty Internal Compile Plugin Base
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\Data;
use Smarty\Exception;
/**
* This class does extend all internal compile plugins
*
*/
abstract class Base implements CompilerInterface {
/**
* Array of names of required attribute required by tag
*
* @var array
*/
protected $required_attributes = [];
/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected $optional_attributes = [];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];
/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = ['nocache'];
/**
* @var bool
*/
protected $cacheable = true;
public function isCacheable(): bool {
return $this->cacheable;
}
/**
* Converts attributes into parameter array strings
*
* @param array $_attr
*
* @return array
*/
protected function formatParamsArray(array $_attr): array {
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
$_paramsArray[] = var_export($_key, true) . "=>" . $_value;
}
return $_paramsArray;
}
/**
* This function checks if the attributes passed are valid
* The attributes passed for the tag to compile are checked against the list of required and
* optional attributes. Required attributes must be present. Optional attributes are check against
* the corresponding list. The keyword '_any' specifies that any attribute will be accepted
* as valid
*
* @param object $compiler compiler object
* @param array $attributes attributes applied to the tag
*
* @return array of mapped attributes for further processing
*/
protected function getAttributes($compiler, $attributes) {
return (new AttributeCompiler(
$this->required_attributes,
$this->optional_attributes,
$this->shorttag_order,
$this->option_flags
))->getAttributes($compiler, $attributes);
}
/**
* Push opening tag name on stack
* Optionally additional data can be saved on stack
*
* @param Template $compiler compiler object
* @param string $openTag the opening tag's name
* @param mixed $data optional data saved
*/
protected function openTag(Template $compiler, $openTag, $data = null) {
$compiler->openTag($openTag, $data);
}
/**
* Pop closing tag
* Raise an error if this stack-top doesn't match with expected opening tags
*
* @param Template $compiler compiler object
* @param array|string $expectedTag the expected opening tag names
*
* @return mixed any type the opening tag's name or saved data
*/
protected function closeTag(Template $compiler, $expectedTag) {
return $compiler->closeTag($expectedTag);
}
/**
* @param mixed $scope
* @param array $invalidScopes
*
* @return int
* @throws Exception
*/
protected function convertScope($scope): int {
static $scopes = [
'local' => Data::SCOPE_LOCAL, // current scope
'parent' => Data::SCOPE_PARENT, // parent scope (definition unclear)
'tpl_root' => Data::SCOPE_TPL_ROOT, // highest template (keep going up until parent is not a template)
'root' => Data::SCOPE_ROOT, // highest scope (definition unclear)
'global' => Data::SCOPE_GLOBAL, // smarty object
'smarty' => Data::SCOPE_SMARTY, // @deprecated alias of 'global'
];
$_scopeName = trim($scope, '\'"');
if (is_numeric($_scopeName) && in_array($_scopeName, $scopes)) {
return (int) $_scopeName;
}
if (isset($scopes[$_scopeName])) {
return $scopes[$_scopeName];
}
$err = var_export($_scopeName, true);
throw new Exception("illegal value '{$err}' for \"scope\" attribute");
}
/**
* Compiles code for the tag
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null): string;
}

View File

@@ -0,0 +1,228 @@
<?php
/**
* Smarty Internal Plugin Compile Block Plugin
* Compiles code for the execution of block plugin
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
use Smarty\Exception;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Block Plugin Class
*
*/
class BlockCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* nesting level
*
* @var int
*/
private $nesting = 0;
/**
* Compiles code for the execution of block plugin
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of block plugin
* @param string $function PHP function name
*
* @return string compiled code
* @throws CompilerException
* @throws Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (!isset($tag[5]) || substr($tag, -5) !== 'close') {
$output = $this->compileOpeningTag($compiler, $args, $tag, $function);
} else {
$output = $this->compileClosingTag($compiler, $tag, $parameter, $function);
}
return $output;
}
/**
* Compiles code for the {$smarty.block.child} property
*
* @param Template $compiler compiler object
*
* @return string compiled code
* @throws CompilerException
*/
public function compileChild(\Smarty\Compiler\Template $compiler) {
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->trigger_template_error(
"'{\$smarty.block.child}' used outside {block} tags ",
$compiler->getParser()->lex->taglineno
);
}
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]['callsChild'] = true;
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
$output .= '$_smarty_tpl->getInheritance()->callChild($_smarty_tpl, $this' . ");\n";
$output .= "?>\n";
return $output;
}
/**
* Compiles code for the {$smarty.block.parent} property
*
* @param Template $compiler compiler object
*
* @return string compiled code
* @throws CompilerException
*/
public function compileParent(\Smarty\Compiler\Template $compiler) {
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->trigger_template_error(
"'{\$smarty.block.parent}' used outside {block} tags ",
$compiler->getParser()->lex->taglineno
);
}
$compiler->suppressNocacheProcessing = true;
$output = "<?php \n";
$output .= '$_smarty_tpl->getInheritance()->callParent($_smarty_tpl, $this' . ");\n";
$output .= "?>\n";
return $output;
}
/**
* Returns true if this block is cacheable.
*
* @param Smarty $smarty
* @param $function
*
* @return bool
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return $smarty->getBlockHandler($function)->isCacheable();
}
/**
* Returns the code used for the isset check
*
* @param string $tag tag name
* @param string $function base tag or method name
*
* @return string
*/
protected function getIsCallableCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getBlockHandler(" . var_export($function, true) . ")";
}
/**
* Returns the full code used to call the callback
*
* @param string $tag tag name
* @param string $function base tag or method name
*
* @return string
*/
protected function getFullCallbackCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getBlockHandler(" . var_export($function, true) . ")->handle";
}
/**
* @param Template $compiler
* @param array $args
* @param string|null $tag
* @param string|null $function
*
* @return string
*/
private function compileOpeningTag(Template $compiler, array $args, ?string $tag, ?string $function): string {
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->nesting++;
unset($_attr['nocache']);
$_params = 'array(' . implode(',', $this->formatParamsArray($_attr)) . ')';
if (!$this->blockIsCacheable($compiler->getSmarty(), $function)) {
$compiler->tag_nocache = true;
}
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, $tag, [$_params, $compiler->tag_nocache]);
// compile code
$output = "<?php \$_block_repeat=true;
if (!" . $this->getIsCallableCode($tag, $function) .") {\nthrow new \\Smarty\\Exception('block tag \'{$tag}\' not callable or registered');\n}\n
echo " . $this->getFullCallbackCode($tag, $function) . "({$_params}, null, \$_smarty_tpl, \$_block_repeat);
while (\$_block_repeat) {
ob_start();
?>";
return $output;
}
/**
* @param Template $compiler
* @param string $tag
* @param array $parameter
* @param string|null $function
*
* @return string
* @throws CompilerException
* @throws Exception
*/
private function compileClosingTag(Template $compiler, string $tag, array $parameter, ?string $function): string {
// closing tag of block plugin, restore nocache
$base_tag = substr($tag, 0, -5);
[$_params, $nocache_pushed] = $this->closeTag($compiler, $base_tag);
// compile code
if (!isset($parameter['modifier_list'])) {
$mod_pre = $mod_post = $mod_content = '';
$mod_content2 = 'ob_get_clean()';
} else {
$mod_content2 = "\$_block_content{$this->nesting}";
$mod_content = "\$_block_content{$this->nesting} = ob_get_clean();\n";
$mod_pre = "ob_start();\n";
$mod_post = 'echo ' . $compiler->compileModifier($parameter['modifier_list'], 'ob_get_clean()')
. ";\n";
}
$output = "<?php {$mod_content}\$_block_repeat=false;\n{$mod_pre}";
$callback = $this->getFullCallbackCode($base_tag, $function);
$output .= "echo {$callback}({$_params}, {$mod_content2}, \$_smarty_tpl, \$_block_repeat);\n";
$output .= "{$mod_post}}\n?>";
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return $output;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Smarty\Compile;
/**
* This class does extend all internal compile plugins
*
*/
interface CompilerInterface {
/**
* Compiles code for the tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string;
public function isCacheable(): bool;
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile;
class DefaultHandlerBlockCompiler extends BlockCompiler {
/**
* @inheritDoc
*/
protected function getIsCallableCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->hasPlugin(" .
var_export($function, true) . ", 'block')";
}
/**
* @inheritDoc
*/
protected function getFullCallbackCode($tag, $function): string {
return "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" .
var_export($function, true) . ", 'block')";
}
/**
* @inheritDoc
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return true;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Smarty\Compile;
use Smarty\Compiler\Template;
class DefaultHandlerFunctionCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['_any'];
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of tag
* @param string $function name of function
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->getSmarty()->getRuntime('DefaultPluginHandler')->getCallback(" . var_export($function, true) .
",'function')($_params, \$_smarty_tpl)";
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
return "<?php echo {$output};?>\n";
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* Smarty Internal Plugin Compile Registered Function
* Compiles code for the execution of a registered function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compiler\Template;
use Smarty\FunctionHandler\AttributeFunctionHandlerInterface;
/**
* Smarty Internal Plugin Compile Registered Function Class
*/
class FunctionCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['_any'];
/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];
/**
* Compiles code for the execution of a registered function
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of tag
* @param string $function name of function
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if ($functionHandler = $compiler->getSmarty()->getFunctionHandler($function)) {
$attribute_overrides = [];
if ($functionHandler instanceof AttributeFunctionHandlerInterface) {
$attribute_overrides = $functionHandler->getSupportedAttributes();
}
// check and get attributes
$_attr = (new AttributeCompiler(
$attribute_overrides['required_attributes'] ?? $this->required_attributes,
$attribute_overrides['optional_attributes'] ?? $this->optional_attributes,
$attribute_overrides['shorttag_order'] ?? $this->shorttag_order,
$attribute_overrides['option_flags'] ?? $this->option_flags
))->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
$output = "\$_smarty_tpl->getSmarty()->getFunctionHandler(" . var_export($function, true) . ")";
$output .= "->handle($_params, \$_smarty_tpl)";
} else {
$compiler->trigger_template_error("unknown function '{$function}'", null, true);
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
return $output;
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
class BCPluginWrapper extends Base {
private $callback;
public function __construct($callback) {
$this->callback = $callback;
}
/**
* @inheritDoc
*/
public function compile($params, \Smarty\Compiler\Template $compiler) {
return call_user_func($this->callback, $params, $compiler);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
abstract class Base implements ModifierCompilerInterface {
/**
* Compiles code for the modifier
*
* @param array $params array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
abstract public function compile($params, \Smarty\Compiler\Template $compiler);
/**
* evaluate compiler parameter
*
* @param array $params parameter array as given to the compiler function
* @param integer $index array index of the parameter to convert
* @param mixed $default value to be returned if the parameter is not present
*
* @return mixed evaluated value of parameter or $default
* @throws Exception if parameter is not a literal (but an expression, variable, …)
* @author Rodney Rehm
*/
protected function literal_compiler_param($params, $index, $default = null)
{
// not set, go default
if (!isset($params[ $index ])) {
return $default;
}
// test if param is a literal
if (!preg_match('/^([\'"]?)[a-zA-Z0-9-]+(\\1)$/', $params[ $index ])) {
throw new Exception(
'$param[' . $index .
'] is not a literal and is thus not evaluatable at compile time'
);
}
$t = null;
eval("\$t = " . $params[ $index ] . ";");
return $t;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty cat modifier plugin
* Type: modifier
* Name: cat
* Date: Feb 24, 2003
* Purpose: catenate a value to a variable
* Input: string to catenate
* Example: {$var|cat:"foo"}
*
* @author Uwe Tews
*/
class CatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return '(' . implode(').(', $params) . ')';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_characters modifier plugin
* Type: modifier
* Name: count_characters
* Purpose: count the number of characters in a text
*
* @author Uwe Tews
*/
class CountCharactersModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ]) || $params[ 1 ] !== 'true') {
return 'preg_match_all(\'/[^\s]/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\',' . $params[ 0 ] . ', $tmp)';
}
return 'mb_strlen((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_paragraphs modifier plugin
* Type: modifier
* Name: count_paragraphs
* Purpose: count the number of paragraphs in a text
*
* @author Uwe Tews
*/
class CountParagraphsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// count \r or \n characters
return '(preg_match_all(\'#[\r\n]+#\', ' . $params[ 0 ] . ', $tmp)+1)';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_sentences modifier plugin
* Type: modifier
* Name: count_sentences
* Purpose: count the number of sentences in a text
*
* @author Uwe Tews
*/
class CountSentencesModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// find periods, question marks, exclamation marks with a word before but not after.
return 'preg_match_all("#\w[\.\?\!](\W|$)#S' . \Smarty\Smarty::$_UTF8_MODIFIER . '", ' . $params[ 0 ] . ', $tmp)';
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty count_words modifier plugin
* Type: modifier
* Name: count_words
* Purpose: count the number of words in a text
*
* @author Uwe Tews
*/
class CountWordsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
// expression taken from http://de.php.net/manual/en/function.str-word-count.php#85592
return 'preg_match_all(\'/\p{L}[\p{L}\p{Mn}\p{Pd}\\\'\x{2019}]*/' . \Smarty\Smarty::$_UTF8_MODIFIER . '\', ' .
$params[ 0 ] . ', $tmp)';
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty default modifier plugin
* Type: modifier
* Name: default
* Purpose: designate default value for empty variables
*
* @author Uwe Tews
*/
class DefaultModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$output = $params[ 0 ];
if (!isset($params[ 1 ])) {
$params[ 1 ] = "''";
}
array_shift($params);
foreach ($params as $param) {
$output = '(($tmp = ' . $output . ' ?? null)===null||$tmp===\'\' ? ' . $param . ' ?? null : $tmp)';
}
return $output;
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty empty modifier plugin
*/
class EmptyModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (count($params) !== 1) {
throw new CompilerException("Invalid number of arguments for empty. empty expects exactly 1 parameter.");
}
return 'empty(' . $params[0] . ')';
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
/**
* Smarty escape modifier plugin
* Type: modifier
* Name: escape
* Purpose: escape string for output
*
* @author Rodney Rehm
*/
class EscapeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
try {
$esc_type = $this->literal_compiler_param($params, 1, 'html');
$char_set = $this->literal_compiler_param($params, 2, \Smarty\Smarty::$_CHARSET);
$double_encode = $this->literal_compiler_param($params, 3, true);
if (!$char_set) {
$char_set = \Smarty\Smarty::$_CHARSET;
}
switch ($esc_type) {
case 'html':
case 'force':
// in case of auto-escaping, and without the 'force' option, no double-escaping
if ($compiler->getSmarty()->escape_html && $esc_type != 'force')
return $params[0];
// otherwise, escape the variable
return 'htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
var_export($double_encode, true) . ')';
// no break
case 'htmlall':
$compiler->setRawOutput(true);
return 'htmlentities(mb_convert_encoding((string)' . $params[ 0 ] . ', \'UTF-8\', ' .
var_export($char_set, true) . '), ENT_QUOTES, \'UTF-8\', ' .
var_export($double_encode, true) . ')';
// no break
case 'url':
$compiler->setRawOutput(true);
return 'rawurlencode((string)' . $params[ 0 ] . ')';
case 'urlpathinfo':
$compiler->setRawOutput(true);
return 'str_replace("%2F", "/", rawurlencode((string)' . $params[ 0 ] . '))';
case 'quotes':
$compiler->setRawOutput(true);
// escape unescaped single quotes
return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'", (string)' . $params[ 0 ] . ')';
case 'javascript':
$compiler->setRawOutput(true);
// escape quotes and backslashes, newlines, etc.
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
return 'strtr((string)' .
$params[ 0 ] .
', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r",
"\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S",
"`" => "\\\\`", "\${" => "\\\\\\$\\{"))';
}
} catch (Exception $e) {
// pass through to regular plugin fallback
}
return '$_smarty_tpl->getSmarty()->getModifierCallback(\'escape\')(' . join(', ', $params) . ')';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty from_charset modifier plugin
* Type: modifier
* Name: from_charset
* Purpose: convert character encoding from $charset to internal encoding
*
* @author Rodney Rehm
*/
class FromCharsetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[ 0 ] . ', "' . addslashes(\Smarty\Smarty::$_CHARSET) . '", ' . $params[ 1 ] . ')';
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty indent modifier plugin
* Type: modifier
* Name: indent
* Purpose: indent lines of text
*
* @author Uwe Tews
*/
class IndentModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = 4;
}
if (!isset($params[ 2 ])) {
$params[ 2 ] = "' '";
}
return 'preg_replace(\'!^!m\',str_repeat(' . $params[ 2 ] . ',' . $params[ 1 ] . '),' . $params[ 0 ] . ')';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty is_array modifier plugin
*/
class IsArrayModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (count($params) !== 1) {
throw new CompilerException("Invalid number of arguments for is_array. is_array expects exactly 1 parameter.");
}
return 'is_array(' . $params[0] . ')';
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\CompilerException;
/**
* Smarty isset modifier plugin
*/
class IssetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$params = array_filter($params, function($v) { return !empty($v); });
if (count($params) < 1) {
throw new CompilerException("Invalid number of arguments for isset. isset expects at least one parameter.");
}
$tests = [];
foreach ($params as $param) {
$tests[] = 'null !== (' . $param . ' ?? null)';
}
return '(' . implode(' && ', $tests) . ')';
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty json_encode modifier plugin
*/
class JsonEncodeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'json_encode(' . $params[0] . (isset($params[1]) ? ', (int) ' . $params[1] : '') . ')';
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty lower modifier plugin
* Type: modifier
* Name: lower
* Purpose: convert string to lowercase
*
* @author Monte Ohrt <monte at ohrt dot com>
* @author Uwe Tews
*/
class LowerModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'mb_strtolower((string) ' . $params[ 0 ] . ', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
interface ModifierCompilerInterface {
/**
* Compiles code for the modifier
*
* @param array $params array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($params, \Smarty\Compiler\Template $compiler);
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty nl2br modifier plugin
* Type: modifier
* Name: nl2br
* Purpose: insert HTML line breaks before all newlines in a string
*
*/
class Nl2brModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'nl2br((string) ' . $params[0] . ', (bool) ' . ($params[1] ?? true) . ')';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty noprint modifier plugin
* Type: modifier
* Name: noprint
* Purpose: return an empty string
*
* @author Uwe Tews
*/
class NoPrintModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return "''";
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
use Smarty\Exception;
/**
* Smarty raw modifier plugin
* Type: modifier
* Name: raw
* Purpose: when escaping is enabled by default, generates a raw output of a variable
*
* @author Amaury Bouchard
*/
class RawModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$compiler->setRawOutput(true);
return ($params[0]);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty round modifier plugin
* Type: modifier
* Name: round
* Purpose: Returns the rounded value of num to specified precision (number of digits after the decimal point)
*
*/
class RoundModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'round((float) ' . $params[0] . ', (int) ' . ($params[1] ?? 0) . ', (int) ' . ($params[2] ?? PHP_ROUND_HALF_UP) . ')';
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty str_repeat modifier plugin
* Type: modifier
* Name: str_repeat
* Purpose: returns string repeated times times
*
*/
class StrRepeatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'str_repeat((string) ' . $params[0] . ', (int) ' . $params[1] . ')';
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty string_format modifier plugin
* Type: modifier
* Name: string_format
* Purpose: format strings via sprintf
*
* @author Uwe Tews
*/
class StringFormatModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'sprintf(' . $params[ 1 ] . ',' . $params[ 0 ] . ')';
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strip modifier plugin
* Type: modifier
* Name: strip
* Purpose: Replace all repeated spaces, newlines, tabs
* with a single space or supplied replacement string.
* Example: {$var|strip} {$var|strip:"&nbsp;"}
* Date: September 25th, 2002
*
* @author Uwe Tews
*/
class StripModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = "' '";
}
return "preg_replace('!\s+!" . \Smarty\Smarty::$_UTF8_MODIFIER . "', {$params[1]},{$params[0]})";
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strip_tags modifier plugin
* Type: modifier
* Name: strip_tags
* Purpose: strip html tags from text
*
* @author Uwe Tews
*/
class StripTagsModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ]) || $params[ 1 ] === true || trim($params[ 1 ], '"') === 'true') {
return "preg_replace('!<[^>]*?>!', ' ', (string) {$params[0]})";
} else {
return 'strip_tags((string) ' . $params[ 0 ] . ')';
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty strlen modifier plugin
* Type: modifier
* Name: strlen
* Purpose: return the length of the given string
*
*/
class StrlenModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'strlen((string) ' . $params[0] . ')';
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty substr modifier plugin
*/
class SubstrModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'substr((string) ' . $params[0] . ', (int) ' . $params[1] .
(isset($params[2]) ? ', (int) ' . $params[2] : '') . ')';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty to_charset modifier plugin
* Type: modifier
* Name: to_charset
* Purpose: convert character encoding from internal encoding to $charset
*
* @author Rodney Rehm
*/
class ToCharsetModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = '"ISO-8859-1"';
}
return 'mb_convert_encoding(' . $params[ 0 ] . ', ' . $params[ 1 ] . ', "' . addslashes(\Smarty\Smarty::$_CHARSET) . '")';
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty unescape modifier plugin
* Type: modifier
* Name: unescape
* Purpose: unescape html entities
*
* @author Rodney Rehm
*/
class UnescapeModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
$esc_type = $this->literal_compiler_param($params, 1, 'html');
if (!isset($params[ 2 ])) {
$params[ 2 ] = '\'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\'';
}
switch ($esc_type) {
case 'entity':
case 'htmlall':
return 'html_entity_decode(mb_convert_encoding(' . $params[ 0 ] . ', ' . $params[ 2 ] . ', \'UTF-8\'), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ' . $params[ 2 ] . ')';
case 'html':
return 'htmlspecialchars_decode(' . $params[ 0 ] . ', ENT_QUOTES)';
case 'url':
return 'rawurldecode(' . $params[ 0 ] . ')';
default:
return $params[ 0 ];
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty upper modifier plugin
* Type: modifier
* Name: lower
* Purpose: convert string to uppercase
*
* @author Uwe Tews
*/
class UpperModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
return 'mb_strtoupper((string) ' . $params[ 0 ] . ' ?? \'\', \'' . addslashes(\Smarty\Smarty::$_CHARSET) . '\')';
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Smarty\Compile\Modifier;
/**
* Smarty wordwrap modifier plugin
* Type: modifier
* Name: wordwrap
* Purpose: wrap a string of text at a given length
*
* @author Uwe Tews
*/
class WordWrapModifierCompiler extends Base {
public function compile($params, \Smarty\Compiler\Template $compiler) {
if (!isset($params[ 1 ])) {
$params[ 1 ] = 80;
}
if (!isset($params[ 2 ])) {
$params[ 2 ] = '"\n"';
}
if (!isset($params[ 3 ])) {
$params[ 3 ] = 'false';
}
return 'smarty_mb_wordwrap(' . $params[ 0 ] . ',' . $params[ 1 ] . ',' . $params[ 2 ] . ',' . $params[ 3 ] . ')';
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Smarty Internal Plugin Compile Modifier
* Compiles code for modifier execution
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
/**
* Smarty Internal Plugin Compile Modifier Class
*
*/
class ModifierCompiler extends Base {
/**
* Compiles code for modifier execution
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$output = $parameter['value'];
// loop over list of modifiers
foreach ($parameter['modifierlist'] as $single_modifier) {
/* @var string $modifier */
$modifier = $single_modifier[0];
$modifier_params = array_values($single_modifier);
$modifier_params[0] = $output;
$params = implode(',', $modifier_params);
if (!is_object($compiler->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedModifier($modifier, $compiler)
) {
if ($handler = $compiler->getModifierCompiler($modifier)) {
$output = $handler->compile($modifier_params, $compiler);
} elseif ($compiler->getSmarty()->getModifierCallback($modifier)) {
$output = sprintf(
'$_smarty_tpl->getSmarty()->getModifierCallback(%s)(%s)',
var_export($modifier, true),
$params
);
} elseif ($callback = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)) {
$output = (new \Smarty\Compile\Modifier\BCPluginWrapper($callback))->compile($modifier_params, $compiler);
} elseif ($function = $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER)) {
if (!is_array($function)) {
$output = "{$function}({$params})";
} else {
$operator = is_object($function[0]) ? '->' : '::';
$output = $function[0] . $operator . $function[1] . '(' . $params . ')';
}
} else {
$compiler->trigger_template_error("unknown modifier '{$modifier}'", null, true);
}
}
}
return (string)$output;
}
/**
* Wether this class will be able to compile the given modifier.
* @param string $modifier
* @param Template $compiler
*
* @return bool
* @throws CompilerException
*/
public function canCompileForModifier(string $modifier, \Smarty\Compiler\Template $compiler): bool {
return $compiler->getModifierCompiler($modifier)
|| $compiler->getSmarty()->getModifierCallback($modifier)
|| $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIERCOMPILER)
|| $compiler->getPluginFromDefaultHandler($modifier, \Smarty\Smarty::PLUGIN_MODIFIER);
}
}

View File

@@ -0,0 +1,44 @@
<?php
/**
* Smarty Internal Plugin Compile Object Block Function
* Compiles code for registered objects as block function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
/**
* Smarty Internal Plugin Compile Object Block Function Class
*
*/
class ObjectMethodBlockCompiler extends BlockCompiler {
/**
* @inheritDoc
*/
protected function getIsCallableCode($tag, $function): string {
$callbackObject = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]";
return "(isset({$callbackObject}) && is_callable(array({$callbackObject}, '{$function}')))";
}
/**
* @inheritDoc
*/
protected function getFullCallbackCode($tag, $function): string {
$callbackObject = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]";
return "{$callbackObject}->{$function}";
}
/**
* @inheritDoc
*/
protected function blockIsCacheable(\Smarty\Smarty $smarty, $function): bool {
return true;
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* Smarty Internal Plugin Compile Object Function
* Compiles code for registered objects as function
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
/**
* Smarty Internal Plugin Compile Object Function Class
*
*/
class ObjectMethodCallCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the execution of function plugin
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
* @param string $tag name of function
* @param string $function name of method to call
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
$_assign = null;
if (isset($_attr['assign'])) {
$_assign = $_attr['assign'];
unset($_attr['assign']);
}
// method or property ?
if (is_callable([$compiler->getSmarty()->registered_objects[$tag][0], $function])) {
// convert attributes into parameter array string
if ($compiler->getSmarty()->registered_objects[$tag][2]) {
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)";
} else {
$_params = implode(',', $_attr);
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}({$_params})";
}
} else {
// object property
$output = "\$_smarty_tpl->getSmarty()->registered_objects['{$tag}'][0]->{$function}";
}
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
if (empty($_assign)) {
return "<?php echo {$output};?>\n";
} else {
return "<?php \$_smarty_tpl->assign({$_assign},{$output});?>\n";
}
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* Smarty Internal Plugin Compile Print Expression
* Compiles any tag which will output an expression or variable
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compiler\BaseCompiler;
/**
* Smarty Internal Plugin Compile Print Expression Class
*
*/
class PrintExpressionCompiler extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['assign'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $option_flags = ['nocache', 'nofilter'];
/**
* Compiles code for generating output from any expression
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = $parameter['value'];
// tag modifier
if (!empty($parameter['modifierlist'])) {
$output = $compiler->compileModifier($parameter['modifierlist'], $output);
}
if (isset($_attr['assign'])) {
// assign output to variable
return "<?php \$_smarty_tpl->assign({$_attr['assign']},{$output});?>";
} else {
// display value
if (!$_attr['nofilter']) {
// default modifier
if ($compiler->getSmarty()->getDefaultModifiers()) {
$modifierlist = [];
foreach ($compiler->getSmarty()->getDefaultModifiers() as $key => $single_default_modifier) {
preg_match_all(
'/(\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|:|[^:]+)/',
$single_default_modifier,
$mod_array
);
for ($i = 0, $count = count($mod_array[0]); $i < $count; $i++) {
if ($mod_array[0][$i] !== ':') {
$modifierlist[$key][] = $mod_array[0][$i];
}
}
}
$output = $compiler->compileModifier($modifierlist, $output);
}
if ($compiler->getTemplate()->getSmarty()->escape_html && !$compiler->isRawOutput()) {
$output = "htmlspecialchars((string) ({$output}), ENT_QUOTES, '" . addslashes(\Smarty\Smarty::$_CHARSET) . "')";
}
}
$output = "<?php echo {$output};?>\n";
$compiler->setRawOutput(false);
}
return $output;
}
}

View File

@@ -0,0 +1,134 @@
<?php
/**
* Smarty Internal Plugin Compile Special Smarty Variable
* Compiles the special $smarty variables
*
* @author Uwe Tews
*/
namespace Smarty\Compile;
use Smarty\Compile\Base;
use Smarty\Compile\Tag\Capture;
use Smarty\Compile\Tag\ForeachTag;
use Smarty\Compile\Tag\Section;
use Smarty\Compiler\Template;
use Smarty\CompilerException;
/**
* Smarty Internal Plugin Compile special Smarty Variable Class
*
*/
class SpecialVariableCompiler extends Base {
/**
* Compiles code for the special $smarty variables
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
* @param array $parameter
* @param null $tag
* @param null $function
*
* @return string compiled code
* @throws CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
$variable = smarty_strtolower_ascii($compiler->getId($_index[0]));
if ($variable === false) {
$compiler->trigger_template_error("special \$Smarty variable name index can not be variable", null, true);
}
if (!isset($compiler->getSmarty()->security_policy)
|| $compiler->getSmarty()->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)
) {
switch ($variable) {
case 'foreach':
return (new ForeachTag())->compileSpecialVariable($compiler, $_index);
case 'section':
return (new Section())->compileSpecialVariable($compiler, $_index);
case 'capture':
return (new Capture())->compileSpecialVariable($compiler, $_index);
case 'now':
return 'time()';
case 'cookies':
if (isset($compiler->getSmarty()->security_policy)
&& !$compiler->getSmarty()->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_COOKIE';
break;
case 'get':
case 'post':
case 'env':
case 'server':
case 'session':
case 'request':
if (isset($compiler->getSmarty()->security_policy)
&& !$compiler->getSmarty()->security_policy->allow_super_globals
) {
$compiler->trigger_template_error("(secure mode) super globals not permitted");
break;
}
$compiled_ref = '$_' . smarty_strtoupper_ascii($variable);
break;
case 'template':
return '$_smarty_tpl->template_resource';
case 'template_object':
if (isset($compiler->getSmarty()->security_policy)) {
$compiler->trigger_template_error("(secure mode) template_object not permitted");
break;
}
return '$_smarty_tpl';
case 'current_dir':
return '$_smarty_current_dir';
case 'version':
return "\\Smarty\\Smarty::SMARTY_VERSION";
case 'const':
if (isset($compiler->getSmarty()->security_policy)
&& !$compiler->getSmarty()->security_policy->allow_constants
) {
$compiler->trigger_template_error("(secure mode) constants not permitted");
break;
}
if (strpos($_index[1], '$') === false && strpos($_index[1], '\'') === false) {
return "(defined('{$_index[1]}') ? constant('{$_index[1]}') : null)";
} else {
return "(defined({$_index[1]}) ? constant({$_index[1]}) : null)";
}
// no break
case 'config':
if (isset($_index[2])) {
return "(is_array(\$tmp = \$_smarty_tpl->getConfigVariable($_index[1])) ? \$tmp[$_index[2]] : null)";
} else {
return "\$_smarty_tpl->getConfigVariable($_index[1])";
}
// no break
case 'ldelim':
return "\$_smarty_tpl->getLeftDelimiter()";
case 'rdelim':
return "\$_smarty_tpl->getRightDelimiter()";
default:
$compiler->trigger_template_error('$smarty.' . trim($_index[0], "'") . ' is not defined');
break;
}
if (isset($_index[1])) {
array_shift($_index);
foreach ($_index as $_ind) {
$compiled_ref = $compiled_ref . "[$_ind]";
}
}
return $compiled_ref;
}
return '';
}
}

View File

@@ -1,40 +1,46 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Append
* Compiles the {append} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Append Class
*
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Append extends Smarty_Internal_Compile_Assign
class Append extends Assign
{
/**
* @inheritdoc
*/
protected $optional_attributes = ['scope', 'index'];
/**
* Compiles code for the {append} tag
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
* @throws \Smarty\CompilerException
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
// the following must be assigned at runtime because it will be overwritten in parent class
$this->required_attributes = array('var', 'value');
$this->shorttag_order = array('var', 'value');
$this->optional_attributes = array('scope', 'index');
$this->mapCache = array();
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// map to compile assign attributes
if (isset($_attr[ 'index' ])) {
$_params[ 'smarty_internal_index' ] = '[' . $_attr[ 'index' ] . ']';

View File

@@ -0,0 +1,95 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Assign
* Compiles the {assign} tag
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Assign Class
*
*/
class Assign extends Base
{
/**
* @inheritdoc
*/
protected $required_attributes = ['var', 'value'];
/**
* @inheritdoc
*/
protected $optional_attributes = ['scope'];
/**
* @inheritdoc
*/
protected $shorttag_order = ['var', 'value'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = array('nocache', 'noscope');
/**
* Compiles code for the {assign} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
$_nocache = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_var = $compiler->getId($_attr[ 'var' ])) {
$_var = "'{$_var}'";
} else {
$_var = $_attr[ 'var' ];
}
if ($compiler->tag_nocache || $compiler->isNocacheActive()) {
$_nocache = true;
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($_attr[ 'var' ]);
}
// scope setup
if ($_attr[ 'noscope' ]) {
$_scope = -1;
} else {
$_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : null;
}
if (isset($parameter[ 'smarty_internal_index' ])) {
$output =
"<?php \$_tmp_array = \$_smarty_tpl->getValue({$_var}) ?? [];\n";
$output .= "if (!(is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess)) {\n";
$output .= "settype(\$_tmp_array, 'array');\n";
$output .= "}\n";
$output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
$output .= "\$_smarty_tpl->assign({$_var}, \$_tmp_array, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>";
} else {
$output = "<?php \$_smarty_tpl->assign({$_var}, {$_attr['value']}, " . var_export($_nocache, true) . ", " . var_export($_scope, true) . ");?>";
}
return $output;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
class BCPluginWrapper extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('_any');
private $callback;
public function __construct($callback, bool $cacheable = true) {
$this->callback = $callback;
$this->cacheable = $cacheable;
}
/**
* @inheritDoc
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty());
}
}

92
src/Compile/Tag/Block.php Normal file
View File

@@ -0,0 +1,92 @@
<?php
/**
* This file is part of Smarty.
*
* (c) 2015 Uwe Tews
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Smarty\Compile\Tag;
use Smarty\ParseTree\Template;
/**
* Smarty Internal Plugin Compile Block Class
*
* @author Uwe Tews <uwe.tews@googlemail.com>
*/
class Block extends Inheritance {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = ['hide', 'nocache'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['assign'];
/**
* Compiles code for the {block} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->_cache['blockNesting'] = 0;
}
if ($compiler->_cache['blockNesting'] === 0) {
// make sure that inheritance gets initialized in template code
$this->registerInit($compiler);
$this->option_flags = ['hide', 'nocache', 'append', 'prepend'];
} else {
$this->option_flags = ['hide', 'nocache'];
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
++$compiler->_cache['blockNesting'];
$_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(mt_rand(), true));
$this->openTag(
$compiler,
'block',
[
$_attr, $compiler->tag_nocache, $compiler->getParser()->current_buffer,
$compiler->getTemplate()->getCompiled()->getNocacheCode(), $_className
]
);
$compiler->getParser()->current_buffer = new Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
$compiler->suppressNocacheProcessing = true;
return '';
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\ParseTree\Template;
/**
* Smarty Internal Plugin Compile BlockClose Class
*/
class BlockClose extends Inheritance {
/**
* Compiles code for the {/block} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool true
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$_attr, $_nocache, $_buffer, $_has_nocache_code, $_className] = $this->closeTag($compiler, ['block']);
$_block = [];
if (isset($compiler->_cache['blockParams'])) {
$_block = $compiler->_cache['blockParams'][$compiler->_cache['blockNesting']] ?? [];
unset($compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]);
}
$_name = $_attr['name'];
$_assign = $_attr['assign'] ?? null;
unset($_attr[ 'assign' ], $_attr[ 'name' ]);
foreach ($_attr as $name => $stat) {
if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat !== 'false')) {
$_block[ $name ] = 'true';
}
}
// get compiled block code
$_functionCode = $compiler->getParser()->current_buffer;
// setup buffer for template function code
$compiler->getParser()->current_buffer = new Template();
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {block {$_name}} ") . "\n";
$output .= "class {$_className} extends \\Smarty\\Runtime\\Block\n";
$output .= "{\n";
foreach ($_block as $property => $value) {
$output .= "public \${$property} = " . var_export($value, true) . ";\n";
}
$output .= "public function callBlock(\\Smarty\\Template \$_smarty_tpl) {\n";
$output .= (new \Smarty\Compiler\CodeFrame($compiler->getTemplate()))->insertLocalVariables();
if ($compiler->getTemplate()->getCompiled()->getNocacheCode()) {
$output .= "\$_smarty_tpl->getCached()->hashes['{$compiler->getTemplate()->getCompiled()->nocache_hash}'] = true;\n";
}
if (isset($_assign)) {
$output .= "ob_start();\n";
}
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php\n";
if (isset($_assign)) {
$output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean());\n";
}
$output .= "}\n";
$output .= "}\n";
$output .= $compiler->cStyleComment(" {/block {$_name}} ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->blockOrFunctionCode .= $compiler->getParser()->current_buffer->to_smarty_php($compiler->getParser());
$compiler->getParser()->current_buffer = new Template();
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($_has_nocache_code);
$compiler->tag_nocache = $_nocache;
$compiler->getParser()->current_buffer = $_buffer;
$output = "<?php \n";
if ($compiler->_cache['blockNesting'] === 1) {
$output .= "\$_smarty_tpl->getInheritance()->instanceBlock(\$_smarty_tpl, '$_className', $_name);\n";
} else {
$output .= "\$_smarty_tpl->getInheritance()->instanceBlock(\$_smarty_tpl, '$_className', $_name, \$this->tplIndex);\n";
}
$output .= "?>\n";
--$compiler->_cache['blockNesting'];
if ($compiler->_cache['blockNesting'] === 0) {
unset($compiler->_cache['blockNesting']);
}
$compiler->suppressNocacheProcessing = true;
return $output;
}
}

View File

@@ -0,0 +1,123 @@
<?php
/**
* Smarty Internal Plugin Compile Break
* Compiles the {break} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Break Class
*
*/
class BreakTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['levels'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['levels'];
/**
* Tag name may be overloaded by ContinueTag
*
* @var string
*/
protected $tag = 'break';
/**
* Compiles code for the {break} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$levels, $foreachLevels] = $this->checkLevels($args, $compiler);
$output = "<?php ";
if ($foreachLevels > 0 && $this->tag === 'continue') {
$foreachLevels--;
}
if ($foreachLevels > 0) {
/* @var ForeachTag $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore($foreachLevels);
}
$output .= "{$this->tag} {$levels};?>";
return $output;
}
/**
* check attributes and return array of break and foreach levels
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return array
* @throws \Smarty\CompilerException
*/
public function checkLevels($args, \Smarty\Compiler\Template $compiler) {
static $_is_loopy = ['for' => true, 'foreach' => true, 'while' => true, 'section' => true];
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
if (isset($_attr['levels'])) {
if (!is_numeric($_attr['levels'])) {
$compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
}
$levels = $_attr['levels'];
} else {
$levels = 1;
}
$level_count = $levels;
$tagStack = $compiler->getTagStack();
$stack_count = count($tagStack) - 1;
$foreachLevels = 0;
$lastTag = '';
while ($level_count > 0 && $stack_count >= 0) {
if (isset($_is_loopy[$tagStack[$stack_count][0]])) {
$lastTag = $tagStack[$stack_count][0];
if ($level_count === 0) {
break;
}
$level_count--;
if ($tagStack[$stack_count][0] === 'foreach') {
$foreachLevels++;
}
}
$stack_count--;
}
if ($level_count !== 0) {
$compiler->trigger_template_error("cannot {$this->tag} {$levels} level(s)", null, true);
}
if ($lastTag === 'foreach' && $this->tag === 'break' && $foreachLevels > 0) {
$foreachLevels--;
}
return [$levels, $foreachLevels];
}
}

81
src/Compile/Tag/Call.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
/**
* Smarty Internal Plugin Compile Function_Call
* Compiles the calls of user defined tags defined by {function}
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Function_Call Class
*/
class Call extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['_any'];
/**
* Compiles the calls of user defined tags defined by {function}
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr['assign'];
}
//$_name = trim($_attr['name'], "''");
$_name = $_attr['name'];
unset($_attr['name'], $_attr['assign'], $_attr['nocache']);
// set flag (compiled code of {function} must be included in cache file
if (!$compiler->getTemplate()->caching || $compiler->isNocacheActive() || $compiler->tag_nocache) {
$_nocache = 'true';
} else {
$_nocache = 'false';
}
$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';
//$compiler->suppressNocacheProcessing = true;
// was there an assign attribute
if (isset($_assign)) {
$_output =
"<?php ob_start();\n\$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n";
} else {
$_output =
"<?php \$_smarty_tpl->getSmarty()->getRuntime('TplFunction')->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n";
}
return $_output;
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Capture Class
*
*/
class Capture extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['name', 'assign', 'append'];
/**
* Compiles code for the {$smarty.capture.xxx}
*
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public static function compileSpecialVariable(
\Smarty\Compiler\Template $compiler,
$parameter = null
) {
return '$_smarty_tpl->getSmarty()->getRuntime(\'Capture\')->getBuffer($_smarty_tpl' .
(isset($parameter[1]) ? ", {$parameter[ 1 ]})" : ')');
}
/**
* Compiles code for the {capture} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param null $parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$buffer = $_attr['name'] ?? "'default'";
$assign = $_attr['assign'] ?? 'null';
$append = $_attr['append'] ?? 'null';
$compiler->_cache['capture_stack'][] = $compiler->tag_nocache;
if ($compiler->tag_nocache) {
// push a virtual {nocache} tag onto the stack.
$compiler->openTag('nocache');
}
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
}
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Smarty Internal Plugin Compile Capture
* Compiles the {capture} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Captureclose Class
*
*/
class CaptureClose extends Base {
/**
* Compiles code for the {/capture} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param null $parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if (array_pop($compiler->_cache['capture_stack'])) {
// pop the virtual {nocache} tag from the stack.
$compiler->closeTag('nocache');
$compiler->tag_nocache = true;
}
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->close(\$_smarty_tpl);?>";
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* Smarty Internal Plugin Compile Config Load
* Compiles the {config load} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Smarty;
/**
* Smarty Internal Plugin Compile Config Load Class
*
*/
class ConfigLoad extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['file', 'section'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['section'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $option_flags = [];
/**
* Compiles code for the {config_load} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
$conf_file = $_attr['file'];
$section = $_attr['section'] ?? 'null';
// create config object
return "<?php\n\$_smarty_tpl->configLoad({$conf_file}, {$section});\n?>\n";
}
}

View File

@@ -0,0 +1,27 @@
<?php
/**
* Smarty Internal Plugin Compile Continue
* Compiles the {continue} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Continue Class
*
*/
class ContinueTag extends BreakTag {
/**
* Tag name
*
* @var string
*/
protected $tag = 'continue';
}

45
src/Compile/Tag/Debug.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
/**
* Smarty Internal Plugin Compile Debug
* Compiles the {debug} tag.
* It opens a window the the Smarty Debugging Console.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Debug Class
*
*/
class Debug extends Base {
/**
* Compiles code for the {debug} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes, may trigger errors
$this->getAttributes($compiler, $args);
// compile always as nocache
$compiler->tag_nocache = true;
// display debug template
$_output =
"<?php \$_smarty_debug = new \\Smarty\\Debug;\n \$_smarty_debug->display_debug(\$_smarty_tpl);\n";
$_output .= "unset(\$_smarty_debug);\n?>";
return $_output;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile ElseIf Class
*
*/
class ElseIfTag extends Base {
/**
* Compiles code for the {elseif} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'elseif']);
if (!isset($parameter['if condition'])) {
$compiler->trigger_template_error('missing elseif condition', null, true);
}
$assignCode = '';
$var = '';
if (is_array($parameter['if condition'])) {
$condition_by_assign = true;
if (is_array($parameter['if condition']['var'])) {
$var = $parameter['if condition']['var']['var'];
} else {
$var = $parameter['if condition']['var'];
}
if ($compiler->isNocacheActive()) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($var);
}
$prefixVar = $compiler->getNewPrefixVariable();
$assignCode = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n";
$assignCompiler = new Assign();
$assignAttr = [];
$assignAttr[]['value'] = $prefixVar;
if (is_array($parameter['if condition']['var'])) {
$assignAttr[]['var'] = $parameter['if condition']['var']['var'];
$assignCode .= $assignCompiler->compile(
$assignAttr,
$compiler,
['smarty_internal_index' => $parameter['if condition']['var']['smarty_internal_index']]
);
} else {
$assignAttr[]['var'] = $parameter['if condition']['var'];
$assignCode .= $assignCompiler->compile($assignAttr, $compiler, []);
}
} else {
$condition_by_assign = false;
}
$prefixCode = $compiler->getPrefixCode();
if (empty($prefixCode)) {
if ($condition_by_assign) {
$this->openTag($compiler, 'elseif', [$nesting + 1, $compiler->tag_nocache]);
$_output = $compiler->appendCode("<?php } else {\n?>", $assignCode);
return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
} else {
$this->openTag($compiler, 'elseif', [$nesting, $nocache_pushed]);
return "<?php } elseif ({$parameter['if condition']}) {?>";
}
} else {
$_output = $compiler->appendCode("<?php } else {\n?>", $prefixCode);
$this->openTag($compiler, 'elseif', [$nesting + 1, $nocache_pushed]);
if ($condition_by_assign) {
$_output = $compiler->appendCode($_output, $assignCode);
return $compiler->appendCode($_output, "<?php if ({$prefixVar}) {?>");
} else {
return $compiler->appendCode($_output, "<?php if ({$parameter['if condition']}) {?>");
}
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Else Class
*
*/
class ElseTag extends Base {
/**
* Compiles code for the {else} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $compiler->tag_nocache] = $this->closeTag($compiler, ['if', 'elseif']);
$this->openTag($compiler, 'else', [$nesting, $compiler->tag_nocache]);
return '<?php } else { ?>';
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* Smarty Internal Plugin Compile Eval
* Compiles the {eval} tag.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Eval Class
*
*/
class EvalTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $required_attributes = ['var'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $optional_attributes = ['assign'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
public $shorttag_order = ['var', 'assign'];
/**
* Compiles code for the {eval} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
$_assign = $_attr['assign'];
}
// create template object
$_output =
"\$_template = new \\Smarty\\Template('eval:'.{$_attr[ 'var' ]}, \$_smarty_tpl->getSmarty(), \$_smarty_tpl);";
//was there an assign attribute?
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign($_assign,\$_template->fetch());";
} else {
$_output .= 'echo $_template->fetch();';
}
return "<?php $_output ?>";
}
}

View File

@@ -0,0 +1,87 @@
<?php
/**
* Smarty Internal Plugin Compile extend
* Compiles the {extends} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile extend Class
*
*/
class ExtendsTag extends Inheritance {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['file'];
/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected $optional_attributes = [];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['file'];
/**
* Compiles code for the {extends} tag extends: resource
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', $compiler->getParser()->lex->line - 1);
}
if (strpos($_attr['file'], '$_tmp') !== false) {
$compiler->trigger_template_error('illegal value for file attribute', $compiler->getParser()->lex->line - 1);
}
// add code to initialize inheritance
$this->registerInit($compiler, true);
$this->compileEndChild($compiler, $_attr['file']);
return '';
}
/**
* Add code for inheritance endChild() method to end of template
*
* @param \Smarty\Compiler\Template $compiler
* @param null|string $template optional inheritance parent template
*
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
private function compileEndChild(\Smarty\Compiler\Template $compiler, $template = null) {
$compiler->getParser()->template_postfix[] = new \Smarty\ParseTree\Tag(
$compiler->getParser(),
'<?php $_smarty_tpl->getInheritance()->endChild($_smarty_tpl' .
(isset($template) ? ", {$template}, \$_smarty_current_dir" : '') . ");\n?>"
);
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Smarty Internal Plugin Compile For
* Compiles the {for} {forelse} {/for} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Forclose Class
*
*/
class ForClose extends Base {
/**
* Compiles code for the {/for} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['for', 'forelse']);
$output = "<?php }\n";
if ($openTag !== 'forelse') {
$output .= "}\n";
}
$output .= "?>";
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return $output;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Forelse Class
*
*/
class ForElse extends Base {
/**
* Compiles code for the {forelse} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$tagName, $nocache_pushed] = $this->closeTag($compiler, ['for']);
$this->openTag($compiler, 'forelse', ['forelse', $nocache_pushed]);
return "<?php }} else { ?>";
}
}

101
src/Compile/Tag/ForTag.php Normal file
View File

@@ -0,0 +1,101 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile For Class
*
*/
class ForTag extends Base {
/**
* Compiles code for the {for} tag
* Smarty supports two different syntax's:
* - {for $var in $array}
* For looping over arrays or iterators
* - {for $x=0; $x<$y; $x++}
* For general loops
* The parser is generating different sets of attribute by which this compiler can
* determine which syntax is used.
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
if ($parameter === 0) {
$this->required_attributes = ['start', 'to'];
$this->optional_attributes = ['max', 'step'];
} else {
$this->required_attributes = ['start', 'ifexp', 'var', 'step'];
$this->optional_attributes = [];
}
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = "<?php\n";
if ($parameter === 1) {
foreach ($_attr['start'] as $_statement) {
if (is_array($_statement['var'])) {
$var = $_statement['var']['var'];
$index = $_statement['var']['smarty_internal_index'];
} else {
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
}
if (is_array($_attr['var'])) {
$var = $_attr['var']['var'];
$index = $_attr['var']['smarty_internal_index'];
} else {
$var = $_attr['var'];
$index = '';
}
$output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
} else {
$_statement = $_attr['start'];
if (is_array($_statement['var'])) {
$var = $_statement['var']['var'];
$index = $_statement['var']['smarty_internal_index'];
} else {
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);";
if (isset($_attr['step'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;";
}
if (isset($_attr['max'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n";
}
$output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n";
$output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration === 1;";
$output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration === \$_smarty_tpl->tpl_vars[$var]->total;";
}
$output .= '?>';
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this for loop
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'for', ['for', $compiler->tag_nocache]);
return $output;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* Smarty Internal Plugin Compile Foreach
* Compiles the {foreach} {foreachelse} {/foreach} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Foreachclose Class
*
*/
class ForeachClose extends Base {
/**
* Compiles code for the {/foreach} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach', 'foreachelse']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
$output = "<?php\n";
if ($restore) {
$output .= "\$_smarty_tpl->setVariable('{$item}', {$localVariablePrefix}Backup);\n";
}
$output .= "}\n";
/* @var \Smarty\Compile\Tag\ForeachTag $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore(1);
$output .= "?>";
return $output;
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Foreachelse Class
*
*/
class ForeachElse extends Base {
/**
* Compiles code for the {foreachelse} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach']);
$this->openTag($compiler, 'foreachelse', ['foreachelse', $nocache_pushed, $localVariablePrefix, $item, false]);
$output = "<?php\n";
if ($restore) {
$output .= "\$_smarty_tpl->setVariable('{$item}', {$localVariablePrefix}Backup);\n";
}
$output .= "}\nif ({$localVariablePrefix}DoElse) {\n?>";
return $output;
}
}

View File

@@ -0,0 +1,206 @@
<?php
/**
* Smarty Internal Plugin Compile ForeachSection
* Shared methods for {foreach} {section} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile ForeachSection Class
*
*/
abstract class ForeachSection extends Base {
/**
* Name of this tag
*
* @var string
*/
protected $tagName = '';
/**
* Valid properties of $smarty.xxx variable
*
* @var array
*/
protected $nameProperties = [];
/**
* {section} tag has no item properties
*
* @var array
*/
protected $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
protected $isNamed = true;
/**
* @var array
*/
protected $matchResults = [];
/**
* Preg search pattern
*
* @var string
*/
private $propertyPreg = '';
/**
* Offsets in preg match result
*
* @var array
*/
private $resultOffsets = [];
/**
* Start offset
*
* @var int
*/
private $startOffset = 0;
/**
* Scan sources for used tag attributes
*
* @param array $attributes
* @param \Smarty\Compiler\Template $compiler
*
* @throws \Smarty\Exception
*/
protected function scanForProperties($attributes, \Smarty\Compiler\Template $compiler) {
$this->propertyPreg = '~(';
$this->startOffset = 1;
$this->resultOffsets = [];
$this->matchResults = ['named' => [], 'item' => []];
if (isset($attributes['name'])) {
$this->buildPropertyPreg(true, $attributes);
}
if (isset($this->itemProperties)) {
if ($this->isNamed) {
$this->propertyPreg .= '|';
}
$this->buildPropertyPreg(false, $attributes);
}
$this->propertyPreg .= ')\W~i';
// Template source
$this->matchTemplateSource($compiler);
// Parent template source
$this->matchParentTemplateSource($compiler);
}
/**
* Build property preg string
*
* @param bool $named
* @param array $attributes
*/
private function buildPropertyPreg($named, $attributes) {
if ($named) {
$this->resultOffsets['named'] = $this->startOffset = $this->startOffset + 3;
$this->propertyPreg .= "(([\$]smarty[.]{$this->tagName}[.]" .
($this->tagName === 'section' ? "|[\[]\s*" : '') .
"){$attributes['name']}[.](";
$properties = $this->nameProperties;
} else {
$this->resultOffsets['item'] = $this->startOffset = $this->startOffset + 2;
$this->propertyPreg .= "([\$]{$attributes['item']}[@](";
$properties = $this->itemProperties;
}
$propName = reset($properties);
while ($propName) {
$this->propertyPreg .= "{$propName}";
$propName = next($properties);
if ($propName) {
$this->propertyPreg .= '|';
}
}
$this->propertyPreg .= '))';
}
/**
* Find matches in source string
*
* @param string $source
*/
private function matchProperty($source) {
preg_match_all($this->propertyPreg, $source, $match);
foreach ($this->resultOffsets as $key => $offset) {
foreach ($match[$offset] as $m) {
if (!empty($m)) {
$this->matchResults[$key][smarty_strtolower_ascii($m)] = true;
}
}
}
}
/**
* Find matches in template source
*
* @param \Smarty\Compiler\Template $compiler
*/
private function matchTemplateSource(\Smarty\Compiler\Template $compiler) {
$this->matchProperty($compiler->getParser()->lex->data);
}
/**
* Find matches in all parent template source
*
* @param \Smarty\Compiler\Template $compiler
*
* @throws \Smarty\Exception
*/
private function matchParentTemplateSource(\Smarty\Compiler\Template $compiler) {
// search parent compiler template source
$nextCompiler = $compiler;
while ($nextCompiler !== $nextCompiler->getParentCompiler()) {
$nextCompiler = $nextCompiler->getParentCompiler();
if ($compiler !== $nextCompiler) {
// get template source
$_content = $nextCompiler->getTemplate()->getSource()->getContent();
if ($_content !== '') {
// run pre filter if required
$_content = $nextCompiler->getSmarty()->runPreFilters($_content, $nextCompiler->getTemplate());
$this->matchProperty($_content);
}
}
}
}
/**
* Compiles code for the {$smarty.foreach.xxx} or {$smarty.section.xxx}tag
*
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compileSpecialVariable(\Smarty\Compiler\Template $compiler, $parameter) {
$tag = smarty_strtolower_ascii(trim($parameter[0], '"\''));
$name = isset($parameter[1]) ? $compiler->getId($parameter[1]) : false;
if (!$name) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} name attribute", null, true);
}
$property = isset($parameter[2]) ? smarty_strtolower_ascii($compiler->getId($parameter[2])) : false;
if (!$property || !in_array($property, $this->nameProperties)) {
$compiler->trigger_template_error("missing or illegal \$smarty.{$tag} property attribute", null, true);
}
$tagVar = "'__smarty_{$tag}_{$name}'";
return "(\$_smarty_tpl->getValue({$tagVar})['{$property}'] ?? null)";
}
}

View File

@@ -0,0 +1,286 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Foreach Class
*
*/
class ForeachTag extends ForeachSection {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['from', 'item'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['name', 'key', 'properties'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['from', 'item', 'key', 'name'];
/**
* counter
*
* @var int
*/
private static $counter = 0;
/**
* Name of this tag
*
* @var string
*/
protected $tagName = 'foreach';
/**
* Valid properties of $smarty.foreach.name.xxx variable
*
* @var array
*/
protected $nameProperties = ['first', 'last', 'index', 'iteration', 'show', 'total'];
/**
* Valid properties of $item@xxx variable
*
* @var array
*/
protected $itemProperties = ['first', 'last', 'index', 'iteration', 'show', 'total', 'key'];
/**
* Flag if tag had name attribute
*
* @var bool
*/
protected $isNamed = false;
/**
* Compiles code for the {foreach} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$from = $_attr['from'];
$item = $compiler->getId($_attr['item']);
if ($item === false) {
$item = $this->getVariableName($_attr['item']);
}
$key = $name = null;
$attributes = ['item' => $item];
if (isset($_attr['key'])) {
$key = $compiler->getId($_attr['key']);
if ($key === false) {
$key = $this->getVariableName($_attr['key']);
}
$attributes['key'] = $key;
}
if (isset($_attr['name'])) {
$this->isNamed = true;
$name = $attributes['name'] = $compiler->getId($_attr['name']);
}
foreach ($attributes as $a => $v) {
if ($v === false) {
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
}
}
$fromName = $this->getVariableName($_attr['from']);
if ($fromName) {
foreach (['item', 'key'] as $a) {
if (isset($attributes[$a]) && $attributes[$a] === $fromName) {
$compiler->trigger_template_error(
"'{$a}' and 'from' may not have same variable name '{$fromName}'",
null,
true
);
}
}
}
$itemVar = "\$_smarty_tpl->getVariable('{$item}')";
$localVariablePrefix = '$foreach' . self::$counter++;
// search for used tag attributes
$itemAttr = [];
$namedAttr = [];
$this->scanForProperties($attributes, $compiler);
if (!empty($this->matchResults['item'])) {
$itemAttr = $this->matchResults['item'];
}
if (!empty($this->matchResults['named'])) {
$namedAttr = $this->matchResults['named'];
}
if (isset($_attr['properties']) && preg_match_all('/[\'](.*?)[\']/', $_attr['properties'], $match)) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->itemProperties)) {
$itemAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
if ($this->isNamed) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->nameProperties)) {
$nameAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
}
}
if (isset($itemAttr['first'])) {
$itemAttr['index'] = true;
}
if (isset($namedAttr['first'])) {
$namedAttr['index'] = true;
}
if (isset($namedAttr['last'])) {
$namedAttr['iteration'] = true;
$namedAttr['total'] = true;
}
if (isset($itemAttr['last'])) {
$itemAttr['iteration'] = true;
$itemAttr['total'] = true;
}
if (isset($namedAttr['show'])) {
$namedAttr['total'] = true;
}
if (isset($itemAttr['show'])) {
$itemAttr['total'] = true;
}
$keyTerm = '';
if (isset($attributes['key'])) {
$keyTerm = "\$_smarty_tpl->getVariable('{$key}')->value => ";
}
if (isset($itemAttr['key'])) {
$keyTerm = "{$itemVar}->key => ";
}
if ($this->isNamed) {
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
}
$needTotal = isset($itemAttr['total']);
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
// Register tag
$this->openTag(
$compiler,
'foreach',
['foreach', $compiler->tag_nocache, $localVariablePrefix, $item, !empty($itemAttr)]
);
// generate output code
$output = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->getSmarty()->getRuntime('Foreach')->init(\$_smarty_tpl, $from, " .
var_export($item, true);
if ($name || $needTotal || $key) {
$output .= ', ' . var_export($needTotal, true);
}
if ($name || $key) {
$output .= ', ' . var_export($key, true);
}
if ($name) {
$output .= ', ' . var_export($name, true) . ', ' . var_export($namedAttr, true);
}
$output .= ");\n";
if (isset($itemAttr['show'])) {
$output .= "{$itemVar}->show = ({$itemVar}->total > 0);\n";
}
if (isset($itemAttr['iteration'])) {
$output .= "{$itemVar}->iteration = 0;\n";
}
if (isset($itemAttr['index'])) {
$output .= "{$itemVar}->index = -1;\n";
}
$output .= "{$localVariablePrefix}DoElse = true;\n";
$output .= "foreach (\$_from ?? [] as {$keyTerm}{$itemVar}->value) {\n";
$output .= "{$localVariablePrefix}DoElse = false;\n";
if (isset($attributes['key']) && isset($itemAttr['key'])) {
$output .= "\$_smarty_tpl->assign('{$key}', {$itemVar}->key);\n";
}
if (isset($itemAttr['iteration'])) {
$output .= "{$itemVar}->iteration++;\n";
}
if (isset($itemAttr['index'])) {
$output .= "{$itemVar}->index++;\n";
}
if (isset($itemAttr['first'])) {
$output .= "{$itemVar}->first = !{$itemVar}->index;\n";
}
if (isset($itemAttr['last'])) {
$output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;\n";
}
if (isset($foreachVar)) {
if (isset($namedAttr['iteration'])) {
$output .= "{$foreachVar}->value['iteration']++;\n";
}
if (isset($namedAttr['index'])) {
$output .= "{$foreachVar}->value['index']++;\n";
}
if (isset($namedAttr['first'])) {
$output .= "{$foreachVar}->value['first'] = !{$foreachVar}->value['index'];\n";
}
if (isset($namedAttr['last'])) {
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] === {$foreachVar}->value['total'];\n";
}
}
if (!empty($itemAttr)) {
$output .= "{$localVariablePrefix}Backup = clone \$_smarty_tpl->getVariable('{$item}');\n";
}
$output .= '?>';
return $output;
}
/**
* Get variable name from string
*
* @param string $input
*
* @return bool|string
*/
private function getVariableName($input) {
if (preg_match('~^[$]_smarty_tpl->getValue\([\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*\]\)$~', $input, $match)) {
return $match[1];
}
return false;
}
/**
* Compiles code for to restore saved template variables
*
* @param int $levels number of levels to restore
*
* @return string compiled code
*/
public function compileRestore($levels) {
return "\$_smarty_tpl->getSmarty()->getRuntime('Foreach')->restore(\$_smarty_tpl, {$levels});";
}
}

View File

@@ -0,0 +1,164 @@
<?php
/**
* Smarty Internal Plugin Compile Function
* Compiles the {function} {/function} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Functionclose Class
*
*/
class FunctionClose extends Base {
/**
* Compiler object
*
* @var object
*/
private $compiler = null;
/**
* Compiles code for the {/function} tag
*
* @param array $args array with attributes from parser
* @param object|\Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, ['function']);
$_attr = $saved_data[0];
$_name = trim($_attr['name'], '\'"');
$parentCompiler = $compiler->getParentCompiler();
$parentCompiler->tpl_function[$_name]['compiled_filepath'] =
$parentCompiler->getTemplate()->getCompiled()->filepath;
$parentCompiler->tpl_function[$_name]['uid'] = $compiler->getTemplate()->getSource()->uid;
$_parameter = $_attr;
unset($_parameter['name']);
// default parameter
$_paramsArray = $this->formatParamsArray($_attr);
$_paramsCode = (new \Smarty\Compiler\CodeFrame($compiler->getTemplate()))->insertLocalVariables();
if (!empty($_paramsArray)) {
$_params = 'array(' . implode(',', $_paramsArray) . ')';
$_paramsCode .= "\$params = array_merge($_params, \$params);\n";
}
$_functionCode = $compiler->getParser()->current_buffer;
// setup buffer for template function code
$compiler->getParser()->current_buffer = new \Smarty\ParseTree\Template();
$_funcName = "smarty_template_function_{$_name}_{$compiler->getTemplate()->getCompiled()->nocache_hash}";
$_funcNameCaching = $_funcName . '_nocache';
if ($compiler->getTemplate()->getCompiled()->getNocacheCode()) {
$parentCompiler->tpl_function[$_name]['call_name_caching'] = $_funcNameCaching;
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {$_funcNameCaching} ") . "\n";
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
$output .= "function {$_funcNameCaching} (\\Smarty\\Template \$_smarty_tpl,\$params) {\n";
$output .= "ob_start();\n";
$output .= "\$_smarty_tpl->getCompiled()->setNocacheCode(true);\n";
$output .= $_paramsCode;
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->assign(\$key, \$value);\n}\n";
$output .= "\$params = var_export(\$params, true);\n";
$output .= "echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->pushStack();\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->assign(\\\$key, \\\$value);\n}\n?>";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";?>";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/<?php ";
$output .= "\\\$_smarty_tpl->popStack();?>\n";
$output .= "/*/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%*/\";\n?>";
$output .= "<?php echo str_replace('{$compiler->getTemplate()->getCompiled()->nocache_hash}', \$_smarty_tpl->getCompiled()->nocache_hash ?? '', ob_get_clean());\n";
$output .= "}\n}\n";
$output .= $compiler->cStyleComment("/ {$_funcName}_nocache ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$_functionCode = new \Smarty\ParseTree\Tag(
$compiler->getParser(),
preg_replace_callback(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->getTemplate()->getCompiled()->nocache_hash}%%\*\/';(\?>\n)?)/",
[$this, 'removeNocache'],
$_functionCode->to_smarty_php($compiler->getParser())
)
);
}
$parentCompiler->tpl_function[$_name]['call_name'] = $_funcName;
$output = "<?php\n";
$output .= $compiler->cStyleComment(" {$_funcName} ") . "\n";
$output .= "if (!function_exists('{$_funcName}')) {\n";
$output .= "function {$_funcName}(\\Smarty\\Template \$_smarty_tpl,\$params) {\n";
$output .= $_paramsCode;
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->assign(\$key, \$value);\n}\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$compiler->getParser()->current_buffer->append_subtree($compiler->getParser(), $_functionCode);
$output = "<?php\n}}\n";
$output .= $compiler->cStyleComment("/ {$_funcName} ") . "\n\n";
$output .= "?>\n";
$compiler->getParser()->current_buffer->append_subtree(
$compiler->getParser(),
new \Smarty\ParseTree\Tag(
$compiler->getParser(),
$output
)
);
$parentCompiler->blockOrFunctionCode .= $compiler->getParser()->current_buffer->to_smarty_php($compiler->getParser());
// restore old buffer
$compiler->getParser()->current_buffer = $saved_data[1];
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($saved_data[2]);
$compiler->getTemplate()->caching = $saved_data[3];
return '';
}
/**
* Remove nocache code
*
* @param $match
*
* @return string
*/
public function removeNocache($match) {
$hash = $this->compiler->getTemplate()->getCompiled()->nocache_hash;
$code =
preg_replace(
"/((<\?php )?echo '\/\*%%SmartyNocache:{$hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$hash}%%\*\/';(\?>\n)?)/",
'',
$match[0]
);
return str_replace(['\\\'', '\\\\\''], ['\'', '\\\''], $code);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Function Class
*
*/
class FunctionTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['name'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the {function} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
unset($_attr['nocache']);
$_name = trim($_attr['name'], '\'"');
if (!preg_match('/^[a-zA-Z0-9_\x80-\xff]+$/', $_name)) {
$compiler->trigger_template_error("Function name contains invalid characters: {$_name}", null, true);
}
$compiler->getParentCompiler()->tpl_function[$_name] = [];
$save = [
$_attr, $compiler->getParser()->current_buffer, $compiler->getTemplate()->getCompiled()->getNocacheCode(),
$compiler->getTemplate()->caching,
];
$this->openTag($compiler, 'function', $save);
// Init temporary context
$compiler->getParser()->current_buffer = new \Smarty\ParseTree\Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
return '';
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Smarty Internal Plugin Compile If
* Compiles the {if} {else} {elseif} {/if} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Ifclose Class
*
*/
class IfClose extends Base {
/**
* Compiles code for the {/if} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'else', 'elseif']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
$tmp = '';
for ($i = 0; $i < $nesting; $i++) {
$tmp .= '}';
}
return "<?php {$tmp}?>";
}
}

70
src/Compile/Tag/IfTag.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile If Class
*
*/
class IfTag extends Base {
/**
* Compiles code for the {if} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'if', [1, $compiler->tag_nocache]);
if (!isset($parameter['if condition'])) {
$compiler->trigger_template_error('missing if condition', null, true);
}
if (is_array($parameter['if condition'])) {
if (is_array($parameter['if condition']['var'])) {
$var = $parameter['if condition']['var']['var'];
} else {
$var = $parameter['if condition']['var'];
}
if ($compiler->isNocacheActive()) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($var);
}
$prefixVar = $compiler->getNewPrefixVariable();
$_output = "<?php {$prefixVar} = {$parameter[ 'if condition' ][ 'value' ]};?>\n";
$assignAttr = [];
$assignAttr[]['value'] = $prefixVar;
$assignCompiler = new Assign();
if (is_array($parameter['if condition']['var'])) {
$assignAttr[]['var'] = $parameter['if condition']['var']['var'];
$_output .= $assignCompiler->compile(
$assignAttr,
$compiler,
['smarty_internal_index' => $parameter['if condition']['var']['smarty_internal_index']]
);
} else {
$assignAttr[]['var'] = $parameter['if condition']['var'];
$_output .= $assignCompiler->compile($assignAttr, $compiler, []);
}
$_output .= "<?php if ({$prefixVar}) {?>";
return $_output;
} else {
return "<?php if ({$parameter['if condition']}) {?>";
}
}
}

View File

@@ -0,0 +1,189 @@
<?php
/**
* Smarty Internal Plugin Compile Include
* Compiles the {include} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
use Smarty\Compiler\Template;
use Smarty\Data;
use Smarty\Smarty;
use Smarty\Template\Compiled;
/**
* Smarty Internal Plugin Compile Include Class
*
*/
class IncludeTag extends Base {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $required_attributes = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $shorttag_order = ['file'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $option_flags = ['nocache', 'inline', 'caching'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BaseCompiler
*/
protected $optional_attributes = ['_any'];
/**
* Compiles code for the {include} tag
*
* @param array $args array with attributes from parser
* @param Template $compiler compiler object
*
* @return string
* @throws \Exception
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$uid = $t_hash = null;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$fullResourceName = $source_resource = $_attr['file'];
$variable_template = false;
// parse resource_name
if (preg_match('/^([\'"])(([A-Za-z0-9_\-]{2,})[:])?(([^$()]+)|(.+))\1$/', $source_resource, $match)) {
$type = !empty($match[3]) ? $match[3] : $compiler->getTemplate()->getSmarty()->default_resource_type;
$name = !empty($match[5]) ? $match[5] : $match[6];
$handler = \Smarty\Resource\BasePlugin::load($compiler->getSmarty(), $type);
if ($handler->recompiled) {
$variable_template = true;
}
if (!$variable_template) {
if ($type !== 'string') {
$fullResourceName = "{$type}:{$name}";
$compiled = $compiler->getParentCompiler()->getTemplate()->getCompiled();
if (isset($compiled->includes[$fullResourceName])) {
$compiled->includes[$fullResourceName]++;
} else {
if ("{$compiler->getTemplate()->getSource()->type}:{$compiler->getTemplate()->getSource()->name}" ==
$fullResourceName
) {
// recursive call of current template
$compiled->includes[$fullResourceName] = 2;
} else {
$compiled->includes[$fullResourceName] = 1;
}
}
$fullResourceName = $match[1] . $fullResourceName . $match[1];
}
}
}
// scope setup
$_scope = isset($_attr['scope']) ? $this->convertScope($_attr['scope']) : 0;
// assume caching is off
$_caching = Smarty::CACHING_OFF;
// caching was on and {include} is not in nocache mode
if ($compiler->getTemplate()->caching && !$compiler->isNocacheActive()) {
$_caching = \Smarty\Template::CACHING_NOCACHE_CODE;
}
/*
* if the {include} tag provides individual parameter for caching or compile_id
* the subtemplate must not be included into the common cache file and is treated like
* a call in nocache mode.
*
*/
$call_nocache = $compiler->isNocacheActive();
if ($_attr['nocache'] !== true && $_attr['caching']) {
$_caching = $_new_caching = (int)$_attr['caching'];
$call_nocache = true;
} else {
$_new_caching = Smarty::CACHING_LIFETIME_CURRENT;
}
if (isset($_attr['cache_lifetime'])) {
$_cache_lifetime = $_attr['cache_lifetime'];
$call_nocache = true;
$_caching = $_new_caching;
} else {
$_cache_lifetime = '$_smarty_tpl->cache_lifetime';
}
if (isset($_attr['cache_id'])) {
$_cache_id = $_attr['cache_id'];
$call_nocache = true;
$_caching = $_new_caching;
} else {
$_cache_id = '$_smarty_tpl->cache_id';
}
// assign attribute
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed
if ($_assign = $compiler->getId($_attr['assign'])) {
$_assign = "'{$_assign}'";
if ($call_nocache) {
// create nocache var to make it know for further compiling
$compiler->setNocacheInVariable($_attr['assign']);
}
} else {
$_assign = $_attr['assign'];
}
}
$has_compiled_template = false;
// delete {include} standard attributes
unset($_attr['file'], $_attr['assign'], $_attr['cache_id'], $_attr['cache_lifetime'], $_attr['nocache'], $_attr['caching'], $_attr['scope'], $_attr['inline']);
// remaining attributes must be assigned as smarty variable
$_vars = 'array()';
if (!empty($_attr)) {
$_pairs = [];
// create variables
foreach ($_attr as $key => $value) {
$_pairs[] = "'$key'=>$value";
}
$_vars = 'array(' . join(',', $_pairs) . ')';
}
if ($call_nocache) {
$compiler->tag_nocache = true;
}
$_output = "<?php ";
// was there an assign attribute
if (isset($_assign)) {
$_output .= "ob_start();\n";
}
$_output .= "\$_smarty_tpl->renderSubTemplate({$fullResourceName}, $_cache_id, \$_smarty_tpl->compile_id, " .
"$_caching, $_cache_lifetime, $_vars, (int) {$_scope}, \$_smarty_current_dir);\n";
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n";
}
$_output .= "?>";
return $_output;
}
}

View File

@@ -1,44 +1,49 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Shared Inheritance
* Shared methods for {extends} and {block} tags
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Shared Inheritance Class
*
* @package Smarty
* @subpackage Compiler
*/
class Smarty_Internal_Compile_Shared_Inheritance extends Smarty_Internal_CompileBase
abstract class Inheritance extends Base
{
/**
* Compile inheritance initialization code as prefix
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param \Smarty\Compiler\Template $compiler
* @param bool|false $initChildSequence if true force child template
*/
public static function postCompile(Smarty_Internal_TemplateCompilerBase $compiler, $initChildSequence = false)
public static function postCompile(\Smarty\Compiler\Template $compiler, $initChildSequence = false)
{
$compiler->prefixCompiledCode .= "<?php \$_smarty_tpl->_loadInheritance();\n\$_smarty_tpl->inheritance->init(\$_smarty_tpl, " .
$compiler->prefixCompiledCode .= "<?php \$_smarty_tpl->getInheritance()->init(\$_smarty_tpl, " .
var_export($initChildSequence, true) . ");\n?>\n";
}
/**
* Register post compile callback to compile inheritance initialization code
*
* @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param \Smarty\Compiler\Template $compiler
* @param bool|false $initChildSequence if true force child template
*/
public function registerInit(Smarty_Internal_TemplateCompilerBase $compiler, $initChildSequence = false)
public function registerInit(\Smarty\Compiler\Template $compiler, $initChildSequence = false)
{
if ($initChildSequence || !isset($compiler->_cache[ 'inheritanceInit' ])) {
$compiler->registerPostCompileCallback(
array('Smarty_Internal_Compile_Shared_Inheritance', 'postCompile'),
array(self::class, 'postCompile'),
array($initChildSequence),
'inheritanceInit',
$initChildSequence

View File

@@ -0,0 +1,41 @@
<?php
/**
* Smarty Internal Plugin Compile Ldelim
* Compiles the {ldelim} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Ldelim Class
*
*/
class Ldelim extends Base {
/**
* Compiles code for the {ldelim} tag
* This tag does output the left delimiter
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
$compiler->trigger_template_error('nocache option not allowed', null, true);
}
return $compiler->getTemplate()->getLeftDelimiter();
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Nocache Class
*
*/
class Nocache extends Base {
/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = [];
/**
* Compiles code for the {nocache} tag
* This tag does not generate compiled output. It only sets a compiler flag.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->openTag($compiler, 'nocache');
return '';
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Smarty Internal Plugin Compile Nocache
* Compiles the {nocache} {/nocache} tags.
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Nocacheclose Class
*
*/
class NocacheClose extends Base {
/**
* Compiles code for the {/nocache} tag
* This tag does not generate compiled output. It only sets a compiler flag.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->closeTag($compiler, ['nocache']);
return '';
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* Smarty Internal Plugin Compile Rdelim
* Compiles the {rdelim} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Rdelim Class
*
*/
class Rdelim extends Ldelim {
/**
* Compiles code for the {rdelim} tag
* This tag does output the right delimiter.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
parent::compile($args, $compiler);
return $compiler->getTemplate()->getRightDelimiter();
}
}

399
src/Compile/Tag/Section.php Normal file
View File

@@ -0,0 +1,399 @@
<?php
namespace Smarty\Compile\Tag;
/**
* Smarty Internal Plugin Compile Section Class
*
*/
class Section extends ForeachSection {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $required_attributes = ['name', 'loop'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $shorttag_order = ['name', 'loop'];
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
protected $optional_attributes = ['start', 'step', 'max', 'show', 'properties'];
/**
* counter
*
* @var int
*/
protected $counter = 0;
/**
* Name of this tag
*
* @var string
*/
protected $tagName = 'section';
/**
* Valid properties of $smarty.section.name.xxx variable
*
* @var array
*/
protected $nameProperties = [
'first', 'last', 'index', 'iteration', 'show', 'total', 'rownum', 'index_prev',
'index_next', 'loop',
];
/**
* {section} tag has no item properties
*
* @var array
*/
protected $itemProperties = null;
/**
* {section} tag has always name attribute
*
* @var bool
*/
protected $isNamed = true;
/**
* Compiles code for the {section} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$attributes = ['name' => $compiler->getId($_attr['name'])];
unset($_attr['name']);
foreach ($attributes as $a => $v) {
if ($v === false) {
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", null, true);
}
}
$local = "\$__section_{$attributes['name']}_" . $this->counter++ . '_';
$sectionVar = "\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']";
if ($compiler->tag_nocache) {
// push a {nocache} tag onto the stack to prevent caching of this block
$this->openTag($compiler, 'nocache');
}
$this->openTag($compiler, 'section', ['section', $compiler->tag_nocache]);
$initLocal = [];
$initNamedProperty = [];
$initFor = [];
$incFor = [];
$cmpFor = [];
$propValue = [
'index' => "{$sectionVar}->value['index']", 'show' => 'true', 'step' => 1,
'iteration' => "{$local}iteration",
];
$propType = ['index' => 2, 'iteration' => 2, 'show' => 0, 'step' => 0,];
// search for used tag attributes
$this->scanForProperties($attributes, $compiler);
if (!empty($this->matchResults['named'])) {
$namedAttr = $this->matchResults['named'];
}
if (isset($_attr['properties']) && preg_match_all("/['](.*?)[']/", $_attr['properties'], $match)) {
foreach ($match[1] as $prop) {
if (in_array($prop, $this->nameProperties)) {
$namedAttr[$prop] = true;
} else {
$compiler->trigger_template_error("Invalid property '{$prop}'", null, true);
}
}
}
$namedAttr['index'] = true;
$output = "<?php\n";
foreach ($_attr as $attr_name => $attr_value) {
switch ($attr_name) {
case 'loop':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$t = 0;
} else {
$v = "(is_array(@\$_loop=$attr_value) ? count(\$_loop) : max(0, (int) \$_loop))";
$t = 1;
}
if ($t === 1) {
$initLocal['loop'] = $v;
$v = "{$local}loop";
}
break;
case 'show':
if (is_bool($attr_value)) {
$v = $attr_value ? 'true' : 'false';
$t = 0;
} else {
$v = "(bool) $attr_value";
$t = 3;
}
break;
case 'step':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$v = ($v === 0) ? 1 : $v;
$t = 0;
break;
}
$initLocal['step'] = "((int)@$attr_value) === 0 ? 1 : (int)@$attr_value";
$v = "{$local}step";
$t = 2;
break;
case 'max':
case 'start':
if (is_numeric($attr_value)) {
$v = (int)$attr_value;
$t = 0;
break;
}
$v = "(int)@$attr_value";
$t = 3;
break;
}
if ($t === 3 && $compiler->getId($attr_value)) {
$t = 1;
}
$propValue[$attr_name] = $v;
$propType[$attr_name] = $t;
}
if (isset($namedAttr['step'])) {
$initNamedProperty['step'] = $propValue['step'];
}
if (isset($namedAttr['iteration'])) {
$propValue['iteration'] = "{$sectionVar}->value['iteration']";
}
$incFor['iteration'] = "{$propValue['iteration']}++";
$initFor['iteration'] = "{$propValue['iteration']} = 1";
if ($propType['step'] === 0) {
if ($propValue['step'] === 1) {
$incFor['index'] = "{$sectionVar}->value['index']++";
} elseif ($propValue['step'] > 1) {
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
} else {
$incFor['index'] = "{$sectionVar}->value['index'] -= " . -$propValue['step'];
}
} else {
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
}
if (!isset($propValue['max'])) {
$propValue['max'] = $propValue['loop'];
$propType['max'] = $propType['loop'];
} elseif ($propType['max'] !== 0) {
$propValue['max'] = "{$propValue['max']} < 0 ? {$propValue['loop']} : {$propValue['max']}";
$propType['max'] = 1;
} else {
if ($propValue['max'] < 0) {
$propValue['max'] = $propValue['loop'];
$propType['max'] = $propType['loop'];
}
}
if (!isset($propValue['start'])) {
$start_code =
[1 => "{$propValue['step']} > 0 ? ", 2 => '0', 3 => ' : ', 4 => $propValue['loop'], 5 => ' - 1'];
if ($propType['loop'] === 0) {
$start_code[5] = '';
$start_code[4] = $propValue['loop'] - 1;
}
if ($propType['step'] === 0) {
if ($propValue['step'] > 0) {
$start_code = [1 => '0'];
$propType['start'] = 0;
} else {
$start_code[1] = $start_code[2] = $start_code[3] = '';
$propType['start'] = $propType['loop'];
}
} else {
$propType['start'] = 1;
}
$propValue['start'] = join('', $start_code);
} else {
$start_code =
[
1 => "{$propValue['start']} < 0 ? ", 2 => 'max(', 3 => "{$propValue['step']} > 0 ? ", 4 => '0',
5 => ' : ', 6 => '-1', 7 => ', ', 8 => "{$propValue['start']} + {$propValue['loop']}", 10 => ')',
11 => ' : ', 12 => 'min(', 13 => $propValue['start'], 14 => ', ',
15 => "{$propValue['step']} > 0 ? ", 16 => $propValue['loop'], 17 => ' : ',
18 => $propType['loop'] === 0 ? $propValue['loop'] - 1 : "{$propValue['loop']} - 1",
19 => ')',
];
if ($propType['step'] === 0) {
$start_code[3] = $start_code[5] = $start_code[15] = $start_code[17] = '';
if ($propValue['step'] > 0) {
$start_code[6] = $start_code[18] = '';
} else {
$start_code[4] = $start_code[16] = '';
}
}
if ($propType['start'] === 0) {
if ($propType['loop'] === 0) {
$start_code[8] = $propValue['start'] + $propValue['loop'];
}
$propType['start'] = $propType['step'] + $propType['loop'];
$start_code[1] = '';
if ($propValue['start'] < 0) {
for ($i = 11; $i <= 19; $i++) {
$start_code[$i] = '';
}
if ($propType['start'] === 0) {
$start_code = [
max(
$propValue['step'] > 0 ? 0 : -1,
$propValue['start'] + $propValue['loop']
),
];
}
} else {
for ($i = 1; $i <= 11; $i++) {
$start_code[$i] = '';
}
if ($propType['start'] === 0) {
$start_code =
[
min(
$propValue['step'] > 0 ? $propValue['loop'] : $propValue['loop'] - 1,
$propValue['start']
),
];
}
}
}
$propValue['start'] = join('', $start_code);
}
if ($propType['start'] !== 0) {
$initLocal['start'] = $propValue['start'];
$propValue['start'] = "{$local}start";
}
$initFor['index'] = "{$sectionVar}->value['index'] = {$propValue['start']}";
if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
$propValue['total'] = $propValue['loop'];
$propType['total'] = $propType['loop'];
} else {
$propType['total'] =
$propType['start'] + $propType['loop'] + $propType['step'] + $propType['max'];
if ($propType['total'] === 0) {
$propValue['total'] =
min(
ceil(
($propValue['step'] > 0 ? $propValue['loop'] - $propValue['start'] :
(int)$propValue['start'] + 1) / abs($propValue['step'])
),
$propValue['max']
);
} else {
$total_code = [
1 => 'min(', 2 => 'ceil(', 3 => '(', 4 => "{$propValue['step']} > 0 ? ",
5 => $propValue['loop'], 6 => ' - ', 7 => $propValue['start'], 8 => ' : ',
9 => $propValue['start'], 10 => '+ 1', 11 => ')', 12 => '/ ', 13 => 'abs(',
14 => $propValue['step'], 15 => ')', 16 => ')', 17 => ", {$propValue['max']})",
];
if (!isset($propValue['max'])) {
$total_code[1] = $total_code[17] = '';
}
if ($propType['loop'] + $propType['start'] === 0) {
$total_code[5] = $propValue['loop'] - $propValue['start'];
$total_code[6] = $total_code[7] = '';
}
if ($propType['start'] === 0) {
$total_code[9] = (int)$propValue['start'] + 1;
$total_code[10] = '';
}
if ($propType['step'] === 0) {
$total_code[13] = $total_code[15] = '';
if ($propValue['step'] === 1 || $propValue['step'] === -1) {
$total_code[2] = $total_code[12] = $total_code[14] = $total_code[16] = '';
} elseif ($propValue['step'] < 0) {
$total_code[14] = -$propValue['step'];
}
$total_code[4] = '';
if ($propValue['step'] > 0) {
$total_code[8] = $total_code[9] = $total_code[10] = '';
} else {
$total_code[5] = $total_code[6] = $total_code[7] = $total_code[8] = '';
}
}
$propValue['total'] = join('', $total_code);
}
}
if (isset($namedAttr['loop'])) {
$initNamedProperty['loop'] = "'loop' => {$propValue['loop']}";
}
if (isset($namedAttr['total'])) {
$initNamedProperty['total'] = "'total' => {$propValue['total']}";
if ($propType['total'] > 0) {
$propValue['total'] = "{$sectionVar}->value['total']";
}
} elseif ($propType['total'] > 0) {
$initLocal['total'] = $propValue['total'];
$propValue['total'] = "{$local}total";
}
$cmpFor['iteration'] = "{$propValue['iteration']} <= {$propValue['total']}";
foreach ($initLocal as $key => $code) {
$output .= "{$local}{$key} = {$code};\n";
}
$_vars = 'array(' . join(', ', $initNamedProperty) . ')';
$output .= "{$sectionVar} = new \\Smarty\\Variable({$_vars});\n";
$cond_code = "{$propValue['total']} !== 0";
if ($propType['total'] === 0) {
if ($propValue['total'] === 0) {
$cond_code = 'false';
} else {
$cond_code = 'true';
}
}
if ($propType['show'] > 0) {
$output .= "{$local}show = {$propValue['show']} ? {$cond_code} : false;\n";
$output .= "if ({$local}show) {\n";
} elseif ($propValue['show'] === 'true') {
$output .= "if ({$cond_code}) {\n";
} else {
$output .= "if (false) {\n";
}
$jinit = join(', ', $initFor);
$jcmp = join(', ', $cmpFor);
$jinc = join(', ', $incFor);
$output .= "for ({$jinit}; {$jcmp}; {$jinc}){\n";
if (isset($namedAttr['rownum'])) {
$output .= "{$sectionVar}->value['rownum'] = {$propValue['iteration']};\n";
}
if (isset($namedAttr['index_prev'])) {
$output .= "{$sectionVar}->value['index_prev'] = {$propValue['index']} - {$propValue['step']};\n";
}
if (isset($namedAttr['index_next'])) {
$output .= "{$sectionVar}->value['index_next'] = {$propValue['index']} + {$propValue['step']};\n";
}
if (isset($namedAttr['first'])) {
$output .= "{$sectionVar}->value['first'] = ({$propValue['iteration']} === 1);\n";
}
if (isset($namedAttr['last'])) {
$output .= "{$sectionVar}->value['last'] = ({$propValue['iteration']} === {$propValue['total']});\n";
}
$output .= '?>';
return $output;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/**
* Smarty Internal Plugin Compile Section
* Compiles the {section} {sectionelse} {/section} tags
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Sectionclose Class
*/
class SectionClose extends Base {
/**
* Compiles code for the {/section} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['section', 'sectionelse']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
}
$output = "<?php\n";
if ($openTag === 'sectionelse') {
$output .= "}\n";
} else {
$output .= "}\n}\n";
}
$output .= '?>';
return $output;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Sectionelse Class
*
*/
class SectionElse extends Base {
/**
* Compiles code for the {sectionelse} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['section']);
$this->openTag($compiler, 'sectionelse', ['sectionelse', $nocache_pushed]);
return "<?php }} else {\n ?>";
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Setfilter Class
*
*/
class Setfilter extends Base {
/**
* Compiles code for setfilter tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->variable_filter_stack[] = $compiler->getSmarty()->getDefaultModifiers();
// The modifier_list is passed as an array of array's. The inner arrays have the modifier at index 0,
// and, possibly, parameters at subsequent indexes, e.g. [ ['escape','"mail"'] ]
// We will collapse them so the syntax is OK for ::setDefaultModifiers() as follows: [ 'escape:"mail"' ]
$newList = [];
foreach($parameter['modifier_list'] as $modifier) {
$newList[] = implode(':', $modifier);
}
$compiler->getSmarty()->setDefaultModifiers($newList);
return '';
}
}

View File

@@ -0,0 +1,43 @@
<?php
/**
* Smarty Internal Plugin Compile Setfilter
* Compiles code for setfilter tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Setfilterclose Class
*
*/
class SetfilterClose extends Base {
/**
* Compiles code for the {/setfilter} tag
* This tag does not generate compiled output. It resets variable filter.
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->getAttributes($compiler, $args);
// reset variable filter to previous state
$compiler->getSmarty()->setDefaultModifiers(
count($compiler->variable_filter_stack) ? array_pop($compiler->variable_filter_stack) : []
);
return '';
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Smarty Internal Plugin Compile While
* Compiles the {while} tag
*
* @author Uwe Tews
*/
namespace Smarty\Compile\Tag;
use Smarty\Compile\Base;
/**
* Smarty Internal Plugin Compile Whileclose Class
*
*/
class WhileClose extends Base {
/**
* Compiles code for the {/while} tag
*
* @param array $args array with attributes from parser
* @param \Smarty\Compiler\Template $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;
$nocache_pushed = $this->closeTag($compiler, ['while']);
if ($nocache_pushed) {
// pop the pushed virtual nocache tag
$this->closeTag($compiler, 'nocache');
$compiler->tag_nocache = true;
}
return "<?php }?>\n";
}
}

Some files were not shown because too many files have changed in this diff Show More