/**
  * Fixes environment once event occurs
  *
  * @param Config  $config
  * @param string  $event
  * @param Config|null $old_config
  * @throws Util_Environment_Exceptions
  */
 public function fix_on_event($config, $event, $old_config = null)
 {
     if ($config->get_boolean('cdn.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.queue.interval') != $old_config->get_integer('cdn.queue.interval')) {
             $this->unschedule_queue_process();
         }
         if (!wp_next_scheduled('w3_cdn_cron_queue_process')) {
             wp_schedule_event(time(), 'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process');
         }
     } else {
         $this->unschedule_queue_process();
     }
     if ($config->get_boolean('cdn.enabled') && $config->get_boolean('cdn.autoupload.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.autoupload.interval') != $old_config->get_integer('cdn.autoupload.interval')) {
             $this->unschedule_upload();
         }
         if (!wp_next_scheduled('w3_cdn_cron_upload')) {
             wp_schedule_event(time(), 'w3_cdn_cron_upload', 'w3_cdn_cron_upload');
         }
     } else {
         $this->unschedule_upload();
     }
     $exs = new Util_Environment_Exceptions();
     if ($config->get_boolean('cdn.enabled')) {
         try {
             $this->table_create($event == 'activate');
         } catch (\Exception $ex) {
             $exs->push($ex);
         }
     }
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
 /**
  * Fixes environment after plugin deactivation
  *
  * @throws Util_Environment_Exceptions
  * @return array
  */
 public function fix_after_deactivation()
 {
     $exs = new Util_Environment_Exceptions();
     try {
         $this->delete_addin();
     } catch (Util_WpFile_FilesystemOperationException $ex) {
         $exs->push($ex);
     }
     $this->unschedule();
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
 /**
  * Fixes environment after plugin deactivation
  *
  * @param Config  $config
  * @throws Util_Environment_Exceptions
  */
 public function fix_after_deactivation($config)
 {
     $exs = new Util_Environment_Exceptions();
     // call plugin-related handlers
     foreach ($this->get_handlers($config) as $h) {
         try {
             $h->fix_after_deactivation();
         } catch (Util_Environment_Exceptions $ex) {
             $exs->push($ex);
         }
     }
     try {
         do_action('w3tc_environment_fix_after_deactivation', $config);
     } catch (Util_Environment_Exceptions $ex) {
         $exs->push($ex);
     }
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
 /**
  * Minifiers availability error handling
  *
  * @param Config  $config
  * @param Util_Environment_Exceptions $exs
  */
 private function verify_engine_working($config, $exs)
 {
     $minifiers_errors = array();
     if ($config->get_string('minify.js.engine') == 'yuijs') {
         $path_java = $config->get_string('minify.yuijs.path.java');
         $path_jar = $config->get_string('minify.yuijs.path.jar');
         if (!file_exists($path_java)) {
             $minifiers_errors[] = sprintf('YUI Compressor (JS): JAVA executable path was not found. The default minifier JSMin will be used instead.');
         } elseif (!file_exists($path_jar)) {
             $minifiers_errors[] = sprintf('YUI Compressor (JS): JAR file path was not found. The default minifier JSMin will be used instead.');
         }
     }
     if ($config->get_string('minify.css.engine') == 'yuicss') {
         $path_java = $config->get_string('minify.yuicss.path.java');
         $path_jar = $config->get_string('minify.yuicss.path.jar');
         if (!file_exists($path_java)) {
             $minifiers_errors[] = sprintf('YUI Compressor (CSS): JAVA executable path was not found. The default CSS minifier will be used instead.');
         } elseif (!file_exists($path_jar)) {
             $minifiers_errors[] = sprintf('YUI Compressor (CSS): JAR file path was not found. The default CSS minifier will be used instead.');
         }
     }
     if ($config->get_string('minify.js.engine') == 'ccjs') {
         $path_java = $config->get_string('minify.ccjs.path.java');
         $path_jar = $config->get_string('minify.ccjs.path.jar');
         if (!file_exists($path_java)) {
             $minifiers_errors[] = sprintf('Closure Compiler: JAVA executable path was not found. The default minifier JSMin will be used instead.');
         } elseif (!file_exists($path_jar)) {
             $minifiers_errors[] = sprintf('Closure Compiler: JAR file path was not found. The default minifier JSMin will be used instead.');
         }
     }
     if (count($minifiers_errors)) {
         $minify_error = 'The following minifiers cannot be found or are no longer working:</p><ul>';
         foreach ($minifiers_errors as $minifiers_error) {
             $minify_error .= '<li>' . $minifiers_error . '</li>';
         }
         $minify_error .= '</ul><p>This message will automatically disappear once the issue is resolved.';
         $exs->push(new Util_Environment_Exception($minify_error));
     }
 }
 /**
  * Fixes folders
  *
  * @param Config  $config
  * @param Util_Environment_Exceptions $exs
  */
 private function fix_folders($config, $exs)
 {
     if (!$config->get_boolean('pgcache.enabled')) {
         return;
     }
     // folder that we delete if exists and not writeable
     if ($config->get_string('pgcache.engine') == 'file_generic') {
         $dir = W3TC_CACHE_PAGE_ENHANCED_DIR;
     } else {
         if ($config->get_string('pgcache.engine') != 'file') {
             $dir = W3TC_CACHE_DIR . '/page';
         } else {
             return;
         }
     }
     try {
         if (file_exists($dir) && !is_writeable($dir)) {
             Util_WpFile::delete_folder($dir, '', $_SERVER['REQUEST_URI']);
         }
     } catch (Util_WpFile_FilesystemRmdirException $ex) {
         $exs->push($ex);
     }
 }
 /**
  * Check config file
  *
  * @param Config  $config
  * @param Util_Environment_Exceptions $exs
  */
 private function notify_no_config_present($config, $exs)
 {
     if ((file_exists(Config::util_config_filename(0, false)) || file_exists(Config::util_config_filename_legacy(0, false))) && $config->get_integer('common.instance_id', 0) != 0) {
         return;
     }
     $onclick = 'document.location.href=\'' . addslashes(wp_nonce_url('admin.php?page=w3tc_general&w3tc_save_options', 'w3tc')) . '\';';
     $button = '<input type="button" class="button w3tc" ' . 'value="save the settings" onclick="' . $onclick . '" />';
     $exs->push(new Util_Environment_Exception('<strong>W3 Total Cache:</strong> ' . 'Default settings are in use. The configuration file could ' . 'not be read or doesn\'t exist. Please ' . $button . ' to create the file.'));
 }
 /**
  * Used to display Util_Environment_Exceptions in UI
  *
  * @param Util_Environment_Exceptions $exs
  * @return array(before_errors = [], required_changes =>, later_errors => [])
  */
 public static function parse_environment_exceptions($exs)
 {
     $exceptions = $exs->exceptions();
     $commands = '';
     $required_changes = '';
     $before_errors = array();
     $later_errors = array();
     $operation_error_already_shown = false;
     foreach ($exceptions as $ex) {
         if ($ex instanceof Util_WpFile_FilesystemOperationException) {
             if (!$operation_error_already_shown) {
                 $m = $ex->getMessage();
                 if (strlen($m) > 0) {
                     $before_errors[] = $m;
                     // if multiple operations failed when
                     // they tried to fix environment - show only first
                     // otherwise can duplication information about
                     // absense of permissions
                     $operation_error_already_shown = true;
                 }
                 if ($ex instanceof Util_WpFile_FilesystemWriteException) {
                     $required_changes .= sprintf(__('Create the <strong>%s</strong> file and paste the following text into it:
                     <textarea>%s</textarea> <br />', 'w3-total-cache'), $ex->filename(), esc_textarea($ex->file_contents()));
                 } else {
                     if ($ex instanceof Util_WpFile_FilesystemModifyException) {
                         $modification_content = $ex->file_contents();
                         if (strlen($modification_content) > 0) {
                             $modification_content = '<textarea style="height: 100px; width: 100%;">' . esc_textarea($modification_content) . '</textarea>';
                         }
                         $required_changes .= $ex->modification_description() . $modification_content . '<br />';
                     } else {
                         if ($ex instanceof Util_WpFile_FilesystemCopyException) {
                             $commands .= 'cp ' . $ex->source_filename() . ' ' . $ex->destination_filename() . '<br />';
                         } else {
                             if ($ex instanceof Util_WpFile_FilesystemMkdirException) {
                                 $commands .= 'mkdir ' . $ex->folder() . '<br />';
                                 $commands .= 'chmod 777 ' . $ex->folder() . '<br />';
                             } else {
                                 if ($ex instanceof Util_WpFile_FilesystemRmException) {
                                     $commands .= 'rm ' . $ex->filename() . '<br />';
                                 } else {
                                     if ($ex instanceof Util_WpFile_FilesystemRmdirException) {
                                         $commands .= 'rm -rf ' . $ex->folder() . '<br />';
                                     } else {
                                         if ($ex instanceof Util_WpFile_FilesystemChmodException) {
                                             $commands .= 'chmod 777 ' . $ex->filename() . '<br />';
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($ex instanceof Util_Environment_Exception) {
                 $t = $ex->technical_message();
                 if (strlen($t) > 0) {
                     $t = '<br />' . '<a class="w3tc_read_technical_info" href="#">' . __('Technical info', 'w3-total-cache') . '</a>' . '<div class="w3tc_technical_info" style="display: none">' . $t . '</div>';
                 }
                 $later_errors[] = $ex->getMessage() . $t;
             } else {
                 // unknown command
                 $later_errors[] = $ex->getMessage();
             }
         }
     }
     if (strlen($commands) > 0) {
         $required_changes .= __('Execute next commands in a shell:', 'w3-total-cache') . '<br><strong>' . $commands . '</strong>';
     }
     return array('before_errors' => $before_errors, 'required_changes' => $required_changes, 'later_errors' => $later_errors);
 }
 /**
  *
  *
  * @param Util_Environment_Exceptions $exs
  * @param string  $path
  * @param string  $rules
  * @param string  $start
  * @param string  $end
  * @param array   $order
  */
 public static function add_rules($exs, $path, $rules, $start, $end, $order)
 {
     if (empty($path)) {
         return;
     }
     $data = @file_get_contents($path);
     if ($data === false) {
         $data = '';
     }
     $rules_missing = !empty($rules) && strstr(Util_Rule::clean_rules($data), Util_Rule::clean_rules($rules)) === false;
     if (!$rules_missing) {
         return;
     }
     $replace_start = strpos($data, $start);
     $replace_end = strpos($data, $end);
     if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
         $replace_length = $replace_end - $replace_start + strlen($end) + 1;
     } else {
         $replace_start = false;
         $replace_length = 0;
         $search = $order;
         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);
     }
     if (strpos($path, W3TC_CACHE_DIR) === false || Util_Environment::is_nginx()) {
         try {
             Util_WpFile::write_to_file($path, $data);
         } catch (Util_WpFile_FilesystemOperationException $ex) {
             if ($replace_start !== 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-caceh'), $path, $start, $end), $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:', 'w3-total-cache'), $path), $path, $rules));
             }
             return;
         }
     } else {
         if (!@file_exists(dirname($path))) {
             Util_File::mkdir_from(dirname($path), W3TC_CACHE_DIR);
         }
         if (!@file_put_contents($path, $data)) {
             try {
                 Util_WpFile::delete_folder(dirname($path), '', $_SERVER['REQUEST_URI']);
             } catch (Util_WpFile_FilesystemOperationException $ex) {
                 $exs->push($ex);
                 return;
             }
         }
     }
     Util_Rule::after_rules_modified();
 }