diff --git a/public/index.php b/public/index.php index 1acce64..a864db4 100644 --- a/public/index.php +++ b/public/index.php @@ -3,8 +3,7 @@ define('APP_ROOT', dirname(dirname(__FILE__))); -define('CLASSES_DIR', APP_ROOT . '/src/classes'); -define('LIB_DIR', APP_ROOT . '/src/lib'); +define('SRC_DIR', APP_ROOT . '/src'); define('STORAGE_DIR', APP_ROOT . '/storage'); define('TEMPLATES_DIR', APP_ROOT . '/templates'); @@ -12,15 +11,25 @@ define('TICKS_DIR', STORAGE_DIR . '/ticks'); define('DATA_DIR', STORAGE_DIR . '/db'); define('DB_FILE', DATA_DIR . '/tkr.sqlite'); -$include_dirs = [ - LIB_DIR, - CLASSES_DIR, -]; +// Defining this in the index instead of lib/util.php +// to avoid chicken-and-egg issues with including it +function recursive_glob(string $pattern, string $directory): array { + $files = []; + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($directory) + ); -foreach ($include_dirs as $include_dir){ - foreach (glob($include_dir . '/*.php') as $file) { - require_once $file; + foreach ($iterator as $file) { + if ($file->isFile() && fnmatch($pattern, $file->getFilename())) { + $files[] = $file->getPathname(); + } } + + return $files; +} + +foreach (recursive_glob('*.php', SRC_DIR) as $file) { + require_once $file; } confirm_setup(); diff --git a/src/classes/Config.php b/src/Config/Config.php similarity index 100% rename from src/classes/Config.php rename to src/Config/Config.php diff --git a/src/classes/User.php b/src/User/User.php similarity index 87% rename from src/classes/User.php rename to src/User/User.php index 95f610a..44f8975 100644 --- a/src/classes/User.php +++ b/src/User/User.php @@ -1,10 +1,4 @@ exec("CREATE TABLE IF NOT EXISTS user ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT NOT NULL, - display_name TEXT NOT NULL, - password_hash TEXT NOT NULL, - about TEXT NULL, - website TEXT NULL, - mood TEXT NULL - )"); - - $db->exec("CREATE TABLE IF NOT EXISTS settings ( - id INTEGER PRIMARY KEY, - site_title TEXT NOT NULL, - site_description TEXT NULL, - base_path TEXT NOT NULL, - items_per_page INTEGER NOT NULL - )"); - - // See if there's any data in the tables - $user_count = (int) $db->query("SELECT COUNT(*) FROM user")->fetchColumn(); - $settings_count = (int) $db->query("SELECT COUNT(*) FROM settings")->fetchColumn(); - - // If either table has no records and we aren't on setup.php, redirect to setup.php - if ($user_count === 0 || $settings_count === 0){ - if (basename($_SERVER['PHP_SELF']) !== 'setup.php'){ - header('Location: setup.php'); - exit; - } - } else { - // If setup is complete and we are on setup.php, redirect to index.php. - if (basename($_SERVER['PHP_SELF']) === 'setup.php'){ - header('Location: index.php'); - exit; - } - }; -} - -function get_db(): PDO { - verify_data_dir(DATA_DIR, true); - - try { - $db = new PDO("sqlite:" . DB_FILE); - $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); - } catch (PDOException $e) { - die("Database connection failed: " . $e->getMessage()); - } - - return $db; -}