Example #1
0
 /**
  * Get minified CSS path.
  *
  * @param array $files
  * @return string
  */
 public function getMinifyCss($files)
 {
     $basedir = 'cache/';
     $target = $basedir . sha1(serialize($files)) . '.css';
     // create basedir if it doesnt exist
     if (!is_dir($basedir)) {
         @mkdir($basedir, 0777, true);
     }
     // if minified file already exists, make sure it's up to date
     if (file_exists($target)) {
         $data = self::readMinificationHeader($target);
         if (self::getLastModified($data['files']) == $data['modified']) {
             trace_notice('Using cached css minification file');
             return $target . '?' . $data['modified'];
         }
         trace_notice('Updating css minification file');
     }
     // Combine files (and process imports)
     $imports = array();
     $content = "";
     foreach ($files as $file) {
         $processor = new CssProcessor($file['source']);
         $relative = Curry_Util::getRelativePath($basedir, dirname($file['source']));
         $content .= $processor->getContent($file['media'], $relative);
         $imports = array_merge($imports, $processor->getImportedFiles());
     }
     // Minify
     $source = new Minify_Source(array('id' => $target, 'content' => $content, 'contentType' => Minify::TYPE_CSS));
     $content = Minify::combine(array($source));
     // Add header
     $header = array('files' => $imports, 'modified' => self::getLastModified($imports));
     $content = "/* " . json_encode($header) . " */\n" . $content;
     // Write content
     file_put_contents($target, $content);
     return $target . '?' . $header['modified'];
 }
Example #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;
         }
     }
 }
Example #3
0
 /**
  * Get array with language strings for the specified language. This function caches the language strings in memory.
  *
  * @param Language|string|null $language Language, langcode (string) or null for active language.
  * @return array
  */
 private static function _getLanguage($language)
 {
     if ($language === null) {
         $language = self::$currentLanguage;
     } else {
         if (is_string($language)) {
             $language = LanguageQuery::create()->findPk($language);
         } else {
             if (!$language instanceof Language) {
                 throw new Exception('Invalid language');
             }
         }
     }
     if ($language) {
         // initialize language strings
         if (!array_key_exists($language->getLangcode(), self::$languageStrings)) {
             trace_notice('Loading translation-strings for ' . $language->getName() . ' (' . $language->getLangcode() . ')');
             self::$languageStrings[$language->getLangcode()] = LanguageStringTranslationQuery::create()->filterByLanguage($language)->find()->toKeyValue('StringId', 'Translation');
         }
     }
     return $language;
 }
Example #4
0
 /**
  * Send error notification email.
  *
  * @param Exception $e
  */
 public static function sendErrorNotification(Exception $e)
 {
     try {
         // Create form to recreate error
         $method = strtoupper($_SERVER['REQUEST_METHOD']);
         $hidden = Curry_Html::createHiddenFields($method == 'POST' ? $_POST : $_GET);
         $action = url(Curry_URL::getRequestUri())->getAbsolute();
         $form = '<form action="' . $action . '" method="' . $method . '">' . $hidden . '<button type="submit">Execute</button></form>';
         // Create mail
         $mail = new Curry_Mail();
         $mail->addTo(Curry_Core::$config->curry->adminEmail);
         $mail->setSubject('Error on ' . Curry_Core::$config->curry->name);
         $mail->setBodyHtml('<html><body>' . '<h1>' . get_class($e) . '</h1>' . '<h2>' . htmlspecialchars($e->getMessage()) . '</h2>' . '<p><strong>Method:</strong> ' . $method . '<br/>' . '<strong>URL:</strong> ' . $action . '<br/>' . '<strong>File:</strong> ' . htmlspecialchars($e->getFile()) . '(' . $e->getLine() . ')</p>' . '<h2>Recreate</h2>' . $form . '<h2>Trace</h2>' . '<pre>' . htmlspecialchars($e->getTraceAsString()) . '</pre>' . '<h2>Variables</h2>' . '<h3>$_GET</h3>' . '<pre>' . htmlspecialchars(print_r($_GET, true)) . '</pre>' . '<h3>$_POST</h3>' . '<pre>' . htmlspecialchars(print_r($_POST, true)) . '</pre>' . '<h3>$_SERVER</h3>' . '<pre>' . htmlspecialchars(print_r($_SERVER, true)) . '</pre>' . '</body></html>');
         $mail->send();
         trace_notice('Sent error notification');
     } catch (Exception $e) {
         trace_warning('Failed to send error notification');
     }
 }