MyBB Community Forums

Full Version: Using PHP in MyBB Problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, So new MyBB user here. I am trying to move away from PHPBB and on to MyBB.

At the moment I am using a custom RSS JCarousel script. You can see it in action at www.fwkzt.org

Now I know MyBB 1.8 doesn't like PHP straight out the box so I installed the PHP and Template Conditionals (2.0) plugin to enable me entering PHP into MyBB.

Now the problem I have is that I have a ?> with in my <?php ?> tags and the plugin thinks this is a end of php tag and closes it off. So only a portion of the PHP script is called.

Is there a work around I can look in to or a different PHP plugin? I would really hate to use some shitty RSS feed widget instead of JCarousel as it's awesome.


Here is my script which is used:


    <html>
    <head>
    <title>{$mybb->settings['bbname']}</title>
    <script type="text/javascript" src="http://www.ryanhamlin.co.uk/FWKZT/jscripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://www.ryanhamlin.co.uk/FWKZT/jscripts/jcarousel.basic.js"></script>
    <script type="text/javascript" src="http://www.ryanhamlin.co.uk/FWKZT/jscripts/jquery.jcarousel.min.js"></script>
    <link rel="stylesheet" href="http://www.ryanhamlin.co.uk/FWKZT/inc/jcarousel.basic.css" type="text/css" />
    {$headerinclude}
    <script type="text/javascript">
    <!--
            lang.no_new_posts = "{$lang->no_new_posts}";
            lang.click_mark_read = "{$lang->click_mark_read}";
    // -->
    </script>
    </head>
    <body>
    {$header}{$mysteamlist}
   
    <div class="jcarousel-wrapper">
                    <div class="jcarousel">
                        <ul>
                         <!-- PHP -->
    <?php
                                                    $ch = curl_init();
                                                    curl_setopt($ch, CURLOPT_URL, "http://www.fwkzt.org/feed.php?mode=news");
                                                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                                                    $feed = curl_exec($ch);
                                                    curl_close($ch);
                                                    if ($feed)
                                                    {
                                                            try
                                                            {
                                                                    $xml = new SimpleXmlElement($feed, LIBXML_NOCDATA);
                                                                    $cnt = count($xml->entry);
                                                                    if($cnt > 10)
                                                                            $cnt = 10;
                                           
                                                                    for($i=0; $i<$cnt; $i++)
                                                                    {
                                                                            $urlAtt = $xml->entry[$i]->link->attributes();
                                                                            $url = $urlAtt['href'];
                                                                            $title = $xml->entry[$i]->title;
                                                                            $desc = $xml->entry[$i]->content;
                                                                           
                                                                            $html = new SimpleXmlElement('<html>'.trim($desc).'</html>', LIBXML_NOCDATA);
                                                                            $elements = $html->xpath("img|/img|//img");
                                                                            $image = 0;
                                                                            $margin = 0;
                                                                            if(count($elements) > 0)
                                                                            {
                                                                                    $image = $elements[0]->attributes();
                                                                                    $margin = 40;
                                                                            }
                                                                            else
                                                                            {
                                                                             $image = "";
                                                                            }
                                                                            $desc = trim(strip_tags($desc,"
                                                                            <p></p><br><b></b>"));
                                                                            $desc = preg_replace(array("#^(<br\s?/?>)+#","#(<br\s?/?>){2,}#"), array("","<br><br>"), $desc);
                                                                            $skip = strpos($desc," ",450);
                                                                            $alt = strrpos($desc,"<p>");
     
                                                                            if($alt < $skip && $alt !== false || $skip === false)
                                                                                    $skip = $alt;
                                                                            echo '<li style="background-image: url(\''.$image.'\');"><div id="announcement" style="margin-left:'.$margin.'%;"><h2><a href="'.$url.'">'.$title.'</a></h2><p>'.substr($desc,0,$skip).($skip==$alt?'':'...').'</p><p style="text-align:right;"><a href="'.$url.'">More&#x276f;</a></div></li>';
                                                                    }
                                                            }
                                                            catch(Exception $ex)
                                                            {
                                                                    echo "Error Thrown:";
                                                                    echo $ex;
                                                                    echo $ex->getCode();
                                                                    echo $ex->getFile();
                                                                    echo $ex->getLine();
                                                            }
                                                    }
    ?>
                                                    <!-- ENDPHP -->
                        </ul>
                    </div>                
                    <p class="jcarousel-pagination">
                       
                    </p>
    </div>
     
    {$forums}
    {$boardstats}
     
     
    <br class="clear" />
    {$footer}
    </body>
    </html>

I guess the problematic line is the following:
$desc = preg_replace(array("#^(<br\s?/?>)+#","#(<br\s?/?>){2,}#"), array("","<br><br>"), $desc);

Generally, I don't think it's a good idea to spice up templates with php-code (after all, the user-interface should be completely free of any operations) but my idea would be to escape the question-marks with PHP's chr()-function. I'm just typing this up on the fly, so it might have some errors, but a solution (although not really increasing the readability) would be:
$desc = preg_replace(array("#^(<br\s?/".chr(63).">)+#","#(<br\s?/".chr(63).">){2,}#"), array("","<br><br>"), $desc);

Try to see if this helps Wink
Thanks for that I will give it a try and see if I can get it to work. Yeah I get pretty much everything I need from myBB apart from the RSS slider on index pulling anouncement posts and steam badge integration. (The one on the community mybb page is a bit crap!)

I will let you know how it goes