public static function inspect() { $repo = Git::open(ABSPATH); if (is_object($repo) && $repo->test_git()) { $status_data = $repo->run('status --porcelain'); $changed_files = array(); if (preg_match_all('/(^.+?)\\s(.*)$/m', $status_data, $changes, PREG_SET_ORDER)) { foreach ($changes as $changed_item) { $change = trim($changed_item[1]); $file = trim($changed_item[2]); $changed_files[$change][] = $file; } } $routine_options = ACI_Routine_Handler::get_options(__CLASS__); if (!is_array($routine_options)) { $routine_options = array(); } if (!is_array($routine_options['changed_files'])) { $routine_options['changed_files'] = array(); } if (empty($routine_options['ignore_files'])) { $routine_options['ignore_files'] = self::$_default_ignore_files; } else { if (!is_array($routine_options['ignore_files'])) { $routine_options['ignore_files'] = (array) $routine_options['ignore_files']; } } foreach (array_keys($changed_files) as $change) { foreach ($routine_options['ignore_files'] as $file_path) { if (!empty($file_path)) { $files_to_ignore = preg_grep('/^' . str_replace('\\*', '*', preg_quote($file_path, '/') . '/'), $changed_files[$change]); if (is_array($files_to_ignore) && count($files_to_ignore) > 0) { foreach (array_keys($files_to_ignore) as $ignore_file_key) { unset($changed_files[$change][$ignore_file_key]); } } } } if (count($changed_files[$change]) > 0) { switch ($change) { case 'A': AC_Inspector::log('Git repository has ' . count($changed_files[$change]) . ' NEW file(s).', __CLASS__); break; case 'M': AC_Inspector::log('Git repository has ' . count($changed_files[$change]) . ' MODIFIED file(s).', __CLASS__); break; case 'D': AC_Inspector::log('Git repository has ' . count($changed_files[$change]) . ' DELETED file(s).', __CLASS__); break; case '??': AC_Inspector::log('Git repository has ' . count($changed_files[$change]) . ' UNTRACKED file(s).', __CLASS__); break; } } } $routine_options['changed_files'] = $changed_files; ACI_Routine_Handler::set_options(__CLASS__, $routine_options); } }
public static function setup() { self::$_options = ACI_Routine_Handler::get_options(__CLASS__); if (!is_array(self::$_options)) { self::$_options = array(); } if (self::$_force_default_allowed_dirs) { self::$_options['allowed_dirs'] = self::$_default_allowed_dirs; } else { if (!is_array(self::$_options['allowed_dirs']) || empty(self::$_options['allowed_dirs'])) { self::$_options['allowed_dirs'] = self::$_default_allowed_dirs; } else { self::$_options['allowed_dirs'] = wp_parse_args(self::$_options['allowed_dirs'], self::$_default_allowed_dirs); } } if (is_link(rtrim(ABSPATH, '/'))) { self::$_real_abspath = realpath(readlink(rtrim(ABSPATH, '/'))); } else { self::$_real_abspath = rtrim(ABSPATH, '/'); } $wildcard_dir_paths = preg_grep("/^(.*\\/[^\\*]*)?([\\*]+\\/)(.*)\$/", self::$_options['allowed_dirs']); if (is_array($wildcard_dir_paths) && count($wildcard_dir_paths) > 0) { foreach ($wildcard_dir_paths as $wc_path) { $wc_path = trim($wc_path, '/'); if ("/*" == substr($wc_path, -2)) { $resolved_paths = glob(self::$_real_abspath . '/' . substr($wc_path, 0, -2), GLOB_ONLYDIR); foreach ($resolved_paths as &$res_path) { if (!empty($res_path)) { $res_path = $res_path . "/*"; } } } else { $resolved_paths = glob(self::$_real_abspath . '/' . $wc_path, GLOB_ONLYDIR); } $allowed_path_key = array_search($wc_path, self::$_options['allowed_dirs']); unset(self::$_options['allowed_dirs'][$allowed_path_key]); if (is_array($resolved_paths) && count($resolved_paths) > 0) { self::$_options['allowed_dirs'] = array_merge(self::$_options['allowed_dirs'], $resolved_paths); } } } self::$_options['allowed_dirs'] = array_filter(array_unique(self::$_options['allowed_dirs'])); }
public function routine_settings_field($args) { $routine = $args['routine']; $log_levels = AC_Inspector::get_log_levels(); $routine_settings = ACI_Routine_Handler::get_options($routine); $schedules = false; if (ACI_Routine_Handler::is_scheduled($routine)) { $schedules = wp_get_schedules(); } if (!empty($routine_settings['description'])) { ?> <tr valign="top"> <td colspan="2" class="description-row" scope="row" valign="top"><div class="howto"><?php echo $routine_settings['description']; ?> </div></td> </tr> <?php } if (isset($routine_settings['site_specific_settings']) && is_multisite() && is_plugin_active_for_network(ACI_PLUGIN_BASENAME)) { ?> <tr valign="top"> <td scope="row" valign="top"><?php _e('Site specific settings', ACI_PLUGIN_TEXTDOMAIN); ?> </td> <td> <select id="<?php echo $routine; ?> _site_specific_settings" name="<?php echo self::$_plugin_action_slug; ?> [<?php echo $routine; ?> ][site_specific_settings]"> <option value="1"<?php echo $routine_settings['site_specific_settings'] ? " selected" : ""; ?> ><?php _e('Yes', ACI_PLUGIN_TEXTDOMAIN); ?> </option> <option value="0"<?php echo $routine_settings['site_specific_settings'] ? "" : " selected"; ?> ><?php _e('No', ACI_PLUGIN_TEXTDOMAIN); ?> </option> </select> <div class="howto"><?php _e('Submit your settings to enable/disable site-specific settings', ACI_PLUGIN_TEXTDOMAIN); ?> </div> </td> </tr> <?php } if ($routine_settings['site_specific_settings'] && is_multisite() && is_plugin_active_for_network(ACI_PLUGIN_BASENAME)) { global $wpdb; $site_blog_ids = $wpdb->get_col("SELECT blog_id FROM " . $wpdb->prefix . "blogs"); if (is_array($site_blog_ids)) { foreach ($site_blog_ids as $site_blog_id) { $sitename = get_blog_details($site_blog_id)->blogname; ?> <tr valign="top"> <td scope="row" valign="top"><?php printf(__('Log level on %s', ACI_PLUGIN_TEXTDOMAIN), $sitename); ?> </td> <td> <select id="<?php echo $routine; ?> _<?php echo $site_blog_id; ?> _log_level" name="<?php echo self::$_plugin_action_slug; ?> [<?php echo $routine; ?> ][<?php echo $site_blog_id; ?> ][log_level]"> <?php foreach ($log_levels as $level) { ?> <option value="<?php echo $level; ?> "<?php echo $level == $routine_settings[$site_blog_id]['log_level'] ? " selected" : ""; ?> ><?php _e(ucfirst($level), ACI_PLUGIN_TEXTDOMAIN); ?> </option> <?php } ?> </select> </td> </tr> <?php if ($schedules) { if (!array_key_exists('schedule', $routine_settings[$site_blog_id]) || empty($routine_settings[$site_blog_id]['schedule']) || !in_array($routine_settings[$site_blog_id]['schedule'], array_keys($schedules))) { $scheduled = 'daily'; } else { $scheduled = $routine_settings[$site_blog_id]['schedule']; } ?> <tr valign="top"> <td scope="row" valign="top"><?php printf(__('Recurrence on %s', ACI_PLUGIN_TEXTDOMAIN), $sitename); ?> </td> <td> <select id="<?php echo $routine; ?> _<?php echo $site_blog_id; ?> _schedule" name="<?php echo self::$_plugin_action_slug; ?> [<?php echo $routine; ?> ][<?php echo $site_blog_id; ?> ][schedule]"> <?php foreach (array_keys($schedules) as $schedule) { ?> <option value="<?php echo $schedule; ?> "<?php echo $schedule == $scheduled ? " selected" : ""; ?> ><?php _e($schedules[$schedule]['display'], ACI_PLUGIN_TEXTDOMAIN); ?> </option> <?php } ?> </select> </td> </tr> <?php } } } } else { ?> <tr valign="top"> <td scope="row"><?php _e('Log level', ACI_PLUGIN_TEXTDOMAIN); ?> </td> <td> <select id="<?php echo $routine; ?> _log_level" name="<?php echo self::$_plugin_action_slug; ?> [<?php echo $routine; ?> ][log_level]"> <?php foreach ($log_levels as $level) { ?> <option value="<?php echo $level; ?> "<?php echo $level == $routine_settings['log_level'] ? " selected" : ""; ?> ><?php _e(ucfirst($level), ACI_PLUGIN_TEXTDOMAIN); ?> </option> <?php } ?> </select> </td> </tr> <?php if ($schedules) { if (!array_key_exists('schedule', $routine_settings) || empty($routine_settings['schedule']) || !in_array($routine_settings['schedule'], array_keys($schedules))) { $scheduled = 'daily'; } else { $scheduled = $routine_settings['schedule']; } ?> <tr valign="top"> <td scope="row" valign="top"><?php _e('Recurrence', ACI_PLUGIN_TEXTDOMAIN); ?> </td> <td> <select id="<?php echo $routine; ?> _schedule" name="<?php echo self::$_plugin_action_slug; ?> [<?php echo $routine; ?> ][schedule]"> <?php foreach (array_keys($schedules) as $schedule) { ?> <option value="<?php echo $schedule; ?> "<?php echo $schedule == $scheduled ? " selected" : ""; ?> ><?php _e($schedules[$schedule]['display'], ACI_PLUGIN_TEXTDOMAIN); ?> </option> <?php } ?> </select> </td> </tr> <?php } } do_action($routine . '_settings_field', $routine_settings, $args); }
public static function log($message, $routine = '', $args = array()) { if (!is_array($args) && is_numeric($args)) { // For backwards compatibility... $args = array('site_id' => $args); } $default_args = array('site_id' => get_current_blog_id(), 'site_specific_settings' => false, 'log_level' => 'notice', 'success' => false, 'error' => false); if (!empty($routine)) { $routine_options = ACI_Routine_Handler::get_options($routine); $routine_args = wp_parse_args($routine_options, $default_args); $args = wp_parse_args($args, $routine_args); if (is_array($args)) { if ($args['site_specific_settings'] && is_multisite() && is_plugin_active_for_network(ACI_PLUGIN_BASENAME)) { $site_id = is_numeric($site_id) ? $site_id : get_current_blog_id(); if (is_array($args[$site_id]) && isset($args[$site_id]['log_level'])) { $log_level = $args[$site_id]['log_level']; } else { if (is_array($args[1]) && isset($args[1]['log_level'])) { $log_level = $args[1]['log_level']; } } } if (empty($log_level) && isset($args['log_level'])) { $log_level = $args['log_level']; } } } else { $args = wp_parse_args($args, $default_args); } if (!empty($message) && $log_level != 'ignore') { // Fallback to notice if no default or user supplied log level if (empty($log_level) || !in_array($log_level, self::$_log_levels)) { $log_level = "notice"; } if ($args['success']) { $log_level = "success"; } if ($args['error']) { $log_level = "error"; } $output = '[' . date("d M, Y H:i:s") . '] [' . __CLASS__ . '] [ ' . strtoupper($log_level) . ' ] - '; if (is_array($message) || is_object($message)) { error_log($output, 3, self::$log_path); error_log(print_r($message, true) . "\n", 3, self::$log_path); if (defined('WP_CLI') && WP_CLI) { echo $output . "\n"; print_r($message); echo "\n"; } } else { $message = $output . $message; error_log($message . "\n", 3, self::$log_path); if (defined('WP_CLI') && WP_CLI) { echo $message . "\n"; } } self::$log_count++; if ($args['error']) { self::$error_count++; } if ($args['success']) { self::$success_count++; } } }
/** * If applicable, attempt to repair what is broken. ** WARNING! Do not attempt without doing a backup first! ** * * ## OPTIONS * * [<routine>] * : Try to repair what is broken according to specific inspection routine * * ## EXAMPLES * * wp angry-inspector repair file-permissions * * @synopsis [--force] [<routine>] [<other-routine>] */ function repair($routines = array(), $assoc_args) { $all_routines = ACI_Routine_Handler::get_inspection_routines(); $all_routine_slugs = array(); $force_repair = true; foreach ($all_routines as $key => $routine) { $all_routine_slugs[$key] = str_replace('_', '-', str_replace('aci_routine_', '', str_replace('aci_routine_check_', '', strtolower($routine)))); } if (empty($routines) || !is_array($routines) || 0 == count($routines)) { $force_repair = false; $routines = $all_routine_slugs; } if ($assoc_args['force']) { $force_repair = true; } foreach ($routines as $routine) { if (in_array($routine, $all_routine_slugs)) { $total_log_count = AC_Inspector::$log_count; $total_error_count = AC_Inspector::$error_count; $total_success_count = AC_Inspector::$success_count; $routine_key = array_search($routine, $all_routine_slugs); $routine_options = ACI_Routine_Handler::get_options($all_routines[$routine_key]); $repair_method = ACI_Routine_Handler::get_repair_method($all_routines[$routine_key], $routine_options); $enabled_routine = false; if ($force_repair) { $enabled_routine = true; ACI_Routine_Handler::force_enable($all_routines[$routine_key]); } else { if (!empty($routine_options['site_specific_settings']) && is_multisite() && is_plugin_active_for_network(ACI_PLUGIN_BASENAME)) { $current_site_id = get_current_blog_id(); if ($routine_options[$current_site_id]['log_level'] != 'ignore') { $enabled_routine = true; } } else { if ($routine_options['log_level'] != 'ignore') { $enabled_routine = true; } } } if (!$enabled_routine) { echo "Skipping disabled routine {$routine}...\n\n"; continue; } if (empty($repair_method)) { WP_CLI::error("No repair method for {$routine}, skipping..."); } if (is_array($repair_method)) { if (class_exists($repair_method[0]) && method_exists($repair_method[0], $repair_method[1])) { echo "Calling repair method for {$routine}...\n"; call_user_func($repair_method); } else { WP_CLI::error("Failed to load the repair method for {$routine}."); } } else { if (function_exists($repair_method)) { echo "Calling repair method for {$routine}...\n"; call_user_func($repair_method); } else { WP_CLI::error("Failed to load the repair method for {$routine}."); break; } } $routine_log_count = AC_Inspector::$log_count - $total_log_count; $routine_error_count = AC_Inspector::$error_count - $total_error_count; $routine_success_count = AC_Inspector::$success_count - $total_success_count; if ($routine_error_count > 0) { WP_CLI::error("Repair method for routine '{$routine}' yielded {$routine_error_count} error(s).\n"); } else { if ($routine_success_count > 0 || $routine_log_count > 0) { WP_CLI::success("Successfully performed repair method for routine '{$routine}' with no errors.\n"); } else { WP_CLI::success("Nothing seems broken. If it ain't broke, don't fix it.\n"); } } } else { WP_CLI::error("Unrecognized repair method for routine '{$routine}'."); } } }