Как включить файл, отмеченный в строке в PHP? - PullRequest
1 голос
/ 12 апреля 2020

У меня есть строка, в которую пользователь может добавить специальную отметку, например [include=example_file], и я хотел бы включить «example_file. php» в место, где находится отметка.

Пользовательский текст может быть похожим на:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industrys standard dummy text ever since the 1500s.

[include=contact_form] // here should be contact_form.php included

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industrys standard dummy text ever since the 1500s

Я использую это, чтобы отобразить текст пользователя, теперь:

<?php echo $this->view['users_text']; ?>

... и новый вывод должен быть таким:

<?php
// echo first paragraph
include "contact_form.php";
// echo second paragraph
?>

Как проще всего включить файлы, отмеченные этими специальными отметками?

1 Ответ

1 голос
/ 12 апреля 2020

Будьте осторожны с атаками LFI, но для этого сопоставьте заполнитель с регулярным выражением, l oop над ним, используйте значение для загрузки файла в буфер, а затем замените заполнитель буфером.

Как это:

<?php
$content = '
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industrys standard dummy text ever since the 1500s.

[include=contact_form] // here should be contact_form.php included

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industrys standard dummy text ever since the 1500s

[include=cat] // here should be cat.php included
';

// match 
if (preg_match_all('#\[include=(?P<name>.*?)\]#', $content, $includes)) {
    // loop
    foreach($includes['name'] as $include) {
        // buffer
        ob_start();
        //include($include.'.php'); // ?
        echo 'I was from '.$include.'.php';
        $buffer = ob_get_clean();

        // replace
        $content = str_replace('[include='.$include.']', $buffer, $content);
    }
}

echo $content;

https://3v4l.org/qPqbI

...