/**
  * Validate config options, setting errors as appropriate.
  *
  * This is called when options are updated from settings screen, generating errors as appropriate,
  * and when plugin is initialised, in which case errors not generated and capabilities not checked.
  *
  * @param array $options options to validated
  * @param bool $update_check if FALSE, only validate, no error reporting and don't validate capabilities
  * @return bool TRUE if options OK, FALSE otherwise
  */
 private function options_validate(&$options = array(), $update_check = TRUE)
 {
     //if nothing is configured we don't validate, but no error generated
     if (empty($options['sites_conf']) && empty($options['dbs_conf']) && empty($options['accept']) && empty($options['debug_output_level'])) {
         return FALSE;
     }
     $valid = TRUE;
     if (empty($options['accept'])) {
         if ($update_check) {
             SitePushErrors::add_error('You must accept the warning before using SitePush.', 'error', 'accept');
         }
         $valid = FALSE;
     }
     if (empty($options['sites_conf']) || !file_exists($options['sites_conf']) || @parse_ini_file($options['sites_conf']) === FALSE) {
         if ($update_check) {
             if (file_exists($options['sites_conf'])) {
                 SitePushErrors::add_error('Sites config file present, but it cannot be read.', 'error', 'sites_conf');
             } else {
                 SitePushErrors::add_error('Path not valid - sites config file not found.', 'error', 'sites_conf');
             }
         }
         $valid = FALSE;
     }
     if (empty($options['dbs_conf']) || !file_exists($options['dbs_conf']) || @parse_ini_file($options['dbs_conf']) === FALSE) {
         if ($update_check) {
             if (file_exists($options['dbs_conf'])) {
                 SitePushErrors::add_error('DB config file present, but it cannot be read.', 'error', 'dbs_conf');
             } else {
                 SitePushErrors::add_error('Path not valid - DB config file not found.', 'error', 'dbs_conf');
             }
         }
         $valid = FALSE;
     }
     if (is_multisite() && (empty($options['domain_map_conf']) || !file_exists($options['domain_map_conf']) || @parse_ini_file($options['domain_map_conf']) === FALSE)) {
         if ($update_check) {
             if (file_exists($options['domain_map_conf'])) {
                 SitePushErrors::add_error('Domain map config file present, but it cannot be read.', 'error', 'sites_conf');
             } else {
                 SitePushErrors::add_error('Path not valid - domain map config file not found.', 'error', 'sites_conf');
             }
         }
         $valid = FALSE;
     }
     if (!empty($options['sites_conf']) && !empty($options['dbs_conf']) && $options['dbs_conf'] == $options['sites_conf']) {
         if ($update_check) {
             SitePushErrors::add_error('Sites and DBs config files cannot be the same file.', 'error', 'dbs_conf');
         }
         $valid = FALSE;
     }
     if (!empty($options['backup_path']) && !file_exists($options['backup_path'])) {
         if ($update_check) {
             SitePushErrors::add_error('Path not valid - backup directory not found.', 'error', 'backup_path');
         }
         $valid = FALSE;
     }
     if (!$this->validate_plugin_deactivates($options)) {
         if ($update_check) {
             SitePushErrors::add_error('You cannot have SitePush try to deactivate itself. Please remove SitePush from the Deactivate Plugins options.', 'error', 'plugin_deactivates');
         }
         $valid = FALSE;
     }
     if (!$this->validate_db_custom_table_groups($options)) {
         if ($update_check) {
             SitePushErrors::add_error('Custom database table groups is not in the correct format.', 'error', 'backup_path');
         }
         $valid = FALSE;
     }
     if ($options['rsync_path'] && !file_exists($options['rsync_path'])) {
         if ($update_check) {
             SitePushErrors::add_error('Path not valid - rsync not found.', 'error', 'rsync_path');
         }
         $valid = FALSE;
     }
     if ($options['mysql_path'] && !file_exists($options['mysql_path'])) {
         if ($update_check) {
             SitePushErrors::add_error('Path not valid - mysql not found.', 'error', 'mysql_path');
         }
         $valid = FALSE;
     }
     if ($options['mysqldump_path'] && !file_exists($options['mysqldump_path'])) {
         if ($update_check) {
             SitePushErrors::add_error('Path not valid - mysqldump not found.', 'error', 'mysqldump_path');
         }
         $valid = FALSE;
     }
     if (!empty($options['timezone'])) {
         @($tz = timezone_open($options['timezone']));
         if (FALSE === $tz) {
             if ($update_check) {
                 SitePushErrors::add_error("{$options['timezone']} is not a valid timezone. See <a href='http://php.net/manual/en/timezones.php' target='_blank'>list of supported timezones</a> for valid values.", 'error', 'timezone');
             }
             $valid = FALSE;
         }
     }
     //Make sure current admin has whatever capabilities are required for SitePush
     //we need to use WP settings_error API here, because error is fixed before SitePushErrors can report it
     if ($update_check) {
         if (!current_user_can($options['capability'])) {
             $error = "SitePush capability ({$options['capability']}) cannot be a capability which you do not have. It has been reset to " . self::$default_capability . ".";
             SitePushErrors::force_show_wp_errors();
             SitePushErrors::add_error($error, 'error', 'capability');
             if (function_exists('add_settings_error')) {
                 add_settings_error('sitepush', 'sitepush-capability-error', $error);
             }
             $options['capability'] = self::$default_capability;
         }
         if (!current_user_can($options['admin_capability'])) {
             $error = "SitePush admin capability ({$options['admin_capability']}) cannot be a capability which you do not have. It has been reset to " . self::$default_admin_capability . ".";
             SitePushErrors::force_show_wp_errors();
             SitePushErrors::add_error($error, 'error', 'admin-capability');
             if (function_exists('add_settings_error')) {
                 add_settings_error('sitepush', 'sitepush-admin-capability-error', $error);
             }
             $options['admin_capability'] = self::$default_capability;
         }
     }
     return $valid && !SitePushErrors::is_error();
 }
 public static function force_show_wp_errors()
 {
     self::$force_show_wp_errors = TRUE;
     set_transient('sitepush_force_show_wp_errors', TRUE, 30);
 }