func.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. import os
  2. import sys
  3. import platform
  4. for i in range(0, 2):
  5. try:
  6. import werkzeug.routing
  7. import werkzeug.debug
  8. import flask_compress
  9. import flask_reggie
  10. import tornado.ioloop
  11. import tornado.httpserver
  12. import tornado.wsgi
  13. import urllib.request
  14. import email.mime.text
  15. import sqlite3
  16. import pymysql
  17. import hashlib
  18. import smtplib
  19. import bcrypt
  20. import zipfile
  21. import difflib
  22. import shutil
  23. import threading
  24. import logging
  25. import random
  26. import flask
  27. import json
  28. import html
  29. import re
  30. if sys.version_info < (3, 6):
  31. import sha3
  32. from .mark import *
  33. except ImportError as e:
  34. if i == 0:
  35. print(e)
  36. print('----')
  37. if platform.system() == 'Linux':
  38. ok = os.system('python3 -m pip install --user -r requirements.txt')
  39. if ok == 0:
  40. print('----')
  41. os.execl(sys.executable, sys.executable, *sys.argv)
  42. else:
  43. raise
  44. elif platform.system() == 'Windows':
  45. ok = os.system('python -m pip install --user -r requirements.txt')
  46. if ok == 0:
  47. print('----')
  48. os.execl(sys.executable, sys.executable, *sys.argv)
  49. else:
  50. raise
  51. else:
  52. print('----')
  53. print(e)
  54. raise
  55. else:
  56. print('----')
  57. print(e)
  58. raise
  59. app_var = json.loads(open('data/app_var.json', encoding='utf-8').read())
  60. def load_conn(data):
  61. global conn
  62. global curs
  63. conn = data
  64. curs = conn.cursor()
  65. load_conn2(data)
  66. def send_email(who, title, data):
  67. smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  68. try:
  69. curs.execute(db_change('select name, data from other where name = "g_email" or name = "g_pass"'))
  70. rep_data = curs.fetchall()
  71. if rep_data:
  72. g_email = ''
  73. g_pass = ''
  74. for i in rep_data:
  75. if i[0] == 'g_email':
  76. g_email = i[1]
  77. else:
  78. g_pass = i[1]
  79. smtp.login(g_email, g_pass)
  80. msg = email.mime.text.MIMEText(data)
  81. msg['Subject'] = title
  82. smtp.sendmail(g_email, who, msg.as_string())
  83. smtp.quit()
  84. except:
  85. print('----')
  86. print('Error : Email send error')
  87. def last_change(data):
  88. json_address = re.sub("(((?!\.|\/).)+)\.html$", "set.json", skin_check())
  89. try:
  90. json_data = json.loads(open(json_address).read())
  91. except:
  92. json_data = 0
  93. if json_data != 0:
  94. for j_data in json_data:
  95. if "class" in json_data[j_data]:
  96. if "require" in json_data[j_data]:
  97. re_data = re.compile("<((?:" + j_data + ")( (?:(?!>).)*)?)>")
  98. s_data = re_data.findall(data)
  99. for i_data in s_data:
  100. e_data = 0
  101. for j_i_data in json_data[j_data]["require"]:
  102. re_data_2 = re.compile("( |^)" + j_i_data + " *= *[\'\"]" + json_data[j_data]["require"][j_i_data] + "[\'\"]")
  103. if not re_data_2.search(i_data[1]):
  104. re_data_2 = re.compile("( |^)" + j_i_data + "=" + json_data[j_data]["require"][j_i_data] + "(?: |$)")
  105. if not re_data_2.search(i_data[1]):
  106. e_data = 1
  107. break
  108. if e_data == 0:
  109. re_data_3 = re.compile("<" + i_data[0] + ">")
  110. data = re_data_3.sub("<" + i_data[0] + " class=\"" + json_data[j_data]["class"] + "\">", data)
  111. else:
  112. re_data = re.compile("<(?P<in>" + j_data + "(?: (?:(?!>).)*)?)>")
  113. data = re_data.sub("<\g<in> class=\"" + json_data[j_data]["class"] + "\">", data)
  114. return data
  115. def easy_minify(data, tool = None):
  116. return last_change(data)
  117. def render_set(title = '', data = '', num = 0, s_data = 0, include = None):
  118. if acl_check(title, 'render') == 1:
  119. return 'HTTP Request 401.3'
  120. elif s_data == 1:
  121. return data
  122. else:
  123. if data != None:
  124. return render_do(title, data, num, include)
  125. else:
  126. return 'HTTP Request 404'
  127. def captcha_get():
  128. data = ''
  129. if ip_or_user() != 0:
  130. curs.execute(db_change('select data from other where name = "recaptcha"'))
  131. recaptcha = curs.fetchall()
  132. if recaptcha and recaptcha[0][0] != '':
  133. curs.execute(db_change('select data from other where name = "sec_re"'))
  134. sec_re = curs.fetchall()
  135. if sec_re and sec_re[0][0] != '':
  136. data += '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + recaptcha[0][0] + '<hr class=\"main_hr\">'
  137. return data
  138. def update():
  139. #v3.1.5
  140. try:
  141. num = 1
  142. curs.execute(db_change('select title, sub from topic where id = "1" order by date asc'))
  143. db_data = curs.fetchall()
  144. for i in db_data:
  145. curs.execute(db_change("update topic set code = ? where title = ? and sub = ? and id = '1'"), [str(num), i[0], i[1]])
  146. num += 1
  147. print('----')
  148. print('Add topic code')
  149. print('----')
  150. except:
  151. pass
  152. conn.commit()
  153. def topic_change(num):
  154. curs.execute(db_change('select title, sub from topic where id = "1" and code = ?'), [str(num)])
  155. db_data = curs.fetchall()
  156. if db_data:
  157. return [db_data[0][0], db_data[0][1]]
  158. else:
  159. return ['Test', 'Test']
  160. def pw_encode(data, data2 = '', type_d = ''):
  161. if type_d == '':
  162. curs.execute(db_change('select data from other where name = "encode"'))
  163. set_data = curs.fetchall()
  164. type_d = set_data[0][0]
  165. if type_d == 'sha256':
  166. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  167. elif type_d == 'sha3':
  168. if sys.version_info < (3, 6):
  169. return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest()
  170. else:
  171. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  172. else:
  173. if data2 != '':
  174. salt_data = bytes(data2, 'utf-8')
  175. else:
  176. salt_data = bcrypt.gensalt(11)
  177. return bcrypt.hashpw(bytes(data, 'utf-8'), salt_data).decode()
  178. def pw_check(data, data2, type_d = 'no', id_d = ''):
  179. curs.execute(db_change('select data from other where name = "encode"'))
  180. db_data = curs.fetchall()
  181. if type_d != 'no':
  182. if type_d == '':
  183. set_data = 'bcrypt'
  184. else:
  185. set_data = type_d
  186. else:
  187. set_data = db_data[0][0]
  188. while 1:
  189. if set_data in ['sha256', 'sha3']:
  190. data3 = pw_encode(data = data, type_d = set_data)
  191. if data3 == data2:
  192. re_data = 1
  193. else:
  194. re_data = 0
  195. break
  196. else:
  197. try:
  198. if pw_encode(data, data2, 'bcrypt') == data2:
  199. re_data = 1
  200. else:
  201. re_data = 0
  202. break
  203. except:
  204. set_data = db_data[0][0]
  205. if db_data[0][0] != set_data and re_data == 1 and id_d != '':
  206. curs.execute(db_change("update user set pw = ?, encode = ? where id = ?"), [pw_encode(data), db_data[0][0], id_d])
  207. return re_data
  208. def captcha_post(re_data, num = 1):
  209. if num == 1:
  210. if ip_or_user() != 0 and captcha_get() != '':
  211. curs.execute(db_change('select data from other where name = "sec_re"'))
  212. sec_re = curs.fetchall()
  213. if sec_re and sec_re[0][0] != '':
  214. try:
  215. data = urllib.request.urlopen('https://www.google.com/recaptcha/api/siteverify?secret=' + sec_re[0][0] + '&response=' + re_data)
  216. except:
  217. pass
  218. if data and data.getcode() == 200:
  219. json_data = json.loads(data.read().decode(data.headers.get_content_charset()))
  220. if json_data['success'] == True:
  221. return 0
  222. else:
  223. return 1
  224. else:
  225. return 0
  226. else:
  227. return 0
  228. else:
  229. return 0
  230. else:
  231. pass
  232. def load_lang(data, num = 2, safe = 0):
  233. if num == 1:
  234. curs.execute(db_change("select data from other where name = 'language'"))
  235. rep_data = curs.fetchall()
  236. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), 'rt', encoding='utf-8').read()
  237. lang = json.loads(json_data)
  238. if data in lang:
  239. if safe == 1:
  240. return lang[data]
  241. else:
  242. return html.escape(lang[data])
  243. else:
  244. return html.escape(data + ' (M)')
  245. else:
  246. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip_check()])
  247. rep_data = curs.fetchall()
  248. if rep_data:
  249. try:
  250. json_data = open(os.path.join('language', rep_data[0][0] + '.json'), 'rt', encoding='utf-8').read()
  251. lang = json.loads(json_data)
  252. except:
  253. return load_lang(data, 1, safe)
  254. if data in lang:
  255. if safe == 1:
  256. return lang[data]
  257. else:
  258. return html.escape(lang[data])
  259. else:
  260. return load_lang(data, 1, safe)
  261. else:
  262. return load_lang(data, 1, safe)
  263. def load_oauth(provider):
  264. if(provider == '_README'):
  265. return { "support" : ["discord", "facebook", "naver", "kakao"] }
  266. else:
  267. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf-8').read())
  268. return oauth[provider]
  269. def update_oauth(provider, target, content):
  270. oauth = json.loads(open(app_var['path_oauth_setting'], encoding='utf-8').read())
  271. oauth[provider][target] = content
  272. with open(app_var['path_oauth_setting'], 'w') as f:
  273. f.write(json.dumps(oauth, sort_keys=True, indent=4))
  274. return 'Done'
  275. def ip_or_user(data = ''):
  276. if data == '':
  277. data = ip_check()
  278. if re.search('(\.|:)', data):
  279. return 1
  280. else:
  281. return 0
  282. def edit_button():
  283. insert_list = []
  284. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  285. db_data = curs.fetchall()
  286. for get_data in db_data:
  287. insert_list += [[get_data[1], get_data[0]]]
  288. data = ''
  289. for insert_data in insert_list:
  290. data += '<a href="javascript:do_insert_data(\'content\', \'' + insert_data[0] + '\')">(' + insert_data[1] + ')</a> '
  291. if admin_check() == 1:
  292. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  293. return data + '<hr class=\"main_hr\">'
  294. def ip_warring():
  295. if ip_or_user() != 0:
  296. curs.execute(db_change('select data from other where name = "no_login_warring"'))
  297. data = curs.fetchall()
  298. if data and data[0][0] != '':
  299. text_data = '<span>' + data[0][0] + '</span><hr class=\"main_hr\">'
  300. else:
  301. text_data = '<span>' + load_lang('no_login_warring') + '</span><hr class=\"main_hr\">'
  302. else:
  303. text_data = ''
  304. return text_data
  305. def skin_check(set_n = 0):
  306. skin = 'marisa'
  307. curs.execute(db_change('select data from other where name = "skin"'))
  308. skin_exist = curs.fetchall()
  309. if skin_exist and skin_exist[0][0] != '':
  310. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  311. skin = skin_exist[0][0]
  312. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip_check()])
  313. skin_exist = curs.fetchall()
  314. if skin_exist and skin_exist[0][0] != '':
  315. if os.path.exists(os.path.abspath('./views/' + skin_exist[0][0] + '/index.html')) == 1:
  316. skin = skin_exist[0][0]
  317. if set_n == 0:
  318. return './views/' + skin + '/index.html'
  319. else:
  320. return skin
  321. def next_fix(link, num, page, end = 50):
  322. list_data = ''
  323. if num == 1:
  324. if len(page) == end:
  325. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  326. elif len(page) != end:
  327. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>'
  328. else:
  329. list_data += '<hr class=\"main_hr\"><a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>'
  330. return list_data
  331. def other2(data):
  332. for _ in range(0, 3 - len(data)):
  333. data += ['']
  334. req_list = ''
  335. main_css_ver = 6
  336. if not 'main_css_load' in flask.session or not 'main_css_ver' in flask.session or flask.session['main_css_ver'] != main_css_ver:
  337. for i_data in os.listdir(os.path.join("views", "main_css", "css")):
  338. file_date = str(int(os.path.getmtime(os.path.join("views", "main_css", "css", i_data))))
  339. req_list += '<link rel="stylesheet" href="/views/main_css/css/' + i_data + '?ver=' + file_date + '">'
  340. for i_data in os.listdir(os.path.join("views", "main_css", "js")):
  341. file_date = str(int(os.path.getmtime(os.path.join("views", "main_css", "js", i_data))))
  342. req_list += '<script src="/views/main_css/js/' + i_data + '?ver=' + file_date + '"></script>'
  343. flask.session['main_css_load'] = req_list
  344. flask.session['main_css_ver'] = main_css_ver
  345. else:
  346. req_list = flask.session['main_css_load']
  347. data = data[0:2] + ['', '''
  348. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
  349. <link rel="stylesheet"
  350. href="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css"
  351. integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ"
  352. crossorigin="anonymous">
  353. <script src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js"
  354. integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij"
  355. crossorigin="anonymous"></script>
  356. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  357. ''' + req_list] + data[2:]
  358. return data
  359. def cut_100(data):
  360. data = re.sub('<(((?!>).)*)>', ' ', data)
  361. data = re.sub('\n', ' ', data)
  362. data = re.sub('^ +', '', data)
  363. data = re.sub(' +$', '', data)
  364. data = re.sub(' {2,}', ' ', data)
  365. return data[0:100] + '...'
  366. def wiki_set(num = 1):
  367. if num == 1:
  368. data_list = []
  369. curs.execute(db_change('select data from other where name = ?'), ['name'])
  370. db_data = curs.fetchall()
  371. if db_data and db_data[0][0] != '':
  372. data_list += [db_data[0][0]]
  373. else:
  374. data_list += ['Wiki']
  375. curs.execute(db_change('select data from other where name = "license"'))
  376. db_data = curs.fetchall()
  377. if db_data and db_data[0][0] != '':
  378. data_list += [db_data[0][0]]
  379. else:
  380. data_list += ['CC 0']
  381. data_list += ['', '']
  382. curs.execute(db_change('select data from other where name = "logo"'))
  383. db_data = curs.fetchall()
  384. if db_data and db_data[0][0] != '':
  385. data_list += [db_data[0][0]]
  386. else:
  387. data_list += [data_list[0]]
  388. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_check(1)])
  389. db_data = curs.fetchall()
  390. if db_data and db_data[0][0] != '':
  391. if len(re.findall('<', db_data[0][0])) % 2 != 1:
  392. data_list += [db_data[0][0]]
  393. else:
  394. data_list += ['']
  395. else:
  396. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  397. db_data = curs.fetchall()
  398. if db_data and db_data[0][0] != '':
  399. if len(re.findall('<', db_data[0][0])) % 2 != 1:
  400. data_list += [db_data[0][0]]
  401. else:
  402. data_list += ['']
  403. else:
  404. data_list += ['']
  405. return data_list
  406. if num == 2:
  407. var_data = 'FrontPage'
  408. curs.execute(db_change('select data from other where name = "frontpage"'))
  409. elif num == 3:
  410. var_data = '2'
  411. curs.execute(db_change('select data from other where name = "upload"'))
  412. db_data = curs.fetchall()
  413. if db_data and db_data[0][0] != '':
  414. return db_data[0][0]
  415. else:
  416. return var_data
  417. def diff(seqm):
  418. output = []
  419. for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
  420. if opcode == 'equal':
  421. output += [html.escape(seqm.a[a0:a1])]
  422. elif opcode == 'insert':
  423. output += ["<span style='background:#CFC;'>" + html.escape(seqm.b[b0:b1]) + "</span>"]
  424. elif opcode == 'delete':
  425. output += ["<span style='background:#FDD;'>" + html.escape(seqm.a[a0:a1]) + "</span>"]
  426. elif opcode == 'replace':
  427. output += ["<span style='background:#FDD;'>" + html.escape(seqm.a[a0:a1]) + "</span>"]
  428. output += ["<span style='background:#CFC;'>" + html.escape(seqm.b[b0:b1]) + "</span>"]
  429. end = ''.join(output)
  430. end = end.replace('\r\n', '\n')
  431. sub = ''
  432. if not re.search('\n$', end):
  433. end += '\n'
  434. num = 0
  435. left = 1
  436. while 1:
  437. data = re.search('((?:(?!\n).)*)\n', end)
  438. if data:
  439. data = data.groups()[0]
  440. left += 1
  441. if re.search('<span style=\'(?:(?:(?!\').)+)\'>', data):
  442. num += 1
  443. if re.search('<\/span>', data):
  444. num -= 1
  445. sub += str(left) + ' : ' + re.sub('(?P<in>(?:(?!\n).)*)\n', '\g<in>', data, 1) + '<br>'
  446. else:
  447. if re.search('<\/span>', data):
  448. num -= 1
  449. sub += str(left) + ' : ' + re.sub('(?P<in>(?:(?!\n).)*)\n', '\g<in>', data, 1) + '<br>'
  450. else:
  451. if num > 0:
  452. sub += str(left) + ' : ' + re.sub('(?P<in>.*)\n', '\g<in>', data, 1) + '<br>'
  453. end = re.sub('((?:(?!\n).)*)\n', '', end, 1)
  454. else:
  455. break
  456. return sub
  457. def admin_check(num = None, what = None, name = ''):
  458. if name == '':
  459. ip = ip_check()
  460. else:
  461. ip = name
  462. curs.execute(db_change("select acl from user where id = ?"), [ip])
  463. user = curs.fetchall()
  464. if user:
  465. reset = 0
  466. back_num = num
  467. while 1:
  468. if num == 1:
  469. check = 'ban'
  470. elif num == 2:
  471. check = 'nothing'
  472. elif num == 3:
  473. check = 'toron'
  474. elif num == 4:
  475. check = 'check'
  476. elif num == 5:
  477. check = 'acl'
  478. elif num == 6:
  479. check = 'hidel'
  480. elif num == 7:
  481. check = 'give'
  482. else:
  483. check = 'owner'
  484. curs.execute(db_change('select name from alist where name = ? and acl = ?'), [user[0][0], check])
  485. if curs.fetchall():
  486. if what:
  487. curs.execute(db_change("insert into re_admin (who, what, time) values (?, ?, ?)"), [ip, what, get_time()])
  488. conn.commit()
  489. return 1
  490. else:
  491. if back_num == 'all':
  492. if num == 'all':
  493. num = 1
  494. elif num != 8:
  495. num += 1
  496. else:
  497. break
  498. elif num:
  499. num = None
  500. else:
  501. break
  502. return 0
  503. def ip_pas(raw_ip, type_d = 0):
  504. hide = 0
  505. if ip_or_user(raw_ip) != 0:
  506. curs.execute(db_change("select data from other where name = 'ip_view'"))
  507. data = curs.fetchall()
  508. if data and data[0][0] != '':
  509. if re.search('\.', raw_ip):
  510. ip = re.sub('\.([^.]*)\.([^.]*)$', '.*.*', raw_ip)
  511. else:
  512. ip = re.sub(':([^:]*):([^:]*)$', ':*:*', raw_ip)
  513. if not admin_check(1):
  514. hide = 1
  515. else:
  516. ip = raw_ip
  517. else:
  518. if type_d == 0:
  519. curs.execute(db_change("select title from data where title = ?"), ['user:' + raw_ip])
  520. if curs.fetchall():
  521. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  522. else:
  523. ip = '<a id="not_thing" href="/w/' + url_pas('user:' + raw_ip) + '">' + raw_ip + '</a>'
  524. if admin_check('all', None, raw_ip) == 1:
  525. ip = '<b>' + ip + '</b>'
  526. else:
  527. ip = raw_ip
  528. if type_d == 0:
  529. if ban_check(raw_ip) == 1:
  530. ip = '<s>' + ip + '</s>'
  531. if hide == 0:
  532. ip += ' <a href="/tool/' + url_pas(raw_ip) + '">(' + load_lang('tool') + ')</a>'
  533. return ip
  534. def custom():
  535. if 'head' in flask.session:
  536. if len(re.findall('<', flask.session['head'])) % 2 != 1:
  537. user_head = flask.session['head']
  538. else:
  539. user_head = ''
  540. else:
  541. user_head = ''
  542. ip = ip_check()
  543. if ip_or_user(ip) == 0:
  544. user_icon = 1
  545. user_name = ip
  546. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  547. data = curs.fetchall()
  548. if data:
  549. email = data[0][0]
  550. else:
  551. email = ''
  552. if admin_check('all') == 1:
  553. user_admin = '1'
  554. user_acl_list = []
  555. curs.execute(db_change("select acl from user where id = ?"), [ip])
  556. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  557. user_acl = curs.fetchall()
  558. for i in user_acl:
  559. user_acl_list += [i[0]]
  560. if user_acl != []:
  561. user_acl_list = user_acl_list
  562. else:
  563. user_acl_list = '0'
  564. else:
  565. user_admin = '0'
  566. user_acl_list = '0'
  567. curs.execute(db_change("select count(name) from alarm where name = ?"), [ip])
  568. count = curs.fetchall()
  569. if count:
  570. user_notice = str(count[0][0])
  571. else:
  572. user_notice = '0'
  573. else:
  574. user_icon = 0
  575. user_name = load_lang('user')
  576. email = ''
  577. user_admin = '0'
  578. user_acl_list = '0'
  579. user_notice = '0'
  580. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  581. if curs.fetchall():
  582. user_topic = '1'
  583. else:
  584. user_topic = '0'
  585. return [
  586. '',
  587. '',
  588. user_icon,
  589. user_head,
  590. email,
  591. user_name,
  592. user_admin,
  593. str(ban_check()),
  594. user_notice,
  595. user_acl_list,
  596. ip,
  597. user_topic
  598. ]
  599. def load_skin(data = '', set_n = 0):
  600. skin_return_data = ''
  601. system_file = ['main_css']
  602. if data == '':
  603. ip = ip_check()
  604. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  605. data = curs.fetchall()
  606. if not data:
  607. curs.execute(db_change('select data from other where name = "skin"'))
  608. data = curs.fetchall()
  609. if not data or data[0][0] == '':
  610. data = [['marisa']]
  611. if set_n == 0:
  612. for skin_data in os.listdir(os.path.abspath('views')):
  613. if not skin_data in system_file:
  614. if data[0][0] == skin_data:
  615. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  616. else:
  617. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  618. else:
  619. skin_return_data = []
  620. for skin_data in os.listdir(os.path.abspath('views')):
  621. if not skin_data in system_file:
  622. if data[0][0] == skin_data:
  623. skin_return_data = [skin_data] + skin_return_data
  624. else:
  625. skin_return_data += [skin_data]
  626. else:
  627. if set_n == 0:
  628. for skin_data in os.listdir(os.path.abspath('views')):
  629. if not skin_data in system_file:
  630. if data == skin_data:
  631. skin_return_data = '<option value="' + skin_data + '">' + skin_data + '</option>' + skin_return_data
  632. else:
  633. skin_return_data += '<option value="' + skin_data + '">' + skin_data + '</option>'
  634. else:
  635. skin_return_data = []
  636. for skin_data in os.listdir(os.path.abspath('views')):
  637. if not skin_data in system_file:
  638. if data == skin_data:
  639. skin_return_data = [skin_data] + skin_return_data
  640. else:
  641. skin_return_data += [skin_data]
  642. return skin_return_data
  643. def slow_edit_check():
  644. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  645. slow_edit = curs.fetchall()
  646. if slow_edit and slow_edit != '0' and admin_check(5) != 1:
  647. slow_edit = slow_edit[0][0]
  648. curs.execute(db_change("select date from history where ip = ? order by date desc limit 1"), [ip_check()])
  649. last_edit_data = curs.fetchall()
  650. if last_edit_data:
  651. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  652. now_edit_data = int((datetime.datetime.now() - datetime.timedelta(seconds = int(slow_edit))).strftime("%Y%m%d%H%M%S"))
  653. if last_edit_data > now_edit_data:
  654. return 1
  655. return 0
  656. def acl_check(name = 'test', tool = '', sub = 'test'):
  657. ip = ip_check()
  658. if ban_check() == 1:
  659. return 1
  660. if tool != 'topic' and tool != 'render' and name:
  661. acl_c = re.search("^user:((?:(?!\/).)*)", name)
  662. if acl_c:
  663. acl_n = acl_c.groups()
  664. if admin_check(5) == 1:
  665. return 0
  666. curs.execute(db_change("select decu from acl where title = ?"), ['user:' + acl_n[0]])
  667. acl_data = curs.fetchall()
  668. if acl_data:
  669. if acl_data[0][0] == 'all':
  670. return 0
  671. if acl_data[0][0] == 'user' and not re.search("(\.|:)", ip):
  672. return 0
  673. if ip != acl_n[0] or re.search("(\.|:)", ip):
  674. return 1
  675. if ip == acl_n[0] and not re.search("(\.|:)", ip) and not re.search("(\.|:)", acl_n[0]):
  676. return 0
  677. else:
  678. return 1
  679. if re.search("^file:", name) and admin_check(None, 'file edit (' + name + ')') != 1:
  680. return 1
  681. if tool == '':
  682. end = 3
  683. elif tool == 'topic' or tool == 'render':
  684. end = 2
  685. else:
  686. end = 1
  687. for i in range(0, end):
  688. if tool == '':
  689. if i == 0:
  690. curs.execute(db_change("select decu from acl where title = ?"), [name])
  691. elif i == 1:
  692. curs.execute(db_change('select data from other where name = "edit"'))
  693. else:
  694. curs.execute(db_change("select view from acl where title = ?"), [name])
  695. num = 5
  696. elif tool == 'topic':
  697. if i == 0:
  698. curs.execute(db_change("select dis from acl where title = ?"), [name])
  699. else:
  700. curs.execute(db_change('select data from other where name = "discussion"'))
  701. num = 3
  702. elif tool == 'upload':
  703. curs.execute(db_change("select data from other where name = 'upload_acl'"))
  704. num = 5
  705. elif tool == 'edit_req':
  706. curs.execute(db_change("select data from other where name = 'edit_req_acl'"))
  707. num = 5
  708. else:
  709. if i == 0:
  710. curs.execute(db_change("select view from acl where title = ?"), [name])
  711. if i == 1:
  712. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  713. num = 5
  714. acl_data = curs.fetchall()
  715. if acl_data and acl_data[0][0] != 'normal':
  716. if acl_data[0][0] == 'user':
  717. if ip_or_user(ip) == 1:
  718. return 1
  719. if acl_data[0][0] == 'admin':
  720. if ip_or_user(ip) == 1:
  721. return 1
  722. if admin_check(num) != 1:
  723. return 1
  724. if acl_data[0][0] == '50_edit':
  725. if ip_or_user(ip) == 1:
  726. return 1
  727. if admin_check(num) != 1:
  728. curs.execute(db_change("select count(title) from history where ip = ?"), [ip])
  729. count = curs.fetchall()
  730. if count:
  731. count = count[0][0]
  732. else:
  733. count = 0
  734. if count < 50:
  735. return 1
  736. if acl_data[0][0] == 'email':
  737. if ip_or_user(ip) == 1:
  738. return 1
  739. if admin_check(num) != 1:
  740. curs.execute(db_change("select data from user_set where id = ? and name = 'email'"), [ip])
  741. email = curs.fetchall()
  742. if not email:
  743. return 1
  744. if acl_data[0][0] == 'owner':
  745. if admin_check() != 1:
  746. return 1
  747. if tool == 'topic':
  748. curs.execute(db_change("select title from rd where title = ? and sub = ? and not stop = ''"), [name, sub])
  749. if curs.fetchall():
  750. if admin_check(3, 'topic (' + name + ')') != 1:
  751. return 1
  752. return 0
  753. def ban_check(ip = None, tool = None):
  754. if not ip:
  755. ip = ip_check()
  756. if admin_check(None, None, ip) == 1:
  757. return 0
  758. band = re.search("^([0-9]{1,3}\.[0-9]{1,3})", ip)
  759. if band:
  760. band_it = band.groups()[0]
  761. else:
  762. band_it = '-'
  763. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  764. conn.commit()
  765. curs.execute(db_change("select login, block from ban where ((end > ? and end like '2%') or end = '') and band = 'regex'"), [get_time()])
  766. regex_d = curs.fetchall()
  767. for test_r in regex_d:
  768. g_regex = re.compile(test_r[1])
  769. if g_regex.search(ip):
  770. if tool and tool == 'login':
  771. if test_r[0] != 'O':
  772. return 1
  773. else:
  774. return 1
  775. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = 'O'"), [get_time(), band_it])
  776. band_d = curs.fetchall()
  777. if band_d:
  778. if tool and tool == 'login':
  779. if band_d[0][0] != 'O':
  780. return 1
  781. else:
  782. return 1
  783. curs.execute(db_change("select login from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ''"), [get_time(), ip])
  784. ban_d = curs.fetchall()
  785. if ban_d:
  786. if tool and tool == 'login':
  787. if ban_d[0][0] != 'O':
  788. return 1
  789. else:
  790. return 1
  791. return 0
  792. def ban_insert(name, end, why, login, blocker, type_d = None):
  793. now_time = get_time()
  794. if type_d:
  795. band = type_d
  796. else:
  797. if re.search("^([0-9]{1,3}\.[0-9]{1,3})$", name):
  798. band = 'O'
  799. else:
  800. band = ''
  801. curs.execute(db_change("delete from ban where (end < ? and end like '2%')"), [get_time()])
  802. curs.execute(db_change("select block from ban where ((end > ? and end like '2%') or end = '') and block = ? and band = ?"), [get_time(), name, band])
  803. if curs.fetchall():
  804. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [
  805. name,
  806. 'release',
  807. now_time,
  808. blocker,
  809. '',
  810. band
  811. ])
  812. curs.execute(db_change("delete from ban where block = ? and band = ?"), [name, band])
  813. else:
  814. if login != '':
  815. login = 'O'
  816. else:
  817. login = ''
  818. if end != '0':
  819. end = int(number_check(end))
  820. time = datetime.datetime.now()
  821. plus = datetime.timedelta(seconds = end)
  822. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  823. else:
  824. r_time = ''
  825. curs.execute(db_change("insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"), [name, r_time, now_time, blocker, why, band])
  826. curs.execute(db_change("insert into ban (block, end, why, band, login) values (?, ?, ?, ?, ?)"), [name, r_time, why, band, login])
  827. conn.commit()
  828. def rd_plus(title, sub, date):
  829. curs.execute(db_change("select title from rd where title = ? and sub = ?"), [title, sub])
  830. if curs.fetchall():
  831. curs.execute(db_change("update rd set date = ? where title = ? and sub = ?"), [date, title, sub])
  832. else:
  833. curs.execute(db_change("insert into rd (title, sub, date) values (?, ?, ?)"), [title, sub, date])
  834. conn.commit()
  835. def history_plus(title, data, date, ip, send, leng, t_check = '', d_type = ''):
  836. curs.execute(db_change("select id from history where title = ? and type = '' order by id + 0 desc limit 1"), [title])
  837. id_data = curs.fetchall()
  838. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  839. if d_type != 'req':
  840. curs.execute(db_change("select title from history where title = ? and id = ? and type = 'req'"), [title, id_data])
  841. if curs.fetchall():
  842. curs.execute(db_change("update history set type = 'req_close' where title = ? and id = ? and type = 'req'"), [
  843. title,
  844. id_data
  845. ])
  846. send = re.sub('\(|\)|<|>', '', send)
  847. if len(send) > 128:
  848. send = send[:128]
  849. if t_check != '':
  850. send += ' (' + t_check + ')'
  851. curs.execute(db_change("insert into history (id, title, data, date, ip, send, leng, hide, type) values (?, ?, ?, ?, ?, ?, ?, '', ?)"), [
  852. id_data,
  853. title,
  854. data,
  855. date,
  856. ip,
  857. send,
  858. leng,
  859. d_type
  860. ])
  861. def leng_check(first, second):
  862. if first < second:
  863. all_plus = '+' + str(second - first)
  864. elif second < first:
  865. all_plus = '-' + str(first - second)
  866. else:
  867. all_plus = '0'
  868. return all_plus
  869. def number_check(data):
  870. try:
  871. int(data)
  872. return data
  873. except:
  874. return '1'
  875. def edit_filter_do(data):
  876. if admin_check(1) != 1:
  877. curs.execute(db_change("select regex, sub from filter where regex != ''"))
  878. for data_list in curs.fetchall():
  879. match = re.compile(data_list[0], re.I)
  880. if match.search(data):
  881. ban_insert(
  882. ip_check(),
  883. '0' if data_list[1] == 'X' else data_list[1],
  884. 'edit filter',
  885. None,
  886. 'tool:edit filter'
  887. )
  888. return 1
  889. return 0
  890. def redirect(data = '/'):
  891. return flask.redirect(data)
  892. def re_error(data):
  893. conn.commit()
  894. if data == '/ban':
  895. if ban_check() == 1:
  896. end = '<div id="get_user_info"></div><script>load_user_info("' + ip_check() + '");</script>'
  897. else:
  898. end = load_lang('authority_error')
  899. return easy_minify(flask.render_template(skin_check(),
  900. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  901. data = '<h2>' + load_lang('error') + '</h2>' + end,
  902. menu = 0
  903. ))
  904. else:
  905. error_data = re.search('\/error\/([0-9]+)', data)
  906. if error_data:
  907. num = int(error_data.groups()[0])
  908. if num == 1:
  909. data = load_lang('no_login_error')
  910. elif num == 2:
  911. data = load_lang('no_exist_user_error')
  912. elif num == 3:
  913. data = load_lang('authority_error')
  914. elif num == 4:
  915. data = load_lang('no_admin_block_error')
  916. elif num == 5:
  917. data = load_lang('skin_error')
  918. elif num == 6:
  919. data = load_lang('same_id_exist_error')
  920. elif num == 7:
  921. data = load_lang('long_id_error')
  922. elif num == 8:
  923. data = load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id') + ' ' + load_lang('filter') + ')</a>'
  924. elif num == 9:
  925. data = load_lang('file_exist_error')
  926. elif num == 10:
  927. data = load_lang('password_error')
  928. elif num == 11:
  929. data = load_lang('topic_long_error')
  930. elif num == 12:
  931. data = load_lang('email_error')
  932. elif num == 13:
  933. data = load_lang('recaptcha_error')
  934. elif num == 14:
  935. data = load_lang('file_extension_error')
  936. elif num == 15:
  937. data = load_lang('edit_record_error')
  938. elif num == 16:
  939. data = load_lang('same_file_error')
  940. elif num == 17:
  941. data = load_lang('file_capacity_error') + wiki_set(3)
  942. elif num == 19:
  943. data = load_lang('decument_exist_error')
  944. elif num == 20:
  945. data = load_lang('password_diffrent_error')
  946. elif num == 21:
  947. data = load_lang('edit_filter_error')
  948. elif num == 22:
  949. data = load_lang('file_name_error')
  950. elif num == 23:
  951. data = load_lang('regex_error')
  952. elif num == 24:
  953. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  954. slow_data = curs.fetchall()
  955. data = load_lang('fast_edit_error') + slow_data[0][0]
  956. elif num == 25:
  957. data = load_lang('too_many_dec_error')
  958. elif num == 26:
  959. data = load_lang('application_not_found')
  960. else:
  961. data = '???'
  962. if num == 5:
  963. return easy_minify(flask.render_template(skin_check(),
  964. imp = [load_lang('skin_set'), wiki_set(1), custom(), other2([0, 0])],
  965. data = '<div id="main_skin_set"><h2>' + load_lang('error') + '</h2><ul><li>' + data + '</li></ul></div>',
  966. menu = 0
  967. ))
  968. else:
  969. return easy_minify(flask.render_template(skin_check(),
  970. imp = [load_lang('error'), wiki_set(1), custom(), other2([0, 0])],
  971. data = '<h2>' + load_lang('error') + '</h2><ul><li>' + data + '</li></ul>',
  972. menu = 0
  973. )), 401
  974. else:
  975. return redirect('/')