Esempio n. 1
0
 /**
  * Wrap module with inline admin controls.
  *
  * @param string $content
  * @param Curry_PageModuleWrapper $pageModuleWrapper
  * @return string
  */
 protected function adminModule($content, Curry_PageModuleWrapper $pageModuleWrapper)
 {
     $user = User::getUser();
     $pageId = $pageModuleWrapper->getPageRevision()->getPageId();
     $page = $pageModuleWrapper->getPageRevision()->getPage();
     $pageModuleId = $pageModuleWrapper->getPageModuleId();
     $commands = array();
     $templatePermission = $user->hasAccess('Curry_Backend_Template');
     $contentPermission = $user->hasPagePermission($page, PageAccessPeer::PERM_CONTENT);
     $modulePermission = $user->hasPagePermission($page, PageAccessPeer::PERM_MODULES);
     $createPermission = $user->hasPagePermission($page, PageAccessPeer::PERM_CREATE_MODULE);
     if (!$user->hasModuleAccess($pageModuleWrapper)) {
         return $content;
     }
     if ($contentPermission || $modulePermission) {
         $url = url('admin.php', array('module' => 'Curry_Backend_Page', 'view' => 'Module', 'page_id' => $pageId, 'page_module_id' => $pageModuleId));
         if ($user->hasPagePermission($page, PageAccessPeer::PERM_CONTENT)) {
             $commands['edit'] = array('Name' => 'Edit', 'Url' => $url, 'Class' => 'iframe');
         }
     }
     if ($pageModuleWrapper->getTemplate() && $templatePermission) {
         $url = url('admin.php', array('module' => 'Curry_Backend_Template', 'view' => 'Edit', 'file' => $pageModuleWrapper->getTemplate()));
         $commands['template'] = array('Name' => 'Edit template', 'Url' => $url, 'Class' => 'iframe');
     }
     if ($modulePermission) {
         $url = url('admin.php', array('module' => 'Curry_Backend_Page', 'view' => 'ModuleProperties', 'page_id' => $pageId, 'page_module_id' => $pageModuleId));
         $commands['properties'] = array('Name' => 'Properties', 'Url' => $url, 'Class' => 'iframe');
     }
     if ($createPermission && ($contentPermission && !$pageModuleWrapper->isInherited() || $modulePermission)) {
         $url = url('admin.php', array('module' => 'Curry_Backend_Page', 'view' => 'DeleteModule', 'page_id' => $pageId, 'page_module_id' => $pageModuleId));
         $commands['delete'] = array('Name' => 'Delete', 'Url' => $url, 'Class' => 'iframe');
     }
     $module = $pageModuleWrapper->createObject();
     $module->setPageGenerator($this);
     $commands = $module->getInlineCommands($commands);
     if (!count($commands)) {
         return $content;
     }
     $tpl = Curry_Twig_Template::loadTemplateString(Curry_InlineAdmin::getAdminModuleTpl());
     return $tpl->render(array('Id' => $pageModuleId, 'Name' => $pageModuleWrapper->getName(), 'ClassName' => $pageModuleWrapper->getClassName(), 'Content' => $content, 'commands' => $commands));
 }
Esempio n. 2
0
 /**
  * Handle the specified request.
  *
  * @param Curry_Request $request
  */
 public function handle(Curry_Request $request)
 {
     trace_notice('Starting request at ' . $request->getUri());
     if (Curry_Core::$config->curry->autoPublish) {
         $this->autoPublish();
     }
     $page = null;
     $vars = array('curry' => array());
     $options = array();
     $forceShow = false;
     $showWorking = false;
     if (Curry_Core::$config->curry->setup) {
         die('Site is not yet configured, go to admin.php and configure your site.');
     }
     // check if we have a valid backend-user logged in
     $validUser = !!User::getUser();
     if ($validUser) {
         // check for inline-admin
         $adminNamespace = new Zend_Session_Namespace('Curry_Admin');
         if (Curry_Core::$config->curry->liveEdit && !$request->getParam('curry_force_show')) {
             if ($request->hasParam('curry_inline_admin')) {
                 $adminNamespace->inlineAdmin = $request->getParam('curry_inline_admin') ? true : false;
             }
             if ($adminNamespace->inlineAdmin) {
                 $options['inlineAdmin'] = true;
                 $forceShow = true;
                 $showWorking = true;
                 Curry_InlineAdmin::$active = true;
             }
         }
         // show working revision? (default is published)
         if ($request->getParam('curry_show_working')) {
             $forceShow = true;
             $showWorking = true;
         }
         // show inactive pages?
         if ($request->getParam('curry_force_show')) {
             $forceShow = true;
         }
         if ($showWorking) {
             Page::setRevisionType(Page::WORKING_REVISION);
         }
     }
     // Maintenance enabled?
     if (Curry_Core::$config->curry->maintenance->enabled && !$forceShow) {
         Curry_Core::log("Maintenance enabled");
         header('HTTP/1.1 503 Service Temporarily Unavailable');
         header('Status: 503 Service Temporarily Unavailable');
         header('Retry-After: 3600');
         $message = 'Page is down for maintenance, please check back later.';
         if (Curry_Core::$config->curry->maintenance->message) {
             $message = Curry_Core::$config->curry->maintenance->message;
         }
         $page = Curry_Core::$config->curry->maintenance->page;
         if ($page !== null) {
             $page = PageQuery::create()->findPk((int) $page);
         }
         if (!$page) {
             die($message);
         }
         $vars['curry']['MaintenanceMessage'] = $message;
     }
     // Check force domain?
     if (Curry_Core::$config->curry->forceDomain && !$forceShow) {
         $uri = $request->getUri();
         $url = parse_url(Curry_Core::$config->curry->baseUrl);
         if (strcasecmp($_SERVER['HTTP_HOST'], $url['host']) !== 0) {
             $location = substr(Curry_Core::$config->curry->baseUrl, 0, -1) . $uri;
             header("Location: " . $location, true, 301);
             exit;
         }
     }
     // Parameters to show a single module
     if ($request->getParam('curry_show_page_module_id')) {
         $options['pageModuleId'] = $request->getParam('curry_show_page_module_id');
     }
     if (isAjax() && $request->getParam('curry_ajax_page_module_id')) {
         $options['pageModuleId'] = $request->getParam('curry_ajax_page_module_id');
     }
     // Attempt to find cached page
     if ($request->getMethod() === 'GET') {
         $time = microtime(true);
         $cacheName = __CLASS__ . '_Page_' . md5($request->getUri());
         if (($cache = Curry_Core::$cache->load($cacheName)) !== false) {
             trace_notice('Using cached page content');
             foreach ($cache['headers'] as $header) {
                 header($header);
             }
             echo $cache['content'];
             Curry_Core::triggerHook('Curry_Application::render', $cache['page_id'], $cache['page_revision_id'], microtime(true) - $time, 0);
             return;
         }
     }
     // attempt to find the requested page
     if (!$page) {
         try {
             $page = $this->findPage($request);
             $page = $this->redirectPage($page, $request);
         } catch (Exception $e) {
             Curry_Core::log('Error when trying to find page: ' . $e->getMessage(), Zend_Log::ERR);
             $page = null;
         }
         // make sure page is enabled
         if ($page instanceof Page && !$forceShow && !$page->getEnabled()) {
             Curry_Core::log('Page is not accessible', Zend_Log::ERR);
             $page = null;
         }
     }
     // Page was not found, attempt to find 404 page
     if (!$page) {
         header("HTTP/1.1 404 Not Found");
         if (Curry_Core::$config->curry->errorPage->notFound) {
             $page = PageQuery::create()->findPk(Curry_Core::$config->curry->errorPage->notFound);
             if (!$page || !$page->getEnabled()) {
                 throw new Exception('Page not found, additionally the page-not-found page could not be found.');
             }
         } else {
             die('Page not found');
         }
     }
     // Set language
     $language = $page->getInheritedProperty('Language');
     $fallbackLanguage = Curry_Core::$config->curry->fallbackLanguage;
     if ($language) {
         $this->setLanguage($language);
     } else {
         if ($fallbackLanguage) {
             trace_warning('Using fallback language');
             $this->setLanguage($fallbackLanguage);
         } else {
             trace_warning('Language not set for page');
         }
     }
     // Attempt to render page
     try {
         $this->render($page->getPageRevision(), $request, $vars, $options);
     } catch (Curry_Exception_Unauthorized $e) {
         Curry_Core::log($e->getMessage(), Zend_Log::ERR);
         if (!headers_sent()) {
             header("HTTP/1.1 " . $e->getStatusCode() . " " . $e->getMessage());
         }
         if (Curry_Core::$config->curry->errorPage->unauthorized) {
             Curry_Core::log('Showing unauthorized page', Zend_Log::NOTICE);
             $page = PageQuery::create()->findPk(Curry_Core::$config->curry->errorPage->unauthorized);
             if (!$page) {
                 throw new Exception('Unauthorized page not found');
             }
             try {
                 $vars = array('curry' => array('error' => array('Message' => $e->getMessage(), 'Trace' => $e->getTraceAsString())));
                 $options = array();
                 $this->render($page->getPageRevision(), $request, $vars, $options);
             } catch (Exception $e2) {
                 Curry_Core::log('An error occured while trying to generate the unauthorized page: ' . $e2->getMessage(), Zend_Log::ERR);
                 throw $e;
             }
         } else {
             throw $e;
         }
     } catch (Curry_Exception_HttpError $e) {
         Curry_Core::log($e->getMessage(), Zend_Log::ERR);
         if (!headers_sent()) {
             header("HTTP/1.1 " . $e->getStatusCode() . " " . $e->getMessage());
         }
     } catch (Exception $e) {
         Curry_Core::log($e->getMessage(), Zend_Log::ERR);
         if (!headers_sent()) {
             header("HTTP/1.1 500 Internal server error");
         }
         if (Curry_Core::$config->curry->errorNotification) {
             Curry_Core::sendErrorNotification($e);
         }
         if (Curry_Core::$config->curry->errorPage->error) {
             Curry_Core::log('Trying to show error page', Zend_Log::NOTICE);
             $page = PageQuery::create()->findPk(Curry_Core::$config->curry->errorPage->error);
             if (!$page) {
                 throw new Exception('Error page not found');
             }
             try {
                 $vars = array('curry' => array('error' => array('Message' => $e->getMessage(), 'Trace' => $e->getTraceAsString())));
                 $options = array();
                 $this->render($page->getPageRevision(), $request, $vars, $options);
             } catch (Exception $e2) {
                 Curry_Core::log('An error occured, additionally an error occured while trying to generate the error page: ' . $e2->getMessage(), Zend_Log::ERR);
                 throw $e;
             }
         } else {
             throw $e;
         }
     }
 }