Преглед изворни кода

File-based "transactions" dans log files better protected against concurrent access...

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@3659 a333f486-631f-4898-b8df-5754b55c2be0
dflaven пре 10 година
родитељ
комит
0c62212a4f
3 измењених фајлова са 48 додато и 3 уклоњено
  1. 29 3
      application/transaction.class.inc.php
  2. 16 0
      core/config.class.inc.php
  3. 3 0
      core/log.class.inc.php

+ 29 - 3
application/transaction.class.inc.php

@@ -37,6 +37,11 @@ class privUITransaction
 	 */
 	 */
 	public static function GetNewTransactionId()
 	public static function GetNewTransactionId()
 	{
 	{
+		$bTransactionsEnabled = MetaModel::GetConfig()->Get('transactions_enabled');
+		if (!$bTransactionsEnabled)
+		{
+			return 'notransactions'; // Any value will do
+		}
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		if (!class_exists($sClass, false))
 		if (!class_exists($sClass, false))
 		{
 		{
@@ -57,6 +62,11 @@ class privUITransaction
 	 */
 	 */
 	public static function IsTransactionValid($id, $bRemoveTransaction = true)
 	public static function IsTransactionValid($id, $bRemoveTransaction = true)
 	{
 	{
+		$bTransactionsEnabled = MetaModel::GetConfig()->Get('transactions_enabled');
+		if (!$bTransactionsEnabled)
+		{
+			return true; // All values are valid
+		}
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		if (!class_exists($sClass, false))
 		if (!class_exists($sClass, false))
 		{
 		{
@@ -73,6 +83,11 @@ class privUITransaction
 	 */
 	 */
 	public static function RemoveTransaction($id)
 	public static function RemoveTransaction($id)
 	{
 	{
+		$bTransactionsEnabled = MetaModel::GetConfig()->Get('transactions_enabled');
+		if (!$bTransactionsEnabled)
+		{
+			return; // Nothing to do
+		}
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		$sClass = 'privUITransaction'.MetaModel::GetConfig()->Get('transaction_storage');
 		if (!class_exists($sClass, false))
 		if (!class_exists($sClass, false))
 		{
 		{
@@ -191,7 +206,7 @@ class privUITransactionFile
 			throw new Exception('The directory "'.APPROOT.'data/transactions" must be writable to the application.');
 			throw new Exception('The directory "'.APPROOT.'data/transactions" must be writable to the application.');
 		}
 		}
 		self::CleanupOldTransactions();
 		self::CleanupOldTransactions();
-		$id = basename(tempnam(APPROOT.'data/transactions', substr(UserRights::GetUser(), 0, 10).'-'));
+		$id = basename(tempnam(APPROOT.'data/transactions', self::GetUserPrefix()));
 		self::Info('GetNewTransactionId: Created transaction: '.$id);
 		self::Info('GetNewTransactionId: Created transaction: '.$id);
 
 
 		return (string)$id;
 		return (string)$id;
@@ -286,15 +301,22 @@ class privUITransactionFile
 	{
 	{
 		clearstatcache();
 		clearstatcache();
 		$aResult = array();
 		$aResult = array();
-		$aTransactions = glob(APPROOT.'data/transactions/'.UserRights::GetUser().'-*');
+		$aTransactions = glob(APPROOT.'data/transactions/'.self::GetUserPrefix().'*');
 		foreach($aTransactions as $sFileName)
 		foreach($aTransactions as $sFileName)
 		{
 		{
-			$aResult[] = date('Y-m-d H:i:s', filectime($sFileName)).' - '.basename($sFileName);
+			$aResult[] = date('Y-m-d H:i:s', filemtime($sFileName)).' - '.basename($sFileName);
 		}
 		}
 		sort($aResult);
 		sort($aResult);
 		return $aResult;
 		return $aResult;
 	}
 	}
 
 
+	protected static function GetUserPrefix()
+	{
+		$sPrefix = substr(UserRights::GetUser(), 0, 10);
+		$sPrefix = preg_replace('/[^a-zA-Z0-9-_]/', '_', $sPrefix);
+		return $sPrefix.'-';
+	}
+
 	protected static function Info($sText)
 	protected static function Info($sText)
 	{
 	{
 		self::Write('Info | '.$sText);
 		self::Write('Info | '.$sText);
@@ -312,6 +334,9 @@ class privUITransactionFile
 	
 	
 	protected static function Write($sText)
 	protected static function Write($sText)
 	{
 	{
+		$bLogEnabled = MetaModel::GetConfig()->Get('log_transactions');
+		if ($bLogEnabled)
+		{
 		$hLogFile = @fopen(APPROOT.'log/transactions.log', 'a');
 		$hLogFile = @fopen(APPROOT.'log/transactions.log', 'a');
 		if ($hLogFile !== false)
 		if ($hLogFile !== false)
 		{
 		{
@@ -321,6 +346,7 @@ class privUITransactionFile
 			fflush($hLogFile);
 			fflush($hLogFile);
 			flock($hLogFile, LOCK_UN);
 			flock($hLogFile, LOCK_UN);
 			fclose($hLogFile);
 			fclose($hLogFile);
+			}
 		}
 		}
 	}
 	}
 }
 }

+ 16 - 0
core/config.class.inc.php

@@ -809,6 +809,22 @@ class Config
 			'source_of_value' => '',
 			'source_of_value' => '',
 			'show_in_conf_sample' => false,
 			'show_in_conf_sample' => false,
 		),
 		),
+		'transactions_enabled' => array(
+			'type' => 'bool',
+			'description' => 'Whether or not the whole mechanism to prevent multiple submissions of a page is enabled.',
+			'default' => true,
+			'value' => '',
+			'source_of_value' => '',
+			'show_in_conf_sample' => false,
+		),
+		'log_transactions' => array(
+			'type' => 'bool',
+			'description' => 'Whether or not to enable the debug log for the transactions.',
+			'default' => false,
+			'value' => '',
+			'source_of_value' => '',
+			'show_in_conf_sample' => false,
+		),
 		'concurrent_lock_enabled' => array(
 		'concurrent_lock_enabled' => array(
 			'type' => 'bool',
 			'type' => 'bool',
 			'description' => 'Whether or not to activate the locking mechanism in order to prevent concurrent edition of the same object.',
 			'description' => 'Whether or not to activate the locking mechanism in order to prevent concurrent edition of the same object.',

+ 3 - 0
core/log.class.inc.php

@@ -59,8 +59,11 @@ class FileLog
 		$hLogFile = @fopen($this->m_sFile, 'a');
 		$hLogFile = @fopen($this->m_sFile, 'a');
 		if ($hLogFile !== false)
 		if ($hLogFile !== false)
 		{
 		{
+			flock($hLogFile, LOCK_EX);
 			$sDate = date('Y-m-d H:i:s');
 			$sDate = date('Y-m-d H:i:s');
 			fwrite($hLogFile, "$sDate | $sText\n");
 			fwrite($hLogFile, "$sDate | $sText\n");
+			fflush($hLogFile);
+			flock($hLogFile, LOCK_UN);
 			fclose($hLogFile);
 			fclose($hLogFile);
 		}
 		}
 	}
 	}