Setting up my convoluted web parsing structure... in short:
authorpaulr <me@pjr.cc>
Sat, 23 Jul 2011 20:46:57 +0000 (06:46 +1000)
committerpaulr <me@pjr.cc>
Sat, 23 Jul 2011 20:46:57 +0000 (06:46 +1000)
call index.php, which loads config and webrequestor

Web requester loads urlparser which then determines the class handling
the page

web requester then calls class->go($url)

class can then call GLCASPageBuilder(class, function, title) which
then builds a static page and then calls class->function(url) to build
the body.

FUN!

libglcas/config.php [new file with mode: 0644]
libglcas/lib.php [new file with mode: 0644]
libglcas/urlparser.php [new file with mode: 0644]
libglcas/web.php [new file with mode: 0644]
plugins/admin.php [new file with mode: 0644]
plugins/glcas_plugin.php [new file with mode: 0644]
plugins/repo.php [new file with mode: 0644]
plugins/webbase.php [new file with mode: 0644]
www/.htaccess [new file with mode: 0644]
www/index.php [new file with mode: 0644]

diff --git a/libglcas/config.php b/libglcas/config.php
new file mode 100644 (file)
index 0000000..634b69a
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+class GLCASConfig {
+       
+       function __construct()
+       {
+               $this->configPath = "";
+               $this->config = "";
+       }
+       
+       function loadConfig($configpath)
+       {
+               error_log("loadConfig $configpath");
+               $this->configPath = $configpath;
+               $flt = file_get_contents($configpath);
+               $this->config = unserialize(base64_decode($flt));
+               
+               
+       }
+       
+       function setConfigVar($var, $value)
+       {
+               if(is_array($this->config)) {
+                       foreach($this->config as $vkey => $val) {
+                               if($vkey == "$var") {
+                                       error_log("reset config of $var to $value");
+                                       $this->config[$var] = $value;
+                                       return true;
+                               }
+                       }
+               } else error_log("config isnt array");
+               
+               // otherwise, set it
+               error_log("set config of $var to $value");
+               $this->config[$var] = $value;
+       }
+       
+       function delConfigVar($var)
+       {
+               if(is_array($this->config)) {
+                       foreach($this->config as $vkey => $val) {
+                               if($vkey == "$var") {
+                                       error_log("remove config of $var to $value");
+                                       unset($this->config[$var]);
+                                       return true;
+                               }
+                       }
+               } else error_log("config isnt array");
+       }
+       
+       function saveConfig()
+       {
+               file_put_contents($this->configPath, base64_encode(serialize($this->config)));
+       }
+       
+       function debug()
+       {
+               echo "<pre>";
+               echo "path: ".$this->configPath."\n";
+               print_r($this->config);
+               echo "</pre>";
+       }
+       
+       private $configPath;
+       private $config;
+       
+}
+
+?>
\ No newline at end of file
diff --git a/libglcas/lib.php b/libglcas/lib.php
new file mode 100644 (file)
index 0000000..05811a6
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/*
+ * 
+ * The Main lib.php file.
+ * 
+ */
+
+//require_once();
+
+// get the lib root, and set include path from it
+$LIB_ROOT_FS = realpath(dirname(__FILE__));
+global $LIB_ROOT_FS;
+
+/*
+ * I shouldnt ever need to do this bit, but its here, just in case
+$adpath = realpath($LIB_ROOT_FS."/../");
+error_log("added libglcas path as $adpath");
+set_include_path(get_include_path().PATH_SEPARATOR.$adpath);
+
+*/
+require_once("libglcas/urlparser.php");
+require_once("libglcas/web.php");
+require_once("libglcas/config.php");
+
+function glcas_pluginLoader($path="")
+{
+       global $LIB_ROOT_FS;
+       if($path ==  "") {
+               // try and find it
+               $plpath = realpath($LIB_ROOT_FS."/../plugins/");
+               if(file_exists($plpath."/glcas_plugin.php")) {
+                       // here it is, load away
+                       error_log("attempting to load plugins from $plpath");
+                       $dh = opendir("$plpath");
+                       if($dh) {
+                               while(($file = readdir($dh))!==false) {
+                                       $mt = preg_match("/.*.php$/", $file);
+                                       if($mt > 0) {
+                                               error_log("loading plugin $file");
+                                               require_once("$plpath/$file");
+                                               //echo "required $basedir/$file\n";
+                                       }
+                               }
+                       }                       
+               }
+       }
+}
+
+?>
\ No newline at end of file
diff --git a/libglcas/urlparser.php b/libglcas/urlparser.php
new file mode 100644 (file)
index 0000000..89c9bb1
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+// the purpose of this plugin is to take a url and figure out the owning class that should be called
+error_log("glcasurlparser loaded");
+
+class GLCASUrlParser {
+       function __construct($urlconfig, $config)
+       {
+               $v = print_r($urlconfig, true);
+               error_log("constructor for urlparser, $v");
+               $this->urlClasses = $urlconfig;
+               $this->config = $config;
+       }
+       
+       function getClass($url)
+       {
+               error_log("getclass for $url");
+               //echo "<pre>URL CLasses";
+               //print_r($this->urlClasses);
+               //echo "</pre>";
+               $default = "";
+               $base = "";
+               if(is_array($this->urlClasses)) {
+                       foreach($this->urlClasses as $key => $val) {
+                               error_log("checking url $url against $key, $val");
+                               if($url == "/") {
+                                       $base = $val;
+                                       error_log("base set to $val");
+                                       return new $val($this->config);
+                               } 
+                               
+                               // TODO: this is quite messy really, need to think about how i do /'s in urls for url parsers
+                               if($key=="*") {
+                                       $default = $val;
+                                       error_log("catchall set to $val");
+                               } else if($key != "/") {
+                                       // now the rest
+                                       if(preg_match("/$key/", $url)) {
+                                               error_log("matched $url to $key and $val");
+                                               return new $val($this->config);
+                                       }
+                               }
+                       }
+               }
+               error_log("get class returns default");
+               return new $default($this->config);
+       }
+       
+       private $urlClasses;
+       private $config;
+}
+
+?>
\ No newline at end of file
diff --git a/libglcas/web.php b/libglcas/web.php
new file mode 100644 (file)
index 0000000..6c3723b
--- /dev/null
@@ -0,0 +1,68 @@
+<?php 
+
+// if i believed in name spacing in php, i'd use it.
+error_log("glcasweb loaded");
+
+class GLCASWeb {
+       
+       function __construct($config)
+       {
+               $this->config = $config;
+       }
+       
+       function go($urlhandlers)
+       {
+               $url = "/";
+               if(isset($_REQUEST["q"])) {
+                       $url = $_REQUEST["q"];
+               }
+               
+               // create a url parser
+               $urlparser = new GLCASUrlParser($urlhandlers, $this->config);
+               
+               // 
+               $call_class = $urlparser->getClass($url);
+               $call_class->go($url);
+       }
+       
+       private $config;
+       
+}
+
+function GLCASpageBuilder($bodyClass, $bodyFunction, $title="GLCAS")
+{
+       global $WEB_ROOT_FS;
+       
+       // TODO: load css
+       // TODO: load js
+       // header
+       echo "<html><title>$title</title><body>";
+       
+       // page top
+       echo "<h1>GLCAS</h1><br>";
+       echo "<table><tr><td>messages go here<td></tr><tr><td>";
+       
+       // menu, then body
+       echo "<table><tr><td>Menu goes here</td></tr><tr><td>";
+       // body
+       $url = "/";
+       if(isset($_REQUEST["q"])) {
+               $url = $_REQUEST["q"];
+       }
+       
+       if($bodyClass != null) {
+               $bodyClass->$bodyFunction($url);
+       } else $bodyFunction($url);
+       echo "</td></tr></table>";
+       
+       
+       // close the big wrap-around table
+       echo "</td></tr></table>";
+       
+       // footer
+       echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
+       
+}
+
+
+?>
\ No newline at end of file
diff --git a/plugins/admin.php b/plugins/admin.php
new file mode 100644 (file)
index 0000000..4e24665
--- /dev/null
@@ -0,0 +1,24 @@
+<?php 
+
+error_log("admin loaded");
+
+global $URL_HANDLERS;
+$URL_HANDLERS["admin.*"] = "GLCASAdmin";
+
+class GLCASAdmin {
+       function __construct($config)
+       {
+               $this->config = $config;
+               error_log("constructor for GLCASAdmin");
+               
+       }
+       
+       function go($url)
+       {
+               echo "admin page";
+       }
+       
+       private $config;
+}
+
+?>
\ No newline at end of file
diff --git a/plugins/glcas_plugin.php b/plugins/glcas_plugin.php
new file mode 100644 (file)
index 0000000..c0d36b4
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+// this is a plugin just to show the plugin path.
+error_log("glcase_plugin loaded");
+
+global $URL_HANDLERS;
+
+$URL_HANDLERS["repo"] = "repo";
+
+?>
\ No newline at end of file
diff --git a/plugins/repo.php b/plugins/repo.php
new file mode 100644 (file)
index 0000000..804a5ce
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+$URL_HANDLERS["*"] = "GLCASRepo";
+
+class GLCASRepo {
+       function __construct($config)
+       {
+               $this->config = $config;
+       }
+       
+       function go($url)
+       {
+               echo "i am the repo man, i repo the repo";
+       }
+       
+       private $config;
+}
+
+?>
\ No newline at end of file
diff --git a/plugins/webbase.php b/plugins/webbase.php
new file mode 100644 (file)
index 0000000..13dbcc3
--- /dev/null
@@ -0,0 +1,29 @@
+<?php 
+
+global $URL_HANDLERS;
+$URL_HANDLERS["/"] = "GLCASWebBase";
+
+
+class GLCASWebBase {
+       function __construct($config)
+       {
+               $this->config = $config;
+               error_log("construct GLCASWebBase");
+       }
+       
+       function go($url)
+       {
+               error_log("go GLCASWebBase");
+               GLCASpageBuilder($this, "body");
+       }
+       
+       function body($url)
+       {
+               echo "i am disturbing";
+       }
+       
+       private $config;
+}
+
+
+?>
\ No newline at end of file
diff --git a/www/.htaccess b/www/.htaccess
new file mode 100644 (file)
index 0000000..e1f7b2e
--- /dev/null
@@ -0,0 +1,8 @@
+RewriteEngine on
+RewriteBase /src/eclipse-workspace/glcas/www/ 
+RewriteRule ^index\.php.* - [L]
+RewriteRule ^soap\.php.* - [L]
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
+
diff --git a/www/index.php b/www/index.php
new file mode 100644 (file)
index 0000000..4657e52
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+$WEB_ROOT_FS = realpath(dirname(__FILE__));
+
+global $WEB_ROOT_FS, $URL_HANDLERS;
+
+// add libglcas as if it were a path in ../libglcas
+if(file_exists("../libglcas")) {
+       $path = realpath($WEB_ROOT_FS."/../");
+       error_log("added glcas path as $path");
+       set_include_path(get_include_path().PATH_SEPARATOR.$path);
+}
+
+// include the based library
+require_once("libglcas/lib.php");
+
+// load plugins
+glcas_pluginLoader();
+
+// find our config
+$configpath = "";
+
+// TODO: do this better
+if(file_exists($WEB_ROOT_FS."/../var")) {
+       
+       // is it there?
+       if(file_exists($WEB_ROOT_FS."/../var/glcas/webconfig")) {
+               $configpath = realpath("$WEB_ROOT_FS/../var/glcas/webconfig");
+       } else {        
+               // if not, attempt to create
+               if(!file_exists($WEB_ROOT_FS."/../var/glcas")) {
+                       mkdir($WEB_ROOT_FS."/../var/glcas");
+               }
+               // success!
+               touch("$WEB_ROOT_FS/../var/glcas/webconfig");
+               $configpath = realpath("$WEB_ROOT_FS/../var/glcas/webconfig");
+       }
+}
+
+
+$glconfig = new GLCASConfig();
+$glconfig->loadConfig($configpath);
+$webResponder = new GLCASWeb($glconfig);
+$webResponder->go($URL_HANDLERS);
+/*
+
+echo "<pre>";
+$vars = get_defined_vars();
+foreach($vars as $var => $vkey) {
+       echo "VAR $var is:\n";
+       var_dump($$var);
+       echo "\n\n";
+}
+
+echo "</pre>";
+/**/
+
+?>
\ No newline at end of file