Ejemplo n.º 1
0
 /** {@inheritdoc} */
 public function showMain()
 {
     $rootPage = PageQuery::create()->findRoot();
     $code = Curry_Backend_PageSyncHelper::getPageCode($rootPage);
     $localChecksum = sha1(serialize($code));
     if (isPost('fetch')) {
         Curry_Application::returnJson($code);
     }
     $form = new Curry_Form(array('csrfCheck' => false, 'action' => (string) url('', $_GET), 'method' => 'post', 'elements' => array('url' => array('text', array('label' => 'URL', 'placeholder' => 'http://example.com/admin.php', 'value' => isset($_COOKIE['curry:remote_url']) ? $_COOKIE['curry:remote_url'] : '')), 'user' => array('text', array('label' => 'User', 'value' => isset($_COOKIE['curry:remote_user']) ? $_COOKIE['curry:remote_user'] : '******')), 'password' => array('password', array('label' => 'Password', 'value' => '')), 'submit' => array('submit', array('class' => 'btn btn-primary', 'label' => 'Fetch')))));
     if (isPost('code')) {
         // we have page-code
         if ($localChecksum !== $_POST['local_checksum']) {
             throw new Exception('Local pages were changed during synchronization process, aborting!');
         }
         $remoteCode = json_decode($_POST['code'], true);
         // Update selected pages
         if (isset($_POST['page'])) {
             $updatedPages = Curry_Backend_PageSyncHelper::restorePages($rootPage, $remoteCode, array_keys($_POST['page']));
             $this->addMessage(count($updatedPages) . ' pages updated!', self::MSG_SUCCESS);
         }
         // Delete selected pages
         if (isset($_POST['delete'])) {
             $pagesToDelete = array_keys($_POST['delete']);
             foreach ($pagesToDelete as $pageId) {
                 $page = PageQuery::create()->findPk($pageId);
                 if (!$page) {
                     throw new Exception('Unable to find page to delete.');
                 }
                 if (!$page->isLeaf()) {
                     $this->addMessage('Unable to delete page "' . $page->getName() . '" because it has subpages.', self::MSG_ERROR);
                     continue;
                 }
                 $dependantPages = $page->getDependantPages();
                 if (count($dependantPages)) {
                     $this->addMessage('Unable to delete page "' . $page->getName() . '" because other pages depend on it.', self::MSG_ERROR);
                     continue;
                 }
                 $page->delete();
                 $this->addMessage('Deleted page "' . $page->getName() . '"', self::MSG_WARNING);
             }
         }
     } else {
         if (isPost() && $form->isValid($_POST)) {
             // have user/password
             try {
                 $context = stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(array('login_username' => $form->user->getValue(), 'login_password' => $form->password->getValue(), 'fetch' => '1')))));
                 $remote = (string) url($form->url->getValue(), array('module' => 'Curry_Backend_PageSync'));
                 $remoteResponse = file_get_contents($remote, null, $context);
                 if ($remoteResponse === false) {
                     throw new Exception('Invalid response');
                 }
                 $remoteCode = json_decode($remoteResponse, true);
                 if ($remoteCode === null) {
                     throw new Exception('Invalid json: ' . $remoteResponse);
                 }
                 setcookie('curry:remote_url', $form->url->getValue(), time() + 86400 * 365);
                 setcookie('curry:remote_user', $form->user->getValue(), time() + 86400 * 365);
                 $this->addMainContent('<form action="' . url('', $_GET) . '" method="post" class="well">');
                 $this->addMainContent('<input type="hidden" name="code" value="' . htmlspecialchars($remoteResponse) . '" />');
                 $this->addMainContent('<input type="hidden" name="local_checksum" value="' . htmlspecialchars($localChecksum) . '" />');
                 $this->addMainContent('<ul>');
                 $this->comparePageCode($code, $remoteCode);
                 $this->addMainContent('</ul>');
                 $this->addMainContent('<button type="submit" class="btn btn-primary">Sync</button>');
                 $this->addMainContent('</form>');
             } catch (Exception $e) {
                 $this->addMainContent($form);
                 $this->addMessage($e->getMessage(), self::MSG_ERROR);
             }
         } else {
             $this->addMainContent(self::INTRO);
             $this->addMainContent($form);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Copy/paste pages.
  */
 public function showCopyPaste()
 {
     $ses = new \Zend\Session\Container(__CLASS__);
     $ses->pageView = 'CopyPaste';
     $page = self::getPage(PageAccessPeer::PERM_MODULES);
     $copyForm = new Curry_Form(array('action' => url('', array('view', 'page_id')), 'method' => 'post', 'elements' => array('copy_code' => array('textarea', array('label' => 'Page Code', 'value' => json_encode(Curry_Backend_PageSyncHelper::getPageCode($page)), 'onclick' => 'select()', 'rows' => 3)))));
     $form = new Curry_Form(array('action' => url('', array('view', 'page_id')), 'method' => 'post', 'elements' => array('code' => array('textarea', array('label' => 'Paste code', 'onclick' => 'select()', 'rows' => 3)), 'subpages' => array('checkbox', array('label' => 'Subpages', 'value' => true)), 'submit' => array('submit', array('label' => 'Paste')))));
     if (isPost() && $form->isValid($_POST)) {
         $pageData = json_decode($form->code->getValue(), true);
         $subpages = (bool) $form->subpages->getValue();
         if ($pageData && $pageData['name']) {
             unset($pageData['name']);
             $pages = Curry_Backend_PageSyncHelper::restorePages($page, $pageData, $subpages);
             $this->addMessage('Pasted ' . count($pages) . ' page(s) successfully.', self::MSG_SUCCESS);
         }
     }
     $this->addPageMenu($page);
     $this->addMainContent($copyForm);
     $this->addMainContent($form);
 }