94 {
95 $versions = count($this->file_object->getVersions());
96 if ($versions === 1) {
97 $version_one = $this->file_object->getDirectory(1) . "/" . $this->file_object->getFileName();
99 $version_two = $this->file_object->getDirectory(2) . "/" . $this->file_object->getFileName();
101 if (!$this->fs->has($relative_version_one) && $this->fs->has($relative_version_two)) {
102
103
104 }
105 }
106
108
109 $dirname = dirname($this->relative_path);
110 if (!$this->fs->has($dirname)) {
111 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: Directory not found");
112
113 return false;
114 }
115 try {
116 $first_file = reset($this->fs->listContents($dirname));
117 } catch (DirectoryNotFoundException $e) {
118 $this->
log(
"FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
119
120 return false;
121 }
123 try {
125
126 if (!$this->fs->has($valid_filename)) {
127 $this->fs->rename($first_file->getPath(), $valid_filename);
128
129
130 $this->
log(
"Sanitized File Path: {$valid_filename}");
131 }
133
134 return true;
135 } catch (Exception $e) {
136 $this->
log(
"FAILED: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}. Will try using native PHP");
137
138 try {
139
140 $scandir = scandir(dirname($this->absolute_path));
141 if (isset($scandir[2])) {
142 $first_file = $scandir[2];
143 if (is_file($first_file)) {
145 if (rename($first_file, $valid_filename)) {
147 $this->
log(
"Sanitized File Path: {$valid_filename}");
148 }
149 } else {
150 throw new Exception("is not a file: " . $first_file);
151 }
152 } else {
153 throw new Exception("no File found in " . dirname($this->absolute_path));
154 }
155 } catch (Exception $e) {
156 $this->
log(
"FAILED AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
157
158 try {
159 foreach (new DirectoryIterator(dirname($this->absolute_path)) as $item) {
160 if ($item->isDot()) {
161 continue;
162 }
163 if ($item->isFile()) {
165 if (rename($item->getPathname(), $valid_filename)) {
167 $this->
log(
"Sanitized File Path: {$valid_filename}");
168 }
169 break;
170 }
171 }
172 } catch (Exception $e) {
173 $this->
log(
"FAILED AGAIN and AGAIN: Sanitizing File Path: {$this->file_object->getFile()}. Message: {$e->getMessage()}");
174 }
175 }
176
177 return false;
178 }
179 }
180
181 return false;
182 }
183
184 return true;
185 }
saveNewNameForFileObject($valid_filename)
santitizeFilename($first_file)
Class FlySystemFileAccessTest.