Ok whole case, updated, I have some code saved and encoded in the database like this:
W1BIUF0KW1ZJRVdJRF0xWy9WSUVXSURdCjw/ICRjb25uID0gbmV3IFBETyggREJfRFNOLCBEQl9VU0VSTkFNRSwgREJfUEFTU1dPUkQgKTsgJHNxbCA9ICJzZWxlY3QgKiBmcm9tIHV6aXZhdGVsZSI7JHN0ID0gJGNvbm4tPnByZXBhcmUoICRzcWwgKTskc3QtPmV4ZWN1dGUoKTskY29ubiA9IG51bGw7IGZvcmVhY2ggKCAkc3QgYXMgJHN0cmVzdWx0ICkgeyA/Pgo8PyBlY2hvICRzdHJlc3VsdFsiaWQiXTs/Pgo8PyB9ID8+ClsvUEhQXQoKCgoKCgoKCgoKCgoKCgo=
decoded result is this:
[PHP]
[VIEWID]1[/VIEWID]
<? $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "select * from uzivatele";$st = $conn->prepare( $sql );$st->execute();$conn = null; foreach ( $st as $stresult ) { ?>
<? echo $stresult["id"];?>
<? } ?>
[/PHP]
Then I have root file where I download all template parts that are encoded like I stated, I decode them, take the code inside [PHP][/PHP] and create a seperate phpview file that generates some content and that content I need to be swapped for the previous code that was inside [PHP][/PHP]. Of course If I generate from that php file another html file I can use file_get_contents, but I would like to find approach with just the one php file
<?
if ( !defined( 'DB_DSN' ) ) {require( "./../../pripojeni.php" );}
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
function get_string_between( $string, $start, $end ) {
$string = ' ' . $string;
$ini = strpos( $string, $start );
if ( $ini == 0 ) return '';
$ini += strlen( $start );
$len = strpos( $string, $end, $ini ) - $ini;
return substr( $string, $ini, $len );
}
function replace_between($str, $needle_start, $needle_end, $replacement) {
$pos = strpos($str, $needle_start);
$start = $pos === false ? 0 : $pos + strlen($needle_start);
$pos = strpos($str, $needle_end, $start);
$end = $pos === false ? strlen($str) : $pos;
return substr_replace($str, $replacement, $start, $end - $start);
}
$aktivniverzeproaktivniobsah = $_GET[ "id" ];
$sqlaktivniobsah = "select tp.* from template tp left join sekce sk on sk.template=tp.id left join verze vz on vz.id=sk.kverzi where vz.id=$aktivniverzeproaktivniobsah order by sk.poradi ASC";
$staktivniobsah = $conn->prepare( $sqlaktivniobsah );
$staktivniobsah->execute();
$conn = null;
$contentdecoded="";
foreach ( $staktivniobsah as $aktivniobsah ) {
$idcko = $aktivniobsah[ 'id' ];
$contentdecoded .= base64_decode( $aktivniobsah[ "obsah" ] );
}
if ( str_contains( $contentdecoded, '[PHP]' ) ) {
$str = get_string_between( $contentdecoded, '[PHP]', '[/PHP]' );
$idview = get_string_between( $contentdecoded, '[VIEWID]', '[/VIEWID]' );
if ( !empty( $idview )and!empty( $str ) ) {
$fp = fopen( 'wms/admin/views/view' . $idview . '.php', 'w' );
fwrite( $fp, $str );
fclose( $fp );
}
$dokument=file_get_contents( 'wms/admin/views/view' . $idview . '.php' );
$contentdecoded=replace_between($contentdecoded, '[PHP]', '[/PHP]', $dokument );
}
echo $contentdecoded;
?>
2
Answers
You can either change
view$idview.php
to be a function which returns a string or you can use Output Buffering to capture the output and render it where needed. Whatrequire
does is that it loads and executes (if any executable code is present) the file, therefore the things appear above the replace. Also note thatrequire
doesn’t seem to have any output value, so storing it into a$document
variable will assign probably nothing.Use file_get_contents instead of require to get the contents of a file.