PHP post删除样式属性

时间:2013-02-17 19:10:59

标签: javascript jquery ajax codeigniter styles

我有一个奇怪的情况,我似乎无法在谷歌找到答案。

我正在使用javascript数组,将JSON.stringify应用于它,然后通过AJAX发布到php控制器,以便将现在的json_encoded数组存储在表中。在通过ajax发布后,$ _POST以某种方式剥离了正在提交的html上的styles属性。

以下是通过javascript / jquery抓取的示例html:

<"div class="blahblah" style="border:1px solid #000000;"><strong>test</strong></div>

以下是AJAX邮政编码:

var post_data = [];
    $("divclasshere").each(function(){
        post_data.push({html:$(this).html()});
    });
    var data = JSON.stringify(post_data); 
    $.ajax({
        type: "POST",
        url: "save",
        data: { content: data },
        success: function(result){
        }
    });

这是控制器功能,将其保存到db:

$data = array(
    'content' => $this->input->post('content')
);
$this->db->update('table', $data);

如果我在PHP控制器上的数据上打印__,我得到(例子)

<div class="blahblah"><strong>test</strong></div>

但div类没有样式属性=&#34; blahblah&#34;元件。如果有所作为,我正在使用CodeIgniter?在某些情况下,它剥离第一部分:style =&#34; border:1px并留下实线#000000;&#34;

编辑:

以下是发布的内容(例如):

content:[{"html":"<div class=\"content\" style=\"border:1px solid #000000;\"></div>"}]

以下是print_r&#39; d:

<pre>[{"html":"<div class=\"content\"  solid #000000;\"></div>"}]

1 个答案:

答案 0 :(得分:3)

核心_remove_evil_attributes函数从标记中删除样式属性。为了解决这个问题,你有一个解决方法。只需在应用程序的核心目录( application / core / MY_security.php )中创建一个文件名My_Security.php,然后将以下代码粘贴到其中以覆盖默认功能。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Security extends CI_Security {
    function __construct()
    {
      parent::__construct();
    }

    // --------------------------------------------------------------------

    /*
        * Modified for cb_cms
     */
    protected function _remove_evil_attributes($str, $is_image)
    {
        // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
        $allowed = array("your allowed url's without domain like '/admin/edittext/'");
        if(in_array($_SERVER['REQUEST_URI'],$allowed)){
            $evil_attributes = array('on\w*', 'xmlns');
        }else{
            $evil_attributes = array('on\w*', 'style', 'xmlns');
        }

        if ($is_image === TRUE)
        {
            /*
             * Adobe Photoshop puts XML metadata into JFIF images, 
             * including namespacing, so we have to allow this for images.
             */
            unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
        }

        do {
            $str = preg_replace(
                "#<(/?[^><]+?)([^A-Za-z\-])(".implode('|', $evil_attributes).")(\s*=\s*)([\"][^>]*?[\"]|[\'][^>]*?[\']|[^>]*?)([\s><])([><]*)#i",
                "<$1$6",
                $str, -1, $count
            );
        } while ($count);

        return $str;
    }

} 
?>
相关问题