func.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. # Init
  2. import os
  3. import sys
  4. import platform
  5. import json
  6. import smtplib
  7. import zipfile
  8. import shutil
  9. import logging
  10. import random
  11. import email.mime.text
  12. import email.utils
  13. import email.header
  14. import urllib.request
  15. # Init-Version
  16. version_list = json.loads(open('version.json', encoding = 'utf8').read())
  17. print('Version : ' + version_list['beta']['r_ver'])
  18. print('DB set version : ' + version_list['beta']['c_ver'])
  19. print('Skin set version : ' + version_list['beta']['s_ver'])
  20. print('----')
  21. # Init-PIP_Install
  22. data_up_date = 1
  23. if os.path.exists(os.path.join('data', 'version.json')):
  24. data_load_ver = open(os.path.join('data', 'version.json'), encoding = 'utf8').read()
  25. if data_load_ver == version_list['beta']['r_ver']:
  26. data_up_date = 0
  27. if data_up_date == 1:
  28. with open(os.path.join('data', 'version.json'), 'w', encoding = 'utf8') as f:
  29. f.write(version_list['beta']['r_ver'])
  30. if platform.system() in ('Linux', 'Windows'):
  31. if platform.python_implementation() == 'PyPy':
  32. os.system(
  33. 'pypy' + ('3' if platform.system() != 'Windows' else '') + ' ' + \
  34. '-m pip install --upgrade --user -r requirements.txt'
  35. )
  36. else:
  37. os.system(
  38. 'python' + ('3' if platform.system() != 'Windows' else '') + ' ' + \
  39. '-m pip install --upgrade --user -r requirements.txt'
  40. )
  41. print('----')
  42. try:
  43. os.execl(sys.executable, sys.executable, *sys.argv)
  44. except:
  45. pass
  46. try:
  47. os.execl(sys.executable, '"' + sys.executable + '"', *sys.argv)
  48. except:
  49. print('Error : restart failed')
  50. raise
  51. else:
  52. print('Error : automatic installation is not supported.')
  53. print('Help : try "python3 -m pip install -r requirements.txt"')
  54. else:
  55. print('PIP check pass')
  56. print('----')
  57. # Init-Load
  58. from .func_mark import *
  59. from diff_match_patch import diff_match_patch
  60. import netius.servers
  61. import waitress
  62. import werkzeug.routing
  63. import werkzeug.debug
  64. import flask
  65. import requests
  66. import pymysql
  67. if sys.version_info < (3, 6):
  68. import sha3
  69. # Init-Global
  70. global_lang = {}
  71. global_wiki_set = {}
  72. global_db_set = ''
  73. data_css_ver = '124'
  74. data_css = ''
  75. conn = ''
  76. # Func
  77. # Func-main
  78. def load_conn(data):
  79. global conn
  80. conn = data
  81. def do_db_set(db_set):
  82. global global_db_set
  83. global_db_set = db_set
  84. # Func-init
  85. class get_db_connect_old:
  86. def __init__(self, db_set):
  87. self.db_set = db_set
  88. self.conn = ''
  89. def db_load(self):
  90. if self.db_set['type'] == 'sqlite':
  91. self.conn = sqlite3.connect(
  92. self.db_set['name'] + '.db',
  93. check_same_thread = False,
  94. isolation_level = None
  95. )
  96. self.conn.execute('pragma journal_mode = wal')
  97. else:
  98. self.conn = pymysql.connect(
  99. host = self.db_set['mysql_host'],
  100. user = self.db_set['mysql_user'],
  101. password = self.db_set['mysql_pw'],
  102. charset = 'utf8mb4',
  103. port = int(self.db_set['mysql_port']),
  104. )
  105. curs = self.conn.cursor()
  106. try:
  107. curs.execute(db_change(
  108. 'create database ' + self.db_set['name'] + ' ' + \
  109. 'default character set utf8mb4;'
  110. ))
  111. except:
  112. pass
  113. self.conn.select_db(self.db_set['name'])
  114. load_conn(self.conn)
  115. return self.conn
  116. def db_get(self):
  117. # if self.db_set['type'] != 'sqlite':
  118. # self.conn.ping(reconnect = True)
  119. return self.conn
  120. class get_db_connect:
  121. # 임시 DB 커넥션 동작 구조
  122. # Init 파트
  123. # DB 커넥트(get_db_connect_old) -> func.py로 conn 넘겨줌
  124. # route 파트
  125. # DB 새로 커넥트 -> func.py에서 쓰던 conn은 conn_sub로 보관 ->
  126. # func.py로 conn 넘겨줌 -> 모든 라우터 과정이 끝나면 conn_sub를 다시 func.py에 conn으로 넘겨줌 ->
  127. # DB 커넥트 종료
  128. def __init__(self):
  129. global global_db_set
  130. global conn
  131. self.conn_sub = conn
  132. self.db_set = global_db_set
  133. def __enter__(self):
  134. if self.db_set['type'] == 'sqlite':
  135. self.conn = sqlite3.connect(
  136. self.db_set['name'] + '.db',
  137. check_same_thread = False,
  138. isolation_level = None
  139. )
  140. self.conn.execute('pragma journal_mode = wal')
  141. else:
  142. self.conn = pymysql.connect(
  143. host = self.db_set['mysql_host'],
  144. user = self.db_set['mysql_user'],
  145. password = self.db_set['mysql_pw'],
  146. charset = 'utf8mb4',
  147. port = int(self.db_set['mysql_port']),
  148. )
  149. curs = self.conn.cursor()
  150. try:
  151. curs.execute(db_change(
  152. 'create database ' + self.db_set['name'] + ' ' + \
  153. 'default character set utf8mb4;'
  154. ))
  155. except:
  156. pass
  157. self.conn.select_db(self.db_set['name'])
  158. load_conn(self.conn)
  159. return self.conn
  160. def __exit__(self, exc_type, exc_value, traceback):
  161. load_conn(self.conn_sub)
  162. self.conn.close()
  163. class class_check_json:
  164. def do_check_set_json():
  165. if os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
  166. set_data = {}
  167. set_data['db'] = os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data'
  168. set_data['db'] = os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
  169. else:
  170. if os.path.exists(os.path.join('data', 'set.json')):
  171. db_set_list = ['db', 'db_type']
  172. set_data = json.loads(open(
  173. os.path.join('data', 'set.json'),
  174. encoding = 'utf8'
  175. ).read())
  176. for i in db_set_list:
  177. if not i in set_data:
  178. os.remove(os.path.join('data', 'set.json'))
  179. break
  180. if not os.path.exists(os.path.join('data', 'set.json')):
  181. set_data = {}
  182. normal_db_type = ['sqlite', 'mysql']
  183. print('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ', end = '')
  184. data_get = str(input())
  185. if data_get == '' or not data_get in normal_db_type:
  186. set_data['db_type'] = 'sqlite'
  187. else:
  188. set_data['db_type'] = data_get
  189. all_src = []
  190. if set_data['db_type'] == 'sqlite':
  191. for i_data in os.listdir("."):
  192. f_src = re.search(r"(.+)\.db$", i_data)
  193. if f_src:
  194. all_src += [f_src.group(1)]
  195. print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
  196. data_get = str(input())
  197. if data_get == '':
  198. set_data['db'] = 'data'
  199. else:
  200. set_data['db'] = data_get
  201. with open(os.path.join('data', 'set.json'), 'w', encoding = 'utf8') as f:
  202. f.write(json.dumps(set_data))
  203. print('DB name : ' + set_data['db'])
  204. print('DB type : ' + set_data['db_type'])
  205. data_db_set = {}
  206. data_db_set['name'] = set_data['db']
  207. data_db_set['type'] = set_data['db_type']
  208. return data_db_set
  209. def do_check_mysql_json(data_db_set):
  210. if os.path.exists(os.path.join('data', 'mysql.json')):
  211. db_set_list = ['user', 'password', 'host', 'port']
  212. set_data = json.loads(
  213. open(
  214. os.path.join('data', 'mysql.json'),
  215. encoding = 'utf8'
  216. ).read()
  217. )
  218. for i in db_set_list:
  219. if not i in set_data:
  220. os.remove(os.path.join('data', 'mysql.json'))
  221. break
  222. set_data_mysql = set_data
  223. if not os.path.exists(os.path.join('data', 'mysql.json')):
  224. set_data_mysql = {}
  225. print('DB user ID : ', end = '')
  226. set_data_mysql['user'] = str(input())
  227. print('DB password : ', end = '')
  228. set_data_mysql['password'] = str(input())
  229. print('DB host (localhost) : ', end = '')
  230. set_data_mysql['host'] = str(input())
  231. if set_data_mysql['host'] == '':
  232. set_data_mysql['host'] = 'localhost'
  233. print('DB port (3306) : ', end = '')
  234. set_data_mysql['port'] = str(input())
  235. if set_data_mysql['port'] == '':
  236. set_data_mysql['port'] = '3306'
  237. with open(
  238. os.path.join('data', 'mysql.json'),
  239. 'w',
  240. encoding = 'utf8'
  241. ) as f:
  242. f.write(json.dumps(set_data_mysql))
  243. data_db_set['mysql_user'] = set_data_mysql['user']
  244. data_db_set['mysql_pw'] = set_data_mysql['password']
  245. if 'host' in set_data_mysql:
  246. data_db_set['mysql_host'] = set_data_mysql['host']
  247. else:
  248. data_db_set['mysql_host'] = 'localhost'
  249. if 'port' in set_data_mysql:
  250. data_db_set['mysql_port'] = set_data_mysql['port']
  251. else:
  252. data_db_set['mysql_port'] = '3306'
  253. return data_db_set
  254. def __init__(self):
  255. self.data_db_set = {}
  256. def __new__(self):
  257. self.data_db_set = self.do_check_set_json()
  258. if self.data_db_set['type'] == 'mysql':
  259. self.data_db_set = self.do_check_mysql_json(self.data_db_set)
  260. return self.data_db_set
  261. def update(ver_num, set_data):
  262. curs = conn.cursor()
  263. print('----')
  264. # 업데이트 하위 호환 유지 함수
  265. if ver_num < 3160027:
  266. print('Add init set')
  267. set_init()
  268. if ver_num < 3170002:
  269. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  270. if not curs.fetchall():
  271. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  272. curs.execute(db_change(
  273. "insert into html_filter (html, kind) values (?, 'extension')"
  274. ), [i])
  275. if ver_num < 3170400:
  276. curs.execute(db_change("select title, sub, code from topic where id = '1'"))
  277. for i in curs.fetchall():
  278. curs.execute(db_change(
  279. "update topic set code = ? where title = ? and sub = ?"
  280. ), [
  281. i[2],
  282. i[0],
  283. i[1]
  284. ])
  285. curs.execute(db_change(
  286. "update rd set code = ? where title = ? and sub = ?"
  287. ), [
  288. i[2],
  289. i[0],
  290. i[1]
  291. ])
  292. if ver_num < 3171800:
  293. curs.execute(db_change("select data from other where name = 'recaptcha'"))
  294. change_rec = curs.fetchall()
  295. if change_rec and change_rec[0][0] != '':
  296. new_rec = re.search(r'data-sitekey="([^"]+)"', change_rec[0][0])
  297. if new_rec:
  298. curs.execute(db_change(
  299. "update other set data = ? where name = 'recaptcha'"
  300. ), [new_rec.group(1)])
  301. else:
  302. curs.execute(db_change("update other set data = '' where name = 'recaptcha'"))
  303. curs.execute(db_change("update other set data = '' where name = 'sec_re'"))
  304. if ver_num < 3172800 and \
  305. set_data['db_type'] == 'mysql':
  306. get_data_mysql = json.loads(open('data/mysql.json', encoding = 'utf8').read())
  307. with open('data/mysql.json', 'w') as f:
  308. f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }')
  309. if ver_num < 3183603:
  310. curs.execute(db_change("select block from ban where band = 'O'"))
  311. for i in curs.fetchall():
  312. curs.execute(db_change(
  313. "update ban set block = ?, band = 'regex' where block = ? and band = 'O'"
  314. ), [
  315. '^' + i[0].replace('.', '\\.'),
  316. i[0]
  317. ])
  318. curs.execute(db_change("select block from rb where band = 'O'"))
  319. for i in curs.fetchall():
  320. curs.execute(db_change(
  321. "update rb set block = ?, band = 'regex' where block = ? and band = 'O'"
  322. ), [
  323. '^' + i[0].replace('.', '\\.'),
  324. i[0]
  325. ])
  326. if ver_num < 3190201:
  327. today_time = get_time()
  328. curs.execute(db_change("select block, end, why, band, login from ban"))
  329. for i in curs.fetchall():
  330. curs.execute(db_change(
  331. "insert into rb (block, end, today, why, band, login, ongoing) " + \
  332. "values (?, ?, ?, ?, ?, ?, ?)"
  333. ), [
  334. i[0],
  335. i[1],
  336. today_time,
  337. i[2],
  338. i[3],
  339. i[4],
  340. '1'
  341. ])
  342. if ver_num < 3191301:
  343. curs.execute(db_change('' + \
  344. 'select id, title, date from history ' + \
  345. 'where not title like "user:%" ' + \
  346. 'order by date desc ' + \
  347. 'limit 50' + \
  348. ''))
  349. data_list = curs.fetchall()
  350. for get_data in data_list:
  351. curs.execute(db_change(
  352. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  353. ), [
  354. get_data[0],
  355. get_data[1],
  356. get_data[2]
  357. ])
  358. if ver_num < 3202400:
  359. curs.execute(db_change("select data from other where name = 'update'"))
  360. get_data = curs.fetchall()
  361. if get_data and get_data[0][0] == 'master':
  362. curs.execute(db_change("update other set data = 'beta' where name = 'update'"), [])
  363. if ver_num < 3202600:
  364. curs.execute(db_change("select name, regex, sub from filter"))
  365. for i in curs.fetchall():
  366. curs.execute(db_change(
  367. "insert into html_filter (html, kind, plus, plus_t) " + \
  368. "values (?, 'regex_filter', ?, ?)"
  369. ), [
  370. i[0],
  371. i[1],
  372. i[2]
  373. ])
  374. curs.execute(db_change("select title, link, icon from inter"))
  375. for i in curs.fetchall():
  376. curs.execute(db_change(
  377. "insert into html_filter (html, kind, plus, plus_t) " + \
  378. "values (?, 'inter_wiki', ?, ?)"), [
  379. i[0],
  380. i[1],
  381. i[2]
  382. ])
  383. if ver_num < 3203400:
  384. curs.execute(db_change("select user, css from custom"))
  385. for i in curs.fetchall():
  386. curs.execute(db_change(
  387. "insert into user_set (name, id, data) values ('custom_css', ?, ?)"
  388. ), [
  389. re.sub(r' \(head\)$', '', i[0]),
  390. i[1]
  391. ])
  392. if ver_num < 3205500:
  393. curs.execute(db_change("select title, decu, dis, view, why from acl"))
  394. for i in curs.fetchall():
  395. curs.execute(db_change(
  396. "insert into acl (title, data, type) values (?, ?, ?)"
  397. ), [i[0], i[1], 'decu'])
  398. curs.execute(db_change(
  399. "insert into acl (title, data, type) values (?, ?, ?)"
  400. ), [i[0], i[2], 'dis'])
  401. curs.execute(db_change(
  402. "insert into acl (title, data, type) values (?, ?, ?)"
  403. ), [i[0], i[3], 'view'])
  404. curs.execute(db_change(
  405. "insert into acl (title, data, type) values (?, ?, ?)"
  406. ), [i[0], i[4], 'why'])
  407. if ver_num < 3300101:
  408. # 캐시 초기화
  409. curs.execute(db_change('delete from cache_data'))
  410. if ver_num < 3300301:
  411. # regex_filter 오류 해결
  412. curs.execute(db_change(
  413. 'delete from html_filter where kind = "regex_filter" and html is null'
  414. ))
  415. if ver_num < 3302302:
  416. # user이랑 user_set 테이블의 통합
  417. curs.execute(db_change('select id, pw, acl, date, encode from user'))
  418. for i in curs.fetchall():
  419. curs.execute(db_change(
  420. "insert into user_set (name, id, data) values (?, ?, ?)"
  421. ), ['pw', i[0], i[1]])
  422. curs.execute(db_change(
  423. "insert into user_set (name, id, data) values (?, ?, ?)"
  424. ), ['acl', i[0], i[2]])
  425. curs.execute(db_change(
  426. "insert into user_set (name, id, data) values (?, ?, ?)"
  427. ), ['date', i[0], i[3]])
  428. curs.execute(db_change(
  429. "insert into user_set (name, id, data) values (?, ?, ?)"
  430. ), ['encode', i[0], i[4]])
  431. if ver_num < 3400101:
  432. # user_set이랑 user_application 테이블의 통합
  433. curs.execute(db_change('' + \
  434. 'select id, pw, date, encode, question, answer, ip, ua, email ' + \
  435. 'from user_application' + \
  436. ''))
  437. for i in curs.fetchall():
  438. sql_data = {}
  439. sql_data['id'] = i[0]
  440. sql_data['pw'] = i[1]
  441. sql_data['date'] = i[2]
  442. sql_data['encode'] = i[3]
  443. sql_data['question'] = i[4]
  444. sql_data['answer'] = i[5]
  445. sql_data['ip'] = i[6]
  446. sql_data['ua'] = i[7]
  447. sql_data['email'] = i[8]
  448. curs.execute(db_change(
  449. "insert into user_set (name, id, data) values (?, ?, ?)"
  450. ), ['application', i[0], json.dumps(sql_data)])
  451. conn.commit()
  452. # 아이피 상태인 이메일 제거 예정
  453. print('Update completed')
  454. def set_init_always(ver_num):
  455. curs = conn.cursor()
  456. curs.execute(db_change('delete from other where name = "ver"'))
  457. curs.execute(db_change('insert into other (name, data) values ("ver", ?)'), [ver_num])
  458. curs.execute(db_change('delete from alist where name = "owner"'))
  459. curs.execute(db_change('insert into alist (name, acl) values ("owner", "owner")'))
  460. if not os.path.exists(load_image_url()):
  461. os.makedirs(load_image_url())
  462. conn.commit()
  463. def set_init():
  464. curs = conn.cursor()
  465. # 초기값 설정 함수
  466. curs.execute(db_change("select html from html_filter where kind = 'email'"))
  467. if not curs.fetchall():
  468. for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']:
  469. curs.execute(db_change(
  470. "insert into html_filter (html, kind, plus, plus_t) values (?, 'email', '', '')"
  471. ), [i])
  472. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  473. if not curs.fetchall():
  474. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  475. curs.execute(db_change(
  476. "insert into html_filter (html, kind, plus, plus_t) values (?, 'extension', '', '')"
  477. ), [i])
  478. curs.execute(db_change(
  479. 'select data from other ' + \
  480. 'where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'
  481. ))
  482. if not curs.fetchall():
  483. for i in [
  484. ['smtp_server', 'smtp.gmail.com'],
  485. ['smtp_port', '587'],
  486. ['smtp_security', 'starttls']
  487. ]:
  488. curs.execute(db_change(
  489. "insert into other (name, data) values (?, ?)"
  490. ), [i[0], i[1]])
  491. curs.execute(db_change('select data from other where name = "key"'))
  492. rep_data = curs.fetchall()
  493. if not rep_data:
  494. curs.execute(db_change('insert into other (name, data) values ("key", ?)'), [load_random_key()])
  495. curs.execute(db_change('select data from other where name = "count_all_title"'))
  496. if not curs.fetchall():
  497. curs.execute(db_change('insert into other (name, data) values ("count_all_title", "0")'))
  498. conn.commit()
  499. # Func-simple
  500. ## Func-simple-without_DB
  501. def get_default_admin_group():
  502. return ['owner', 'ban']
  503. def load_random_key(long = 128):
  504. return ''.join(
  505. random.choice(
  506. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  507. ) for i in range(long)
  508. )
  509. def http_warning():
  510. return '''
  511. <div id="http_warning_text"></div>
  512. <script>
  513. if(window.location.protocol !== 'https:') {
  514. document.getElementById('http_warning_text').innerHTML = "''' + \
  515. load_lang('http_warning') + \
  516. '''";
  517. document.getElementById('http_warning_text').style.margin = "10px 0px 0px 0px";
  518. }
  519. </script>
  520. '''
  521. def next_fix(link, num, page, end = 50):
  522. list_data = ''
  523. if num == 1:
  524. if len(page) == end:
  525. list_data += '' + \
  526. '<hr class="main_hr">' + \
  527. '<a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  528. ''
  529. elif len(page) != end:
  530. list_data += '' + \
  531. '<hr class="main_hr">' + \
  532. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>' + \
  533. ''
  534. else:
  535. list_data += '' + \
  536. '<hr class="main_hr">' + \
  537. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  538. ''
  539. return list_data
  540. def leng_check(A, B):
  541. # B -> new
  542. # A -> old
  543. return '0' if A == B else (('-' + str(A - B)) if A > B else ('+' + str(B - A)))
  544. def number_check(data):
  545. try:
  546. int(data)
  547. return data
  548. except:
  549. return '1'
  550. def check_int(data):
  551. try:
  552. int(data)
  553. return data
  554. except:
  555. return ''
  556. def redirect(data = '/'):
  557. return flask.redirect(flask.request.host_url[:-1] + data)
  558. def get_acl_list(type_d = 'normal'):
  559. if type_d == 'user':
  560. return ['', 'user', 'all']
  561. else:
  562. return ['', 'all', 'user', 'admin', 'owner', '50_edit', 'email', 'ban', 'before', '30_day', 'ban_admin', 'not_all']
  563. ## Func-simple-with_DB
  564. def load_image_url():
  565. curs = conn.cursor()
  566. curs.execute(db_change('select data from other where name = "image_where"'))
  567. image_where = curs.fetchall()
  568. image_where = image_where[0][0] if image_where else os.path.join('data', 'images')
  569. return image_where
  570. def load_domain():
  571. curs = conn.cursor()
  572. curs.execute(db_change("select data from other where name = 'domain'"))
  573. domain = curs.fetchall()
  574. domain = domain[0][0] if domain and domain[0][0] != '' else flask.request.host_url
  575. return domain
  576. def edit_button(editor_display = '0'):
  577. curs = conn.cursor()
  578. insert_list = []
  579. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  580. db_data = curs.fetchall()
  581. for get_data in db_data:
  582. insert_list += [[get_data[1], get_data[0]]]
  583. data = ''
  584. for insert_data in insert_list:
  585. data += '' + \
  586. '<a href="' + \
  587. 'javascript:do_insert_data(\'textarea_edit_view\', \'' + insert_data[0] + '\', ' + editor_display + ')' + \
  588. '">(' + insert_data[1] + ')</a> ' + \
  589. ''
  590. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  591. data += '<hr class="main_hr">'
  592. return data
  593. def ip_warning():
  594. curs = conn.cursor()
  595. if ip_or_user() != 0:
  596. curs.execute(db_change('select data from other where name = "no_login_warning"'))
  597. data = curs.fetchall()
  598. if data and data[0][0] != '':
  599. text_data = '' + \
  600. '<span>' + data[0][0] + '</span>' + \
  601. '<hr class="main_hr">' + \
  602. ''
  603. else:
  604. text_data = '' + \
  605. '<span>' + load_lang('no_login_warning') + '</span>' + \
  606. '<hr class="main_hr">' + \
  607. ''
  608. else:
  609. text_data = ''
  610. return text_data
  611. # Func-login
  612. def pw_encode(data, type_d = ''):
  613. curs = conn.cursor()
  614. if type_d == '':
  615. curs.execute(db_change('select data from other where name = "encode"'))
  616. set_data = curs.fetchall()
  617. type_d = set_data[0][0]
  618. if type_d == 'sha256':
  619. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  620. else:
  621. if sys.version_info < (3, 6):
  622. return sha3.sha3_256(bytes(data, 'utf-8')).hexdigest()
  623. else:
  624. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  625. def pw_check(data, data2, type_d = 'no', id_d = ''):
  626. curs = conn.cursor()
  627. curs.execute(db_change('select data from other where name = "encode"'))
  628. db_data = curs.fetchall()
  629. if type_d != 'no':
  630. if type_d == '':
  631. set_data = 'sha3'
  632. else:
  633. set_data = type_d
  634. else:
  635. set_data = db_data[0][0]
  636. re_data = 1 if pw_encode(data, set_data) == data2 else 0
  637. if db_data[0][0] != set_data and re_data == 1 and id_d != '':
  638. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'pw'"), [
  639. pw_encode(data),
  640. id_d
  641. ])
  642. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'encode'"), [
  643. db_data[0][0],
  644. id_d
  645. ])
  646. return re_data
  647. # Func-skin
  648. def easy_minify(data, tool = None):
  649. # without_DB
  650. return data
  651. def load_lang(data, safe = 0):
  652. curs = conn.cursor()
  653. global global_lang
  654. ip = ip_check()
  655. if ip_or_user(ip) == 0:
  656. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip])
  657. rep_data = curs.fetchall()
  658. elif 'lang' in flask.session:
  659. rep_data = [[flask.session['lang']]]
  660. else:
  661. curs.execute(db_change("select data from other where name = 'language'"))
  662. rep_data = curs.fetchall()
  663. if not rep_data or rep_data[0][0] in ('', 'default'):
  664. curs.execute(db_change("select data from other where name = 'language'"))
  665. rep_data = curs.fetchall()
  666. if rep_data:
  667. lang_name = rep_data[0][0]
  668. else:
  669. lang_name = 'en-US'
  670. if lang_name in global_lang:
  671. lang = global_lang[lang_name]
  672. else:
  673. lang_list = os.listdir('lang')
  674. if (lang_name + '.json') in lang_list:
  675. lang = json.loads(open(
  676. os.path.join('lang', lang_name + '.json'),
  677. encoding = 'utf8'
  678. ).read())
  679. global_lang[lang_name] = lang
  680. else:
  681. lang = {}
  682. if data in lang:
  683. if safe == 1:
  684. return lang[data]
  685. else:
  686. return html.escape(lang[data])
  687. return html.escape(data + ' (' + lang_name + ')')
  688. def skin_check(set_n = 0):
  689. curs = conn.cursor()
  690. # 개편 필요?
  691. skin_list = load_skin('tenshi', 1)
  692. skin = skin_list[0]
  693. ip = ip_check()
  694. user_need_skin = ''
  695. if ip_or_user(ip) == 0:
  696. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  697. skin_exist = curs.fetchall()
  698. if skin_exist:
  699. user_need_skin = skin_exist[0][0]
  700. else:
  701. if 'skin' in flask.session:
  702. user_need_skin = flask.session['skin']
  703. if user_need_skin == '':
  704. curs.execute(db_change('select data from other where name = "skin"'))
  705. skin_exist = curs.fetchall()
  706. if skin_exist:
  707. user_need_skin = skin_exist[0][0]
  708. if user_need_skin != '' and user_need_skin in skin_list:
  709. skin = user_need_skin
  710. if set_n == 0:
  711. return './views/' + skin + '/index.html'
  712. else:
  713. return skin
  714. def wiki_css(data):
  715. # without_DB
  716. global data_css
  717. global data_css_ver
  718. data += ['' for _ in range(0, 3 - len(data))]
  719. if data_css == '':
  720. for i_data in os.listdir(os.path.join("views", "main_css", "css")):
  721. if not i_data in ('sub'):
  722. data_css += '<link rel="stylesheet" href="/views/main_css/css/' + i_data + '?ver=' + data_css_ver + '">'
  723. for i_data in os.listdir(os.path.join("views", "main_css", "js")):
  724. if not i_data in ('render', 'route'):
  725. data_css += '<script src="/views/main_css/js/' + i_data + '?ver=' + data_css_ver + '"></script>'
  726. data = data[0:2] + ['', '''
  727. <link rel="stylesheet"
  728. href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/styles/default.min.css">
  729. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.2.0/build/highlight.min.js"></script>
  730. <link defer rel="stylesheet"
  731. href="https://cdn.jsdelivr.net/npm/katex@0.13.2/dist/katex.min.css"
  732. integrity="sha384-Cqd8ihRLum0CCg8rz0hYKPoLZ3uw+gES2rXQXycqnL5pgVQIflxAUDS7ZSjITLb5"
  733. crossorigin="anonymous">
  734. <script src="https://cdn.jsdelivr.net/npm/katex@0.13.2/dist/katex.min.js"
  735. integrity="sha384-1Or6BdeNQb0ezrmtGeqQHFpppNd7a/gw29xeiSikBbsb44xu3uAo8c7FwbF5jhbd"
  736. crossorigin="anonymous"></script>
  737. ''' + data_css] + data[2:]
  738. return data
  739. def cut_100(data):
  740. # without_DB
  741. data = re.search(r'<pre style="display: none;" id="render_content_load">([^<>]+)<\/pre>', data)
  742. if data:
  743. data = data.group(1)
  744. if len(data) > 100:
  745. return data[0:100] + '...'
  746. else:
  747. return data[0:len(data)]
  748. else:
  749. return ''
  750. def wiki_set(num = 1):
  751. curs = conn.cursor()
  752. if num == 1:
  753. skin_name = skin_check(1)
  754. data_list = []
  755. curs.execute(db_change('select data from other where name = ?'), ['name'])
  756. db_data = curs.fetchall()
  757. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['Wiki']
  758. curs.execute(db_change('select data from other where name = "license"'))
  759. db_data = curs.fetchall()
  760. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['ARR']
  761. data_list += ['', '']
  762. curs.execute(db_change('select data from other where name = "logo" and coverage = ?'), [skin_name])
  763. db_data = curs.fetchall()
  764. if db_data and db_data[0][0] != '':
  765. data_list += [db_data[0][0]]
  766. else:
  767. curs.execute(db_change('select data from other where name = "logo" and coverage = ""'))
  768. db_data = curs.fetchall()
  769. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else [data_list[0]]
  770. head_data = ''
  771. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  772. db_data = curs.fetchall()
  773. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  774. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_name])
  775. db_data = curs.fetchall()
  776. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  777. data_list += [head_data]
  778. elif num == 2:
  779. curs.execute(db_change('select data from other where name = "frontpage"'))
  780. db_data = curs.fetchall()
  781. data_list = db_data[0][0] if db_data and db_data[0][0] != '' else 'FrontPage'
  782. elif num == 3:
  783. curs.execute(db_change('select data from other where name = "upload"'))
  784. db_data = curs.fetchall()
  785. data_list = db_data[0][0] if db_data and db_data[0][0] != '' else '2'
  786. else:
  787. data_list = ''
  788. return data_list
  789. def wiki_custom():
  790. curs = conn.cursor()
  791. ip = ip_check()
  792. if ip_or_user(ip) == 0:
  793. user_icon = 1
  794. user_name = ip
  795. curs.execute(db_change("select data from user_set where id = ? and name = 'custom_css'"), [ip])
  796. user_head = curs.fetchall()
  797. user_head = user_head[0][0] if user_head else ''
  798. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  799. email = curs.fetchall()
  800. email = email[0][0] if email else ''
  801. if admin_check('all') == 1:
  802. user_admin = '1'
  803. user_acl_list = []
  804. curs.execute(db_change("select data from user_set where id = ? and name = 'acl'"), [ip])
  805. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  806. user_acl = curs.fetchall()
  807. for i in user_acl:
  808. user_acl_list += [i[0]]
  809. user_acl_list = user_acl_list if user_acl != [] else '0'
  810. else:
  811. user_admin = '0'
  812. user_acl_list = '0'
  813. curs.execute(db_change("select count(*) from alarm where name = ?"), [ip])
  814. count = curs.fetchall()
  815. user_notice = str(count[0][0]) if count else '0'
  816. else:
  817. user_icon = 0
  818. user_name = load_lang('user')
  819. email = ''
  820. user_admin = '0'
  821. user_acl_list = '0'
  822. user_notice = '0'
  823. user_head = flask.session['head'] if 'head' in flask.session else ''
  824. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  825. user_topic = '1' if curs.fetchall() else '0'
  826. split_path = flask.request.path.split('/')
  827. if len(split_path) > 1:
  828. split_path = split_path[1]
  829. else:
  830. split_path = 0
  831. return [
  832. '',
  833. '',
  834. user_icon,
  835. user_head,
  836. email,
  837. user_name,
  838. user_admin,
  839. str(ban_check()),
  840. user_notice,
  841. user_acl_list,
  842. ip,
  843. user_topic,
  844. split_path
  845. ]
  846. def load_skin(data = '', set_n = 0, default = 0):
  847. # without_DB
  848. # data -> 가장 앞에 있을 스킨 이름
  849. # set_n == 0 -> 스트링으로 반환
  850. # set_n == 1 -> 리스트로 반환
  851. # default == 0 -> 디폴트 미포함
  852. # default == 1 -> 디폴트 포함
  853. if set_n == 0:
  854. skin_return_data = ''
  855. else:
  856. skin_return_data = []
  857. skin_list_get = os.listdir('views')
  858. if default == 1:
  859. skin_list_get = ['default'] + skin_list_get
  860. for skin_data in skin_list_get:
  861. if skin_data != 'default':
  862. see_data = skin_data
  863. else:
  864. see_data = load_lang('default')
  865. if skin_data != 'main_css':
  866. if set_n == 0:
  867. if skin_data == data:
  868. skin_return_data = '' + \
  869. '<option value="' + skin_data + '">' + \
  870. see_data + \
  871. '</option>' + \
  872. '' + skin_return_data
  873. else:
  874. skin_return_data += '' + \
  875. '<option value="' + skin_data + '">' + \
  876. see_data + \
  877. '</option>' + \
  878. ''
  879. else:
  880. if skin_data == data:
  881. skin_return_data = [skin_data] + skin_return_data
  882. else:
  883. skin_return_data += [skin_data]
  884. return skin_return_data
  885. # Func-markup
  886. def render_set(doc_name = '', doc_data = '', data_type = 'view', data_in = '', doc_acl = ''):
  887. # without_DB
  888. # data_type in ['view', 'raw', 'api_view', 'backlink']
  889. doc_acl = acl_check(doc_name, 'render') if doc_acl == '' else doc_acl
  890. doc_data = 0 if doc_data == None else doc_data
  891. if doc_acl == 1:
  892. return 'HTTP Request 401.3'
  893. else:
  894. if data_type == 'raw':
  895. return doc_data
  896. else:
  897. if doc_data != 0:
  898. get_class_render = class_do_render(conn)
  899. return get_class_render.do_render(doc_name, doc_data, data_type, data_in)
  900. else:
  901. return 'HTTP Request 404'
  902. # Func-request
  903. def send_email(who, title, data):
  904. curs = conn.cursor()
  905. try:
  906. curs.execute(db_change('' + \
  907. 'select name, data from other ' + \
  908. 'where name = "smtp_email" or name = "smtp_pass" or name = "smtp_server" or name = "smtp_port" or name = "smtp_security"' + \
  909. ''))
  910. rep_data = curs.fetchall()
  911. smtp_email = ''
  912. smtp_pass = ''
  913. smtp_server = ''
  914. smtp_security = ''
  915. smtp_port = ''
  916. smtp = ''
  917. for i in rep_data:
  918. if i[0] == 'smtp_email':
  919. smtp_email = i[1]
  920. elif i[0] == 'smtp_pass':
  921. smtp_pass = i[1]
  922. elif i[0] == 'smtp_server':
  923. smtp_server = i[1]
  924. elif i[0] == 'smtp_security':
  925. smtp_security = i[1]
  926. elif i[0] == 'smtp_port':
  927. smtp_port = i[1]
  928. smtp_port = int(smtp_port)
  929. if smtp_security == 'plain':
  930. smtp = smtplib.SMTP(smtp_server, smtp_port)
  931. elif smtp_security == 'starttls':
  932. smtp = smtplib.SMTP(smtp_server, smtp_port)
  933. smtp.starttls()
  934. else:
  935. # if smtp_security == 'tls':
  936. smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
  937. smtp.login(smtp_email, smtp_pass)
  938. domain = load_domain()
  939. wiki_name = wiki_set()[0]
  940. msg = email.mime.text.MIMEText(data)
  941. msg['Subject'] = title
  942. msg['From'] = 'openNAMU <noreply@' + domain + '>'
  943. msg['To'] = who
  944. smtp.sendmail('openNAMU@' + domain, who, msg.as_string())
  945. smtp.quit()
  946. return 1
  947. except Exception as e:
  948. print('----')
  949. print('Error : email send error')
  950. print(e)
  951. return 0
  952. def captcha_get():
  953. curs = conn.cursor()
  954. data = ''
  955. if ip_or_user() != 0:
  956. curs.execute(db_change('select data from other where name = "recaptcha"'))
  957. recaptcha = curs.fetchall()
  958. curs.execute(db_change('select data from other where name = "sec_re"'))
  959. sec_re = curs.fetchall()
  960. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  961. rec_ver = curs.fetchall()
  962. if recaptcha and recaptcha[0][0] != '' and sec_re and sec_re[0][0] != '':
  963. if not rec_ver or rec_ver[0][0] == '':
  964. data += '' + \
  965. '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
  966. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  967. '<hr class="main_hr">' + \
  968. ''
  969. elif rec_ver[0][0] == 'v3':
  970. data += '' + \
  971. '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
  972. '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
  973. '<script type="text/javascript">' + \
  974. 'grecaptcha.ready(function() {' + \
  975. 'grecaptcha.execute(\'' + recaptcha[0][0] + '\', {action: \'homepage\'}).then(function(token) {' + \
  976. 'document.getElementById(\'g-recaptcha\').value = token;' + \
  977. '});' + \
  978. '});' + \
  979. '</script>' + \
  980. ''
  981. else:
  982. data += '''
  983. <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
  984. <div class="h-captcha" data-sitekey="''' + recaptcha[0][0] + '''"></div>
  985. <hr class="main_hr">
  986. '''
  987. return data
  988. def captcha_post(re_data, num = 1):
  989. curs = conn.cursor()
  990. if num == 1 and ip_or_user() != 0:
  991. curs.execute(db_change('select data from other where name = "sec_re"'))
  992. sec_re = curs.fetchall()
  993. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  994. rec_ver = curs.fetchall()
  995. if captcha_get() != '':
  996. if not rec_ver or rec_ver[0][0] in ('', 'v3'):
  997. data = requests.get(
  998. 'https://www.google.com/recaptcha/api/siteverify' + \
  999. '?secret=' + sec_re[0][0] + '&response=' + re_data
  1000. )
  1001. if data.status_code == 200:
  1002. json_data = json.loads(data.text)
  1003. if json_data['success'] != True:
  1004. return 1
  1005. else:
  1006. data = requests.get(
  1007. 'https://hcaptcha.com/siteverify' + \
  1008. '?secret=' + sec_re[0][0] + '&response=' + re_data
  1009. )
  1010. if data.status_code == 200:
  1011. json_data = json.loads(data.text)
  1012. if json_data['success'] != True:
  1013. return 1
  1014. return 0
  1015. # Func-user
  1016. def ip_or_user(data = ''):
  1017. # without_DB
  1018. # 1 == ip
  1019. # 0 == reg
  1020. if data == '':
  1021. data = ip_check()
  1022. if re.search(r'(\.|:)', data):
  1023. return 1
  1024. else:
  1025. return 0
  1026. def admin_check(num = None, what = None, name = ''):
  1027. curs = conn.cursor()
  1028. ip = ip_check() if name == '' else name
  1029. time_data = get_time()
  1030. pass_ok = 0
  1031. if ip_or_user(ip) == 0:
  1032. curs.execute(db_change(
  1033. "select data from user_set where id = ? and name = 'acl'"
  1034. ), [ip])
  1035. user_auth = curs.fetchall()
  1036. if user_auth:
  1037. user_auth = user_auth[0][0]
  1038. check = {
  1039. 0 : 'owner',
  1040. 1 : 'ban',
  1041. 2 : 'nothing',
  1042. 3 : 'toron',
  1043. 4 : 'check',
  1044. 5 : 'acl',
  1045. 6 : 'hidel',
  1046. 7 : 'give'
  1047. }
  1048. if not num:
  1049. check = check[0]
  1050. elif num == 'all':
  1051. check = [check[i] for i in check]
  1052. else:
  1053. check = check[num]
  1054. curs.execute(db_change(
  1055. 'select name from alist where name = ? and acl = "owner"'
  1056. ), [user_auth])
  1057. if curs.fetchall():
  1058. pass_ok = 1
  1059. else:
  1060. if num == 'all':
  1061. curs.execute(db_change(
  1062. 'select name from alist where name = ?'
  1063. ), [user_auth])
  1064. else:
  1065. curs.execute(db_change(
  1066. 'select name from alist where name = ? and acl = ?'
  1067. ), [user_auth, check])
  1068. if curs.fetchall():
  1069. pass_ok = 1
  1070. if pass_ok == 1:
  1071. if what:
  1072. curs.execute(db_change(
  1073. "insert into re_admin (who, what, time) values (?, ?, ?)"
  1074. ), [ip, what, time_data])
  1075. conn.commit()
  1076. return 1
  1077. return 0
  1078. def acl_check(name = 'test', tool = '', topic_num = '1'):
  1079. curs = conn.cursor()
  1080. ip = ip_check()
  1081. get_ban = ban_check()
  1082. if tool == '' and name:
  1083. if tool == '' and acl_check(name, 'render') == 1:
  1084. return 1
  1085. user_page = re.search(r"^user:((?:(?!\/).)*)", name)
  1086. if user_page:
  1087. user_page = user_page.group(1)
  1088. if admin_check(5) == 1:
  1089. return 0
  1090. if get_ban == 1:
  1091. return 1
  1092. curs.execute(db_change(
  1093. "select data from acl where title = ? and type = 'decu'"
  1094. ), [name])
  1095. acl_data = curs.fetchall()
  1096. if acl_data:
  1097. if acl_data[0][0] == 'all':
  1098. return 0
  1099. elif acl_data[0][0] == 'user' and not ip_or_user(ip) == 1:
  1100. return 0
  1101. if ip == user_page and not ip_or_user(ip) == 1:
  1102. return 0
  1103. return 1
  1104. elif tool == 'topic':
  1105. curs.execute(db_change("select title from rd where code = ?"), [topic_num])
  1106. name = curs.fetchall()
  1107. name = name[0][0] if name else 'test'
  1108. if tool in ['topic']:
  1109. end = 3
  1110. elif tool in ['render', 'vote', '']:
  1111. end = 2
  1112. else:
  1113. end = 1
  1114. for i in range(0, end):
  1115. if tool == '':
  1116. if i == 0:
  1117. curs.execute(db_change(
  1118. "select data from acl where title = ? and type = 'decu'"
  1119. ), [name])
  1120. '''
  1121. elif i == 1:
  1122. curs.execute(db_change(
  1123. "select plus from html_filter where kind = 'document'"
  1124. ))
  1125. '''
  1126. else:
  1127. curs.execute(db_change(
  1128. 'select data from other where name = "edit"'
  1129. ))
  1130. num = 5
  1131. elif tool == 'topic':
  1132. if i == 0:
  1133. curs.execute(db_change(
  1134. "select acl from rd where code = ?"
  1135. ), [topic_num])
  1136. elif i == 1:
  1137. curs.execute(db_change(
  1138. "select data from acl where title = ? and type = 'dis'"
  1139. ), [name])
  1140. else:
  1141. curs.execute(db_change(
  1142. 'select data from other where name = "discussion"'
  1143. ))
  1144. num = 3
  1145. elif tool == 'upload':
  1146. curs.execute(db_change(
  1147. "select data from other where name = 'upload_acl'"
  1148. ))
  1149. num = 5
  1150. elif tool == 'many_upload':
  1151. curs.execute(db_change(
  1152. "select data from other where name = 'many_upload_acl'"
  1153. ))
  1154. num = 5
  1155. elif tool == 'vote':
  1156. if i == 0:
  1157. curs.execute(db_change(
  1158. 'select acl from vote where id = ? and user = ""'
  1159. ), [topic_num])
  1160. else:
  1161. curs.execute(db_change(
  1162. 'select data from other where name = "vote_acl"'
  1163. ))
  1164. num = None
  1165. else:
  1166. # tool == 'render'
  1167. if i == 0:
  1168. curs.execute(db_change(
  1169. "select data from acl where title = ? and type = 'view'"
  1170. ), [name])
  1171. else:
  1172. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  1173. num = 5
  1174. acl_data = curs.fetchall()
  1175. if not acl_data:
  1176. acl_data = [['normal']]
  1177. elif acl_data and acl_data[0][0] == '':
  1178. acl_data = [['normal']]
  1179. if acl_data[0][0] != 'normal':
  1180. if not acl_data[0][0] in ['ban', 'ban_admin'] and get_ban == 1 and tool != 'render':
  1181. return 1
  1182. if acl_data[0][0] in ['all', 'ban']:
  1183. return 0
  1184. elif acl_data[0][0] == 'user':
  1185. if ip_or_user(ip) != 1:
  1186. return 0
  1187. elif acl_data[0][0] == 'admin':
  1188. if ip_or_user(ip) != 1:
  1189. if admin_check(num) == 1:
  1190. return 0
  1191. elif acl_data[0][0] == '50_edit':
  1192. if ip_or_user(ip) != 1:
  1193. if admin_check(num) == 1:
  1194. return 0
  1195. else:
  1196. curs.execute(db_change(
  1197. "select count(*) from history where ip = ?"
  1198. ), [ip])
  1199. count = curs.fetchall()
  1200. count = count[0][0] if count else 0
  1201. if count >= 50:
  1202. return 0
  1203. elif acl_data[0][0] == 'before':
  1204. if ip_or_user(ip) != 1:
  1205. if admin_check(num) == 1:
  1206. return 0
  1207. curs.execute(db_change(
  1208. "select ip from history where title = ? and ip = ?"
  1209. ), [name, ip])
  1210. if curs.fetchall():
  1211. return 0
  1212. elif acl_data[0][0] == '30_day':
  1213. if ip_or_user(ip) != 1:
  1214. if admin_check(num) == 1:
  1215. return 0
  1216. else:
  1217. curs.execute(db_change(
  1218. "select data from user_set where id = ? and name = 'date'"
  1219. ), [ip])
  1220. user_date = curs.fetchall()[0][0]
  1221. time_1 = datetime.datetime.strptime(
  1222. user_date,
  1223. '%Y-%m-%d %H:%M:%S'
  1224. ) + datetime.timedelta(days = 30)
  1225. time_2 = datetime.datetime.strptime(
  1226. get_time(),
  1227. '%Y-%m-%d %H:%M:%S'
  1228. )
  1229. if time_2 > time_1:
  1230. return 0
  1231. elif acl_data[0][0] == 'email':
  1232. if ip_or_user(ip) != 1:
  1233. if admin_check(num) == 1:
  1234. return 0
  1235. else:
  1236. curs.execute(db_change(
  1237. "select data from user_set where id = ? and name = 'email'"
  1238. ), [ip])
  1239. if curs.fetchall():
  1240. return 0
  1241. elif acl_data[0][0] == 'owner':
  1242. if admin_check() == 1:
  1243. return 0
  1244. elif acl_data[0][0] == 'ban_admin':
  1245. if admin_check(1) == 1 or get_ban == 1:
  1246. return 0
  1247. elif acl_data[0][0] == 'not_all':
  1248. return 1
  1249. return 1
  1250. elif i == (end - 1):
  1251. if get_ban == 1 and tool != 'render':
  1252. return 1
  1253. if tool == 'topic':
  1254. curs.execute(db_change(
  1255. "select title from rd where code = ? and stop != ''"
  1256. ), [topic_num])
  1257. if curs.fetchall():
  1258. if admin_check(3, 'topic (code ' + topic_num + ')') == 1:
  1259. return 0
  1260. else:
  1261. return 1
  1262. else:
  1263. return 0
  1264. else:
  1265. return 0
  1266. return 1
  1267. def ban_check(ip = None, tool = ''):
  1268. curs = conn.cursor()
  1269. ip = ip_check() if not ip else ip
  1270. tool = '' if not tool else tool
  1271. if admin_check(None, None, ip) == 1:
  1272. return 0
  1273. curs.execute(db_change(
  1274. "update rb set ongoing = '' " + \
  1275. "where end < ? and end != '' and ongoing = '1'"
  1276. ), [get_time()])
  1277. conn.commit()
  1278. curs.execute(db_change("" + \
  1279. "select login, block from rb " + \
  1280. "where band = 'regex' and ongoing = '1'" + \
  1281. ""))
  1282. regex_d = curs.fetchall()
  1283. for test_r in regex_d:
  1284. g_regex = re.compile(test_r[1])
  1285. if g_regex.search(ip):
  1286. if tool == 'login':
  1287. if test_r[0] != 'O':
  1288. return 1
  1289. else:
  1290. return 1
  1291. curs.execute(db_change("" + \
  1292. "select login from rb " + \
  1293. "where block = ? and band = '' and ongoing = '1'" + \
  1294. "" + \
  1295. ""), [ip])
  1296. ban_d = curs.fetchall()
  1297. if ban_d:
  1298. if tool == 'login':
  1299. if ban_d[0][0] != 'O':
  1300. return 1
  1301. else:
  1302. return 1
  1303. return 0
  1304. def ip_pas(raw_ip, type_data = 0):
  1305. curs = conn.cursor()
  1306. hide = 0
  1307. end_ip = {}
  1308. return_data = 0
  1309. if type(raw_ip) != type([]):
  1310. get_ip = [raw_ip]
  1311. return_data = 1
  1312. else:
  1313. get_ip = raw_ip
  1314. curs.execute(db_change("select data from other where name = 'ip_view'"))
  1315. ip_view = curs.fetchall()
  1316. ip_view = ip_view[0][0] if ip_view else ''
  1317. ip_view = '' if admin_check(1) == 1 else ip_view
  1318. get_ip = list(set(get_ip))
  1319. for raw_ip in get_ip:
  1320. change_ip = 0
  1321. is_this_ip = ip_or_user(raw_ip)
  1322. if is_this_ip != 0 and ip_view != '':
  1323. ip = re.sub(r'\.([^.]*)\.([^.]*)$', '.*.*', raw_ip)
  1324. ip = re.sub(r':([^:]*):([^:]*)$', ':*:*', ip)
  1325. change_ip = 1
  1326. else:
  1327. ip = raw_ip
  1328. if type_data == 0 and change_ip == 0:
  1329. ip = '<span class="opennamu_ip_render">' + raw_ip + '</span>'
  1330. end_ip[raw_ip] = ip
  1331. if return_data == 1:
  1332. return end_ip[raw_ip]
  1333. else:
  1334. return end_ip
  1335. # Func-edit
  1336. def slow_edit_check():
  1337. curs = conn.cursor()
  1338. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1339. slow_edit = curs.fetchall()
  1340. if slow_edit and slow_edit != '0' and admin_check(5) != 1:
  1341. slow_edit = slow_edit[0][0]
  1342. curs.execute(db_change(
  1343. "select date from history where ip = ? order by date desc limit 1"
  1344. ), [ip_check()])
  1345. last_edit_data = curs.fetchall()
  1346. if last_edit_data:
  1347. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  1348. now_edit_data = int(
  1349. (datetime.datetime.now() - datetime.timedelta(
  1350. seconds = int(slow_edit))
  1351. ).strftime("%Y%m%d%H%M%S")
  1352. )
  1353. if last_edit_data > now_edit_data:
  1354. return 1
  1355. return 0
  1356. def edit_filter_do(data):
  1357. curs = conn.cursor()
  1358. if admin_check(1) != 1:
  1359. curs.execute(db_change(
  1360. "select plus, plus_t from html_filter where kind = 'regex_filter' and plus != ''"
  1361. ))
  1362. for data_list in curs.fetchall():
  1363. match = re.compile(data_list[0], re.I)
  1364. if match.search(data):
  1365. ban_insert(
  1366. ip_check(),
  1367. '0' if data_list[1] == 'X' else data_list[1],
  1368. 'edit filter',
  1369. None,
  1370. 'tool:edit filter'
  1371. )
  1372. return 1
  1373. return 0
  1374. # Func-insert
  1375. def add_alarm(who, context):
  1376. curs = conn.cursor()
  1377. curs.execute(db_change(
  1378. 'insert into alarm (name, data, date) values (?, ?, ?)'
  1379. ), [who, context, get_time()])
  1380. conn.commit()
  1381. def add_user(user_name, user_pw, user_email = '', user_encode = ''):
  1382. curs = conn.cursor()
  1383. if user_encode == '':
  1384. user_pw_hash = pw_encode(user_pw)
  1385. curs.execute(db_change('select data from other where name = "encode"'))
  1386. data_encode = curs.fetchall()
  1387. data_encode = data_encode[0][0]
  1388. else:
  1389. user_pw_hash = user_pw
  1390. data_encode = user_encode
  1391. curs.execute(db_change("select id from user_set limit 1"))
  1392. if not curs.fetchall():
  1393. user_auth = 'owner'
  1394. else:
  1395. user_auth = 'user'
  1396. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'pw', ?)"), [
  1397. user_name,
  1398. user_pw_hash
  1399. ])
  1400. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'acl', ?)"), [
  1401. user_name,
  1402. user_auth
  1403. ])
  1404. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'date', ?)"), [
  1405. user_name,
  1406. get_time()
  1407. ])
  1408. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'encode', ?)"), [
  1409. user_name,
  1410. data_encode
  1411. ])
  1412. if user_email != '':
  1413. curs.execute(db_change("insert into user_set (name, id, data) values ('email', ?, ?)"), [
  1414. user_name,
  1415. user_email
  1416. ])
  1417. conn.commit()
  1418. def ua_plus(u_id, u_ip, u_agent, time):
  1419. curs = conn.cursor()
  1420. curs.execute(db_change("select data from other where name = 'ua_get'"))
  1421. rep_data = curs.fetchall()
  1422. if rep_data and rep_data[0][0] != '':
  1423. pass
  1424. else:
  1425. curs.execute(db_change(
  1426. "insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"
  1427. ), [
  1428. u_id,
  1429. u_ip,
  1430. u_agent,
  1431. time
  1432. ])
  1433. conn.commit()
  1434. def ban_insert(name, end, why, login, blocker, type_d = None):
  1435. curs = conn.cursor()
  1436. now_time = get_time()
  1437. band = type_d if type_d else ''
  1438. curs.execute(db_change(
  1439. "update rb set ongoing = '' where end < ? and end != '' and ongoing = '1'"
  1440. ), [now_time])
  1441. curs.execute(db_change("" + \
  1442. "select block from rb " + \
  1443. "where ((end > ? and end != '') or end = '') and block = ? and " + \
  1444. "band = ? and ongoing = '1'" + \
  1445. ""), [now_time, name, band])
  1446. if curs.fetchall():
  1447. curs.execute(db_change(
  1448. "insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"
  1449. ), [
  1450. name,
  1451. 'release',
  1452. now_time,
  1453. blocker,
  1454. '',
  1455. band
  1456. ])
  1457. curs.execute(db_change(
  1458. "update rb set ongoing = '' where block = ? and band = ? and ongoing = '1'"
  1459. ), [name, band])
  1460. else:
  1461. login = 'O' if login != '' else ''
  1462. if end != '0':
  1463. end = int(number_check(end))
  1464. time = datetime.datetime.now()
  1465. plus = datetime.timedelta(seconds = end)
  1466. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  1467. else:
  1468. r_time = ''
  1469. curs.execute(db_change(
  1470. "insert into rb (block, end, today, blocker, why, band, ongoing, login) " + \
  1471. "values (?, ?, ?, ?, ?, ?, '1', ?)"
  1472. ), [
  1473. name,
  1474. r_time,
  1475. now_time,
  1476. blocker,
  1477. why,
  1478. band,
  1479. login
  1480. ])
  1481. conn.commit()
  1482. def rd_plus(topic_num, date, name = None, sub = None):
  1483. curs = conn.cursor()
  1484. curs.execute(db_change("select code from rd where code = ?"), [topic_num])
  1485. if curs.fetchall():
  1486. curs.execute(db_change("update rd set date = ? where code = ?"), [date, topic_num])
  1487. else:
  1488. curs.execute(db_change(
  1489. "insert into rd (title, sub, code, date) values (?, ?, ?, ?)"
  1490. ), [name, sub, topic_num, date])
  1491. conn.commit()
  1492. def history_plus(title, data, date, ip, send, leng, t_check = '', mode = ''):
  1493. curs = conn.cursor()
  1494. if mode == 'add':
  1495. curs.execute(db_change(
  1496. "select id from history where title = ? order by id + 0 asc limit 1"
  1497. ), [title])
  1498. id_data = curs.fetchall()
  1499. id_data = str(int(id_data[0][0]) - 1) if id_data else '0'
  1500. else:
  1501. curs.execute(db_change(
  1502. "select id from history where title = ? order by id + 0 desc limit 1"
  1503. ), [title])
  1504. id_data = curs.fetchall()
  1505. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  1506. mode = mode if not re.search('^user:', title) else 'user'
  1507. send = re.sub(r'\(|\)|<|>', '', send)
  1508. send = send[:128] if len(send) > 128 else send
  1509. send = send + ' (' + t_check + ')' if t_check != '' else send
  1510. if mode != 'add' and mode != 'user':
  1511. curs.execute(db_change("select count(*) from rc where type = 'normal'"))
  1512. if curs.fetchall()[0][0] >= 200:
  1513. curs.execute(db_change(
  1514. "select id, title from rc where type = 'normal' order by date asc limit 1"
  1515. ))
  1516. rc_data = curs.fetchall()
  1517. if rc_data:
  1518. curs.execute(db_change(
  1519. 'delete from rc where id = ? and title = ? and type = "normal"'
  1520. ), [
  1521. rc_data[0][0],
  1522. rc_data[0][1]
  1523. ])
  1524. curs.execute(db_change(
  1525. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  1526. ), [
  1527. id_data,
  1528. title,
  1529. date
  1530. ])
  1531. if mode != 'add':
  1532. curs.execute(db_change("select count(*) from rc where type = ?"), [mode])
  1533. if curs.fetchall()[0][0] >= 200:
  1534. curs.execute(db_change(
  1535. "select id, title from rc where type = ? order by date asc limit 1"
  1536. ), [mode])
  1537. rc_data = curs.fetchall()
  1538. if rc_data:
  1539. curs.execute(db_change(
  1540. 'delete from rc where id = ? and title = ? and type = ?'
  1541. ), [
  1542. rc_data[0][0],
  1543. rc_data[0][1],
  1544. mode
  1545. ])
  1546. curs.execute(db_change(
  1547. "insert into rc (id, title, date, type) values (?, ?, ?, ?)"
  1548. ), [
  1549. id_data,
  1550. title,
  1551. date,
  1552. mode
  1553. ])
  1554. curs.execute(db_change(
  1555. "insert into history (id, title, data, date, ip, send, leng, hide, type) " + \
  1556. "values (?, ?, ?, ?, ?, ?, ?, '', ?)"
  1557. ), [
  1558. id_data,
  1559. title,
  1560. data,
  1561. date,
  1562. ip,
  1563. send,
  1564. leng,
  1565. mode
  1566. ])
  1567. # Func-error
  1568. def re_error(data):
  1569. curs = conn.cursor()
  1570. conn.commit()
  1571. if data == '/ban':
  1572. if ban_check() == 1:
  1573. end = '<div id="get_user_info"></div><script>load_user_info("' + ip_check() + '");</script>'
  1574. else:
  1575. end = '<ul class="inside_ul"><li>' + load_lang('authority_error') + '</li></ul>'
  1576. return easy_minify(flask.render_template(skin_check(),
  1577. imp = [load_lang('error'), wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1578. data = '<h2>' + load_lang('error') + '</h2>' + end,
  1579. menu = 0
  1580. )), 401
  1581. else:
  1582. num = int(number_check(data.replace('/error/', '')))
  1583. if num == 1:
  1584. data = load_lang('no_login_error')
  1585. elif num == 2:
  1586. data = load_lang('no_exist_user_error')
  1587. elif num == 3:
  1588. data = load_lang('authority_error')
  1589. elif num == 4:
  1590. data = load_lang('no_admin_block_error')
  1591. elif num == 5:
  1592. data = load_lang('skin_error')
  1593. elif num == 6:
  1594. data = load_lang('same_id_exist_error')
  1595. elif num == 7:
  1596. data = load_lang('long_id_error')
  1597. elif num == 8:
  1598. data = load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id_filter_list') + ')</a>'
  1599. elif num == 9:
  1600. data = load_lang('file_exist_error')
  1601. elif num == 10:
  1602. data = load_lang('password_error')
  1603. elif num == 11:
  1604. data = load_lang('topic_long_error')
  1605. elif num == 12:
  1606. data = load_lang('email_error')
  1607. elif num == 13:
  1608. data = load_lang('recaptcha_error')
  1609. elif num == 14:
  1610. data = load_lang('file_extension_error') + ' <a href="/extension_filter">(' + load_lang('extension_filter_list') + ')</a>'
  1611. elif num == 15:
  1612. data = load_lang('edit_record_error')
  1613. elif num == 16:
  1614. data = load_lang('same_file_error')
  1615. elif num == 17:
  1616. data = load_lang('file_capacity_error') + wiki_set(3)
  1617. elif num == 18:
  1618. data = load_lang('email_send_error')
  1619. elif num == 19:
  1620. data = load_lang('decument_exist_error')
  1621. elif num == 20:
  1622. data = load_lang('password_diffrent_error')
  1623. elif num == 21:
  1624. data = load_lang('edit_filter_error')
  1625. elif num == 22:
  1626. data = load_lang('file_name_error')
  1627. elif num == 23:
  1628. data = load_lang('regex_error')
  1629. elif num == 24:
  1630. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1631. slow_edit = curs.fetchall()
  1632. slow_edit = '' if not slow_edit else slow_edit[0][0]
  1633. data = load_lang('fast_edit_error') + slow_edit
  1634. elif num == 25:
  1635. data = load_lang('too_many_dec_error')
  1636. elif num == 26:
  1637. data = load_lang('application_not_found')
  1638. elif num == 27:
  1639. data = load_lang("invalid_password_error")
  1640. elif num == 28:
  1641. data = load_lang('watchlist_overflow_error')
  1642. elif num == 29:
  1643. data = load_lang('copyright_disagreed')
  1644. elif num == 30:
  1645. data = load_lang('ie_wrong_callback')
  1646. elif num == 33:
  1647. data = load_lang('restart_fail_error')
  1648. elif num == 34:
  1649. data = load_lang("update_error") + ' <a href="https://github.com/opennamu/opennamu">(Github)</a>'
  1650. elif num == 35:
  1651. data = load_lang('same_email_error')
  1652. elif num == 36:
  1653. data = load_lang('input_email_error')
  1654. else:
  1655. data = '???'
  1656. if num == 5:
  1657. if flask.request.path != '/main_skin_set':
  1658. title = load_lang('skin_set')
  1659. tool = [['main_skin_set', load_lang('main_skin_set')]]
  1660. load_skin_set = ''
  1661. else:
  1662. title = load_lang('main_skin_set')
  1663. tool = [['skin_set', load_lang('skin_set')]]
  1664. load_skin_set = '<script>main_css_skin_set();</script>'
  1665. return easy_minify(flask.render_template(skin_check(),
  1666. imp = [title, wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1667. data = '' + \
  1668. '<div id="main_skin_set">' + \
  1669. '<h2>' + load_lang('error') + '</h2>' + \
  1670. '<ul class="inside_ul">' + \
  1671. '<li>' + data + ' <a href="/main_skin_set">(' + load_lang('main_skin_set') + ')</a></li>' + \
  1672. '</ul>' + \
  1673. '</div>' + \
  1674. load_skin_set,
  1675. menu = tool
  1676. ))
  1677. else:
  1678. return easy_minify(flask.render_template(skin_check(),
  1679. imp = [load_lang('error'), wiki_set(1), wiki_custom(), wiki_css([0, 0])],
  1680. data = '' + \
  1681. '<h2>' + load_lang('error') + '</h2>' + \
  1682. '<ul class="inside_ul">' + \
  1683. '<li>' + data + '</li>' + \
  1684. '</ul>' + \
  1685. '',
  1686. menu = 0
  1687. )), 400