Login   Register  
PHP Classes
elePHPant
Icontem

array_regenerate_keys

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Antz_IntelliForm  >  All threads  >  array_regenerate_keys  >  (Un) Subscribe thread alerts  
Subject:array_regenerate_keys
Summary:missing function and other fixes
Messages:3
Author:Fabian Schmengler
Date:2010-04-15 22:01:21
Update:2010-04-16 11:34:08
 

  1. array_regenerate_keys   Reply  
Picture of Fabian Schmengler
Fabian Schmengler
2010-04-15 22:01:21
If somebody else wonders what array_regenerate_keys in the purge-method does, I found it on antzcode.com and it can safely replaced with the php core function array_values which does the same.

Two additional changes I made to use the class:

1. changed one line in the submitted-method:

if(!isset($_SESSION['antzSeed'][$id])||!is_array($_SESSION['antzSeed'][$id])) $_SESSION['antzSeed'][$id] = array();

so there will be no E_NOTICE if the key $id does not exist.

2. changed the restore-method:

if (isset($_SESSION['intelliForm'][$key])) $_POST = $_SESSION['intelliForm'][$key];

so that $_POST will not be cleared if there is nothing to restore. Otherwise the data is lost completely and cannot be inserted in the form again if the user sends the form after the seed expired.

  2. Re: array_regenerate_keys   Reply  
Picture of Anthony Gallon
Anthony Gallon
2010-04-16 09:41:55 - In reply to message 1 from Fabian Schmengler
Hi Fabian,

Would you mind posting you current version of the file so I can merge changes to the repository, cheers.

  3. Re: array_regenerate_keys   Reply  
Picture of Fabian Schmengler
Fabian Schmengler
2010-04-16 11:34:08 - In reply to message 2 from Anthony Gallon
Hi,

no problem, my current version looks like that, look what you can use of it:

<?php

/**
* Original class by Anthony Gallon (version 2009-03-11), slightly modified by Fabian Schmengler:
*
* Replaced global vars with class vars:
* - $Antz_IntelliForm_hasrun => Antz_IntelliForm::$hasRun
* - $CONFIG->tmpDir (???) => Antz_IntelliForm::$tmpDir
*
* Changed class constant expireTime to class var, should be changeable from outside!
* @todo refactor that each form is represented by an IntelliForm object?
*
* submitted(): + check isset($_SESSION['antzSeed'][$id]
* purge(): array_regenerate_keys => array_values
* restore(): don't clear $_POST if nothing to restore (!)
*
* Added initialization file Antz_IntelliForm.inc to set default temp dir and THIS_PAGE_URL constant
*
* @author Anthony Gallon
* @author Fabian Schmengler <fschmengler@sgh-it.eu>
* @package Antz
*
*/


/**
* Prevent warnings of resubmitting posted forms, save form data, seed forms with a random token for recognition and to reduce cross site scripting.
* @author antz
* @package Antz
*/
class Antz_IntelliForm
{

/**
* how long are forms kept for ( seconds )
* @static int $expireTime
*/
public static $expireTime = 300;

public static $hasRun = 0;
public static $tmpDir = '/tmp';

/**
* Save form contents for later restoration
*
* @param string $key
* @param int $expire (seconds)
*/
public static function save($key, $expire='')
{
$expire = ($expire=='') ? (Antz_IntelliForm::$expireTime + time()) : ($expire+time());
if(!isset($_SESSION['intelliForm'])||!is_array($_SESSION['intelliForm']))$_SESSION['intelliForm'] = array();
$_SESSION['intelliForm'][$key] = $_POST;
$_SESSION['intelliForm'][$key]['intelliFormExpires'] = $expire;
}


/**
* Restore form contents from a previous save
*
* @param string $key
*/
public static function restore($key)
{
//$_POST = (isset($_SESSION['intelliForm'][$key])) ? $_SESSION['intelliForm'][$key] : array();
if (isset($_SESSION['intelliForm'][$key])) $_POST = $_SESSION['intelliForm'][$key];
}


/**
* Clear a saved form
*
* @param string $key
*/
public static function clear($key)
{
if(isset($_SESSION['intelliForm'][$key])) unset($_SESSION['intelliForm'][$key]);
}


/**
* clear all expired saves
*
*/
public static function purge()
{
if(isset($_SESSION['intelliForm']) && is_array($_SESSION['intelliForm'])){
foreach($_SESSION['intelliForm'] as $key => $post){
if($post['intelliFormExpires'] <= time()){
unset($_SESSION['intelliForm'][$key]);
};
};
};
// clear form seeds ( max 15 forms per page)
while(isset($_SESSION['antzSeed']) && count($_SESSION['antzSeed']) > 15){
//unset($_SESSION['antzSeed'][0]);
//$_SESSION['antzSeed'] = array_values($_SESSION['antzSeed']);
// even better:
array_shift($_SESSION['antzSeed']);
};
}


/**
* Call this before doing anything else, to bypass the pesty confirm prompt
* that appears when resubmitting post content
*
*/
public static function antiRepost()
{

// just in case the function gets called twice in one page load, we would get a bad loop happening!
if(Antz_IntelliForm::$hasRun>0) return;
else Antz_IntelliForm::$hasRun++;

if(isset($_POST['antzSeed'])){
// form has been submitted
$_SESSION['post'] = $_POST;

// move the files to a new temp location
foreach($_FILES as $k => $file){
$suffix = rand(0,999);
if($file['tmp_name']=='') continue;
rename($file['tmp_name'], Antz_IntelliForm::$tmpDir.'/'.$suffix.$file['name']);
//echo $file['tmp_name'].'<br />';
$_FILES[$k]['tmp_name'] = Antz_IntelliForm::$tmpDir.'/'.$suffix.$file['name'];
chmod(Antz_IntelliForm::$tmpDir.'/'.$suffix.$file['name'], 0777);
};

$_SESSION['files']=$_FILES;

// work out the requested page and redirect to it
header('location:'.THIS_PAGE_URL);

die('<script>window.location="'.THIS_PAGE_URL.'"</script>
<a href="'.THIS_PAGE_URL.'">Continue >></a>');

}elseif(isset($_SESSION['post'])){
$_POST = $_SESSION['post'];
$_FILES = $_SESSION['files'];
$_REQUEST = array_merge($_REQUEST, $_POST);
unset($_SESSION['post']);
};
}


/**
* Checks to see if the form has been submitted with a valid seed
*
* @param string $id namespace
* @param bool $del delete the seed
* @return bool $isSubmitted
*/
public static function submitted($id='default', $del=true)
{
if(!isset($_POST['antzSeed'])){return false;};

$seed = $_POST['antzSeed'];
if(!isset($_SESSION['antzSeed'])||!is_array($_SESSION['antzSeed'])){$_SESSION['antzSeed']=array();};
if(!isset($_SESSION['antzSeed'][$id])||!is_array($_SESSION['antzSeed'][$id])) $_SESSION['antzSeed'][$id] = array();

if(in_array($seed, $_SESSION['antzSeed'][$id])){
$tmp = array_flip($_SESSION['antzSeed'][$id]);
if($del) $_SESSION['antzSeed'][$id][$tmp[$seed]]=mt_rand(0,99999999);
unset($tmp, $seed);
return true;
}else{
return false;
};
}

/**
* Plant a seed to ensure forms are accepted by a verified session.
* Check with Antz_IntelliForm::submitted()
* @param string $id
* @return string $htmlHiddenInputAsText
*/
public static function seed($id='default')
{
$seed = mt_rand(0,99999999);
if(!isset($_SESSION['antzSeed'])||!is_array($_SESSION['antzSeed'])){$_SESSION['antzSeed']=array();};
if(!isset($_SESSION['antzSeed'][$id]) || !is_array($_SESSION['antzSeed'][$id])) $_SESSION['antzSeed'][$id] = array();
$_SESSION['antzSeed'][$id][]=$seed;
return '<div style="display: none"><input type="hidden" name="antzSeed" value="'.$seed.'"></div>';
}

}