From f5cae0d99c427c19267fd7f9b715ed91fe9de21f Mon Sep 17 00:00:00 2001
From: Terrtia
Date: Wed, 23 May 2018 16:58:56 +0200
Subject: [PATCH] taxonomie + add tags + tags display
---
var/www/Flask_server.py | 16 +
var/www/modules/Tags/Flask_Tags.py | 319 +++++++++++++++++-
var/www/modules/Tags/templates/Tags.html | 21 +-
.../Tags/templates/edit_taxonomie.html | 171 ++++++++++
var/www/modules/Tags/templates/tagged.html | 39 ++-
.../modules/Tags/templates/taxonomies.html | 119 +++++++
.../browsepastes/Flask_browsepastes.py | 6 +-
.../templates/important_paste_by_module.html | 8 +-
var/www/modules/search/Flask_search.py | 4 +-
var/www/modules/search/templates/search.html | 4 +-
var/www/modules/showpaste/Flask_showpaste.py | 17 +-
.../showpaste/templates/show_saved_paste.html | 133 +++++++-
var/www/static/js/tags.js | 8 +-
13 files changed, 803 insertions(+), 62 deletions(-)
create mode 100644 var/www/modules/Tags/templates/edit_taxonomie.html
create mode 100644 var/www/modules/Tags/templates/taxonomies.html
diff --git a/var/www/Flask_server.py b/var/www/Flask_server.py
index 0be6854a..95b2f60d 100755
--- a/var/www/Flask_server.py
+++ b/var/www/Flask_server.py
@@ -18,6 +18,8 @@ sys.path.append('./modules/')
import Paste
from Date import Date
+from pytaxonomies import Taxonomies
+
# Import config
import Flask_config
@@ -82,6 +84,7 @@ for module_name, txt in list(to_add_to_header_dico.items()):
to_add_to_header = []
for module_name, txt in to_add_to_header_dico.items():
to_add_to_header.append(txt)
+print(to_add_to_header)
modified_header = modified_header.replace('', '\n'.join(to_add_to_header))
@@ -113,6 +116,19 @@ def searchbox():
return render_template("searchbox.html")
+# ========== INITIAL taxonomies ============
+r_serv_tags = redis.StrictRedis(
+ host=cfg.get("ARDB_Tags", "host"),
+ port=cfg.getint("ARDB_Tags", "port"),
+ db=cfg.getint("ARDB_Tags", "db"),
+ decode_responses=True)
+# add default ail taxonomies
+r_serv_tags.sadd('active_taxonomies', 'infoleak')
+# add default tags
+taxonomies = Taxonomies()
+for tag in taxonomies.get('infoleak').machinetags():
+ r_serv_tags.sadd('active_tag_infoleak', tag)
+
# ============ MAIN ============
if __name__ == "__main__":
diff --git a/var/www/modules/Tags/Flask_Tags.py b/var/www/modules/Tags/Flask_Tags.py
index 2b723c79..0c8be22d 100644
--- a/var/www/modules/Tags/Flask_Tags.py
+++ b/var/www/modules/Tags/Flask_Tags.py
@@ -11,6 +11,8 @@ import json
import Paste
+from pytaxonomies import Taxonomies
+
# ============ VARIABLES ============
import Flask_config
@@ -39,26 +41,62 @@ def get_all_tags():
all_tags = r_serv_tags.smembers('list_tags')
list_tags = []
- id = 0
for tag in all_tags:
list_tags.append( tag )
- id += 1
return jsonify(list_tags)
+@Tags.route("/Tags/get_all_tags_taxonomies")
+def get_all_tags_taxonomies():
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ active_taxonomie = r_serv_tags.smembers('active_taxonomies')
+
+ list_tags = []
+ for taxonomie in active_taxonomie:
+ #l_tags = taxonomies.get(taxonomie).machinetags()
+ l_tags = r_serv_tags.smembers('active_tag_' + taxonomie)
+ for tag in l_tags:
+ list_tags.append( tag )
+
+ return jsonify(list_tags)
+
+@Tags.route("/Tags/get_tags_taxonomie")
+def get_tags_taxonomie():
+
+ taxonomie = request.args.get('taxonomie')
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ active_taxonomie = r_serv_tags.smembers('active_taxonomies')
+
+ #verify input
+ if taxonomie in list_taxonomies:
+ if taxonomie in active_taxonomie:
+
+ list_tags = []
+ #l_tags = taxonomies.get(taxonomie).machinetags()
+ l_tags = r_serv_tags.smembers('active_tag_' + taxonomie)
+ for tag in l_tags:
+ list_tags.append( tag )
+
+ return jsonify(list_tags)
+
+ else:
+ return 'this taxinomie is disable'
+ else:
+ return 'INCORRECT INPUT'
+
+
@Tags.route("/Tags/get_tagged_paste")
def get_tagged_paste():
- tags = request.args.get('ltags')[1:-1]
- tags = tags.replace('\\','')
+ tags = request.args.get('ltags')
list_tags = tags.split(',')
- tmp_list_tags = []
-
- # remove " char
- for tag in list_tags:
- tmp_list_tags.append(tag[1:-1])
- list_tags = tmp_list_tags
# TODO verify input
@@ -106,8 +144,11 @@ def get_tagged_paste():
paste_date.append(curr_date)
paste_linenum.append(paste.get_lines_info()[0])
p_tags = r_serv_metadata.smembers('tag:'+path)
+ complete_tags = []
l_tags = []
for tag in p_tags:
+ complete_tag = tag
+
tag = tag.split('=')
if len(tag) > 1:
if tag[1] != '':
@@ -119,7 +160,7 @@ def get_tagged_paste():
else:
tag = tag[0]
- l_tags.append(tag)
+ l_tags.append( (tag,complete_tag) )
paste_tags.append(l_tags)
@@ -131,6 +172,7 @@ def get_tagged_paste():
return render_template("tagged.html",
year=currentSelectYear,
all_path=all_path,
+ tags=tags,
paste_tags=paste_tags,
bootstrap_label=bootstrap_label,
content=all_content,
@@ -139,12 +181,6 @@ def get_tagged_paste():
char_to_display=max_preview_modal,
finished=finished)
- return 'OK'
-
-@Tags.route("/Tags/res")
-def get_tagged_paste_res():
-
- return render_template("res.html")
@Tags.route("/Tags/remove_tag")
def remove_tag():
@@ -183,6 +219,255 @@ def confirm_tag():
return 'incompatible tag'
+@Tags.route("/Tags/addTags")
+def addTags():
+
+ tags = request.args.get('tags')
+ path = request.args.get('path')
+
+ list_tag = tags.split(',')
+
+ taxonomies = Taxonomies()
+ active_taxonomies = r_serv_tags.smembers('active_taxonomies')
+
+ if not path:
+ return 'INCORRECT INPUT'
+
+ for tag in list_tag:
+ # verify input
+ tax = tag.split(':')[0]
+ if tax in active_taxonomies:
+ if tag in r_serv_tags.smembers('active_tag_' + tax):
+
+ #add tag
+ r_serv_metadata.sadd('tag:'+path, tag)
+ r_serv_tags.sadd(tag, path)
+ #add new tag in list of all used tags
+ r_serv_tags.sadd('list_tags', tag)
+
+ else:
+ return 'INCORRECT INPUT'
+ else:
+ return 'INCORRECT INPUT'
+
+ return redirect(url_for('showsavedpastes.showsavedpaste', paste=path))
+
+@Tags.route("/Tags/thumbs_up_paste")
+def thumbs_up_paste():
+
+ #TODO verify input
+ path = request.args.get('paste')
+
+ '''positive_t = 'infoleak:confirmed="true-positive"'
+ positive_f = 'infoleak:confirmed="false-positive"'
+
+ negative_t = 'infoleak:confirmed="true-negative"'
+
+ list_tags = r_serv_metadata.smembers('tag:'+path)
+
+ if(list_tags > 0):
+
+ if positive_f in list_tags:
+ r_serv_metadata.srem('tag:'+path, positive_f)
+ r_serv_metadata.sadd('tag:'+path, positive_t)
+
+ r_serv_tags.srem(positive_f, path)
+ r_serv_tags.sadd(positive_t, path)
+ #add new tag in list of all used tags
+ r_serv_tags.sadd('list_tags', positive_t)
+
+ return redirect(url_for('showsavedpastes.showsavedpaste', paste=path))
+
+
+
+ if positive_t in list_tags:
+ return redirect(url_for('showsavedpastes.showsavedpaste', paste=path))
+ else:
+ r_serv_metadata.sadd('tag:'+path, negative_t)
+ r_serv_tags.sadd(negative_t, path)
+ #add new tag in list of all used tags
+ r_serv_tags.sadd('list_tags', negative_t)'''
+
+ return redirect(url_for('showsavedpastes.showsavedpaste', paste=path))
+
+@Tags.route("/Tags/thumbs_down_paste")
+def thumbs_down_paste():
+
+ #TODO verify input
+ path = request.args.get('paste')
+
+ '''list_tags = r_serv_metadata.smembers('tag:'+path)'''
+
+ return redirect(url_for('showsavedpastes.showsavedpaste', paste=path))
+
+
+@Tags.route("/Tags/taxonomies")
+def taxonomies():
+
+ active_taxonomies = r_serv_tags.smembers('active_taxonomies')
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ id = []
+ name = []
+ description = []
+ version = []
+ enabled = []
+ n_tags = []
+
+ for taxonomie in list_taxonomies:
+ id.append(taxonomie)
+ name.append(taxonomies.get(taxonomie).name)
+ description.append(taxonomies.get(taxonomie).description)
+ version.append(taxonomies.get(taxonomie).version)
+ if taxonomie in active_taxonomies:
+ enabled.append(True)
+ else:
+ enabled.append(False)
+
+ n = str(r_serv_tags.scard('active_tag_' + taxonomie))
+ n_tags.append(n + '/' + str(len(taxonomies.get(taxonomie).machinetags())) )
+
+ return render_template("taxonomies.html",
+ id=id,
+ all_name = name,
+ description = description,
+ version = version,
+ enabled = enabled,
+ n_tags=n_tags)
+ #return 'O'
+
+@Tags.route("/Tags/edit_taxonomie")
+def edit_taxonomie():
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ id = request.args.get('taxonomie')
+
+ #verify input
+ if id in list(taxonomies.keys()):
+ active_tag = r_serv_tags.smembers('active_tag_' + id)
+ list_tag = taxonomies.get(id).machinetags()
+ list_tag_desc = taxonomies.get(id).machinetags_expanded()
+
+ active_taxonomies = r_serv_tags.smembers('active_taxonomies')
+ if id in active_taxonomies:
+ active = True
+ else:
+ active = False
+
+ name = taxonomies.get(id).name
+ description = taxonomies.get(id).description
+ version = taxonomies.get(id).version
+
+ status = []
+ for tag in list_tag:
+ if tag in active_tag:
+ status.append(True)
+ else:
+ status.append(False)
+
+ return render_template("edit_taxonomie.html",
+ id=id,
+ name=name,
+ description = description,
+ version = version,
+ active=active,
+ all_tags = list_tag,
+ list_tag_desc=list_tag_desc,
+ status = status)
+
+ else:
+ return 'INVALID TAXONOMIE'
+
+@Tags.route("/Tags/test")
+def test():
+ return 'test',
+
+@Tags.route("/Tags/disable_taxonomie")
+def disable_taxonomie():
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ id = request.args.get('taxonomie')
+
+ if id in list_taxonomies:
+ r_serv_tags.srem('active_taxonomies', id)
+ for tag in taxonomies.get(id).machinetags():
+ r_serv_tags.srem('active_tag_' + id, tag)
+
+ return redirect(url_for('Tags.taxonomies'))
+
+ else:
+ return "INCORRECT INPUT"
+
+
+
+@Tags.route("/Tags/active_taxonomie")
+def active_taxonomie():
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ id = request.args.get('taxonomie')
+
+ # verify input
+ if id in list_taxonomies:
+ r_serv_tags.sadd('active_taxonomies', id)
+ for tag in taxonomies.get(id).machinetags():
+ r_serv_tags.sadd('active_tag_' + id, tag)
+
+ return redirect(url_for('Tags.taxonomies'))
+
+ else:
+ return "INCORRECT INPUT"
+
+@Tags.route("/Tags/edit_taxonomie_tag")
+def edit_taxonomie_tag():
+
+ taxonomies = Taxonomies()
+ list_taxonomies = list(taxonomies.keys())
+
+ arg1 = request.args.getlist('tag_enabled')
+ arg2 = request.args.getlist('tag_disabled')
+
+ id = request.args.get('taxonomie')
+
+ #verify input
+ if id in list_taxonomies:
+ list_tag = taxonomies.get(id).machinetags()
+
+ #check tags validity
+ if ( all(elem in list_tag for elem in arg1) or (len(arg1) == 0) ) and ( all(elem in list_tag for elem in arg2) or (len(arg2) == 0) ):
+
+ active_tag = r_serv_tags.smembers('active_tag_' + id)
+
+ diff = list(set(arg1) ^ set(list_tag))
+
+ #remove tags
+ for tag in diff:
+ r_serv_tags.srem('active_tag_' + id, tag)
+
+ #all tags unchecked
+ if len(arg1) == 0 and len(arg2) == 0:
+ r_serv_tags.srem('active_taxonomies', id)
+
+ #add new tags
+ for tag in arg2:
+ r_serv_tags.sadd('active_taxonomies', id)
+ r_serv_tags.sadd('active_tag_' + id, tag)
+
+ return redirect(url_for('Tags.taxonomies'))
+ else:
+ return "INCORRECT INPUT"
+
+ else:
+ return "INCORRECT INPUT"
+
+
# ========= REGISTRATION =========
app.register_blueprint(Tags)
diff --git a/var/www/modules/Tags/templates/Tags.html b/var/www/modules/Tags/templates/Tags.html
index cb476749..be02a0bc 100644
--- a/var/www/modules/Tags/templates/Tags.html
+++ b/var/www/modules/Tags/templates/Tags.html
@@ -34,28 +34,32 @@
-
+
+
+
+
+
+ {% include 'navbar.html' %}
+
+
+
+
+
+
+
{{ name }}
+ {% if active %}
+ Enabled
+ {% endif %}
+ {% if not active %}
+ Disabled
+ {% endif %}
+
+
+ {{ description }}
+
+ Version: {{ version }}
+ {% if active %}
+
+ Disable Taxonomie
+
+ {% endif %}
+ {% if not active %}
+
+ Enable Taxonomie
+
+ {% endif %}
+
+
+
+
+
+
+
+
+
+
diff --git a/var/www/modules/Tags/templates/edit_taxonomie.html b/var/www/modules/Tags/templates/edit_taxonomie.html
new file mode 100644
index 00000000..becaef6c
--- /dev/null
+++ b/var/www/modules/Tags/templates/edit_taxonomie.html
@@ -0,0 +1,171 @@
+
+
+
+