Wraps a set of HTML nodes in an HTML5-document

@param  string $nodes   One or more HTML-nodes, as a string
@param  string $charset Charset to specify in meta tag
@return string
18558
protected function wrapInHtml5Document($nodes, $charset = null) {
        $charset = strtolower($charset ?: Validator::CHARSET_UTF_8);

        $document  = '<!DOCTYPE html>' . PHP_EOL;
        $document .= '<html><head>' . PHP_EOL;
        $document .= '<meta charset="' . $charset . '">' . PHP_EOL;
        $document .= '<title>Validation document</title>' . PHP_EOL;
        $document .= '</head><body>' . $nodes . '</body></html>';

        return $document;
    }
1088460
private function _getCharset($charset=null)
    {
        if (! $charset) {
            return;
        }

        $expr   = '/^(charset=)(\w+)$/';
        $tokens = array_filter(
            $charset, function ($token) use ($expr) {
                return preg_match($expr, $token, $matches);
            }
        );
        if (count($tokens) > 0) {
            preg_match($expr, array_shift($tokens), $matches);
            $_charset = $matches[2];
        } else {
            $_charset = null;
        }

        return $_charset;
    }
1067812
function parse_charset($charset) {
		$charset = trim(strtolower($charset));
		if (isset($this->synonyms[$charset])) {
			$charset = $this->synonyms[$charset];
		}

		return $charset;
	}
1312175
private function notifyHeadersOfCharset($charset)
    {
        foreach ($this->headers as $headerGroup) {
            foreach ($headerGroup as $header) {
                $header->setCharset($charset);
            }
        }
    }
559125
public function setBodyHtml($html, $charset = null, $encoding = \Zend_Mime::ENCODING_QUOTEDPRINTABLE)
    {
        $this->setType(\Zend_Mime::MULTIPART_RELATED);

        $dom = new \DOMDocument(null, $this->getCharset());
        @$dom->loadHTML($html);

        $images = $dom->getElementsByTagName('img');
        for ($i = 0; $i < $images->length; $i++) {
            $img = $images->item($i);
            $url = $img->getAttribute('src');

            try {
                $client = new \Zend_Http_Client($url);
                $response = $client->request();

                if ($response->getStatus() === 200) {
                    $imageContent = $response->getBody();

                    $pathinfo = pathinfo($url);
                    $mimeType = $response->getHeader('Content-Type');

                    $cid = md5($pathinfo['filename']);
                    $html = str_replace($url, 'cid:' . $cid, $html);

                    $mime = new \Zend_Mime_Part($imageContent);
                    $mime->id = $cid;
                    $mime->type = $mimeType;
                    $mime->disposition = \Zend_Mime::DISPOSITION_INLINE;
                    $mime->encoding = \Zend_Mime::ENCODING_BASE64;
                    $mime->filename = $pathinfo['basename'];

                    $this->addAttachment($mime);
                }
            } catch (\Exception $exc) {
                //Error on client connect - bad url?
            }
        }

        return parent::setBodyHtml($html, $charset, $encoding);
    }
977780
protected function wrapInHtml4Document($nodes, $charset = null, $parser = null) {
        if ($parser === Validator::PARSER_HTML4TR) {
            $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
        } else {
            $doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
        }

        $charset = strtolower($charset ?: Validator::CHARSET_UTF_8);

        $document  = $doctype . PHP_EOL;
        $document .= '<html><head>' . PHP_EOL;
        $document .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '">' . PHP_EOL;
        $document .= '<title>Validation document</title>' . PHP_EOL;
        $document .= '</head><body>' . $nodes . '</body></html>';

        return $document;
    }
533288
public function wash($html)
    {
        // Charset seems to be ignored (probably if defined in the HTML document)
        $node = new DOMDocument('1.0', $this->config['charset']);
        $this->extlinks = false;

        $html = $this->cleanup($html);

        // Find base URL for images
        if (preg_match('/<base\s+href=[\'"]*([^\'"]+)/is', $html, $matches)) {
            $this->config['base_url'] = $matches[1];
        }
        else {
            $this->config['base_url'] = '';
        }

        // Detect max nesting level (for dumpHTML) (#1489110)
        $this->max_nesting_level = (int) @ini_get('xdebug.max_nesting_level');

        // SVG need to be parsed as XML
        $this->is_xml = stripos($html, '<html') === false && stripos($html, '<svg') !== false;
        $method       = $this->is_xml ? 'loadXML' : 'loadHTML';
        $options      = 0;

        // Use optimizations if supported
        if (PHP_VERSION_ID >= 50400) {
            $options = LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NONET;
            @$node->{$method}($html, $options);
        }
        else {
            @$node->{$method}($html);
        }

        return $this->dumpHtml($node);
    }
869058
protected static function htmlentitiesApply($str, $encodeTags, $isXml, $charsets)
    {
        self::getCfg(); // sets htmlentitiesmore
        $return = '';
        if (!$encodeTags) {
            // $debug->log('don\'t encode tags');
            $return = self::htmlentitiesSansTags($str, $charsets);
        }
        // now entityfy final (or only) segment
        if (!in_array(trim($str), array('', '0'), true)) {
            foreach ($charsets as $charset) {
                // change: was call_user_func_array
                $strEntityfied = @htmlentities($str, ENT_COMPAT, $charset);
                if (!empty($strEntityfied)) {
                    #$debug->log('success with '.$charset);
                    break;
                }
            }
            if (empty($strEntityfied)) {
                // $debug->warn('unable to convert');
                $strEntityfied = $str;
            }
            $str = preg_replace('/&amp;(#\d+;|[a-zA-Z]+;)/', '&\\1', $strEntityfied);
            $str = str_replace(
                array_keys(self::$htmlentitiesMore),
                array_values(self::$htmlentitiesMore),
                $str
            );
            $return .= $str;
        }
        if ($isXml) {
            if (empty(self::$xmlentitiesHtml)) {
                self::buildEntities();
            }
            $return = str_replace(self::$xmlentitiesHtml, self::$xmlentitiesUtf8, $return);
        }
        return $return;
    }
393502
public function charsetChanged($charset)
    {
        $this->charset = $charset;
        $this->encoder->charsetChanged($charset);
        $this->paramEncoder->charsetChanged($charset);
    }
255105
public function setCharset($charset)
    {
        $this->charset = $charset;
        if (!$this->headerCharset) {
            $this->headerCharset = $charset;
        }

        return $this;
    }
83492
private static function _cleanHTML(DOMNode $Node, array $filters){

		$node_contents = '';
		$tag_name = '';

		if(isset($Node->tagName)){

			$tag_name = strtolower(trim($Node->tagName));
		}

		// Drop tags

		assert('isset($filters[\'tags_drop\']) && is_array($filters[\'tags_drop\'])');
		if(!isset($filters['tags_drop']) || !is_array($filters['tags_drop'])){

			$filters['tags_drop'] = [];
		}

		if($Node instanceof DOMComment || in_array($tag_name, $filters['tags_drop'])){

			return '';
		}

		// Clean node contents

		if(!isset($Node->childNodes) || $Node->childNodes->length == 0){

			// As no charset conversion is needed, nodeValue can be directly used

			$node_contents = html_entity_decode($Node->nodeValue, ENT_QUOTES, DEFAULT_CHARSET);

			// Reduce every possible combination of whitespaces to a single space-character

			$node_contents = preg_replace('/\s+/', ' ', $node_contents);

			// Reapply HTML entities

			$node_contents = htmlentities($node_contents, ENT_QUOTES, DEFAULT_CHARSET);
		}
		else{

			foreach($Node->childNodes as $Child){

				$node_contents .= self::_cleanHTML($Child, $filters);
			}
		}

		// Nameless and disallowed nodes (return content only, no tags)

		assert('isset($filters[\'tags\']) && is_array($filters[\'tags\'])');
		if(!isset($filters['tags']) || !is_array($filters['tags'])){

			$filters['tags'] = [];
		}

		if($tag_name == '' || !in_array($tag_name, $filters['tags'])){

			return $node_contents;
		}

		// Allowed nodes (return tag and content)

		else{

			$node_attributes = self::_cleanAttributes($Node, $filters);

			// If (after filtering) "a" or "img" elements have no attributes, discard them

			if($node_attributes == '' && ($tag_name == 'a' || $tag_name == 'img')){

				return $node_contents;
			}

			switch($tag_name){

				// Empty elements (c.q. self-closing)

				case 'br':
				case 'hr':
				case 'input':
				case 'img':

					return "<$tag_name$node_attributes>";

				break;

				// Elements with content

				default:

					// Empty "a" elements (c.q. page anchors) are allowed

					if(trim($node_contents) == '' && $tag_name != 'a') return '';

					return "<$tag_name$node_attributes>$node_contents</$tag_name>";
			}
		}
	}
552753
public function setCharset($charset = null)
    {
        if (! is_null($charset)) {
            $this->charset = $charset;
        } elseif (defined('THEMOSIS_CHARSET')) {
            $this->charset = THEMOSIS_CHARSET;
        } elseif (function_exists('get_bloginfo')) {
            $this->charset = get_bloginfo('charset');
        }

        return $this;
    }
108194
public static function mapFrom( $charset )
    {
        $lowerCharset = strtolower( $charset );
        return isset( self::$reverseMapping[$lowerCharset] ) ? self::$reverseMapping[$lowerCharset] : $charset;
    }
399078
public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE)
    {
        if ($charset === null) {
            $charset = $this->_charset;
        }

        $mp = new Zend_Mime_Part($html);
        $mp->encoding = $encoding;
        $mp->type = Zend_Mime::TYPE_HTML;
        $mp->disposition = Zend_Mime::DISPOSITION_INLINE;
        $mp->charset = $charset;

        $this->_bodyHtml = $mp;

        return $this;
    }
654694
public function setCharset(string $charset): ViewFactory
	{
		$this->globalVariables['__charset__'] = $this->charset = $charset;

		return $this;
	}
854026
public function charset($charset = null)
    {
        if (!func_num_args()) {
            list($mime, $charset) = $this->_inspectContentType();
            return $this->_charset = $charset;
        }
        $this->_charset = $charset ? strtoupper($charset) : null;
        $this->_updateContentType();
        return $this;
    }
1340240
public function setCharset($charset)
    {
        $this->charset = $charset;
        $this->factory->charsetChanged($charset);
        $this->notifyHeadersOfCharset($charset);
    }