/** * Build a list of sites * * @param ControllerContext $controllerContext * @return array */ public function buildSiteList(ControllerContext $controllerContext) { $requestUriHost = $controllerContext->getRequest()->getHttpRequest()->getUri()->getHost(); $domainsFound = false; $sites = array(); foreach ($this->siteRepository->findOnline() as $site) { $uri = null; $active = false; /** @var $site Site */ if ($site->hasActiveDomains()) { $activeHostPatterns = $site->getActiveDomains()->map(function ($domain) { return $domain->getHostPattern(); })->toArray(); $active = in_array($requestUriHost, $activeHostPatterns, true); if ($active) { $uri = $controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true)->uriFor('index', array(), 'Backend\\Backend', 'Neos.Neos'); } else { $uri = $controllerContext->getUriBuilder()->reset()->uriFor('switchSite', array('site' => $site), 'Backend\\Backend', 'Neos.Neos'); } $domainsFound = true; } $sites[] = array('name' => $site->getName(), 'nodeName' => $site->getNodeName(), 'uri' => $uri, 'active' => $active); } if ($domainsFound === false) { $uri = $controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true)->uriFor('index', array(), 'Backend\\Backend', 'Neos.Neos'); $sites[0]['uri'] = $uri; } return $sites; }
/** * Add a domain record * * @param string $siteNodeName The nodeName of the site rootNode, e.g. "neostypo3org" * @param string $hostname The hostname to match on, e.g. "flow.neos.io" * @param string $scheme The scheme for linking (http/https) * @param integer $port The port for linking (0-49151) * @return void */ public function addCommand($siteNodeName, $hostname, $scheme = null, $port = null) { $site = $this->siteRepository->findOneByNodeName($siteNodeName); if (!$site instanceof Site) { $this->outputLine('<error>No site found with nodeName "%s".</error>', [$siteNodeName]); $this->quit(1); } $domains = $this->domainRepository->findByHostname($hostname); if ($domains->count() > 0) { $this->outputLine('<error>The host name "%s" is not unique.</error>', [$hostname]); $this->quit(1); } $domain = new Domain(); if ($scheme !== null) { $domain->setScheme($scheme); } if ($port !== null) { $domain->setPort($port); } $domain->setSite($site); $domain->setHostname($hostname); $domainValidator = $this->validatorResolver->getBaseValidatorConjunction(Domain::class); $result = $domainValidator->validate($domain); if ($result->hasErrors()) { foreach ($result->getFlattenedErrors() as $propertyName => $errors) { $firstError = array_pop($errors); $this->outputLine('<error>Validation failed for "' . $propertyName . '": ' . $firstError . '</error>'); $this->quit(1); } } $this->domainRepository->add($domain); $this->outputLine('Domain entry created.'); }
/** * Upload a new image, and return its metadata. * * Depending on the $metadata argument it will return asset metadata for the AssetEditor * or image metadata for the ImageEditor * * @param Asset $asset * @param string $metadata Type of metadata to return ("Asset" or "Image") * @return string */ public function uploadAssetAction(Asset $asset, $metadata) { $this->response->setHeader('Content-Type', 'application/json'); /** @var Site $currentSite */ $currentSite = $this->siteRepository->findOneByNodeName($this->request->getInternalArgument('__siteNodeName')); if ($currentSite !== null && $currentSite->getAssetCollection() !== null) { $currentSite->getAssetCollection()->addAsset($asset); $this->assetCollectionRepository->update($currentSite->getAssetCollection()); } switch ($metadata) { case 'Asset': $result = $this->getAssetProperties($asset); if ($this->persistenceManager->isNewObject($asset)) { $this->assetRepository->add($asset); } break; case 'Image': $result = $this->getImageInterfacePreviewData($asset); if ($this->persistenceManager->isNewObject($asset)) { $this->imageRepository->add($asset); } break; default: $this->response->setStatus(400); $result = array('error' => 'Invalid "metadata" type: ' . $metadata); } return json_encode($result); }
/** * @param FinisherContext $finisherContext * @return void * @throws Exception */ public function importSite(FinisherContext $finisherContext) { $formValues = $finisherContext->getFormRuntime()->getFormState()->getFormValues(); if (isset($formValues['prune']) && intval($formValues['prune']) === 1) { $this->nodeDataRepository->removeAll(); $this->workspaceRepository->removeAll(); $this->domainRepository->removeAll(); $this->siteRepository->removeAll(); $this->persistenceManager->persistAll(); } if (!empty($formValues['packageKey'])) { if ($this->packageManager->isPackageAvailable($formValues['packageKey'])) { throw new Exception(sprintf('The package key "%s" already exists.', $formValues['packageKey']), 1346759486); } $packageKey = $formValues['packageKey']; $siteName = $formValues['siteName']; $generatorService = $this->objectManager->get(\Neos\SiteKickstarter\Service\GeneratorService::class); $generatorService->generateSitePackage($packageKey, $siteName); } elseif (!empty($formValues['site'])) { $packageKey = $formValues['site']; } $this->deactivateOtherSitePackages($packageKey); $this->packageManager->activatePackage($packageKey); if (!empty($packageKey)) { try { $contentContext = $this->contextFactory->create(array('workspaceName' => 'live')); $this->siteImportService->importFromPackage($packageKey, $contentContext); } catch (\Exception $exception) { $finisherContext->cancel(); $this->systemLogger->logException($exception); throw new SetupException(sprintf('Error: During the import of the "Sites.xml" from the package "%s" an exception occurred: %s', $packageKey, $exception->getMessage()), 1351000864); } } }
/** * Prevents invalid calls to the site respository in case the site data property is not available. * * @return null|object */ protected function getCurrentSite() { if (!isset($this->data['site']) || $this->data['site'] === null) { return null; } return $this->siteRepository->findByIdentifier($this->data['site']); }
/** * @param AssetCollection $assetCollection * @return void */ public function deleteAssetCollectionAction(AssetCollection $assetCollection) { foreach ($this->siteRepository->findByAssetCollection($assetCollection) as $site) { $site->setAssetCollection(null); $this->siteRepository->update($site); } parent::deleteAssetCollectionAction($assetCollection); }
/** * Determines the current domain and site from the request and sets the resulting values as * as defaults. * * @param array $defaultContextProperties * @return array */ protected function setDefaultSiteAndDomainFromCurrentRequest(array $defaultContextProperties) { $currentDomain = $this->domainRepository->findOneByActiveRequest(); if ($currentDomain !== null) { $defaultContextProperties['currentSite'] = $currentDomain->getSite(); $defaultContextProperties['currentDomain'] = $currentDomain; } else { $defaultContextProperties['currentSite'] = $this->siteRepository->findDefault(); } return $defaultContextProperties; }
/** * Deactivate a site * * This command deactivates the specified site. * * @param string $siteNode The node name of the site to deactivate * @return void */ public function deactivateCommand($siteNode) { $site = $this->siteRepository->findOneByNodeName($siteNode); if (!$site instanceof Site) { $this->outputLine('<error>Site not found.</error>'); $this->quit(1); } $site->setState(Site::STATE_OFFLINE); $this->siteRepository->update($site); $this->outputLine('Site deactivated.'); }
/** * Updates or creates a site with the given $siteNodeName * * @param string $siteNodeName * @return Site */ protected function getSiteByNodeName($siteNodeName) { $site = $this->siteRepository->findOneByNodeName($siteNodeName); if ($site === null) { $site = new Site($siteNodeName); $this->siteRepository->add($site); } else { $this->siteRepository->update($site); } return $site; }
/** * Deletes a domain attached to a site * * @param Domain $domain A domain to delete * @Flow\IgnoreValidation("$domain") * @return void */ public function deleteDomainAction(Domain $domain) { $site = $domain->getSite(); if ($site->getPrimaryDomain() === $domain) { $site->setPrimaryDomain(null); $this->siteRepository->update($site); } $this->domainRepository->remove($domain); $this->addFlashMessage('The domain "%s" has been deleted.', 'Domain deleted', Message::SEVERITY_OK, array(htmlspecialchars($domain)), 1412373310); $this->unsetLastVisitedNodeAndRedirect('edit', null, null, array('site' => $site)); }
/** * Default action, displays the login screen * * @param string $username Optional: A username to pre-fill into the username field * @param boolean $unauthorized * @return void */ public function indexAction($username = null, $unauthorized = false) { if ($this->securityContext->getInterceptedRequest() || $unauthorized) { $this->response->setStatus(401); } if ($this->authenticationManager->isAuthenticated()) { $this->redirect('index', 'Backend\\Backend'); } $currentDomain = $this->domainRepository->findOneByActiveRequest(); $currentSite = $currentDomain !== null ? $currentDomain->getSite() : $this->siteRepository->findDefault(); $this->view->assignMultiple(['styles' => array_filter($this->settings['userInterface']['backendLoginForm']['stylesheets']), 'username' => $username, 'site' => $currentSite]); }
/** * Builds an array of changes for sites in the given workspace * * @param Workspace $selectedWorkspace * @return array */ protected function computeSiteChanges(Workspace $selectedWorkspace) { $siteChanges = []; foreach ($this->publishingService->getUnpublishedNodes($selectedWorkspace) as $node) { /** @var NodeInterface $node */ if (!$node->getNodeType()->isOfType('Neos.Neos:ContentCollection')) { $pathParts = explode('/', $node->getPath()); if (count($pathParts) > 2) { $siteNodeName = $pathParts[2]; $q = new FlowQuery([$node]); $document = $q->closest('[instanceof Neos.Neos:Document]')->get(0); // $document will be null if we have a broken root line for this node. This actually should never happen, but currently can in some scenarios. if ($document !== null) { $documentPath = implode('/', array_slice(explode('/', $document->getPath()), 3)); $relativePath = str_replace(sprintf(SiteService::SITES_ROOT_PATH . '/%s/%s', $siteNodeName, $documentPath), '', $node->getPath()); if (!isset($siteChanges[$siteNodeName]['siteNode'])) { $siteChanges[$siteNodeName]['siteNode'] = $this->siteRepository->findOneByNodeName($siteNodeName); } $siteChanges[$siteNodeName]['documents'][$documentPath]['documentNode'] = $document; $change = ['node' => $node, 'contentChanges' => $this->renderContentChanges($node)]; if ($node->getNodeType()->isOfType('Neos.Neos:Node')) { $change['configuration'] = $node->getNodeType()->getFullConfiguration(); } $siteChanges[$siteNodeName]['documents'][$documentPath]['changes'][$relativePath] = $change; } } } } $liveContext = $this->contextFactory->create(['workspaceName' => 'live']); ksort($siteChanges); foreach ($siteChanges as $siteKey => $site) { foreach ($site['documents'] as $documentKey => $document) { $liveDocumentNode = $liveContext->getNodeByIdentifier($document['documentNode']->getIdentifier()); $siteChanges[$siteKey]['documents'][$documentKey]['isMoved'] = $liveDocumentNode && $document['documentNode']->getPath() !== $liveDocumentNode->getPath(); $siteChanges[$siteKey]['documents'][$documentKey]['isNew'] = $liveDocumentNode === null; foreach ($document['changes'] as $changeKey => $change) { $liveNode = $liveContext->getNodeByIdentifier($change['node']->getIdentifier()); $siteChanges[$siteKey]['documents'][$documentKey]['changes'][$changeKey]['isNew'] = is_null($liveNode); $siteChanges[$siteKey]['documents'][$documentKey]['changes'][$changeKey]['isMoved'] = $liveNode && $change['node']->getPath() !== $liveNode->getPath(); } } ksort($siteChanges[$siteKey]['documents']); } return $siteChanges; }
/** * Remove all sites and their respective nodes and domains * * @return void */ public function pruneAll() { foreach ($this->siteRepository->findAll() as $site) { $this->pruneSite($site); } }
/** * Renders the URI to a given node instance or -path. * * @param ControllerContext $controllerContext * @param mixed $node A node object or a string node path, if a relative path is provided the baseNode argument is required * @param NodeInterface $baseNode * @param string $format Format to use for the URL, for example "html" or "json" * @param boolean $absolute If set, an absolute URI is rendered * @param array $arguments Additional arguments to be passed to the UriBuilder (for example pagination parameters) * @param string $section * @param boolean $addQueryString If set, the current query parameters will be kept in the URI * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the URI. Only active if $addQueryString = TRUE * @param boolean $resolveShortcuts INTERNAL Parameter - if FALSE, shortcuts are not redirected to their target. Only needed on rare backend occasions when we want to link to the shortcut itself. * @return string The rendered URI * @throws \InvalidArgumentException if the given node/baseNode is not valid * @throws NeosException if no URI could be resolved for the given node */ public function createNodeUri(ControllerContext $controllerContext, $node = null, NodeInterface $baseNode = null, $format = null, $absolute = false, array $arguments = array(), $section = '', $addQueryString = false, array $argumentsToBeExcludedFromQueryString = array(), $resolveShortcuts = true) { $this->lastLinkedNode = null; if (!($node instanceof NodeInterface || is_string($node) || $baseNode instanceof NodeInterface)) { throw new \InvalidArgumentException('Expected an instance of NodeInterface or a string for the node argument, or alternatively a baseNode argument.', 1373101025); } if (is_string($node)) { $nodeString = $node; if ($nodeString === '') { throw new NeosException(sprintf('Empty strings can not be resolved to nodes.', $nodeString), 1415709942); } preg_match(NodeInterface::MATCH_PATTERN_CONTEXTPATH, $nodeString, $matches); if (isset($matches['WorkspaceName']) && $matches['WorkspaceName'] !== '') { $node = $this->propertyMapper->convert($nodeString, NodeInterface::class); } else { if ($baseNode === null) { throw new NeosException('The baseNode argument is required for linking to nodes with a relative path.', 1407879905); } /** @var ContentContext $contentContext */ $contentContext = $baseNode->getContext(); $normalizedPath = $this->nodeService->normalizePath($nodeString, $baseNode->getPath(), $contentContext->getCurrentSiteNode()->getPath()); $node = $contentContext->getNode($normalizedPath); } if (!$node instanceof NodeInterface) { throw new NeosException(sprintf('The string "%s" could not be resolved to an existing node.', $nodeString), 1415709674); } } elseif (!$node instanceof NodeInterface) { $node = $baseNode; } if (!$node instanceof NodeInterface) { throw new NeosException(sprintf('Node must be an instance of NodeInterface or string, given "%s".', gettype($node)), 1414772029); } $this->lastLinkedNode = $node; if ($resolveShortcuts === true) { $resolvedNode = $this->nodeShortcutResolver->resolveShortcutTarget($node); } else { // this case is only relevant in extremely rare occasions in the Neos Backend, when we want to generate // a link towards the *shortcut itself*, and not to its target. $resolvedNode = $node; } if (is_string($resolvedNode)) { return $resolvedNode; } if (!$resolvedNode instanceof NodeInterface) { throw new NeosException(sprintf('Could not resolve shortcut target for node "%s"', $node->getPath()), 1414771137); } /** @var ActionRequest $request */ $request = $controllerContext->getRequest()->getMainRequest(); $uriBuilder = clone $controllerContext->getUriBuilder(); $uriBuilder->setRequest($request); $uri = $uriBuilder->reset()->setSection($section)->setArguments($arguments)->setAddQueryString($addQueryString)->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)->setFormat($format ?: $request->getFormat())->uriFor('show', array('node' => $resolvedNode), 'Frontend\\Node', 'Neos.Neos'); $siteNode = $resolvedNode->getContext()->getCurrentSiteNode(); if (NodePaths::isSubPathOf($siteNode->getPath(), $resolvedNode->getPath())) { /** @var Site $site */ $site = $resolvedNode->getContext()->getCurrentSite(); } else { $nodePath = NodePaths::getRelativePathBetween(SiteService::SITES_ROOT_PATH, $resolvedNode->getPath()); list($siteNodeName) = explode('/', $nodePath); $site = $this->siteRepository->findOneByNodeName($siteNodeName); } if ($site->hasActiveDomains()) { $requestUriHost = $request->getHttpRequest()->getBaseUri()->getHost(); $activeHostPatterns = $site->getActiveDomains()->map(function ($domain) { return $domain->getHostPattern(); })->toArray(); if (!in_array($requestUriHost, $activeHostPatterns, true)) { $uri = $this->createSiteUri($controllerContext, $site) . '/' . ltrim($uri, '/'); } elseif ($absolute === true) { $uri = $request->getHttpRequest()->getBaseUri() . ltrim($uri, '/'); } } elseif ($absolute === true) { $uri = $request->getHttpRequest()->getBaseUri() . ltrim($uri, '/'); } return $uri; }