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 = '
'; + + if(!isset($theme['config']) || empty($theme['config'])) { + $body .= '

(No configuration required.)

'; + } else { + $body .= ''; + foreach($theme['config'] as &$c) { + $body .= ''; + } + $body .= '
' . $c['title'] . ''; + switch($c['type']) { + case 'text': + default: + $body .= ''; + } + $body .= '
'; + } + + $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 .= ''; + foreach($themes as &$_theme) { + $theme = loadThemeConfig($_theme); + + markup($theme['description']); + + $body .= '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + ''; + } + $body .= '
Name' . htmlentities($theme['name']) . '
Version' . htmlentities($theme['version']) . '
Description' . $theme['description'] . '
Thumbnail
Actions
    ' . + '
  • ' . + 'Use' . + '
  • ' . + '
  • ' . + confirmLink('Remove', 'Uninstall theme', 'Are you sure you want to permanently remove this theme?', 'themes/' . $_theme . '/uninstall') . + '
  • ' . + '
'; + } + echo Element('page.html', Array( + 'config'=>$config, + 'title'=>'Select theme', + 'body'=>$body, + 'mod'=>true + ) + ); + } } elseif(preg_match('/^\/noticeboard\/delete\/(\d+)$/', $query, $match)) { if($mod['type'] < $config['mod']['noticeboard_delete']) error($config['error']['noaccess']); diff --git a/templates/homepage/frameset/theme.php b/templates/homepage/frameset/theme.php new file mode 100644 index 00000000..3964a469 --- /dev/null +++ b/templates/homepage/frameset/theme.php @@ -0,0 +1,89 @@ + 'Page title', + 'name' => 'title', + 'type' => 'text' + ); + + // Unique function name for building everything + $config['build_function'] = 'frameset_build'; + + function frameset_build($settings) { + Frameset::build($settings); + } + + // Wrap functions in a class so they don't interfere with normal Tinyboard operations + class Frameset { + public static function build($settings) { + global $config; + + file_put_contents($config['dir']['home'] . $config['file_index'], Frameset::homepage($settings)); + file_put_contents($config['dir']['home'] . 'sidebar.html', Frameset::sidebar($settings)); + } + + // Build homepage + public static function homepage($settings) { + global $config; + + // HTML5 + return '' + . '' + . '' + . '' + . '' . $settings['title'] . '' + . '' + // Sidebar + . '' + // Main + . '' + // Finish page + . ''; + } + + // Build sidebar + public static function sidebar($settings) { + global $config, $board; + + $body = '' + . '' + . '' + . '' + . '' . $settings['title'] . '' + . '

Boards

' + // Finish page + . ''; + + return $body; + } + }; + +?> \ No newline at end of file diff --git a/templates/homepage/frameset/thumb.PNG b/templates/homepage/frameset/thumb.PNG new file mode 100644 index 00000000..fd912d1a Binary files /dev/null and b/templates/homepage/frameset/thumb.PNG differ diff --git a/templates/homepage/frameset/thumb.png b/templates/homepage/frameset/thumb.png new file mode 100644 index 00000000..918ec5e6 Binary files /dev/null and b/templates/homepage/frameset/thumb.png differ