Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Sep 4, 2023
1 parent 119f6b6 commit 0ed6a63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions htdocs/core/lib/website.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ function dolStripPhpCode($str, $replacewith = '')
function dolKeepOnlyPhpCode($str)
{
$str = str_replace('<?=', '<?php', $str);
$str = str_replace('<?php', '__LTINTPHP__', $str);
$str = str_replace('<?', '<?php', $str); // replace the short_open_tag. It is recommended to set this is Off in php.ini
$str = str_replace('__LTINTPHP__', '<?php', $str);

$newstr = '';

Expand Down
23 changes: 23 additions & 0 deletions test/phpunit/WebsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,27 @@ public function testCheckPHPCode()
print __METHOD__." result checkPHPCode=".$result."\n";
$this->assertEquals($result, 1, 'checkPHPCode did not detect the string was dangerous');
}

/**
* testDolKeepOnlyPhpCode
*
* @return void
*/
public function testDolKeepOnlyPhpCode()
{
$s = 'HTML content <?php exec("eee"); ?> and more HTML content';
$result = dolKeepOnlyPhpCode($s);
print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
$this->assertEquals('<?php exec("eee"); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');

$s = 'HTML content <? exec("eee"); ?> and more HTML content';
$result = dolKeepOnlyPhpCode($s);
print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
$this->assertEquals('<?php exec("eee"); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');

$s = 'HTML content <?php test() <?php test2(); ?> and more HTML content';
$result = dolKeepOnlyPhpCode($s);
print __METHOD__." result dolKeepOnlyPhpCode=".$result."\n";
$this->assertEquals('<?php test() ?><?php test2(); ?>', $result, 'dolKeepOnlyPhpCode did extract the correct string');
}
}

0 comments on commit 0ed6a63

Please sign in to comment.