transaction.class.inc.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 (optionally) 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. * @param bool $bRemoveTransaction True if the transaction must be removed
  49. * @return bool True if the transaction is valid, false otherwise
  50. */
  51. public static function IsTransactionValid($id, $bRemoveTransaction = true)
  52. {
  53. $bResult = false;
  54. if (isset($_SESSION['transactions']))
  55. {
  56. // Strictly speaking, the eight lines below should be grouped together
  57. // inside the same critical section as above
  58. // sem_acquire($rSemIdentified);
  59. if (isset($_SESSION['transactions'][$id]))
  60. {
  61. $bResult = true;
  62. if ($bRemoveTransaction)
  63. {
  64. unset($_SESSION['transactions'][$id]);
  65. }
  66. }
  67. // sem_release($rSemIdentified);
  68. }
  69. return $bResult;
  70. }
  71. /**
  72. * Removes the transaction specified by its id
  73. * @param int $id The Identifier (as returned by GetNewTranscationId) of the transaction to be removed.
  74. * @return void
  75. */
  76. public static function RemoveTransaction($id)
  77. {
  78. if (isset($_SESSION['transactions']))
  79. {
  80. // Strictly speaking, the three lines below should be grouped together
  81. // inside the same critical section as above
  82. // sem_acquire($rSemIdentified);
  83. if (isset($_SESSION['transactions'][$id]))
  84. {
  85. unset($_SESSION['transactions'][$id]);
  86. }
  87. // sem_release($rSemIdentified);
  88. }
  89. }
  90. }
  91. ?>