Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file FormUtil.class.php

Documentation is available at FormUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: FormUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  * @subpackage FormUtil
  12.  */
  13.  
  14. /**
  15.  * FormUtil
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage FormUtil
  19.  */
  20. class FormUtil
  21. {
  22.     /**
  23.      * Return the requested key from input in a safe way. This function
  24.      * is safe to use for recursive arrays and either returns a non-empty
  25.      * string or the (optional) default.
  26.      *
  27.      * This method is based on pnVarCleanFromInput but array-safe.
  28.      *
  29.      * @param key        The field to return
  30.      * @param default    The value to return if the requested field is not found (optional) (default=false)
  31.      * @param source     The sourc field to get a parameter from
  32.      *
  33.      * @return The requested input key or the specified default
  34.      */
  35.     function getPassedValue ($key$default=null$source=null)
  36.     {
  37.         if (!$key{
  38.             return pn_exit ('Empty key passed to FormUtil::getPassedValueSafe() ...');
  39.         }
  40.  
  41.         static $cache array();
  42.  
  43.         $source strtoupper($source);
  44.         $src    ($source $source 'REQUEST''_' ($default != null $default 'null');
  45.  
  46.         if (!defined('_PNINSTALLVER'&& isset($cache[$src][$key])) {
  47.             return $cache[$src][$key];
  48.         }
  49.  
  50.         $doClean false;
  51.         switch (true)
  52.         {
  53.             case (isset($_REQUEST[$key]&& !isset($_FILES[$key]&& (!$source || $source=='R' || $source=='REQUEST')):
  54.                 $value $_REQUEST[$key];
  55.                 $doClean true;
  56.                 break;
  57.             case isset($_GET[$key]&& (!$source || $source=='G' || $source=='GET'):
  58.                 $value $_GET[$key];
  59.                 $doClean true;
  60.                 break;
  61.             case isset($_POST[$key]&& (!$source || $source=='P' || $source=='POST'):
  62.                 $value $_POST[$key];
  63.                 $doClean true;
  64.                 break;
  65.             case isset($_COOKIE[$key]&& (!$source || $source=='C' || $source=='COOKIE'):
  66.                 $value $_COOKIE[$key];
  67.                 $doClean true;
  68.                 break;
  69.             case isset($_FILES[$key]&& ($source=='F' || $source=='FILES'):
  70.                 $value $_FILES[$key];
  71.                 break;
  72.             case (isset($_GET[$key]|| isset($_POST[$key])) && ($source=='GP' || $source=='GETPOST'):
  73.                 if (isset($_GET[$key])) {
  74.                     $value $_GET[$key];
  75.                 }
  76.                 if (isset($_POST[$key])) {
  77.                     $value $_POST[$key];
  78.                 }
  79.                 $doClean true;
  80.                 break;
  81.             default:
  82.                 if ($source{
  83.                     static $valid array ('R''REQUEST''G''GET''P''POST''C''COOKIE''F''FILES''GP''GETPOST');
  84.                     if (!in_array($source$valid)) {
  85.                         pn_exit ('Invalid input source [' DataUtil::formatForDisplay($source'] received ...');
  86.                         return $default;
  87.                     }
  88.                 }
  89.         }
  90.  
  91.         if (isset($value&& !is_null($value))
  92.         {
  93.             if (is_array($value)) {
  94.                 FormUtil::cleanArray ($value);
  95.             }
  96.             else
  97.             {
  98.                 static $alwaysclean array('name''module''type''file''authid');
  99.                 if (in_array($key$alwaysclean)) {
  100.                     $doClean true;
  101.                 }
  102.                 if ($doClean && !defined('_PNINSTALLVER')) {
  103.                     FormUtil::cleanValue ($value);
  104.                 }
  105.             }
  106.  
  107.             $cache[$src][$key$value;
  108.             return $value;
  109.         }
  110.  
  111.         $cache[$src][$key$default;
  112.         return $default;
  113.     }
  114.  
  115.  
  116.     /**
  117.      * Clean an array acquired from input. This method is safe to use for recursive arrays
  118.      * and cleans the array in place as well as returning it.
  119.      *
  120.      * @param array     The array to clean up
  121.      *
  122.      * @return The the altered/cleaned data array.
  123.      */
  124.     function cleanArray (&$array)
  125.     {
  126.         if (!is_array($array)) {
  127.             return pn_exit ('Non-array passed to FormUtil::cleanArray () ...');
  128.         }
  129.  
  130.         $ak array_keys ($array);
  131.         $kc count($ak);
  132.  
  133.         for ($i=0$i<$kc$i++)
  134.         {
  135.             $key $ak[$i];
  136.             if (is_array($array[$key])) {
  137.                 FormUtil::cleanArray($array[$key]);
  138.             }
  139.             else {
  140.                 FormUtil::cleanValue($array[$key]);
  141.             }
  142.         }
  143.  
  144.         return $array;
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Clean an individual data element in place; cleans the array in place
  150.      * as well as returning it.
  151.      *
  152.      * @param value     The value to clean
  153.      *
  154.      * @return reference to the original (altered/cleaned) data array
  155.      */
  156.     function cleanValue (&$value)
  157.     {
  158.         static $isAdmin null;
  159.         if ($isAdmin === null)
  160.           $isAdmin SecurityUtil::checkPermission('.*''.*'ACCESS_ADMIN);
  161.  
  162.         if (!$value{
  163.             return $value;
  164.         }
  165.  
  166.         if (get_magic_quotes_gpc()) {
  167.             pnStripslashes($value);
  168.         }
  169.  
  170.         if (!$isAdmin)
  171.         {
  172.           static $replace array();
  173.           static $search  array('|</?\s*SCRIPT.*?>|si',
  174.                                   '|</?\s*FRAME.*?>|si',
  175.                                   '|</?\s*OBJECT.*?>|si',
  176.                                   '|</?\s*META.*?>|si',
  177.                                   '|</?\s*APPLET.*?>|si',
  178.                                   '|</?\s*LINK.*?>|si',
  179.                                   '|</?\s*IFRAME.*?>|si',
  180.                                   '|STYLE\s*=\s*"[^"]*"|si');
  181.  
  182.           $value preg_replace($search$replace$value);
  183.         }
  184.  
  185.         $value trim($value);
  186.         return $value;
  187.     }
  188.  
  189.  
  190.     /**
  191.      * Return a boolean indicating whether the specified field is required
  192.      *
  193.      * @param validationInfo   The plain (non-structured) validation array
  194.      * @param field            The fieldname
  195.      *
  196.      * @return boolean indicating whether or not the specified field is required
  197.      */
  198.     function isRequiredField ($validationInfo$field)
  199.     {
  200.         if (!$validationInfo{
  201.             return pn_exit ('Empty validationInfo passed to FormUtil::isRequiredField() ...');
  202.         }
  203.  
  204.         if (!$field{
  205.             return pn_exit ('Empty fieldname passed to FormUtil::isRequiredField() ...');
  206.         }
  207.  
  208.         $rec $validationInfo[$field];
  209.  
  210.         if (!$rec{
  211.             return false;
  212.         }
  213.  
  214.         return $rec[1];
  215.     }
  216.  
  217.  
  218.     /**
  219.      * Get the required field marker (or nothing) for the specified field
  220.      *
  221.      * @param validationInfo   The plain (non-structured) validation array
  222.      * @param field            The fieldname
  223.      *
  224.      * @return The required field marker or an empty string
  225.      */
  226.     function getRequiredFieldMarker ($validationInfo$field)
  227.     {
  228.         if (FormUtil::isRequiredField($validationInfo$field)) {
  229.             return _REQUIRED_MARKER;
  230.         }
  231.  
  232.         return _MARKER_NONE;
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Clear the validation error array
  238.      *
  239.      * @param objectType       The (string) object type
  240.      *
  241.      * @return nothing 
  242.      */
  243.     function clearValidationErrors ($objectType=null)
  244.     {
  245.         if ($objectType{
  246.             if (isset($_SESSION['validationErrors'][$objectType])) {
  247.                 unset ($_SESSION['validationErrors'][$objectType]);
  248.             }
  249.         else {
  250.             if (isset($_SESSION['validationErrors'])) {
  251.                 unset ($_SESSION['validationErrors']);
  252.             }
  253.         }
  254.     }
  255.  
  256.  
  257.     /**
  258.      * Clear the objects which failed validation
  259.      *
  260.      * @param objectType       The (string) object type
  261.      *
  262.      * @return nothing 
  263.      */
  264.     function clearValidationFailedObjects ($objectType=null)
  265.     {
  266.         if ($objectType{
  267.             if (isset($_SESSION['validationFailedObjects'][$objectType])) {
  268.                 unset ($_SESSION['validationFailedObjects'][$objectType]);
  269.             }
  270.         else {
  271.             if (isset($_SESSION['validationFailedObjects'])) {
  272.                 unset ($_SESSION['validationFailedObjects']);
  273.             }
  274.         }
  275.     }
  276.  
  277.  
  278.     /**
  279.      * Get the validation errors
  280.      *
  281.      * @return The validation error array or null
  282.      */
  283.     function getValidationErrors ()
  284.     {
  285.         static $ve null;
  286.         if (!$ve{
  287.             if (isset($_SESSION['validationErrors']&& is_array($_SESSION['validationErrors'])) {
  288.                 $ve $_SESSION['validationErrors'];
  289.                 unset ($_SESSION['validationErrors']);
  290.             }
  291.         }
  292.  
  293.         return $ve;
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Return the objects which failed validation
  299.      *
  300.      * @return The validation error array or null
  301.      */
  302.     function getFailedValidationObjects ($objectType=null)
  303.     {
  304.         static $objects null;
  305.         if (!$objects{
  306.             if (isset($_SESSION['validationFailedObjects']&& is_array($_SESSION['validationFailedObjects'])) {
  307.                 if ($objectType{
  308.                     $objects $_SESSION['validationFailedObjects'][$objectType];
  309.                     unset ($_SESSION['validationFailedObjects'][$objectType]);
  310.                 else {
  311.                     $objects $_SESSION['validationFailedObjects'];
  312.                     unset ($_SESSION['validationFailedObjects']);
  313.                 }
  314.             }
  315.         }
  316.  
  317.         return $objects;
  318.     }
  319.  
  320.  
  321.  
  322.     /**
  323.      * Return a boolean indicating whether or not the specified field failed validation
  324.      *
  325.      * @param objectType       The (string) object type
  326.      * @param field            The fieldname (optional) (default=null)
  327.      *
  328.      * @return boolean indicating whether or not the specified field failed validation
  329.      */
  330.     function hasValidationErrors ($objectType$field=null)
  331.     {
  332.         if (!$objectType{
  333.             return pn_exit ('Empty objectType passed to FormUtil::hasValidationErrors() ...');
  334.         }
  335.  
  336.         if (!$field{
  337.             return pn_exit ('Empty field passed to FormUtil::hasValidationErrors() ...');
  338.         }
  339.  
  340.         $ve FormUtil::getValidationErrors ();
  341.         return (boolean)($ve[$objectType][$field]);
  342.     }
  343.  
  344.  
  345.     /**
  346.      * Get the required field marker (or nothing) for the specified field
  347.      *
  348.      * @param objectType       The (string) object type
  349.      * @param field            The fieldname
  350.      *
  351.      * @return The validation error marker or an empty string
  352.      */
  353.     function getValidationFieldMarker ($objectType$field)
  354.     {
  355.         if (FormUtil::hasValidationErrors($objectType$field)) {
  356.             return _VALIDATION_MARKER;
  357.         }
  358.  
  359.         return _MARKER_NONE;
  360.     }
  361.  
  362.  
  363.     /**
  364.      * Get the validation error for the specified field
  365.      *
  366.      * @param objectType       The (string) object type
  367.      * @param field            The fieldname to get the error for
  368.      *
  369.      * @return The validation error or an empty string
  370.      */
  371.     function getValidationError ($objectType$field)
  372.     {
  373.         if (!FormUtil::hasValidationErrors($objectType$field)) {
  374.             return '';
  375.         }
  376.  
  377.         $ve FormUtil::getValidationErrors ();
  378.         $error $ve[$objectType][$field];
  379.         if ($error{
  380.             $error '&nbsp;' $error;
  381.         }
  382.  
  383.         return $error;
  384.     }
  385.  
  386.  
  387.     /**
  388.      * Get the appropriate field marker
  389.      *
  390.      * @param objectType       The (string) object type
  391.      * @param validationInfo   The plain (non-structured) validation array
  392.      * @param field            The fieldname
  393.      *
  394.      * @return The a marker string or an 'nbsp';
  395.      */
  396.     function getFieldMarker ($objectType$validationInfo$field)
  397.     {
  398.         if (FormUtil::hasValidationErrors($objectType$field)) {
  399.             return _VALIDATION_MARKER;
  400.         }
  401.         else
  402.         if (FormUtil::isRequiredField($validationInfo$field)) {
  403.             return _REQUIRED_MARKER;
  404.         }
  405.  
  406.         return _MARKER_NONE;
  407.     }
  408.  
  409.  
  410.     /**
  411.      * Return a newly created pnFormRender instance with the given name
  412.      *
  413.      * @return The newly created pnFormRender instance.
  414.      */
  415.     function newPNForm ($name)
  416.     {
  417.         Loader::requireOnce('includes/pnForm.php');
  418.         return new pnFormRender ($name);
  419.     }
  420. }

Documentation generated on Fri, 18 Jul 2008 21:45:05 +0200 by phpDocumentor 1.4.1