Пример #1
0
 /**
  * Loads a presenter, calls its method and sets the resulting string into
  * the PRESENTER_RESULT template variable.
  *
  * @return array
  * @throws \Exception
  */
 protected function render()
 {
     $presenter = $this->createPresenter($this->getRequiredTemplateVariable('Presenter'));
     try {
         $arguments = $this->getArguments($presenter);
         $result = call_user_func_array($presenter, $arguments);
     } catch (\Exception $e) {
         if ($this->throwRenderExceptions) {
             throw $e;
         }
         $this->Logger->error($e->getMessage() . "\n\nURL: " . \URLUtils::fullUrl() . "\n\nTemplate Vars:\n" . print_r($this->templateVars, true) . (isset($arguments) ? "\n\n{$presenter} args:\n" . print_r($arguments, true) : ''));
         $result = '';
     }
     $this->setTemplateVariable('PRESENTER_RESULT', $result);
     // just return something so the template engine thinks
     // something happened.
     return array(array());
 }
Пример #2
0
 public function getFullURL()
 {
     return URLUtils::fullUrl();
 }
Пример #3
0
 protected function _sendErrorNotification($prefix, $message, $subject = null, $customMessage = false, $extraRecipients = null)
 {
     $c = '';
     if ($customMessage) {
         $c .= $customMessage . "\n\n";
     }
     if ($message instanceof Exception) {
         $c .= get_class($message) . ": " . $message->getMessage() . "\n";
         if ($message instanceof SQLException) {
             $c .= "SQL: " . $message->getSQL() . "\n";
         }
         $c .= "Code: " . $message->getCode() . "\n";
         $c .= "File: " . $message->getFile() . "\n";
         $c .= "Line: " . $message->getLine() . "\n";
         $c .= "Stack Trace: " . $message->getTraceAsString() . "\n\n";
     } else {
         $c .= str_replace("\t", "  ", $message) . "\n\n";
     }
     $c .= "\nURL: " . URLUtils::fullUrl() . "\n\n";
     if ($this->verbose) {
         $debugServerVars = array_intersect_key($_SERVER, array_flip(array('SCRIPT_URL', 'SCRIPT_URI', 'ENVIRONMENT', 'PATH', 'SERVER_SIGNATURE', 'SERVER_SOFTWARE', 'SERVER_NAME', 'SERVER_ADDR', 'SERVER_PORT', 'REMOTE_ADDR', 'DOCUMENT_ROOT', 'SERVER_ADMIN', 'SCRIPT_FILENAME', 'REMOTE_PORT', 'GATEWAY_INTERFACE', 'SERVER_PROTOCOL', 'REQUEST_METHOD', 'QUERY_STRING', 'REQUEST_URI', 'SCRIPT_NAME', 'PHP_SELF', 'REQUEST_TIME', 'DEVICE_VIEW', 'DESIGN', 'DOMAIN', 'CONTEXT', 'ROUTER_BASE', 'MATCHED_ALIAS', 'REWRITE_BASE', 'DEPLOYMENT_BASE_PATH', 'SITE', 'SYSTEM_VERSION')));
         $c .= "SERVER: " . print_r($this->Security->filterLoggedParameters($debugServerVars), true) . "\n\n";
         $headers = array();
         foreach ($_SERVER as $name => $value) {
             if (strpos($name, 'HTTP_') === 0) {
                 $headers[$name] = $value;
             }
         }
         if (!empty($headers)) {
             $c .= "HEADERS: " . print_r($this->Security->filterLoggedParameters($headers), true) . "\n\n";
         }
         if (isset($_FILES)) {
             $c .= "FILES: " . print_r($_FILES, true) . "\n\n";
         }
         if (isset($_POST)) {
             $c .= "POST: " . print_r($this->Security->filterLoggedParameters($_POST), true) . "\n\n";
         }
         if (isset($_GET)) {
             $c .= "GET: " . print_r($this->Security->filterLoggedParameters($_GET), true) . "\n\n";
         }
         if (isset($_SESSION)) {
             $c .= "SESSION: " . print_r($this->Security->filterLoggedParameters($_SESSION), true) . "\n\n";
         }
     }
     if ($message instanceof Exception) {
         if (!$subject) {
             $subject = '%k: %.100m';
         }
         $subject = preg_replace_callback('/%(\\.(\\d+))?([kmcfl%])/', function ($match) use($message) {
             switch ($match[3]) {
                 case 'k':
                     $out = get_class($message);
                     break;
                 case 'm':
                     $out = $message->getMessage();
                     break;
                 case 'c':
                     $out = $message->getCode();
                     break;
                 case 'f':
                     $out = $message->getFile();
                     break;
                 case 'l':
                     $out = $message->getLine();
                     break;
                 default:
                     $out = $match[3];
             }
             if (!empty($match[2])) {
                 $out = substr($out, 0, $match[2]);
             }
             return $out;
         }, $subject);
     } else {
         if (!$subject) {
             $subject = substr($message, 0, 100);
         }
     }
     // don't send email about post max or missing boundary errors
     // there's nothing we can do code wise to fix those anyways
     $isPostContentLengthError = false;
     if (false !== strpos($c, 'POST Content-Length of') || false !== strpos($c, 'Missing boundary in multipart/form-data')) {
         $isPostContentLengthError = true;
         $prefix = 'WARN';
     }
     $context = isset($_SERVER['CONTEXT']) ? $_SERVER['CONTEXT'] : 'none';
     if (array_key_exists('SITE', $_SERVER)) {
         $appName = (string) $_SERVER['SITE']['slug'];
         if ($appName === $context) {
             $appName = $this->SiteService->getAnchoredSite()->Slug;
         }
     } else {
         $appName = $_SERVER['SERVER_NAME'];
     }
     $deviceView = isset($_SERVER['DEVICE_VIEW']) ? $_SERVER['DEVICE_VIEW'] : '';
     $design = isset($_SERVER['DESIGN']) ? $_SERVER['DESIGN'] : '';
     if (!empty($deviceView) && !empty($design)) {
         $errorPrefix = "[{$this->environment}][{$appName}][{$context}][{$deviceView}:{$design}][{$prefix}]";
     } else {
         $errorPrefix = "[{$this->environment}][{$appName}][{$context}][{$prefix}]";
     }
     $subject = "{$errorPrefix} {$subject}";
     if (!$this->multiline) {
         $c = str_replace("\n", "<br/>", $c);
     }
     // Do standard PHP error logging
     error_log("{$errorPrefix} {$c}");
     // Bail out if we shouldn't be sending emails.
     if (!$this->sendEmails || empty($this->systemEmailAddress) || empty($this->Email) || $isPostContentLengthError) {
         return;
     }
     $recipients = array($this->systemEmailAddress);
     if (!empty($extraRecipients)) {
         if (!is_array($extraRecipients)) {
             $recipients[] = $extraRecipients;
         } else {
             $recipients = array_merge($recipients, $extraRecipients);
         }
     }
     // emails always need line breaks replaced
     if ($this->multiline) {
         $c = str_replace("\n", "<br/>", $c);
     }
     $body = '<p><font face="&#39;courier new&#39;, monospace">' . $c . '</font></p>';
     $this->Email->clear();
     $this->Email->from($this->sendEmailsFrom);
     foreach ($recipients as $recipient) {
         $this->Email->to($recipient);
     }
     $this->Email->subject($subject);
     $this->Email->body($body);
     $this->Email->altMessage($c);
     if (is_a($this->Email, 'EmailTagInterface')) {
         $this->Email->tag('error');
     }
     $this->Email->send();
 }
 /**
  * Loads a presenter, calls its method and returns the result.
  *
  * @param Template $template
  * @return string
  * @throws Exception
  */
 protected function render(Template $template)
 {
     $presenter = $this->createPresenter($template->getPresenter());
     try {
         $arguments = $this->getArguments($presenter, $template);
         $this->logger->debug($arguments);
         $result = call_user_func_array($presenter, $arguments);
     } catch (Exception $e) {
         if ($this->throwRenderExceptions) {
             throw $e;
         }
         $this->logger->error($e->getMessage() . "\n\nURL: " . URLUtils::fullUrl() . "\n\nTemplate:\n" . print_r($template, true) . (isset($arguments) ? "\n\n{$presenter} args:\n" . print_r($arguments, true) : ''));
         $result = '';
     }
     return $result;
 }
Пример #5
0
 public function processTemplate(Template $template, $contentType, &$globals, $isFinalPass = false, $isDependentPass = false)
 {
     if (!$isFinalPass && $template->isDeferred() || $isDependentPass && !$template->isDependent()) {
         $this->Logger->debug("Deferring template [{$template->getName()}]");
         // defer my execution
         return $template->getMatchString();
     }
     $this->Logger->debug("Processing template [{$template->getName()}]");
     if ($this->benchmarkRendering) {
         $this->Benchmark->start('process-template-' . $template->getName());
     }
     if ($this->noTemplatesCalled == true) {
         $this->topTemplate = $template;
         $this->noTemplatesCalled = false;
         $template->setTopTemplate(true);
     }
     $templateEngine = $this->getTemplateEngine($template->getName());
     try {
         $template = $templateEngine->loadTemplateExtended($template, $globals);
     } catch (Exception $e) {
         if ($this->throwRenderExceptions) {
             throw $e;
         } else {
             $this->Logger->error($e->getMessage() . "\nURL: " . URLUtils::fullUrl());
             return '';
         }
     }
     $cacheThisModule = false;
     $usedCache = false;
     if ($this->cacheEnabled && $template->isCacheable()) {
         $cacheThisModule = true;
     }
     if ($cacheThisModule) {
         $cacheKey = $this->TemplateCache->getTemplateCacheKey($template, $globals);
         if (!in_array($template->getName(), (array) $this->pushCache) && ($cachedTemplate = $this->TemplateCache->get($cacheKey))) {
             // check the timestamp
             //if ($cachedTemplate->getTimestamp() == $template->getTimestamp()) {
             $this->Logger->debug("Loaded from cache [" . $template->getName() . "]");
             $locals = $template->Locals;
             // load from cache
             $template = $templateEngine->loadFromCache($cachedTemplate);
             $template->Locals = $locals;
             $content = $template->getProcessedContent();
             $usedCache = true;
             //}
         }
     }
     if (!$usedCache) {
         $this->Logger->debug("Processing [{$template->getName()}]");
         // process the template and all dependent includes
         $content = $templateEngine->processTemplateContents($template, $globals, $this, $isFinalPass, $isDependentPass);
         $template->setProcessedContent($content);
         if ($cacheThisModule) {
             // snapshot of the cache contents at this moment
             $toBeCachedTemplate = clone $templateEngine->prepareForCache($template);
         }
     }
     // parse all independent includes, outside of caching (recurse!)
     $content = $templateEngine->parseTemplateIncludes($content, $contentType, $template, $globals, $this, $isFinalPass, false);
     if (!$usedCache) {
         $this->Logger->debug("Processing template set globals [{$template->getName()}]...");
         // parse template set globals
         $templateSetGlobals = $templateEngine->processTemplateSetGlobals($template, $globals, $isFinalPass);
     } else {
         $templateSetGlobals = $cachedTemplate->getTemplateSetGlobals();
     }
     $globals = array_merge($globals, $templateSetGlobals);
     // store in cache
     if (!$usedCache && $cacheThisModule) {
         $toBeCachedTemplate->setIndependentTemplates($template->isIndependentTemplates());
         $toBeCachedTemplate->setParsedIndependent(true);
         if ($template->isTopTemplate()) {
             $toBeCachedTemplate->setTemplateSetGlobals($globals);
         } else {
             $toBeCachedTemplate->setTemplateSetGlobals($templateSetGlobals);
         }
         $this->Logger->debug("Storing in cache [" . $toBeCachedTemplate->getName() . "]");
         unset($toBeCachedTemplate->Locals);
         unset($toBeCachedTemplate->Data);
         unset($toBeCachedTemplate->Contents);
         unset($toBeCachedTemplate->SetMatches);
         unset($toBeCachedTemplate->File);
         unset($toBeCachedTemplate->TemplateBlocks);
         $this->TemplateCache->putTemplate($cacheKey, $toBeCachedTemplate);
     }
     if ($this->benchmarkRendering) {
         $this->Benchmark->end('process-template-' . $template->getName());
     }
     return $content;
 }
 protected function callbackAssets($match)
 {
     $assetBlock = $match[0];
     $assetType = $match[1];
     $assetParams = $match[3];
     try {
         if ($assetType == "version") {
             $fname = $this->parseParameterValue(substr($assetParams, 4), $this->tempAssetsFV, $this->tempAssetsFinalParse);
             //removes 'src=/'
             return $this->AssetAggregator->renderVersionedFile($fname);
         } elseif ($assetType == "resolve") {
             $fname = $this->parseParameterValue(substr($assetParams, 4), $this->tempAssetsFV, $this->tempAssetsFinalParse);
             //removes 'src=/'
             return $this->AssetAggregator->renderResolvedFile($fname);
         } elseif ($assetType == "relative") {
             $fname = $this->parseParameterValue(substr($assetParams, 4), $this->tempAssetsFV, $this->tempAssetsFinalParse);
             //removes 'src=/'
             $url = $this->AssetAggregator->renderResolvedFile($fname);
             $pos = strpos($url, '/', 8);
             return substr($url, $pos);
         }
     } catch (Exception $e) {
         if ($this->throwRenderExceptions) {
             throw $e;
         } else {
             $this->Logger->error($e->getMessage() . "\nURL: " . URLUtils::fullUrl());
         }
     }
     return $assetBlock;
     //throw new Exception("ERROR PARSING ASSETS: unrecognized type, allowed types = [css,js,version]");
 }