func.py 102 KB

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