Index: /trunk/comments.php
===================================================================
--- /trunk/comments.php	(revision 7494)
+++ /trunk/comments.php	(revision 7495)
@@ -505,5 +505,5 @@
       {
 	$tpl_comment['IN_EDIT'] = true;
-	$key = get_comment_post_key($comment['image_id']);
+	$key = get_ephemeral_key(2, $comment['image_id']);
 	$tpl_comment['KEY'] = $key;
 	$tpl_comment['IMAGE_ID'] = $comment['image_id'];
Index: /trunk/include/functions.inc.php
===================================================================
--- /trunk/include/functions.inc.php	(revision 7494)
+++ /trunk/include/functions.inc.php	(revision 7495)
@@ -1334,23 +1334,35 @@
 
 /**
- * returns a "secret key" that is to be sent back when a user enters a comment
- *
- * @param int image_id
- */
-function get_comment_post_key($image_id)
-{
-  global $conf;
-
-  $time = time();
-
-  return sprintf(
-    '%s:%s',
-    $time,
-    hash_hmac(
-      'md5',
-      $time.':'.$image_id,
-      $conf['secret_key']
-      )
-    );
+ * returns a "secret key" that is to be sent back when a user posts a form
+ *
+ * @param int valid_after_seconds - key validity start time from now
+ */
+function get_ephemeral_key($valid_after_seconds, $aditionnal_data_to_hash = '')
+{
+	global $conf;
+	$time = round(microtime(true), 1);
+	return $time.':'.$valid_after_seconds.':'
+		.hash_hmac(
+			'md5', 
+			$time.substr($_SERVER['REMOTE_ADDR'],0,5).$valid_after_seconds.$aditionnal_data_to_hash, 
+			$conf['secret_key']);
+}
+
+function verify_ephemeral_key($key, $aditionnal_data_to_hash = '')
+{
+	global $conf;
+	$time = microtime(true);
+	$key = explode( ':', @$key );
+	if ( count($key)!=3
+		or $key[0]>$time-(float)$key[1] // page must have been retrieved more than X sec ago
+		or $key[0]<$time-3600 // 60 minutes expiration
+		or hash_hmac(
+			  'md5', $key[0].substr($_SERVER['REMOTE_ADDR'],0,5).$key[1].$aditionnal_data_to_hash, $conf['secret_key']
+			) != $key[2]
+	  )
+	{
+		return false;
+	}
+	return true;
 }
 
Index: /trunk/include/functions_comment.inc.php
===================================================================
--- /trunk/include/functions_comment.inc.php	(revision 7494)
+++ /trunk/include/functions_comment.inc.php	(revision 7495)
@@ -120,12 +120,5 @@
   }
 
-  $key = explode( ':', @$key );
-  if ( count($key)!=2
-        or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
-        or $key[0]<time()-3600 // 60 minutes expiration
-        or hash_hmac(
-              'md5', $key[0].':'.$comm['image_id'], $conf['secret_key']
-            ) != $key[1]
-      )
+  if ( !verify_ephemeral_key(@$key, $comm['image_id']) )
   {
     $comment_action='reject';
@@ -249,11 +242,5 @@
   $comment_action = 'validate';
 
-  $key = explode( ':', $post_key );
-  if ( count($key)!=2
-       or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
-       or $key[0]<time()-3600 // 60 minutes expiration
-       or hash_hmac('md5', $key[0].':'.$comment['image_id'], $conf['secret_key']
-		    ) != $key[1]
-       )
+  if ( !verify_ephemeral_key($post_key, $comment['image_id']) )
   {
     $comment_action='reject';
Index: /trunk/include/picture_comment.inc.php
===================================================================
--- /trunk/include/picture_comment.inc.php	(revision 7494)
+++ /trunk/include/picture_comment.inc.php	(revision 7495)
@@ -199,5 +199,5 @@
 	{
 	  $tpl_comment['IN_EDIT'] = true;
-	  $key = get_comment_post_key($page['image_id']);
+	  $key = get_comment_post_key(2, $page['image_id']);
 	  $tpl_comment['KEY'] = $key;
 	  $tpl_comment['CONTENT'] = $row['content'];
@@ -234,5 +234,5 @@
   if ($show_add_comment_form)
   {
-    $key = get_comment_post_key($page['image_id']);
+    $key = get_ephemeral_key(3, $page['image_id']);
     $content = '';
     if ('reject'===@$comment_action)
Index: /trunk/include/ws_functions.inc.php
===================================================================
--- /trunk/include/ws_functions.inc.php	(revision 7494)
+++ /trunk/include/ws_functions.inc.php	(revision 7495)
@@ -726,5 +726,5 @@
   {
     $comment_post_data['author'] = stripslashes($user['username']);
-    $comment_post_data['key'] = get_comment_post_key($params['image_id']);
+    $comment_post_data['key'] = get_ephemeral_key(2, $params['image_id']);
   }
 
Index: /trunk/register.php
===================================================================
--- /trunk/register.php	(revision 7494)
+++ /trunk/register.php	(revision 7495)
@@ -41,4 +41,10 @@
 if (isset($_POST['submit']))
 {
+  if (!verify_ephemeral_key(@$_POST['key']))
+  {
+		set_status_header(403);
+    array_push($errors, 'Invalid/expired form key');
+  }
+
   if ($_POST['password'] != $_POST['password_conf'])
   {
@@ -47,5 +53,5 @@
 
   $errors =
-      register_user(htmlspecialchars($_POST['login'],ENT_COMPAT,'utf-8'),
+      register_user($_POST['login'],
                     $_POST['password'],
                     $_POST['mail_address'],
@@ -59,8 +65,13 @@
     redirect(make_index_url());
   }
+	$registration_post_key = get_ephemeral_key(2);
+}
+else
+{
+	$registration_post_key = get_ephemeral_key(6);
 }
 
-$login = !empty($_POST['login'])?$_POST['login']:'';
-$email = !empty($_POST['mail_address'])?$_POST['mail_address']:'';
+$login = !empty($_POST['login'])?htmlspecialchars(stripslashes($_POST['login'])):'';
+$email = !empty($_POST['mail_address'])?htmlspecialchars(stripslashes($_POST['mail_address'])):'';
 
 //----------------------------------------------------- template initialization
@@ -75,8 +86,8 @@
 $template->assign(array(
   'U_HOME' => make_index_url(),
-
+	'F_KEY' => $registration_post_key,
   'F_ACTION' => 'register.php',
-  'F_LOGIN' => htmlspecialchars($login, ENT_QUOTES, 'utf-8'),
-  'F_EMAIL' => htmlspecialchars($email, ENT_QUOTES, 'utf-8')
+  'F_LOGIN' => $login,
+  'F_EMAIL' => $email
   ));
 
Index: /trunk/themes/default/template/register.tpl
===================================================================
--- /trunk/themes/default/template/register.tpl	(revision 7494)
+++ /trunk/themes/default/template/register.tpl	(revision 7495)
@@ -55,4 +55,5 @@
 
   <p class="bottomButtons">
+		<input type="hidden" name="key" value="{$F_KEY}" >
     <input class="submit" type="submit" name="submit" value="{'Register'|@translate}">
     <input class="submit" type="reset" value="{'Reset'|@translate}">
