| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Common\Helper; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Common\Exception\IOException; |
| 6 |
/** |
| 7 |
* This class provides helper functions to help with the file system operations |
| 8 |
* like files/folders creation & deletion. |
| 9 |
*/ |
| 10 |
class FileSystemHelper implements FileSystemHelperInterface |
| 11 |
{ |
| 12 |
/** @var string Real path of the base folder where all the I/O can occur */ |
| 13 |
protected $baseFolderRealPath; |
| 14 |
/** |
| 15 |
* @param string $baseFolderPath The path of the base folder where all the I/O can occur |
| 16 |
*/ |
| 17 |
public function __construct(string $baseFolderPath) |
| 18 |
{ |
| 19 |
$this->baseFolderRealPath = \realpath($baseFolderPath); |
| 20 |
} |
| 21 |
/** |
| 22 |
* Creates an empty folder with the given name under the given parent folder. |
| 23 |
* |
| 24 |
* @param string $parentFolderPath The parent folder path under which the folder is going to be created |
| 25 |
* @param string $folderName The name of the folder to create |
| 26 |
* |
| 27 |
* @throws \OpenSpout\Common\Exception\IOException If unable to create the folder or if the folder path is not inside of the base folder |
| 28 |
* |
| 29 |
* @return string Path of the created folder |
| 30 |
*/ |
| 31 |
public function createFolder($parentFolderPath, $folderName) |
| 32 |
{ |
| 33 |
$this->throwIfOperationNotInBaseFolder($parentFolderPath); |
| 34 |
$folderPath = $parentFolderPath . '/' . $folderName; |
| 35 |
$wasCreationSuccessful = \mkdir($folderPath, 0777, \true); |
| 36 |
if (!$wasCreationSuccessful) { |
| 37 |
throw new IOException("Unable to create folder: {$folderPath}"); |
| 38 |
} |
| 39 |
return $folderPath; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Creates a file with the given name and content in the given folder. |
| 43 |
* The parent folder must exist. |
| 44 |
* |
| 45 |
* @param string $parentFolderPath The parent folder path where the file is going to be created |
| 46 |
* @param string $fileName The name of the file to create |
| 47 |
* @param string $fileContents The contents of the file to create |
| 48 |
* |
| 49 |
* @throws \OpenSpout\Common\Exception\IOException If unable to create the file or if the file path is not inside of the base folder |
| 50 |
* |
| 51 |
* @return string Path of the created file |
| 52 |
*/ |
| 53 |
public function createFileWithContents($parentFolderPath, $fileName, $fileContents) |
| 54 |
{ |
| 55 |
$this->throwIfOperationNotInBaseFolder($parentFolderPath); |
| 56 |
$filePath = $parentFolderPath . '/' . $fileName; |
| 57 |
$wasCreationSuccessful = \file_put_contents($filePath, $fileContents); |
| 58 |
if (\false === $wasCreationSuccessful) { |
| 59 |
throw new IOException("Unable to create file: {$filePath}"); |
| 60 |
} |
| 61 |
return $filePath; |
| 62 |
} |
| 63 |
/** |
| 64 |
* Delete the file at the given path. |
| 65 |
* |
| 66 |
* @param string $filePath Path of the file to delete |
| 67 |
* |
| 68 |
* @throws \OpenSpout\Common\Exception\IOException If the file path is not inside of the base folder |
| 69 |
*/ |
| 70 |
public function deleteFile($filePath) |
| 71 |
{ |
| 72 |
$this->throwIfOperationNotInBaseFolder($filePath); |
| 73 |
if (\file_exists($filePath) && \is_file($filePath)) { |
| 74 |
\unlink($filePath); |
| 75 |
} |
| 76 |
} |
| 77 |
/** |
| 78 |
* Delete the folder at the given path as well as all its contents. |
| 79 |
* |
| 80 |
* @param string $folderPath Path of the folder to delete |
| 81 |
* |
| 82 |
* @throws \OpenSpout\Common\Exception\IOException If the folder path is not inside of the base folder |
| 83 |
*/ |
| 84 |
public function deleteFolderRecursively($folderPath) |
| 85 |
{ |
| 86 |
$this->throwIfOperationNotInBaseFolder($folderPath); |
| 87 |
$itemIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folderPath, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); |
| 88 |
foreach ($itemIterator as $item) { |
| 89 |
if ($item->isDir()) { |
| 90 |
\rmdir($item->getPathname()); |
| 91 |
} else { |
| 92 |
\unlink($item->getPathname()); |
| 93 |
} |
| 94 |
} |
| 95 |
\rmdir($folderPath); |
| 96 |
} |
| 97 |
/** |
| 98 |
* All I/O operations must occur inside the base folder, for security reasons. |
| 99 |
* This function will throw an exception if the folder where the I/O operation |
| 100 |
* should occur is not inside the base folder. |
| 101 |
* |
| 102 |
* @param string $operationFolderPath The path of the folder where the I/O operation should occur |
| 103 |
* |
| 104 |
* @throws \OpenSpout\Common\Exception\IOException If the folder where the I/O operation should occur |
| 105 |
* is not inside the base folder or the base folder does not exist |
| 106 |
*/ |
| 107 |
protected function throwIfOperationNotInBaseFolder(string $operationFolderPath) |
| 108 |
{ |
| 109 |
$operationFolderRealPath = \realpath($operationFolderPath); |
| 110 |
if (!$this->baseFolderRealPath) { |
| 111 |
throw new IOException("The base folder path is invalid: {$this->baseFolderRealPath}"); |
| 112 |
} |
| 113 |
$isInBaseFolder = 0 === \strpos($operationFolderRealPath, $this->baseFolderRealPath); |
| 114 |
if (!$isInBaseFolder) { |
| 115 |
throw new IOException("Cannot perform I/O operation outside of the base folder: {$this->baseFolderRealPath}"); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|