/**
  * Save Form data
  */
 public static function save_post_form($tab, $jobid)
 {
     if (!current_user_can('backwpup_jobs_edit')) {
         return __('Sorry, you don\'t have permissions to do that.', 'backwpup');
     }
     $destinations = BackWPup::get_registered_destinations();
     $job_types = BackWPup::get_job_types();
     switch ($tab) {
         case 'job':
             BackWPup_Option::update($jobid, 'jobid', $jobid);
             if (isset($_POST['type']) && is_array($_POST['type'])) {
                 foreach ((array) $_POST['type'] as $typeid) {
                     if (empty($job_types[$typeid])) {
                         unset($_POST['type'][$typeid]);
                     }
                 }
                 if (is_array($_POST['type'])) {
                     sort($_POST['type']);
                 }
             } else {
                 $_POST['type'] = array();
             }
             //test if job type makes backup
             /* @var BackWPup_JobTypes $job_type */
             $makes_file = FALSE;
             foreach ($job_types as $type_id => $job_type) {
                 if (in_array($type_id, $_POST['type'])) {
                     if ($job_type->creates_file()) {
                         $makes_file = TRUE;
                         break;
                     }
                 }
             }
             if (!$makes_file) {
                 $_POST['destinations'] = array();
             }
             BackWPup_Option::update($jobid, 'type', $_POST['type']);
             if (isset($_POST['destinations']) && is_array($_POST['destinations'])) {
                 foreach ((array) $_POST['destinations'] as $key => $destid) {
                     if (empty($destinations[$destid])) {
                         //remove all destinations that not exists
                         unset($_POST['destinations'][$key]);
                     }
                     if (class_exists('BackWPup_Pro', FALSE) && $_POST['backuptype'] == 'sync') {
                         //if sync remove all not sync destinations
                         if (!$destinations[$destid]['can_sync']) {
                             unset($_POST['destinations'][$key]);
                         }
                     }
                 }
                 if (is_array($_POST['destinations'])) {
                     sort($_POST['destinations']);
                 }
             } else {
                 $_POST['destinations'] = array();
             }
             BackWPup_Option::update($jobid, 'destinations', $_POST['destinations']);
             $name = esc_html(trim($_POST['name']));
             if (empty($name) || $_POST['name'] == __('New Job', 'backwpup')) {
                 $name = sprintf(__('Job with ID %d', 'backwpup'), $jobid);
             }
             BackWPup_Option::update($jobid, 'name', $name);
             $emails = explode(',', $_POST['mailaddresslog']);
             foreach ($emails as $key => $email) {
                 $emails[$key] = sanitize_email(trim($email));
                 if (!is_email($emails[$key])) {
                     unset($emails[$key]);
                 }
             }
             $mailaddresslog = implode(', ', $emails);
             BackWPup_Option::update($jobid, 'mailaddresslog', $mailaddresslog);
             $_POST['mailaddresssenderlog'] = trim($_POST['mailaddresssenderlog']);
             if (empty($_POST['mailaddresssenderlog'])) {
                 BackWPup_Option::delete($jobid, 'mailaddresssenderlog');
             } else {
                 BackWPup_Option::update($jobid, 'mailaddresssenderlog', $_POST['mailaddresssenderlog']);
             }
             BackWPup_Option::update($jobid, 'mailerroronly', isset($_POST['mailerroronly']) && $_POST['mailerroronly'] == 1 ? TRUE : FALSE);
             if (class_exists('BackWPup_Pro', FALSE)) {
                 BackWPup_Option::update($jobid, 'backuptype', esc_html($_POST['backuptype']));
             } else {
                 BackWPup_Option::update($jobid, 'backuptype', 'archive');
             }
             BackWPup_Option::update($jobid, 'archiveformat', esc_html($_POST['archiveformat']));
             BackWPup_Option::update($jobid, 'archivename', BackWPup_Job::sanitize_file_name($_POST['archivename']));
             break;
         case 'cron':
             if ($_POST['activetype'] == '' || $_POST['activetype'] == 'wpcron' || $_POST['activetype'] == 'easycron' || $_POST['activetype'] == 'link') {
                 BackWPup_Option::update($jobid, 'activetype', $_POST['activetype']);
             }
             BackWPup_Option::update($jobid, 'cronselect', $_POST['cronselect'] == 'advanced' ? 'advanced' : 'basic');
             if (isset($_POST['cronselect']) && $_POST['cronselect'] == 'advanced') {
                 //save advanced
                 if (empty($_POST['cronminutes']) || $_POST['cronminutes'][0] == '*') {
                     if (!empty($_POST['cronminutes'][1])) {
                         $_POST['cronminutes'] = array('*/' . $_POST['cronminutes'][1]);
                     } else {
                         $_POST['cronminutes'] = array('*');
                     }
                 }
                 if (empty($_POST['cronhours']) || $_POST['cronhours'][0] == '*') {
                     if (!empty($_POST['cronhours'][1])) {
                         $_POST['cronhours'] = array('*/' . $_POST['cronhours'][1]);
                     } else {
                         $_POST['cronhours'] = array('*');
                     }
                 }
                 if (empty($_POST['cronmday']) || $_POST['cronmday'][0] == '*') {
                     if (!empty($_POST['cronmday'][1])) {
                         $_POST['cronmday'] = array('*/' . $_POST['cronmday'][1]);
                     } else {
                         $_POST['cronmday'] = array('*');
                     }
                 }
                 if (empty($_POST['cronmon']) || $_POST['cronmon'][0] == '*') {
                     if (!empty($_POST['cronmon'][1])) {
                         $_POST['cronmon'] = array('*/' . $_POST['cronmon'][1]);
                     } else {
                         $_POST['cronmon'] = array('*');
                     }
                 }
                 if (empty($_POST['cronwday']) || $_POST['cronwday'][0] == '*') {
                     if (!empty($_POST['cronwday'][1])) {
                         $_POST['cronwday'] = array('*/' . $_POST['cronwday'][1]);
                     } else {
                         $_POST['cronwday'] = array('*');
                     }
                 }
                 $cron = implode(",", $_POST['cronminutes']) . ' ' . implode(",", $_POST['cronhours']) . ' ' . implode(",", $_POST['cronmday']) . ' ' . implode(",", $_POST['cronmon']) . ' ' . implode(",", $_POST['cronwday']);
                 BackWPup_Option::update($jobid, 'cron', $cron);
             } else {
                 //Save basic
                 if ($_POST['cronbtype'] == 'mon') {
                     BackWPup_Option::update($jobid, 'cron', $_POST['moncronminutes'] . ' ' . $_POST['moncronhours'] . ' ' . $_POST['moncronmday'] . ' * *');
                 }
                 if ($_POST['cronbtype'] == 'week') {
                     BackWPup_Option::update($jobid, 'cron', $_POST['weekcronminutes'] . ' ' . $_POST['weekcronhours'] . ' * * ' . $_POST['weekcronwday']);
                 }
                 if ($_POST['cronbtype'] == 'day') {
                     BackWPup_Option::update($jobid, 'cron', $_POST['daycronminutes'] . ' ' . $_POST['daycronhours'] . ' * * *');
                 }
                 if ($_POST['cronbtype'] == 'hour') {
                     BackWPup_Option::update($jobid, 'cron', $_POST['hourcronminutes'] . ' * * * *');
                 }
             }
             //reschedule
             wp_clear_scheduled_hook('backwpup_cron', array('id' => $jobid));
             if (BackWPup_Option::get($jobid, 'activetype') == 'wpcron') {
                 $cron_next = BackWPup_Cron::cron_next(BackWPup_Option::get($jobid, 'cron'));
                 wp_schedule_single_event($cron_next, 'backwpup_cron', array('id' => $jobid));
             }
             $easy_cron_job_id = BackWPup_Option::get($jobid, 'easycronjobid');
             if (BackWPup_Option::get($jobid, 'activetype') == 'easycron') {
                 BackWPup_EasyCron::update($jobid);
             } elseif (!empty($easy_cron_job_id)) {
                 BackWPup_EasyCron::delete($jobid);
             }
             break;
         default:
             if (strstr($tab, 'dest-')) {
                 $dest_class = BackWPup::get_destination(str_replace('dest-', '', $tab));
                 $dest_class->edit_form_post_save($jobid);
             }
             if (strstr($tab, 'jobtype-')) {
                 $id = strtoupper(str_replace('jobtype-', '', $tab));
                 $job_types[$id]->edit_form_post_save($jobid);
             }
     }
     //saved message
     $messages = BackWPup_Admin::get_messages();
     if (empty($messages['error'])) {
         $url = BackWPup_Job::get_jobrun_url('runnowlink', $jobid);
         BackWPup_Admin::message(sprintf(__('Changes for job <i>%s</i> saved.', 'backwpup'), BackWPup_Option::get($jobid, 'name')) . ' <a href="' . network_admin_url('admin.php') . '?page=backwpupjobs">' . __('Jobs overview', 'backwpup') . '</a> | <a href="' . $url['url'] . '">' . __('Run now', 'backwpup') . '</a>');
     }
 }
 /**
  * @param $jobid
  */
 public function edit_form_post_save($jobid)
 {
     if (!empty($_POST['sugaremail']) && !empty($_POST['sugarpass']) && $_POST['authbutton'] === __('Authenticate with Sugarsync!', 'backwpup')) {
         try {
             $sugarsync = new BackWPup_Destination_SugarSync_API();
             $refresh_token = $sugarsync->get_Refresh_Token(sanitize_email($_POST['sugaremail']), $_POST['sugarpass']);
             if (!empty($refresh_token)) {
                 BackWPup_Option::update($jobid, 'sugarrefreshtoken', $refresh_token);
             }
         } catch (Exception $e) {
             BackWPup_Admin::message('SUGARSYNC: ' . $e->getMessage(), TRUE);
         }
     }
     if (isset($_POST['authbutton']) && $_POST['authbutton'] === __('Delete Sugarsync authentication!', 'backwpup')) {
         BackWPup_Option::delete($jobid, 'sugarrefreshtoken');
     }
     if (isset($_POST['authbutton']) && $_POST['authbutton'] === __('Create Sugarsync account', 'backwpup')) {
         try {
             $sugarsync = new BackWPup_Destination_SugarSync_API();
             $sugarsync->create_account(sanitize_email($_POST['sugaremail']), $_POST['sugarpass']);
         } catch (Exception $e) {
             BackWPup_Admin::message('SUGARSYNC: ' . $e->getMessage(), TRUE);
         }
     }
     $_POST['sugardir'] = trailingslashit(str_replace('//', '/', str_replace('\\', '/', trim(sanitize_text_field($_POST['sugardir'])))));
     if (substr($_POST['sugardir'], 0, 1) == '/') {
         $_POST['sugardir'] = substr($_POST['sugardir'], 1);
     }
     if ($_POST['sugardir'] == '/') {
         $_POST['sugardir'] = '';
     }
     BackWPup_Option::update($jobid, 'sugardir', $_POST['sugardir']);
     BackWPup_Option::update($jobid, 'sugarroot', isset($_POST['sugarroot']) ? sanitize_text_field($_POST['sugarroot']) : '');
     BackWPup_Option::update($jobid, 'sugarmaxbackups', isset($_POST['sugarmaxbackups']) ? absint($_POST['sugarmaxbackups']) : 0);
 }
 /**
  * @param $job_object
  *
  * @return bool
  */
 public function job_run_archive(BackWPup_Job $job_object)
 {
     $job_object->substeps_todo = 2 + $job_object->backup_filesize;
     if ($job_object->steps_data[$job_object->step_working]['SAVE_STEP_TRY'] != $job_object->steps_data[$job_object->step_working]['STEP_TRY']) {
         $job_object->log(sprintf(__('%d. Try to send backup file to Dropbox&#160;&hellip;', 'backwpup'), $job_object->steps_data[$job_object->step_working]['STEP_TRY']));
     }
     try {
         $dropbox = new BackWPup_Destination_Dropbox_API($job_object->job['dropboxroot']);
         // cahnge oauth1 to oauth2 token
         if (!empty($job_object->job['dropboxsecret']) && empty($job_object->job['dropboxtoken']['access_token'])) {
             $dropbox->setOAuthTokens(array('access_token' => $job_object->job['dropboxtoken'], 'oauth_token_secret' => BackWPup_Encryption::decrypt($job_object->job['dropboxsecret'])));
             $job_object->job['dropboxtoken'] = $dropbox->token_from_oauth1();
             BackWPup_Option::update($job_object->job['jobid'], 'dropboxtoken', $job_object->job['dropboxtoken']);
             BackWPup_Option::delete($job_object->job['jobid'], 'dropboxsecret');
         }
         // set the tokens
         $dropbox->setOAuthTokens($job_object->job['dropboxtoken']);
         //get account info
         if ($job_object->steps_data[$job_object->step_working]['SAVE_STEP_TRY'] != $job_object->steps_data[$job_object->step_working]['STEP_TRY']) {
             $info = $dropbox->accountInfo();
             if (!empty($info['uid'])) {
                 if ($job_object->is_debug()) {
                     $user = $info['display_name'] . ' (' . $info['email'] . ')';
                 } else {
                     $user = $info['display_name'];
                 }
                 $job_object->log(sprintf(__('Authenticated with Dropbox of user: %s', 'backwpup'), $user));
                 //Quota
                 if ($job_object->is_debug()) {
                     $dropboxfreespase = $info['quota_info']['quota'] - $info['quota_info']['shared'] - $info['quota_info']['normal'];
                     $job_object->log(sprintf(__('%s available on your Dropbox', 'backwpup'), size_format($dropboxfreespase, 2)));
                 }
             } else {
                 $job_object->log(__('Not Authenticated with Dropbox!', 'backwpup'), E_USER_ERROR);
                 return false;
             }
             $job_object->log(__('Uploading to Dropbox&#160;&hellip;', 'backwpup'));
         }
         // put the file
         self::$backwpup_job_object =& $job_object;
         if ($job_object->substeps_done < $job_object->backup_filesize) {
             //only if upload not complete
             $response = $dropbox->upload($job_object->backup_folder . $job_object->backup_file, $job_object->job['dropboxdir'] . $job_object->backup_file);
             if ($response['bytes'] == $job_object->backup_filesize) {
                 if (!empty($job_object->job['jobid'])) {
                     BackWPup_Option::update($job_object->job['jobid'], 'lastbackupdownloadurl', network_admin_url('admin.php') . '?page=backwpupbackups&action=downloaddropbox&file=' . ltrim($response['path'], '/') . '&jobid=' . $job_object->job['jobid']);
                 }
                 $job_object->substeps_done = 1 + $job_object->backup_filesize;
                 $job_object->log(sprintf(__('Backup transferred to %s', 'backwpup'), 'https://content.dropboxapi.com/1/files/' . $job_object->job['dropboxroot'] . $response['path']), E_USER_NOTICE);
             } else {
                 if ($response['bytes'] != $job_object->backup_filesize) {
                     $job_object->log(__('Uploaded file size and local file size don\'t match.', 'backwpup'), E_USER_ERROR);
                 } else {
                     $job_object->log(sprintf(__('Error transfering backup to %s.', 'backwpup') . ' ' . $response['error'], __('Dropbox', 'backwpup')), E_USER_ERROR);
                 }
                 return false;
             }
         }
         $backupfilelist = array();
         $filecounter = 0;
         $files = array();
         $metadata = $dropbox->metadata($job_object->job['dropboxdir']);
         if (is_array($metadata)) {
             foreach ($metadata['contents'] as $data) {
                 if ($data['is_dir'] != true) {
                     $file = basename($data['path']);
                     if ($job_object->is_backup_archive($file)) {
                         $backupfilelist[strtotime($data['modified'])] = $file;
                     }
                     $files[$filecounter]['folder'] = "https://content.dropboxapi.com/1/files/" . $job_object->job['dropboxroot'] . dirname($data['path']) . "/";
                     $files[$filecounter]['file'] = $data['path'];
                     $files[$filecounter]['filename'] = basename($data['path']);
                     $files[$filecounter]['downloadurl'] = network_admin_url('admin.php?page=backwpupbackups&action=downloaddropbox&file=' . $data['path'] . '&jobid=' . $job_object->job['jobid']);
                     $files[$filecounter]['filesize'] = $data['bytes'];
                     $files[$filecounter]['time'] = strtotime($data['modified']) + get_option('gmt_offset') * 3600;
                     $filecounter++;
                 }
             }
         }
         if ($job_object->job['dropboxmaxbackups'] > 0 && is_object($dropbox)) {
             //Delete old backups
             if (count($backupfilelist) > $job_object->job['dropboxmaxbackups']) {
                 ksort($backupfilelist);
                 $numdeltefiles = 0;
                 while ($file = array_shift($backupfilelist)) {
                     if (count($backupfilelist) < $job_object->job['dropboxmaxbackups']) {
                         break;
                     }
                     $response = $dropbox->fileopsDelete($job_object->job['dropboxdir'] . $file);
                     //delete files on Cloud
                     if ($response['is_deleted'] == 'true') {
                         foreach ($files as $key => $filedata) {
                             if ($filedata['file'] == '/' . $job_object->job['dropboxdir'] . $file) {
                                 unset($files[$key]);
                             }
                         }
                         $numdeltefiles++;
                     } else {
                         $job_object->log(sprintf(__('Error while deleting file from Dropbox: %s', 'backwpup'), $file), E_USER_ERROR);
                     }
                 }
                 if ($numdeltefiles > 0) {
                     $job_object->log(sprintf(_n('One file deleted from Dropbox', '%d files deleted on Dropbox', $numdeltefiles, 'backwpup'), $numdeltefiles), E_USER_NOTICE);
                 }
             }
         }
         set_site_transient('backwpup_' . $job_object->job['jobid'] . '_dropbox', $files, YEAR_IN_SECONDS);
     } catch (Exception $e) {
         $job_object->log(E_USER_ERROR, sprintf(__('Dropbox API: %s', 'backwpup'), $e->getMessage()), $e->getFile(), $e->getLine());
         return false;
     }
     $job_object->substeps_done++;
     return true;
 }
Exemple #4
0
 public static function status($backwpup_jobid)
 {
     if (empty($backwpup_jobid)) {
         $id = get_site_option('backwpup_cfg_easycronjobid');
     } else {
         $id = BackWPup_Option::get($backwpup_jobid, 'easycronjobid');
     }
     if (empty($id)) {
         return array();
     }
     $cron_job = get_site_transient('backwpup_easycron_' . $id);
     if (!empty($cron_job)) {
         return $cron_job;
     }
     $message = self::query_api('detail', array('id' => $id));
     if ($message['status'] == 'success' && !empty($message['cron_job'])) {
         set_site_transient('backwpup_easycron_' . $id, $message['cron_job'], 3600 - 30);
         return $message['cron_job'];
     } else {
         if ($message['error']['code'] == 25) {
             if (empty($backwpup_jobid)) {
                 delete_site_option('backwpup_cfg_easycronjobid');
             } else {
                 BackWPup_Option::delete($backwpup_jobid, 'easycronjobid');
             }
         }
     }
     return array();
 }