Extension ext-user for sub id tracking continued:
[mailer.git] / inc / request-functions.php
index 60a1addea0b10dc328f6b99c73738b245c505260..e0051a169edc6da09958b57ae5ac2276f48452b9 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 02/28/2009 *
- * ===============                              Last change: 02/28/2009 *
+ * Mailer v0.2.1-FINAL                                Start: 02/28/2009 *
+ * ===================                          Last change: 02/28/2009 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : request-functions.php                            *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Spezialle Funktionen fuer die Anfragebehandlung  *
  * -------------------------------------------------------------------- *
- * $Revision:: 856                                                    $ *
- * $Date:: 2009-03-06 20:24:32 +0100 (Fr, 06. Mär 2009)              $ *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author:: stelzi                                                   $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
+ * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
+       die();
 } // END - if
 
+// Initialize the request elements
+function initRequest () {
+       $GLOBALS['raw_request']['get']  = (array) $_GET;
+       $GLOBALS['raw_request']['post'] = (array) $_POST;
+}
+
 // Wrapper for elements in $_GET
-function REQUEST_GET ($element) {
+function getRequestElement ($element) {
        // By default no element is there
-       $value = null;
+       $value = NULL;
 
        // Is the element cached or there?
-       if (isset($GLOBALS['cache_array']['request_get'][$element])) {
+       if (isset($GLOBALS['cache_request']['get'][$element])) {
                // Then use the cache
-               $value = $GLOBALS['cache_array']['request_get'][$element];
-       } elseif (REQUEST_ISSET_GET($element)) {
+               $value = $GLOBALS['cache_request']['get'][$element];
+       } elseif (isGetRequestElementSet($element)) {
                // Then get it directly
-               $value = SQL_ESCAPE($_GET[$element]);
+               $value = SQL_ESCAPE($GLOBALS['raw_request']['get'][$element]);
 
                // Store it in cache
-               $GLOBALS['cache_array']['request_get'][$element] = $value;
+               $GLOBALS['cache_request']['get'][$element] = $value;
        } // END - if
 
        // Return value
@@ -64,31 +68,32 @@ function REQUEST_GET ($element) {
 }
 
 // Checks if an element in $_GET exists
-function REQUEST_ISSET_GET ($element, $extra="") {
-       if (empty($extra)) {
-               return (isset($_GET[$element]));
+function isGetRequestElementSet ($element, $subElement = '') {
+       if (empty($subElement)) {
+               return ((isset($GLOBALS['raw_request']['get'][$element])) && ('' . ($GLOBALS['raw_request']['get'][$element] . '') != ''));
        } else {
-               return (isset($_GET[$element][$extra]));
+               return ((isset($GLOBALS['raw_request']['get'][$element][$subElement])) && ('' . ($GLOBALS['raw_request']['get'][$element][$subElement] . '') != ''));
        }
 }
 
 // Removes an element from $_GET
-function REQUEST_UNSET_GET ($element) {
-       unset($_GET[$element]);
+function unsetGetRequestElement ($element) {
+       unset($GLOBALS['cache_request']['get'][$element]);
+       unset($GLOBALS['raw_request']['get'][$element]);
 }
 
 // Getter for whole $_GET array
-function REQUEST_GET_ARRAY () {
-       return $_GET;
+function getRequestArray () {
+       return $GLOBALS['raw_request']['get'];
 }
 
 // Counts entries in $_GET or returns false if not an array
-function REQUEST_GET_COUNT () {
+function countRequestGet () {
        // By default this is not an array
        $count = false;
 
        // Get the array
-       $GET = REQUEST_GET_ARRAY();
+       $GET = getRequestArray();
 
        // Is it an array?
        if (is_array($GET)) {
@@ -100,69 +105,116 @@ function REQUEST_GET_COUNT () {
 }
 
 // Setter for element in $_GET
-function REQUEST_SET_GET ($element, $value) {
-       $_GET[SQL_ESCAPE($element)] = SQL_ESCAPE($value);
+function setGetRequestElement ($element, $value) {
+       // Escape both
+       $element = SQL_ESCAPE($element);
+       $value   = SQL_ESCAPE($value);
+
+       // Set in $_GET
+       $GLOBALS['raw_request']['get'][$element] = $value;
+
+       // Update cache
+       $GLOBALS['cache_request']['get'][$element] = $value;
 }
 
 // Wrapper for elements in $_POST
-function REQUEST_POST ($element) {
+function postRequestElement ($element, $subElement = NULL) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ' - ENTERED!');
        // By default no element is there
-       $value = null;
+       $value = NULL;
 
-       // Is the element there?
-       if (REQUEST_ISSET_POST($element)) {
+       // Is the element in cache?
+       if (isset($GLOBALS['cache_request']['post'][$element][$subElement])) {
+               // Then use it
+               $value = $GLOBALS['cache_request']['post'][$element][$subElement];
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - CACHE!');
+       } elseif (isPostRequestElementSet($element)) {
                // Then use it
-               $value = $_POST[$element];
+               $value = $GLOBALS['raw_request']['post'][$element];
+
+               // Is $subElement set?
+               if ((!is_null($subElement)) && (isPostRequestElementSet($element, $subElement))) {
+                       // Then use this
+                       $value = SQL_ESCAPE($value[$subElement]);
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ' - SUB!');
+               } elseif (!is_array($value)) {
+                       // Escape it here
+                       $value = SQL_ESCAPE($value);
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ' - REGULAR!');
+               }
+
+               // Set it in cache
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element=' . $element . ',subElement=' . $subElement . ',value=' . $value.' - ADDED!');
+               $GLOBALS['cache_request']['post'][$element][$subElement] = $value;
        } // END - if
 
        // Return value
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'element[' . gettype($element) . ']=' . $element . ',subElement[' . gettype($subElement) . ']=' . $subElement . ',value[' . gettype($value) . ']=' . $value . ' - EXIT!');
        return $value;
 }
 
 // Checks if an element in $_POST exists
-function REQUEST_ISSET_POST ($element, $extra="") {
-       if (empty($extra)) {
-               return (isset($_POST[$element]));
+function isPostRequestElementSet ($element, $subElement = NULL) {
+       /*
+        * Always check that $element is a string and that $subElement is NULL or
+        * a string as numerical indexes are not wanted in POST data (in this
+        * project).
+        */
+       assert(is_string($element) && ((is_null($subElement)) || (is_string($subElement)) || (is_int($subElement))));
+
+       // Is a sub element set?
+       if (is_null($subElement)) {
+               // No, then only check $element
+               return ((isset($GLOBALS['raw_request']['post'][$element])) && ((is_array($GLOBALS['raw_request']['post'][$element])) || (('' . $GLOBALS['raw_request']['post'][$element] . '') != '')));
        } else {
-               return (isset($_POST[$element][$extra]));
+               // Yes, then check both together
+               return ((isset($GLOBALS['raw_request']['post'][$element][$subElement])) && (('' . $GLOBALS['raw_request']['post'][$element][$subElement] . '') != ''));
        }
 }
 
 // Removes an element from $_POST
-function REQUEST_UNSET_POST ($element) {
-       unset($_POST[$element]);
+function unsetPostRequestElement ($element) {
+       unset($GLOBALS['raw_request']['post'][$element]);
+       unset($GLOBALS['cache_request']['post'][$element]);
 }
 
 // Getter for whole $_POST array
-function REQUEST_POST_ARRAY () {
-       return $_POST;
+function postRequestArray () {
+       return $GLOBALS['raw_request']['post'];
+}
+
+// Setter for whole $_POST array
+function setPostRequestArray ($postData) {
+       $GLOBALS['raw_request']['post'] = $postData;
 }
 
 // Counts entries in $_POST or returns false if not an array
-function REQUEST_POST_COUNT () {
+function countRequestPost () {
        // By default this is not an array
        $count = false;
 
        // Get the array
-       $POST = REQUEST_POST_ARRAY();
+       $postData = postRequestArray();
 
        // Is it an array?
-       if (is_array($POST)) {
+       if (is_array($postData)) {
                // Then count it
-               $count = count($POST);
+               $count = count($postData);
        } // END - if
 
        // Return value
+       return $count;
 }
 
 // Setter for element in $_POST
-function REQUEST_SET_POST ($element, $value) {
+function setPostRequestElement ($element, $value) {
+       // Is $element or $value an array?
        if (is_array($element)) {
                // Set array
-               $eval = "\$_POST['";
+               $eval = "\$GLOBALS['raw_request']['post']['";
 
                // Add all entries
-               $eval .= implode("''", $element);
+               $eval .= implode("']['", $element);
 
                // Finish eval() command
                $eval .= sprintf("'] = \"%s\";", SQL_ESCAPE($value));
@@ -170,19 +222,59 @@ function REQUEST_SET_POST ($element, $value) {
                // And run it
                eval($eval);
        } elseif (is_array($value)) {
+               // Escape element
+               $element = SQL_ESCAPE($element);
+
                // Value is an array so set it directly
-               $_POST[SQL_ESCAPE($element)] = $value;
+               $GLOBALS['raw_request']['post'][$element] = $value;
        } else {
+               // Escape both
+               $element = SQL_ESCAPE($element);
+               $value   = SQL_ESCAPE($value);
+
                // Set regular entry
-               $_POST[SQL_ESCAPE($element)] = SQL_ESCAPE($value);
+               $GLOBALS['raw_request']['post'][$element] = $value;
        }
+
+       // Update cache
+       $GLOBALS['cache_request']['post'][$element][null] = $value;
 }
 
-// Checks wether a form was sent. If so, the $_POST['ok'] element must be set
-function IS_FORM_SENT () {
+// Checks whether a form was sent. If so, the $_POST['ok'] element must be set
+function isFormSent ($requestParameter = 'ok') {
        // Simply wrap it!
-       return REQUEST_ISSET_POST('ok');
+       return isPostRequestElementSet($requestParameter);
+}
+
+// Getter for request URI
+function getRequestUri () {
+       // Is it not set?
+       if (!isset($_SERVER['REQUEST_URI'])) {
+               // Return empty string
+               return '';
+       } // END - if
+
+       // Return it
+       return $_SERVER['REQUEST_URI'];
+}
+
+// Add all GET parameters to a string (without leading sign)
+function addAllGetRequestParameters () {
+       // Init variable
+       $return = '';
+
+       // Now add all parameters
+       foreach (getRequestArray() as $key => $value) {
+               // Add it secured
+               $return .= SQL_ESCAPE($key) . '=' . SQL_ESCAPE($value) . '&amp;';
+       } // END - foreach
+
+       // Remove trailing &amp;
+       $return = substr($return, 0, -5);
+
+       // Return it
+       return $return;
 }
 
-//
+// [EOF]
 ?>