From 8db695166ed64dd2a3a3db7518a75186adb728ee Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Wed, 17 Mar 2010 02:29:12 +0000 Subject: [PATCH] Initial commit of HandBrakeCluster PHP framework Replaced dodgy placeholder with a set of PHP classes for displaying a HandBrake webui. Current code uses Smarty as a templating engine, and includes an ugly but functional set of pages. The HandBrake classes are at present only placeholders, and offer no real functionality. Working class autoloader for the HandBrakeCluster_ hierarchy. --- .htaccess.dist | 10 ++ HandBrakeCluster/Config.class.php | 13 +++ HandBrakeCluster/Database.class.php | 13 +++ HandBrakeCluster/Job.class.php | 17 ++++ HandBrakeCluster/Log.class.php | 15 +++ HandBrakeCluster/Main.class.php | 88 +++++++++++++++++ HandBrakeCluster/Page.class.php | 41 ++++++++ HandBrakeCluster/RequestParser.class.php | 44 +++++++++ current-jobs.php | 86 ----------------- index.php | 14 +++ pages/home.php | 11 +++ pages/job-details.php | 0 pages/jobs.php | 0 pages/logs.php | 0 styles/normal.css | 116 +++++++++++++++++++++++ templates/home.tpl | 24 +++++ templates/index.tpl | 50 ++++++++++ templates/job-details.tpl | 1 + templates/jobs.tpl | 1 + templates/logs.tpl | 1 + templates/navigation.tpl | 5 + templates/sidebar.tpl | 5 + tmp/cache/.gitignore | 0 tmp/templates/.gitignore | 0 24 files changed, 469 insertions(+), 86 deletions(-) create mode 100644 .htaccess.dist create mode 100644 HandBrakeCluster/Config.class.php create mode 100644 HandBrakeCluster/Database.class.php create mode 100644 HandBrakeCluster/Job.class.php create mode 100644 HandBrakeCluster/Log.class.php create mode 100644 HandBrakeCluster/Main.class.php create mode 100644 HandBrakeCluster/Page.class.php create mode 100644 HandBrakeCluster/RequestParser.class.php delete mode 100644 current-jobs.php create mode 100644 index.php create mode 100644 pages/home.php create mode 100644 pages/job-details.php create mode 100644 pages/jobs.php create mode 100644 pages/logs.php create mode 100644 styles/normal.css create mode 100644 templates/home.tpl create mode 100644 templates/index.tpl create mode 100644 templates/job-details.tpl create mode 100644 templates/jobs.tpl create mode 100644 templates/logs.tpl create mode 100644 templates/navigation.tpl create mode 100644 templates/sidebar.tpl create mode 100644 tmp/cache/.gitignore create mode 100644 tmp/templates/.gitignore diff --git a/.htaccess.dist b/.htaccess.dist new file mode 100644 index 0000000..e6e06cc --- /dev/null +++ b/.htaccess.dist @@ -0,0 +1,10 @@ + + + RewriteEngine on + RewriteBase / + + RewriteCond %{REQUEST_FILENAME} !-f + RewriteCond %{REQUEST_FILENAME} !-d + RewriteRule ^(.*)$ index.php?l=$1 + + diff --git a/HandBrakeCluster/Config.class.php b/HandBrakeCluster/Config.class.php new file mode 100644 index 0000000..b4455f9 --- /dev/null +++ b/HandBrakeCluster/Config.class.php @@ -0,0 +1,13 @@ +filename = $filename; + } + +}; + +?> diff --git a/HandBrakeCluster/Database.class.php b/HandBrakeCluster/Database.class.php new file mode 100644 index 0000000..23945de --- /dev/null +++ b/HandBrakeCluster/Database.class.php @@ -0,0 +1,13 @@ +config = $config; + } + +} + +?> diff --git a/HandBrakeCluster/Job.class.php b/HandBrakeCluster/Job.class.php new file mode 100644 index 0000000..f430527 --- /dev/null +++ b/HandBrakeCluster/Job.class.php @@ -0,0 +1,17 @@ +id = 42; + } + + public function id() { + return $this->id; + } + +}; + +?> diff --git a/HandBrakeCluster/Log.class.php b/HandBrakeCluster/Log.class.php new file mode 100644 index 0000000..54c8b24 --- /dev/null +++ b/HandBrakeCluster/Log.class.php @@ -0,0 +1,15 @@ +database = $database; + $this->config = $config; + } + +} + +?> diff --git a/HandBrakeCluster/Main.class.php b/HandBrakeCluster/Main.class.php new file mode 100644 index 0000000..e6fbd89 --- /dev/null +++ b/HandBrakeCluster/Main.class.php @@ -0,0 +1,88 @@ +smarty = new Smarty(); + + $this->smarty->template_dir = './templates'; + $this->smarty->compile_dir = './tmp/templates'; + $this->smarty->cache_dir = './tmp/cache'; + $this->smarty->config_fir = './config'; + + $this->smarty->assign('version', '0.1'); + $this->smarty->assign('base_uri', '/handbrake/'); + + $request_string = isset($_GET['l']) ? $_GET['l'] : ''; + $this->request = new HandBrakeCluster_RequestParser($request_string); + } + + public static function instance() { + if (!self::$instance) { + self::$instance = new HandBrakeCluster_Main(); + } + + return self::$instance; + } + + public function smarty() { + return $this->smarty; + } + + public function config() { + return $this->config; + } + + public function database() { + return $this->database; + } + + public function log() { + return $this->log; + } + + public function request() { + return $this->request; + } + + public static function initialise() { + spl_autoload_register(array('HandBrakeCluster_Main','autoload')); + } + + public static function autoload($classname) { + // Ensure the classname contains only valid class name characters + if (!preg_match('/^[A-Z][a-zA-Z0-9_]*$/', $classname)) { + throw new Exception('Illegal characters in classname'); // TODO Subclass this exception + } + + // Ensure the class to load begins with our prefix + if (!preg_match('/^HandBrakeCluster_/', $classname)) { + return; + } + + // Replace any underscores with directory separators + $filename = preg_replace('/_/', '/', $classname); + + // Tack on the class file suffix + $filename .= '.class.php'; + + // If this file exists, load it + if (file_exists($filename)) { + require_once $filename; + } + } +} + +HandBrakeCluster_Main::initialise(); + +?> diff --git a/HandBrakeCluster/Page.class.php b/HandBrakeCluster/Page.class.php new file mode 100644 index 0000000..6e40ee9 --- /dev/null +++ b/HandBrakeCluster/Page.class.php @@ -0,0 +1,41 @@ +smarty = $smarty; + $this->request = $request; + $this->page = $request->page(); + } + + public function page() { + return $this->page; + } + + public function template_filename() { + $template_filename = $this->page() . '.tpl'; + + if (!$this->smarty->template_exists($template_filename)) { + $template_filename = 'home.tpl'; + } + + return $template_filename; + } + + public function evaluate() { + $code_filename = 'pages/' . $this->page() . '.php'; + if (!file_exists($code_filename)) { + throw Exception('Template code file does not exist!'); + } + eval("include '$code_filename';"); + + } + +}; + +?> diff --git a/HandBrakeCluster/RequestParser.class.php b/HandBrakeCluster/RequestParser.class.php new file mode 100644 index 0000000..f831660 --- /dev/null +++ b/HandBrakeCluster/RequestParser.class.php @@ -0,0 +1,44 @@ +request_string = $request_string; + + $this->parse(); + } + + public function parse() { + $components = explode('/', $this->request_string); + + if (!$components) { + return; + } + + // The first token is the page to execute + $this->page = array_shift($components); + + // The subsequent tokens are parameters for this page in key value pairs + while ($components) { + $this->vars[array_shift($components)] = $components ? array_shift($components) : null; + } + } + + public function page() { + return $this->page; + } + + public function get($key) { + if (isset($this->vars[$key])) { + return $this->vars[$key]; + } + return null; + } + +}; + +?> diff --git a/current-jobs.php b/current-jobs.php deleted file mode 100644 index 980d8e1..0000000 --- a/current-jobs.php +++ /dev/null @@ -1,86 +0,0 @@ - - - - Current HandBrake Ripping Jobs - - - - -

Current HandBrake Ripping Jobs

- - -
- -
- - -
- -
- - - - - diff --git a/index.php b/index.php new file mode 100644 index 0000000..fffab37 --- /dev/null +++ b/index.php @@ -0,0 +1,14 @@ +smarty(); + +$page = new HandBrakeCluster_Page($smarty, $main->request()); +$page->evaluate(); + +$smarty->assign('page_content', $smarty->fetch($page->template_filename())); + +$smarty->display('index.tpl'); + +?> diff --git a/pages/home.php b/pages/home.php new file mode 100644 index 0000000..eb6d7ad --- /dev/null +++ b/pages/home.php @@ -0,0 +1,11 @@ +smarty->assign('running_jobs', $running_jobs); + $this->smarty->assign('completed_jobs;', $completed_jobs); + +?> diff --git a/pages/job-details.php b/pages/job-details.php new file mode 100644 index 0000000..e69de29 diff --git a/pages/jobs.php b/pages/jobs.php new file mode 100644 index 0000000..e69de29 diff --git a/pages/logs.php b/pages/logs.php new file mode 100644 index 0000000..e69de29 diff --git a/styles/normal.css b/styles/normal.css new file mode 100644 index 0000000..983079c --- /dev/null +++ b/styles/normal.css @@ -0,0 +1,116 @@ +body { + margin: 0em; + padding: 0em; + background: #a7a09a; + font-family: verdana, helvetica, sans-serif; +} + +a { + color: gray; +} + +label { + margin-left: 1em; + margin-right: 1em; +} + +table { +} + +tr { +} + +tr:hover { + background-color: lightBlue; +} + +#container { + width: 75%; + min-width: 75em; + margin: auto; +} + +#banner { + margin: 0em; + padding: 0em; + background: #eeeeee; +} + +#banner h1 { + padding: 0.5em; +} + +#navigation { + background: #eeeeee; + margin-top: 0em; + margin-bottom: 1em; + padding: 0.2em; +} + +#navigation ul { + margin: 0em; + padding: 0em 5em;; + list-style: none; +} + +#navigation li { + margin: 1em; + padding: 0em; + display: inline; +} + +#page-container { + margin: 0em; + padding: 0em; +} + +#sidebar { + float: left; + width: 20em; + margin-bottom: 1em; + + background: #eeeeee; +} + +#page { + margin-left: 21em; + margin-bottom: 1em; + padding: 0.5em; + + background: #eeeeee; +} + +#footer { + clear: both; + padding: 2em; + font-size: smaller; + font-style: italic; + + background: #eeeeee; + color: #333333; + +} + +#errors { + background: peachpuff; + color: darkred; + margin: 1em; +} + +#messages { + background: lightcyan; + color: darkblue; + margin: 1em; +} + +.default { + background: beige; + color: darkgray; + font-style: italic; +} + +.icon { + height: 16px; + width: 16px; +} + diff --git a/templates/home.tpl b/templates/home.tpl new file mode 100644 index 0000000..244dfd3 --- /dev/null +++ b/templates/home.tpl @@ -0,0 +1,24 @@ +

Summary

+ +

Running Jobs

+ +{if $running_jobs} + {foreach from=$running_jobs item=job} +
  • Job {$job->id()}
  • + {/foreach} +{else} + There are no currently running jobs. +{/if} + +

    Completed Jobs

    + +{if $completed_jobs} + +{else} + There are no recently completed jobs. +{/if} + diff --git a/templates/index.tpl b/templates/index.tpl new file mode 100644 index 0000000..88fb197 --- /dev/null +++ b/templates/index.tpl @@ -0,0 +1,50 @@ + + + + HandBrake Cluster WebUI + + + + + +
    + + + + + +
    + + + +
    + + {if $messages} +
    + {foreach from=$messages item=message} + {$message} + {/foreach} +
    + {/if} + + {$page_content} + +
    + +
    + + + +
    + + + diff --git a/templates/job-details.tpl b/templates/job-details.tpl new file mode 100644 index 0000000..304c0ed --- /dev/null +++ b/templates/job-details.tpl @@ -0,0 +1 @@ +Job details... diff --git a/templates/jobs.tpl b/templates/jobs.tpl new file mode 100644 index 0000000..6789b01 --- /dev/null +++ b/templates/jobs.tpl @@ -0,0 +1 @@ +Jobs diff --git a/templates/logs.tpl b/templates/logs.tpl new file mode 100644 index 0000000..c74a514 --- /dev/null +++ b/templates/logs.tpl @@ -0,0 +1 @@ +Logs... diff --git a/templates/navigation.tpl b/templates/navigation.tpl new file mode 100644 index 0000000..ebf75dd --- /dev/null +++ b/templates/navigation.tpl @@ -0,0 +1,5 @@ + diff --git a/templates/sidebar.tpl b/templates/sidebar.tpl new file mode 100644 index 0000000..ebf75dd --- /dev/null +++ b/templates/sidebar.tpl @@ -0,0 +1,5 @@ + diff --git a/tmp/cache/.gitignore b/tmp/cache/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/tmp/templates/.gitignore b/tmp/templates/.gitignore new file mode 100644 index 0000000..e69de29