/** * Class constructor. * * @param \BackBee\BBApplication $application * @param array $config */ public function __construct(BBApplication $application, array $config) { $this->_application = $application; $this->_config = $config; if (true === array_key_exists('basedir', $config)) { $this->_basedir = $config['basedir']; File::resolveFilepath($this->_basedir, null, array('include_path' => $this->_application->getRepository())); } else { $this->_basedir = $this->_application->getRepository(); } }
/** * Handles a media file request. * * @param string $filename The media file to provide * * @throws FrontControllerException * * @return Response */ public function mediaAction($type, $filename, $includePath = array()) { $includePath = array_merge($includePath, array($this->application->getStorageDir(), $this->application->getMediaDir())); if (null !== $this->application->getBBUserToken()) { $includePath[] = $this->application->getTemporaryDir(); } $matches = array(); if (preg_match('/([a-f0-9]{3})\\/([a-f0-9]{29})\\/(.*)\\.([^\\.]+)/', $filename, $matches)) { $filename = $matches[1] . '/' . $matches[2] . '.' . $matches[4]; } elseif (preg_match('/([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})\\/.*\\.([^\\.]+)/', $filename, $matches)) { $filename = $matches[1] . $matches[2] . $matches[3] . $matches[4] . $matches[5] . $matches[6] . $matches[7] . $matches[8] . '.' . $matches[9]; File::resolveMediapath($filename, null, array('include_path' => $includePath)); } File::resolveFilepath($filename, null, array('include_path' => $includePath)); $this->application->info(sprintf('Handling image URL `%s`.', $filename)); if (false === file_exists($filename) || false === is_readable($filename)) { $request = $this->application->getRequest(); throw new FrontControllerException(sprintf('The file `%s` can not be found (referer: %s).', $request->getHost() . '/' . $request->getPathInfo(), $request->server->get('HTTP_REFERER')), FrontControllerException::NOT_FOUND); } return $this->createMediaResponse($filename); }
/** * Returns the default classcontent thumbnail filepath. * * @param array $base_directories list of every resources directories of current application * * @return string */ private function getDefaultClassContentThumbnailFilepath(array $base_directories) { $filename = 'default_thumbnail.png'; File::resolveFilepath($filename, null, array('include_path' => $base_directories)); return $filename; }
/** * Return the real yaml file path of the loading class. * * @param string $path * * @return string The real path if found */ private function resolveFilePath($path) { $path = str_replace(array($this->_protocol . '://', '/'), array('', DIRECTORY_SEPARATOR), $path); foreach ($this->_includeExtensions as $ext) { $filename = $path . $ext; File::resolveFilepath($filename, null, array('include_path' => $this->_classcontentdir)); if (true === is_file($filename)) { return $filename; } } return $path; }
public static function onFlushElementFile(Event $event) { $revision = $event->getTarget(); $content = $revision->getContent(); if (!$content instanceof ElementFile || !is_file($content->path)) { return; } $application = $event->getApplication(); $em = $application->getEntityManager(); $uow = $em->getUnitOfWork(); if ($uow->isScheduledForDelete($content)) { return; } $fileRepository = $em->getRepository('BackBee\\ClassContent\\Element\\File'); $fileRepository->setDirectories($application); $fileRepository->commitFile($content); $moveFrom = $content->path; File::resolveFilepath($moveFrom, null, array('base_dir' => $application->getMediaDir())); $content->setParam('stat', json_encode(stat($moveFrom))); if ($content instanceof ElementImage) { list($width, $height) = getimagesize($moveFrom); $content->setParam('width', $width); $content->setParam('height', $height); } $uow->recomputeSingleEntityChangeSet($em->getClassMetadata('BackBee\\ClassContent\\Revision'), $revision); }
public static function onRemoveElementFile(Event $event) { $dispatcher = $event->getDispatcher(); $application = $dispatcher->getApplication(); try { $content = $event->getEventArgs()->getEntity(); if (!$content instanceof ElementFile) { return; } $includePath = array($application->getStorageDir(), $application->getMediaDir()); if (null !== $application->getBBUserToken()) { $includePath[] = $application->getTemporaryDir(); } $filename = $content->path; File::resolveFilepath($filename, null, array('include_path' => $includePath)); @unlink($filename); } catch (\Exception $e) { $application->warning('Unable to delete file: ' . $e->getMessage()); } }
public function removeThumbnail(Layout $layout, BBApplication $app) { $thumbnailfile = $layout->getPicPath(); if (empty($thumbnail)) { return true; } File::resolveFilepath($thumbnailfile, null, array('include_path' => $app->getResourceDir())); while (true === is_file($thumbnailfile) && true === is_writable($thumbnailfile)) { @unlink($thumbnailfile); $thumbnailfile = $layout->getPicPath(); File::resolveFilepath($thumbnailfile, null, array('include_path' => $app->getResourceDir())); } return true; }
/** * Prepend one directory at the begining of resources dirs. * * @param type $dir * * @return ApplicationInterface */ public function unshiftResourceDir($dir) { File::resolveFilepath($dir); $resourcedir = $this->getResourceDir(); array_unshift($resourcedir, $dir); $this->resourceDir = $resourcedir; return $this; }
public function testResolveFilepath() { $twigFilePath = $this->getFixturesFolder() . 'file.twig'; File::resolveFilepath($twigFilePath); $this->assertEquals($this->getFixturesFolder() . 'file.twig', $twigFilePath); }
/** * Move an uploaded file to the temporary directory and update file content. * * @param \BackBee\CoreDomain\ClassContent\AbstractClassContent $file * @param string $newfilename * @param string $originalname * * @return boolean|string * @throws \BackBee\ClassContent\Exception\ClassContentException Occures on invalid content type provided */ public function updateFile(AbstractClassContent $file, $newfilename, $originalname = null, $src = null) { if (false === $file instanceof ElementFile) { throw new ClassContentException('Invalid content type'); } if (null === $originalname) { $originalname = $file->originalname; } $base_dir = $this->_temporarydir; $file->originalname = $originalname; $file->path = Media::getPathFromContent($file); if (null === $file->getDraft()) { $base_dir = $this->isInMediaLibrary($file) ? $this->_mediadir : $this->_storagedir; } $moveto = $file->path; File::resolveFilepath($moveto, null, array('base_dir' => $base_dir)); try { if ($src === null) { File::resolveFilepath($newfilename, null, array('base_dir' => $this->_temporarydir)); File::move($newfilename, $moveto); } else { $dir = dirname($moveto); if (!is_dir($dir)) { File::mkdir($dir); } file_put_contents($moveto, base64_decode($src)); } $this->dispatchPostUploadEvent($moveto, $file->path); } catch (\BackBee\Exception\BBException $e) { return false; } return $moveto; }
public function renderTemplate($filename, array $templateDir, array $params = [], array $vars = []) { foreach ($params as $key => $v) { $this->setParam($key, $v); } foreach ($vars as $k => $v) { $this->assign($k, $v); } try { File::resolveFilepath($filename, null, array('include_path' => $templateDir)); ob_start(); include $filename; return ob_get_clean(); } catch (FrontControllerException $fe) { ob_end_clean(); throw $fe; } catch (Exception $e) { ob_end_clean(); throw new RendererException($e->getMessage() . ' in ' . $filename, RendererException::RENDERING_ERROR, $e); } }
/** * Updates a file script of a layout. * * @param Layout $layout The layout to update * * @return string The filename of the updated script */ public function updateLayout(Layout $layout) { if (null === $layout->getSite()) { return false; } $layoutfile = $this->getLayoutFile($layout); File::resolveFilepath($layoutfile, null, array('include_path' => $this->_layoutdir)); if (false === file_exists($layoutfile)) { File::resolveFilepath($layoutfile, null, array('base_dir' => $this->_layoutdir[1])); } if (false === file_exists($layoutfile) && false === touch($layoutfile)) { throw new RendererException(sprintf('Unable to create file %s.', $layoutfile), RendererException::LAYOUT_ERROR); } if (!is_writable($layoutfile)) { throw new RendererException(sprintf('Unable to open file %s in writing mode.', $layoutfile), RendererException::LAYOUT_ERROR); } return $layoutfile; }
/** * @see BackBee\Renderer\RendererInterface::partial() */ public function partial($template = null, $params = null) { $this->templateFile = $template; File::resolveFilepath($this->templateFile, null, array('include_path' => $this->_scriptdir)); if (!is_file($this->templateFile) || !is_readable($this->templateFile)) { throw new RendererException(sprintf('Unable to find file \'%s\' in path (%s)', $template, implode(', ', $this->_scriptdir)), RendererException::SCRIPTFILE_ERROR); } // Assign parameters if (null !== $params) { $params = (array) $params; foreach ($params as $param => $value) { $this->setParam($param, $value); } } return $this->renderTemplate(true); }