Back to General Articles

[How To] Never Worry About SQL Injections and Queries Again
by vbresults 27 Mar 2012

Why use this? Queries are normally cumbersome to write for vBulletin, and when managing a lot of queries in even a small-medium-sized, it is easy to miss sanitizing an input here or there. If you use this tool, the SQL Injection prevention is completely automatic, queries are easier to create and maintain, and blocks of code that are common among similar queries are eliminated.

What is this? This tool prevents SQL injections by placing a layer of protection between the query and database. First, it is important to note that preventing SQL injections is top priority. It takes 1 SQL Injection for someone to take over an entire forum, as history has proven time and time again. After developing a few dozen add-ons, I realized that queries are annoying to write for vB, especially with this fact in mind. I constantly found myself checking queries over and over to make sure everything was safe. It became a big problem for applications that were too small to be moved over to a PHP framework (which has tools like this built in), but too large to police each database query. This tool is intended for add-on authors or people adding custom functionality to their forums themselves.

How is this used? This tool will require only basic knowledge of http://php.net/manual/en/language.types.string.php, http://php.net/manual/en/function.array.php, functions, and http://php.net/manual/en/function.printf.php's format -- nothing specific to vBulletin. If you are already writing vBulletin queries, then you are mostly ready. How to use this is listed below.

Let's compare the common ways of executing queries versus using Extended Databases.

1. Fetch one record; we are getting the username and userid of a user with the email address we input.

http://members.vbulletin.com/api/vBulletin/vB_Database.html#query_first (common way)
PHP Code:
$query 'SELECT userid, username FROM ' TABLE_PREFIX 'user WHERE email = "' $vbulletin->db->escape_string($vbulletin->GPC['email']) . '"';

$user $vbulletin->db->query_first($query); 
ExtendedDatabases_Query::first (improved way)
PHP Code:
$user ExtendedDatabases_Query::first(array(
    
'query' => 'SELECT userid, username FROM %1$suser WHERE email = "%2$s"',
    
'params' => $vbulletin->GPC['email']
)); 
Both will return exactly the same thing. One will not require direct use of TABLE_PREFIX, vB_Database::escape_string, or bizarre quoting. %1$s will always be the table prefix whether params is an array or not.

2. Fetch multiple records; we are getting the usernames for each user who has a username starting with our input (e.g. we input lancer, it returns lancerforhire).

http://members.vbulletin.com/api/vBulletin/vB_Database.html#query_read_slave (common way)
PHP Code:
$query 'SELECT username FROM ' TABLE_PREFIX 'user WHERE username REGEXP "^' preg_quote($vbulletin->db->escape_string($vbulletin->GPC['username'])) . '"';
$resource $vbulletin->db->query_read_slave($query);
$records null;

if (!
$vbulletin->db->num_rows($resource)) {
    
// no records
}

while (
$record $vbulletin->db->fetch_array($resource)) {
    
$records[] = $record;
}

$vbulletin->db->free_result($resource); 
ExtendedDatabases_Query::read (improved way)
PHP Code:
$records ExtendedDatabases_Query::read(array(
    
'query' => 'SELECT username FROM %1$suser WHERE username REGEXP "^%2$s"',
    
'params' => preg_quote($vbulletin->GPC['username'])
));

if (!
$records) {
    
// no records

Once again, both do exactly the same thing. One is just a LOT simpler. SQL injection prevention and other vital functions are AUTOMATIC.

3. Fetch a single field from a single record; we are getting the first full username for the user who has a username starting with our input (e.g. we input lancer, it returns lancerforhire). New in 1.4!

http://members.vbulletin.com/api/vBulletin/vB_Database.html#query_first (common way)
PHP Code:
$query 'SELECT username FROM ' TABLE_PREFIX 'user WHERE username REGEXP "^' preg_quote($vbulletin->db->escape_string($vbulletin->GPC['username'])) . '"';

$user $vbulletin->db->query_first($query);

$username $user $user['username'] : null
ExtendedDatabases_Query::field (improved way)
PHP Code:
$username ExtendedDatabases_Query::field(array(
    
'name' => 'username',
    
'query' => 'SELECT username FROM %1$suser WHERE username REGEXP "^%2$s"',
    
'params' => preg_quote($vbulletin->GPC['username'])
)); 

4. Fetch multiple records; we are getting the username, user id, and user group id of all users whose primary groups match our input (array). New in 1.4!

http://members.vbulletin.com/api/vBulletin/vB_Database.html#query_read_slave (common way)
PHP Code:
$query 'SELECT username, userid, usergroupid FROM ' TABLE_PREFIX 'user
    WHERE usergroup IN (' 
explode(', '$vbulletin->GPC['usergroupids']) . ')';

$resource $vbulletin->db->query_read_slave($query);
$records null;

if (!
$vbulletin->db->num_rows($resource)) {
    
// no records
}

while (
$record $vbulletin->db->fetch_array($resource)) {
    
$records[] = $record;
}

$vbulletin->db->free_result($resource); 
ExtendedDatabases_Query::read with array sub-parameters (improved way)
PHP Code:
$records ExtendedDatabases_Query::read(array(
    
'query' => 'SELECT username, userid, usergroupid FROM %1$suser
        WHERE %2$s'
    'params' 
=> array(
        
'usergroupid' => $vbulletin->GPC['usergroupids']
    )
));

if (!
$records) {
    
// no records

---

ExtendedDatabases_Query::write is the successor to vB_Database::query_write; I do not believe this warrants another example.

---

Implement this Right Now

If you are a coder, implement this immediately. If you are not a coder, ask the developers of your installed add-ons to do it. It's very simple and has long term benefits, but if you don't do it right now, chances are you never will (unless your forum implodes).

---

How do I get this?

Download the product-extended_databases_ Xml and add it (extended_databases) as a product dependency. Yes; that's it!
Attached Files
File Type: xml product-extended_databases-1.4.xml (4.2 KB, 25 views)

vblts.ru supports vBulletin®, 2022-2024