transaction.class.inc.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * This class records the pending "transactions" corresponding to forms that have not been
  18. * submitted yet, in order to prevent double submissions. When created a transaction remains valid
  19. * until the user's session expires
  20. * @package iTop
  21. */
  22. class privUITransaction
  23. {
  24. /**
  25. * Create a new transaction id, store it in the session and return its id
  26. * @param void
  27. * @return int The identifier of the new transaction
  28. */
  29. public static function GetNewTransactionId()
  30. {
  31. if (!isset($_SESSION['transactions']))
  32. {
  33. $_SESSION['transactions'] = array();
  34. }
  35. // Strictly speaking, the two lines below should be grouped together
  36. // by a critical section
  37. // sem_acquire($rSemIdentified);
  38. $id = 1 + count($_SESSION['transactions']);
  39. $_SESSION['transactions'][$id] = true;
  40. // sem_release($rSemIdentified);
  41. return sprintf("%d", $id);
  42. }
  43. /**
  44. * Check whether a transaction is valid or not and remove the valid transaction from
  45. * the session so that another call to IsTransactionValid for the same transaction id
  46. * will return false
  47. * @param int $id Identifier of the transaction, as returned by GetNewTransactionId
  48. * @return bool True if the transaction is valid, false otherwise
  49. */
  50. public static function IsTransactionValid($id)
  51. {
  52. $bResult = false;
  53. if (isset($_SESSION['transactions']))
  54. {
  55. // Strictly speaking, the three lines below should be grouped together
  56. // inside the same critical section as above
  57. // sem_acquire($rSemIdentified);
  58. if (isset($_SESSION['transactions'][$id]))
  59. {
  60. $bResult = true;
  61. unset($_SESSION['transactions'][$id]);
  62. }
  63. // sem_release($rSemIdentified);
  64. }
  65. return $bResult;
  66. }
  67. }
  68. ?>