Skip to content

Commit

Permalink
Fix inventory download link (glpi-project#9380)
Browse files Browse the repository at this point in the history
* Fix inventory download link
Fix inventory getInventoryFileName() method usage

* use param for dir prepending, and create dir only on writing operations

* Fix returned value

* Fix

* Update inc/features/inventoriable.class.php

Co-authored-by: Guillaume Bougard <[email protected]>

Co-authored-by: Cédric Anne <[email protected]>
  • Loading branch information
g-bougard and cedric-anne authored Aug 23, 2021
1 parent e47bb81 commit b8cebd2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion front/inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
if (isset($_GET['refused'])) {
$refused = new RefusedEquipment();
$refused->getFromDB($_GET['refused']);
$contents = file_get_contents(GLPI_INVENTORY_DIR . '/' . $refused->getInventoryFileName());
$contents = file_get_contents($refused->getInventoryFileName());
} else if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$inventory_request->addError('Method not allowed');
$handle = false;
Expand Down
29 changes: 19 additions & 10 deletions inc/features/inventoriable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,32 @@ public function pre_purgeInventory() {
}


public function getInventoryFileName() {
/**
* Get inventory file name.
*
* @param bool $prepend_dir_path Indicated wether the GLPI_INVENTORY_DIR have to be prepend to returned value.
*
* @return void|string
*/
public function getInventoryFileName(bool $prepend_dir_path = true): ?string {
if (!$this->isDynamic()) {
return;
return null;
}

$inventory_dir_path = GLPI_INVENTORY_DIR . '/';

$conf = new Conf();
//most files will be XML for now
$download_file = $conf->buildInventoryFileName(static::getType(), $this->fields['id'], 'xml');
if (!file_exists($download_file)) {
$download_file = $conf->buildInventoryFileName(static::getType(), $this->fields['id'], 'json');
if (!file_exists($download_file)) {
Toolbox::logWarning('Inventory file missing: ' . $download_file);
$download_file = null;
$filename = $conf->buildInventoryFileName(static::getType(), $this->fields['id'], 'xml');
if (!file_exists($inventory_dir_path . $filename)) {
$filename = $conf->buildInventoryFileName(static::getType(), $this->fields['id'], 'json');
if (!file_exists($inventory_dir_path . $filename)) {
Toolbox::logWarning('Inventory file missing: ' . $filename);
return null;
}
}

return $download_file;
return ($prepend_dir_path ? $inventory_dir_path : '') . $filename;
}

/**
Expand All @@ -87,7 +96,7 @@ protected function showInventoryInfo() {
echo '<tr>';
echo '<th colspan="4">'.__('Inventory information');

$download_file = $this->getInventoryFileName();
$download_file = $this->getInventoryFileName(false);
if ($download_file !== null) {
$href = sprintf(
"%s/front/document.send.php?file=_inventory/%s",
Expand Down
17 changes: 3 additions & 14 deletions inc/inventory/conf.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,22 +687,11 @@ function getRights($interface = 'central') {
*/
public function buildInventoryFileName($itemtype, $items_id, $ext): string {
$files_per_dir = 1000;
$num_subdir = floor($items_id / $files_per_dir);

$subdir = sprintf(
'%s/%s/%s',
GLPI_INVENTORY_DIR,
Toolbox::slugify($itemtype),
$num_subdir
);

if (!is_dir($subdir)) {
mkdir($subdir, 0755, true);
}

return sprintf(
'%s/%s.%s',
$subdir,
'%s/%s/%s.%s',
Toolbox::slugify($itemtype),
floor($items_id / $files_per_dir),
$items_id,
$ext
);
Expand Down
6 changes: 5 additions & 1 deletion inc/inventory/inventory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ private function handleInventoryFile() {
}
$id = $item->fields['id'];

$filename = $this->conf->buildInventoryFileName($itemtype, $id, $ext);
$filename = GLPI_INVENTORY_DIR . '/' . $this->conf->buildInventoryFileName($itemtype, $id, $ext);
$subdir = dirname($filename);
if (!is_dir($subdir)) {
mkdir($subdir, 0755, true);
}
copy($tmpfile, $filename);
}

Expand Down
2 changes: 1 addition & 1 deletion inc/ruleimportassetcollection.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function prepareInputDataForProcess($input, $params) {
$refused->getFromDB($refused_id);

$inventory_request = new \Glpi\Inventory\Request();
$contents = file_get_contents(GLPI_INVENTORY_DIR . '/' . $refused->getInventoryFileName());
$contents = file_get_contents($refused->getInventoryFileName());
$inventory_request
->testRules()
->handleRequest($contents);
Expand Down

0 comments on commit b8cebd2

Please sign in to comment.