|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asymptix\core; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Content manipulations functionality. |
|
7
|
|
|
* |
|
8
|
|
|
* @category Asymptix PHP Framework |
|
9
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
|
10
|
|
|
* @copyright (c) 2015 - 2016, Dmytro Zarezenko |
|
11
|
|
|
* |
|
12
|
|
|
* @git https://github.com/Asymptix/Framework |
|
13
|
|
|
* @license http://opensource.org/licenses/MIT |
|
14
|
|
|
*/ |
|
15
|
|
|
class Content { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Renders some template to the string value. |
|
19
|
|
|
* You could use $_TPL list inside the template to pass variables to the |
|
20
|
|
|
* template. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $tplPath Path to the template. |
|
23
|
|
|
* @param array $tplVariables Variables list. |
|
24
|
|
|
* |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function render($tplPath, $tplVariables = []) { |
|
28
|
|
|
$_TPL = $tplVariables; |
|
29
|
|
|
|
|
30
|
|
|
ob_start(); |
|
31
|
|
|
include($tplPath); |
|
32
|
|
|
$content = ob_get_contents(); |
|
33
|
|
|
ob_end_clean(); |
|
34
|
|
|
|
|
35
|
|
|
return $content; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Performs str_replace() on a content. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $content Content. |
|
42
|
|
|
* @param array $tplVariables Replacements list, keys will be replaced by |
|
43
|
|
|
* values. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function replace($content, $tplVariables = []) { |
|
48
|
|
|
return str_replace( |
|
49
|
|
|
array_keys($tplVariables), |
|
50
|
|
|
array_values($tplVariables), |
|
51
|
|
|
$content |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns XSS secured string before output it (use htmlspecialchars()). |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $value Value to secure. |
|
59
|
|
|
* @param int $flags A bitmask of one or more of the following flags, |
|
60
|
|
|
* which specify how to handle quotes, invalid code unit sequences |
|
61
|
|
|
* and the used document type. The default is ENT_QUOTES. |
|
62
|
|
|
* @param string $encoding An optional argument defining the encoding used |
|
63
|
|
|
* when converting characters. The default is 'UTF-8'. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string The converted string. If the input string contains an |
|
66
|
|
|
* invalid code unit sequence within the given encoding an empty |
|
67
|
|
|
* string will be returned, unless either the ENT_IGNORE or |
|
68
|
|
|
* ENT_SUBSTITUTE flags are set. |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function secure($value, $flags = ENT_QUOTES, $encoding = 'UTF-8') { |
|
71
|
|
|
return htmlspecialchars($value, $flags, $encoding, false); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|