/**
  * Write all active rules to .htaccess file.
  *
  * @return boolean True on success, false on failure.
  */
 static function write_to_htaccess()
 {
     global $aio_wp_security;
     //figure out what server is being used
     if (AIOWPSecurity_Utility::get_server_type() == -1) {
         $aio_wp_security->debug_logger->log_debug("Unable to write to .htaccess - server type not supported!", 4);
         return false;
         //unable to write to the file
     }
     //clean up old rules first
     if (AIOWPSecurity_Utility_Htaccess::delete_from_htaccess() == -1) {
         $aio_wp_security->debug_logger->log_debug("Delete operation of .htaccess file failed!", 4);
         return false;
         //unable to write to the file
     }
     $htaccess = ABSPATH . '.htaccess';
     if (!($f = @fopen($htaccess, 'a+'))) {
         @chmod($htaccess, 0644);
         if (!($f = @fopen($htaccess, 'a+'))) {
             $aio_wp_security->debug_logger->log_debug("chmod operation on .htaccess failed!", 4);
             return false;
         }
     }
     AIOWPSecurity_Utility_File::backup_and_rename_htaccess($htaccess);
     //TODO - we dont want to continually be backing up the htaccess file
     @ini_set('auto_detect_line_endings', true);
     $ht = explode(PHP_EOL, implode('', file($htaccess)));
     //parse each line of file into array
     $rules = AIOWPSecurity_Utility_Htaccess::getrules();
     $rulesarray = explode(PHP_EOL, $rules);
     $rulesarray = apply_filters('aiowps_htaccess_rules_before_writing', $rulesarray);
     $contents = array_merge($rulesarray, $ht);
     if (!($f = @fopen($htaccess, 'w+'))) {
         $aio_wp_security->debug_logger->log_debug("Write operation on .htaccess failed!", 4);
         return false;
         //we can't write to the file
     }
     $blank = false;
     //write each line to file
     foreach ($contents as $insertline) {
         if (trim($insertline) == '') {
             if ($blank == false) {
                 fwrite($f, PHP_EOL . trim($insertline));
             }
             $blank = true;
         } else {
             $blank = false;
             fwrite($f, PHP_EOL . trim($insertline));
         }
     }
     @fclose($f);
     return true;
     //success
 }