Пример #1
0
 /**
  * Renders a template, and either outputs or returns it.
  *
  * @param mixed $template      The name of the template to load, or a StringTemplate object.
  * @param array $variables     The variables that should be available to the template
  * @param bool  $return        Whether to return the results, rather than output them
  * @param bool  $processOutput
  * @throws HttpException
  * @return mixed
  */
 public function renderTemplate($template, $variables = array(), $return = false, $processOutput = false)
 {
     if (($output = craft()->templates->render($template, $variables)) !== false) {
         if ($processOutput) {
             $output = $this->processOutput($output);
         }
         if ($return) {
             return $output;
         } else {
             // Get the template file's MIME type
             $templateFile = craft()->templates->findTemplate($template);
             $extension = IOHelper::getExtension($templateFile, 'html');
             $mimeType = IOHelper::getMimeTypeByExtension('.' . $extension);
             if (!$mimeType) {
                 $mimeType = 'text/html';
             }
             header('Content-Type: ' . $mimeType . '; charset=utf-8');
             if ($mimeType == 'text/html') {
                 // Are there any head/foot nodes left in the queue?
                 $headHtml = craft()->templates->getHeadHtml();
                 $footHtml = craft()->templates->getFootHtml();
                 if ($headHtml) {
                     if (($endHeadPos = stripos($output, '</head>')) !== false) {
                         $output = substr($output, 0, $endHeadPos) . $headHtml . substr($output, $endHeadPos);
                     } else {
                         $output .= $headHtml;
                     }
                 }
                 if ($footHtml) {
                     if (($endBodyPos = stripos($output, '</body>')) !== false) {
                         $output = substr($output, 0, $endBodyPos) . $footHtml . substr($output, $endBodyPos);
                     } else {
                         $output .= $footHtml;
                     }
                 }
             }
             // Output to the browser!
             echo $output;
             // End the request
             craft()->end();
         }
     } else {
         throw new HttpException(404);
     }
 }
 /**
  * Sends a resource back to the browser.
  *
  * @param string $path
  *
  * @throws HttpException
  * @return null
  */
 public function sendResource($path)
 {
     if (PathHelper::ensurePathIsContained($path) === false) {
         throw new HttpException(404);
     }
     $cachedPath = $this->getCachedResourcePath($path);
     if ($cachedPath) {
         if ($cachedPath == ':(') {
             // 404
             $realPath = false;
         } else {
             // We've got it already
             $realPath = $cachedPath;
         }
     } else {
         // We don't have a cache of the file system path, so let's get it
         $realPath = $this->getResourcePath($path);
         // Now cache it
         $this->cacheResourcePath($path, $realPath);
     }
     if ($realPath === false || !IOHelper::fileExists($realPath)) {
         throw new HttpException(404);
     }
     // If there is a timestamp and HTTP_IF_MODIFIED_SINCE exists, check the timestamp against requested file's last
     // modified date. If the last modified date is less than the timestamp, return a 304 not modified and let the
     // browser serve it from cache.
     $timestamp = craft()->request->getParam($this->dateParam, null);
     if ($timestamp !== null && array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
         $requestDate = DateTime::createFromFormat('U', $timestamp);
         $lastModifiedFileDate = IOHelper::getLastTimeModified($realPath);
         if ($lastModifiedFileDate && $lastModifiedFileDate <= $requestDate) {
             // Let the browser serve it from cache.
             HeaderHelper::setHeader('HTTP/1.1 304 Not Modified');
             craft()->end();
         }
     }
     // Note that $content may be empty -- they could be requesting a blank text file or something. It doens't matter.
     // No need to throw a 404.
     $content = IOHelper::getFileContents($realPath);
     // Normalize URLs in CSS files
     $mimeType = IOHelper::getMimeTypeByExtension($realPath);
     if (mb_strpos($mimeType, 'css') !== false) {
         $content = preg_replace_callback('/(url\\(([\'"]?))(.+?)(\\2\\))/', array(&$this, '_normalizeCssUrl'), $content);
     }
     if (!craft()->config->get('useXSendFile')) {
         $options['forceDownload'] = false;
         if (craft()->request->getQuery($this->dateParam)) {
             $options['cache'] = true;
         }
         craft()->request->sendFile($realPath, $content, $options);
     } else {
         craft()->request->xSendFile($realPath);
     }
     // You shall not pass.
     craft()->end();
 }
 /**
  * @return mixed
  */
 public function getMimeType()
 {
     if (!$this->_mimeType) {
         $extension = IOHelper::getExtension($this->getPath(), 'html');
         $this->_mimeType = IOHelper::getMimeTypeByExtension('.' . $extension);
     }
     return $this->_mimeType;
 }