V2/Session.pm untested

filter: Statementstmt 0.0% 0 / 329 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 616 CC 74 SCAR 51.3
CC
74
Cov
0%
_localLogin
506.0
setSession
210.0
setSiteFriendly
132.0
CRAP 169.6
1 package FWS::V2::Session;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 no warnings 'uninitialized';
7  
8 =head1 NAME
9  
10 FWS::V2::Session - Framework Sites version 2 session related 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 my $fws = FWS::V2->new();
26  
27 $fws->saveFormValue( 'thisThing' );
28  
29  
30 =head1 DESCRIPTION
31  
32 FWS version 2 session methods are used to manipulate how a session is maintained, authenticated, and initiated.
33  
34 =head1 METHODS
35  
36  
37 =head2 formArray
38  
39 Return a list of all the available form values passed.
40  
41 #
42 # get the array
43 #
44 my @formArray = $fws->formArray();
45  
46 =cut
47  
48 sub formArray {
49 my ( $self ) = @_;
50 my @returnArray;
51 for my $key ( keys %{$self->{form}} ) { push ( @returnArray, $key ) }
52 return @returnArray;
53 }
54  
55  
56 =head2 formValue
57  
58 Get or set a form value. If the value was not set, it will always return an empty string, never undefined.
59  
60 #
61 # set the form value
62 #
63 $fws->formValue( 'myVar', 'This is what its set to' );
64  
65 #
66 # get the form value and pass it to the html rendering
67 #
68 $valueHash{html} .= $fws->formValue( 'myVar' );
69  
70 =cut
71  
72 sub formValue {
73 my ( $self, $field, $fieldVal ) = @_;
74 if ( defined $fieldVal ) { $self->{form}{$field} = $fieldVal }
75 if ( !defined $self->{form}{$field}) { $self->{form}{$field} = '' }
76 return $self->{form}{$field};
77 }
78  
79  
80 =head2 fwsGUID
81  
82 Retrieve the GUID for the fws site. If it does not yet exist, make a new one.
83  
84 print $fws->fwsGUID();
85  
86 =cut
87  
88 sub fwsGUID {
89 my ( $self ) = @_;
90  
91 #
92 # if is not set, set it and create the site id
93 #
94 if ( !$self->siteValue( 'fwsGUID' ) ) {
95  
96 #
97 # get the sid for the fws site
98 #
99 my ( $fwsGUID ) = $self->getSiteGUID( 'fws' );
100  
101 #
102 # if its blank make a new one
103 #
104 if ( !$fwsGUID ) {
105 $fwsGUID = $self->createGUID( 'f' );
106 my ( $adminGUID ) = $self->getSiteGUID( 'admin' );
107 $self->runSQL( SQL => "insert into site set sid='fws', guid='" . $fwsGUID . "', site_guid='" . $self->safeSQL( $adminGUID ) . "'" );
108 }
109  
110 #
111 # add it as a siteValue and return the result
112 #
113 $self->siteValue( 'fwsGUID', $fwsGUID ) ;
114 return $fwsGUID;
115 }
116  
117 #
118 # I already know it, just return the result
119 #
120 return $self->siteValue( 'fwsGUID' );
121 }
122  
123  
124 =head2 setFormValues
125  
126 Gather the passed form values, and from it set the language formValue and the session formValue.
127  
128 #
129 # get the array
130 #
131 $fws->setFormValues();
132  
133 =cut
134  
135 sub setFormValues {
136 my ( $self ) = @_;
137 use CGI qw(:cgi);
138 my $cgi = CGI->new();
139 $CGI::POST_MAX=-1;
140 foreach my $paramIn ( $cgi->param ) { $self->{form}{$paramIn} = $cgi->param( $paramIn ) }
141  
142 #
143 # grab the one from the cookie if we have it
144 #
145 my $cookieSession = $cgi->cookie( $self->{sessionCookieName} );
146 if ( $cookieSession && !$self->{form}{session} ) { $self->{form}{session} = $cookieSession }
147  
148 #
149 # if fws_lang is not set, lets set it
150 #
151 if ( $self->{form}{fws_lang} ) {
152 $self->language( uc( $self->{form}{fws_lang} ) )
153 }
154  
155 return;
156 }
157  
158  
159 =head2 setSession
160  
161 Set the session for a FWS web based page rendering.
162  
163 #
164 # Set the session
165 #
166 $fws->setSession();
167  
168 =cut
169  
170 sub setSession {
171 my ( $self ) = @_;
172  
173 #
174 # kill session was set, lets make sure the session wasn't passed
175 #
176 if ( $self->formValue( 'killSession' ) ) {
177 #
178 # run the SQL to delete the session and then ditch the session id
179 #
180 $self->runSQL( SQL => "delete from fws_sessions where id='" . $self->safeSQL( $self->formValue( 'session' ) ) . "'" );
181 $self->formValue( 'session', '' )
182 }
183  
184 #
185 # pull the current session
186 #
187 my ( $id, $fws_lang, $s_e, $s_a, $a_exp, $s_s, $s_b, $s_bs, $s_ip, $extra ) = @{$self->runSQL( SQL => "select id,fws_lang,e,a,a_exp,s,b,bs,ip,extra_value from fws_sessions where id='" . $self->safeSQL( $self->formValue( 'session' ) ) . "'" )};
188  
189 #
190 # set the FWS_SESSION so we can see if it changed
191 #
192 $self->formValue( "FWS_SESSION", $s_b . "|" . $fws_lang . "|" . $s_bs . "|" . $s_ip . "|" . $s_e . "|" . $s_a . "|" . $a_exp . "|" . $s_s . "|" . $extra) ;
193  
194 #
195 # if the session isn't in there, or the session is blank
196 #
197 if ( !$id || $ENV{REMOTE_ADDR} ne $s_ip || length( $self->formValue( 'session' ) ) != 33 ) {
198 $s_b = $s_bs = $s_ip = $s_e = $s_a = $s_s = $extra = '';
199 $a_exp = '0';
200 $self->adminLogOut();
201 $self->userLogOut();
202 $self->formValue( 'session', $self->createGUID( 'u' ) );
203 $self->runSQL( SQL => "insert into fws_sessions ( id ) values ('" . $self->safeSQL( $self->formValue( 'session' ) ) . "')" );
204 }
205  
206 #
207 # read the extra and save it
208 #
209 my @extraSplit = split( /\|/, $extra );
210 my %saveWithSession = $self->_saveWithSessionHash();
211 while (@extraSplit) {
212 my $fieldName = shift( @extraSplit );
213 my $fieldValue = shift( @extraSplit );
214 #
215 # for security reasons lets make sure we aren't touching the major ones
216 #
217 if ( $fieldName !~ /^(p|b|s|bs|e|l|a|a2|fws_lang|id)$/ ) {
218 #
219 # only grab the one from the session, if we have not passed it to the script to change it
220 #
221 if ( $self->formValue( $fieldName ) eq '' ) { $self->formValue( $fieldName, $self->urlDecode( $fieldValue ) ) }
222 $saveWithSession{$fieldName} = 1;
223 }
224 }
225  
226 $self->_saveWithSessionHash( %saveWithSession );
227  
228 #
229 # set the goodies
230 #
231 if ( $self->formValue( 'b' ) eq '' ) { $self->{userLoginId} = $s_b; $self->formValue( 'b', $s_b ) }
232 if ( $self->formValue( 'fws_lang' ) eq '' ) { $self->language( $fws_lang ) }
233 if ( $self->formValue( 'editMode' ) eq '' ) { $self->formValue( 'editMode', $s_e ) }
234 if ( $self->formValue( 'bs' ) eq '' ) { $self->{adminLoginId} = $s_bs; $self->formValue( 'bs', $s_bs ) }
235 if ( $self->formValue( 'a' ) eq '' ) { $self->formValue( 'a', $s_a ) }
236  
237 #
238 # variablize time for consistancy
239 #
240 my $theTime = time();
241  
242 #
243 # if we have an A value lets do some stuff with the aff exp
244 #
245 if ( $self->formValue( 'a' ) ne '' ) {
246  
247 #
248 # this is a new affiliate or it has been switched lets reset tht time
249 #
250 if ( $self->formValue( 'a' ) ne $s_a ) { $a_exp = $theTime + $self->{affiliateExpMax} }
251  
252 #
253 # if for whatever reason it has never been set, lets set it now before we do any calcs
254 #
255 if ( $a_exp < 1 ) { $a_exp = $theTime + $self->{affiliateExpMax} }
256  
257 #
258 # check to see if what ever it is, is expired, if so, lets blank out the affiliateId
259 #
260 if ( $a_exp < $theTime ) {
261 $self->formValue( 'a', '' );
262 $a_exp = '0';
263 }
264 }
265  
266 #
267 # if the affilaite has not been set, then make sure the a_exp is so no time gets slapped on to a new one
268 #
269 else { $a_exp = '0' }
270  
271 #
272 # Set Affiliate ID: if it was set or derived and the exp date;
273 #
274 $self->{affiliateId} = $self->formValue( 'a' );
275 $self->{affiliateExp} = $a_exp;
276 $self->{adminSafeMode} = $s_s ||= 0;
277  
278 return;
279 }
280  
281  
282 =head2 setSiteFriendly
283  
284 Determine the site friendly url and set it.
285  
286 #
287 # Set the site friendly URL
288 #
289 $fws->setSiteFriendly();
290  
291 =cut
292  
293 sub setSiteFriendly {
294 my ( $self ) = @_;
295  
296 #
297 # 404 page descisions
298 #
299 my $sid = $self->formValue( 's' );
300  
301 #
302 # clean up the sid to make sure there is no funny business going on
303 # its used pretty loosly so this will save some front end work
304 #
305 $sid =~ s/[^a-z0-9]//sgi;
306  
307 #
308 # if sid is 404 or '' lets default out sid
309 #
310 if ( $sid eq '404' || $sid eq '' ) {
311  
312 #
313 # for completeness lets set these to the admin user if they are not set
314 #
315 ( $sid ) = @{$self->runSQL( SQL=> "select sid from site where default_site = '1' limit 1" )};
316 if ( $sid eq '' ) { $sid = 'admin' }
317 }
318  
319  
320 #
321 # if p is not set, then lets figure out how to set it
322 #
323 if ( $self->formValue( 'p' ) eq '' && $self->formValue( 'pageAction' ) eq '' ) {
324  
325 #
326 # set the friendlyURL from the URI
327 #
328 my $friendlyURL = $ENV{REQUEST_URI};
329 $friendlyURL =~ s/^\///sg;
330 $friendlyURL =~ s/\/$//sg;
331 $friendlyURL =~ s/\?(.*)//sg;
332  
333  
334 #
335 # add any url params pass to the formValue pieces
336 #
337 my $urlParams = $1;
338 my @pairs = split(/&/, $urlParams);
339 foreach my $pair (@pairs){
340 my ( $name, $value ) = split ( /=/, $pair );
341 $self->formValue( $name, $value );
342 }
343  
344 #
345 # switch us to the fws site because we are looking for admin
346 # and set the site ID to what it needs to be
347 #
348 if ( $self->formValue( 'p' ) eq 'admin' || $friendlyURL eq 'admin' ) {
349 $self->formValue( 'p' , 'admin' );
350 }
351 else {
352 #
353 # get permalink ready for parsing to see if we need to do some exta form field settings
354 # we ditch any extension to make it so people can imitate any type of document
355 #
356 ( my $guidBasedURL = $friendlyURL ) =~ s/\..*//sg;
357 my ( $permPage, $permId ) = split( /\//, $guidBasedURL );
358  
359  
360 #
361 # Add to the union for potential tables that alos hold friendlies
362 #
363 my $addToUnion;
364 my $adminCount = 3;
365 for my $table ( keys %{$self->{dataSchema}} ) {
366 if ( $table ne 'data' && $self->{dataSchema}{$table}{friendly_url}{type} ) {
367 $addToUnion .= "union SELECT " . $self->safeSQL( $table ) . ".guid," . $self->safeSQL( $table ) . ".page_friendly_url," . $self->safeSQL( $adminCount ) . " as ordering from " . $self->safeSQL( $table ) . " where friendly_url='" . $self->safeSQL ( $friendlyURL ) . "' ";
368 $adminCount++;
369 }
370 }
371  
372  
373 #
374 # got a match, siteGUID the siteGUID and and find a freidnly match
375 #
376 if ( $sid ne '' && $friendlyURL ne '' ) {
377 my ( $p, $pageFriendlyURL, $theOrder ) = @{$self->runSQL( SQL =>
378 "select data.guid,data.page_friendly_url,1 as ordering from data left join site on site.guid=data.site_guid where site.sid='" . $self->safeSQL( $sid ) . "' and friendly_url='" . $self->safeSQL( $friendlyURL ) . "' ".
379 "union select data.guid,data.page_friendly_url,2 as ordering from data left join site on site.guid=data.site_guid where data.guid='" . $self->safeSQL( $permPage ) . "' or (site.sid='" . $self->safeSQL( $sid ) . "' and friendly_url='" . $self->safeSQL( $permPage ) . "') ".
380 "union select data.guid,data.page_friendly_url,3 as ordering from data left join site on site.guid=data.site_guid where site.sid='fws' and friendly_url='" . $self->safeSQL ( $friendlyURL ) . "' ".
381 $addToUnion.
382 "union select '','',999 as ordering order by ordering" )};
383  
384 #
385 # if order is two, set ID to what ever the second number is after the /
386 #
387 if ( $theOrder eq '2' ) { $self->formValue( 'id', $self->safeQuery( $permId ) ) }
388  
389 if ( $pageFriendlyURL ne '' ) {
390 #
391 # if this page came from a friendly but has pageFriendlyURL, this is special!
392 # it will use the pageFriendlyURL place of its intended page and set its ID to what page it would have been.
393 #
394 $self->formValue( 'id', $p );
395 ( $p ) = @{$self->runSQL( SQL => "select data.guid from data left join site on site.guid=data.site_guid where site.sid='" . $self->safeSQL( $sid ) . "' and friendly_url='" . $self->safeSQL( $pageFriendlyURL ) . "'" )};
396  
397 #
398 # If its blank from the query, then we are talking about the guid from the post
399 #
400 if ( !$p ) { $p = $pageFriendlyURL }
401 }
402 if ( $p ne '' ) {
403  
404 #
405 # the page does exist and it will show something.
406 # lets just set it the p value and content will
407 # figure out what to do with security in mind also.
408 # if not we will break out the home page of the site.
409 #
410 $self->formValue( 'p', $self->safeQuery( $p ) );
411 }
412 }
413 }
414 }
415  
416 #
417 # at this point we will ne know the sid. lets set it!
418 #
419 $self->{siteId} = $sid;
420 $self->formValue( 's' , $self->safeQuery( $sid ) );
421  
422 return;
423 }
424  
425  
426 =head2 language
427  
428 Get or set the current language. If no language is currently set, the first entry in language will be used. If languageArray has not be set, then 'EN' will be used. The language can be set by being passed as a form value with the name 'fws_lang'.
429  
430 #
431 # set the language
432 #
433 $fws->language( 'FR' );
434  
435 #
436 # get the language
437 #
438 $valueHash{html} .= 'The current language is: ' . $fws->language() . '<br />';
439  
440 =cut
441  
442 sub language {
443 my ( $self, $lang ) = @_;
444 if ( defined $lang) { $self->{_language} = $lang }
445 if ( !$self->{_language} ) {
446 my @langArray = $self->languageArray();
447 $self->{_language} = $langArray[0];
448 }
449 if ( !$self->{_language} ) { $self->{_language} = 'EN' }
450 return uc( $self->{_language} );
451 }
452  
453  
454 =head2 languageArray
455  
456 Set the languages the site will use. The first one in the list will be considered default. If languageArray is not set it will just contain 'EN'.
457  
458 #
459 # set the languages available
460 #
461 $fws->languageArray( 'EN', 'FR', 'SP' );
462  
463 =cut
464  
465 sub languageArray {
466 my ( $self, @languageArray ) = @_;
467 if ( defined $languageArray[0] ) { $self->{_languageArray} = uc( join( '|', @languageArray ) ) }
468 if ( !$self->{_languageArray} ) { return }
469 return ( split( /\|/, $self->{_languageArray} ) );
470 }
471  
472  
473 =head2 processLogin
474  
475 Process the web action for logins
476  
477 #
478 # Run login
479 #
480 $fws->processLogin();
481  
482 =cut
483  
484 sub processLogin {
485 my ( $self, $loginType ) = @_;
486  
487 $self->runScript( 'preLogin' );
488  
489 if ( $self->formValue( 'pageAction' ) eq 'logout' ) {
490 $self->userLogOut();
491 }
492  
493 if ( $self->formValue( 'pageAction' ) eq 'adminLogOut' ) {
494 $self->adminLogOut();
495 $self->formValue( 'editMode', 0 );
496 }
497  
498 my $loginStatusNote;
499 $loginStatusNote = $self->_localLogin();
500  
501 #
502 # if statuNote is not blank we failed login criteria
503 #
504 if ( $loginStatusNote ne '' ) {
505 my $pageActionSave = $self->formValue( 'pageActionSave' );
506 if ( $pageActionSave eq '' ) {$self->formValue( 'pageActionSave', $self->formValue( 'pageAction' ) ) }
507 }
508  
509 $self->runScript( 'postLogin' );
510  
511 return;
512 }
513  
514  
515 =head2 adminLogOut
516  
517 Log out FWS admin user
518  
519 #
520 # Log out currently logged in admin user
521 #
522 $fws->adminLogOut();
523  
524 =cut
525  
526 sub adminLogOut {
527 my ( $self ) = @_;
528 $self->{adminLoginId} = '';
529 $self->formValue( 'bs_hold', $self->formValue( 'bs' ) );
530 $self->formValue( 'bs', '' );
531 return;
532 }
533  
534  
535 =head2 userLogOut
536  
537 Log out FWS site user
538  
539 #
540 # Log out currently logged in site user
541 #
542 $fws->userLogOut();
543  
544 =cut
545  
546 sub userLogOut {
547 my ( $self ) = @_;
548 $self->{userLoginId} = '';
549 $self->formValue( 'b', '' );
550  
551 return;
552 }
553  
554  
555 =head2 cryptPassword
556  
557 Crypt password passing the crypt mmethod you would like to use. Set $fws->{FWSCrypt} to set method. Current default is perl crypt.
558  
559 Current Methods:
560 crypt: Perl default crypt
561  
562  
563 Example:
564 #
565 # Log out currently logged in site user
566 #
567 my $cryptedPass = $fws->cryptPassword( 'thePasswordYourCrypting' );
568  
569 =cut
570  
571  
572 sub cryptPassword {
573 my ( $self, $password, $cryptType ) = @_;
574  
575 #
576 # only one exported to core at this point, no need to check
577 #
578 return substr( crypt( $password, substr( $password, 0 , 2 ) ), 2 );
579 }
580  
581 sub _localLogin {
582 my ( $self ) = @_;
583  
584 #
585 # Login if the site critera works for an admin
586 #
587 if ( $self->formValue( 'bs' ) ne '' && $self->formValue( 'l_password' ) ne '' && $self->formValue( 'pageAction' ) ne 'adminLogOut' ) {
588 my ( $adminPass ) = @{$self->runSQL( SQL => "select admin_user_password from admin_user where user_id='" . $self->safeSQL( $self->formValue( 'bs' ) ) . "'" )};
589  
590 #
591 # take the admin user and crypt the password
592 #
593 my $formPassword = $self->cryptPassword( $self->formValue( 'l_password' ) );
594  
595 #
596 # do a switcharoo if we are loggin in as admin
597 # this is only used for site setup and will be disabled once a user
598 # creates an admin account
599 #
600 if ( $self->formValue( 'bs' ) eq 'admin' ) {
601  
602 #
603 # check if we have an isAdmin account. so we can disable this password
604 #
605 my $noAdmin = 1;
606 my @extraArray = @{$self->runSQL( SQL => "SELECT extra_value from admin_user" )};
607 while (@extraArray) {
608 #
609 # combine the hashes together and check for isAdmin
610 #
611 my $extraValue = shift @extraArray;
612 my %adminHash = $self->mergeExtra( $extraValue );
613 if ( $adminHash{isAdmin} ) {
614 $adminPass = '';
615 $noAdmin = 0;
616 }
617 }
618  
619 #
620 # there isn't an admin account yet, we can still use the one in the go file
621 #
622 if ( $noAdmin ) {
623 $formPassword = $self->formValue( 'l_password' );
624 $adminPass = $self->{adminPassword};
625 }
626 }
627  
628 if ( $adminPass eq $formPassword && $adminPass ne '' ) {
629 $self->{adminLoginId} = $self->formValue( 'bs' );
630 if ( $self->formValue( 'p' ) eq $self->{adminURL} ) { $self->formValue( 'p', $self->homeGUID() ) }
631 }
632 else {
633 $self->formValue( 'statusNote', $self->formValue( 'statusNote' ) . 'Your login criteria was incorrect.' );
634 $self->adminLogOut();
635 }
636 }
637  
638  
639 #
640 # Login as a profile
641 #
642 if ( ( ( $self->formValue( 'b' ) ne '' && $self->formValue( 'password' ) ne '' ) ) && $self->formValue( 'pageAction' ) ne 'logout' ) {
643 #
644 # set the BH field in case we need to use it to show in the field
645 #
646 $self->formValue( 'bh', $self->formValue( 'b' ) );
647 my ( $userGUID, $active, $passCheck, $googleAppsId ) = @{$self->runSQL( SQL => "select guid, active, profile_password, google_id from profile where email like '" . $self->safeSQL( $self->formValue( 'b' ) ) . "'" )};
648  
649 my $formPassword = $self->cryptPassword( $self->formValue( 'password' ) );
650 if ( ( $passCheck eq $formPassword && $passCheck ne '' ) ) {
651  
652 if (!$active) {
653 $self->formValue( 'statusNote', $self->formValue( 'statusNote' ) . 'Your account has been disabled.' )
654 }
655 else {
656  
657 $self->{userLoginId} = $self->formValue( 'b' );
658  
659 #
660 # if for ever reason we are on the default login page, lets dump you to the home page
661 # we don't want an endless loop
662 #
663 if ( $self->formValue( 'p' ) eq 'login' ) { $self->formValue( 'p', $self->homeGUID() ) }
664  
665 if ( $self->formValue( 'SAMLRequest' ) ne '' ) {
666 require Google::SAML::Response;
667 Google::SAML::Response->import();
668  
669 if ( $googleAppsId eq '' ) { $self->formValue( 'statusNote', $self->formValue( 'statusNote' ) . 'Your account does not have a google id associated to it. Contact your user administrator to add this to your account.' ) }
670 else {
671 my $saml = Google::SAML::Response->new( {
672 key => $self->{googleAppsKeyFile},
673 login => $googleAppsId,
674 request => $self->urlDecode( $self->formValue( 'SAMLRequest' ) )
675 } );
676 if ( $self->formValue( 'RelayState' ) ne '' ) {
677 $self->printPage( content => $saml->get_google_form( $self->urlDecode( $self->formValue( 'RelayState' ) ) ) );
678 }
679 }
680 }
681 }
682 }
683 else { $self->userLogOut() }
684 }
685  
686 #
687 # if we logged in as a admin user lets set our permissions
688 #
689 if ( $self->{adminLoginId} ) {
690  
691 my ( $extraValue ) = @{$self->runSQL( SQL => "SELECT extra_value from admin_user where user_id='" . $self->safeSQL( $self->{adminLoginId} ) . "'" )};
692  
693 #
694 # combine the hashes together
695 #
696 my %adminHash = $self->mergeExtra( $extraValue );
697  
698 #
699 # get the keys and and set them
700 #
701 for my $key ( keys %adminHash ) { $self->userValue( $key, $adminHash{$key} ) }
702  
703 #
704 # if we logged in as admin - or we have isAdmin clicked. lets give the full montie
705 #
706 if ( $self->{adminLoginId} eq 'admin' || $self->userValue( 'isAdmin' ) eq '1' ) {
707 #
708 # just to make sure, if did come in to 'admin' then mark isAdmin to 1
709 #
710 $self->userValue( 'isAdmin', 1 );
711  
712 #
713 # Restrict the login if they came in on the adminSafePassword
714 #
715 if ( $self->{adminSafeMode} eq '1' ) {
716 $self->userValue( 'showDeveloper', '0' );
717 $self->userValue( 'showAdminUsers', '0' );
718 $self->userValue( 'isAdmin', '0' );
719 }
720 }
721  
722 #
723 # if we are not an admin person. lets make sure we cant touch the admin table
724 #
725 else { $self->userValue( 'showAdminUsers', '0' ) }
726 }
727  
728 #
729 # we aren't logged in as an admin, lets ditch edit mode!
730 #
731 if ( !$self->{adminLoginId} ) { $self->formValue( 'editMode', 0 ) }
732  
733  
734 #
735 # we aren't logged in at all, lets ditch anything dangerous
736 #
737 #
738 if ( !$self->{userLoginId} && !$self->{adminLoginId} ) {
739  
740 #
741 # safe page actions its allowd to be set to. if not ditch the pageAction
742 # this is an artifact of some old code pageAction code, and eventually should be removed
743 #
744 if ( $self->formValue( 'pageAction' ) !~ /^(\d+|addToNewsList|formMail|recoverPassword|updateCart|addProfile)$/) {
745 $self->formValue( 'pageAction', '' )
746 }
747 if ( $self->formValue( 'l_password' ) ne '' ) { $self->formValue( 'editMode', 0 ) }
748 if ( $self->formValue( 'password' ) ne '' || $self->formValue( 'l_password' ) ne '' ) {
749 return 'Your password is invalid or expired';
750 }
751 }
752  
753 return;
754 }
755  
756  
757 =head2 saveFormValue
758  
759 Mark a formValue to remain persistant with the session. The return of this function will be the current saveWithSessionHash.
760
761 #
762 # This should go in a 'init' element or located int he go.pl file
763 # at any point it can be referenced via formValue
764 #
765 $fws->saveFormValue( 'myCity' );
766  
767 =cut
768  
769 sub saveFormValue {
770 my ( $self, $fieldName ) = @_;
771 #
772 # add to session
773 #
774 my %saveWithSession = $self->_saveWithSessionHash();
775 $saveWithSession{$fieldName} = 1;
776 return $self->_saveWithSessionHash( %saveWithSession );
777 }
778  
779 =head2 saveSession
780  
781 Commit the current session to the sessions table. This is only needed if you need to cmmit the session before you perform a 302 redirect or if yuor immitating the pringPage() method with a custom one. Not the session does actually do anything if its values have not chagned from its innital load from the session table.
782  
783 #
784 # This should go in a 'init' element or located int he go.pl file
785 # at any point it can be referenced via formValue
786 #
787 $fws->saveSession();
788  
789 =cut
790  
791 sub saveSession {
792 my ( $self, $fieldName ) = @_;
793  
794 #
795 # get the save with session hash put together
796 #
797 my $sessionScript = '';
798 my %saveWithSessionHash = $self->_saveWithSessionHash();
799 for my $sessionKey ( keys %saveWithSessionHash ) {
800 my $keyValue = $self->formValue( $sessionKey );
801 $sessionScript .= $sessionKey . '|' . $self->urlEncode( $keyValue ) . '|';
802 }
803  
804 #
805 # set the editMode in range if it is blank
806 #
807 if ( !$self->formValue( 'editMode' ) ) { $self->formValue( 'editMode', 0 ) }
808  
809 #
810 # Set the cookie and update the session.. and other groovy header rutines
811 # only if it is diffrent lets update it
812 #
813 if ( $self->formValue( 'FWS_SESSION' ) ne $self->{userLoginId} . '|' . $self->language() . '|' . $self->{adminLoginId} . '|' . $ENV{REMOTE_ADDR} . '|' . $self->formValue( 'editMode' ) . '|' . $self->{affiliateId} . '|' . $self->{affiliateExp} . '|' . $self->{adminSafeMode} . '|' . $sessionScript ) {
814 #
815 # run the SQL to update the session
816 #
817 $self->runSQL( SQL => "update fws_sessions set fws_lang='" . $self->safeSQL( $self->language() ) . "', b='" . $self->safeSQL( $self->{userLoginId} ) . "', s='" . $self->safeSQL( $self->{adminSafeMode} ) . "', bs='" . $self->safeSQL( $self->{adminLoginId} ) . "', ip='" . $self->safeSQL( $ENV{REMOTE_ADDR} ) . "', e='" . $self->safeSQL( $self->formValue( 'editMode' ) ) . "', a='" . $self->safeSQL( $self->{affiliateId} ) . "', a_exp='" . $self->safeSQL( $self->{affiliateExp} ) . "', extra_value='" . $self->safeSQL( $sessionScript ) . "' where id='" . $self->safeSQL( $self->formValue( 'session' ) ) . "'" );
818 }
819 }
820  
821  
822 =head2 setSiteValues
823  
824 Set default values derived from the site settings for a site. This will also set the formValue 'p' to the homeGUID if no 'p' value is currently set.
825  
826 #
827 # currently rendered site
828 #
829 $fws->setSiteValues();
830  
831 #
832 # set site values for some other site
833 #
834 $fws->setSiteValues( 'othersite' );
835  
836 =cut
837  
838 sub setSiteValues {
839 my ( $self, $siteId ) = @_;
840  
841 #
842 # pre-set the valuse if they are not already
843 #
844 if ( $siteId ) { $self->{siteId} = $siteId }
845  
846 #
847 # if for any reason this thing is still blank, lets use our default "site"
848 #
849 if ( !$self->{siteId} ) { $self->{siteId} = 'site' }
850  
851 #
852 # get and set site assigned values
853 #
854 my $siteExtraValue;
855 my $default_site;
856 ( $self->{siteId}, $self->{languageArray}, $self->{siteName}, $self->{site}{cssDevel}, $self->{site}{jsDevel}, $self->{siteGUID}, $siteExtraValue, $default_site, $self->{gatewayUserID}, $self->{gatewayType}, $self->{email}, $self->{site}{homeGUID} ) = @{$self->runSQL( SQL => "select sid, language_array, name, css_devel, js_devel, guid, extra_value, default_site, gateway_user_id, gateway_type, email, home_guid from site where sid='" . $self->safeSQL( $self->{siteId} ) . "'" )};
857  
858 #
859 # if we didn't get defined then then this is not the default site
860 #
861 $default_site ||= 0;
862
863 #
864 # if this is STILL blank, something bad happened, lets still set it to site
865 #
866 $self->{siteId} ||= 'site';
867  
868 #
869 # move the lang into the useable array
870 #
871 $self->languageArray( split( ',', $self->{languageArray} ) ) if $self->{languageArray};
872  
873 #
874 # if for any reason homeGUID is blank, lets make a new one and its page
875 # but only do it, if we have a real site. Just skip it because we could
876 # be doing something else that isn't site rendering
877 #
878 if ( !$self->{site}{homeGUID} && $self->{siteGUID} ) {
879 my $homeGUID = $self->{siteGUID};
880 $homeGUID =~ s/^./h/sg;
881 $self->siteValue( 'homeGUID', $homeGUID );
882 $self->runSQL( SQL => "update site set home_guid='" . $self->safeSQL( $homeGUID ) . "' where guid='" . $self->safeSQL( $self->{siteGUID} ) . "'" );
883  
884 #
885 # make the actual page
886 # there isn't actually a type home, this is the flag that allows you to make a
887 # xref that does not have a parent it will be flipped to 'page'
888 #
889 $self->saveData( type => 'home', parent => '', newGUID => $homeGUID );
890 }
891  
892 #
893 # check to see if there is no level... if so then we need create a new admin account
894 #
895 if ( !$self->{siteGUID} ) { print $self->createFWSDatabase() }
896  
897 #
898 # convert the values and fields to global valuse
899 #
900 my %siteHash = $self->mergeExtra( $siteExtraValue );
901  
902 #
903 # get the keys and and set them
904 #
905 for my $key ( keys %siteHash ) { $self->siteValue( $key, $siteHash{$key} ) }
906  
907 #
908 # set the data cache hash
909 #
910 my @indexArray = split( /,/, $self->siteValue( 'dataCacheIndex' ) );
911 while (@indexArray) { $self->{dataCacheFields}->{shift(@indexArray)} = 1 }
912  
913 #
914 # if we are not the default site turn off friendlies
915 #
916 if ( $default_site ne '1' ) { $self->siteValue( 'noFriendlies', '1' ) }
917 else { $self->siteValue( 'noFriendlies', '0' ) }
918  
919 #
920 # Now that we have the session we can set the queryHead
921 #
922 $self->{queryHead} = "?fws_noCache=" .
923 $self->createPassword( composition => 'qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKZXCVBNM',lowLength=>6,highLength=>6) .
924 "&session=" . $self->formValue( 'session' ) .
925 "&s=" . $self->{siteId} . "&";
926  
927 #
928 # if p is still blank, lets set it
929 #
930 if ( $self->formValue( 'p' ) eq '' ) { $self->formValue( 'p', $self->siteValue( 'homeGUID' ) ) }
931  
932 #
933 # set where we will get FWS files from
934 #
935 $self->{fileFWSPath} = $self->fileWebPath() . '/fws';
936  
937 return;
938 }
939  
940  
941 =head2 siteValue
942  
943 Get or save a siteValue for the session.
944  
945 #
946 # save something
947 #
948 $fws->siteValue( 'something', 'this is something' );
949  
950 #
951 # get something
952 #
953 my $something = $fws->siteValue( 'something' );
954  
955 =cut
956  
957 sub siteValue {
958 my ( $self, $field, $fieldVal ) = @_;
959 if ( defined $fieldVal ) { $self->{site}{$field} = $fieldVal }
960 if ( !defined $self->{site}{$field} ) { $self->{site}{$field} = '' }
961 return $self->{site}{$field};
962 }
963  
964  
965 =head2 userValue
966  
967 Get an admin user value. This is used mostly for security flags and is only used to get values FWS has set.
968  
969 #
970 # is the admin user a developer?
971 #
972 my $isDeveloper = $fws->userValue( 'isDeveloper' );
973  
974 =cut
975  
976 sub userValue {
977 my ( $self, $field, $fieldVal ) = @_;
978 if ( defined $fieldVal ) { $self->{user}{$field} = $fieldVal }
979 if ( !defined $self->{user}{$field} ) { $self->{user}{$field} = '' }
980 return $self->{user}{$field};
981 }
982  
983  
984 sub _saveWithSessionHash {
985 my ( $self, %saveWithSessionHash ) = @_;
986 if ( keys %saveWithSessionHash ) { %{$self->{_saveWithSessionHash}} = %saveWithSessionHash }
987  
988 #
989 # add the save with session site value directive also
990
991 my @addSession = split( /,/, $self->siteValue( 'saveWithSession' ) );
992 while ( @addSession ) { ${$self->{_saveWithSessionHash}}{ shift @addSession } = 1 }
993  
994 return %{$self->{_saveWithSessionHash}};
995 }
996  
997  
998  
999  
1000 =head1 AUTHOR
1001  
1002 Nate Lewis, C<< <nlewis at gnetworks.com> >>
1003  
1004 =head1 BUGS
1005  
1006 Please report any bugs or feature requests to C<bug-fws-v2 at rt.cpan.org>, or through
1007 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FWS-V2>. I will be notified, and then you'll
1008 automatically be notified of progress on your bug as I make changes.
1009  
1010  
1011  
1012  
1013 =head1 SUPPORT
1014  
1015 You can find documentation for this module with the perldoc command.
1016  
1017 perldoc FWS::V2::Session
1018  
1019  
1020 You can also look for information at:
1021  
1022 =over 4
1023  
1024 =item * RT: CPAN's request tracker (report bugs here)
1025  
1026 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FWS-V2>
1027  
1028 =item * AnnoCPAN: Annotated CPAN documentation
1029  
1030 L<http://annocpan.org/dist/FWS-V2>
1031  
1032 =item * CPAN Ratings
1033  
1034 L<http://cpanratings.perl.org/d/FWS-V2>
1035  
1036 =item * Search CPAN
1037  
1038 L<http://search.cpan.org/dist/FWS-V2/>
1039  
1040 =back
1041  
1042  
1043 =head1 LICENSE AND COPYRIGHT
1044  
1045 Copyright 2013 Nate Lewis.
1046  
1047 This program is free software; you can redistribute it and/or modify it
1048 under the terms of either: the GNU General Public License as published
1049 by the Free Software Foundation; or the Artistic License.
1050  
1051 See http://dev.perl.org/licenses/ for more information.
1052  
1053  
1054 =cut
1055  
1056 1; # End of FWS::V2::Session