| @@ -1,73 +1,187 @@ | ||
| 1 | 1 | <?php |
| 2 | 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; | |
| 3 | +class Apache_Module extends Red_Module | |
| 4 | +{ | |
| 5 | + var $name; | |
| 6 | + var $allow_ip = false; | |
| 7 | + var $raw; | |
| 8 | + var $ban_ip = false; | |
| 9 | + | |
| 10 | + var $site = ''; | |
| 11 | + var $location = ''; | |
| 12 | + var $canonical = ''; | |
| 13 | + var $strip_index = ''; | |
| 14 | + var $memory_limit = ''; | |
| 15 | + var $error_level = ''; | |
| 16 | + | |
| 17 | + function is_valid () | |
| 18 | + { | |
| 19 | + if (!$this->location || !file_exists (dirname ($this->location)) && !is_writable (dirname ($this->location))) | |
| 20 | + return false; | |
| 21 | + return true; | |
| 10 | 22 | } |
| 11 | - | |
| 12 | - public function get_name() { | |
| 13 | - return 'Apache'; | |
| 23 | + | |
| 24 | + function load ($data) | |
| 25 | + { | |
| 26 | + $mine = array ('location', 'canonical', 'strip_index', 'memory_limit', 'error_level', 'ban_ip', 'allow_ip', 'raw', 'site'); | |
| 27 | + foreach ($mine AS $key) | |
| 28 | + { | |
| 29 | + if (isset ($data[$key])) | |
| 30 | + $this->$key = $data[$key]; | |
| 31 | + } | |
| 14 | 32 | } |
| 15 | - | |
| 16 | - public function get_location() { | |
| 17 | - return $this->location; | |
| 33 | + | |
| 34 | + function module_flush_delete () | |
| 35 | + { | |
| 36 | + @unlink ($this->location); | |
| 18 | 37 | } |
| 38 | + | |
| 39 | + function module_flush ($items) | |
| 40 | + { | |
| 41 | + // Produce the .htaccess file | |
| 42 | + include_once (dirname (__FILE__).'/../models/htaccess.php'); | |
| 19 | 43 | |
| 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 | - } | |
| 44 | + $htaccess = new Red_Htaccess ($this); | |
| 45 | + if (is_array ($items) && count ($items) > 0) | |
| 46 | + { | |
| 47 | + foreach ($items AS $item) | |
| 48 | + $htaccess->add ($item); | |
| 27 | 49 | } |
| 50 | + | |
| 51 | + $htaccess->save ($this->location, $this->name); | |
| 28 | 52 | } |
| 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 update( array $data ) { | |
| 53 | - include_once dirname( dirname( __FILE__ ) ) . '/models/htaccess.php'; | |
| 54 | - | |
| 55 | - $save = array( | |
| 56 | - 'location' => isset( $data['location'] ) ? trim( $data['location'] ) : '', | |
| 53 | + | |
| 54 | + function save ($data) | |
| 55 | + { | |
| 56 | + $save = array | |
| 57 | + ( | |
| 58 | + 'location' => $data['location'], | |
| 59 | + 'canonical' => $data['canonical'], | |
| 60 | + 'strip_index' => $data['strip_index'], | |
| 61 | + 'memory_limit' => $data['memory_limit'], | |
| 62 | + 'error_level' => $data['error_level'], | |
| 63 | + 'ban_ip' => $data['ban_ip'], | |
| 64 | + 'allow_ip' => $data['allow_ip'], | |
| 65 | + 'raw' => $data['raw'], | |
| 66 | + 'site' => preg_replace ('@https?://@', '', $data['site']) | |
| 57 | 67 | ); |
| 58 | - | |
| 59 | - if ( ! empty( $this->location ) && $save['location'] !== $this->location ) { | |
| 60 | - // Location has moved. Remove from old location | |
| 61 | - $htaccess = new Red_Htaccess(); | |
| 62 | - $htaccess->save( $this->location, '' ); | |
| 63 | - } | |
| 64 | - | |
| 65 | - $this->load( $save ); | |
| 66 | - | |
| 67 | - if ( $save['location'] !== '' && $this->flush_module() === false ) { | |
| 68 | - $save['location'] = ''; | |
| 69 | - } | |
| 70 | - | |
| 68 | + | |
| 69 | + $this->load ($save); | |
| 71 | 70 | return $save; |
| 72 | 71 | } |
| 72 | + | |
| 73 | + function config () | |
| 74 | + { | |
| 75 | + ?> | |
| 76 | + <tr> | |
| 77 | + <th valign="top"><?php _e ('Location', 'redirection'); ?>:</th> | |
| 78 | + <td> | |
| 79 | + <input type="text" name="location" value="<?php echo htmlspecialchars ($this->location) ?>" style="width: 95%"/> | |
| 80 | + <?php if ($this->location == '') : ?> | |
| 81 | + <br/> | |
| 82 | + <span class="sub"><?php printf (__ ('WordPress is installed in: <code>%s</code>', 'redirection'), ABSPATH); ?></span> | |
| 83 | + <?php endif; ?> | |
| 84 | + </td> | |
| 85 | + </tr> | |
| 86 | + <tr> | |
| 87 | + <th><?php _e ('Canonical', 'redirection'); ?>:</th> | |
| 88 | + <td> | |
| 89 | + <select name="canonical"> | |
| 90 | + <?php echo $this->select (array ('default' => __ ('Leave as is', 'redirection'), 'nowww' => sprintf (__ ('Strip WWW (%s)', 'redirection'), preg_replace ('@https?://(www)?@', '', get_bloginfo ('url'))), 'www' => sprintf (__ ('Force WWW (www.%s)', 'redirection'), preg_replace ('@https?://(www)?@', '', get_bloginfo ('url')))), $this->canonical); ?> | |
| 91 | + </select> | |
| 92 | + | |
| 93 | + <br/> | |
| 94 | + <strong><?php _e ('Strip Index', 'redirection'); ?>:</strong> | |
| 95 | + <select name="strip_index"> | |
| 96 | + <?php echo $this->select (array ('default' => __ ('Leave as is', 'redirection'), 'yes' => __ ('Strip index files (html,php)', 'redirection')), $this->strip_index); ?> | |
| 97 | + </select> | |
| 98 | + </td> | |
| 99 | + </tr> | |
| 100 | + <tr> | |
| 101 | + <th><?php _e ('Memory Limit', 'redirection'); ?>:</th> | |
| 102 | + <td> | |
| 103 | + <select name="memory_limit"> | |
| 104 | + <?php echo $this->select (array ('0' => __ ('Server default', 'redirection'), '8' => '8MB', '16' => '16MB', '32' => '32MB', '64' => '64MB', '128' => '128MB'), $this->memory_limit); ?> | |
| 105 | + </select> | |
| 106 | + | |
| 107 | + <strong><?php _e ('Error Level', 'redirection'); ?>:</strong> | |
| 108 | + <select name="error_level"> | |
| 109 | + <?php echo $this->select (array ('default' => __ ('Server default', 'redirection'), 'none' => __ ('No errors', 'redirection'), 'error' => __ ('Show errors', 'redirection')), $this->error_level); ?> | |
| 110 | + </select> | |
| 111 | + </td> | |
| 112 | + </tr> | |
| 113 | + <tr> | |
| 114 | + <th><?php _e ('Ban IPs', 'redirection'); ?>:</th> | |
| 115 | + <td> | |
| 116 | + <input type="text" name="ban_ip" value="<?php echo htmlspecialchars ($this->ban_ip) ?>" style="width: 95%"/> | |
| 117 | + </td> | |
| 118 | + </tr> | |
| 119 | + <tr> | |
| 120 | + <th><?php _e ('Allow IPs', 'redirection'); ?>:</th> | |
| 121 | + <td> | |
| 122 | + <input type="text" name="allow_ip" value="<?php echo htmlspecialchars ($this->allow_ip) ?>" style="width: 95%"/> | |
| 123 | + </td> | |
| 124 | + </tr> | |
| 125 | + <tr> | |
| 126 | + <th valign="top"><?php _e ('Raw .htaccess', 'redirection'); ?>:</th> | |
| 127 | + <td> | |
| 128 | + <textarea style="width: 95%" name="raw"><?php echo htmlspecialchars ($this->raw)?></textarea> | |
| 129 | + </td> | |
| 130 | + </tr> | |
| 131 | + <tr> | |
| 132 | + <th><?php _e ('Site URL', 'redirection'); ?>:</th> | |
| 133 | + <td> | |
| 134 | + <input type="text" size="40" name="site" value="<?php echo htmlspecialchars ($this->site) ?>"/> | |
| 135 | + <span class="sub"><?php _e ('Advanced: For management of external sites', 'redirection'); ?></span> | |
| 136 | + </td> | |
| 137 | + </tr> | |
| 138 | + | |
| 139 | + <?php | |
| 140 | + } | |
| 141 | + | |
| 142 | + function options () | |
| 143 | + { | |
| 144 | + echo '<p>'; | |
| 145 | + if ($this->location) | |
| 146 | + { | |
| 147 | + if (!file_exists (dirname ($this->location))) | |
| 148 | + { | |
| 149 | + echo '<code>'.$this->location.'</code></p>'; | |
| 150 | + echo __ ('<strong>Location is invalid - check that path exists</strong>', 'redirection'); | |
| 151 | + return; | |
| 152 | + } | |
| 153 | + else if ((file_exists ($this->location) && !is_writable ($this->location)) || (!file_exists ($this->location) && !is_writable (dirname ($this->location)))) | |
| 154 | + { | |
| 155 | + echo '<code>'.$this->location.'</code></p>'; | |
| 156 | + echo __ ('<strong>Could not write to configured <code>.htaccess</code> file - check file permissions</strong>', 'redirection'); | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + else | |
| 160 | + echo '<code>'.$this->location.'</code>'; | |
| 161 | + } | |
| 162 | + else | |
| 163 | + echo __ ('<strong>Disabled: enter the location of an <code>.htaccess</code> file for this to be valid</strong>', 'redirection'); | |
| 164 | + echo '</p>'; | |
| 165 | + | |
| 166 | + $options = array (); | |
| 167 | + if ($this->canonical != 'default' && !empty ($this->canonical)) | |
| 168 | + $options[] = ($this->canonical == 'nowww') ? __ ('strip WWW', 'redirection') : __ ('force WWW', 'redirection'); | |
| 169 | + | |
| 170 | + if ($this->strip_index != 'default' && !empty ($this->strip_index)) | |
| 171 | + $options[] = __ ('strip index', 'redirection'); | |
| 172 | + | |
| 173 | + if ($this->memory_limit > 0 && !empty ($this->memory_limit)) | |
| 174 | + $options[] = sprintf (__ ('memory limit at %dMB', 'redirection'), $this->memory_limit); | |
| 175 | + | |
| 176 | + if ($this->error_level != 'default' && !empty ($this->error_level)) | |
| 177 | + $options[] = ($this->error_level == 'none') ? __ ('no errors', 'redirection') : __ ('show errors', 'redirection'); | |
| 178 | + | |
| 179 | + if (!empty ($this->ban_ip)) | |
| 180 | + $options[] = __ ('IPs are banned', 'redirection'); | |
| 181 | + | |
| 182 | + if (!empty ($this->allow_ip)) | |
| 183 | + $options[] = __ ('IPs are allowed', 'redirection'); | |
| 184 | + | |
| 185 | + echo '<small>'.ucfirst (implode (', ', $options)).'</small>'; | |
| 186 | + } | |
| 73 | 187 | } |