Ignore:
Timestamp:
Dec 27, 2022, 7:50:23 PM (17 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HTML/BBCodeParser2.php

    r887 r888  
    8484     * @var      array
    8585     */
    86     var $_definedTags  = array();
     86    private array $_definedTags  = array();
    8787
    8888    /**
     
    9292     * @var      string
    9393     */
    94     var $_text          = '';
     94    public string $_text = '';
    9595
    9696    /**
     
    100100     * @var      string
    101101     */
    102     var $_preparsed    = '';
     102    private string $_preparsed = '';
    103103
    104104    /**
     
    108108     * @var      array
    109109     */
    110     var $_tagArray      = array();
     110    private array $_tagArray = array();
    111111
    112112    /**
     
    116116     * @var      string
    117117     */
    118     var $_parsed        = '';
     118   private string $_parsed = '';
    119119
    120120    /**
     
    124124     * @var      array
    125125     */
    126     var $_options = array(
     126    public array $_options = array(
    127127        'quotestyle'    => 'double',
    128128        'quotewhat'     => 'all',
     
    139139     * @var      array
    140140     */
    141     var $_filters      = array();
     141    private array $_filters = array();
    142142
    143143    /**
     
    155155     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    156156     */
    157     function __construct($options = array())
     157    function __construct(array $options = array())
    158158    {
    159159        // set the options passed as an argument
    160         foreach ($options as $k => $v )  {
     160        foreach ($options as $k => $v )
     161        {
    161162            $this->_options[$k] = $v;
    162163        }
     
    166167        if ($this->_options['open'] != '' && strpos($preg_escape, $this->_options['open'])) {
    167168            $this->_options['open_esc'] = "\\".$this->_options['open'];
    168         } else {
     169        } else
     170        {
    169171            $this->_options['open_esc'] = $this->_options['open'];
    170172        }
    171173        if ($this->_options['close'] != '' && strpos($preg_escape, $this->_options['close'])) {
    172174            $this->_options['close_esc'] = "\\".$this->_options['close'];
    173         } else {
     175        } else
     176        {
    174177            $this->_options['close_esc'] = $this->_options['close'];
    175178        }
     
    180183
    181184        // return if this is a subclass
    182         if (is_subclass_of($this, 'HTML_BBCodeParser2_Filter')) {
     185        if (is_subclass_of($this, 'HTML_BBCodeParser2_Filter'))
     186        {
    183187            return;
    184188        }
     
    195199     * @author Lorenzo Alberton <l.alberton@quipo.it>
    196200     */
    197     function setOption($name, $value)
     201    function setOption(string $name, $value): void
    198202    {
    199203        $this->_options[$name] = $value;
     
    206210     * @author Lorenzo Alberton <l.alberton@quipo.it>
    207211     */
    208     function addFilter($filter)
     212    function addFilter(string $filter)
    209213    {
    210214        $filter = ucfirst($filter);
    211         if (!array_key_exists($filter, $this->_filters)) {
     215        if (!array_key_exists($filter, $this->_filters))
     216        {
    212217            $class = 'HTML_BBCodeParser2_Filter_'.$filter;
    213218            @include_once 'HTML/BBCodeParser2/Filter/'.$filter.'.php';
    214             if (!class_exists($class)) {
     219            if (!class_exists($class))
     220            {
    215221                throw new InvalidArgumentException("Failed to load filter $filter");
    216222            }
     
    229235     * @author Lorenzo Alberton <l.alberton@quipo.it>
    230236     */
    231     function removeFilter($filter)
     237    function removeFilter($filter): void
    232238    {
    233239        $filter = ucfirst(trim($filter));
    234         if (!empty($filter) && array_key_exists($filter, $this->_filters)) {
     240        if (!empty($filter) && array_key_exists($filter, $this->_filters))
     241        {
    235242            unset($this->_filters[$filter]);
    236243        }
     
    238245        // preserving the others
    239246        $this->_definedTags = array();
    240         foreach (array_keys($this->_filters) as $filter) {
     247        foreach (array_keys($this->_filters) as $filter)
     248        {
    241249            $this->_definedTags = array_merge(
    242250                $this->_definedTags,
     
    253261     * @author Lorenzo Alberton <l.alberton@quipo.it>
    254262     */
    255     function addFilters($filters)
    256     {
    257         if (is_string($filters)) {
     263    function addFilters($filters): bool
     264    {
     265        if (is_string($filters))
     266        {
    258267            //comma-separated list
    259             if (strpos($filters, ',') !== false) {
     268            if (strpos($filters, ',') !== false)
     269            {
    260270                $filters = explode(',', $filters);
    261             } else {
     271            } else
     272            {
    262273                $filters = array($filters);
    263274            }
    264275        }
    265         if (!is_array($filters)) {
     276        if (!is_array($filters))
     277        {
    266278            //invalid format
    267279            return false;
    268280        }
    269         foreach ($filters as $filter) {
    270             if (trim($filter)){
     281        foreach ($filters as $filter)
     282        {
     283            if (trim($filter))
     284            {
    271285                $this->addFilter($filter);
    272286            }
     
    291305     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    292306     */
    293     function _preparse()
     307    function _preparse(): void
    294308    {
    295309        // default: assign _text to _preparsed, to be overwritten by filters
     
    297311
    298312        // return if this is a subclass
    299         if (is_subclass_of($this, 'HTML_BBCodeParser2')) {
     313        if (is_subclass_of($this, 'HTML_BBCodeParser2'))
     314        {
    300315            return;
    301316        }
    302317
    303318        // walk through the filters and execute _preparse
    304         foreach ($this->_filters as $filter) {
     319        foreach ($this->_filters as $filter)
     320        {
    305321            $filter->setText($this->_preparsed);
    306322            $filter->_preparse();
     
    326342     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    327343     */
    328     function _buildTagArray()
     344    function _buildTagArray(): void
    329345    {
    330346        $this->_tagArray = array();
     
    333349        $strLength = strlen($str);
    334350
    335         while (($strPos < $strLength)) {
     351        while (($strPos < $strLength))
     352        {
    336353            $tag = array();
    337354            $openPos = strpos($str, $this->_options['open'], $strPos);
    338             if ($openPos === false) {
     355            if ($openPos === false)
     356            {
    339357                $openPos = $strLength;
    340358                $nextOpenPos = $strLength;
    341359            }
    342             if ($openPos + 1 > $strLength) {
     360            if ($openPos + 1 > $strLength)
     361            {
    343362                $nextOpenPos = $strLength;
    344             } else {
     363            } else
     364            {
    345365                $nextOpenPos = strpos($str, $this->_options['open'], $openPos + 1);
    346                 if ($nextOpenPos === false) {
     366                if ($nextOpenPos === false)
     367                {
    347368                    $nextOpenPos = $strLength;
    348369                }
    349370            }
    350371            $closePos = strpos($str, $this->_options['close'], $strPos);
    351             if ($closePos === false) {
     372            if ($closePos === false)
     373            {
    352374                $closePos = $strLength + 1;
    353375            }
    354376
    355             if ($openPos == $strPos) {
    356                 if (($nextOpenPos < $closePos)) {
     377            if ($openPos == $strPos)
     378            {
     379                if (($nextOpenPos < $closePos))
     380                {
    357381                    // new open tag before closing tag: treat as text
    358382                    $newPos = $nextOpenPos;
    359383                    $tag['text'] = substr($str, $strPos, $nextOpenPos - $strPos);
    360384                    $tag['type'] = 0;
    361                 } else {
     385                } else
     386                {
    362387                    // possible valid tag
    363388                    $newPos = $closePos + 1;
    364389                    $newTag = $this->_buildTag(substr($str, $strPos, $closePos - $strPos + 1));
    365                     if (($newTag !== false)) {
     390                    if (($newTag !== false))
     391                    {
    366392                        $tag = $newTag;
    367                     } else {
     393                    } else
     394                    {
    368395                        // no valid tag after all
    369396                        $tag['text'] = substr($str, $strPos, $closePos - $strPos + 1);
     
    371398                    }
    372399                }
    373             } else {
     400            } else
     401            {
    374402                // just text
    375403                $newPos = $openPos;
     
    379407
    380408            // join 2 following text elements
    381             if ($tag['type'] === 0 && isset($prev) && $prev['type'] === 0) {
     409            if ($tag['type'] === 0 && isset($prev) && $prev['type'] === 0)
     410            {
    382411                $tag['text'] = $prev['text'].$tag['text'];
    383412                array_pop($this->_tagArray);
     
    404433     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    405434     */
    406     function _buildTag($str)
     435    function _buildTag(string $str): bool|array
    407436    {
    408437        $tag = array('text' => $str, 'attributes' => array());
    409438
    410         if (substr($str, 1, 1) == '/') {        // closing tag
    411 
     439        if (substr($str, 1, 1) == '/')
     440        {        // closing tag
    412441            $tag['tag'] = strtolower(substr($str, 2, strlen($str) - 3));
    413             if (!in_array($tag['tag'], array_keys($this->_definedTags))) {
     442            if (!in_array($tag['tag'], array_keys($this->_definedTags)))
     443            {
    414444                return false;                   // nope, it's not valid
    415             } else {
     445            } else
     446            {
    416447                $tag['type'] = 2;
    417448                return $tag;
    418449            }
    419         } else {                                // opening tag
    420 
     450        } else
     451        {                                // opening tag
    421452            $tag['type'] = 1;
    422             if (strpos($str, ' ') && (strpos($str, '=') === false)) {
     453            if (strpos($str, ' ') && (strpos($str, '=') === false))
     454            {
    423455                return false;                   // nope, it's not valid
    424456            }
     
    429461            $ce = $this->_options['close_esc'];
    430462            $tagArray = array();
    431             if (preg_match("!$oe([a-z0-9]+)[^$ce]*$ce!i", $str, $tagArray) == 0) {
     463            if (preg_match("!$oe([a-z0-9]+)[^$ce]*$ce!i", $str, $tagArray) == 0)
     464            {
    432465                return false;
    433466            }
    434467            $tag['tag'] = strtolower($tagArray[1]);
    435             if (!in_array($tag['tag'], array_keys($this->_definedTags))) {
     468            if (!in_array($tag['tag'], array_keys($this->_definedTags)))
     469            {
    436470                return false;                   // nope, it's not valid
    437471            }
     
    441475            $attributeArray = array();
    442476            $regex = "![\s$oe]([a-z0-9]+)=(\"[^\s$ce]+\"|[^\s$ce]";
    443             if ($tag['tag'] != 'url') {
     477            if ($tag['tag'] != 'url')
     478            {
    444479                $regex .= "[^=]";
    445480            }
    446481            $regex .= "+)(?=[\s$ce])!i";
    447482            preg_match_all($regex, $str, $attributeArray, PREG_SET_ORDER);
    448             foreach ($attributeArray as $attribute) {
     483            foreach ($attributeArray as $attribute)
     484            {
    449485                $attNam = strtolower($attribute[1]);
    450                 if (in_array($attNam, array_keys($this->_definedTags[$tag['tag']]['attributes']))) {
    451                     if ($attribute[2][0] == '"' && $attribute[2][strlen($attribute[2])-1] == '"') {
     486                if (in_array($attNam, array_keys($this->_definedTags[$tag['tag']]['attributes'])))
     487                {
     488                    if ($attribute[2][0] == '"' && $attribute[2][strlen($attribute[2])-1] == '"')
     489                    {
    452490                        $tag['attributes'][$attNam] = substr($attribute[2], 1, -1);
    453                     } else {
     491                    } else
     492                    {
    454493                        $tag['attributes'][$attNam] = $attribute[2];
    455494                    }
     
    476515     * @author   Stijn de Reede  <sjr@gmx.co.uk>, Seth Price <seth@pricepages.org>
    477516     */
    478     function _validateTagArray()
     517    function _validateTagArray(): void
    479518    {
    480519        $newTagArray = array();
    481520        $openTags = array();
    482         foreach ($this->_tagArray as $tag) {
     521        foreach ($this->_tagArray as $tag)
     522        {
     523            echo('newTagArray: ');
     524            print_r($newTagArray);
    483525            $prevTag = end($newTagArray);
    484526
    485527            // TODO: why prevTag cann by type bool?
    486             if (!is_array($prevTag)) continue;
     528            if (!is_array($prevTag))
     529            {
     530                echo('tag: ');
     531                print_r($tag);
     532                continue;
     533            }
    487534
    488535            switch ($tag['type']) {
     
    497544                    $child !== true )
    498545                {
    499                     if (trim($tag['text']) == '') {
     546                    if (trim($tag['text']) == '')
     547                    {
    500548                        //just an empty indentation or newline without value?
    501                         continue;
     549                        continue 2;
    502550                    }
    503551                    $newTagArray[] = $child;
     
    505553                }
    506554
    507                 if ($prevTag['type'] === 0) {
     555                if ($prevTag['type'] === 0)
     556                {
    508557                    $tag['text'] = $prevTag['text'].$tag['text'];
    509558                    array_pop($newTagArray);
     
    515564                if (!$this->_isAllowed(end($openTags), $tag['tag']) ||
    516565                   ($parent = $this->_parentNeeded(end($openTags), $tag['tag'])) === true ||
    517                    ($child  = $this->_childNeeded(end($openTags),  $tag['tag'])) === true) {
     566                   ($child  = $this->_childNeeded(end($openTags),  $tag['tag'])) === true)
     567                   {
    518568                    $tag['type'] = 0;
    519                     if ($prevTag['type'] === 0) {
     569                    if ($prevTag['type'] === 0)
     570                    {
    520571                        $tag['text'] = $prevTag['text'].$tag['text'];
    521572                        array_pop($newTagArray);
    522573                    }
    523                 } else {
    524                     if ($parent) {
     574                } else
     575                {
     576                    if ($parent)
     577                    {
    525578                        /*
    526579                         * Avoid use of parent if we can help it. If we are
     
    531584                         * current tag.
    532585                         */
    533                         if ($tag['tag'] == end($openTags)){
     586                        if ($tag['tag'] == end($openTags))
     587                        {
    534588                            $newTagArray[] = $this->_buildTag('[/'.$tag['tag'].']');
    535589                            array_pop($openTags);
    536                         } else {
     590                        } else
     591                        {
    537592                            $newTagArray[] = $parent;
    538593                            $openTags[] = $parent['tag'];
     
    549604
    550605            case 2:
    551                 if (($tag['tag'] == end($openTags) || $this->_isAllowed(end($openTags), $tag['tag']))) {
    552                     if (in_array($tag['tag'], $openTags)) {
     606                if (($tag['tag'] == end($openTags) || $this->_isAllowed(end($openTags), $tag['tag'])))
     607                {
     608                    if (in_array($tag['tag'], $openTags))
     609                    {
    553610                        $tmpOpenTags = array();
    554                         while (end($openTags) != $tag['tag']) {
     611                        while (end($openTags) != $tag['tag'])
     612                        {
    555613                            $newTagArray[] = $this->_buildTag('[/'.end($openTags).']');
    556614                            $tmpOpenTags[] = end($openTags);
     
    569627                        }*/
    570628                    }
    571                 } else {
     629                } else
     630                {
    572631                    $tag['type'] = 0;
    573                     if ($prevTag['type'] === 0) {
     632                    if ($prevTag['type'] === 0)
     633                    {
    574634                        $tag['text'] = $prevTag['text'].$tag['text'];
    575635                        array_pop($newTagArray);
     
    580640            }
    581641        }
    582         while (end($openTags)) {
     642        while (end($openTags))
     643        {
    583644            $newTagArray[] = $this->_buildTag('[/'.end($openTags).']');
    584645            array_pop($openTags);
     
    605666    {
    606667        if (!isset($this->_definedTags[$in]['parent']) ||
    607             ($this->_definedTags[$in]['parent'] == 'all')
    608         ) {
     668            ($this->_definedTags[$in]['parent'] == 'all'))
     669        {
    609670            return false;
    610671        }
     
    612673        $ar = explode('^', $this->_definedTags[$in]['parent']);
    613674        $tags = explode(',', $ar[1]);
    614         if ($ar[0] == 'none'){
    615             if ($out && in_array($out, $tags)) {
     675        if ($ar[0] == 'none')
     676        {
     677            if ($out && in_array($out, $tags))
     678            {
    616679                return false;
    617680            }
     
    619682            return $this->_buildTag('['.$tags[0].']');
    620683        }
    621         if ($ar[0] == 'all' && $out && !in_array($out, $tags)) {
     684        if ($ar[0] == 'all' && $out && !in_array($out, $tags))
     685        {
    622686            return false;
    623687        }
     
    642706     * @author   Seth Price <seth@pricepages.org>
    643707     */
    644     function _childNeeded($out, $in)
     708    function _childNeeded($out, $in): bool
    645709    {
    646710        if (!isset($this->_definedTags[$out]['child']) ||
    647            ($this->_definedTags[$out]['child'] == 'all')
    648         ) {
     711           ($this->_definedTags[$out]['child'] == 'all'))
     712        {
    649713            return false;
    650714        }
     
    652716        $ar = explode('^', $this->_definedTags[$out]['child']);
    653717        $tags = explode(',', $ar[1]);
    654         if ($ar[0] == 'none'){
    655             if ($in && in_array($in, $tags)) {
     718        if ($ar[0] == 'none')
     719        {
     720            if ($in && in_array($in, $tags))
     721            {
    656722                return false;
    657723            }
     
    659725            return $this->_buildTag('['.$tags[0].']');
    660726        }
    661         if ($ar[0] == 'all' && $in && !in_array($in, $tags)) {
     727        if ($ar[0] == 'all' && $in && !in_array($in, $tags))
     728        {
    662729            return false;
    663730        }
     
    680747     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    681748     */
    682     function _isAllowed($out, $in)
    683     {
    684         if (!$out || ($this->_definedTags[$out]['allowed'] == 'all')) {
     749    function _isAllowed($out, $in): bool
     750    {
     751        if (!$out || ($this->_definedTags[$out]['allowed'] == 'all'))
     752        {
    685753            return true;
    686754        }
    687         if ($this->_definedTags[$out]['allowed'] == 'none') {
     755        if ($this->_definedTags[$out]['allowed'] == 'none')
     756        {
    688757            return false;
    689758        }
     
    691760        $ar = explode('^', $this->_definedTags[$out]['allowed']);
    692761        $tags = explode(',', $ar[1]);
    693         if ($ar[0] == 'none' && in_array($in, $tags)) {
     762        if ($ar[0] == 'none' && in_array($in, $tags))
     763        {
    694764            return true;
    695765        }
    696         if ($ar[0] == 'all'  && in_array($in, $tags)) {
     766        if ($ar[0] == 'all'  && in_array($in, $tags))
     767        {
    697768            return false;
    698769        }
     
    712783     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    713784     */
    714     function _buildParsedString()
     785    function _buildParsedString(): void
    715786    {
    716787        $this->_parsed = '';
    717         foreach ($this->_tagArray as $tag) {
    718             switch ($tag['type']) {
     788        foreach ($this->_tagArray as $tag)
     789        {
     790            switch ($tag['type'])
     791            {
    719792
    720793            // just text
     
    728801                if ($this->_options['quotestyle'] == 'single') $q = "'";
    729802                if ($this->_options['quotestyle'] == 'double') $q = '"';
    730                 foreach ($tag['attributes'] as $a => $v) {
     803                foreach ($tag['attributes'] as $a => $v)
     804                {
    731805                    //prevent XSS attacks. IMHO this is not enough, though...
    732806                    //@see http://pear.php.net/bugs/bug.php?id=5609
     
    736810
    737811                    if (($this->_options['quotewhat'] == 'nothing') ||
    738                         (($this->_options['quotewhat'] == 'strings') && is_numeric($v))
    739                     ) {
     812                        (($this->_options['quotewhat'] == 'strings') && is_numeric($v)))
     813                    {
    740814                        $this->_parsed .= ' '.sprintf($this->_definedTags[$tag['tag']]['attributes'][$a], $v, '');
    741                     } else {
     815                    } else
     816                    {
    742817                        $this->_parsed .= ' '.sprintf($this->_definedTags[$tag['tag']]['attributes'][$a], $v, $q);
    743818                    }
    744819                }
    745                 if ($this->_definedTags[$tag['tag']]['htmlclose'] == '' && $this->_options['xmlclose']) {
     820                if ($this->_definedTags[$tag['tag']]['htmlclose'] == '' && $this->_options['xmlclose'])
     821                {
    746822                    $this->_parsed .= ' /';
    747823                }
     
    751827            // closing tag
    752828            case 2:
    753                 if ($this->_definedTags[$tag['tag']]['htmlclose'] != '') {
     829                if ($this->_definedTags[$tag['tag']]['htmlclose'] != '')
     830                {
    754831                    $this->_parsed .= '</'.$this->_definedTags[$tag['tag']]['htmlclose'].'>';
    755832                }
     
    769846     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    770847     */
    771     function setText($str)
     848    function setText(string $str): void
    772849    {
    773850        $this->_text = $str;
     
    783860     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    784861     */
    785     function getText()
     862    function getText(): string
    786863    {
    787864        return $this->_text;
     
    797874     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    798875     */
    799     function getPreparsed()
     876    function getPreparsed(): string
    800877    {
    801878        return $this->_preparsed;
     
    811888     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    812889     */
    813     function getParsed()
     890    function getParsed(): string
    814891    {
    815892        return $this->_parsed;
     
    827904     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    828905     */
    829     function parse()
     906    function parse(): void
    830907    {
    831908        $this->_preparse();
     
    838915     * Quick method to do setText(), parse() and getParsed at once
    839916     *
     917     * @return   string
     918     * @access   public
     919     * @see      parse()
     920     * @see      $_text
     921     * @author   Stijn de Reede  <sjr@gmx.co.uk>
     922     */
     923    function qparse(string $str): string
     924    {
     925        $this->_text = $str;
     926        $this->parse();
     927        return $this->_parsed;
     928    }
     929
     930    /**
     931     * Quick static method to do setText(), parse() and getParsed at once
     932     *
    840933     * @return   none
    841934     * @access   public
     
    844937     * @author   Stijn de Reede  <sjr@gmx.co.uk>
    845938     */
    846     function qparse($str)
    847     {
    848         $this->_text = $str;
    849         $this->parse();
    850         return $this->_parsed;
    851     }
    852 
    853     /**
    854      * Quick static method to do setText(), parse() and getParsed at once
    855      *
    856      * @return   none
    857      * @access   public
    858      * @see      parse()
    859      * @see      $_text
    860      * @author   Stijn de Reede  <sjr@gmx.co.uk>
    861      */
    862     function staticQparse($str)
    863     {
    864         $p = new HTML_BBCodeParser2();
    865         $str = $p->qparse($str);
    866         unset($p);
     939    function staticQparse(string $str): string
     940    {
     941        $Parser = new HTML_BBCodeParser2();
     942        $str = $Parser->qparse($str);
     943        unset($Parser);
    867944        return $str;
    868945    }
    869946}
    870 ?>
Note: See TracChangeset for help on using the changeset viewer.