手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:utf8

drupal的db_query安全过滤

这篇 文章已经是07年的了,不过,我一直没有关注过drupal的db类,所以对于这方面我也根本 就没有在意过。8过,我在我的siterpc中,我也是采用了sprintf的写法,不是为了安全,而是为了格式化。因为在siterpc里我们几乎不接受外部变量,所有的变量都是由我们自己传入,所以对于安全方便忽略了很多。但还是用sprintf来格式化SQL语句了。

因此看到这篇drupal的db_query安全过滤,突然发现,可以让我的代码少写很多了。(参数可变时请使用vsprintf)

以下是原文 :

Drupal通过C风格的字符串输出格式实现了对sql语句的安全过滤。
使用方法:

PHP代码
  1. db_query("SELECT n.nid FROM {node} n WHERE n.type = '%s'"$type);//正确做法  
  2. //这不等于以下语句,使用sprintf并不能避免mysql注入。  
  3. db_query(sprintf("SELECT n.nid FROM {node} n WHERE n.type = '%s'"$type)); //不正确  
drupal db_query核心代码如下:
PHP代码
  1. /** 
  2.  * Indicates the place holders that should be replaced in _db_query_callback(). 
  3.  */  
  4. define('DB_QUERY_REGEXP''/(%d|%s|%%|%f|%b)/');  
  5.   
  6. /** 
  7.  * Runs a basic query in the active database. 
  8.  * 
  9.  * User-supplied arguments to the query should be passed in as separate 
  10.  * parameters so that they can be properly escaped to avoid SQL injection 
  11.  * attacks. 
  12.  * 
  13.  * @param $query 
  14.  *   A string containing an SQL query. 
  15.  * @param ... 
  16.  *   A variable number of arguments which are substituted into the query 
  17.  *   using printf() syntax. Instead of a variable number of query arguments, 
  18.  *   you may also pass a single array containing the query arguments. 
  19.  
  20.  *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose 
  21.  *   in '') and %%. 
  22.  * 
  23.  *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0, 
  24.  *   and TRUE values to decimal 1. 
  25.  * 
  26.  * @return 
  27.  *   A database query result resource, or FALSE if the query was not 
  28.  *   executed correctly. 
  29.  */  
  30. function db_query($query) {  
  31.   $args = func_get_args();  
  32.   array_shift($args);  
  33.   $query = db_prefix_tables($query);  
  34.   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax  
  35.     $args = $args[0];  
  36.   }  
  37.   _db_query_callback($args, TRUE);  
  38.   $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback'$query);  
  39.   return _db_query($query);  
  40. }  
  41.   
  42. /** 
  43.  * Helper function for db_query(). 
  44.  */  
  45. function _db_query_callback($match$init = FALSE) {  
  46.   static $args = NULL;  
  47.   if ($init) {  
  48.     $args = $match;  
  49.     return;  
  50.   }  
  51.   
  52.   switch ($match[1]) {  
  53.     case '%d'// We must use type casting to int to convert FALSE/NULL/(TRUE?)  
  54.       return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe  
  55.     case '%s': 
  56.       return db_escape_string(array_shift($args)); 
  57.     case '%%':  
  58.       return '%'; 
  59.     case '%f': 
  60.       return (float) array_shift($args); 
  61.     case '%b': // binary data  
  62.       return db_encode_blob(array_shift($args));  
  63.   }  
  64. }  
简单的对一些输入做了处理。参考: http://drupal.org/node/101496

原文来自于:http://www.luochunhui.com/id/315

Tags: drupal, php, escape_string

生日一天

一天中所拍摄的照片。。

大小: 52.52 K
尺寸: 500 x 375
浏览: 2434 次
点击打开新窗口浏览全图

大小: 56.91 K
尺寸: 500 x 375
浏览: 2681 次
点击打开新窗口浏览全图

大小: 38.2 K
尺寸: 282 x 376
浏览: 2401 次
点击打开新窗口浏览全图

大小: 56.01 K
尺寸: 500 x 375
浏览: 2465 次
点击打开新窗口浏览全图

大小: 55.53 K
尺寸: 500 x 375
浏览: 2619 次
点击打开新窗口浏览全图

更多相册请点击:http://picasaweb.google.com/xiaoyy2008/20090620#

Tags: 生日, 肖佑阳

Windows Server 2008登陆Dreamspark 免费使用

9月初,微软允诺Windows Server 2008会登录Dreamspark, 而且是no later than Oct 1st。
9月30号,Win2k8的标准版真的出现在了学生软件资源社区上,这意味着和别的学生软件一样,只要通过简单的验证操作就可以免费下载和使用。Bingo!免费的Server来临了。

访问:Windows Server 2008 Standard Edition


不过,即使你登录了,下载这些软件的时候,还是需要激活KEY的,激活的时候,去的是英文网站,在上面有个verify,好象CHINA只支持三种类型的大学。看起来仿佛几乎没用?

感慨一下下

Tags: windows, server, dreamspark, 2008, 免费