view_w.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. from .tool.func import *
  2. def view_w(name = 'Test', do_type = ''):
  3. with get_db_connect() as conn:
  4. curs = conn.cursor()
  5. sub = 0
  6. history_color = 0
  7. menu = []
  8. user_doc = ''
  9. category_total = ''
  10. file_data = ''
  11. doc_type = ''
  12. redirect_to = None
  13. now_time = get_time()
  14. ip = ip_check()
  15. uppage = re.sub(r"/([^/]+)$", '', name)
  16. uppage = 0 if uppage == name else uppage
  17. curs.execute(db_change("select sub from rd where title = ? and not stop = 'O' order by date desc"), [name])
  18. topic = 1 if curs.fetchall() else 0
  19. curs.execute(db_change("select title from data where title like ?"), [name + '/%'])
  20. down = 1 if curs.fetchall() else 0
  21. if re.search(r'^category:', name):
  22. name_view = name
  23. doc_type = 'category'
  24. category_doc = ''
  25. category_sub = ''
  26. count_sub_category = 0
  27. count_category = 0
  28. curs.execute(db_change("select distinct link from back where title = ? and type = 'cat' order by link asc"), [name])
  29. category_sql = curs.fetchall()
  30. for data in category_sql:
  31. link_view = data[0]
  32. if get_main_skin_set(conn, flask.session, 'main_css_category_change_title', ip) != 'off':
  33. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_view' limit 1"), [name, data[0]])
  34. db_data = curs.fetchall()
  35. if db_data and db_data[0][0] != '':
  36. link_view = db_data[0][0]
  37. link_blur = ''
  38. curs.execute(db_change("select data from back where title = ? and link = ? and type = 'cat_blur' limit 1"), [name, data[0]])
  39. db_data = curs.fetchall()
  40. if db_data:
  41. link_blur = 'opennamu_category_blur'
  42. if data[0].startswith('category:'):
  43. category_sub += '<li><a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a></li>'
  44. count_sub_category += 1
  45. else:
  46. category_doc += '' + \
  47. '<li>' + \
  48. '<a class="' + link_blur + '" href="/w/' + url_pas(data[0]) + '">' + html.escape(link_view) + '</a> ' + \
  49. '<a class="opennamu_link_inter" href="/xref/' + url_pas(data[0]) + '">(' + get_lang(conn, 'backlink') + ')</a>' + \
  50. '</li>' + \
  51. ''
  52. count_category += 1
  53. if category_sub != '':
  54. category_total += '' + \
  55. '<h2 id="cate_under">' + get_lang(conn, 'under_category') + '</h2>' + \
  56. '<ul class="opennamu_ul">' + \
  57. '<li>' + get_lang(conn, 'all') + ' : ' + str(count_sub_category) + '</li>' + \
  58. category_sub + \
  59. '</ul>' + \
  60. ''
  61. if category_doc != '':
  62. category_total += '' + \
  63. '<h2 id="cate_normal">' + get_lang(conn, 'category_title') + '</h2>' + \
  64. '<ul class="opennamu_ul">' + \
  65. '<li>' + get_lang(conn, 'all') + ' : ' + str(count_category) + '</li>' + \
  66. category_doc + \
  67. '</ul>' + \
  68. ''
  69. elif re.search(r"^user:([^/]*)", name):
  70. name_view = name
  71. doc_type = 'user'
  72. user_name = ''
  73. match = re.search(r"^user:([^/]*)", name)
  74. if match:
  75. user_name = html.escape(match.group(1))
  76. user_doc = ''
  77. # S admin or owner 특수 틀 추가
  78. if admin_check(conn, 'all', None, user_name) == 1:
  79. if admin_check(conn, None, None, user_name) == 1:
  80. curs.execute(db_change('select data from other where name = "phrase_user_page_owner"'))
  81. db_data = curs.fetchall()
  82. if db_data and db_data[0][0] != '':
  83. user_doc += db_data[0][0] + '<br>'
  84. else:
  85. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  86. db_data = curs.fetchall()
  87. if db_data and db_data[0][0] != '':
  88. user_doc += db_data[0][0] + '<br>'
  89. else:
  90. curs.execute(db_change('select data from other where name = "phrase_user_page_admin"'))
  91. db_data = curs.fetchall()
  92. if db_data and db_data[0][0] != '':
  93. user_doc += db_data[0][0] + '<br>'
  94. # E
  95. user_doc += '''
  96. <div id="opennamu_get_user_info">''' + html.escape(user_name) + '''</div>
  97. <hr class="main_hr">
  98. '''
  99. if name == 'user:' + user_name:
  100. menu += [['w/' + url_pas(name) + '/' + url_pas(now_time.split()[0]), get_lang(conn, 'today_doc')]]
  101. elif re.search(r"^file:", name):
  102. curs.execute(db_change('select id from history where title = ? order by date desc limit 1'), [name])
  103. db_data = curs.fetchall()
  104. rev = db_data[0][0] if db_data else '1'
  105. name_view = name
  106. doc_type = 'file'
  107. mime_type = re.search(r'([^.]+)$', name)
  108. if mime_type:
  109. mime_type = mime_type.group(1)
  110. else:
  111. mime_type = 'jpg'
  112. file_name = re.sub(r'\.([^.]+)$', '', name)
  113. file_name = re.sub(r'^file:', '', file_name)
  114. file_all_name = sha224_replace(file_name) + '.' + mime_type
  115. file_path_name = os.path.join(load_image_url(conn), file_all_name)
  116. if os.path.exists(file_path_name):
  117. try:
  118. img = Image.open(file_path_name)
  119. width, height = img.size
  120. file_res = str(width) + 'x' + str(height)
  121. except:
  122. file_res = 'Vector'
  123. file_size = str(round(os.path.getsize(file_path_name) / 1000, 1))
  124. file_data = '''
  125. <img src="/image/''' + url_pas(file_all_name) + '''.cache_v''' + rev + '''">
  126. <h2>''' + get_lang(conn, 'data') + '''</h2>
  127. <table>
  128. <tr><td>''' + get_lang(conn, 'url') + '''</td><td><a href="/image/''' + url_pas(file_all_name) + '''">''' + get_lang(conn, 'link') + '''</a></td></tr>
  129. <tr><td>''' + get_lang(conn, 'volume') + '''</td><td>''' + file_size + '''KB</td></tr>
  130. <tr><td>''' + get_lang(conn, 'resolution') + '''</td><td>''' + file_res + '''</td></tr>
  131. </table>
  132. <h2>''' + get_lang(conn, 'content') + '''</h2>
  133. '''
  134. menu += [['delete_file/' + url_pas(name), get_lang(conn, 'file_delete')]]
  135. else:
  136. file_data = ''
  137. else:
  138. curs.execute(db_change("select link from back where title = ? and type = 'include' limit 1"), [name])
  139. doc_type = 'include' if curs.fetchall() else doc_type
  140. curs.execute(db_change("select title, data from back where link = ? and type = 'redirect' limit 1"), [name])
  141. db_data = curs.fetchall()
  142. if db_data:
  143. doc_type = 'redirect'
  144. curs.execute(db_change("select title from data where title = ?"), [db_data[0][0]])
  145. if curs.fetchall():
  146. redirect_to = url_pas(db_data[0][0]) + db_data[0][1]
  147. name_view = name
  148. end_data = '''
  149. <div id="opennamu_preview_area">
  150. <textarea id="opennamu_editor_doc_name" style="display: none;">''' + html.escape(name) + '''</textarea>
  151. <script defer src="/views/main_css/js/route/w.js''' + cache_v() + '''"></script>
  152. <script>window.addEventListener("DOMContentLoaded", function() { opennamu_w("''' + ('from' if do_type == 'from' else '') + '''"); });</script>
  153. </div>
  154. '''
  155. curs.execute(db_change("select data from data where title = ?"), [name])
  156. data = curs.fetchall()
  157. description = ''
  158. if acl_check(conn, name, 'render') == 1:
  159. response_data = 401
  160. curs.execute(db_change('select data from other where name = "error_401"'))
  161. sql_d = curs.fetchall()
  162. if sql_d and sql_d[0][0] != '':
  163. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul class="opennamu_ul"><li>' + sql_d[0][0] + '</li></ul>'
  164. else:
  165. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul class="opennamu_ul"><li>' + get_lang(conn, 'authority_error') + '</li></ul>'
  166. elif not data:
  167. response_data = 404
  168. curs.execute(db_change('select data from other where name = "error_404"'))
  169. db_data = curs.fetchall()
  170. if db_data and db_data[0][0] != '':
  171. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul class="opennamu_ul"><li>' + db_data[0][0] + '</li></ul>'
  172. else:
  173. end_data = '<h2>' + get_lang(conn, 'error') + '</h2><ul class="opennamu_ul"><li>' + get_lang(conn, 'decument_404_error') + '</li></ul>'
  174. curs.execute(db_change('select ip from history where title = ? limit 1'), [name])
  175. db_data = curs.fetchall()
  176. history_color = 1 if db_data else 0
  177. else:
  178. response_data = 200
  179. description = data[0][0].replace('\r', '').replace('\n', ' ')[0:200]
  180. curs.execute(db_change("select title from acl where title = ?"), [name])
  181. acl = 1 if curs.fetchall() else 0
  182. menu_acl = 1 if acl_check(conn, name, 'document_edit') == 1 else 0
  183. if response_data == 404:
  184. menu += [['edit/' + url_pas(name), get_lang(conn, 'create'), menu_acl]]
  185. else:
  186. menu += [['edit/' + url_pas(name), get_lang(conn, 'edit'), menu_acl]]
  187. menu += [
  188. ['topic/' + url_pas(name), get_lang(conn, 'discussion'), topic],
  189. ['history/' + url_pas(name), get_lang(conn, 'history'), history_color],
  190. ['xref/' + url_pas(name), get_lang(conn, 'backlink')],
  191. ['acl/' + url_pas(name), get_lang(conn, 'setting'), acl],
  192. ]
  193. if flask.session and 'lastest_document' in flask.session:
  194. if type(flask.session['lastest_document']) != type([]):
  195. flask.session['lastest_document'] = []
  196. else:
  197. flask.session['lastest_document'] = []
  198. if do_type == 'from':
  199. menu += [['w/' + url_pas(name), get_lang(conn, 'pass')]]
  200. last_page = ''
  201. for for_a in reversed(range(0, len(flask.session['lastest_document']))):
  202. last_page = flask.session['lastest_document'][for_a]
  203. curs.execute(db_change("select link from back where (title = ? or link = ?) and type = 'redirect' limit 1"), [last_page, last_page])
  204. if curs.fetchall():
  205. break
  206. if last_page != name:
  207. redirect_text = '{0} ➤ {1}'
  208. curs.execute(db_change('select data from other where name = "redirect_text"'))
  209. db_data = curs.fetchall()
  210. if db_data and db_data[0][0] != '':
  211. redirect_text = db_data[0][0]
  212. try:
  213. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  214. except:
  215. redirect_text = '{0} ➤ {1}'
  216. redirect_text = redirect_text.format('<a href="/w_from/' + url_pas(last_page) + '">' + html.escape(last_page) + '</a>', '<b>' + html.escape(name) + '</b>')
  217. end_data = '''
  218. <div class="opennamu_redirect" id="redirect">
  219. ''' + redirect_text + '''
  220. </div>
  221. <hr class="main_hr">
  222. ''' + end_data
  223. if len(flask.session['lastest_document']) >= 10:
  224. flask.session['lastest_document'] = flask.session['lastest_document'][-9:] + [name]
  225. else:
  226. flask.session['lastest_document'] += [name]
  227. flask.session['lastest_document'] = list(reversed(dict.fromkeys(reversed(flask.session['lastest_document']))))
  228. if redirect_to and do_type != 'from':
  229. return redirect(conn, '/w_from/' + redirect_to)
  230. view_history_on = get_main_skin_set(conn, flask.session, 'main_css_view_history', ip)
  231. if view_history_on == 'on':
  232. end_data = '' + \
  233. '<div class="opennamu_trace">' + \
  234. '<a class="opennamu_trace_button" href="javascript:opennamu_do_trace_spread();"> (+)</a>' + \
  235. get_lang(conn, 'trace') + ' : ' + \
  236. ' ← '.join(
  237. [
  238. '<a href="/w/' + url_pas(for_a) + '">' + html.escape(for_a) + '</a>'
  239. for for_a in reversed(flask.session['lastest_document'])
  240. ]
  241. ) + \
  242. '</div>' + \
  243. '<hr class="main_hr">' + \
  244. '' + end_data
  245. if uppage != 0:
  246. menu += [['w/' + url_pas(uppage), get_lang(conn, 'upper')]]
  247. if down:
  248. menu += [['down/' + url_pas(name), get_lang(conn, 'sub')]]
  249. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'last_edit'"), [name])
  250. r_date = curs.fetchall()
  251. r_date = r_date[0][0] if r_date else 0
  252. div = file_data + user_doc + end_data + category_total
  253. if doc_type == '':
  254. curs.execute(db_change('select data from other where name = "outdated_doc_warning_date"'))
  255. db_data = curs.fetchall()
  256. if db_data and db_data[0][0] != '' and r_date != 0:
  257. time_1 = datetime.datetime.strptime(r_date, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(days = int(number_check(db_data[0][0])))
  258. time_2 = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
  259. if time_2 > time_1:
  260. curs.execute(db_change('select data from other where name = "outdated_doc_warning"'))
  261. db_data = curs.fetchall()
  262. div = (db_data[0][0] if db_data and db_data[0][0] != '' else get_lang(conn, 'old_page_warning')) + '<hr class="main_hr">' + div
  263. curs.execute(db_change("select data from other where name = 'body'"))
  264. body = curs.fetchall()
  265. div = (body[0][0] + div) if body else div
  266. curs.execute(db_change("select data from other where name = 'bottom_body'"))
  267. body = curs.fetchall()
  268. div += body[0][0] if body else ''
  269. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'document_top'"), [name])
  270. body = curs.fetchall()
  271. div = (body[0][0] + div) if body else div
  272. if ip_or_user(ip) == 0:
  273. curs.execute(db_change("select data from user_set where id = ? and data = ?"), [ip, name])
  274. watch_list = 2 if curs.fetchall() else 1
  275. menu += [['star_doc_from/' + url_pas(name), ('☆' if watch_list == 1 else '★'), watch_list - 1]]
  276. else:
  277. watch_list = 0
  278. menu += [['doc_watch_list/1/' + url_pas(name), get_lang(conn, 'watchlist')]]
  279. return easy_minify(conn, flask.render_template(skin_check(conn),
  280. imp = [name_view, wiki_set(conn), wiki_custom(conn), wiki_css([sub, r_date, watch_list, description])],
  281. data = div,
  282. menu = menu
  283. )), response_data