/** * @return mixed */ public function isEmpty() { if (!$this->_isEmpty) { $this->_isEmpty = IOHelper::isFolderEmpty($this->getRealPath()); } return $this->_isEmpty; }
public function getTemplateFiles() { $folderEmpty = true; if (IOHelper::isFolderEmpty(craft()->path->getPluginsPath() . 'formbuilder2/templates/email/layouts')) { throw new HttpException(404, Craft::t('Looks like you don\'t have any templates in your email/layouts folder.')); } else { $folderEmpty = false; } $fileList = IOHelper::getFolderContents(craft()->path->getPluginsPath() . 'formbuilder2/templates/email/layouts'); $files = []; $filesModel = []; if (!$folderEmpty) { foreach ($fileList as $key => $file) { $files[$key] = ['fileName' => IOHelper::getFileName($file, false), 'fileOriginalName' => IOHelper::getFileName($file), 'fileNameCleaned' => IOHelper::cleanFilename(IOHelper::getFileName($file, false)), 'fileExtension' => IOHelper::getExtension($file), 'filePath' => $file, 'fileContents' => IOHelper::getFileContents($file)]; $filesModel[] = FormBuilder2_FileModel::populateModel($files[$key]); } } return $filesModel; }
/** * Remove any temp files and/or folders that might have been created. * * @param string $unzipFolder * @param string $handle * * @return null */ private function _cleanTempFiles($unzipFolder, $handle) { $path = $handle == 'craft' ? craft()->path->getAppPath() : craft()->path->getPluginsPath() . $handle . '/'; // Get rid of all the .bak files/folders. $filesToDelete = IOHelper::getFolderContents($path, true, ".*\\.bak\$"); // Now delete any files/folders that were marked for deletion in the manifest file. $manifestData = UpdateHelper::getManifestData($unzipFolder, $handle); if ($manifestData) { foreach ($manifestData as $row) { if (UpdateHelper::isManifestVersionInfoLine($row)) { continue; } $rowData = explode(';', $row); if ($rowData[1] == PatchManifestFileAction::Remove) { if (UpdateHelper::isManifestLineAFolder($rowData[0])) { $tempFilePath = UpdateHelper::cleanManifestFolderLine($rowData[0]); } else { $tempFilePath = $rowData[0]; } $filesToDelete[] = $path . $tempFilePath; } // In case we did the whole app folder if ($rowData[0][0] == '*') { $filesToDelete[] = rtrim(IOHelper::normalizePathSeparators($path), '/') . '.bak/'; } } foreach ($filesToDelete as $fileToDelete) { if (IOHelper::fileExists($fileToDelete)) { if (IOHelper::isWritable($fileToDelete)) { Craft::log('Deleting file: ' . $fileToDelete, LogLevel::Info, true); IOHelper::deleteFile($fileToDelete, true); // If that was the last file in this folder, nuke the folder. if (IOHelper::isFolderEmpty(IOHelper::getFolderName($fileToDelete))) { IOHelper::deleteFolder(IOHelper::getFolderName($fileToDelete)); } } } else { if (IOHelper::folderExists($fileToDelete)) { if (IOHelper::isWritable($fileToDelete)) { Craft::log('Deleting .bak folder:' . $fileToDelete, LogLevel::Info, true); IOHelper::clearFolder($fileToDelete, true); IOHelper::deleteFolder($fileToDelete, true); } } } } } // Clear the temp folder. IOHelper::clearFolder(craft()->path->getTempPath(), true); }
/** * @inheritDoc IZip::add() * * @param string $sourceZip * @param string $pathToAdd * @param string $basePath * @param null $pathPrefix * * @return bool */ public function add($sourceZip, $pathToAdd, $basePath, $pathPrefix = null) { $zip = new \ZipArchive(); $zipContents = $zip->open($sourceZip); if ($zipContents !== true) { Craft::log('Unable to open zip file: ' . $sourceZip, LogLevel::Error); return false; } if (IOHelper::fileExists($pathToAdd)) { $folderContents = array($pathToAdd); } else { $folderContents = IOHelper::getFolderContents($pathToAdd, true); } foreach ($folderContents as $itemToZip) { if (IOHelper::isReadable($itemToZip)) { // Figure out the relative path we'll be adding to the zip. $relFilePath = mb_substr($itemToZip, mb_strlen($basePath)); if ($pathPrefix) { $pathPrefix = IOHelper::normalizePathSeparators($pathPrefix); $relFilePath = $pathPrefix . $relFilePath; } if (IOHelper::folderExists($itemToZip)) { if (IOHelper::isFolderEmpty($itemToZip)) { $zip->addEmptyDir($relFilePath); } } elseif (IOHelper::fileExists($itemToZip)) { // We can't use $zip->addFile() here but it's a terrible, horrible, POS method that's buggy on Windows. $fileContents = IOHelper::getFileContents($itemToZip); if (!$zip->addFromString($relFilePath, $fileContents)) { Craft::log('There was an error adding the file ' . $itemToZip . ' to the zip: ' . $itemToZip, LogLevel::Error); } } } } $zip->close(); return true; }
/** * Will add either a file or a folder to an existing zip file. If it is a folder, it will add the contents recursively. * * @param string $sourceZip The zip file to be added to. * @param string $pathToAdd A file or a folder to add. If it is a folder, it will recursively add the contents of the folder to the zip. * @param string $basePath The root path of the file(s) to be added that will be removed before adding. * @param string $pathPrefix A path to be prepended to each file before it is added to the zip. * @return bool */ public function add($sourceZip, $pathToAdd, $basePath, $pathPrefix = null) { $zip = new \PclZip($sourceZip); if (IOHelper::fileExists($pathToAdd)) { $folderContents = array($pathToAdd); } else { $folderContents = IOHelper::getFolderContents($pathToAdd, true); } $filesToAdd = array(); foreach ($folderContents as $itemToZip) { if (IOHelper::isReadable($itemToZip)) { if (IOHelper::folderExists($itemToZip) && IOHelper::isFolderEmpty($itemToZip) || IOHelper::fileExists($itemToZip)) { $filesToAdd[] = $itemToZip; } } } if (!$pathPrefix) { $pathPrefix = ''; } $result = $zip->add($filesToAdd, PCLZIP_OPT_ADD_PATH, $pathPrefix, PCLZIP_OPT_REMOVE_PATH, $basePath); if ($result == 0) { Craft::log('Unable to add to zip file: ' . $sourceZip, LogLevel::Error); return false; } return true; }