Guest User

Untitled

a guest
Jun 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. <?php
  2. /****
  3.  
  4. This plugin escapes modx tags that are enclosed in %EscapeModx% %/EscapeModx% tags.
  5.  
  6. eg in a resource content in a RTE:
  7. %EscapeModx%
  8. [[!AndIf?
  9. &condition=`[[+description]]%%notempty%%1||[[+urlLink]]%%notempty%%1`
  10. &operator=`OR`
  11. &operatorSeparator = `%%`
  12. &then=`this is an example`
  13. ]]
  14. %/EscapeModx%
  15.  
  16. The following events should be set:
  17. - OnBeforeDocFormSave
  18. - OnDocFormRender
  19. - OnWebPagePrerender
  20. ***/
  21.  
  22. $eventName = $modx->event->name;
  23. $escapeModxKeyIn = '%EscapeModx%';//escape tag in
  24. $escapeModxKeyOut = '%/EscapeModx%';//escape tag out
  25. $escapeModxTags_debug = false;
  26.  
  27. switch ($eventName) {
  28. /*** OnBeforeDocFormSave ***/
  29. case 'OnBeforeDocFormSave' :
  30. $output = $resource->get('content');
  31. if ($escapeModxTags_debug) if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags-------OnBeforeDocFormSave-----".$output);
  32.  
  33. if (strpos($output, $escapeModxKeyIn) === false) return;
  34.  
  35. preg_match_all('/'.preg_quote($escapeModxKeyIn,'/').'(.*?)'.preg_quote($escapeModxKeyOut,'/').'/ims', $output, $matches);
  36.  
  37. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnBeforeDocFormSave--------".print_r($matches,true));
  38.  
  39. foreach ($matches[1] as $key => $match) {
  40. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnBeforeDocFormSave--------".$output);
  41. $match64 = base64_encode($match);
  42. $output=str_replace($escapeModxKeyIn.$match.$escapeModxKeyOut,$escapeModxKeyIn.'Encoded64'.$match64.$escapeModxKeyOut,$output);
  43. }
  44. $resource->set('content', $output);
  45. break;
  46.  
  47. /*** OnDocFormRender ***/
  48. case 'OnDocFormRender' :
  49. $output = $resource->get('content');
  50. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnDocFormRender--------".$output);
  51.  
  52. if (strpos($output, $escapeModxKeyIn) === false) return;
  53.  
  54. preg_match_all('/'.preg_quote($escapeModxKeyIn,'/').'Encoded64(.*?)'.preg_quote($escapeModxKeyOut,'/').'/ims', $output, $matches);
  55.  
  56. foreach ($matches[1] as $key => $match64) {
  57. $match = base64_decode($match64);
  58. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnDocFormRender--------".$match);
  59. $output=str_replace($escapeModxKeyIn.'Encoded64'.$match64.$escapeModxKeyOut,$escapeModxKeyIn.$match.$escapeModxKeyOut,$output);
  60. }
  61. $resource->set('content', $output);
  62.  
  63. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnDocFormRender--content------".$resource->get('content').print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,15) ,true));
  64. break;
  65.  
  66. /*** OnWebPagePrerender ***/
  67. case 'OnWebPagePrerender' :
  68. $output = $modx->resource->_output;
  69. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnWebPagePrerender--------".$output);
  70.  
  71. if (strpos($output, $escapeModxKeyIn) === false) return;
  72.  
  73. preg_match_all('/'.preg_quote($escapeModxKeyIn,'/').'Encoded64(.*?)'.preg_quote($escapeModxKeyOut,'/').'/ims', $output, $matches);
  74.  
  75. foreach ($matches[1] as $key => $match64) {
  76. $match = base64_decode($match64);
  77. $output=str_replace($escapeModxKeyIn.'Encoded64'.$match64.$escapeModxKeyOut,$match,$output);
  78. }
  79. $modx->resource->_output = $output;
  80.  
  81. if ($escapeModxTags_debug) $modx->log(MODX_LOG_LEVEL_DEBUG,"--> escapeModxTags----OnWebPagePrerender--content------".$modx->resource->_output);
  82.  
  83. break;
  84. }
Add Comment
Please, Sign In to add comment