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