diff --git a/inc/config.php b/inc/config.php index 58b6a860..020120e3 100644 --- a/inc/config.php +++ b/inc/config.php @@ -208,6 +208,13 @@ //$config['dir']['static'] = $config['root'] . 'static/'; // Where to store the .html templates. This folder and templates must exist or fatal errors will be thrown. $config['dir']['template'] = getcwd() . '/templates'; + // For the homepage generation files (themes, etc.) + $config['dir']['homepage'] = getcwd() . '/templates/homepage'; + // Same as above, but a URI (accessable by web interface, not locally) + $config['dir']['homepage_uri'] = 'templates/homepage'; + // Homepage directory + $config['dir']['home'] = ''; + // Static images // These can be URLs OR base64 (data URI scheme) //$config['image_sticky'] = $config['dir']['static'] . 'sticky.gif'; @@ -393,6 +400,8 @@ $config['mod']['noticeboard_delete'] = ADMIN; // Public ban messages; attached to posts $config['mod']['public_ban'] = MOD; + // Manage and install themes for homepage + $config['mod']['themes'] = ADMIN; // Mod links (full HTML) // Correspond to above permission directives diff --git a/inc/functions.php b/inc/functions.php index 7ec58e79..144cdec1 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -93,6 +93,25 @@ } } + function loadThemeConfig($_theme) { + global $config; + + // Load theme information into $theme + include $config['dir']['homepage'] . '/' . $_theme . '/theme.php'; + return $theme; + } + + function themeSettings() { + $query = query("SELECT * FROM `theme_settings`") or error(db_error()); + $settings = Array(); + + while($s = $query->fetch()) { + $settings[$s['name']] = $s['value']; + } + + return $settings; + } + function sprintf3($str, $vars, $delim = '%') { $replaces = array(); foreach($vars as $k => $v) { diff --git a/mod.php b/mod.php index 2c112d57..90872734 100644 --- a/mod.php +++ b/mod.php @@ -247,6 +247,131 @@ 'mod'=>true ) ); + } elseif(preg_match('/^\/themes(\/(\w+))?$/', $query, $match)) { + if($mod['type'] < $config['mod']['themes']) error($config['error']['noaccess']); + + if(!is_dir($config['dir']['homepage'])) + error('Homepage directory doesn\'t exist!'); + if(!$dir = opendir($config['dir']['homepage'])) + error('Cannot open homepage directory; check permissions.'); + + if(isset($match[2])) { + $_theme = $match[2]; + + $theme = loadThemeConfig($_theme); + + if(isset($_POST['install'])) { + // Check if everything is submitted + foreach($theme['config'] as &$c) { + if(!isset($_POST[$c['name']]) && $c['type'] != 'checkbox') + error(spritnf($config['error']['required'], $c['title'])); + } + + // Clear previous settings + query("TRUNCATE TABLE `theme_settings`") or error(db_error()); + + foreach($theme['config'] as &$c) { + $query = prepare("INSERT INTO `theme_settings` VALUES(:name, :value)"); + $query->bindValue(':name', $c['name']); + $query->bindValue(':value', $_POST[$c['name']]); + $query->execute() or error(db_error($query)); + } + + $query = prepare("INSERT INTO `theme_settings` VALUES('theme', :value)"); + $query->bindValue(':value', $_theme); + $query->execute() or error(db_error($query)); + + // Build theme + $config['build_function'](themeSettings()); + } else { + $body = '
'; + + echo Element('page.html', Array( + 'config'=>$config, + 'title'=>'Installing "' . htmlentities($theme['name']) . '"', + 'body'=>$body, + 'mod'=>true + ) + ); + } + } else { + + // Scan directory for themes + $themes = Array(); + while($file = readdir($dir)) { + if($file[0] != '.' && is_dir($config['dir']['homepage'] . '/' . $file)) { + $themes[] = $file; + } + } + closedir($dir); + + $body = ''; + if(empty($themes)) { + $body = '(No themes installed.)
'; + } else { + $body .= 'Name | ' . + '' . htmlentities($theme['name']) . ' | ' . + '
---|---|
Version | ' . + '' . htmlentities($theme['version']) . ' | ' . + '
Description | ' . + '' . $theme['description'] . ' | ' . + '
Thumbnail | ' . + '' . + ' |
Actions | ' . + '
| ' .
+ '