Login   Register  
PHP Classes
elePHPant
Icontem

File: form_handler.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Max V. Moiseenko  >  form_handler  >  form_handler.php  >  Download  
File: form_handler.php
Role: ???
Content type: text/plain
Description: definition of a class
Class: form_handler
Author: By
Last change:
Date: 13 years ago
Size: 18,829 bytes
 

Contents

Class file image Download
<?
include("html_mime_mail.php");

################################################################################################
class form_handler extends html_mime_mail{
  var $form_fields=array();//form fields
  var $title;//form title
  var $lang='ru';//language
  var $answer="";//answer table
  var $form_check="true";//sring which will be iserted into javascript check function
  var $url='.';//path at 'action' option of form

  /**************************************************************************************/
  //constutor
  //public void
  function form_handler($lang){
    $this->lang=$lang;
  }
  /**************************************************************************************/
  //add fields text,hidden,textarea,password,submit,reset
  //public void
  function add_field($name,$ru,$en, $value_ru='',$value_en='', $type='text', $obligation=false, $size='50'){
    $this->form_fields[count($this->form_fields)]=array(
      'name'=>$name,
      'ru'=>$ru,
      'en'=>$en,
      'type'=>$type,
      'value_ru'=>$value_ru,
      'value_en'=>$value_en,
      'size'=>$size,
      'obligation'=>$obligation
    );
  }
  /**************************************************************************************/
  //add field checkbox
  //public void
  function add_checkbox($name,$ru,$en, $value='', $type='checkbox', $obligation=false, $checked=false){
    $this->form_fields[count($this->form_fields)]=array(
      'name'=>$name,
      'ru'=>$ru,
      'en'=>$en,
      'value'=>$value,
      'type'=>$type,
      'checked'=>$checked,
      'obligation'=>$obligation
    );
  }
  /**************************************************************************************/
  /* add field select
   * example format of $values
   * $values=array( 
   *   "email" => array('ru'=>"e-mail", 'en'=>"e-mail", 'selected'=>true),
   *   "fax"   => array('ru'=>"факс", 'en'=>"fax", 'selected'=>false) );
   */
  //public void
  function add_select($name,$ru,$en, $values, $obligation=false, $size=1, $multiple=false){
    $this->form_fields[count($this->form_fields)]=array(
      'name'=>$name,
      'ru'=>$ru,
      'en'=>$en,
      'type'=>"select",
      'values'=>$values,
      'size'=>$size,
      'multiple'=>$multiple,
      'obligation'=>$obligation
    );
  }
  /**************************************************************************************/
  /* add feild radio
   * example format of $values
   * $values=array( 
   *   "email" => array('ru'=>"e-mail", 'en'=>"e-mail", 'checked'=>true),
   *   "fax"   => array('ru'=>"факс",'en'=>"fax",'checked'=>false) );
   */
  //public void
  function add_radio($name,$ru,$en, $values, $obligation=false){
    $this->form_fields[count($this->form_fields)]=array(
      'name'=>$name,
      'ru'=>$ru,
      'en'=>$en,
      'type'=>"radio",
      'values'=>$values,
      'obligation'=>$obligation
    );
  }
  /**************************************************************************************/
  //add title of form
  //public void
  function add_title($ru,$en){
    $this->title['ru']=$ru;
    $this->title['en']=$en;
  }
  /**************************************************************************************/
  //add commet into form
  //public void
  function add_comment($ru,$en){
    $this->form_fields[count($this->form_fields)]=array(
      'ru'=>$ru,
      'en'=>$en,
      'type'=>"comment"
    );
  }
  /**************************************************************************************/
  //add file field
  //public void
  function add_file($name, $ru,$en,$filetype,$filesize=20000, $obligation=false){
    $this->form_fields[count($this->form_fields)]=array(
      'name'=>$name,
      'ru'=>$ru,
      'en'=>$en,
      'filetypes'=>$filetype,
      'filesize'=>$filesize,
      'obligation'=>$obligation,
      'type'=>"file"
    );
  }
  /**************************************************************************************/
  //print form into html output
  //public string
  function print_form(){
    $str ="<form action='".$this->url."' method=post name='fomer' enctype='multipart/form-data' onsubmit='return check();'>";
    $str.="<p><table width=510 border=0 cellspacing=0 cellpadding=1 bgcolor=#cccccc><tr><td>";
    $str.="<table width=100% border=0 cellspacing=0 cellpadding=2 bgcolor=#ffffff>";
    $str.="<tr bgcolor=#cccccc><td colspan=2><b>".$this->title[$this->lang]."</b></td></tr>";
    $str.="<tr><td width=1%><img src='".LEVEL."images/spacer.gif' width=190 height=1></td><td width=*><img src='".LEVEL."images/spacer.gif' width=310 height=1></td></tr>";

    for($i=0;$i<count($this->form_fields);$i++){
      $bg=($i%2)? "#eeeeee" : "#ffffff";
      switch($this->form_fields[$i]['type']){
        case 'text':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang];
          
          if($this->form_fields[$i]['obligation']){
            $this->form_check.=" && (f.elements['".$this->form_fields[$i]['name']."'].value!='".$this->form_fields[$i]['value_'.$this->lang]."')";
            $str.="&nbsp;*</td><td>";
          }else $str.="</td><td>";

          $str.="<input type=".$this->form_fields[$i]['type']." name='".$this->form_fields[$i]['name']."' value='".$this->form_fields[$i]['value_'.$this->lang]."' size=".$this->form_fields[$i]['size']." style='width:100%;'>";
          $str.="</td></tr>";
          break;

        case 'checkbox':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang];
          $str.=($this->form_fields[$i]['obligation'])? "&nbsp;*</td><td>" : "</td><td>";
          $str.="<input type=checkbox name='".$this->form_fields[$i]['name']."'";
          $str.=($this->form_fields[$i]['checked'])? " checked>" : ">";
          $str.="</td></tr>";
          break;

        case 'textarea':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang];
          if($this->form_fields[$i]['obligation']){
            $this->form_check.=" && (f.elements['".$this->form_fields[$i]['name']."'].value!='".$this->form_fields[$i]['value_'.$this->lang]."')";
            $str.="&nbsp;*</td><td>";
          }else $str.="</td><td>";
          $str.="<textarea name='".$this->form_fields[$i]['name']."' cols=50 rows=5 style='width:100%;'>".$this->form_fields[$i]['value_'.$this->lang]."</textarea>";
          $str.="</td></tr>";
          break;

        case 'hidden':
          $str.="<input type=hidden name='".$this->form_fields[$i]['name']."' value='".$this->form_fields[$i]['value']."'>";
          break;

        case 'select':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang];
          if($this->form_fields[$i]['obligation']){
            $this->form_check.=" && (f.elements['".$this->form_fields[$i]['name']."'].value!='".$this->form_fields[$i]['value']."')";
            $str.="&nbsp;*</td><td>";
          }else $str.="</td><td>";
          $str.="<select name='".$this->form_fields[$i]['name'];
          $str.=($this->form_fields[$i]['multiple'])? "[]' multiple" : "'";
          $str.=($this->form_fields[$i]['size'] > 1)? " size=".$this->form_fields[$i]['size']." style='width:100%;'>" : " style='width:100%;'>";
          while(list($key,$val)=each($this->form_fields[$i]['values']) ){
            $str.="<option value='".$key."'";
            $str.=($val['selected'])? " selected>" : ">";
            $str.=$val[$this->lang]."</option>";
          }
          $str.="</select>";
          $str.="</td></tr>";
          break;

        case 'radio':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang];
          if($this->form_fields[$i]['obligation']){
            $this->form_check.=" && (f.elements['".$this->form_fields[$i]['name']."'].value!='".$this->form_fields[$i]['value']."')";
            $str.="&nbsp;*</td><td>";
          }else $str.="</td><td>";
          while(list($key,$val)=each($this->form_fields[$i]['values']) ){
            $str.="<input type=radio name='".$this->form_fields[$i]['name']."' value='".$key."'";
            $str.=($val['checked'])? " checked>&nbsp;" : ">&nbsp;";
            $str.=$val[$this->lang]."<br>";
          }
          $str.="</td></tr>";
          break;

        case 'comment':
          $str.="<tr bgcolor=$bg><td class=doc colspan=2>";
          $str.=$this->form_fields[$i][$this->lang];
          $str.="</td></tr>";
          break;

        case 'file':
          $str.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
          $str.="<input type=file name='".$this->form_fields[$i]['name']."' style='width:100%;'>";
          $str.="</td></tr>";
          break;

        case 'submit':
          $str.="<tr bgcolor=$bg><td class=doc colspan=2>".$this->form_fields[$i][$this->lang];
          $str.="<input type=submit name='".$this->form_fields[$i]['name']."' value='".$this->form_fields[$i]['value_'.$this->lang]."'>";
          $str.="</td></tr>";
          break;

        case 'reset':
          $str.="<tr bgcolor=$bg><td class=doc colspan=2>".$this->form_fields[$i][$this->lang];
          $str.="<input type=reset name='".$this->form_fields[$i]['name']."' value='".$this->form_fields[$i]['value_'.$this->lang]."'>";
          $str.="</td></tr>";
          break;

        case 'image';
        default:
          break;

      }
    }
    $str.="<tr bgcolor=#cccccc><td colspan=2 align=center><input type=submit name=ok value='Ok'></td></tr></table>";
    $str.="</td></tr></table><br>";
    $str.="</p></form>";
    if($this->form_check!="true"){
      if($this->lang=='ru'){
        $str.="<p><span class=doc>* - обязательны для заполнения</span></p>";
      }else{
        $str.="<p><span class=doc>* - are obligatory for filling</span></p>";
      }
      $str.="<script><!--".CRLF;
      $str.="var f=document.forms['fomer'];".CRLF;
      $str.="function check(){".CRLF;
      $str.=" if(".$this->form_check."){".CRLF;
      $str.=" f.submit();".CRLF;
      $str.=" return true;".CRLF;
      $str.="}else{".CRLF;
      if($this->lang=='ru'){
        $str.=" alert('Обязательные поля для заполнения обозначены звёздочкой!');".CRLF;
      }else{
        $str.=" alert('The obligatory fields for filling are marked by an asterisk!');".CRLF;
      }
      $str.=" return false;".CRLF;
      $str.="}}".CRLF;
      $str.="//--></script>";
    }
    return $str;
  }
  /**************************************************************************************/
  //convert plain text into html specialchars
  //private string
  function htmlspecial($str){
    $str=htmlspecialchars(stripslashes($str));
    $str=ereg_replace("\n","<br>",$str);
    return $str;
  }
  /**************************************************************************************/
  //make anwer string as html table
  //private string
  function make_html_table(){
    global $HTTP_POST_FILES,$PATH_TRANSLATED,$SERVER_NAME,$PHP_SELF;
    $this->answer ="<p><table width=510 border=0 cellspacing=0 cellpadding=1 bgcolor=#cccccc><tr><td>";
    $this->answer.="<table width=100% border=0 cellspacing=0 cellpadding=3 bgcolor=#ffffff>";
    $this->answer.="<tr bgcolor=#cccccc><td colspan=2><b>".$this->title[$this->lang]."</b></td></tr>";
    //<img src='".LEVEL."images/arrow-down-blue.gif' width=16 height=16 align=right>
    $this->answer.="<tr><td width=30%></td><td width=*></td></tr>";

    for($i=0;$i<count($this->form_fields);$i++){
      $bg=($i%2)? "#eeeeee" : "#ffffff";
      switch($this->form_fields[$i]['type']){
        case 'text':
        case 'textarea':
          $this->answer.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
          $this->answer.=$this->htmlspecial( $GLOBALS[$this->form_fields[$i]['name']] );
          $this->answer.="</td></tr>";
          break;

        case 'select':
          $this->answer.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
          if( is_array($GLOBALS[$this->form_fields[$i]['name']]) ){
            for($j=0;$j<count($GLOBALS[$this->form_fields[$i]['name']]);$j++){
              $this->answer.=$this->form_fields[$i]['values'][ $GLOBALS[$this->form_fields[$i]['name']][$j] ][$this->lang]."<br>";
            }
          }else{
            $this->answer.=$this->form_fields[$i]['values'][ $GLOBALS[$this->form_fields[$i]['name']] ][$this->lang];
          }
          $this->answer.="</td></tr>";
          break;

        case 'radio':
          $this->answer.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
          $this->answer.=$this->form_fields[$i]['values'][ $GLOBALS[$this->form_fields[$i]['name']] ][$this->lang];
          $this->answer.="</td></tr>";
          break;

        case 'checkbox':
          if($GLOBALS[$this->form_fields[$i]['name']]=="on"){
            $this->answer.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
            $this->answer.=$this->form_fields[$i]['value'];
            $this->answer.="</td></tr>";
          }
          break;

        case 'file':
          if($this->is_file_uploaded($HTTP_POST_FILES[ $this->form_fields[$i]['name'] ]['tmp_name'])){
            $this->form_fields[$i]['tmp_name']=$HTTP_POST_FILES[ $this->form_fields[$i]['name'] ]['tmp_name'];
            $this->form_fields[$i]['post_name']=$HTTP_POST_FILES[ $this->form_fields[$i]['name'] ]['name'];
            $this->form_fields[$i]['post_type']=$HTTP_POST_FILES[ $this->form_fields[$i]['name'] ]['type'];
            $this->form_fields[$i]['post_size']=$HTTP_POST_FILES[ $this->form_fields[$i]['name'] ]['size'];
            $this->form_fields[$i]['move_into']=dirname($PATH_TRANSLATED)."/".$this->form_fields[$i]['post_name'];

            $this->answer.="<tr bgcolor=$bg><td class=doc>".$this->form_fields[$i][$this->lang]."</td><td>";
            if( !eregi($this->form_fields[$i]['filetypes'], $this->form_fields[$i]['post_type'] ) ){
              $this->answer.="Image type uncorrect</td></tr>";
              break;
            }elseif($this->form_fields[$i]['post_size'] > $this->form_fields[$i]['filesize']){
              $this->answer.="Image size exceed maximum</td></tr>";
              break;
            }else{
              @$this->move($this->form_fields[$i]['tmp_name'], $this->form_fields[$i]['move_into']);

              $img="img.php?id=".$this->encrypt($this->form_fields[$i]['post_name'])."&name=".urlencode($this->form_fields[$i]['post_name']);
              $this->add_html_image("".$this->form_fields[$i]['post_name'], $img, $this->form_fields[$i]['post_type']);

              $this->answer.="<img src='".$img."' alt='";
              $this->answer.="name: ".$this->form_fields[$i]['post_name']."\n";
              $this->answer.="size: ".$this->form_fields[$i]['post_size']."\n";
              $this->answer.="type: ".$this->form_fields[$i]['post_type'];
              $this->answer.="'><br>";
              $this->answer.="</td></tr>";
            }


            //$this->del($this->form_fields[$i]['move_into']);
          }
          break;


        case 'hidden':
        case 'password':
        case 'submit':
        case 'reset':
        case 'image':
        default:
          break;
      }
    }
    $this->answer.="</table></td></tr></table></p>";
  }

  /**************************************************************************************/
  //send e-mail message
  //public void
  function send_form($to,$subject=''){
    $this->add_html($this->answer, "");
    $this->build_message();
    $this->send($to, "you@email.adress", $subject);
  }

  /**************************************************************************************/
  //is really file uploaded from remote computer
  //private boolean
  function is_file_uploaded($filename) {
    if(function_exists(is_uploaded_file))return is_uploaded_file($filename);
    else{
      if(!$tmp_dir=get_cfg_var('upload_tmp_dir'))$tmp_dir=dirname(tempnam('',''));
      $tmp_file=$tmp_dir.'/'.basename($filename);
      if(WIN) return (ereg_replace('/+','',$tmp_file)==$filename);
      else return (ereg_replace('/+','/',$tmp_file)==$filename);
    }
  }
  /**************************************************************************************/
  var $challenge="secret string";
  
  function validate_param($param,$response){
    $param=$this->decrypt($param);
    $response=$this->decrypt($response);
    $exspected_response = md5($param.":".$this->challenge);
    return $exspected_response == $response;
  }
  /**************************************************************************************/
  function encrypt($str){
    return urlencode(md5($str.":".$this->challenge));
  }
  /**************************************************************************************/
  function decrypt($str){
    return urldecode($str);
  }
  /**************************************************************************************/
  //public string
  function mime_types($file){
    switch(strtolower(ereg_replace("^(.+)([\.])(.+)$","\\3",$file))){
      case "bmp": $mime="image/bmp";  break;
      case "gif": $mime="image/gif";  break;
      case "jpeg": case "jpg": case "jpe": $mime="image/pjpeg"; break;
      case "png": $mime="image/png"; break;
    }
    return $mime;
  }
  /**************************************************************************************/
  //public boolean
  function read_file($file){
    @set_time_limit(600);
    header("Content-type: ".$this->mime_types($file));
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($file);
  }
  /**************************************************************************************/
  //move file on a server
  //public boolean
  function move($from,$to){
    //if(!file_exists($from) || file_exists($to)) return false; else
    if(!rename($from,$to)) return false;
    return true;
  }
  /**************************************************************************************/
  //delete file
  //public boolean
  function del($file){
    if(!file_exists($file)){
      return false;
    }elseif(is_file($file)){
      if(!unlink($file)) return false;
    }
    return true;
  }
  /**************************************************************************************/
}//end class
################################################################################################

?>