Advertisement
MarcHumer

Prefix-Hardcode-Checker

Feb 14th, 2025 (edited)
213
0
171 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | Source Code | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MH Prefix Hardcode Checker
  4. 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.
  5. Version: 1.0
  6. Author: Marc Humer
  7. */
  8.  
  9. class MH_Prefix_Hardcode_Checker {
  10.     private $new_prefix_tables = [];
  11.  
  12.     public function __construct() {
  13.         add_action('admin_menu', [$this, 'add_admin_menu']);
  14.     }
  15.  
  16.     public function add_admin_menu() {
  17.         add_menu_page(
  18.             'Prefix Hardcode Checker',
  19.             'Prefix Checker',
  20.             'manage_options',
  21.             'mh-prefix-checker',
  22.             [$this, 'display_page'],
  23.             'dashicons-search',
  24.             100
  25.         );
  26.     }
  27.  
  28.     private function get_current_db_prefix() {
  29.         global $wpdb;
  30.         return $wpdb->prefix;
  31.     }
  32.  
  33.     private function get_tables_with_new_prefix() {
  34.         global $wpdb;
  35.         $current_prefix = $this->get_current_db_prefix();
  36.         $tables = $wpdb->get_col("SHOW TABLES LIKE '{$current_prefix}%'");
  37.         foreach ($tables as $table) {
  38.             $new_table = preg_replace('#^' . preg_quote($current_prefix, '#') . '#', 'wp_', $table);
  39.             $this->new_prefix_tables[] = $new_table;
  40.         }
  41.     }
  42.  
  43.     private function remove_comments($content) {
  44.         if (empty($content)) return '';
  45.         $content = preg_replace('#/\*.*?\*/#s', '', $content);
  46.         $content = preg_replace('#//.*$#m', '', $content);
  47.         $content = preg_replace('#<!--.*?-->#s', '', $content);
  48.         $content = preg_replace('#/\*.*?\*/#s', '', $content);
  49.         return $content;
  50.     }
  51.  
  52.     private function search_for_hardcoded_prefixes() {
  53.         $files_found = [];
  54.         $this->get_tables_with_new_prefix();
  55.         $content_dir = WP_CONTENT_DIR;
  56.         $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($content_dir));
  57.         foreach ($iterator as $file) {
  58.             if (pathinfo($file, PATHINFO_EXTENSION) === 'php' || pathinfo($file, PATHINFO_EXTENSION) === 'js') {
  59.                 $file_content = file_get_contents($file);
  60.                 $clean_content = $this->remove_comments($file_content);
  61.                 foreach ($this->new_prefix_tables as $table) {
  62.                     if (preg_match('#(?:\s|\'|\"|^)(' . preg_quote($table, '#') . ')(?!_)#', $clean_content)) {
  63.                         $lines = file($file);
  64.                         foreach ($lines as $line_number => $line) {
  65.                             if (strpos($line, $table) !== false) {
  66.                                 $files_found[] = [
  67.                                     'file' => $file,
  68.                                     'match' => $table,
  69.                                     'line_number' => $line_number + 1,
  70.                                     'code' => substr(trim($line), 0, 50) . (strlen(trim($line)) > 50 ? ' &hellip;' : ''),
  71.                                 ];
  72.                             }
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.         return $files_found;
  79.     }
  80.  
  81.     public function display_page() {
  82.         $results = $this->search_for_hardcoded_prefixes();
  83.         ?>
  84.         <div class="wrap">
  85.             <h1>Gefundene hartcodierte Präfixe</h1>
  86.             <table class="wp-list-table widefat fixed striped">
  87.                 <thead>
  88.                     <tr>
  89.                         <th>Verzeichnis/Dateiname</th>
  90.                         <th>Gefundener String</th>
  91.                         <th>Inhalt</th>
  92.                         <th>Zeilen-Nr</th>
  93.                     </tr>
  94.                 </thead>
  95.                 <tbody>
  96.                     <?php if (count($results) > 0): ?>
  97.                         <?php foreach ($results as $result): ?>
  98.                             <tr>
  99.                                 <td><?php echo "/".esc_html(str_replace(DIRECTORY_SEPARATOR, '/', str_replace(ABSPATH, '', $result['file']))); ?></td>
  100.                                 <td><?php echo esc_html($result['match']); ?></td>
  101.                                 <td><?php echo esc_html($result['code']); ?></td>
  102.                                 <td><?php echo esc_html($result['line_number']); ?></td>
  103.                             </tr>
  104.                         <?php endforeach; ?>
  105.                     <?php else: ?>
  106.                         <tr>
  107.                             <td colspan="4">Keine hartcodierten Präfixe gefunden.</td>
  108.                         </tr>
  109.                     <?php endif; ?>
  110.                 </tbody>
  111.             </table>
  112.         </div>
  113.         <?php
  114.     }
  115. }
  116.  
  117. new MH_Prefix_Hardcode_Checker();
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement