func.py 44 KB

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