PHP code has gone PEAR Shaped with BBCode

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1659 Views - Last Post: 15 December 2011 - 01:57 PM Rate Topic: -----

#1 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

PHP code has gone PEAR Shaped with BBCode

Posted 10 December 2011 - 03:38 PM

I have downloaded the PEAR package called HTML_BBCodeParser and have set it up rather successfully. However when a user wants to add an image, it all goes wrong.

When they input the following code:
[image]http://www.urltoimage.com/image.jpg[/image]



The following output is seen:
[img="http://www.urltoimage.com/image.jpg"]



And when looking at the HTML source:

[img="<a target="_blank" href="http://mybox.dyndns-web.com/megabite/Images/LogoAlpha.png">http://www.urltoimage.com/image.jpg</a>"]



The code of the Images.php in the Filters directory is as follows:

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: Stijn de Reede <sjr@gmx.co.uk>                               |
// +----------------------------------------------------------------------+
//
// $Id: Images.php,v 1.8 2007/07/02 17:44:47 cweiske Exp $
//

/**
* @package  HTML_BBCodeParser
* @author   Stijn de Reede  <sjr@gmx.co.uk>
*/
require_once 'HTML/BBCodeParser/Filter.php';

class HTML_BBCodeParser_Filter_Images extends HTML_BBCodeParser_Filter
{

    /**
    * An array of tags parsed by the engine
    *
    * @access   private
    * @var      array
    */
    var $_definedTags = array(
        'img' => array(
            'htmlopen'  => 'img',
            'htmlclose' => '',
            'allowed'   => 'none',
            'attributes'=> array(
                'img'   => 'src=%2$s%1$s%2$s',
                'w'     => 'width=%2$s%1$d%2$s',
                'h'     => 'height=%2$s%1$d%2$s',
                'alt'   => 'alt=%2$s%1$s%2$s',
            )
        )
    );

    /**
    * Executes statements before the actual array building starts
    *
    * This method should be overwritten in a filter if you want to do
    * something before the parsing process starts. This can be useful to
    * allow certain short alternative tags which then can be converted into
    * proper tags with preg_replace() calls.
    * The main class walks through all the filters and and calls this
    * method if it exists. The filters should modify their private $_text
    * variable.
    *
    * @return   none
    * @access   private
    * @see      $_text
    * @author   Stijn de Reede  <sjr@gmx.co.uk>
    */
    function _preparse()
    {
        $options = PEAR::getStaticProperty('HTML_BBCodeParser','_options');
        $o  = $options['open'];
        $c  = $options['close'];
        $oe = $options['open_esc'];
        $ce = $options['close_esc'];
        $this->_preparsed = preg_replace(
			"!".$oe."img(\s?.*)".$ce."(.*)".$oe."/img".$ce."!Ui",
			$o."img=\"\$2\"\$1".$c.$o."/img".$c,
			$this->_text);
    }
}



And the ini file is as follows:

[HTML_BBCodeParser]

; possible values: single|double
; use single or double quotes for attributes
quotestyle  = double

; possible values: all|nothing|strings
; quote all attribute values, none, or only the strings
quotewhat   = all

; the opening tag character
open        = [

; the closing tag character
close       = ]

; possible values: true|false
; use xml style closing tags for single html tags (<img> or <img />)
xmlclose    = true

; possible values: a comma seperated list of filters
; comma seperated list of filters to use
filters     = Basic,Extended,Links,Images,Lists,Email



Anyone experiencing this problem or know how to fix it - I've literally tried everything.

Is This A Good Question/Topic? 1
  • +

Replies To: PHP code has gone PEAR Shaped with BBCode

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 10 December 2011 - 04:12 PM

I'm not too great with RegEx, but I think there might be a problem with the following method:
    function _preparse()
    {
        $options = PEAR::getStaticProperty('HTML_BBCodeParser','_options');
        $o  = $options['open'];
        $c  = $options['close'];
        $oe = $options['open_esc'];
        $ce = $options['close_esc'];
        $this->_preparsed = preg_replace(
			"!".$oe."img(\s?.*)".$ce."(.*)".$oe."/img".$ce."!Ui",
			$o."img=\"\$2\"\$1".$c.$o."/img".$c,
			$this->_text);
    }


preg_replace takes three arguments - pattern, replacement, and subject. It looks like the pattern it is trying to match "img" tags rather than "image" tags.

As I said, I'm not great with RegEx, so this answer might be completely wrong. In any case, +1 for the great topic title :)
Was This Post Helpful? 1
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: PHP code has gone PEAR Shaped with BBCode

Posted 10 December 2011 - 05:30 PM

That's because BBCode is supposed to match img tags. It's a standard tag. I've only ever seen the [image] variant on cheaply done free forum software.
Was This Post Helpful? 1
  • +
  • -

#4 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 03:13 AM

Just tried that as well Valek and got the same result
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 04:04 AM

kumaraj said:

filters = Basic,Extended,Links,Images,Lists,Email

Try placing the "Images" in front of "Links".

If the URL to the image is an absolute URL, and if the links are being parsed before the images, it may be corrupting them.

To test this, remove the "http://" from the link and use a relative path instead. If that fixes things, I'm probably right. If not then there is something else going on.
Was This Post Helpful? 2
  • +
  • -

#6 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 11:13 AM

Haha e_i_pi, chuckled to myself when I came up with that title, you were right it was searching for img and not image but even with the [ img ] tag it didn't work :(

Atli, followed your instructions and got zilch, I even restarted my computer and got nothing :(

I just can't tell what's going on :@
Was This Post Helpful? 0
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 02:02 PM

Just so that we're on the right page, when the following text is entered:
[img w=20 h=50 alt=abc]http://myimage.com/myimage.jpg[/img]


...what is outputted? You should be seeing this:
<img src="http://myimage.com/myimage.jpg" width="20" height="50" alt="abc" />



From your original post, it looks like what's being outputted is getting parsed through a filter for [url] tags.
Was This Post Helpful? 1
  • +
  • -

#8 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 02:45 PM

You should see

Quote

<img src="http://myimage.com/myimage.jpg" width="20" height="50" alt="abc" />



but instead you see:
[img="<a target="_blank" href="http://myimage.com/myimage.jpg">http://myimage.com/myimage.jpg</a>" w=20 h=50 alt=abc]



So it is being parsed through the URL tag even though the Image filter preceeds the url one in the .INI file.

Odd huh?

This post has been edited by kumaraj: 11 December 2011 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 11 December 2011 - 06:03 PM

Look, I'm not sure what the soution is right now (and I'm at work, so I can't dawdle on DIC too much), but I need to get a BBCode parser into my site anyhow, so I'll try this one out. Once I get it in, I'll tinkle around with it and see what I come up with. What's the version you're running? Do you have a link handy?
Was This Post Helpful? 1
  • +
  • -

#10 kumaraj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 12-February 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 11:13 AM

Thanks e_i_pi for your help. I am running PHP5 and the latest version of BBCodeParser (I'm not sure the exact number). I really appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#11 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 05:13 PM

Well, my help might take a little while to come lol. I'm running WAMPServer locally on Windows7 OS, which is nightmarish, but I've gotten it to work. Now, I've just installed the latest version of PEAR, and I'm having include path issues. I keep getting errors thrown like this:
Fatal error: require_once() [function.require]: Failed opening required 'PHPUnit/Framework/TestSuite.php' (include_path='.;C:\php5\pear') in C:\wamp\www\sun\test.php on line 2


Easy enough to fix, just go into php.ini and change the include path. Problem 1 was that WAMPServer wasn't pointing to the correct php.ini file. I've taken the easy route out and simply updated the incorrect php.ini file, rather than redirect the config file to the correct php.ini. That solves the path name resolution issue.

Now, if I run the following script:
<?php
require_once 'System.php';
require_once 'PHPUnit/Framework/TestSuite.php';
?>


...I get this problem:
Fatal error: Interface 'PHPUnit_Framework_Test' not found in C:\wamp\bin\php\php5.3.1\PEAR\PHPUnit\Framework\TestSuite.php on line 83


Well, I can see where the interface is declared (\PEAR\PHPUnit\Framework\Test.php), but it seems that that file is never included/required. I have tried including PEAR5.php, PEAR.php, and System.php, all to no avail.

Are you able to guide me as to what I have to do to get PEAR accessible from regular PHP script? If I run this script:
<?php
require_once 'System.php';
var_dump(class_exists('System', false));
?>


...as per step #4 of this page of the PEAR manual that helps you check if PEAR works, then I get the expected output of bool(true). So it looks like it's working, except it's failing to autoload the prerequisite interfaces :whatsthat:

This post has been edited by e_i_pi: 12 December 2011 - 05:15 PM

Was This Post Helpful? 1
  • +
  • -

#12 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 06:03 PM

Okay, you can ignore the monster post. I just installed HTML_BBCodeParser 1.2.2 on my live site and started fiddling around with it.

I'm able to replicate the error in your original post. And, I have a fix for you :sorcerer:

In your .ini file you have the filters in the following order:
filters     = Basic,Extended,Links,Images,Lists,Email



Because 'Links' comes before 'Images', the URL to the image is being parsed as a link before it gets parsed as an image location. Simply change that line of code in your .ini file to this:
filters     = Basic,Extended,Images,Links,Lists,Email


...et voila! Fixed >_<
Was This Post Helpful? 1
  • +
  • -

#13 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 06:21 PM

That's what I suggested earlier, but it doesn't seem to have worked for him.
Was This Post Helpful? 1
  • +
  • -

#14 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 07:40 PM

View PostAtli, on 12 December 2011 - 06:21 PM, said:

That's what I suggested earlier, but it doesn't seem to have worked for him.

Hmm this is true. +1 for the earlier answer.

I did manage to replicate his problem exactly though. Perhaps he was using [image] tags after he had updated the filtering order?
Was This Post Helpful? 1
  • +
  • -

#15 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 782
  • View blog
  • Posts: 1,663
  • Joined: 30-January 09

Re: PHP code has gone PEAR Shaped with BBCode

Posted 12 December 2011 - 10:19 PM

Well, PEAR seems great, but I hate reliance on 3rd party products, so I've ported the HTML_BBCodeParser over to PHP 5.3 compliant code, complete with namespaces and publicity/privacy declaration throughout. The only functionality I have not ported over yet is setting the options via an ini file. If you like I can zip it up and send you over a copy.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2