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() {
35 // there's about 99.999999999% probability this loop will run only once :)
37 $key = md5(microtime() + rand() + $_SERVER["REMOTE_ADDR"]);
38 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1', array ($key));
39 if (!db_numrows($res)) {
43 $res = db_query_params('INSERT INTO form_keys (key,is_used,creation_date) VALUES ($1, 0, $2)', array ($key,time()));
53 * form_key_is_valid() - Checks the db to see if the given key is already used. In case it's not already used
57 * @return boolean True if the given key is already used. False if not.
60 function form_key_is_valid($key) {
61 // Fail back mode if key is empty. This can happen when there is
62 // a problem with the generation. In this case, it may be better
63 // to disable this check instead of blocking all the application.
68 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1 and is_used=0 FOR UPDATE', array ($key));
69 if (!$res || !db_numrows($res)) {
73 $res = db_query_params ('UPDATE form_keys SET is_used=1 WHERE key=$1', array ($key));
83 * 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.
86 * @return boolean True if the given key is successfully released. False if not.
89 function form_release_key($key) {
91 $res = db_query_params ('SELECT * FROM form_keys WHERE key=$1 FOR UPDATE', array ($key));
92 if (!$res || !db_numrows($res)) {
96 $res = db_query_params ('UPDATE form_keys SET is_used=0 WHERE key=$1', array ($key));
107 // c-file-style: "bsd"