File Coverage

blib/lib/FWS/V2/Check.pm
Criterion Covered Total %
statement 12 59 20.3
branch 0 18 0.0
condition 0 2 0.0
subroutine 4 11 36.3
pod 7 7 100.0
total 23 97 23.7


line stmt bran cond sub pod time code
1             package FWS::V2::Check;
2              
3 1     1   25 use 5.006;
  1         3  
  1         31  
4 1     1   4 use strict;
  1         2  
  1         30  
5 1     1   5 use warnings;
  1         3  
  1         51  
6 1     1   5 no warnings 'uninitialized';
  1         2  
  1         751  
7              
8             =head1 NAME
9              
10             FWS::V2::Check - Framework Sites version 2 validation and checking methods
11              
12             =head1 VERSION
13              
14             Version 1.13091122
15              
16             =cut
17              
18             our $VERSION = '1.13091122';
19              
20              
21             =head1 SYNOPSIS
22              
23             use FWS::V2;
24              
25             #
26             # Create $fws
27             #
28             my $fws = FWS::V2->new();
29              
30             #
31             # all simple boolean response in conditionals
32             #
33             if ( $fws->isValidEmail( 'this@email.com') ) { print "Its not real, but it could be!\n" }
34             else { print "Yuck, bad email.\n" }
35              
36              
37             =head1 DESCRIPTION
38              
39             Simple methods that will return boolean results based on the validation of the passed parameter.
40              
41             =cut
42              
43              
44             =head1 METHODS
45              
46             =head2 isAdminLoggedIn
47              
48             Return a 0 or 1 depending if a admin user is currently logged in.
49              
50             #
51             # do something if logged in as an admin user
52             #
53             if ( $fws->isAdminLoggedIn() ) { $valueHash{html} .= 'I am logged in as a admin
' }
54              
55             =cut
56              
57             sub isAdminLoggedIn {
58 0     0 1   my ( $self, $loginType ) = @_;
59 0 0         if ( $self->{adminLoginId} ) { return 1 }
  0            
60 0           return 0;
61             }
62              
63              
64             =head2 isUserLoggedIn
65              
66             Return a 0 or 1 depending if a site user is currently logged in.
67              
68             #
69             # do something if logged in as an site user
70             #
71             if ( $fws->isUserLoggedIn() ) { $valueHash{html} .= 'I am logged in as a user
' }
72              
73             =cut
74              
75             sub isUserLoggedIn {
76 0     0 1   my ( $self, $loginType ) = @_;
77 0 0         if ( $self->{userLoginId} ) { return 1 }
  0            
78 0           return 0;
79             }
80              
81              
82             =head2 isValidEmail
83              
84             Return a boolean response to validate if an email address is well formed.
85              
86             =cut
87              
88             sub isValidEmail {
89 0     0 1   my ( $self, $fieldValue ) = @_;
90 0 0         if ( $fieldValue !~ /^\w+[\w|\.|-]*\w+@(\w+[\w|\.|-]*\w+\.[a-z]{2,4}|(\d{1,3}\.){3}\d{1,3})$/i ) { return 0 }
  0            
91 0           return 1;
92             }
93              
94              
95             =head2 isCaptchaValid
96              
97             Built in captcha support will return 1 or 0 based on the last captcha post.
98              
99             =cut
100              
101             sub isCaptchaValid {
102 0     0 1   my ( $self ) = @_;
103 0           my $publicKey = $self->siteValue( 'captchaPublicKey' );
104 0           my $privateKey = $self->siteValue( 'captchaPrivateKey' );
105 0           my $returnHTML;
106 0 0         if ( $publicKey ) {
107 0           require Captcha::reCAPTCHA;
108 0           Captcha::reCAPTCHA->import();
109 0           my $captcha = Captcha::reCAPTCHA->new();
110 0           my $result = $captcha->check_answer( $privateKey, $ENV{REMOTE_ADDR}, $self->formValue( 'recaptcha_challenge_field' ), $self->formValue( 'recaptcha_response_field' ) );
111 0 0         if ( !$result->{is_valid} ) { return 0 }
  0            
112             }
113 0           return 1;
114             }
115              
116              
117             =head2 isStrongPassword
118              
119             FWS standard strong password checker. Upper, lower, number, at least 6 chars.
120              
121             =cut
122              
123             sub isStrongPassword {
124 0     0 1   my ( $self, $fieldValue ) = @_;
125 0 0         if ( $fieldValue !~ /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/) { return 0 }
  0            
126 0           return 1;
127             }
128              
129              
130             =head2 isElementPresent
131              
132             See if an element is present on the current page. This is here for some legacy code but should not be used because it is not good practice and could be slow if the page is complex. Just find another way to achieve the same result of knowing if something is present on a page.
133              
134             =cut
135              
136             sub isElementPresent {
137 0     0 1   my ( $self, $guid, $elementName ) = @_;
138              
139             #
140             # Lets check if the formavalue FWS_elementblahblah is set if, so we have already looked this up and don't need to re-run it
141             #
142 0           my $isPresent = $self->formValue( 'FWS_ELEMENT_PRESENT_' . $elementName );
143              
144             #
145             # if it is blank, then we do need to run it for the first time :(
146             #
147 0 0         if ( !$isPresent ) {
148              
149             #
150             # pull from the database to see if its there
151             #
152 0           my $pageId = $self->getPageGUID( $guid );
153 0           ( $isPresent ) = @{$self->runSQL( SQL => "select 1 from data left join guid_xref on data.guid=child where guid_xref.parent='". $self->safeSQL( $pageId ) . "' and data.site_guid='" . $self->safeSQL( $self->{siteGUID} ) . "' and (element_type like '" . $self->safeSQL( $elementName ) . "')" )};
  0            
154              
155             #
156             # if it comes back as "NO NO NO!" then it will be blank. so we will need to set it to 0
157             #
158 0   0       $isPresent ||= 0;
159              
160             #
161             # Set the form value to what the value is so then we don't have to worry about it the next time we are here
162             #
163 0           $self->formValue( 'FWS_ELEMENT_PRESENT_' . $elementName, $isPresent );
164             }
165              
166             #
167             # pass back the value if we have gotten it from the cache or we had to look it up
168             #
169 0           return $isPresent;
170             }
171              
172              
173             =head2 dateDiff
174              
175             Return the amount of time between two dates in days or seconds.
176              
177             Possible Parameters:
178              
179             =over 4
180              
181             =item * date
182              
183             The base date to compare against
184              
185             =item * compDate
186              
187             A date in the future or past compare it to. If not passed, the current date will be used.
188              
189             =item * format
190              
191             The date format used. Default is SQLTime, you can choose epoch as an alternative
192              
193             =item * type
194              
195             The compare type to return as. Default is in 'seconds', you set this to 'days' if you would like the amount in days with its remainder as a decimal.
196              
197             =back
198              
199             =cut
200              
201             sub dateDiff {
202 0     0 1   my ( $self, %paramHash ) = @_;
203              
204 0           my $format = 'SQLTime';
205              
206 0           my $epoch1 = $self->formatDate( format => 'epoch', $format => $paramHash{date} );
207 0           my $epoch2 = $self->formatDate( format => 'epoch', $format => $paramHash{compDate} );
208              
209 0           my $secDiff = ( $epoch2 - $epoch1 );
210              
211             #
212             # if its 0 lets get out of here so we don't have devide by 0 errors
213             #
214 0 0         if ( $secDiff == 0 ) { return 0 }
  0            
215              
216 0 0         if ( $paramHash{type} =~ /day/i ) { return $secDiff / 86400 }
  0            
217              
218 0           return $secDiff;
219             }
220              
221              
222             =head1 AUTHOR
223              
224             Nate Lewis, C<< >>
225              
226             =head1 BUGS
227              
228             Please report any bugs or feature requests to C, or through
229             the web interface at L. I will be notified, and then you'll
230             automatically be notified of progress on your bug as I make changes.
231              
232              
233             =head1 SUPPORT
234              
235             You can find documentation for this module with the perldoc command.
236              
237             perldoc FWS::V2::Check
238              
239              
240             You can also look for information at:
241              
242             =over 4
243              
244             =item * RT: CPAN's request tracker (report bugs here)
245              
246             L
247              
248             =item * AnnoCPAN: Annotated CPAN documentation
249              
250             L
251              
252             =item * CPAN Ratings
253              
254             L
255              
256             =item * Search CPAN
257              
258             L
259              
260             =back
261              
262             =head1 LICENSE AND COPYRIGHT
263              
264             Copyright 2013 Nate Lewis.
265              
266             This program is free software; you can redistribute it and/or modify it
267             under the terms of either: the GNU General Public License as published
268             by the Free Software Foundation; or the Artistic License.
269              
270             See http://dev.perl.org/licenses/ for more information.
271              
272              
273             =cut
274              
275             1; # End of FWS::V2::Check