#!/usr/bin/perl -w use strict; use warnings; # Where are my things? my $img_dir = "/img/"; #must be in docroot my $pages_dir = "./pages/"; my $scripts_dir = "./scripts/"; my $css_dir = "/css/"; #must be in docroot my $js_dir = "/js/"; #must be in docroot my $data_dir = "./dynamic/"; # What are my configurations? my $site_base = "/"; my $site_name = "Murkfall Mountain"; my $site_symbol = "⛰"; my $site500 = "There was an oopsy"; my $site404 = "There is nothing here..."; # Make some tools sub clean_input { my $input = shift; # I think I need to address what this line is actually doing. if ($input =~ m!%2F!) { print "Location: /hax\r\n\r\n"; exit; } $input =~ s!%(..)!chr hex $1!ge; $input =~ s!\+! !g; return $input; } sub get_info { my $clientIP = ($ENV{"REMOTE_ADDR"} or "0.0.0.0"); my $proto = ($ENV{"REQUEST_SCHEME"} or "http"); my $query = (split(/\?/,($ENV{"QUERY_STRING"} or "")))[0]; my $referrer = ($ENV{"HTTP_REFERER"} or ""); my $PageURL = ($ENV{"REQUEST_URI"} or "home"); #We might need to look at more of these. return ($proto,$clientIP,$query,$referrer,$PageURL); } our @request = get_info(); sub http_status { my ($status,$content,$target) = @_; my $header = ""; $header .= "status: ".$status."\r\n"; $header .= "Location: ".$target."\r\n" if $target; $header .= "Content-Type: ".$content."\r\n" if $content; $header .= "\r\n"; return $header; } sub html_tag { my $tag = shift; my $content = shift; my @attrib_array = @_; my $string = "\n<".$tag; my $end = " />"; if (@attrib_array) { foreach(@attrib_array) { $string .= ' '.$_->{attribute}.'="'.$_->{value}.'"'; } } if ($content) { $string =~ s/^\s//g; $string .= ">".$content; $end = ""; } return $string.$end; } sub soft_die { print http_status(500,"text/html; charset=utf-8"); print "\n"; print html_tag("html",html_tag("body",$site500)); exit; } sub page404 { print http_status(404,"text/html; charset=utf-8"); print "\n"; print html_tag("html", html_tag("head",header()). html_tag("body", render_logo($site_symbol). page_template($site404))); exit; } sub normalize_string { return join(" ",(map { ucfirst($_) } split("_",shift))); } # lets build a website sub header { my $header = ""; $header .= html_tag( "meta", undef, {attribute=>'http-equiv', value=>'Content-Type'}, {attribute=>'content', value=>'text/html; charset=utf-8'}); # Make title dynamic $header .= html_tag('title',$site_name); if (-e $img_dir.'favicon.gif') { $header .= html_tag( 'link', undef, {attribute=>'rel', value=>'icon'}, {attribute=>'type', value=>'image/gif'}, {attribute=>'href', value=>$img_dir.'favicon.gif'}); } if (-e $css_dir.'css.css') { $header .= html_tag( 'link', undef, {attribute=>'rel', value=>"stylesheet"}, {attribute=>'type',value=>"text/css"}, {attribute=>'href',value=>$css_dir.'css.css'}); } if (-e $js_dir.'bluecore.js') { $header .= html_tag( 'script', " ", {attribute=>'type', value=>"text/javascript"}, {attribute=>'src',value=>$js_dir.'bluecore.js'}); } if (-e $js_dir.'base.js') { $header .= html_tag( 'script', " ", {attribute=>'type', value=>"text/javascript"}, {attribute=>'src', value=>$js_dir.'base.js'}); } $header .= html_tag( 'meta', undef, {attribute=>'name', value=>"viewport"}, {attribute=>'content', value=>"user-scalable=no"}); return $header; } sub render_logo { my $symbol = shift; return html_tag( 'div', html_tag( 'span', $symbol, {attribute=>'id',value=>"SiteSymbol"}). html_tag( 'span', $site_name, {attribute=>'id',value=>'SiteName'}), {attribute=>'id',value=>'logo'}); } sub get_page { my $pageURL = shift; $pageURL =~ s/^$site_base//g; $pageURL =~ s/%20/ /g; my @URLvars = split(/\//,$pageURL); my $page_name = $URLvars[0]; unless ($page_name) { $page_name = "home";} if (-e $scripts_dir.$page_name.".pl") { return qx($scripts_dir$page_name.pl @URLvars); } else { my $sub_page_name = $page_name; if ($page_name =~ m/_dir$/) { $sub_page_name .= "/".$URLvars[1]; $page_name =~ s/_dir$//g; } open(PAGE, "<", $pages_dir.$sub_page_name) or return page404($pageURL." we looked here:".$pages_dir.$sub_page_name); my @content = ; close(PAGE); return @content; } } ############## Menu needs refactoring ################### sub getmenu { open(MENUFILE, "<", $data_dir."menucont") or return "There is no menu yet"; while(my $item = ) { push(@_,$item); } close(MENUFILE); chomp @_; @_; } sub menu_item { my $name = $_[0]; if ($_[0] =~ m/\//){$name = (split("/",$_[0]))[1]} $name =~ s/_/ /g; my $item_content = '
  • '; $item_content .= ucfirst $name; $item_content .= '
  • '; return $item_content; } sub render_menu { my $id = shift(@_); my $menu_content = ''; $menu_content .= ''; $menu_content .= "\n"; return $menu_content; } sub submenu { my $page = (split("/",shift(@_)))[1]; $page =~ s/_dir$//g; my @list = []; opendir(DIR,$pages_dir.$page."_dir/") or return ""; while (my $file = readdir(DIR)) { next if ($file =~ m/^\./); push(@list,$page."_dir/".$file); } closedir(DIR); shift(@list); return render_menu("sub",@list); } ######################### /menu ########################### sub page_template { my $content = join("\n",@_); my $page_content = html_tag( 'div', $content # For the main sites command prompt # .html_tag('span'," ", {attribute=>'id', value=>"prompt"}) # .html_tag('span',"█", {attribute=>'class', value=>"cursor"}) ,{attribute=>'id', value=>'screen'}); $page_content .= render_menu("menu",getmenu()); #will need to me changed after menu refactor # $page_content .= submenu(clean_input($request[4])); #comment this out if the site doesn't have submenus. $page_content .= html_tag('div',"Murkfall.net",{attribute=>'id',value=>"footer"}); return $page_content; } sub build_page { my @content = get_page(shift); #should 404 if page doesn't exsist print http_status(200,"text/html; charset=utf-8"); print "\n"; print html_tag("html", html_tag("head",header()). html_tag("body", render_logo($site_symbol). page_template(@content), #for the main site # {attribute=>'onkeydown',value=>"HandleStroke(event)"} )); } build_page(clean_input($request[4]));