PluginProbe
Redirection / 4.3.2
Redirection v4.3.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / modules / apache.php

apache.php in Redirection 4.3.2, at modules/apache.php

90 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = rtrim( $location, '/' ) . '/.htaccess';
65 return rtrim( dirname( $location ), '/' ) . '/.htaccess';
66 }
67
68 public function update( array $data ) {
69 include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php';
70
71 $save = [
72 'location' => isset( $data['location'] ) ? $this->sanitize_location( trim( $data['location'] ) ) : '',
73 ];
74
75 if ( ! empty( $this->location ) && $save['location'] !== $this->location ) {
76 // Location has moved. Remove from old location
77 $htaccess = new Red_Htaccess();
78 $htaccess->save( $this->location, '' );
79 }
80
81 $this->load( $save );
82
83 if ( $save['location'] !== '' && $this->flush_module() === false ) {
84 $save['location'] = '';
85 }
86
87 return $save;
88 }
89 }
90