Toss some of the random environment setup code into a separate class.
authorChuck Hagenbuch <chuck@horde.org>
Wed, 23 Sep 2009 20:47:01 +0000 (16:47 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 23 Sep 2009 20:47:25 +0000 (16:47 -0400)
framework/Rampage/lib/Horde/Rampage/Environment.php [new file with mode: 0644]

diff --git a/framework/Rampage/lib/Horde/Rampage/Environment.php b/framework/Rampage/lib/Horde/Rampage/Environment.php
new file mode 100644 (file)
index 0000000..28e5909
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+class Horde_Rampage_Environment
+{
+    public function setup()
+    {
+        set_magic_quotes_runtime(0);
+        $this->reverseMagicQuotes();
+        $this->reverseRegisterGlobals();
+    }
+
+    /**
+     * @author Ilia Alshanetsky <ilia@php.net>
+     */
+    public function reverseMagicQuotes()
+    {
+        if (get_magic_quotes_gpc()) {
+            $input = array(&$_GET, &$_POST, &$_REQUEST, &$_COOKIE, &$_ENV, &$_SERVER);
+
+            while (list($k, $v) = each($input)) {
+                foreach ($v as $key => $val) {
+                    if (!is_array($val)) {
+                        $key = stripslashes($key);
+                        $input[$k][$key] = stripslashes($val);
+                        continue;
+                    }
+                    $input[] =& $input[$k][$key];
+                }
+            }
+
+            unset($input);
+        }
+    }
+
+    /**
+     * Get rid of register_globals variables.
+     *
+     * @author Richard Heyes
+     * @author Stefan Esser
+     * @url http://www.phpguru.org/article.php?ne_id=60
+     */
+    public function reverseRegisterGlobals()
+    {
+        if (ini_get('register_globals')) {
+            // Variables that shouldn't be unset
+            $noUnset = array(
+                'GLOBALS',
+                '_GET',
+                '_POST',
+                '_COOKIE',
+                '_REQUEST',
+                '_SERVER',
+                '_ENV',
+                '_FILES',
+            );
+
+            $input = array_merge(
+                $_GET,
+                $_POST,
+                $_COOKIE,
+                $_SERVER,
+                $_ENV,
+                $_FILES,
+                isset($_SESSION) ? $_SESSION : array()
+            );
+
+            foreach ($input as $k => $v) {
+                if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
+                    unset($GLOBALS[$k]);
+                }
+            }
+        }
+    }
+}