/**
  *
  */
 function w3tc_default_remove_add_in()
 {
     $module = Util_Request::get_string('w3tc_default_remove_add_in');
     // in the case of missing permissions to delete
     // environment will use that to try to override addin via ftp
     set_transient('w3tc_remove_add_in_' . $module, 'yes', 600);
     switch ($module) {
         case 'pgcache':
             Util_WpFile::delete_file(W3TC_ADDIN_FILE_ADVANCED_CACHE);
             $src = W3TC_INSTALL_FILE_ADVANCED_CACHE;
             $dst = W3TC_ADDIN_FILE_ADVANCED_CACHE;
             try {
                 Util_WpFile::copy_file($src, $dst);
             } catch (Util_WpFile_FilesystemOperationException $ex) {
             }
             break;
         case 'dbcache':
             Util_WpFile::delete_file(W3TC_ADDIN_FILE_DB);
             break;
         case 'objectcache':
             Util_WpFile::delete_file(W3TC_ADDIN_FILE_OBJECT_CACHE);
             break;
     }
     Util_Admin::redirect(array('w3tc_note' => 'add_in_removed'), true);
 }
 /**
  * Fixes folders
  *
  * @param Config  $config
  * @param Util_Environment_Exceptions $exs
  */
 private function fix_folders($config, $exs)
 {
     // folder that we delete if exists and not writeable
     if ($config->get_boolean('minify.enabled') && $config->get_string('minify.engine') == 'file') {
         $dir = W3TC_CACHE_MINIFY_DIR;
         try {
             if (file_exists($dir) && !is_writeable($dir)) {
                 Util_WpFile::delete_folder($dir, '', $_SERVER['REQUEST_URI']);
             }
         } catch (Util_WpFile_FilesystemRmdirException $ex) {
             $exs->push($ex);
         }
     }
 }
 /**
  * Deletes add-in
  *
  * @throws Util_WpFile_FilesystemOperationException
  */
 private function delete_addin()
 {
     if ($this->is_dbcache_add_in()) {
         Util_WpFile::delete_file(W3TC_ADDIN_FILE_DB);
     }
 }
    /**
     * Writes directives to WP .htaccess / nginx.conf
     * returns true if modifications has been made
     */
    private function rules_core_add($config, $exs)
    {
        $path = Util_Rule::get_pgcache_rules_core_path();
        if ($path === false) {
            return;
        }
        $original_data = @file_get_contents($path);
        if ($original_data === false) {
            $original_data = '';
        }
        $data = $original_data;
        if ($has_wpsc = Util_Rule::has_rules($data, W3TC_MARKER_BEGIN_PGCACHE_WPSC, W3TC_MARKER_END_PGCACHE_WPSC)) {
            $data = Util_Rule::erase_rules($data, W3TC_MARKER_BEGIN_PGCACHE_WPSC, W3TC_MARKER_END_PGCACHE_WPSC);
        }
        $rules = $this->rules_core_generate($config);
        $rules_missing = strstr(Util_Rule::clean_rules($data), Util_Rule::clean_rules($rules)) === false;
        if (!$has_wpsc && !$rules_missing) {
            return;
        }
        // modification of file not required
        $replace_start = strpos($data, W3TC_MARKER_BEGIN_PGCACHE_CORE);
        $replace_end = strpos($data, W3TC_MARKER_END_PGCACHE_CORE);
        if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
            $replace_length = $replace_end - $replace_start + strlen(W3TC_MARKER_END_PGCACHE_CORE) + 1;
        } else {
            $replace_start = false;
            $replace_length = 0;
            $search = array(W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0, W3TC_MARKER_BEGIN_WORDPRESS => 0, W3TC_MARKER_END_MINIFY_CORE => strlen(W3TC_MARKER_END_MINIFY_CORE) + 1, W3TC_MARKER_END_BROWSERCACHE_CACHE => strlen(W3TC_MARKER_END_BROWSERCACHE_CACHE) + 1, W3TC_MARKER_END_PGCACHE_CACHE => strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1, W3TC_MARKER_END_MINIFY_CACHE => strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1);
            foreach ($search as $string => $length) {
                $replace_start = strpos($data, $string);
                if ($replace_start !== false) {
                    $replace_start += $length;
                    break;
                }
            }
        }
        if ($replace_start !== false) {
            $data = Util_Rule::trim_rules(substr_replace($data, $rules, $replace_start, $replace_length));
        } else {
            $data = Util_Rule::trim_rules($data . $rules);
        }
        try {
            Util_WpFile::write_to_file($path, $data);
        } catch (Util_WpFile_FilesystemOperationException $ex) {
            if ($has_wpsc) {
                $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s</strong> and remove all lines between and including
								<strong>%s</strong> and <strong>%s</strong> markers.', 'w3-total-cache'), $path, W3TC_MARKER_BEGIN_PGCACHE_WPSC, W3TC_MARKER_END_PGCACHE_WPSC), $path));
            }
            if ($rules_missing) {
                if (strpos($data, W3TC_MARKER_BEGIN_PGCACHE_CORE) !== false) {
                    $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s</strong> and replace all lines between and including
									<strong>%s</strong> and <strong>%s</strong> markers with:', 'w3-total-cache'), $path, W3TC_MARKER_BEGIN_PGCACHE_CORE, W3TC_MARKER_END_PGCACHE_CORE), $path, $rules));
                } else {
                    $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s</strong> and add the following rules above the WordPress
									directives:'), $path), $path, $rules));
                }
            }
            return;
        }
        Util_Rule::after_rules_modified();
    }
 /**
  * Deletes add-in
  *
  * @throws Util_WpFile_FilesystemOperationException
  */
 private function delete_addin()
 {
     if ($this->is_objectcache_add_in()) {
         Util_WpFile::delete_file(W3TC_ADDIN_FILE_OBJECT_CACHE);
     }
 }
 /**
  * Checks if addins in wp-content is available and correct version.
  *
  * @param Util_Environment_Exceptions $exs
  */
 private function create_required_folders($exs)
 {
     // folders that we create if not exists
     $directories = array(W3TC_CACHE_DIR, W3TC_CONFIG_DIR);
     foreach ($directories as $directory) {
         try {
             Util_WpFile::create_writeable_folder($directory, WP_CONTENT_DIR);
         } catch (Util_WpFile_FilesystemOperationException $ex) {
             $exs->push($ex);
         }
     }
     // folders that we delete if exists and not writeable
     $directories = array(W3TC_CACHE_TMP_DIR, W3TC_CACHE_BLOGMAP_FILENAME, W3TC_CACHE_DIR . '/object', W3TC_CACHE_DIR . '/db');
     foreach ($directories as $directory) {
         try {
             if (file_exists($directory) && !is_writeable($directory)) {
                 Util_WpFile::delete_folder($directory);
             }
         } catch (Util_WpFile_FilesystemRmdirException $ex) {
             $exs->push($ex);
         }
     }
 }
 /**
  * Deletes maintenance mode file
  */
 public static function disable_maintenance_mode()
 {
     Util_WpFile::delete_file(Util_Environment::site_path() . '/.maintenance');
 }
 /**
  * Remove rules
  */
 public static function remove_rules($exs, $path, $start, $end)
 {
     if (!file_exists($path)) {
         return;
     }
     $data = @file_get_contents($path);
     if ($data === false) {
         return;
     }
     if (strstr($data, $start) === false) {
         return;
     }
     $data = Util_Rule::erase_rules($data, $start, $end);
     try {
         Util_WpFile::write_to_file($path, $data);
     } catch (Util_WpFile_FilesystemOperationException $ex) {
         $exs->push(new Util_WpFile_FilesystemModifyException($ex->getMessage(), $ex->credentials_form(), sprintf(__('Edit file <strong>%s</strong> and remove all lines between and including <strong>%s</strong>
             and <strong>%s</strong> markers.', 'w3-total-cache'), $path, $start, $end), $path));
     }
 }