-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathScript.php
More file actions
231 lines (187 loc) · 5.8 KB
/
Copy pathScript.php
File metadata and controls
231 lines (187 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/*
* Textpattern Content Management System
* https://textpattern.com/
*
* Copyright (C) 2026 The Textpattern Development Team
*
* This file is part of Textpattern.
*
* Textpattern is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2.
*
* Textpattern is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* A CSP-aware <script> tag.
*
* Replaces script_js().
*
* @since 4.9.0
* @package UI
*/
namespace Textpattern\UI;
class Script extends Tag implements UIInterface
{
/**
* Route this script tag only on these event(s).
*
* @var null|array
*/
protected $targetEvent = null;
/**
* Route this script tag only on these step(s).
*
* @var null|array
*/
protected $targetStep = null;
/**
* Content store. Can be added to and flushed at will.
*
* @var string
*/
protected static $store = '';
/**
* Content for the <noscript> tag.
*
* @var string
*/
protected $noscript = null;
/**
* Location for the src attribute.
*
* Overrides any content added to the tag.
*
* @var string
*/
protected $src = null;
/**
* Whether to add cache-busting content (version/date) to the src link
*
* @var array
*/
protected $append = array(
'version' => false,
'date' => false, // @todo. Unsupported at present
);
/**
* Construct content for the script tag.
*
* If <script> tags are passed in as content, they are removed.
*
* @param string $content The script content, without surrounding script tags
*/
public function __construct($content = null)
{
parent::__construct('script');
if ($content !== null) {
$this->setContent($content);
}
}
/**
* Set/append content between the tags. Chainable.
*
* Call this multiple times to append.
*
* @param string $content Content to set
*/
public function setContent($content, $flush = null)
{
$content = preg_replace('#<(/?)(script)#i', '\\x3c$1$2', $content);
if ($flush === null) {
$this->content = $content;
} elseif ($flush === false) {
self::$store .= n.$content.n;
} elseif ($flush === true) {
$this->content = self::$store.n.$content;
self::$store = '';
}
return $this;
}
/**
* Set content for the noscript tag. Chainable.
*
* @param string $content Content to set when scripting is unavailable
*/
public function setNoscript($content)
{
$this->noscript = n.$content.n;
return $this;
}
/**
* Set the events/steps to which this script tag will be attached. Chainable.
*
* @param array|string $evt Array or comma-separated list of events for this tag
* @param array|string $stp Array or comma-separated list of steps for this tag
*/
public function setRoute($evt = null, $stp = null)
{
$this->targetEvent = empty($evt) ? null : (is_array($evt) ? $evt : do_list_unique($evt));
$this->targetStep = empty($stp) ? null : (is_array($stp) ? $stp : do_list_unique($stp));
return $this;
}
/**
* Set the source script. Chainable.
*
* Overrides any content added to the tag.
*
* Note that appending info to the source only works for stable releases, not dev.
*
* @param string $src Source of the script
* @param string $append Cache-busting content to add ('version' or 'date')
*/
public function setSource($src, $append = null)
{
$this->src = (string)$src;
if ($append && array_key_exists($append, $this->append) && strpos(txp_version, '-dev') === false) {
$this->append[$append] = true;
}
return $this;
}
/**
* Render the tag.
*
* @return string HTML
*/
public function render($flavour = 'complete')
{
global $event, $step, $csp_nonce;
if (
($this->targetEvent === null || in_array($event, $this->targetEvent)) &&
($this->targetStep === null || in_array($step, $this->targetStep))
) {
// Include the nonce if a script-src element uses it in
// the Content Security Policy.
if ($csp_nonce && preg_match_all("/script-src(-elem|-attr)?\s+('[a-zA-Z0-9\-]+'\s+)*'nonce-.*?(?=;)/", CONTENT_SECURITY_POLICY) > 0) {
$this->setAtt('nonce', $csp_nonce);
}
if ($this->src) {
if ($this->append['version']) {
$ext = pathinfo($this->src, PATHINFO_EXTENSION);
if ($ext) {
$this->src = substr($this->src, 0, (strlen($ext) + 1) * -1);
$ext = '.'.$ext;
}
$this->src .= '.v'.txp_version.$ext;
}
$this->setAtt('src', $this->src);
return n.parent::render('complete');
}
$out = n.parent::render('complete');
if ($this->noscript) {
$noscript = new \Textpattern\UI\Tag('noscript');
$noscript->setContent($this->noscript);
$out .= n.$noscript->render('complete');
}
return $out;
}
return '';
}
}