diff --git a/acc.bat b/acc.bat new file mode 100644 index 0000000..6dc3da5 --- /dev/null +++ b/acc.bat @@ -0,0 +1 @@ +python -m streamlit run account/main.py \ No newline at end of file diff --git a/acc.sh b/acc.sh new file mode 100644 index 0000000..7716383 --- /dev/null +++ b/acc.sh @@ -0,0 +1 @@ +python -m streamlit run account/manager.py \ No newline at end of file diff --git a/account/__pycache__/loader.cpython-312.pyc b/account/__pycache__/loader.cpython-312.pyc index 6946cc3..b45d892 100644 Binary files a/account/__pycache__/loader.cpython-312.pyc and b/account/__pycache__/loader.cpython-312.pyc differ diff --git a/account/__pycache__/reliability.cpython-312.pyc b/account/__pycache__/reliability.cpython-312.pyc index 9cb9dc0..73141c0 100644 Binary files a/account/__pycache__/reliability.cpython-312.pyc and b/account/__pycache__/reliability.cpython-312.pyc differ diff --git a/account/__pycache__/userid.cpython-312.pyc b/account/__pycache__/userid.cpython-312.pyc new file mode 100644 index 0000000..742c442 Binary files /dev/null and b/account/__pycache__/userid.cpython-312.pyc differ diff --git a/account/__pycache__/username.cpython-312.pyc b/account/__pycache__/username.cpython-312.pyc new file mode 100644 index 0000000..7c2d9e7 Binary files /dev/null and b/account/__pycache__/username.cpython-312.pyc differ diff --git a/account/create.py b/account/create.py deleted file mode 100644 index ac67604..0000000 --- a/account/create.py +++ /dev/null @@ -1,37 +0,0 @@ -import smtplib -import time -import streamlit as st -from account.loader import account_database_loader - -conn = account_database_loader() -cursor = conn.cursor() - -def add_user(email, username, password): - cursor.execute('''INSERT INTO users (email, username, password) VALUES (?, ?, ?)''', (email, username, password)) - conn.commit() - -def check_existing_email(email): - cursor.execute("SELECT * FROM users WHERE email=?", (email,)) - return cursor.fetchone() is not None - -def check_existing_username(username): - cursor.execute("SELECT * FROM users WHERE username=?", (username,)) - return cursor.fetchone() is not None - -st.title('User Registration') - -email = st.text_input('Email:') -username = st.text_input('Username:') -password = st.text_input('Password:', type='password') -confirm_button = st.button('Register') - -if confirm_button: - if check_existing_email(email): - st.error('This email is already registered. Please use a different email.') - elif check_existing_username(username): - st.error('This user name already in use. Please use another username.') - else: - with st.spinner('Checking the given information...'): - time.sleep(1) - add_user(email, username, password) - st.success('Your account has been successfully created.') diff --git a/account/database.py b/account/database.py index a011abf..18c6bf3 100644 --- a/account/database.py +++ b/account/database.py @@ -12,4 +12,4 @@ def create_users_database(): reliability INTGER DEFAULT 0 )''') conn.commit() - conn.close() \ No newline at end of file + conn.close() diff --git a/account/loader.py b/account/loader.py index 50a0010..aca3c92 100644 --- a/account/loader.py +++ b/account/loader.py @@ -1,5 +1,7 @@ import sqlite3 +from log.write import sys_log def account_database_loader(): conn = sqlite3.connect('./database/users-account.db') + sys_log("Loaded", "users-account.db") return conn diff --git a/account/main.py b/account/main.py new file mode 100644 index 0000000..6939d98 --- /dev/null +++ b/account/main.py @@ -0,0 +1,111 @@ +import smtplib +import time +import streamlit as st +from account.loader import account_database_loader +from account.reliability import get_user_reliability +from account.userid import get_user_id +from account.username import get_username +from log.write import sys_log + +conn = account_database_loader() +cursor = conn.cursor() + +def add_user(email, username, password): + cursor.execute('''INSERT INTO users (email, username, password) VALUES (?, ?, ?)''', (email, username, password)) + sys_log("Created User Account", "Username: " + username + " Email: " + email + " Password: " + password) + conn.commit() + +def update_password(user_id, email, new_password): + cursor.execute("UPDATE users SET password = ? WHERE id = ?", (new_password, user_id)) + sys_log("Changed User Password", "Username: " + username + " User ID: " + str(user_id) + " Email: " + email + " Password: " + password) + conn.commit() + +def update_username(user_id, email, new_username): + cursor.execute("UPDATE users SET username = ? WHERE id = ?", (new_username, user_id)) + sys_log("Changed Username", "Username: " + username + " User ID: " + str(user_id) + " Email: " + email + " Password: " + password) + conn.commit() + +def check_existing_email(email): + cursor.execute("SELECT * FROM users WHERE email=?", (email,)) + return cursor.fetchone() is not None + +def check_existing_username(username): + cursor.execute("SELECT * FROM users WHERE username=?", (username,)) + return cursor.fetchone() is not None + +st.title('Account Manager') + +st.session_state.setdefault('form_state', True) +AForm = st.session_state.form_state + +with st.form(key = 'Account_Form'): + col1, col2, col3 = st.columns([0.1, 0.1, 0.1]) + + if AForm: + with col1: + submitted1 = st.form_submit_button('Create') + with col2: + submitted2 = st.form_submit_button('Change') + with col3: + submitted3 = st.form_submit_button('Find') + + if submitted1: + email = st.text_input('Email:') + username = st.text_input('Username:') + password = st.text_input('Password:', type='password') + + if email and username and password: + if check_existing_email(email): + st.error('This email is already registered. Please use a different email.') + elif check_existing_username(username): + st.error('This user name already in use. Please use another username.') + else: + with st.spinner('Checking the given information...'): + add_user(email, username, password) + time.sleep(1) + st.success('Your account has been successfully created.') + + if submitted2: + email = st.text_input('Email: ') + username = st.text_input('Username: ') + password = st.text_input('Password: ', type='password') + + st.markdown('---') + + if get_user_reliability(cursor, username, password) is not None: + user_id = get_user_id(cursor, username) + new_username = st.text_input('New Username: ') + new_password = st.text_input('New Password: ', type='password') + if new_username: + update_username(user_id, email, new_username) + st.success('Your account username has been successfully updated.') + if new_password: + update_password(user_id, email, new_password) + st.success('Your account password has been successfully updated.') + elif get_user_reliability(cursor, username, password) is None: + pass + else: + st.error("Couldn't find your account, please check again.") + + close_button = st.form_submit_button('Close') + + if close_button: + st.session_state.form_state = False + + if submitted3: + username = st.text_input('Username: ') + user_id = st.text_input('User ID: ') + + st.markdown('---') + + if username and not user_id: + st.write("User ID: " + str(get_user_id(cursor, username))) + elif user_id and not username: + st.write("Username: " + get_username(cursor, user_id)) + + else: + open_button = st.form_submit_button('Open Form') + if open_button: + st.session_state.form_state = True + + \ No newline at end of file diff --git a/account/reliability.py b/account/reliability.py index 9e6ec3f..6149aaa 100644 --- a/account/reliability.py +++ b/account/reliability.py @@ -1,5 +1,3 @@ -from account.loader import account_database_loader - def get_user_reliability(cursor, username, password): cursor.execute('SELECT password FROM users WHERE username = ?', (username,)) row = cursor.fetchone() diff --git a/account/userid.py b/account/userid.py new file mode 100644 index 0000000..be45f9b --- /dev/null +++ b/account/userid.py @@ -0,0 +1,11 @@ +import sqlite3 + +def get_user_id(cursor, username): + cursor.execute("SELECT id FROM users WHERE username = ?", (username,)) + + rows = cursor.fetchall() + + for row in rows: + userid = row[0] + + return userid diff --git a/account/username.py b/account/username.py new file mode 100644 index 0000000..50a0b55 --- /dev/null +++ b/account/username.py @@ -0,0 +1,11 @@ +import sqlite3 + +def get_username(cursor, user_id): + cursor.execute("SELECT username FROM users WHERE id = ?", (user_id,)) + + rows = cursor.fetchall() + + for row in rows: + username = row[0] + + return username diff --git a/adpn.bat b/adpn.bat new file mode 100644 index 0000000..747882a --- /dev/null +++ b/adpn.bat @@ -0,0 +1 @@ +python adpn.py \ No newline at end of file diff --git a/adpn.py b/adpn.py new file mode 100644 index 0000000..73dc88b --- /dev/null +++ b/adpn.py @@ -0,0 +1,129 @@ +import os +import shutil +import sqlite3 +from account.userid import get_user_id +from account.username import get_username +from log.write import log, sys_log +from account.loader import account_database_loader +from account.reliability import get_user_reliability + +print('Welcome to MonoSearch Administrator Panel') + +def compare_databases(): + try: + conn1 = sqlite3.connect("./database/search-index.db") + conn2 = sqlite3.connect("./database/censorship.db") + + cur1 = conn1.cursor() + cur2 = conn2.cursor() + + table_name = "information" + + cur1.execute(f"SELECT * FROM {table_name}") + cur2.execute(f"SELECT * FROM {table_name}") + + data1 = cur1.fetchall() + data2 = cur2.fetchall() + + set1 = set(data1) + set2 = set(data2) + + different_list = list(set2 - set1) + + result = { + "Censorship:": different_list, + "Search Index:": list(set1-set2) + } + + print(result) + + conn1.close() + conn2.close() + + if different_list: + return False + else: + return True + except Exception as e: + print("Error comparing databases:", str(e)) + return False + +def synchronization_databases(): + try: + if not compare_databases(): + print("Databases cannot be synchronized when there are differences between them.") + return + else: + os.remove("./database/censorship.db") + shutil.copy("./database/search-index.db", "./database/censorship.db") + print("Synchronization successful.") + except Exception as e: + print("Error synchronizing databases:", str(e)) + +account_conn = account_database_loader() +account_cursor = account_conn.cursor() + +def list_users_database(): + account_cursor.execute("SELECT * FROM users") + + rows = account_cursor.fetchall() + + for row in rows: + print("ID:", row[0]) + print("Email:", row[1]) + print("Username:", row[2]) + print("Password:", row[3]) + print("Reliability:", row[4]) + print("--------------------") + +def get_reliability_from_id(user_id): + account_cursor.execute("SELECT reliability FROM users WHERE id = ?", (user_id,)) + + rows = account_cursor.fetchall() + + for row in rows: + reliability = row[0] + + return reliability + +def change_reliability_by_user_id(user_id, new_reliability): + account_cursor.execute("UPDATE users SET reliability = ? WHERE id = ?", (new_reliability, user_id)) + account_conn.commit() + + sys_log("Changed User Reliability", "Username: " + get_username(account_cursor, user_id) + " Reliability: " + str(new_reliability)) + +username = input('Username: ') +password = input('Password: ') + +reliability = get_user_reliability(account_cursor, username, password) + +if reliability is None: + print("Account does not exist.") +elif reliability >= 4: + print('Logged in successfully.') +else: + print('You do not have sufficient rights to access the panel.') + exit() + +while(True): + command = input('>>> ') + + if command == "exit": + exit() + elif command == "check": + compare_databases() + elif command == "sync": + synchronization_databases() + elif command == "log": + with open('log.txt', 'r') as file: + for line in file: + print(line.strip()) + elif command == "users-list": + list_users_database() + elif command == "users-rel": + user_id = input('User ID: ') + print("Current reliability: " + str(get_reliability_from_id(user_id))) + new_reliability = input('Change reliability to: ') + change_reliability_by_user_id(user_id, new_reliability) + print('Changed reliability successfully.') + \ No newline at end of file diff --git a/adpn.sh b/adpn.sh new file mode 100644 index 0000000..747882a --- /dev/null +++ b/adpn.sh @@ -0,0 +1 @@ +python adpn.py \ No newline at end of file diff --git a/initializer/__pycache__/database.cpython-312.pyc b/initializer/__pycache__/database.cpython-312.pyc index 1a2e06e..0485d50 100644 Binary files a/initializer/__pycache__/database.cpython-312.pyc and b/initializer/__pycache__/database.cpython-312.pyc differ diff --git a/initializer/__pycache__/loader.cpython-312.pyc b/initializer/__pycache__/loader.cpython-312.pyc index fa608ab..5fd5b11 100644 Binary files a/initializer/__pycache__/loader.cpython-312.pyc and b/initializer/__pycache__/loader.cpython-312.pyc differ diff --git a/initializer/database.py b/initializer/database.py index f505bf3..0132e40 100644 --- a/initializer/database.py +++ b/initializer/database.py @@ -1,4 +1,5 @@ import sqlite3 +from log.write import log, sys_log from datetime import datetime from account.database import create_users_database @@ -40,4 +41,6 @@ def Initializer_Database(): Initializer_Censorship_Database() create_users_database() - return conn + sys_log("Initializer Database", "search-index.db") + sys_log("Initializer Censorship Database", "censorship.db") + sys_log("Create Users Account Database", "users-account.db") diff --git a/initializer/loader.py b/initializer/loader.py index ffff057..c62d3ef 100644 --- a/initializer/loader.py +++ b/initializer/loader.py @@ -1,9 +1,12 @@ import sqlite3 +from log.write import sys_log def database_loader(): conn = sqlite3.connect('./database/search-index.db') + sys_log("Loaded", "search-index.db") return conn def censorship_database_loader(): conn = sqlite3.connect('./database/censorship.db') + sys_log("Loaded", "censorship.db") return conn diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/log/__pycache__/write.cpython-312.pyc b/log/__pycache__/write.cpython-312.pyc new file mode 100644 index 0000000..f6eda6c Binary files /dev/null and b/log/__pycache__/write.cpython-312.pyc differ diff --git a/log/write.py b/log/write.py new file mode 100644 index 0000000..163b352 --- /dev/null +++ b/log/write.py @@ -0,0 +1,14 @@ +from account.reliability import get_user_reliability +from account.userid import get_user_id + +def log(cursor, username, password, job, string): + log_file = open("./log.txt", "a") + log_string = "User: " + username + " (ID:" + str(get_user_id(cursor, username)) + ")/(Reliability:" + str(get_user_reliability(cursor, username, password)) + "): " + job + ": " + string + log_file.write(log_string + '\n') + log_file.close() + +def sys_log(job, string): + log_file = open("./log.txt", "a") + log_string = "SYS: " + job + ": " + string + log_file.write(log_string + '\n') + log_file.close() diff --git a/main.py b/main.py index c9b60ed..2c22db3 100644 --- a/main.py +++ b/main.py @@ -12,11 +12,11 @@ st.title('MonoSearch') -st.session_state.setdefault('add_state', True) +st.session_state.setdefault('form_state', True) with st.form('Input_Form'): col1, col2, col3, col4, col5 = st.columns([3, 0.8, 0.6, 0.6, 0.8]) - AForm = st.session_state.add_state + AForm = st.session_state.form_state with col1: keyword = st.text_input('Try to search something!', value='Try to search something!', placeholder='Try to search something!', label_visibility='collapsed') diff --git a/manager/__pycache__/manager.cpython-312.pyc b/manager/__pycache__/manager.cpython-312.pyc index c1eedf7..22f6cd7 100644 Binary files a/manager/__pycache__/manager.cpython-312.pyc and b/manager/__pycache__/manager.cpython-312.pyc differ diff --git a/manager/manager.py b/manager/manager.py index aa1fcf7..1242a77 100644 --- a/manager/manager.py +++ b/manager/manager.py @@ -1,4 +1,6 @@ import streamlit as st +from account.userid import get_user_id +from log.write import log from account.loader import account_database_loader from account.reliability import get_user_reliability from initializer.loader import censorship_database_loader @@ -17,11 +19,13 @@ def manager_insert_data(conn, username, password, link, title, text, description insert_data(censorship_conn, link, title, text, description, keywords, shorttext) st.success("Your add request has been sent to the administrator.") censorship_conn.close() - elif reliability == 1: + elif reliability >= 1: insert_data(conn, link, title, text, description, keywords, shorttext) else: st.error("The user's reliability cannot be determined.") + log(cursor, username, password, "Insert Data", "Link: " + link + " Title: " + title + " Text: " + text + " Description: " + description + " Keywords: " + keywords + " ShortText: " + shorttext) + cursor.close() account_conn.close() @@ -36,11 +40,13 @@ def manager_edit_data(conn, username, password, site_id, link, title, text, desc edit_data(censorship_conn, site_id, link, title, text, description, keywords, shorttext) st.success("Your edit request has been sent to the administrator.") censorship_conn.close() - elif reliability == 1: + elif reliability >= 2: edit_data(conn, site_id, link, title, text, description, keywords, shorttext) else: st.error("The user's reliability cannot be determined.") + log(cursor, username, password, "Edit Data", "Site ID: " + site_id + " Link: " + link + " Title: " + title + " Text: " + text + " Description: " + description + " Keywords: " + keywords + " ShortText: " + shorttext) + cursor.close() account_conn.close() @@ -50,10 +56,13 @@ def manager_remove_data(conn, username, password, site_id): reliability = get_user_reliability(cursor, username, password) - if reliability == 1: + if reliability >= 3: remove_data(conn, site_id) st.success("Data removed successfully.") - account_conn.close() - cursor.close() + else: + st.error("The user's reliability cannot be determined.") + log(cursor, username, password, "Remove Data", "Site ID: " + site_id) + account_conn.close() + cursor.close()