示例#1
0
 public function uploadApplianceBackup($archive_files)
 {
     // parse the given URL
     $url_aux = $this->site_url . "/upload";
     // mastersite url to upload file
     $url = parse_url($url_aux);
     if ($url['scheme'] != 'http') {
         die('Error: Only HTTP request are supported !');
     }
     // extract host and path:
     $host = $url['host'];
     $port = isset($url['port']) ? $url['port'] : 80;
     $path = $url['path'];
     $data = "";
     $boundary = "---------------------" . substr(md5(rand(0, 32000)), 0, 10);
     //Collect post data (login info)
     $this->loadSettings();
     $postdata = array('username' => $this->username, 'password' => $this->password, 'serial_number' => $this->serial_number);
     //Collect Postdata
     foreach ($postdata as $key => $val) {
         $data .= "--{$boundary}\r\n";
         $data .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n\r\n" . $val . "\r\n";
     }
     $data .= "--{$boundary}\r\n";
     $files = array('file' => array('name' => self::ARCHIVE_FILE));
     $gz_options = array('basedir' => $this->archive_base_dir, 'overwrite' => 1, 'storepaths' => 0, 'level' => 3);
     /*
      * simulate compress and check for file size archive
      */
     $this->setStage(self::BACKUP_STAGE, self::ARCHIVE_BACKUP);
     $create_gz = new gzip_file(Appliance::ARCHIVE_FILE);
     $create_gz->set_options(array_merge(array('count' => 1), $gz_options));
     $create_gz->add_files($archive_files);
     $create_gz->create_archive();
     $archive_size = $create_gz->get_count();
     $this->upload_archive_size = $archive_size;
     $file_post_data = array();
     foreach ($files as $key => $file) {
         $file_post_data[0] = "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\r\n";
         $file_post_data[1] = "Content-Type: application/octect-stream\r\n";
         $file_post_data[2] = "Content-Transfer-Encoding: binary\r\n\r\n";
         $file_post_data[3] = "\r\n";
         $file_post_data[4] = "--{$boundary}--\r\n";
     }
     $content_size = $archive_size + strlen($data) + strlen(implode($file_post_data));
     $this->setStage(self::BACKUP_STAGE, self::UPLOAD_BACKUP);
     // open a socket connection
     $fp = fsockopen($host, $port, $errno, $errstr);
     if (!$fp) {
         $error_msg = "Could not connect to host. {$errstr} ({$errno})";
         return array('success' => false, 'info' => $error_msg, 'error' => $error_msg);
     }
     fputs($fp, "POST {$path} HTTP/1.1\r\n");
     fputs($fp, "Host: {$host}\r\n");
     fputs($fp, "Content-Type: multipart/form-data; boundary={$boundary}\r\n");
     fputs($fp, "Content-length: " . $content_size . "\r\n");
     fputs($fp, "Connection: close\r\n\r\n");
     fputs($fp, $data);
     foreach ($files as $key => $file) {
         $create_gz = new gzip_file(Appliance::ARCHIVE_BACKUP);
         $create_gz->set_options(array_merge(array('stream' => 1, 'cbk' => array($this, 'uploadApplianceProgress'), 'socket' => $fp), $gz_options));
         $create_gz->add_files($archive_files);
         fputs($fp, $file_post_data[0]);
         fputs($fp, $file_post_data[1]);
         fputs($fp, $file_post_data[2]);
         $create_gz->create_archive();
         fputs($fp, $file_post_data[3]);
         fputs($fp, $file_post_data[4]);
     }
     $result = '';
     while (!feof($fp)) {
         // receive the results of the request
         $result .= fgets($fp, 128);
     }
     // close the socket connection:
     fclose($fp);
     // split the result header from the content
     $result = explode("\r\n\r\n", $result, 2);
     $header = isset($result[0]) ? $result[0] : '';
     $content = isset($result[1]) ? $result[1] : '';
     $response_status = explode(" ", $header);
     $response_status_code = $response_status[1];
     $decoded = json_decode($content, true);
     if ($decoded) {
         if ($decoded['success']) {
             $decoded['serial_number'] = $this->serial_number;
             $decoded['success'] = true;
             return $decoded;
         } else {
             $error = $decoded['response']['errorMessage'];
             return array('success' => false, 'agent' => 'MASTERSITE', 'info' => 'An error occurred! ' . $error, 'error' => $error);
         }
     }
     if ($response_status_code != 200) {
         return array('success' => false, 'agent' => 'MASTERSITE', 'info' => 'An error occurred! ' . $error, 'error' => $error);
     }
     return array('success' => true, 'agent' => 'MASTERSITE');
     // status 200 send ok
 }