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?
November 22, 2008, 02:16:16 PM

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 386 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
Change text in remember "...
by gerborg
[November 21, 2008, 05:14:57 PM]

Log playtime from buddys ...
by Temar
[November 20, 2008, 10:33:57 AM]

Vote Core module and Simp...
by Temar
[November 19, 2008, 09:26:52 AM]

Restrict access for one m...
by Organizer
[November 19, 2008, 03:21:19 AM]

Custom / Revised Modules ...
by Elesar1
[November 17, 2008, 03:51:46 PM]

TWC
by Temar
[November 16, 2008, 11:39:12 AM]

Are there any FUN modules...
by Elesar1
[November 15, 2008, 07:39:15 PM]

Call to a member function...
by exxie
[November 15, 2008, 09:29:31 AM]

Ported Modules
by Alreadythere
[November 14, 2008, 06:10:07 PM]

cURL and other non-defaul...
by Temar
[November 14, 2008, 04:11:44 PM]
Stats
Members
Total Members: 1235
Latest: DDDepressionnn
Stats
Total Posts: 11037
Total Topics: 1496
Online Today: 18
Online Ever: 168
(July 01, 2007, 09:30:02 PM)
Users Online
Users: 1
Guests: 20
Total: 21
xlDanek

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