SMF 2 Quote Cleaner Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsw686
    Member
    • Sep 2007
    • 47
    • 3.8.x

    SMF 2 Quote Cleaner Code

    I wanted to share this with the community. I wrote this function to fix quotes in imported posts so they wouldn't show the unparsable SMF style quote code and the view post button next to the author's name would work. It uses the importpostid column to lookup the new postid. There are no guarantees on this, so make sure to test it out before doing the real import.

    In the # Posts section of cleaner.php find
    Code:
            $text = str_replace(array_keys($replacer), $replacer, $post['pagetext']);
    Add after
    Code:
            $text = parse_quote($text);
    In the # PM text section of cleaner.php find
    Code:
            $text = str_replace(array_keys($replacer), $replacer, $pm['message']);
    Add after
    Code:
            $text = parse_quote($text);
    Place this at the bottom of cleaner.php
    Code:
    function parse_quote($post)
    {
        global $Db_target, $targettableprefix;
    
        // no quote tag found, next post
        if(($start = strpos($post, '[quote author=')) === false)
            return $post;
    
        // no end of tag found, next post
        if(($end = strpos($post, ']', $start)) === false)
            return $post;
    
        // grab section to replace
        $found = substr($post, $start, $end - $start + 1);
        echo '<br /><br \>found: ' . $found . "<br \>\n";
    
        $parts = explode(' ', $found);
        for($i=0; $i<count($parts); $i++)
        {
            $part = $parts[$i];
    
            if(($pos = strpos($part, 'author=')) !== false)
            {            
                $filter['author'] = substr($part, $pos + 7);
    
                while(strpos($parts[$i + 1], '=') === false    && $i < count($parts))
                {
                    $i++;
                    $filter['author'] .= ' ' . $parts[$i];
                }
    
                $filter['author'] = trim($filter['author']);
                if($filter['author'][strlen($filter['author']) - 1] == ']')
                    $filter['author'] = substr($filter['author'], 0, strlen($filter['author']) - 1);
                 
                echo 'filtered: author: ' . $filter['author'] . "<br \>\n";            
            }
    
            if(($pos = strpos($part, 'link=topic=')) !== false)
            {            
                $tmp = explode('#', substr($part, $pos));
                if(isset($post[1]))
                {
                    $tmppost = substr($tmp[1], 3);
                    echo 'filtered: post: ' . $tmppost . "<br \>\n";
    
                    $request = $Db_target->query("
                        SELECT postid
                        FROM " . $targettableprefix . "post
                        WHERE importpostid = " . $tmppost);
    
                    if($row = $Db_target->fetch_array($request))
                    {
                            $filter['post'] = $row['postid'];
                            echo 'converted: post: ' . $filter['post'] . "<br \>\n";
                    }
                }
            }        
        }
    
        if(isset($filter['author']) && isset($filter['post']))
        {
            $replace = '[quote=' . $filter['author'] . ';' . $filter['post'] . ']';
            echo 'replacing: post: ' . $replace . "\n";
            $post = str_replace($found, $replace, $post);
        }
        else if(isset($filter['author']))
        {
            $replace = '[quote=' . $filter['author'] . ']';
            echo 'replacing: author: ' . $replace . "\n";
            $post = str_replace($found, $replace, $post);
        }
        else
        {
            echo "replacing: generic\n";
            $post = str_replace($found, '[quote]', $post);
        }
    
        return parse_quote($post);
    }
    If interested I also used the following for the replacer varaibles

    Code:
    // Set true or false as to the data you want to clean
    $do_posts            = true;
    $do_sigs            = true;
    $do_thread_titles    = false;
    $do_pm_text            = true;
    $do_pm_text_title    = false;
    Code:
    $replacer = array(
                "[li]"    => "[*]",
                "[/li]"    => "",
                "&quot;"    => "\"",
                "&amp;" => "&",
    The Reptile File
  • rsw686
    Member
    • Sep 2007
    • 47
    • 3.8.x

    #2
    I thought I would add an example to clarify what the code does.

    After I imported SMF quotes in topics looked like the below. Basically vBulletin does not have BB code for the SMF quote style so it displays as unparsed.

    [quote author=rsw686 link=topic=317354.msg2107862#msg2107862 date=1244903477]
    [/quote]

    My quote cleaner code formats them to vBulletin style and updates the post reference. By updating the post reference you can click the link to take you to the quoted posted.

    Originally posted by rsw686
    The Reptile File

    Comment

    • RedWingFan
      Senior Member
      • Sep 2004
      • 371
      • 4.0.0

      #3
      Here's a helpful addition: SMF2 has extended [ IMG ] tags, adding an alt attribute, and height/width. These need to be simplified in order to work in VB. Rather than change one type of tag to another, all we are doing is outputting a simple IMG tag to replace what is there.

      First of all, add this function, parse_img(), underneath the parse_quote() function at the bottom of cleaner.php:

      PHP Code:
      function parse_img($post)
      {
          global 
      $Db_target$targettableprefix;

          
      // no img tag found, next post
          
      if(($start strpos($post'[img ')) === false)
              return 
      $post;

          
      // no end of tag found, next post
          
      if(($end strpos($post']'$start)) === false)
              return 
      $post;

          
      // grab section to replace
          
      $found substr($post$start$end $start 1);
          echo 
      '<br /><br \>img found: ' $found "<br \>\n";

          echo 
      "replacing: img generic\n";
          
      $post str_replace($found'[img]'$post);

          return 
      parse_img($post);

      Next, add the following lines to cleaner.php up above the functions. You will be adding this in two places: under both #Posts and #PM Text. (If you allow signatures with images, you may want to add it there as well.)

      Find:
      Code:
      		$text = parse_quote($text);
      Add after:
      Code:
      		$text = parse_img($text);
      Do this, and you're golden! IMG tags sanitized and ready to use!

      Comment

      • jonnnie78
        New Member
        • Aug 2013
        • 3
        • 5.0.X

        #4
        Hello,

        We have imported a SMF forum but even after proceeding with what is told in this topic the forum continue not to show the html format, any ideas?


        Regards,

        Comment

        • jonnnie78
          New Member
          • Aug 2013
          • 3
          • 5.0.X

          #5
          no one to help?

          Comment

          • BirdOPrey5
            Senior Member
            • Jul 2008
            • 9613
            • 5.6.3

            #6
            Not sure what you mean by "forum continue to not show the html format." - HTML is disabled in forums by default, it is a security risk to allow raw HTML posts. If you really want to enable it you go to Admin CP -> Forums & Moderators -> Forum Manager, choose the forum(s), and enable HTML in the forum, and save changes.

            Comment

            • jonnnie78
              New Member
              • Aug 2013
              • 3
              • 5.0.X

              #7
              Hello Joe,

              Thanks for your response.

              What i mean is this:




              As you can see all images and html code doesnt work, and we have the forum full of this.


              Regards,

              João Neves

              Comment

              • someguy03
                New Member
                • Jul 2014
                • 4
                • 4.0.x

                #8
                [QUOTE=rsw686;n317074]I wanted to share this with the community. I wrote this function to fix quotes in imported posts so they wouldn't show the unparsable SMF style quote code and the view post button next to the author's name would work. It uses the importpostid column to lookup the new postid. There are no guarantees on this, so make sure to test it out before doing the real import.

                In the # Posts section of cleaner.php find
                Code:
                 $text = str_replace(array_keys($replacer), $replacer, $post['pagetext']);
                Add after
                Code:
                 $text = parse_quote($text);
                In the # PM text section of cleaner.php find
                Code:
                 $text = str_replace(array_keys($replacer), $replacer, $pm['message']);
                Add after
                Code:
                 $text = parse_quote($text);
                Place this at the bottom of cleaner.php
                Code:
                function parse_quote($post)
                {
                global $Db_target, $targettableprefix;
                
                // no quote tag found, next post
                if(($start = strpos($post, '[quote author=')) === false)
                return $post;
                
                // no end of tag found, next post
                if(($end = strpos($post, ']', $start)) === false)
                return $post;
                
                // grab section to replace
                $found = substr($post, $start, $end - $start + 1);
                echo '<br /><br \>found: ' . $found . "<br \>\n";
                
                $parts = explode(' ', $found);
                for($i=0; $i<count($parts); $i++)
                {
                $part = $parts[$i];
                
                if(($pos = strpos($part, 'author=')) !== false)
                {
                $filter['author'] = substr($part, $pos + 7);
                
                while(strpos($parts[$i + 1], '=') === false && $i < count($parts))
                {
                $i++;
                $filter['author'] .= ' ' . $parts[$i];
                }
                
                $filter['author'] = trim($filter['author']);
                if($filter['author'][strlen($filter['author']) - 1] == ']')
                $filter['author'] = substr($filter['author'], 0, strlen($filter['author']) - 1);
                
                echo 'filtered: author: ' . $filter['author'] . "<br \>\n";
                }
                
                if(($pos = strpos($part, 'link=topic=')) !== false)
                {
                $tmp = explode('#', substr($part, $pos));
                if(isset($post[1]))
                {
                $tmppost = substr($tmp[1], 3);
                echo 'filtered: post: ' . $tmppost . "<br \>\n";
                
                $request = $Db_target->query("
                SELECT postid
                FROM " . $targettableprefix . "post
                WHERE importpostid = " . $tmppost);
                
                if($row = $Db_target->fetch_array($request))
                {
                $filter['post'] = $row['postid'];
                echo 'converted: post: ' . $filter['post'] . "<br \>\n";
                }
                }
                }
                }
                
                if(isset($filter['author']) && isset($filter['post']))
                {
                $replace = '[quote=' . $filter['author'] . ';' . $filter['post'] . ']';
                echo 'replacing: post: ' . $replace . "\n";
                $post = str_replace($found, $replace, $post);
                }
                else if(isset($filter['author']))
                {
                $replace = '[quote=' . $filter['author'] . ']';
                echo 'replacing: author: ' . $replace . "\n";
                $post = str_replace($found, $replace, $post);
                }
                else
                {
                echo "replacing: generic\n";
                $post = str_replace($found, '[quote]', $post);
                }
                
                return parse_quote($post);
                }
                [/quote]

                If interested I also used the following for the replacer varaibles

                Code:
                // Set true or false as to the data you want to clean
                $do_posts = true;
                $do_sigs = true;
                $do_thread_titles = false;
                $do_pm_text = true;
                $do_pm_text_title = false;
                Code:
                $replacer = array(
                "[li]" => "[*]",
                "[/li]" => "",
                "&quot;" => "\"",
                "&amp;" => "&",
                Hi all,

                I ran the quote repair script from cleaner.php and cleared the post cache, but nothing happened. All of my quotes are exactly the same. Does this script no longer work? I am on vBulletin 4.2.2.

                Comment

                • Mark.B
                  vBulletin Support
                  • Feb 2004
                  • 24286
                  • 6.0.X

                  #9
                  The thread is five years old, and the user hasn't been online for four years. He wrote this for vB3. If it doesn't work on vB4 then I'm afraid there's nothing we can do.
                  MARK.B
                  vBulletin Support
                  ------------
                  My Unofficial vBulletin 6.0.0 Demo: https://www.talknewsuk.com
                  My Unofficial vBulletin Cloud Demo: https://www.adminammo.com

                  Comment

                  • someguy03
                    New Member
                    • Jul 2014
                    • 4
                    • 4.0.x

                    #10
                    Originally posted by Mark.B
                    The thread is five years old, and the user hasn't been online for four years. He wrote this for vB3. If it doesn't work on vB4 then I'm afraid there's nothing we can do.
                    Hi Mark,

                    I am aware of the age of the thread. Due to the lack of technical support for Impex (to some degree), I imagine many users must struggle with similar issues. Where do most get support for said issues? Paid support somewhere else? I don't want to just keep opening tickets and bothering the support staff when Impex isn't officially supported.

                    It amazes me that there is no recent threads from other users about this. I am a Wordpress developer, so perhaps I am just used to working with a wider support base. Maybe less people are running forums nowadays than I thought

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...