Просмотр исходного кода

WIP on implementing Config class, theming on the backend

* Removes bad/unneeded test, will prolly move to phpunit anyways
* Some images probably broken in file browser, user attribution, etc.

Next up, adding a configuration UI for this mess
Andy Baugh 7 лет назад
Родитель
Сommit
82c718faed
40 измененных файлов с 128 добавлено и 130 удалено
  1. 1 2
      bin/install
  2. 24 24
      index.php
  3. 0 41
      lib/configure.php
  4. 8 10
      lib/tCMS/Auth.inc
  5. 49 0
      lib/tCMS/Config.inc
  6. 30 21
      sys/admin/index.php
  7. 0 8
      sys/admin/logout.inc
  8. 0 11
      t/admin-configure.t
  9. 0 0
      templates/default/admin/bengine.inc
  10. 2 2
      templates/default/admin/login.inc
  11. 6 0
      templates/default/admin/logout.inc
  12. 0 0
      templates/default/admin/mbengine.inc
  13. 1 1
      templates/default/admin/settings.inc
  14. 7 10
      templates/default/header.tmpl
  15. 0 0
      themed/default/css/avatars.css
  16. 0 0
      themed/default/css/print.css
  17. 0 0
      themed/default/css/screen.css
  18. 0 0
      themed/default/css/structure.css
  19. 0 0
      themed/default/img/avatar/humm.gif
  20. 0 0
      themed/default/img/icon/favicon.ico
  21. 0 0
      themed/default/img/icon/rss.png
  22. 0 0
      themed/default/img/icon/tCMS.svg
  23. 0 0
      themed/default/img/mime/denied.gif
  24. 0 0
      themed/default/img/mime/missing.gif
  25. 0 0
      themed/default/img/mime/tsarchive.gif
  26. 0 0
      themed/default/img/mime/tsaudio.gif
  27. 0 0
      themed/default/img/mime/tscode.gif
  28. 0 0
      themed/default/img/mime/tsdoc.png
  29. 0 0
      themed/default/img/mime/tsdownload.gif
  30. 0 0
      themed/default/img/mime/tsfile.gif
  31. 0 0
      themed/default/img/mime/tsfolder-up.gif
  32. 0 0
      themed/default/img/mime/tsfolder.gif
  33. 0 0
      themed/default/img/mime/tsimage.gif
  34. 0 0
      themed/default/img/mime/tsmodel.gif
  35. 0 0
      themed/default/img/mime/tsmovie.gif
  36. 0 0
      themed/default/img/mime/tsprop.png
  37. 0 0
      themed/default/img/mime/tsschematic.gif
  38. 0 0
      themed/default/img/mime/tssoftware.gif
  39. 0 0
      themed/default/img/mime/tssticky.gif
  40. 0 0
      themed/default/img/sys/testpattern.jpg

+ 1 - 2
bin/install

@@ -61,9 +61,8 @@ if( !file_exists("$private_base/conf/users.json") ) {
 }
 
 echo "[INFO] Installing public data to $public_base now...\n";
-$files = [ 'css', 'fileshare', 'img', 'index.php', 'sys' ];
+$files = [ 'themed', 'fileshare', 'index.php', 'sys' ];
 install( $public_base, $wd, $files );
-ensure_directories_exist( [ "$public_base/css/custom", "$public_base/img/icon/custom" ] );
 
 echo "[INFO] Done.\n";
 exit(0);

+ 24 - 24
index.php

@@ -1,37 +1,37 @@
 <?php
-# Get protocol bits, etc.
-$protocol = ( !empty($_SERVER["HTTPS"] ) ) ? 'https'       : 'http';
-$nav      = ( !empty($_GET['nav'] ) )      ? $_GET['nav']  : '';
-$post     = ( !empty($_GET['post'] ) )     ? $_GET['post'] : '';
-# DOCUMENT_ROOT can be misconfigured, __FILE__, __DIR__, etc. can't be
-$docroot  = realpath( __DIR__ );
+    // Setup includes to work right. Much of this is duped in Config.inc, but gotta get this info to include it, so..
+    $user_info = posix_getpwuid(posix_geteuid());
+    $dir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
+    $basedir = ( file_exists( $dir . "/.tCMS_basedir") ? file_get_contents("$dir/.tCMS_basedir") : "$dir/.tCMS" );
+    set_include_path(get_include_path() . PATH_SEPARATOR . "$basedir/lib");
+    require_once "tCMS/Config.inc";
 
-# Grab the main configuration file. We need to see where that lives first though.
-if( file_exists( "$docroot/basedir" ) ) {
-    $fh = fopen( "$docroot/basedir" );
-    $basedir = trim(fgets( $fh )); # I only want the first line
-    fclose($fh);
-}
-if( empty( $basedir ) ) {
-    $basedir = posix_getpwuid(posix_geteuid())['dir'] . "/.tCMS";
-}
+    // Get the config, set the theme (also set the basedir so we don't have to fetch it again).
+    $conf_obj->set_base_dir($basedir);
+    $conf_obj = new Config;
+    $config = $conf_obj->get();
+    $theme = ( !array_key_exists( 'theme', $config ) || empty($config['theme']) ? 'default' : $config['theme'] );
+    $themedir = "$basedir/templates/$theme";
 
-if(!file_exists("$basedir/conf/main.json")) {
-	# XXX Need to have manual be hosted in repo under sys/admin/manual
-	include( "$basedir/templates/default/notconfigured.tmpl" );
-	die();
-}
-$config = json_decode(file_get_contents("$basedir/conf/main.json"),true);
-// Not sure if I'll ever really even need to localize (see html tag attrs).
+    if(empty($config)) {
+        # XXX Need to have manual be hosted in repo under sys/admin/manual
+        include( "$themedir/notconfigured.tmpl" );
+        die();
+    }
+
+    $nav      = ( !empty($_GET['nav'] ) )      ? $_GET['nav']  : '';
+    $post     = ( !empty($_GET['post'] ) )     ? $_GET['post'] : '';
+
+    // Not sure if I'll ever really even need to localize (see html tag attrs).
 ?>
 <!doctype html>
 <html dir="ltr" lang="en-US">
- <?php include("$basedir/templates/" . $config['theme'] . "/header.tmpl"); ?>
+ <?php include "$themedir/header.tmpl"; ?>
  <body>
   <div id="topkek">
    <?php
     //Site's Titlebar comes in here
-    include("$basedir/templates/" . $config['theme'] . "/nav.inc");
+    include "$themedir/nav.inc";
    ?>
   </div>
   <div id="littlemenu">

+ 0 - 41
lib/configure.php

@@ -1,41 +0,0 @@
-<?php
-class configure {
-    # INPUT:
-    #   Param 1: Config file to read. Throws on failure to read.
-    #   Param 2: undef or ARRAY of keys you wanna fetch. undef/invalid assumes we'll be wanting all of them.
-    # OUTPUT: ARRAY of the requested KEY => VALUE pairs.
-    public static function get_config_values( $file = null, $desired_configs = null ) {
-        if( !file_exists( $file ) ) throw new Exception( "$file doesn't exist." );
-        $config = file_get_contents( $file );
-        if( empty( $config ) )      throw new Exception( "$file couldn't be opened or is empty" );
-        $config = json_decode( $config, true );
-        if( empty( $config ) )      throw new Exception( "$file is not valid JSON" );
-        if( !is_array( $config) )   throw new Exception( "Decoded $file's JSON string is not an array" );
-        return $config;
-    }
-    # INPUT: ARRAY, preferably the one returned by get_config_values.
-    # OUTPUT: ARRAY [ result => BOOL, reason => STRING ].
-    public static function validate_config( $config = array() ) {
-        #OK, so here we have to start making some 'assumptions' RE mutli-user environments (cPanel, etc.)
-        $caller_info = posix_getpwuid();
-        $basedir = $caller_info['dir'] . "/.tCMS";
-        if( !file_exists( $basedir) || !is_dir( $basedir ) ) throw new Exception( "~/.tCMS doesn't exist." );
-        $model_file = "$basedir/model.json";
-        $model = self::get_config_values( $model_file );
-        foreach ( $model as $key => $val ) {
-            if( !array_key_exists( $key ) ) {
-                return [ 'result' => 0, 'reason' => "Config file was missing required key '$key'" ];
-            }
-            # TODO check various directories here, make sure we can read/write as appropriate
-        }
-        return 1;
-    }
-    # INPUT:
-    #   Param 1: Config file to write. Throws if it fails to write.
-    #   Param 1: ARRAY of KEY => VALUE pairs you wanna set. Throws on invalid input.
-    # OUTPUT: BOOL regarding success/failure.
-    public static function set_config_values( $file = null, $desired_configs = null ) {
-        return;
-    }
-}
-?>

+ 8 - 10
lib/auth.inc → lib/tCMS/Auth.inc

@@ -1,5 +1,7 @@
 <?php
-    class auth {
+    require_once "tCMS/Config.inc";
+
+    class Auth {
 
         public function __construct() {
             ini_set( "session.cookie_httponly", true );
@@ -30,7 +32,7 @@
             return session_id();
         }
 
-        public function invalidate_auth( $redirect=true, $to="" ) { 
+        public function invalidate_auth( $redirect=true, $to="" ) {
             // need to invalidate the session here, though we may not have loaded it yet, so do that first.
             $session_status = session_status();
             if( $session_status !== PHP_SESSION_ACTIVE || $session_status !== 2 ) session_start();
@@ -48,7 +50,7 @@
                 http_response_code(401);
                 header('Content-Type: application/json');
                 echo json_encode( [ 'code' => '401', 'message' => 'Unauthorized' ] );
-            }   
+            }
             die();
         }   
 
@@ -61,13 +63,9 @@
                 ini_set('session.entropy_length', '32');
             }
             // Check it
-            $user_info = posix_getpwuid(posix_geteuid());
-            $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
-            $confdir = ( file_exists( "$homedir/.tCMS_basedir") ? file_get_contents("$homedir/.tCMS_basedir") . "/conf" : "$homedir/.tCMS/conf" );
-            $conf = file_get_contents("$confdir/users.json");
-            if( $conf === false ) return [ 'err' => 1, "msg" => "Login Failed: Configuration missing" ];
-            $conf = json_decode( $conf, 1 );
-            if( $conf === null ) return [ 'err' => 1, "msg" => "Login Failed: Configuration malformed" ];
+            $conf_obj = new Config;
+            $conf = $conf_obj->get('users');
+            if( empty($conf) return [ 'err' => 1, "msg" => "Login Failed: Configuration missing or malformed" ];
             if( empty($conf[$user]) || empty($conf[$user]['auth_hash']) || !password_verify( $pass, $conf[$user]['auth_hash'] ) ) return [ 'err' => 1, 'msg' => "Login Failed" ];
 
             // Gotta have a touch of eval

+ 49 - 0
lib/tCMS/Config.inc

@@ -0,0 +1,49 @@
+<?php
+    class Config {
+
+        private $confdir = null;
+        private $basedir = null;
+        private $homedir = null;
+        private array $conf = [
+            'users' => [],
+            'main'  => [],
+        ];
+
+        // Get the configuration.
+        public function get( $type='main' ) {
+            if( !empty($this->conf[$type]) ) return $this->conf[$type];
+            $confdir = $this->get_conf_dir();
+            $tmp_conf = @json_decode( @file_get_contents("$confdir/$type.json"), true );
+            if( !empty( $tmp_conf ) ) $this->conf[$type] = $tmp_conf;
+            return $this->conf[$type];
+        }
+
+        public function get_base_dir() {
+            if( !empty($this->basedir) ) return $this->basedir;
+            $homedir = $this->get_home_dir();
+            $tmp_basedir = ( file_exists( "$homedir/.tCMS_basedir") ? file_get_contents("$homedir/.tCMS_basedir") : "$homedir/.tCMS" );
+            $this->basedir = $tmp_basedir;
+            return $this->basedir;
+        }
+
+        public function set_base_dir($dir) {
+            $this->basedir = $dir;
+            return;
+        }
+
+        public function get_conf_dir() {
+            if( !empty($this->confdir) ) return $this->confdir;
+            $basedir = $this->get_base_dir();
+            $this->confdir = "$basedir/conf";
+            return $this->confdir;
+        }
+
+        public function get_home_dir() {
+            if( !empty($this->homedir) ) return $this->homedir;
+            $user_info = posix_getpwuid(posix_geteuid());
+            $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
+            $this->homedir = $tmp_confdir;
+            return $this->homedir;
+        }
+    }
+?>

+ 30 - 21
sys/admin/index.php

@@ -1,30 +1,42 @@
 <?php
-    $args = ( $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST : $_GET );
+
+    // Setup includes to work right. Much of this is duped in Config.inc, but gotta get this info to include it, so..
     $user_info = posix_getpwuid(posix_geteuid());
-    // Probably the 'sanest' default I could think of when you have no homedir
     $dir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
     $basedir = ( file_exists( $dir . "/.tCMS_basedir") ? file_get_contents("$dir/.tCMS_basedir") : "$dir/.tCMS" );
+    set_include_path(get_include_path() . PATH_SEPARATOR . "$basedir/lib");
+    require_once "tCMS/Config.inc";
+
+    // Get the config, set the theme (also set the basedir so we don't have to fetch it again).
+    $conf_obj->set_base_dir($basedir);
+    $conf_obj = new Config;
+    $config = $conf_obj->get();
+    $theme = ( !array_key_exists( 'theme', $config ) || empty($config['theme']) ? 'default' : $config['theme'] );
+    $themedir = "$basedir/templates/$theme";
+
+    // Begin dispatch
+    $args = ( $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST : $_GET );
     if( !empty($args['app']) && $args['app'] == 'login' ) {
-        include("login.inc");
+        include "$themedir/admin/login.inc";
         die();
     } elseif( !empty($args['app']) && $args['app'] == 'logout' ) {
-        include("logout.inc");
+        include "$themedir/admin/logout.inc";
         die();
     } else {
-        include_once("$basedir/lib/auth.inc");
-        $auth = new auth;
+        require_once "tCMS/Auth.inc";
+        $auth = new Auth;
         $auth->ensure_auth();
     }
     if( empty($args['app']) || $args['app'] == 'config' ) {
-        $kontent = "settings.inc";
+        $kontent = "$themedir/admin/settings.inc";
     } elseif ($args['app'] == 'blog') {
-        $kontent = "bengine.inc";
+        $kontent = "$themedir/admin/bengine.inc";
     } elseif ($args['app'] == 'microblog') {
-        $kontent = "mbengine.inc";
+        $kontent = "$themedir/admin/mbengine.inc";
     } elseif ($args['app'] == 'users' ) {
-        $kontent = "users.inc";
+        $kontent = "$themedir/admin/users.inc";
     } else {
-        $kontent = "settings.inc";
+        $kontent = "$themedir/admin/settings.inc";
     }
 ?>
 <!doctype html>
@@ -33,20 +45,17 @@
   <meta charset="utf-8" />
   <meta name="description" content="tCMS Control Panel"/>
   <meta name="viewport" content="width=device-width">
-  <link rel="stylesheet" type="text/css" href="../../css/structure.css" />
-  <link rel="stylesheet" type="text/css" href="../../css/screen.css" media="screen" />
-  <link rel="stylesheet" type="text/css" href="../../css/print.css" media="print" />
   <?php
-    if(file_exists('../../css/custom/avatars.css')) {
-      echo '<link rel="stylesheet" type="text/css" href="../../css/custom/avatars.css" />';
-    } else {
-      echo '<link rel="stylesheet" type="text/css" href="../../css/avatars.css" />';
-    }
+    $links  = '<link rel="stylesheet" type="text/css" href="../../themed/' . $theme . '/css/structure.css" />';
+    $links += '<link rel="stylesheet" type="text/css" href="../../themed/' . $theme . '/css/screen.css" media="screen" />';
+    $links += '<link rel="stylesheet" type="text/css" href="../../themed/' . $theme . '/css/print.css" media="print" />';
+    $links += '<link rel="icon" type="image/vnd.microsoft.icon" href="../../themed/' . $theme . '/img/icon/favicon.ico" />';
+    echo $links;
+
+    // TODO inject avatars these via style tags based on config
   ?>
-  <link rel="icon" type="image/vnd.microsoft.icon" href="../../img/icon/favicon.ico" />
   <title>tCMS Admin</title>
   <?php
-   $config = @json_decode(@file_get_contents("$basedir/config/main.json"),true);
   ?>
  </head>
  <body>

+ 0 - 8
sys/admin/logout.inc

@@ -1,8 +0,0 @@
-<?php
-    $user_info = posix_getpwuid(posix_geteuid());
-    $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www' );
-    $libdir = ( file_exists( "$homedir/.tCMS_basedir") ? file_get_contents("$homedir/.tCMS_basedir") . "/lib" : "$homedir/.tCMS/lib" );
-    include_once("$libdir/auth.inc");
-    auth::invalidate_auth();
-    die();
-?>

+ 0 - 11
t/admin-configure.t

@@ -1,11 +0,0 @@
-<?php
-$basedir = realpath(dirname(__FILE__));
-require_once("$basedir/lib/testmore/testmore.php");
-
-plan(2);
-
-require_ok("$basedir/../sys/admin/lib/configure.php");
-$config = configure::get_config_values("$basedir/../sys/admin/config/main.json.example");
-ok( $config, "configure::get_config_values for known file fetches OK");
-#print_r( $config );
-?>

+ 0 - 0
sys/admin/bengine.inc → templates/default/admin/bengine.inc


+ 2 - 2
sys/admin/login.inc → templates/default/admin/login.inc

@@ -18,8 +18,8 @@
         }
         $loginMsg = "Unknown Error";
         if( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
-            require_once("$basedir/lib/auth.inc"); //Authenticate
-            $auth = new auth;
+            require_once "tCMS/Auth.inc"; //Authenticate
+            $auth = new Auth;
             $login = $auth->do_auth( $args['username'], $args['password'] );
             if(!is_array($login) || !empty($login['err']) || empty( $login['success'] ) ) {
                 $loginFailure = 1;

+ 6 - 0
templates/default/admin/logout.inc

@@ -0,0 +1,6 @@
+<?php
+    require_once "tCMS/auth.inc";
+    $auth = new Auth;
+    $auth->invalidate_auth();
+    die();
+?>

+ 0 - 0
sys/admin/mbengine.inc → templates/default/admin/mbengine.inc


+ 1 - 1
sys/admin/settings.inc → templates/default/admin/settings.inc

@@ -20,5 +20,5 @@ Themes: Coming soon.
  </select>
 </form>-->
 <p>
-  Want to write your own theme (or modify one since I haven't finished the theme selector here)? Please see the <a href="teodesian.net/tCMS/index.php?nav=5&post=fileshare/manual/Chapter 03-Customization.post" title="GET UR MIND RITE">styling guide</a>.
+  Want to write your own theme (or modify one since I haven't finished the theme selector here)? Please see the <a href="https://tcms.troglodyne.net/index.php?nav=5&post=fileshare/manual/Chapter 03-Customization.post" title="GET UR MIND RITE">styling guide</a>.
 </p>

+ 7 - 10
templates/default/header.tmpl

@@ -2,17 +2,14 @@
   <meta charset="utf-8" />
   <meta name="description" content="A Simple CMS by teodesian.net"/>
   <meta name="viewport" content="width=device-width">
-  <link rel="stylesheet" type="text/css" href="css/structure.css" />
-  <link rel="stylesheet" type="text/css" href="css/screen.css" media="screen" />
-  <link rel="stylesheet" type="text/css" href="css/print.css" media="print" />
   <?php
-    foreach ( [ 'avatars', 'screen', 'print', 'favicon.ico' ] as $file ) {
-        $rel  = ( $file == 'favicon.ico' )    ? "icon"                     : "stylesheet";
-        $type = ( $file == 'favicon.ico' )    ? "image/vnd.microsoft.icon" : "text/css"; 
-        $dir  = ( $file == 'favicon.ico' )    ? "img/icon"                 : "css";
-        $href = ( file_exists( "$dir/$file" ) ? "$dir/custom/$file"        : "$dir/$file";
-        echo "<link rel=\"$rel\" type=\"$type\" href=\"$href\" />";
-    }
+    $links  = '<link rel="stylesheet" type="text/css" href="themed/' . $theme . '/css/structure.css" />';
+    $links += '<link rel="stylesheet" type="text/css" href="themed/' . $theme . '/css/screen.css" media="screen" />';
+    $links += '<link rel="stylesheet" type="text/css" href="themed/' . $theme . '/css/print.css" media="print" />';
+    $links += '<link rel="icon" type="image/vnd.microsoft.icon" href="themed/' . $theme . '/img/icon/favicon.ico" />';
+    echo $links;
+
+    // TODO inject avatars these via style tags based on config
   ?>
   <title>
    <?php echo $config['htmltitle']; ?>

+ 0 - 0
css/avatars.css → themed/default/css/avatars.css


+ 0 - 0
css/print.css → themed/default/css/print.css


+ 0 - 0
css/screen.css → themed/default/css/screen.css


+ 0 - 0
css/structure.css → themed/default/css/structure.css


+ 0 - 0
img/avatar/humm.gif → themed/default/img/avatar/humm.gif


+ 0 - 0
img/icon/favicon.ico → themed/default/img/icon/favicon.ico


+ 0 - 0
img/icon/rss.png → themed/default/img/icon/rss.png


+ 0 - 0
img/icon/tCMS.svg → themed/default/img/icon/tCMS.svg


+ 0 - 0
img/mime/denied.gif → themed/default/img/mime/denied.gif


+ 0 - 0
img/mime/missing.gif → themed/default/img/mime/missing.gif


+ 0 - 0
img/mime/tsarchive.gif → themed/default/img/mime/tsarchive.gif


+ 0 - 0
img/mime/tsaudio.gif → themed/default/img/mime/tsaudio.gif


+ 0 - 0
img/mime/tscode.gif → themed/default/img/mime/tscode.gif


+ 0 - 0
img/mime/tsdoc.png → themed/default/img/mime/tsdoc.png


+ 0 - 0
img/mime/tsdownload.gif → themed/default/img/mime/tsdownload.gif


+ 0 - 0
img/mime/tsfile.gif → themed/default/img/mime/tsfile.gif


+ 0 - 0
img/mime/tsfolder-up.gif → themed/default/img/mime/tsfolder-up.gif


+ 0 - 0
img/mime/tsfolder.gif → themed/default/img/mime/tsfolder.gif


+ 0 - 0
img/mime/tsimage.gif → themed/default/img/mime/tsimage.gif


+ 0 - 0
img/mime/tsmodel.gif → themed/default/img/mime/tsmodel.gif


+ 0 - 0
img/mime/tsmovie.gif → themed/default/img/mime/tsmovie.gif


+ 0 - 0
img/mime/tsprop.png → themed/default/img/mime/tsprop.png


+ 0 - 0
img/mime/tsschematic.gif → themed/default/img/mime/tsschematic.gif


+ 0 - 0
img/mime/tssoftware.gif → themed/default/img/mime/tssoftware.gif


+ 0 - 0
img/mime/tssticky.gif → themed/default/img/mime/tssticky.gif


+ 0 - 0
img/sys/testpattern.jpg → themed/default/img/sys/testpattern.jpg