dede cfg replacestr怎么用?

99ANYc3cd6
预计阅读时长 22 分钟
位置: 首页 DEDE建站 正文

Of course. Let's break down cfg_replacestr in DedeCMS (织梦内容管理系统).

dede cfg replacestr
(图片来源网络,侵删)

This is a powerful but often misunderstood configuration setting used for security and content filtering.


What is cfg_replacestr?

cfg_replacestr is a configuration variable located in the dede/sys_config.php file. It's an array used to define a list of "bad words" or sensitive keywords that you want to automatically find and replace within user-submitted content.

Its primary purpose is to act as a simple, built-in content filter and security measure to prevent users from posting malicious scripts, spam, or other undesirable content.


How It Works

When DedeCMS processes content that a user submits (e.g., through a comment, a guestbook entry, or a form), it checks the content against the list of keywords defined in cfg_replacestr.

dede cfg replacestr
(图片来源网络,侵删)

For each keyword it finds, it replaces it with the corresponding replacement string you've defined.

The Logic: $cfg_replacestr is an associative array where:

  • Key ('keyword'): The word or phrase you want to find.
  • Value ('replacement'): The string you want to replace the keyword with.

Example: If you have this in your cfg_replacestr:

$cfg_replacestr = array(
    '黑客' => 'H**K',
    '病毒' => 'V**S',
    '<script>' => '&lt;script&gt;'
);

And a user submits the comment: “小心黑客,他们可能传播病毒!<script>alert('xss')</script>”

dede cfg replacestr
(图片来源网络,侵删)

DedeCMS will process it and change it to: “小心H**K,他们可能传播V**S!&lt;script&gt;alert('xss')&lt;/script&gt;”


Where is it Used? (The Hook)

The cfg_replacestr array is not applied everywhere automatically. It's specifically used by a core function called FilterSearch().

This function is typically called in the following scenarios:

  1. Comments (评论): When a user submits a comment on an article or a picture.
  2. Guestbook (留言簿): When a user signs the guestbook.
  3. Member/Member Model Content (会员/会员模型内容): When members submit content through their own publishing areas.
  4. Other User-Submitted Forms: Any custom form that is designed to use this filtering mechanism.

You can find the FilterSearch() function in /include/helpers/filter.helper.php.


Practical Examples

Let's look at some common use cases for cfg_replacestr.

Example 1: Filtering Profanity and Sensitive Words

This is the most common use case. You want to block bad words.

// In dede/sys_config.php
$cfg_replacestr = array(
    '傻逼'   => '***',
    '脑残'   => '文明用语',
    '操'     => '@#$%',
    'TMD'    => '文明用语'
);

Example 2: Preventing Cross-Site Scripting (XSS)

This is a critical security measure. You want to prevent users from injecting malicious JavaScript.

// In dede/sys_config.php
$cfg_replacestr = array(
    '<script'   => '&lt;script',
    '</script>' => '&lt;/script&gt;',
    'javascript:' => 'java script:',
    'onload='    => 'on load='
);

Note: Simply replacing <script> with &lt;script&gt; is a good first step, but advanced XSS attacks can use other HTML tags and attributes. A more robust solution involves a dedicated security library, but this is a basic DedeCMS-level protection.

Example 3: Blocking Spam Keywords

Stop users from posting specific spam phrases.

// In dede/sys_config.php
$cfg_replacestr = array(
    'QQ群'      => '联系群',
    '加微信'    => '联系微信',
    '淘宝店'    => '我的店铺',
    'Viagra'    => 'V**A'
);

How to Modify cfg_replacestr

Warning: Always back up your files before making changes.

  1. Connect to your server via FTP or use the file manager in your hosting control panel.
  2. Navigate to the DedeCMS root directory.
  3. Open the file: /dede/sys_config.php.
  4. Find the $cfg_replacestr array. It might be empty, or it might already have some entries.
  5. Add your own keywords and replacements in the array(...) format.
  6. Save the file.
  7. Clear the DedeCMS cache for the changes to take effect. You can usually do this in the system's "System" -> "Clear Cache" section of the backend.

Important Considerations and Limitations

  • It's Not a Silver Bullet: cfg_replacestr is a basic filter. It can be bypassed by skilled attackers (e.g., using case variations like <SCRIPT>, or encoding). For a high-traffic or security-sensitive site, you should consider more advanced solutions.
  • Performance: Having an extremely long list of keywords can have a minor impact on performance, as every piece of user-submitted content must be scanned. For most sites, this is negligible.
  • Case Sensitivity: The default string replacement is case-sensitive. The word "Hack" and "hack" would be treated as different words. To make it case-insensitive, you would need to modify the FilterSearch() function itself, which is more advanced.
  • Word Boundaries: The filter looks for the exact string. If you block "hack", it will also block "hacker" because "hack" is contained within it. This is usually the desired behavior, but be aware of it.

Summary

Feature Description
What is it? A security/content filter array in dede/sys_config.php.
Purpose To automatically find and replace "bad words" in user-submitted content.
How it Works It's an associative array: ['keyword' => 'replacement'].
Where it's Used Primarily in comments, guestbooks, and member submissions via the FilterSearch() function.
Common Uses Blocking profanity, preventing basic XSS attacks, and stopping spam.
How to Edit Modify the array in /dede/sys_config.php and clear the cache.
Limitation It's a basic filter and can be bypassed. Not a substitute for a comprehensive security strategy.
-- 展开阅读全文 --
头像
织梦查看gbk版本 utf-8版本
« 上一篇 12-07
dede arcpagelist 样式
下一篇 » 12-07

相关文章

取消
微信二维码
支付宝二维码

目录[+]