isAllowedTag($tag) && ! $this->isPixelTracker($tag, $attributes); } /** * Return the HTML opening tag * * @access public * @param string $tag Tag name * @param string $attributes Attributes converted in html * @return string */ public function openHtmlTag($tag, $attributes = '') { return '<'.$tag.(empty($attributes) ? '' : ' '.$attributes).($this->isSelfClosingTag($tag) ? '/>' : '>'); } /** * Return the HTML closing tag * * @access public * @param string $tag Tag name * @return string */ public function closeHtmlTag($tag) { return $this->isSelfClosingTag($tag) ? '' : ''; } /** * Return true is the tag is self-closing * * @access public * @param string $tag Tag name * @return boolean */ public function isSelfClosingTag($tag) { return $tag === 'br' || $tag === 'img'; } /** * Check if a tag is on the whitelist * * @access public * @param string $tag Tag name * @return boolean */ public function isAllowedTag($tag) { return in_array($tag, $this->tag_whitelist); } /** * Detect if an image tag is a pixel tracker * * @access public * @param string $tag Tag name * @param array $attributes Tag attributes * @return boolean */ public function isPixelTracker($tag, array $attributes) { return $tag === 'img' && isset($attributes['height']) && isset($attributes['width']) && $attributes['height'] == 1 && $attributes['width'] == 1; } /** * Remove script tags * * @access public * @param string $data Input data * @return string */ public function removeBlacklistedTags($data) { $dom = XmlParser::getDomDocument($data); if ($dom === false) { return ''; } $xpath = new DOMXpath($dom); $nodes = $xpath->query(implode(' | ', $this->tag_blacklist)); foreach ($nodes as $node) { $node->parentNode->removeChild($node); } return $dom->saveXML(); } /** * Remove empty tags * * @access public * @param string $data Input data * @return string */ public function removeEmptyTags($data) { return preg_replace('/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU', '', $data); } /** * Replace

by only one * * @access public * @param string $data Input data * @return string */ public function removeMultipleBreakTags($data) { return preg_replace("/(\s*)+/", "
", $data); } /** * Set whitelisted tags adn attributes for each tag * * @access public * @param array $values List of tags: ['video' => ['src', 'cover'], 'img' => ['src']] * @return Tag */ public function setWhitelistedTags(array $values) { $this->tag_whitelist = $values ?: $this->tag_whitelist; return $this; } }