| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { exit; } |
| 4 |
|
| 5 |
function gdsih_list_database_tables() { |
| 6 |
global $wpdb; |
| 7 |
|
| 8 |
return array( |
| 9 |
$wpdb->base_prefix.'gdsec_csp_reports' => 10, |
| 10 |
$wpdb->base_prefix.'gdsec_xxp_reports' => 6 |
| 11 |
); |
| 12 |
} |
| 13 |
|
| 14 |
function gdsih_install_database() { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
$charset_collate = ''; |
| 18 |
|
| 19 |
if (!empty($wpdb->charset)) { |
| 20 |
$charset_collate = "default CHARACTER SET $wpdb->charset"; |
| 21 |
} |
| 22 |
|
| 23 |
if (!empty($wpdb->collate)) { |
| 24 |
$charset_collate.= " COLLATE $wpdb->collate"; |
| 25 |
} |
| 26 |
|
| 27 |
$tables = array( |
| 28 |
'csp_reports' => $wpdb->base_prefix.'gdsec_csp_reports', |
| 29 |
'xxp_reports' => $wpdb->base_prefix.'gdsec_xxp_reports', |
| 30 |
); |
| 31 |
|
| 32 |
$query = "CREATE TABLE ".$tables['csp_reports']." ( |
| 33 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 34 |
reported datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 35 |
ip varchar(64) NOT NULL, |
| 36 |
violated_directive varchar(128) NOT NULL DEFAULT '', |
| 37 |
effective_directive varchar(128) NOT NULL DEFAULT '', |
| 38 |
document_uri text NOT NULL, |
| 39 |
blocked_uri text NOT NULL, |
| 40 |
referrer text NOT NULL, |
| 41 |
original_policy text NOT NULL, |
| 42 |
user_agent text NOT NULL, |
| 43 |
PRIMARY KEY (id), |
| 44 |
KEY reported (reported), |
| 45 |
KEY ip (ip), |
| 46 |
KEY violated_directive (violated_directive), |
| 47 |
KEY effective_directive (effective_directive) |
| 48 |
) $charset_collate; |
| 49 |
CREATE TABLE ".$tables['xxp_reports']." ( |
| 50 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 51 |
reported datetime NOT NULL DEFAULT '0000-00-00 00:00:00', |
| 52 |
ip varchar(64) NOT NULL, |
| 53 |
request_url text NOT NULL, |
| 54 |
request_body longtext NOT NULL, |
| 55 |
user_agent text NOT NULL, |
| 56 |
PRIMARY KEY (id), |
| 57 |
KEY reported (reported), |
| 58 |
KEY ip (ip) |
| 59 |
) $charset_collate;"; |
| 60 |
|
| 61 |
require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
| 62 |
|
| 63 |
return dbDelta($query); |
| 64 |
} |
| 65 |
|
| 66 |
function gdsih_check_database() { |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
$result = array(); |
| 70 |
$tables = gdsih_list_database_tables(); |
| 71 |
|
| 72 |
foreach ($tables as $table => $count) { |
| 73 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") == $table) { |
| 74 |
$columns = $wpdb->get_results("SHOW COLUMNS FROM $table"); |
| 75 |
|
| 76 |
if ($count != count($columns)) { |
| 77 |
$result[$table] = array("status" => "error", "msg" => __("Some columns are missing.", "gd-security-headers")); |
| 78 |
} else { |
| 79 |
$result[$table] = array("status" => "ok"); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$result[$table] = array("status" => "error", "msg" => __("Table missing.", "gd-security-headers")); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $result; |
| 87 |
} |
| 88 |
|
| 89 |
function gdsih_truncate_database_tables() { |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
$tables = array_keys(gdsih_list_database_tables()); |
| 93 |
|
| 94 |
foreach ($tables as $table) { |
| 95 |
$wpdb->query("TRUNCATE TABLE ".$table); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
function gdsih_drop_database_tables() { |
| 100 |
global $wpdb; |
| 101 |
|
| 102 |
$tables = array_keys(gdsih_list_database_tables()); |
| 103 |
|
| 104 |
foreach ($tables as $table) { |
| 105 |
$wpdb->query("DROP TABLE IF EXISTS ".$table); |
| 106 |
} |
| 107 |
} |
| 108 |
|