From 0405aaa3dac94da58585ff9ba7b131d5fc7c490f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 6 Jul 2019 09:32:41 +0200 Subject: [PATCH] Review comments and fixing stickler complaints. Signed-off-by: DL6ER --- .../Scripts/database_migration/gravity-db.sh | 10 ++++----- .../database_migration/gravity/1_to_2.sql | 2 +- advanced/Scripts/webpage.sh | 22 +++++++++++++++---- advanced/Templates/gravity.db.sql | 2 +- gravity.sh | 15 ++++++++----- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/advanced/Scripts/database_migration/gravity-db.sh b/advanced/Scripts/database_migration/gravity-db.sh index 492546be..55411214 100644 --- a/advanced/Scripts/database_migration/gravity-db.sh +++ b/advanced/Scripts/database_migration/gravity-db.sh @@ -11,11 +11,11 @@ # Please see LICENSE file for your rights under this license. upgrade_gravityDB(){ - local version=$(sqlite3 "$1" "SELECT "value" FROM "info" WHERE "property" = 'version';") + local version + version=$(sqlite3 "$1" "SELECT "value" FROM "info" WHERE "property" = 'version';") - case "$version" in - 1) + if [[ "$version" == "1" ]]; then sqlite3 "$1" < "/etc/.pihole/advanced/Scripts/database_migration/gravity/1_to_2.sql" - ;; - esac + version=2 + fi } diff --git a/advanced/Scripts/database_migration/gravity/1_to_2.sql b/advanced/Scripts/database_migration/gravity/1_to_2.sql index ba051e25..073eced5 100644 --- a/advanced/Scripts/database_migration/gravity/1_to_2.sql +++ b/advanced/Scripts/database_migration/gravity/1_to_2.sql @@ -1,4 +1,4 @@ -CREATE TABLE auditlist +CREATE TABLE domain_auditlist ( id INTEGER PRIMARY KEY AUTOINCREMENT, domain TEXT UNIQUE NOT NULL, diff --git a/advanced/Scripts/webpage.sh b/advanced/Scripts/webpage.sh index 91d35d98..516ea4e4 100755 --- a/advanced/Scripts/webpage.sh +++ b/advanced/Scripts/webpage.sh @@ -542,24 +542,38 @@ Teleporter() { php /var/www/html/admin/scripts/pi-hole/php/teleporter.php > "pi-hole-teleporter_${datetimestamp}.tar.gz" } +checkDomain() +{ + local domain validDomain + # Convert to lowercase + domain="${1,,}" + validDomain=$(grep -P "^((-|_)*[a-z\\d]((-|_)*[a-z\\d])*(-|_)*)(\\.(-|_)*([a-z\\d]((-|_)*[a-z\\d])*))*$" <<< "${domain}") # Valid chars check + validDomain=$(grep -P "^[^\\.]{1,63}(\\.[^\\.]{1,63})*$" <<< "${validDomain}") # Length of each label + echo "${validDomain}" +} + addAudit() { shift # skip "-a" shift # skip "audit" - local domains="('${1}')" + local domains validDomain + domains="('$(checkDomain "${1}")')" shift # skip first domain, as it has already been added for domain in "$@" do # Insert only the domain here. The date_added field will be # filled with its default value (date_added = current timestamp) - domains="${domains},('${domain}')" + validDomain="$(checkDomain "${domain}")" + if [[ -n "${validDomain}" ]]; then + domains="${domains},('${domain}')" + fi done - sqlite3 "${gravityDBfile}" "INSERT INTO \"auditlist\" (domain) VALUES ${domains};" + sqlite3 "${gravityDBfile}" "INSERT INTO \"domain_auditlist\" (domain) VALUES ${domains};" } clearAudit() { - sqlite3 "${gravityDBfile}" "DELETE FROM \"auditlist\";" + sqlite3 "${gravityDBfile}" "DELETE FROM \"domain_auditlist\";" } SetPrivacyLevel() { diff --git a/advanced/Templates/gravity.db.sql b/advanced/Templates/gravity.db.sql index 78999e7c..113c035f 100644 --- a/advanced/Templates/gravity.db.sql +++ b/advanced/Templates/gravity.db.sql @@ -81,7 +81,7 @@ CREATE TABLE gravity domain TEXT PRIMARY KEY ); -CREATE TABLE auditlist +CREATE TABLE domain_auditlist ( id INTEGER PRIMARY KEY AUTOINCREMENT, domain TEXT UNIQUE NOT NULL, diff --git a/gravity.sh b/gravity.sh index 09c66c4f..3a8afc7a 100755 --- a/gravity.sh +++ b/gravity.sh @@ -115,25 +115,28 @@ database_table_from_file() { inputfile="${source}" else # Apply format for white-, blacklist, regex, and adlist tables - local rowid - declare -i rowid - rowid=1 # Read file line by line if [[ "${table}" == "auditlist" ]]; then + local rowid + declare -i rowid + rowid=1 grep -v '^ *#' < "${source}" | while IFS= read -r domain do # Only add non-empty lines - if [[ ! -z "${domain}" ]]; then + if [[ -n "${domain}" ]]; then # Auditlist table format echo "${rowid},\"${domain}\",${timestamp}" >> "${tmpFile}" rowid+=1 fi done else + local rowid + declare -i rowid + rowid=1 grep -v '^ *#' < "${source}" | while IFS= read -r domain do # Only add non-empty lines - if [[ ! -z "${domain}" ]]; then + if [[ -n "${domain}" ]]; then # White-, black-, and regexlist format echo "${rowid},\"${domain}\",1,${timestamp},${timestamp},\"Migrated from ${source}\"" >> "${tmpFile}" rowid+=1 @@ -198,7 +201,7 @@ migrate_to_database() { if [ -e "${auditFile}" ]; then # Store audit domains in database echo -e " ${INFO} Migrating content of ${auditFile} into new database" - database_table_from_file "auditlist" "${auditFile}" + database_table_from_file "domain_auditlist" "${auditFile}" fi }