PluginProbe
RSVP and Event Management / 2.7.5
RSVP and Event Management v2.7.5
trunk 0.5.0 0.6.0 0.7.0 0.8.0 0.9.0 0.9.5 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.5.0 1.6.0 1.6.1 1.6.2 1.6.5 1.7.0 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 All 136 releases
rsvp / external-libs / spout / src / Spout / Common / Helper / FileSystemHelper.php

FileSystemHelper.php in RSVP and Event Management 2.7.5, at external-libs/spout/src/Spout/Common/Helper/FileSystemHelper.php

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