L* R*
HOME FORUM DOWNLOADS
Content
  Links
     Browse SVN
     SVN Commit log
     Documentation (Wiki)
  Developers
     Taskmanager
User
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 14, 2008, 10:36:40 AM

Login with username, password and session length
Search



Advanced search
Support GoPHP5.org
BeBot - An Anarchy Online/Age Of Conan chat automaton > Forum > Development > Feedback and Suggestions > Topic: Smarter, Safer, MySQL.php modification with full Backwards Compatability
Pages: [1]   Go Down
« previous next »
Print
Author Topic: Smarter, Safer, MySQL.php modification with full Backwards Compatability  (Read 367 times)
0 Members and 1 Guest are viewing this topic.
Neo-Vortex
Freshman
*
Offline Offline

Posts: 11


Smarter, Safer, MySQL.php modification with full Backwards Compatability
« on: February 26, 2007, 02:08:20 AM »

Hey, something I've been using on my bots for quite some time now is the use of printf() style args with MySQL.php, this also allows me to automagically escape strings eliminating possible SQL injection vulnerabilities, and best of all, remains backwards compatible, the changes are also quite simple and it would make sql coding in modules (and bebot itself) much nicer to read.

In MySQL.php the following function is added

Code:
        function smartescape($args)
        {
                if (is_numeric($args)) return $args;
                return mysql_real_escape_string($args);
        }

        function smartargs($args)
        {
                $query = array_shift($args);
                if (empty($args)) return $query;
                $args  = array_map(array($this, 'smartescape'), $args);
                array_unshift($args, $query);
                return call_user_func_array('sprintf', $args);
        }

Then the following line is added at the start of the each SQL function - ie, select(), query(), and returnQuery()

Code:
$sql = $this -> smartargs(func_get_args());

Then instead of say the following (example pulled from the news module):
Code:
$this -> bot -> db -> query("INSERT INTO news (type, time, name, news) VALUES ('2' ," . time() .", '" . $name . "', '" . addslashes($msg) . "')");
You would do this:
Code:
$this -> bot -> db -> query("INSERT INTO news (type, time, name, news) VALUES ('2' ,'%d', '%s', '%s')", time(), $name, $msg);
Which is much nicer on the eyes, with all escaping done for you automagically Smiley, but best of all, the previous example would work perfectly fine also still, so there is no forced change that would break older modules.

The only potential issue would be if you are using the '%' character to specify a wildcard in which case you would have to escape it by doing '%%' instead or sprintf() would think it should process it, however to eliminate backwards-compatability issues for where '%' has been used unescaped, it will detect if there are any extra arguments parsed, and if there are none (ie, it is just the query itself), it will not process it using printf() to eliminate this Smiley

Edit: Little change that prevents warnings for if you pass ints to it - old still works, just on rare occasions complains Tongue
« Last Edit: February 28, 2007, 04:02:46 AM by Neo-Vortex » Logged
Khalem
BeBot Founder
Administrator
Grandmaster
********
Offline Offline

Gender: Male
Posts: 670



WWW
Re: Smarter, Safer, MySQL.php modification with full Backwards Compatability
« Reply #1 on: February 27, 2007, 11:43:35 AM »

I've been planning to do this, but not gotten around to it.

Will have a look at this again this weekend.
Logged

BeBot Founder and Fixer Kingpin
Madman coder and destroyer of good code
Pages: [1]   Go Up
Print
BeBot - An Anarchy Online/Age Of Conan chat automaton > Forum > Development > Feedback and Suggestions > Topic: Smarter, Safer, MySQL.php modification with full Backwards Compatability
« previous next »
 
Jump to:  

Recent
Buddylist for guildbot
by Zacix
[Today at 09:43:40 AM]

Link 2 Guilds via 2 Bots?
by Temar
[Today at 08:42:11 AM]

OnlineOrg
by Karsta
[October 13, 2008, 06:54:20 AM]

!security Levels
by Nytridr
[October 12, 2008, 11:36:45 PM]

Bossloot for 5.2
by Brac
[October 12, 2008, 04:45:17 PM]

Ported Modules
by Jiheld
[October 12, 2008, 09:00:13 AM]

raid/point system suggest...
by Temar
[October 11, 2008, 11:45:40 PM]

!timers suggestions: alts...
by Alreadythere
[October 11, 2008, 03:19:46 PM]

!server does not work
by Alreadythere
[October 11, 2008, 06:54:38 AM]

TWC
by clashbot
[October 10, 2008, 08:52:57 AM]
Stats
Members
Total Members: 989
Latest: Syphae
Stats
Total Posts: 10814
Total Topics: 1459
Online Today: 14
Online Ever: 168
(July 01, 2007, 09:30:02 PM)
Users Online
Users: 2
Guests: 13
Total: 15
Zacix

Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC
TinyPortal v0.9.8 © Bloc | NewDef design by Bloc
Page created in 0.252 seconds with 28 queries. (Pretty URLs adds 0.026s, 4q)
Loading...