We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    This is what I had in mind (untested):


    $token = '~!@MyToken~!@';
    
    switch ($modx->event->name) {
        case 'OnParseDocument':
            $resource->_output = str_replace('[[', $token, $resource->_output );
            break;
    
        case 'OnWebPagePrerender':
            $resource->_output = str_replace($token, '[[', $resource->_output);
            break;
    }
      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
      • 38237
      • 83 Posts
      Thanks Bob,

      I've been trying to write a plugin for few hours now, but no luck, now i figured out that nothing happens on "OnWebPagePrerender" event, I even put
      $modx->documentOutput = "Hello";
      $resource->_output = "NOTHING";

      Also tried "OnWebPageComplete" event, but no luck. However, "OnParseDocument" event works, and i could successfully replace the square brackets and got the PDF in full length.
        • 3749
        • 24,544 Posts
        That's very strange. I've used $modx->resource->_output many times in OnWebPagePrerender for things like this with no trouble.

        I don't know why that wouldn't work for you. Is there any chance you're using 'OnWebPagePreRender" or some other variant spelling?
          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
          • 38237
          • 83 Posts
          Mission accomplished

          Thanks Bob, Susan

          Bob this is dedicated to you smiley

          Made out of your "FixedPre" Add-On with some modification according to your instructions.

          <?php
          switch ($modx->event->name) {
              case 'OnParseDocument':
          	  if (! function_exists('quote_meta') ) {
          		function quote_meta($a) {
          		  $lhs = array("[", "]");
          		  $rhs = array("091X2", "092X2");
          		  $b = str_replace("&", "\46", $a[2]);  //save "&"
          		  $lhs_preg = array('|<!(!*)notag>|',  '|<!(!*)/notag>|');
          		  $rhs_preg = array('<$1notag>',  '<$1/notag>');
          		  $b = preg_replace($lhs_preg, $rhs_preg, $b);
          		  $b = str_replace($lhs, $rhs, $b);
          		  return str_replace("\46", "093X", $b);
          			  }
          		  }
          	  $output =& $modx->documentOutput;
          	  $output = preg_replace_callback("#(<notag>)(.*?)(</notag>)#s", "quote_meta", $output);
          	break;
              case 'OnWebPagePrerender':
          	  $output = &$modx->resource->_output; // get a reference to the output
          	  $output = str_replace('091X2','[',$output);
          	  $output = str_replace('092X2',']',$output);
          	  $output = str_replace('093X','&',$output);
          	break;
          }


          However, still not passing all files, some files are now returned blank. The best option is to have a feature request to have a special tag like the comment, but for not processing any symbols or brackets.
            • 3749
            • 24,544 Posts
            I'm glad you got it sorted. smiley

            Is there anything different about the files that are returned blank?

            Have you tried using a Static Resource with no template? That might bypass the parser.
              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
              • 38237
              • 83 Posts
              Hi Bob,

              There was some other processing(validating) & stipping on the downloaded files, so I found the best way to download a file with modx is to create an external PHP file placed in the manager folder adding user validation in it, here is it just in case it is useful for others:


              <?php
              if (!defined('MODX_CORE_PATH'))  { define('MODX_CORE_PATH', '/PATH_TO_CORE/'); }
              if (!defined('MODX_CONFIG_KEY')) { define('MODX_CONFIG_KEY', 'config'); }
              require_once( MODX_CORE_PATH . 'model/modx/modx.class.php');
              $modx = new modx();
              $modx->initialize('mgr');
              //=============================
              
              // Validate user membership & session
              $user = $modx->getUser();
              if ( ($user->isMember('Administrator') || $user->isMember('Editors')) && $user->hasSessionContext('mgr') )
              {
              	$id = $_GET['id'];
              	$table = str_replace( 'modx', 'custom', $modx->getOption('table_prefix') ) . files;
              	$sql = "SELECT `file_name`, `file_size`, `file_type`, `file_data` FROM `$table` where `id`=$id LIMIT 1" ;
              	$query = $modx->query($sql);
              	
              	if ($query) { $row = $query->fetch(PDO::FETCH_ASSOC); }
              	$name = $row['file_name'];
              	$size = $row['file_size'];
              	$type = $row['file_type'];
              	$file = $row['file_data'];
              	
              	//header("Content-Disposition: attachment; filename=$name"); //Uncomment for download otherwise browser opens PDF in beowser
              	header("Content-length: $size");
              	header("Content-type: $type");
              	echo $file;		
              }
              ?>
                • 3749
                • 24,544 Posts
                Looks good! smiley
                  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
                  • 38237
                  • 83 Posts
                  Haven't tested yet, but now 2.3.x has PDF file type, hope the header-disposition can be modified per resource.