| 1 |
<?php |
| 2 |
|
| 3 |
// no direct access! |
| 4 |
defined('ABSPATH') or die("No direct access"); |
| 5 |
|
| 6 |
global $wpdb; |
| 7 |
|
| 8 |
$gspeech_s_enc = ""; |
| 9 |
$gspeech_h_enc = ""; |
| 10 |
$gspeech_hh_enc = ""; |
| 11 |
|
| 12 |
class GSpeeech_Processor { |
| 13 |
|
| 14 |
private static function process_install() { |
| 15 |
|
| 16 |
if(!is_admin()) |
| 17 |
return; |
| 18 |
|
| 19 |
global $wpdb; |
| 20 |
|
| 21 |
$query = "SHOW TABLES LIKE '".$wpdb->prefix."gspeech_data'"; |
| 22 |
$wpdb->get_results($query); |
| 23 |
$num_r = $wpdb->num_rows; |
| 24 |
|
| 25 |
$plugin_version = '2.0.0'; |
| 26 |
|
| 27 |
if($num_r > 0) { |
| 28 |
|
| 29 |
$sql_g = "SELECT * FROM ".$wpdb->prefix."gspeech_data"; |
| 30 |
$row_g = $wpdb->get_row($sql_g); |
| 31 |
$plugin_version = $row_g->plugin_version; |
| 32 |
} |
| 33 |
|
| 34 |
$plg__ = explode('.', $plugin_version); |
| 35 |
$plg_v_1 = $plg__[0]; |
| 36 |
$plg_v_2 = isset($plg__[1]) ? $plg__[1] : 0; |
| 37 |
$plg_v_3 = isset($plg__[2]) ? $plg__[2] : 0; |
| 38 |
|
| 39 |
$sh_ = ($plg_v_1 == 1 || ($plg_v_1 == 3 && $plg_v_2 == 0)) ? 1 : 0; |
| 40 |
|
| 41 |
$gspeech_db_version = get_option("gspeech_db_version"); |
| 42 |
|
| 43 |
$current_db_version = 1; |
| 44 |
$new_db_version = GSPEECH_NEW_DB_VER; |
| 45 |
$current_db_version = intval($gspeech_db_version) == 0 ? $current_db_version : $gspeech_db_version; |
| 46 |
|
| 47 |
if($current_db_version < $new_db_version) { |
| 48 |
|
| 49 |
self::process_db(); |
| 50 |
|
| 51 |
if(!$gspeech_db_version) |
| 52 |
add_option("gspeech_db_version", $new_db_version); |
| 53 |
else |
| 54 |
update_option("gspeech_db_version", $new_db_version); |
| 55 |
} |
| 56 |
|
| 57 |
// update sh_ |
| 58 |
if($sh_ == 1) { |
| 59 |
$sql = "UPDATE `".$wpdb->prefix."gspeech_data` SET `sh_` = '1'"; |
| 60 |
$wpdb->query($sql); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private static function process_db() { |
| 65 |
|
| 66 |
global $wpdb; |
| 67 |
|
| 68 |
$plg_v = GSPEECH_PLG_VERSION; |
| 69 |
|
| 70 |
$domain = get_site_url() ?? ''; |
| 71 |
$m_ = get_option('admin_email', '') ?? ''; |
| 72 |
$n_ = get_option('blogname', '') ?? ''; |
| 73 |
$str = 'domain=' . $domain . '&email=' . $m_ . '&name=' . $n_ . '&version=' . $plg_v . '&plugin=gsp_backend'; |
| 74 |
$d_ = base64_encode($str); |
| 75 |
|
| 76 |
require_once(ABSPATH . '/wp-admin/includes/upgrade.php'); |
| 77 |
|
| 78 |
$query = "SHOW TABLES LIKE '".$wpdb->prefix."gspeech_data'"; |
| 79 |
$wpdb->get_results($query); |
| 80 |
$num_r = $wpdb->num_rows; |
| 81 |
|
| 82 |
if ($num_r == 0) { |
| 83 |
$sql = |
| 84 |
" |
| 85 |
CREATE TABLE `".$wpdb->prefix."gspeech_data` ( |
| 86 |
`widget_id` text NOT NULL, |
| 87 |
`lazy_load` tinyint(3) UNSIGNED NOT NULL, |
| 88 |
`crypto` text NOT NULL, |
| 89 |
`reload_session` tinyint(3) UNSIGNED NOT NULL, |
| 90 |
`plugin_version` text NOT NULL, |
| 91 |
`version_index` mediumint(8) UNSIGNED NOT NULL, |
| 92 |
`email` text NOT NULL, |
| 93 |
`sh_` tinyint(3) UNSIGNED NOT NULL DEFAULT '0', |
| 94 |
`sh_w_loaded` tinyint(3) UNSIGNED NOT NULL DEFAULT '0', |
| 95 |
`plan` tinyint(3) UNSIGNED NOT NULL DEFAULT '0', |
| 96 |
`appsumo` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' |
| 97 |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
| 98 |
"; |
| 99 |
dbDelta($sql); |
| 100 |
|
| 101 |
$sql = "INSERT IGNORE INTO `".$wpdb->prefix."gspeech_data` (`widget_id`, `lazy_load`, `crypto`, `reload_session`, `plugin_version`, `version_index`, `email`, `sh_`, `sh_w_loaded`, `plan`, `appsumo`) VALUES('', 1, '', 0, '".$plg_v."', 0, '', 0, 0, 0, 0)"; |
| 102 |
$wpdb->query($sql); |
| 103 |
|
| 104 |
// Store all fields in wp_options (only if not already set) |
| 105 |
if (false === get_option('gspeech_widget_id', false)) { |
| 106 |
update_option('gspeech_widget_id', ''); |
| 107 |
} |
| 108 |
if (false === get_option('gspeech_lazy_load', false)) { |
| 109 |
update_option('gspeech_lazy_load', 1); |
| 110 |
} |
| 111 |
if (false === get_option('gspeech_crypto', false)) { |
| 112 |
update_option('gspeech_crypto', ''); |
| 113 |
} |
| 114 |
if (false === get_option('gspeech_reload_session', false)) { |
| 115 |
update_option('gspeech_reload_session', 0); |
| 116 |
} |
| 117 |
if (false === get_option('gspeech_plugin_version', false)) { |
| 118 |
update_option('gspeech_plugin_version', $plg_v); |
| 119 |
} |
| 120 |
if (false === get_option('gspeech_version_index', false)) { |
| 121 |
update_option('gspeech_version_index', 0); |
| 122 |
} |
| 123 |
if (false === get_option('gspeech_email', false)) { |
| 124 |
update_option('gspeech_email', ''); |
| 125 |
} |
| 126 |
if (false === get_option('gspeech_sh_', false)) { |
| 127 |
update_option('gspeech_sh_', 0); |
| 128 |
} |
| 129 |
if (false === get_option('gspeech_sh_w_loaded', false)) { |
| 130 |
update_option('gspeech_sh_w_loaded', 0); |
| 131 |
} |
| 132 |
if (false === get_option('gspeech_plan', false)) { |
| 133 |
update_option('gspeech_plan', 0); |
| 134 |
} |
| 135 |
if (false === get_option('gspeech_appsumo', false)) { |
| 136 |
update_option('gspeech_appsumo', 0); |
| 137 |
} |
| 138 |
} else { |
| 139 |
// Migrate existing data to options |
| 140 |
$row_g = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."gspeech_data"); |
| 141 |
|
| 142 |
if (false === get_option('gspeech_widget_id', false)) { |
| 143 |
update_option('gspeech_widget_id', $row_g->widget_id); |
| 144 |
} |
| 145 |
if (false === get_option('gspeech_lazy_load', false)) { |
| 146 |
update_option('gspeech_lazy_load', $row_g->lazy_load); |
| 147 |
} |
| 148 |
if (false === get_option('gspeech_crypto', false)) { |
| 149 |
update_option('gspeech_crypto', $row_g->crypto); |
| 150 |
} |
| 151 |
if (false === get_option('gspeech_reload_session', false)) { |
| 152 |
update_option('gspeech_reload_session', $row_g->reload_session); |
| 153 |
} |
| 154 |
if (false === get_option('gspeech_plugin_version', false)) { |
| 155 |
update_option('gspeech_plugin_version', $row_g->plugin_version); |
| 156 |
} |
| 157 |
if (false === get_option('gspeech_version_index', false)) { |
| 158 |
update_option('gspeech_version_index', $row_g->version_index); |
| 159 |
} |
| 160 |
if (false === get_option('gspeech_email', false)) { |
| 161 |
update_option('gspeech_email', $row_g->email); |
| 162 |
} |
| 163 |
if (false === get_option('gspeech_sh_', false)) { |
| 164 |
update_option('gspeech_sh_', $row_g->sh_); |
| 165 |
} |
| 166 |
if (false === get_option('gspeech_sh_w_loaded', false)) { |
| 167 |
update_option('gspeech_sh_w_loaded', $row_g->sh_w_loaded); |
| 168 |
} |
| 169 |
if (false === get_option('gspeech_plan', false)) { |
| 170 |
update_option('gspeech_plan', $row_g->plan); |
| 171 |
} |
| 172 |
if (false === get_option('gspeech_appsumo', false)) { |
| 173 |
update_option('gspeech_appsumo', $row_g->appsumo); |
| 174 |
} |
| 175 |
|
| 176 |
// Preserve column-adding logic for upgrades |
| 177 |
$query = "SHOW COLUMNS FROM `".$wpdb->prefix."gspeech_data` LIKE 'email'"; |
| 178 |
$rows = $wpdb->get_results($query); |
| 179 |
|
| 180 |
if (sizeof($rows) == 0) { |
| 181 |
$sql = "ALTER TABLE `".$wpdb->prefix."gspeech_data` "; |
| 182 |
$sql .= "ADD `email` TEXT NOT NULL AFTER `version_index`, "; |
| 183 |
$sql .= "ADD `sh_` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `email`, "; |
| 184 |
$sql .= "ADD `sh_w_loaded` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `sh_`, "; |
| 185 |
$sql .= "ADD `plan` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `sh_w_loaded`, "; |
| 186 |
$sql .= "ADD `appsumo` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `plan`"; |
| 187 |
$wpdb->query($sql); |
| 188 |
|
| 189 |
// Update options with default values for new columns |
| 190 |
if (false === get_option('gspeech_email', false)) { |
| 191 |
update_option('gspeech_email', ''); |
| 192 |
} |
| 193 |
if (false === get_option('gspeech_sh_', false)) { |
| 194 |
update_option('gspeech_sh_', 0); |
| 195 |
} |
| 196 |
if (false === get_option('gspeech_sh_w_loaded', false)) { |
| 197 |
update_option('gspeech_sh_w_loaded', 0); |
| 198 |
} |
| 199 |
if (false === get_option('gspeech_plan', false)) { |
| 200 |
update_option('gspeech_plan', 0); |
| 201 |
} |
| 202 |
if (false === get_option('gspeech_appsumo', false)) { |
| 203 |
update_option('gspeech_appsumo', 0); |
| 204 |
} |
| 205 |
} else { |
| 206 |
$query = "SHOW COLUMNS FROM `".$wpdb->prefix."gspeech_data` LIKE 'plan'"; |
| 207 |
$rows = $wpdb->get_results($query); |
| 208 |
|
| 209 |
if (sizeof($rows) == 0) { |
| 210 |
$sql = "ALTER TABLE `".$wpdb->prefix."gspeech_data` "; |
| 211 |
$sql .= "ADD `plan` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `sh_w_loaded`"; |
| 212 |
$wpdb->query($sql); |
| 213 |
|
| 214 |
if (false === get_option('gspeech_plan', false)) { |
| 215 |
update_option('gspeech_plan', 0); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
$query = "SHOW COLUMNS FROM `".$wpdb->prefix."gspeech_data` LIKE 'appsumo'"; |
| 220 |
$rows = $wpdb->get_results($query); |
| 221 |
|
| 222 |
if (sizeof($rows) == 0) { |
| 223 |
$sql = "ALTER TABLE `".$wpdb->prefix."gspeech_data` "; |
| 224 |
$sql .= "ADD `appsumo` tinyint(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `plan`"; |
| 225 |
$wpdb->query($sql); |
| 226 |
|
| 227 |
if (false === get_option('gspeech_appsumo', false)) { |
| 228 |
update_option('gspeech_appsumo', 0); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
$sql = "UPDATE `".$wpdb->prefix."gspeech_data` SET `plugin_version` = '".$plg_v."', `version_index` = `version_index` + 1"; |
| 234 |
$wpdb->query($sql); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private static function process_enc_data() { |
| 239 |
|
| 240 |
if (is_admin()) { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
global $gspeech_s_enc, $gspeech_h_enc, $gspeech_hh_enc; |
| 245 |
|
| 246 |
$gspeech_s_enc = ""; |
| 247 |
$gspeech_h_enc = ""; |
| 248 |
$gspeech_hh_enc = ""; |
| 249 |
} |
| 250 |
|
| 251 |
public static function init() { |
| 252 |
|
| 253 |
self::process_install(); |
| 254 |
|
| 255 |
add_action('init', [__CLASS__, 'run_process_enc_data'], 10); |
| 256 |
} |
| 257 |
|
| 258 |
public static function run_process_enc_data() { |
| 259 |
|
| 260 |
self::process_enc_data(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
GSpeeech_Processor::init(); |
| 265 |
|