Announcement

Collapse
No announcement yet.

SMF 2 Quote Cleaner Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #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 View Post
    The Reptile File

    Comment


    • #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


      • #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


        • #5
          no one to help?

          Comment


          • #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


            • #7
              Hello Joe,

              Thanks for your response.

              What i mean is this:

              http://comunidade.apostaganha.pt/for...ng-19-05-18h30


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


              Regards,

              Joćo Neves

              Comment


              • #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, '
                ', $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;" => "&",
                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


                • #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

                  TalkNewsUK - My vBulletin 5.5.4 Demo
                  AdminAmmo - My Cloud Demo

                  Comment


                  • #10
                    Originally posted by Mark.B View Post
                    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...
                    X