3 * Form management functions
7 * This file is part of GForge.
9 * GForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * GForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * form_generate_key() - Returns a new key, and registers it in the db.
28 * @return int A new identifier.
31 function form_generate_key() {
32 global $sys_database_type;
36 // there's about 99.999999999% probability this loop will run only once :)
38 $key = md5(microtime() + rand() + $_SERVER["REMOTE_ADDR"]);
39 if ( $sys_database_type == "mysql" ) {
40 $sql = "SELECT * FROM form_keys WHERE `key`='".$key."'";
42 $sql = "SELECT * FROM form_keys WHERE key='".$key."'";
45 if (!db_numrows($res)) {
49 if ( $sys_database_type == "mysql" ) {
50 $res = db_query("INSERT INTO form_keys (`key`,is_used,creation_date) VALUES ('".$key."',0,".time().")");
52 $res = db_query("INSERT INTO form_keys (key,is_used,creation_date) VALUES ('".$key."',0,".time().")");
63 * form_key_is_valid() - Checks the db to see if the given key is already used. In case it�s not already used
67 * @return boolean True if the given key is already used. False if not.
70 function form_key_is_valid($key) {
71 global $sys_database_type;
74 if ( $sys_database_type == "mysql" ) {
75 $sql = "SELECT * FROM form_keys WHERE `key`='$key' and is_used=0 FOR UPDATE";
77 $sql = "SELECT * FROM form_keys WHERE key='$key' and is_used=0 FOR UPDATE";
80 if (!$res || !db_numrows($res)) {
84 if ( $sys_database_type == "mysql" ) {
85 $sql = "UPDATE form_keys SET is_used=1 WHERE `key`='$key'";
87 $sql = "UPDATE form_keys SET is_used=1 WHERE key='$key'";
99 * form_release_key() - Releases the given key if it is already used. If the given key it�s not in the db, it returns false.
101 * @param int The key.
102 * @return boolean True if the given key is successfully released. False if not.
105 function form_release_key($key) {
106 global $sys_database_type;
109 if ( $sys_database_type == "mysql" ) {
110 $sql = "SELECT * FROM form_keys WHERE `key`='$key' FOR UPDATE";
112 $sql = "SELECT * FROM form_keys WHERE key='$key' FOR UPDATE";
115 if (!$res || !db_numrows($res)) {
119 if ( $sys_database_type == "mysql" ) {
120 $sql = "UPDATE form_keys SET is_used=0 WHERE `key`='$key'";
122 $sql = "UPDATE form_keys SET is_used=0 WHERE key='$key'";