/** * Create .htaccess file and deny backups directory access from web */ protected function _hideBackupsForApache() { $htaccessPath = $this->_baseDir . DS . '.htaccess'; if (!$this->_filesystem->isFile($htaccessPath)) { $this->_filesystem->write($htaccessPath, 'deny from all'); $this->_filesystem->changePermissions($htaccessPath, 0644); } }
/** * Check and process robots file * * @return Mage_Backend_Model_Config_Backend_Admin_Robots */ protected function _afterSave() { if ($this->getValue()) { $this->_filesystem->write($this->_filePath, $this->getValue()); } return parent::_afterSave(); }
/** * Set the backup file content * * @param string $content * @return Mage_Backup_Model_Backup */ public function setFile(&$content) { if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) { Mage::throwException($this->_helper->__('Wrong order of creation for new backup.')); } $this->_filesystem->write($this->_getFilePath(), $content); return $this; }
public function replaceTmpEncryptKey($key = null) { if (!$key) { $key = md5(Mage::helper('Mage_Core_Helper_Data')->getRandomString(10)); } $localXml = $this->_filesystem->read($this->_localConfigFile); $localXml = str_replace(self::TMP_ENCRYPT_KEY_VALUE, $key, $localXml); $this->_filesystem->write($this->_localConfigFile, $localXml); return $this; }
/** * Save file to storage * * @param string $filePath * @param string $content * @param bool $overwrite * @return bool */ public function saveFile($filePath, $content, $overwrite = false) { if (strpos($filePath, $this->getMediaBaseDirectory()) !== 0) { $filePath = $this->getMediaBaseDirectory() . DS . $filePath; } try { if (!$this->_filesystem->isFile($filePath) || $overwrite && $this->_filesystem->delete($filePath)) { $this->_filesystem->write($filePath, $content); return true; } } catch (Magento_Filesystem_Exception $e) { $this->_logger->log($e->getMessage()); Mage::throwException($this->_helper->__('Unable to save file: %s', $filePath)); } return false; }
/** * 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; }
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; } $pear = Varien_Pear::getInstance(); $dir = Mage::getBaseDir('var') . DS . 'pear'; try { $this->_filesystem->write($dir . DS . 'package.xml', $this->getPackageXml()); } catch (Magento_Filesystem_Exception $e) { return false; } $pkgver = $this->getName() . '-' . $this->getReleaseVersion(); $this->unsPackageXml(); $this->unsRoles(); $xml = Mage::helper('Mage_Core_Helper_Data')->assocToXml($this->getData()); $xml = new Varien_Simplexml_Element($xml->asXML()); try { $this->_filesystem->write($dir . DS . $fileName . '.xml', $xml->asNiceXml()); } catch (Magento_Filesystem_Exception $e) { return false; } return true; }
/** * Put store into maintenance mode * * @return bool */ public function turnOnMaintenanceMode() { $maintenanceFlagFile = $this->getMaintenanceFlagFilePath(); $result = $this->_filesystem->write($maintenanceFlagFile, 'maintenance', Mage::getBaseDir()); return $result !== false; }
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; }
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage Path '/tmp/../path/file.txt' is out of working directory '/tmp' * @dataProvider workingDirDataProvider * @param string|null $workingDirectory */ public function testWriteIsolation($workingDirectory) { $invalidPath = '/tmp/../path/file.txt'; $adapterMock = $this->getMockBuilder('Magento_Filesystem_AdapterInterface')->getMock(); $adapterMock->expects($this->once())->method('isDirectory')->with('/tmp')->will($this->returnValue(true)); $adapterMock->expects($this->never())->method('write'); $filesystem = new Magento_Filesystem($adapterMock); $filesystem->setWorkingDirectory('/tmp'); $filesystem->write($invalidPath, 'TEST TEST', $workingDirectory); }