mirror of
https://github.com/smarty-php/smarty.git
synced 2026-08-04 04:24:18 +02:00
Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 97aeb14c6f | |||
| ed454551a3 | |||
| f4b26a3cbf | |||
| 20a8026ccd | |||
| 27910bf2c1 | |||
| cb95216671 | |||
| d6ac3297df | |||
| 560475a2f2 | |||
| 382d66305d | |||
| 44bac8d58c | |||
| f8e1ba76f7 | |||
| 629504485f | |||
| 71036be8be | |||
| 78f0cf5f65 | |||
| 64ad6442ca | |||
| 7eff7d6fb0 | |||
| 05f1a7deda | |||
| 3f97b7352e | |||
| a6711b3833 | |||
| 52056ce65a | |||
| 8b96efad45 | |||
| bfa02f3b91 | |||
| 89dc506638 | |||
| e68065bc33 | |||
| db2547287c | |||
| 962f266483 | |||
| 8aeb5d6821 | |||
| a2e1df1768 | |||
| fe6d333c60 | |||
| 50c4604857 | |||
| e5a6ff42f8 | |||
| 3cc56392c6 | |||
| 17025423e1 | |||
| 18d3e564da | |||
| 0d2c38b482 | |||
| f90348971b | |||
| 9e0536de18 | |||
| 265cf4f3bc | |||
| 47f95ad324 | |||
| 02633ecaba | |||
| 6786623afb | |||
| 2d97169086 | |||
| 0f098802c6 | |||
| bbcd5817f6 | |||
| 612094d763 | |||
| 0ef0dda825 | |||
| cef8951100 | |||
| 323aefa89f | |||
| 277648b81c | |||
| 8d3a7af308 | |||
| cdf0f2b164 | |||
| 995b7eb36f | |||
| 059bea274c | |||
| f707dadecb | |||
| ace1c8e90f | |||
| 19ae410bf5 | |||
| baad3115cd | |||
| 6f4f06db7e | |||
| a535445dfd | |||
| 215d81a9fa | |||
| efb416e5ef | |||
| 29c0e5ac51 | |||
| 23be620fc8 | |||
| e6ab34654b | |||
| c7576eb1a1 | |||
| 56aadc697f | |||
| cd962280ce | |||
| cb5efd04c3 | |||
| 3a91df797e | |||
| 6983788dfb | |||
| 09875a61f4 | |||
| 428a701b18 | |||
| dff1955cd5 |
+5
-2
@@ -7,8 +7,11 @@
|
||||
|
||||
# exclude from git export
|
||||
/tests export-ignore
|
||||
/utilities/ export-ignore
|
||||
|
||||
/demo export-ignore
|
||||
/docs export-ignore
|
||||
/utilities export-ignore
|
||||
/docker-compose.yml export-ignore
|
||||
/.github export-ignore
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
||||
/make-release.sh export-ignore
|
||||
|
||||
@@ -28,6 +28,7 @@ jobs:
|
||||
- "7.3"
|
||||
- "7.4"
|
||||
- "8.0"
|
||||
- "8.1"
|
||||
|
||||
compiler:
|
||||
- default
|
||||
@@ -36,6 +37,9 @@ jobs:
|
||||
- os: ubuntu-latest
|
||||
php-version: "8.0"
|
||||
compiler: jit
|
||||
- os: ubuntu-latest
|
||||
php-version: "8.1"
|
||||
compiler: jit
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
lexer/*.php
|
||||
lexer/*.php.bak
|
||||
lexer/*.out
|
||||
utilies/*.php
|
||||
|
||||
# Dev
|
||||
phpunit*
|
||||
|
||||
@@ -6,6 +6,53 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [4.2.0] - 2022-08-01
|
||||
|
||||
### Fixed
|
||||
- Fixed problems with smarty_mb_str_replace [#549](https://github.com/smarty-php/smarty/issues/549)
|
||||
- Fixed second parameter of unescape modifier not working [#777](https://github.com/smarty-php/smarty/issues/777)
|
||||
|
||||
### Changed
|
||||
- Updated HTML of the debug template [#599](https://github.com/smarty-php/smarty/pull/599)
|
||||
|
||||
## [4.1.1] - 2022-05-17
|
||||
|
||||
### Security
|
||||
- Prevent PHP injection through malicious block name or include file name. This addresses CVE-2022-29221
|
||||
|
||||
### Fixed
|
||||
- Exclude docs and demo from export and composer [#751](https://github.com/smarty-php/smarty/pull/751)
|
||||
- PHP 8.1 deprecation notices in demo/plugins/cacheresource.pdo.php [#706](https://github.com/smarty-php/smarty/issues/706)
|
||||
- PHP 8.1 deprecation notices in truncate modifier [#699](https://github.com/smarty-php/smarty/issues/699)
|
||||
- Math equation `max(x, y)` didn't work anymore [#721](https://github.com/smarty-php/smarty/issues/721)
|
||||
- Fix PHP 8.1 deprecated warning when calling rtrim [#743](https://github.com/smarty-php/smarty/pull/743)
|
||||
- PHP 8.1: fix deprecation in escape modifier [#727](https://github.com/smarty-php/smarty/pull/727)
|
||||
|
||||
## [4.1.0] - 2022-02-06
|
||||
|
||||
### Added
|
||||
- PHP8.1 compatibility [#713](https://github.com/smarty-php/smarty/pull/713)
|
||||
|
||||
## [4.0.4] - 2022-01-18
|
||||
|
||||
### Fixed
|
||||
- Fixed illegal characters bug in math function security check [#702](https://github.com/smarty-php/smarty/issues/702)
|
||||
|
||||
## [4.0.3] - 2022-01-10
|
||||
|
||||
### Security
|
||||
- Prevent evasion of the `static_classes` security policy. This addresses CVE-2021-21408
|
||||
|
||||
## [4.0.2] - 2022-01-10
|
||||
|
||||
### Security
|
||||
- Prevent arbitrary PHP code execution through maliciously crafted expression for the math function. This addresses CVE-2021-29454
|
||||
|
||||
## [4.0.1] - 2022-01-09
|
||||
|
||||
### Security
|
||||
- Rewrote the mailto function to not use `eval` when encoding with javascript
|
||||
|
||||
## [4.0.0] - 2021-11-25
|
||||
|
||||
## [4.0.0-rc.0] - 2021-10-13
|
||||
|
||||
@@ -7,7 +7,7 @@ Smarty is a template engine for PHP, facilitating the separation of presentation
|
||||
Read the [documentation](https://smarty-php.github.io/smarty/) to find out how to use it.
|
||||
|
||||
## Requirements
|
||||
Smarty can be run with PHP 7.1 to PHP 8.0.
|
||||
Smarty can be run with PHP 7.1 to PHP 8.1.
|
||||
|
||||
## Installation
|
||||
Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/).
|
||||
@@ -17,12 +17,4 @@ To get the latest stable version of Smarty use:
|
||||
composer require smarty/smarty
|
||||
````
|
||||
|
||||
To get the latest, unreleased version, use:
|
||||
```bash
|
||||
composer require smarty/smarty:dev-master
|
||||
````
|
||||
|
||||
To get the previous stable version of Smarty, Smarty 3, use:
|
||||
```bash
|
||||
composer require smarty/smarty:^3
|
||||
````
|
||||
More in the [Getting Started](./docs/getting-started.md) section of the docs.
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
"keywords": [
|
||||
"templating"
|
||||
],
|
||||
"homepage": "https://www.smarty.net",
|
||||
"homepage": "https://smarty-php.github.io/smarty/",
|
||||
"license": "LGPL-3.0",
|
||||
"authors": [
|
||||
{
|
||||
@@ -27,7 +27,7 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/smarty-php/smarty/issues",
|
||||
"forum": "https://www.smarty.net/forums/"
|
||||
"forum": "https://github.com/smarty-php/smarty/discussions"
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.1 || ^8.0"
|
||||
|
||||
@@ -196,7 +196,7 @@ class Smarty_CacheResource_Pdo extends Smarty_CacheResource_Custom
|
||||
* @return void
|
||||
* @access protected
|
||||
*/
|
||||
protected function fetch($id, $name, $cache_id = null, $compile_id = null, &$content, &$mtime)
|
||||
protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
|
||||
{
|
||||
$stmt = $this->getFetchStatement($this->fetchColumns, $id, $cache_id, $compile_id);
|
||||
$stmt->execute();
|
||||
@@ -244,7 +244,7 @@ class Smarty_CacheResource_Pdo extends Smarty_CacheResource_Custom
|
||||
* @return boolean success
|
||||
* @access protected
|
||||
*/
|
||||
protected function save($id, $name, $cache_id = null, $compile_id = null, $exp_time, $content)
|
||||
protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
|
||||
{
|
||||
$stmt = $this->pdo->prepare($this->insertStatement);
|
||||
$stmt->bindValue('id', $id);
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
version: "2"
|
||||
services:
|
||||
base:
|
||||
build:
|
||||
context: .
|
||||
volumes:
|
||||
- .:/app
|
||||
working_dir: /app
|
||||
entrypoint: sh ./utilities/testrunners/run-test.sh
|
||||
php71:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php71/Dockerfile
|
||||
php72:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php72/Dockerfile
|
||||
php73:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php73/Dockerfile
|
||||
php74:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php74/Dockerfile
|
||||
php80:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php80/Dockerfile
|
||||
php81:
|
||||
extends:
|
||||
service: base
|
||||
build:
|
||||
dockerfile: ./utilities/testrunners/php81/Dockerfile
|
||||
@@ -1,5 +0,0 @@
|
||||
BUGS
|
||||
====
|
||||
|
||||
Check the `BUGS` file that comes with the latest distribution of Smarty,
|
||||
or check the website.
|
||||
@@ -1,15 +0,0 @@
|
||||
Resources {#inforesources}
|
||||
=========
|
||||
|
||||
Smarty\'s homepage is located at [URL.SMARTY](&url.smarty;)
|
||||
|
||||
- You can join the mailing list by sending an e-mail to ``. An archive
|
||||
of the mailing list can be viewed at [here](&url.ml.archive;)
|
||||
|
||||
- Forums are at [URL.FORUMS](&url.forums;)
|
||||
|
||||
- The wiki is located at [URL.WIKI](&url.wiki;)
|
||||
|
||||
- Join the chat at [irc.freenode.net\#smarty](&url.wiki;)
|
||||
|
||||
- FAQ\'s are [here](&url.faq_1;) and [here](&url.faq_2;)
|
||||
@@ -1,6 +1,18 @@
|
||||
Basic Syntax
|
||||
============
|
||||
|
||||
A simple Smarty template could look like this:
|
||||
```html
|
||||
<h1>{$title|escape}</h1>
|
||||
<ul>
|
||||
{foreach $cities as $city}
|
||||
<li>{$city.name|escape} ({$city.population})</li>
|
||||
{foreachelse}
|
||||
<li>no cities found</li>
|
||||
{/foreach}
|
||||
</ul>
|
||||
```
|
||||
|
||||
All Smarty template tags are enclosed within delimiters. By default
|
||||
these are `{` and `}`, but they can be
|
||||
[changed](../programmers/api-variables/variable-left-delimiter.md).
|
||||
@@ -11,6 +23,7 @@ displayed as static content, or unchanged. When Smarty encounters
|
||||
template tags, it attempts to interpret them, and displays the
|
||||
appropriate output in their place.
|
||||
|
||||
The basis components of the Smarty syntax are:
|
||||
- [Comments](./language-basic-syntax/language-syntax-comments.md)
|
||||
- [Variables](./language-basic-syntax/language-syntax-variables.md)
|
||||
- [Functions](./language-basic-syntax/language-syntax-functions.md)
|
||||
|
||||
@@ -21,7 +21,6 @@ Built-in Functions {#language.builtin.functions}
|
||||
- [{ldelim},{rdelim}](./language-builtin-functions/language-function-ldelim.md)
|
||||
- [{literal}](./language-builtin-functions/language-function-literal.md)
|
||||
- [{nocache}](./language-builtin-functions/language-function-nocache.md)
|
||||
- [{php}](./language-builtin-functions/language-function-php.md)
|
||||
- [{section},{sectionelse}](./language-builtin-functions/language-function-section.md)
|
||||
- [{setfilter}](./language-builtin-functions/language-function-setfilter.md)
|
||||
- [{strip}](./language-builtin-functions/language-function-strip.md)
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
{php} {#language.function.php}
|
||||
=====
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> `{php}` tags are deprecated from Smarty, and should not be used. Put
|
||||
> your PHP logic in PHP scripts or plugin functions instead.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> As of Smarty 3.1 the `{php}` tags are only available from
|
||||
> [SmartyBC](#bc).
|
||||
|
||||
The `{php}` tags allow PHP code to be embedded directly into the
|
||||
template. They will not be escaped, regardless of the
|
||||
[`$php_handling`](#variable.php.handling) setting.
|
||||
|
||||
|
||||
{php}
|
||||
// including a php script directly from the template.
|
||||
include('/path/to/display_weather.php');
|
||||
{/php}
|
||||
|
||||
|
||||
|
||||
|
||||
{* this template includes a {php} block that assign's the variable $varX *}
|
||||
{php}
|
||||
global $foo, $bar;
|
||||
if($foo == $bar){
|
||||
echo 'This will be sent to browser';
|
||||
}
|
||||
// assign a variable to Smarty
|
||||
$this->assign('varX','Toffee');
|
||||
{/php}
|
||||
{* output the variable *}
|
||||
<strong>{$varX}</strong> is my fav ice cream :-)
|
||||
|
||||
|
||||
|
||||
See also [`$php_handling`](#variable.php.handling),
|
||||
[`{include_php}`](#language.function.include.php),
|
||||
[`{include}`](#language.function.include),
|
||||
[`{insert}`](#language.function.insert) and [componentized
|
||||
templates](#tips.componentized.templates).
|
||||
@@ -0,0 +1,152 @@
|
||||
Features
|
||||
=======
|
||||
|
||||
Some of Smarty's features:
|
||||
- It is extremely fast.
|
||||
- It is efficient since the PHP parser does the dirty work.
|
||||
- No template parsing overhead, only compiles once.
|
||||
- It is smart about [recompiling](#variable.compile.check) only the
|
||||
template files that have changed.
|
||||
- You can easily create your own custom
|
||||
[functions](#language.custom.functions) and [variable
|
||||
modifiers](#language.modifiers), so the template language is
|
||||
extremely extensible.
|
||||
- Configurable template [{delimiter}](#variable.left.delimiter) tag
|
||||
syntax, so you can use `{$foo}`, `{{$foo}}`, `<!--{$foo}-->`, etc.
|
||||
- The [`{if}..{elseif}..{else}..{/if}`](#language.function.if)
|
||||
constructs are passed to the PHP parser, so the `{if...}` expression
|
||||
syntax can be as simple or as complex an evaluation as you like.
|
||||
- Allows unlimited nesting of
|
||||
[`sections`](#language.function.section), `if's` etc.
|
||||
- Built-in [caching](#caching) support
|
||||
- Arbitrary [template](#resources) sources
|
||||
- [Template Inheritance](#advanced.features.template.inheritance) for
|
||||
easy management of template content.
|
||||
- [Plugin](#plugins) architecture
|
||||
|
||||
## Separation of presentation from application code
|
||||
- This means templates can certainly contain logic under the condition
|
||||
that it is for presentation only. Things such as
|
||||
[including](./designers/language-builtin-functions/language-function-include.md) other templates,
|
||||
[alternating](./designers/language-custom-functions/language-function-cycle.md) table row colors,
|
||||
[upper-casing](./designers/language-modifiers/language-modifier-upper.md) a variable,
|
||||
[looping](./designers/language-builtin-functions/language-function-foreach.md) over an array of data and
|
||||
rendering it are examples of presentation logic.
|
||||
- This does not mean however that Smarty forces a separation of
|
||||
business and presentation logic. Smarty has no knowledge of which is
|
||||
which, so placing business logic in the template is your own doing.
|
||||
- Also, if you desire *no* logic in your templates you certainly can
|
||||
do so by boiling the content down to text and variables only.
|
||||
|
||||
## How does it work?
|
||||
|
||||
Under the hood, Smarty "compiles" (basically copies and converts) the
|
||||
templates into PHP scripts. This happens once when each template is
|
||||
first invoked, and then the compiled versions are used from that point
|
||||
forward. Smarty takes care of this for you, so the template designer
|
||||
just edits the Smarty templates and never has to manage the compiled
|
||||
versions. This approach keeps the templates easy to maintain, and yet
|
||||
keeps execution times extremely fast since the compiled code is just
|
||||
PHP. And of course, all PHP scripts take advantage of PHP op-code caches
|
||||
such as APC.
|
||||
|
||||
## Template Inheritance
|
||||
|
||||
Template inheritance was introduced in Smarty 3. Before template
|
||||
inheritance, we managed our templates in
|
||||
pieces such as header and footer templates. This organization lends
|
||||
itself to many problems that require some hoop-jumping, such as managing
|
||||
content within the header/footer on a per-page basis. With template
|
||||
inheritance, instead of including other templates we maintain our
|
||||
templates as single pages. We can then manipulate blocks of content
|
||||
within by inheriting them. This makes templates intuitive, efficient and
|
||||
easy to manage. See
|
||||
[Template Inheritance](./programmers/advanced-features/advanced-features-template-inheritance.md)
|
||||
for more info.
|
||||
|
||||
## Why not use XML/XSLT syntax?
|
||||
There are a couple of good reasons. First, Smarty can be used for more
|
||||
than just XML/HTML based templates, such as generating emails,
|
||||
javascript, CSV, and PDF documents. Second, XML/XSLT syntax is even more
|
||||
verbose and fragile than PHP code! It is perfect for computers, but
|
||||
horrible for humans. Smarty is about being easy to read, understand and
|
||||
maintain.
|
||||
|
||||
## Template Security
|
||||
Although Smarty insulates you from PHP, you still have the option to use
|
||||
it in certain ways if you wish. Template security forces the restriction
|
||||
of PHP (and select Smarty functions.) This is useful if you have third
|
||||
parties editing templates, and you don't want to unleash the full power
|
||||
of PHP or Smarty to them.
|
||||
|
||||
## Integration
|
||||
Sometimes Smarty gets compared to Model-View-Controller (MVC)
|
||||
frameworks. Smarty is not an MVC, it is just the presentation layer,
|
||||
much like the View (V) part of an MVC. As a matter of fact, Smarty can
|
||||
easily be integrated as the view layer of an MVC. Many of the more
|
||||
popular ones have integration instructions for Smarty, or you may find
|
||||
some help here in the forums and documentation.
|
||||
|
||||
## Other Template Engines
|
||||
Smarty is not the only engine following the *"Separate Programming Code
|
||||
from Presentation"* philosophy. For instance, Python has template
|
||||
engines built around the same principles such as Django Templates and
|
||||
CheetahTemplate. *Note: Languages such as Python do not mix with HTML
|
||||
natively, which give them the advantage of proper programming code
|
||||
separation from the outset. There are libraries available to mix Python
|
||||
with HTML, but they are typically avoided.*
|
||||
|
||||
## What Smarty is Not
|
||||
|
||||
Smarty is not an application development framework. Smarty is not an
|
||||
MVC. Smarty is not an alternative to Laravel, Symfony, CodeIgniter,
|
||||
or any of the other application development frameworks for PHP.
|
||||
|
||||
Smarty is a template engine, and works as the (V)iew component of your
|
||||
application. Smarty can easily be coupled to any of the engines listed
|
||||
above as the view component. No different than any other software,
|
||||
Smarty has a learning curve. Smarty does not guarantee good application
|
||||
design or proper separation of presentation, this still needs to be
|
||||
addressed by a competent developer and web designer.
|
||||
|
||||
## Is Smarty Right for Me?
|
||||
|
||||
Smarty is not meant to be a tool for every job. The important thing is
|
||||
to identify if Smarty fits your needs. There are some important
|
||||
questions to ask yourself:
|
||||
|
||||
### Template Syntax
|
||||
Are you content with PHP tags mixed with HTML? Are your
|
||||
web designers comfortable with PHP? Would your web designers prefer a
|
||||
tag-based syntax designed for presentation? Some experience working with
|
||||
both Smarty and PHP helps answer these questions.
|
||||
|
||||
### The Business Case
|
||||
Is there a requirement to insulate the templates from
|
||||
PHP? Do you have untrusted parties editing templates that you do not
|
||||
wish to unleash the power of PHP to? Do you need to programmatically
|
||||
control what is and is not available within the templates? Smarty
|
||||
supplies these capabilities by design.
|
||||
|
||||
## Feature set
|
||||
Does Smarty's features such as caching, template
|
||||
inheritance and plugin architecture save development cycles writing code
|
||||
that would be needed otherwise? Does the codebase or framework you plan
|
||||
on using have the features you need for the presentation component?
|
||||
|
||||
## Sites using Smarty
|
||||
Many well-known PHP projects make use of Smarty such as XOOPS CMS, CMS Made Simple, Tiki
|
||||
CMS/Groupware and X-Cart to name a few.
|
||||
|
||||
## Summary
|
||||
Whether you are using Smarty for a small website or massive enterprise
|
||||
solution, it can accommodate your needs. There are numerous features
|
||||
that make Smarty a great choice:
|
||||
|
||||
- separation of PHP from HTML/CSS just makes sense
|
||||
- readability for organization and management
|
||||
- security for 3rd party template access
|
||||
- feature completeness, and easily extendable to your own needs
|
||||
- massive user base, Smarty is here to stay
|
||||
- LGPL license for commercial use
|
||||
- 100% free to use, open source project
|
||||
+116
-364
@@ -1,267 +1,56 @@
|
||||
What is Smarty?
|
||||
===============
|
||||
|
||||
Smarty is a template engine for PHP. More specifically, it facilitates a
|
||||
manageable way to separate application logic and content from its
|
||||
presentation. This is best described in a situation where the
|
||||
application programmer and the template designer play different roles,
|
||||
or in most cases are not the same person.
|
||||
|
||||
For example, let\'s say you are creating a web page that is displaying a
|
||||
newspaper article.
|
||||
|
||||
- The article `$headline`, `$tagline`, `$author` and `$body` are
|
||||
content elements, they contain no information about how they will be
|
||||
presented. They are [passed](#api.assign) into Smarty by the
|
||||
application.
|
||||
|
||||
- Then the template designer edits the templates and uses a
|
||||
combination of HTML tags and [template tags](#language.basic.syntax)
|
||||
to format the presentation of these
|
||||
[variables](#language.syntax.variables) with elements such as
|
||||
tables, div\'s, background colors, font sizes, style sheets, svg
|
||||
etc.
|
||||
|
||||
- One day the programmer needs to change the way the article content
|
||||
is retrieved, ie a change in application logic. This change does not
|
||||
affect the template designer, the content will still arrive in the
|
||||
template exactly the same.
|
||||
|
||||
- Likewise, if the template designer wants to completely redesign the
|
||||
templates, this would require no change to the application logic.
|
||||
|
||||
- Therefore, the programmer can make changes to the application logic
|
||||
without the need to restructure templates, and the template designer
|
||||
can make changes to templates without breaking application logic.
|
||||
|
||||
One design goal of Smarty is the separation of business logic and
|
||||
presentation logic.
|
||||
|
||||
- This means templates can certainly contain logic under the condition
|
||||
that it is for presentation only. Things such as
|
||||
[including](#language.function.include) other templates,
|
||||
[alternating](#language.function.cycle) table row colors,
|
||||
[upper-casing](#language.modifier.upper) a variable,
|
||||
[looping](#language.function.foreach) over an array of data and
|
||||
[displaying](#api.display) it are examples of presentation logic.
|
||||
|
||||
- This does not mean however that Smarty forces a separation of
|
||||
business and presentation logic. Smarty has no knowledge of which is
|
||||
which, so placing business logic in the template is your own doing.
|
||||
|
||||
- Also, if you desire *no* logic in your templates you certainly can
|
||||
do so by boiling the content down to text and variables only.
|
||||
|
||||
**Some of Smarty\'s features:**
|
||||
|
||||
- It is extremely fast.
|
||||
|
||||
- It is efficient since the PHP parser does the dirty work.
|
||||
|
||||
- No template parsing overhead, only compiles once.
|
||||
|
||||
- It is smart about [recompiling](#variable.compile.check) only the
|
||||
template files that have changed.
|
||||
|
||||
- You can easily create your own custom
|
||||
[functions](#language.custom.functions) and [variable
|
||||
modifiers](#language.modifiers), so the template language is
|
||||
extremely extensible.
|
||||
|
||||
- Configurable template [{delimiter}](#variable.left.delimiter) tag
|
||||
syntax, so you can use `{$foo}`, `{{$foo}}`, `<!--{$foo}-->`, etc.
|
||||
|
||||
- The [`{if}..{elseif}..{else}..{/if}`](#language.function.if)
|
||||
constructs are passed to the PHP parser, so the `{if...}` expression
|
||||
syntax can be as simple or as complex an evaluation as you like.
|
||||
|
||||
- Allows unlimited nesting of
|
||||
[`sections`](#language.function.section), `if's` etc.
|
||||
|
||||
- Built-in [caching](#caching) support
|
||||
|
||||
- Arbitrary [template](#resources) sources
|
||||
|
||||
- [Template Inheritance](#advanced.features.template.inheritance) for
|
||||
easy management of template content.
|
||||
|
||||
- [Plugin](#plugins) architecture
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Requirements {#installation.requirements}
|
||||
============
|
||||
|
||||
Smarty requires a web server running PHP 5.2 or greater.
|
||||
|
||||
Basic Installation {#installing.smarty.basic}
|
||||
==================
|
||||
|
||||
Install the Smarty library files which are in the `/libs/` sub directory
|
||||
of the distribution. These are `.php` files that you SHOULD NOT edit.
|
||||
They are shared among all applications and only get changed when you
|
||||
upgrade to a new version of Smarty.
|
||||
|
||||
In the examples below the Smarty tarball has been unpacked to:
|
||||
|
||||
- `/usr/local/lib/Smarty-v.e.r/` for \*nix machines
|
||||
|
||||
- and `c:\webroot\libs\Smarty-v.e.r\` for the windows environment.
|
||||
|
||||
<!-- -->
|
||||
|
||||
|
||||
Smarty-v.e.r/
|
||||
libs/
|
||||
Smarty.class.php
|
||||
debug.tpl
|
||||
sysplugins/* (everything)
|
||||
plugins/* (everything)
|
||||
|
||||
|
||||
|
||||
Smarty uses a PHP [constant](&url.php-manual;define) named
|
||||
[`SMARTY_DIR`](#constant.smarty.dir) which is the **full system file
|
||||
path** to the Smarty `libs/` directory. Basically, if your application
|
||||
can find the `Smarty.class.php` file, you do not need to set the
|
||||
[`SMARTY_DIR`](#constant.smarty.dir) as Smarty will figure it out on its
|
||||
own. Therefore, if `Smarty.class.php` is not in your
|
||||
[include\_path](&url.php-manual;ini.core.php#ini.include-path), or you
|
||||
do not supply an absolute path to it in your application, then you must
|
||||
define `SMARTY_DIR` manually. `SMARTY_DIR` **must include a trailing
|
||||
slash/**.
|
||||
|
||||
::: {.informalexample}
|
||||
Here\'s how you create an instance of Smarty in your PHP scripts:
|
||||
|
||||
|
||||
<?php
|
||||
// NOTE: Smarty has a capital 'S'
|
||||
require_once('Smarty.class.php');
|
||||
$smarty = new Smarty();
|
||||
?>
|
||||
|
||||
|
||||
:::
|
||||
|
||||
Try running the above script. If you get an error saying the
|
||||
`Smarty.class.php` file could not be found, you need to do one of the
|
||||
following:
|
||||
|
||||
|
||||
<?php
|
||||
// *nix style (note capital 'S')
|
||||
define('SMARTY_DIR', '/usr/local/lib/Smarty-v.e.r/libs/');
|
||||
|
||||
// windows style
|
||||
define('SMARTY_DIR', 'c:/webroot/libs/Smarty-v.e.r/libs/');
|
||||
|
||||
// hack version example that works on both *nix and windows
|
||||
// Smarty is assumend to be in 'includes/' dir under current script
|
||||
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/');
|
||||
|
||||
require_once(SMARTY_DIR . 'Smarty.class.php');
|
||||
$smarty = new Smarty();
|
||||
?>
|
||||
|
||||
|
||||
|
||||
|
||||
<?php
|
||||
// *nix style (note capital 'S')
|
||||
require_once('/usr/local/lib/Smarty-v.e.r/libs/Smarty.class.php');
|
||||
|
||||
// windows style
|
||||
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');
|
||||
|
||||
$smarty = new Smarty();
|
||||
?>
|
||||
|
||||
|
||||
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
; Paths and Directories ;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
; *nix: "/path1:/path2"
|
||||
include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/"
|
||||
|
||||
; Windows: "\path1;\path2"
|
||||
include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"
|
||||
|
||||
|
||||
<?php
|
||||
// *nix
|
||||
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'/usr/local/lib/Smarty-v.e.r/libs/');
|
||||
|
||||
// windows
|
||||
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'c:/webroot/lib/Smarty-v.e.r/libs/');
|
||||
?>
|
||||
|
||||
|
||||
|
||||
Now that the library files are in place, it\'s time to setup the Smarty
|
||||
directories for your application:
|
||||
|
||||
- Smarty requires four directories which are by default named
|
||||
`templates/`, `templates_c/`, `configs/` and `cache/`
|
||||
|
||||
- Each of these are definable by the Smarty class properties
|
||||
[`$template_dir`](#variable.template.dir),
|
||||
[`$compile_dir`](#variable.compile.dir),
|
||||
[`$config_dir`](#variable.config.dir), and
|
||||
[`$cache_dir`](#variable.cache.dir) respectively
|
||||
|
||||
- It is highly recommended that you setup a separate set of these
|
||||
directories for each application that will use Smarty
|
||||
|
||||
- You can verify if your system has the correct access rights for
|
||||
these directories with [`testInstall()`](#api.test.install).
|
||||
|
||||
For our installation example, we will be setting up the Smarty
|
||||
environment for a guest book application. We picked an application only
|
||||
for the purpose of a directory naming convention. You can use the same
|
||||
environment for any application, just replace `guestbook/` with the name
|
||||
of your application.
|
||||
|
||||
|
||||
/usr/local/lib/Smarty-v.e.r/libs/
|
||||
Smarty.class.php
|
||||
debug.tpl
|
||||
sysplugins/*
|
||||
plugins/*
|
||||
|
||||
/web/www.example.com/
|
||||
guestbook/
|
||||
templates/
|
||||
index.tpl
|
||||
templates_c/
|
||||
configs/
|
||||
cache/
|
||||
htdocs/
|
||||
index.php
|
||||
|
||||
|
||||
|
||||
Be sure that you know the location of your web server\'s document root
|
||||
as a file path. In the following examples, the document root is
|
||||
`/web/www.example.com/guestbook/htdocs/`. The Smarty directories are
|
||||
only accessed by the Smarty library and never accessed directly by the
|
||||
web browser. Therefore to avoid any security concerns, it is recommended
|
||||
(but not mandatory) to place these directories *outside* of the web
|
||||
server\'s document root.
|
||||
|
||||
You will need as least one file under your document root, and that is
|
||||
the script accessed by the web browser. We will name our script
|
||||
`index.php`, and place it in a subdirectory under the document root
|
||||
`/htdocs/`.
|
||||
|
||||
Smarty will need **write access** (windows users please ignore) to the
|
||||
[`$compile_dir`](#variable.compile.dir) and
|
||||
[`$cache_dir`](#variable.cache.dir) directories (`templates_c/` and
|
||||
`cache/`), so be sure the web server user account can write to them.
|
||||
==============
|
||||
|
||||
## Requirements
|
||||
Smarty can be run with PHP 7.1 to PHP 8.1.
|
||||
|
||||
## Installation
|
||||
Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/).
|
||||
|
||||
To get the latest stable version of Smarty use:
|
||||
```bash
|
||||
composer require smarty/smarty
|
||||
````
|
||||
|
||||
To get the latest, unreleased version, use:
|
||||
```bash
|
||||
composer require smarty/smarty:dev-master
|
||||
````
|
||||
|
||||
To get the previous stable version of Smarty, Smarty 3, use:
|
||||
```bash
|
||||
composer require smarty/smarty:^3
|
||||
````
|
||||
|
||||
Here's how you create an instance of Smarty in your PHP scripts:
|
||||
```php
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
$smarty = new Smarty();
|
||||
```
|
||||
|
||||
Now that the library files are in place, it's time to setup the Smarty
|
||||
directories for your application.
|
||||
|
||||
Smarty requires four directories which are by default named
|
||||
[`templates`](./programmers/api-variables/variable-template-dir.md),
|
||||
[`configs`](./programmers/api-variables/variable-config-dir.md),
|
||||
[`templates_c`](./programmers/api-variables/variable-compile-dir.md)
|
||||
and
|
||||
[`cache`](./programmers/api-variables/variable-cache-dir.md)
|
||||
relative to the current working directory.
|
||||
|
||||
The defaults can be changed as follows:
|
||||
```php
|
||||
$smarty = new Smarty();
|
||||
$smarty->setTemplateDir('/some/template/dir');
|
||||
$smarty->setConfigDir('/some/config/dir');
|
||||
$smarty->setCompileDir('/some/compile/dir');
|
||||
$smarty->setCacheDir('/some/cache/dir');
|
||||
```
|
||||
|
||||
The compile dir and cache dir need to be writable for the user running the PHP script.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
@@ -270,66 +59,62 @@ Smarty will need **write access** (windows users please ignore) to the
|
||||
> can look in your `httpd.conf` file to see what user and group are
|
||||
> being used.
|
||||
|
||||
```bash
|
||||
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
|
||||
chmod 770 /web/www.example.com/guestbook/templates_c/
|
||||
|
||||
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
|
||||
chmod 770 /web/www.example.com/guestbook/templates_c/
|
||||
chown nobody:nobody /web/www.example.com/guestbook/cache/
|
||||
chmod 770 /web/www.example.com/guestbook/cache/
|
||||
```
|
||||
|
||||
chown nobody:nobody /web/www.example.com/guestbook/cache/
|
||||
chmod 770 /web/www.example.com/guestbook/cache/
|
||||
You can verify if your system has the correct access rights for
|
||||
these directories with [`testInstall()`](./programmers/api-functions/api-test-install.md):
|
||||
|
||||
|
||||
```php
|
||||
$smarty = new Smarty();
|
||||
$smarty->setTemplateDir('/some/template/dir');
|
||||
$smarty->setConfigDir('/some/config/dir');
|
||||
$smarty->setCompileDir('/some/compile/dir');
|
||||
$smarty->setCacheDir('/some/cache/dir');
|
||||
$smarty->testInstall();
|
||||
```
|
||||
|
||||
Now, let's create the `index.tpl` file that Smarty will display. This
|
||||
needs to be located in the [`$template_dir`](./programmers/api-variables/variable-template-dir.md).
|
||||
|
||||
```html
|
||||
{* Smarty *}
|
||||
Hello {$name}, welcome to Smarty!
|
||||
```
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> `chmod 770` will be fairly tight security, it only allows user
|
||||
> "nobody" and group "nobody" read/write access to the directories. If
|
||||
> you would like to open up read access to anyone (mostly for your own
|
||||
> convenience of viewing these files), you can use `775` instead.
|
||||
|
||||
We need to create the `index.tpl` file that Smarty will display. This
|
||||
needs to be located in the [`$template_dir`](#variable.template.dir).
|
||||
|
||||
|
||||
{* Smarty *}
|
||||
|
||||
Hello {$name}, welcome to Smarty!
|
||||
|
||||
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> `{* Smarty *}` is a template [comment](#language.syntax.comments). It
|
||||
> `{* Smarty *}` is a template [comment](./designers/language-basic-syntax/language-syntax-comments.md). It
|
||||
> is not required, but it is good practice to start all your template
|
||||
> files with this comment. It makes the file easy to recognize
|
||||
> regardless of the file extension. For example, text editors could
|
||||
> recognize the file and turn on special syntax highlighting.
|
||||
|
||||
Now lets edit `index.php`. We\'ll create an instance of Smarty,
|
||||
[`assign()`](#api.assign) a template variable and
|
||||
[`display()`](#api.display) the `index.tpl` file.
|
||||
Now lets edit our php file. We'll create an instance of Smarty,
|
||||
[`assign()`](./programmers/api-functions/api-assign.md) a template variable and
|
||||
[`display()`](./programmers/api-functions/api-display.md) the `index.tpl` file.
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
require_once(SMARTY_DIR . 'Smarty.class.php');
|
||||
$smarty = new Smarty();
|
||||
|
||||
$smarty = new Smarty();
|
||||
$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
||||
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
||||
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
|
||||
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');
|
||||
|
||||
$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
||||
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
||||
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
|
||||
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');
|
||||
$smarty->assign('name', 'Ned');
|
||||
$smarty->display('index.tpl');
|
||||
|
||||
$smarty->assign('name','Ned');
|
||||
|
||||
//** un-comment the following line to show the debug console
|
||||
//$smarty->debugging = true;
|
||||
|
||||
$smarty->display('index.tpl');
|
||||
|
||||
?>
|
||||
|
||||
|
||||
```
|
||||
|
||||
> **Note**
|
||||
>
|
||||
@@ -340,78 +125,45 @@ Now lets edit `index.php`. We\'ll create an instance of Smarty,
|
||||
> absolute paths. This ensures that Smarty is getting files from the
|
||||
> directories you intended.
|
||||
|
||||
Now navigate to the `index.php` file with the web browser. You should
|
||||
see *\"Hello Ned, welcome to Smarty!\"*
|
||||
Now, run your PHP file. You should see *\"Hello Ned, welcome to Smarty!\"*
|
||||
|
||||
You have completed the basic setup for Smarty!
|
||||
|
||||
Extended Setup {#installing.smarty.extended}
|
||||
## Extended Setup {#installing.smarty.extended}
|
||||
==============
|
||||
|
||||
This is a continuation of the [basic
|
||||
installation](#installing.smarty.basic), please read that first!
|
||||
|
||||
A slightly more flexible way to setup Smarty is to [extend the
|
||||
class](&url.php-manual;ref.classobj) and initialize your Smarty
|
||||
A slightly more flexible way to setup Smarty is to extend the Smarty
|
||||
class and initialize your Smarty
|
||||
environment. So instead of repeatedly setting directory paths, assigning
|
||||
the same vars, etc., we can do that in one place.
|
||||
|
||||
Lets create a new directory `/php/includes/guestbook/` and make a new
|
||||
file called `setup.php`. In our example environment, `/php/includes` is
|
||||
in our `include_path`. Be sure you set this up too, or use absolute file
|
||||
paths.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Smarty_GuestBook extends Smarty {
|
||||
|
||||
<?php
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// load Smarty library
|
||||
require('Smarty.class.php');
|
||||
$this->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
||||
$this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
||||
$this->setConfigDir('/web/www.example.com/guestbook/configs/');
|
||||
$this->setCacheDir('/web/www.example.com/guestbook/cache/');
|
||||
|
||||
// The setup.php file is a good place to load
|
||||
// required application library files, and you
|
||||
// can do that right here. An example:
|
||||
// require('guestbook/guestbook.lib.php');
|
||||
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
|
||||
$this->assign('app_name', 'Guest Book');
|
||||
}
|
||||
|
||||
class Smarty_GuestBook extends Smarty {
|
||||
}
|
||||
```
|
||||
|
||||
function __construct()
|
||||
{
|
||||
|
||||
// Class Constructor.
|
||||
// These automatically get set with each new instance.
|
||||
|
||||
parent::__construct();
|
||||
|
||||
$this->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
||||
$this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
||||
$this->setConfigDir('/web/www.example.com/guestbook/configs/');
|
||||
$this->setCacheDir('/web/www.example.com/guestbook/cache/');
|
||||
|
||||
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
|
||||
$this->assign('app_name', 'Guest Book');
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
|
||||
Now lets alter the `index.php` file to use `setup.php`:
|
||||
|
||||
|
||||
<?php
|
||||
|
||||
require('guestbook/setup.php');
|
||||
|
||||
$smarty = new Smarty_GuestBook();
|
||||
|
||||
$smarty->assign('name','Ned');
|
||||
|
||||
$smarty->display('index.tpl');
|
||||
?>
|
||||
|
||||
|
||||
|
||||
Now you see it is quite simple to bring up an instance of Smarty, just
|
||||
use `Smarty_GuestBook()` which automatically initializes everything for
|
||||
our application.
|
||||
Now, we can use `Smarty_GuestBook` instead of `Smarty` in our scripts:
|
||||
```php
|
||||
$smarty = new Smarty_GuestBook();
|
||||
$smarty->assign('name','Ned');
|
||||
$smarty->display('index.tpl');
|
||||
```
|
||||
|
||||
+27
-4
@@ -1,12 +1,31 @@
|
||||
# Smarty 4 Documentation
|
||||
|
||||
Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
|
||||
|
||||
It allows you to write **templates**, using **variables**, **modifiers**, **functions** and **comments**, like this:
|
||||
```html
|
||||
<h1>{$title|escape}</h1>
|
||||
|
||||
<p>
|
||||
The number of pixels is: {math equation="x * y" x=$height y=$width}.
|
||||
</p>
|
||||
```
|
||||
|
||||
When this template is rendered, with the value "Hello world" for the variable $title, 640 for $width,
|
||||
and 480 for $height, the result is:
|
||||
```html
|
||||
<h1>Hello world</h1>
|
||||
|
||||
<p>
|
||||
The number of pixels is: 307200.
|
||||
</p>
|
||||
```
|
||||
|
||||
## Introduction
|
||||
- [Preface](./preface.md)
|
||||
- [Philosophy](./philosophy.md) - or "Why do I need a template engine?"
|
||||
- [Features](./features.md) - or "Why do I want Smarty?"
|
||||
- [Getting Started](./getting-started.md)
|
||||
|
||||
## Smarty for template developers
|
||||
## Smarty for template designers
|
||||
- [Basic Syntax](./designers/language-basic-syntax.md)
|
||||
- [Variables](./designers/language-variables.md)
|
||||
- [Variable Modifiers](./designers/language-modifiers.md)
|
||||
@@ -24,4 +43,8 @@ Smarty is a template engine for PHP, facilitating the separation of presentation
|
||||
- [Caching](./programmers/caching.md)
|
||||
- [Resources](./programmers/resources.md)
|
||||
- [Advanced Features](./programmers/advanced-features.md)
|
||||
- [Extending Smarty With Plugins](./programmers/plugins.md)
|
||||
- [Extending Smarty With Plugins](./programmers/plugins.md)
|
||||
|
||||
## Other
|
||||
- [Some random tips & tricks](./appendixes/tips.md)
|
||||
- [Troubleshooting](./appendixes/troubleshooting.md)
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
Philosophy
|
||||
=======
|
||||
|
||||
## What is Smarty?
|
||||
|
||||
Smarty is a template engine for PHP. More specifically, it facilitates a
|
||||
manageable way to separate application logic and content from its
|
||||
presentation. This is best described in a situation where the
|
||||
application programmer and the template designer play different roles,
|
||||
or in most cases are not the same person.
|
||||
|
||||
For example, let\'s say you are creating a web page that is displaying a
|
||||
newspaper article.
|
||||
|
||||
- The article `$headline`, `$tagline`, `$author` and `$body` are
|
||||
content elements, they contain no information about how they will be
|
||||
presented. They are [passed](#api.assign) into Smarty by the
|
||||
application.
|
||||
|
||||
- Then the template designer edits the templates and uses a
|
||||
combination of HTML tags and [template tags](#language.basic.syntax)
|
||||
to format the presentation of these
|
||||
[variables](#language.syntax.variables) with elements such as
|
||||
tables, div\'s, background colors, font sizes, style sheets, svg
|
||||
etc.
|
||||
|
||||
- One day the programmer needs to change the way the article content
|
||||
is retrieved, ie a change in application logic. This change does not
|
||||
affect the template designer, the content will still arrive in the
|
||||
template exactly the same.
|
||||
|
||||
- Likewise, if the template designer wants to completely redesign the
|
||||
templates, this would require no change to the application logic.
|
||||
|
||||
- Therefore, the programmer can make changes to the application logic
|
||||
without the need to restructure templates, and the template designer
|
||||
can make changes to templates without breaking application logic.
|
||||
|
||||
## Goals
|
||||
|
||||
The Smarty design was largely driven by these goals:
|
||||
- clean separation of presentation from application code
|
||||
- PHP backend, Smarty template frontend
|
||||
- complement PHP, not replace it
|
||||
- fast development/deployment for programmers and designers
|
||||
- quick and easy to maintain
|
||||
- syntax easy to understand, no PHP knowledge necessary
|
||||
- flexibility for custom development
|
||||
- security: insulation from PHP
|
||||
- free, open source
|
||||
|
||||
|
||||
|
||||
## Two camps of thought
|
||||
|
||||
When it comes to templating in PHP, there are basically two camps of
|
||||
thought. The first camp exclaims that \"PHP is a template engine\". This
|
||||
approach simply mixes PHP code with HTML. Although this approach is
|
||||
fastest from a pure script-execution point of view, many would argue
|
||||
that the PHP syntax is messy and complicated when mixed with tagged
|
||||
markup such as HTML.
|
||||
|
||||
The second camp exclaims that presentation should be void of all
|
||||
programming code, and instead use simple tags to indicate where
|
||||
application content is revealed. This approach is common with other
|
||||
template engines (even in other programming languages), and is also the
|
||||
approach that Smarty takes. The idea is to keep the templates focused
|
||||
squarely on presentation, void of application code, and with as little
|
||||
overhead as possible.
|
||||
|
||||
## Why is separating PHP from templates important?
|
||||
|
||||
Two major benefits:
|
||||
|
||||
- SYNTAX: Templates typically consist of semantic markup such as HTML.
|
||||
PHP syntax works well for application code, but quickly degenerates
|
||||
when mixed with HTML. Smarty\'s simple {tag} syntax is designed
|
||||
specifically to express presentation. Smarty focuses your templates
|
||||
on presentation and less on \"code\". This lends to quicker template
|
||||
deployment and easier maintenance. Smarty syntax requires no working
|
||||
knowledge of PHP, and is intuitive for programmers and
|
||||
non-programmers alike.
|
||||
|
||||
- INSULATION: When PHP is mixed with templates, there are no
|
||||
restrictions on what type of logic can be injected into a template.
|
||||
Smarty insulates the templates from PHP, creating a controlled
|
||||
separation of presentation from business logic. Smarty also has
|
||||
security features that can further enforce restrictions on
|
||||
templates.
|
||||
|
||||
## Web designers and PHP
|
||||
|
||||
A common question: "Web designers have to learn a syntax anyway, why
|
||||
not PHP?" Of course web designers can learn PHP, and they may already
|
||||
be familiar with it. The issue isn't their ability to learn PHP, it is
|
||||
about the consequences of mixing PHP with HTML. If designers use PHP, it
|
||||
is too easy to add code into templates that doesn't belong there (you
|
||||
just handed them a swiss-army knife when they just needed a knife.) You
|
||||
can teach them the rules of application design, but this is probably
|
||||
something they don't really need to learn (now they are developers!)
|
||||
The PHP manual is also an overwhelming pile of information to sift
|
||||
through. It is like handing the owner of a car the factory assembly
|
||||
manual when all they need is the owners manual. Smarty gives web
|
||||
designers exactly the tools they need, and gives developers fine-grained
|
||||
control over those tools. The simplicity of the tag-based syntax is also
|
||||
a huge welcome for designers, it helps them streamline the organization
|
||||
and management of templates.
|
||||
|
||||
-212
@@ -1,212 +0,0 @@
|
||||
Preface
|
||||
=======
|
||||
|
||||
**The Philosophy**
|
||||
|
||||
The Smarty design was largely driven by these goals:
|
||||
|
||||
- clean separation of presentation from application code
|
||||
- PHP backend, Smarty template frontend
|
||||
- complement PHP, not replace it
|
||||
- fast development/deployment for programmers and designers
|
||||
- quick and easy to maintain
|
||||
- syntax easy to understand, no PHP knowledge necessary
|
||||
- flexibility for custom development
|
||||
- security: insulation from PHP
|
||||
- free, open source
|
||||
|
||||
**What is Smarty?**
|
||||
|
||||
Smarty is a template engine for PHP, facilitating the separation of
|
||||
presentation (HTML/CSS) from application logic. This implies that *PHP
|
||||
code is application logic*, and is separated from the presentation.
|
||||
|
||||
**Two camps of thought**
|
||||
|
||||
When it comes to templating in PHP, there are basically two camps of
|
||||
thought. The first camp exclaims that \"PHP is a template engine\". This
|
||||
approach simply mixes PHP code with HTML. Although this approach is
|
||||
fastest from a pure script-execution point of view, many would argue
|
||||
that the PHP syntax is messy and complicated when mixed with tagged
|
||||
markup such as HTML.
|
||||
|
||||
The second camp exclaims that presentation should be void of all
|
||||
programming code, and instead use simple tags to indicate where
|
||||
application content is revealed. This approach is common with other
|
||||
template engines (even in other programming languages), and is also the
|
||||
approach that Smarty takes. The idea is to keep the templates focused
|
||||
squarely on presentation, void of application code, and with as little
|
||||
overhead as possible.
|
||||
|
||||
**Why is separating PHP from templates important?**
|
||||
|
||||
Two major benefits:
|
||||
|
||||
- SYNTAX: Templates typically consist of semantic markup such as HTML.
|
||||
PHP syntax works well for application code, but quickly degenerates
|
||||
when mixed with HTML. Smarty\'s simple {tag} syntax is designed
|
||||
specifically to express presentation. Smarty focuses your templates
|
||||
on presentation and less on \"code\". This lends to quicker template
|
||||
deployment and easier maintenance. Smarty syntax requires no working
|
||||
knowledge of PHP, and is intuitive for programmers and
|
||||
non-programmers alike.
|
||||
|
||||
- INSULATION: When PHP is mixed with templates, there are no
|
||||
restrictions on what type of logic can be injected into a template.
|
||||
Smarty insulates the templates from PHP, creating a controlled
|
||||
separation of presentation from business logic. Smarty also has
|
||||
security features that can further enforce restrictions on
|
||||
templates.
|
||||
|
||||
**Web designers and PHP**
|
||||
|
||||
A common question: \"Web designers have to learn a syntax anyways, why
|
||||
not PHP?\" Of course web designers can learn PHP, and they may already
|
||||
be familiar with it. The issue isn\'t their ability to learn PHP, it is
|
||||
about the consequences of mixing PHP with HTML. If designers use PHP, it
|
||||
is too easy to add code into templates that doesn\'t belong there (you
|
||||
just handed them a swiss-army knife when they just needed a knife.) You
|
||||
can teach them the rules of application design, but this is probably
|
||||
something they don\'t really need to learn (now they are developers!)
|
||||
The PHP manual is also an overwhelming pile of information to sift
|
||||
through. It is like handing the owner of a car the factory assembly
|
||||
manual when all they need is the owners manual. Smarty gives web
|
||||
designers exactly the tools they need, and gives developers fine-grained
|
||||
control over those tools. The simplicity of the tag-based syntax is also
|
||||
a huge welcome for designers, it helps them streamline the organization
|
||||
and management of templates.
|
||||
|
||||
**Implementation is Important**
|
||||
|
||||
Although Smarty gives you the tools to make a clean separation of
|
||||
presentation from application code, it also gives you plenty of room to
|
||||
bend those rules. A poor implementation (i.e. injecting PHP in
|
||||
templates) will cause more problems than the presentation separation was
|
||||
meant to resolve. The documentation does a good job of indicating what
|
||||
things to watch out for. Also see the Best Practices section of the
|
||||
Smarty website.
|
||||
|
||||
**How does it work?**
|
||||
|
||||
Under the hood, Smarty \"compiles\" (basically copies and converts) the
|
||||
templates into PHP scripts. This happens once when each template is
|
||||
first invoked, and then the compiled versions are used from that point
|
||||
forward. Smarty takes care of this for you, so the template designer
|
||||
just edits the Smarty templates and never has to manage the compiled
|
||||
versions. This approach keeps the templates easy to maintain, and yet
|
||||
keeps execution times extremely fast since the compiled code is just
|
||||
PHP. And of course, all PHP scripts take advantage of PHP op-code caches
|
||||
such as APC.
|
||||
|
||||
**Template Inheritance**
|
||||
|
||||
Template inheritance is new to Smarty 3, and it\'s one of many great new
|
||||
features. Before template inheritance, we managed our templates in
|
||||
pieces such as header and footer templates. This organization lends
|
||||
itself to many problems that require some hoop-jumping, such as managing
|
||||
content within the header/footer on a per-page basis. With template
|
||||
inheritance, instead of including other templates we maintain our
|
||||
templates as single pages. We can then manipulate blocks of content
|
||||
within by inheriting them. This makes templates intuitive, efficient and
|
||||
easy to manage. See the Template Inheritance section of th Smarty
|
||||
website for more info.
|
||||
|
||||
**Why not use XML/XSLT syntax?**
|
||||
|
||||
There are a couple of good reasons. First, Smarty can be used for more
|
||||
than just XML/HTML based templates, such as generating emails,
|
||||
javascript, CSV, and PDF documents. Second, XML/XSLT syntax is even more
|
||||
verbose and fragile than PHP code! It is perfect for computers, but
|
||||
horrible for humans. Smarty is about being easy to read, understand and
|
||||
maintain.
|
||||
|
||||
**Template Security**
|
||||
|
||||
Although Smarty insulates you from PHP, you still have the option to use
|
||||
it in certain ways if you wish. Template security forces the restriction
|
||||
of PHP (and select Smarty functions.) This is useful if you have third
|
||||
parties editing templates, and you don\'t want to unleash the full power
|
||||
of PHP or Smarty to them.
|
||||
|
||||
**Integration**
|
||||
|
||||
Sometimes Smarty gets compared to Model-View-Controller (MVC)
|
||||
frameworks. Smarty is not an MVC, it is just the presentation layer,
|
||||
much like the View (V) part of an MVC. As a matter of fact, Smarty can
|
||||
easily be integrated as the view layer of an MVC. Many of the more
|
||||
popular ones have integration instructions for Smarty, or you may find
|
||||
some help here in the forums and documentation.
|
||||
|
||||
**Other Template Engines**
|
||||
|
||||
Smarty is not the only engine following the *\"Separate Programming Code
|
||||
from Presentation\"* philosophy. For instance, Python has template
|
||||
engines built around the same principles such as Django Templates and
|
||||
CheetahTemplate. *Note: Languages such as Python do not mix with HTML
|
||||
natively, which give them the advantage of proper programming code
|
||||
separation from the outset. There are libraries available to mix Python
|
||||
with HTML, but they are typically avoided.*
|
||||
|
||||
**What Smarty is Not**
|
||||
|
||||
Smarty is not an application development framework. Smarty is not an
|
||||
MVC. Smarty is not an alternative to Zend Framework, CodeIgniter,
|
||||
PHPCake, or any of the other application development frameworks for PHP.
|
||||
|
||||
Smarty is a template engine, and works as the (V)iew component of your
|
||||
application. Smarty can easily be coupled to any of the engines listed
|
||||
above as the view component. No different than any other software,
|
||||
Smarty has a learning curve. Smarty does not guarantee good application
|
||||
design or proper separation of presentation, this still needs to be
|
||||
addressed by a competent developer and web designer.
|
||||
|
||||
**Is Smarty Right for Me?**
|
||||
|
||||
Smarty is not meant to be a tool for every job. The important thing is
|
||||
to identify if Smarty fits your needs. There are some important
|
||||
questions to ask yourself:
|
||||
|
||||
TEMPLATE SYNTAX. Are you content with PHP tags mixed with HTML? Are your
|
||||
web designers comfortable with PHP? Would your web designers prefer a
|
||||
tag-based syntax designed for presentation? Some experience working with
|
||||
both Smarty and PHP helps answer these questions.
|
||||
|
||||
THE BUSINESS CASE: Is there a requirement to insulate the templates from
|
||||
PHP? Do you have untrusted parties editing templates that you do not
|
||||
wish to unleash the power of PHP to? Do you need to programmatically
|
||||
control what is and is not available within the templates? Smarty
|
||||
supplies these capabilities by design.
|
||||
|
||||
FEATURE SET: Does Smarty\'s features such as caching, template
|
||||
inheritance and plugin architecture save development cycles writing code
|
||||
that would be needed otherwise? Does the codebase or framework you plan
|
||||
on using have the features you need for the presentation component?
|
||||
|
||||
Templating in PHP is a hot topic, and opinions widely vary. It is
|
||||
important that you understand Smarty, understand your own requirements,
|
||||
and make an informed decision for yourself. You are welcome to ask
|
||||
specific questions in the forums or the IRC channel.
|
||||
|
||||
See also the section about \"Use Cases and Work Flow\" on the Smarty
|
||||
website.
|
||||
|
||||
**Sites using Smarty**
|
||||
|
||||
There are tens of thousands of unique visitors on the Smarty website
|
||||
daily, mostly developers reading documentation. Many well-known PHP
|
||||
projects make use of Smarty such as XOOPS CMS, CMS Made Simple, Tiki
|
||||
CMS/Groupware and X-Cart to name a few.
|
||||
|
||||
**Summary**
|
||||
|
||||
Whether you are using Smarty for a small website or massive enterprise
|
||||
solution, it can accommodate your needs. There are numerous features
|
||||
that make Smarty a great choice:
|
||||
|
||||
- separation of PHP from HTML/CSS just makes sense
|
||||
- readability for organization and management
|
||||
- security for 3rd party template access
|
||||
- feature completeness, and easily extendable to your own needs
|
||||
- massive user base, Smarty is here to stay
|
||||
- LGPL license for commercial use
|
||||
- 100% free to use, open source project
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
Setting `$escape_html` to TRUE will escape all template variable output
|
||||
by wrapping it in
|
||||
`htmlspecialchars({$output}, ENT_QUOTES, SMARTY_RESOURCE_CHAR_SET);`,
|
||||
`htmlspecialchars({$output}, ENT_QUOTES, $char_set);`,
|
||||
which is the same as `{$variable|escape:"html"}`.
|
||||
|
||||
Template designers can choose to selectively disable this feature by
|
||||
|
||||
@@ -5,10 +5,10 @@ Charset Encoding {#charset.encoding}
|
||||
================
|
||||
|
||||
There are a variety of encodings for textual data, ISO-8859-1 (Latin1)
|
||||
and UTF-8 being the most popular. Unless specified otherwise with the
|
||||
`SMARTY_RESOURCE_CHAR_SET` constant, Smarty recognizes `UTF-8` as the
|
||||
internal charset if [Multibyte String](https://www.php.net/mbstring) is
|
||||
available, `ISO-8859-1` if not.
|
||||
and UTF-8 being the most popular. Unless you change `Smarty::$_CHARSET`,
|
||||
Smarty recognizes `UTF-8` as the internal charset if
|
||||
[Multibyte String](https://www.php.net/mbstring) is available,
|
||||
`ISO-8859-1` if not.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
@@ -36,8 +36,9 @@ available, `ISO-8859-1` if not.
|
||||
if (function_exists('mb_internal_charset')) {
|
||||
mb_internal_charset('EUC-JP');
|
||||
}
|
||||
define('SMARTY_RESOURCE_CHAR_SET', 'EUC-JP');
|
||||
|
||||
require_once 'libs/Smarty.class.php';
|
||||
Smarty::$_CHARSET = 'EUC-JP';
|
||||
$smarty = new Smarty();
|
||||
|
||||
|
||||
|
||||
@@ -747,6 +747,9 @@ value(res) ::= doublequoted_with_quotes(s). {
|
||||
|
||||
|
||||
value(res) ::= varindexed(vi) DOUBLECOLON static_class_access(r). {
|
||||
if ($this->security && $this->security->static_classes !== array()) {
|
||||
$this->compiler->trigger_template_error('dynamic static class not allowed by security setting');
|
||||
}
|
||||
$prefixVar = $this->compiler->getNewPrefixVariable();
|
||||
if (vi['var'] === '\'smarty\'') {
|
||||
$this->compiler->appendPrefixCode("<?php {$prefixVar} = ". $this->compiler->compileTag('private_special_variable',array(),vi['smarty_internal_index']).';?>');
|
||||
|
||||
@@ -98,7 +98,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* smarty version
|
||||
*/
|
||||
const SMARTY_VERSION = '4.0.0';
|
||||
const SMARTY_VERSION = '4.2.0';
|
||||
/**
|
||||
* define variable scopes
|
||||
*/
|
||||
@@ -867,7 +867,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
$this->plugins_dir = (array)$this->plugins_dir;
|
||||
}
|
||||
foreach ($this->plugins_dir as $k => $v) {
|
||||
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v, '/\\') . DIRECTORY_SEPARATOR, true);
|
||||
$this->plugins_dir[ $k ] = $this->_realpath(rtrim($v ?? '', '/\\') . DIRECTORY_SEPARATOR, true);
|
||||
}
|
||||
$this->_cache[ 'plugin_files' ] = array();
|
||||
$this->_pluginsDirNormalized = true;
|
||||
@@ -1345,7 +1345,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
*/
|
||||
private function _normalizeDir($dirName, $dir)
|
||||
{
|
||||
$this->{$dirName} = $this->_realpath(rtrim($dir, "/\\") . DIRECTORY_SEPARATOR, true);
|
||||
$this->{$dirName} = $this->_realpath(rtrim($dir ?? '', "/\\") . DIRECTORY_SEPARATOR, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1367,7 +1367,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
}
|
||||
foreach ($dir as $k => $v) {
|
||||
if (!isset($processed[ $k ])) {
|
||||
$dir[ $k ] = $v = $this->_realpath(rtrim($v, "/\\") . DIRECTORY_SEPARATOR, true);
|
||||
$dir[ $k ] = $v = $this->_realpath(rtrim($v ?? '', "/\\") . DIRECTORY_SEPARATOR, true);
|
||||
$processed[ $k ] = true;
|
||||
}
|
||||
}
|
||||
|
||||
+34
-19
@@ -1,9 +1,9 @@
|
||||
{capture name='_smarty_debug' assign=debug_output}
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Smarty Debug Console</title>
|
||||
<style type="text/css">
|
||||
<style>
|
||||
{literal}
|
||||
body, h1, h2, h3, td, th, p {
|
||||
font-family: sans-serif;
|
||||
@@ -31,6 +31,7 @@
|
||||
padding: 2px;
|
||||
border-top: 1px solid black;
|
||||
}
|
||||
|
||||
h3 {
|
||||
text-align: left;
|
||||
font-weight: bold;
|
||||
@@ -67,11 +68,11 @@
|
||||
color: green;
|
||||
}
|
||||
|
||||
.odd {
|
||||
tr:nth-child(odd) {
|
||||
background-color: #eeeeee;
|
||||
}
|
||||
|
||||
.even {
|
||||
tr:nth-child(even) {
|
||||
background-color: #fafafa;
|
||||
}
|
||||
|
||||
@@ -84,13 +85,16 @@
|
||||
color: black;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
#blue h3 {
|
||||
color: blue;
|
||||
}
|
||||
|
||||
#normal div {
|
||||
color: black;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
#table_assigned_vars th {
|
||||
color: blue;
|
||||
font-weight: bold;
|
||||
@@ -99,7 +103,6 @@
|
||||
#table_config_vars th {
|
||||
color: maroon;
|
||||
}
|
||||
|
||||
{/literal}
|
||||
</style>
|
||||
</head>
|
||||
@@ -112,11 +115,11 @@
|
||||
<h2>included templates & config files (load time in seconds)</h2>
|
||||
<div>
|
||||
{foreach $template_data as $template}
|
||||
<font color=brown>{$template.name}</font>
|
||||
<br /> <span class="exectime">
|
||||
<span style="color: brown;">{$template.name}</span>
|
||||
<br> <span class="exectime">
|
||||
(compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"})
|
||||
</span>
|
||||
<br />
|
||||
<br>
|
||||
{/foreach}
|
||||
</div>
|
||||
{/if}
|
||||
@@ -125,13 +128,22 @@
|
||||
|
||||
<table id="table_assigned_vars">
|
||||
{foreach $assigned_vars as $vars}
|
||||
<tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">
|
||||
<td><h3><font color=blue>${$vars@key}</font></h3>
|
||||
{if isset($vars['nocache'])}<b>Nocache</b><br />{/if}
|
||||
{if isset($vars['scope'])}<b>Origin:</b> {$vars['scope']|debug_print_var nofilter}{/if}
|
||||
<tr>
|
||||
<td>
|
||||
<h3 style="color: blue;">${$vars@key}</h3>
|
||||
{if isset($vars['nocache'])}<strong>Nocache</strong><br>{/if}
|
||||
{if isset($vars['scope'])}<strong>Origin:</strong> {$vars['scope']|debug_print_var nofilter}{/if}
|
||||
</td>
|
||||
<td>
|
||||
<h3>Value</h3>
|
||||
{$vars['value']|debug_print_var:10:80 nofilter}
|
||||
</td>
|
||||
<td>
|
||||
{if isset($vars['attributes'])}
|
||||
<h3>Attributes</h3>
|
||||
{$vars['attributes']|debug_print_var nofilter}
|
||||
{/if}
|
||||
</td>
|
||||
<td><h3>Value</h3>{$vars['value']|debug_print_var:10:80 nofilter}</td>
|
||||
<td>{if isset($vars['attributes'])}<h3>Attributes</h3>{$vars['attributes']|debug_print_var nofilter} {/if}</td>
|
||||
{/foreach}
|
||||
</table>
|
||||
|
||||
@@ -139,11 +151,14 @@
|
||||
|
||||
<table id="table_config_vars">
|
||||
{foreach $config_vars as $vars}
|
||||
<tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">
|
||||
<td><h3><font color=blue>#{$vars@key}#</font></h3>
|
||||
{if isset($vars['scope'])}<b>Origin:</b> {$vars['scope']|debug_print_var nofilter}{/if}
|
||||
<tr>
|
||||
<td>
|
||||
<h3 style="color: blue;">#{$vars@key}#</h3>
|
||||
{if isset($vars['scope'])}<strong>Origin:</strong> {$vars['scope']|debug_print_var nofilter}{/if}
|
||||
</td>
|
||||
<td>
|
||||
{$vars['value']|debug_print_var:10:80 nofilter}
|
||||
</td>
|
||||
<td>{$vars['value']|debug_print_var:10:80 nofilter}</td>
|
||||
</tr>
|
||||
{/foreach}
|
||||
|
||||
|
||||
@@ -94,22 +94,19 @@ function smarty_function_mailto($params)
|
||||
);
|
||||
return;
|
||||
}
|
||||
// FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
|
||||
if ($encode === 'javascript') {
|
||||
$string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
|
||||
$string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
|
||||
$js_encode = '';
|
||||
for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
|
||||
$js_encode .= '%' . bin2hex($string[ $x ]);
|
||||
}
|
||||
return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
|
||||
return '<script type="text/javascript">document.write(unescape(\'' . $js_encode . '\'))</script>';
|
||||
} elseif ($encode === 'javascript_charcode') {
|
||||
$string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
|
||||
for ($x = 0, $y = strlen($string); $x < $y; $x++) {
|
||||
for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
|
||||
$ord[] = ord($string[ $x ]);
|
||||
}
|
||||
$_ret = "<script type=\"text/javascript\" language=\"javascript\">\n" . "{document.write(String.fromCharCode(" .
|
||||
implode(',', $ord) . "))" . "}\n" . "</script>\n";
|
||||
return $_ret;
|
||||
return '<script type="text/javascript">document.write(String.fromCharCode(' . implode(',', $ord) . '))</script>';
|
||||
} elseif ($encode === 'hex') {
|
||||
preg_match('!^(.*)(\?.*)$!', $address, $match);
|
||||
if (!empty($match[ 2 ])) {
|
||||
|
||||
@@ -28,7 +28,12 @@ function smarty_function_math($params, $template)
|
||||
'int' => true,
|
||||
'abs' => true,
|
||||
'ceil' => true,
|
||||
'acos' => true,
|
||||
'acosh' => true,
|
||||
'cos' => true,
|
||||
'cosh' => true,
|
||||
'deg2rad' => true,
|
||||
'rad2deg' => true,
|
||||
'exp' => true,
|
||||
'floor' => true,
|
||||
'log' => true,
|
||||
@@ -39,27 +44,51 @@ function smarty_function_math($params, $template)
|
||||
'pow' => true,
|
||||
'rand' => true,
|
||||
'round' => true,
|
||||
'asin' => true,
|
||||
'asinh' => true,
|
||||
'sin' => true,
|
||||
'sinh' => true,
|
||||
'sqrt' => true,
|
||||
'srand' => true,
|
||||
'tan' => true
|
||||
'atan' => true,
|
||||
'atanh' => true,
|
||||
'tan' => true,
|
||||
'tanh' => true
|
||||
);
|
||||
|
||||
// be sure equation parameter is present
|
||||
if (empty($params[ 'equation' ])) {
|
||||
trigger_error("math: missing equation parameter", E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
$equation = $params[ 'equation' ];
|
||||
|
||||
// Remove whitespaces
|
||||
$equation = preg_replace('/\s+/', '', $equation);
|
||||
|
||||
// Adapted from https://www.php.net/manual/en/function.eval.php#107377
|
||||
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
|
||||
$functionsOrVars = '((?:0x[a-fA-F0-9]+)|([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))';
|
||||
$operators = '[,+\/*\^%-]'; // Allowed math operators
|
||||
$regexp = '/^(('.$number.'|'.$functionsOrVars.'|('.$functionsOrVars.'\s*\((?1)+\)|\((?1)+\)))(?:'.$operators.'(?1))?)+$/';
|
||||
|
||||
if (!preg_match($regexp, $equation)) {
|
||||
trigger_error("math: illegal characters", E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
|
||||
// make sure parenthesis are balanced
|
||||
if (substr_count($equation, '(') !== substr_count($equation, ')')) {
|
||||
trigger_error("math: unbalanced parenthesis", E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
|
||||
// disallow backticks
|
||||
if (strpos($equation, '`') !== false) {
|
||||
trigger_error("math: backtick character not allowed in equation", E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
|
||||
// also disallow dollar signs
|
||||
if (strpos($equation, '$') !== false) {
|
||||
trigger_error("math: dollar signs not allowed in equation", E_USER_WARNING);
|
||||
@@ -96,6 +125,7 @@ function smarty_function_math($params, $template)
|
||||
}
|
||||
$smarty_math_result = null;
|
||||
eval("\$smarty_math_result = " . $equation . ";");
|
||||
|
||||
if (empty($params[ 'format' ])) {
|
||||
if (empty($params[ 'assign' ])) {
|
||||
return $smarty_math_result;
|
||||
|
||||
@@ -29,6 +29,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $
|
||||
if (!$char_set) {
|
||||
$char_set = Smarty::$_CHARSET;
|
||||
}
|
||||
|
||||
$string = (string)$string;
|
||||
|
||||
switch ($esc_type) {
|
||||
case 'html':
|
||||
if ($_double_encode) {
|
||||
|
||||
@@ -42,8 +42,8 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_wo
|
||||
if (!$middle) {
|
||||
return mb_substr($string, 0, $length, Smarty::$_CHARSET) . $etc;
|
||||
}
|
||||
return mb_substr($string, 0, $length / 2, Smarty::$_CHARSET) . $etc .
|
||||
mb_substr($string, -$length / 2, $length, Smarty::$_CHARSET);
|
||||
return mb_substr($string, 0, intval($length / 2), Smarty::$_CHARSET) . $etc .
|
||||
mb_substr($string, -intval($length / 2), $length, Smarty::$_CHARSET);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
@@ -56,7 +56,7 @@ function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_wo
|
||||
if (!$middle) {
|
||||
return substr($string, 0, $length) . $etc;
|
||||
}
|
||||
return substr($string, 0, $length / 2) . $etc . substr($string, -$length / 2);
|
||||
return substr($string, 0, intval($length / 2)) . $etc . substr($string, -intval($length / 2));
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
@@ -42,10 +42,10 @@ function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompile
|
||||
switch ($esc_type) {
|
||||
case 'html':
|
||||
if ($_double_encode) {
|
||||
return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
|
||||
return 'htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
|
||||
var_export($double_encode, true) . ')';
|
||||
} elseif ($double_encode) {
|
||||
return 'htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
|
||||
return 'htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
|
||||
} else {
|
||||
// fall back to modifier.escape.php
|
||||
}
|
||||
@@ -54,12 +54,12 @@ function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompile
|
||||
if (Smarty::$_MBSTRING) {
|
||||
if ($_double_encode) {
|
||||
// php >=5.2.3 - go native
|
||||
return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
|
||||
return 'mb_convert_encoding(htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' .
|
||||
var_export($char_set, true) . ', ' . var_export($double_encode, true) .
|
||||
'), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
|
||||
} elseif ($double_encode) {
|
||||
// php <5.2.3 - only handle double encoding
|
||||
return 'mb_convert_encoding(htmlspecialchars(' . $params[ 0 ] . ', ENT_QUOTES, ' .
|
||||
return 'mb_convert_encoding(htmlspecialchars((string)' . $params[ 0 ] . ', ENT_QUOTES, ' .
|
||||
var_export($char_set, true) . '), "HTML-ENTITIES", ' . var_export($char_set, true) . ')';
|
||||
} else {
|
||||
// fall back to modifier.escape.php
|
||||
@@ -68,26 +68,26 @@ function smarty_modifiercompiler_escape($params, Smarty_Internal_TemplateCompile
|
||||
// no MBString fallback
|
||||
if ($_double_encode) {
|
||||
// php >=5.2.3 - go native
|
||||
return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
|
||||
return 'htmlentities((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ', ' .
|
||||
var_export($double_encode, true) . ')';
|
||||
} elseif ($double_encode) {
|
||||
// php <5.2.3 - only handle double encoding
|
||||
return 'htmlentities(' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
|
||||
return 'htmlentities((string)' . $params[ 0 ] . ', ENT_QUOTES, ' . var_export($char_set, true) . ')';
|
||||
} else {
|
||||
// fall back to modifier.escape.php
|
||||
}
|
||||
// no break
|
||||
case 'url':
|
||||
return 'rawurlencode(' . $params[ 0 ] . ')';
|
||||
return 'rawurlencode((string)' . $params[ 0 ] . ')';
|
||||
case 'urlpathinfo':
|
||||
return 'str_replace("%2F", "/", rawurlencode(' . $params[ 0 ] . '))';
|
||||
return 'str_replace("%2F", "/", rawurlencode((string)' . $params[ 0 ] . '))';
|
||||
case 'quotes':
|
||||
// escape unescaped single quotes
|
||||
return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'",' . $params[ 0 ] . ')';
|
||||
return 'preg_replace("%(?<!\\\\\\\\)\'%", "\\\'", (string)' . $params[ 0 ] . ')';
|
||||
case 'javascript':
|
||||
// escape quotes and backslashes, newlines, etc.
|
||||
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
|
||||
return 'strtr(' .
|
||||
return 'strtr((string)' .
|
||||
$params[ 0 ] .
|
||||
', array("\\\\" => "\\\\\\\\", "\'" => "\\\\\'", "\"" => "\\\\\"", "\\r" => "\\\\r", "\\n" => "\\\n", "</" => "<\/", "<!--" => "<\!--", "<s" => "<\s", "<S" => "<\S" ))';
|
||||
}
|
||||
|
||||
@@ -14,20 +14,28 @@
|
||||
* @author Rodney Rehm
|
||||
*
|
||||
* @param array $params parameters
|
||||
* @param Smarty_Internal_TemplateCompilerBase $compiler
|
||||
*
|
||||
* @return string with compiled code
|
||||
*/
|
||||
function smarty_modifiercompiler_unescape($params)
|
||||
function smarty_modifiercompiler_unescape($params, Smarty_Internal_TemplateCompilerBase $compiler)
|
||||
{
|
||||
if (!isset($params[ 1 ])) {
|
||||
$params[ 1 ] = 'html';
|
||||
}
|
||||
$compiler->template->_checkPlugins(
|
||||
array(
|
||||
array(
|
||||
'function' => 'smarty_literal_compiler_param',
|
||||
'file' => SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$esc_type = smarty_literal_compiler_param($params, 1, 'html');
|
||||
|
||||
if (!isset($params[ 2 ])) {
|
||||
$params[ 2 ] = '\'' . addslashes(Smarty::$_CHARSET) . '\'';
|
||||
} else {
|
||||
$params[ 2 ] = "'{$params[ 2 ]}'";
|
||||
}
|
||||
switch (trim($params[ 1 ], '"\'')) {
|
||||
|
||||
switch ($esc_type) {
|
||||
case 'entity':
|
||||
case 'htmlall':
|
||||
if (Smarty::$_MBSTRING) {
|
||||
|
||||
@@ -44,9 +44,43 @@ if (!function_exists('smarty_mb_str_replace')) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$parts = mb_split(preg_quote($search), $subject);
|
||||
$mb_reg_charset = mb_regex_encoding();
|
||||
// Check if mbstring regex is using UTF-8
|
||||
$reg_is_unicode = !strcasecmp($mb_reg_charset, "UTF-8");
|
||||
if(!$reg_is_unicode) {
|
||||
// ...and set to UTF-8 if not
|
||||
mb_regex_encoding("UTF-8");
|
||||
}
|
||||
|
||||
// See if charset used by Smarty is matching one used by regex...
|
||||
$current_charset = mb_regex_encoding();
|
||||
$convert_result = (bool)strcasecmp(Smarty::$_CHARSET, $current_charset);
|
||||
if($convert_result) {
|
||||
// ...convert to it if not.
|
||||
$subject = mb_convert_encoding($subject, $current_charset, Smarty::$_CHARSET);
|
||||
$search = mb_convert_encoding($search, $current_charset, Smarty::$_CHARSET);
|
||||
$replace = mb_convert_encoding($replace, $current_charset, Smarty::$_CHARSET);
|
||||
}
|
||||
|
||||
$parts = mb_split(preg_quote($search), $subject) ?: array();
|
||||
// If original regex encoding was not unicode...
|
||||
if(!$reg_is_unicode) {
|
||||
// ...restore original regex encoding to avoid breaking the system.
|
||||
mb_regex_encoding($mb_reg_charset);
|
||||
}
|
||||
if($parts === false) {
|
||||
// This exception is thrown if call to mb_split failed.
|
||||
// Usually it happens, when $search or $replace are not valid for given mb_regex_encoding().
|
||||
// There may be other cases for it to fail, please file an issue if you find a reproducible one.
|
||||
throw new SmartyException("Source string is not a valid $current_charset sequence (probably)");
|
||||
}
|
||||
|
||||
$count = count($parts) - 1;
|
||||
$subject = implode($replace, $parts);
|
||||
// Convert results back to charset used by Smarty, if needed.
|
||||
if($convert_result) {
|
||||
$subject = mb_convert_encoding($subject, Smarty::$_CHARSET, $current_charset);
|
||||
}
|
||||
}
|
||||
return $subject;
|
||||
}
|
||||
|
||||
@@ -196,8 +196,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
|
||||
*/
|
||||
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
||||
{
|
||||
clearstatcache(true, $cached->lock_id);
|
||||
if (is_file($cached->lock_id)) {
|
||||
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 {
|
||||
|
||||
@@ -125,7 +125,7 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
|
||||
// setup buffer for template function code
|
||||
$compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
|
||||
$output = "<?php\n";
|
||||
$output .= "/* {block {$_name}} */\n";
|
||||
$output .= $compiler->cStyleComment(" {block {$_name}} ") . "\n";
|
||||
$output .= "class {$_className} extends Smarty_Internal_Block\n";
|
||||
$output .= "{\n";
|
||||
foreach ($_block as $property => $value) {
|
||||
@@ -155,7 +155,7 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
|
||||
}
|
||||
$output .= "}\n";
|
||||
$output .= "}\n";
|
||||
$output .= "/* {/block {$_name}} */\n\n";
|
||||
$output .= $compiler->cStyleComment(" {/block {$_name}} ") . "\n\n";
|
||||
$output .= "?>\n";
|
||||
$compiler->parser->current_buffer->append_subtree(
|
||||
$compiler->parser,
|
||||
|
||||
@@ -134,7 +134,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
if ($compiler->template->compiled->has_nocache_code) {
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
|
||||
$output = "<?php\n";
|
||||
$output .= "/* {$_funcNameCaching} */\n";
|
||||
$output .= $compiler->cStyleComment(" {$_funcNameCaching} ") . "\n";
|
||||
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
|
||||
$output .= "function {$_funcNameCaching} (Smarty_Internal_Template \$_smarty_tpl,\$params) {\n";
|
||||
$output .= "ob_start();\n";
|
||||
@@ -157,9 +157,9 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
|
||||
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
|
||||
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
|
||||
$output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
|
||||
$output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash ?? '', ob_get_clean());\n";
|
||||
$output .= "}\n}\n";
|
||||
$output .= "/*/ {$_funcName}_nocache */\n\n";
|
||||
$output .= $compiler->cStyleComment("/ {$_funcName}_nocache ") . "\n\n";
|
||||
$output .= "?>\n";
|
||||
$compiler->parser->current_buffer->append_subtree(
|
||||
$compiler->parser,
|
||||
@@ -179,7 +179,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
}
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
|
||||
$output = "<?php\n";
|
||||
$output .= "/* {$_funcName} */\n";
|
||||
$output .= $compiler->cStyleComment(" {$_funcName} ") . "\n";
|
||||
$output .= "if (!function_exists('{$_funcName}')) {\n";
|
||||
$output .= "function {$_funcName}(Smarty_Internal_Template \$_smarty_tpl,\$params) {\n";
|
||||
$output .= $_paramsCode;
|
||||
@@ -196,7 +196,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
);
|
||||
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
||||
$output = "<?php\n}}\n";
|
||||
$output .= "/*/ {$_funcName} */\n\n";
|
||||
$output .= $compiler->cStyleComment("/ {$_funcName} ") . "\n\n";
|
||||
$output .= "?>\n";
|
||||
$compiler->parser->current_buffer->append_subtree(
|
||||
$compiler->parser,
|
||||
|
||||
@@ -318,14 +318,14 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
||||
}
|
||||
// get compiled code
|
||||
$compiled_code = "<?php\n\n";
|
||||
$compiled_code .= "/* Start inline template \"{$sourceInfo}\" =============================*/\n";
|
||||
$compiled_code .= $compiler->cStyleComment(" Start inline template \"{$sourceInfo}\" =============================") . "\n";
|
||||
$compiled_code .= "function {$tpl->compiled->unifunc} (Smarty_Internal_Template \$_smarty_tpl) {\n";
|
||||
$compiled_code .= "?>\n" . $tpl->compiler->compileTemplateSource($tpl, null, $compiler->parent_compiler);
|
||||
$compiled_code .= "<?php\n";
|
||||
$compiled_code .= "}\n?>\n";
|
||||
$compiled_code .= $tpl->compiler->postFilter($tpl->compiler->blockOrFunctionCode);
|
||||
$compiled_code .= "<?php\n\n";
|
||||
$compiled_code .= "/* End inline template \"{$sourceInfo}\" =============================*/\n";
|
||||
$compiled_code .= $compiler->cStyleComment(" End inline template \"{$sourceInfo}\" =============================") . "\n";
|
||||
$compiled_code .= '?>';
|
||||
unset($tpl->compiler);
|
||||
if ($tpl->compiled->has_nocache_code) {
|
||||
|
||||
@@ -93,7 +93,7 @@ class Smarty_Internal_Compile_Insert extends Smarty_Internal_CompileBase
|
||||
}
|
||||
if (!empty($_dir)) {
|
||||
foreach ((array)$_dir as $_script_dir) {
|
||||
$_script_dir = rtrim($_script_dir, '/\\') . DIRECTORY_SEPARATOR;
|
||||
$_script_dir = rtrim($_script_dir ?? '', '/\\') . DIRECTORY_SEPARATOR;
|
||||
if (file_exists($_script_dir . $_script)) {
|
||||
$_filepath = $_script_dir . $_script;
|
||||
break;
|
||||
|
||||
@@ -93,7 +93,7 @@ class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_C
|
||||
}
|
||||
// autoescape html
|
||||
if ($compiler->template->smarty->escape_html) {
|
||||
$output = "htmlspecialchars({$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
|
||||
$output = "htmlspecialchars((string) {$output}, ENT_QUOTES, '" . addslashes(Smarty::$_CHARSET) . "')";
|
||||
}
|
||||
// loop over registered filters
|
||||
if (!empty($compiler->template->smarty->registered_filters[ Smarty::FILTER_VARIABLE ])) {
|
||||
|
||||
@@ -157,10 +157,12 @@ class Smarty_Internal_Config_File_Compiler
|
||||
$this->smarty->_debug->end_compile($this->template);
|
||||
}
|
||||
// template header code
|
||||
$template_header =
|
||||
"<?php /* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
|
||||
"\n";
|
||||
$template_header .= " compiled from '{$this->template->source->filepath}' */ ?>\n";
|
||||
$template_header = sprintf(
|
||||
"<?php /* Smarty version %s, created on %s\n compiled from '%s' */ ?>\n",
|
||||
Smarty::SMARTY_VERSION,
|
||||
date("Y-m-d H:i:s"),
|
||||
str_replace('*/', '* /' , $this->template->source->filepath)
|
||||
);
|
||||
$code = '<?php $_smarty_tpl->smarty->ext->configLoad->_loadConfigVars($_smarty_tpl, ' .
|
||||
var_export($this->config_data, true) . '); ?>';
|
||||
return $template_header . $this->template->smarty->ext->_codeFrame->create($this->template, $code);
|
||||
|
||||
@@ -44,9 +44,12 @@ class Smarty_Internal_Runtime_CodeFrame
|
||||
$properties[ 'file_dependency' ] = $_template->cached->file_dependency;
|
||||
$properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
|
||||
}
|
||||
$output = "<?php\n";
|
||||
$output .= "/* Smarty version {$properties[ 'version' ]}, created on " . strftime("%Y-%m-%d %H:%M:%S") .
|
||||
"\n from '" . str_replace('*/', '* /', $_template->source->filepath) . "' */\n\n";
|
||||
$output = sprintf(
|
||||
"<?php\n/* Smarty version %s, created on %s\n from '%s' */\n\n",
|
||||
$properties[ 'version' ],
|
||||
date("Y-m-d H:i:s"),
|
||||
str_replace('*/', '* /', $_template->source->filepath)
|
||||
);
|
||||
$output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n";
|
||||
$dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
|
||||
($cache ? 'true' : 'false') . ')';
|
||||
|
||||
@@ -29,12 +29,7 @@ class Smarty_Internal_Runtime_WriteFile
|
||||
{
|
||||
$_error_reporting = error_reporting();
|
||||
error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
|
||||
$_file_perms = property_exists($smarty, '_file_perms') ? $smarty->_file_perms : 0644;
|
||||
$_dir_perms =
|
||||
property_exists($smarty, '_dir_perms') ? (isset($smarty->_dir_perms) ? $smarty->_dir_perms : 0777) : 0771;
|
||||
if ($_file_perms !== null) {
|
||||
$old_umask = umask(0);
|
||||
}
|
||||
$old_umask = umask(0);
|
||||
$_dirpath = dirname($_filepath);
|
||||
// if subdirs, create dir structure
|
||||
if ($_dirpath !== '.') {
|
||||
@@ -42,7 +37,7 @@ class Smarty_Internal_Runtime_WriteFile
|
||||
// loop if concurrency problem occurs
|
||||
// see https://bugs.php.net/bug.php?id=35326
|
||||
while (!is_dir($_dirpath)) {
|
||||
if (@mkdir($_dirpath, $_dir_perms, true)) {
|
||||
if (@mkdir($_dirpath, 0771, true)) {
|
||||
break;
|
||||
}
|
||||
clearstatcache();
|
||||
@@ -89,11 +84,9 @@ class Smarty_Internal_Runtime_WriteFile
|
||||
error_reporting($_error_reporting);
|
||||
throw new SmartyException("unable to write file {$_filepath}");
|
||||
}
|
||||
if ($_file_perms !== null) {
|
||||
// set file permissions
|
||||
chmod($_filepath, $_file_perms);
|
||||
umask($old_umask);
|
||||
}
|
||||
// set file permissions
|
||||
chmod($_filepath, 0644);
|
||||
umask($old_umask);
|
||||
error_reporting($_error_reporting);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -422,9 +422,6 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
try {
|
||||
// save template object in compiler class
|
||||
$this->template = $template;
|
||||
if (property_exists($this->template->smarty, 'plugin_search_order')) {
|
||||
$this->plugin_search_order = $this->template->smarty->plugin_search_order;
|
||||
}
|
||||
if ($this->smarty->debugging) {
|
||||
if (!isset($this->smarty->_debug)) {
|
||||
$this->smarty->_debug = new Smarty_Internal_Debug();
|
||||
@@ -1135,7 +1132,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
flush();
|
||||
}
|
||||
$e = new SmartyCompilerException($error_text);
|
||||
$e->line = $line;
|
||||
$e->setLine($line);
|
||||
$e->source = trim(preg_replace('![\t\r\n]+!', ' ', $match[ $line - 1 ]));
|
||||
$e->desc = $args;
|
||||
$e->template = $this->template->source->filepath;
|
||||
@@ -1439,6 +1436,10 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
*/
|
||||
abstract protected function doCompile($_content, $isTemplateSource = false);
|
||||
|
||||
public function cStyleComment($string) {
|
||||
return '/*' . str_replace('*/', '* /' , $string) . '*/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile Tag
|
||||
*
|
||||
|
||||
@@ -2397,6 +2397,9 @@ public static $yy_action = array(
|
||||
}
|
||||
// line 749 "../smarty/lexer/smarty_internal_templateparser.y"
|
||||
public function yy_r94(){
|
||||
if ($this->security && $this->security->static_classes !== array()) {
|
||||
$this->compiler->trigger_template_error('dynamic static class not allowed by security setting');
|
||||
}
|
||||
$prefixVar = $this->compiler->getNewPrefixVariable();
|
||||
if ($this->yystack[$this->yyidx + -2]->minor['var'] === '\'smarty\'') {
|
||||
$this->compiler->appendPrefixCode("<?php {$prefixVar} = ". $this->compiler->compileTag('private_special_variable',array(),$this->yystack[$this->yyidx + -2]->minor['smarty_internal_index']).';?>');
|
||||
|
||||
@@ -16,12 +16,12 @@ class SmartyCompilerException extends SmartyException
|
||||
}
|
||||
|
||||
/**
|
||||
* The line number of the template error
|
||||
*
|
||||
* @type int|null
|
||||
* @param int $line
|
||||
*/
|
||||
public $line = null;
|
||||
|
||||
public function setLine($line)
|
||||
{
|
||||
$this->line = $line;
|
||||
}
|
||||
/**
|
||||
* The template source snippet relating to the error
|
||||
*
|
||||
|
||||
@@ -30,7 +30,8 @@ class CacheResourceTestCommon extends PHPUnit_Smarty
|
||||
|
||||
public function compiledPrefilter($text, Smarty_Internal_Template $tpl)
|
||||
{
|
||||
return str_replace('#', $tpl->getTemplateVars('test'), $text);
|
||||
$replace = $tpl->getTemplateVars('test');
|
||||
return str_replace('#', $replace ?? '', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ class Smarty_Resource_Ambiguous extends Smarty_Internal_Resource_File
|
||||
|
||||
public function __construct($directory)
|
||||
{
|
||||
$this->directory = rtrim($directory, "/\\") . DIRECTORY_SEPARATOR;
|
||||
$this->directory = rtrim($directory ?? '', "/\\") . DIRECTORY_SEPARATOR;
|
||||
// parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ class ResourceStream
|
||||
$v = &$GLOBALS[$this->varname];
|
||||
$l = strlen($data);
|
||||
$p = &$this->position;
|
||||
$v = substr($v, 0, $p) . $data . substr($v, $p += $l);
|
||||
$v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l);
|
||||
|
||||
return $l;
|
||||
}
|
||||
|
||||
@@ -257,19 +257,41 @@ class SecurityTest extends PHPUnit_Smarty
|
||||
$this->assertEquals('25', $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
/**
|
||||
* test not trusted PHP function
|
||||
* @runInSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
public function testNotTrustedStaticClass()
|
||||
{
|
||||
/**
|
||||
* test not trusted PHP function
|
||||
* @runInSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
*/
|
||||
public function testNotTrustedStaticClass()
|
||||
{
|
||||
$this->expectException('SmartyException');
|
||||
$this->expectExceptionMessage('access to static class \'mysecuritystaticclass\' not allowed by security setting');
|
||||
$this->smarty->security_policy->static_classes = array('null');
|
||||
$this->smarty->fetch('string:{mysecuritystaticclass::square(5)}');
|
||||
}
|
||||
|
||||
/**
|
||||
* test not trusted PHP function
|
||||
*/
|
||||
public function testNotTrustedStaticClassEval()
|
||||
{
|
||||
$this->expectException('SmartyException');
|
||||
$this->expectExceptionMessage('dynamic static class not allowed by security setting');
|
||||
$this->smarty->security_policy->static_classes = array('null');
|
||||
$this->smarty->fetch('string:{$test = "mysecuritystaticclass"}{$test::square(5)}');
|
||||
}
|
||||
|
||||
/**
|
||||
* test not trusted PHP function
|
||||
*/
|
||||
public function testNotTrustedStaticClassSmartyVar()
|
||||
{
|
||||
$this->expectException('SmartyException');
|
||||
$this->expectExceptionMessage('dynamic static class not allowed by security setting');
|
||||
$this->smarty->security_policy->static_classes = array('null');
|
||||
$this->smarty->fetch('string:{$smarty.template_object::square(5)}');
|
||||
}
|
||||
|
||||
public function testChangedTrustedDirectory()
|
||||
{
|
||||
$this->smarty->security_policy->secure_dir = array(
|
||||
@@ -344,9 +366,9 @@ class SecurityTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
* In security mode, accessing $smarty.template_object should be illegal.
|
||||
* @expectedException SmartyCompilerException
|
||||
*/
|
||||
public function testSmartyTemplateObject() {
|
||||
$this->expectException(SmartyCompilerException::class);
|
||||
$this->smarty->display('string:{$smarty.template_object}');
|
||||
}
|
||||
|
||||
@@ -394,7 +416,7 @@ class ResourceStreamSecurity
|
||||
$v = &$GLOBALS[$this->varname];
|
||||
$l = strlen($data);
|
||||
$p = &$this->position;
|
||||
$v = substr($v, 0, $p) . $data . substr($v, $p += $l);
|
||||
$v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l);
|
||||
|
||||
return $l;
|
||||
}
|
||||
|
||||
+1
@@ -210,6 +210,7 @@ class PluginFunctionHtmlSelectDateTest extends PHPUnit_Smarty
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->setUpSmarty(dirname(__FILE__));
|
||||
$this->smarty->setErrorReporting(E_ALL & ~E_DEPRECATED);
|
||||
|
||||
$year = date('Y');
|
||||
$this->now = mktime(15, 0, 0, 2, 20, $year);
|
||||
|
||||
@@ -54,7 +54,7 @@ class PluginFunctionMailtoTest extends PHPUnit_Smarty
|
||||
|
||||
public function testEncodeJavascript()
|
||||
{
|
||||
$result = '<script type="text/javascript">eval(unescape(\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e%27%29%3b\'))</script>';
|
||||
$result = '<script type="text/javascript">document.write(unescape(\'%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e\'))</script>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{mailto address="me@example.com" encode="javascript"}');
|
||||
$this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl));
|
||||
}
|
||||
@@ -62,7 +62,7 @@ class PluginFunctionMailtoTest extends PHPUnit_Smarty
|
||||
public function testEncodeJavascriptWithoutMbstring()
|
||||
{
|
||||
Smarty::$_MBSTRING = false;
|
||||
$result = '<script type="text/javascript">eval(unescape(\'%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e%27%29%3b\'))</script>';
|
||||
$result = '<script type="text/javascript">document.write(unescape(\'%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%20%3e%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%3c%2f%61%3e\'))</script>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{mailto address="me@example.com" encode="javascript"}');
|
||||
$this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl));
|
||||
Smarty::$_MBSTRING = true;
|
||||
@@ -70,7 +70,7 @@ class PluginFunctionMailtoTest extends PHPUnit_Smarty
|
||||
|
||||
public function testEncodeJavascriptCharcode()
|
||||
{
|
||||
$result = "<script type=\"text/javascript\" language=\"javascript\">\n{document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62))}\n</script>\n";
|
||||
$result = '<script type="text/javascript">document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62))</script>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{mailto address="me@example.com" encode="javascript_charcode"}');
|
||||
$this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl));
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class PluginFunctionMailtoTest extends PHPUnit_Smarty
|
||||
public function testEncodeJavascriptCharcodeWithoutMbstring()
|
||||
{
|
||||
Smarty::$_MBSTRING = false;
|
||||
$result = "<script type=\"text/javascript\" language=\"javascript\">\n{document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62))}\n</script>\n";
|
||||
$result = '<script type="text/javascript">document.write(String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111,58,109,101,64,101,120,97,109,112,108,101,46,99,111,109,34,32,62,109,101,64,101,120,97,109,112,108,101,46,99,111,109,60,47,97,62))</script>';
|
||||
$tpl = $this->smarty->createTemplate('eval:{mailto address="me@example.com" encode="javascript_charcode"}');
|
||||
$this->assertEquals(str_replace("\r", '', $result), $this->smarty->fetch($tpl));
|
||||
Smarty::$_MBSTRING = true;
|
||||
|
||||
+7
-2
@@ -63,6 +63,11 @@ class PluginModifierUnescapeTest extends PHPUnit_Smarty
|
||||
$tpl = $this->smarty->createTemplate('string:{"' . $encoded . '"|unescape:"url"}');
|
||||
$this->assertEquals($result, $this->smarty->fetch($tpl));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
public function testCharset()
|
||||
{
|
||||
$tpl = $this->smarty->createTemplate("string:{''Stiff Opposition Expected to Casketless Funeral Plan''|unescape:'htmlall':'utf-8'}");
|
||||
$this->assertEquals("'Stiff Opposition Expected to Casketless Funeral Plan'", $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -435,9 +435,9 @@ class CompileFunctionTest extends PHPUnit_Smarty
|
||||
|
||||
/**
|
||||
* Test handling of function names that are a security risk
|
||||
* @expectedException SmartyCompilerException
|
||||
*/
|
||||
public function testIllegalFunctionName() {
|
||||
$this->expectException(SmartyCompilerException::class);
|
||||
$this->smarty->fetch('string:{function name=\'rce(){};echo "hi";function \'}{/function}');
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,30 @@ class MathTest extends PHPUnit_Smarty
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testMultipleOperators()
|
||||
{
|
||||
$this->smarty->disableSecurity();
|
||||
$expected = "2 -- 2";
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = 5}{$y = 4}{math equation="x - y + 1" x=$x y=$y} -- {math equation="5 - 4 + 1"}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testMathMaxFunctionParameters()
|
||||
{
|
||||
$this->smarty->disableSecurity();
|
||||
$expected = max(0, 2) . ' -- ' . max(0, 2, 3);
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = 0}{$y = 2}{$z = 3}{math equation="max(x, y)" x=$x y=$y} -- {math equation="max(x, y, z)" x=$x y=$y z=$z}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testMathMinFunctionParameters()
|
||||
{
|
||||
$this->smarty->disableSecurity();
|
||||
$expected = min(1, 2) . ' -- ' . min(1, 2, 0);
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = 1}{$y = 2}{$z = 0}{math equation="min(x, y)" x=$x y=$y} -- {math equation="min(x, y, z)" x=$x y=$y z=$z}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testSyntaxSin()
|
||||
{
|
||||
$this->smarty->disableSecurity();
|
||||
@@ -107,4 +131,29 @@ class MathTest extends PHPUnit_Smarty
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = "4"}{$y = "5.5"}{math equation="x * y" x=$x y=$y format="%0.2f"} -- {math equation="20.5 / 5" format="%0.2f"}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testBackticksIllegal()
|
||||
{
|
||||
$this->expectException(PHPUnit\Framework\Error\Warning::class);
|
||||
$expected = "22.00";
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = "4"}{$y = "5.5"}{math equation="`ls` x * y" x=$x y=$y}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testDollarSignsIllegal()
|
||||
{
|
||||
$this->expectException(PHPUnit\Framework\Error\Warning::class);
|
||||
$expected = "22.00";
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = "4"}{$y = "5.5"}{math equation="$" x=$x y=$y}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
public function testBracketsIllegal()
|
||||
{
|
||||
$this->expectException(PHPUnit\Framework\Error\Warning::class);
|
||||
$expected = "I";
|
||||
$tpl = $this->smarty->createTemplate('eval:{$x = "0"}{$y = "1"}{math equation="((y/x).(x))[x]" x=$x y=$y}');
|
||||
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ class VariableStream
|
||||
$v = &$GLOBALS[$this->varname];
|
||||
$l = strlen($data);
|
||||
$p = &$this->position;
|
||||
$v = substr($v, 0, $p) . $data . substr($v, $p += $l);
|
||||
$v = substr($v ?? '', 0, $p) . $data . substr($v ?? '', $p += $l);
|
||||
|
||||
return $l;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Smarty PHPunit tests - issue #549 regression tests
|
||||
*
|
||||
* @package PHPunit
|
||||
* @author Andrey Repin <anrdaemon@yandex.ru>
|
||||
*/
|
||||
|
||||
/**
|
||||
* class for compiler tests
|
||||
*
|
||||
* @runTestsInSeparateProcess
|
||||
* @preserveGlobalState disabled
|
||||
* @backupStaticAttributes enabled
|
||||
*
|
||||
* mb_split breaks if Smarty encoding is not the same as mbstring regex encoding.
|
||||
*/
|
||||
class MbSplitEncodingIssue549Test extends PHPUnit_Smarty
|
||||
{
|
||||
/** @var string Saved Smarty charset */
|
||||
private $charset;
|
||||
|
||||
/** @var array Source data for tests, hexed to protect from accidental reencoding */
|
||||
private $data = array(
|
||||
"subject" => '4772c3bc6e6577616c64', // "Grünewald"
|
||||
"pattern" => '77616c64', // "wald"
|
||||
"replacement" => '7374c3bc726d', // "stürm"
|
||||
"result" => '4772c3bc6e657374c3bc726d', // "Grünestürm"
|
||||
);
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
if(!\Smarty::$_MBSTRING)
|
||||
{
|
||||
$this->markTestSkipped("mbstring extension is not in use by Smarty");
|
||||
}
|
||||
|
||||
$this->charset = \Smarty::$_CHARSET;
|
||||
$this->setUpSmarty(dirname(__FILE__));
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
\Smarty::$_CHARSET = $this->charset ?: \Smarty::$_CHARSET;
|
||||
$this->cleanDirs();
|
||||
}
|
||||
|
||||
/** Provider for testReplaceModifier
|
||||
*/
|
||||
public function encodingPairsProvider()
|
||||
{
|
||||
return array(
|
||||
"with non-UNICODE src/non-UNICODE regex (PHP < 5.6 default)" => array("Windows-1252", "EUC-JP"),
|
||||
"with UTF-8 src/non-UNICODE regex (PHP < 5.6 default)" => array("UTF-8", "EUC-JP"),
|
||||
"with UTF-8 src/UTF-8 regex (PHP >= 5.6)" => array("UTF-8", "UTF-8"),
|
||||
"with non-UNICODE src/UTF-8 regex" => array("Windows-1252", "UTF-8"),
|
||||
);
|
||||
}
|
||||
|
||||
/** Test behavior of `replace` modifier with different source and regex encodings
|
||||
*
|
||||
* @dataProvider encodingPairsProvider
|
||||
*/
|
||||
public function testReplaceModifier($mb_int_encoding, $mb_regex_encoding)
|
||||
{
|
||||
$data = $this->data;
|
||||
\array_walk($data, function(&$value, $key) use($mb_int_encoding) {
|
||||
$value = \mb_convert_encoding(pack("H*", $value), $mb_int_encoding, "UTF-8");
|
||||
});
|
||||
\extract($data, \EXTR_SKIP);
|
||||
|
||||
\mb_regex_encoding($mb_regex_encoding);
|
||||
\Smarty::$_CHARSET = $mb_int_encoding;
|
||||
$this->assertEquals($result, $this->smarty->fetch("string:{\"$subject\"|replace:\"$pattern\":\"$replacement\"}"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# Ignore anything in here, but keep this directory
|
||||
*
|
||||
@@ -0,0 +1,2 @@
|
||||
# Ignore anything in here, but keep this directory
|
||||
*
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:7.1-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:7.2-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:7.3-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:7.4-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:8.0-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM php:8.1-cli
|
||||
|
||||
## Basic utilities
|
||||
RUN apt-get update -yqq && apt-get install -y curl apt-utils git zip unzip
|
||||
|
||||
## Composer
|
||||
COPY ./utilities/testrunners/shared/install-composer.sh /root/install-composer.sh
|
||||
WORKDIR /root
|
||||
RUN sh ./install-composer.sh
|
||||
RUN mv ./composer.phar /usr/local/bin/composer
|
||||
Executable
+2
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
composer update && php ./vendor/phpunit/phpunit/phpunit
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
|
||||
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
|
||||
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
|
||||
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
|
||||
|
||||
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
|
||||
then
|
||||
>&2 echo 'ERROR: Invalid installer checksum'
|
||||
rm composer-setup.php
|
||||
exit 1
|
||||
fi
|
||||
|
||||
php composer-setup.php --quiet
|
||||
RESULT=$?
|
||||
rm composer-setup.php
|
||||
exit $RESULT
|
||||
Reference in New Issue
Block a user