| 1 | # $Id: NewSpirit.pm,v 1.24 2006/05/16 15:00:00 joern Exp $ | ||
| 2 | |||
| 3 | package CIPP::Runtime::NewSpirit; | ||
| 4 | |||
| 5 | $VERSION = "1.0.2"; | ||
| 6 | |||
| 7 | package CIPP::Runtime::NewSpirit::Project; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | my %PROJECT_INSTANCES; | ||
| 13 | |||
| 14 | sub get_project { shift->{project} } | ||
| 15 | |||
| 16 | sub get_prod_dir { shift->{config}->{prod_dir} } | ||
| 17 | sub get_cgi_dir { shift->{config}->{prod_dir}."/cgi-bin"} | ||
| 18 | sub get_doc_dir { shift->{config}->{prod_dir}."/htdocs" } | ||
| 19 | sub get_l10n_dir { shift->{config}->{prod_dir}."/l10n" } | ||
| 20 | sub get_config_dir { shift->{config}->{config_dir} } | ||
| 21 | sub get_inc_dir { shift->{config}->{inc_dir} } | ||
| 22 | sub get_lib_dir { shift->{config}->{lib_dir} } | ||
| 23 | sub get_log_dir { shift->{config}->{log_dir} } | ||
| 24 | sub get_log_file { shift->{config}->{log_file} } | ||
| 25 | sub get_cgi_url { shift->{config}->{cgi_url} } | ||
| 26 | sub get_doc_url { shift->{config}->{doc_url} } | ||
| 27 | sub get_url_par_delimiter { shift->{config}->{url_par_delimiter} } | ||
| 28 | sub get_http_header { shift->{config}->{http_header} } | ||
| 29 | sub get_add_lib_dirs { shift->{config}->{add_lib_dirs} ||[] } | ||
| 30 | sub get_add_prod_dirs { shift->{config}->{add_prod_dirs} ||[] } | ||
| 31 | sub get_error_show { shift->{config}->{error_show} } | ||
| 32 | sub get_error_text { shift->{config}->{error_text} } | ||
| 33 | sub get_utf8 { shift->{config}->{utf8} } | ||
| 34 | sub get_xhtml { shift->{config}->{xhtml} } | ||
| 35 | sub get_cipp_compiler_version { shift->{config}->{cipp_compiler_version}} | ||
| 36 | sub get_cipp_runtime_version { "new.spirit $CIPP::Runtime::NewSpirit::VERSION" } | ||
| 37 | sub get_cipp2_runtime { shift->{config}->{cipp2_runtime} } | ||
| 38 | |||
| 39 | sub get_config { shift->{config} } | ||
| 40 | sub get_request_cnt { shift->{request_cnt} } | ||
| 41 | sub get_init_error_message { shift->{init_error_message} } | ||
| 42 | |||
| 43 | sub set_config { shift->{config} = $_[1] } | ||
| 44 | sub set_request_cnt { shift->{request_cnt} = $_[1] } | ||
| 45 | sub set_init_error_message { shift->{init_error_message} = $_[1] } | ||
| 46 | |||
| 47 | sub init { | ||
| 48 | my $type = shift; | ||
| 49 | my %par = @_; | ||
| 50 | my ($back_prod_path, $project) = | ||
| 51 | @par{'back_prod_path','project'}; | ||
| 52 | |||
| 53 | # only one instance per process and project | ||
| 54 | return $PROJECT_INSTANCES{$project} | ||
| 55 | if defined $PROJECT_INSTANCES{$project}; | ||
| 56 | |||
| 57 | # determine relative project prod root path | ||
| 58 | my $script_name = ( $0 =~ /\.cgi$/ ? $0 : undef ) || | ||
| 59 | $ENV{SCRIPT_FILENAME} || | ||
| 60 | './foo.cgi'; | ||
| 61 | |||
| 62 | my $prod_dir = $back_prod_path; | ||
| 63 | |||
| 64 | if ( $script_name =~ m!^(.*)[/\\][^/\\]+$! ) { | ||
| 65 | $prod_dir = "$1/$back_prod_path"; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $self = bless { | ||
| 69 | project => $project, | ||
| 70 | request_cnt => 0, | ||
| 71 | config => {}, | ||
| 72 | }, $type; | ||
| 73 | |||
| 74 | # read base config to get absolute path of project root | ||
| 75 | $self->read_base_config ( | ||
| 76 | filename => "$prod_dir/config/cipp.conf" | ||
| 77 | ) or return; | ||
| 78 | |||
| 79 | # setup @INC array | ||
| 80 | $self->setup_inc_path; | ||
| 81 | |||
| 82 | # register project instance | ||
| 83 | $PROJECT_INSTANCES{$project} = $self; | ||
| 84 | |||
| 85 | # load Encode module if utf8 is set and we have Perl >= 5.8.0 | ||
| 86 | if ( $self->get_utf8 and $] >= 5.008 ) { | ||
| 87 | require Encode; | ||
| 88 | binmode STDOUT, ":utf8"; | ||
| 89 | } else { | ||
| 90 | binmode STDOUT; | ||
| 91 | } | ||
| 92 | |||
| 93 | # load CIPP2 Runtime, if requested | ||
| 94 | require CIPP::Runtime if $self->get_cipp2_runtime; | ||
| 95 | |||
| 96 | # init l10n/gettext framework | ||
| 97 | $self->init_l10n; | ||
| 98 | |||
| 99 | # return handle | ||
| 100 | return $self; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub setup_inc_path { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | unshift @INC, $self->get_lib_dir; | ||
| 107 | unshift @INC, @{$self->get_add_lib_dirs}; | ||
| 108 | unshift @INC, map { "$_/lib" } @{$self->get_add_prod_dirs}; | ||
| 109 | |||
| 110 | 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub handle { | ||
| 114 | my $class = shift; | ||
| 115 | my %par = @_; | ||
| 116 | my ($project) = @par{'project'}; | ||
| 117 | return $PROJECT_INSTANCES{$project} | ||
| 118 | } | ||
| 119 | |||
| 120 | sub read_base_config { | ||
| 121 | my $self = shift; | ||
| 122 | my %par = @_; | ||
| 123 | my ($filename) = @par{'filename'}; | ||
| 124 | |||
| 125 | $filename ||= $self->get_config_dir."/cipp.conf"; | ||
| 126 | |||
| 127 | my $config = do $filename; | ||
| 128 | |||
| 129 | if ( not ref $config ) { | ||
| 130 | require Cwd; | ||
| 131 | if ( not -f $filename ) { | ||
| 132 | $self->set_init_error_message ("CIPP base config '$filename' not found. CWD=".Cwd::cwd()); | ||
| 133 | } elsif ( not -r $filename ) { | ||
| 134 | $self->set_init_error_message ("CIPP base config '$filename' not readable."); | ||
| 135 | } else { | ||
| 136 | $self->set_init_error_message ("CIPP base config '$filename' has wrong format."); | ||
| 137 | } | ||
| 138 | |||
| 139 | $self->init_error; | ||
| 140 | |||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | $self->set_config ($config); | ||
| 145 | |||
| 146 | 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub new_request { | ||
| 150 | my $self = shift; | ||
| 151 | my %par = @_; | ||
| 152 | my ($program_name, $mime_type) = | ||
| 153 | @par{'program_name','mime_type'}; | ||
| 154 | |||
| 155 | $self->set_request_cnt ( $self->get_request_cnt + 1 ); | ||
| 156 | |||
| 157 | my $request = CIPP::Runtime::NewSpirit::Request->new ( | ||
| 158 | project_handle => $self, | ||
| 159 | program_name => $program_name, | ||
| 160 | mime_type => $mime_type, | ||
| 161 | ); | ||
| 162 | |||
| 163 | $request->init; | ||
| 164 | |||
| 165 | $CIPP::request = $request; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub init_error { | ||
| 169 | my $self = shift; | ||
| 170 | |||
| 171 | my $message = $self->get_init_error_message; | ||
| 172 | my $project = $self->get_project; | ||
| 173 | |||
| 174 | print "Content-type: text/html\n\n"; | ||
| 175 | print "<h1>Project initialization error</h1>\n"; | ||
| 176 | print "<p><b>Project:</b><blockquote>$project</blockquote>\n"; | ||
| 177 | print "<p><b>Message:</b><blockquote>$message</blockquote>\n"; | ||
| 178 | |||
| 179 | 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub init_l10n { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | my $l10n_dir = $self->get_l10n_dir; | ||
| 186 | my $domains_file = "$l10n_dir/domains.conf"; | ||
| 187 | return unless -f $domains_file; | ||
| 188 | |||
| 189 | my $conf = do $domains_file; | ||
| 190 | |||
| 191 | require Encode; | ||
| 192 | require Locale::Messages; | ||
| 193 | require POSIX; | ||
| 194 | |||
| 195 | foreach my $module ( values %{$conf} ) { | ||
| 196 | my $domain = $module->{domain}; | ||
| 197 | Locale::Messages::bindtextdomain($domain, $l10n_dir); | ||
| 198 | Locale::Messages::bind_textdomain_codeset($domain, "utf-8"); | ||
| 199 | Locale::Messages::bind_textdomain_filter($domain, sub { | ||
| 200 | Encode::_utf8_on($_[0]);$_[0] | ||
| 201 | }); | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | package CIPP::Runtime::NewSpirit::Request; | ||
| 208 | |||
| 209 | use vars qw ( @ISA ); | ||
| 210 | use strict; | ||
| 211 | use Carp; | ||
| 212 | |||
| 213 | use CIPP::Runtime::Request; | ||
| 214 | @ISA = qw ( CIPP::Runtime::Request ); | ||
| 215 | |||
| 216 | sub get_mime_type { shift->{mime_type} } | ||
| 217 | sub set_mime_type { shift->{mime_type} = $_[1] } | ||
| 218 | |||
| 219 | sub new { | ||
| 220 | my $type = shift; | ||
| 221 | my %par = @_; | ||
| 222 | my ($mime_type) = @par{'mime_type'}; | ||
| 223 | |||
| 224 | my $self = bless $type->SUPER::new(@_), $type; | ||
| 225 | |||
| 226 | $self->set_mime_type ($mime_type); | ||
| 227 | |||
| 228 | if ( $self->get_project_handle->get_utf8 and $] >= 5.008 ) { | ||
| 229 | binmode STDOUT, ":utf8"; | ||
| 230 | } else { | ||
| 231 | binmode STDOUT; | ||
| 232 | } | ||
| 233 | |||
| 234 | return $self; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub init { | ||
| 238 | my $self = shift; | ||
| 239 | |||
| 240 | # determine name of the script | ||
| 241 | # ($0 is messed up with SpeedyCGI, so fallback to | ||
| 242 | # SCRIPT_FILENAME if no .cgi in there) | ||
| 243 | my $script_name = ( $0 =~ /\.cgi$/ ? $0 : undef ) || | ||
| 244 | $ENV{SCRIPT_FILENAME}; | ||
| 245 | $self->set_script_name($script_name); | ||
| 246 | |||
| 247 | # change to program dir | ||
| 248 | $script_name =~ m!^(.*)[/\\][^/\\]+$!; | ||
| 249 | chdir $1 if $1; | ||
| 250 | |||
| 251 | # set $CIPP::ee (End of Element, XHTML conformity) | ||
| 252 | $CIPP::ee = $self->get_xhtml ? " /" : ""; | ||
| 253 | |||
| 254 | 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub print_http_header { | ||
| 258 | my $self = shift; | ||
| 259 | |||
| 260 | my $mime_type = $self->get_mime_type; | ||
| 261 | |||
| 262 | if ( $mime_type =~ m!text/html!i && | ||
| 263 | $mime_type !~ /charset=/i && | ||
| 264 | $self->get_utf8 ) { | ||
| 265 | $mime_type = "text/html; charset=utf-8"; | ||
| 266 | } | ||
| 267 | |||
| 268 | if ( $mime_type ne 'cipp/dynamic' ) { | ||
| 269 | $self->get_http_header->{'content-type'} = $mime_type; | ||
| 270 | $self->SUPER::print_http_header(@_); | ||
| 271 | } | ||
| 272 | |||
| 273 | 1; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub get_db_config { | ||
| 277 | my $self = shift; | ||
| 278 | my %par = @_; | ||
| 279 | my ($db) = @par{'db'}; | ||
| 280 | |||
| 281 | my $filename = $self->get_project_handle->get_config_dir."/$db.db-conf"; | ||
| 282 | |||
| 283 | if ( not -e $filename ) { | ||
| 284 | foreach my $config_dir ( map { $_."/config" } | ||
| 285 | @{$self->get_project_handle | ||
| 286 | ->get_add_prod_dirs} ) { | ||
| 287 | $filename = "$config_dir/$db.db-conf"; | ||
| 288 | last if -e $filename; | ||
| 289 | } | ||
| 290 | |||
| 291 | # set full_path to this project's config dir, if not | ||
| 292 | # found. This produces an error message which belongs | ||
| 293 | # to this project. | ||
| 294 | $filename = $self->get_config_dir."/$db.db-conf" if not -e $filename; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $config = do $filename; | ||
| 298 | |||
| 299 | if ( not ref $config ) { | ||
| 300 | croak "Database config file '$filename' not found or wrong format."; | ||
| 301 | } | ||
| 302 | |||
| 303 | return $config; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub resolve_filename { | ||
| 307 | my $self = shift; | ||
| 308 | my %par = @_; | ||
| 309 | |||
| 310 | my ($name, $throw, $type) = @par{'name','throw','type'}; | ||
| 311 | |||
| 312 | $throw ||= "resolve_filename"; | ||
| 313 | |||
| 314 | my $orig_name = $name; | ||
| 315 | $name =~ s!^[^\.]+\.!!; | ||
| 316 | |||
| 317 | my $filename; | ||
| 318 | |||
| 319 | if ( $type eq 'cipp-config' ) { | ||
| 320 | #-- $CIPP::Runtime::NewSpirit::CONFIG_DIR is set | ||
| 321 | #-- in PerlCheck.pm resp. cipp_perlcheck.pl, if | ||
| 322 | #-- this is a Project Install. It's set to the | ||
| 323 | #-- config directory of the local project installation. | ||
| 324 | my $config_dir = | ||
| 325 | $CIPP::Runtime::NewSpirit::CONFIG_DIR || | ||
| 326 | $self->get_config_dir; | ||
| 327 | |||
| 328 | $filename = $config_dir."/".$name.".config"; | ||
| 329 | |||
| 330 | if ( not -e $filename ) { | ||
| 331 | foreach my $config_dir ( map { $_."/config" } | ||
| 332 | @{$self->get_project_handle | ||
| 333 | ->get_add_prod_dirs} ) { | ||
| 334 | $filename = "$config_dir/$name.config"; | ||
| 335 | last if -e $filename; | ||
| 336 | } | ||
| 337 | |||
| 338 | # set full_path to this project's config dir, if not | ||
| 339 | # found. This produces an error message which belongs | ||
| 340 | # to this project. | ||
| 341 | $filename = $self->get_config_dir."/".$name.".config" | ||
| 342 | if not -e $filename; | ||
| 343 | } | ||
| 344 | |||
| 345 | } else { | ||
| 346 | $self->error ( | ||
| 347 | message => "Unknown object type '$type'" | ||
| 348 | ); | ||
| 349 | } | ||
| 350 | |||
| 351 | die "$throw\tFile '$filename' for object '$orig_name', type '$type' not found" | ||
| 352 | if not -f $filename; | ||
| 353 | |||
| 354 | return $filename; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub get_include_name { | ||
| 358 | my $self = shift; | ||
| 359 | my %par = @_; | ||
| 360 | my ($filename) = @par{'filename'}; | ||
| 361 | |||
| 362 | $filename =~ s/\.[^.]+$//; | ||
| 363 | $filename =~ s!/!.!g; | ||
| 364 | $filename = $self->get_project_handle->get_project.".$filename"; | ||
| 365 | |||
| 366 | return $filename; | ||
| 367 | } | ||
| 368 | |||
| 369 | sub get_object_url { | ||
| 370 | my $self = shift; | ||
| 371 | my %par = @_; | ||
| 372 | my ($name, $throw) = @par{'name','throw'}; | ||
| 373 | |||
| 374 | $throw ||= "geturl"; | ||
| 375 | |||
| 376 | my $object = $name; | ||
| 377 | my $project = $self->get_project_handle->get_project; | ||
| 378 | my $cgi_dir = $self->get_project_handle->get_cgi_dir; | ||
| 379 | $object =~ s/\./\//g; | ||
| 380 | $object =~ s![^\/]*!$project!; | ||
| 381 | |||
| 382 | # check if this is a CGI | ||
| 383 | if ( -f "$cgi_dir/$object.cgi" ) { | ||
| 384 | return $self->get_project_handle->get_cgi_url."/$object.cgi"; | ||
| 385 | } | ||
| 386 | |||
| 387 | # Ok, must be a static document | ||
| 388 | my $doc_dir = $self->get_project_handle->get_doc_dir; | ||
| 389 | my @filenames = glob "$doc_dir/$object.*"; | ||
| 390 | |||
| 391 | # is this ambiguous or no files found? | ||
| 392 | if ( scalar @filenames == 0 ) { | ||
| 393 | die "$throw\tUnable to resolve object '$name'"; | ||
| 394 | |||
| 395 | } elsif ( scalar @filenames > 1 ) { | ||
| 396 | die "$throw\tObject identifier '$name' is ambiguous"; | ||
| 397 | } | ||
| 398 | |||
| 399 | # ok, we found exactly one file | ||
| 400 | my $file = $filenames[0]; | ||
| 401 | $file =~ s!^$doc_dir/!!; | ||
| 402 | |||
| 403 | return $self->get_project_handle->get_doc_url."/$file"; | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | 1; |