func.py 103 KB

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