3 * FusionForge form management
5 * Copyright 2005, GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * form_generate_key() - Returns a new key, and registers it in the db.
29 * @return int A new identifier.
32 function form_generate_key() {
33 global $sys_database_type;
37 // there's about 99.999999999% probability this loop will run only once :)
39 $key = md5(microtime() + rand() + $_SERVER["REMOTE_ADDR"]);
40 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1', array ($key));
41 if (!db_numrows($res)) {
45 $res = db_query_params('INSERT INTO form_keys (key,is_used,creation_date) VALUES ($1, 0, $2)', array ($key,time()));
55 * form_key_is_valid() - Checks the db to see if the given key is already used. In case it's not already used
59 * @return boolean True if the given key is already used. False if not.
62 function form_key_is_valid($key) {
63 global $sys_database_type;
65 // Fail back mode if key is empty. This can happen when there is
66 // a problem with the generation. In this case, it may be better
67 // to disable this check instead of blocking all the application.
72 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1 and is_used=0 FOR UPDATE', array ($key));
73 if (!$res || !db_numrows($res)) {
77 $res = db_query_params ('UPDATE form_keys SET is_used=1 WHERE key=$1', array ($key));
87 * 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.
90 * @return boolean True if the given key is successfully released. False if not.
93 function form_release_key($key) {
94 global $sys_database_type;
97 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1 FOR UPDATE', array ($key));
98 if (!$res || !db_numrows($res)) {
102 $res = db_query_params ('UPDATE form_keys SET is_used=0 WHERE key=$1', array ($key));
113 // c-file-style: "bsd"