Index: trunk/admin/cat_modify.php
===================================================================
--- trunk/admin/cat_modify.php	(revision 4357)
+++ trunk/admin/cat_modify.php	(revision 4367)
@@ -68,5 +68,5 @@
       'id' => $_GET['cat_id'],
       'name' => @$_POST['name'],
-      'commentable' => $_POST['commentable'],
+      'commentable' => isset($_POST['commentable'])?$_POST['commentable']:'false',
       'uploadable' =>
         isset($_POST['uploadable']) ? $_POST['uploadable'] : 'false',
Index: trunk/admin/element_set.php
===================================================================
--- trunk/admin/element_set.php	(revision 4357)
+++ trunk/admin/element_set.php	(revision 4367)
@@ -197,5 +197,5 @@
     $query = 'SELECT id
   FROM '.IMAGES_TABLE.'
-  WHERE date_available BETWEEN DATE_SUB("'.$row['date'].'", INTERVAL 1 DAY) AND "'.$row['date'].'"';
+  WHERE date_available BETWEEN '.pwg_db_get_recent_period_expression(1, $row['date']).' AND \''.$row['date'].'\'';
     $page['cat_elements_id'] = array_from_query($query, 'id');
   }
Index: trunk/admin/include/functions_metadata.php
===================================================================
--- trunk/admin/include/functions_metadata.php	(revision 4357)
+++ trunk/admin/include/functions_metadata.php	(revision 4367)
@@ -247,5 +247,5 @@
     {
       $query.= '
-    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
+    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
 ';
     }
Index: trunk/admin/menubar.php
===================================================================
--- trunk/admin/menubar.php	(revision 4357)
+++ trunk/admin/menubar.php	(revision 4367)
@@ -70,6 +70,6 @@
   $query = '
 UPDATE '.CONFIG_TABLE.'
-  SET value=""
-  WHERE param="blk_'.addslashes($menu->get_id()).'"
+  SET value=\'\'
+  WHERE param=\'blk_'.addslashes($menu->get_id()).'\'
   LIMIT 1';
   pwg_query($query);
@@ -144,7 +144,7 @@
   $query = '
 UPDATE '.CONFIG_TABLE.'
-  SET value="'.addslashes(serialize($mb_conf_db)).'"
-  WHERE param="blk_'.addslashes($menu->get_id()).'"
-  LIMIT 1';
+  SET value=\''.addslashes(serialize($mb_conf_db)).'\'
+  WHERE param=\'blk_'.addslashes($menu->get_id()).'\'
+  ';
   pwg_query($query);
 }
Index: trunk/admin/site_update.php
===================================================================
--- trunk/admin/site_update.php	(revision 4357)
+++ trunk/admin/site_update.php	(revision 4367)
@@ -149,5 +149,5 @@
     {
       $query.= '
-    AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\'
+    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$_POST['cat'].'(,|$)\'
 ';
     }
@@ -208,8 +208,5 @@
 
   // next category id available
-  $query = '
-SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id
-  FROM '.CATEGORIES_TABLE;
-  list($next_id) = pwg_db_fetch_row(pwg_query($query));
+  $next_id = pwg_db_nextval('id', CATEGORIES_TABLE);
 
   // retrieve sub-directories fulldirs from the site reader
@@ -400,8 +397,5 @@
 
   // next element id available
-  $query = '
-SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
-  FROM '.IMAGES_TABLE;
-  list($next_element_id) = pwg_db_fetch_row(pwg_query($query));
+  $next_element_id = pwg_db_nextval('id', IMAGES_TABLE);
 
   $start = get_moment();
Index: trunk/comments.php
===================================================================
--- trunk/comments.php	(revision 4357)
+++ trunk/comments.php	(revision 4367)
@@ -52,9 +52,9 @@
 $since_options = array(
   1 => array('label' => l10n('today'),
-             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 1 DAY)'),
+             'clause' => 'date > '.pwg_db_get_recent_period_expression(1)),
   2 => array('label' => sprintf(l10n('last %d days'), 7),
-             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 7 DAY)'),
+             'clause' => 'date > '.pwg_db_get_recent_period_expression(7)),
   3 => array('label' => sprintf(l10n('last %d days'), 30),
-             'clause' => 'date > SUBDATE(CURDATE(), INTERVAL 30 DAY)'),
+             'clause' => 'date > '.pwg_db_get_recent_period_expression(30)),
   4 => array('label' => l10n('the beginning'),
              'clause' => '1=1') // stupid but generic
@@ -139,5 +139,5 @@
 if ( !is_admin() )
 {
-  $page['where_clauses'][] = 'validated="true"';
+  $page['where_clauses'][] = 'validated=\'true\'';
 }
 
Index: trunk/feed.php
===================================================================
--- trunk/feed.php	(revision 4357)
+++ trunk/feed.php	(revision 4367)
@@ -157,5 +157,5 @@
     $query = '
 UPDATE '.USER_FEED_TABLE.'
-  SET last_check = DATE_ADD(\''.$dbnow.'\', INTERVAL -15 DAY )
+  SET last_check = '.pwg_db_get_recent_period_expression(-15, $dbnow).'
   WHERE id = \''.$feed_id.'\'
 ;';
Index: trunk/include/calendar_base.class.php
===================================================================
--- trunk/include/calendar_base.class.php	(revision 4357)
+++ trunk/include/calendar_base.class.php	(revision 4367)
@@ -269,5 +269,5 @@
     if ( empty($page['chronology_date']) )
       return;
-    $query = 'SELECT CONCAT_WS("-"';
+    $query = 'SELECT CONCAT_WS(\'-\'';
     for ($i=0; $i<count($page['chronology_date']); $i++)
     {
Index: trunk/include/calendar_monthly.class.php
===================================================================
--- trunk/include/calendar_monthly.class.php	(revision 4357)
+++ trunk/include/calendar_monthly.class.php	(revision 4367)
@@ -348,5 +348,5 @@
     $query.= $this->get_date_where();
     $query.= '
-  ORDER BY RAND()
+  ORDER BY '.DB_RANDOM_FUNCTION.'()
   LIMIT 1';
     unset ( $page['chronology_date'][CDAY] );
Index: trunk/include/category_cats.inc.php
===================================================================
--- trunk/include/category_cats.inc.php	(revision 4357)
+++ trunk/include/category_cats.inc.php	(revision 4367)
@@ -36,7 +36,5 @@
   FROM '.CATEGORIES_TABLE.' c INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
   ON id = cat_id and user_id = '.$user['id'].'
-  WHERE date_last >= SUBDATE(
-    CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY
-  )
+  WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
 '.get_sql_condition_FandF
   (
@@ -105,5 +103,5 @@
       "\n  AND"
     ).'
-  ORDER BY RAND()
+  ORDER BY '.DB_RANDOM_FUNCTION.'()
   LIMIT 1
 ;';
@@ -133,5 +131,5 @@
       "\n  AND"
     ).'
-    ORDER BY RAND()
+    ORDER BY '.DB_RANDOM_FUNCTION.'()
     LIMIT 1
   ;';
Index: trunk/include/dblayer/functions_mysql.inc.php
===================================================================
--- trunk/include/dblayer/functions_mysql.inc.php	(revision 4357)
+++ trunk/include/dblayer/functions_mysql.inc.php	(revision 4367)
@@ -22,4 +22,9 @@
 // +-----------------------------------------------------------------------+
 
+define('DB_ENGINE', 'MySQL');
+
+define('DB_REGEX_OPERATOR', 'REGEXP');
+define('DB_RANDOM_FUNCTION', 'RAND');
+
 /**
  *
@@ -106,4 +111,14 @@
 
   return $result;
+}
+
+function pwg_db_nextval($column, $table)
+{
+  $query = '
+SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1)
+  FROM '.$table;
+  list($next) = pwg_db_fetch_row(pwg_query($query));
+
+  return $next;
 }
 
@@ -444,4 +459,84 @@
 }
 
+function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
+{
+  if ($date!='CURRENT_DATE')
+  {
+    $date = '\''.$date.'\'';
+  }
+
+  return 'SUBDATE('.$date.',INTERVAL '.$period.' DAY)';
+}
+
+function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
+{
+  $query = '
+SELECT '.pwg_db_get_recent_period_expression($period);
+  list($d) = pwg_db_fetch_row(pwg_query($query));
+
+  return $d;
+}
+
+/**
+ * returns an array containing the possible values of an enum field
+ *
+ * @param string tablename
+ * @param string fieldname
+ */
+function get_enums($table, $field)
+{
+  // retrieving the properties of the table. Each line represents a field :
+  // columns are 'Field', 'Type'
+  $result = pwg_query('desc '.$table);
+  while ($row = pwg_db_fetch_assoc($result))
+  {
+    // we are only interested in the the field given in parameter for the
+    // function
+    if ($row['Field'] == $field)
+    {
+      // retrieving possible values of the enum field
+      // enum('blue','green','black')
+      $options = explode(',', substr($row['Type'], 5, -1));
+      foreach ($options as $i => $option)
+      {
+        $options[$i] = str_replace("'", '',$option);
+      }
+    }
+  }
+  pwg_db_free_result($result);
+  return $options;
+}
+
+// get_boolean transforms a string to a boolean value. If the string is
+// "false" (case insensitive), then the boolean value false is returned. In
+// any other case, true is returned.
+function get_boolean( $string )
+{
+  $boolean = true;
+  if ( 'false' == strtolower($string) )
+  {
+    $boolean = false;
+  }
+  return $boolean;
+}
+
+/**
+ * returns boolean string 'true' or 'false' if the given var is boolean
+ *
+ * @param mixed $var
+ * @return mixed
+ */
+function boolean_to_string($var)
+{
+  if (is_bool($var))
+  {
+    return $var ? 'true' : 'false';
+  }
+  else
+  {
+    return $var;
+  }
+}
+
 // my_error returns (or send to standard output) the message concerning the
 // error occured for the last mysql query.
Index: trunk/include/filter.inc.php
===================================================================
--- trunk/include/filter.inc.php	(revision 4357)
+++ trunk/include/filter.inc.php	(revision 4367)
@@ -97,6 +97,5 @@
     }
   $query.= '
-    date_available >= SUBDATE(
-      CURRENT_DATE,INTERVAL '.$filter['recent_period'].' DAY)';
+    date_available >= '.pwg_db_get_recent_period_expression($filter['recent_period']);
 
     $filter['visible_images'] = implode(',', array_from_query($query, 'image_id'));
Index: trunk/include/functions.inc.php
===================================================================
--- trunk/include/functions.inc.php	(revision 4357)
+++ trunk/include/functions.inc.php	(revision 4367)
@@ -33,65 +33,4 @@
 
 //----------------------------------------------------------- generic functions
-
-/**
- * returns an array containing the possible values of an enum field
- *
- * @param string tablename
- * @param string fieldname
- */
-function get_enums($table, $field)
-{
-  // retrieving the properties of the table. Each line represents a field :
-  // columns are 'Field', 'Type'
-  $result = pwg_query('desc '.$table);
-  while ($row = pwg_db_fetch_assoc($result))
-  {
-    // we are only interested in the the field given in parameter for the
-    // function
-    if ($row['Field'] == $field)
-    {
-      // retrieving possible values of the enum field
-      // enum('blue','green','black')
-      $options = explode(',', substr($row['Type'], 5, -1));
-      foreach ($options as $i => $option)
-      {
-        $options[$i] = str_replace("'", '',$option);
-      }
-    }
-  }
-  pwg_db_free_result($result);
-  return $options;
-}
-
-// get_boolean transforms a string to a boolean value. If the string is
-// "false" (case insensitive), then the boolean value false is returned. In
-// any other case, true is returned.
-function get_boolean( $string )
-{
-  $boolean = true;
-  if ( 'false' == strtolower($string) )
-  {
-    $boolean = false;
-  }
-  return $boolean;
-}
-
-/**
- * returns boolean string 'true' or 'false' if the given var is boolean
- *
- * @param mixed $var
- * @return mixed
- */
-function boolean_to_string($var)
-{
-  if (is_bool($var))
-  {
-    return $var ? 'true' : 'false';
-  }
-  else
-  {
-    return $var;
-  }
-}
 
 // The function get_moment returns a float value coresponding to the number
@@ -541,6 +480,6 @@
   VALUES
   (
-    CURDATE(),
-    CURTIME(),
+    CURRENT_DATE,
+    CURRENT_TIME,
     '.$user['id'].',
     \''.$_SERVER['REMOTE_ADDR'].'\',
@@ -1498,7 +1437,5 @@
   {
     // Use MySql date in order to standardize all recent "actions/queries"
-    list($cache['get_icon']['sql_recent_date']) =
-      pwg_db_fetch_row(pwg_query('select SUBDATE(
-      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)'));
+    $cache['get_icon']['sql_recent_date'] = pwg_db_get_recent_period($user['recent_period']);
   }
 
Index: trunk/include/functions_category.inc.php
===================================================================
--- trunk/include/functions_category.inc.php	(revision 4357)
+++ trunk/include/functions_category.inc.php	(revision 4367)
@@ -378,5 +378,5 @@
     OR ';
     }
-    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
+    $query.= 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'';
   }
   $query.= '
Index: trunk/include/functions_notification.inc.php
===================================================================
--- trunk/include/functions_notification.inc.php	(revision 4357)
+++ trunk/include/functions_notification.inc.php	(revision 4367)
@@ -464,5 +464,5 @@
     AND date_available="'.$dates[$i]['date_available'].'"
     AND tn_ext IS NOT NULL
-  ORDER BY RAND(NOW())
+  ORDER BY '.DB_RANDOM_FUNCTION.'())
   LIMIT '.$max_elements.'
 ;';
Index: trunk/include/functions_plugins.inc.php
===================================================================
--- trunk/include/functions_plugins.inc.php	(revision 4357)
+++ trunk/include/functions_plugins.inc.php	(revision 4367)
@@ -225,5 +225,5 @@
   if (!empty($state))
   {
-    $clauses[] = 'state="'.$state.'"';
+    $clauses[] = 'state=\''.$state.'\'';
   }
   if (!empty($id))
Index: trunk/include/functions_user.inc.php
===================================================================
--- trunk/include/functions_user.inc.php	(revision 4357)
+++ trunk/include/functions_user.inc.php	(revision 4367)
@@ -386,6 +386,6 @@
   ('.$userdata['id'].',\''.boolean_to_string($userdata['need_update']).'\','
   .$userdata['cache_update_time'].',\''
-  .$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].',"'
-  .$userdata['image_access_type'].'","'.$userdata['image_access_list'].'")';
+  .$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].',\''
+  .$userdata['image_access_type'].'\',\''.$userdata['image_access_list'].'\')';
       pwg_query($query);
     }
@@ -633,5 +633,5 @@
   if ( isset($filter_days) )
   {
-    $query .= ' AND i.date_available > SUBDATE(CURRENT_DATE,INTERVAL '.$filter_days.' DAY)';
+    $query .= ' AND i.date_available > '.pwg_db_get_recent_period_expression($filter_days);
   }
 
@@ -1040,5 +1040,5 @@
        '.$conf['user_fields']['password'].' AS password
   FROM '.USERS_TABLE.'
-  WHERE '.$conf['user_fields']['username'].' = \''.mysql_real_escape_string($username).'\'
+  WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\'
 ;';
   $row = pwg_db_fetch_assoc(pwg_query($query));
Index: trunk/include/section_init.inc.php
===================================================================
--- trunk/include/section_init.inc.php	(revision 4357)
+++ trunk/include/section_init.inc.php	(revision 4367)
@@ -450,6 +450,5 @@
     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
   WHERE
-    date_available >= SUBDATE(
-      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)
+    date_available >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
     '.$forbidden.'
   '.$conf['order_by'].'
Index: trunk/include/ws_functions.inc.php
===================================================================
--- trunk/include/ws_functions.inc.php	(revision 4357)
+++ trunk/include/ws_functions.inc.php	(revision 4367)
@@ -122,13 +122,13 @@
         $matches[1][$i] = 'date_available'; break;
       case 'rand': case 'random':
-        $matches[1][$i] = 'RAND()'; break;
+        $matches[1][$i] = DB_RANDOM_FUNCTION.'()'; break;
     }
     $sortable_fields = array('id', 'file', 'name', 'hit', 'average_rate',
-      'date_creation', 'date_available', 'RAND()' );
+      'date_creation', 'date_available', DB_RANDOM_FUNCTION.'()' );
     if ( in_array($matches[1][$i], $sortable_fields) )
     {
       if (!empty($ret))
         $ret .= ', ';
-      if ($matches[1][$i] != 'RAND()' )
+      if ($matches[1][$i] != DB_RANDOM_FUNCTION.'()' )
       {
         $ret .= $tbl_name;
@@ -231,5 +231,5 @@
     if ($params['recursive'])
     {
-      $where_clauses[] = 'uppercats REGEXP \'(^|,)'.$cat_id.'(,|$)\'';
+      $where_clauses[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$cat_id.'(,|$)\'';
     }
     else
@@ -378,5 +378,5 @@
   else if ($params['cat_id']>0)
   {
-    $where[] = 'uppercats REGEXP \'(^|,)'.
+    $where[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.
       (int)($params['cat_id'])
       .'(,|$)\'';
Index: trunk/random.php
===================================================================
--- trunk/random.php	(revision 4357)
+++ trunk/random.php	(revision 4367)
@@ -52,5 +52,5 @@
     'WHERE'
   ).'
-  ORDER BY RAND(NOW())
+  ORDER BY '.DB_RANDOM_FUNCTION.'()
   LIMIT '.min(50, $conf['top_number'],$user['nb_image_page']).'
 ;';
