HTML.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package Trog::Routes::HTML;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use File::Touch();
  7. use Trog::Config;
  8. use Trog::Data;
  9. my $conf = Trog::Config::get();
  10. my $template_dir = 'www/templates';
  11. my $theme_dir;
  12. $theme_dir = "themes/".$conf->param('general.theme') if $conf->param('general.theme') && -d "www/themes/".$conf->param('general.theme');
  13. use lib 'www';
  14. # TODO Things which should be themable
  15. our $landing_page = 'default.tx';
  16. our $htmltitle = 'title.tx';
  17. our $midtitle = 'midtitle.tx';
  18. our $rightbar = 'rightbar.tx';
  19. our $leftbar = 'leftbar.tx';
  20. our $footbar = 'footbar.tx';
  21. our %routes = (
  22. default => {
  23. callback => \&Trog::Routes::HTML::setup,
  24. },
  25. '/' => {
  26. method => 'GET',
  27. callback => \&Trog::Routes::HTML::index,
  28. },
  29. # This should only be enabled to debug
  30. # '/setup' => {
  31. # method => 'GET',
  32. # callback => \&Trog::Routes::HTML::setup,
  33. # },
  34. '/login' => {
  35. method => 'GET',
  36. callback => \&Trog::Routes::HTML::login,
  37. },
  38. '/auth' => {
  39. method => 'POST',
  40. nostatic => 1,
  41. callback => \&Trog::Routes::HTML::login,
  42. },
  43. '/config' => {
  44. method => 'GET',
  45. auth => 1,
  46. callback => \&Trog::Routes::HTML::config,
  47. },
  48. '/config/save' => {
  49. method => 'POST',
  50. auth => 1,
  51. callback => \&Trog::Routes::HTML::config_save,
  52. },
  53. '/post' => {
  54. method => 'GET',
  55. auth => 1,
  56. callback => \&Trog::Routes::HTML::post,
  57. },
  58. '/post/save' => {
  59. method => 'POST',
  60. auth => 1,
  61. callback => \&Trog::Routes::HTML::post,
  62. },
  63. '/posts' => {
  64. method => 'GET',
  65. callback => \&Trog::Routes::HTML::posts,
  66. },
  67. '/profile' => {
  68. method => 'POST',
  69. auth => 1,
  70. callback => \&Trog::Routes::HTML::profile,
  71. },
  72. '/themeclone' => {
  73. method => 'POST',
  74. auth => 1,
  75. callback => \&Trog::Routes::HTML::themeclone,
  76. },
  77. );
  78. # Build aliases for /post with extra data
  79. my @post_aliases = qw{news blog image video audio about files};
  80. @routes{map { "/$_" } @post_aliases} = map { my %copy = %{$routes{'/posts'}}; $copy{data} = { tag => [$_] }; \%copy } @post_aliases;
  81. # Grab theme routes
  82. my $themed = 0;
  83. if ($theme_dir) {
  84. my $theme_mod = "$theme_dir/routes.pm";
  85. if (-f "www/$theme_mod" ) {
  86. require $theme_mod;
  87. @routes{keys(%Theme::routes)} = values(%Theme::routes);
  88. $themed = 1;
  89. }
  90. }
  91. # TODO build a sitemap.xml based on the above routing table, and robots.txt
  92. sub index ($query, $input, $render_cb, $content = '', $i_styles = []) {
  93. $query->{theme_dir} = $theme_dir || '';
  94. my $processor = Text::Xslate->new(
  95. path => $template_dir,
  96. );
  97. my $t_processor;
  98. $t_processor = Text::Xslate->new(
  99. path => "www/$theme_dir/templates",
  100. ) if $theme_dir;
  101. $content ||= _pick_processor($rightbar,$processor,$t_processor)->render($landing_page,$query);
  102. my @styles = ('/styles/avatars.css'); #TODO generate file for users
  103. if ($theme_dir) {
  104. unshift(@styles, _themed_style("screen.css")) if -f 'www/'._themed_style("screen.css");
  105. unshift(@styles, _themed_style("structure.css")) if -f 'www/'._themed_style("structure.css");
  106. }
  107. push( @styles, @$i_styles);
  108. #TODO allow theming of print css
  109. my $search_info = Trog::Data->new($conf);
  110. return $render_cb->('index.tx',{
  111. user => $query->{user},
  112. search_lang => $search_info->{lang},
  113. search_help => $search_info->{help},
  114. route => $query->{route},
  115. theme_dir => $theme_dir,
  116. content => $content,
  117. title => $conf->param('general.title'), #TODO control in theme instead
  118. htmltitle => _pick_processor("templates/$htmltitle" ,$processor,$t_processor)->render($htmltitle,$query),
  119. midtitle => _pick_processor("templates/$midtitle" ,$processor,$t_processor)->render($midtitle,$query),
  120. rightbar => _pick_processor("templates/$rightbar" ,$processor,$t_processor)->render($rightbar,$query),
  121. leftbar => _pick_processor("templates/$leftbar" ,$processor,$t_processor)->render($leftbar,$query),
  122. footbar => _pick_processor("templates/$footbar" ,$processor,$t_processor)->render($footbar,$query),
  123. stylesheets => \@styles,
  124. });
  125. }
  126. sub setup ($query, $input, $render_cb) {
  127. File::Touch::touch("$ENV{HOME}/.tcms/setup");
  128. return $render_cb->('notconfigured.tx', {
  129. title => 'tCMS Requires Setup to Continue...',
  130. stylesheets => _build_themed_styles('notconfigured.css'),
  131. });
  132. }
  133. sub login ($query, $input, $render_cb) {
  134. # Redirect if we actually have a logged in user.
  135. # Note to future me -- this user value is overwritten explicitly in server.psgi.
  136. # If that ever changes, you will die
  137. $query->{to} //= '/config';
  138. if ($query->{user}) {
  139. return $routes{$query->{to}}{callback}->($query,$input,$render_cb);
  140. }
  141. #Set the cookiez and issue a 302 back to ourselves if we have good creds
  142. my $postdata = _input2postdata($input);
  143. #Check and see if we have no users. If so we will just accept whatever creds are passed.
  144. my $hasusers = -f "$ENV{HOME}/.tcms/has_users";
  145. my $btnmsg = $hasusers ? "Log In" : "Register";
  146. my @headers;
  147. if ($postdata->{username} && $postdata->{password}) {
  148. if (!$hasusers) {
  149. # Make the first user
  150. Trog::Auth::useradd($postdata->{username}, $postdata->{password});
  151. File::Touch::touch("$ENV{HOME}/.tcms/has_users");
  152. }
  153. $query->{failed} = 1;
  154. my $cookie = Trog::Auth::mksession($postdata->{username}, $postdata->{password});
  155. if ($cookie) {
  156. # TODO secure / sameSite cookie to kill csrf, maybe do rememberme with Expires=~0
  157. @headers = (
  158. "Set-Cookie: tcmslogin=$cookie; HttpOnly",
  159. );
  160. $query->{failed} = 0;
  161. }
  162. }
  163. $query->{failed} //= -1;
  164. return $render_cb->('login.tx', {
  165. title => 'tCMS 2 ~ Login',
  166. to => $query->{to},
  167. failure => int( $query->{failed} ),
  168. message => int( $query->{failed} ) < 1 ? "Login Successful, Redirecting..." : "Login Failed.",
  169. btnmsg => $btnmsg,
  170. stylesheets => _build_themed_styles('login.css'),
  171. }, @headers);
  172. }
  173. sub config ($query, $input, $render_cb) {
  174. if (!$query->{user}) {
  175. $query->{to} = '/config';
  176. return login($query,$input,$render_cb);
  177. }
  178. my $tags = ['profile'];
  179. my $posts = _post_helper($query, $tags);
  180. my $css = _build_themed_styles('config.css');
  181. my $js = _build_themed_scripts('post.js');
  182. push(@$css, '/styles/avatars.css');
  183. $query->{failure} //= -1;
  184. return $render_cb->('config.tx', {
  185. title => 'Configure tCMS',
  186. stylesheets => $css,
  187. scripts => $js,
  188. themes => _get_themes(),
  189. data_models => _get_data_models(),
  190. current_theme => $conf->param('general.theme'),
  191. current_data_model => $conf->param('general.data_model'),
  192. route => '/about',
  193. category => '/about',
  194. types => ['profile'],
  195. can_edit => 1,
  196. posts => $posts,
  197. edittype => 'profile',
  198. message => $query->{message},
  199. failure => $query->{failure},
  200. to => '/config',
  201. });
  202. }
  203. sub _get_themes {
  204. my $dir = 'www/themes';
  205. opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
  206. my @tdirs = grep { !/^\./ && -d "$dir/$_" } readdir($dh);
  207. closedir $dh;
  208. return \@tdirs;
  209. }
  210. sub _get_data_models {
  211. my $dir = 'lib/Trog/Data';
  212. opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
  213. my @dmods = map { s/\.pm$//g; $_ } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh);
  214. closedir $dh;
  215. return \@dmods
  216. }
  217. sub config_save ($query, $input, $render_cb) {
  218. my $postdata = _input2postdata($input);
  219. $conf->param( 'general.theme', $postdata->{theme} ) if defined $postdata->{theme};
  220. $conf->param( 'general.data_model', $postdata->{data_model} ) if $postdata->{data_model};
  221. $query->{failure} = 1;
  222. $query->{message} = "Failed to save configuration!";
  223. if ($conf->save()) {
  224. $query->{failure} = 0;
  225. $query->{message} = "Configuration updated succesfully.";
  226. }
  227. # TODO we need to soft-restart the server at this point. Maybe we can just hot-load config on each page when we get to have static renders? Probably not worth the perf hit for paywall users.
  228. return config($query, $input, $render_cb);
  229. }
  230. # TODO actually do stuff
  231. sub profile ($query, $input, $render_cb) {
  232. return config($query, $input, $render_cb);
  233. }
  234. sub themeclone ($query, $input, $render_cb) {
  235. my $postdata = _input2postdata($input);
  236. my ($theme, $newtheme) = ($postdata->{theme},$postdata->{newtheme});
  237. my $themedir = 'www/themes';
  238. $query->{failure} = 1;
  239. $query->{message} = "Failed to clone theme '$theme' as '$newtheme'!";
  240. require File::Copy::Recursive;
  241. if ($theme && $newtheme && File::Copy::Recursive::dircopy( "$themedir/$theme", "$themedir/$newtheme" )) {
  242. $query->{failure} = 0;
  243. $query->{message} = "Successfully cloned theme '$theme' as '$newtheme'.";
  244. }
  245. return config($query, $input, $render_cb);
  246. }
  247. sub post ($query, $input, $render_cb) {
  248. if (!$query->{user}) {
  249. $query->{to} = '/config';
  250. return login($query,$input,$render_cb);
  251. }
  252. my $tags = _coerce_array($query->{tag});
  253. my $posts = _post_helper($query, $tags);
  254. my $css = _build_themed_styles('post.css');
  255. my $js = _build_themed_scripts('post.js');
  256. push(@$css, '/styles/avatars.css');
  257. return $render_cb->('post.tx', {
  258. title => 'New Post',
  259. stylesheets => $css,
  260. scripts => $js,
  261. posts => $posts,
  262. can_edit => 1,
  263. types => [qw{microblog blog file}],
  264. route => '/posts',
  265. category => '/posts',
  266. edittype => $query->{type} || 'microblog',
  267. });
  268. }
  269. sub post_save ($query, $input, $render_cb) {
  270. return post($query, $input, $render_cb);
  271. }
  272. sub posts ($query, $input, $render_cb) {
  273. my $tags = _coerce_array($query->{tag});
  274. my $posts = _post_helper($query, $tags);
  275. my $fmt = $query->{format} || '';
  276. return _rss($posts) if $fmt eq 'rss';
  277. my $processor ||= Text::Xslate->new(
  278. path => _dir_for_resource('posts.tx'),
  279. );
  280. my $styles = _build_themed_styles('posts.css');
  281. my $content = $processor->render('posts.tx', {
  282. title => "Posts tagged @$tags",
  283. date => 'TODO',
  284. posts => $posts,
  285. route => $query->{route},
  286. category => $themed ? Theme::path_to_tile($query->{route}) : $query->{route},
  287. });
  288. return Trog::Routes::HTML::index($query, $input, $render_cb, $content, $styles);
  289. }
  290. sub _post_helper ($query, $tags) {
  291. my $data = Trog::Data->new($conf);
  292. return $data->get(
  293. tags => $tags,
  294. like => $query->{like},
  295. );
  296. }
  297. sub _rss ($posts) {
  298. return [200, ["Content-type: text/plain\n"], ["TODO"]];
  299. }
  300. sub _input2postdata ($input) {
  301. #Set the cookiez and issue a 302 back to ourselves if we have good creds
  302. my ($slurpee,$postdata) = ('',{});
  303. while (<$input>) { $slurpee .= $_ }
  304. $postdata = URL::Encode::url_params_mixed($slurpee) if $slurpee;
  305. return $postdata;
  306. }
  307. # Deal with Params which may or may not be arrays
  308. sub _coerce_array ($param) {
  309. my $p = $param || [];
  310. $p = [$param] if $param && (ref $param ne 'ARRAY');
  311. return $p;
  312. }
  313. sub _build_themed_styles ($style) {
  314. my @styles = ("/styles/$style");
  315. my $ts = _themed_style($style);
  316. push(@styles, $ts) if $theme_dir && -f "www/$ts";
  317. return \@styles;
  318. }
  319. sub _build_themed_scripts ($script) {
  320. my @scripts = ("/scripts/$script");
  321. my $ts = _themed_style($script);
  322. push(@scripts, $ts) if $theme_dir && -f "www/$ts";
  323. return \@scripts;
  324. }
  325. sub _pick_processor($file, $normal, $themed) {
  326. return _dir_for_resource($file) eq $template_dir ? $normal : $themed;
  327. }
  328. # Pick appropriate dir based on whether theme override exists
  329. sub _dir_for_resource ($resource) {
  330. return $theme_dir && -f "www/$theme_dir/$resource" ? $theme_dir : $template_dir;
  331. }
  332. sub _themed_style ($resource) {
  333. return _dir_for_resource("styles/$resource")."/styles/$resource";
  334. }
  335. sub _themed_script ($resource) {
  336. return _dir_for_resource("scripts/$resource")."/scripts/$resource";
  337. }
  338. 1;