From 52c0c493633d3294df422f2f48489fda6d287cbc Mon Sep 17 00:00:00 2001 From: Bluesaxman Date: Sun, 21 Apr 2019 08:41:19 -0600 Subject: [PATCH] initial git commit of pcms --- index.pl | 253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100755 index.pl diff --git a/index.pl b/index.pl new file mode 100755 index 0000000..0c63b46 --- /dev/null +++ b/index.pl @@ -0,0 +1,253 @@ +#!/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 $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 { # needs to support attributes + 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","There was an oopsy")); + 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); + # Check these files to see if they exsist, and only add if they do. + $header .= html_tag( + 'link', + undef, + {attribute=>'rel', value=>'icon'}, + {attribute=>'type', value=>'image/gif'}, + {attribute=>'href', value=>$img_dir.'favicon.gif'}); + $header .= html_tag( + 'link', + undef, + {attribute=>'rel', value=>"stylesheet"}, + {attribute=>'type',value=>"text/css"}, + {attribute=>'href',value=>$css_dir.'css'}); + $header .= html_tag( + 'script', + " ", + {attribute=>'type', value=>"text/javascript"}, + {attribute=>'src',value=>$js_dir.'bluecore.js'}); + $header .= html_tag( + 'script', + " ", + {attribute=>'type', value=>"text/javascript"}, + {attribute=>'src', value=>$js_dir.'base'}); + $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 .= '
      '; + my @list = sort {$a cmp $b} @_; + foreach (@list) { + $menu_content .= menu_item($_) if(-e $pages_dir.$_); + } + $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 sides 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]));