4
4
import shutil
5
5
from concurrent .futures import ThreadPoolExecutor
6
6
from tqdm import tqdm
7
- from config import ( log_filename , YELLOW , BLUE , GREEN , RED , RESET , YAKPRO )
7
+ from config import log_filename , YELLOW , BLUE , GREEN , RED , RESET , YAKPRO
8
8
from subprocess import call
9
9
10
10
# Configure logging
@@ -54,11 +54,7 @@ def obfuscate_file(args):
54
54
obfuscate_php (input_file , obfuscation_options , create_backup , output_directory )
55
55
56
56
def process_directory (directory , obfuscation_options , exclude_list , create_backup , output_directory , max_workers = 4 ):
57
- total_files = 0
58
- for root , _ , files in os .walk (directory ):
59
- for file in files :
60
- if file .lower ().endswith (".php" ):
61
- total_files += 1
57
+ total_files = sum ([len (files ) for r , d , files in os .walk (directory ) if any (f .lower ().endswith ('.php' ) for f in files )])
62
58
63
59
progress_bar = tqdm (total = total_files , desc = "Obfuscating" , unit = "file" )
64
60
@@ -82,15 +78,28 @@ def process_directory(directory, obfuscation_options, exclude_list, create_backu
82
78
83
79
progress_bar .close ()
84
80
81
+ def validate_mode_input (mode ):
82
+ try :
83
+ mode = int (mode )
84
+ if mode not in [1 , 2 , 3 ]:
85
+ raise ValueError
86
+ return mode
87
+ except ValueError :
88
+ logging .error ("Invalid mode input. Please enter 1, 2, or 3." )
89
+ print (f"{ RED } Invalid mode. Choose from: 1, 2, or 3{ RESET } " )
90
+ sys .exit (1 )
91
+
92
+ def validate_directory_input (directory_path ):
93
+ if not os .path .isdir (directory_path ):
94
+ logging .error (f"Invalid directory path: { directory_path } " )
95
+ print (f"{ RED } Invalid directory path{ RESET } " )
96
+ sys .exit (1 )
97
+
85
98
def main ():
86
99
print (f"{ GREEN } Welcome to the PHP Obfuscator!{ RESET } " )
87
100
print (f"{ GREEN } Follow the prompts to obfuscate your PHP files.\n { RESET } " )
88
101
89
- print (f"{ GREEN } Choose the mode for obfuscating your PHP files:{ RESET } " )
90
- print (f"{ BLUE } 1: Single file{ RESET } " )
91
- print (f"{ BLUE } 2: Multiple files{ RESET } " )
92
- print (f"{ BLUE } 3: Entire project directory{ RESET } " )
93
- mode = input (f"{ GREEN } Enter the mode number (1/2/3): { RESET } " )
102
+ mode = validate_mode_input (input (f"{ GREEN } Enter the mode number (1/2/3): { RESET } " ))
94
103
95
104
output_directory = input (f"{ GREEN } Enter the output directory path: { RESET } " )
96
105
if not os .path .exists (output_directory ):
@@ -99,23 +108,21 @@ def main():
99
108
exclude_input = input (f"{ GREEN } Enter file or directory paths to exclude (separated by a space) (you can skip this step): { RESET } " )
100
109
exclude_list = [os .path .abspath (exclude .strip ()) for exclude in exclude_input .split ()]
101
110
102
- backup_input = input (f"{ GREEN } Create backups of original PHP files? (y/n): { RESET } " ).lower ()
103
- create_backup = backup_input == 'y'
111
+ create_backup = input (f"{ GREEN } Create backups of original PHP files? (y/n): { RESET } " ).lower () == 'y'
104
112
105
113
obfuscation_options = get_obfuscation_options ()
106
114
107
- if mode == '1' :
115
+ if mode == 1 :
108
116
input_file = input (f"{ GREEN } Enter the PHP file path: { RESET } " )
109
117
if not input_file .lower ().endswith (".php" ) or not os .path .isfile (input_file ):
110
118
logging .warning ("Invalid PHP file path" )
111
119
print (f"{ RED } Invalid PHP file path{ RESET } " )
112
120
sys .exit (1 )
113
- # Checking for exclusions should be inside this mode '1' block
114
121
if any (os .path .commonpath ([input_file , exclude ]) == os .path .abspath (exclude ) for exclude in exclude_list ):
115
122
logging .info (f"Skipping { input_file } : excluded" )
116
123
else :
117
124
obfuscate_php (input_file , obfuscation_options , create_backup , output_directory )
118
- if mode == '2' :
125
+ elif mode == 2 :
119
126
file_paths = input (f"{ GREEN } Enter the PHP file paths separated by a space: { RESET } " )
120
127
files = file_paths .split ()
121
128
@@ -125,12 +132,9 @@ def main():
125
132
print (f"{ RED } Skipping { input_file } : not a valid PHP file{ RESET } " )
126
133
continue
127
134
obfuscate_php (input_file , obfuscation_options , create_backup , output_directory )
128
- elif mode == '3' :
135
+ elif mode == 3 :
129
136
input_directory = input (f"{ GREEN } Enter the project directory path: { RESET } " )
130
- if not os .path .isdir (input_directory ):
131
- logging .warning ("Invalid directory path" )
132
- print (f"{ RED } Invalid directory path{ RESET } " )
133
- sys .exit (1 )
137
+ validate_directory_input (input_directory )
134
138
process_directory (input_directory , obfuscation_options , exclude_list , create_backup , output_directory )
135
139
else :
136
140
logging .warning ("Invalid mode. Choose from: 1, 2, or 3" )
0 commit comments