Index: /trunk/admin/comments.php
===================================================================
--- /trunk/admin/comments.php	(revision 3449)
+++ /trunk/admin/comments.php	(revision 3450)
@@ -135,8 +135,10 @@
 
 $query = '
-SELECT c.id, c.image_id, c.date, c.author, c.content, i.path, i.tn_ext
+SELECT c.id, c.image_id, c.date, c.author, u.username, c.content, i.path, i.tn_ext
   FROM '.COMMENTS_TABLE.' AS c
     INNER JOIN '.IMAGES_TABLE.' AS i
       ON i.id = c.image_id
+    LEFT JOIN '.USERS_TABLE.' AS u
+      ON u.id = c.author_id
   WHERE validated = \'false\'
   ORDER BY c.date DESC
@@ -152,4 +154,12 @@
         )
      );
+  if (empty($row['author_id'])) 
+  {
+    $author_name = $row['author'];
+  }
+  else
+  {
+    $author_name = $row['username'];
+  }
   $template->append(
     'comments',
@@ -160,5 +170,5 @@
       'ID' => $row['id'],
       'TN_SRC' => $thumb,
-      'AUTHOR' => trigger_event('render_comment_author', $row['author']),
+      'AUTHOR' => trigger_event('render_comment_author', $author_name),
       'DATE' => format_date($row['date'], true),
       'CONTENT' => trigger_event('render_comment_content',$row['content'])
Index: /trunk/comments.php
===================================================================
--- /trunk/comments.php	(revision 3449)
+++ /trunk/comments.php	(revision 3450)
@@ -101,5 +101,7 @@
 if (isset($_GET['author']) and !empty($_GET['author']))
 {
-  $page['where_clauses'][] = 'com.author = \''.$_GET['author'].'\'';
+  $page['where_clauses'][] = 
+    'u.username = \''.addslashes($_GET['author']).'\'
+     OR author = \''.addslashes($_GET['author']).'\'';    
 }
 
@@ -262,8 +264,10 @@
 
 $query = '
-SELECT COUNT(DISTINCT(id))
+SELECT COUNT(DISTINCT(com.id))
   FROM '.IMAGE_CATEGORY_TABLE.' AS ic
     INNER JOIN '.COMMENTS_TABLE.' AS com
     ON ic.image_id = com.image_id
+    LEFT JOIN '.USERS_TABLE.' As u
+    ON u.id = com.author_id
   WHERE '.implode('
     AND ', $page['where_clauses']).'
@@ -296,10 +300,14 @@
      , ic.category_id
      , com.author
+     , com.author_id
+     , username
      , com.date
      , com.content
      , com.validated
   FROM '.IMAGE_CATEGORY_TABLE.' AS ic
-    INNER JOIN '.COMMENTS_TABLE.' AS com
+    INNER JOIN '.COMMENTS_TABLE.' AS com    
     ON ic.image_id = com.image_id
+    LEFT JOIN '.USERS_TABLE.' AS u
+    ON u.id = com.author_id
   WHERE '.implode('
     AND ', $page['where_clauses']).'
@@ -367,8 +375,15 @@
           );
 
-    $author = $comment['author'];
-    if (empty($comment['author']))
-    {
-      $author = l10n('guest');
+    if (!empty($comment['author'])) 
+    {
+      $author = $comment['author'];
+      if ($author == 'guest')
+      {
+	$author = l10n('guest');
+      }
+    }
+    else
+    {
+      $author = $comment['username'];
     }
 
@@ -383,5 +398,5 @@
         );
 
-    if (can_manage_comment('delete', $comment['author'])) 
+    if (can_manage_comment('delete', $comment['author_id'])) 
     {
       $url = get_root_url().'comments.php'
@@ -392,5 +407,5 @@
 		       );
     }
-    if (can_manage_comment('edit', $comment['author']))
+    if (can_manage_comment('edit', $comment['author_id']))
     {
       $url = get_root_url().'comments.php'
Index: /trunk/include/functions_comment.inc.php
===================================================================
--- /trunk/include/functions_comment.inc.php	(revision 3449)
+++ /trunk/include/functions_comment.inc.php	(revision 3450)
@@ -92,4 +92,5 @@
       $comm['author'] = 'guest';
     }
+    $comm['author_id'] = $conf['guest_id'];
     // if a guest try to use the name of an already existing user, he must be
     // rejected
@@ -110,6 +111,8 @@
   else
   {
-    $comm['author'] = $user['username'];
-  }
+    $comm['author'] = '';
+    $comm['author_id'] = $user['id'];
+  }
+
   if ( empty($comm['content']) )
   { // empty comment content
@@ -135,5 +138,5 @@
 SELECT id FROM '.COMMENTS_TABLE.'
   WHERE date > FROM_UNIXTIME('.$reference_date.')
-    AND author = "'.addslashes($comm['author']).'"';
+    AND author_id = '.$comm['author_id'];
     if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
     {
@@ -152,7 +155,8 @@
     $query = '
 INSERT INTO '.COMMENTS_TABLE.'
-  (author, content, date, validated, validation_date, image_id)
+  (author, author_id, content, date, validated, validation_date, image_id)
   VALUES (
     "'.addslashes($comm['author']).'",
+    '.$comm['author_id'].',
     "'.addslashes($comm['content']).'",
     NOW(),
@@ -167,19 +171,23 @@
     $comm['id'] = mysql_insert_id();
 
-    if
-      (
-        ($comment_action=='validate' and $conf['email_admin_on_comment'])
-        or
-        ($comment_action!='validate' and $conf['email_admin_on_comment_validation'])
-      )
+    if (($comment_action=='validate' and $conf['email_admin_on_comment']) or
+	($comment_action!='validate' 
+	 and $conf['email_admin_on_comment_validation']))
     {
       include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
 
-      $del_url =
-          get_absolute_root_url().'comments.php?delete='.$comm['id'];
-
+      $del_url = get_absolute_root_url().'comments.php?delete='.$comm['id'];
+
+      if (empty($comm['author'])) 
+      {
+	$author_name = $user['username'];
+      }
+      else
+      {
+	$author_name = $comm['author'];
+      }
       $keyargs_content = array
       (
-        get_l10n_args('Author: %s', $comm['author']),
+        get_l10n_args('Author: %s', $author_name),
         get_l10n_args('Comment: %s', $comm['content']),
         get_l10n_args('', ''),
@@ -198,5 +206,5 @@
       pwg_mail_notification_admins
       (
-        get_l10n_args('Comment by %s', $comm['author']),
+        get_l10n_args('Comment by %s', $author_name),
         $keyargs_content
       );
@@ -219,5 +227,5 @@
   if (!is_admin())
   {
-    $user_where_clause = '   AND author = \''.$GLOBALS['user']['username'].'\'';
+    $user_where_clause = '   AND author_id = \''.$GLOBALS['user']['id'].'\'';
   }
   $query = '
@@ -265,5 +273,5 @@
 SELECT id FROM '.COMMENTS_TABLE.'
   WHERE date > FROM_UNIXTIME('.$reference_date.')
-    AND author = "'.$GLOBALS['user']['username'].'"';
+    AND author_id = '.$comm['author_id'];
     if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
     {
@@ -287,6 +295,6 @@
     if (!is_admin())
     {
-      $user_where_clause = '   AND author = \''.
-	$GLOBALS['user']['username'].'\'';
+      $user_where_clause = '   AND author_id = \''.
+	$GLOBALS['user']['id'].'\'';
     }
     $query = '
Index: /trunk/include/functions_user.inc.php
===================================================================
--- /trunk/include/functions_user.inc.php	(revision 3449)
+++ /trunk/include/functions_user.inc.php	(revision 3450)
@@ -1203,5 +1203,5 @@
  * @return bool
  */
-function can_manage_comment($action, $comment_author) 
+function can_manage_comment($action, $comment_author_id) 
 {
   if (!in_array($action, array('delete','edit'))) {
@@ -1209,5 +1209,6 @@
   }
   return (is_admin() || 
-	  (($GLOBALS['user']['username'] == $comment_author) 
+	  (($GLOBALS['user']['id'] == $comment_author_id) 
+	   && !is_a_guest()
 	   && $GLOBALS['conf'][sprintf('user_can_%s_comment', $action)]));
 }
Index: /trunk/include/picture_comment.inc.php
===================================================================
--- /trunk/include/picture_comment.inc.php	(revision 3449)
+++ /trunk/include/picture_comment.inc.php	(revision 3450)
@@ -129,6 +129,8 @@
 
     $query = '
-SELECT id,author,date,image_id,content,validated
-  FROM '.COMMENTS_TABLE.'
+SELECT com.id,author,author_id,username,date,image_id,content,validated
+  FROM '.COMMENTS_TABLE.' AS com
+  LEFT JOIN '.USERS_TABLE.' AS u
+    ON u.id = author_id
   WHERE image_id = '.$page['image_id'].
 $validated_clause.'
@@ -140,10 +142,20 @@
     while ($row = mysql_fetch_array($result))
     {
+      if (!empty($row['author'])) 
+      {
+	$author = $row['author'];
+	if ($author == 'guest')
+	{
+	  $author = l10n('guest');
+	}
+      }
+      else
+      {
+	$author = $row['username'];
+      }
+
       $tpl_comment =
         array(
-          'AUTHOR' => trigger_event('render_comment_author',
-            empty($row['author'])
-            ? l10n('guest')
-            : $row['author']),
+          'AUTHOR' => trigger_event('render_comment_author', $author),
 
           'DATE' => format_date( $row['date'], true),
@@ -152,5 +164,5 @@
         );
 
-      if (can_manage_comment('delete', $row['author']))
+      if (can_manage_comment('delete', $row['author_id']))
       {
         $tpl_comment['U_DELETE'] =
@@ -162,5 +174,5 @@
 			 );
       }
-      if (can_manage_comment('edit', $row['author']))
+      if (can_manage_comment('edit', $row['author_id']))
       {
 	$tpl_comment['U_EDIT'] =
Index: /trunk/install/db/82-database.php
===================================================================
--- /trunk/install/db/82-database.php	(revision 3450)
+++ /trunk/install/db/82-database.php	(revision 3450)
@@ -0,0 +1,44 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery                                  |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
+// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
+// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify  |
+// | it under the terms of the GNU General Public License as published by  |
+// | the Free Software Foundation                                          |
+// |                                                                       |
+// | This program is distributed in the hope that it will be useful, but   |
+// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
+// | General Public License for more details.                              |
+// |                                                                       |
+// | You should have received a copy of the GNU General Public License     |
+// | along with this program; if not, write to the Free Software           |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA.                                                                  |
+// +-----------------------------------------------------------------------+
+
+if (!defined('PHPWG_ROOT_PATH'))
+{
+  die('Hacking attempt!');
+}
+
+$upgrade_description = 'add new column to save author_id. 
+Guest users names are saved in author column';
+
+$query = '
+ALTER TABLE '.PREFIX_TABLE.'comments
+  ADD COLUMN author_id smallint(5) DEFAULT NULL
+;';
+
+pwg_query($query);
+
+echo
+"\n"
+. $upgrade_description
+."\n"
+;
+?>
