HTML.pm 27 KB

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