HTML.pm 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. package Trog::Routes::HTML;
  2. use strict;
  3. use warnings;
  4. no warnings qw{experimental once};
  5. use feature qw{signatures state};
  6. use POSIX qw{strftime};
  7. use Errno qw{ENOENT};
  8. use File::Touch();
  9. use File::Basename qw{basename};
  10. use List::Util();
  11. use List::MoreUtils();
  12. use Capture::Tiny qw{capture};
  13. use HTML::SocialMeta;
  14. use Clone qw{clone};
  15. use Encode qw{encode_utf8};
  16. use Digest::MD5 qw{md5_hex};
  17. use Digest::SHA qw{sha256_hex};
  18. use IO::Compress::Gzip;
  19. use Path::Tiny();
  20. use File::Basename qw{dirname};
  21. use URI();
  22. use URI::Escape();
  23. use FindBin::libs;
  24. use Trog::Log qw{:all};
  25. use Trog::Utils;
  26. use Trog::Config;
  27. use Trog::Auth;
  28. use Trog::Data;
  29. use Trog::FileHandler;
  30. use Trog::Themes;
  31. use Trog::Renderer;
  32. use Trog::Email;
  33. use Trog::Component::EmojiPicker;
  34. our $landing_page = 'default.tx';
  35. our $htmltitle = 'title.tx';
  36. our $midtitle = 'midtitle.tx';
  37. our $rightbar = 'rightbar.tx';
  38. our $leftbar = 'leftbar.tx';
  39. our $topbar = 'topbar.tx';
  40. our $footbar = 'footbar.tx';
  41. our $categorybar = 'categories.tx';
  42. # Note to maintainers: never ever remove backends from this list.
  43. # the auth => 1 is a crucial protection.
  44. our %routes = (
  45. default => {
  46. callback => \&Trog::Routes::HTML::setup,
  47. nomap => 1,
  48. },
  49. '/index' => {
  50. method => 'GET',
  51. callback => \&Trog::Routes::HTML::index,
  52. },
  53. # This should only be enabled to debug
  54. # '/setup' => {
  55. # method => 'GET',
  56. # callback => \&Trog::Routes::HTML::setup,
  57. # },
  58. '/login' => {
  59. method => 'GET',
  60. callback => \&Trog::Routes::HTML::login,
  61. noindex => 1,
  62. },
  63. '/logout' => {
  64. method => 'GET',
  65. callback => \&Trog::Routes::HTML::logout,
  66. noindex => 1,
  67. nocache => 1,
  68. },
  69. '/auth' => {
  70. method => 'POST',
  71. callback => \&Trog::Routes::HTML::login,
  72. nocache => 1,
  73. noindex => 1,
  74. },
  75. '/totp' => {
  76. method => 'GET',
  77. auth => 1,
  78. callback => \&Trog::Routes::HTML::totp,
  79. },
  80. '/post/save' => {
  81. method => 'POST',
  82. auth => 1,
  83. callback => \&Trog::Routes::HTML::post_save,
  84. },
  85. '/post/delete' => {
  86. method => 'POST',
  87. auth => 1,
  88. callback => \&Trog::Routes::HTML::post_delete,
  89. },
  90. '/config/save' => {
  91. method => 'POST',
  92. auth => 1,
  93. callback => \&Trog::Routes::HTML::config_save,
  94. },
  95. '/themeclone' => {
  96. method => 'POST',
  97. auth => 1,
  98. callback => \&Trog::Routes::HTML::themeclone,
  99. },
  100. '/profile' => {
  101. method => 'POST',
  102. auth => 1,
  103. callback => \&Trog::Routes::HTML::profile,
  104. },
  105. '/manual' => {
  106. method => 'GET',
  107. auth => 1,
  108. callback => \&Trog::Routes::HTML::manual,
  109. },
  110. '/lib/(.*)' => {
  111. method => 'GET',
  112. auth => 1,
  113. captures => ['module'],
  114. callback => \&Trog::Routes::HTML::manual,
  115. },
  116. '/password_reset' => {
  117. method => 'GET',
  118. callback => \&Trog::Routes::HTML::resetpass,
  119. noindex => 1,
  120. },
  121. # When we are logged in, we get a diff ver of the page.
  122. '/secure/password_reset' => {
  123. method => 'GET',
  124. callback => \&Trog::Routes::HTML::resetpass,
  125. auth => 1,
  126. },
  127. '/request_password_reset' => {
  128. method => 'POST',
  129. callback => \&Trog::Routes::HTML::do_resetpass,
  130. noindex => 1,
  131. nocache => 1,
  132. },
  133. '/request_totp_clear' => {
  134. method => 'POST',
  135. callback => \&Trog::Routes::HTML::do_totp_clear,
  136. noindex => 1,
  137. nocache => 1,
  138. },
  139. '/processed' => {
  140. method => 'GET',
  141. callback => \&Trog::Routes::HTML::processed,
  142. noindex => 1,
  143. },
  144. '/metrics' => {
  145. method => 'GET',
  146. auth => 1,
  147. callback => \&Trog::Routes::HTML::metrics,
  148. },
  149. '/admin/sessions' => {
  150. method => 'GET',
  151. auth => 1,
  152. callback => \&Trog::Routes::HTML::sessions,
  153. noindex => 1,
  154. nocache => 1,
  155. },
  156. '/admin/wyzzerdd' => {
  157. method => 'GET',
  158. auth => 1,
  159. callback => \&Trog::Routes::HTML::post_wizard,
  160. noindex => 1,
  161. nocache => 1,
  162. },
  163. #TODO transform into posts?
  164. '/sitemap',
  165. => {
  166. method => 'GET',
  167. callback => \&Trog::Routes::HTML::sitemap,
  168. },
  169. '/sitemap_index.xml',
  170. => {
  171. method => 'GET',
  172. callback => \&Trog::Routes::HTML::sitemap,
  173. data => { xml => 1 },
  174. },
  175. '/sitemap_index.xml.gz',
  176. => {
  177. method => 'GET',
  178. callback => \&Trog::Routes::HTML::sitemap,
  179. data => { xml => 1, compressed => 1 },
  180. },
  181. '/sitemap/static.xml' => {
  182. method => 'GET',
  183. callback => \&Trog::Routes::HTML::sitemap,
  184. data => { xml => 1, map => 'static' },
  185. },
  186. '/sitemap/static.xml.gz' => {
  187. method => 'GET',
  188. callback => \&Trog::Routes::HTML::sitemap,
  189. data => { xml => 1, compressed => 1, map => 'static' },
  190. },
  191. '/sitemap/(.*).xml' => {
  192. method => 'GET',
  193. callback => \&Trog::Routes::HTML::sitemap,
  194. data => { xml => 1 },
  195. captures => ['map'],
  196. },
  197. '/sitemap/(.*).xml.gz' => {
  198. method => 'GET',
  199. callback => \&Trog::Routes::HTML::sitemap,
  200. data => { xml => 1, compressed => 1 },
  201. captures => ['map'],
  202. },
  203. '/humans.txt' => {
  204. method => 'GET',
  205. callback => \&Trog::Routes::HTML::posts,
  206. data => { tag => ['about'] },
  207. },
  208. '/styles/avatars.css' => {
  209. method => 'GET',
  210. callback => \&Trog::Routes::HTML::avatars,
  211. data => { tag => ['about'] },
  212. },
  213. '/avatar/(.*)' => {
  214. method => 'GET',
  215. callback => \&Trog::Routes::HTML::libravatar,
  216. captures => ['avatar_hash'],
  217. noindex => 1,
  218. nocache => 1,
  219. },
  220. '/favicon.ico' => {
  221. method => 'GET',
  222. callback => \&Trog::Routes::HTML::icon,
  223. },
  224. '/totp_qr/(.*)' => {
  225. auth => 1,
  226. method => 'GET',
  227. callback => \&Trog::Routes::HTML::totp_qr,
  228. },
  229. '/styles/rss-style.xsl' => {
  230. method => 'GET',
  231. callback => \&Trog::Routes::HTML::rss_style,
  232. },
  233. );
  234. # Grab theme routes
  235. my $themed = 0;
  236. my $theme_dir = Trog::Themes::get_dir();
  237. if ($theme_dir) {
  238. my $theme_mod = "$theme_dir/routes.pm";
  239. if ( -f $theme_mod ) {
  240. use lib '.';
  241. require $theme_mod;
  242. @routes{ keys(%Theme::routes) } = values(%Theme::routes);
  243. $themed = 1;
  244. }
  245. else {
  246. # Use the special "default" theme
  247. require Theme;
  248. }
  249. }
  250. =head1 PRIMARY ROUTE
  251. =head2 index
  252. Implements the primary route used by all pages not behind auth.
  253. Most subsequent functions simply pass content to this function.
  254. =cut
  255. sub index ( $query, $content = '', $i_styles = [], $i_scripts = [] ) {
  256. $query->{theme_dir} = Trog::Themes::td();
  257. my $to_render = $query->{template} // $landing_page;
  258. $content ||= Trog::Renderer->render( template => $to_render, data => $query, component => 1, contenttype => 'text/html' );
  259. return $content if ref $content eq "ARRAY";
  260. my @styles;
  261. unshift( @styles, qw{embed.css} ) if $query->{embed};
  262. unshift( @styles, qw{screen.css structure.css} );
  263. push( @styles, @$i_styles );
  264. my @p_styles = qw{structure.css print.css};
  265. unshift( @p_styles, qw{embed.css} ) if $query->{embed};
  266. my @series = _get_series(0);
  267. my $title = $query->{primary_post}{title} // $query->{title} // $Theme::default_title // 'tCMS';
  268. # Handle link "unfurling" correctly
  269. my ( $default_tags, $meta_desc, $meta_tags ) = _build_social_meta( $query, $title );
  270. #Do embed content
  271. my $tmpl = $query->{embed} ? 'embed.tx' : 'index.tx';
  272. $query->{theme_dir} =~ s/^\/www\///;
  273. # TO support theming we have to do things like this rather than with an include directive in the templates.
  274. my $htmltitle = Trog::Renderer->render( template => $htmltitle, data => $query, component => 1, contenttype => 'text/html' );
  275. return $htmltitle if ref $htmltitle eq 'ARRAY';
  276. my $midtitle = Trog::Renderer->render( template => $midtitle, data => $query, component => 1, contenttype => 'text/html' );
  277. return $midtitle if ref $midtitle eq 'ARRAY';
  278. my $rightbar = Trog::Renderer->render( template => $rightbar, data => $query, component => 1, contenttype => 'text/html' );
  279. return $rightbar if ref $rightbar eq 'ARRAY';
  280. my $leftbar = Trog::Renderer->render( template => $leftbar, data => $query, component => 1, contenttype => 'text/html' );
  281. return $leftbar if ref $leftbar eq 'ARRAY';
  282. my $topbar = Trog::Renderer->render( template => $topbar, data => $query, component => 1, contenttype => 'text/html' );
  283. return $topbar if ref $topbar eq 'ARRAY';
  284. my $footbar = Trog::Renderer->render( template => $footbar, data => $query, component => 1, contenttype => 'text/html' );
  285. return $footbar if ref $footbar eq 'ARRAY';
  286. my $categorybar = Trog::Renderer->render( template => $categorybar, data => { %$query, categories => \@series }, component => 1, contenttype => 'text/html' );
  287. return $categorybar if ref $categorybar eq 'ARRAY';
  288. # Grab the avatar class for the logged in user
  289. if ( $query->{user} ) {
  290. $query->{user_class} = Trog::Auth::username2classname( $query->{user} );
  291. }
  292. state $data;
  293. $data //= Trog::Data->new(Trog::Config::get());
  294. return finish_render(
  295. $tmpl,
  296. {
  297. %$query,
  298. search_lang => $data->lang(),
  299. search_help => $data->help(),
  300. theme_dir => Trog::Themes::td(),
  301. content => $content,
  302. title => $title,
  303. htmltitle => $htmltitle,
  304. midtitle => $midtitle,
  305. rightbar => $rightbar,
  306. leftbar => $leftbar,
  307. topbar => $topbar,
  308. footbar => $footbar,
  309. categorybar => $categorybar,
  310. categories => \@series,
  311. stylesheets => \@styles,
  312. print_styles => \@p_styles,
  313. scripts => $i_scripts,
  314. show_madeby => $Theme::show_madeby ? 1 : 0,
  315. embed => $query->{embed} ? 1 : 0,
  316. embed_video => $query->{primary_post}{is_video},
  317. default_tags => $default_tags,
  318. meta_desc => $meta_desc,
  319. meta_tags => $meta_tags,
  320. }
  321. );
  322. }
  323. sub _build_social_meta ( $query, $title ) {
  324. return ( undef, undef, undef ) unless $query->{social_meta} && $query->{route} && $query->{domain};
  325. my $default_tags = $Theme::default_tags;
  326. $default_tags .= ',' . join( ',', @{ $query->{primary_post}->{tags} } ) if $default_tags && $query->{primary_post}->{tags};
  327. my $primary_data = ref $query->{primary_post}{data} eq 'ARRAY' ? $query->{primary_post}{data}[0] : $query->{primary_post}{data};
  328. my $meta_desc = $primary_data // $Theme::description // "tCMS Site";
  329. $meta_desc = Trog::Utils::strip_and_trunc($meta_desc) || '';
  330. my $meta_tags = '';
  331. my $card_type = 'summary';
  332. $card_type = 'featured_image' if $query->{primary_post} && $query->{primary_post}{is_image};
  333. $card_type = 'player' if $query->{primary_post} && $query->{primary_post}{is_video};
  334. my $image = $Theme::default_image ? "https://$query->{domain}/" . Trog::Themes::get_dir() . "/$Theme::default_image" : '';
  335. $image = "https://$query->{domain}/$query->{primary_post}{preview}" if $query->{primary_post} && $query->{primary_post}{preview};
  336. $image = "https://$query->{domain}/$query->{primary_post}{href}" if $query->{primary_post} && $query->{primary_post}{is_image};
  337. my $primary_route = "https://$query->{domain}/$query->{route}";
  338. $primary_route =~ s/[\/]+/\//g;
  339. my $display_name = $Theme::display_name || 'Another tCMS Site';
  340. my $extra_tags = '';
  341. my %sopts = (
  342. site => '',
  343. image => '',
  344. fb_app_id => '',
  345. site_name => $display_name,
  346. app_name => $display_name,
  347. title => $title,
  348. description => $meta_desc,
  349. url => $primary_route,
  350. );
  351. $sopts{site} = $Theme::twitter_account if $Theme::twitter_account;
  352. $sopts{image} = $image if $image;
  353. $sopts{fb_app_id} = $Theme::fb_app_id if $Theme::fb_app_id;
  354. if ( $query->{primary_post} && $query->{primary_post}{is_video} ) {
  355. #$sopts{player} = "$primary_route?embed=1";
  356. $sopts{player} = "https://$query->{domain}/$query->{primary_post}{href}";
  357. #XXX don't hardcode this
  358. $sopts{player_width} = 1280;
  359. $sopts{player_height} = 720;
  360. $extra_tags .= "<meta property='og:video:type' content='$query->{primary_post}{content_type}' />\n";
  361. }
  362. my $social = HTML::SocialMeta->new(%sopts);
  363. $meta_tags = eval { $social->create($card_type) };
  364. $meta_tags =~ s/content="video"/content="video:other"/mg if $meta_tags;
  365. $meta_tags .= $extra_tags if $extra_tags;
  366. print STDERR "WARNING: Theme misconfigured, social media tags will not be included\n$@\n" if $theme_dir && !$meta_tags;
  367. return ( $default_tags, $meta_desc, $meta_tags );
  368. }
  369. =head1 NORMAL ROUTES
  370. These are expected to either return a 200, or redirect to something which does.
  371. =head2 setup
  372. One time setup page; should only display to the first user to visit the site which we presume to be the administrator.
  373. =cut
  374. sub setup ($query) {
  375. File::Touch::touch("config/setup");
  376. Trog::Renderer->render(
  377. template => 'notconfigured.tx',
  378. data => {
  379. title => 'tCMS Requires Setup to Continue...',
  380. stylesheets => _build_themed_styles( ['notconfigured.css'] ),
  381. %$query,
  382. },
  383. contenttype => 'text/html',
  384. code => 200,
  385. );
  386. }
  387. =head2 totp
  388. Enable 2 factor auth via TOTP for the currently authenticated user.
  389. Returns a page with a QR code & TOTP uri for pasting into your authenticator app of choice.
  390. =cut
  391. sub totp ($query) {
  392. my $active_user = $query->{user};
  393. my $domain = $query->{domain};
  394. $query->{failure} //= -1;
  395. my ( $uri, $qr, $failure, $message, $totp ) = Trog::Auth::totp( $active_user, $domain );
  396. my $now_tm = time;
  397. my $now_string = strftime("%a %b %e T%H:%M:%SZ %Y", gmtime($now_tm));
  398. return Trog::Routes::HTML::index(
  399. {
  400. title => 'Enable TOTP 2-Factor Auth',
  401. theme_dir => Trog::Themes::td(),
  402. uri => $uri,
  403. qr => $qr,
  404. failure => $failure,
  405. message => $message,
  406. template => 'totp.tx',
  407. is_admin => 1,
  408. cur_code => $totp->expected_totp_code($now_tm),
  409. timestamp => $now_string,
  410. %$query,
  411. },
  412. undef,
  413. [qw{post.css}],
  414. );
  415. }
  416. =head2 login
  417. 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.
  418. =cut
  419. sub login ($query) {
  420. # Redirect if we actually have a logged in user.
  421. # Note to future me -- this user value is overwritten explicitly in server.psgi.
  422. # If that ever changes, you will die
  423. $query->{to} //= $query->{route};
  424. $query->{to} = '/config' if List::Util::any { $query->{to} eq $_ } qw{/login /logout};
  425. if ( $query->{user} ) {
  426. DEBUG("Login by $query->{user}, redirecting to $query->{to}");
  427. return $query->{tpsgi}->see_also( $query->{to} );
  428. }
  429. #Check and see if we have no users. If so we will just accept whatever creds are passed.
  430. my $hasusers = -f "config/has_users";
  431. my $btnmsg = $hasusers ? "Log In" : "Register";
  432. my $headers;
  433. my $has_totp = 0;
  434. if ( $query->{username} && $query->{password} ) {
  435. if ( !$hasusers ) {
  436. # Make the first user
  437. Trog::Auth::useradd( $query->{username}, $query->{display_name}, $query->{password}, ['admin'], $query->{contact_email} );
  438. # Add a stub user page and the initial series.
  439. my $dat = Trog::Data->new(Trog::Config::get());
  440. _setup_initial_db( $dat, $query->{username}, $query->{display_name}, $query->{contact_email} );
  441. # Ensure we stop registering new users
  442. File::Touch::touch("config/has_users");
  443. }
  444. $query->{failed} = 1;
  445. my $cookie = Trog::Auth::mksession( $query->{username}, $query->{password}, $query->{token}, $query->{ip} // '' );
  446. if ($cookie) {
  447. # TODO secure / sameSite cookie to kill csrf, maybe do rememberme with Expires=~0
  448. my $secure = '';
  449. $secure = '; Secure' if $query->{scheme} eq 'https';
  450. $headers = {
  451. "Set-Cookie" => "tcmslogin=$cookie; HttpOnly; SameSite=Strict$secure",
  452. };
  453. $query->{failed} = 0;
  454. }
  455. }
  456. $query->{failed} //= -1;
  457. return Trog::Renderer->render(
  458. template => 'login.tx',
  459. data => {
  460. title => 'tCMS 2 ~ Login',
  461. to => $query->{to},
  462. failure => int( $query->{failed} ),
  463. message => int( $query->{failed} ) < 1 ? "Login Successful, Redirecting..." : "Login Failed.",
  464. btnmsg => $btnmsg,
  465. stylesheets => _build_themed_styles( [qw{structure.css screen.css login.css}] ),
  466. theme_dir => Trog::Themes::td(),
  467. has_users => $hasusers,
  468. %$query,
  469. },
  470. headers => $headers,
  471. contenttype => 'text/html',
  472. code => 200,
  473. nocache => $query->{nocache},
  474. );
  475. }
  476. sub _setup_initial_db ( $dat, $user, $display_name, $contact_email ) {
  477. $dat->add(
  478. {
  479. "aclname" => "series",
  480. "acls" => [],
  481. "callback" => "Trog::Routes::HTML::series",
  482. method => 'GET',
  483. "data" => "Series",
  484. "href" => "/series",
  485. "local_href" => "/series",
  486. "preview" => "/img/sys/testpattern.jpg",
  487. "tags" => [qw{series topbar}],
  488. visibility => 'public',
  489. "title" => "Series",
  490. user => $user,
  491. form => 'series.tx',
  492. child_form => 'series.tx',
  493. aliases => [],
  494. },
  495. {
  496. "aclname" => "about",
  497. "acls" => [],
  498. "callback" => "Trog::Routes::HTML::series",
  499. method => 'GET',
  500. "data" => "About",
  501. "href" => "/about",
  502. "local_href" => "/about",
  503. "preview" => "/img/sys/testpattern.jpg",
  504. "tags" => [qw{series topbar public}],
  505. visibility => 'public',
  506. "title" => "About",
  507. user => $user,
  508. form => 'series.tx',
  509. child_form => 'profile.tx',
  510. aliases => [],
  511. },
  512. {
  513. "aclname" => "config",
  514. acls => [],
  515. "callback" => "Trog::Routes::HTML::config",
  516. 'method' => 'GET',
  517. "content_type" => "text/html",
  518. "data" => "Config",
  519. "href" => "/config",
  520. "local_href" => "/config",
  521. "preview" => "/img/sys/testpattern.jpg",
  522. "tags" => [qw{admin}],
  523. visibility => 'private',
  524. "title" => "Configure tCMS",
  525. user => $user,
  526. aliases => [],
  527. },
  528. {
  529. title => $display_name,
  530. data => 'Default user',
  531. preview => '/img/avatar/humm.gif',
  532. wallpaper => '/img/sys/testpattern.jpg',
  533. tags => ['about'],
  534. visibility => 'public',
  535. acls => ['admin'],
  536. local_href => "/users/$display_name",
  537. display_name => $display_name,
  538. contact_email => $contact_email,
  539. callback => "Trog::Routes::HTML::users",
  540. method => 'GET',
  541. user => $user,
  542. form => 'profile.tx',
  543. aliases => [],
  544. },
  545. );
  546. }
  547. =head2 logout
  548. Deletes your users' session and opens the index.
  549. =cut
  550. sub logout ($query) {
  551. Trog::Auth::killsession( $query->{user}, $query->{ip} // '' ) if $query->{user};
  552. delete $query->{user};
  553. return Trog::Routes::HTML::index($query);
  554. }
  555. =head2 config
  556. Renders the configuration page, or redirects you back to the login page.
  557. =cut
  558. sub config ( $query = {} ) {
  559. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  560. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  561. $query->{failure} //= -1;
  562. #XXX ACHTUNG config::simple has this brain damaged behavior of returning a multiple element array when you access something that does not exist.
  563. #XXX straight up dying would be preferrable.
  564. #XXX anyways, this means you can NEVER NEVER NEVER access a param from within a hash directly. YOU HAVE BEEN WARNED!
  565. my $conf = Trog::Config::get();
  566. state $theme = $conf->param('general.theme') // '';
  567. state $dm = $conf->param('general.data_model') // 'DUMMY';
  568. state $embeds = $conf->param('security.allow_embeds_from') // '';
  569. state $hostname = $conf->param('general.hostname') // '';
  570. return Trog::Routes::HTML::index(
  571. {
  572. title => 'Configure tCMS',
  573. theme_dir => Trog::Themes::td(),
  574. stylesheets => [qw{config.css}],
  575. scripts => [qw{post.js}],
  576. themes => _get_themes() || [],
  577. data_models => _get_data_models(),
  578. current_theme => $theme,
  579. current_data_model => $dm,
  580. message => $query->{message},
  581. failure => $query->{failure},
  582. to => '/config',
  583. scheme => $query->{scheme},
  584. embeds => $embeds,
  585. is_admin => 1,
  586. template => 'config.tx',
  587. %$query,
  588. hostname => $hostname,
  589. },
  590. undef,
  591. [qw{config.css}],
  592. );
  593. }
  594. =head2 resetpass
  595. =head2 do_resetpass
  596. =head2 do_totp_clear
  597. Routes for user service of their authentication details.
  598. =cut
  599. sub resetpass ($query) {
  600. $query->{failure} //= -1;
  601. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  602. return Trog::Routes::HTML::index(
  603. {
  604. title => 'Request Authentication Resets',
  605. theme_dir => Trog::Themes::td(),
  606. stylesheets => [qw{config.css}],
  607. scripts => [qw{post.js}],
  608. message => $query->{message},
  609. failure => $query->{failure},
  610. scheme => $query->{scheme},
  611. template => 'resetpass.tx',
  612. is_admin => $is_admin,
  613. %$query,
  614. },
  615. undef,
  616. [qw{config.css}],
  617. );
  618. }
  619. sub do_resetpass ($query) {
  620. my $user = $query->{username};
  621. # User Does not exist
  622. return $query->{tpsgi}->forbidden($query) if !Trog::Auth::user_exists($user);
  623. # User exists, but is not logged in this session
  624. return $query->{tpsgi}->forbidden($query) if !$query->{user} && Trog::Auth::user_has_session($user);
  625. my $token = Trog::Utils::uuid();
  626. my $newpass = $query->{password} // Trog::Utils::uuid();
  627. my $res = Trog::Auth::add_change_request( type => 'reset_pass', user => $user, secret => $newpass, token => $token );
  628. die "Could not add auth change request!" unless $res;
  629. # If the user is logged in, just do the deed, otherwise send them the token in an email
  630. if ( $query->{user} ) {
  631. return $query->{tpsgi}->see_also("/api/auth_change_request/$token");
  632. }
  633. Trog::Email::contact(
  634. $user,
  635. "root\@$query->{domain}",
  636. "$query->{domain}: Password reset URL for $user",
  637. { uri => "$query->{scheme}://$query->{domain}/api/auth_change_request/$token", template => 'password_reset.tx' }
  638. );
  639. return $query->{tpsgi}->see_also("/processed");
  640. }
  641. sub do_totp_clear ($query) {
  642. my $user = $query->{username};
  643. # User Does not exist
  644. return $query->{tpsgi}->forbidden($query) if !Trog::Auth::user_exists($user);
  645. # User exists, but is not logged in this session
  646. return $query->{tpsgi}->forbidden($query) if !$query->{user} && Trog::Auth::user_has_session($user);
  647. my $token = Trog::Utils::uuid();
  648. my $res = Trog::Auth::add_change_request( type => 'clear_totp', user => $user, token => $token );
  649. die "Could not add auth change request!" unless $res;
  650. # If the user is logged in, just do the deed, otherwise send them the token in an email
  651. if ( $query->{user} ) {
  652. return $query->{tpsgi}->see_also("/api/auth_change_request/$token");
  653. }
  654. Trog::Email::contact(
  655. $user,
  656. "root\@$query->{domain}",
  657. "$query->{domain}: Password reset URL for $user",
  658. { uri => "$query->{scheme}://$query->{domain}/api/auth_change_request/$token", template => 'totp_reset.tx' }
  659. );
  660. return $query->{tpsgi}->see_also("/processed");
  661. }
  662. sub _get_series ( $edit = 0 ) {
  663. state $data;
  664. $data //= Trog::Data->new(Trog::Config::get());
  665. my @series = $data->get(
  666. acls => [qw{public}],
  667. tags => [qw{topbar}],
  668. limit => 10,
  669. page => 1,
  670. );
  671. @series = map { $_->{local_href} = "/post$_->{local_href}"; $_ } @series if $edit;
  672. return @series;
  673. }
  674. sub _get_themes {
  675. my $dir = 'www/themes';
  676. opendir( my $dh, $dir ) || do { die "Can't opendir $dir: $!" unless $!{ENOENT} };
  677. my @tdirs = grep { !/^\./ && -d "$dir/$_" } readdir($dh);
  678. closedir $dh;
  679. return \@tdirs;
  680. }
  681. sub _get_data_models {
  682. foreach my $incdir (@INC) {
  683. my $dir = "$incdir/Trog/Data";
  684. next unless -d $dir;
  685. opendir( my $dh, $dir ) || die "Can't opendir $dir: $!";
  686. my @dmods = map { s/\.pm$//g; $_ } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh);
  687. closedir $dh;
  688. return \@dmods;
  689. }
  690. die "Could not find tCMS data modules! Is tCMS in \@INC?";
  691. }
  692. =head2 config_save
  693. Implements /config/save route. Saves what little configuration we actually use to ~/.tcms/tcms.conf
  694. =cut
  695. sub config_save ($query) {
  696. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  697. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  698. my $conf = Trog::Config::get();
  699. $conf->param( 'general.theme', $query->{theme} ) if defined $query->{theme};
  700. $conf->param( 'general.data_model', $query->{data_model} ) if $query->{data_model};
  701. $conf->param( 'security.allow_embeds_from', $query->{embeds} ) if $query->{embeds};
  702. $conf->param( 'general.hostname', $query->{hostname} ) if $query->{hostname};
  703. $query->{failure} = 1;
  704. $query->{message} = "Failed to save configuration!";
  705. if ( $conf->write($Trog::Config::home_cfg) ) {
  706. $query->{failure} = 0;
  707. $query->{message} = "Configuration updated succesfully.";
  708. }
  709. #We need to reap the children with outdated configuration.
  710. $query->{tpsgi}->signal_restart_parent();
  711. return config($query);
  712. }
  713. =head2 themeclone
  714. Clone a theme by copying a directory.
  715. =cut
  716. sub themeclone ($query) {
  717. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  718. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  719. my ( $theme, $newtheme ) = ( $query->{theme}, $query->{newtheme} );
  720. my $themedir = 'www/themes';
  721. $query->{failure} = 1;
  722. $query->{message} = "Failed to clone theme '$theme' as '$newtheme'!";
  723. require File::Copy::Recursive;
  724. if ( $theme && $newtheme && File::Copy::Recursive::dircopy( "$themedir/$theme", "$themedir/$newtheme" ) ) {
  725. $query->{failure} = 0;
  726. $query->{message} = "Successfully cloned theme '$theme' as '$newtheme'.";
  727. }
  728. return $query->{tpsgi}->see_also('/config');
  729. }
  730. =head2 post_save
  731. Saves posts submitted via the /post pages
  732. =cut
  733. sub post_save ($qq) {
  734. return $qq->{tpsgi}->see_also('/login') unless $qq->{user};
  735. return $qq->{tpsgi}->forbidden($qq) unless grep { $_ eq 'admin' } @{ $qq->{user_acls} };
  736. my $query = clone($qq);
  737. my $to = delete $query->{to};
  738. #Copy this down since it will be deleted later
  739. my $acls = $query->{acls};
  740. $query->{tags} = Trog::Utils::coerce_array( $query->{tags} );
  741. # Support data with multiple pages like presentations
  742. $query->{data} = Trog::Utils::coerce_array( $query->{data} ) if $query->{data_is_array};
  743. $query->{attachments} = Trog::Utils::coerce_array( $query->{attachments} );
  744. # Filter bits and bobs XXX this is done deeper in, may require removal
  745. delete $query->{primary_post};
  746. delete $query->{social_meta};
  747. delete $query->{deflate};
  748. delete $query->{acls};
  749. # Ensure there are no null tags
  750. @{ $query->{tags} } = grep { defined $_ } @{ $query->{tags} };
  751. # Posts will always be GET
  752. $query->{method} = 'GET';
  753. state $data;
  754. $data //= Trog::Data->new(Trog::Config::get());
  755. $data->add($query) and die "Could not add post";
  756. # Instruct tpsgi to invalidate the cached render.
  757. $qq->{tpsgi}->add_post_close_callback(sub {
  758. # XXX there is not a great way to find what needs to be re-rendered because posts can include other posts.
  759. # As such we just have to nuke all the .html renders.
  760. $qq->{tpsgi}->invalidate_renders('html');
  761. });
  762. # Force a reload of the routing table
  763. $qq->{tpsgi}->signal_restart_parent();
  764. return $qq->{tpsgi}->see_also('/secure'.$to);
  765. }
  766. =head2 profile
  767. Saves / updates new users.
  768. =cut
  769. sub profile ($query) {
  770. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  771. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  772. # Find the user's post and edit it
  773. state $data;
  774. $data //= Trog::Data->new(Trog::Config::get());
  775. my @userposts = $data->get( tags => ['about'], acls => [qw{admin}] );
  776. # Users are always self-authored, you see
  777. my $user_obj = List::Util::first { ( $_->{user} || '' ) eq $query->{username} } @userposts;
  778. if ( $query->{username} ne $user_obj->{user} || $query->{password} || $query->{contact_email} ne $user_obj->{contact_email} || $query->{display_name} ne $user_obj->{display_name} ) {
  779. my $for_user = Trog::Auth::acls4user( $query->{username} );
  780. #TODO support non-admin users
  781. my @acls = @$for_user ? @$for_user : qw{admin};
  782. Trog::Auth::useradd( $query->{username}, $query->{display_name}, $query->{password}, \@acls, $query->{contact_email} );
  783. }
  784. #Make sure it is "self-authored", redact pw
  785. $query->{user} = delete $query->{username};
  786. delete $query->{password};
  787. # Use the display name as the title
  788. $query->{title} = $query->{display_name};
  789. my %merged = (
  790. %$user_obj,
  791. %$query,
  792. $query->{display_name} ? ( local_href => "/users/$query->{display_name}" ) : ( local_href => $user_obj->{local_href} ),
  793. );
  794. return post_save( \%merged );
  795. }
  796. =head2 post_delete
  797. deletes posts.
  798. =cut
  799. sub post_delete ($query) {
  800. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  801. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  802. state $data;
  803. $data //= Trog::Data->new(Trog::Config::get());
  804. $data->delete($query) and die "Could not delete post";
  805. return $query->{tpsgi}->see_also( $query->{to} );
  806. }
  807. my %enrich = (
  808. 'invoice.tx' => sub {
  809. my ($post,$query) = @_;
  810. # TODO it's probably a performance problem doing this per post rather than only for the series entry
  811. my %ret = ( entities => [ _post_helper({ form => 'entities.tx', visibility => 'public' }, [], $query->{user_acls}) ] );
  812. return %ret unless ref $post->{data} eq 'ARRAY';
  813. # Figure out the payee/payor
  814. $ret{payee_entity} = List::Util::first { $_->{id} eq $post->{payee} } @{$ret{entities}};
  815. $ret{payor_entity} = List::Util::first { $_->{id} eq $post->{payor} } @{$ret{entities}};
  816. # Build total
  817. my $denom;
  818. my @prices = map {
  819. my $price = 0;
  820. ($denom, $price) = m/^Price:\s*(\D)(\d+)/mix;
  821. $price;
  822. } @{$post->{data}};
  823. $denom //= '$';
  824. $ret{total} = $denom.(List::Util::sum(@prices) // 0);
  825. return %ret;
  826. },
  827. );
  828. =head2 series
  829. Series specific view, much like the users/ route
  830. Displays identified series, not all series.
  831. =cut
  832. sub series ($query) {
  833. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  834. # rip away /secure if present
  835. $query->{route} =~ s|^/secure||;
  836. #we are either viewed one of two ways, /post/$id or /$aclname
  837. my ( undef, $aclname, $id ) = split( /\//, $query->{route} );
  838. $query->{aclname} = $aclname if !$id;
  839. $query->{id} = $id if $id;
  840. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  841. $query->{exclude_tags} = ['topbar'] if !$is_admin && $aclname && $aclname eq 'series';
  842. #XXX I'd prefer to overload id to actually *be* the aclname...
  843. # but this way, accomodates things like the flat file time-indexing hack.
  844. # TODO I should probably have it for all posts, and make *everything* a series.
  845. # WE can then do threaded comments/posts.
  846. # That will essentially necessitate it *becoming* the ID for real.
  847. #Grab the relevant tag (aclname), then pass that to posts
  848. my @posts = _post_helper( $query, ['series'], $query->{user_acls} );
  849. # Enrich posts with additional data based on the template if necessary
  850. if (exists $enrich{$posts[0]->{child_form}}) {
  851. my $callback = $enrich{$posts[0]->{child_form}};
  852. my %extra_data = $callback->($posts[0], $query);
  853. foreach my $key (keys(%extra_data)) {
  854. $posts[0]->{$key} = $extra_data{$key};
  855. }
  856. }
  857. delete $query->{id};
  858. delete $query->{aclname};
  859. $query->{subhead} = $posts[0]->{data};
  860. $query->{title} = $posts[0]->{title};
  861. $query->{tag} = $posts[0]->{aclname};
  862. $query->{primary_post} = $posts[0];
  863. $query->{in_series} = 1;
  864. return posts($query);
  865. }
  866. =head2 avatars
  867. Returns the avatars.css.
  868. =cut
  869. sub avatars ($query) {
  870. push( @{ $query->{user_acls} }, 'public' );
  871. my $tags = Trog::Utils::coerce_array( $query->{tag} );
  872. my @posts = _post_helper( $query, $tags, $query->{user_acls} );
  873. if (@posts) {
  874. # Set the eTag so that we don't get a re-fetch
  875. $query->{etag} = "$posts[0]{id}-$posts[0]{version}";
  876. }
  877. @posts = map { $_->{id} =~ tr/-/_/; $_->{id} = "a_$_->{id}"; $_ } @posts;
  878. return Trog::Renderer->render(
  879. template => 'avatars.tx',
  880. data => {
  881. users => \@posts,
  882. %$query,
  883. },
  884. code => 200,
  885. contenttype => 'text/css',
  886. );
  887. }
  888. =head2 libravatar
  889. Implements the libravatar protocol (https://wiki.libravatar.org/running_your_own/).
  890. Accepts GET /avatar/{hash} where {hash} is the MD5 or SHA-256 of the user's
  891. lowercased email address, and redirects to their profile avatar image.
  892. Returns 404 when no matching user is found or the user has no avatar.
  893. =cut
  894. sub libravatar ($query) {
  895. my $hash = lc( $query->{avatar_hash} // '' );
  896. my $users = Trog::Auth::users_with_emails();
  897. my $matched_user;
  898. for my $u (@$users) {
  899. next unless $u->{contact_email};
  900. my $email = lc( $u->{contact_email} );
  901. if ( $hash eq sha256_hex($email) || $hash eq md5_hex($email) ) {
  902. $matched_user = $u->{name};
  903. last;
  904. }
  905. }
  906. return $query->{tpsgi}->notfound($query) unless $matched_user;
  907. my @posts = _post_helper( { author => $matched_user }, ['about'], [qw{public admin}] );
  908. my $preview = @posts ? $posts[0]->{preview} : '';
  909. return $query->{tpsgi}->notfound($query) unless $preview;
  910. return $query->{tpsgi}->redirect_permanent("/$preview");
  911. }
  912. =head2 users
  913. Implements direct user profile view.
  914. =cut
  915. sub users ($query) {
  916. # rip away /secure if present
  917. $query->{route} =~ s|^/secure||;
  918. # Capture the username
  919. my ( undef, undef, $display_name ) = split( /\//, $query->{route} );
  920. $display_name = URI::Escape::uri_unescape($display_name);
  921. my $username = Trog::Auth::display2username($display_name);
  922. return $query->{tpsgi}->notfound($query) unless $username;
  923. $query->{username} //= $username;
  924. push( @{ $query->{user_acls} }, 'public' );
  925. $query->{exclude_tags} = ['about'];
  926. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  927. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  928. push( @{ $query->{exclude_tags} }, 'topbar' ) if !$is_admin;
  929. my @posts = _post_helper( { author => $query->{username} }, ['about'], $query->{user_acls} );
  930. $query->{id} = $posts[0]->{id};
  931. $query->{title} = $posts[0]->{display_name};
  932. $posts[0]->{title} = $posts[0]->{display_name};
  933. $query->{user_obj} = $posts[0];
  934. $query->{primary_post} = $posts[0];
  935. $query->{in_series} = 1;
  936. return posts($query);
  937. }
  938. =head2 posts
  939. Display multi or single posts, supports RSS and pagination.
  940. =cut
  941. sub posts ( $query, $direct = 0 ) {
  942. # Allow rss.xml to tell what posts to loop over
  943. my $fmt = $query->{format} || '';
  944. #Process the input URI to capture tag/id
  945. $query->{route} //= $query->{to};
  946. # rip away /secure if present
  947. $query->{route} =~ s|^/secure||;
  948. my ( undef, undef, $id ) = split( /\//, $query->{route} );
  949. my $tags = Trog::Utils::coerce_array( $query->{tag} );
  950. $query->{id} = $id if $id && !$query->{in_series};
  951. my $is_admin = grep { $_ eq 'admin' } @{ $query->{user_acls} };
  952. push( @{ $query->{user_acls} }, 'public' );
  953. push( @{ $query->{user_acls} }, 'unlisted' ) if $query->{id};
  954. push( @{ $query->{user_acls} }, 'private' ) if $is_admin;
  955. my @posts;
  956. # Discover this user's visibility, so we can make them post in this category by default
  957. my $user_visibility = 'public';
  958. if ( $query->{user_obj} ) {
  959. #Optimize the /users/* route
  960. @posts = ( $query->{user_obj} );
  961. $user_visibility = $query->{user_obj}->{visibility};
  962. }
  963. else {
  964. if ( $query->{user} ) {
  965. my @me = _post_helper( { author => $query->{user} }, ['about'], $query->{user_acls} );
  966. $user_visibility = $me[0]->{visibility};
  967. }
  968. @posts = _post_helper( $query, $tags, $query->{user_acls} );
  969. }
  970. if ( $query->{id} ) {
  971. $query->{primary_post} = $posts[0] if @posts;
  972. }
  973. #OK, so if we have a user as the ID we found, go grab the rest of their posts
  974. if ( $query->{id} && @posts && List::Util::any { $_ eq 'about' } @{ $posts[0]->{tags} } ) {
  975. my $user = shift(@posts);
  976. my $id = delete $query->{id};
  977. $query->{author} = $user->{user};
  978. @posts = _post_helper( $query, $tags, $query->{user_acls} );
  979. @posts = grep { $_->{id} ne $id } @posts;
  980. unshift @posts, $user;
  981. }
  982. if ( !$is_admin ) {
  983. return $query->{tpsgi}->notfound($query) unless @posts;
  984. }
  985. # Set the eTag so that we don't get a re-fetch
  986. $query->{etag} = "$posts[0]{id}-$posts[0]{version}" if @posts;
  987. #Correct page headers
  988. my $ph = $themed ? _themed_title( $query->{route} ) : $query->{route};
  989. return _rss( $query, $ph, \@posts ) if $fmt eq 'rss';
  990. #XXX Is used by the sitemap, maybe just fix there?
  991. my @post_aliases = map { $_->{local_href} } _get_series();
  992. # Allow themes to put in custom headers/footers on posts
  993. my ( $header, $footer );
  994. $header = Trog::Renderer->render(
  995. template => 'headers/' . $query->{primary_post}{header},
  996. data => { theme_dir => Trog::Themes::td(), %$query },
  997. component => 1,
  998. contenttype => 'text/html',
  999. ) if $query->{primary_post}{header};
  1000. return $header if ref $header eq 'ARRAY';
  1001. $footer = Trog::Renderer->render(
  1002. template => 'footers/' . $query->{primary_post}{footer},
  1003. data => { theme_dir => Trog::Themes::td(), %$query },
  1004. component => 1,
  1005. contenttype => 'text/html',
  1006. ) if $query->{primary_post}{footer};
  1007. return $header if ref $footer eq 'ARRAY';
  1008. # List the available headers/footers
  1009. my $headers = Trog::Themes::templates_in_dir( "headers", 'text/html', 1 );
  1010. my $footers = Trog::Themes::templates_in_dir( "footers", 'text/html', 1 );
  1011. #XXX used to be post.css, but probably not good anymore?
  1012. my $styles = [];
  1013. # Build page title if it wasn't set by a wrapping sub
  1014. $query->{title} = "$query->{domain} : $query->{title}" if $query->{title} && $query->{domain};
  1015. $query->{title} ||= @$tags && $query->{domain} ? "$query->{domain} : @$tags" : undef;
  1016. #Handle paginator vars
  1017. $query->{limit} ||= 25;
  1018. my $limit = int( $query->{limit} );
  1019. my $now_year = ( localtime(time) )[5] + 1900;
  1020. my $oldest_year = $now_year - 20; #XXX actually find oldest post year
  1021. # Handle post style.
  1022. if ( $query->{style} ) {
  1023. undef $header;
  1024. undef $footer;
  1025. }
  1026. my $older = !@posts ? 0 : $posts[-1]->{created};
  1027. $query->{failure} //= -1;
  1028. $query->{id} //= '';
  1029. my $newer = !@posts ? 0 : $posts[0]->{created};
  1030. #XXX messed up data has to be fixed unfortunately
  1031. @$tags = List::Util::uniq @$tags;
  1032. #Filter displaying visibility tags
  1033. my @visibuddies = qw{public unlisted private};
  1034. foreach my $post (@posts) {
  1035. @{ $post->{tags} } = grep {
  1036. my $tag = $_;
  1037. !grep { $tag eq $_ } @visibuddies
  1038. } @{ $post->{tags} };
  1039. }
  1040. #XXX note that we are explicitly relying on the first tag to be the ACL
  1041. my $aclselected = $tags->[0] || '';
  1042. my @acls = map {
  1043. $_->{selected} = $_->{aclname} eq $aclselected ? 'selected' : '';
  1044. $_
  1045. } _post_helper( {}, ['series'], $query->{user_acls} );
  1046. my $forms = Trog::Themes::templates_in_dir( "forms", 'text/html', 1 );
  1047. my $edittype = $query->{primary_post} ? $query->{primary_post}->{child_form} : $query->{form};
  1048. my $tiled = $query->{primary_post} ? !$is_admin && $query->{primary_post}->{tiled} : 0;
  1049. state $data;
  1050. $data //= Trog::Data->new(Trog::Config::get());
  1051. # Grab the rest of the tags to dump into the edit form
  1052. my @tags_all = $data->tags();
  1053. #Filter out the visibilities and special series tags
  1054. @tags_all = grep {
  1055. my $subj = $_;
  1056. scalar( grep { $_ eq $subj } qw{public private unlisted admin series about topbar} ) == 0
  1057. } @tags_all;
  1058. @posts = map {
  1059. my $subject = $_;
  1060. my @et = grep {
  1061. my $subj = $_;
  1062. grep { $subj eq $_ } @tags_all
  1063. } @{ $subject->{tags} };
  1064. @et = grep { $_ ne $aclselected } @et;
  1065. $_->{extra_tags} = \@et;
  1066. $_
  1067. } @posts;
  1068. my @et = List::MoreUtils::singleton( @$tags, @tags_all );
  1069. $query->{author} = $query->{primary_post}{user} // $posts[0]{user};
  1070. my $picker = Trog::Component::EmojiPicker::render();
  1071. return $picker if ref $picker eq 'ARRAY';
  1072. # Enrich posts with additional data based on the template if necessary
  1073. foreach my $poast (@posts) {
  1074. if (exists $enrich{$poast->{form}}) {
  1075. my $callback = $enrich{$poast->{form}};
  1076. my %extra_data = $callback->($poast, $query);
  1077. foreach my $key (keys(%extra_data)) {
  1078. $poast->{$key} = $extra_data{$key};
  1079. }
  1080. }
  1081. }
  1082. #XXX the only reason this is needed is due to direct=1
  1083. #XXX is this even used?
  1084. my $content = Trog::Renderer->render(
  1085. template => 'posts.tx',
  1086. data => {
  1087. acls => \@acls,
  1088. can_edit => $is_admin,
  1089. forms => $forms,
  1090. post => { tags => $tags, extra_tags => \@et, form => $edittype, visibility => $user_visibility, addpost => 1 },
  1091. post_visibilities => \@visibuddies,
  1092. failure => $query->{failure},
  1093. to => $query->{to},
  1094. message => $query->{failure} ? "Failed to add post!" : "Successfully added Post as $query->{id}",
  1095. direct => $direct,
  1096. title => $query->{title},
  1097. author => $query->{primary_post}{user} // $posts[0]{user},
  1098. primary_post => $query->{primary_post},
  1099. style => $query->{style},
  1100. posts => \@posts,
  1101. like => $query->{like},
  1102. in_series => exists $query->{in_series} || !!( $query->{route} =~ m/^\/series\// ),
  1103. route => $query->{route},
  1104. limit => $limit,
  1105. pages => scalar(@posts) == $limit,
  1106. older => $older,
  1107. newer => $newer,
  1108. sizes => [ 25, 50, 100 ],
  1109. rss => !$query->{id} && !$query->{older},
  1110. tiled => $tiled,
  1111. category => $ph,
  1112. subhead => $query->{subhead},
  1113. header => $header,
  1114. footer => $footer,
  1115. headers => $headers,
  1116. footers => $footers,
  1117. years => [ reverse( $oldest_year .. $now_year ) ],
  1118. months => [ 0 .. 11 ],
  1119. emoji_picker => $picker,
  1120. embed => $query->{embed},
  1121. nochrome => $query->{nochrome},
  1122. },
  1123. contenttype => 'text/html',
  1124. component => 1,
  1125. );
  1126. # Something exploded
  1127. return $content if ref $content eq "ARRAY";
  1128. return $content if $direct;
  1129. return Trog::Routes::HTML::index( $query, $content, $styles );
  1130. }
  1131. sub _themed_title ($path) {
  1132. return $path unless %Theme::paths;
  1133. return $Theme::paths{$path} ? $Theme::paths{$path} : $path;
  1134. }
  1135. sub _post_helper ( $query, $tags, $acls ) {
  1136. state $data;
  1137. $data //= Trog::Data->new(Trog::Config::get());
  1138. $query->{page} ||= 1;
  1139. $query->{limit} ||= 25;
  1140. my @d = $data->get(
  1141. older => $query->{older},
  1142. newer => $query->{newer},
  1143. page => int( $query->{page} ),
  1144. limit => int( $query->{limit} ),
  1145. tags => $tags,
  1146. exclude_tags => $query->{exclude_tags},
  1147. acls => $acls,
  1148. aclname => $query->{aclname},
  1149. like => $query->{like},
  1150. author => $query->{author},
  1151. id => $query->{id},
  1152. version => $query->{version},
  1153. form => $query->{form},
  1154. );
  1155. return map {
  1156. my $subj = $_;
  1157. $subj->{local_href} = "/secure$_->{local_href}";
  1158. $subj
  1159. } @d if $query->{user};
  1160. return @d;
  1161. }
  1162. =head2 sitemap
  1163. Return the sitemap index unless the static or a set of dynamic routes is requested.
  1164. We have a maximum of 99,990,000 posts we can make under this model
  1165. As we have 10,000 * 10,000 posts which are indexable via the sitemap format.
  1166. 1 top level index slot (10k posts) is taken by our static routes, the rest will be /posts.
  1167. Passing ?xml=1 will result in an appropriate sitemap.xml instead.
  1168. This is used to generate the static sitemaps as expected by search engines.
  1169. Passing compressed=1 will gzip the output.
  1170. =cut
  1171. sub sitemap ($query) {
  1172. state $data;
  1173. $data //= Trog::Data->new(Trog::Config::get());
  1174. state $etag = "sitemap-" . time();
  1175. my ( @to_map, $is_index, $route_type );
  1176. my $warning = '';
  1177. $query->{map} //= '';
  1178. if ( $query->{map} eq 'static' ) {
  1179. # Return the map of static routes
  1180. $route_type = 'Static Routes';
  1181. @to_map = grep { !defined $routes{$_}->{captures} && !$routes{$_}->{auth} && !$routes{$_}->{noindex} && !$routes{$_}->{nomap} } keys(%routes);
  1182. }
  1183. elsif ( !$query->{map} ) {
  1184. # Return the index instead
  1185. @to_map = ('static');
  1186. my $tot = $data->count();
  1187. my $size = 50000;
  1188. my $pages = int( $tot / $size ) + ( ( $tot % $size ) ? 1 : 0 );
  1189. # Truncate pages at 10k due to standard
  1190. my $clamped = $pages > 49999 ? 49999 : $pages;
  1191. $warning = "More posts than possible to represent in sitemaps & index! Old posts have been truncated." if $pages > 49999;
  1192. foreach my $page ( $clamped .. 1 ) {
  1193. push( @to_map, "$page" );
  1194. }
  1195. $is_index = 1;
  1196. }
  1197. else {
  1198. $route_type = "Posts: Page $query->{map}";
  1199. # Return the map of the particular range of dynamic posts
  1200. $query->{limit} = 50000;
  1201. $query->{page} = $query->{map};
  1202. @to_map = _post_helper( $query, [], ['public'] );
  1203. }
  1204. if ( $query->{xml} ) {
  1205. DEBUG("RENDER SITEMAP XML");
  1206. my $sm;
  1207. my $xml_date = time();
  1208. my $fmt = "xml";
  1209. $fmt .= ".gz" if $query->{compressed};
  1210. if ( !$query->{map} ) {
  1211. require WWW::SitemapIndex::XML;
  1212. $sm = WWW::SitemapIndex::XML->new();
  1213. foreach my $url (@to_map) {
  1214. $sm->add(
  1215. loc => "http://$query->{domain}/sitemap/$url.$fmt",
  1216. lastmod => $xml_date,
  1217. );
  1218. }
  1219. }
  1220. else {
  1221. require WWW::Sitemap::XML;
  1222. $sm = WWW::Sitemap::XML->new();
  1223. my $changefreq = $query->{map} eq 'static' ? 'monthly' : 'daily';
  1224. foreach my $url (@to_map) {
  1225. my $true_uri = "http://$query->{domain}$url";
  1226. if ( ref $url eq 'HASH' ) {
  1227. my $is_user_page = grep { $_ eq 'about' } @{ $url->{tags} };
  1228. $true_uri = "http://$query->{domain}/posts/$url->{id}";
  1229. $true_uri = "http://$query->{domain}/users/$url->{title}" if $is_user_page;
  1230. }
  1231. my %out = (
  1232. loc => $true_uri,
  1233. lastmod => $xml_date,
  1234. mobile => 1,
  1235. changefreq => $changefreq,
  1236. priority => 1.0,
  1237. );
  1238. if ( ref $url eq 'HASH' ) {
  1239. #add video & preview image if applicable
  1240. $out{images} = [
  1241. {
  1242. loc => "http://$query->{domain}$url->{href}",
  1243. caption => $url->{data},
  1244. title => substr( $url->{title}, 0, 100 ),
  1245. }
  1246. ]
  1247. if $url->{is_image};
  1248. # Truncate descriptions
  1249. my $desc = substr( $url->{data}, 0, 2048 ) || '';
  1250. my $href = $url->{href} || '';
  1251. my $preview = $url->{preview} || '';
  1252. my $domain = $query->{domain} || '';
  1253. $out{videos} = [
  1254. {
  1255. content_loc => "http://$domain$href",
  1256. thumbnail_loc => "http://$domain$preview",
  1257. title => substr( $url->{title}, 0, 100 ) || '',
  1258. description => $desc,
  1259. }
  1260. ]
  1261. if $url->{is_video};
  1262. }
  1263. $sm->add(%out);
  1264. }
  1265. }
  1266. my $xml = $sm->as_xml();
  1267. require IO::String;
  1268. my $buf = IO::String->new();
  1269. my $ct = 'application/xml';
  1270. $xml->toFH( $buf, 0 );
  1271. seek $buf, 0, 0;
  1272. if ( $query->{compressed} ) {
  1273. require IO::Compress::Gzip;
  1274. my $compressed = IO::String->new();
  1275. IO::Compress::Gzip::gzip( $buf => $compressed );
  1276. $ct = 'application/gzip';
  1277. $buf = $compressed;
  1278. seek $compressed, 0, 0;
  1279. }
  1280. #XXX This is one of the few exceptions where we don't use finish_render, as it *requires* gzip.
  1281. return [ 200, [ "Content-type" => $ct, 'ETag' => $etag ], $buf ];
  1282. }
  1283. @to_map = sort @to_map unless $is_index;
  1284. my $styles = ['sitemap.css'];
  1285. $query->{title} = "$query->{domain} : Sitemap";
  1286. $query->{template} = 'sitemap.tx',
  1287. $query->{to_map} = \@to_map,
  1288. $query->{is_index} = $is_index,
  1289. $query->{route_type} = $route_type,
  1290. $query->{etag} = $etag;
  1291. return Trog::Routes::HTML::index( $query, undef, $styles );
  1292. }
  1293. sub _rss ( $query, $subtitle, $posts ) {
  1294. require XML::RSS;
  1295. my $rss = XML::RSS->new( version => '2.0', stylesheet => '/styles/rss-style.xsl' );
  1296. my $now = DateTime->from_epoch( epoch => time() );
  1297. my $port = $query->{port} ? ":$query->{port}" : '';
  1298. $rss->channel(
  1299. title => "$query->{domain}",
  1300. subtitle => $subtitle,
  1301. link => "http://$query->{domain}$port/$query->{route}?format=xml",
  1302. language => 'en', #TODO localization
  1303. description => "$query->{domain} : $query->{route}",
  1304. pubDate => $now,
  1305. lastBuildDate => $now,
  1306. );
  1307. $rss->image(
  1308. title => $query->{domain},
  1309. url => "/favicon.ico",
  1310. link => "http://$query->{domain}$port",
  1311. width => 32,
  1312. height => 32,
  1313. description => "$query->{domain} favicon",
  1314. );
  1315. foreach my $post (@$posts) {
  1316. my $url = "http://$query->{domain}$port$post->{local_href}";
  1317. _post2rss( $rss, $url, $post );
  1318. next unless ref $post->{aliases} eq 'ARRAY';
  1319. foreach my $alias ( @{ $post->{aliases} } ) {
  1320. $url = "http://$query->{domain}$port$alias";
  1321. _post2rss( $rss, $url, $post );
  1322. }
  1323. }
  1324. return Trog::Renderer->render(
  1325. template => 'raw.tx',
  1326. data => {
  1327. etag => $query->{etag},
  1328. body => encode_utf8( $rss->as_string ),
  1329. scheme => $query->{scheme},
  1330. },
  1331. headers => { 'Content-Disposition' => 'inline; filename="rss.xml"' },
  1332. #XXX if you do the "proper" content-type of application/rss+xml, browsers download rather than display.
  1333. contenttype => "text/xml",
  1334. code => 200,
  1335. );
  1336. }
  1337. sub _post2rss ( $rss, $url, $post ) {
  1338. $rss->add_item(
  1339. title => $post->{title},
  1340. permaLink => $url,
  1341. link => $url,
  1342. enclosure => { url => $url, type => "text/html" },
  1343. description => "<![CDATA[$post->{data}]]>",
  1344. pubDate => DateTime->from_epoch( epoch => $post->{created} ), #TODO format like Thu, 23 Aug 1999 07:00:00 GMT
  1345. author => $post->{user}, #TODO translate to "email (user)" format
  1346. );
  1347. }
  1348. =head2 manual
  1349. Implements the /manual and /lib/* routes.
  1350. Basically a thin wrapper around Pod::Html.
  1351. =cut
  1352. sub manual ($query) {
  1353. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1354. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1355. require Pod::Html;
  1356. require Capture::Tiny;
  1357. #Fix links from Pod::HTML
  1358. $query->{module} =~ s/\.html$//g if $query->{module};
  1359. $query->{failure} //= -1;
  1360. my $infile = $query->{module} ? "$query->{module}.pm" : 'tCMS/Manual.pod';
  1361. my $found = 0;
  1362. foreach my $libdir (@INC) {
  1363. $found = $libdir if -f "$libdir/$infile";
  1364. last if $found;
  1365. }
  1366. return $query->{tpsgi}->notfound($query) unless $found;
  1367. my $content = capture { Pod::Html::pod2html( qw{--podpath=lib --podroot=.}, "--infile=$found/$infile" ) };
  1368. return Trog::Routes::HTML::index(
  1369. {
  1370. title => 'tCMS Manual',
  1371. theme_dir => Trog::Themes::td(),
  1372. content => $content,
  1373. template => 'manual.tx',
  1374. is_admin => 1,
  1375. %$query,
  1376. },
  1377. undef,
  1378. ['post.css'],
  1379. );
  1380. }
  1381. sub processed ($query) {
  1382. return Trog::Routes::HTML::index(
  1383. {
  1384. title => "Your request has been processed",
  1385. theme_dir => Trog::Themes::td(),
  1386. },
  1387. "Your request has been processed.<br /><br />You will recieve subsequent communications about this matter via means you have provided earlier.",
  1388. ['post.css']
  1389. );
  1390. }
  1391. sub metrics ($query) {
  1392. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1393. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1394. $query->{failure} //= -1;
  1395. return Trog::Routes::HTML::index(
  1396. {
  1397. title => 'tCMS Metrics',
  1398. theme_dir => Trog::Themes::td(),
  1399. template => 'metrics.tx',
  1400. is_admin => 1,
  1401. %$query,
  1402. },
  1403. undef,
  1404. ['post.css'],
  1405. ['chart.js'],
  1406. );
  1407. }
  1408. sub sessions ($query) {
  1409. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1410. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1411. my $limit = int( $query->{limit} // 100 );
  1412. my $events = Trog::Auth::audit_log(
  1413. limit => $limit,
  1414. username => $query->{filter_user} // '',
  1415. event_type => $query->{filter_event} // '',
  1416. );
  1417. # Format timestamps for display
  1418. @$events = map {
  1419. my $e = {%$_};
  1420. my @t = localtime( $e->{event_time} );
  1421. $e->{event_ts} = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0] );
  1422. $e->{session_short} = $e->{session_id} ? substr( $e->{session_id}, 0, 8 ) . '...' : '';
  1423. $e
  1424. } @$events;
  1425. return Trog::Routes::HTML::index(
  1426. {
  1427. title => 'tCMS Session Audit Log',
  1428. theme_dir => Trog::Themes::td(),
  1429. template => 'sessions.tx',
  1430. is_admin => 1,
  1431. events => $events,
  1432. limit => $limit,
  1433. filter_user => $query->{filter_user} // '',
  1434. filter_event => $query->{filter_event} // '',
  1435. %$query,
  1436. },
  1437. undef,
  1438. ['post.css'],
  1439. );
  1440. }
  1441. sub post_wizard ($query) {
  1442. return $query->{tpsgi}->see_also('/login') unless $query->{user};
  1443. return $query->{tpsgi}->forbidden($query) unless grep { $_ eq 'admin' } @{ $query->{user_acls} };
  1444. # Get the existing post types
  1445. my $forms = Trog::Themes::templates_in_dir( "forms", 'text/html', 1 );
  1446. return Trog::Routes::HTML::index(
  1447. {
  1448. title => 'tCMS Post Wizard',
  1449. theme_dir => Trog::Themes::td(),
  1450. template => 'post_wizard.tx',
  1451. is_admin => 1,
  1452. forms => $forms,
  1453. %$query,
  1454. },
  1455. undef,
  1456. ['post.css'],
  1457. );
  1458. }
  1459. # basically a file rewrite rule for themes
  1460. sub icon ($query) {
  1461. my $path = $query->{route};
  1462. my $tpath = Trog::Themes::themed("img/icon/$path");
  1463. return $query->{tpsgi}->serve( $path, $tpath, $query->{start}, $query->{streaming}, $query->{ranges}, $query->{last_fetched}, $query->{deflate} );
  1464. }
  1465. sub totp_qr ($query) {
  1466. my $fname = basename($query->{route});
  1467. # For authenticated content, we have to now do a serve().
  1468. return $query->{tpsgi}->serve(
  1469. "totp/$fname",
  1470. "totp/$fname",
  1471. $query->{start},
  1472. $query->{streaming},
  1473. $query->{ranges},
  1474. $query->{last_fetched},
  1475. $query->{deflate},
  1476. );
  1477. }
  1478. sub rss_style ($query) {
  1479. $query->{port} = ":$query->{port}" if $query->{port};
  1480. $query->{title} = qq{<xsl:value-of select="rss/channel/title"/>};
  1481. $query->{no_doctype} = 1;
  1482. # Due to this being html rather than XML, we can't use an include directive.
  1483. $query->{header} = Trog::Renderer->render( template => 'header.tx', data => $query, contenttype => 'text/html', component => 1 );
  1484. $query->{footer} = Trog::Renderer->render( template => 'footer.tx', data => $query, contenttype => 'text/html', component => 1 );
  1485. return Trog::Renderer->render(
  1486. template => 'rss-style.tx',
  1487. contenttype => 'text/xsl',
  1488. data => $query,
  1489. code => 200,
  1490. );
  1491. }
  1492. sub _build_themed_styles ($styles) {
  1493. my @styles = map { ( Trog::Themes::themed_style("$_") ) } @{ Trog::Utils::coerce_array($styles) };
  1494. return \@styles;
  1495. }
  1496. sub _build_themed_scripts ($scripts) {
  1497. my @scripts = map { Trog::Themes::themed_script("$_") } @{ Trog::Utils::coerce_array($scripts) };
  1498. return \@scripts;
  1499. }
  1500. sub finish_render ( $template, $vars, %headers ) {
  1501. #XXX default vars that need to be pulled from config
  1502. $vars->{lang} //= 'en-US';
  1503. $vars->{title} //= 'tCMS';
  1504. $vars->{stylesheets} //= [];
  1505. $vars->{print_styles} //= [qw{structure.css print.css}];
  1506. $vars->{scripts} //= [];
  1507. # Theme-ize the paths
  1508. $vars->{stylesheets} = [ @{ _build_themed_styles( $vars->{stylesheets} ) } ];
  1509. $vars->{print_styles} = [ @{ _build_themed_styles( $vars->{print_styles} ) } ];
  1510. $vars->{scripts} = [ map { s/^www\///; $_ } @{ _build_themed_scripts( $vars->{scripts} ) } ];
  1511. # Add in avatars.css, it's special
  1512. push( @{ $vars->{stylesheets} }, "/styles/avatars.css" );
  1513. # Absolute-ize the paths for scripts & stylesheets
  1514. @{ $vars->{stylesheets} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{stylesheets} };
  1515. @{ $vars->{print_styles} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{print_styles} };
  1516. @{ $vars->{scripts} } = map { CORE::index( $_, '/' ) == 0 ? $_ : "/$_" } @{ $vars->{scripts} };
  1517. # TODO Smash together the stylesheets and minify
  1518. $vars->{contenttype} //= $Trog::Vars::content_types{html};
  1519. $vars->{cachecontrol} //= $Trog::Vars::cache_control{revalidate};
  1520. $vars->{code} ||= 200;
  1521. $vars->{theme_dir} =~ s/^\/www\/// if $vars->{theme_dir};
  1522. $vars->{header} = Trog::Renderer->render( template => 'header.tx', data => $vars, contenttype => 'text/html', component => 1 );
  1523. $vars->{footer} = Trog::Renderer->render( template => 'footer.tx', data => $vars, contenttype => 'text/html', component => 1 );
  1524. return Trog::Renderer->render( template => $template, data => $vars, contenttype => 'text/html', code => $vars->{code} );
  1525. }
  1526. 1;