diff --git a/application/transaction.class.inc.php b/application/transaction.class.inc.php new file mode 100644 index 000000000..ffc55c257 --- /dev/null +++ b/application/transaction.class.inc.php @@ -0,0 +1,111 @@ + "gui", + "key_type" => "autoincrement", + "key_label" => "", + "name_attcode" => "expiration_date", + "state_attcode" => "", + "reconc_keys" => array(), + "db_table" => "priv_transaction", + "db_key_field" => "id", + "db_finalclass_field" => "", + ); + MetaModel::Init_Params($aParams); + MetaModel::Init_AddAttribute(new AttributeDateTime("expiration_date", array("allowed_values"=>null, "sql"=>"expiration_date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array()))); + + MetaModel::Init_SetZListItems('details', array('expiration_date')); // Attributes to be displayed for the complete details + MetaModel::Init_SetZListItems('list', array('expiration_date')); // Attributes to be displayed for a list + } + /** + * Create a new transaction, store it in the database and return its id + * @param void + * @return int The identifier of the new transaction + */ + public static function GetNewTransactionId() + { + // First remove all the expired transactions... + self::CleanupExpiredTransactions(); + $oTransaction = new privUITransaction(); + $sDate = date('Y-m-d H:i:s', time()+TRANSACTION_EXPIRATION_DELAY); + $oTransaction->Set('expiration_date', $sDate); // 4 h delay by default + $oTransaction->DBInsert(); + return sprintf("%d", $oTransaction->GetKey()); + } + + /** + * Check whether a transaction is valid or not and remove the valid transaction from + * the database so that another call to IsTransactionValid for the same transaction + * will return false + * @param int $id Identifier of the transaction, as returned by GetNewTransactionId + * @return bool True if the transaction is valid, false otherwise + */ + public static function IsTransactionValid($id) + { + // First remove all the expired transactions... + self::CleanupExpiredTransactions(); + // TO DO put a critical section around this part to be 100% safe... + // sem_acquire(...) + $bResult = false; + $oTransaction = MetaModel::GetObject('privUITransaction', $id, false /* MustBeFound */); + if ($oTransaction) + { + $bResult = true; + $oTransaction->DBDelete(); + } + // sem_release(...) + return $bResult; + } + + /** + * Remove from the database all transactions that have expired + */ + protected static function CleanupExpiredTransactions() + { + $sQuery = 'SELECT privUITransaction WHERE expiration_date < NOW()'; + $oSearch = DBObjectSearch::FromOQL($sQuery); + $oSet = new DBObjectSet($oSearch); + while($oTransaction = $oSet->Fetch()) + { + $oTransaction->DBDelete(); + } + } + +} +?> diff --git a/application/utils.inc.php b/application/utils.inc.php index 490db8459..16de6f929 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -24,6 +24,7 @@ */ require_once('../core/config.class.inc.php'); +require_once('../application/transaction.class.inc.php'); define('ITOP_CONFIG_FILE', '../config-itop.php'); @@ -31,6 +32,11 @@ class FileUploadException extends Exception { } + +/** + * Helper functions to interact with forms: read parameters, upload files... + * @package iTop + */ class utils { private static $m_oConfig = null; @@ -116,14 +122,12 @@ class utils public static function GetNewTransactionId() { - // TO DO implement the real mechanism here - return sprintf("%08x", rand(0,2000000000)); + return privUITransaction::GetNewTransactionId(); } public static function IsTransactionValid($sId) { - // TO DO implement the real mechanism here - return true; + return privUITransaction::IsTransactionValid($sId); } public static function ReadFromFile($sFileName)