| 1 |
<?php |
| 2 |
|
| 3 |
class Apache_Module extends Red_Module { |
| 4 |
const MODULE_ID = 2; |
| 5 |
|
| 6 |
private $location = ''; |
| 7 |
|
| 8 |
public function get_id() { |
| 9 |
return self::MODULE_ID; |
| 10 |
} |
| 11 |
|
| 12 |
public function get_name() { |
| 13 |
return 'Apache'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_location() { |
| 17 |
return $this->location; |
| 18 |
} |
| 19 |
|
| 20 |
protected function load( $data ) { |
| 21 |
$mine = array( 'location' ); |
| 22 |
|
| 23 |
foreach ( $mine as $key ) { |
| 24 |
if ( isset( $data[ $key ] ) ) { |
| 25 |
$this->$key = $data[ $key ]; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
protected function flush_module() { |
| 31 |
include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php'; |
| 32 |
|
| 33 |
if ( empty( $this->location ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$items = Red_Item::get_all_for_module( $this->get_id() ); |
| 38 |
|
| 39 |
// Produce the .htaccess file |
| 40 |
$htaccess = new Red_Htaccess(); |
| 41 |
if ( is_array( $items ) && count( $items ) > 0 ) { |
| 42 |
foreach ( $items as $item ) { |
| 43 |
if ( $item->is_enabled() ) { |
| 44 |
$htaccess->add( $item ); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $htaccess->save( $this->location ); |
| 50 |
} |
| 51 |
|
| 52 |
public function can_save( $location ) { |
| 53 |
$location = $this->sanitize_location( $location ); |
| 54 |
|
| 55 |
if ( @fopen( $location, 'a' ) === false ) { |
| 56 |
$error = error_get_last(); |
| 57 |
return new WP_Error( 'redirect', isset( $error['message'] ) ? $error['message'] : 'Unknown error' ); |
| 58 |
} |
| 59 |
|
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
private function sanitize_location( $location ) { |
| 64 |
$location = str_replace( '.htaccess', '', $location ); |
| 65 |
$location = rtrim( $location, '/' ) . '/.htaccess'; |
| 66 |
return rtrim( dirname( $location ), '/' ) . '/.htaccess'; |
| 67 |
} |
| 68 |
|
| 69 |
public function update( array $data ) { |
| 70 |
include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php'; |
| 71 |
|
| 72 |
$save = [ |
| 73 |
'location' => isset( $data['location'] ) ? $this->sanitize_location( trim( $data['location'] ) ) : '', |
| 74 |
]; |
| 75 |
|
| 76 |
if ( ! empty( $this->location ) && $save['location'] !== $this->location ) { |
| 77 |
// Location has moved. Remove from old location |
| 78 |
$htaccess = new Red_Htaccess(); |
| 79 |
$htaccess->save( $this->location, '' ); |
| 80 |
} |
| 81 |
|
| 82 |
$this->load( $save ); |
| 83 |
|
| 84 |
if ( $save['location'] !== '' && $this->flush_module() === false ) { |
| 85 |
$save['location'] = ''; |
| 86 |
} |
| 87 |
|
| 88 |
return $save; |
| 89 |
} |
| 90 |
} |
| 91 |
|