/** * Create .htaccess file and deny backups directory access from web * * @return void */ protected function _hideBackupsForApache() { $filename = '.htaccess'; if (!$this->_varDirectory->isFile($filename)) { $this->_varDirectory->writeFile($filename, 'deny from all'); } }
/** * Write down contents to a temporary file and return its absolute path * * @param string $relativePath * @param string $contents * @return string */ public function createFile($relativePath, $contents) { $filePath = $this->config->getLessMaterializationRelativePath() . '/' . $relativePath; if (!$this->tmpDirectory->isExist($filePath)) { $this->tmpDirectory->writeFile($filePath, $contents); } return $this->tmpDirectory->getAbsolutePath($filePath); }
/** * @param string $dbCode * @throws LocalizedException */ protected function downloadDb($dbCode) { $contents = $this->loadDbContent($this->getDbUrl($dbCode)); $this->directory->writeFile($this->getDbArchiveFilePath($dbCode), $contents); if (!$this->directory->isExist($this->getDbArchiveFilePath($dbCode))) { throw new LocalizedException(__('Cannot save db file.')); } }
/** * @inheritdoc * @throws FileSystemException */ public function lockProcess($lockName) { $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $this->lockFilePath = $this->getFilePath($lockName); while ($this->isProcessLocked()) { sleep(1); } $this->tmpDirectory->writeFile($this->lockFilePath, time()); }
/** * Minify template file * * @param string $file * @return void */ public function minify($file) { $file = $this->rootDirectory->getRelativePath($file); $content = preg_replace('#(?<!]]>)\\s+</#', '</', preg_replace('#((?:<\\?php\\s+(?!echo|print|if|elseif|else)[^\\?]*)\\?>)\\s+#', '$1 ', preg_replace('#(?<!' . implode('|', $this->inlineHtmlTags) . ')\\> \\<#', '><', preg_replace('#(?ix)(?>[^\\S ]\\s*|\\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\\b))*+)' . '(?:<(?>textarea|pre|script)\\b|\\z))#', ' ', preg_replace('#(?<!:|\\\\|\'|")//(?!\\s*\\<\\!\\[)(?!\\s*]]\\>)[^\\n\\r]*#', '', preg_replace('#(?<!:)//[^\\n\\r]*(\\s\\?\\>)#', '$1', preg_replace('#//[^\\n\\r]*(\\<\\?php)[^\\n\\r]*(\\s\\?\\>)[^\\n\\r]*#', '', $this->rootDirectory->readFile($file)))))))); if (!$this->htmlDirectory->isExist()) { $this->htmlDirectory->create(); } $this->htmlDirectory->writeFile($file, rtrim($content)); }
/** * @inheritdoc * @throws FileSystemException */ public function lockProcess($lockName) { if ($this->getState()->getMode() == State::MODE_PRODUCTION) { return; } $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $this->lockFilePath = $this->getFilePath($lockName); while ($this->isProcessLocked()) { usleep(1000); } $this->tmpDirectory->writeFile($this->lockFilePath, time()); }
/** * Perform necessary preprocessing and materialization when the specified asset is requested * * Returns an array of two elements: * - directory code where the file is supposed to be found * - relative path to the file * * Automatically caches the obtained successful results or returns false if source file was not found * * @param LocalInterface $asset * @return array|bool */ private function preProcess(LocalInterface $asset) { $sourceFile = $this->findSourceFile($asset); if (!$sourceFile) { return false; } $dirCode = \Magento\Framework\App\Filesystem::ROOT_DIR; $path = $this->rootDir->getRelativePath($sourceFile); $cacheId = $path . ':' . $asset->getPath(); $cached = $this->cache->load($cacheId); if ($cached) { return unserialize($cached); } $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($asset, $this->rootDir->readFile($path), $this->getContentType($path)); $preProcessors = $this->preProcessorPool->getPreProcessors($chain->getOrigContentType(), $chain->getTargetContentType()); foreach ($preProcessors as $processor) { $processor->process($chain); } $chain->assertValid(); if ($chain->isChanged()) { $dirCode = \Magento\Framework\App\Filesystem::VAR_DIR; $path = self::TMP_MATERIALIZATION_DIR . '/source/' . $asset->getPath(); $this->varDir->writeFile($path, $chain->getContent()); } $result = array($dirCode, $path); $this->cache->save(serialize($result), $cacheId); return $result; }
/** * Perform necessary preprocessing and materialization when the specified asset is requested * * Returns an array of two elements: * - directory code where the file is supposed to be found * - relative path to the file * * Automatically caches the obtained successful results or returns false if source file was not found * * @param LocalInterface $asset * @return array|bool */ private function preProcess(LocalInterface $asset) { $sourceFile = $this->findSourceFile($asset); $dirCode = DirectoryList::ROOT; $path = $this->rootDir->getRelativePath($sourceFile); $cacheId = $path . ':' . $asset->getPath(); $cached = $this->cache->load($cacheId); if ($cached) { return unserialize($cached); } $origContent = $path ? $this->rootDir->readFile($path) : ''; $origContentType = $this->getContentType($path) ?: $asset->getContentType(); $chain = $this->chainFactory->create( [ 'asset' => $asset, 'origContent' => $origContent, 'origContentType' => $origContentType, 'origAssetPath' => $path ] ); $this->preProcessorPool->process($chain); $chain->assertValid(); if ($chain->isChanged()) { $dirCode = DirectoryList::VAR_DIR; $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath(); $this->varDir->writeFile($path, $chain->getContent()); } $result = [$dirCode, $path]; $this->cache->save(serialize($result), $cacheId); return $result; }
/** * Perform necessary preprocessing and materialization when the specified asset is requested * * Returns an array of two elements: * - directory code where the file is supposed to be found * - relative path to the file * * Automatically caches the obtained successful results or returns false if source file was not found * * @param LocalInterface $asset * @return array|bool */ private function preProcess(LocalInterface $asset) { $sourceFile = $this->findSourceFile($asset); if ($sourceFile !== false) { $path = $this->rootDir->getRelativePath($sourceFile); } else { // No original file, the resulting file may be generated by a pre-processor $path = false; } $cacheId = $path . ':' . $asset->getPath(); $cached = $this->cache->load($cacheId); if ($cached) { return unserialize($cached); } $chain = $this->createChain($asset, $path); $this->preProcessorPool->process($chain); $chain->assertValid(); $dirCode = DirectoryList::ROOT; if ($chain->isChanged()) { $dirCode = DirectoryList::VAR_DIR; $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath(); $this->varDir->writeFile($path, $chain->getContent()); } $result = [$dirCode, $path]; $this->cache->save(serialize($result), $cacheId); return $result; }
/** * Create physical certificate file based on DB data * * @param string $file * @return void */ protected function _createCertFile($file) { if ($this->varDirectory->isDirectory(self::BASEPATH_PAYPAL_CERT)) { $this->_removeOutdatedCertFile(); } $this->varDirectory->writeFile($file, $this->encryptor->decrypt($this->getContent())); }
/** * Perform necessary preprocessing and materialization when the specified asset is requested * * Returns an array of two elements: * - directory where the file is supposed to be found * - relative path to the file * * returns false if source file was not found * * @param LocalInterface $asset * @return array|bool */ private function preProcess(LocalInterface $asset) { $sourceFile = $this->findSourceFile($asset); $dir = $this->rootDir->getAbsolutePath(); $path = ''; if ($sourceFile) { $path = basename($sourceFile); $dir = dirname($sourceFile); } $chain = $this->createChain($asset, $dir, $path); $this->preProcessorPool->process($chain); $chain->assertValid(); if ($chain->isChanged()) { $dir = $this->varDir->getAbsolutePath(); $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath(); $this->varDir->writeFile($path, $chain->getContent()); } if (empty($path)) { $result = false; } else { $result = [$dir, $path]; } return $result; }
/** * Set the backup file content * * @param string &$content * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function setFile(&$content) { if (!$this->hasData('time') || !$this->hasData('type') || !$this->hasData('path')) { throw new \Magento\Framework\Exception\LocalizedException(__('Please correct the order of creation for a new backup.')); } $this->varDirectory->writeFile($this->_getFilePath(), $content); return $this; }
/** * Directory structure initializing * * @return void */ protected function _initFilesystem() { $this->_mediaDirectory->create($this->_path); $this->_mediaDirectory->create($this->_quotePath); $this->_mediaDirectory->create($this->_orderPath); // Directory listing and hotlink secure $path = $this->_path . '/.htaccess'; if (!$this->_mediaDirectory->isFile($path)) { $this->_mediaDirectory->writeFile($path, "Order deny,allow\nDeny from all"); } }
/** * Proceed moving a file from TMP to destination folder * * @param string $fileName * @return array */ public function move($fileName) { if (preg_match('/\\bhttps?:\\/\\//i', $fileName, $matches)) { $url = str_replace($matches[0], '', $fileName); $read = $this->_readFactory->create($url, DriverPool::HTTP); $fileName = preg_replace('/[^a-z0-9\\._-]+/i', '', $fileName); $this->_directory->writeFile($this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName), $read->readAll()); } $filePath = $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName); $this->_setUploadFile($filePath); $result = $this->save($this->getDestDir()); $result['name'] = self::getCorrectFileName($result['name']); return $result; }
/** * Sets list of allowed IP addresses * * @param string $addresses * @return bool * @throws \InvalidArgumentException */ public function setAddresses($addresses) { $addresses = (string) $addresses; if (empty($addresses)) { if ($this->flagDir->isExist(self::IP_FILENAME)) { return $this->flagDir->delete(self::IP_FILENAME); } return true; } if (!preg_match('/^[^\\s,]+(,[^\\s,]+)*$/', $addresses)) { throw new \InvalidArgumentException("One or more IP-addresses is expected (comma-separated)\n"); } $result = $this->flagDir->writeFile(self::IP_FILENAME, $addresses); return false !== $result ? true : false; }
/** * Write status information to the file. * * @param string $text * @param string $filePath * @return $this * @throws \RuntimeException */ protected function writeMessageToFile($text, $filePath) { $isNewFile = !$this->varReaderWriter->isExist($filePath); if (!$isNewFile && $this->varReaderWriter->readFile($filePath)) { $text = "\n{$text}"; } try { $this->varReaderWriter->writeFile($filePath, $text, 'a+'); } catch (FileSystemException $e) { throw new \RuntimeException(sprintf('Cannot add status information to "%s"', $filePath)); } if ($isNewFile) { chmod($filePath, 0777); } return $this; }
/** * Perform necessary preprocessing and materialization when the specified asset is requested * * Returns an array of two elements: * - directory code where the file is supposed to be found * - relative path to the file * * returns false if source file was not found * * @param LocalInterface $asset * @return array|bool */ private function preProcess(LocalInterface $asset) { $sourceFile = $this->findSourceFile($asset); $path = $this->rootDir->getRelativePath($sourceFile); $chain = $this->createChain($asset, $path); $this->preProcessorPool->process($chain); $chain->assertValid(); $dirCode = DirectoryList::ROOT; if ($chain->isChanged()) { $dirCode = DirectoryList::VAR_DIR; $path = DirectoryList::TMP_MATERIALIZATION_DIR . '/source/' . $chain->getTargetAssetPath(); $this->varDir->writeFile($path, $chain->getContent()); } $result = [$dirCode, $path]; return $result; }
/** * @return string Path to config file * @throws \Exception */ public function makeConfig() { if (!$this->directory->isExist($this->basePath)) { //$this->directory->delete($this->basePath); $this->directory->create($this->basePath); $this->directory->changePermissions($this->basePath, 0777); } $jsonData = []; $sphinxData = ['time' => date('d.m.Y H:i:s'), 'host' => $this->host, 'port' => $this->port, 'fallback_port' => $this->port - 1, 'logdir' => $this->directory->getAbsolutePath($this->basePath), 'sphinxdir' => $this->directory->getAbsolutePath($this->basePath), 'indexes' => '', 'localdir' => dirname(dirname(__FILE__)), 'custom' => $this->config->getAdditionalSearchdConfig()]; $sphinxTemplate = $this->config->getSphinxConfigurationTemplate(); $indexTemplate = $this->config->getSphinxIndexConfigurationTemplate(); /** @var \Mirasvit\Search\Model\Index $index */ foreach ($this->indexCollectionFactory->create() as $index) { foreach (array_keys($this->storeManager->getStores()) as $storeId) { $indexName = $index->getIndexInstance()->getIndexer()->getIndexName($storeId); $data = ['name' => $indexName, 'min_word_len' => 1, 'path' => $this->directory->getAbsolutePath($this->basePath) . '/' . $indexName, 'custom' => $this->config->getAdditionalIndexConfig()]; $jsonAttributes = []; $attributes = []; foreach (array_keys($index->getIndexInstance()->getAttributes(true)) as $attribute) { $attributes[] = " rt_field = {$attribute}"; $jsonAttributes[] = $attribute; if (count($attributes) > 250) { break; } } $attributes[] = " rt_field = options"; $jsonAttributes[] = "options"; $data['attributes'] = implode(PHP_EOL, $attributes); $sphinxData['indexes'] .= $this->helper->filterTemplate($indexTemplate, $data); $jsonData[$indexName] = $jsonAttributes; } } $config = $this->helper->filterTemplate($sphinxTemplate, $sphinxData); if ($this->directory->isWritable($this->basePath)) { $this->directory->writeFile($this->configFilePath, $config); $this->directory->writeFile($this->configFilePath . '.attr', json_encode($jsonData)); } else { if ($this->directory->isExist($this->configFilePath)) { throw new \Exception(__('File %1 does not writable', $this->configFilePath)); } else { throw new \Exception(__('Directory %1 does not writable', $this->basePath)); } } return $this->directory->getAbsolutePath($this->configFilePath); }
/** * Create a tree of self-sustainable files and return the topmost source file, * ready for passing to 3rd party library * * @param Chain $chain * @return string Absolute path of generated topmost source file */ public function generateFileTree(Chain $chain) { /** * wait if generation process has already started */ while ($this->isProcessLocked()) { sleep(1); } $lockFilePath = $this->config->getMaterializationRelativePath() . '/' . self::LOCK_FILE; $this->tmpDirectory->writeFile($lockFilePath, time()); $this->magentoImportProcessor->process($chain); $this->importProcessor->process($chain); $this->relatedGenerator->generate($this->importProcessor); $contentType = $chain->getContentType(); $relativePath = preg_replace('#\\.css$#', '.' . $contentType, $chain->getAsset()->getPath()); $tmpFilePath = $this->temporaryFile->createFile($relativePath, $chain->getContent()); $this->tmpDirectory->delete($lockFilePath); return $tmpFilePath; }
/** * Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array * TODO: to be removed in scope of MAGETWO-53476 * * @param string[] * * @return void */ private function enableCacheTypes($cacheTypes) { if (empty($cacheTypes)) { return; } $envPath = $this->getEnvPath(); if ($this->write->isReadable($this->write->getRelativePath($envPath))) { $envData = (include $envPath); foreach ($cacheTypes as $cacheType) { if (isset($envData['cache_types'][$cacheType])) { $envData['cache_types'][$cacheType] = 1; } } $formatter = new PhpFormatter(); $contents = $formatter->format($envData); $this->write->writeFile($this->write->getRelativePath($envPath), $contents); if (function_exists('opcache_invalidate')) { opcache_invalidate($this->write->getAbsolutePath($envPath)); } } }
/** * Minify template file * * @param string $file * @return void */ public function minify($file) { $dir = dirname($file); $fileName = basename($file); $content = preg_replace( '#(?<!]]>)\s+</#', '</', preg_replace( '#((?:<\?php\s+(?!echo|print|if|elseif|else)[^\?]*)\?>)\s+#', '$1 ', preg_replace( '#(?<!' . implode('|', $this->inlineHtmlTags) . ')\> \<#', '><', preg_replace( '#(?ix)(?>[^\S ]\s*|\s{2,})(?=(?:(?:[^<]++|<(?!/?(?:textarea|pre|script)\b))*+)' . '(?:<(?>textarea|pre|script)\b|\z))#', ' ', preg_replace( '#(?<!:|\\\\|\'|")//(?!\s*\<\!\[)(?!\s*]]\>)[^\n\r]*#', '', preg_replace( '#(?<!:)//[^\n\r]*(\s\?\>)#', '$1', preg_replace( '#(?<!:)//[^\n\r]*(\<\?php)[^\n\r]*(\s\?\>)[^\n\r]*#', '', $this->readFactory->create($dir)->readFile($fileName) ) ) ) ) ) ) ); if (!$this->htmlDirectory->isExist()) { $this->htmlDirectory->create(); } $this->htmlDirectory->writeFile($this->getRelativeGeneratedPath($file), rtrim($content)); }
/** * Create a tree of self-sustainable files and return the topmost LESS file, ready for passing to 3rd party library * * @param Chain $chain * @return string Absolute path of generated LESS file */ public function generateFileTree(Chain $chain) { /** * @bug This logic is duplicated at \Magento\Framework\View\Asset\PreProcessor\Pool * If you need to extend or modify behavior of LESS preprocessing, you must account for both places */ /** * wait if generation process has already started */ while ($this->isProcessLocked()) { sleep(1); } $lockFilePath = $this->config->getLessMaterializationRelativePath() . '/' . self::LOCK_FILE; $this->tmpDirectory->writeFile($lockFilePath, time()); $this->magentoImportProcessor->process($chain); $this->importProcessor->process($chain); $this->relatedGenerator->generate($this->importProcessor); $lessRelativePath = preg_replace('#\\.css$#', '.less', $chain->getAsset()->getPath()); $tmpFilePath = $this->temporaryFile->createFile($lessRelativePath, $chain->getContent()); $this->tmpDirectory->delete($lockFilePath); return $tmpFilePath; }
/** * Goes to specified host/path and fetches reports from there. * Save reports to database. * * @param \Magento\Framework\Filesystem\Io\Sftp $connection * @return int Number of report rows that were fetched and saved successfully * @throws \Magento\Framework\Exception\LocalizedException */ public function fetchAndSave(\Magento\Framework\Filesystem\Io\Sftp $connection) { $fetched = 0; $listing = $this->_filterReportsList($connection->rawls()); foreach ($listing as $filename => $attributes) { $localCsv = 'PayPal_STL_' . uniqid(\Magento\Framework\Math\Random::getRandomNumber()) . time() . '.csv'; if ($connection->read($filename, $this->_tmpDirectory->getAbsolutePath($localCsv))) { if (!$this->_tmpDirectory->isWritable($localCsv)) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a target file for reading reports.')); } $encoded = $this->_tmpDirectory->readFile($localCsv); $csvFormat = 'new'; $fileEncoding = mb_detect_encoding($encoded); if (self::FILES_OUT_CHARSET != $fileEncoding) { $decoded = @iconv($fileEncoding, self::FILES_OUT_CHARSET . '//IGNORE', $encoded); $this->_tmpDirectory->writeFile($localCsv, $decoded); $csvFormat = 'old'; } // Set last modified date, this value will be overwritten during parsing if (isset($attributes['mtime'])) { $date = new \DateTime(); $lastModified = $date->setTimestamp($attributes['mtime']); $this->setReportLastModified($lastModified->format('Y-m-d H:i:s')); } $this->setReportDate($this->_fileNameToDate($filename))->setFilename($filename)->parseCsv($localCsv, $csvFormat); if ($this->getAccountId()) { $this->save(); } if ($this->_dataSaveAllowed) { $fetched += count($this->_rows); } // clean object and remove parsed file $this->unsetData(); $this->_tmpDirectory->delete($localCsv); } } return $fetched; }
/** * Write JSON string into queue * * @param string $data * @return void */ public function write($data) { $this->writer->writeFile($this->queueFileBasename, $data); }
/** * Write down contents to a temporary file and return its absolute path * * @param string $relativePath * @param string $contents * @return string */ protected function createFile($relativePath, $contents) { $filePath = Source::TMP_MATERIALIZATION_DIR . '/' . self::TMP_LESS_DIR . '/' . $relativePath; $this->tmpDirectory->writeFile($filePath, $contents); return $this->tmpDirectory->getAbsolutePath($filePath); }
/** * Write the message to file * * @param string $message * @return void */ private function writeToFile($message) { $this->directory->writeFile($this->logFile, $message, 'a+'); }
/** * Decode base64 encoded content and save it in system tmp folder * * @param ContentInterface $fileContent * @return array */ protected function decodeContent(ContentInterface $fileContent) { $tmpFileName = $this->getTmpFileName(); $fileSize = $this->systemTmpDirectory->writeFile($tmpFileName, base64_decode($fileContent->getFileData())); return ['name' => $fileContent->getName(), 'type' => self::DEFAULT_MIME_TYPE, 'tmp_name' => $this->systemTmpDirectory->getAbsolutePath($tmpFileName), 'error' => 0, 'size' => $fileSize]; }
/** * {@inheritdoc} */ public function save($data) { $this->directory->writeFile($this->fileName, $data, 'w'); }