-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-string-finder.php
79 lines (68 loc) · 2.55 KB
/
php-string-finder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Plugin Name: PHP String Finder in WordPress
* Description: A plugin to search for a string within PHP files.
* Version: 1.0
* Author: D.Kandekore
*/
// Prevent direct file access
if (!defined('ABSPATH')) {
exit;
}
class EnhancedPHPStringFinder {
private $maxFileSize = 500000; // Skip files larger than this size (in bytes).
public function __construct() {
add_action('admin_menu', array($this, 'adminMenu'));
}
public function adminMenu() {
add_management_page(
'PHP String Finder',
'PHP String Finder',
'manage_options',
'php-string-finder',
array($this, 'createAdminPage')
);
}
public function createAdminPage() {
?>
<div class="wrap">
<h2>Enhanced PHP String Finder</h2>
<form method="post">
<?php wp_nonce_field('enhanced_php_string_finder_search'); ?>
<p>
<label for="string_search">Enter String to Search:</label>
<input type="text" name="string_search" required />
</p>
<input type="submit" name="search_string" class="button button-primary" value="Search" />
</form>
</div>
<?php
if (isset($_POST['search_string'])) {
if (!wp_verify_nonce($_POST['_wpnonce'], 'enhanced_php_string_finder_search')) {
die('Invalid request.');
}
$search = stripslashes(sanitize_text_field($_POST['string_search']));
// Set a time limit for this script to avoid timeouts on lengthy searches
set_time_limit(600); // 10 minutes
$this->searchStringInFiles(ABSPATH, $search); // Search in WordPress root directory
}
}
private function searchStringInFiles($dir, $search) {
try {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
if ($file->getExtension() === 'php' && $file->getSize() <= $this->maxFileSize) {
$content = file_get_contents($file->getRealPath());
if (strpos($content, $search) !== false) {
echo "<p>String found in: " . esc_html($file->getRealPath()) . "</p>";
}
}
}
} catch (Exception $e) {
echo "<p>Error encountered: " . esc_html($e->getMessage()) . "</p>";
}
}
}
// Instantiate the plugin class
new EnhancedPHPStringFinder();
?>