Zend - The PHP Company




Forms

Add Code


Phorm Jr.  

Type: application
Added by: Darguz
Entered: 15/03/2000
Last modified: 03/12/2001
Rating: ***** (5 votes)
Views: 21942
Form processing program. A trimmed-down version of the popular Phorm script, Phorm Jr. has just email, acknowledgement and text log capabilities, but with the same security and error-handling features of its big brother.


<?
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 1999 Holotech Enterprises (phorm@holotech.net)
 *
 * This is a trimmed-down version of the popular Phorm script, with only
 * email, acknowledgement and text log functions. The full version of 
 * Phorm is available at http://www.phorm.com/, as well as full
 * documentation and example files for Phorm Jr.
 *
 * You may freely distribute this script as-is and without modifications.
 * You may use this script freely, and modify it for your own purposes.
 * There is no charge for this script, but if you like it, I'd be happy to
 * hear about it at phormjr@phorm.com.
 *
 * I hope you find this program useful. Aloha.
 *                                                 Alan Little
 *                                                 March 2002
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

  
function VarSub(&$ph_string) {
    
$ph_pstring $ph_string;
    while (
ereg("\{\{([^}]*)\}\}"$ph_pstring$ph_regs)) {
      
$ph_pstring str_replace($ph_regs[0], ""$ph_pstring);
      
$ph_SubVar $ph_regs[1];

//    If $$ph_SubVar is an array, break it down
      
ereg("^([A-Za-z0-9_]*)(([[A-Za-z0-9_]*])*)$"$ph_SubVar$ph_regs2);
      
$ph_SubVar $ph_regs2[1];
      global $
$ph_SubVar;

      
$ph_indices split("]["ereg_replace("^[|]$"""$ph_regs2[2]));
      
$ph_SubVal = $$ph_SubVar$ph_idx 0;
      while (
is_array($ph_SubVal))
        
$ph_SubVal $ph_SubVal[$ph_indices[$ph_idx++]];

      
$ph_string str_replace($ph_regs[0], $ph_SubVal$ph_string);
    }
  }

  if (!isset(
$ph_ForceDec)) $ph_ForceDec true;
/*
  Unset any ph_ variables to prevent anyone from trying to sneak something in */
  
while (list($ph_var$ph_val) = each($GLOBALS))
    if (
ereg("^ph_"$ph_var) && !ereg("^ph_ForceDec$"$ph_var)) {
      unset($
$ph_var);
      unset(
$ph_var);
    }
  
reset($GLOBALS);

  
define("ph_DECLARE""{{FORM}}");

  
$ph_Home ".";

  
$ph_Vers "Phorm Jr. v2.0";

  
$ph_HTLink =
    
"<BR><BR><CENTER><FONT SIZE=1>".
    
"<A HREF="http://www.phorm.com/">$ph_Vers</A>".
    
"</FONT></CENTER>";

  
$ph_HTTag =
    
"nnnn--n$ph_Vers by Holotech Enterprises http://www.holotech.net/";

/*
  Just to be sure */
  
$HTTP_REFERER getenv("HTTP_REFERER");
  
$REMOTE_ADDR  getenv("REMOTE_ADDR");

/*
  Record the configuration variables passed from the form, then unset them */
  
if ($ph_ForceDec) {
    while (list(
$ph_var$ph_val) = each($GLOBALS))
      if (
ereg("^PHORM_"$ph_var)) {
        
$ph_varHold[$ph_var] = $ph_val;
        unset($
$ph_var);
      }
  }
  
reset($GLOBALS);

  if (!
$PHORM_LOGQUOT$PHORM_LOGQUOT "'";
  if (!
$PHORM_LDELIM$PHORM_LDELIM "t";

/*
  Restore $PHORM_NAME and process the configuration file */
  
$PHORM_NAME $ph_varHold["PHORM_NAME"];
  
$ph_Config basename($PHORM_NAME).".phm";
  if (
$ph_debug3) echo "<B>NS:</B> Config file $ph_Config<BR>";
  if (!
is_readable($ph_Config)) {
    
$ph_Errs['020'] = "### - Unable to open configuration file $ph_Config.<BR><BR>n";
    
$ph_Abort true;
  }
  else {
    include (
"$ph_Config");
  }

/*
  Check the Referer */
  
if ($PHORM_REFERER && !$ph_Abort) {
    if (
$ph_debug3) echo "<B>NS:</B> Referer<BR>";

    
ereg("https?://([^/]*)/([^?]*)"$HTTP_REFERER$ph_regs);
    
$ph_RefHost $ph_regs[1]; $ph_RefPath $ph_regs[2];

    if (!
ereg("|$ph_RefHost|"$PHORM_REFERER)
    &&  !
ereg("|$ph_RefHost/$ph_RefPath|"$PHORM_REFERER)) {
      
$ph_Errs['080'] = 
        
"### - Invalid access. This Phorm Jr. installation cannot be accessed from [$HTTP_REFERER].";
      
$ph_Abort true;
    }
  }

/*
  Restore only the declared PHORM_ variables from the form */
  
if ($ph_ForceDec && is_array($ph_varHold))
    while (list(
$ph_var$ph_val) = each($ph_varHold))
      if ($
$ph_var == "{{FORM}}") $$ph_var $ph_varHold[$ph_var];

/* If there's no ack template, redirect back to the calling form. Note that
   if $HTTP_REFERER is empty, the visitor will get a "302 Found" result
   (from Apache, anyway) */
  
if (!$PHORM_ACK && !$ph_Abort) {
    if (!
$HTTP_REFERER) {
      
$ph_Alerts[060] = 
        
"### - Warning: No ack template specified, and $HTTP_REFERER ".
        
"is empty. Visitor received 302 Found status message.n";
    }
    
Header("Location:$HTTP_REFERER");
  }

/*
  Check required fields */
  
if ($PHORM_REQ && !$ph_Abort) {
    if (
$ph_debug3) echo "NS: PHORM_REQ";
     
    
$ph_ReqVars explode(" "$PHORM_REQ);
    while (list(,
$ph_ReqVar) = each($ph_ReqVars))
      if (!$
$ph_ReqVar)
        
$ph_VarList.= "<B>$ph_ReqVar</B><BR>";

    if (
$ph_VarList) {
      
$ph_Errs['REQ'] = 
        
"The following required fields are empty:<BR>$ph_VarList";
      
$ph_Abort true;
    }
  }

/*
  Open the acknowledgement template file. */
  
if ($PHORM_ACK && !$ph_Abort)  {
    if (
$ph_debug3) echo "NS: Open Ack Template <B>$PHORM_ACK</B>.<BR>";
    if (!
$ph_af = @fopen($PHORM_ACK,"r")) {
      
$ph_Errs['070'] = 
        
"### - Unable to open acknowledgement template file <B>$PHORM_ACK</B>.<BR><BR>n";
      
$ph_Abort true;
    }
  }

/*
  Open the mail template file. */
  
if ($PHORM_TMPL && !$ph_Abort)  {
    if (
$ph_debug3) echo "NS: Open Mail Template <B>$PHORM_TMPL</B>.<BR>";
    if (!
$ph_tf = @fopen($PHORM_TMPL,"r")) {
      
$ph_Alerts['101'] = "### - Unable to open mail template file <B>$PHORM_TMPL</B>.<BR><BR>n";
    }
  }

/*
  Open the log file */
  
if ($PHORM_LOG && $PHORM_LOGVAR && !$ph_Abort) {
    if (
$ph_debug3) echo "<B>NS:</B> Open Log File <B>$lPHORM_LOG</B>.<BR>";
    if (!
$ph_lf = @fopen($PHORM_LOG"a")) {
      
$ph_Alerts['080'] = "### - Unable to open the text log file $PHORM_LOG.";
      
$PHORM_LOG "";
    }
  }

/*
  Log to text file */
  
if ($PHORM_LOG && $PHORM_LOGVAR && !$ph_Abort) {
    if (
$ph_debug2) echo "<B>JS:</B> Log to text file <B>$PHORM_LOG</B>.<BR>";
    
$ph_LogVars explode(" "$PHORM_LOGVAR);
    
$ph_idx 0;
    
$ph_LogLine "";
    while (list(,
$ph_LogVar) = each($ph_LogVars)) {
      if (
ereg("'"$ph_LogVar)) $ph_Quote $PHORM_LOGQUOT; else $ph_Quote "";
      
$ph_LogVar str_replace("'"""$ph_LogVar);

//    If $$ph_LogVar is an array, break it down
      
ereg("^([A-Za-z0-9_]*)(([[A-Za-z0-9_]*])*)$"$ph_LogVar$ph_regs);
      
$ph_LogVar $ph_regs[1];
      
$ph_indices split("]["ereg_replace("^[|]$"""$ph_regs[2]));
      
$ph_Value = $$ph_LogVar$ph_idx 0;
      while (
is_array($ph_Value))
        
$ph_Value $ph_Value[$ph_indices[$ph_idx++]];

      
$ph_LogLine.= $ph_Quote.$ph_Value.$ph_Quote.$PHORM_LDELIM;
    }
    
fputs($ph_lf$ph_LogLine."n");
  }

/*
  Read the mail template file and mail it to the user */
  
if ($ph_tf && !$ph_Abort) {
    if (
$ph_debug2) echo "<B>JS:</B> Mail Template <B>$PHORM_TMPL</B><BR>";
    if (!
$PHORM_FROM$PHORM_FROM $PHORM_TO;
    
$ph_Message "";

    while(!
feof($ph_tf)) {
      
$ph_tline fgets($ph_tf1024);

//    Parse the line for variable replacements.
      
VarSub($ph_tline);

      
$ph_Message.= $ph_tline;
    } 
// (!feof($ph_tf))
    
$ph_Message.= $ph_HTTag;
    
fclose($ph_tf);

    
$ph_Headers "From: $PHORM_FROMrn".
                  
"X-Mailer: PHP3/$ph_Versrn";
    
Mail($PHORM_TO,
         
$PHORM_SUBJECT,
         
$ph_Message,
         
$ph_Headers);

  } 
// if ($ph_tf)

/*
  Read the ack template and output it. */
  
if ($ph_af && !$ph_Abort)  {
    if (
$ph_debug2) echo "<B>JS:</B> Ack Template <B>$PHORM_ACK</B><BR>";
    while(!
feof($ph_af)) {
      
$ph_aline fgets($ph_af1024);

//    Add our tag at the end of the file.
      
if ((ereg("</BODY>"$ph_aline) || feof($ph_af)) && !$ph_tagd) {
        echo 
$ph_HTLink; echo "<!-- Generated by Phorm -->"$ph_tagd=true; }

//    Parse the line for variable replacements.
      
VarSub($ph_aline);

      echo 
$ph_aline;
    } 
// (!feof($ph_af))
    
fclose($ph_af);
  } 
// (PHORM_ACK)

/*
  Display any fatal error messages to the visitor. Display internal alerts, if PHORM_ALERTTO is
  not defined. */
  
$ph_section "errors";
  if (
$ph_Errs || ($ph_Alerts && ($ph_debug1 || !$PHORM_ALERTTO))) {
    unset(
$ph_Redirect);
    if (
is_array($ph_Errs)) {
      while (list(
$ph_errno$ph_err) = each($ph_Errs)) {
        
$ph_err str_replace("###"$ph_errno$ph_err);
        
$ph_errs.= "$ph_err<BR>n";
      }
      
reset($ph_Errs);
    }
    if (
is_array($ph_Alerts)) {
      while (list(
$ph_alertno$ph_alert) = each($ph_Alerts)) {
        
$ph_alert str_replace("###"$ph_alertno$ph_alert);
        
$ph_alerts.= "$ph_alert<BR>n";
      }
      
reset($ph_Alerts);
    }
    if (
$ph_errs$ph_errmsg "<B>Errors:</B><BR>n$ph_errsn<BR><BR>n";
    if (
$ph_alerts$ph_errmsg.= "<B>Alerts:</B><BR>n$ph_alertsn";
    
$ph_ErrPage $PHORM_NAME."_err.html";
    if (
is_file($ph_ErrPage)) {
      
$ph_errpage implode(""file("$ph_ErrPage"));
      
$ph_errpage str_replace("<!-- Phorm Messages -->"$ph_errmsg$ph_errpage);
      echo 
$ph_errpage;
    }
    else {
      echo 
"<HMTL>n".
           
"<HEAD>n".
           
"<TITLE>Phorm Error</TITLE>n".
           
"</HEAD>n".
           
"<BODY BGCOLOR=#FFC0C0 TEXT=#800000>n".
           
"n";
      if (
$ph_Errs) echo "<FONT SIZE=5>ERROR<BR><BR></FONT>n".
                         
"$ph_errsn";
      if (
$ph_alerts && ($ph_debug1 || !$PHORM_ALERTTO)) {
        echo 
"Alerts:<BR> $ph_alerts";
      }
      echo 
"$ph_HTLinkn".
           
"</BODY></HTML>";
    }
  }

/*
  Email any alerts and errors to the user, if an address was specified. */
  
if (($ph_Alerts || $ph_Errs) && $PHORM_ALERTTO) {
    
$ph_Message "";
    
$ph_errs ""$ph_alerts "";
    if (
is_array($ph_Errs)) {
      while (list(
$ph_errno$ph_err) = each($ph_Errs)) {
        
$ph_err str_replace("###"$ph_errno$ph_err);
        
$ph_errs.= "$ph_errn";
      }
    }
    if (
is_array($ph_Alerts)) {
      while (list(
$ph_alertno$ph_alert) = each($ph_Alerts)) {
        
$ph_alert str_replace("###"$ph_alertno$ph_alert);
        
$ph_alerts.= "$ph_alertn";
      }
    }
    
$ph_errs   ereg_replace("<[^>]*>"""$ph_errs);
    
$ph_alerts ereg_replace("<[^>]*>"""$ph_alerts);

    if (
$ph_errs$ph_Message "Fatal errors displayed to visitor:n$ph_errsnn";
    if (
$ph_alerts$ph_Message.= "Internal Alerts:n$ph_alertsn";

    
$ph_Message.= "Phorm Variables:n";
    while (list(
$ph_key$ph_val) = each($GLOBALS)) {
      if (
ereg("^PHORM_"$ph_key)) {
        
$ph_Message.= "$ph_key$ph_valn";
      }
    }
    
reset($GLOBALS);
    
$ph_Message.= "nHTTP_REFERER: $HTTP_REFERERnREMOTE_ADDR$REMOTE_ADDRn";
    
$ph_Message.= $ph_HTTag;
    if (!
$PHORM_ALERTFRM$PHORM_ALERTFRM "Phorm Processor <Phorm.Processor>";
    
$ph_Headers "From: $PHORM_ALERTFRMrn".
                  
"X-Mailer: PHP3/$ph_Versrn";
    
Mail($PHORM_ALERTTO,
         
"Phorm Alert",
         
$ph_Message,
         
$ph_Headers);
  }
/*
  Phorm Jr. configuration variables. For more detail, see the full 
  documentation available at http://www.phorm.com/

  $PHORM_NAME:    The name of the form. Phorm Jr. appends .phm to this, and
                  looks for a configuration file of that name.
  $PHORM_REFERER: A list of valid referers for your Phorm Jr. installation.
  $PHORM_ALERTTO: Email address to send internal error messages to, such as
                  unable to open the log file or database. If it is not
                  specified, all error messages will be displayed to the
                  visitor. If it is specified, only errors which affect the
                  visitor's operation will be displayed.
  $PHORM_ACK:     An HTML file, containing variable substitutions, to output
                  to the user after form submission. If unspecified, no ack
                  is displayed, and Phorm Jr. attempts to redirect the user
                  back to the URL of the form.
  $PHORM_REQ:     A space-delimited list of all required fields.
  $PHORM_TMPL:    Text template file for formatting contents to be emailed to
                  user. If not specified, contents are not mailed.
  $PHORM_TO:      Email address to send the contents to. It is required if
                  PHORM_TMPL is specified.
  $PHORM_FROM:    Email address for the FROM field of the emailed contents.
                  NOTE: If this is blank, it will be set to the same as 
                  PHORM_TO.
  $PHORM_SUBJECT: Subject field for emailed contents.
  $PHORM_LOG:     The name of a text file to log entries to
  $PHORM_LOGVAR:  A space-delimited list of variables to log to $PHORM_LOG
  $PHORM_LDELIM:  Log delimiter -- a character or string of characters to be
                  placed between variable values in the text log. The default 
                  is a tab character.
  $PHORM_LOGQUOT: The character to use to quote values in the text log. The 
                  default is the ' character.
*/
?>


Usage Example




Rate This Script





Search



This Category All Categories