/** * When installed, this plugin converts RichText fields into BetterRedactor * fields. It also creates a folder, craft/config/redactor_plugins, and * populates it with some starting plugins. */ public function onAfterInstall() { craft()->db->createCommand()->update('fields', array('type' => 'BetterRedactor'), array('type' => 'RichText')); $config_folder = craft()->path->getConfigPath() . '/redactor_plugins'; if (!IOHelper::folderExists($config_folder)) { $initial_folder = craft()->path->getPluginsPath() . '/betterredactor/redactor_plugins'; $files = array_filter(scandir($initial_folder), function ($file) use($initial_folder) { return is_file("{$initial_folder}/{$file}"); }); foreach ($files as $file) { if (preg_match('((.js|.css)$)i', $file)) { IOHelper::copyFile("{$initial_folder}/{$file}", "{$config_folder}/{$file}"); } } } }
/** * @param string $unzipFolder * * @throws Exception * @return array */ private function _validateNewRequirements($unzipFolder) { $requirementsFolderPath = $unzipFolder . '/app/etc/requirements/'; $requirementsFile = $requirementsFolderPath . 'Requirements.php'; $errors = array(); if (!IOHelper::fileExists($requirementsFile)) { throw new Exception(Craft::t('The Requirements file is required and it does not exist at {path}.', array('path' => $requirementsFile))); } // Make sure we can write to craft/app/requirements if (!IOHelper::isWritable(craft()->path->getAppPath() . 'etc/requirements/')) { throw new Exception(StringHelper::parseMarkdown(Craft::t('Craft CMS needs to be able to write to your craft/app/etc/requirements folder and cannot. Please check your [permissions]({url}).', array('url' => 'http://craftcms.com/docs/updating#one-click-updating')))); } $tempFileName = StringHelper::UUID() . '.php'; // Make a dupe of the requirements file and give it a random file name. IOHelper::copyFile($requirementsFile, $requirementsFolderPath . $tempFileName); $newTempFilePath = craft()->path->getAppPath() . 'etc/requirements/' . $tempFileName; // Copy the random file name requirements to the requirements folder. // We don't want to execute any PHP from the storage folder. IOHelper::copyFile($requirementsFolderPath . $tempFileName, $newTempFilePath); require_once $newTempFilePath; $checker = new RequirementsChecker(); $checker->run(); if ($checker->getResult() == RequirementResult::Failed) { foreach ($checker->getRequirements() as $requirement) { if ($requirement->getResult() == InstallStatus::Failed) { Craft::log('Requirement "' . $requirement->getName() . '" failed with the message: ' . $requirement->getNotes(), LogLevel::Error, true); $errors[] = $requirement->getNotes(); } } } // Cleanup IOHelper::deleteFile($newTempFilePath); return $errors; }
/** * @param $destination * * @return bool */ public function copy($destination) { if (!IOHelper::copyFile($this->getRealPath(), $destination)) { return false; } return true; }
/** * Replace physical file. * * @param AssetFileModel $oldFile The assetFileModel representing the original file. * @param AssetFileModel $replaceWith The assetFileModel representing the new file. * * @return null */ public function replaceFile(AssetFileModel $oldFile, AssetFileModel $replaceWith) { if ($oldFile->kind == 'image') { craft()->assetTransforms->deleteThumbnailsForFile($oldFile); $this->deleteSourceFile($oldFile->getFolder()->path . $oldFile->filename); $this->purgeCachedSourceFile($oldFile->getFolder(), $oldFile->filename); // For remote sources, fetch the source image and move it in the old ones place if (!$this->isSourceLocal()) { $localCopy = $this->getLocalCopy($replaceWith); if ($oldFile->kind == "image") { IOHelper::copyFile($localCopy, craft()->path->getAssetsImageSourcePath() . $oldFile->id . '.' . IOHelper::getExtension($oldFile->filename)); } IOHelper::deleteFile($localCopy); } } $this->moveSourceFile($replaceWith, craft()->assets->getFolderById($oldFile->folderId), $oldFile->filename, true); // Update file info $oldFile->width = $replaceWith->width; $oldFile->height = $replaceWith->height; $oldFile->size = $replaceWith->size; $oldFile->dateModified = $replaceWith->dateModified; craft()->assets->storeFile($oldFile); }
/** * @param $manifestData * @param $sourceTempFolder * @param $handle * * @return bool */ public static function doFileUpdate($manifestData, $sourceTempFolder, $handle) { if ($handle == 'craft') { $destDirectory = craft()->path->getAppPath(); $sourceFileDirectory = 'app/'; } else { $destDirectory = craft()->path->getPluginsPath() . $handle . '/'; $sourceFileDirectory = ''; } try { foreach ($manifestData as $row) { if (static::isManifestVersionInfoLine($row)) { continue; } $folder = false; $rowData = explode(';', $row); if (static::isManifestLineAFolder($rowData[0])) { $folder = true; $tempPath = static::cleanManifestFolderLine($rowData[0]); } else { $tempPath = $rowData[0]; } $destFile = IOHelper::normalizePathSeparators($destDirectory . $tempPath); $sourceFile = IOHelper::getRealPath(IOHelper::normalizePathSeparators($sourceTempFolder . '/' . $sourceFileDirectory . $tempPath)); switch (trim($rowData[1])) { // update the file case PatchManifestFileAction::Add: if ($folder) { Craft::log('Updating folder: ' . $destFile, LogLevel::Info, true); $tempFolder = rtrim($destFile, '/') . StringHelper::UUID() . '/'; $tempTempFolder = rtrim($destFile, '/') . '-tmp/'; IOHelper::createFolder($tempFolder); IOHelper::copyFolder($sourceFile, $tempFolder); IOHelper::rename($destFile, $tempTempFolder); IOHelper::rename($tempFolder, $destFile); IOHelper::clearFolder($tempTempFolder); IOHelper::deleteFolder($tempTempFolder); } else { Craft::log('Updating file: ' . $destFile, LogLevel::Info, true); IOHelper::copyFile($sourceFile, $destFile); } break; } } } catch (\Exception $e) { Craft::log('Error updating files: ' . $e->getMessage(), LogLevel::Error); UpdateHelper::rollBackFileChanges($manifestData, $handle); return false; } return true; }
/** * Store a local image copy to a destination path. * * @param $localCopy * @param $destination * * @return null */ public function storeLocalSource($localCopy, $destination) { $maxCachedImageSize = $this->getCachedCloudImageSize(); // Resize if constrained by maxCachedImageSizes setting if ($maxCachedImageSize > 0 && ImageHelper::isImageManipulatable($localCopy)) { craft()->images->loadImage($localCopy, $maxCachedImageSize, $maxCachedImageSize)->scaleToFit($maxCachedImageSize, $maxCachedImageSize)->setQuality(100)->saveAs($destination); } else { IOHelper::copyFile($localCopy, $destination); } }
/** * Replace physical file. * * @param AssetFileModel $oldFile The assetFileModel representing the original file. * @param AssetFileModel $replaceWith The assetFileModel representing the new file. * @param string $filenameToUse The filename to use for the replaced file. If left * empty, will use the name of the new file. * * @return null */ public function replaceFile(AssetFileModel $oldFile, AssetFileModel $replaceWith, $filenameToUse = "") { if ($oldFile->kind == 'image') { craft()->assetTransforms->deleteAllTransformData($oldFile); $this->deleteSourceFile($oldFile->getFolder()->path . $oldFile->filename); $this->purgeCachedSourceFile($oldFile->getFolder(), $oldFile->filename); // For remote sources, fetch the source image and move it in the old ones place if (!$this->isSourceLocal()) { if ($replaceWith->kind == 'image') { $localCopy = $replaceWith->getTransformSource(); IOHelper::copyFile($localCopy, craft()->path->getAssetsImageSourcePath() . $oldFile->id . '.' . IOHelper::getExtension($oldFile->filename)); } } } $newFileName = !empty($filenameToUse) ? $filenameToUse : $oldFile->filename; $folder = craft()->assets->getFolderById($oldFile->folderId); $filenameChanges = StringHelper::toLowerCase($newFileName) != StringHelper::toLowerCase($replaceWith->filename); // If the filename does not change, this can trigger errors in some source types. if ($filenameChanges) { $this->moveSourceFile($replaceWith, $folder, $newFileName, true); } // Update file info $oldFile->width = $replaceWith->width; $oldFile->height = $replaceWith->height; $oldFile->size = $replaceWith->size; $oldFile->kind = $replaceWith->kind; $oldFile->dateModified = $replaceWith->dateModified; $oldFile->filename = $newFileName; if (empty($filenameToUse)) { $replaceWith->filename = $this->getNameReplacement($folder, $replaceWith->filename); craft()->assets->storeFile($replaceWith); } else { // If the file name has not changed, we're reusing the source file, // so we have to prevent deletion of source file here. craft()->assets->deleteFiles($replaceWith->id, $filenameChanges); } craft()->assets->storeFile($oldFile); }
/** * @inheritDoc BaseAssetSourceType::copySourceFile() * * @param string $sourceUri * @param string $targetUri * * @return bool */ protected function copySourceFile($sourceUri, $targetUri) { if ($sourceUri == $targetUri) { return true; } return IOHelper::copyFile($this->getSourceFileSystemPath() . $sourceUri, $this->getSourceFileSystemPath() . $targetUri, true); }
/** * Attempt to backup each of the update manifest files by copying them to a file with the same name with a .bak extension. * If there is an exception thrown, we attempt to roll back all of the changes. * * @access private * @param $unzipFolder * @return bool */ private function _backupFiles($unzipFolder) { $manifestData = UpdateHelper::getManifestData($unzipFolder); try { foreach ($manifestData as $row) { if (UpdateHelper::isManifestVersionInfoLine($row)) { continue; } // No need to back up migration files. if (UpdateHelper::isManifestMigrationLine($row)) { continue; } $rowData = explode(';', $row); $filePath = IOHelper::normalizePathSeparators(craft()->path->getAppPath() . $rowData[0]); // It's a folder if (UpdateHelper::isManifestLineAFolder($filePath)) { $folderPath = UpdateHelper::cleanManifestFolderLine($filePath); if (IOHelper::folderExists($folderPath)) { Craft::log('Backing up folder ' . $folderPath, LogLevel::Info, true); IOHelper::createFolder($folderPath . '.bak'); IOHelper::copyFolder($folderPath . '/', $folderPath . '.bak/'); } } else { // If the file doesn't exist, it's probably a new file. if (IOHelper::fileExists($filePath)) { Craft::log('Backing up file ' . $filePath, LogLevel::Info, true); IOHelper::copyFile($filePath, $filePath . '.bak'); } } } } catch (\Exception $e) { Craft::log('Error updating files: ' . $e->getMessage(), LogLevel::Error); UpdateHelper::rollBackFileChanges($manifestData); return false; } return true; }
/** * Copy a transform for a file from source location to target location. * * @param AssetFileModel $file * @param $source * @param $target * @return mixed */ public function copyTransform(AssetFileModel $file, $source, $target) { $fileFolder = $file->getFolder(); $basePath = $this->_getSourceFileSystemPath() . $fileFolder->fullPath; IOHelper::copyFile($basePath . $source . '/' . $file->filename, $basePath . $target . '/' . $file->filename); }