OXIESEC PANEL
- Current Dir:
/
/
var
/
www
/
zopalv1
/
opalv1
Server IP: 10.0.0.4
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
09/25/2021 03:02:46 AM
rwxr-xr-x
📄
0__init__.py
5.13 KB
09/20/2021 06:07:13 AM
rw-r--r--
📄
__init__.py
18.61 KB
09/20/2021 06:07:13 AM
rw-r--r--
📁
__pycache__
-
09/20/2021 06:07:09 AM
rwxr-xr-x
📁
documents
-
09/20/2021 06:07:07 AM
rwxr-xr-x
📄
o2__init__.py
8.26 KB
09/20/2021 06:07:14 AM
rw-r--r--
📄
o__init__.py
11.75 KB
09/20/2021 06:07:14 AM
rw-r--r--
Editing: o2__init__.py
Close
#!/bin/usr/python3 import flask from flask import Flask, flash, request, redirect, url_for, jsonify, Response from flask import send_file, send_from_directory, safe_join, abort from werkzeug.utils import secure_filename import pymongo from pymongo import MongoClient from bson import json_util, ObjectId, Binary, Code from bson.json_util import dumps import json from datetime import datetime import os PDF_FOLDER = '/var/www/html/pdf' ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} app = flask.Flask(__name__) app.config['DEBUG'] = True app.config["PDF_FOLDER"] = PDF_FOLDER @app.route('/v1/resources/articlesbyauthor', methods=['GET']) def api_author(): if 'author' in request.args: my_author = request.args['author'] #return my_author else: return "Error: author not specified. Please provide an author" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test query = {'author' : { "$regex" : my_author, "$options": "i" }} if 'session' in request.args: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "fileurl": 1, "_id": 0}) else: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "_id": 0}) for art in articles: art_results.append(art) return Response(dumps({'status': 200, 'data': art_results}), mimetype='application/json') # return jsonify({'status': 200, 'data': art_results}) @app.route('/v1/resources/articlesbytitle', methods=['GET']) def api_title(): if 'title' in request.args: keyword = request.args['title'] # return my_author else: return "Error: keyword not specified. Please provide a keyword" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test # #build_fragment = "author:" + "/" + my_author + "/" query = {'title' : { "$regex" : keyword, "$options": "i" }} if 'session' in request.args: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "fileurl": 1, "_id": 0}) else: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "_id": 0}) #articles = collection.find(query) for art in articles: art_results.append(art) return Response(dumps({'status': 200, 'data': art_results}), mimetype='application/json') #return jsonify({'status': 200, 'data': art_results}) @app.route('/v1/resources/articlesbycategory', methods=['GET']) def api_category(): if 'category' in request.args: cat_key = request.args['category'] # return my_author else: return "Error: keyword not specified. Please provide a keyword" art_results = [] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test query = {'category' : { "$regex" : cat_key, "$options": "i" }} #articles = collection.find(query) if 'session' in request.args: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "fileurl": 1, "_id": 0}) else: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "_id": 0}) for art in articles: art_results.append(art) return Response(dumps({'status': 200, 'data': art_results}), mimetype='application/json') @app.route('/v1/resources/cms/upload', methods=['POST']) def api_upload(): my_title = request.form['title'] my_author = request.form['author'] my_category = request.form['category'] my_year_published = request.form['year_published'] my_first_sentence = request.form['first_sentence'] client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test my_id = collection.count() my_doi = "10.34048/OPAL/" + str(my_year_published) + "/"+ str(my_id) #pdf_fileurl = str(my_id) + "_" + filename my_pdf = request.files['filePdf'] filename = secure_filename(my_pdf.filename) filename = str(my_id) + "_" + filename my_pdf.save(os.path.join(app.config['PDF_FOLDER'], filename)) query = {"id" : my_id+1, "title": my_title, "author": my_author, "category": my_category, "year_published" : my_year_published, "doi": my_doi, "first_sentence" : my_first_sentence, "fileurl" : filename} result = collection.insert_one(query) if result: return jsonify({'status': 200, 'file_name': filename}) else: return jsonify({'status': 404, 'err_msg': "Could not save the article"}) @app.route('/v1/resources/articles/featured', methods=['GET']) def api_featured(): client = MongoClient("localhost", 27017) if not client: return jsonify({'status': 404, 'error': "Could not connect to resource"}) db = client.opalv1 collection = db.test random_result = collection.find({}, { "id" : 1, "title" : 1 , "first_sentence" : 1, "_id" : 0 }).limit(3) featured_articles = [] for art in random_result: featured_articles.append(art) return jsonify({'status': 200, 'data': featured_articles}) @app.route('/v1/resources/article', methods=['GET']) def api_featured_article(): if 'id' in request.args: my_id = int(request.args['id']) else: return "Error: id not found. Please provide a valid id" client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.test query = {"id" : my_id} id_count = collection.count() if my_id > id_count: return jsonify({'status': 400, 'err_msg': 'Please check your article id'}) if 'session' in request.args: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "fileurl": 1, "_id": 0}) else: articles = collection.find(query, {"id": 1, "title": 1, "author": 1, "category": 1, "doi":1, "year_published": 1, "first_sentence": 1, "_id": 0}) for art in articles: return jsonify({'status': 200, 'data': art}) @app.route('/v1/resources/userregistration', methods=['GET']) def api_user_registration(): my_fname = request.args['fname'] my_lname = request.args['lname'] #my_username = request.args['username'] my_email = request.args['email'] my_pw = request.args['password'] my_type = request.args['type'] my_affil = request.args['affiliation'] my_city = request.args['city'] my_country = request.args['country'] my_session = "opal" + datetime.utcnow().strftime("%Y%m%d%H%M%S") client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.users check_email = collection.find({"email" : my_email}) check_email_list = list(check_email) if check_email_list: return jsonify({'status': 500, 'err_msg': "A user by that email already exists"}) query = {"fname" : my_fname, "lname": my_lname, "email": my_email, "password": my_pw, "type" : my_type, "affiliation" : my_affil, "city" : my_city, "country" : my_country} collection.insert_one(query) result = collection.find({"email" : my_email, "password" : my_pw}, { "type" : 1, "fname" : 1, "lname" : 1, "_id" : 0 }) result_list = list(result) result_dict = result_list[0] my_data = {'session' : my_session} my_data.update(result_dict) if result: return jsonify({'status': 200, 'data': my_data}) else: return "Sorry could not add user" @app.route('/v1/resources/userauth', methods=['GET']) def api_user_auth(): my_email = request.args['email'] my_pw = request.args['password'] my_type = request.args['utype'] my_session = "opal-" + datetime.utcnow().strftime("%Y%m%d%H%M%S") client = MongoClient("localhost", 27017) db = client.opalv1 collection = db.users #query = collection.find({"username" : my_username, "password" : my_pw, "type" : my_type}) #result = collection.find({"username" : my_username, "password" : my_pw}, { "type" : 1, "fname" : 1, "lname" : 1, "_id" : 0 }) result = collection.find({"email" : my_email, "password" : my_pw, "type" : my_type}, { "type" : 1, "fname" : 1, "lname" : 1, "_id" : 0 }) result_list = list(result) if result_list: result_dict = result_list[0] else: return jsonify({'status': 500, 'err_msg': "Sorry could not find the user"}) my_data = {'session' : my_session} my_data.update(result_dict) #return jsonify({'status': 200, 'type' : json.dumps(art_results), 'data': my_session}) return jsonify({'status': 200, 'data': my_data}) if __name__ == "__main__": app.run()