/** * @return mixed */ public function isWritable() { if (!$this->_isWritable) { $this->_isWritable = IOHelper::isWritable($this->getRealPath()); } return $this->_isWritable; }
public function onBeforeInstall() { $craftTemplateFolder = realpath(CRAFT_TEMPLATES_PATH); if (!IOHelper::isWritable($craftTemplateFolder)) { throw new Exception(Craft::t('Your Template folder is not writeable by PHP. ' . 'InstaBlog needs PHP to have permissions to create template files. Give PHP write permissions to ' . $craftTemplateFolder . ' and try Install again.')); } $sources = craft()->assetSources->getAllSourceIds(); if (empty($sources)) { throw new Exception(Craft::t('You don\'t have any asset sources set up. ' . 'InstaBlog needs an asset source to be defined. Please create an asset source ' . ' and try Install again.')); } }
public function onBeforeInstall() { if (!(craft()->getEdition() > 0 || craft()->sections->canHaveMore("channel"))) { throw new Exception(Craft::t('Your version of Craft only permits one channel section. ' . 'Please upgrade or remove the existing channel section before installing InstaBlog.')); } $craftTemplateFolder = realpath(CRAFT_TEMPLATES_PATH); if (!IOHelper::isWritable($craftTemplateFolder)) { throw new Exception(Craft::t('Your Template folder is not writeable by PHP. ' . 'InstaBlog needs PHP to have permissions to create template files. Give PHP write permissions to ' . $craftTemplateFolder . ' and try Install again.')); } $sources = craft()->assetSources->getAllSourceIds(); if (empty($sources)) { throw new Exception(Craft::t('You don\'t have any asset sources set up. ' . 'InstaBlog needs an asset source to be defined. Please create an asset source ' . ' and try Install again.')); } }
/** * @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; }
/** * Resolves a resource path to the actual file system path, or returns false if the resource cannot be found. * * @param string $path * * @throws HttpException * @return string */ public function getResourcePath($path) { $segs = explode('/', $path); // Special resource routing if (isset($segs[0])) { switch ($segs[0]) { case 'js': // Route to js/compressed/ if useCompressedJs is enabled // unless js/uncompressed/* is requested, in which case drop the uncompressed/ seg if (isset($segs[1]) && $segs[1] == 'uncompressed') { array_splice($segs, 1, 1); } else { if (craft()->config->get('useCompressedJs')) { array_splice($segs, 1, 0, 'compressed'); } } $path = implode('/', $segs); break; case 'userphotos': if (isset($segs[1]) && $segs[1] == 'temp') { if (!isset($segs[2])) { return false; } return craft()->path->getTempUploadsPath() . 'userphotos/' . $segs[2] . '/' . $segs[3]; } else { if (!isset($segs[3])) { return false; } $size = AssetsHelper::cleanAssetName($segs[2], false); // Looking for either a numeric size or "original" keyword if (!is_numeric($size) && $size != "original") { return false; } $username = AssetsHelper::cleanAssetName($segs[1], false); $filename = AssetsHelper::cleanAssetName($segs[3]); $userPhotosPath = craft()->path->getUserPhotosPath() . $username . '/'; $sizedPhotoFolder = $userPhotosPath . $size . '/'; $sizedPhotoPath = $sizedPhotoFolder . $filename; // If the photo doesn't exist at this size, create it. if (!IOHelper::fileExists($sizedPhotoPath)) { $originalPhotoPath = $userPhotosPath . 'original/' . $filename; if (!IOHelper::fileExists($originalPhotoPath)) { return false; } IOHelper::ensureFolderExists($sizedPhotoFolder); if (IOHelper::isWritable($sizedPhotoFolder)) { craft()->images->loadImage($originalPhotoPath)->resize($size)->saveAs($sizedPhotoPath); } else { Craft::log('Tried to write to target folder and could not: ' . $sizedPhotoFolder, LogLevel::Error); } } return $sizedPhotoPath; } case 'defaultuserphoto': return craft()->path->getResourcesPath() . 'images/user.svg'; case 'tempuploads': array_shift($segs); return craft()->path->getTempUploadsPath() . implode('/', $segs); case 'tempassets': array_shift($segs); return craft()->path->getAssetsTempSourcePath() . implode('/', $segs); case 'assetthumbs': if (empty($segs[1]) || empty($segs[2]) || !is_numeric($segs[1]) || !is_numeric($segs[2])) { return $this->_getBrokenImageThumbPath(); } $fileModel = craft()->assets->getFileById($segs[1]); if (empty($fileModel)) { return $this->_getBrokenImageThumbPath(); } $size = $segs[2]; try { return craft()->assetTransforms->getThumbServerPath($fileModel, $size); } catch (\Exception $e) { return $this->_getBrokenImageThumbPath(); } case 'icons': if (empty($segs[1]) || !preg_match('/^\\w+/i', $segs[1])) { return false; } return $this->_getIconPath($segs[1]); case 'rebrand': if (!in_array($segs[1], array('logo', 'icon'))) { return false; } return craft()->path->getRebrandPath() . $segs[1] . "/" . $segs[2]; case 'transforms': try { if (!empty($segs[1])) { $transformIndexModel = craft()->assetTransforms->getTransformIndexModelById((int) $segs[1]); } if (empty($transformIndexModel)) { throw new HttpException(404); } $url = craft()->assetTransforms->ensureTransformUrlByIndexModel($transformIndexModel); } catch (Exception $exception) { throw new HttpException(404, $exception->getMessage()); } craft()->request->redirect($url, true, 302); craft()->end(); case '404': throw new HttpException(404); } } // Check app/resources folder first. $appResourcePath = craft()->path->getResourcesPath() . $path; if (IOHelper::fileExists($appResourcePath)) { return $appResourcePath; } // See if the first segment is a plugin handle. if (isset($segs[0])) { $pluginResourcePath = craft()->path->getPluginsPath() . $segs[0] . '/' . 'resources/' . implode('/', array_splice($segs, 1)); if (IOHelper::fileExists($pluginResourcePath)) { return $pluginResourcePath; } } // Maybe a plugin wants to do something custom with this URL craft()->plugins->loadPlugins(); $pluginPath = craft()->plugins->callFirst('getResourcePath', array($path), true); if ($pluginPath && IOHelper::fileExists($pluginPath)) { return $pluginPath; } // Couldn't find the file return false; }
/** * @return bool */ private function _isConfigFolderWritable() { return IOHelper::isWritable(IOHelper::getFolderName(craft()->path->getLicenseKeyPath())); }
/** * Checks to see if Craft can write to a defined set of folders/files that are needed for auto-update to work. * * @return array|null */ public function getUnwritableFolders() { $checkPaths = array(craft()->path->getAppPath(), craft()->path->getPluginsPath()); $errorPath = null; foreach ($checkPaths as $writablePath) { if (!IOHelper::isWritable($writablePath)) { $errorPath[] = IOHelper::getRealPath($writablePath); } } return $errorPath; }
/** * @inheritDoc BaseAssetSourceType::createSourceFolder() * * @param AssetFolderModel $parentFolder * @param string $folderName * * @return bool */ protected function createSourceFolder(AssetFolderModel $parentFolder, $folderName) { if (!IOHelper::isWritable($this->getSourceFileSystemPath() . $parentFolder->path)) { return false; } return IOHelper::createFolder($this->getSourceFileSystemPath() . $parentFolder->path . $folderName); }
/** * Perform pre-flight checks to ensure we can run. * * @return this */ protected function flightcheck() { if (!self::$_pluginSettings) { throw new Exception(Craft::t('Minimee is not installed.')); } if (!$this->settings->enabled) { throw new Exception(Craft::t('Minimee has been disabled via settings.')); } if (!$this->settings->validate()) { $exceptionErrors = ''; foreach ($this->settings->getErrors() as $error) { $exceptionErrors .= implode('. ', $error); } throw new Exception(Craft::t('Minimee has detected invalid plugin settings: ') . $exceptionErrors); } if ($this->settings->useResourceCache()) { IOHelper::ensureFolderExists($this->makePathToStorageFolder()); } else { if (!IOHelper::folderExists($this->settings->cachePath)) { throw new Exception(Craft::t('Minimee\'s Cache Folder does not exist: ' . $this->settings->cachePath)); } if (!IOHelper::isWritable($this->settings->cachePath)) { throw new Exception(Craft::t('Minimee\'s Cache Folder is not writable: ' . $this->settings->cachePath)); } } if (!$this->assets) { throw new Exception(Craft::t('Minimee has no assets to operate upon.')); } if (!$this->type) { throw new Exception(Craft::t('Minimee has no value for `type`.')); } return $this; }
/** * Checks to see if the files that we are about to update are writable by Craft. * * @access private * @param $unzipFolder * @return bool */ private function _validateManifestPathsWritable($unzipFolder) { $manifestData = UpdateHelper::getManifestData($unzipFolder); $writableErrors = array(); foreach ($manifestData as $row) { if (UpdateHelper::isManifestVersionInfoLine($row)) { continue; } $rowData = explode(';', $row); $filePath = IOHelper::normalizePathSeparators(craft()->path->getAppPath() . $rowData[0]); if (UpdateHelper::isManifestLineAFolder($filePath)) { $filePath = UpdateHelper::cleanManifestFolderLine($filePath); } // Check to see if the file/folder we need to update is writable. if (IOHelper::fileExists($filePath) || IOHelper::folderExists($filePath)) { if (!IOHelper::isWritable($filePath)) { $writableErrors[] = $filePath; } } else { if (($folderPath = IOHelper::folderExists(IOHelper::getFolderName($filePath))) == true) { if (!IOHelper::isWritable($folderPath)) { $writableErrors[] = $filePath; } } } } return $writableErrors; }
/** * Resolves a resource path to the actual file system path, or returns false if the resource cannot be found. * * @param string $path * * @throws HttpException * @return string */ public function getResourcePath($path) { $segs = explode('/', $path); // Special resource routing if (isset($segs[0])) { switch ($segs[0]) { case 'js': // Route to js/compressed/ if useCompressedJs is enabled if (craft()->config->get('useCompressedJs') && !craft()->request->getQuery('uncompressed')) { array_splice($segs, 1, 0, 'compressed'); $path = implode('/', $segs); } break; case 'userphotos': if (isset($segs[1]) && $segs[1] == 'temp') { if (!isset($segs[2])) { return false; } return craft()->path->getTempUploadsPath() . 'userphotos/' . $segs[2] . '/' . $segs[3]; } else { if (!isset($segs[3])) { return false; } $size = AssetsHelper::cleanAssetName($segs[2]); // Looking for either a numeric size or "original" keyword if (!is_numeric($size) && $size != "original") { return false; } $username = AssetsHelper::cleanAssetName($segs[1]); $filename = AssetsHelper::cleanAssetName($segs[3]); $userPhotosPath = craft()->path->getUserPhotosPath() . $username . '/'; $sizedPhotoFolder = $userPhotosPath . $size . '/'; $sizedPhotoPath = $sizedPhotoFolder . $filename; // If the photo doesn't exist at this size, create it. if (!IOHelper::fileExists($sizedPhotoPath)) { $originalPhotoPath = $userPhotosPath . 'original/' . $filename; if (!IOHelper::fileExists($originalPhotoPath)) { return false; } IOHelper::ensureFolderExists($sizedPhotoFolder); if (IOHelper::isWritable($sizedPhotoFolder)) { craft()->images->loadImage($originalPhotoPath)->resize($size)->saveAs($sizedPhotoPath); } else { Craft::log('Tried to write to target folder and could not: ' . $sizedPhotoFolder, LogLevel::Error); } } return $sizedPhotoPath; } case 'defaultuserphoto': if (!isset($segs[1]) || !is_numeric($segs[1])) { return; } $size = $segs[1]; $sourceFile = craft()->path->getResourcesPath() . 'images/' . static::DefaultUserphotoFilename; $targetFolder = craft()->path->getUserPhotosPath() . '__default__/'; IOHelper::ensureFolderExists($targetFolder); if (IOHelper::isWritable($targetFolder)) { $targetFile = $targetFolder . $size . '.' . IOHelper::getExtension($sourceFile); craft()->images->loadImage($sourceFile)->resize($size)->saveAs($targetFile); return $targetFile; } else { Craft::log('Tried to write to the target folder, but could not:' . $targetFolder, LogLevel::Error); } case 'tempuploads': array_shift($segs); return craft()->path->getTempUploadsPath() . implode('/', $segs); case 'tempassets': array_shift($segs); return craft()->path->getAssetsTempSourcePath() . implode('/', $segs); case 'assetthumbs': if (empty($segs[1]) || empty($segs[2]) || !is_numeric($segs[1]) || !is_numeric($segs[2])) { return false; } $fileModel = craft()->assets->getFileById($segs[1]); if (empty($fileModel)) { return false; } $size = $segs[2]; return craft()->assetTransforms->getThumbServerPath($fileModel, $size); case 'icons': if (empty($segs[1]) || empty($segs[2]) || !is_numeric($segs[2]) || !preg_match('/^(?P<extension>[a-z_0-9]+)/i', $segs[1])) { return false; } $ext = StringHelper::toLowerCase($segs[1]); $size = $segs[2]; $iconPath = $this->_getIconPath($ext, $size); return $iconPath; case 'logo': return craft()->path->getStoragePath() . implode('/', $segs); case 'transforms': try { if (!empty($segs[1])) { $transformIndexModel = craft()->assetTransforms->getTransformIndexModelById((int) $segs[1]); } if (empty($transformIndexModel)) { throw new HttpException(404); } $url = craft()->assetTransforms->ensureTransformUrlByIndexModel($transformIndexModel); } catch (Exception $exception) { throw new HttpException(404, $exception->getMessage()); } craft()->request->redirect($url, true, 302); craft()->end(); } } // Check app/resources folder first. $appResourcePath = craft()->path->getResourcesPath() . $path; if (IOHelper::fileExists($appResourcePath)) { return $appResourcePath; } // See if the first segment is a plugin handle. if (isset($segs[0])) { $pluginResourcePath = craft()->path->getPluginsPath() . $segs[0] . '/' . 'resources/' . implode('/', array_splice($segs, 1)); if (IOHelper::fileExists($pluginResourcePath)) { return $pluginResourcePath; } } // Maybe a plugin wants to do something custom with this URL craft()->plugins->loadPlugins(); $pluginPaths = craft()->plugins->call('getResourcePath', array($path)); foreach ($pluginPaths as $path) { if ($path && IOHelper::fileExists($path)) { return $path; } } // Couldn't find the file return false; }
/** * Create a physical folder, return TRUE on success. * * @param AssetFolderModel $parentFolder * @param $folderName * @return boolean */ protected function _createSourceFolder(AssetFolderModel $parentFolder, $folderName) { if (!IOHelper::isWritable($this->_getSourceFileSystemPath() . $parentFolder->fullPath)) { return false; } return IOHelper::createFolder($this->_getSourceFileSystemPath() . $parentFolder->fullPath . $folderName, IOHelper::getWritableFolderPermissions()); }
/** */ public function run() { $this->init(); $installResult = InstallStatus::Success; foreach ($this->_requirements as $requirement) { if ($requirement->getResult() == RequirementResult::Failed) { $installResult = InstallStatus::Failure; break; } else { if ($requirement->getResult() == RequirementResult::Warning) { $installResult = InstallStatus::Warning; } } } $writableFolders = $this->_getWritableFolders(); $errorFolders = null; foreach ($writableFolders as $writableFolder) { if (!IOHelper::isWritable($writableFolder)) { $errorFolders[] = IOHelper::getRealPath($writableFolder); $installResult = InstallStatus::Failure; } } $this->_result = $installResult; $this->_serverInfo = $this->_calculateServerInfo(); $this->_errorFolders = $errorFolders; }