Ejemplo n.º 1
0
/**
 * Returns truncated html formatted content
 *
 * @param string $articlecontent the source string
 * @param int $shorten new size
 * @param string $shortenindicator
 * @param bool $forceindicator set to true to include the indicator no matter what
 * @return string
 */
function shortenContent($articlecontent, $shorten, $shortenindicator, $forceindicator = false)
{
    global $_user_tags;
    if ($shorten && ($forceindicator || mb_strlen($articlecontent) > $shorten)) {
        $allowed_tags = getAllowedTags('allowed_tags');
        //remove script to be replaced later
        $articlecontent = preg_replace('~<script.*?/script>~is', '', $articlecontent);
        //remove HTML comments
        $articlecontent = preg_replace('~<!--.*?-->~is', '', $articlecontent);
        $short = mb_substr($articlecontent, 0, $shorten);
        $short2 = kses($short . '</p>', $allowed_tags);
        if (($l2 = mb_strlen($short2)) < $shorten) {
            $c = 0;
            $l1 = $shorten;
            $delta = $shorten - $l2;
            while ($l2 < $shorten && $c++ < 5) {
                $open = mb_strrpos($short, '<');
                if ($open > mb_strrpos($short, '>')) {
                    $l1 = mb_strpos($articlecontent, '>', $l1 + 1) + $delta;
                } else {
                    $l1 = $l1 + $delta;
                }
                $short = mb_substr($articlecontent, 0, $l1);
                preg_match_all('/(<p>)/', $short, $open);
                preg_match_all('/(<\\/p>)/', $short, $close);
                if (count($open) > count($close)) {
                    $short .= '</p>';
                }
                $short2 = kses($short, $allowed_tags);
                $l2 = mb_strlen($short2);
            }
            $shorten = $l1;
        }
        $short = truncate_string($articlecontent, $shorten, '');
        if ($short != $articlecontent) {
            //	we actually did remove some stuff
            // drop open tag strings
            $open = mb_strrpos($short, '<');
            if ($open > mb_strrpos($short, '>')) {
                $short = mb_substr($short, 0, $open);
            }
            if (class_exists('tidy')) {
                $short = zpFunctions::tidyHTML($short . $shortenindicator);
            } else {
                $short = trim(cleanHTML($short . $shortenindicator));
            }
        }
        $articlecontent = $short;
    }
    if (isset($matches)) {
        //replace the script text
        foreach ($matches[0] as $script) {
            $articlecontent = $script . $articlecontent;
        }
    }
    return $articlecontent;
}
Ejemplo n.º 2
0
/**
 * HTML encodes the non-metatag part of the string.
 *
 * @param string $original string to be encoded
 * @param bool $allowScript set to false to prevent pass-through of script tags.
 * @return string
 */
function html_encodeTagged($original, $allowScript = true)
{
    $tags = array();
    $str = $original;
    //javascript
    if ($allowScript) {
        preg_match_all('!<script.*>.*</script>!ixs', $str, $matches);
        foreach (array_unique($matches[0]) as $key => $tag) {
            $tags[2]['%' . $key . '$j'] = $tag;
            $str = str_replace($tag, '%' . $key . '$j', $str);
        }
    } else {
        $str = preg_replace('|<a(.*)href(.*)=(.*)javascript|ixs', '%$x', $str);
        $tags[2]['%$x'] = '&lt;a href=<strike>javascript</strike>';
        $str = preg_replace('|<(.*)onclick|ixs', '%$c', $str);
        $tags[2]['%$c'] = '&lt;<strike>onclick</strike>';
    }
    //strip html comments
    $str = preg_replace('~<!--.*?-->~is', '', $str);
    // markup
    preg_match_all("/<\\/?\\w+((\\s+(\\w|\\w[\\w-]*\\w)(\\s*=\\s*(?:\".*?\"|'.*?'|[^'\">\\s]+))?)+\\s*|\\s*)\\/?>/i", $str, $matches);
    foreach (array_unique($matches[0]) as $key => $tag) {
        $tags[2]['%' . $key . '$s'] = $tag;
        $str = str_replace($tag, '%' . $key . '$s', $str);
    }
    //entities
    preg_match_all('/(&[a-z0-9#]+;)/i', $str, $matches);
    foreach (array_unique($matches[0]) as $key => $entity) {
        $tags[3]['%' . $key . '$e'] = $entity;
        $str = str_replace($entity, '%' . $key . '$e', $str);
    }
    $str = htmlspecialchars($str, ENT_FLAGS, LOCAL_CHARSET);
    foreach (array_reverse($tags, true) as $taglist) {
        $str = strtr($str, $taglist);
    }
    if ($str != $original) {
        $str = zpFunctions::tidyHTML($str);
    }
    return $str;
}