getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; error_log($message); echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } public function is_wpvivid_backup($file_name) { if (preg_match('/wpvivid-.*_.*_.*\.zip$/', $file_name)) { return true; } else { return false; } } function is_backup_file_free() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } try { $file_name=sanitize_text_field($_POST['file_name']); if (isset($file_name)) { if ($this->is_wpvivid_backup($file_name)) { $ret['result'] = WPVIVID_SUCCESS; $backupdir=WPvivid_Setting::get_backupdir(); $filePath = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.$backupdir.DIRECTORY_SEPARATOR.$file_name; if(file_exists($filePath)) { $ret['is_exists']=true; } else { $ret['is_exists']=false; } } else { $ret['result'] = WPVIVID_FAILED; $ret['error'] = $file_name . ' is not created by WPvivid backup plugin.'; } } else { $ret['result'] = WPVIVID_FAILED; $ret['error'] = 'Failed to post file name.'; } echo wp_json_encode($ret); } catch (Exception $error) { $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } function upload_files_finish_free() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } try { $ret = $this->_rescan_local_folder_set_backup(); echo wp_json_encode($ret); } catch (Exception $error) { $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; error_log($message); echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } function delete_upload_incomplete_backup() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } try { if(isset($_POST['incomplete_backup'])&&!empty($_POST['incomplete_backup'])) { $json = sanitize_text_field($_POST['incomplete_backup']); $json = stripslashes($json); $incomplete_backup = json_decode($json, true); if(is_array($incomplete_backup) && !empty($incomplete_backup)) { $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR; foreach ($incomplete_backup as $backup) { $backup = basename($backup); if (preg_match('/wpvivid-.*_.*_.*\.zip$/', $backup)) { @wp_delete_file($path.$backup); } else if(preg_match('/'.apply_filters('wpvivid_white_label_file_prefix', 'wpvivid').'-.*_.*_.*\.zip$/', $backup)) { @wp_delete_file($path.$backup); } } } $ret['result']='success'; echo wp_json_encode($ret); } } catch (Exception $error) { $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; error_log($message); echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } function get_file_id() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } $file_name=sanitize_text_field($_POST['file_name']); if(isset($file_name)) { if(preg_match('/wpvivid-.*_.*_.*\.zip$/',$file_name)) { if(preg_match('/wpvivid-(.*?)_/',$file_name,$matches)) { $id= $matches[0]; $id=substr($id,0,strlen($id)-1); if(WPvivid_Backuplist::get_backup_by_id($id)===false) { $ret['result']=WPVIVID_SUCCESS; $ret['id']=$id; } else { $ret['result']=WPVIVID_FAILED; $ret['error']='The uploading backup already exists in Backups list.'; } } else { $ret['result']=WPVIVID_FAILED; $ret['error']=$file_name . ' is not created by WPvivid backup plugin.'; } } else { $ret['result']=WPVIVID_FAILED; $ret['error']=$file_name . ' is not created by WPvivid backup plugin.'; } } else { $ret['result']=WPVIVID_FAILED; $ret['error']='Failed to post file name.'; } echo wp_json_encode($ret); die(); } function check_file_is_a_wpvivid_backup($file_name,&$backup_id) { if(preg_match('/wpvivid-.*_.*_.*\.zip$/',$file_name)) { if(preg_match('/wpvivid-(.*?)_/',$file_name,$matches)) { $id= $matches[0]; $id=substr($id,0,strlen($id)-1); if(WPvivid_Backuplist::get_backup_by_id($id)===false) { $backup_id=$id; return true; } else { return false; } } else { return false; } } else { return false; } } function upload_files() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } try { $chunk = isset($_REQUEST["chunk"]) ? intval(sanitize_key($_REQUEST["chunk"])) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval(sanitize_key($_REQUEST["chunks"])) : 0; $fileName = isset($_REQUEST["name"]) ? sanitize_text_field($_REQUEST["name"]) : $_FILES["file"]["name"]; $validate = wp_check_filetype( $fileName ); if ( $validate['type'] == false ) { echo wp_json_encode(array('result'=>'failed','error'=>"File type is not allowed.")); die(); } $backupdir=WPvivid_Setting::get_backupdir(); $filePath = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.$backupdir.DIRECTORY_SEPARATOR.$fileName; $out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab"); if ($out) { // Read binary input stream and append it to temp file $options['test_form'] =true; $options['action'] ='wpvivid_upload_files'; $options['test_type'] = false; $options['ext'] = 'zip'; $options['type'] = 'application/zip'; add_filter('upload_dir', array($this, 'upload_dir')); $status = wp_handle_upload($_FILES['async-upload'],$options); remove_filter('upload_dir', array($this, 'upload_dir')); $in = @fopen($status['file'], "rb"); if ($in) { while ($buff = fread($in, 4096)) fwrite($out, $buff); } else { echo wp_json_encode(array('result'=>'failed','error'=>"Failed to open tmp file.path:".$status['file'])); die(); } @fclose($in); @fclose($out); @wp_delete_file($status['file']); } else { echo wp_json_encode(array('result'=>'failed','error'=>"Failed to open input stream.path:{$filePath}.part")); die(); } if (!$chunks || $chunk == $chunks - 1) { // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); } echo wp_json_encode(array('result' => WPVIVID_SUCCESS)); } catch (Exception $error) { $message = 'An exception has occurred. class: '.get_class($error).';msg: '.$error->getMessage().';code: '.$error->getCode().';line: '.$error->getLine().';in_file: '.$error->getFile().';'; error_log($message); echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } function upload_files_finish() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } $ret['html']=false; if(isset($_POST['files'])) { $files=sanitize_text_field($_POST['files']); $files =stripslashes($files); $files =json_decode($files,true); if(is_null($files)) { die(); } $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR; $backup_data['result']='success'; $backup_data['files']=array(); if(preg_match('/wpvivid-.*_.*_.*\.zip$/',$files[0]['name'])) { if(preg_match('/wpvivid-(.*?)_/',$files[0]['name'],$matches_id)) { if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}/',$files[0]['name'],$matches)) { $backup_time=$matches[0]; $time_array=explode('-',$backup_time); if(sizeof($time_array)>4) $time=$time_array[0].'-'.$time_array[1].'-'.$time_array[2].' '.$time_array[3].':'.$time_array[4]; else $time=$backup_time; $time=strtotime($time); } else { $time=time(); } $id= $matches_id[0]; $id=substr($id,0,strlen($id)-1); $unlinked_file = ''; $check_result=true; foreach ($files as $file) { $res=$this->check_is_a_wpvivid_backup($path.$file['name']); if($res === true) { $add_file['file_name']=$file['name']; $add_file['size']=filesize($path.$file['name']); $backup_data['files'][]=$add_file; } else { $check_result=false; $unlinked_file .= 'file name: '.$file['name'].', error: '.$res; } } if($check_result === true){ WPvivid_Backuplist::add_new_upload_backup($id,$backup_data,$time,''); $html = ''; $html = apply_filters('wpvivid_add_backup_list', $html); $ret['result']=WPVIVID_SUCCESS; $ret['html'] = $html; } else{ foreach ($files as $file) { $this->clean_tmp_files($path, $file['name']); @wp_delete_file($path . $file['name']); } $ret['result']=WPVIVID_FAILED; $ret['error']='Upload file failed.'; $ret['unlinked']=$unlinked_file; } } else { $ret['result']=WPVIVID_FAILED; $ret['error']='The backup is not created by WPvivid backup plugin.'; } } else { $ret['result']=WPVIVID_FAILED; $ret['error']='The backup is not created by WPvivid backup plugin.'; } } else{ $ret['result']=WPVIVID_FAILED; $ret['error']='Failed to post file name.'; } echo wp_json_encode($ret); die(); } function clean_tmp_files($path, $filename){ $handler=opendir($path); if($handler===false) return; while(($file=readdir($handler))!==false) { if (!is_dir($path.$file) && preg_match('/wpvivid-.*_.*_.*\.tmp$/', $file)) { $iPos = strrpos($file, '_'); $file_temp = substr($file, 0, $iPos); if($file_temp === $filename) { @wp_delete_file($path.$file); } } } @closedir($handler); } function wpvivid_check_remove_update_backup($path){ $backup_list = WPvivid_Setting::get_option('wpvivid_backup_list'); $remove_backup_array = array(); $update_backup_array = array(); $tmp_file_array = array(); $remote_backup_list=WPvivid_Backuplist::get_has_remote_backuplist(); foreach ($backup_list as $key => $value){ if(!in_array($key, $remote_backup_list)) { $need_remove = true; $need_update = false; if (is_dir($path)) { $handler = opendir($path); if($handler===false) return true; while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { if (!is_dir($path . $filename)) { if ($this->check_wpvivid_file_info($filename, $backup_id, $need_update)) { if ($key === $backup_id) { $need_remove = false; } if ($need_update) { if ($this->check_is_a_wpvivid_backup($path . $filename) === true) { if (!in_array($filename, $tmp_file_array)) { $add_file['file_name'] = $filename; $add_file['size'] = filesize($path . $filename); $tmp_file_array[] = $filename; $update_backup_array[$backup_id]['files'][] = $add_file; } } } } } } } if ($handler) { @closedir($handler); } } if ($need_remove) { $remove_backup_array[] = $key; } } } $this->wpvivid_remove_update_local_backup_list($remove_backup_array, $update_backup_array); return true; } function check_wpvivid_file_info($file_name, &$backup_id, &$need_update=false){ if(preg_match('/wpvivid-.*_.*_.*\.zip$/',$file_name)) { if(preg_match('/wpvivid-(.*?)_/',$file_name,$matches)) { $id= $matches[0]; $id=substr($id,0,strlen($id)-1); $backup_id=$id; if(WPvivid_Backuplist::get_backup_by_id($id)===false) { $need_update = false; return true; } else { $need_update = true; return true; } } else { return false; } } else { return false; } } function wpvivid_remove_update_local_backup_list($remove_backup_array, $update_backup_array){ $backup_list = WPvivid_Setting::get_option('wpvivid_backup_list'); foreach ($remove_backup_array as $remove_backup_id){ unset($backup_list[$remove_backup_id]); } foreach ($update_backup_array as $update_backup_id => $data){ $backup_list[$update_backup_id]['backup']['files'] = $data['files']; } WPvivid_Setting::update_option('wpvivid_backup_list', $backup_list); } function _rescan_local_folder_set_backup(){ $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR; $this->wpvivid_check_remove_update_backup($path); $backups=array(); $count = 0; if(is_dir($path)) { $handler = opendir($path); if($handler!==false) { while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { $count++; if (is_dir($path . $filename)) { continue; } else { if($this->check_file_is_a_wpvivid_backup($filename,$backup_id)) { if($this->zip_check_sum($path . $filename)) { if($this->check_is_a_wpvivid_backup($path.$filename) === true) { $backups[$backup_id]['files'][]=$filename; } else { $ret['incomplete_backup'][] = $filename; } } } } } } if($handler) @closedir($handler); } } else{ $ret['result']=WPVIVID_FAILED; $ret['error']='Failed to get local storage directory.'; } if(!empty($backups)) { foreach ($backups as $backup_id =>$backup) { $backup_data['result']='success'; $backup_data['files']=array(); if(empty($backup['files'])) continue; $time=false; foreach ($backup['files'] as $file) { if($time===false) { if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}/',$file,$matches)) { $backup_time=$matches[0]; $time_array=explode('-',$backup_time); if(sizeof($time_array)>4) $time=$time_array[0].'-'.$time_array[1].'-'.$time_array[2].' '.$time_array[3].':'.$time_array[4]; else $time=$backup_time; $time=strtotime($time); } else { $time=time(); } } $add_file['file_name']=$file; $add_file['size']=filesize($path.$file); $backup_data['files'][]=$add_file; } WPvivid_Backuplist::add_new_upload_backup($backup_id,$backup_data,$time,''); } } $ret['result']=WPVIVID_SUCCESS; $html = ''; $tour = true; $html = apply_filters('wpvivid_add_backup_list', $html, 'wpvivid_backup_list', $tour); $ret['html'] = $html; return $ret; } function rescan_local_folder_set_backup() { check_ajax_referer( 'wpvivid_ajax', 'nonce' ); $check=current_user_can('manage_options'); $check=apply_filters('wpvivid_ajax_check_security',$check); if(!$check) { die(); } $ret = $this->_rescan_local_folder_set_backup(); echo wp_json_encode($ret); die(); } public function wpvivid_rebuild_backup_list(){ $this->_rescan_local_folder_set_backup(); } static function rescan_local_folder() { $backupdir=WPvivid_Setting::get_backupdir(); ?>