-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjambo.py
428 lines (316 loc) · 14.1 KB
/
jambo.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Just Another Module Build script for Opencart.
"""
import os
import sys
import argparse
import fnmatch
import re
import datetime
import zipfile
import base64
PATH = {
'module': 'module',
'addons': 'addons',
'assets': 'assets',
'storage': '.storage',
'exclude': '.exclude',
}
CONST = {}
def parse_constants():
"""Load data from config file in the format CONST=value."""
file_name = '.module.cf'
constants = {}
if os.path.exists(file_name):
with open(file_name, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and not line.startswith('/'):
key_value = line.split('=', 1)
if len(key_value) == 2:
key, value = key_value
constants[key.strip()] = value.strip()
return constants
def is_binary_file(file_path):
"""Checks the first 1024 bytes to see if the file is binary"""
with open(file_path, 'rb') as f:
return b'\0' in f.read(1024)
def minify(code):
"""Minify PHP code by remove spaces and new lines"""
# 1. Skip "<?php" at the beginning and "?>" at the end
code = re.sub(r'^<\?php', '', code)
code = re.sub(r'\?>$', '', code)
# 2. Saving string variables
strings = []
def save_strings(match):
# Save string var and return the marker
strings.append(match.group(0))
return f"__STRING_{len(strings) - 1}__"
# Replace strings with special markers
code = re.sub(r'"(.*?)"|\'(.*?)\'', save_strings, code)
# 3. Remove single-line comments // and # only outside the lines
code = re.sub(r'//(?!.*__STRING_\d+).*', '', code)
code = re.sub(r'#(?!.*__STRING_\d+).*', '', code)
# 4. Remove multi-line comments /* ... */ including new lines
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
# 5. Replacing multiple spaces and new lines with one (outside the string vars)
code = re.sub(r'\s+', ' ', code)
# 6. Remove extra spaces around operators (for example, =, +, -, *, /, (, ), {, })
code = re.sub(r'\s*([<>=+\-*/(){}\[\]\.:,;])\s*', r'\1', code)
# 7. Return string variables to their places
def restore_strings(match):
index = int(match.group(1)) # Get the row index
return strings[index] # Return the line to its place
# 8. Return lines instead of markers
code = re.sub(r'__STRING_(\d+)__', restore_strings, code)
return code.strip()
def replace(file_path):
"""Replacing tags."""
if not os.path.exists(file_path):
raise FileNotFoundError(f"File {file_path} not found.")
if is_binary_file(file_path):
return ''
with open(file_path, 'r', encoding='utf-8') as f:
content = f.readlines()
processed_content = []
for line_num, line in enumerate(content, 1):
# File content insertion
tags = re.findall(r'STUB_RAW\(\'(.*?)\'\);', line)
for file_name in tags:
include_file = os.path.join(PATH['assets'], file_name)
if not os.path.exists(include_file):
raise FileNotFoundError(f"Missing file tag: {include_file} in file {file_path}:{line_num}")
with open(include_file, 'r', encoding='utf-8') as ef:
include_content = ef.read()
line = line.replace(f"STUB_RAW('{file_name}');", include_content.strip())
# Replacement of base64 tags
tags = re.findall(r'STUB_B64\((.*?)\)', line)
for file_name in tags:
include_file = os.path.join(PATH['assets'], file_name)
if not os.path.exists(include_file):
raise FileNotFoundError(f"Missing file tag: {include_file} in file {file_path}:{line_num}")
with open(include_file, 'r', encoding='utf-8') as ef:
include_content = ef.read()
include_content_b64 = base64.b64encode(include_content.encode('utf-8')).decode('utf-8')
line = line.replace(f"STUB_B64\({file_name})", include_content_b64)
# Replacement of variable tags
tags = re.findall(r'STUB\((.*?)\)', line)
for const_name in tags:
if const_name not in CONST:
raise ValueError(f"Missing variable tag: {const_name} in file {file_path}:{line_num}")
const_value = CONST[const_name]
line = line.replace(f"STUB({const_name})", const_value)
# xor code
tags = re.findall(r'STUB_ENC\((.*?)\)', line)
key = CONST['MODULE'].lower().replace(' ', '_')
for file_name in tags:
include_file = os.path.join(PATH['assets'], file_name)
if not os.path.exists(include_file):
raise FileNotFoundError(f"Missing file tag: {include_file} in file {file_path}:{line_num}")
with open(include_file, 'r', encoding='utf-8') as ef:
include_content = ef.read()
line = line.replace(f"STUB_ENC({file_name})", xor(minify(include_content), key))
processed_content.append(line)
return ''.join(processed_content)
def xor(data, key):
encrypted = ''.join(chr(ord(c) ^ ord(key[i % len(key)])) for i, c in enumerate(data))
return base64.b64encode(encrypted.encode()).decode()
def encrypt_file(content):
"""Encrypt content."""
# TODO: Додати шифрування пізніше
return content
def decrypt_file(content):
"""Decrypt content."""
# TODO: Додати дешифрування пізніше
return content
def is_exclusion(path):
"""
Check if the path matches one of the patterns in the exceptions file.
The following patterns are supported: */, /*/ for any nesting of directories.
"""
# Read patterns from the file
with open(PATH['exclude'], 'r', encoding='utf-8') as f:
exclusion_patterns = [line.strip() for line in f if line.strip() and not line.startswith(('#', '/'))]
# Normalize the path for checking between OSes
path = os.path.relpath(path, start=os.getcwd()).replace(os.sep, '/')
is_directory = os.path.isdir(path)
for pattern in exclusion_patterns:
if '*/' in pattern:
pattern = pattern.replace('*/', '**/')
if '/*/' in pattern:
pattern = pattern.replace('/*/', '/**/')
if is_directory:
if pattern.endswith('/'):
if os.path.basename(path) + '/' == pattern:
return True
if path.endswith('/' + pattern.strip('/')):
return True
if fnmatch.fnmatch(path + '/', pattern):
return True
else:
if path == pattern:
return True
if os.path.basename(path) == pattern:
return True
if fnmatch.fnmatch(path, pattern):
return True
return False
def clear_directory(directory):
"""Clear directory."""
for root, dirs, files in os.walk(directory, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
def copy(from_, into_, encrypt=False):
"""
Recursively copy files from `from_` to `into_`, subject to global exclusions.
Encryption or decryption depending on the encrypt parameter.
"""
clear_directory(into_)
for root, dirs, files in os.walk(from_):
dirs[:] = [d for d in dirs if not is_exclusion(os.path.join(root, d))]
for file in files:
if is_exclusion(os.path.join(root, file)):
continue
rel_path = os.path.relpath(os.path.join(root, file), from_)
source_file = os.path.join(root, file)
dest_file = os.path.join(into_, rel_path)
dest_file_dir = os.path.dirname(dest_file)
print(f"{source_file}")
if not os.path.exists(dest_file_dir):
os.makedirs(dest_file_dir)
with open(source_file, 'rb') as sf:
file_content = sf.read()
if encrypt:
file_content = encrypt_file(file_content)
else:
file_content = decrypt_file(file_content)
# Записуємо результат в dest_file
with open(dest_file, 'wb') as df:
df.write(file_content)
def zip(source_dir, output_zip):
"""Create a zip archive from the files in `source_dir`, replacing tags in text files before."""
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(source_dir):
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, source_dir)
zip_info = zipfile.ZipInfo(rel_path)
set_date = datetime.datetime(datetime.datetime.now().year, 1, 1, 0, 0, 1) # Set date to 01.01.YEAR
zip_info.date_time = set_date.timetuple()[:6]
if not is_binary_file(file_path):
content = replace(file_path) # replace tags
zipf.writestr(zip_info, content)
else:
# Binary files simply add to the archive
with open(file_path, 'rb') as f:
zipf.writestr(zip_info, f.read())
def parse_arguments():
parser = argparse.ArgumentParser(description='Processing source code of OpenCart modules.')
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-e',
action='store_true',
help='Encrypt source files to the {PATH[storage]} directory.',
)
group.add_argument(
'-d',
action='store_true',
help='Decrypt source files from the {PATH[storage]} directory.',
)
group.add_argument(
'-z',
type=int,
metavar='N',
help='Create zip archive. N - module or addon number.',
)
parser.add_argument(
'-v',
metavar='XX',
help='Optional, used with -z 0. Module version, e.g., 2x, 3x, 4x, etc.',
)
# If specified -h/--help or notihing, help will be displayed automatically
return parser.parse_args()
def main():
global CONST
CONST = parse_constants()
args = parse_arguments()
if args.e:
clear_directory(PATH['storage'])
for dir_name in [PATH['assets'], PATH['addons'], PATH['module']]:
from_ = dir_name
into_ = os.path.join(PATH['storage'], dir_name)
if os.path.exists(from_):
copy(from_, into_, encrypt=True)
elif args.d:
for dir_name in [PATH['assets'], PATH['addons'], PATH['module']]:
from_ = os.path.join(PATH['storage'], dir_name)
into_ = dir_name
if os.path.exists(from_):
copy(from_, into_, encrypt=False)
elif args.z is not None:
n = args.z
module_name = CONST['MODULE'].lower().replace(' ', '_')
if n == 0:
if not args.v:
print('You must specify the version using the -v option (2x, 3x, etc.).')
sys.exit(1)
version = args.v
CONST['CODE'] = module_name
CONST['BASE_NAME'] = module_name
CONST['FULL_NAME'] = '/ocmod.space/' + module_name
source_dir = os.path.join(PATH['module'], 'src', version)
output_zip = os.path.join(PATH['module'], 'zip', version, f"{module_name}.ocmod.zip")
else:
# Отримуємо список аддонів
addons_list = sorted(
[d for d in os.listdir(PATH['addons']) if os.path.isdir(os.path.join(PATH['addons'], d))])
if n - 1 >= len(addons_list):
print(f"Addon associated with number {n} not found.")
sys.exit(1)
addon_name = '__'.join((module_name, addons_list[n - 1].replace('-', '_')))
CONST['ADDON'] = addon_name.replace('__', ' | ').replace('_', ' ').title().replace(' | ', '|')
CONST['CODE'] = addon_name
CONST['BASE_NAME'] = addon_name.replace('__', '/')
CONST['FULL_NAME'] = '/ocmod.space/' + CONST['BASE_NAME']
source_dir = os.path.join(PATH['addons'], addons_list[n - 1], 'src')
output_zip = os.path.join(PATH['addons'], addons_list[n - 1], 'zip', f"{addon_name}.ocmod.zip")
if os.path.exists(source_dir):
os.makedirs(os.path.dirname(output_zip), exist_ok=True)
zip(source_dir, output_zip)
print(f"Archive {output_zip} created!")
else:
print(f"Directory {source_dir} does not exist!")
else:
# Display help and a list of available modules/addons
print('usage: jambo.py [-h] [-e | -d | -z N] [-v XX]')
print('\nProcessing source code of OpenCart modules.')
print('\noptions:')
print(f" -e Encrypt source files into the {PATH['storage']} directory.")
print(f" -d Play and decrypt the structure from the {PATH['storage']} directory.")
print(' -z N [-v XX] Create zip archive for module or addon number N.')
print(' -v XX used for N=0 and specify module version, e.g. 2x, 3x, 4x, etc.')
print(' -h, --help Print this help.')
print('\nAvailable modules and addons:')
# Module
module_versions = []
module_src_path = os.path.join(PATH['module'], 'src')
if os.path.exists(module_src_path):
module_versions = [
d for d in os.listdir(module_src_path) if os.path.isdir(os.path.join(module_src_path, d))
]
if module_versions:
print('Module (N=0):')
for v in module_versions:
print(f" -v{v} module/src/{v}")
# Addons
if os.path.exists(PATH['addons']):
addons_list = sorted(
[d for d in os.listdir(PATH['addons']) if os.path.isdir(os.path.join(PATH['addons'], d))])
if addons_list:
print('Addons:')
for idx, addon in enumerate(addons_list, start=1):
print(f" -z{idx} - addons/{addon}")
if __name__ == '__main__':
main()