func.py 95 KB

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