transaction.class.inc.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * This class records the pending "transactions" corresponding to forms that have not been
  20. * submitted yet, in order to prevent double submissions. When created a transaction remains valid
  21. * until the user's session expires
  22. *
  23. * @package iTop
  24. * @copyright Copyright (C) 2010-2012 Combodo SARL
  25. * @license http://opensource.org/licenses/AGPL-3.0
  26. */
  27. class privUITransaction
  28. {
  29. /**
  30. * Create a new transaction id, store it in the session and return its id
  31. * @param void
  32. * @return int The identifier of the new transaction
  33. */
  34. public static function GetNewTransactionId()
  35. {
  36. if (!isset($_SESSION['transactions']))
  37. {
  38. $_SESSION['transactions'] = array();
  39. }
  40. // Strictly speaking, the two lines below should be grouped together
  41. // by a critical section
  42. // sem_acquire($rSemIdentified);
  43. $id = str_replace(array('.', ' '), '', microtime()); //1 + count($_SESSION['transactions']);
  44. $_SESSION['transactions'][$id] = true;
  45. // sem_release($rSemIdentified);
  46. return (string)$id;
  47. }
  48. /**
  49. * Check whether a transaction is valid or not and (optionally) remove the valid transaction from
  50. * the session so that another call to IsTransactionValid for the same transaction id
  51. * will return false
  52. * @param int $id Identifier of the transaction, as returned by GetNewTransactionId
  53. * @param bool $bRemoveTransaction True if the transaction must be removed
  54. * @return bool True if the transaction is valid, false otherwise
  55. */
  56. public static function IsTransactionValid($id, $bRemoveTransaction = true)
  57. {
  58. $bResult = false;
  59. if (isset($_SESSION['transactions']))
  60. {
  61. // Strictly speaking, the eight lines below should be grouped together
  62. // inside the same critical section as above
  63. // sem_acquire($rSemIdentified);
  64. if (isset($_SESSION['transactions'][$id]))
  65. {
  66. $bResult = true;
  67. if ($bRemoveTransaction)
  68. {
  69. unset($_SESSION['transactions'][$id]);
  70. }
  71. }
  72. // sem_release($rSemIdentified);
  73. }
  74. return $bResult;
  75. }
  76. /**
  77. * Removes the transaction specified by its id
  78. * @param int $id The Identifier (as returned by GetNewTranscationId) of the transaction to be removed.
  79. * @return void
  80. */
  81. public static function RemoveTransaction($id)
  82. {
  83. if (isset($_SESSION['transactions']))
  84. {
  85. // Strictly speaking, the three lines below should be grouped together
  86. // inside the same critical section as above
  87. // sem_acquire($rSemIdentified);
  88. if (isset($_SESSION['transactions'][$id]))
  89. {
  90. unset($_SESSION['transactions'][$id]);
  91. }
  92. // sem_release($rSemIdentified);
  93. }
  94. }
  95. }
  96. ?>