/**
  * {@inheritdoc}
  */
 public function loadUserByUsername($username)
 {
     $json = FilesystemTools::readFile(sprintf('%s/users/users.json', $this->configurationHandler->siteDir()));
     $users = json_decode($json, true);
     if (array_key_exists($username, $users)) {
         $userData = $users[$username];
         return new User($username, $userData["password"], $userData["salt"], $userData["roles"]);
     }
     throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
 }
 private function updateBlock($file, $slotName, $blockName)
 {
     $json = FilesystemTools::readFile($file);
     $block = JsonTools::toBlock($this->serializer, $json);
     if (null === $block) {
         return null;
     }
     $block->setName($blockName);
     $block->setSlotName($slotName);
     return $block;
 }
 /**
  * Removes the block from the given slot
  *
  * @param $sourceDir
  * @param array $options
  * @param $username
  */
 public function remove($sourceDir, array $options, $username)
 {
     $dir = $this->init($sourceDir, $options, $username)->getDirInUse();
     $blockName = $options["blockname"];
     $blocksDir = $dir . '/blocks';
     $filename = sprintf('%s/%s.json', $blocksDir, $blockName);
     $options["block"] = JsonTools::jsonDecode(FilesystemTools::readFile($filename));
     Dispatcher::dispatch(BlockEvents::BLOCK_REMOVING, new BlockRemovingEvent($this->serializer, $filename));
     $this->filesystem->remove($filename);
     $this->removeBlockFromSlotFile($options, $dir);
     Dispatcher::dispatch(BlockEvents::BLOCK_REMOVED, new BlockRemovedEvent($this->serializer, $filename));
     DataLogger::log(sprintf('Block "%s" has been removed from the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 private function addBlock($dir, array $options, $blockName)
 {
     $filename = sprintf('%s/blocks/%s.json', $dir, $blockName);
     $block = BlockFactory::createBlock($options["type"]);
     $block->setName($blockName);
     $block->setSlotName($options["slot"]);
     $blockClass = get_class($block);
     $encodedBlock = $this->serializer->serialize($block, 'json');
     $event = Dispatcher::dispatch(BlockEvents::BLOCK_ADDING, new BlockAddingEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     $blockContent = $event->getFileContent();
     FilesystemTools::writeFile($filename, $blockContent);
     Dispatcher::dispatch(BlockEvents::BLOCK_ADDED, new BlockAddedEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     return $blockContent;
 }
 /**
  * Approves a contribution
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  *
  * @return array The approved block
  */
 public function approve($sourceDir, array $options, $username)
 {
     $this->init($sourceDir, $options, $username);
     $sourceFilename = sprintf('%s/blocks/%s.json', $this->contributorDir, $options['blockname']);
     $targetFilename = sprintf('%s/blocks/%s.json', $this->productionDir, $options['blockname']);
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVING, new BlockApprovingEvent($this->serializer, $sourceFilename, $targetFilename));
     $blockValues = JsonTools::jsonDecode(FilesystemTools::readFile($sourceFilename));
     $blockValues["history"] = array();
     FilesystemTools::writeFile($targetFilename, json_encode($blockValues));
     $slotDefinitionContribution = $this->getSlotDefinition($this->getContributorDir());
     $this->saveSlotDefinition($this->productionDir, $slotDefinitionContribution);
     Dispatcher::dispatch(BlockEvents::BLOCK_APPROVED, new BlockApprovedEvent($this->serializer, $sourceFilename, $targetFilename));
     DataLogger::log(sprintf('Block "%s" has been approved on the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
     return $blockValues;
 }
 private function writeSlots($template, array $slots, $repeat)
 {
     foreach ($slots as $slotName) {
         $fileName = sprintf('%s/%s/%s.json', $this->themeDir, $template, $slotName);
         $value = array("blocks" => array());
         if (file_exists($fileName)) {
             $slot = json_decode(FilesystemTools::readFile($fileName), true);
             if ($slot["repeat"] == $repeat) {
                 continue;
             }
             $value["blocks"] = $slot["blocks"];
         }
         $value["repeat"] = $repeat;
         FilesystemTools::writeFile($fileName, json_encode($value));
     }
 }
 /**
  * Edits the given block
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  * @param array $values
  */
 public function edit($sourceDir, array $options, $username, $values)
 {
     $this->resolveOptions($options);
     $this->init($sourceDir, $options, $username);
     $this->createContributorDir($sourceDir, $options, $username);
     $filename = sprintf('%s/blocks/%s.json', $this->getDirInUse(), $options["blockname"]);
     $currentBlock = $options["baseBlock"] = JsonTools::jsonDecode(FilesystemTools::readFile($filename));
     $values = $this->parseChildren($values);
     $block = JsonTools::join($currentBlock, $values);
     $encodedBlock = json_encode($block);
     $blockClass = BlockFactory::getBlockClass($block["type"]);
     $event = Dispatcher::dispatch(BlockEvents::BLOCK_EDITING, new BlockEditingEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     $blockContent = $event->getFileContent();
     FilesystemTools::writeFile($filename, $blockContent);
     Dispatcher::dispatch(BlockEvents::BLOCK_EDITED, new BlockEditedEvent($this->serializer, $filename, $encodedBlock, $blockClass));
     DataLogger::log(sprintf('Block "%s" has been edited on the "%s" slot on page "%s" for the "%s_%s" language', $options["blockname"], $options["slot"], $options["page"], $options["language"], $options["country"]));
 }
 /**
  * Archives the given block
  *
  * @param string $sourceDir
  * @param array $options
  * @param string $username
  * @param array $block
  */
 public function archive($sourceDir, array $options, $username, $block)
 {
     $this->resolveOptions($options);
     $block = json_decode($block, true);
     $block["history"] = array();
     $this->init($sourceDir, $options, $username);
     $historyDirName = sprintf('%s/archive/%s', $this->getDirInUse(), $options["blockname"]);
     $historyFileName = $historyDirName . '/history.json';
     if (!is_dir($historyDirName)) {
         mkdir($historyDirName);
     }
     $history = array();
     if (file_exists($historyFileName)) {
         $history = json_decode(file_get_contents($historyFileName), true);
     }
     $history = array_merge($history, array($block["history_name"] => $block));
     FilesystemTools::writeFile($historyFileName, json_encode($history));
 }
 /**
  * Implements the action to update the user information
  * @param array $options
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function save(array $options)
 {
     $resolver = new OptionsResolver();
     $this->configureOptions($resolver);
     $this->options = $resolver->resolve($options);
     $request = $this->options["request"];
     $password = $request->get('password');
     $user = $this->fetchUser($this->options['security'], $this->options['configuration_handler']);
     $factory = $this->options['encoder_factory'];
     $encoder = $factory->getEncoder($user);
     $salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
     $password = $encoder->encodePassword($password, $salt);
     $userName = "******";
     $usersFile = $this->options['configuration_handler']->usersDir() . '/users.json';
     $users = json_decode(FilesystemTools::readFile($usersFile), true);
     $user = $users[$userName];
     $user["password"] = $password;
     $user["salt"] = $salt;
     $users[$userName] = $user;
     FilesystemTools::writeFile($usersFile, json_encode($users));
 }
 private function saveQueueFile($message)
 {
     $queueDir = dirname($this->queueFile);
     if (!is_dir($queueDir)) {
         mkdir($queueDir);
     }
     $queue = array("error" => $message, "queue" => $this->queue["queue"]);
     FilesystemTools::writeFile($this->queueFile, json_encode($queue));
 }
 private function fetchPageAttributes($sourceDir)
 {
     $fileName = null !== $this->username ? $this->username : '******';
     $file = sprintf('%s/pages/pages/%s/%s.json', $sourceDir, $this->pageName, $fileName);
     $this->pageAttributes = json_decode(FilesystemTools::readFile($file), true);
     $fileName = null !== $this->username ? $this->username : '******';
     $file = sprintf('%s/pages/pages/%s/%s/%s.json', $sourceDir, $this->pageName, $this->currentLanguage, $fileName);
     if ($fileName === 'seo' && !file_exists($file)) {
         throw new PageNotPublishedException('production_page_not_available');
     }
     $this->seoAttributes = json_decode(FilesystemTools::readFile($file), true);
 }
 private function getBaseDir($sourceDir)
 {
     return FilesystemTools::slotDir($sourceDir, $this->options);
 }
 private function fetchSiteInfo()
 {
     $this->siteInfo = json_decode(FilesystemTools::readFile($this->siteDir . '/site.json'), true);
     $fullLanguage = explode('_', $this->siteInfo["locale_default"]);
     $this->language = $fullLanguage[0];
     $this->country = $fullLanguage[1];
 }
 private function parseConfiguration()
 {
     $informationFile = $this->pluginDir . '/plugin.json';
     if (!file_exists($informationFile)) {
         throw new LogicException(sprintf('You must define a plugin.json file for the %s plugin.', $this->name));
     }
     $information = json_decode(FilesystemTools::readFile($informationFile), true);
     if (null === $information) {
         throw new LogicException(sprintf('The %s plugin is empty.', $this->name));
     }
     $this->information = array();
     foreach ($information as $key => $param) {
         $tokens = explode('-', $key);
         $method = "get";
         foreach ($tokens as $token) {
             $method .= ucfirst($token);
         }
         $this->information[$method] = $param;
     }
 }
 private function doSaveBlocks(BlockManagerApprover $approver, $basePath, array $options)
 {
     $options["slot"] = basename($basePath);
     $slotPath = sprintf('%s/contributors/%s', FilesystemTools::slotDir($this->baseDir, $options), $this->username);
     if (!is_dir($slotPath)) {
         return array();
     }
     $activeSlotDefinition = json_decode(FilesystemTools::readFile($basePath . '/active/slot.json'), true);
     $contributorSlotDefinition = json_decode(FilesystemTools::readFile($slotPath . '/slot.json'), true);
     $removedBlocks = array();
     $contributorSlotDefinitionBlocks = $contributorSlotDefinition["blocks"];
     if (null === $contributorSlotDefinitionBlocks) {
         $contributorSlotDefinitionBlocks = array();
     }
     if (null !== $activeSlotDefinition) {
         $removedBlocks = array_diff_key($activeSlotDefinition["blocks"], $contributorSlotDefinitionBlocks);
     }
     foreach ($removedBlocks as $blockName) {
         $options["blockname"] = $blockName;
         $approver->approveRemoval($this->baseDir, $options, $this->username);
     }
     $approvedBlocks = array();
     $blocks = array_diff_key($contributorSlotDefinitionBlocks, $removedBlocks);
     foreach ($blocks as $blockName) {
         $options["blockname"] = $blockName;
         $approved = $approver->approve($this->baseDir, $options, $this->username);
         $approvedBlocks[] = $approved;
     }
     return $approvedBlocks;
 }
 /**
  * Find the slots parsing the theme's templates
  *
  * @return array
  */
 protected function findSlotsInTemplates()
 {
     $templates = $this->findTemplates();
     $slots = array();
     foreach ($templates["base"] as $templateName => $templateFile) {
         $templateContents = FilesystemTools::readFile($templateFile);
         $slots = array_merge_recursive($slots, $this->findSlots($templateName, $templateContents));
     }
     $baseSlots["base"] = $slots;
     $slots = array();
     foreach ($templates["template"] as $templateName => $templateFile) {
         $templateContents = FilesystemTools::readFile($templateFile);
         $slots[$templateName] = $this->findSlots($templateName, $templateContents);
     }
     return array('base' => $baseSlots, 'templates' => $slots);
 }
 /**
  * Saves the slot definition
  *
  * @param string $dir
  * @param array $slot
  */
 protected function saveSlotDefinition($dir, array $slot)
 {
     $slotsFilename = $this->getSlotDefinitionFile($dir);
     FilesystemTools::writeFile($slotsFilename, json_encode($slot), $this->filesystem);
 }
 /**
  * Approves the page in production
  * @param $pageName
  * @param $languageName
  *
  * @return string The json seo content
  */
 public function approve($pageName, $languageName)
 {
     $this->contributorDefined();
     $baseDir = $this->pagesDir . '/' . $pageName . '/' . $languageName;
     $sourceFile = $baseDir . '/' . $this->username . '.json';
     $targetFile = $baseDir . '/seo.json';
     if (!file_exists($targetFile)) {
         throw new PageNotPublishedException('exception_page_not_published');
     }
     $values = json_decode(FilesystemTools::readFile($sourceFile), true);
     if (!empty($values["current_permalink"])) {
         $values["changed_permalinks"][] = $values["current_permalink"];
         $values["current_permalink"] = "";
     }
     $encodedSeo = json_encode($values);
     $event = Dispatcher::dispatch(PageEvents::PAGE_APPROVING, new PageApprovingEvent($sourceFile, $encodedSeo));
     $encodedSeo = $event->getFileContent();
     FilesystemTools::writeFile($sourceFile, $encodedSeo);
     FilesystemTools::writeFile($targetFile, $encodedSeo);
     Dispatcher::dispatch(PageEvents::PAGE_APPROVED, new PageApprovedEvent($sourceFile, $encodedSeo));
     DataLogger::log(sprintf('Page SEO attributes "%s" for language "%s" were approved', $pageName, $languageName));
     return $encodedSeo;
 }
 private function initTemplates()
 {
     $templateNames = array_keys($this->templates["template"]);
     foreach ($templateNames as $templateName) {
         $blocks = array();
         $templateFileName = $this->themeDir . '/' . $templateName;
         if (!is_dir($templateFileName)) {
             $this->templateBlocks[$templateName] = $blocks;
             continue;
         }
         $finder = new Finder();
         $slotFiles = $finder->files()->in($this->themeDir . '/' . $templateName);
         foreach ($slotFiles as $slotFile) {
             $slotName = basename($slotFile, '.json');
             $slot = json_decode(FilesystemTools::readFile((string) $slotFile), true);
             $blocks[$slotName] = $slot["blocks"];
         }
         $this->templateBlocks[$templateName] = $blocks;
     }
 }
 /**
  * Edits the handled page
  * @param array $values
  *
  * @return string The encoded page
  */
 public function edit(array $values)
 {
     $currentName = $values["currentName"];
     unset($values["currentName"]);
     $pageDir = $this->pagesDir . '/' . $values["name"];
     if (!is_dir($pageDir)) {
         $pageDir = $this->pagesDir . '/' . $currentName;
     }
     $pageFile = $pageDir . '/' . $this->pageFile;
     $currentValues = json_decode(FilesystemTools::readFile($pageFile), true);
     if (array_key_exists("template", $values) && $currentValues["template"] != $values["template"]) {
         Dispatcher::dispatch(PageCollectionEvents::TEMPLATE_CHANGED, new TemplateChangedEvent($currentValues["template"], $values["template"], $this->username));
     }
     $values = array_merge($currentValues, $values);
     $values = $this->slugifyPageName($values);
     $targetFolder = $this->pagesDir . '/' . $values["name"];
     $encodedPage = json_encode($values);
     $event = Dispatcher::dispatch(PageCollectionEvents::PAGE_COLLECTION_EDITING, new PageCollectionEditingEvent($pageFile, $encodedPage));
     $encodedPage = $event->getFileContent();
     FilesystemTools::writeFile($pageFile, $encodedPage);
     if ($currentName != $values["name"]) {
         rename($pageDir, $targetFolder);
     }
     Dispatcher::dispatch(PageCollectionEvents::PAGE_COLLECTION_EDITED, new PageCollectionEditedEvent($pageFile, $encodedPage));
     DataLogger::log(sprintf('Page "%s" was successfully edited', $currentName));
     return $encodedPage;
 }
 private function generateLanguagesRoutes($routes, $page, $seoFileName)
 {
     $page = (string) $page;
     $pageName = basename($page);
     $languagesFinder = new Finder();
     $languages = $languagesFinder->directories()->depth(0)->in($page);
     foreach ($languages as $language) {
         $language = (string) $language;
         $seoFile = $language . '/' . $seoFileName;
         if (!file_exists($seoFile)) {
             continue;
         }
         $languageName = basename($language);
         $languageTokens = explode('_', $languageName);
         $routeName = '_' . $languageName . '_' . $pageName;
         $values = array('_locale' => $languageTokens[0], 'country' => $languageTokens[1], 'page' => $pageName);
         $this->routes["pages"][] = $routeName;
         $pattern = $this->pattern;
         if (substr($pattern, -1) != '/') {
             $pattern .= '/';
         }
         $pageValues = json_decode(FilesystemTools::readFile($seoFile), true);
         $this->addChangedPermalinks($routes, $routeName, $pattern, $pageValues);
         $values = array_merge($values, array('_controller' => $this->frontController));
         $routes->add($routeName, new Route($pattern . $pageValues["permalink"], $values));
     }
 }
 private function moveBlockToSameSlot($baseDir, array $options, $username)
 {
     $sourceDir = $this->init($baseDir, $options, $username)->getDirInUse();
     $slotsFilename = sprintf('%s/slot.json', $sourceDir);
     $slot = JsonTools::jsonDecode(FilesystemTools::readFile($slotsFilename), true);
     $blocks = $slot["blocks"];
     $key = array_search($options["blockname"], $blocks);
     $blockName = $blocks[$key];
     unset($blocks[$key]);
     array_splice($blocks, $options["position"], 0, $blockName);
     $slot["blocks"] = $blocks;
     $encodedSlot = json_encode($slot);
     $targetFile = sprintf('%s/blocks/%s.json', $sourceDir, $options["blockname"]);
     $event = Dispatcher::dispatch(BlockEvents::BLOCK_MOVING_SAME_SLOT, new BlockMovingSameSlotEvent($this->serializer, $blocks, $options["position"], $targetFile, $encodedSlot));
     $slotContent = $event->getFileContent();
     FilesystemTools::writeFile($slotsFilename, $slotContent);
     $block = FilesystemTools::readFile($targetFile);
     Dispatcher::dispatch(BlockEvents::BLOCK_MOVED_SAME_SLOT, new BlockMovedSameSlotEvent($this->serializer, $blocks, $options["position"], $targetFile, $encodedSlot));
     DataLogger::log(sprintf('Block "%s" has been moved to position "%s" on the slot "%s" on "%s" page for "%s_%s" language', $options["blockname"], $options["position"], $options["slot"], $options["page"], $options["language"], $options["country"]));
     return $block;
 }
 private function saveTemplateSlots(array $slots, $templateName)
 {
     foreach ($slots as $slot) {
         $slotName = $slot->getSlotName();
         $templateSlotFile = sprintf('%s/%s/%s.json', $this->themeDir, $templateName, $slotName);
         if (!file_exists($templateSlotFile)) {
             continue;
         }
         $templateSlotContent = json_decode(file_get_contents($templateSlotFile), true);
         $blocks = array();
         foreach ($slot->getProductionEntities() as $block) {
             $blocks[] = json_decode($block, true);
         }
         $templateSlotContent["blocks"] = $blocks;
         FilesystemTools::writeFile($templateSlotFile, json_encode($templateSlotContent));
     }
 }
 /**
  * Generate blocks for the current slot
  * @param array $blocks
  * @param string $blocksDir
  * @param string $targetDir
  */
 protected function generateBlocks(array $blocks, $blocksDir, $targetDir)
 {
     $c = 1;
     $generatedBlocks = array();
     foreach ($blocks as $block) {
         $blockName = 'block' . $c;
         $fileName = sprintf('%s/%s.json', $blocksDir, $blockName);
         $generatedBlocks[] = $blockName;
         $value = $block;
         if (is_array($value)) {
             $value = json_encode($block);
         }
         FilesystemTools::writeFile($fileName, $value);
         $c++;
     }
     $slotDefinition = array('next' => $c, 'blocks' => $generatedBlocks, 'revision' => 1);
     FilesystemTools::writeFile($targetDir . '/slot.json', json_encode($slotDefinition));
 }