Skip to content

Commit 126f8f2

Browse files
authored
Key Improvements - Added Modular Functions like validate_mode_input and validate_directory_input for better code organization. Improved error handling with more specific messages and logging. Added more logging details to help debug issues. Validated user inputs for mode and directory path.
1 parent 7952e18 commit 126f8f2

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

main.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
from concurrent.futures import ThreadPoolExecutor
66
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
88
from subprocess import call
99

1010
# Configure logging
@@ -54,11 +54,7 @@ def obfuscate_file(args):
5454
obfuscate_php(input_file, obfuscation_options, create_backup, output_directory)
5555

5656
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)])
6258

6359
progress_bar = tqdm(total=total_files, desc="Obfuscating", unit="file")
6460

@@ -82,15 +78,28 @@ def process_directory(directory, obfuscation_options, exclude_list, create_backu
8278

8379
progress_bar.close()
8480

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+
8598
def main():
8699
print(f"{GREEN}Welcome to the PHP Obfuscator!{RESET}")
87100
print(f"{GREEN}Follow the prompts to obfuscate your PHP files.\n{RESET}")
88101

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}"))
94103

95104
output_directory = input(f"{GREEN}Enter the output directory path: {RESET}")
96105
if not os.path.exists(output_directory):
@@ -99,23 +108,21 @@ def main():
99108
exclude_input = input(f"{GREEN}Enter file or directory paths to exclude (separated by a space) (you can skip this step): {RESET}")
100109
exclude_list = [os.path.abspath(exclude.strip()) for exclude in exclude_input.split()]
101110

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'
104112

105113
obfuscation_options = get_obfuscation_options()
106114

107-
if mode == '1':
115+
if mode == 1:
108116
input_file = input(f"{GREEN}Enter the PHP file path: {RESET}")
109117
if not input_file.lower().endswith(".php") or not os.path.isfile(input_file):
110118
logging.warning("Invalid PHP file path")
111119
print(f"{RED}Invalid PHP file path{RESET}")
112120
sys.exit(1)
113-
# Checking for exclusions should be inside this mode '1' block
114121
if any(os.path.commonpath([input_file, exclude]) == os.path.abspath(exclude) for exclude in exclude_list):
115122
logging.info(f"Skipping {input_file}: excluded")
116123
else:
117124
obfuscate_php(input_file, obfuscation_options, create_backup, output_directory)
118-
if mode == '2':
125+
elif mode == 2:
119126
file_paths = input(f"{GREEN}Enter the PHP file paths separated by a space: {RESET}")
120127
files = file_paths.split()
121128

@@ -125,12 +132,9 @@ def main():
125132
print(f"{RED}Skipping {input_file}: not a valid PHP file{RESET}")
126133
continue
127134
obfuscate_php(input_file, obfuscation_options, create_backup, output_directory)
128-
elif mode == '3':
135+
elif mode == 3:
129136
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)
134138
process_directory(input_directory, obfuscation_options, exclude_list, create_backup, output_directory)
135139
else:
136140
logging.warning("Invalid mode. Choose from: 1, 2, or 3")

0 commit comments

Comments
 (0)