go_api_topic.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from .tool.func import *
  2. def api_topic_thread_make(user_id, date, data, code, color = '', blind = '', add_style = '', admin_check = 1, topic_num = ''):
  3. if blind == 'O':
  4. if data == '':
  5. color_b = 'opennamu_comment_blind'
  6. else:
  7. color_b = 'opennamu_comment_blind_admin'
  8. class_b = 'opennamu_comment_blind_js'
  9. else:
  10. color_b = 'opennamu_comment_blind_not'
  11. class_b = ''
  12. admin_check_box = ''
  13. if admin_check == 1 and topic_num != '':
  14. admin_check_box = '<input type="checkbox" class="opennamu_blind_button" id="opennamu_blind_' + topic_num + '_' + code + '">'
  15. return '''
  16. <span class="''' + class_b + '''">
  17. <table class="opennamu_comment" style="''' + add_style + '''">
  18. <tr>
  19. <td class="opennamu_comment_color_''' + color + '''">
  20. ''' + admin_check_box + '''
  21. <a href="#thread_shortcut" id="''' + code + '''">#''' + code + '''</a>
  22. ''' + user_id + '''
  23. <span style="float: right;">''' + date + '''</span>
  24. </td>
  25. </tr>
  26. <tr>
  27. <td class="''' + color_b + ''' opennamu_comment_data_main" id="thread_''' + code + '''">
  28. ''' + data + '''
  29. <div id="opennamu_topic_req_''' + code + '''"></div>
  30. </td>
  31. </tr>
  32. </table>
  33. <hr class="main_hr">
  34. </span>
  35. '''
  36. def api_topic_thread_pre_render(curs, data, num, ip, topic_num = '', name = '', sub = '', do_type = 'thread'):
  37. # 이거 좀 엉성해서 언젠간 손 보고 싶음
  38. call_thread_regex = r"( |\n|^)(?:#([0-9]+)(?:-([0-9]+))?)( |\n|$)"
  39. call_thread_count = len(re.findall(call_thread_regex, data)) * 3
  40. while 1:
  41. rd_data = re.search(call_thread_regex, data)
  42. if call_thread_count < 0:
  43. break
  44. elif not rd_data:
  45. break
  46. else:
  47. rd_data = rd_data.groups()
  48. view_data = rd_data[1]
  49. send_topic_num = topic_num
  50. if rd_data[2]:
  51. view_data += '-' + rd_data[2]
  52. if do_type == 'thread':
  53. send_topic_num = rd_data[2]
  54. else:
  55. set_id = topic_num.split('-')
  56. send_topic_num = set_id[0] + '-' + rd_data[2]
  57. view_data += '-' + set_id[0]
  58. if do_type == 'thread':
  59. curs.execute(db_change("select ip from topic where code = ? and id = ?"), [send_topic_num, rd_data[1]])
  60. else:
  61. if rd_data[1] == '0':
  62. set_id = send_topic_num.split('-')
  63. set_id = ['', ''] if len(set_id) < 2 else set_id
  64. curs.execute(db_change('select set_data from bbs_data where set_name = "user_id" and set_id = ? and set_code = ?'), [set_id[0], set_id[1]])
  65. else:
  66. curs.execute(db_change('select set_data from bbs_data where set_name = "comment_user_id" and set_id = ? and set_code = ?'), [send_topic_num, rd_data[1]])
  67. ip_data = curs.fetchall()
  68. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  69. if do_type == 'thread':
  70. add_alarm(conn, ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  71. else:
  72. set_id = topic_num.split('-')
  73. set_id = ['', ''] if len(set_id) < 2 else set_id
  74. add_alarm(conn, 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>')
  75. data = re.sub(call_thread_regex, rd_data[0] + '<topic_a_' + do_type + '>#' + view_data + '</topic_a_' + do_type + '>' + rd_data[3], data, 1)
  76. call_thread_count -= 1
  77. call_user_regex = r"( |\n|^)(?:@([^ \n]+))( |\n|$)"
  78. call_user_count = len(re.findall(call_user_regex, data)) * 3
  79. while 1:
  80. rd_data = re.search(call_user_regex, data)
  81. if call_user_count < 0:
  82. break
  83. elif not rd_data:
  84. break
  85. else:
  86. rd_data = rd_data.groups()
  87. curs.execute(db_change("select ip from history where ip = ? limit 1"), [rd_data[1]])
  88. ip_data = curs.fetchall()
  89. if not ip_data:
  90. curs.execute(db_change("select ip from topic where ip = ? limit 1"), [rd_data[1]])
  91. ip_data = curs.fetchall()
  92. if ip_data and ip_or_user(ip_data[0][0]) == 0:
  93. if do_type == 'thread':
  94. add_alarm(conn, ip_data[0][0], ip, '<a href="/thread/' + topic_num + '#' + num + '">' + html.escape(name) + ' - ' + html.escape(sub) + '#' + num + '</a>')
  95. else:
  96. set_id = topic_num.split('-')
  97. add_alarm(conn, 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>')
  98. data = re.sub(call_user_regex, rd_data[0] + '<topic_call>@' + rd_data[1] + '</topic_call>' + rd_data[2], data, 1)
  99. call_user_count -= 1
  100. return data
  101. def api_topic(db_set, topic_num = 1, tool = 'normal', s_num = '', e_num = ''):
  102. with get_db_connect() as conn:
  103. topic_num = str(topic_num)
  104. other_set = {}
  105. other_set["topic_num"] = topic_num
  106. other_set["tool"] = tool
  107. other_set["s_num"] = str(s_num)
  108. other_set["e_num"] = str(e_num)
  109. other_set["ip"] = ip_check()
  110. other_set = json.dumps(other_set)
  111. if acl_check(conn, '', 'topic_view', topic_num) != 1:
  112. if platform.system() == 'Linux':
  113. if platform.machine() in ["AMD64", "x86_64"]:
  114. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  115. else:
  116. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  117. else:
  118. if platform.machine() in ["AMD64", "x86_64"]:
  119. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  120. else:
  121. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  122. data = data.decode('utf8')
  123. return flask.Response(response = data, status = 200, mimetype = 'application/json')
  124. else:
  125. return flask.jsonify({})