| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
// Exit if accessed directly |
| 5 |
if (!defined('ABSPATH')) |
| 6 |
exit; |
| 7 |
|
| 8 |
/** |
| 9 |
* Helper method to check if user is in the plugins page. |
| 10 |
* |
| 11 |
* @author |
| 12 |
* @since 1.4.0 |
| 13 |
* |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
function wpdbbkp_is_plugins_page() |
| 17 |
{ |
| 18 |
if (function_exists('get_current_screen')) { |
| 19 |
$screen = get_current_screen(); |
| 20 |
if (is_object($screen)) { |
| 21 |
if ($screen->id == 'plugins' || $screen->id == 'plugins-network') { |
| 22 |
return true; |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
function wpdbbkp_get_current_url() |
| 30 |
{ |
| 31 |
|
| 32 |
$link = "http"; |
| 33 |
|
| 34 |
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') { |
| 35 |
$link = "https"; |
| 36 |
} |
| 37 |
|
| 38 |
$link .= "://"; |
| 39 |
$link .= $_SERVER['HTTP_HOST']; |
| 40 |
$link .= $_SERVER['REQUEST_URI']; |
| 41 |
|
| 42 |
return $link; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* display deactivation logic on plugins page |
| 47 |
* |
| 48 |
* @since 1.4.0 |
| 49 |
*/ |
| 50 |
|
| 51 |
|
| 52 |
function wpdbbkp_add_deactivation_feedback_modal() |
| 53 |
{ |
| 54 |
|
| 55 |
|
| 56 |
if (!is_admin() && !wpdbbkp_is_plugins_page()) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$current_user = wp_get_current_user(); |
| 61 |
if (!($current_user instanceof WP_User)) { |
| 62 |
$email = ''; |
| 63 |
} else { |
| 64 |
$email = trim($current_user->user_email); |
| 65 |
} |
| 66 |
|
| 67 |
require_once WPDB_PATH . "includes/admin/deactivate-feedback.php"; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* send feedback via email |
| 73 |
* |
| 74 |
* @since 1.4.0 |
| 75 |
*/ |
| 76 |
function wpdbbkp_send_feedback() |
| 77 |
{ |
| 78 |
|
| 79 |
if (!isset($_POST['wpdbbkp_security_nonce'])) { |
| 80 |
return; |
| 81 |
} |
| 82 |
if (!wp_verify_nonce($_POST['wpdbbkp_security_nonce'], 'wpdbbkp-pub-nonce')) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
if (!current_user_can('manage_options')) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($_POST['data'])) { |
| 91 |
parse_str($_POST['data'], $form); |
| 92 |
} |
| 93 |
|
| 94 |
$text = ''; |
| 95 |
if (isset($form['wpdbbkp_disable_text'])) { |
| 96 |
$text = implode("\n\r", $form['wpdbbkp_disable_text']); |
| 97 |
} |
| 98 |
|
| 99 |
$headers = array(); |
| 100 |
|
| 101 |
$from = isset($form['wpdbbkp_disable_from']) ? $form['wpdbbkp_disable_from'] : ''; |
| 102 |
if ($from) { |
| 103 |
$headers[] = "From: $from"; |
| 104 |
$headers[] = "Reply-To: $from"; |
| 105 |
} |
| 106 |
|
| 107 |
$subject = isset($form['wpdbbkp_disable_reason']) ? $form['wpdbbkp_disable_reason'] : '(no reason given)'; |
| 108 |
|
| 109 |
$subject = $subject . ' - Backup for WP Publisher'; |
| 110 |
|
| 111 |
if ($subject == 'technical - Backup for WP Publisher') { |
| 112 |
|
| 113 |
$text = trim($text); |
| 114 |
|
| 115 |
if (!empty($text)) { |
| 116 |
|
| 117 |
$text = 'technical issue description: ' . $text; |
| 118 |
|
| 119 |
} else { |
| 120 |
|
| 121 |
$text = 'no description: ' . $text; |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
$success = wp_mail('team@magazine3.in', $subject, $text, $headers); |
| 127 |
|
| 128 |
wp_die(); |
| 129 |
} |
| 130 |
add_action('wp_ajax_wpdbbkp_send_feedback', 'wpdbbkp_send_feedback'); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
add_action('admin_enqueue_scripts', 'wpdbbkp_enqueue_makebetter_email_js'); |
| 135 |
|
| 136 |
function wpdbbkp_enqueue_makebetter_email_js() |
| 137 |
{ |
| 138 |
|
| 139 |
if (!is_admin() && !wpdbbkp_is_plugins_page()) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
wp_register_script('wpdbbkp-make-better-js', WPDB_PLUGIN_URL . '/assets/js/make-better-admin.js', array('jquery'), WPDB_VERSION, true); |
| 145 |
wp_localize_script( |
| 146 |
'wpdbbkp-make-better-js', |
| 147 |
'wpdbbkp_pub_script_vars', |
| 148 |
array( |
| 149 |
'nonce' => wp_create_nonce('wpdbbkp-pub-nonce') |
| 150 |
) |
| 151 |
); |
| 152 |
wp_enqueue_script('wpdbbkp-make-better-js'); |
| 153 |
wp_enqueue_style('wpdbbkp-make-better-css', WPDB_PLUGIN_URL . '/assets/css/make-better-admin.css', false, WPDB_VERSION); |
| 154 |
} |
| 155 |
|
| 156 |
add_filter('admin_footer', 'wpdbbkp_add_deactivation_feedback_modal'); |
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
* Read the contents of a file using the WordPress filesystem API. |
| 161 |
* |
| 162 |
* @param string $file_path The path to the file. |
| 163 |
* @return string|false The file contents or false on failure. |
| 164 |
*/ |
| 165 |
function wpdbbkp_read_file_contents($file_path) |
| 166 |
{ |
| 167 |
global $wp_filesystem; |
| 168 |
|
| 169 |
// Initialize the WordPress filesystem, no more using file_get_contents function |
| 170 |
if (empty($wp_filesystem)) { |
| 171 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 172 |
WP_Filesystem(); |
| 173 |
} |
| 174 |
|
| 175 |
// Check if the file exists and is readable |
| 176 |
if ($wp_filesystem->exists($file_path) && $wp_filesystem->is_readable($file_path)) { |
| 177 |
return $wp_filesystem->get_contents($file_path); |
| 178 |
} |
| 179 |
|
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Write data to a file using the WordPress File System API for smaller files and PHP native functions for large files. |
| 185 |
* |
| 186 |
* @param string $filename The path to the file to write data to. |
| 187 |
* @param string $data The data to write. |
| 188 |
* @param bool $append Whether to append the data to the file (default: false). |
| 189 |
*/ |
| 190 |
function wpdbbkp_write_file_contents($filename, $data, $append = false) |
| 191 |
{ |
| 192 |
global $wp_filesystem; |
| 193 |
|
| 194 |
if (!function_exists('WP_Filesystem')) { |
| 195 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 196 |
} |
| 197 |
|
| 198 |
if (!WP_Filesystem()) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Ensure the directory is writable. |
| 203 |
$dir = dirname($filename); |
| 204 |
if (!$wp_filesystem->exists($dir)) { |
| 205 |
$wp_filesystem->mkdir($dir); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
$file_size = $wp_filesystem->size($filename); |
| 210 |
|
| 211 |
// Use the WordPress File System API for small files. |
| 212 |
if ($file_size < 104857600) { // 100 MB as threshold for large files. |
| 213 |
// Check if the file exists; create it if it does not. |
| 214 |
if (!$wp_filesystem->exists($filename)) { |
| 215 |
$wp_filesystem->put_contents($filename, '', FS_CHMOD_FILE); |
| 216 |
} |
| 217 |
|
| 218 |
// Read current contents if appending. |
| 219 |
$current_content = $append ? $wp_filesystem->get_contents($filename) : ''; |
| 220 |
|
| 221 |
// Write the new data. |
| 222 |
$wp_filesystem->put_contents($filename, $current_content . $data, FS_CHMOD_FILE); |
| 223 |
} else { |
| 224 |
// Use native PHP functions for very large files. |
| 225 |
$mode = $append ? 'a' : 'w'; |
| 226 |
//phpcs:ignore -- using native PHP functions for large files. |
| 227 |
$file = fopen($filename, $mode); |
| 228 |
|
| 229 |
if ($file) { |
| 230 |
//phpcs:ignore -- using native PHP functions for large files. |
| 231 |
fwrite($file, $data); |
| 232 |
//phpcs:ignore -- using native PHP functions for large files. |
| 233 |
fclose($file); |
| 234 |
} else { |
| 235 |
error_log("Failed to open file for writing: $filename"); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Check if a directory is writable using the WordPress File System API. |
| 242 |
* |
| 243 |
* @param string $dir The path to the directory. |
| 244 |
* @return bool True if the directory is writable, false otherwise. |
| 245 |
*/ |
| 246 |
function wpdbbkp_is_writable($dir) |
| 247 |
{ |
| 248 |
global $wp_filesystem; |
| 249 |
|
| 250 |
if (!function_exists('WP_Filesystem')) { |
| 251 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 252 |
} |
| 253 |
|
| 254 |
if (!WP_Filesystem()) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
return $wp_filesystem->is_writable($dir); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Check if a file exists using the WordPress File System API. |
| 265 |
* |
| 266 |
* @param string $file The path to the file. |
| 267 |
* @return bool True if the file exists, false otherwise. |
| 268 |
*/ |
| 269 |
function wpdbbkp_file_exists($file) |
| 270 |
{ |
| 271 |
global $wp_filesystem; |
| 272 |
|
| 273 |
if (!function_exists('WP_Filesystem')) { |
| 274 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 275 |
} |
| 276 |
|
| 277 |
if (!WP_Filesystem()) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
return $wp_filesystem->exists($file); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Filters an array of backup files to ensure only unique filenames are included. |
| 286 |
* |
| 287 |
* @param array $backups Array of backup files. |
| 288 |
* @return array Filtered array of backup files with unique filenames. |
| 289 |
*/ |
| 290 |
function wpdbbkp_filter_unique_filenames($backups) |
| 291 |
{ |
| 292 |
$unique_filenames = []; |
| 293 |
$filtered_backups = []; |
| 294 |
|
| 295 |
if (!empty($backups)) { |
| 296 |
foreach ($backups as $backup) { |
| 297 |
if (isset($backup['filename']) && !in_array($backup['filename'], $unique_filenames)) { |
| 298 |
$unique_filenames[] = $backup['filename']; |
| 299 |
$filtered_backups[] = $backup; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return $filtered_backups; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* function to get current user name and email |
| 309 |
*/ |
| 310 |
function wpdbbkp_get_current_user_name_email() |
| 311 |
{ |
| 312 |
$current_user = wp_get_current_user(); |
| 313 |
if ($current_user) { |
| 314 |
if ($current_user instanceof WP_User) { |
| 315 |
if ($current_user->exists()) { |
| 316 |
return array('name' => base64_encode($current_user->display_name ? $current_user->display_name : $current_user->user_login), 'email' => base64_encode($current_user->user_email)); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return array('name' => '', 'email' => ''); |
| 322 |
} |
| 323 |
|
| 324 |
function wpdbbkp_if_remote_active() |
| 325 |
{ |
| 326 |
$remote = get_option('wp_db_backup_destination_cd', 0); |
| 327 |
$token = get_option('wpdb_clouddrive_token', false); |
| 328 |
if ($remote == 1 && $token) { |
| 329 |
return true; |
| 330 |
} |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
function wpdbbkp_save_remote_token() |
| 337 |
{ |
| 338 |
|
| 339 |
$json_response = array('status' => 'fail', 'message' => 'Something went wrong, please try again later.'); |
| 340 |
if (!isset($_POST['wpdbbkp_security_nonce'])) { |
| 341 |
$json_response['message'] = 'Invalid request'; |
| 342 |
return; |
| 343 |
} |
| 344 |
if (!wp_verify_nonce($_POST['wpdbbkp_security_nonce'], 'wpdbbkp_ajax_check_nonce')) { |
| 345 |
$json_response['message'] = 'Invalid request'; |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
if (!current_user_can('manage_options')) { |
| 350 |
$json_response['message'] = 'Unauthorized request'; |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
$token = isset($_POST['token']) ? wp_unslash($_POST['token']) : ''; |
| 355 |
|
| 356 |
if ($token) { |
| 357 |
update_option('wpdb_clouddrive_token', sanitize_text_field($token)); |
| 358 |
update_option('wp_db_backup_destination_cd', 1); |
| 359 |
$json_response['status'] = 'success'; |
| 360 |
$json_response['message'] = 'Token Saved'; |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
echo wp_json_encode($json_response); |
| 365 |
wp_die(); |
| 366 |
} |
| 367 |
add_action('wp_ajax_wpdbbkp_save_remote_token', 'wpdbbkp_save_remote_token'); |