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