This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tplLib.py
329 lines (323 loc) · 16.3 KB
/
tplLib.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
import datetime, os, random, shutil, time
from classesLib import TplSection, UniParam, MacroResult, MacroToCallable, Page, ItemEntry, FileList, object_from_dict
from scriptLib import Commands
from helpersLib import replace_str, read_ini, concat_dict, concat_list, purify, smartsize, sort, join_path, strip_starting_spaces, replace_str_at_position
from cfgLib import Config
class MacroNotClosedProperly(Exception):
""" Exception: Macro not closed properly """
class TooManyRecurs(Exception):
""" Exception: Too many recurs in macro execution """
class Interpreter():
""" Template interpreter, manages:
- Sections
- String parsing
- Macro execute (handler)
"""
def __init__(self, tpl_file='hfs.tpl'):
self.handler = Commands()
self.uptime_start = time.time()
self.cached_pages = {}
self.symbols = {
'style': self.handler.sym_style,
'user': self.handler.sym_user,
'login-link': lambda p: self.get_section('login-link', p, True, True),
'loggedin': lambda p: self.get_section('loggedin', p, True, True),
'ip': lambda p: MacroResult(p.request.host),
'version': lambda p: MacroResult(Config.version),
'timestamp': lambda p: MacroResult(str(datetime.datetime.now())),
'uptime': lambda p: MacroResult(str(datetime.timedelta(seconds=round(time.time() - self.uptime_start)))),
'connections': lambda p: MacroResult('0'),
'speed-out': lambda p: MacroResult('0'),
'speed-in': lambda p: MacroResult('0'),
'total-out': lambda p: MacroResult('0'),
'total-in': lambda p: MacroResult('0'),
'total-downloads': lambda p: MacroResult('0'),
'total-uploads': lambda p: MacroResult('0'),
'number-addresses': lambda p: MacroResult('0'),
'number-addresses-downloading': lambda p: MacroResult('0'),
'build': lambda p: MacroResult(Config.build),
'sequencial': lambda p: MacroResult('0'),
'number-addresses-ever': lambda p: MacroResult('0'),
'port': lambda p: MacroResult(Config.port),
'folder': lambda p: MacroResult((p.request.path_virtual_dir + '/') if p.request != None else ''),
'encoded-folder': lambda p: MacroResult(purify(join_path(p.request.path_virtual_dir, '/')) if p.request != None else '')
}
self.sections = object_from_dict({
'_empty': TplSection('', [], {}),
'': TplSection('', ['public'], {
'files': lambda p: self.get_page('files', p),
'up': lambda p: self.get_section('up', p, True, True),
'upload-link': lambda p: self.get_section('upload-link', p, True, True),
'host': lambda p: MacroResult(p.request.host),
'number': lambda p: MacroResult(str(p.filelist.count)),
'number-files': lambda p: MacroResult(str(p.filelist.count_files)),
'number-folders': lambda p: MacroResult(str(p.filelist.count_folders)),
'total-size': lambda p: MacroResult(smartsize(sum([os.stat(x.path).st_size for x in p.filelist.items]))),
'total-kbytes': lambda p: MacroResult(str(sum([os.stat(x.path).st_size for x in p.filelist.items]) // 1024)),
'total-bytes': lambda p: MacroResult(str(sum([os.stat(x.path).st_size for x in p.filelist.items]))),
'list': self.get_list,
'folder-item-comment': lambda p: MacroResult('')
}),
'files': TplSection('', [], {
'list': self.get_list,
'item-archive': lambda p: self.get_section('item-archive', p, True, True),
}),
'nofiles': TplSection('', [], {}),
'upload': TplSection('', [], {
'diskfree': lambda p: MacroResult(smartsize(shutil.disk_usage(p.request.path_real_dir).free)),
})
})
f = open(tpl_file, 'r', encoding='utf-8')
c = '\n' + f.read()
f.close()
s = c.split('\n[')
for i in s:
t = i.split(']\n', 1)
if len(t) <= 1:
continue
plus = False
prepend = False
if t[0][0:1] == '+':
plus = True
t[0] = t[0][1:]
elif t[0][0:1] == '^':
prepend = True
t[0] = t[0][1:]
p = t[0].split('|')
for j in p[0].split('='):
j = j.strip()
if j not in self.sections:
self.sections[j] = TplSection('', [], {})
if plus:
self.sections[j].content += t[1].strip('\n') + '\n'
elif prepend:
self.sections[j].content = t[1] + self.sections[j].content.strip('\n') + '\n'
else:
self.sections[j].content = t[1].strip('\n') + '\n'
self.sections[j].params = p[1:]
self.translations = {}
for i in self.sections.get('special:strings', self.sections['_empty']).content.split('\n'):
pair = i.split('=', 1)
if len(pair) < 2:
continue
if pair[0] not in self.translations:
self.translations[pair[0]] = pair[1]
alias_from_txt = read_ini('alias.txt')
for i in alias_from_txt:
self.handler[i] = MacroToCallable(alias_from_txt[i], UniParam([], interpreter=self), True)
for i in self.sections.get('special:alias', self.sections['_empty']).content.split('\n'):
pair = i.split('=', 1)
if len(pair) < 2:
continue
self.handler[pair[0]] = MacroToCallable(pair[1], UniParam([], interpreter=self), True)
return
def get_list(self, param: UniParam):
""" Get filelist, called by symbol `%list%`.
"""
page_content = param.filelist.to_list(param)
param.request.listing_completed = True
return Page(page_content, 200)
def get_section(self, section_name: str, param: UniParam, do_parse=True, force=False) -> MacroResult:
""" Get a section from template. What this returns is a `MacroResult`.
`section_name`: Name of section.
`param`: `UniParam` for parsing macros and symbols.
`do_parse`: Parse the content?
`force`: Get this section even if not public?
"""
section: TplSection = self.sections.get(section_name, None)
if section == None:
return None
param.symbols = concat_dict(param.symbols, section.symbols)
return self.parse_text(section.content, param) if do_parse else MacroResult(section.content)
def section_to_page(self, section_name, param: UniParam):
# Deep copy param, prevent modifying original one
uni_param = UniParam(param.params, interpreter=param.interpreter, request=param.request, filelist=param.filelist, statistics=param.statistics)
section = self.get_section(section_name, uni_param, True, True)
if section == None:
return self.get_page('error-page', UniParam(['not found', 404], interpreter=self, request=param.request))
status = 200
return Page(section.content, status, section.headers)
def get_page(self, page_name: str, param: UniParam) -> Page:
uni_param = param
if page_name == '':
page = self.section_to_page('', param)
page.content = replace_str(page.content, '%build-time%', str(round(time.time() - param.request.build_time_start, 3)))
return page
elif page_name == 'files':
if param.filelist.count == 0:
nofiles = self.get_section('nofiles', param, True, True)
return Page(nofiles.content, 200)
return self.section_to_page('files', param)
elif page_name == 'list':
return self.get_list(uni_param)
elif page_name == 'upload':
return self.section_to_page('upload', param)
elif page_name == 'upload-results':
page = self.section_to_page('upload-results', param)
_success = self.get_section('upload-success', uni_param, False, True)
_failed = self.get_section('upload-failed', uni_param, False, True)
uploaded_files = []
upload_result = param.params[0]
assert type(upload_result) == dict, 'param.params[0] is not a dict'
for i in upload_result:
result = upload_result[i]
if result[0] == True:
uploaded_files.append(self.parse_text(_success.content, UniParam([], symbols={
'item-name': lambda p: MacroResult(i),
'item-size': lambda p: MacroResult(smartsize(os.stat(param.request.path_real + i).st_size)),
'speed': lambda p: MacroResult('0')
}, interpreter=self, request=param.request)).content)
else:
uploaded_files.append(self.parse_text(_failed.content, UniParam([], symbols={
'item-name': lambda p: MacroResult(i),
'reason': lambda p: MacroResult(result[1])
}, interpreter=self, request=param.request)).content)
page.content = replace_str(page.content, '%uploaded-files%', ''.join(uploaded_files))
return page
elif page_name == 'error-page':
error_type = param.params[0]
error_status = param.params[1]
base_page = self.get_section('error-page', UniParam([], symbols={}, request=param.request, interpreter=self, statistics=param.statistics))
content = self.get_section(error_type, UniParam([], symbols={}, request=param.request, interpreter=self, statistics=param.statistics))
headers = concat_dict(base_page.headers, content.headers)
return Page(replace_str(base_page.content, '%content%', content.content), error_status, headers)
def parse_symbols(self, text: str, param: UniParam, *symbols):
for i in symbols:
for j in i:
if '%%%s%%' % j in text:
text = text.replace('%%%s%%' % j, i[j](param).content)
return text
def parse_text(self, text: str, param: UniParam) -> MacroResult:
""" Parse a string and apply symbols and macros
"""
text = self.parse_symbols(text, param, param.symbols, self.symbols)
macro_level = 0
quote_level = 0
position = 0
newlines = 0
last_newline_at = 0
length = len(text)
full_macro = ['']
broken = False
disconnect = False
headers = {}
while position < length:
# We parse content char by char and find tokens
last_macro_at = position
char0 = text[position]
mark0 = text[position:position + 2]
if char0 == '\n':
newlines += 1
last_newline_at = position
if mark0 == '{.' and quote_level == 0:
macro_level += 1
if macro_level > 100:
raise TooManyRecurs('Too many recurs at line %d, column %d' % (newlines, position - last_newline_at))
elif mark0 == '.}' and quote_level == 0:
# Macros are executed inner to outer, so when we find the first CLOSE,
# we reverse parse the content and find the first OPEN,
# and collect parameters, then execute & replace the macro
# Also note, many things have inverted logic here :)
while position >= 0:
char1 = text[position]
mark1 = text[position:position + 2]
if char1 == '\n':
newlines -= 1
last_newline_at = position
if mark1 == '{.' and quote_level == 0:
macro_level -= 1
full_macro = list(reversed(full_macro))
full_macro[0] = full_macro[0][1:]
full_macro[-1] = full_macro[-1][0:-1]
result = MacroResult('')
if not (broken or disconnect):
result = self.exec_macro(full_macro, param)
if result.do_break:
broken = True
if result.disconnect:
disconnect = True
return MacroResult('', do_break=True, disconnect=True, headers=headers)
for i in result.headers:
headers[i] = result.headers[i]
macro_str = '{.' + '|'.join(full_macro) + '.}'
text = replace_str(text, macro_str, result.content)
text = self.parse_symbols(text, param, param.symbols, self.symbols)
length = len(text)
full_macro = ['']
break
elif mark1 == '{:':
quote_level -= 1
elif mark1 == ':}':
quote_level += 1
if char1 == '|' and quote_level == 0:
full_macro.append('')
else:
full_macro[-1] = char1 + full_macro[-1]
if position == 0:
raise MacroNotClosedProperly('Macro not closed properly at line %d, column %d' % (newlines, last_macro_at - last_newline_at))
position -= 1
continue
elif mark0 == '{:':
quote_level += 1
elif mark0 == ':}':
quote_level -= 1
position += 1
if macro_level != 0:
raise MacroNotClosedProperly('Macro not closed properly at level %i' % macro_level)
# text = self.unquote(text, param, False).content
return MacroResult(text, do_break=broken, disconnect=disconnect, headers=headers)
def unquote(self, text: str, param: UniParam, do_parse=True) -> MacroResult:
quote_level = 0
position = 0
length = len(text)
while position < length:
mark0 = text[position:position + 2]
if mark0 == '{:':
quote_level += 1
if quote_level == 1:
text = replace_str_at_position(text, position, '{:', '')
length -= 2
continue
elif mark0 == ':}':
quote_level -= 1
if quote_level == 0:
text = replace_str_at_position(text, position, ':}', '')
length -= 2
continue
position += 1
# text = text.replace('{:', '').replace(':}', '')
# Deep copy param, prevent modifying original one
uni_param = UniParam(param.params, interpreter=param.interpreter, request=param.request, filelist=param.filelist, statistics=param.statistics)
return self.parse_text(text, uni_param) if do_parse else MacroResult(text)
def is_quoted(self, text: str) -> bool:
return text[0:2] == '{:' and text[-2:] == ':}'
shortcuts = {
'!': 'translation',
'$': 'section',
'?': 'urlvar',
'^': 'call'
}
operation_signs = ('<>', '!=', '<=', '>=', '<', '>', '=')
def to_normal_macro(self, params: list):
params = [x for x in params] # deep copy
params[0] = params[0].strip()
if params[0] == '':
return params
if params[0] not in self.operation_signs:
for i in self.operation_signs:
if i in params[0] and i != params[0]:
p = params[0].split(i)
params = [i, p[0].strip(), p[1].strip()]
break
if params[0][0] in self.shortcuts and params[0] != '!=':
params.append(params[0][1:])
params[0] = self.shortcuts[params[0][0]]
return params
def exec_macro(self, params: list, param: UniParam) -> MacroResult:
params = self.to_normal_macro(params)
if params[-1].split('/')[-1] == params[0] and len(params) > 2:
params[-1] = '/'.join(params[-1].split('/')[0:-1])
# params = [strip_starting_spaces(x) for x in params]
param.params = params
result = self.handler[params[0]](param)
return result