Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility to php 3.2 #15

Open
Hideman85 opened this issue Oct 10, 2016 · 10 comments
Open

Compatibility to php 3.2 #15

Hideman85 opened this issue Oct 10, 2016 · 10 comments

Comments

@Hideman85
Copy link

I test your new version on my forum and have just one probleme ...
I find it and correct it :
In routing.yml you have :

o0johntam0o_codeboxplus_base_controller:
    pattern: /codeboxplus
    defaults: { _controller: o0johntam0o.codeboxplus.controller:base }

o0johntam0o_codeboxplus_download_controller:
    pattern: /codeboxplus/download/{id}-{part}
    defaults: { _controller: o0johntam0o.codeboxplus.controller:downloader, id:0, part:0 }
    requirements:
        id: \d+
        part: \d+

And you need :

o0johntam0o_codeboxplus_base_controller:
    path: '/codeboxplus'
    defaults: { _controller: 'o0johntam0o.codeboxplus.controller:base' }

o0johntam0o_codeboxplus_download_controller:
    path: '/codeboxplus/download/{id}-{part}'
    defaults: { _controller: 'o0johntam0o.codeboxplus.controller:downloader', id:0, part:0 }
    requirements:
        id: \d+
        part: \d+

So you just need to create an other branch to apply change ....

Thanks for you for your update ;)

@Hideman85
Copy link
Author

Hideman85 commented Oct 10, 2016

I detect secondary issues :
One line of tow it insert an blank line .... i don t know what .... let s view it :
Input : https://snag.gy/Zd7Y3m.jpg
Result : https://snag.gy/ijb5OC.jpg

@o0johntam0o
Copy link
Owner

o0johntam0o commented Oct 11, 2016

phpBB v3.2 has a bit of change in events and BBCode, but currently I have no time to discover it.
There's some file need to be update to compatible with phpBB v3.2:

  • /controller/main.php (Downloading code)
  • /event/main_listener.php (Showing code box)
  • /migrations/v200.php (Install the extension)

Edit: New branch for phpBB3.2 https://github.com/o0johntam0o/phpBB-Extension-Codebox-Plus/tree/phpbb3.2

@Hideman85
Copy link
Author

Hideman85 commented Oct 13, 2016

I think I found the bug ...

In file '/event/main_listener.php' you have this function :

    /*
    * Event: core.viewtopic_post_rowset_data (viewtopic.php)
    */
  public function viewtopic_event($event)
    {
        if (isset($event['rowset_data']))
        {
            $rowset_data = $event['rowset_data'];
            $post_text = isset($rowset_data['post_text']) ? $rowset_data['post_text'] : '';
            $bbcode_uid = isset($rowset_data['bbcode_uid']) ? $rowset_data['bbcode_uid'] : '';
            $post_id = isset($rowset_data['post_id']) ? $rowset_data['post_id'] : 0;
            $part = 0;
            var_dump($post_text);
            var_dump('$bbcode_uid = '.$bbcode_uid);
            $post_text = preg_replace_callback("#\[codebox=(.*?) file=(.*?):$bbcode_uid\](.*?)\[\/codebox:$bbcode_uid\]#msi",
                            function ($arg) use ($post_id, &$part)
                            {
                                return $this->codebox_template($arg[3], $arg[1], $arg[2], $post_id, ++$part);
                            },
                            $post_text);
            //$post_text = preg_replace("#\[codebox=(.*?) file=(.*?):$bbcode_uid\](.*?)\[/codebox:$bbcode_uid\]#msie", "\$this->codebox_template('\$3', '\$1', '\$2', \$post_id, ++\$part)", $post_text);

            if (isset($rowset_data['post_text']) && $part > 0)
            {
                $rowset_data['post_text'] = $post_text;
                $event['rowset_data'] = $rowset_data;
            }
        }
    }


But regex doesn't match ... I printed $post_text and $bbcode_uid for understand the regex ... but i don't understand this ... So what I got :
string(22) "$bbcode_uid = 69e5lv78"
string(1231) "<r><CODEBOX codebox="java" file="Untitled.java"><s>[Codebox=java file=Untitled.java]</s>
/******************************************************************************<br/>
 *  Compilation:  javac HelloWorld.java<br/>
 *  Execution:    java HelloWorld<br/>
 *<br/>
 *  Prints "Hello, World". By tradition, this is everyone's first program.<br/>
 *<br/>
 *  % java HelloWorld<br/>
 *  Hello, World<br/>
 *<br/>
 *  These 17 lines of text are comments. They are not part of the program;<br/>
 *  they serve to remind us about its properties. The first two lines tell<br/>
 *  us what to type to compile and test the program. The next line describes<br/>
 *  the purpose of the program. The next few lines give a sample execution<br/>
 *  of the program and the resulting output. We will always include such <br/>
 *  lines in our programs and encourage you to do the same.<br/>
 *<br/>
 ******************************************************************************/<br/>
<br/>
public class HelloWorld {<br/>
<br/>
    public static void main(String[] args) {<br/>
        // Prints "Hello, World" to the terminal window.<br/>
        System.out.println("Hello, World");<br/>
    }<br/>
<br/>
}<br/>

<e>[/Codebox]</e></CODEBOX></r>"

So regex `"#\[codebox=(.*?) file=(.*?):$bbcode_uid\](.*?)\[\/codebox:$bbcode_uid\]#msi"` doesn't match ...
So the display was created in second call :
    /*
    * Event: core.modify_format_display_text_after (message_parser.php)
    * Use: $this->codebox_template()
    * Generate text for preview
    */
    public function message_parser_event($event)
    {
        if (isset($event['text']))
        {
            var_dump($event);
            $text = $event['text'];
            $post_id = 0;
            $part = 0;
            $text = preg_replace_callback('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>(.*?)</code></div>#msi',
                        function ($arg) use ($post_id, &$part)
                        {
                            return $this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', "\n", $arg[3]), $arg[1], $arg[2], $post_id, ++$part);
                        },
                        $text);
            //$text = preg_replace('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>(.*?)</code></div>#msie', "\$this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', '\n', '\$3'), '\$1', '\$2', \$post_id, ++\$part)", $text);
            $event['text'] = $text;
        }
  }

So the code has been highlighting .... but $post_id is equal to 0 ( is normal because this event is fire when you want a preview ... but is fire when you read topic too ... )
And if $post_id is equal to 0 the download controller not be call ...

    public function codebox_template($code = '', $lang = 'text', $file = '', $id = 0, $part = 0)
    {
        [...]
        if ($id != 0 && $this->download_enabled && $lang != 'NULL')
        {
            $re .= '&nbsp;<a href="' . $this->helper->route('o0johntam0o_codeboxplus_download_controller', array('id' => $id, 'part' => $part)) . '" onclick="window.open(this.href); return false;">';
            $re .= '[' . $this->user->lang['CODEBOX_PLUS_DOWNLOAD'] . ']</a> ' . '('. $file . ')';
        }
        [...]
    }

So I think we need just update the first regex to match with input ...
And for double line just need to update this :
/*
    * Event: core.modify_format_display_text_after (message_parser.php)
    * Use: $this->codebox_template()
    * Generate text for preview
    */
    public function message_parser_event($event)
    {
        if (isset($event['text']))
        {
            $text = $event['text'];
            $post_id = 0;
            $part = 0;
            $text = preg_replace_callback('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>(.*?)</code></div>#msi',
                        function ($arg) use ($post_id, &$part)
                        {
                            // Before : 
                            //return $this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', "\n", $arg[3]), $arg[1], $arg[2], $post_id, ++$part);
                            // After : 
                            return $this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', "", $arg[3]), $arg[1], $arg[2], $post_id, ++$part);
                        },
                        $text);
            //$text = preg_replace('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>(.*?)</code></div>#msie', "\$this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', '\n', '\$3'), '\$1', '\$2', \$post_id, ++\$part)", $text);
            $event['text'] = $text;
        }
  }

@Hideman85
Copy link
Author

Hideman85 commented Oct 19, 2016

I try to find the good regex .... but I need your help
My regex done for one use of your BBCode : http://regexr.com/3efkj
But not for multiple use : http://regexr.com/3efkg

@o0johntam0o
Copy link
Owner

o0johntam0o commented Oct 20, 2016

Hi Hideman85, I guess the new regex should be something like this <r><CODEBOX codebox="(.*?)" file="(.*?)"><s>[Codebox=.*? file=.*?]</s>(.*?)<e>[/Codebox]</e></CODEBOX></r>
Hope that it's helpful for you.
Regard.

@Hideman85
Copy link
Author

Hideman85 commented Oct 21, 2016

Balise <r> mean start of post and </r> mean end of post.
Wheras one post can have many use of <codebox> ....
So I search one regex for match all of use ....
And I need replace the text by the return of call $this->codebox_template ....
Thanks for your help

@Hideman85
Copy link
Author

Hideman85 commented Oct 26, 2016

Good regex is '#<CODEBOX codebox=".*" file=".*"><s>\[Codebox=.* file=(.*)\]<\/s>(.*?)<e>\[\/Codebox]<\/e><\/CODEBOX>#si' :
Is perfect on regex tester : http://regexr.com/3eh4m
But fail in php :

preg_match_all('#<CODEBOX codebox=".*" file=".*"><s>\[Codebox=.* file=(.*)\]<\/s>(.*?)<e>\[\/Codebox]<\/e><\/CODEBOX>#si', $post_data['post_text'], $code_data);

var_dump($code_data);

http://www.pastefile.com/bbgFtG

It's same regex but doesnt match all ....

This is just the last incompatibility ....

I just add link to my forum where I try to debug it : http://factionacraft.raidghost.com/forum/viewtopic.php?f=2&t=16

Thanks in advance for your help

@o0johntam0o
Copy link
Owner

Hi Hideman85, you can start with the following instructions:
In "getSubscribedEvents", change this line 'core.modify_text_for_display_after' => 'message_parser_event' to 'core.modify_text_for_display_after' => 'functions_content_event'

Add this function:

    public function functions_content_event($event)
    {
        if (isset($event['text']))
        {
            $text = $event['text'];
            $post_id = 0;
            $part = 0;
            $callback = function($arg) use ($post_id, &$part)
            {
                return $this->codebox_template(preg_replace('#\<br\\s*/?\>#msi', "\n", $arg[4]), $arg[1], $arg[2], $arg[3], ++$part);
            };
            $text = preg_replace_callback('#<div class="codebox" title="' . preg_quote($this->user->lang['CODEBOX_PLUS_TITLE']) . '" data-language="(.*?)" data-filename="(.*?)"><p>.*?</p><code>\(([0-9]+)\)(.*?)</code></div>#msi', $callback, $text);
            $event['text'] = $text;
        }
    }

Edit the function "viewtopic_event":

    public function viewtopic_event($event)
    {
        if (isset($event['rowset_data']))
        {
            $rowset_data = $event['rowset_data'];
            $post_text = isset($rowset_data['post_text']) ? $rowset_data['post_text'] : '';
            $post_id = isset($rowset_data['post_id']) ? $rowset_data['post_id'] : 0;
            $callback = function($arg) use ($post_id)
            {
                return $arg[1] . "($post_id)" . $arg[2] . $arg[3];
            };
            $post_text = preg_replace_callback("#(<CODEBOX codebox=\".*?\" file=\".*?\"><s>\[Codebox=.*? file=.*?\]</s>)(.*?)(<e>\[/Codebox\]</e></CODEBOX>)#msi", $callback, $post_text, -1, $part);

            if (isset($rowset_data['post_text']) && $part > 0)
            {
                $rowset_data['post_text'] = $post_text;
                $event['rowset_data'] = $rowset_data;
            }
        }
    }

Good luck!

@Hideman85
Copy link
Author

Hi @o0johntam0o it's just perfect ❤️ last bug it's download ....
I use this :
preg_match_all('#<CODEBOX codebox=".*" file=".*"><s>\[Codebox=.* file=(.*)\]<\/s>(.*?)<e>\[\/Codebox]<\/e><\/CODEBOX>#msi', $post_data['post_text'], $code_data);

In your function private function codebox_output($id = 0, $part = 0) but it's abnormal because should match for all but just the last <codebox> match ....
Why the first doesn't match ?

You can try here : http://factionacraft.raidghost.com/forum/viewtopic.php?f=2&t=16

Thanks in advance for your help

@Hideman85
Copy link
Author

If you want I can send you my change for your extension styling ....
It's just full CSS styling and I use GeSHi color theme so anybody can choose his theme ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants