HTML.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. package Trog::Routes::HTML;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  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. our $landing_page = 'default.tx';
  15. our $htmltitle = 'title.tx';
  16. our $midtitle = 'midtitle.tx';
  17. our $rightbar = 'rightbar.tx';
  18. our $leftbar = 'leftbar.tx';
  19. our $footbar = 'footbar.tx';
  20. our %routes = (
  21. default => {
  22. callback => \&Trog::Routes::HTML::setup,
  23. },
  24. '/' => {
  25. method => 'GET',
  26. callback => \&Trog::Routes::HTML::index,
  27. },
  28. # This should only be enabled to debug
  29. # '/setup' => {
  30. # method => 'GET',
  31. # callback => \&Trog::Routes::HTML::setup,
  32. # },
  33. '/login' => {
  34. method => 'GET',
  35. callback => \&Trog::Routes::HTML::login,
  36. },
  37. '/auth' => {
  38. method => 'POST',
  39. nostatic => 1,
  40. callback => \&Trog::Routes::HTML::login,
  41. },
  42. '/config' => {
  43. method => 'GET',
  44. auth => 1,
  45. callback => \&Trog::Routes::HTML::config,
  46. },
  47. '/config/save' => {
  48. method => 'POST',
  49. auth => 1,
  50. callback => \&Trog::Routes::HTML::config_save,
  51. },
  52. '/post' => {
  53. method => 'GET',
  54. auth => 1,
  55. callback => \&Trog::Routes::HTML::post,
  56. },
  57. '/post/save' => {
  58. method => 'POST',
  59. auth => 1,
  60. callback => \&Trog::Routes::HTML::post,
  61. },
  62. '/post/(.*)' => {
  63. method => 'GET',
  64. auth => 1,
  65. callback => \&Trog::Routes::HTML::post,
  66. captures => ['id'],
  67. },
  68. '/posts/(.*)' => {
  69. method => 'GET',
  70. callback => \&Trog::Routes::HTML::posts,
  71. captures => ['id'],
  72. },
  73. '/posts' => {
  74. method => 'GET',
  75. callback => \&Trog::Routes::HTML::posts,
  76. },
  77. '/profile' => {
  78. method => 'POST',
  79. auth => 1,
  80. callback => \&Trog::Routes::HTML::profile,
  81. },
  82. '/themeclone' => {
  83. method => 'POST',
  84. auth => 1,
  85. callback => \&Trog::Routes::HTML::themeclone,
  86. },
  87. '/sitemap', => {
  88. method => 'GET',
  89. callback => \&Trog::Routes::HTML::sitemap,
  90. },
  91. '/sitemap_index.xml', => {
  92. method => 'GET',
  93. callback => \&Trog::Routes::HTML::sitemap,
  94. data => { xml => 1 },
  95. },
  96. '/sitemap_index.xml.gz', => {
  97. method => 'GET',
  98. callback => \&Trog::Routes::HTML::sitemap,
  99. data => { xml => 1, compressed => 1 },
  100. },
  101. '/sitemap/static.xml' => {
  102. method => 'GET',
  103. callback => \&Trog::Routes::HTML::sitemap,
  104. data => { xml => 1, map => 'static' },
  105. },
  106. '/sitemap/static.xml.gz' => {
  107. method => 'GET',
  108. callback => \&Trog::Routes::HTML::sitemap,
  109. data => { xml => 1, compressed => 1, map => 'static' },
  110. },
  111. '/sitemap/(.*).xml' => {
  112. method => 'GET',
  113. callback => \&Trog::Routes::HTML::sitemap,
  114. data => { xml => 1 },
  115. captures => ['map'],
  116. },
  117. '/sitemap/(.*).xml.gz' => {
  118. method => 'GET',
  119. callback => \&Trog::Routes::HTML::sitemap,
  120. data => { xml => 1, compressed => 1},
  121. captures => ['map'],
  122. },
  123. );
  124. # Build aliases for /posts and /post with extra data
  125. my @post_aliases = qw{news blog image video audio about files series};
  126. @routes{map { "/$_" } @post_aliases} = map { my %copy = %{$routes{'/posts'}}; $copy{data}{tag} = [$_]; \%copy } @post_aliases;
  127. #TODO clean this up so we don't need _build_post_type
  128. @routes{map { "/post/$_" } qw{image video audio files}} = map { my %copy = %{$routes{'/post'}}; $copy{data}{tag} = [$_]; $copy{data}{type} = 'file'; \%copy } qw{image video audio files};
  129. $routes{'/post/news'} = { method => 'GET', auth => 1, callback => \&Trog::Routes::HTML::post, data => { tag => ['news'], type => 'microblog' } };
  130. $routes{'/post/blog'} = { method => 'GET', auth => 1, callback => \&Trog::Routes::HTML::post, data => { tag => ['blog'], type => 'blog' } };
  131. $routes{'/post/about'} = { method => 'GET', auth => 1, callback => \&Trog::Routes::HTML::post, data => { tag => ['profile'], type => 'profile' } };
  132. $routes{'/post/series'} = { method => 'GET', auth => 1, callback => \&Trog::Routes::HTML::post, data => { tag => ['series'], type => 'series' } };
  133. # Build aliases for /posts/(.*) and /post/(.*) with extra data
  134. @routes{map { "/$_/(.*)" } @post_aliases} = map { my %copy = %{$routes{'/posts/(.*)'}}; \%copy } @post_aliases;
  135. @routes{map { "/$_/(.*)" } @post_aliases} = map { my %copy = %{$routes{'/post/(.*)'}}; \%copy } @post_aliases;
  136. # Grab theme routes
  137. my $themed = 0;
  138. if ($theme_dir) {
  139. my $theme_mod = "$theme_dir/routes.pm";
  140. if (-f "www/$theme_mod" ) {
  141. require $theme_mod;
  142. @routes{keys(%Theme::routes)} = values(%Theme::routes);
  143. $themed = 1;
  144. }
  145. }
  146. sub index ($query, $input, $render_cb, $content = '', $i_styles = []) {
  147. $query->{theme_dir} = $theme_dir || '';
  148. my $processor = Text::Xslate->new(
  149. path => $template_dir,
  150. );
  151. my $t_processor;
  152. $t_processor = Text::Xslate->new(
  153. path => "www/$theme_dir/templates",
  154. ) if $theme_dir;
  155. $content ||= _pick_processor($rightbar,$processor,$t_processor)->render($landing_page,$query);
  156. my @styles = ('/styles/avatars.css'); #TODO generate file for users
  157. if ($theme_dir) {
  158. unshift(@styles, _themed_style("screen.css")) if -f 'www/'._themed_style("screen.css");
  159. unshift(@styles, _themed_style("structure.css")) if -f 'www/'._themed_style("structure.css");
  160. }
  161. push( @styles, @$i_styles);
  162. #TODO allow theming of print css
  163. my $search_info = Trog::Data->new($conf);
  164. return $render_cb->('index.tx',{
  165. code => $query->{code},
  166. user => $query->{user},
  167. search_lang => $search_info->{lang},
  168. search_help => $search_info->{help},
  169. route => $query->{route},
  170. theme_dir => $theme_dir,
  171. content => $content,
  172. title => $conf->param('general.title'), #TODO control in theme instead
  173. htmltitle => _pick_processor("templates/$htmltitle" ,$processor,$t_processor)->render($htmltitle,$query),
  174. midtitle => _pick_processor("templates/$midtitle" ,$processor,$t_processor)->render($midtitle,$query),
  175. rightbar => _pick_processor("templates/$rightbar" ,$processor,$t_processor)->render($rightbar,$query),
  176. leftbar => _pick_processor("templates/$leftbar" ,$processor,$t_processor)->render($leftbar,$query),
  177. footbar => _pick_processor("templates/$footbar" ,$processor,$t_processor)->render($footbar,$query),
  178. stylesheets => \@styles,
  179. });
  180. }
  181. =head1 ADMIN ROUTES
  182. These are things that issue returns other than 200, and are not directly accessible by users via any defined route.
  183. =head2 notfound, forbidden, badrequest
  184. Implements the 4XX status codes. Override templates named the same for theming this.
  185. =cut
  186. sub _generic_route ($rname, $code, $title, $query,$input,$render_cb) {
  187. $query->{code} = $code;
  188. my $processor = Text::Xslate->new(
  189. path => _dir_for_resource("$rname.tx"),
  190. );
  191. my $styles = _build_themed_styles("$rname.css");
  192. my $content = $processor->render("$rname.tx", {
  193. title => $title,
  194. route => $query->{route},
  195. user => $query->{user},
  196. styles => $styles,
  197. });
  198. return Trog::Routes::HTML::index($query, $input, $render_cb, $content, $styles);
  199. }
  200. sub notfound (@args) {
  201. return _generic_route('notfound',404,"Return to sender, Address unknown", @args);
  202. }
  203. sub forbidden (@args) {
  204. return _generic_route('forbidden', 403, "STAY OUT YOU RED MENACE", @args);
  205. }
  206. sub badrequest (@args) {
  207. return _generic_route('badrequest', 400, "Bad Request", @args);
  208. }
  209. # TODO Rate limiting route
  210. =head1 NORMAL ROUTES
  211. These are expected to either return a 200, or redirect to something which does.
  212. =head2 setup
  213. One time setup page; should only display to the first user to visit the site which we presume to be the administrator.
  214. =cut
  215. sub setup ($query, $input, $render_cb) {
  216. File::Touch::touch("$ENV{HOME}/.tcms/setup");
  217. return $render_cb->('notconfigured.tx', {
  218. title => 'tCMS Requires Setup to Continue...',
  219. stylesheets => _build_themed_styles('notconfigured.css'),
  220. });
  221. }
  222. =head2 login
  223. Sets the user cookie if the provided user exists, or sets up the user as an admin with the provided credentials in the event that no users exist.
  224. =cut
  225. sub login ($query, $input, $render_cb) {
  226. # Redirect if we actually have a logged in user.
  227. # Note to future me -- this user value is overwritten explicitly in server.psgi.
  228. # If that ever changes, you will die
  229. $query->{to} //= '/config';
  230. if ($query->{user}) {
  231. return $routes{$query->{to}}{callback}->($query,$input,$render_cb);
  232. }
  233. #Set the cookiez and issue a 302 back to ourselves if we have good creds
  234. my $postdata = _input2postdata($input);
  235. #Check and see if we have no users. If so we will just accept whatever creds are passed.
  236. my $hasusers = -f "$ENV{HOME}/.tcms/has_users";
  237. my $btnmsg = $hasusers ? "Log In" : "Register";
  238. my @headers;
  239. if ($postdata->{username} && $postdata->{password}) {
  240. if (!$hasusers) {
  241. # Make the first user
  242. Trog::Auth::useradd($postdata->{username}, $postdata->{password}, ['admin'] );
  243. File::Touch::touch("$ENV{HOME}/.tcms/has_users");
  244. }
  245. $query->{failed} = 1;
  246. my $cookie = Trog::Auth::mksession($postdata->{username}, $postdata->{password});
  247. if ($cookie) {
  248. # TODO secure / sameSite cookie to kill csrf, maybe do rememberme with Expires=~0
  249. @headers = (
  250. "Set-Cookie: tcmslogin=$cookie; HttpOnly",
  251. );
  252. $query->{failed} = 0;
  253. }
  254. }
  255. $query->{failed} //= -1;
  256. return $render_cb->('login.tx', {
  257. title => 'tCMS 2 ~ Login',
  258. to => $query->{to},
  259. failure => int( $query->{failed} ),
  260. message => int( $query->{failed} ) < 1 ? "Login Successful, Redirecting..." : "Login Failed.",
  261. btnmsg => $btnmsg,
  262. stylesheets => _build_themed_styles('login.css'),
  263. }, @headers);
  264. }
  265. =head2 config
  266. Renders the configuration page, or redirects you back to the login page.
  267. =cut
  268. sub config ($query, $input, $render_cb) {
  269. if (!$query->{user}) {
  270. $query->{to} = '/config';
  271. return login($query,$input,$render_cb);
  272. }
  273. #NOTE: we are relying on this to skip the ACL check with 'admin', this may not be viable in future?
  274. return forbidden($query, $input, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  275. my $css = _build_themed_styles('config.css');
  276. my $js = _build_themed_scripts('post.js');
  277. $query->{failure} //= -1;
  278. return $render_cb->('config.tx', {
  279. title => 'Configure tCMS',
  280. stylesheets => $css,
  281. scripts => $js,
  282. themes => _get_themes(),
  283. data_models => _get_data_models(),
  284. current_theme => $conf->param('general.theme') // '',
  285. current_data_model => $conf->param('general.data_model') // 'DUMMY',
  286. message => $query->{message},
  287. failure => $query->{failure},
  288. to => '/config',
  289. });
  290. }
  291. sub _get_themes {
  292. my $dir = 'www/themes';
  293. opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
  294. my @tdirs = grep { !/^\./ && -d "$dir/$_" } readdir($dh);
  295. closedir $dh;
  296. return \@tdirs;
  297. }
  298. sub _get_data_models {
  299. my $dir = 'lib/Trog/Data';
  300. opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
  301. my @dmods = map { s/\.pm$//g; $_ } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh);
  302. closedir $dh;
  303. return \@dmods
  304. }
  305. =head2 config_save
  306. Implements /config/save route. Saves what little configuration we actually use to ~/.tcms/tcms.conf
  307. =cut
  308. sub config_save ($query, $input, $render_cb) {
  309. my $postdata = _input2postdata($input);
  310. $conf->param( 'general.theme', $postdata->{theme} ) if defined $postdata->{theme};
  311. $conf->param( 'general.data_model', $postdata->{data_model} ) if $postdata->{data_model};
  312. $query->{failure} = 1;
  313. $query->{message} = "Failed to save configuration!";
  314. if ($conf->save($Trog::Config::home_config)) {
  315. $query->{failure} = 0;
  316. $query->{message} = "Configuration updated succesfully.";
  317. }
  318. # 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.
  319. return config($query, $input, $render_cb);
  320. }
  321. # TODO actually do stuff
  322. sub profile ($query, $input, $render_cb) {
  323. return config($query, $input, $render_cb);
  324. }
  325. =head2 themeclone
  326. Clone a theme by copying a directory.
  327. =cut
  328. sub themeclone ($query, $input, $render_cb) {
  329. my $postdata = _input2postdata($input);
  330. my ($theme, $newtheme) = ($postdata->{theme},$postdata->{newtheme});
  331. my $themedir = 'www/themes';
  332. $query->{failure} = 1;
  333. $query->{message} = "Failed to clone theme '$theme' as '$newtheme'!";
  334. require File::Copy::Recursive;
  335. if ($theme && $newtheme && File::Copy::Recursive::dircopy( "$themedir/$theme", "$themedir/$newtheme" )) {
  336. $query->{failure} = 0;
  337. $query->{message} = "Successfully cloned theme '$theme' as '$newtheme'.";
  338. }
  339. return config($query, $input, $render_cb);
  340. }
  341. =head2 post
  342. Display the route for making new posts.
  343. =cut
  344. sub post ($query, $input, $render_cb) {
  345. if (!$query->{user}) {
  346. $query->{to} = '/post';
  347. return login($query,$input,$render_cb);
  348. }
  349. return forbidden($query, $input, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  350. my $tags = _coerce_array($query->{tag});
  351. my $posts = _post_helper($query, $tags, $query->{acls});
  352. my $css = _build_themed_styles('post.css');
  353. my $js = _build_themed_scripts('post.js');
  354. push(@$css, '/styles/avatars.css');
  355. return $render_cb->('post.tx', {
  356. title => 'New Post',
  357. post_visibilities => ['public', 'private', 'unlisted'],
  358. stylesheets => $css,
  359. scripts => $js,
  360. posts => $posts,
  361. can_edit => 1,
  362. types => [qw{microblog blog file series profile}],
  363. route => '/posts',
  364. category => '/posts',
  365. page => int($query->{page} || 1),
  366. limit => int($query->{limit} || 1),
  367. sizes => [25,50,100],
  368. id => $query->{id},
  369. acls => _post_helper({}, ['series'], $query->{acls}),
  370. edittype => $query->{type} || 'microblog',
  371. });
  372. }
  373. #TODO actually do stuff
  374. sub post_save ($query, $input, $render_cb) {
  375. return post($query, $input, $render_cb);
  376. }
  377. =head2 posts
  378. Display multi or single posts, supports RSS and pagination.
  379. =cut
  380. sub posts ($query, $input, $render_cb) {
  381. my $tags = _coerce_array($query->{tag});
  382. #TODO If we have a direct ID query, we should show unlisted videos as well as public ones IFF they have a valid campaign ID attached to query
  383. push(@{$query->{acls}}, 'public');
  384. my $posts = _post_helper($query, $tags, $query->{acls});
  385. return notfound($query,$input,$render_cb) unless @$posts;
  386. my $fmt = $query->{format} || '';
  387. return _rss($query,$posts) if $fmt eq 'rss';
  388. my $processor = Text::Xslate->new(
  389. path => _dir_for_resource('posts.tx'),
  390. );
  391. my $styles = _build_themed_styles('posts.css');
  392. my $content = $processor->render('posts.tx', {
  393. title => "Posts tagged @$tags",
  394. posts => $posts,
  395. route => $query->{route},
  396. page => int($query->{page} || 1),
  397. limit => int($query->{limit} || 1),
  398. sizes => [25,50,100],
  399. rss => !$query->{id},
  400. tiled => scalar(grep { $_ eq $query->{route} } qw{/files /audio /video /image /series}),
  401. category => $themed ? Theme::path_to_tile($query->{route}) : $query->{route},
  402. });
  403. return Trog::Routes::HTML::index($query, $input, $render_cb, $content, $styles);
  404. }
  405. sub _post_helper ($query, $tags, $acls) {
  406. state $data = Trog::Data->new($conf);
  407. return $data->get(
  408. page => int($query->{page} || 1),
  409. limit => int($query->{limit} || 25),
  410. tags => $tags,
  411. acls => $acls,
  412. like => $query->{like},
  413. id => $query->{id},
  414. );
  415. }
  416. =head2 sitemap
  417. Return the sitemap index unless the static or a set of dynamic routes is requested.
  418. We have a maximum of 99,990,000 posts we can make under this model
  419. As we have 10,000 * 10,000 posts which are indexable via the sitemap format.
  420. 1 top level index slot (10k posts) is taken by our static routes, the rest will be /posts.
  421. Passing ?xml=1 will result in an appropriate sitemap.xml instead.
  422. This is used to generate the static sitemaps as expected by search engines.
  423. Passing compressed=1 will gzip the output.
  424. =cut
  425. sub sitemap ($query, $input, $render_cb) {
  426. my (@to_map, $is_index, $route_type);
  427. my $warning = '';
  428. $query->{map} //= '';
  429. if ($query->{map} eq 'static') {
  430. # Return the map of static routes
  431. $route_type = 'Static Routes';
  432. @to_map = grep { !defined $routes{$_}->{captures} && $_ !~ m/^default|login|auth$/ && !$routes{$_}->{auth} } keys(%routes);
  433. } elsif ( !$query->{map} ) {
  434. # Return the index instead
  435. @to_map = ('static');
  436. my $data = Trog::Data->new($conf);
  437. my $tot = $data->total_posts();
  438. my $size = 50000;
  439. my $pages = int($tot / $size) + (($tot % $size) ? 1 : 0);
  440. # Truncate pages at 10k due to standard
  441. my $clamped = $pages > 49999 ? 49999 : $pages;
  442. $warning = "More posts than possible to represent in sitemaps & index! Old posts have been truncated." if $pages > 49999;
  443. foreach my $page ($clamped..1) {
  444. push(@to_map, "$page");
  445. }
  446. $is_index = 1;
  447. } else {
  448. $route_type = "Posts: Page $query->{map}";
  449. # Return the map of the particular range of dynamic posts
  450. $query->{limit} = 50000;
  451. $query->{page} = $query->{map};
  452. @to_map = @{_post_helper($query, [], ['public'])};
  453. }
  454. if ( $query->{xml} ) {
  455. my $sm;
  456. my $xml_date = time();
  457. my $fmt = "xml";
  458. $fmt .= ".gz" if $query->{compressed};
  459. if ( !$query->{map}) {
  460. require WWW::SitemapIndex::XML;
  461. $sm = WWW::SitemapIndex::XML->new();
  462. foreach my $url (@to_map) {
  463. $sm->add(
  464. loc => "http://$query->{domain}/sitemap/$url.$fmt",
  465. lastmod => $xml_date,
  466. );
  467. }
  468. } else {
  469. require WWW::Sitemap::XML;
  470. $sm = WWW::Sitemap::XML->new();
  471. my $changefreq = $query->{map} eq 'static' ? 'monthly' : 'daily';
  472. foreach my $url (@to_map) {
  473. my $true_uri = "http://$query->{domain}$url";
  474. $true_uri = "http://$query->{domain}/posts/$url->{id}" if ref $url eq 'HASH';
  475. my %data = (
  476. loc => $true_uri,
  477. lastmod => $xml_date,
  478. mobile => 1,
  479. changefreq => $changefreq,
  480. priority => 1.0,
  481. );
  482. #TODO add video & preview image if applicable
  483. $sm->add(%data);
  484. }
  485. }
  486. my $xml = $sm->as_xml();
  487. require IO::String;
  488. my $buf = IO::String->new();
  489. my $ct = 'application/xml';
  490. $xml->toFH($buf, 0);
  491. seek $buf, 0, 0;
  492. if ($query->{compressed}) {
  493. require IO::Compress::Gzip;
  494. my $compressed = IO::String->new();
  495. IO::Compress::Gzip::gzip($buf => $compressed);
  496. $ct = 'application/gzip';
  497. $buf = $compressed;
  498. seek $compressed, 0, 0;
  499. }
  500. return [200,["Content-type:$ct\n"], $buf];
  501. }
  502. @to_map = sort @to_map unless $is_index;
  503. my $processor = Text::Xslate->new(
  504. path => _dir_for_resource('sitemap.tx'),
  505. );
  506. my $styles = _build_themed_styles('sitemap.css');
  507. my $content = $processor->render('sitemap.tx', {
  508. title => "Site Map",
  509. to_map => \@to_map,
  510. is_index => $is_index,
  511. route_type => $route_type,
  512. route => $query->{route},
  513. });
  514. return Trog::Routes::HTML::index($query,$input,$render_cb,$content,$styles);
  515. }
  516. sub _rss ($query,$posts) {
  517. require XML::RSS;
  518. my $rss = XML::RSS->new (version => '2.0');
  519. my $now = DateTime->from_epoch(epoch => time());
  520. $rss->channel(
  521. title => "$query->{domain}",
  522. link => "http://$query->{domain}/$query->{route}?format=rss",
  523. language => 'en', #TODO localization
  524. description => 'tCMS website', #TODO make configurable
  525. pubDate => $now, #TODO format
  526. lastBuildDate => $now, #TODO format
  527. );
  528. #TODO configurability
  529. $rss->image(
  530. title => $query->{domain},
  531. url => "http://$query->{domain}/img/icon/tcms.svg",
  532. link => "http://$query->{domain}",
  533. width => 88,
  534. height => 31,
  535. description => 'tCMS image'
  536. );
  537. foreach my $post (@$posts) {
  538. my $url = "http://$query->{domain}/posts/$post->{id}";
  539. $rss->add_item(
  540. title => $post->{title},
  541. permaLink => $url,
  542. link => $url,
  543. enclosure => { url => $url, type=>"text/html" },
  544. description => "<![CDATA[$post->{data}]]>",
  545. pubDate => DateTime->from_epoch(epoch => $post->{created} ), #TODO format like Thu, 23 Aug 1999 07:00:00 GMT
  546. author => $post->{user}, #TODO translate to "email (user)" format
  547. );
  548. }
  549. return [200, ["Content-type: application/rss+xml\n"], [$rss->as_string]];
  550. }
  551. sub _input2postdata ($input) {
  552. #Set the cookiez and issue a 302 back to ourselves if we have good creds
  553. my ($slurpee,$postdata) = ('',{});
  554. while (<$input>) { $slurpee .= $_ }
  555. $postdata = URL::Encode::url_params_mixed($slurpee) if $slurpee;
  556. return $postdata;
  557. }
  558. # Deal with Params which may or may not be arrays
  559. sub _coerce_array ($param) {
  560. my $p = $param || [];
  561. $p = [$param] if $param && (ref $param ne 'ARRAY');
  562. return $p;
  563. }
  564. sub _build_themed_styles ($style) {
  565. my @styles = ("/styles/$style");
  566. my $ts = _themed_style($style);
  567. push(@styles, $ts) if $theme_dir && -f "www/$ts";
  568. return \@styles;
  569. }
  570. sub _build_themed_scripts ($script) {
  571. my @scripts = ("/scripts/$script");
  572. my $ts = _themed_style($script);
  573. push(@scripts, $ts) if $theme_dir && -f "www/$ts";
  574. return \@scripts;
  575. }
  576. sub _pick_processor($file, $normal, $themed) {
  577. return _dir_for_resource($file) eq $template_dir ? $normal : $themed;
  578. }
  579. # Pick appropriate dir based on whether theme override exists
  580. sub _dir_for_resource ($resource) {
  581. return $theme_dir && -f "www/$theme_dir/$resource" ? $theme_dir : $template_dir;
  582. }
  583. sub _themed_style ($resource) {
  584. return _dir_for_resource("styles/$resource")."/styles/$resource";
  585. }
  586. sub _themed_script ($resource) {
  587. return _dir_for_resource("scripts/$resource")."/scripts/$resource";
  588. }
  589. 1;