Exemplo n.º 1
0
 /**
  * @inheritDoc IFieldType::prepValueFromPost()
  *
  * @param mixed $value
  *
  * @return mixed
  */
 public function prepValueFromPost($value)
 {
     // Temporary fix (hopefully) for a Redactor bug where some HTML will get submitted when the field is blank,
     // if any text was typed into the field, and then deleted
     if ($value == '<p><br></p>') {
         $value = '';
     }
     if ($value) {
         // Swap any pagebreak <hr>'s with <!--pagebreak-->'s
         $value = preg_replace('/<hr class="redactor_pagebreak".*?>/', '<!--pagebreak-->', $value);
         if ($this->getSettings()->purifyHtml) {
             $purifier = new \CHtmlPurifier();
             $purifier->setOptions(array('Attr.AllowedFrameTargets' => array('_blank'), 'HTML.AllowedComments' => array('pagebreak')));
             $value = $purifier->purify($value);
         }
         if ($this->getSettings()->cleanupHtml) {
             // Remove <span> and <font> tags
             $value = preg_replace('/<(?:span|font)\\b[^>]*>/', '', $value);
             $value = preg_replace('/<\\/(?:span|font)>/', '', $value);
             // Remove inline styles
             $value = preg_replace('/(<(?:h1|h2|h3|h4|h5|h6|p|div|blockquote|pre|strong|em|b|i|u|a)\\b[^>]*)\\s+style="[^"]*"/', '$1', $value);
             // Remove empty tags
             $value = preg_replace('/<(h1|h2|h3|h4|h5|h6|p|div|blockquote|pre|strong|em|a|b|i|u)\\s*><\\/\\1>/', '', $value);
         }
     }
     // Find any element URLs and swap them with ref tags
     $value = preg_replace_callback('/(href=|src=)([\'"])[^\'"#]+?(#[^\'"#]+)?(?:#|%23)(\\w+):(\\d+)(:' . HandleValidator::$handlePattern . ')?\\2/', function ($matches) {
         return $matches[1] . $matches[2] . '{' . $matches[4] . ':' . $matches[5] . (!empty($matches[6]) ? $matches[6] : ':url') . '}' . (!empty($matches[3]) ? $matches[3] : '') . $matches[2];
     }, $value);
     // Encode any 4-byte UTF-8 characters.
     $value = StringHelper::encodeMb4($value);
     return $value;
 }
 /**
  * Ends a template cache.
  *
  * @param string      $key        The template cache key.
  * @param bool        $global     Whether the cache should be stored globally.
  * @param string|null $duration   How long the cache should be stored for.
  * @param mixed|null  $expiration When the cache should expire.
  * @param string      $body       The contents of the cache.
  *
  * @throws \Exception
  * @return null
  */
 public function endTemplateCache($key, $global, $duration, $expiration, $body)
 {
     // Make sure template caching is enabled.
     if (!$this->_isTemplateCachingEnabled()) {
         return;
     }
     // If there are any transform generation URLs in the body, don't cache it.
     // stripslashes($body) in case the URL has been JS-encoded or something.
     // Can't use getResourceUrl() here because that will append ?d= or ?x= to the URL.
     if (strpos(stripslashes($body), UrlHelper::getSiteUrl(craft()->config->getResourceTrigger() . '/transforms'))) {
         return;
     }
     // Encode any 4-byte UTF-8 characters
     $body = StringHelper::encodeMb4($body);
     // Figure out the expiration date
     if ($duration) {
         $expiration = new DateTime($duration);
     }
     if (!$expiration) {
         $duration = craft()->config->getCacheDuration();
         if ($duration <= 0) {
             $duration = 31536000;
             // 1 year
         }
         $duration += time();
         $expiration = new DateTime('@' . $duration);
     }
     // Save it
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         craft()->db->createCommand()->insert(static::$_templateCachesTable, array('cacheKey' => $key, 'locale' => craft()->language, 'path' => $global ? null : $this->_getPath(), 'expiryDate' => DateTimeHelper::formatTimeForDb($expiration), 'body' => $body), false);
         $cacheId = craft()->db->getLastInsertID();
         // Tag it with any element criteria that were output within the cache
         if (!empty($this->_cacheCriteria[$key])) {
             $values = array();
             foreach ($this->_cacheCriteria[$key] as $criteria) {
                 $flattenedCriteria = $criteria->getAttributes(null, true);
                 $values[] = array($cacheId, $criteria->getElementType()->getClassHandle(), JsonHelper::encode($flattenedCriteria));
             }
             craft()->db->createCommand()->insertAll(static::$_templateCacheCriteriaTable, array('cacheId', 'type', 'criteria'), $values, false);
             unset($this->_cacheCriteria[$key]);
         }
         // Tag it with any element IDs that were output within the cache
         if (!empty($this->_cacheElementIds[$key])) {
             $values = array();
             foreach ($this->_cacheElementIds[$key] as $elementId) {
                 $values[] = array($cacheId, $elementId);
             }
             craft()->db->createCommand()->insertAll(static::$_templateCacheElementsTable, array('cacheId', 'elementId'), $values, false);
             unset($this->_cacheElementIds[$key]);
         }
         if ($transaction !== null) {
             $transaction->commit();
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
 }