| 1 | package Puzzle::Block; | ||
| 2 | |||
| 3 | our $VERSION = '0.17'; | ||
| 4 | |||
| 5 | use Params::Validate qw(:types); | ||
| 6 | use base 'Class::Container'; | ||
| 7 | |||
| 8 | use HTML::Mason::Exceptions; | ||
| 9 | |||
| 10 | use HTML::Mason::MethodMaker( | ||
| 11 | read_only => [qw(isa_error direct_output)], | ||
| 12 | read_write => [ | ||
| 13 | 'html', | ||
| 14 | [ headers => { parse => 'string', type => SCALAR|UNDEF, default => ''} ], | ||
| 15 | [ body => { parse => 'string', type => SCALAR|UNDEF, default => ''} ], | ||
| 16 | [ title => { parse => 'string', type => SCALAR|UNDEF, default => ''} ], | ||
| 17 | [ body_attributes => { parse => 'string', type => SCALAR|UNDEF, default => ''} ], | ||
| 18 | [ comp => { isa => 'HTML::Mason::Component' } ], | ||
| 19 | ] | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub comp_path { | ||
| 23 | my $self = shift; | ||
| 24 | my $m = $self->container->container->_mason; | ||
| 25 | if (@_) { | ||
| 26 | # reset component | ||
| 27 | undef $self->{comp}; | ||
| 28 | $self->{comp_path} = shift; | ||
| 29 | $m->comp_exists($self->{comp_path}) && | ||
| 30 | $self->comp($m->fetch_comp($self->{comp_path})); | ||
| 31 | } | ||
| 32 | return $self->{comp_path} | ||
| 33 | } | ||
| 34 | |||
| 35 | sub process { | ||
| 36 | my $self = shift; | ||
| 37 | my $puzzle = $self->container->container; | ||
| 38 | my $tmpl = $puzzle->tmpl; | ||
| 39 | $self->reset; | ||
| 40 | unless ($self->comp) { | ||
| 41 | $self->body("Unable to find <b>" . $self->comp_path . "</b> file"); | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | # voglio anche l'header | ||
| 45 | $tmpl->autoDeleteHeader(0); | ||
| 46 | $self->html($self->get_html); | ||
| 47 | # ora mi estraggo gli header e li rimuovo | ||
| 48 | $tmpl->autoDeleteHeader(1); | ||
| 49 | $tmpl->tmplfile(\$self->html); | ||
| 50 | $self->body($tmpl->output); | ||
| 51 | my $headers = $tmpl->header_css . $tmpl->header_js; | ||
| 52 | # add meta refresh if present | ||
| 53 | my $metas = $tmpl->header_tokens->{meta}; | ||
| 54 | my $metaf = q/http-equiv\s*=\s*"\s*refresh\s*"/; | ||
| 55 | map {($_->[0] =~ /$metaf/) && ($headers .= $_->[0])} @$metas; | ||
| 56 | $self->headers($headers); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub process_as_center { | ||
| 60 | my $self = shift; | ||
| 61 | my $puzzle = $self->container->container; | ||
| 62 | my $tmpl = $puzzle->tmpl; | ||
| 63 | my $m = $puzzle->_mason; | ||
| 64 | $self->reset; | ||
| 65 | # recupero l'intero blocco html in vari modi | ||
| 66 | $tmpl->autoDeleteHeader(0); | ||
| 67 | # ottengo, in qualche modo, la pagina html con il body | ||
| 68 | eval {$self->html($self->get_html)}; | ||
| 69 | if ($@) { | ||
| 70 | # c'e' un errore | ||
| 71 | if ( isa_mason_exception($@) && | ||
| 72 | !isa_mason_exception($@, 'Abort') && | ||
| 73 | !isa_mason_exception($@, 'Decline')) { | ||
| 74 | if ($puzzle->error ne '') { | ||
| 75 | # se ho definito questa variabile, allora e' | ||
| 76 | # un errore mio e mostro questo come html | ||
| 77 | # altrimenti uso quello di default | ||
| 78 | $self->html($puzzle->error); | ||
| 79 | } else { | ||
| 80 | $self->html($@->as_html); | ||
| 81 | } | ||
| 82 | } else { | ||
| 83 | # e' un errore mason | ||
| 84 | $self->html($@); | ||
| 85 | # print Data::Dumper::Dumper($self->html); | ||
| 86 | $self->{isa_error} = 1; | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | } else { | ||
| 90 | $self->{isa_error} = 0; | ||
| 91 | } | ||
| 92 | if( ($m->apache_req->content_type ne '' && | ||
| 93 | lc($m->apache_req->content_type) ne 'text/html') | ||
| 94 | || ($self->html =~/<[fF][Rr][Aa][Mm][Ee][Ss][Ee][Tt]/) ) | ||
| 95 | { | ||
| 96 | # risultato non di tipo html...output diretto oppure | ||
| 97 | # pagina con frame...output diretto | ||
| 98 | $self->{direct_output} = 1; | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | # tutto regolare...procedo | ||
| 102 | # rimuovo l'header | ||
| 103 | $tmpl->autoDeleteHeader(1); | ||
| 104 | #my $pippo = $self->html; | ||
| 105 | $tmpl->tmplfile(\$self->html); | ||
| 106 | $self->body($tmpl->output); | ||
| 107 | #altero la configurazione sulla base delle informazioni | ||
| 108 | # eventualmente presenti nell'<HEAD> dell'HTML | ||
| 109 | $self->alterConfig($nextComp); | ||
| 110 | $self->headers($tmpl->header_css . $tmpl->header_js); | ||
| 111 | $self->body_attributes($tmpl->body_attributes); | ||
| 112 | $self->title($tmpl->header_tokens->{title}->[0]->[1]) | ||
| 113 | } | ||
| 114 | |||
| 115 | sub process_as_login { | ||
| 116 | my $self = shift; | ||
| 117 | my $puzzle = $self->container->container; | ||
| 118 | my $tmpl = $puzzle->tmpl; | ||
| 119 | unless ($self->comp) { | ||
| 120 | #$self->body("Login file in <b>" . $puzzle->cfg->login . "</b> not found"); | ||
| 121 | $puzzle->args->set({'exception.cod' => "Login file not found", | ||
| 122 | 'exception.descr' => "Login file in <b>" . $puzzle->cfg->login . | ||
| 123 | "</b> not found"}); | ||
| 124 | $puzzle->exception->print; | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | $tmpl->autoDeleteHeader(0); | ||
| 128 | $puzzle->args->page($ENV{SCRIPT_NAME}); | ||
| 129 | $puzzle->args->group(join(' or ',@{$puzzle->cfg->gids} )); | ||
| 130 | $self->html($self->get_html); | ||
| 131 | # rimuovo l'header | ||
| 132 | $tmpl->autoDeleteHeader(1); | ||
| 133 | $tmpl->tmplfile(\$self->html); | ||
| 134 | $self->body($tmpl->output); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub alterConfig() { | ||
| 138 | my $self = shift; | ||
| 139 | my $puzzle = $self->container->container; | ||
| 140 | my $tmpl = $puzzle->tmpl; | ||
| 141 | # recupero i token nell'header | ||
| 142 | my $htokens = $tmpl->header_tokens; | ||
| 143 | # recupero dall'header del body i tags meta | ||
| 144 | my $metatags = $htokens->{meta}; | ||
| 145 | # cerco tra i vari metatag quelli interessanti | ||
| 146 | foreach (@{$metatags}) { | ||
| 147 | if ($_->[0] =~m| | ||
| 148 | [Nn][Aa][Mm][Ee]\s*=\s* | ||
| 149 | "(.*?)" # solo i tag mcs_* | ||
| 150 | \s+[Cc][Oo][Nn][Tt][Ee][Nn][Tt]\s*=\s* | ||
| 151 | "(.*?)" # ne estraggo il valore | ||
| 152 | |x) { | ||
| 153 | if (exists $puzzle->cfg->{$1} && $1 eq 'gids') { | ||
| 154 | # array | ||
| 155 | # if (ref($puzzle->cfg->{$1}) eq 'ARRAY') { | ||
| 156 | # # gia' un array | ||
| 157 | # push @{$puzzle->cfg->{$1}},$2; | ||
| 158 | # } else { | ||
| 159 | # # trasformo in array | ||
| 160 | # my $scalar_value = $puzzle->cfg->{$1}; | ||
| 161 | # $puzzle->cfg->{$1} = [$scalar_value,$2]; | ||
| 162 | # } | ||
| 163 | $puzzle->cfg->gids([$2]); | ||
| 164 | } else { | ||
| 165 | $puzzle->cfg->{$1} = $2; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | foreach (keys %{$puzzle->cfg}) { | ||
| 170 | if (eval {$self->comp->attr($_)} ne '') { | ||
| 171 | # vedo se esiste un attributo nella | ||
| 172 | # pagina che si chiama come il metacod, se esiste | ||
| 173 | # lo imposto e questa cosa prevale sull'eventuale | ||
| 174 | # impostazione del metacod nell'HTML della pagina | ||
| 175 | $puzzle->cfg->{$_} = $self->comp->attr($_); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | sub get_html { | ||
| 181 | my $self = shift; | ||
| 182 | my $puzzle = $self->container->container; | ||
| 183 | my $tmpl = $puzzle->tmpl; | ||
| 184 | my $m = $puzzle->_mason; | ||
| 185 | my $filePath = $self->comp->path; | ||
| 186 | my $fileName = $self->comp->name; | ||
| 187 | return $m->scomp($self->comp,%{$m->request_args}) if ($fileName eq 'autohandler'); | ||
| 188 | my $ret = ''; | ||
| 189 | if ($fileName =~ /.+\.m{0,1}pl$/i) { | ||
| 190 | # decido che al componente passo solo ARGS | ||
| 191 | # le altre cose le deve recuperare in altro modo | ||
| 192 | $ret = $m->scomp($self->comp,%{$m->request_args}); | ||
| 193 | if( $m->apache_req->content_type ne '' && | ||
| 194 | lc($m->apache_req->content_type) ne 'text/html') | ||
| 195 | { | ||
| 196 | # non e' una pagina html...ritorno subito; | ||
| 197 | return $ret; | ||
| 198 | } | ||
| 199 | } elsif ($fileName =~ /.+\.mplcom$/i) { | ||
| 200 | return $m->call_next; | ||
| 201 | } elsif ($ret eq '') { | ||
| 202 | # alla pagina statica passo tutti i parametri %as | ||
| 203 | # piu' tutti i parametri di sessione | ||
| 204 | # piu' tutti gli userdata | ||
| 205 | $ret = $tmpl->mhtml(undef,$filePath); | ||
| 206 | } | ||
| 207 | return $ret; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub reset { | ||
| 211 | my $s = shift; | ||
| 212 | $s->{direct_output} = 0; | ||
| 213 | $s->{isa_error} = 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; |