In order to gain more experience in WordPress I delved into its code base to study its inner working and its workflow, and I was quite astonished when I saw that:
-
They implement register_globals (an excerpt from wp-includes/class-wp.php):
// The query_vars property will be extracted to the GLOBALS. So care should // be taken when naming global variables that might interfere with the // WordPress environment. function register_globals() { global $wp_query; // Extract updated query vars back into global namespace. foreach ( (array) $wp_query->query_vars as $key => $value) { $GLOBALS[$key] = $value; } -
They rely on magic quotes (exerpt from wp-includes/functions.php. magic_quotes_gpc is turned off at bootstrapping, before calling this function):
function add_magic_quotes( $array ) { foreach ( (array) $array as $k => $v ) { if ( is_array( $v ) ) { $array[$k] = add_magic_quotes( $v ); } else { $array[$k] = addslashes( $v ); } - They rely on addslashes (but since 2.8.0 they introduced also mysql_real_escape_string, but the
_weak_escape()function that usesaddslashes()still exists in the wpdb class)
UPDATE: I see they emulate prepared statements by usingsprintf()and custom placedholders, so queries should be safe I think. Still I’m puzzled on why they don’t provide at least mysqli, after all the detection of Mysql and PHP version happens early in the bootstrapping sequence.
Now, from the year-long frequentation of SO I learned a lot of things, especially that the above three function are “deprecated” and show security issues, and are watched in horror by many.
But WP must have a reason to use them. I’d like to know from more experienced programmers if there are really security issues, or if sometimes their usage is just too clouded in rumors and false convinctions. I know that magic_quotes is an heritage from the past, and the same could be said for addslashes (at least when used for databases), but while googling before asking this I found many websites talking about using addslashes() over mysql_real_escape_string().
I’m interested in knowing a clear, detailed reason on why those badly depicted functions are used; WordPress had had many improvements over the years, addressing different aspects, and yet these functions are still used; I’m looking, therefore, to a concrete explanation over the positive aspects that somehow override the negative ones and justify the usage of those functions.
I’m not looking for opinions (I perfectly know they’re offtopic here), nor I am ranting about WordPress, I hope this is clear. I’d just like to know why many php programmers consider these functions “bad”, and yet a worldwide giant like WordPress, who’s at the 3rd version now, still uses them.
Is this for compatibility with different servers and php versions? (they check very earl for those, though).
Is there something I miss about this functions, how important they can be in an environment like wordpress (or in general)? I’m quite confused, to be honest.
(WordPress Open Tickets over Time)
Don’t rely on the WordPress codebase to do assumptions about good practice or current standards in PHP coding. I’m saying this as someone who has fiddled with wordpress development over a longer period of time.
WordPress codebase is about 10 years old, it’s full of legacy code[1]. The program can not evolve on the code-level much because of that, so you find a lot of workarounds for problems that are already solved nowadays much better.
Just take this story: PHP had magic quotes. WordPress developers thought it was useful. So for those hosts that did not have it configured, they added it. Ending up whith code that expects slashed input data often and at various places. The simple thing is, now they just can’t change it to proper input processing and sanitization easily because of the usage of (super)globals introducing static global state nearly everywhere.
You can not easily refactor such code.
Same for the database class. It has a long history, originally based on an early version of ezSQL. At that time there was not
mysql_real_escape_stringand when it was introduced, the WP devs had the problem that not all installation bases support it.So don’t wonder about the coding practice you find inside the WordPress code. You’ll learn how things could have been done years ago and with more or less outdated PHP versions. It’s not that long ago that WordPress switched to PHP 5 for example.
This might not be your list of priorities (hopefully), projects differ here a lot. But having a legacy code-base alone is a burden regardless how project priorities are set. WordPress is only one example.
[1] see Milestones of WordPress: Early Project Timeline (ca. 2000 to 2005))