Skip to content

Commit 0ed6a63

Browse files
committed
1 parent 119f6b6 commit 0ed6a63

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

htdocs/core/lib/website.lib.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ function dolStripPhpCode($str, $replacewith = '')
7676
function 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

test/phpunit/WebsiteTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)