| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @phpstan-import-type RedModuleOptions from Red_Module |
| 5 |
*/ |
| 6 |
class Apache_Module extends Red_Module { |
| 7 |
const MODULE_ID = 2; |
| 8 |
|
| 9 |
/** |
| 10 |
* Location path for .htaccess file |
| 11 |
* |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $location = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* Get module ID |
| 18 |
* |
| 19 |
* @return int |
| 20 |
*/ |
| 21 |
public function get_id() { |
| 22 |
return self::MODULE_ID; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get module name |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function get_name() { |
| 31 |
return 'Apache'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get .htaccess file location |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
public function get_location() { |
| 40 |
return $this->location; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Load module data |
| 45 |
* |
| 46 |
* @param RedModuleOptions $options Options. |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
protected function load( array $options ) { |
| 50 |
if ( isset( $options['location'] ) ) { |
| 51 |
$this->location = $options['location']; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Flush module by regenerating .htaccess file |
| 57 |
* |
| 58 |
* @return bool |
| 59 |
*/ |
| 60 |
protected function flush_module() { |
| 61 |
include_once dirname( __DIR__ ) . '/models/htaccess.php'; |
| 62 |
|
| 63 |
if ( empty( $this->location ) ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$items = Red_Item::get_all_for_module( $this->get_id() ); |
| 68 |
|
| 69 |
// Produce the .htaccess file |
| 70 |
$htaccess = new Red_Htaccess(); |
| 71 |
if ( count( $items ) > 0 ) { |
| 72 |
foreach ( $items as $item ) { |
| 73 |
if ( $item->is_enabled() ) { |
| 74 |
$htaccess->add( $item ); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $htaccess->save( $this->location ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check if .htaccess file can be saved to the given location |
| 84 |
* |
| 85 |
* @param string $location File path. |
| 86 |
* @return WP_Error|true |
| 87 |
*/ |
| 88 |
public function can_save( $location ) { |
| 89 |
$location = $this->sanitize_location( $location ); |
| 90 |
|
| 91 |
$file = fopen( $location, 'a' ); |
| 92 |
if ( $file === false ) { |
| 93 |
$error = error_get_last(); |
| 94 |
return new WP_Error( 'redirect', isset( $error['message'] ) ? $error['message'] : 'Unknown error' ); |
| 95 |
} |
| 96 |
|
| 97 |
fclose( $file ); |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Sanitize location path to ensure it ends with .htaccess |
| 103 |
* |
| 104 |
* @param string $location File path. |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
private function sanitize_location( $location ) { |
| 108 |
$location = str_replace( '.htaccess', '', $location ); |
| 109 |
$location = rtrim( $location, '/' ) . '/.htaccess'; |
| 110 |
return rtrim( dirname( $location ), '/' ) . '/.htaccess'; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Update module configuration |
| 115 |
* |
| 116 |
* @param RedModuleOptions $data Data. |
| 117 |
* @return array<string, string>|false |
| 118 |
*/ |
| 119 |
public function update( array $data ) { |
| 120 |
include_once dirname( __DIR__ ) . '/models/htaccess.php'; |
| 121 |
|
| 122 |
$new_location = isset( $data['location'] ) ? $data['location'] : ''; |
| 123 |
if ( strlen( $new_location ) > 0 ) { |
| 124 |
$new_location = $this->sanitize_location( trim( $new_location ) ); |
| 125 |
} |
| 126 |
|
| 127 |
$save = [ |
| 128 |
'location' => $new_location, |
| 129 |
]; |
| 130 |
|
| 131 |
if ( ! empty( $this->location ) && $save['location'] !== $this->location && $save['location'] !== '' ) { |
| 132 |
// Location has moved. Remove from old location |
| 133 |
$htaccess = new Red_Htaccess(); |
| 134 |
$htaccess->save( $this->location, false ); |
| 135 |
} |
| 136 |
|
| 137 |
$this->load( $save ); |
| 138 |
|
| 139 |
if ( $save['location'] !== '' && $this->flush_module() === false ) { |
| 140 |
$save['location'] = ''; |
| 141 |
} |
| 142 |
|
| 143 |
return $save; |
| 144 |
} |
| 145 |
} |
| 146 |
|