| 1 |
<?php |
| 2 |
/** |
| 3 |
* The htaccess manager. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Cache_Delivery\Htaccess; |
| 11 |
|
| 12 |
use SolidWP\Performance\Cache_Delivery\Cache_Delivery_Type; |
| 13 |
use SolidWP\Performance\Config\Config; |
| 14 |
use SolidWP\Performance\Cache_Delivery\Contracts\Writable; |
| 15 |
use SolidWP\Performance\Cache_Delivery\Contracts\Readable; |
| 16 |
use SolidWP\Performance\Cache_Delivery\Exceptions\CacheDeliveryReadException; |
| 17 |
use SolidWP\Performance\Psr\Log\LoggerInterface; |
| 18 |
use SolidWP\Performance\View\Exceptions\FileNotFoundException; |
| 19 |
|
| 20 |
/** |
| 21 |
* The htaccess manager. |
| 22 |
* |
| 23 |
* @package SolidWP\Performance |
| 24 |
*/ |
| 25 |
final class Manager { |
| 26 |
|
| 27 |
/** |
| 28 |
* @var Readable |
| 29 |
*/ |
| 30 |
private Readable $reader; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var Writable |
| 34 |
*/ |
| 35 |
private Writable $writer; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Modifier |
| 39 |
*/ |
| 40 |
private Modifier $modifier; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var Template_Loader |
| 44 |
*/ |
| 45 |
private Template_Loader $template_loader; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var Config |
| 49 |
*/ |
| 50 |
private Config $config; |
| 51 |
|
| 52 |
/** |
| 53 |
* @var LoggerInterface |
| 54 |
*/ |
| 55 |
private LoggerInterface $logger; |
| 56 |
|
| 57 |
/** |
| 58 |
* @param Readable $reader The htaccess reader. |
| 59 |
* @param Writable $writer The htaccess writer. |
| 60 |
* @param Modifier $modifier The htaccess modifier. |
| 61 |
* @param Template_Loader $template_loader The htaccess template loader. |
| 62 |
* @param Config $config The config object. |
| 63 |
* @param LoggerInterface $logger The logger. |
| 64 |
*/ |
| 65 |
public function __construct( |
| 66 |
Readable $reader, |
| 67 |
Writable $writer, |
| 68 |
Modifier $modifier, |
| 69 |
Template_Loader $template_loader, |
| 70 |
Config $config, |
| 71 |
LoggerInterface $logger |
| 72 |
) { |
| 73 |
$this->reader = $reader; |
| 74 |
$this->writer = $writer; |
| 75 |
$this->modifier = $modifier; |
| 76 |
$this->template_loader = $template_loader; |
| 77 |
$this->config = $config; |
| 78 |
$this->logger = $logger; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Add our rules to the .htaccess file. |
| 83 |
* |
| 84 |
* @param bool $update Whether to force update the rules. |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public function add_rules( bool $update = false ): bool { |
| 89 |
if ( ! $this->supported() ) { |
| 90 |
$this->logger->debug( 'Skipping adding htaccess rules: Apache not detected' ); |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
if ( ! $this->enabled() ) { |
| 96 |
$this->logger->debug( 'Skipping adding htaccess rules: disabled via config' ); |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Disable adding Solid Performance .htaccess rules. |
| 103 |
* |
| 104 |
* @param bool $disabled True to disable. |
| 105 |
*/ |
| 106 |
if ( apply_filters( 'solidwp/performance/htaccess/disabled', false ) ) { |
| 107 |
$this->logger->debug( 'Skipping adding htaccess rules: Filtered by "solidwp/performance/htaccess/disabled"' ); |
| 108 |
|
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
return $this->write_rules( $update ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Ignores any config or filters and force writes the .htaccess file. |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public function force_add_rules(): bool { |
| 121 |
return $this->write_rules( true ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Write the htaccess rules. |
| 126 |
* |
| 127 |
* @param bool $update Whether to force update the rules. |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
private function write_rules( bool $update = false ): bool { |
| 132 |
try { |
| 133 |
$existing = $this->reader->read(); |
| 134 |
$rules = $this->template_loader->get(); |
| 135 |
|
| 136 |
if ( $update ) { |
| 137 |
$modified = $this->modifier->force_prepend( $existing, $rules ); |
| 138 |
} else { |
| 139 |
$modified = $this->modifier->prepend( $existing, $rules ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $existing === $modified ) { |
| 143 |
$this->logger->info( 'htaccess rules already match; skipping writing' ); |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
return $this->writer->write( $modified ); |
| 149 |
} catch ( CacheDeliveryReadException |FileNotFoundException $e ) { |
| 150 |
$this->logger->error( |
| 151 |
'Error adding htaccess rules', |
| 152 |
[ |
| 153 |
'exception' => $e, |
| 154 |
] |
| 155 |
); |
| 156 |
|
| 157 |
return false; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Remove our rules from the .htaccess file. |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function remove_rules(): bool { |
| 167 |
try { |
| 168 |
$existing = $this->reader->read(); |
| 169 |
$removed = $this->modifier->remove( $existing ); |
| 170 |
|
| 171 |
// Our rules have already been removed. |
| 172 |
if ( $removed === $existing ) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
return $this->writer->write( $removed ); |
| 177 |
} catch ( CacheDeliveryReadException $e ) { |
| 178 |
$this->logger->error( |
| 179 |
'Error removing htaccess rules', |
| 180 |
[ |
| 181 |
'exception' => $e, |
| 182 |
] |
| 183 |
); |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Check if our htaccess rules are in place. |
| 191 |
* |
| 192 |
* @return bool |
| 193 |
*/ |
| 194 |
public function has_rules(): bool { |
| 195 |
try { |
| 196 |
$existing = $this->reader->read(); |
| 197 |
|
| 198 |
return $this->modifier->has_rules( $existing ); |
| 199 |
} catch ( CacheDeliveryReadException $e ) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* If this feature is supported by the current server environment. |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
public function supported(): bool { |
| 210 |
// Allow Solid Security define override. |
| 211 |
if ( defined( 'ITSEC_SERVER_OVERRIDE' ) ) { |
| 212 |
return ITSEC_SERVER_OVERRIDE === 'apache'; |
| 213 |
} |
| 214 |
|
| 215 |
global $is_apache; |
| 216 |
|
| 217 |
return (bool) $is_apache; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Whether this feature is enabled. |
| 222 |
* |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
public function enabled(): bool { |
| 226 |
return $this->config->get( 'page_cache.cache_delivery.method' ) === Cache_Delivery_Type::HTACCESS; |
| 227 |
} |
| 228 |
} |
| 229 |
|