refresh_token = $stream['host']; $this->access_token = $this->refresh_access_token(); $this->path = $stream['path']; $this->mode = $mode; $this->orig_path = $path; $pathinfo = pathinfo($this->path); $this->filename = $pathinfo['basename']; $dir = trim($pathinfo['dirname'], '/\\'); $this->backup_loc = (empty($dir) ? '' : $pathinfo['dirname']); //php://memory not working on localhost $this->tpfile = 'php://temp'; $ret = true; // if its a read mode the check if the file exists if(strpos($this->mode, 'r') !== FALSE){ $this->url_stat($path, ''); if(empty($this->filesize)){ return false; } } if(preg_match('/w|a/is', $this->mode)){ $this->offset = 0; $tfp = fopen($this->tpfile, $this->mode); if(empty($backuply['status']['init_data'])){ backuply_log('----------Creating a new session-------'); $ret = $this->upload_start($tfp); }else{ $ret = true; } fclose($tfp); } return $ret; } // Dropbox Create a file function upload_start($fp){ global $error, $backuply; $upload_url = 'https://content.dropboxapi.com/2/files/upload_session/start'; $headers = array('Authorization: Bearer '.$this->access_token, 'Dropbox-API-Arg: {"close": false}', 'Content-Type: application/octet-stream'); $resp = $this->__curl($upload_url, $headers, $fp, 0); if(empty($resp) || empty($resp['session_id'])){ $error[] = 'Was unable to create session with dropbox'; backuply_log('--------Was unable to create a session with dropbox-------'); return false; } $this->session_id = $resp['session_id']; $backuply['status']['init_data'] = $this->session_id; return true; } function stream_write($data){ global $error; if(!is_resource($this->wp)){ $this->wp = fopen($this->tpfile, 'w+'); } //Initially store the data in a memory fwrite($this->wp, $data); $this->tmpsize += strlen($data); $data_size = strlen($data); // Are we already more than 2 MB ? if($this->tmpsize >= $this->chunk){ rewind($this->wp); //Call upload append function to write the data from PHP Memory stream to Dropbox $this->upload_append($this->session_id, $this->wp, $this->tmpsize); // Close the temp file and reset the variables fclose($this->wp); $this->wp = NULL; $this->tmpsize = 0; } return $data_size; } // Dropbox API to upload function upload_append($session_id, $filep, $data_size){ global $error, $backuply; if(!empty($backuply['status']['init_data'])){ $this->session_id = $backuply['status']['init_data']; } if(!empty($GLOBALS['start_pos'])){ $this->offset = $GLOBALS['start_pos']; } $args = json_encode(array('cursor' => array('session_id' => $this->session_id, 'offset' => $this->offset), 'close' => false) ); $upload_url = 'https://content.dropboxapi.com/2/files/upload_session/append_v2'; $headers = array('Authorization: Bearer '.$this->access_token, 'Dropbox-API-Arg: '.$args, 'Content-Type: application/octet-stream'); $resp = $this->__curl($upload_url, $headers, $filep, $data_size); if(is_null($resp)){ $this->offset += $data_size; $GLOBALS['start_pos'] = $this->offset; return $data_size; } return false; } function stream_close(){ global $backuply, $error; if(preg_match('/w|a/is', $this->mode)){ // Is there still some data left to be written ? if($this->tmpsize > 0){ rewind($this->wp); // Call upload append function to write the data from PHP Memory stream to Dropbox $data_size = $this->upload_append($this->session_id, $this->wp, $this->tmpsize); // Close the temp file and reset the variables fclose($this->wp); $this->wp = NULL; $this->tmpsize = 0; } if(empty($backuply['status']['incomplete_upload'])){ $upload_url = 'https://content.dropboxapi.com/2/files/upload_session/finish'; $headers = array('Authorization: Bearer '.$this->access_token, 'Dropbox-API-Arg: {"cursor":{"session_id":"'.$this->session_id.'","offset":'.$this->offset.'},"commit":{"path":"'.$this->path.'","mode":"add","autorename": true,"mute": false}}', 'Content-Type: application/octet-stream'); $resp = $this->__curl($upload_url, $headers); } } return true; } //In response to file_exists(), is_file(), is_dir() function url_stat($path , $flags){ global $error; $stream = parse_url($path); $this->refresh_token = $stream['host']; if(empty($this->access_token)) { $this->access_token = $this->refresh_access_token(); } $pathinfo = pathinfo($stream['path']); $filename = $pathinfo['basename']; $dir = trim($pathinfo['dirname'], '/\\'); $path = (empty($dir) ? '' : $pathinfo['dirname']); //Metadata for the root folder is unsupported if(!empty($filename)){ $data = json_encode(array('path' => $path.'/'.$filename, 'include_media_info' => false, 'include_deleted' => false, 'include_has_explicit_shared_members' => false)); $url = 'https://api.dropboxapi.com/2/files/get_metadata'; $headers = array('Authorization: Bearer '.$this->access_token, 'Content-Type: application/json'); $resp = $this->__curl($url, $headers, '', 0, $data, '', 1); if($resp['.tag'] == 'file'){ $mode = 0100000; //For File }elseif($resp['.tag'] == 'folder'){ $mode = 0040000; //For DIR } if(!empty($resp['id'])){ $stat = array('dev' => 0, 'ino' => 0, 'mode' => $mode, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => $resp['size'], 'atime' => strtotime($resp['client_modified']), 'mtime' => strtotime($resp['client_modified']), 'ctime' => strtotime($resp['client_modified']), 'blksize' => 0, 'blocks' => 0 ); $this->filesize = $stat['size']; return $stat; } } return false; } // AS of now not used function stream_read($count){ if($count == ''){ return false; } if(empty($this->range_lower_limit)){ $this->range_lower_limit = 0; } $this->range_upper_limit = ($this->range_lower_limit + $count) - 1; if($this->range_upper_limit >= $this->filesize){ $this->range_upper_limit = $this->filesize - 1; } $tmp_file = backuply_glob('backups_info') . '/test.tmp'; $this->__write($this->path, $tmp_file, $this->range_lower_limit, $this->range_upper_limit); $resp = file_get_contents($tmp_file); @unlink($tmp_file); $this->offset = $this->range_upper_limit + 1; $this->range_lower_limit = $this->range_upper_limit + 1; return $resp; } //Download Backup File from Dropbox to local server function download_file_loop($source, $dest, $startpos = 0){ global $error, $data; $stream = parse_url($source); $this->refresh_token = $stream['host']; if(empty($this->access_token)) { $this->access_token = $this->refresh_access_token(); } $path = $stream['path']; //Set $this->filesize variable to remote tar file's size $this->url_stat($source, ''); $this->range_lower_limit = $startpos; $this->range_upper_limit = ($this->range_lower_limit + $this->chunk) - 1; while(!$this->__eof()){ if(time() >= $GLOBALS['end']){ //$GLOBALS['l_readbytes'] = filesize($dest); break; } if($this->range_upper_limit >= $this->filesize){ $this->range_upper_limit = $this->filesize - 1; } $this->__write($path, $dest, $this->range_lower_limit, $this->range_upper_limit); $this->offset = $this->range_upper_limit + 1; $this->range_lower_limit = $this->range_upper_limit + 1; $this->range_upper_limit = ($this->range_lower_limit + $this->chunk) - 1; $percentage = (filesize($dest) / $this->filesize) * 100; backuply_status_log('