static function removeFileInRepo($sha512, $orders_id)
 {
     $repoPath = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository/";
     $manifestsPath = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/";
     $order = new PluginFusioninventoryDeployOrder();
     $rows = $order->find("id != '{$orders_id}'\n            AND json LIKE '%" . substr($sha512, 0, 6) . "%'\n            AND json LIKE '%{$sha512}%'");
     if (count($rows) > 0) {
         //file found in other order, do not remove part in repo
         return FALSE;
     }
     //get current order json
     $datas = json_decode(PluginFusioninventoryDeployOrder::getJson($orders_id), TRUE);
     $multiparts = $datas['associatedFiles'][$sha512]['multiparts'];
     //parse all files part
     foreach ($multiparts as $part_sha512) {
         $dir = $repoPath . self::getDirBySha512($part_sha512) . '/';
         //delete file parts
         unlink($dir . $part_sha512);
     }
     return TRUE;
 }
 /**
  * Used to export package
  *
  */
 function exportPackage($packages_id)
 {
     $this->getFromDB($packages_id);
     if (empty($this->fields['uuid'])) {
         $input = array('id' => $this->fields['id'], 'uuid' => Rule::getUuid());
         $this->update($input);
     }
     $pfDeployOrder = new PluginFusioninventoryDeployOrder();
     $pfDeployFile = new PluginFusioninventoryDeployFile();
     // Generate JSON
     $a_xml = array();
     $input = $this->fields;
     unset($input['id']);
     $a_xml['package'] = $input;
     $a_xml['orders'] = array();
     $a_xml['files'] = array();
     $a_xml['manifests'] = array();
     $a_xml['repository'] = array();
     $a_files = array();
     $a_data = $pfDeployOrder->find("`plugin_fusioninventory_deploypackages_id`='" . $this->fields['id'] . "'");
     foreach ($a_data as $data) {
         unset($data['id']);
         unset($data['plugin_fusioninventory_deploypackages_id']);
         $a_xml['orders'][] = $data;
         $json = json_decode($data['json'], true);
         $a_files = array_merge($a_files, $json['associatedFiles']);
     }
     // Add files
     foreach ($a_files as $files_id => $data) {
         $a_pkgfiles = current($pfDeployFile->find("`sha512`='" . $files_id . "'", '', 1));
         if (count($a_pkgfiles) > 0) {
             unset($a_pkgfiles['id']);
             $a_xml['files'][] = $a_pkgfiles;
         }
     }
     // Create zip with JSON and files
     $name = preg_replace("/[^a-zA-Z0-9]/", '', $this->fields['name']);
     $filename = GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/export/" . $this->fields['uuid'] . "." . $name . ".zip";
     if (file_exists($filename)) {
         unlink($filename);
     }
     $zip = new ZipArchive();
     if ($zip->open($filename) == TRUE) {
         if ($zip->open($filename, ZipArchive::CREATE) == TRUE) {
             $zip->addEmptyDir('files');
             $zip->addEmptyDir('files/manifests');
             $zip->addEmptyDir('files/repository');
             foreach ($a_files as $hash => $data) {
                 $sha512 = file_get_contents(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/" . $hash);
                 $sha512 = trim($sha512);
                 $zip->addFile(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/manifests/" . $hash, "files/manifests/" . $hash);
                 $a_xml['manifests'][] = $hash;
                 $file = PluginFusioninventoryDeployFile::getDirBySha512($sha512) . "/" . $sha512;
                 $zip->addFile(GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/repository/" . $file, "files/repository/" . $file);
                 $a_xml['repository'][] = $file;
             }
             $json_string = json_encode($a_xml);
             $zip->addFromString('information.json', $json_string);
         }
         $zip->close();
         Session::addMessageAfterRedirect(__("Package exported in", "fusioninventory") . " " . GLPI_PLUGIN_DOC_DIR . "/fusioninventory/files/export/" . $this->fields['uuid'] . "." . $name . ".zip");
     }
 }