File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ function dolStripPhpCode($str, $replacewith = '')
7676function dolKeepOnlyPhpCode ($ str )
7777{
7878 $ str = str_replace ('<?= ' , '<?php ' , $ str );
79+ $ str = str_replace ('<?php ' , '__LTINTPHP__ ' , $ str );
80+ $ str = str_replace ('<? ' , '<?php ' , $ str ); // replace the short_open_tag. It is recommended to set this is Off in php.ini
81+ $ str = str_replace ('__LTINTPHP__ ' , '<?php ' , $ str );
7982
8083 $ newstr = '' ;
8184
Original file line number Diff line number Diff line change @@ -226,4 +226,27 @@ public function testCheckPHPCode()
226226 print __METHOD__ ." result checkPHPCode= " .$ result ."\n" ;
227227 $ this ->assertEquals ($ result , 1 , 'checkPHPCode did not detect the string was dangerous ' );
228228 }
229+
230+ /**
231+ * testDolKeepOnlyPhpCode
232+ *
233+ * @return void
234+ */
235+ public function testDolKeepOnlyPhpCode ()
236+ {
237+ $ s = 'HTML content <?php exec("eee"); ?> and more HTML content ' ;
238+ $ result = dolKeepOnlyPhpCode ($ s );
239+ print __METHOD__ ." result dolKeepOnlyPhpCode= " .$ result ."\n" ;
240+ $ this ->assertEquals ('<?php exec("eee"); ?> ' , $ result , 'dolKeepOnlyPhpCode did extract the correct string ' );
241+
242+ $ s = 'HTML content <? exec("eee"); ?> and more HTML content ' ;
243+ $ result = dolKeepOnlyPhpCode ($ s );
244+ print __METHOD__ ." result dolKeepOnlyPhpCode= " .$ result ."\n" ;
245+ $ this ->assertEquals ('<?php exec("eee"); ?> ' , $ result , 'dolKeepOnlyPhpCode did extract the correct string ' );
246+
247+ $ s = 'HTML content <?php test() <?php test2(); ?> and more HTML content ' ;
248+ $ result = dolKeepOnlyPhpCode ($ s );
249+ print __METHOD__ ." result dolKeepOnlyPhpCode= " .$ result ."\n" ;
250+ $ this ->assertEquals ('<?php test() ?><?php test2(); ?> ' , $ result , 'dolKeepOnlyPhpCode did extract the correct string ' );
251+ }
229252}
You can’t perform that action at this time.
0 commit comments