Skip to content

Commit

Permalink
feature 2754: Add "Email" field for user comments + mandatory "Author"
Browse files Browse the repository at this point in the history
git-svn-id: http://piwigo.org/svn/trunk@18164 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
mistic100 committed Sep 23, 2012
1 parent b6d2db8 commit 7e33b84
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 46 deletions.
4 changes: 3 additions & 1 deletion admin/configuration.php
Expand Up @@ -73,7 +73,9 @@
'user_can_delete_comment',
'user_can_edit_comment',
'email_admin_on_comment_edition',
'email_admin_on_comment_deletion'
'email_admin_on_comment_deletion',
'comments_author_mandatory',
'comments_email_mandatory',
);

$display_checkboxes = array(
Expand Down
14 changes: 14 additions & 0 deletions admin/themes/default/template/configuration.tpl
Expand Up @@ -227,6 +227,20 @@ jQuery(document).ready(function () {
{'Validation'|@translate}
</label>
</li>

<li>
<label>
<input type="checkbox" name="comments_author_mandatory" {if ($comments.comments_author_mandatory)}checked="checked"{/if}>
{'Username is mandatory'|@translate}
</label>
</li>

<li>
<label>
<input type="checkbox" name="comments_email_mandatory" {if ($comments.comments_email_mandatory)}checked="checked"{/if}>
{'Email address is mandatory'|@translate}
</label>
</li>

<li>
<label>
Expand Down
17 changes: 17 additions & 0 deletions comments.php
Expand Up @@ -383,6 +383,8 @@
com.image_id,
com.author,
com.author_id,
u.'.$conf['user_fields']['email'].' AS user_email,
com.email,
com.date,
com.website_url,
com.content,
Expand Down Expand Up @@ -473,6 +475,16 @@
'image_file' => $elements[$comment['image_id']]['file'],
)
);

$email = null;
if (!empty($comment['user_email']))
{
$email = $comment['user_email'];
}
else if (!empty($comment['email']))
{
$email = $comment['email'];
}

$tpl_comment = array(
'ID' => $comment['comment_id'],
Expand All @@ -484,6 +496,11 @@
'DATE'=>format_date($comment['date'], true),
'CONTENT'=>trigger_event('render_comment_content',$comment['content']),
);

if (is_admin())
{
$tpl_comment['EMAIL'] = $email;
}

if (can_manage_comment('delete', $comment['author_id']))
{
Expand Down
19 changes: 19 additions & 0 deletions include/functions.inc.php
Expand Up @@ -1725,4 +1725,23 @@ function url_check_format($url)
return (bool)preg_match('@^https?://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$@iS', $url);
}
}

/**
* check email format
*/
function email_check_format($mail_address)
{
if (version_compare(PHP_VERSION, '5.2.0') >= 0)
{
return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false;
}
else
{
$atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // before arobase
$domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
$regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';

return (bool)preg_match($regex, $mail_address);
}
}
?>
41 changes: 35 additions & 6 deletions include/functions_comment.inc.php
Expand Up @@ -91,6 +91,11 @@ function insert_user_comment( &$comm, $key, &$infos )
{
if ( empty($comm['author']) )
{
if ($conf['comments_author_mandatory'])
{
array_push($infos, l10n('Username is mandatory') );
$comment_action='reject';
}
$comm['author'] = 'guest';
}
$comm['author_id'] = $conf['guest_id'];
Expand Down Expand Up @@ -128,13 +133,35 @@ function insert_user_comment( &$comm, $key, &$infos )
}

// website
if ( !empty($comm['website_url']) and !preg_match('/^https?/i', $comm['website_url']) )
if (!empty($comm['website_url']))
{
if (!preg_match('/^https?/i', $comm['website_url']))
{
$comm['website_url'] = 'http://'.$comm['website_url'];
}
if (!url_check_format($comm['website_url']))
{
array_push($infos, l10n('Your website URL is invalid'));
$comment_action='reject';
}
}

// email
if (empty($comm['email']))
{
$comm['website_url'] = 'http://'.$comm['website_url'];
if (!empty($user['email']))
{
$comm['email'] = $user['email'];
}
else if ($conf['comments_email_mandatory'])
{
array_push($infos, l10n('Email address is missing. Please specify an email address.') );
$comment_action='reject';
}
}
if ( !empty($comm['website_url']) and !url_check_format($comm['website_url']) )
else if (!email_check_format($comm['email']))
{
array_push($infos, l10n('Your website URL is invalid'));
array_push($infos, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)'));
$comment_action='reject';
}

Expand Down Expand Up @@ -179,7 +206,7 @@ function insert_user_comment( &$comm, $key, &$infos )
{
$query = '
INSERT INTO '.COMMENTS_TABLE.'
(author, author_id, anonymous_id, content, date, validated, validation_date, image_id, website_url)
(author, author_id, anonymous_id, content, date, validated, validation_date, image_id, website_url, email)
VALUES (
\''.$comm['author'].'\',
'.$comm['author_id'].',
Expand All @@ -189,7 +216,8 @@ function insert_user_comment( &$comm, $key, &$infos )
\''.($comment_action=='validate' ? 'true':'false').'\',
'.($comment_action=='validate' ? 'NOW()':'NULL').',
'.$comm['image_id'].',
'.(!empty($comm['website_url']) ? '\''.$comm['website_url'].'\'' : 'NULL').'
'.(!empty($comm['website_url']) ? '\''.$comm['website_url'].'\'' : 'NULL').',
'.(!empty($comm['email']) ? '\''.$comm['email'].'\'' : 'NULL').'
)
';

Expand All @@ -207,6 +235,7 @@ function insert_user_comment( &$comm, $key, &$infos )
$keyargs_content = array
(
get_l10n_args('Author: %s', stripslashes($comm['author']) ),
get_l10n_args('Email: %s', stripslashes($comm['email']) ),
get_l10n_args('Comment: %s', stripslashes($comm['content']) ),
get_l10n_args('', ''),
get_l10n_args('Manage this user comment: %s', $comment_url)
Expand Down
14 changes: 5 additions & 9 deletions include/functions_user.inc.php
Expand Up @@ -41,22 +41,18 @@ function validate_mail_address($user_id, $mail_address)
return '';
}

$atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // before arobase
$domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name
$regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i';

if ( !preg_match( $regex, $mail_address ) )
if ( !email_check_format($mail_address) )
{
return l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)');
}

if (defined("PHPWG_INSTALLED") and !empty($mail_address))
{
$query = '
select count(*)
from '.USERS_TABLE.'
where upper('.$conf['user_fields']['email'].') = upper(\''.$mail_address.'\')
'.(is_numeric($user_id) ? 'and '.$conf['user_fields']['id'].' != \''.$user_id.'\'' : '').'
SELECT count(*)
FROM '.USERS_TABLE.'
WHERE upper('.$conf['user_fields']['email'].') = upper(\''.$mail_address.'\')
'.(is_numeric($user_id) ? 'AND '.$conf['user_fields']['id'].' != \''.$user_id.'\'' : '').'
;';
list($count) = pwg_db_fetch_row(pwg_query($query));
if ($count != 0)
Expand Down
52 changes: 28 additions & 24 deletions include/picture_comment.inc.php
Expand Up @@ -49,12 +49,13 @@
'author' => trim( @$_POST['author'] ),
'content' => trim( $_POST['content'] ),
'website_url' => trim( $_POST['website_url'] ),
'email' => trim( @$_POST['email'] ),
'image_id' => $page['image_id'],
);

include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');

$comment_action = insert_user_comment($comm, @$_POST['key'], $page['infos']);
$comment_action = insert_user_comment($comm, @$_POST['key'], $page['errors']);

switch ($comment_action)
{
Expand Down Expand Up @@ -143,10 +144,11 @@
com.id,
author,
author_id,
'.$conf['user_fields']['username'].' AS username,
u.'.$conf['user_fields']['email'].' AS user_email,
date,
image_id,
website_url,
com.email,
content,
validated
FROM '.COMMENTS_TABLE.' AS com
Expand All @@ -161,23 +163,25 @@

while ($row = pwg_db_fetch_assoc($result))
{
if (!empty($row['author']))
if ($row['author'] == 'guest')
{
$author = $row['author'];
if ($author == 'guest')
{
$author = l10n('guest');
}
$row['author'] = l10n('guest');
}
else

$email = null;
if (!empty($row['user_email']))
{
$author = stripslashes($row['username']);
$email = $row['user_email'];
}
else if (!empty($row['email']))
{
$email = $row['email'];
}

$tpl_comment =
array(
'ID' => $row['id'],
'AUTHOR' => trigger_event('render_comment_author', $author),
'AUTHOR' => trigger_event('render_comment_author', $row['author']),
'DATE' => format_date($row['date'], true),
'CONTENT' => trigger_event('render_comment_content',$row['content']),
'WEBSITE_URL' => $row['website_url'],
Expand Down Expand Up @@ -215,6 +219,8 @@
}
if (is_admin())
{
$tpl_comment['EMAIL'] = $email;

if ($row['validated'] != 'true')
{
$tpl_comment['U_VALIDATE'] = add_url_params(
Expand Down Expand Up @@ -244,21 +250,19 @@
if ($show_add_comment_form)
{
$key = get_ephemeral_key(3, $page['image_id']);
$content = $author = $website_url = '';
if ('reject'===@$comment_action)
{
$content = htmlspecialchars( stripslashes($comm['content']) );
$author = htmlspecialchars( stripslashes($comm['author']) );
$website_url = htmlspecialchars( stripslashes($comm['website_url']) );
}

$template->assign('comment_add',
array(
'F_ACTION' => $url_self,
'KEY' => $key,
'CONTENT' => $content,
'SHOW_AUTHOR' => !is_classic_user(),
'AUTHOR' => $author ,
'WEBSITE_URL' => $website_url,
'F_ACTION' => $url_self,
'KEY' => $key,
'CONTENT' => stripslashes(@$_POST['content']),
'SHOW_AUTHOR' => !is_classic_user(),
'AUTHOR_MANDATORY' => $conf['comments_author_mandatory'],
'AUTHOR' => stripslashes(@$_POST['author']),
'WEBSITE_URL' => stripslashes(@$_POST['website_url']),
'SHOW_EMAIL' => !is_classic_user() or empty($user['email']),
'EMAIL_MANDATORY' => $conf['comments_email_mandatory'],
'EMAIL' => stripslashes(@$_POST['email']),
));
}
}
Expand Down
2 changes: 2 additions & 0 deletions install/config.sql
Expand Up @@ -6,6 +6,8 @@ INSERT INTO piwigo_config (param,value,comment) VALUES ('log','true','keep an hi
INSERT INTO piwigo_config (param,value,comment) VALUES ('comments_validation','false','administrators validate users comments before becoming visible');
INSERT INTO piwigo_config (param,value,comment) VALUES ('comments_forall','false','even guest not registered can post comments');
INSERT INTO piwigo_config (param,value,comment) VALUES ('comments_order','ASC','comments order on picture page and cie');
INSERT INTO piwigo_config (param,value,comment) VALUES ('comments_author_mandatory','false');
INSERT INTO piwigo_config (param,value,comment) VALUES ('comments_email_mandatory','false');
INSERT INTO piwigo_config (param,value,comment) VALUES ('user_can_delete_comment','false','administrators can allow user delete their own comments');
INSERT INTO piwigo_config (param,value,comment) VALUES ('user_can_edit_comment','false','administrators can allow user edit their own comments');
INSERT INTO piwigo_config (param,value,comment) VALUES ('email_admin_on_comment_edition','false','Send an email to the administrators when a comment is modified');
Expand Down
41 changes: 41 additions & 0 deletions install/db/130-database.php
@@ -0,0 +1,41 @@
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2012 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 "email" field in comments table';

include_once(PHPWG_ROOT_PATH.'include/constants.php');

$query = 'ALTER TABLE `'.COMMENTS_TABLE.'` ADD `email` varchar(255) default NULL;';
pwg_query($query);

conf_update_param('comments_author_mandatory', 'false');
conf_update_param('comments_email_mandatory', 'false');

echo "\n".$upgrade_description."\n";

?>
1 change: 1 addition & 0 deletions install/piwigo_structure-mysql.sql
Expand Up @@ -51,6 +51,7 @@ CREATE TABLE `piwigo_comments` (
`image_id` mediumint(8) unsigned NOT NULL default '0',
`date` datetime NOT NULL default '0000-00-00 00:00:00',
`author` varchar(255) default NULL,
`email` varchar(255) default NULL,
`author_id` smallint(5) DEFAULT NULL,
`anonymous_id` varchar(45) NOT NULL,
`website_url` varchar(255) DEFAULT NULL,
Expand Down
3 changes: 3 additions & 0 deletions language/en_UK/common.lang.php
Expand Up @@ -165,6 +165,7 @@
$lang['Edit'] = 'Edit';
$lang['Email address is missing. Please specify an email address.'] = "Email address is missing. Please specify an email address.";
$lang['Email address'] = "Email address";
$lang['Email address is mandatory'] = 'Email address is mandatory';
$lang['Email: %s'] = "Email: %s";
$lang['Empty query. No criteria has been entered.'] = 'Empty query. No criteria have been entered.';
$lang['End-Date'] = "End date";
Expand Down Expand Up @@ -384,6 +385,7 @@
$lang['Username modification'] = 'Username modification';
$lang['Username or email'] = 'Username or email';
$lang['Username'] = "Username";
$lang['Username is mandatory'] = 'Username is mandatory';
$lang['Username: %s'] = 'Username: %s';
$lang['View in'] = 'View in';
$lang['View'] = "View";
Expand All @@ -407,4 +409,5 @@
$lang['Your Gallery Customization'] = "Your gallery customization";
$lang['Your password has been reset'] = 'Your password has been reset';
$lang['Your username has been successfully changed to : %s'] = 'Your username has been successfully changed to : %s';
$lang['mandatory'] = 'mandatory';
?>
5 changes: 4 additions & 1 deletion language/fr_FR/common.lang.php
Expand Up @@ -407,4 +407,7 @@
$lang['Requested album does not exist'] = 'L\'album demandé n\'existe pas';
$lang['Permalink for album not found'] = 'Permalink pour l\'album non trouvé';
$lang['Requested tag does not exist'] = 'Le tag demandée n\'existe pas';
?>
$lang['Username is mandatory'] = 'Nom d\'utilisateur obligatoire';
$lang['Email address is mandatory'] = 'Adresse email obligatoire';
$lang['mandatory'] = 'obligatoire';
?>

0 comments on commit 7e33b84

Please sign in to comment.