Main Page | Directories | File List | File Members

form.php

Go to the documentation of this file.
00001 <?php
00002 # This file is part of the Savane project
00003 # <http://gna.org/projects/savane/>
00004 #
00005 # $Id: form.php 5397 2006-02-15 22:55:15Z yeupou $
00006 #
00007 #  Copyright 2004-2006 (c) Mathieu Roy <yeupou--gnu.org>
00008 # 
00009 # The Savane project is free software; you can redistribute it and/or
00010 # modify it under the terms of the GNU General Public License
00011 # as published by the Free Software Foundation; either version 2
00012 # of the License, or (at your option) any later version.
00013 #
00014 # The Savane project is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 #
00019 # You should have received a copy of the GNU General Public License
00020 # along with the Savane project; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 # To use this form that disallow duplicates:
00024 #    - form_header must be used on the form
00025 #    - form_check must be used before any insert in the db after submission
00026 #    - form_clean must be used after succesful item submission
00027 #
00028 
00029 # Start the form with unique ID, store it in the database
00030 function form_header ($action, $form_id=false, $method="post", $extra=false)
00031 {
00032   if ($extra)
00033     { $extra = " $extra"; };
00034     
00035   # Keep previous form id, in case of form that are recreated on failure
00036   if (!$form_id)
00037     { 
00038       mt_srand((double)microtime()*1000000);
00039       $form_id=md5(mt_rand(0,1000000));
00040     }
00041   $result = db_query("INSERT INTO form (form_id,timestamp,user_id) VALUES ('$form_id','".time()."','".user_getid()."')");
00042   if (db_affected_rows($result) != 1)
00043     { fb(_("System error while creating the form, report it to admins"), 1); }
00044   
00045   return '
00046   <form action="'.$action.'" method="'.$method.'"'.$extra.'>'.form_input("hidden","form_id",$form_id);
00047 
00048 }
00049 
00050 # Usual input
00051 function form_input ($type, $name, $value="")
00052 {
00053   if ($value != "")
00054     { $value = 'value="'.$value.'"'; }
00055   return '
00056     <input type="'.$type.'" name="'.$name.'" '.$value.' />';
00057 
00058 }
00059 
00060 # Add submit button
00061 function form_submit($text=false, $submit_name="update")
00062 {
00063   if (!$text)
00064     { $text = _("Submit"); }
00065   
00066   return form_input("submit", $submit_name, $text);
00067 
00068 
00069 }
00070 
00071 # Close the form, with submit button
00072 function form_footer ($text=false, $submit_name="update")
00073 {
00074   return '
00075     <div class="center">
00076       '.form_submit($text, $submit_name).'
00077     </div>
00078   </form>';
00079 
00080 }
00081 
00082 # Check whether this is a duplicate or not: return true if the form
00083 # is ok.
00084 # Exit if we found sql wildcards: forged form, probably.
00085 # We do need this extra check for anynomous users. Logged in users can forge
00086 # their id and remove all the form id of their user, if they wish. Its their
00087 # problem.
00088 function form_check ($form_id)
00089 {
00090   if (user_getid() == 0 &&
00091       (strspn($form_id, "abcdefghijklmnopqrstuvwxyz0123456789") != strlen($form_id)))
00092     {
00093       fb(_("Unrecognized unique form_id"), 1);
00094       return 0;
00095     }
00096 
00097   if (db_numrows(db_query("SELECT form_id FROM form WHERE user_id='".user_getid()."' AND form_id='".addslashes($form_id)."'")) < 1)
00098     {
00099       fb(_("Duplicate Post: this form was already submitted."),1);
00100       return 0;
00101     }
00102 
00103   return 1;
00104 }
00105 
00106 # Remove form_id from database: the item was posted
00107 function form_clean ($form_id)
00108 {
00109   $success = db_affected_rows(db_query("DELETE FROM form WHERE user_id='".user_getid()."' AND form_id='".safeinput($form_id)."'"));
00110   if (!$success)
00111     { fb(_("Error during unique form_id removal"), 1); }
00112   
00113   return $success;
00114 }
00115 
00116 
00117 
00118 
00119 ?>

Generated on Sun Feb 26 13:23:03 2006 for Savane PHP Frontend Developer Reference by  doxygen 1.4.4