PHP Classes

File: vendor/jackbooted/forms/Request.php

Recommend this page to a friend!
  Classes of Brett Dutton   JackBooted PHP Framework   vendor/jackbooted/forms/Request.php   Download  
File: vendor/jackbooted/forms/Request.php
Role: Class source
Content type: text/plain
Description: Class source
Class: JackBooted PHP Framework
Web application framework using simplified MVC
Author: By
Last change:
Date: 8 years ago
Size: 4,085 bytes
 

Contents

Class file image Download
<?php
namespace Jackbooted\Forms;

use \
Jackbooted\Security\Cryptography;
use \
Jackbooted\Security\TamperGuard;
/**
 * @copyright Confidential and copyright (c) 2016 Jackbooted Software. All rights reserved.
 *
 * Written by Brett Dutton of Jackbooted Software
 * brett at brettdutton dot com
 *
 * This software is written and distributed under the GNU General Public
 * License which means that its source code is freely-distributed and
 * available to the general public.
 */

/**
 * Class for Managing Form Variables
 */
class Request extends PipeLine {
    private static
$defaultInstance = null;

    public static function
init () {
       
self::$defaultInstance = new Request ( );
    }

   
/**
     * Gets the current form variables. This is called with Request::get because
     * we want to get a pointer, not a copy. The form variables and query
     * string variables are considered the same
     * @param $key
     * @param $def
     * @return ?#M#P#CRequest.defaultInstance.getVar
     */
   
public static function get ( $key=null, $def=null ) {
        return
self::$defaultInstance->getVar ( $key, $def );
    }

   
/**
     * Save the value into the session
     * @param $key
     * @param $val
     * @return void
     */
   
public static function set ( $key, $val ) {
       
self::$defaultInstance->setVar ( $key, $val );
    }

    public static function
check ( ) {
        return
TamperGuard::check ( self::$defaultInstance );
    }

   
/**
     * @param $varsToProcess
     * @return void
     */
   
public function __construct ( &$varsToProcess=null ) {
       
parent::__construct();
        if (
$varsToProcess == null ) $this->formVars = $this->getRequestVars ();
        else if (
is_array ( $varsToProcess ) ) $this->formVars = $varsToProcess;
        else if (
is_string ( $varsToProcess ) ) $this->formVars = $this->convertQueryStringToArray ( $varsToProcess );

       
$this->decryptRequestVars ( $this->formVars );
    }

    private function
decryptRequestVars ( &$arr ) {
        foreach (
$arr as $key => $val ) {
            if (
is_string ( $arr[$key] ) ) {
               
$arr[$key] = Cryptography::de ( $arr[$key] );
            }
            else if (
is_array ( $arr[$key] ) ) {
               
$this->decryptRequestVars ( $arr[$key] );
            }
        }
    }

    private function
getRequestVars ( ) {
       
$vars = array_merge ( $_GET, $_POST );
        if (
get_magic_quotes_gpc () ) {
           
$this->stripSlashes ( $vars );
        }
        return
$vars;
    }

    private function
stripSlashes ( &$arr ) {
        foreach (
$arr as $key => $val ) {
            if (
is_string ( $arr[$key] ) ) {
               
$arr[$key] = stripslashes ( $arr[$key] );
            }
            else if (
is_array ( $arr[$key] ) ) {
               
$this->stripSlashes ( $arr[$key] );
            }
        }
    }

    private function
convertQueryStringToArray ( $queryString ) {
       
$rawArray = explode ( '&', $queryString );
       
$qArray = [];
        foreach (
$rawArray as $element ) {
            list (
$key, $val ) = explode ( '=', $element );
           
$qArray[$key] = urldecode ( $val );
        }
        return
$qArray;
    }

   
/**
     * Gets the current form variables. This is called with Request::get because
     * we want to get a pointer, not a copy. The form variables and query
     * string variables are considered the same
     * @param $key
     * @param $def
     * @return array|string
     */
   
public function getVar ( $key=null, $def=null ) {
       
// if no key then give them the lot
       
if ( $key === null ) return $this->formVars;

        if ( isset (
$this->formVars[$key] ) ) {
            return
$this->formVars[$key];
        }
        else if (
$def === null ) {
            return
'';
        }
        else {
           
$this->setVar ( $key, $def );
            return
$def;
        }
    }

   
/**
     * @param $key
     * @param $val
     * @return void
     */
   
public function setVar ( $key, $val ) {
       
$this->formVars[$key]=$val;
    }
}