We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3232
    • 380 Posts
    I am creating a plugin to modify content before it is saved. I was having problems with the final output and finally decided to see what the content looked like right before it was processed.

    This is the code that was added in the tinymce content editor:

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a risus. Quisque accumsan ligula ac neque. Curabitur a turpis. Fusce nisi. Quisque adipiscing. Sed quis diam sed felis lacinia accumsan. Aliquam adipiscing. Proin vehicula convallis ante. Nulla orci lacus, eleifend sed, ultricies non, porttitor at, leo. Nunc est nibh, varius at, eleifend et, consectetur non, leo. <a href="http://google.com">Nulla faucibus orci et nisl</a>. Proin bibendum felis.</p>


    This is the code right before it is processed:

    <p>\r\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a risus.\r\nQuisque accumsan ligula ac neque. Curabitur a turpis. Fusce nisi.\r\nQuisque adipiscing. Sed quis diam sed felis lacinia accumsan. Aliquam\r\nadipiscing. Proin vehicula convallis ante. Nulla orci lacus, eleifend\r\nsed, ultricies non, porttitor at, leo. Nunc est nibh, varius at,\r\neleifend et, consectetur non, leo. <a href="%5C%22http://google.com%5C%22">Nulla faucibus orci et nisl</a>. Proin bibendum felis.\r\n</p>


    Notice the %5C%22 surrounding the href destination? Where did these characters come from? it is causing problems for my plugin. Is there a way to modify the content string so that it will just display the url?
      • 3749
      • 24,544 Posts
      TinyMCE is adding the extraneous material. You can most likely change the TinyMCS configuration so that it doesn’t do that or you can just turn the RTE off and edit the raw HTML which will be saved as is.

      Here’s some info on configuring TinyMCE:

      http://wiki.modxcms.com/index.php/How_to_Get_TinyMCE_to_Format_HTML_Source_Correctly_in_TinyMCE_2.0.6.1

      http://wiki.moxiecode.com/index.php/TinyMCE:Configuration
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 10449
        • 956 Posts
        FYI, %5C%22 is URL-encoded \" (backslash + double quote).
        -> http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
          • 3232
          • 380 Posts
          Quote from: ganeshXL at May 03, 2009, 12:15 PM

          FYI, %5C%22 is URL-encoded \" (backslash + double quote).
          -> http://www.blooberry.com/indexdot/html/topics/urlencoding.htm


          Is there something I can do to decode the content so I can modify it and then re-encode it?
            • 3232
            • 380 Posts
            Quote from: BobRay at May 03, 2009, 12:10 PM

            TinyMCE is adding the extraneous material. You can most likely change the TinyMCS configuration so that it doesn’t do that or you can just turn the RTE off and edit the raw HTML which will be saved as is.

            Here’s some info on configuring TinyMCE:

            http://wiki.modxcms.com/index.php/How_to_Get_TinyMCE_to_Format_HTML_Source_Correctly_in_TinyMCE_2.0.6.1

            http://wiki.moxiecode.com/index.php/TinyMCE:Configuration

            I disabled the TinyMCE plugin and get the same results, it’s not the editor. Modx is doing some sort of encoding before the OnBeforeDocFormSaved is called. Is there anyway to "undo" this and then when I am done modifying it I can re-encode it?
              • 10449
              • 956 Posts
              Well, \" is essentially what addslashes() does. Because of all the JS/Ajax stuff going on in the manager, maybe the URL-encoding is necessary.
              Try stripslashes() + urldecode()?

              If you want to continue using RTE, try setting tinyMCE entity encoding to "raw" in the plugin settings.
                • 3232
                • 380 Posts
                Quote from: ganeshXL at May 03, 2009, 12:55 PM

                Well, \" is essentially what addslashes() does. Because of all the JS/Ajax stuff going on in the manager, maybe the URL-encoding is necessary.
                Try stripslashes() + urldecode()?

                If you want to continue using RTE, try setting tinyMCE entity encoding to "raw" in the plugin settings.

                This helped greatly, using stripslashes() and addslashes() I remove the \" problem.

                Now I have one last problem, it seems that if there are line returns in the editor, (with or without rte) modx translates these to \r \n which when the slashes are remove changes to rn and since the slashes don’t get added back with them, it remains in the content.

                Is there a way I can just remove these "\r\n"?

                I have tried $content = ereg_replace(’\r\n’," ",$content); but that doesn’t remove them.

                Thanks
                Brian
                  • 30223
                  • 1,010 Posts
                  First of all using ereg here is inappropriate, a simple str_replace would be much faster.Furthermore in an ereg the backslashes have special meaning so for ereg_replace ’\r\n’ is not the same a the string ’\r\n’.

                  The ’\r\n’ are a result of the content being escaped with mysql_real_escape_string (via $modx->db->escape).

                  You can try two things...
                  1. Use $content=str_replace(’\r\n’,"\r\n",$content);
                  This will at least let php recognize them as proper newline characters when you apply stripslashes().

                  2. Act on the raw posted data instead of $content.
                  <?php
                  global $content;
                  
                  //get raw posted content
                  $myContent = $_POST['ta'];
                  //your plugin processing here...
                  $content = $modx->db->escape($myContent);
                  //get out of here....
                  ?>
                  

                  The disadvantage (and it is a big one) is that if you have more than one plugin acting on the OnBeforeDocFormSave event the results may not be what you expect.
                    • 3749
                    • 24,544 Posts
                    Quote from: betoranaldi at May 03, 2009, 12:42 PM

                    Quote from: BobRay at May 03, 2009, 12:10 PM

                    TinyMCE is adding the extraneous material. You can most likely change the TinyMCS configuration so that it doesn’t do that or you can just turn the RTE off and edit the raw HTML which will be saved as is.

                    Here’s some info on configuring TinyMCE:

                    http://wiki.modxcms.com/index.php/How_to_Get_TinyMCE_to_Format_HTML_Source_Correctly_in_TinyMCE_2.0.6.1

                    http://wiki.moxiecode.com/index.php/TinyMCE:Configuration

                    My bad. I would have bet it was TinyMCE. embarrassed

                    I disabled the TinyMCE plugin and get the same results, it’s not the editor. Modx is doing some sort of encoding before the OnBeforeDocFormSaved is called. Is there anyway to "undo" this and then when I am done modifying it I can re-encode it?
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 3232
                      • 380 Posts
                      Quote from: TobyL at May 03, 2009, 08:20 PM

                      First of all using ereg here is inappropriate, a simple str_replace would be much faster.Furthermore in an ereg the backslashes have special meaning so for ereg_replace ’\r\n’ is not the same a the string ’\r\n’.

                      The ’\r\n’ are a result of the content being escaped with mysql_real_escape_string (via $modx->db->escape).

                      You can try two things...
                      1. Use $content=str_replace(’\r\n’,"\r\n",$content);
                      This will at least let php recognize them as proper newline characters when you apply stripslashes().

                      2. Act on the raw posted data instead of $content.
                      <?php
                      global $content;
                      
                      //get raw posted content
                      $myContent = $_POST['ta'];
                      //your plugin processing here...
                      $content = $modx->db->escape($myContent);
                      //get out of here....
                      ?>
                      

                      The disadvantage (and it is a big one) is that if you have more than one plugin acting on the OnBeforeDocFormSave event the results may not be what you expect.


                      Option 1 worked great. I now understand how this is working (mysql_real_escape_string was the culprit.)

                      Thank you for taking the time to go through all of this. It is much appreciated.

                      And thank you to everyone else who posted to this thread, everyone was a great help!