-
Notifications
You must be signed in to change notification settings - Fork 5
/
HtaccessUtils.php
239 lines (213 loc) · 8.79 KB
/
HtaccessUtils.php
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
<?php
namespace WebSharks\ZenCache\Pro;
/*
* Unique comment marker.
*
* @since 151220 Enhancing `.htaccess` tweaks.
*
* @return string Used in `.htaccess` parsing.
*/
$self->htaccess_marker = 'WmVuQ2FjaGU';
/*
* Add template blocks to `/.htaccess` file.
*
* @since 151114 Adding `.htaccess` tweaks.
*
* @return boolean True if added successfully.
*/
$self->addWpHtaccess = function () use ($self) {
global $is_apache;
if (!$is_apache) {
return false; // Not running the Apache web server.
}
if (!$self->options['enable']) {
return false; // Nothing to do.
}
if (!$self->removeWpHtaccess()) {
return false; // Unable to remove.
}
if (!($htaccess = $self->readHtaccessFile())) {
return false; // Failure; could not read file or invalid UTF8 encountered, file may be corrupt.
}
$template_blocks = ''; // Initialize.
if (is_dir($templates_dir = dirname(dirname(dirname(__FILE__))).'/templates/htaccess')) {
foreach (scandir($templates_dir) as $_template_file) {
switch ($_template_file) {
/*[pro strip-from="lite"]*/
case 'cdn-filters.txt':
if ($self->options['cdn_enable']) {
$template_blocks .= trim(file_get_contents($templates_dir.'/'.$_template_file))."\n";
} // Only if CDN filters are enabled at this time.
break;
/*[/pro]*/
}
}
unset($_template_file); // Housekeeping.
}
if(empty($template_blocks)) { // Do we need to add anything to htaccess?
$self->closeHtaccessFile($htaccess); // No need to write to htaccess file in this case.
return true; // Nothing to do, but no failures either.
}
$template_header = '# BEGIN '.NAME.' '.$self->htaccess_marker.' (the '.$self->htaccess_marker.' marker is required for '.NAME.'; do not remove)'."\n";
$template_footer = '# END '.NAME.' '.$self->htaccess_marker;
$htaccess['file_contents'] = $template_header.trim($template_blocks)."\n".$template_footer."\n\n".$htaccess['file_contents'];
if (!$self->writeHtaccessFile($htaccess, true)) {
return false; // Failure; could not write changes.
}
return true; // Added successfully.
};
/*
* Remove template blocks from `/.htaccess` file.
*
* @since 151114 Adding `.htaccess` tweaks.
*
* @return boolean True if removed successfully.
*/
$self->removeWpHtaccess = function () use ($self) {
global $is_apache;
if (!$is_apache) {
return false; // Not running the Apache web server.
}
if (!($htaccess_file = $self->findHtaccessFile())) {
return true; // File does not exist.
}
if (!$self->findHtaccessMarker()) {
return true; // Template blocks are already gone.
}
if (!($htaccess = $self->readHtaccessFile())) {
return false; // Failure; could not read file, create file, or invalid UTF8 encountered, file may be corrupt.
}
$regex = '/#\s*BEGIN\s+'.preg_quote(NAME, '/').'\s+'.$self->htaccess_marker.'.*?#\s*END\s+'.preg_quote(NAME, '/').'\s+'.$self->htaccess_marker.'\s*/is';
$htaccess['file_contents'] = preg_replace($regex, '', $htaccess['file_contents']);
if (!$self->writeHtaccessFile($htaccess, false)) {
return false; // Failure; could not write changes.
}
return true; // Removed successfully.
};
/*
* Finds absolute server path to `/.htaccess` file.
*
* @since 151114 Adding `.htaccess` tweaks.
*
* @return string Absolute server path to `/.htaccess` file;
* else an empty string if unable to locate the file.
*/
$self->findHtaccessFile = function () use ($self) {
$file = ''; // Initialize.
$home_path = $self->wpHomePath();
if (is_file($htaccess_file = $home_path.'.htaccess')) {
$file = $htaccess_file;
}
return $file;
};
/*
* Utility method used to unlock and close htaccess file resource.
*
* @since 151114 Adding `.htaccess` tweaks.
*
* @param array $htaccess Array containing at least an `fp` file resource pointing to htaccess file.
*
* @return bool False on failure, true otherwise.
*/
$self->closeHtaccessFile = function (array $htaccess) use ($self) {
if (!is_resource($htaccess['fp'])) {
return false; // Failure; requires a valid file resource.
}
flock($htaccess['fp'], LOCK_UN);
fclose($htaccess['fp']);
return true;
};
/*
* Utility method used to check if htaccess file contains $htaccess_marker
*
* @since 151114 Adding `.htaccess` tweaks.
*
* @param string $htaccess_marker Unique comment marker used to identify rules added by this plugin.
*
* @return bool False on failure or when marker does not exist in htaccess, true otherwise.
*/
$self->findHtaccessMarker = function ($htaccess_marker = '') use ($self) {
if (!($htaccess_file = $self->findHtaccessFile())) {
return false; // File does not exist.
}
if (!is_readable($htaccess_file)) {
return false; // Not possible.
}
if (($htaccess_file_contents = file_get_contents($htaccess_file)) === false) {
return false; // Failure; could not read file.
}
if (empty($htaccess_marker)) {
$htaccess_marker = $self->htaccess_marker;
}
if (stripos($htaccess_file_contents, $htaccess_marker) === false) {
return false; // Htaccess marker is missing
}
return true; // Htaccess has the marker
};
/*
* Gets contents of `/.htaccess` file with exclusive lock to read+write. If file doesn't exist, we attempt to create it.
*
* @since 151220 Improving `.htaccess` utils.
*
* @param string $htaccess_file Absolute path to the htaccess file. Optional.
* If not provided, we attempt to find it or create it if it doesn't exist.
*
* @return array|bool Returns an array with data necessary to call $self->writeHtaccessFile():
* `fp` a file pointer resource, `file_contents` a string. Returns `false` on failure.
*
* @note If a call to this method is not followed by a call to $self->writeHtaccessFile(),
* you must make sure that you unlock and close the `fp` resource yourself.
*/
$self->readHtaccessFile = function ($htaccess_file = '') use ($self) {
if (empty($htaccess_file) && !($htaccess_file = $self->findHtaccessFile())) {
if (!is_writable($self->wpHomePath()) || file_put_contents($htaccess_file = $self->wpHomePath().'.htaccess', '') === false) {
return false; // Unable to find and/or create `.htaccess`.
} // If it doesn't exist, we create the `.htaccess` file here.
}
if (!is_readable($htaccess_file) || !is_writable($htaccess_file) || (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)) {
return false; // Not possible.
}
if (!($fp = fopen($htaccess_file, 'rb+')) || !flock($fp, LOCK_EX)) {
fclose($fp); // Just in case we opened it before failing to obtain a lock.
return false; // Failure; could not open file and obtain an exclusive lock.
}
if (($file_contents = fread($fp, filesize($htaccess_file))) && ($file_contents === wp_check_invalid_utf8($file_contents))) {
rewind($fp); // Rewind pointer to beginning of file.
return compact('fp', 'file_contents');
} else { // Failure; could not read file or invalid UTF8 encountered, file may be corrupt.
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
};
/*
* Writes to `/.htaccess` file using provided file pointer.
*
* @since 151220 Improving `.htaccess` utils.
*
* @param array $htaccess Array containing `fp` file resource pointing to htaccess file and `file_contents` to write to file.
* @param bool $require_marker Whether or not to require the marker be present in contents before writing.
* @param string $htaccess_marker Unique comment marker used to identify rules added by this plugin.
*
* @return bool True on success, false on failure.
*/
$self->writeHtaccessFile = function (array $htaccess, $require_marker = true, $htaccess_marker = '') use ($self) {
if (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS) {
return false; // Not possible.
}
if (!is_resource($htaccess['fp'])) {
return false;
}
$htaccess_marker = $htaccess_marker ?: $self->htaccess_marker;
$_have_marker = stripos($htaccess['file_contents'], $htaccess_marker);
// Note: rewind() necessary here because we fread() above.
if (($require_marker && $_have_marker === false) || !rewind($htaccess['fp']) || !ftruncate($htaccess['fp'], 0) || !fwrite($htaccess['fp'], $htaccess['file_contents'])) {
flock($htaccess['fp'], LOCK_UN);
fclose($htaccess['fp']);
return false; // Failure; could not write changes.
}
fflush($htaccess['fp']);
flock($htaccess['fp'], LOCK_UN);
fclose($htaccess['fp']);
return true;
};