/** * Check file system full path * * @param string $fullPath * @param bool $recursive * @param bool $existence * @return bool */ protected function _checkFullPath($fullPath, $recursive, $existence) { $result = true; if ($recursive && $this->_filesystem->isDirectory($fullPath)) { $pathsToCheck = $this->_filesystem->getNestedKeys($fullPath); array_unshift($pathsToCheck, $fullPath); } else { $pathsToCheck = array($fullPath); } $skipFileNames = array('.svn', '.htaccess'); foreach ($pathsToCheck as $pathToCheck) { if (in_array(basename($pathToCheck), $skipFileNames)) { continue; } if ($existence) { $setError = !$this->_filesystem->isWritable($fullPath); } else { $setError = $this->_filesystem->has($fullPath) && !$this->_filesystem->isWritable($fullPath); } if ($setError) { $this->_getInstaller()->getDataModel()->addError(Mage::helper('Mage_Install_Helper_Data')->__('Path "%s" must be writable.', $pathToCheck)); $result = false; } } return $result; }
/** * Clear files and directories in storage * * @param string $dir * @return Mage_Core_Model_Resource_File_Storage_File */ public function clear($dir = '') { if (strpos($dir, $this->getMediaBaseDirectory()) !== 0) { $dir = $this->getMediaBaseDirectory() . $dir; } if ($this->_filesystem->isDirectory($dir)) { foreach ($this->_filesystem->getNestedKeys($dir) as $path) { $this->_filesystem->delete($path); } } return $this; }
protected function _setContents($pfm) { $pfm->clearContents(); $contents = $this->getData('contents'); $usesRoles = array(); foreach ($contents['role'] as $i => $role) { if (0 === $i) { continue; } $usesRoles[$role] = 1; $roleDir = $this->getRoleDir($role) . DS; $fullPath = $roleDir . $contents['path'][$i]; switch ($contents['type'][$i]) { case 'file': if (!$this->_filesystem->isFile($fullPath)) { Mage::throwException(Mage::helper('Mage_Adminhtml_Helper_Data')->__("Invalid file: %s", $fullPath)); } $pfm->addFile('/', $contents['path'][$i], array('role' => $role, 'md5sum' => $this->_filesystem->getFileMd5($fullPath))); break; case 'dir': if (!$this->_filesystem->isDirectory($fullPath)) { Mage::throwException(Mage::helper('Mage_Adminhtml_Helper_Data')->__("Invalid directory: %s", $fullPath)); } $path = $contents['path'][$i]; $include = $contents['include'][$i]; $ignore = $contents['ignore'][$i]; $this->_addDir($pfm, $role, $roleDir, $path, $include, $ignore); break; } } $pearRoles = $this->getRoles(); #echo "<pre>".print_r($usesRoles,1)."</pre>"; foreach ($usesRoles as $role => $dummy) { if (empty($pearRoles[$role]['package'])) { continue; } $pfm->addUsesrole($role, $pearRoles[$role]['package']); } }
/** * Return path of the current selected directory or root directory for startup * Try to create target directory if it doesn't exist * * @throws Mage_Core_Exception * @return string */ public function getCurrentPath() { if (!$this->_currentPath) { $currentPath = $this->getStorageRoot(); $path = $this->_getRequest()->getParam($this->getTreeNodeName()); if ($path) { $path = $this->convertIdToPath($path); if ($this->_filesystem->isDirectory($path)) { $currentPath = $path; } } try { if (!$this->_filesystem->isWritable($currentPath)) { $this->_filesystem->createDirectory($currentPath); } } catch (Magento_Filesystem_Exception $e) { $message = Mage::helper('Mage_Cms_Helper_Data')->__('The directory %s is not writable by server.', $currentPath); Mage::throwException($message); } $this->_currentPath = $currentPath; } return $this->_currentPath; }
/** * Save package file to var/connect. * * @return boolean */ public function savePackage() { if ($this->getData('file_name') != '') { $fileName = $this->getData('file_name'); $this->unsetData('file_name'); } else { $fileName = $this->getName(); } if (!preg_match('/^[a-z0-9]+[a-z0-9\\-\\_\\.]*([\\/\\\\]{1}[a-z0-9]+[a-z0-9\\-\\_\\.]*)*$/i', $fileName)) { return false; } if (!$this->getPackageXml()) { $this->generatePackageXml(); } if (!$this->getPackageXml()) { return false; } try { $path = Mage::helper('Mage_Connect_Helper_Data')->getLocalPackagesPath(); $this->_filesystem->write($path . 'package.xml', $this->getPackageXml()); $this->unsPackageXml(); $this->unsTargets(); $xml = Mage::helper('Mage_Core_Helper_Data')->assocToXml($this->getData()); $xml = new Varien_Simplexml_Element($xml->asXML()); // prepare dir to save $parts = explode(DS, $fileName); array_pop($parts); $newDir = implode(DS, $parts); if (!empty($newDir) && !$this->_filesystem->isDirectory($path . $newDir)) { $this->_filesystem->ensureDirectoryExists($path, $newDir, 0777); } $this->_filesystem->write($path . $fileName . '.xml', $xml->asNiceXml()); } catch (Magento_Filesystem_Exception $e) { return false; } return true; }
/** * Create new directory in storage * * @param string $name New directory name * @param string $path Parent directory path * @throws Mage_Core_Exception * @return array New directory info */ public function createDirectory($name, $path) { if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) { Mage::throwException(Mage::helper('Mage_Cms_Helper_Data')->__('Invalid folder name. Please, use alphanumeric characters, underscores and dashes.')); } if (!$this->_filesystem->isDirectory($path) || !$this->_filesystem->isWritable($path)) { $path = $this->getHelper()->getStorageRoot(); } $newPath = $path . DS . $name; if ($this->_filesystem->isDirectory($newPath, $path)) { Mage::throwException(Mage::helper('Mage_Cms_Helper_Data')->__('A directory with the same name already exists. Please try another folder name.')); } $this->_filesystem->createDirectory($newPath); try { if (Mage::helper('Mage_Core_Helper_File_Storage_Database')->checkDbUsage()) { $relativePath = Mage::helper('Mage_Core_Helper_File_Storage_Database')->getMediaRelativePath($newPath); Mage::getModel('Mage_Core_Model_File_Storage_Directory_Database')->createRecursive($relativePath); } $result = array('name' => $name, 'short_name' => $this->getHelper()->getShortFilename($name), 'path' => $newPath, 'id' => $this->getHelper()->convertPathToId($newPath)); return $result; } catch (Magento_Filesystem_Exception $e) { Mage::throwException(Mage::helper('Mage_Cms_Helper_Data')->__('Cannot create new directory.')); } }
public function __destruct() { if ($this->_isMapChanged && $this->_canSaveMap) { if (!$this->_filesystem->isDirectory($this->_mapDir)) { $this->_filesystem->createDirectory($this->_mapDir, 0777); } $this->_filesystem->write($this->_mapFile, serialize($this->_map)); } }
/** * Merge files, located under the same folder, into one and return file name of merged file * * @param array $files list of names relative to the same folder * @param string $contentType * @return string * @throws Magento_Exception if not existing file requested for merge */ protected function _mergeFiles($files, $contentType) { $filesToMerge = array(); $mergedFile = array(); $jsDir = Mage::getBaseDir('js'); $publicDir = $this->_buildPublicViewFilename(''); foreach ($files as $file) { $params = array(); $this->_updateParamDefaults($params); $filesToMerge[$file] = $this->_publishViewFile($file, $params); $mergedFile[] = str_replace('\\', '/', str_replace(array($jsDir, $publicDir), '', $filesToMerge[$file])); } $mergedFile = self::PUBLIC_MERGE_DIR . DS . md5(implode('|', $mergedFile)) . ".{$contentType}"; $mergedFile = $this->_buildPublicViewFilename($mergedFile); $mergedMTimeFile = $mergedFile . '.dat'; $filesMTimeData = ''; foreach ($filesToMerge as $file) { $filesMTimeData .= $this->_filesystem->getMTime($file); } if ($this->_filesystem->has($mergedFile) && $this->_filesystem->has($mergedMTimeFile) && $filesMTimeData == $this->_filesystem->read($mergedMTimeFile)) { return $mergedFile; } if (!$this->_filesystem->isDirectory(dirname($mergedFile))) { $this->_filesystem->createDirectory(dirname($mergedFile), 0777); } $result = array(); foreach ($filesToMerge as $file) { if (!$this->_filesystem->has($file)) { throw new Magento_Exception("Unable to locate file '{$file}' for merging."); } $content = $this->_filesystem->read($file); if ($contentType == self::CONTENT_TYPE_CSS) { $offset = $this->_getFilesOffset($file, dirname($mergedFile)); $content = $this->_applyCssUrlOffset($content, $offset); } $result[] = $content; } $result = ltrim(implode($result)); if ($contentType == self::CONTENT_TYPE_CSS) { $result = $this->_popCssImportsUp($result); } $this->_filesystem->write($mergedFile, $result); $this->_filesystem->write($mergedMTimeFile, $filesMTimeData); return $mergedFile; }
/** * Test isDirectory isolation * @expectedException InvalidArgumentException * @expectedExceptionMessage Path '/tmp/../etc/passwd' is out of working directory '/tmp' * @dataProvider workingDirDataProvider * @param string|null $workingDirectory */ public function testIsDirectoryIsolation($workingDirectory) { $validPath = '/tmp/../etc/passwd'; $filesystem = new Magento_Filesystem($this->_getDefaultAdapterMock()); $filesystem->setWorkingDirectory('/tmp'); $this->assertTrue($filesystem->isDirectory($validPath, $workingDirectory)); }