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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
<?php namespace Imagify\WriteFile;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Abstract class used to add and remove contents to the web.config file. * * @since 1.9 * @author Grégory Viguier */ abstract class AbstractIISDirConfFile extends AbstractWriteDirConfFile {
/** * Insert new contents into the directory conf file. * Replaces existing marked info. Creates file if none exists. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $new_contents Contents to insert. * @return bool|\WP_Error True on write success, a \WP_Error object on failure. */ protected function insert_contents( $new_contents ) { $doc = $this->get_file_contents();
if ( is_wp_error( $doc ) ) { return $doc; }
$marker = static::TAG_NAME; $xpath = new \DOMXPath( $doc );
// Remove previous rules. $old_nodes = $xpath->query( ".//*[starts-with(@name,'$marker')]" );
if ( $old_nodes->length > 0 ) { foreach ( $old_nodes as $old_node ) { $old_node->parentNode->removeChild( $old_node ); } }
// No new contents? Stop here. if ( ! $new_contents ) { return $this->put_file_contents( $doc ); }
$new_contents = preg_split( '/<!--\s+@parent\s+(.+?)\s+-->/', $new_contents, -1, PREG_SPLIT_DELIM_CAPTURE ); unset( $new_contents[0] ); $new_contents = array_chunk( $new_contents, 2 );
foreach ( $new_contents as $i => $new_content ) { $path = rtrim( $new_content[0], '/' ); $new_content = trim( $new_content[1] );
if ( '' === $new_content ) { continue; }
$fragment = $doc->createDocumentFragment(); $fragment->appendXML( $new_content );
$this->get_node( $doc, $xpath, $path, $fragment ); }
return $this->put_file_contents( $doc ); }
/** * Get the unfiltered path to the file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return string */ protected function get_raw_file_path() { return $this->filesystem->get_site_root() . 'web.config'; }
/** ----------------------------------------------------------------------------------------- */ /** OTHER TOOLS ============================================================================= */ /** ----------------------------------------------------------------------------------------- */
/** * Tell if the file is writable. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|\WP_Error True if writable. A \WP_Error object if not. */ public function is_file_writable() { $file_path = $this->get_file_path(); $file_name = $this->filesystem->make_path_relative( $file_path );
if ( $this->is_conf_edition_disabled() ) { return new \WP_Error( 'edition_disabled', sprintf( /* translators: %s is a file name. */ __( 'Edition of the %s file is disabled.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
if ( ! class_exists( '\DOMDocument' ) ) { return new \WP_Error( 'not_domdocument', sprintf( /* translators: 1 is a php class name, 2 is a file name. */ __( 'The class %1$s is not present on your server, a %2$s file cannot be created nor edited.', 'imagify' ), '<code>DOMDocument</code>', '<code>' . esc_html( $file_name ) . '</code>' ) ); }
if ( ! $this->filesystem->exists( $file_path ) ) { $dir_path = $this->filesystem->dir_path( $file_path );
$this->filesystem->make_dir( $dir_path );
if ( ! $this->filesystem->is_writable( $dir_path ) ) { return new \WP_Error( 'parent_not_writable', sprintf( /* translators: %s is a file name. */ __( '%s’s parent folder is not writable.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); } if ( ! $this->filesystem->exists( $file_path ) ) { $result = $this->filesystem->put_contents( $file_path, '<configuration/>' );
if ( ! $result ) { return new \WP_Error( 'not_created', sprintf( /* translators: %s is a file name. */ __( 'The %s file could not be created.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); } } } elseif ( ! $this->filesystem->is_writable( $file_path ) ) { return new \WP_Error( 'not_writable', sprintf( /* translators: %s is a file name. */ __( 'The %s file is not writable.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
return true; }
/** * Get the file contents. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return \DOMDocument|\WP_Error A \DOMDocument object on success, a \WP_Error object on failure. */ protected function get_file_contents() { $writable = $this->is_file_writable();
if ( is_wp_error( $writable ) ) { return $writable; }
$file_path = $this->get_file_path(); $doc = new \DOMDocument();
$doc->preserveWhiteSpace = false;
if ( false === $doc->load( $file_path ) ) { $file_path = $this->get_file_path(); $file_name = $this->filesystem->make_path_relative( $file_path );
return new \WP_Error( 'not_read', sprintf( /* translators: %s is a file name. */ __( 'The %s file could not be read.', 'imagify' ), '<code>' . esc_html( $file_name ) . '</code>' ) ); }
return $doc; }
/** * Put new contents into the file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param \DOMDocument $contents A \DOMDocument object. * @return bool|\WP_Error True on success, a \WP_Error object on failure. */ protected function put_file_contents( $contents ) { $contents->encoding = 'UTF-8'; $contents->formatOutput = true;
saveDomDocument( $contents, $this->get_file_path() );
return true; }
/** * Get a DOMNode node. * If it does not exist it is created recursively. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param \DOMDocument $doc A \DOMDocument element. * @param \DOMXPath $xpath A \DOMXPath element. * @param string $path Path to the desired node. * @param \DOMNode $child A \DOMNode to be prepended. * @return \DOMNode The \DOMNode node. */ protected function get_node( $doc, $xpath, $path, $child ) { $nodelist = $xpath->query( $path );
if ( $nodelist->length > 0 ) { return $this->prepend_node( $nodelist->item( 0 ), $child ); }
$path = explode( '/', $path ); $node = array_pop( $path ); $path = implode( '/', $path );
$final_node = $doc->createElement( $node );
if ( $child ) { $final_node->appendChild( $child ); }
return $this->get_node( $doc, $xpath, $path, $final_node ); }
/** * Prepend a DOMNode node. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param \DOMNode $container_node The \DOMNode that will contain the new node. * @param \DOMNode $new_node The \DOMNode to be prepended. * @return \DOMNode The \DOMNode containing the new node. */ protected function prepend_node( $container_node, $new_node ) { if ( ! $new_node ) { return $container_node; }
if ( $container_node->hasChildNodes() ) { $container_node->insertBefore( $new_node, $container_node->firstChild ); } else { $container_node->appendChild( $new_node ); }
return $container_node; } }
|