PluginProbe
Redirection / 5.3.0
Redirection v5.3.0
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 5.3.0, at modules/apache.php

96 lines 2.2 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 false;
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 $new_location = isset( $data['location'] ) ? $data['location'] : '';
73 if ( strlen( $new_location ) > 0 ) {
74 $new_location = $this->sanitize_location( trim( $data['location'] ) );
75 }
76
77 $save = [
78 'location' => $new_location,
79 ];
80
81 if ( ! empty( $this->location ) && $save['location'] !== $this->location && $save['location'] !== '' ) {
82 // Location has moved. Remove from old location
83 $htaccess = new Red_Htaccess();
84 $htaccess->save( $this->location, '' );
85 }
86
87 $this->load( $save );
88
89 if ( $save['location'] !== '' && $this->flush_module() === false ) {
90 $save['location'] = '';
91 }
92
93 return $save;
94 }
95 }
96