## MasonX::Plugin::UTF8 -
## Copyright © 2009-2010 by CYBAEA Limited.  http://www.cybaea.net/
## patched by Kevin Seghetti to handle arrays

package MasonX::Plugin::UTF8;
use base qw(HTML::Mason::Plugin);

use utf8;
use warnings;
use strict;
use English qw( -no_match_vars );
use Carp;

use version; our $VERSION = qv('0.1.0');

# Other recommended modules (uncomment to use):
# use Scalar::Util qw();       # General-utility scalar subroutines
# use List::Util qw();         # General-utility list subroutines
# use List::MoreUtils qw();    # Provide the stuff missing in List::Util
# use Readonly;                # Creating read-only scalars, arrays, hashes.

# Module implementation here

sub start_request_hook {
    my ( $self, $context ) = @_;
    my $args_ref = $context->args();

    foreach my $arg ( @{$args_ref} ) 
    {
        if(ref $arg eq 'ARRAY') 
        {
            foreach my $subarg ( @{$arg} ) 
            {
                utf8::is_utf8($subarg) || utf8::decode($subarg);
            }
        }
        elsif(ref $arg eq '')
        {
             utf8::is_utf8($arg) || utf8::decode($arg);
        }
        else
        {
            warn "MasonX::Plugin::UTF8.pm: Unexpected type ",ref $arg, " found in parameter conversion";
        }
    }
    return;
}

1;    # Magic true value required at end of module
__END__

=head1 NAME

MasonX::Plugin::UTF8 - Fix Mason's arguments to be UTF-8 strings


=head1 VERSION

This document describes MasonX::Plugin::UTF8 version 0.1.0


=head1 SYNOPSIS

    use MasonX::Plugin::UTF8;

In your Apache configuration file use

    PerlAddVar MasonPlugins "MasonX::Plugin::UTF8"

to ensure that all your form inputs are utf-8 safe.
  
  
=head1 DESCRIPTION

Unfortunately, Mason + Apache + mod_perl is not utf-8 safe by default.
Your form input will be mangled even with the right C<<
accept-chartype="utf-8" >> and C<< enctype="multipart/form-data" >>
attributes.

This module fixes this in a way that shoule be (1) easy to use and (2)
safe.

See L<http://www.cybaea.net/Blogs/TechNotes/Mason-utf-8-clean.html>
for more information about how to make Mason utf-8 safe.

=head1 INTERFACE

=over

=item start_request_hook

This function modifies the request arguments to ensure they are UTF-8
encoded (if they are not so already).

=back

=head1 DIAGNOSTICS

None.

=head1 CONFIGURATION AND ENVIRONMENT
  
MasonX::Plugin::UTF8 requires no configuration files or environment variables.

For the Apache configuration, simply add

    PerlAddVar MasonPlugins "MasonX::Plugin::UTF8"


=head1 DEPENDENCIES

Perl >= v5.8.1.


=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to C<allane@cybaea.com>.

=head1 AUTHOR

Allan Engelhardt  C<< <allane@cybaea.com> >>

=head1 LICENSE AND COPYRIGHT

Copyright © 2009, Allan Engelhardt C<< <allane@cybaea.com> >>.  All
rights reserved.

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, either version 3 of the License, or (at
your option) any later version.

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, see <http://www.gnu.org/licenses/>.

=head1 DISCLAIMER OF WARRANTY

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.

=head1 LIMITATION OF LIABILITY

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER
PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
