<?php
/**
* ClipBoard via Session Example

* @author blueend web:applications AG
* @see http://www.blueend.com/
* @license Free as in Beer

* Use this script at your own risk!
*/
class be_clipboard {
    
    private 
$session_var '_be_clipboard';
    private 
$form_id_param 'form_name';//a param to identify your form
    
    
public function __construct($session_var '_be_clipboard'){
        @
session_start();//We need a session for this example to store our copy values
    
}
    
    
/**
    * Check for copy/past action and return
    * 
    */
    
public function copy_paste(){
        
        if (!(
$_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest')){
            
//This is no ajax call
            
return false;
        }
        
        if(
is_array($_POST) && count($_POST)>0){
            
            
//This is the copy action and we save the input into the session
            
$_SESSION[$this->session_var][$_POST[$this->form_id_param]] = $_POST;
            return 
sprintf(_("Form '%s' saved in clipboard."),$_POST[$this->form_id_param]);
            
        }else if (
$_GET[$this->form_id_param]!='' && is_array($_SESSION[$this->session_var][$_GET[$this->form_id_param]])){
            
            
//We read the values for the given form
            
$flat $this->flatten_array($_SESSION[$this->session_var][$_GET[$this->form_id_param]]);
            
header("X-JSON: ".json_encode(array('form_copy_found'=>true)));
            return 
json_encode($flat);
        }else{
            
header("X-JSON: ".json_encode(array('form_copy_found'=>false)));
            return 
"No valid formdata found";
        }
    }
    
    
/**
    * Flatten array structure to get a JS compatible syntax
    * 
    * @param array $src
    * @param string $key_prefix
    * @return array
    */
    
public function flatten_array($src$key_prefix ''){
        
$flat = array();
        foreach(
$src as $k => $v){
            if(
is_array($v)){
                
$flat array_merge($flat$this->flatten_array($v,$k));
            }else if(
$key_prefix!=''){
                
$flat[$key_prefix.'['.$k.']'] = $v;
            }else{
                
$flat[$k] = $v;
            }
        }
        return 
$flat;
    }
}

$clipbard = new be_clipboard();
if(
$message $clipbard->copy_paste()){
    print 
$message;
    die();
//Stop script execution here
}
?>