Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: MH Prefix Hardcode Checker
- Description: Sucht nach hartcodierten Datenbank-Präfixen in allen PHP- und JS-Dateien in /wp-content und tiefer und gibt eine Liste der betroffenen Dateien aus.
- Version: 1.0
- Author: Marc Humer
- */
- class MH_Prefix_Hardcode_Checker {
- private $new_prefix_tables = [];
- public function __construct() {
- add_action('admin_menu', [$this, 'add_admin_menu']);
- }
- public function add_admin_menu() {
- add_menu_page(
- 'Prefix Hardcode Checker',
- 'Prefix Checker',
- 'manage_options',
- 'mh-prefix-checker',
- [$this, 'display_page'],
- 'dashicons-search',
- 100
- );
- }
- private function get_current_db_prefix() {
- global $wpdb;
- return $wpdb->prefix;
- }
- private function get_tables_with_new_prefix() {
- global $wpdb;
- $current_prefix = $this->get_current_db_prefix();
- $tables = $wpdb->get_col("SHOW TABLES LIKE '{$current_prefix}%'");
- foreach ($tables as $table) {
- $new_table = preg_replace('#^' . preg_quote($current_prefix, '#') . '#', 'wp_', $table);
- $this->new_prefix_tables[] = $new_table;
- }
- }
- private function remove_comments($content) {
- if (empty($content)) return '';
- $content = preg_replace('#/\*.*?\*/#s', '', $content);
- $content = preg_replace('#//.*$#m', '', $content);
- $content = preg_replace('#<!--.*?-->#s', '', $content);
- $content = preg_replace('#/\*.*?\*/#s', '', $content);
- return $content;
- }
- private function search_for_hardcoded_prefixes() {
- $files_found = [];
- $this->get_tables_with_new_prefix();
- $content_dir = WP_CONTENT_DIR;
- $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($content_dir));
- foreach ($iterator as $file) {
- if (pathinfo($file, PATHINFO_EXTENSION) === 'php' || pathinfo($file, PATHINFO_EXTENSION) === 'js') {
- $file_content = file_get_contents($file);
- $clean_content = $this->remove_comments($file_content);
- foreach ($this->new_prefix_tables as $table) {
- if (preg_match('#(?:\s|\'|\"|^)(' . preg_quote($table, '#') . ')(?!_)#', $clean_content)) {
- $lines = file($file);
- foreach ($lines as $line_number => $line) {
- if (strpos($line, $table) !== false) {
- $files_found[] = [
- 'file' => $file,
- 'match' => $table,
- 'line_number' => $line_number + 1,
- 'code' => substr(trim($line), 0, 50) . (strlen(trim($line)) > 50 ? ' …' : ''),
- ];
- }
- }
- }
- }
- }
- }
- return $files_found;
- }
- public function display_page() {
- $results = $this->search_for_hardcoded_prefixes();
- ?>
- <div class="wrap">
- <h1>Gefundene hartcodierte Präfixe</h1>
- <table class="wp-list-table widefat fixed striped">
- <thead>
- <tr>
- <th>Verzeichnis/Dateiname</th>
- <th>Gefundener String</th>
- <th>Inhalt</th>
- <th>Zeilen-Nr</th>
- </tr>
- </thead>
- <tbody>
- <?php if (count($results) > 0): ?>
- <?php foreach ($results as $result): ?>
- <tr>
- <td><?php echo "/".esc_html(str_replace(DIRECTORY_SEPARATOR, '/', str_replace(ABSPATH, '', $result['file']))); ?></td>
- <td><?php echo esc_html($result['match']); ?></td>
- <td><?php echo esc_html($result['code']); ?></td>
- <td><?php echo esc_html($result['line_number']); ?></td>
- </tr>
- <?php endforeach; ?>
- <?php else: ?>
- <tr>
- <td colspan="4">Keine hartcodierten Präfixe gefunden.</td>
- </tr>
- <?php endif; ?>
- </tbody>
- </table>
- </div>
- <?php
- }
- }
- new MH_Prefix_Hardcode_Checker();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement