HTML.pm 27 KB

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