func.py 102 KB

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