PluginProbe
Redirection / 5.9.0
Redirection v5.9.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.9.0, at modules/apache.php

142 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( empty( $this->location ) ) {
62 return false;
63 }
64
65 $items = Red_Item::get_all_for_module( $this->get_id() );
66
67 // Produce the .htaccess file
68 $htaccess = new \Redirection\ImportExport\Htaccess();
69 if ( count( $items ) > 0 ) {
70 foreach ( $items as $item ) {
71 if ( $item->is_enabled() ) {
72 $htaccess->add( $item );
73 }
74 }
75 }
76
77 return $htaccess->save( $this->location );
78 }
79
80 /**
81 * Check if .htaccess file can be saved to the given location
82 *
83 * @param string $location File path.
84 * @return WP_Error|true
85 */
86 public function can_save( $location ) {
87 $location = $this->sanitize_location( $location );
88
89 $file = fopen( $location, 'a' );
90 if ( $file === false ) {
91 $error = error_get_last();
92 return new WP_Error( 'redirect', isset( $error['message'] ) ? $error['message'] : 'Unknown error' );
93 }
94
95 fclose( $file );
96 return true;
97 }
98
99 /**
100 * Sanitize location path to ensure it ends with .htaccess
101 *
102 * @param string $location File path.
103 * @return string
104 */
105 private function sanitize_location( $location ) {
106 $location = str_replace( '.htaccess', '', $location );
107 $location = rtrim( $location, '/' ) . '/.htaccess';
108 return rtrim( dirname( $location ), '/' ) . '/.htaccess';
109 }
110
111 /**
112 * Update module configuration
113 *
114 * @param RedModuleOptions $data Data.
115 * @return array<string, string>|false
116 */
117 public function update( array $data ) {
118 $new_location = isset( $data['location'] ) ? $data['location'] : '';
119 if ( strlen( $new_location ) > 0 ) {
120 $new_location = $this->sanitize_location( trim( $new_location ) );
121 }
122
123 $save = [
124 'location' => $new_location,
125 ];
126
127 if ( ! empty( $this->location ) && $save['location'] !== $this->location && $save['location'] !== '' ) {
128 // Location has moved. Remove from old location
129 $htaccess = new \Redirection\ImportExport\Htaccess();
130 $htaccess->save( $this->location, false );
131 }
132
133 $this->load( $save );
134
135 if ( $save['location'] !== '' && $this->flush_module() === false ) {
136 $save['location'] = '';
137 }
138
139 return $save;
140 }
141 }
142