Esempio n. 1
0
 /**
  * @param $view
  * @param $data
  *
  * @return mixed
  */
 protected function render($view, $data)
 {
     if (!craft()->isConsole() && !craft()->request->isResourceRequest() && !craft()->request->isAjaxRequest() && craft()->config->get('devMode') && in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml'))) {
         $viewFile = craft()->path->getCpTemplatesPath() . 'logging/' . $view . '-firebug.php';
         include craft()->findLocalizedFile($viewFile, 'en');
     }
 }
 /**
  * This method will JSON encode a variable. We're overriding Twig's default implementation to set some stricter
  * encoding options on text/html/xml requests.
  *
  * @param mixed    $value   The value to JSON encode.
  * @param null|int $options Either null or a bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP,
  *                          JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES,
  *                          JSON_FORCE_OBJECT
  *
  * @return mixed The JSON encoded value.
  */
 public function jsonEncodeFilter($value, $options = null)
 {
     if ($options === null && in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml'))) {
         $options = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT;
     }
     return twig_jsonencode_filter($value, $options);
 }
Esempio n. 3
0
 /**
  * @param $view
  * @param $data
  *
  * @return mixed
  */
 protected function render($view, $data)
 {
     if (!craft()->isConsole() && !craft()->request->isResourceRequest() && !craft()->request->isAjaxRequest() && craft()->config->get('devMode') && in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml'))) {
         if (($userAgent = craft()->request->getUserAgent()) !== null && preg_match('/msie [5-9]/i', $userAgent)) {
             echo '<script type="text/javascript">';
             echo IOHelper::getFileContents(IOHelper::getFolderName(__FILE__) . '/../vendors/console-normalizer/normalizeconsole.min.js');
             echo "</script>\n";
         } else {
             $viewFile = craft()->path->getCpTemplatesPath() . 'logging/' . $view . '-firebug.php';
             include craft()->findLocalizedFile($viewFile, 'en');
         }
     }
 }
    /**
     * Figure out how to initiate a new task runner.
     */
    public function handleRequestEnd()
    {
        // Make sure a future call to craft()->end() dosen't trigger this a second time
        craft()->detachEventHandler('onEndRequest', array($this, '_onEndRequest'));
        // Make sure nothing has been output to the browser yet, and there's no pending response body
        if (!headers_sent() && !ob_get_length()) {
            $this->closeAndRun();
        } else {
            if (craft()->request->isSiteRequest() && in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml')) && !craft()->request->isAjaxRequest()) {
                // Just output JS that tells the browser to fire an Ajax request to kick off task running
                $url = JsonHelper::encode(UrlHelper::getActionUrl('tasks/runPendingTasks'));
                // Ajax request code adapted from http://www.quirksmode.org/js/xmlhttp.html - thanks ppk!
                echo <<<EOT
<script type="text/javascript">
/*<![CDATA[*/
(function(){
\tvar XMLHttpFactories = [
\t\tfunction () {return new XMLHttpRequest()},
\t\tfunction () {return new ActiveXObject("Msxml2.XMLHTTP")},
\t\tfunction () {return new ActiveXObject("Msxml3.XMLHTTP")},
\t\tfunction () {return new ActiveXObject("Microsoft.XMLHTTP")}
\t];
\tvar req = false;
\tfor (var i = 0; i < XMLHttpFactories.length; i++) {
\t\ttry {
\t\t\treq = XMLHttpFactories[i]();
\t\t}
\t\tcatch (e) {
\t\t\tcontinue;
\t\t}
\t\tbreak;
\t}
\tif (!req) return;
\treq.open('GET', {$url}, true);
\tif (req.readyState == 4) return;
\treq.send();
})();
/*]]>*/
</script>
EOT;
            }
        }
    }
 /**
  * Returns the MIME type that is going to be included in the response via the Content-Type header.
  *
  * @return string
  * @deprecated Deprecated in 2.2. Use {@link HeaderHelper::getMimeType()} instead.
  */
 public function getMimeType()
 {
     // TODO: Call the deprecator here in Craft 3.0
     return HeaderHelper::getMimeType();
 }
 /**
  * Renders a template, and either outputs or returns it.
  *
  * @param mixed $template      The name of the template to load in a format supported by
  *                             {@link TemplatesService::findTemplate()}, or a {@link 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. (Default is `false`.)
  * @param bool  $processOutput Whether the output should be processed by {@link processOutput()}.
  *
  * @throws HttpException
  * @return mixed The rendered template if $return is set to `true`.
  */
 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 {
             // Set the MIME type for the request based on the matched template's file extension (unless the
             // Content-Type header was already set, perhaps by the template via the {% header %} tag)
             if (!HeaderHelper::isHeaderSet('Content-Type')) {
                 // Safe to assume that findTemplate() will return an actual template path here, and not `false`.
                 // If the template didn't exist, a TemplateLoaderException would have been thrown when calling
                 // craft()->templates->render().
                 $templateFile = craft()->templates->findTemplate($template);
                 $extension = IOHelper::getExtension($templateFile, 'html');
                 if ($extension == 'twig') {
                     $extension = 'html';
                 }
                 HeaderHelper::setContentTypeByExtension($extension);
             }
             // Set the charset header
             HeaderHelper::setHeader(array('charset' => 'utf-8'));
             // Are we serving HTML or XHTML?
             if (in_array(HeaderHelper::getMimeType(), array('text/html', 'application/xhtml+xml'))) {
                 // Are there any head/foot nodes left in the queue?
                 $headHtml = craft()->templates->getHeadHtml();
                 $footHtml = craft()->templates->getFootHtml();
                 if ($headHtml) {
                     if (($endHeadPos = mb_stripos($output, '</head>')) !== false) {
                         $output = mb_substr($output, 0, $endHeadPos) . $headHtml . mb_substr($output, $endHeadPos);
                     } else {
                         $output .= $headHtml;
                     }
                 }
                 if ($footHtml) {
                     if (($endBodyPos = mb_stripos($output, '</body>')) !== false) {
                         $output = mb_substr($output, 0, $endBodyPos) . $footHtml . mb_substr($output, $endBodyPos);
                     } else {
                         $output .= $footHtml;
                     }
                 }
             }
             // Output it into a buffer, in case TasksService wants to close the connection prematurely
             ob_start();
             echo $output;
             // End the request
             craft()->end();
         }
     } else {
         throw new HttpException(404);
     }
 }