func.py 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824
  1. # Init
  2. import os
  3. import sys
  4. import platform
  5. import json
  6. import smtplib
  7. import random
  8. import ipaddress
  9. import subprocess
  10. import email.mime.text
  11. import email.utils
  12. import email.header
  13. # Init-Version
  14. with open('version.json', encoding = 'utf8') as file_data:
  15. version_list = json.loads(file_data.read())
  16. print('Version : ' + version_list['beta']['r_ver'])
  17. print('DB set version : ' + version_list['beta']['c_ver'])
  18. print('Skin set version : ' + version_list['beta']['s_ver'])
  19. print('----')
  20. # Init-PIP_Install
  21. data_up_date = 1
  22. if os.path.exists(os.path.join('data', 'version.json')):
  23. with open(os.path.join('data', 'version.json'), encoding = 'utf8') as file_data:
  24. data_load_ver = file_data.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. python_ver = ''
  32. python_ver = str(sys.version_info.major) + '.' + str(sys.version_info.minor)
  33. run_list = [sys.executable, 'python' + python_ver, 'python3', 'python', 'py -' + python_ver]
  34. for exe_name in run_list:
  35. try:
  36. subprocess.check_call([exe_name, "-m", "pip", "install", "--upgrade", "--user", "-r", "requirements.txt"])
  37. try:
  38. os.execl(exe_name, sys.executable, *sys.argv)
  39. except:
  40. pass
  41. try:
  42. os.execl(exe_name, '"' + sys.executable + '"', *sys.argv)
  43. except:
  44. pass
  45. try:
  46. os.execl(exe_name, os.path.abspath(__file__), *sys.argv)
  47. except:
  48. pass
  49. except:
  50. pass
  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('Error : automatic installation is not supported.')
  56. print('Help : try "python3 -m pip install -r requirements.txt"')
  57. else:
  58. print('PIP check pass')
  59. print('----')
  60. # Init-Load
  61. from .func_tool import *
  62. from .func_render import class_do_render
  63. from diff_match_patch import diff_match_patch
  64. import waitress
  65. import werkzeug.routing
  66. import werkzeug.debug
  67. import flask
  68. import requests
  69. import pymysql
  70. if sys.version_info < (3, 6):
  71. import sha3
  72. # Init-Global
  73. global_lang = {}
  74. global_wiki_set = {}
  75. global_db_set = ''
  76. # Func
  77. # Func-main
  78. def do_db_set(db_set):
  79. global global_db_set
  80. global_db_set = db_set
  81. # Func-init
  82. def get_init_set_list(need = 'all'):
  83. init_set_list = {
  84. 'host' : {
  85. 'display' : 'Host',
  86. 'require' : 'conv',
  87. 'default' : '0.0.0.0'
  88. }, 'port' : {
  89. 'display' : 'Port',
  90. 'require' : 'conv',
  91. 'default' : '3000'
  92. }, 'language' : {
  93. 'display' : 'Language',
  94. 'require' : 'select',
  95. 'default' : 'ko-KR',
  96. 'list' : ['ko-KR', 'en-US']
  97. }, 'markup' : {
  98. 'display' : 'Markup',
  99. 'require' : 'select',
  100. 'default' : 'namumark',
  101. 'list' : ['namumark', 'markdown', 'custom', 'raw']
  102. }, 'encode' : {
  103. 'display' : 'Encryption method',
  104. 'require' : 'select',
  105. 'default' : 'sha3',
  106. 'list' : ['sha3', 'sha3-salt', 'sha3-512', 'sha3-512-salt']
  107. }
  108. }
  109. if need == 'all':
  110. return init_set_list
  111. else:
  112. return init_set_list[need]
  113. class get_db_connect:
  114. def __init__(self, db_type = ''):
  115. global global_db_set
  116. self.db_set = global_db_set
  117. if db_type != '':
  118. self.db_set['type'] = db_type
  119. def __enter__(self):
  120. if self.db_set['type'] == 'sqlite':
  121. self.conn = sqlite3.connect(
  122. self.db_set['name'] + '.db',
  123. check_same_thread = False,
  124. isolation_level = None
  125. )
  126. self.conn.execute('pragma journal_mode = wal')
  127. else:
  128. self.conn = pymysql.connect(
  129. host = self.db_set['mysql_host'],
  130. user = self.db_set['mysql_user'],
  131. password = self.db_set['mysql_pw'],
  132. charset = 'utf8mb4',
  133. port = int(self.db_set['mysql_port']),
  134. autocommit = True
  135. )
  136. try:
  137. self.conn.select_db(self.db_set['name'])
  138. except:
  139. pass
  140. return self.conn
  141. def __exit__(self, exc_type, exc_value, traceback):
  142. self.conn.commit()
  143. self.conn.close()
  144. # class get_whoosh_connect:
  145. class class_check_json:
  146. def do_check_set_json(self):
  147. if os.getenv('NAMU_DB') or os.getenv('NAMU_DB_TYPE'):
  148. set_data = {}
  149. set_data['db'] = os.getenv('NAMU_DB') if os.getenv('NAMU_DB') else 'data'
  150. set_data['db_type'] = os.getenv('NAMU_DB_TYPE') if os.getenv('NAMU_DB_TYPE') else 'sqlite'
  151. else:
  152. if os.path.exists(os.path.join('data', 'set.json')):
  153. db_set_list = ['db', 'db_type']
  154. with open(os.path.join('data', 'set.json'), encoding = 'utf8') as file_data:
  155. set_data = json.loads(file_data.read())
  156. for i in db_set_list:
  157. if not i in set_data:
  158. os.remove(os.path.join('data', 'set.json'))
  159. break
  160. if not os.path.exists(os.path.join('data', 'set.json')):
  161. set_data = {}
  162. normal_db_type = ['sqlite', 'mysql']
  163. print('DB type (' + normal_db_type[0] + ') [' + ', '.join(normal_db_type) + '] : ', end = '')
  164. data_get = str(input())
  165. if data_get == '' or not data_get in normal_db_type:
  166. set_data['db_type'] = 'sqlite'
  167. else:
  168. set_data['db_type'] = data_get
  169. all_src = []
  170. if set_data['db_type'] == 'sqlite':
  171. for i_data in os.listdir("."):
  172. f_src = re.search(r"(.+)\.db$", i_data)
  173. if f_src:
  174. all_src += [f_src.group(1)]
  175. print('DB name (data) [' + ', '.join(all_src) + '] : ', end = '')
  176. data_get = str(input())
  177. if data_get == '':
  178. set_data['db'] = 'data'
  179. else:
  180. set_data['db'] = data_get
  181. with open(os.path.join('data', 'set.json'), 'w', encoding = 'utf8') as f:
  182. f.write(json.dumps(set_data))
  183. print('DB name : ' + set_data['db'])
  184. print('DB type : ' + set_data['db_type'])
  185. data_db_set = {}
  186. data_db_set['name'] = set_data['db']
  187. data_db_set['type'] = set_data['db_type']
  188. return data_db_set
  189. def do_check_mysql_json(self, data_db_set):
  190. if os.path.exists(os.path.join('data', 'mysql.json')):
  191. db_set_list = ['user', 'password', 'host', 'port']
  192. with open(os.path.join('data', 'mysql.json'), encoding = 'utf8') as file_data:
  193. set_data = json.loads(file_data.read())
  194. for i in db_set_list:
  195. if not i in set_data:
  196. os.remove(os.path.join('data', 'mysql.json'))
  197. break
  198. set_data_mysql = set_data
  199. if not os.path.exists(os.path.join('data', 'mysql.json')):
  200. set_data_mysql = {}
  201. print('DB user ID : ', end = '')
  202. set_data_mysql['user'] = str(input())
  203. print('DB password : ', end = '')
  204. set_data_mysql['password'] = str(input())
  205. print('DB host (localhost) : ', end = '')
  206. set_data_mysql['host'] = str(input())
  207. if set_data_mysql['host'] == '':
  208. set_data_mysql['host'] = 'localhost'
  209. print('DB port (3306) : ', end = '')
  210. set_data_mysql['port'] = str(input())
  211. if set_data_mysql['port'] == '':
  212. set_data_mysql['port'] = '3306'
  213. with open(
  214. os.path.join('data', 'mysql.json'),
  215. 'w',
  216. encoding = 'utf8'
  217. ) as f:
  218. f.write(json.dumps(set_data_mysql))
  219. data_db_set['mysql_user'] = set_data_mysql['user']
  220. data_db_set['mysql_pw'] = set_data_mysql['password']
  221. if 'host' in set_data_mysql:
  222. data_db_set['mysql_host'] = set_data_mysql['host']
  223. else:
  224. data_db_set['mysql_host'] = 'localhost'
  225. if 'port' in set_data_mysql:
  226. data_db_set['mysql_port'] = set_data_mysql['port']
  227. else:
  228. data_db_set['mysql_port'] = '3306'
  229. return data_db_set
  230. def __init__(self):
  231. self.data_db_set = {}
  232. def __new__(self):
  233. self.data_db_set = self.do_check_set_json(self)
  234. if self.data_db_set['type'] == 'mysql':
  235. self.data_db_set = self.do_check_mysql_json(self, self.data_db_set)
  236. return self.data_db_set
  237. def get_db_table_list():
  238. # DB table
  239. # Init-Create_DB
  240. # --이거 개편한다더니 도대체 언제?--
  241. create_data = {}
  242. # 폐지 예정 (data_set으로 통합)
  243. create_data['data_set'] = ['doc_name', 'doc_rev', 'set_name', 'set_data']
  244. create_data['data'] = ['title', 'data', 'type']
  245. create_data['history'] = ['id', 'title', 'data', 'date', 'ip', 'send', 'leng', 'hide', 'type']
  246. create_data['rc'] = ['id', 'title', 'date', 'type']
  247. create_data['acl'] = ['title', 'data', 'type']
  248. # 개편 예정 (data_link로 변경)
  249. create_data['back'] = ['title', 'link', 'type', 'data']
  250. # 폐지 예정 (topic_set으로 통합) [가장 시급]
  251. create_data['topic_set'] = ['thread_code', 'set_name', 'set_id', 'set_data']
  252. create_data['rd'] = ['title', 'sub', 'code', 'date', 'band', 'stop', 'agree', 'acl']
  253. create_data['topic'] = ['id', 'data', 'date', 'ip', 'block', 'top', 'code']
  254. # 폐지 예정 (user_set으로 통합)
  255. create_data['rb'] = ['block', 'end', 'today', 'blocker', 'why', 'band', 'login', 'ongoing']
  256. create_data['scan'] = ['user', 'title', 'type']
  257. # 개편 예정 (wiki_set과 wiki_filter과 wiki_vote으로 변경)
  258. create_data['other'] = ['name', 'data', 'coverage']
  259. create_data['html_filter'] = ['html', 'kind', 'plus', 'plus_t']
  260. create_data['vote'] = ['name', 'id', 'subject', 'data', 'user', 'type', 'acl']
  261. # 개편 예정 (auth_list와 auth_log로 변경)
  262. create_data['alist'] = ['name', 'acl']
  263. create_data['re_admin'] = ['who', 'what', 'time']
  264. # 개편 예정 (user_notice와 user_agent로 변경)
  265. create_data['ua_d'] = ['name', 'ip', 'ua', 'today', 'sub']
  266. create_data['user_set'] = ['name', 'id', 'data']
  267. create_data['user_notice'] = ['id', 'name', 'data', 'date', 'readme']
  268. create_data['bbs_set'] = ['set_name', 'set_code', 'set_id', 'set_data']
  269. create_data['bbs_data'] = ['set_name', 'set_code', 'set_id', 'set_data']
  270. return create_data
  271. def update(ver_num, set_data):
  272. with get_db_connect() as conn:
  273. curs = conn.cursor()
  274. print('----')
  275. # 업데이트 하위 호환 유지 함수
  276. if ver_num < 3160027:
  277. print('Add init set')
  278. set_init()
  279. if ver_num < 3170002:
  280. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  281. if not curs.fetchall():
  282. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  283. curs.execute(db_change(
  284. "insert into html_filter (html, kind) values (?, 'extension')"
  285. ), [i])
  286. if ver_num < 3170400:
  287. curs.execute(db_change("select title, sub, code from topic where id = '1'"))
  288. for i in curs.fetchall():
  289. curs.execute(db_change(
  290. "update topic set code = ? where title = ? and sub = ?"
  291. ), [
  292. i[2],
  293. i[0],
  294. i[1]
  295. ])
  296. curs.execute(db_change(
  297. "update rd set code = ? where title = ? and sub = ?"
  298. ), [
  299. i[2],
  300. i[0],
  301. i[1]
  302. ])
  303. if ver_num < 3171800:
  304. curs.execute(db_change("select data from other where name = 'recaptcha'"))
  305. change_rec = curs.fetchall()
  306. if change_rec and change_rec[0][0] != '':
  307. new_rec = re.search(r'data-sitekey="([^"]+)"', change_rec[0][0])
  308. if new_rec:
  309. curs.execute(db_change(
  310. "update other set data = ? where name = 'recaptcha'"
  311. ), [new_rec.group(1)])
  312. else:
  313. curs.execute(db_change("update other set data = '' where name = 'recaptcha'"))
  314. curs.execute(db_change("update other set data = '' where name = 'sec_re'"))
  315. if ver_num < 3172800 and \
  316. set_data['db_type'] == 'mysql':
  317. get_data_mysql = json.loads(open('data/mysql.json', encoding = 'utf8').read())
  318. with open('data/mysql.json', 'w') as f:
  319. f.write('{ "user" : "' + get_data_mysql['user'] + '", "password" : "' + get_data_mysql['password'] + '", "host" : "localhost" }')
  320. if ver_num < 3183603:
  321. curs.execute(db_change("select block from ban where band = 'O'"))
  322. for i in curs.fetchall():
  323. curs.execute(db_change(
  324. "update ban set block = ?, band = 'regex' where block = ? and band = 'O'"
  325. ), [
  326. '^' + i[0].replace('.', '\\.'),
  327. i[0]
  328. ])
  329. curs.execute(db_change("select block from rb where band = 'O'"))
  330. for i in curs.fetchall():
  331. curs.execute(db_change(
  332. "update rb set block = ?, band = 'regex' where block = ? and band = 'O'"
  333. ), [
  334. '^' + i[0].replace('.', '\\.'),
  335. i[0]
  336. ])
  337. if ver_num < 3190201:
  338. today_time = get_time()
  339. curs.execute(db_change("select block, end, why, band, login from ban"))
  340. for i in curs.fetchall():
  341. curs.execute(db_change(
  342. "insert into rb (block, end, today, why, band, login, ongoing) " + \
  343. "values (?, ?, ?, ?, ?, ?, ?)"
  344. ), [
  345. i[0],
  346. i[1],
  347. today_time,
  348. i[2],
  349. i[3],
  350. i[4],
  351. '1'
  352. ])
  353. if ver_num < 3191301:
  354. curs.execute(db_change('' + \
  355. 'select id, title, date from history ' + \
  356. 'where not title like "user:%" ' + \
  357. 'order by date desc ' + \
  358. 'limit 50' + \
  359. ''))
  360. data_list = curs.fetchall()
  361. for get_data in data_list:
  362. curs.execute(db_change(
  363. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  364. ), [
  365. get_data[0],
  366. get_data[1],
  367. get_data[2]
  368. ])
  369. if ver_num < 3202400:
  370. curs.execute(db_change("select data from other where name = 'update'"))
  371. get_data = curs.fetchall()
  372. if get_data and get_data[0][0] == 'master':
  373. curs.execute(db_change("update other set data = 'beta' where name = 'update'"), [])
  374. if ver_num < 3202600:
  375. curs.execute(db_change("select name, regex, sub from filter"))
  376. for i in curs.fetchall():
  377. curs.execute(db_change(
  378. "insert into html_filter (html, kind, plus, plus_t) " + \
  379. "values (?, 'regex_filter', ?, ?)"
  380. ), [
  381. i[0],
  382. i[1],
  383. i[2]
  384. ])
  385. curs.execute(db_change("select title, link, icon from inter"))
  386. for i in curs.fetchall():
  387. curs.execute(db_change(
  388. "insert into html_filter (html, kind, plus, plus_t) " + \
  389. "values (?, 'inter_wiki', ?, ?)"), [
  390. i[0],
  391. i[1],
  392. i[2]
  393. ])
  394. if ver_num < 3203400:
  395. curs.execute(db_change("select user, css from custom"))
  396. for i in curs.fetchall():
  397. curs.execute(db_change(
  398. "insert into user_set (name, id, data) values ('custom_css', ?, ?)"
  399. ), [
  400. re.sub(r' \(head\)$', '', i[0]),
  401. i[1]
  402. ])
  403. if ver_num < 3205500:
  404. curs.execute(db_change("select title, decu, dis, view, why from acl"))
  405. for i in curs.fetchall():
  406. curs.execute(db_change(
  407. "insert into acl (title, data, type) values (?, ?, ?)"
  408. ), [i[0], i[1], 'decu'])
  409. curs.execute(db_change(
  410. "insert into acl (title, data, type) values (?, ?, ?)"
  411. ), [i[0], i[2], 'dis'])
  412. curs.execute(db_change(
  413. "insert into acl (title, data, type) values (?, ?, ?)"
  414. ), [i[0], i[3], 'view'])
  415. curs.execute(db_change(
  416. "insert into acl (title, data, type) values (?, ?, ?)"
  417. ), [i[0], i[4], 'why'])
  418. if ver_num < 3300101:
  419. # 캐시 초기화
  420. curs.execute(db_change('delete from cache_data'))
  421. if ver_num < 3300301:
  422. # regex_filter 오류 해결
  423. curs.execute(db_change(
  424. 'delete from html_filter where kind = "regex_filter" and html is null'
  425. ))
  426. if ver_num < 3302302:
  427. # user이랑 user_set 테이블의 통합
  428. curs.execute(db_change('select id, pw, acl, date, encode from user'))
  429. for i in curs.fetchall():
  430. curs.execute(db_change(
  431. "insert into user_set (name, id, data) values (?, ?, ?)"
  432. ), ['pw', i[0], i[1]])
  433. curs.execute(db_change(
  434. "insert into user_set (name, id, data) values (?, ?, ?)"
  435. ), ['acl', i[0], i[2]])
  436. curs.execute(db_change(
  437. "insert into user_set (name, id, data) values (?, ?, ?)"
  438. ), ['date', i[0], i[3]])
  439. curs.execute(db_change(
  440. "insert into user_set (name, id, data) values (?, ?, ?)"
  441. ), ['encode', i[0], i[4]])
  442. if ver_num < 3400101:
  443. # user_set이랑 user_application 테이블의 통합
  444. curs.execute(db_change('' + \
  445. 'select id, pw, date, encode, question, answer, ip, ua, email ' + \
  446. 'from user_application' + \
  447. ''))
  448. for i in curs.fetchall():
  449. sql_data = {}
  450. sql_data['id'] = i[0]
  451. sql_data['pw'] = i[1]
  452. sql_data['date'] = i[2]
  453. sql_data['encode'] = i[3]
  454. sql_data['question'] = i[4]
  455. sql_data['answer'] = i[5]
  456. sql_data['ip'] = i[6]
  457. sql_data['ua'] = i[7]
  458. sql_data['email'] = i[8]
  459. curs.execute(db_change(
  460. "insert into user_set (name, id, data) values (?, ?, ?)"
  461. ), ['application', i[0], json.dumps(sql_data)])
  462. if ver_num < 3500105:
  463. curs.execute(db_change(
  464. 'delete from acl where title like "file:%" and data = "admin" and type like "decu%"'
  465. ))
  466. if ver_num < 3500106:
  467. curs.execute(db_change("select data from other where name = 'domain'"))
  468. db_data = curs.fetchall()
  469. if db_data and db_data[0][0] != '':
  470. db_data = db_data[0][0]
  471. db_data = re.match(r'[^/]+\/\/([^/]+)', db_data)
  472. if db_data:
  473. db_data = db_data.group(1)
  474. curs.execute(db_change(
  475. "update other set data = ? where name = 'domain'"
  476. ), [
  477. db_data
  478. ])
  479. else:
  480. curs.execute(db_change(
  481. "update other set data = '' where name = 'domain'"
  482. ))
  483. if ver_num < 3500107:
  484. db_table_list = get_db_table_list()
  485. for for_a in db_table_list:
  486. for for_b in db_table_list[for_a]:
  487. curs.execute(db_change("update " + for_a + " set " + for_b + " = '' where " + for_b + " is null"))
  488. if ver_num < 3500113:
  489. db_table_list = get_db_table_list()
  490. for for_a in db_table_list:
  491. for for_b in db_table_list[for_a]:
  492. curs.execute(db_change("update " + for_a + " set " + for_b + " = '' where " + for_b + " is null"))
  493. if ver_num < 3500114:
  494. curs.execute(db_change('delete from alarm'))
  495. if ver_num < 3500354:
  496. curs.execute(db_change("select data from other where name = 'robot'"))
  497. db_data = curs.fetchall()
  498. if db_data:
  499. robot_default = '' + \
  500. 'User-agent: *\n' + \
  501. 'Disallow: /\n' + \
  502. 'Allow: /$\n' + \
  503. 'Allow: /image/\n' + \
  504. 'Allow: /views/\n' + \
  505. 'Allow: /w/' + \
  506. ''
  507. if db_data[0][0] == robot_default:
  508. curs.execute(db_change("insert into other (name, data, coverage) values ('robot_default', 'on', '')"))
  509. if ver_num < 3500355:
  510. # other coverage 오류 해결
  511. curs.execute(db_change("update other set coverage = '' where coverage is null"))
  512. if ver_num < 3500358:
  513. curs.execute(db_change("drop index history_index"))
  514. curs.execute(db_change("create index history_index on history (title, ip)"))
  515. if ver_num < 3500360:
  516. # 마지막 편집 따로 기록하도록
  517. # create_data['data_set'] = ['doc_name', 'doc_rev', 'set_name', 'set_data']
  518. print("Update 3500360...")
  519. curs.execute(db_change('delete from data_set where set_name = "last_edit"'))
  520. curs.execute(db_change("select title from data"))
  521. db_data = curs.fetchall()
  522. for for_a in db_data:
  523. curs.execute(db_change("select date from history where title = ? order by date desc limit 1"), [for_a[0]])
  524. db_data_2 = curs.fetchall()
  525. if db_data_2:
  526. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, '', 'last_edit', ?)"), [for_a[0], db_data_2[0][0]])
  527. curs.execute(db_change('delete from acl where title like "file:%" and data = "admin" and type like "decu%"'))
  528. print("Update 3500360 complete")
  529. if ver_num < 3500361:
  530. # curs.execute(db_change('select id from user_set where name = "email" and data = ?'), [user_email])
  531. curs.execute(db_change('select id from user_set where name = "email"'))
  532. for db_data in curs.fetchall():
  533. if ip_or_user(db_data[0]) == 1:
  534. curs.execute(db_change(
  535. 'delete from user_set where id = ? and name = "email"'
  536. ), [db_data[0]])
  537. # create_data['history'] = ['id', 'title', 'data', 'date', 'ip', 'send', 'leng', 'hide', 'type']
  538. # create_data['rc'] = ['id', 'title', 'date', 'type']
  539. if ver_num == 3500362:
  540. curs.execute(db_change("drop index history_index"))
  541. curs.execute(db_change("create index history_index on history (title, ip)"))
  542. if ver_num < 3500365:
  543. curs.execute(db_change("update back set data = '' where data is null"))
  544. if ver_num < 3500371:
  545. curs.execute(db_change("delete from user_notice"))
  546. user_alarm_count = {}
  547. curs.execute(db_change("select name, data, date from alarm"))
  548. for db_data in curs.fetchall():
  549. if db_data[0] in user_alarm_count:
  550. user_alarm_count[db_data[0]] += 1
  551. else:
  552. user_alarm_count[db_data[0]] = 1
  553. curs.execute(db_change(
  554. 'insert into user_notice (id, name, data, date, readme) values (?, ?, ?, ?, "")'
  555. ), [str(user_alarm_count[db_data[0]]), db_data[0], db_data[1], db_data[2]])
  556. if ver_num < 3500372:
  557. # ID 글자 확인 호환용
  558. curs.execute(db_change('insert into html_filter (html, kind, plus, plus_t) values (?, ?, ?, ?)'), [
  559. r'(?:[^A-Za-zㄱ-힣0-9])',
  560. 'name',
  561. '',
  562. ''
  563. ])
  564. conn.commit()
  565. print('Update completed')
  566. def set_init_always(ver_num):
  567. global global_wiki_set
  568. with get_db_connect() as conn:
  569. curs = conn.cursor()
  570. curs.execute(db_change('delete from other where name = "ver"'))
  571. curs.execute(db_change('insert into other (name, data, coverage) values ("ver", ?, "")'), [ver_num])
  572. curs.execute(db_change('delete from alist where name = "owner"'))
  573. curs.execute(db_change('insert into alist (name, acl) values ("owner", "owner")'))
  574. if not os.path.exists(load_image_url()):
  575. os.makedirs(load_image_url())
  576. curs.execute(db_change('select data from other where name = "key"'))
  577. if not curs.fetchall():
  578. curs.execute(db_change('insert into other (name, data, coverage) values ("key", ?, "")'), [load_random_key()])
  579. curs.execute(db_change('select data from other where name = "salt_key"'))
  580. if not curs.fetchall():
  581. curs.execute(db_change('insert into other (name, data, coverage) values ("salt_key", ?, "")'), [load_random_key(4)])
  582. curs.execute(db_change('select data from other where name = "count_all_title"'))
  583. if not curs.fetchall():
  584. curs.execute(db_change('insert into other (name, data, coverage) values ("count_all_title", "0", "")'))
  585. curs.execute(db_change('select data from other where name = "wiki_access_password_need"'))
  586. db_data = curs.fetchall()
  587. if db_data and db_data[0][0] != '':
  588. curs.execute(db_change('select data from other where name = "wiki_access_password"'))
  589. db_data = curs.fetchall()
  590. if db_data:
  591. global_wiki_set['wiki_access_password'] = db_data[0][0]
  592. def set_init():
  593. with get_db_connect() as conn:
  594. curs = conn.cursor()
  595. # 초기값 설정 함수
  596. curs.execute(db_change("select html from html_filter where kind = 'email'"))
  597. if not curs.fetchall():
  598. for i in ['naver.com', 'gmail.com', 'daum.net', 'kakao.com']:
  599. curs.execute(db_change(
  600. "insert into html_filter (html, kind, plus, plus_t) values (?, 'email', '', '')"
  601. ), [i])
  602. curs.execute(db_change("select html from html_filter where kind = 'extension'"))
  603. if not curs.fetchall():
  604. for i in ['jpg', 'jpeg', 'png', 'gif', 'webp']:
  605. curs.execute(db_change(
  606. "insert into html_filter (html, kind, plus, plus_t) values (?, 'extension', '', '')"
  607. ), [i])
  608. curs.execute(db_change(
  609. 'select data from other ' + \
  610. 'where name = "smtp_server" or name = "smtp_port" or name = "smtp_security"'
  611. ))
  612. if not curs.fetchall():
  613. for i in [
  614. ['smtp_server', 'smtp.gmail.com'],
  615. ['smtp_port', '587'],
  616. ['smtp_security', 'starttls']
  617. ]:
  618. curs.execute(db_change("insert into other (name, data, coverage) values (?, ?, '')"), [i[0], i[1]])
  619. curs.execute(db_change('insert into html_filter (html, kind, plus, plus_t) values (?, ?, ?, ?)'), [
  620. r'(?:[^A-Za-zㄱ-힣0-9])',
  621. 'name',
  622. '',
  623. ''
  624. ])
  625. # Func-simple
  626. ## Func-simple-without_DB
  627. def get_default_admin_group():
  628. return ['owner', 'user', 'ban']
  629. def get_default_robots_txt():
  630. data = '' + \
  631. 'User-agent: *\n' + \
  632. 'Disallow: /\n' + \
  633. 'Allow: /$\n' + \
  634. 'Allow: /w/\n' + \
  635. 'Allow: /sitemap.xml$\n' + \
  636. 'Allow: /sitemap_*.xml$' + \
  637. ''
  638. if os.path.exists('sitemap.xml'):
  639. data += '' + \
  640. '\n' + \
  641. 'Sitemap: ' + load_domain('full') + '/sitemap.xml' + \
  642. ''
  643. return data
  644. def load_random_key(long = 128):
  645. return ''.join(
  646. random.choice(
  647. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  648. ) for i in range(long)
  649. )
  650. def http_warning():
  651. return '''
  652. <div id="opennamu_http_warning_text"></div>
  653. <span style="display: none;" id="opennamu_http_warning_text_lang">''' + load_lang('http_warning') + '''</span>
  654. '''
  655. def get_next_page_bottom(link, num, page, end = 50):
  656. list_data = ''
  657. if num == 1:
  658. if len(page) == end:
  659. list_data += '' + \
  660. '<hr class="main_hr">' + \
  661. '<a href="' + link.format(str(num + 1)) + '">(' + load_lang('next') + ')</a>' + \
  662. ''
  663. elif len(page) != end:
  664. list_data += '' + \
  665. '<hr class="main_hr">' + \
  666. '<a href="' + link.format(str(num - 1)) + '">(' + load_lang('previous') + ')</a>' + \
  667. ''
  668. else:
  669. list_data += '' + \
  670. '<hr class="main_hr">' + \
  671. '<a href="' + link.format(str(num - 1)) + '">(' + load_lang('previous') + ')</a> ' + \
  672. '<a href="' + link.format(str(num + 1)) + '">(' + load_lang('next') + ')</a>' + \
  673. ''
  674. return list_data
  675. def next_fix(link, num, page, end = 50):
  676. list_data = ''
  677. if num == 1:
  678. if len(page) == end:
  679. list_data += '' + \
  680. '<hr class="main_hr">' + \
  681. '<a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  682. ''
  683. elif len(page) != end:
  684. list_data += '' + \
  685. '<hr class="main_hr">' + \
  686. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a>' + \
  687. ''
  688. else:
  689. list_data += '' + \
  690. '<hr class="main_hr">' + \
  691. '<a href="' + link + str(num - 1) + '">(' + load_lang('previous') + ')</a> <a href="' + link + str(num + 1) + '">(' + load_lang('next') + ')</a>' + \
  692. ''
  693. return list_data
  694. def leng_check(A, B):
  695. # B -> new
  696. # A -> old
  697. return '0' if A == B else (('-' + str(A - B)) if A > B else ('+' + str(B - A)))
  698. def number_check(data):
  699. try:
  700. int(data)
  701. return data
  702. except:
  703. return '1'
  704. def check_int(data):
  705. try:
  706. int(data)
  707. return data
  708. except:
  709. return ''
  710. def redirect(data = '/'):
  711. return flask.redirect(load_domain('full') + data)
  712. def get_acl_list(type_d = 'normal'):
  713. if type_d == 'user':
  714. return ['', 'user', 'all']
  715. else:
  716. return ['', 'all', 'user', 'admin', 'owner', '50_edit', 'email', 'ban', 'before', '30_day', 'ban_admin', 'not_all']
  717. ## Func-simple-with_DB
  718. def get_user_title_list(ip = ''):
  719. with get_db_connect() as conn:
  720. curs = conn.cursor()
  721. ip = ip_check() if ip == '' else ip
  722. # default
  723. user_title = {
  724. '' : load_lang('default'),
  725. '🌳' : '🌳 namu',
  726. }
  727. # admin
  728. if admin_check('all', None, ip) == 1:
  729. user_title['✅'] = '✅ admin'
  730. curs.execute(db_change('select name from user_set where id = ? and name = ?'), [ip, 'get_🥚'])
  731. if curs.fetchall():
  732. user_title['🥚'] = '🥚 easter_egg'
  733. return user_title
  734. def load_image_url():
  735. with get_db_connect() as conn:
  736. curs = conn.cursor()
  737. curs.execute(db_change('select data from other where name = "image_where"'))
  738. image_where = curs.fetchall()
  739. image_where = image_where[0][0] if image_where else os.path.join('data', 'images')
  740. return image_where
  741. def load_domain(data_type = 'normal'):
  742. with get_db_connect() as conn:
  743. curs = conn.cursor()
  744. domain = ''
  745. if data_type == 'full':
  746. curs.execute(db_change("select data from other where name = 'http_select'"))
  747. db_data = curs.fetchall()
  748. domain += db_data[0][0] if db_data and db_data[0][0] != '' else 'http'
  749. domain += '://'
  750. curs.execute(db_change("select data from other where name = 'domain'"))
  751. db_data = curs.fetchall()
  752. domain += db_data[0][0] if db_data and db_data[0][0] != '' else flask.request.host
  753. else:
  754. curs.execute(db_change("select data from other where name = 'domain'"))
  755. db_data = curs.fetchall()
  756. domain += db_data[0][0] if db_data and db_data[0][0] != '' else flask.request.host
  757. return domain
  758. def get_tool_js_safe(data):
  759. data = data.replace('\n', '\\\\n')
  760. data = data.replace('\\', '\\\\')
  761. data = data.replace("'", "\\'")
  762. data = data.replace('"', '\\"')
  763. return data
  764. def edit_button(ob_name = 'opennamu_edit_textarea', monaco_ob_name = 'opennamu_monaco_editor'):
  765. with get_db_connect() as conn:
  766. curs = conn.cursor()
  767. insert_list = []
  768. curs.execute(db_change("select html, plus from html_filter where kind = 'edit_top'"))
  769. db_data = curs.fetchall()
  770. for get_data in db_data:
  771. insert_list += [[get_data[1], get_data[0]]]
  772. data = ''
  773. for insert_data in insert_list:
  774. data += '<a href="javascript:do_insert_data(\'' + ob_name + '\', \'' + get_tool_js_safe(insert_data[0]) + '\', \'' + monaco_ob_name + '\');">(' + html.escape(insert_data[1]) + ')</a> '
  775. data += (' ' if data != '' else '') + '<a href="/edit_top">(' + load_lang('add') + ')</a>'
  776. data += '<hr class="main_hr">'
  777. return data
  778. def ip_warning():
  779. with get_db_connect() as conn:
  780. curs = conn.cursor()
  781. if ip_or_user() != 0:
  782. curs.execute(db_change('select data from other where name = "no_login_warning"'))
  783. data = curs.fetchall()
  784. if data and data[0][0] != '':
  785. text_data = '' + \
  786. '<span>' + data[0][0] + '</span>' + \
  787. '<hr class="main_hr">' + \
  788. ''
  789. else:
  790. text_data = '' + \
  791. '<span>' + load_lang('no_login_warning') + '</span>' + \
  792. '<hr class="main_hr">' + \
  793. ''
  794. else:
  795. text_data = ''
  796. return text_data
  797. # Func-login
  798. def pw_encode(data, db_data_encode = ''):
  799. with get_db_connect() as conn:
  800. curs = conn.cursor()
  801. if db_data_encode == '':
  802. curs.execute(db_change('select data from other where name = "encode"'))
  803. db_data = curs.fetchall()
  804. db_data_encode = db_data[0][0] if db_data else 'sha3'
  805. if db_data_encode == 'sha256':
  806. return hashlib.sha256(bytes(data, 'utf-8')).hexdigest()
  807. elif db_data_encode == 'sha3':
  808. return hashlib.sha3_256(bytes(data, 'utf-8')).hexdigest()
  809. elif db_data_encode == 'sha3-512':
  810. return hashlib.sha3_512(bytes(data, 'utf-8')).hexdigest()
  811. else:
  812. curs.execute(db_change('select data from other where name = "salt_key"'))
  813. db_data = curs.fetchall()
  814. db_data_salt = db_data[0][0] if db_data else ''
  815. if db_data_encode == 'sha3-salt':
  816. return hashlib.sha3_256(bytes(data + db_data_salt, 'utf-8')).hexdigest()
  817. else:
  818. return hashlib.sha3_512(bytes(data + db_data_salt, 'utf-8')).hexdigest()
  819. def pw_check(data, data2, type_d = 'no', id_d = ''):
  820. with get_db_connect() as conn:
  821. curs = conn.cursor()
  822. curs.execute(db_change('select data from other where name = "encode"'))
  823. db_data = curs.fetchall()
  824. load_set_data = db_data[0][0] if db_data and db_data[0][0] != '' else 'sha3'
  825. set_data = db_data[0][0] if db_data and db_data[0][0] != '' else 'sha3'
  826. if type_d != 'no':
  827. if type_d == '':
  828. set_data = 'sha3'
  829. else:
  830. set_data = type_d
  831. re_data = 1 if pw_encode(data, set_data) == data2 else 0
  832. if load_set_data != set_data and re_data == 1 and id_d != '':
  833. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'pw'"), [
  834. pw_encode(data),
  835. id_d
  836. ])
  837. curs.execute(db_change("update user_set set data = ? where id = ? and name = 'encode'"), [
  838. load_set_data,
  839. id_d
  840. ])
  841. return re_data
  842. # Func-skin
  843. def easy_minify(data, tool = None):
  844. # without_DB
  845. if 'wiki_access_password' in global_wiki_set:
  846. access_password = global_wiki_set['wiki_access_password']
  847. input_password = flask.request.cookies.get('opennamu_wiki_access', ' ')
  848. if url_pas(access_password) == input_password:
  849. return data
  850. return '''
  851. <script src="/views/main_css/js/route/wiki_access_password.js"></script>
  852. <h2>''' + load_lang('error_password_require_for_wiki_access') + '''</h2>
  853. <input type="password" id="wiki_access">
  854. <input type="submit" onclick="opennamu_do_wiki_access();">
  855. '''
  856. else:
  857. return data
  858. def load_lang(data, safe = 0):
  859. global global_lang
  860. with get_db_connect() as conn:
  861. curs = conn.cursor()
  862. ip = ip_check()
  863. if ip_or_user(ip) == 0:
  864. curs.execute(db_change('select data from user_set where name = "lang" and id = ?'), [ip])
  865. rep_data = curs.fetchall()
  866. elif 'lang' in flask.session:
  867. rep_data = [[flask.session['lang']]]
  868. else:
  869. curs.execute(db_change("select data from other where name = 'language'"))
  870. rep_data = curs.fetchall()
  871. if not rep_data or rep_data[0][0] in ('', 'default'):
  872. curs.execute(db_change("select data from other where name = 'language'"))
  873. rep_data = curs.fetchall()
  874. if rep_data:
  875. lang_name = rep_data[0][0]
  876. else:
  877. lang_name = 'en-US'
  878. if lang_name in global_lang:
  879. lang = global_lang[lang_name]
  880. else:
  881. lang_list = os.listdir('lang')
  882. if (lang_name + '.json') in lang_list:
  883. lang = json.loads(open(
  884. os.path.join('lang', lang_name + '.json'),
  885. encoding = 'utf8'
  886. ).read())
  887. global_lang[lang_name] = lang
  888. else:
  889. lang = {}
  890. if data in lang:
  891. if safe == 1:
  892. return lang[data]
  893. else:
  894. return html.escape(lang[data])
  895. return html.escape(data + ' (' + lang_name + ')')
  896. def skin_check(set_n = 0):
  897. with get_db_connect() as conn:
  898. curs = conn.cursor()
  899. # 개편 필요?
  900. skin_list = load_skin('ringo', 1)
  901. skin = skin_list[0]
  902. ip = ip_check()
  903. user_need_skin = ''
  904. if ip_or_user(ip) == 0:
  905. curs.execute(db_change('select data from user_set where name = "skin" and id = ?'), [ip])
  906. skin_exist = curs.fetchall()
  907. if skin_exist:
  908. user_need_skin = skin_exist[0][0]
  909. else:
  910. if 'skin' in flask.session:
  911. user_need_skin = flask.session['skin']
  912. user_need_skin = '' if user_need_skin == 'default' else user_need_skin
  913. if user_need_skin == '':
  914. curs.execute(db_change('select data from other where name = "skin"'))
  915. skin_exist = curs.fetchall()
  916. if skin_exist:
  917. user_need_skin = skin_exist[0][0]
  918. if user_need_skin != '' and user_need_skin in skin_list:
  919. skin = user_need_skin
  920. if set_n == 0:
  921. return './views/' + skin + '/index.html'
  922. else:
  923. return skin
  924. def wiki_css(data):
  925. # without_DB
  926. data += ['' for _ in range(0, 3 - len(data))]
  927. data_css = ''
  928. data_css_ver = '182'
  929. # Func JS + Defer
  930. data_css += '<script src="/views/main_css/js/func/func.js?ver=' + data_css_ver + '"></script>'
  931. data_css += '<script defer src="/views/main_css/js/func/insert_version.js?ver=' + data_css_ver + '"></script>'
  932. data_css += '<script defer src="/views/main_css/js/func/insert_user_info.js?ver=' + data_css_ver + '"></script>'
  933. data_css += '<script defer src="/views/main_css/js/func/insert_version_skin.js?ver=' + data_css_ver + '"></script>'
  934. data_css += '<script defer src="/views/main_css/js/func/insert_http_warning_text.js?ver=' + data_css_ver + '"></script>'
  935. data_css += '<script defer src="/views/main_css/js/func/ie_end_of_life.js?ver=' + data_css_ver + '"></script>'
  936. data_css += '<script defer src="/views/main_css/js/func/shortcut.js?ver=' + data_css_ver + '"></script>'
  937. # Route JS + Defer
  938. # Route JS
  939. data_css += '<script src="/views/main_css/js/route/editor.js?ver=' + data_css_ver + '"></script>'
  940. data_css += '<script src="/views/main_css/js/route/editor_sub.js?ver=' + data_css_ver + '"></script>'
  941. data_css += '<script src="/views/main_css/js/route/render.js?ver=' + data_css_ver + '"></script>'
  942. data_css += '<script src="/views/main_css/js/route/topic.js?ver=' + data_css_ver + '"></script>'
  943. # Main CSS
  944. data_css += '<link rel="stylesheet" href="/views/main_css/css/main.css?ver=' + data_css_ver + '">'
  945. data = data[0:2] + ['', data_css] + data[2:]
  946. return data
  947. def cut_100(data):
  948. # without_DB
  949. if re.search(r'<div class="opennamu_render_complete">', data):
  950. data = re.sub(r'(.|\r|\n)*<div class="opennamu_render_complete">', '', data, 1)
  951. data = data.replace('<br>', ' ')
  952. data = data.replace('\r', '')
  953. data = data.replace('\n', ' ')
  954. data = re.sub(r'<[^<>]+>', ' ', data)
  955. data = data.replace('\n', ' ')
  956. data = re.sub(r' {2,}', ' ', data)
  957. data = re.sub(r'(^ +| +$)', '', data)
  958. data_len = len(data)
  959. if data_len > 100:
  960. return data[0:100]
  961. else:
  962. return data[0:data_len]
  963. else:
  964. return ''
  965. def wiki_set(num = 1):
  966. with get_db_connect() as conn:
  967. curs = conn.cursor()
  968. ip = ip_check()
  969. skin_name = skin_check(1)
  970. data_list = []
  971. curs.execute(db_change('select data from other where name = ?'), ['name'])
  972. db_data = curs.fetchall()
  973. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['Wiki']
  974. curs.execute(db_change('select data from other where name = "license"'))
  975. db_data = curs.fetchall()
  976. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else ['ARR']
  977. data_list += ['', '']
  978. curs.execute(db_change('select data from other where name = "logo" and coverage = ?'), [skin_name])
  979. db_data = curs.fetchall()
  980. if db_data and db_data[0][0] != '':
  981. data_list += [db_data[0][0]]
  982. else:
  983. curs.execute(db_change('select data from other where name = "logo" and coverage = ""'))
  984. db_data = curs.fetchall()
  985. data_list += [db_data[0][0]] if db_data and db_data[0][0] != '' else [data_list[0]]
  986. head_data = ''
  987. curs.execute(db_change("select data from other where name = 'head' and coverage = ''"))
  988. db_data = curs.fetchall()
  989. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  990. curs.execute(db_change("select data from other where name = 'head' and coverage = ?"), [skin_name])
  991. db_data = curs.fetchall()
  992. head_data += db_data[0][0] if db_data and db_data[0][0] != '' else ''
  993. data_list += [head_data]
  994. curs.execute(db_change("select data from other where name = 'top_menu'"))
  995. db_data = curs.fetchall()
  996. db_data = db_data[0][0] if db_data else ''
  997. db_data = db_data.replace('\r', '')
  998. curs.execute(db_change("select data from user_set where name = '' and id = ?"), [ip])
  999. db_data_2 = curs.fetchall()
  1000. db_data_2 = db_data_2[0][0] if db_data_2 else ''
  1001. db_data += db_data_2.replace('\r', '')
  1002. if db_data != '':
  1003. db_data = db_data.split('\n')
  1004. if len(db_data) % 2 != 0:
  1005. db_data += ['']
  1006. db_data = [[db_data[for_a], db_data[for_a + 1]] for for_a in range(0, len(db_data), 2)]
  1007. data_list += [db_data]
  1008. return data_list
  1009. def wiki_custom():
  1010. with get_db_connect() as conn:
  1011. curs = conn.cursor()
  1012. ip = ip_check()
  1013. skin_name = '_' + skin_check(1)
  1014. if ip_or_user(ip) == 0:
  1015. user_icon = 1
  1016. user_name = ip
  1017. if 'head' in flask.session:
  1018. user_head = flask.session['head']
  1019. else:
  1020. curs.execute(db_change("select data from user_set where id = ? and name = 'custom_css'"), [ip])
  1021. db_data = curs.fetchall()
  1022. user_head = db_data[0][0] if db_data else ''
  1023. flask.session['head'] = db_data[0][0] if db_data else ''
  1024. if 'head' + skin_name in flask.session:
  1025. user_head += flask.session['head' + skin_name]
  1026. else:
  1027. curs.execute(db_change("select data from user_set where id = ? and name = ?"), [ip, 'custom_css' + skin_name])
  1028. db_data = curs.fetchall()
  1029. user_head += db_data[0][0] if db_data else ''
  1030. flask.session['head' + skin_name] = db_data[0][0] if db_data else ''
  1031. curs.execute(db_change('select data from user_set where name = "email" and id = ?'), [ip])
  1032. email = curs.fetchall()
  1033. email = email[0][0] if email else ''
  1034. if admin_check('all') == 1:
  1035. user_admin = '1'
  1036. user_acl_list = []
  1037. curs.execute(db_change("select data from user_set where id = ? and name = 'acl'"), [ip])
  1038. curs.execute(db_change('select acl from alist where name = ?'), [curs.fetchall()[0][0]])
  1039. user_acl = curs.fetchall()
  1040. for i in user_acl:
  1041. user_acl_list += [i[0]]
  1042. user_acl_list = user_acl_list if user_acl != [] else '0'
  1043. else:
  1044. user_admin = '0'
  1045. user_acl_list = '0'
  1046. curs.execute(db_change("select count(*) from user_notice where name = ? and readme = ''"), [ip])
  1047. count = curs.fetchall()
  1048. user_notice = str(count[0][0]) if count else '0'
  1049. else:
  1050. user_icon = 0
  1051. user_name = load_lang('user')
  1052. email = ''
  1053. user_admin = '0'
  1054. user_acl_list = '0'
  1055. user_notice = '0'
  1056. user_head = flask.session['head'] if 'head' in flask.session else ''
  1057. user_head += flask.session['head' + skin_name] if 'head' + skin_name in flask.session else ''
  1058. curs.execute(db_change("select title from rd where title = ? and stop = ''"), ['user:' + ip])
  1059. user_topic = '1' if curs.fetchall() else '0'
  1060. split_path = flask.request.path.split('/')
  1061. if len(split_path) > 1:
  1062. split_path = split_path[1]
  1063. else:
  1064. split_path = 0
  1065. return [
  1066. '',
  1067. '',
  1068. user_icon,
  1069. user_head,
  1070. email,
  1071. user_name,
  1072. user_admin,
  1073. str(ban_check()),
  1074. user_notice,
  1075. user_acl_list,
  1076. ip,
  1077. user_topic,
  1078. split_path
  1079. ]
  1080. def load_skin(data = '', set_n = 0, default = 0):
  1081. # without_DB
  1082. # data -> 가장 앞에 있을 스킨 이름
  1083. # set_n == 0 -> 스트링으로 반환
  1084. # set_n == 1 -> 리스트로 반환
  1085. # default == 0 -> 디폴트 미포함
  1086. # default == 1 -> 디폴트 포함
  1087. if set_n == 0:
  1088. skin_return_data = ''
  1089. else:
  1090. skin_return_data = []
  1091. skin_list_get = os.listdir('views')
  1092. if default == 1:
  1093. skin_list_get = ['default'] + skin_list_get
  1094. for skin_data in skin_list_get:
  1095. if skin_data != 'default':
  1096. see_data = skin_data
  1097. else:
  1098. see_data = load_lang('default')
  1099. if skin_data != 'main_css':
  1100. if set_n == 0:
  1101. if skin_data == data:
  1102. skin_return_data = '' + \
  1103. '<option value="' + skin_data + '">' + \
  1104. see_data + \
  1105. '</option>' + \
  1106. '' + skin_return_data
  1107. else:
  1108. skin_return_data += '' + \
  1109. '<option value="' + skin_data + '">' + \
  1110. see_data + \
  1111. '</option>' + \
  1112. ''
  1113. else:
  1114. if skin_data == data:
  1115. skin_return_data = [skin_data] + skin_return_data
  1116. else:
  1117. skin_return_data += [skin_data]
  1118. return skin_return_data
  1119. # Func-markup
  1120. def render_set(doc_name = '', doc_data = '', data_type = 'view', data_in = '', doc_acl = ''):
  1121. with get_db_connect() as conn:
  1122. curs = conn.cursor()
  1123. # data_type in ['view', 'from', 'thread', 'raw', 'api_view', 'api_thread', 'backlink']
  1124. # data_type을 list 형식으로 개편 필요할 듯
  1125. if doc_name != '':
  1126. doc_acl = acl_check(doc_name, 'render') if doc_acl == '' else doc_acl
  1127. else:
  1128. doc_acl = 0
  1129. data_type = 'view' if data_type == '' else data_type
  1130. doc_data = 0 if doc_data == None else doc_data
  1131. acl_dict = {}
  1132. acl_dict[doc_name] = doc_acl
  1133. ip = ip_check()
  1134. if doc_acl == 1:
  1135. return 'HTTP Request 401.3'
  1136. else:
  1137. if data_type == 'raw':
  1138. return doc_data
  1139. if doc_data != 0:
  1140. render_lang_data = {
  1141. 'toc' : load_lang('toc'),
  1142. 'category' : load_lang('category')
  1143. }
  1144. get_class_render = class_do_render(conn, render_lang_data).do_render(doc_name, doc_data, data_type, data_in)
  1145. if 'include' in get_class_render[2]:
  1146. for_a = 0
  1147. while len(get_class_render[2]['include']) > for_a:
  1148. include_data = get_class_render[2]['include'][for_a]
  1149. if include_data[1] in acl_dict:
  1150. acl_result = acl_dict[include_data[1]]
  1151. else:
  1152. acl_result = acl_check(include_data[1], 'render')
  1153. acl_dict[include_data[1]] = acl_result
  1154. if acl_result == 0:
  1155. include_regex = re.compile('<div id="' + include_data[0] + '"><\\/div>')
  1156. if re.search(include_regex, get_class_render[0]):
  1157. include_data_render = class_do_render(conn, render_lang_data).do_render(include_data[1], include_data[2], data_type, include_data[0] + data_in)
  1158. if len(include_data) > 3:
  1159. include_data_render[0] = '<div id="' + include_data[0] + '" ' + include_data[3] + '>' + include_data_render[0] + '</div>'
  1160. else:
  1161. include_data_render[0] = '<div id="' + include_data[0] + '">' + include_data_render[0] + '</div>'
  1162. get_class_render[0] = re.sub(include_regex, include_data_render[0], get_class_render[0])
  1163. get_class_render[1] += include_data_render[1]
  1164. get_class_render[2]['include'] += include_data_render[2]['include']
  1165. for_a += 1
  1166. if data_type == 'backlink':
  1167. return ''
  1168. get_class_render[0] = '<div class="opennamu_render_complete">' + get_class_render[0] + '</div>'
  1169. curs.execute(db_change("select data from other where name = 'namumark_compatible'"))
  1170. db_data = curs.fetchall()
  1171. if db_data and db_data[0][0] != '':
  1172. get_class_render[0] = '''
  1173. <style>
  1174. .opennamu_render_complete {
  1175. font-size: 14.4px !important;
  1176. }
  1177. .opennamu_render_complete td {
  1178. padding: 5px 10px !important;
  1179. }
  1180. .opennamu_render_complete summary {
  1181. list-style: none !important;
  1182. font-weight: bold;
  1183. }
  1184. </style>
  1185. ''' + get_class_render[0]
  1186. table_set_data = get_main_skin_set(curs, flask.session, 'main_css_table_scroll', ip)
  1187. if table_set_data == 'on':
  1188. get_class_render[0] = '<style>.table_safe { overflow-x: scroll; white-space: nowrap; }</style>' + get_class_render[0]
  1189. joke_set_data = get_main_skin_set(curs, flask.session, 'main_css_view_joke', ip)
  1190. if joke_set_data == 'off':
  1191. get_class_render[0] = '<style>.opennamu_joke { display: none; }</style>' + get_class_render[0]
  1192. math_set_data = get_main_skin_set(curs, flask.session, 'main_css_math_scroll', ip)
  1193. if math_set_data == 'on':
  1194. get_class_render[0] = '<style>.katex .base { overflow-x: scroll; }</style>' + get_class_render[0]
  1195. if data_type == 'api_view' or data_type == 'api_thread':
  1196. return [
  1197. get_class_render[0],
  1198. get_class_render[1]
  1199. ]
  1200. else:
  1201. return get_class_render[0] + '<script>' + get_class_render[1] + '</script>'
  1202. else:
  1203. return 'HTTP Request 404'
  1204. def render_simple_set(data):
  1205. # without_DB
  1206. toc_data = ''
  1207. toc_regex = r'<h([1-6])>([^<>]+)<\/h[1-6]>'
  1208. toc_search_data = re.findall(toc_regex, data)
  1209. heading_stack = [0, 0, 0, 0, 0, 0]
  1210. if toc_search_data:
  1211. toc_data += '''
  1212. <div class="opennamu_TOC" id="toc">
  1213. <span class="opennamu_TOC_title">''' + load_lang('toc') + '''</span>
  1214. <br>
  1215. '''
  1216. for toc_search_in in toc_search_data:
  1217. heading_level = int(toc_search_in[0])
  1218. heading_level_str = str(heading_level)
  1219. heading_stack[heading_level - 1] += 1
  1220. for for_a in range(heading_level, 6):
  1221. heading_stack[for_a] = 0
  1222. heading_stack_str = ''.join([str(for_a) + '.' if for_a != 0 else '' for for_a in heading_stack])
  1223. heading_stack_str = re.sub(r'\.$', '', heading_stack_str)
  1224. toc_data += '''
  1225. <br>
  1226. <span class="opennamu_TOC_list">
  1227. ''' + ('<span style="margin-left: 10px;"></span>' * (heading_stack_str.count('.'))) + '''
  1228. <a href="#s-''' + heading_stack_str + '''">''' + heading_stack_str + '''.</a>
  1229. ''' + toc_search_in[1] + '''
  1230. </span>
  1231. '''
  1232. data = re.sub(toc_regex, '<h' + toc_search_in[0] + ' id="s-' + heading_stack_str + '"><a href="#toc">' + heading_stack_str + '.</a> ' + toc_search_in[1] + '</h' + toc_search_in[0] + '>', data, 1)
  1233. if toc_data != '':
  1234. toc_data += '</div>'
  1235. footnote_data = ''
  1236. footnote_regex = r'<sup>((?:(?!<sup>|<\/sup>).)+)<\/sup>'
  1237. footnote_search_data = re.findall(footnote_regex, data)
  1238. footnote_count = 1
  1239. if footnote_search_data:
  1240. footnote_data += '<div class="opennamu_footnote">'
  1241. for footnote_search in footnote_search_data:
  1242. footnote_count_str = str(footnote_count)
  1243. if footnote_count != 1:
  1244. footnote_data += '<br>'
  1245. footnote_data += '<a id="fn-' + footnote_count_str + '" href="#rfn-' + footnote_count_str + '">(' + footnote_count_str + ')</a> ' + footnote_search
  1246. data = re.sub(footnote_regex, '<sup id="rfn-' + footnote_count_str + '"><a href="#fn-' + footnote_count_str + '">(' + footnote_count_str + ')</a></sup>', data, 1)
  1247. footnote_count += 1
  1248. if footnote_data != '':
  1249. footnote_data += '</div>'
  1250. data = toc_data + data + footnote_data
  1251. return data
  1252. # Func-request
  1253. def send_email(who, title, data):
  1254. with get_db_connect() as conn:
  1255. curs = conn.cursor()
  1256. curs.execute(db_change('' + \
  1257. 'select name, data from other ' + \
  1258. 'where name = "smtp_email" or name = "smtp_pass" or name = "smtp_server" or name = "smtp_port" or name = "smtp_security"' + \
  1259. ''))
  1260. rep_data = curs.fetchall()
  1261. smtp_email = ''
  1262. smtp_pass = ''
  1263. smtp_server = ''
  1264. smtp_security = ''
  1265. smtp_port = ''
  1266. smtp = ''
  1267. for i in rep_data:
  1268. if i[0] == 'smtp_email':
  1269. smtp_email = i[1]
  1270. elif i[0] == 'smtp_pass':
  1271. smtp_pass = i[1]
  1272. elif i[0] == 'smtp_server':
  1273. smtp_server = i[1]
  1274. elif i[0] == 'smtp_security':
  1275. smtp_security = i[1]
  1276. elif i[0] == 'smtp_port':
  1277. smtp_port = i[1]
  1278. smtp_port = int(number_check(smtp_port))
  1279. if smtp_security == 'plain':
  1280. smtp = smtplib.SMTP(smtp_server, smtp_port)
  1281. elif smtp_security == 'starttls':
  1282. smtp = smtplib.SMTP(smtp_server, smtp_port)
  1283. smtp.starttls()
  1284. else:
  1285. # if smtp_security == 'tls':
  1286. smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
  1287. domain = load_domain()
  1288. wiki_name = wiki_set()[0]
  1289. msg = email.mime.text.MIMEText(data)
  1290. msg['Subject'] = title
  1291. msg['From'] = wiki_name + ' <noreply@' + domain + '>'
  1292. msg['To'] = who
  1293. try:
  1294. smtp.login(smtp_email, smtp_pass)
  1295. smtp.sendmail('openNAMU@' + domain, who, msg.as_string())
  1296. smtp.quit()
  1297. return 1
  1298. except Exception as e:
  1299. print('----')
  1300. print('Error : email send error')
  1301. print(e)
  1302. return 0
  1303. def captcha_get():
  1304. with get_db_connect() as conn:
  1305. curs = conn.cursor()
  1306. data = ''
  1307. if ip_or_user() != 0:
  1308. curs.execute(db_change('select data from other where name = "recaptcha"'))
  1309. recaptcha = curs.fetchall()
  1310. curs.execute(db_change('select data from other where name = "sec_re"'))
  1311. sec_re = curs.fetchall()
  1312. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  1313. rec_ver = curs.fetchall()
  1314. if recaptcha and recaptcha[0][0] != '' and sec_re and sec_re[0][0] != '':
  1315. if not rec_ver or rec_ver[0][0] == '':
  1316. data += '' + \
  1317. '<script src="https://www.google.com/recaptcha/api.js" async defer></script>' + \
  1318. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  1319. '<hr class="main_hr">' + \
  1320. ''
  1321. elif rec_ver[0][0] == 'v3':
  1322. data += '' + \
  1323. '<script src="https://www.google.com/recaptcha/api.js?render=' + recaptcha[0][0] + '"></script>' + \
  1324. '<input type="hidden" id="g-recaptcha" name="g-recaptcha">' + \
  1325. '<script type="text/javascript">' + \
  1326. 'grecaptcha.ready(function() {' + \
  1327. 'grecaptcha.execute(\'' + recaptcha[0][0] + '\', {action: \'homepage\'}).then(function(token) {' + \
  1328. 'document.getElementById(\'g-recaptcha\').value = token;' + \
  1329. '});' + \
  1330. '});' + \
  1331. '</script>' + \
  1332. ''
  1333. elif rec_ver[0][0] == 'cf':
  1334. data += '' + \
  1335. '<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>' + \
  1336. '<div class="g-recaptcha" data-sitekey="' + recaptcha[0][0] + '"></div>' + \
  1337. '<hr class="main_hr">' + \
  1338. ''
  1339. else:
  1340. # rec_ver[0][0] == 'h'
  1341. data += '''
  1342. <script src="https://js.hcaptcha.com/1/api.js" async defer></script>
  1343. <div class="h-captcha" data-sitekey="''' + recaptcha[0][0] + '''"></div>
  1344. <hr class="main_hr">
  1345. '''
  1346. return data
  1347. def captcha_post(re_data, num = 1):
  1348. with get_db_connect() as conn:
  1349. curs = conn.cursor()
  1350. if num == 1 and ip_or_user() != 0:
  1351. curs.execute(db_change('select data from other where name = "sec_re"'))
  1352. sec_re = curs.fetchall()
  1353. curs.execute(db_change('select data from other where name = "recaptcha_ver"'))
  1354. rec_ver = curs.fetchall()
  1355. if captcha_get() != '':
  1356. if not rec_ver or rec_ver[0][0] in ('', 'v3'):
  1357. data = requests.post(
  1358. 'https://www.google.com/recaptcha/api/siteverify',
  1359. data = {
  1360. "secret" : sec_re[0][0],
  1361. "response" : re_data
  1362. }
  1363. )
  1364. elif rec_ver[0][0] == 'cf':
  1365. data = requests.post(
  1366. 'https://challenges.cloudflare.com/turnstile/v0/siteverify',
  1367. data = {
  1368. "secret" : sec_re[0][0],
  1369. "response" : re_data
  1370. }
  1371. )
  1372. else:
  1373. # rec_ver[0][0] == 'h'
  1374. data = requests.post(
  1375. 'https://hcaptcha.com/siteverify',
  1376. data = {
  1377. "secret" : sec_re[0][0],
  1378. "response" : re_data
  1379. }
  1380. )
  1381. if data.status_code == 200:
  1382. json_data = json.loads(data.text)
  1383. if json_data['success'] != True:
  1384. return 1
  1385. return 0
  1386. # Func-user
  1387. def do_user_name_check(user_name):
  1388. with get_db_connect() as conn:
  1389. curs = conn.cursor()
  1390. # XSS 필터
  1391. if html.escape(user_name) != user_name:
  1392. return 1
  1393. # ID 필터
  1394. curs.execute(db_change('select html from html_filter where kind = "name"'))
  1395. set_d = curs.fetchall()
  1396. for i in set_d:
  1397. check_r = re.compile(i[0], re.I)
  1398. if check_r.search(user_name):
  1399. return 1
  1400. # ID 길이 제한 (128글자)
  1401. if len(user_name) > 128:
  1402. return 1
  1403. return 0
  1404. def get_admin_auth_list(num = None):
  1405. # without_DB
  1406. check = {
  1407. 0 : 'owner',
  1408. 1 : 'ban',
  1409. 2 : 'nothing',
  1410. 3 : 'toron',
  1411. 4 : 'check',
  1412. 5 : 'acl',
  1413. 6 : 'hidel',
  1414. 7 : 'give'
  1415. }
  1416. if not num:
  1417. check = check[0]
  1418. elif num == 'all':
  1419. check = [check[i] for i in check]
  1420. else:
  1421. check = check[num]
  1422. return check
  1423. def get_admin_list(num = None):
  1424. with get_db_connect() as conn:
  1425. curs = conn.cursor()
  1426. if num == 'all':
  1427. curs.execute(db_change(
  1428. "select data from user_set where data != 'user' and name = 'acl'"
  1429. ))
  1430. db_data = curs.fetchall()
  1431. db_data = [db_data_in[0] for db_data_in in db_data] if db_data else []
  1432. return db_data
  1433. else:
  1434. check = get_admin_auth_list(num)
  1435. admin_list = []
  1436. curs.execute(db_change(
  1437. 'select name from alist where acl = ?'
  1438. ), [check])
  1439. db_data = curs.fetchall()
  1440. for db_data_in in db_data:
  1441. curs.execute(db_change(
  1442. "select id from user_set where data = ? and name = 'acl'"
  1443. ), [db_data_in[0]])
  1444. db_data_2 = curs.fetchall()
  1445. admin_list += [db_data_2_in[0] for db_data_2_in in db_data_2] if db_data_2 else []
  1446. return admin_list
  1447. def admin_check(num = None, what = None, name = ''):
  1448. with get_db_connect() as conn:
  1449. curs = conn.cursor()
  1450. ip = ip_check() if name == '' else name
  1451. time_data = get_time()
  1452. pass_ok = 0
  1453. if ip_or_user(ip) == 0:
  1454. curs.execute(db_change(
  1455. "select data from user_set where id = ? and name = 'acl'"
  1456. ), [ip])
  1457. user_auth = curs.fetchall()
  1458. if user_auth:
  1459. user_auth = user_auth[0][0]
  1460. check = get_admin_auth_list(num)
  1461. curs.execute(db_change(
  1462. 'select name from alist where name = ? and acl = "owner"'
  1463. ), [user_auth])
  1464. if curs.fetchall():
  1465. pass_ok = 1
  1466. else:
  1467. if num == 'all':
  1468. curs.execute(db_change(
  1469. 'select name from alist where name = ?'
  1470. ), [user_auth])
  1471. else:
  1472. curs.execute(db_change(
  1473. 'select name from alist where name = ? and acl = ?'
  1474. ), [user_auth, check])
  1475. if curs.fetchall():
  1476. pass_ok = 1
  1477. if pass_ok == 1:
  1478. if what:
  1479. curs.execute(db_change(
  1480. "insert into re_admin (who, what, time) values (?, ?, ?)"
  1481. ), [ip, what, time_data])
  1482. return 1
  1483. return 0
  1484. def acl_check(name = 'test', tool = '', topic_num = '1'):
  1485. with get_db_connect() as conn:
  1486. curs = conn.cursor()
  1487. ip = ip_check()
  1488. get_ban = ban_check()
  1489. if tool == '' and name:
  1490. if acl_check(name, 'render') == 1:
  1491. return 1
  1492. user_page = re.search(r"^user:((?:(?!\/).)*)", name)
  1493. if user_page:
  1494. user_page = user_page.group(1)
  1495. if admin_check(5) == 1:
  1496. return 0
  1497. if get_ban == 1:
  1498. return 1
  1499. curs.execute(db_change(
  1500. "select data from acl where title = ? and type = 'decu'"
  1501. ), [name])
  1502. acl_data = curs.fetchall()
  1503. if acl_data:
  1504. if acl_data[0][0] == 'all':
  1505. return 0
  1506. elif acl_data[0][0] == 'user' and not ip_or_user(ip) == 1:
  1507. return 0
  1508. if ip == user_page and not ip_or_user(ip) == 1:
  1509. return 0
  1510. return 1
  1511. elif tool in ['document_edit', 'document_move', 'document_delete']:
  1512. if acl_check(name, '') == 1:
  1513. return 1
  1514. elif tool in ['bbs_edit', 'bbs_comment']:
  1515. if acl_check(name, 'bbs_view') == 1:
  1516. return 1
  1517. elif tool == 'topic':
  1518. curs.execute(db_change("select title from rd where code = ?"), [topic_num])
  1519. name = curs.fetchall()
  1520. name = name[0][0] if name else 'test'
  1521. if tool in ['topic']:
  1522. end = 3
  1523. elif tool in ['render', 'vote', '', 'document_edit', 'document_move', 'document_delete', 'document_edit', 'bbs_edit', 'bbs_comment']:
  1524. end = 2
  1525. else:
  1526. end = 1
  1527. for i in range(0, end):
  1528. if tool == '':
  1529. if i == 0:
  1530. curs.execute(db_change(
  1531. "select data from acl where title = ? and type = 'decu'"
  1532. ), [name])
  1533. '''
  1534. elif i == 1:
  1535. curs.execute(db_change(
  1536. "select plus from html_filter where kind = 'document'"
  1537. ))
  1538. '''
  1539. else:
  1540. curs.execute(db_change(
  1541. 'select data from other where name = "edit"'
  1542. ))
  1543. num = 5
  1544. elif tool == 'document_move':
  1545. if i == 0:
  1546. curs.execute(db_change(
  1547. "select data from acl where title = ? and type = 'document_move_acl'"
  1548. ), [name])
  1549. else:
  1550. curs.execute(db_change(
  1551. 'select data from other where name = "document_move_acl"'
  1552. ))
  1553. num = 5
  1554. elif tool == 'document_edit':
  1555. if i == 0:
  1556. curs.execute(db_change(
  1557. "select data from acl where title = ? and type = 'document_edit_acl'"
  1558. ), [name])
  1559. else:
  1560. curs.execute(db_change(
  1561. 'select data from other where name = "document_edit_acl"'
  1562. ))
  1563. num = 5
  1564. elif tool == 'document_delete':
  1565. if i == 0:
  1566. curs.execute(db_change(
  1567. "select data from acl where title = ? and type = 'document_delete_acl'"
  1568. ), [name])
  1569. else:
  1570. curs.execute(db_change(
  1571. 'select data from other where name = "document_delete_acl"'
  1572. ))
  1573. num = 5
  1574. elif tool == 'topic':
  1575. if i == 0:
  1576. curs.execute(db_change(
  1577. "select acl from rd where code = ?"
  1578. ), [topic_num])
  1579. elif i == 1:
  1580. curs.execute(db_change(
  1581. "select data from acl where title = ? and type = 'dis'"
  1582. ), [name])
  1583. else:
  1584. curs.execute(db_change(
  1585. 'select data from other where name = "discussion"'
  1586. ))
  1587. num = 3
  1588. elif tool == 'topic_view':
  1589. curs.execute(db_change("select set_data from topic_set where thread_code = ? and set_name = 'thread_view_acl'"), [
  1590. topic_num
  1591. ])
  1592. num = 3
  1593. elif tool == 'upload':
  1594. curs.execute(db_change(
  1595. "select data from other where name = 'upload_acl'"
  1596. ))
  1597. num = 5
  1598. elif tool == 'many_upload':
  1599. curs.execute(db_change(
  1600. "select data from other where name = 'many_upload_acl'"
  1601. ))
  1602. num = 5
  1603. elif tool == 'vote':
  1604. if i == 0:
  1605. curs.execute(db_change(
  1606. 'select acl from vote where id = ? and user = ""'
  1607. ), [topic_num])
  1608. else:
  1609. curs.execute(db_change(
  1610. 'select data from other where name = "vote_acl"'
  1611. ))
  1612. num = None
  1613. elif tool == 'slow_edit':
  1614. curs.execute(db_change(
  1615. 'select data from other where name = "slow_edit_acl"'
  1616. ))
  1617. num = 'all'
  1618. elif tool == 'edit_bottom_compulsion':
  1619. curs.execute(db_change(
  1620. 'select data from other where name = "edit_bottom_compulsion_acl"'
  1621. ))
  1622. num = 'all'
  1623. elif tool == 'bbs_edit':
  1624. if i == 0:
  1625. curs.execute(db_change('select set_data from bbs_set where set_name = "bbs_edit_acl" and set_id = ?'), [name])
  1626. else:
  1627. curs.execute(db_change('select set_data from bbs_set where set_name = "bbs_acl" and set_id = ?'), [name])
  1628. num = 'all'
  1629. elif tool == 'bbs_comment':
  1630. if i == 0:
  1631. curs.execute(db_change('select set_data from bbs_set where set_name = "bbs_comment_acl" and set_id = ?'), [name])
  1632. else:
  1633. curs.execute(db_change('select set_data from bbs_set where set_name = "bbs_acl" and set_id = ?'), [name])
  1634. num = 'all'
  1635. elif tool == 'bbs_view':
  1636. curs.execute(db_change('select set_data from bbs_set where set_name = "bbs_view_acl" and set_id = ?'), [name])
  1637. num = 'all'
  1638. else:
  1639. # tool == 'render'
  1640. if i == 0:
  1641. curs.execute(db_change(
  1642. "select data from acl where title = ? and type = 'view'"
  1643. ), [name])
  1644. else:
  1645. curs.execute(db_change("select data from other where name = 'all_view_acl'"))
  1646. num = 5
  1647. acl_data = curs.fetchall()
  1648. if not acl_data or acl_data[0][0] == '':
  1649. if tool == 'slow_edit' or tool == 'edit_bottom_compulsion':
  1650. acl_data = [['not_all']]
  1651. else:
  1652. acl_data = [['normal']]
  1653. except_ban_tool_list = ['render', 'topic_view', 'bbs_view']
  1654. if acl_data[0][0] != 'normal':
  1655. if not acl_data[0][0] in ['ban', 'ban_admin'] and not tool in except_ban_tool_list:
  1656. if get_ban == 1:
  1657. return 1
  1658. if acl_data[0][0] in ['all', 'ban']:
  1659. return 0
  1660. elif acl_data[0][0] == 'user':
  1661. if ip_or_user(ip) != 1:
  1662. return 0
  1663. elif acl_data[0][0] == 'admin':
  1664. if ip_or_user(ip) != 1:
  1665. if admin_check(num) == 1:
  1666. return 0
  1667. elif acl_data[0][0] == '50_edit':
  1668. if ip_or_user(ip) != 1:
  1669. if admin_check(num) == 1:
  1670. return 0
  1671. else:
  1672. curs.execute(db_change(
  1673. "select count(*) from history where ip = ?"
  1674. ), [ip])
  1675. count = curs.fetchall()
  1676. count = count[0][0] if count else 0
  1677. if count >= 50:
  1678. return 0
  1679. elif acl_data[0][0] == 'before':
  1680. if ip_or_user(ip) != 1:
  1681. if admin_check(num) == 1:
  1682. return 0
  1683. curs.execute(db_change(
  1684. "select ip from history where title = ? and ip = ?"
  1685. ), [name, ip])
  1686. if curs.fetchall():
  1687. return 0
  1688. elif acl_data[0][0] == '30_day' or acl_data[0][0] == '90_day':
  1689. if ip_or_user(ip) != 1:
  1690. if admin_check(num) == 1:
  1691. return 0
  1692. else:
  1693. curs.execute(db_change(
  1694. "select data from user_set where id = ? and name = 'date'"
  1695. ), [ip])
  1696. user_date = curs.fetchall()[0][0]
  1697. if acl_data[0][0] == '30_day':
  1698. time_1 = datetime.datetime.strptime(
  1699. user_date,
  1700. '%Y-%m-%d %H:%M:%S'
  1701. ) + datetime.timedelta(days = 30)
  1702. else:
  1703. time_1 = datetime.datetime.strptime(
  1704. user_date,
  1705. '%Y-%m-%d %H:%M:%S'
  1706. ) + datetime.timedelta(days = 90)
  1707. time_2 = datetime.datetime.strptime(
  1708. get_time(),
  1709. '%Y-%m-%d %H:%M:%S'
  1710. )
  1711. if time_2 > time_1:
  1712. return 0
  1713. elif acl_data[0][0] == 'email':
  1714. if ip_or_user(ip) != 1:
  1715. if admin_check(num) == 1:
  1716. return 0
  1717. else:
  1718. curs.execute(db_change(
  1719. "select data from user_set where id = ? and name = 'email'"
  1720. ), [ip])
  1721. if curs.fetchall():
  1722. return 0
  1723. elif acl_data[0][0] == 'owner':
  1724. if admin_check() == 1:
  1725. return 0
  1726. elif acl_data[0][0] == 'ban_admin':
  1727. if admin_check(1) == 1 or get_ban == 1:
  1728. return 0
  1729. elif acl_data[0][0] == 'not_all':
  1730. return 1
  1731. return 1
  1732. elif i == (end - 1):
  1733. if not tool in except_ban_tool_list:
  1734. if get_ban == 1:
  1735. return 1
  1736. if tool == 'topic':
  1737. curs.execute(db_change(
  1738. "select title from rd where code = ? and stop != ''"
  1739. ), [topic_num])
  1740. if curs.fetchall():
  1741. if admin_check(3, 'topic (code ' + topic_num + ')') == 1:
  1742. return 0
  1743. else:
  1744. return 1
  1745. else:
  1746. return 0
  1747. else:
  1748. return 0
  1749. return 1
  1750. def ban_check(ip = None, tool = ''):
  1751. with get_db_connect() as conn:
  1752. curs = conn.cursor()
  1753. ip = ip_check() if not ip else ip
  1754. tool = '' if not tool else tool
  1755. if admin_check(None, None, ip) == 1:
  1756. return 0
  1757. curs.execute(db_change(
  1758. "update rb set ongoing = '' " + \
  1759. "where end < ? and end != '' and ongoing = '1'"
  1760. ), [get_time()])
  1761. curs.execute(db_change("" + \
  1762. "select login, block from rb " + \
  1763. "where band = 'regex' and ongoing = '1'" + \
  1764. ""))
  1765. regex_d = curs.fetchall()
  1766. for test_r in regex_d:
  1767. g_regex = re.compile(test_r[1])
  1768. if g_regex.search(ip):
  1769. if tool == 'login':
  1770. if test_r[0] != 'O':
  1771. return 1
  1772. else:
  1773. return 1
  1774. curs.execute(db_change("" + \
  1775. "select login from rb " + \
  1776. "where block = ? and band = '' and ongoing = '1'" + \
  1777. "" + \
  1778. ""), [ip])
  1779. ban_d = curs.fetchall()
  1780. if ban_d:
  1781. if tool == 'login':
  1782. if ban_d[0][0] != 'O':
  1783. return 1
  1784. else:
  1785. return 1
  1786. return 0
  1787. def ip_pas(raw_ip, type_data = 0):
  1788. with get_db_connect() as conn:
  1789. curs = conn.cursor()
  1790. end_ip = {}
  1791. return_data = 0
  1792. if type(raw_ip) != type([]):
  1793. get_ip = [raw_ip]
  1794. return_data = 1
  1795. else:
  1796. get_ip = raw_ip
  1797. admin_auth = admin_check(1)
  1798. curs.execute(db_change("select data from other where name = 'ip_view'"))
  1799. db_data = curs.fetchall()
  1800. ip_view = db_data[0][0] if db_data else ''
  1801. ip_view = '' if admin_auth == 1 else ip_view
  1802. curs.execute(db_change("select data from other where name = 'user_name_view'"))
  1803. db_data = curs.fetchall()
  1804. user_name_view = db_data[0][0] if db_data else ''
  1805. user_name_view = '' if admin_auth == 1 else user_name_view
  1806. get_ip = list(set(get_ip))
  1807. for raw_ip in get_ip:
  1808. change_ip = 0
  1809. is_this_ip = ip_or_user(raw_ip)
  1810. if is_this_ip != 0:
  1811. # ip user
  1812. if ip_view != '':
  1813. try:
  1814. ip = ipaddress.ip_address(raw_ip)
  1815. if type(ip) == ipaddress.IPv6Address:
  1816. ip = ip.exploded
  1817. ip = re.sub(r':([^:]*):([^:]*)$', ':*:*', ip)
  1818. else:
  1819. ip = ip.exploded
  1820. ip = re.sub(r'\.([^.]*)\.([^.]*)$', '.*.*', ip)
  1821. # ip = hashlib.sha3_224(bytes(raw_ip, 'utf-8')).hexdigest()
  1822. # ip = ip[0:4] + '-' + ip[4:8] + '-' + ip[8:12] + '-' + ip[12:16]
  1823. change_ip = 1
  1824. except:
  1825. ip = raw_ip
  1826. else:
  1827. ip = raw_ip
  1828. else:
  1829. # not ip user
  1830. if user_name_view != '':
  1831. curs.execute(db_change("select data from user_set where id = ? and name = 'sub_user_name'"), [raw_ip])
  1832. db_data = curs.fetchall()
  1833. if db_data and db_data[0][0] != '':
  1834. ip = db_data[0][0]
  1835. else:
  1836. ip = load_lang('member')
  1837. change_ip = 1
  1838. else:
  1839. curs.execute(db_change('select data from user_set where name = "user_name" and id = ?'), [raw_ip])
  1840. db_data = curs.fetchall()
  1841. ip = db_data[0][0] if db_data and db_data[0][0] != '' else raw_ip
  1842. if type_data == 0 and change_ip == 0:
  1843. if is_this_ip == 0:
  1844. ip = '<a href="/w/' + url_pas('user:' + raw_ip) + '">' + ip + '</a>'
  1845. if admin_check('all', None, raw_ip) == 1:
  1846. ip = '<b>' + ip + '</b>'
  1847. curs.execute(db_change('select data from user_set where name = "user_title" and id = ?'), [raw_ip])
  1848. db_data = curs.fetchall()
  1849. if db_data:
  1850. ip = db_data[0][0] + ip
  1851. if ban_check(raw_ip) == 1:
  1852. ip = '<s>' + ip + '</s>'
  1853. if ban_check(raw_ip, 'login') == 1:
  1854. ip = '<i>' + ip + '</i>'
  1855. ip = ip + ' <a href="/user/' + url_pas(raw_ip) + '">(' + load_lang('tool') + ')</a>'
  1856. end_ip[raw_ip] = ip
  1857. if return_data == 1:
  1858. return end_ip[raw_ip]
  1859. else:
  1860. return end_ip
  1861. # Func-edit
  1862. def get_edit_text_bottom():
  1863. with get_db_connect() as conn:
  1864. curs = conn.cursor()
  1865. b_text = ''
  1866. curs.execute(db_change('select data from other where name = "edit_bottom_text"'))
  1867. db_data= curs.fetchall()
  1868. if db_data and db_data[0][0] != '':
  1869. b_text = '' + \
  1870. db_data[0][0] + \
  1871. '<hr class="main_hr">' + \
  1872. ''
  1873. return b_text
  1874. def get_edit_text_bottom_check_box():
  1875. with get_db_connect() as conn:
  1876. curs = conn.cursor()
  1877. cccb_text = ''
  1878. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  1879. sql_d = curs.fetchall()
  1880. if sql_d and sql_d[0][0] != '':
  1881. cccb_text = '' + \
  1882. '<input type="checkbox" name="copyright_agreement" value="yes"> ' + sql_d[0][0] + \
  1883. '<hr class="main_hr">' + \
  1884. ''
  1885. return cccb_text
  1886. def do_edit_text_bottom_check_box_check(data):
  1887. with get_db_connect() as conn:
  1888. curs = conn.cursor()
  1889. curs.execute(db_change('select data from other where name = "copyright_checkbox_text"'))
  1890. db_data = curs.fetchall()
  1891. if db_data and db_data[0][0] != '':
  1892. if data != 'yes':
  1893. return 1
  1894. return 0
  1895. def do_edit_send_check(data):
  1896. with get_db_connect() as conn:
  1897. curs = conn.cursor()
  1898. curs.execute(db_change('select data from other where name = "edit_bottom_compulsion"'))
  1899. db_data = curs.fetchall()
  1900. if db_data and db_data[0][0] != '':
  1901. if acl_check(None, 'edit_bottom_compulsion') == 1:
  1902. if data == '':
  1903. return 1
  1904. return 0
  1905. def do_edit_slow_check(do_type = 'edit'):
  1906. with get_db_connect() as conn:
  1907. curs = conn.cursor()
  1908. if do_type == 'edit':
  1909. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  1910. else:
  1911. # do_type == 'thread'
  1912. curs.execute(db_change("select data from other where name = 'slow_thread'"))
  1913. slow_edit = curs.fetchall()
  1914. if slow_edit and slow_edit[0][0] != '':
  1915. if acl_check(None, 'slow_edit') == 1:
  1916. slow_edit = int(number_check(slow_edit[0][0]))
  1917. if do_type == 'edit':
  1918. curs.execute(db_change("select date from history where ip = ? order by date desc limit 1"), [ip_check()])
  1919. else:
  1920. curs.execute(db_change("select date from topic where ip = ? order by date desc limit 1"), [ip_check()])
  1921. last_edit_data = curs.fetchall()
  1922. if last_edit_data:
  1923. last_edit_data = int(re.sub(' |:|-', '', last_edit_data[0][0]))
  1924. now_edit_data = int((
  1925. datetime.datetime.now() - datetime.timedelta(seconds = slow_edit)
  1926. ).strftime("%Y%m%d%H%M%S"))
  1927. if last_edit_data > now_edit_data:
  1928. return 1
  1929. return 0
  1930. def do_edit_filter(data):
  1931. with get_db_connect() as conn:
  1932. curs = conn.cursor()
  1933. if admin_check(1) != 1:
  1934. curs.execute(db_change(
  1935. "select plus, plus_t from html_filter where kind = 'regex_filter' and plus != ''"
  1936. ))
  1937. for data_list in curs.fetchall():
  1938. match = re.compile(data_list[0], re.I)
  1939. if match.search(data):
  1940. ban_insert(
  1941. ip_check(),
  1942. '0' if data_list[1] == 'X' else data_list[1],
  1943. 'edit filter',
  1944. None,
  1945. 'tool:edit filter'
  1946. )
  1947. return 1
  1948. return 0
  1949. def do_title_length_check(name, check_type = 'document'):
  1950. with get_db_connect() as conn:
  1951. curs = conn.cursor()
  1952. if check_type == 'topic':
  1953. curs.execute(db_change('select data from other where name = "title_topic_max_length"'))
  1954. db_data = curs.fetchall()
  1955. if db_data and db_data[0][0] != '':
  1956. db_data = int(number_check(db_data[0][0]))
  1957. if len(name) > db_data:
  1958. return 1
  1959. else:
  1960. curs.execute(db_change('select data from other where name = "title_max_length"'))
  1961. db_data = curs.fetchall()
  1962. if db_data and db_data[0][0] != '':
  1963. db_data = int(number_check(db_data[0][0]))
  1964. if len(name) > db_data:
  1965. return 1
  1966. return 0
  1967. # Func-insert
  1968. def do_add_thread(thread_code, thread_data, thread_top = '', thread_id = ''):
  1969. with get_db_connect() as conn:
  1970. curs = conn.cursor()
  1971. if thread_id == '':
  1972. curs.execute(db_change("select id from topic where code = ? order by id + 0 desc limit 1"), [thread_code])
  1973. db_data = curs.fetchall()
  1974. if db_data:
  1975. thread_id = str(int(db_data[0][0]) + 1)
  1976. else:
  1977. thread_id = '1'
  1978. curs.execute(db_change("insert into topic (id, data, date, ip, block, top, code) values (?, ?, ?, ?, ?, '', ?)"), [
  1979. thread_id,
  1980. thread_data,
  1981. get_time(),
  1982. ip_check(),
  1983. thread_top,
  1984. thread_code
  1985. ])
  1986. def do_reload_recent_thread(topic_num, date, name = None, sub = None):
  1987. with get_db_connect() as conn:
  1988. curs = conn.cursor()
  1989. curs.execute(db_change("select code from rd where code = ?"), [topic_num])
  1990. if curs.fetchall():
  1991. curs.execute(db_change("update rd set date = ? where code = ?"), [
  1992. date,
  1993. topic_num
  1994. ])
  1995. else:
  1996. curs.execute(db_change(
  1997. "insert into rd (title, sub, code, date, band, stop, agree, acl) values (?, ?, ?, ?, '', '', '', '')"
  1998. ), [
  1999. name,
  2000. sub,
  2001. topic_num,
  2002. date
  2003. ])
  2004. def add_alarm(to_user, from_user, context):
  2005. with get_db_connect() as conn:
  2006. curs = conn.cursor()
  2007. if to_user != from_user:
  2008. context = from_user + ' | ' + context
  2009. count = '1'
  2010. curs.execute(db_change("select id from user_notice where name = ? order by id + 0 desc"), [to_user])
  2011. db_data = curs.fetchall()
  2012. if db_data:
  2013. count = str(int(db_data[0][0]) + 1)
  2014. curs.execute(db_change(
  2015. 'insert into user_notice (id, name, data, date, readme) values (?, ?, ?, ?, "")'
  2016. ), [count, to_user, context, get_time()])
  2017. def add_user(user_name, user_pw, user_email = '', user_encode = ''):
  2018. with get_db_connect() as conn:
  2019. curs = conn.cursor()
  2020. if user_encode == '':
  2021. user_pw_hash = pw_encode(user_pw)
  2022. curs.execute(db_change('select data from other where name = "encode"'))
  2023. data_encode = curs.fetchall()
  2024. data_encode = data_encode[0][0]
  2025. else:
  2026. user_pw_hash = user_pw
  2027. data_encode = user_encode
  2028. curs.execute(db_change("select id from user_set limit 1"))
  2029. if not curs.fetchall():
  2030. user_auth = 'owner'
  2031. else:
  2032. user_auth = 'user'
  2033. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'pw', ?)"), [
  2034. user_name,
  2035. user_pw_hash
  2036. ])
  2037. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'acl', ?)"), [
  2038. user_name,
  2039. user_auth
  2040. ])
  2041. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'date', ?)"), [
  2042. user_name,
  2043. get_time()
  2044. ])
  2045. curs.execute(db_change("insert into user_set (id, name, data) values (?, 'encode', ?)"), [
  2046. user_name,
  2047. data_encode
  2048. ])
  2049. if user_email != '':
  2050. curs.execute(db_change("insert into user_set (name, id, data) values ('email', ?, ?)"), [
  2051. user_name,
  2052. user_email
  2053. ])
  2054. def ua_plus(u_id, u_ip, u_agent, time):
  2055. with get_db_connect() as conn:
  2056. curs = conn.cursor()
  2057. curs.execute(db_change("select data from other where name = 'ua_get'"))
  2058. rep_data = curs.fetchall()
  2059. if rep_data and rep_data[0][0] != '':
  2060. pass
  2061. else:
  2062. curs.execute(db_change(
  2063. "insert into ua_d (name, ip, ua, today, sub) values (?, ?, ?, ?, '')"
  2064. ), [
  2065. u_id,
  2066. u_ip,
  2067. u_agent,
  2068. time
  2069. ])
  2070. def ban_insert(name, end, why, login, blocker, type_d = None):
  2071. with get_db_connect() as conn:
  2072. curs = conn.cursor()
  2073. now_time = get_time()
  2074. band = type_d if type_d else ''
  2075. curs.execute(db_change(
  2076. "update rb set ongoing = '' where end < ? and end != '' and ongoing = '1'"
  2077. ), [now_time])
  2078. curs.execute(db_change("" + \
  2079. "select block from rb " + \
  2080. "where ((end > ? and end != '') or end = '') and block = ? and " + \
  2081. "band = ? and ongoing = '1'" + \
  2082. ""), [now_time, name, band])
  2083. if curs.fetchall():
  2084. curs.execute(db_change(
  2085. "insert into rb (block, end, today, blocker, why, band) values (?, ?, ?, ?, ?, ?)"
  2086. ), [
  2087. name,
  2088. 'release',
  2089. now_time,
  2090. blocker,
  2091. '',
  2092. band
  2093. ])
  2094. curs.execute(db_change(
  2095. "update rb set ongoing = '' where block = ? and band = ? and ongoing = '1'"
  2096. ), [name, band])
  2097. else:
  2098. login = 'O' if login != '' else ''
  2099. if end != '0':
  2100. end = int(number_check(end))
  2101. time = datetime.datetime.now()
  2102. plus = datetime.timedelta(seconds = end)
  2103. r_time = (time + plus).strftime("%Y-%m-%d %H:%M:%S")
  2104. else:
  2105. r_time = ''
  2106. curs.execute(db_change(
  2107. "insert into rb (block, end, today, blocker, why, band, ongoing, login) " + \
  2108. "values (?, ?, ?, ?, ?, ?, '1', ?)"
  2109. ), [
  2110. name,
  2111. r_time,
  2112. now_time,
  2113. blocker,
  2114. why,
  2115. band,
  2116. login
  2117. ])
  2118. def history_plus(title, data, date, ip, send, leng, t_check = '', mode = ''):
  2119. with get_db_connect() as conn:
  2120. curs = conn.cursor()
  2121. # 여기 좀 느린 듯
  2122. curs.execute(db_change('select data from other where name = "history_recording_off"'))
  2123. db_data = curs.fetchall()
  2124. if db_data and db_data[0][0] != '':
  2125. return 0
  2126. if mode == 'add':
  2127. curs.execute(db_change(
  2128. "select id from history where title = ? order by id + 0 asc limit 1"
  2129. ), [title])
  2130. id_data = curs.fetchall()
  2131. id_data = str(int(id_data[0][0]) - 1) if id_data else '0'
  2132. else:
  2133. curs.execute(db_change(
  2134. "select id from history where title = ? order by id + 0 desc limit 1"
  2135. ), [title])
  2136. id_data = curs.fetchall()
  2137. id_data = str(int(id_data[0][0]) + 1) if id_data else '1'
  2138. mode = mode if not re.search('^user:', title) else 'user'
  2139. send = re.sub(r'\(|\)|<|>', '', send)
  2140. send = send[:128] if len(send) > 128 else send
  2141. send = send + ' (' + t_check + ')' if t_check != '' else send
  2142. if mode != 'add' and mode != 'user':
  2143. curs.execute(db_change("select count(*) from rc where type = 'normal'"))
  2144. if curs.fetchall()[0][0] >= 200:
  2145. curs.execute(db_change(
  2146. "select id, title from rc where type = 'normal' order by date asc limit 1"
  2147. ))
  2148. rc_data = curs.fetchall()
  2149. if rc_data:
  2150. curs.execute(db_change(
  2151. 'delete from rc where id = ? and title = ? and type = "normal"'
  2152. ), [
  2153. rc_data[0][0],
  2154. rc_data[0][1]
  2155. ])
  2156. curs.execute(db_change(
  2157. "insert into rc (id, title, date, type) values (?, ?, ?, 'normal')"
  2158. ), [
  2159. id_data,
  2160. title,
  2161. date
  2162. ])
  2163. if mode != 'add':
  2164. curs.execute(db_change("select count(*) from rc where type = ?"), [mode])
  2165. if curs.fetchall()[0][0] >= 200:
  2166. curs.execute(db_change(
  2167. "select id, title from rc where type = ? order by date asc limit 1"
  2168. ), [mode])
  2169. rc_data = curs.fetchall()
  2170. if rc_data:
  2171. curs.execute(db_change(
  2172. 'delete from rc where id = ? and title = ? and type = ?'
  2173. ), [
  2174. rc_data[0][0],
  2175. rc_data[0][1],
  2176. mode
  2177. ])
  2178. curs.execute(db_change(
  2179. "insert into rc (id, title, date, type) values (?, ?, ?, ?)"
  2180. ), [
  2181. id_data,
  2182. title,
  2183. date,
  2184. mode
  2185. ])
  2186. curs.execute(db_change(
  2187. "insert into history (id, title, data, date, ip, send, leng, hide, type) " + \
  2188. "values (?, ?, ?, ?, ?, ?, ?, '', ?)"
  2189. ), [
  2190. id_data,
  2191. title,
  2192. data,
  2193. date,
  2194. ip,
  2195. send,
  2196. leng,
  2197. mode
  2198. ])
  2199. data_set_exist = '' if t_check != 'delete' else '1'
  2200. curs.execute(db_change("select doc_name from data_set where doc_name = ? and set_name = 'last_edit'"), [title])
  2201. db_data = curs.fetchall()
  2202. if db_data:
  2203. curs.execute(db_change("update data_set set set_data = ?, doc_rev = ? where doc_name = ? and set_name = 'last_edit'"), [date, data_set_exist, title])
  2204. else:
  2205. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'last_edit', ?)"), [title, data_set_exist, date])
  2206. curs.execute(db_change("select doc_name from data_set where doc_name = ? and set_name = 'length'"), [title])
  2207. db_data = curs.fetchall()
  2208. if db_data:
  2209. curs.execute(db_change("update data_set set set_data = ?, doc_rev = ? where doc_name = ? and set_name = 'length'"), [len(data), data_set_exist, title])
  2210. else:
  2211. curs.execute(db_change("insert into data_set (doc_name, doc_rev, set_name, set_data) values (?, ?, 'length', ?)"), [title, data_set_exist, len(data)])
  2212. # Func-error
  2213. def re_error(data):
  2214. with get_db_connect() as conn:
  2215. curs = conn.cursor()
  2216. if data == '/ban':
  2217. if ban_check() == 1:
  2218. end = '<div id="opennamu_get_user_info">' + ip_check() + '</div>'
  2219. else:
  2220. end = '<ul class="opennamu_ul"><li>' + load_lang('authority_error') + '</li></ul>'
  2221. return easy_minify(flask.render_template(skin_check(),
  2222. imp = [load_lang('error'), wiki_set(), wiki_custom(), wiki_css([0, 0])],
  2223. data = '<h2>' + load_lang('error') + '</h2>' + end,
  2224. menu = 0
  2225. )), 401
  2226. else:
  2227. num = int(number_check(data.replace('/error/', '')))
  2228. if num == 1:
  2229. data = load_lang('no_login_error')
  2230. elif num == 2:
  2231. data = load_lang('no_exist_user_error')
  2232. elif num == 3:
  2233. data = load_lang('authority_error')
  2234. elif num == 4:
  2235. data = load_lang('no_admin_block_error')
  2236. elif num == 5:
  2237. data = load_lang('error_skin_set')
  2238. elif num == 6:
  2239. data = load_lang('same_id_exist_error')
  2240. elif num == 8:
  2241. data = load_lang('long_id_error') + '<br>' + load_lang('id_char_error') + ' <a href="/name_filter">(' + load_lang('id_filter_list') + ')</a>'
  2242. elif num == 9:
  2243. data = load_lang('file_exist_error')
  2244. elif num == 10:
  2245. data = load_lang('password_error')
  2246. elif num == 11:
  2247. data = load_lang('topic_long_error')
  2248. elif num == 12:
  2249. data = load_lang('email_error')
  2250. elif num == 13:
  2251. data = load_lang('recaptcha_error')
  2252. elif num == 14:
  2253. data = load_lang('file_extension_error') + ' <a href="/extension_filter">(' + load_lang('extension_filter_list') + ')</a>'
  2254. elif num == 15:
  2255. data = load_lang('edit_record_error')
  2256. elif num == 16:
  2257. data = load_lang('same_file_error')
  2258. elif num == 17:
  2259. curs.execute(db_change('select data from other where name = "upload"'))
  2260. db_data = curs.fetchall()
  2261. file_max = number_check(db_data[0][0]) if db_data and db_data[0][0] != '' else '2'
  2262. data = load_lang('file_capacity_error') + file_max
  2263. elif num == 18:
  2264. data = load_lang('email_send_error')
  2265. elif num == 19:
  2266. data = load_lang('move_error')
  2267. elif num == 20:
  2268. data = load_lang('password_diffrent_error')
  2269. elif num == 21:
  2270. data = load_lang('edit_filter_error')
  2271. elif num == 22:
  2272. data = load_lang('file_name_error')
  2273. elif num == 23:
  2274. data = load_lang('regex_error')
  2275. elif num == 24:
  2276. curs.execute(db_change("select data from other where name = 'slow_edit'"))
  2277. db_data = curs.fetchall()
  2278. db_data = '' if not db_data else db_data[0][0]
  2279. data = load_lang('fast_edit_error') + db_data
  2280. elif num == 25:
  2281. data = load_lang('too_many_dec_error')
  2282. elif num == 26:
  2283. data = load_lang('application_not_found')
  2284. elif num == 27:
  2285. data = load_lang("invalid_password_error")
  2286. elif num == 28:
  2287. data = load_lang('watchlist_overflow_error')
  2288. elif num == 29:
  2289. data = load_lang('copyright_disagreed')
  2290. elif num == 30:
  2291. data = load_lang('ie_wrong_callback')
  2292. elif num == 33:
  2293. data = load_lang('restart_fail_error')
  2294. elif num == 34:
  2295. data = load_lang("update_error") + ' <a href="https://github.com/opennamu/opennamu">(Github)</a>'
  2296. elif num == 35:
  2297. data = load_lang('same_email_error')
  2298. elif num == 36:
  2299. data = load_lang('input_email_error')
  2300. elif num == 37:
  2301. data = load_lang('error_edit_send_request')
  2302. elif num == 38:
  2303. curs.execute(db_change("select data from other where name = 'title_max_length'"))
  2304. db_data = curs.fetchall()
  2305. db_data = '' if not db_data else db_data[0][0]
  2306. data = load_lang('error_title_length_too_long') + db_data
  2307. elif num == 39:
  2308. curs.execute(db_change("select data from other where name = 'title_topic_max_length'"))
  2309. db_data = curs.fetchall()
  2310. db_data = '' if not db_data else db_data[0][0]
  2311. data = load_lang('error_title_length_too_long') + db_data
  2312. elif num == 40:
  2313. curs.execute(db_change("select data from other where name = 'password_min_length'"))
  2314. db_data = curs.fetchall()
  2315. password_min_length = '' if not db_data else db_data[0][0]
  2316. data = load_lang('error_password_length_too_short') + password_min_length
  2317. elif num == 41:
  2318. curs.execute(db_change("select data from other where name = 'edit_timeout'"))
  2319. db_data = curs.fetchall()
  2320. db_data = '' if not db_data else db_data[0][0]
  2321. data = load_lang('timeout_error') + db_data
  2322. elif num == 42:
  2323. curs.execute(db_change("select data from other where name = 'slow_thread'"))
  2324. db_data = curs.fetchall()
  2325. db_data = '' if not db_data else db_data[0][0]
  2326. data = load_lang('fast_edit_error') + db_data
  2327. else:
  2328. data = '???'
  2329. if num == 5:
  2330. if flask.request.path != '/skin_set':
  2331. data += '<br>' + load_lang('error_skin_set_old') + ' <a href="/skin_set">(' + load_lang('go') + ')</a>'
  2332. title = load_lang('skin_set')
  2333. tool = [['change', load_lang('user_setting')], ['change/skin_set/main', load_lang('main_skin_set')]]
  2334. load_skin_set = ''
  2335. return easy_minify(flask.render_template(skin_check(),
  2336. imp = [title, wiki_set(), wiki_custom(), wiki_css([0, 0])],
  2337. data = '' + \
  2338. '<div id="main_skin_set">' + \
  2339. '<h2>' + load_lang('error') + '</h2>' + \
  2340. '<ul class="opennamu_ul">' + \
  2341. '<li>' + data + '</a></li>' + \
  2342. '</ul>' + \
  2343. '</div>' + \
  2344. load_skin_set,
  2345. menu = tool
  2346. ))
  2347. else:
  2348. return easy_minify(flask.render_template(skin_check(),
  2349. imp = [load_lang('error'), wiki_set(), wiki_custom(), wiki_css([0, 0])],
  2350. data = '' + \
  2351. '<h2>' + load_lang('error') + '</h2>' + \
  2352. '<ul class="opennamu_ul">' + \
  2353. '<li>' + data + '</li>' + \
  2354. '</ul>' + \
  2355. '',
  2356. menu = 0
  2357. )), 400