Преобразуются двойные ковычки в " и <> в > при записи в файл

Рейтинг: 0Ответов: 1Опубликовано: 26.04.2023

Почему то преобразуются двойные ковычки "" в &quot; и <> в &gt; при записи в файл.

Подскажите пожайлуста. Как заставить записывать ковычки (одиночные, двойные) и стрелочки вместо &quot;,&gt; в текстовый файл на сервере?

Строка 16 - $htacces = str_replace("&amp;", "&", $this->request->post['htacces']);

При добавлении $htacces = str_replace("&amp;", "&", "&quot;", "\"", "&lt;", "<", "&gt;", ">", $this->request->post['htacces']); выдает ошибу - Only variables can be passed by reference in

<?php 
class ControllerModuleGixochtacces extends Controller {
    private $error = array();

    public function index() {
        $this->language->load('module/gixochtacces');

        $this->document->setTitle($this->language->get('text_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            if(isset($this->request->post['htacces'])){
                $file = str_replace("system/", "", DIR_SYSTEM) . '.htaccess';

                $handles = fopen($file, 'w+'); 

            $htacces = str_replace("&amp;", "&", $this->request->post['htacces']);
                
                fwrite($handles, $htacces);

                fclose($handles);

                $this->session->data['success'] = $this->language->get('text_success');

                $this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
            }
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_edit'] = $this->language->get('text_edit');

        $this->data['entry_create'] = $this->language->get('entry_create');
        $this->data['entry_clean'] = $this->language->get('entry_clean');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),            
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_module'),
            'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_title'),
            'href'      => $this->url->link('module/gixochtacces', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('module/gixochtacces', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

        $file = str_replace("system/", "", DIR_SYSTEM) . '.htaccess';

        if (file_exists($file)) {
            $this->data['text'] = file_get_contents($file, FILE_USE_INCLUDE_PATH, null);
        } else {
            $this->data['text'] = '';
        }

        $this->template = 'module/gixochtacces.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function getUrl($route){
        $url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
        if ($this->config->get('config_seo_url')) {
            require_once(DIR_CATALOG . 'controller/common/seo_url.php');
            $rewriter = new ControllerCommonSeoUrl($this->registry);
            $url->addRewrite($rewriter);
        }
        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
            $domain = HTTPS_CATALOG;
        } else {
            $domain = HTTP_CATALOG;
        }
        
        return str_replace($domain, "", $url->link($route));
    }

    public function create_htacces(){
        $text = "1111";

        $this->response->setOutput($text);
    }

    protected function validate() {
        if (!$this->user->hasPermission('modify', 'module/gixochtacces')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        
        if (!$this->error) {
            return true;
        } else {
            return false;
        }   
    }
}
?>

Ответы

▲ 0Принят

Очень похоже, что у вас где-то внутри движка делается обработка поступающих данных и они прогоняются через функцию htmlentities() и т.п. функции.

Видимо сделано это в целях "безопастности".

htmlentities() как переводит всякие символы "&", "<", ">" в их HTML коды.

В вашем случае нужно переконвертировать строку обратно.

Для этого можно использовать функцию html_entity_decode()

Либо можно, как вы это пытаетесь сделать, использовать str_replace(). Чтобы c помощью str_replace изменять сразу несколько подстрок, нужно передавать их как массив. В вашем случае это будет выглядеть примерно вот так

$htacces = str_replace(
    ["&amp;","&quot;", "&lt;", "&gt;"],
    ["&", "\"", "<", ">"], 
    $this->request->post['htacces']
);

Первым параметром передается массив строк, которые надо найти, а вторым массив строк на которые надо поменять.