HTML.pm 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. package Trog::Routes::HTML;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Errno qw{ENOENT};
  7. use File::Touch();
  8. use List::Util();
  9. use List::MoreUtils();
  10. use Capture::Tiny qw{capture};
  11. use HTML::SocialMeta;
  12. use Trog::Utils;
  13. use Trog::Config;
  14. use Trog::Data;
  15. my $conf = Trog::Config::get();
  16. my $template_dir = 'www/templates';
  17. my $theme_dir = '';
  18. $theme_dir = "themes/".$conf->param('general.theme') if $conf->param('general.theme') && -d "www/themes/".$conf->param('general.theme');
  19. my $td = $theme_dir ? "/$theme_dir" : '';
  20. use lib 'www';
  21. our $landing_page = 'default.tx';
  22. our $htmltitle = 'title.tx';
  23. our $midtitle = 'midtitle.tx';
  24. our $rightbar = 'rightbar.tx';
  25. our $leftbar = 'leftbar.tx';
  26. our $topbar = 'topbar.tx';
  27. our $footbar = 'footbar.tx';
  28. # Note to maintainers: never ever remove backends from this list.
  29. # the auth => 1 is a crucial protection, even with forbidden() guards in these routes.
  30. our %routes = (
  31. default => {
  32. callback => \&Trog::Routes::HTML::setup,
  33. },
  34. '/' => {
  35. method => 'GET',
  36. callback => \&Trog::Routes::HTML::index,
  37. },
  38. #Deal with most indexDocument directives interfering with proxied requests to /
  39. '/index.html' => {
  40. method => 'GET',
  41. callback => \&Trog::Routes::HTML::index,
  42. },
  43. '/index.php' => {
  44. method => 'GET',
  45. callback => \&Trog::Routes::HTML::index,
  46. },
  47. # This should only be enabled to debug
  48. # '/setup' => {
  49. # method => 'GET',
  50. # callback => \&Trog::Routes::HTML::setup,
  51. # },
  52. '/login' => {
  53. method => 'GET',
  54. callback => \&Trog::Routes::HTML::login,
  55. },
  56. '/logout' => {
  57. method => 'GET',
  58. callback => \&Trog::Routes::HTML::logout,
  59. },
  60. '/auth' => {
  61. method => 'POST',
  62. nostatic => 1,
  63. callback => \&Trog::Routes::HTML::login,
  64. },
  65. '/post/save' => {
  66. method => 'POST',
  67. auth => 1,
  68. callback => \&Trog::Routes::HTML::post_save,
  69. },
  70. '/post/delete' => {
  71. method => 'POST',
  72. auth => 1,
  73. callback => \&Trog::Routes::HTML::post_delete,
  74. },
  75. '/config/save' => {
  76. method => 'POST',
  77. auth => 1,
  78. callback => \&Trog::Routes::HTML::config_save,
  79. },
  80. '/themeclone' => {
  81. method => 'POST',
  82. auth => 1,
  83. callback => \&Trog::Routes::HTML::themeclone,
  84. },
  85. '/profile' => {
  86. method => 'POST',
  87. auth => 1,
  88. callback => \&Trog::Routes::HTML::profile,
  89. },
  90. '/manual' => {
  91. method => 'GET',
  92. auth => 1,
  93. callback => \&Trog::Routes::HTML::manual,
  94. },
  95. '/lib/(.*)' => {
  96. method => 'GET',
  97. auth => 1,
  98. captures => ['module'],
  99. callback => \&Trog::Routes::HTML::manual,
  100. },
  101. #TODO transform into posts?
  102. '/sitemap', => {
  103. method => 'GET',
  104. callback => \&Trog::Routes::HTML::sitemap,
  105. },
  106. '/sitemap_index.xml', => {
  107. method => 'GET',
  108. callback => \&Trog::Routes::HTML::sitemap,
  109. data => { xml => 1 },
  110. },
  111. '/sitemap_index.xml.gz', => {
  112. method => 'GET',
  113. callback => \&Trog::Routes::HTML::sitemap,
  114. data => { xml => 1, compressed => 1 },
  115. },
  116. '/sitemap/static.xml' => {
  117. method => 'GET',
  118. callback => \&Trog::Routes::HTML::sitemap,
  119. data => { xml => 1, map => 'static' },
  120. },
  121. '/sitemap/static.xml.gz' => {
  122. method => 'GET',
  123. callback => \&Trog::Routes::HTML::sitemap,
  124. data => { xml => 1, compressed => 1, map => 'static' },
  125. },
  126. '/sitemap/(.*).xml' => {
  127. method => 'GET',
  128. callback => \&Trog::Routes::HTML::sitemap,
  129. data => { xml => 1 },
  130. captures => ['map'],
  131. },
  132. '/sitemap/(.*).xml.gz' => {
  133. method => 'GET',
  134. callback => \&Trog::Routes::HTML::sitemap,
  135. data => { xml => 1, compressed => 1},
  136. captures => ['map'],
  137. },
  138. '/robots.txt' => {
  139. method => 'GET',
  140. callback => \&Trog::Routes::HTML::robots,
  141. },
  142. '/humans.txt' => {
  143. method => 'GET',
  144. callback => \&Trog::Routes::HTML::posts,
  145. data => { tag => ['about'] },
  146. },
  147. '/styles/avatars.css' => {
  148. method => 'GET',
  149. callback => \&Trog::Routes::HTML::avatars,
  150. data => { tag => ['about'] },
  151. },
  152. );
  153. # Grab theme routes
  154. my $themed = 0;
  155. if ($theme_dir) {
  156. my $theme_mod = "$theme_dir/routes.pm";
  157. if (-f "www/$theme_mod" ) {
  158. require $theme_mod;
  159. @routes{keys(%Theme::routes)} = values(%Theme::routes);
  160. $themed = 1;
  161. }
  162. }
  163. =head1 PRIMARY ROUTE
  164. =head2 index
  165. Implements the primary route used by all pages not behind auth.
  166. Most subsequent functions simply pass content to this function.
  167. =cut
  168. sub index ($query,$render_cb, $content = '', $i_styles = []) {
  169. $query->{theme_dir} = $td;
  170. my $processor = Text::Xslate->new(
  171. path => $template_dir,
  172. );
  173. my $t_processor;
  174. $t_processor = Text::Xslate->new(
  175. path => "www/$theme_dir/templates",
  176. ) if $theme_dir;
  177. $content ||= _pick_processor("templates/$landing_page",$processor,$t_processor)->render($landing_page,$query);
  178. my @styles = ('/styles/avatars.css');
  179. if ($theme_dir) {
  180. if ($query->{embed}) {
  181. unshift(@styles, _themed_style("embed.css")) if -f 'www/'._themed_style("embed.css");
  182. }
  183. unshift(@styles, _themed_style("screen.css")) if -f 'www/'._themed_style("screen.css");
  184. unshift(@styles, _themed_style("print.css")) if -f 'www/'._themed_style("print.css");
  185. unshift(@styles, _themed_style("structure.css")) if -f 'www/'._themed_style("structure.css");
  186. }
  187. push( @styles, @$i_styles );
  188. #TODO allow theming of print css
  189. my $search_info = Trog::Data->new($conf);
  190. my @series = _get_series(0, $search_info);
  191. my $title = $query->{primary_post}{title} // $query->{title} // $Theme::default_title // 'tCMS';
  192. # Handle link "unfurling" correctly
  193. my ($default_tags, $meta_desc, $meta_tags) = _build_social_meta($query,$title);
  194. #Do embed content
  195. my $tmpl = $query->{embed} ? 'embed.tx' : 'index.tx';
  196. return $render_cb->( $tmpl, {
  197. code => $query->{code},
  198. user => $query->{user},
  199. search_lang => $search_info->lang(),
  200. search_help => $search_info->help(),
  201. route => $query->{route},
  202. domain => $query->{domain},
  203. theme_dir => $td,
  204. content => $content,
  205. title => $title,
  206. htmltitle => _pick_processor("templates/$htmltitle" ,$processor,$t_processor)->render($htmltitle,$query),
  207. midtitle => _pick_processor("templates/$midtitle" ,$processor,$t_processor)->render($midtitle,$query),
  208. rightbar => _pick_processor("templates/$rightbar" ,$processor,$t_processor)->render($rightbar,$query),
  209. leftbar => _pick_processor("templates/$leftbar" ,$processor,$t_processor)->render($leftbar,$query),
  210. topbar => _pick_processor("templates/$topbar" ,$processor,$t_processor)->render($topbar,$query),
  211. footbar => _pick_processor("templates/$footbar" ,$processor,$t_processor)->render($footbar,$query),
  212. categories => \@series,
  213. stylesheets => \@styles,
  214. show_madeby => $Theme::show_madeby ? 1 : 0,
  215. embed => $query->{embed} ? 1 : 0,
  216. embed_video => $query->{primary_post}{is_video},
  217. default_tags => $default_tags,
  218. meta_desc => $meta_desc,
  219. meta_tags => $meta_tags,
  220. deflate => $query->{deflate},
  221. });
  222. }
  223. sub _build_social_meta ($query,$title) {
  224. return (undef,undef,undef) unless $query->{social_meta} && $query->{route} && $query->{domain};
  225. my $default_tags = $Theme::default_tags;
  226. $default_tags .= ','.join(',',@{$query->{primary_post}->{tags}}) if $default_tags && $query->{primary_post}->{tags};
  227. my $meta_desc = $query->{primary_post}{data} // $Theme::description // "tCMS Site";
  228. $meta_desc = Trog::Utils::strip_and_trunc($meta_desc);
  229. my $meta_tags = '';
  230. my $card_type = 'summary';
  231. $card_type = 'featured_image' if $query->{primary_post} && $query->{primary_post}{is_image};
  232. $card_type = 'player' if $query->{primary_post} && $query->{primary_post}{is_video};
  233. my $image = $Theme::default_image ? "https://$query->{domain}/$td/$Theme::default_image" : '';
  234. $image = "https://$query->{domain}/$query->{primary_post}{preview}" if $query->{primary_post} && $query->{primary_post}{preview};
  235. $image = "https://$query->{domain}/$query->{primary_post}{href}" if $query->{primary_post} && $query->{primary_post}{is_image};
  236. my $primary_route = "https://$query->{domain}/$query->{route}";
  237. $primary_route =~ s/[\/]+/\//g;
  238. my $display_name = $Theme::display_name // 'Another tCMS Site';
  239. my $extra_tags ='';
  240. my %sopts = (
  241. site_name => $display_name,
  242. app_name => $display_name,
  243. title => $title,
  244. description => $meta_desc,
  245. url => $primary_route,
  246. );
  247. $sopts{site} = $Theme::twitter_account if $Theme::twitter_account;
  248. $sopts{image} = $image if $image;
  249. $sopts{fb_app_id} = $Theme::fb_app_id if $Theme::fb_app_id;
  250. if ($query->{primary_post} && $query->{primary_post}{is_video}) {
  251. #$sopts{player} = "$primary_route?embed=1";
  252. $sopts{player} = "https://$query->{domain}/$query->{primary_post}{href}";
  253. #XXX don't hardcode this
  254. $sopts{player_width} = 1280;
  255. $sopts{player_height} = 720;
  256. $extra_tags .= "<meta property='og:video:type' content='$query->{primary_post}{content_type}' />\n";
  257. }
  258. my $social = HTML::SocialMeta->new(%sopts);
  259. $meta_tags = eval { $social->create($card_type) };
  260. $meta_tags =~ s/content="video"/content="video:other"/mg if $meta_tags;
  261. $meta_tags .= $extra_tags if $extra_tags;
  262. print STDERR "WARNING: Theme misconfigured, social media tags will not be included\n$@\n" if $theme_dir && !$meta_tags;
  263. return ($default_tags, $meta_desc, $meta_tags);
  264. }
  265. =head1 ADMIN ROUTES
  266. These are things that issue returns other than 200, and are not directly accessible by users via any defined route.
  267. =head2 notfound, forbidden, badrequest
  268. Implements the 4XX status codes. Override templates named the same for theming this.
  269. =cut
  270. sub _generic_route ($rname, $code, $title, $query, $render_cb) {
  271. $query->{code} = $code;
  272. my $processor = Text::Xslate->new(
  273. path => _dir_for_resource("$rname.tx"),
  274. );
  275. $query->{title} = $title;
  276. my $styles = _build_themed_styles("$rname.css");
  277. my $content = $processor->render("$rname.tx", {
  278. title => $title,
  279. route => $query->{route},
  280. user => $query->{user},
  281. styles => $styles,
  282. deflate => $query->{deflate},
  283. });
  284. return Trog::Routes::HTML::index($query, $render_cb, $content, $styles);
  285. }
  286. sub notfound (@args) {
  287. return _generic_route('notfound',404,"Return to sender, Address unknown", @args);
  288. }
  289. sub forbidden (@args) {
  290. return _generic_route('forbidden', 403, "STAY OUT YOU RED MENACE", @args);
  291. }
  292. sub badrequest (@args) {
  293. return _generic_route('badrequest', 400, "Bad Request", @args);
  294. }
  295. sub redirect ($to) {
  296. return [302, ["Location" => $to],['']]
  297. }
  298. sub redirect_permanent ($to) {
  299. return [301, ["Location" => $to], ['']];
  300. }
  301. =head1 NORMAL ROUTES
  302. These are expected to either return a 200, or redirect to something which does.
  303. =head2 robots
  304. Return an appropriate robots.txt
  305. =cut
  306. sub robots ($query, $render_cb) {
  307. my $processor = Text::Xslate->new(
  308. path => $template_dir,
  309. );
  310. return [200, ["Content-type:text/plain\n"],[$processor->render('robots.tx', { domain => $query->{domain} })]];
  311. }
  312. =head2 setup
  313. One time setup page; should only display to the first user to visit the site which we presume to be the administrator.
  314. =cut
  315. sub setup ($query, $render_cb) {
  316. File::Touch::touch("config/setup");
  317. return $render_cb->('notconfigured.tx', {
  318. title => 'tCMS Requires Setup to Continue...',
  319. stylesheets => _build_themed_styles('notconfigured.css'),
  320. });
  321. }
  322. =head2 login
  323. 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.
  324. =cut
  325. sub login ($query, $render_cb) {
  326. # Redirect if we actually have a logged in user.
  327. # Note to future me -- this user value is overwritten explicitly in server.psgi.
  328. # If that ever changes, you will die
  329. $query->{to} //= $query->{route};
  330. $query->{to} = '/config' if $query->{to} eq '/login';
  331. if ($query->{user}) {
  332. return $routes{$query->{to}}{callback}->($query,$render_cb);
  333. }
  334. #Check and see if we have no users. If so we will just accept whatever creds are passed.
  335. my $hasusers = -f "config/has_users";
  336. my $btnmsg = $hasusers ? "Log In" : "Register";
  337. my @headers;
  338. if ($query->{username} && $query->{password}) {
  339. if (!$hasusers) {
  340. # Make the first user
  341. Trog::Auth::useradd($query->{username}, $query->{password}, ['admin'] );
  342. # Add a stub user page and the initial series.
  343. my $dat = Trog::Data->new($conf);
  344. _setup_initial_db($dat,$query->{username});
  345. # Ensure we stop registering new users
  346. File::Touch::touch("config/has_users");
  347. }
  348. $query->{failed} = 1;
  349. my $cookie = Trog::Auth::mksession($query->{username}, $query->{password});
  350. if ($cookie) {
  351. # TODO secure / sameSite cookie to kill csrf, maybe do rememberme with Expires=~0
  352. my $secure = '';
  353. $secure = '; Secure' if $query->{scheme} eq 'https';
  354. @headers = (
  355. "Set-Cookie" => "tcmslogin=$cookie; HttpOnly; SameSite=Strict$secure",
  356. );
  357. $query->{failed} = 0;
  358. }
  359. }
  360. $query->{failed} //= -1;
  361. return $render_cb->('login.tx', {
  362. title => 'tCMS 2 ~ Login',
  363. to => $query->{to},
  364. failure => int( $query->{failed} ),
  365. message => int( $query->{failed} ) < 1 ? "Login Successful, Redirecting..." : "Login Failed.",
  366. btnmsg => $btnmsg,
  367. stylesheets => _build_themed_styles('login.css'),
  368. theme_dir => $td,
  369. }, @headers);
  370. }
  371. sub _setup_initial_db ($dat, $user) {
  372. $dat->add(
  373. {
  374. "aclname" => "series",
  375. "acls" => [],
  376. "callback" => "Trog::Routes::HTML::series",
  377. method => 'GET',
  378. "data" => "Series",
  379. "href" => "/series",
  380. "local_href" => "/series",
  381. "preview" => "/img/sys/testpattern.jpg",
  382. "tags" => [qw{series topbar}],
  383. visibility => 'public',
  384. "title" => "Series",
  385. user => $user,
  386. form => 'series.tx',
  387. child_form => 'series.tx',
  388. aliases => [],
  389. },
  390. {
  391. "aclname" => "about",
  392. "acls" => [],
  393. "callback" => "Trog::Routes::HTML::series",
  394. method => 'GET',
  395. "data" => "About",
  396. "href" => "/about",
  397. "local_href" => "/about",
  398. "preview" => "/img/sys/testpattern.jpg",
  399. "tags" => [qw{series topbar public}],
  400. visibility => 'public',
  401. "title" => "About",
  402. user => $user,
  403. form => 'series.tx',
  404. child_form => 'profile.tx',
  405. aliases => [],
  406. },
  407. {
  408. "aclname" => "config",
  409. acls => [],
  410. "callback" => "Trog::Routes::HTML::config",
  411. 'method' => 'GET',
  412. "content_type" => "text/html",
  413. "data" => "Config",
  414. "href" => "/config",
  415. "local_href" => "/config",
  416. "preview" => "/img/sys/testpattern.jpg",
  417. "tags" => [qw{admin}],
  418. visibility => 'private',
  419. "title" => "Configure tCMS",
  420. user => $user,
  421. aliases => [],
  422. },
  423. {
  424. title => $user,
  425. data => 'Default user',
  426. preview => '/img/avatar/humm.gif',
  427. wallpaper => '/img/sys/testpattern.jpg',
  428. tags => ['about'],
  429. visibility => 'public',
  430. acls => ['admin'],
  431. local_href => "/users/$user",
  432. callback => "Trog::Routes::HTML::users",
  433. method => 'GET',
  434. user => $user,
  435. form => 'profile.tx',
  436. aliases => [],
  437. },
  438. );
  439. }
  440. =head2 logout
  441. Deletes your users' session and opens the index.
  442. =cut
  443. sub logout ($query, $render_cb) {
  444. Trog::Auth::killsession($query->{user}) if $query->{user};
  445. delete $query->{user};
  446. return Trog::Routes::HTML::index($query,$render_cb);
  447. }
  448. =head2 config
  449. Renders the configuration page, or redirects you back to the login page.
  450. =cut
  451. sub config ($query, $render_cb) {
  452. if (!$query->{user}) {
  453. return login($query,$render_cb);
  454. }
  455. #NOTE: we are relying on this to skip the ACL check with 'admin', this may not be viable in future?
  456. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  457. my $css = _build_themed_styles('config.css');
  458. my $js = _build_themed_scripts('post.js');
  459. $query->{failure} //= -1;
  460. my @series = _get_series(1);
  461. return $render_cb->('config.tx', {
  462. title => 'Configure tCMS',
  463. theme_dir => $td,
  464. stylesheets => $css,
  465. scripts => $js,
  466. categories => \@series,
  467. themes => _get_themes() || [],
  468. data_models => _get_data_models(),
  469. current_theme => $conf->param('general.theme') // '',
  470. current_data_model => $conf->param('general.data_model') // 'DUMMY',
  471. message => $query->{message},
  472. failure => $query->{failure},
  473. to => '/config',
  474. });
  475. }
  476. sub _get_series($edit=0,$search_info=0) {
  477. $search_info ||= Trog::Data->new($conf);
  478. my @series = $search_info->get(
  479. acls => [qw{public}],
  480. tags => [qw{topbar}],
  481. limit => 10,
  482. page => 1,
  483. );
  484. @series = map { $_->{local_href} = "/post$_->{local_href}"; $_ } @series if $edit;
  485. return @series;
  486. }
  487. sub _get_themes {
  488. my $dir = 'www/themes';
  489. opendir(my $dh, $dir) || do { die "Can't opendir $dir: $!" unless $!{ENOENT} };
  490. my @tdirs = grep { !/^\./ && -d "$dir/$_" } readdir($dh);
  491. closedir $dh;
  492. return \@tdirs;
  493. }
  494. sub _get_data_models {
  495. my $dir = 'lib/Trog/Data';
  496. opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
  497. my @dmods = map { s/\.pm$//g; $_ } grep { /\.pm$/ && -f "$dir/$_" } readdir($dh);
  498. closedir $dh;
  499. return \@dmods
  500. }
  501. =head2 config_save
  502. Implements /config/save route. Saves what little configuration we actually use to ~/.tcms/tcms.conf
  503. =cut
  504. sub config_save ($query, $render_cb) {
  505. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  506. $conf->param( 'general.theme', $query->{theme} ) if defined $query->{theme};
  507. $conf->param( 'general.data_model', $query->{data_model} ) if $query->{data_model};
  508. $query->{failure} = 1;
  509. $query->{message} = "Failed to save configuration!";
  510. if ($conf->write($Trog::Config::home_cfg)) {
  511. $query->{failure} = 0;
  512. $query->{message} = "Configuration updated succesfully.";
  513. }
  514. #Get the PID of the parent port using lsof, send HUP
  515. my $parent = getppid;
  516. kill 'HUP', $parent;
  517. return config($query, $render_cb);
  518. }
  519. =head2 themeclone
  520. Clone a theme by copying a directory.
  521. =cut
  522. sub themeclone ($query, $render_cb) {
  523. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  524. my ($theme, $newtheme) = ($query->{theme},$query->{newtheme});
  525. my $themedir = 'www/themes';
  526. $query->{failure} = 1;
  527. $query->{message} = "Failed to clone theme '$theme' as '$newtheme'!";
  528. require File::Copy::Recursive;
  529. if ($theme && $newtheme && File::Copy::Recursive::dircopy( "$themedir/$theme", "$themedir/$newtheme" )) {
  530. $query->{failure} = 0;
  531. $query->{message} = "Successfully cloned theme '$theme' as '$newtheme'.";
  532. }
  533. return config($query, $render_cb);
  534. }
  535. =head2 post_save
  536. Saves posts submitted via the /post pages
  537. =cut
  538. sub post_save ($query, $render_cb) {
  539. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  540. my $to = delete $query->{to};
  541. #Copy this down since it will be deleted later
  542. my $acls = $query->{acls};
  543. state $data = Trog::Data->new($conf);
  544. $query->{tags} = _coerce_array($query->{tags});
  545. # Filter bits and bobs
  546. delete $query->{primary_post};
  547. delete $query->{social_meta};
  548. delete $query->{deflate};
  549. delete $query->{acls};
  550. # Ensure there are no null tags
  551. @{$query->{tags}} = grep { defined $_ } @{$query->{tags}};
  552. $query->{failure} = $data->add($query);
  553. $query->{to} = $to;
  554. $query->{acls} = $acls;
  555. $query->{message} = $query->{failure} ? "Failed to add post!" : "Successfully added Post";
  556. delete $query->{id};
  557. return posts($query, $render_cb);
  558. }
  559. =head2 profile
  560. Saves / updates new users.
  561. =cut
  562. sub profile ($query, $render_cb) {
  563. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  564. #TODO allow users to do something OTHER than be admins
  565. if ($query->{password}) {
  566. Trog::Auth::useradd($query->{username}, $query->{password}, ['admin'] );
  567. }
  568. #Make sure it is "self-authored", redact pw
  569. $query->{user} = delete $query->{username};
  570. delete $query->{password};
  571. return post_save($query, $render_cb);
  572. }
  573. =head2 post_delete
  574. deletes posts.
  575. =cut
  576. sub post_delete ($query, $render_cb) {
  577. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  578. state $data = Trog::Data->new($conf);
  579. $query->{failure} = $data->delete($query);
  580. $query->{to} = $query->{to};
  581. $query->{message} = $query->{failure} ? "Failed to delete post $query->{id}!" : "Successfully deleted Post $query->{id}";
  582. delete $query->{id};
  583. return posts($query, $render_cb);
  584. }
  585. =head2 series
  586. Series specific view, much like the users/ route
  587. Displays identified series, not all series.
  588. =cut
  589. sub series ($query, $render_cb) {
  590. my $is_admin = grep { $_ eq 'admin' } @{$query->{acls}};
  591. #we are either viewed one of two ways, /post/$id or /$aclname
  592. my (undef,$aclname,$id) = split(/\//,$query->{route});
  593. $query->{aclname} = $aclname if !$id;
  594. $query->{id} = $id if $id;
  595. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  596. $query->{exclude_tags} = ['topbar'] if !$is_admin && $aclname && $aclname eq 'series';
  597. #XXX I'd prefer to overload id to actually *be* the aclname...
  598. # but this way, accomodates things like the flat file time-indexing hack.
  599. # TODO I should probably have it for all posts, and make *everything* a series.
  600. # WE can then do threaded comments/posts.
  601. # That will essentially necessitate it *becoming* the ID for real.
  602. #Grab the relevant tag (aclname), then pass that to posts
  603. my @posts = _post_helper($query, ['series'], $query->{acls});
  604. delete $query->{id};
  605. delete $query->{aclname};
  606. $query->{subhead} = $posts[0]->{data};
  607. $query->{title} = $posts[0]->{title};
  608. $query->{tag} = $posts[0]->{aclname};
  609. $query->{primary_post} = $posts[0];
  610. $query->{in_series} = 1;
  611. return posts($query,$render_cb);
  612. }
  613. =head2 avatars
  614. Returns the avatars.css. Limited to 1000 users.
  615. =cut
  616. sub avatars ($query, $render_cb) {
  617. #XXX if you have more than 1000 editors you should stop
  618. push(@{$query->{acls}}, 'public');
  619. my $tags = _coerce_array($query->{tag});
  620. my $processor = Text::Xslate->new(
  621. path => $template_dir,
  622. );
  623. my @posts = _post_helper($query, $tags, $query->{acls});
  624. my $content = $processor->render('avatars.tx', {
  625. users => \@posts,
  626. });
  627. return [200, ["Content-type" => "text/css" ],[$content]];
  628. }
  629. =head2 users
  630. Implements direct user profile view.
  631. =cut
  632. sub users ($query, $render_cb) {
  633. # Capture the username
  634. my (undef, undef, $username) = split(/\//, $query->{route});
  635. $query->{username} //= $username;
  636. push(@{$query->{acls}}, 'public');
  637. $query->{exclude_tags} = ['about'];
  638. # Don't show topbar series on the series page. That said, don't exclude it from direct series view.
  639. my $is_admin = grep { $_ eq 'admin' } @{$query->{acls}};
  640. push(@{$query->{exclude_tags}}, 'topbar') if !$is_admin;
  641. my @posts = _post_helper({ author => $query->{username} }, ['about'], $query->{acls});
  642. $query->{id} = $posts[0]->{id};
  643. $query->{title} = $posts[0]->{title};
  644. $query->{user_obj} = $posts[0];
  645. $query->{primary_post} = $posts[0];
  646. $query->{in_series} = 1;
  647. return posts($query,$render_cb);
  648. }
  649. =head2 posts
  650. Display multi or single posts, supports RSS and pagination.
  651. =cut
  652. sub posts ($query, $render_cb, $direct=0) {
  653. #Process the input URI to capture tag/id
  654. $query->{route} //= $query->{to};
  655. my (undef, undef, $id) = split(/\//, $query->{route});
  656. my $tags = _coerce_array($query->{tag});
  657. $query->{id} = $id if $id && !$query->{in_series};
  658. my $is_admin = grep { $_ eq 'admin' } @{$query->{acls}};
  659. push(@{$query->{acls}}, 'public');
  660. push(@{$query->{acls}}, 'unlisted') if $query->{id};
  661. push(@{$query->{acls}}, 'private') if $is_admin;
  662. my @posts;
  663. # Discover this user's visibility, so we can make them post in this category by default
  664. my $user_visibility = 'public';
  665. if ($query->{user_obj}) {
  666. #Optimize the /users/* route
  667. @posts = ($query->{user_obj});
  668. $user_visibility = $query->{user_obj}->{visibility};
  669. } else {
  670. if ($query->{user}) {
  671. my @me = _post_helper({ author => $query->{user} }, ['about'], $query->{acls});
  672. $user_visibility = $me[0]->{visibility};
  673. }
  674. @posts = _post_helper($query, $tags, $query->{acls});
  675. }
  676. if ($query->{id}) {
  677. $query->{primary_post} = $posts[0] if @posts;
  678. }
  679. #OK, so if we have a user as the ID we found, go grab the rest of their posts
  680. if ($query->{id} && @posts && grep { $_ eq 'about'} @{$posts[0]->{tags}} ) {
  681. my $user = shift(@posts);
  682. my $id = delete $query->{id};
  683. $query->{author} = $user->{user};
  684. @posts = _post_helper($query, $tags, $query->{acls});
  685. @posts = grep { $_->{id} ne $id } @posts;
  686. unshift @posts, $user;
  687. }
  688. if (!$is_admin) {
  689. return notfound($query, $render_cb) unless @posts;
  690. }
  691. my $fmt = $query->{format} || '';
  692. return _rss($query,\@posts) if $fmt eq 'rss';
  693. my $processor = Text::Xslate->new(
  694. path => $template_dir,
  695. function => {
  696. render_it => sub {
  697. my ($template_string, $options) = @_;
  698. return Text::Xslate->new(
  699. # Prevent a recursive descent. If the renderer is hit again, just do nothing
  700. # XXX unfortunately if the post tries to include itself, it will die.
  701. function => {
  702. embed => sub {
  703. my ($this_id, $style) = @_;
  704. $style //= 'embed';
  705. # If instead the style is 'content', then we will only show the content w/ no formatting, and no title.
  706. return Text::Xslate::mark_raw(Trog::Routes::HTML::posts(
  707. { route => "/post/$this_id", style => $style },
  708. sub {},
  709. 1));
  710. },
  711. },
  712. )->render_string($template_string,$options);
  713. },
  714. },
  715. );
  716. #XXX Is used by the sitemap, maybe just fix there?
  717. my @post_aliases = map { $_->{local_href} } _get_series();
  718. my ($header,$footer);
  719. my $t_processor;
  720. $t_processor = Text::Xslate->new(
  721. path => "www/$theme_dir/templates",
  722. ) if $theme_dir;
  723. $header = _pick_processor('templates/headers/'.$query->{primary_post}{header}, $processor,$t_processor)->render('headers/'.$query->{primary_post}{header}, { theme_dir => $td } ) if $query->{primary_post}{header};
  724. $footer = _pick_processor('templates/footers/'.$query->{primary_post}{footer}, $processor,$t_processor)->render('footers/'.$query->{primary_post}{footer}, { theme_dir => $td } ) if $query->{primary_post}{footer};
  725. # List the available headers/footers
  726. my $headers = _templates_in_dir(-d $theme_dir ? "www/$theme_dir/templates/headers" : "www/templates/headers");
  727. my $footers = _templates_in_dir(-d $theme_dir ? "www/$theme_dir/templates/footers" : "www/templates/footers");
  728. my $styles = _build_themed_styles('posts.css');
  729. #Correct page headers
  730. my $ph = $themed ? _themed_title($query->{route}) : $query->{route};
  731. # Build page title if it wasn't set by a wrapping sub
  732. $query->{title} = "$query->{domain} : $query->{title}" if $query->{title} && $query->{domain};
  733. $query->{title} ||= @$tags && $query->{domain} ? "$query->{domain} : @$tags" : undef;
  734. #Handle paginator vars
  735. my $limit = int($query->{limit} || 25);
  736. my $now_year = (localtime(time))[5] + 1900;
  737. my $oldest_year = $now_year - 20; #XXX actually find oldest post year
  738. # Handle post style.
  739. if ($query->{style}) {
  740. undef $header;
  741. undef $footer;
  742. }
  743. my $older = !@posts ? 0 : $posts[-1]->{created};
  744. $query->{failure} //= -1;
  745. $query->{id} //= '';
  746. #XXX messed up data has to be fixed unfortunately
  747. @$tags = List::Util::uniq @$tags;
  748. #Filter displaying visibility tags
  749. my @visibuddies = qw{public unlisted private};
  750. foreach my $post (@posts) {
  751. @{$post->{tags}} = grep { my $tag = $_; !grep { $tag eq $_ } @visibuddies } @{$post->{tags}};
  752. }
  753. #XXX note that we are explicitly relying on the first tag to be the ACL
  754. my $aclselected = $tags->[0] || '';
  755. my @acls = map {
  756. $_->{selected} = $_->{aclname} eq $aclselected ? 'selected' : '';
  757. $_
  758. } _post_helper({}, ['series'], $query->{acls});
  759. my $forms = _templates_in_dir("$template_dir/forms");
  760. my $edittype = $query->{primary_post} ? $query->{primary_post}->{child_form} : $query->{form};
  761. my $tiled = $query->{primary_post} ? !$is_admin && $query->{primary_post}->{tiled} : 0;
  762. # Grab the rest of the tags to dump into the edit form
  763. state $data = Trog::Data->new($conf);
  764. my @tags_all = $data->tags();
  765. #Filter out the visibilities and special series tags
  766. @tags_all = grep { my $subj = $_; scalar(grep { $_ eq $subj } qw{public private unlisted admin series about topbar}) == 0 } @tags_all;
  767. @posts = map {
  768. my $subject = $_;
  769. my @et = grep { my $subj = $_; grep { $subj eq $_ } @tags_all } @{$subject->{tags}};
  770. @et = grep { $_ ne $aclselected } @et;
  771. $_->{extra_tags} = \@et;
  772. $_
  773. } @posts;
  774. my @et = List::MoreUtils::singleton(@$tags, @tags_all);
  775. my $content = $processor->render('posts.tx', {
  776. acls => \@acls,
  777. can_edit => $is_admin,
  778. forms => $forms,
  779. post => { tags => $tags, extra_tags => \@et, form => $edittype, visibility => $user_visibility, addpost => 1 },
  780. post_visibilities => \@visibuddies,
  781. failure => $query->{failure},
  782. to => $query->{to},
  783. message => $query->{failure} ? "Failed to add post!" : "Successfully added Post as $query->{id}",
  784. direct => $direct,
  785. title => $query->{title},
  786. style => $query->{style},
  787. posts => \@posts,
  788. like => $query->{like},
  789. in_series => exists $query->{in_series} || !!($query->{route} =~ m/^\/series\//),
  790. route => $query->{route},
  791. limit => $limit,
  792. pages => scalar(@posts) == $limit,
  793. older => $older,
  794. sizes => [25,50,100],
  795. rss => !$query->{id} && !$query->{older},
  796. tiled => $tiled,
  797. category => $ph,
  798. subhead => $query->{subhead},
  799. header => $header,
  800. footer => $footer,
  801. headers => $headers,
  802. footers => $footers,
  803. years => [reverse($oldest_year..$now_year)],
  804. months => [0..11],
  805. });
  806. return $content if $direct;
  807. return Trog::Routes::HTML::index($query, $render_cb, $content, $styles);
  808. }
  809. sub _templates_in_dir($path) {
  810. my $forms = [];
  811. return $forms unless -d $path;
  812. opendir(my $dh, $path);
  813. while (my $form = readdir($dh)) {
  814. push(@$forms, $form) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  815. }
  816. close($dh);
  817. return $forms;
  818. }
  819. sub _themed_title ($path) {
  820. return $path unless %Theme::paths;
  821. return $Theme::paths{$path} ? $Theme::paths{$path} : $path;
  822. }
  823. sub _post_helper ($query, $tags, $acls) {
  824. state $data = Trog::Data->new($conf);
  825. return $data->get(
  826. older => $query->{older},
  827. page => int($query->{page} || 1),
  828. limit => int($query->{limit} || 25),
  829. tags => $tags,
  830. exclude_tags => $query->{exclude_tags},
  831. acls => $acls,
  832. aclname => $query->{aclname},
  833. like => $query->{like},
  834. author => $query->{author},
  835. id => $query->{id},
  836. version => $query->{version},
  837. );
  838. }
  839. =head2 sitemap
  840. Return the sitemap index unless the static or a set of dynamic routes is requested.
  841. We have a maximum of 99,990,000 posts we can make under this model
  842. As we have 10,000 * 10,000 posts which are indexable via the sitemap format.
  843. 1 top level index slot (10k posts) is taken by our static routes, the rest will be /posts.
  844. Passing ?xml=1 will result in an appropriate sitemap.xml instead.
  845. This is used to generate the static sitemaps as expected by search engines.
  846. Passing compressed=1 will gzip the output.
  847. =cut
  848. sub sitemap ($query, $render_cb) {
  849. my (@to_map, $is_index, $route_type);
  850. my $warning = '';
  851. $query->{map} //= '';
  852. if ($query->{map} eq 'static') {
  853. # Return the map of static routes
  854. $route_type = 'Static Routes';
  855. @to_map = grep { !defined $routes{$_}->{captures} && $_ !~ m/^default|login|auth$/ && !$routes{$_}->{auth} } keys(%routes);
  856. } elsif ( !$query->{map} ) {
  857. # Return the index instead
  858. @to_map = ('static');
  859. my $data = Trog::Data->new($conf);
  860. my $tot = $data->count();
  861. my $size = 50000;
  862. my $pages = int($tot / $size) + (($tot % $size) ? 1 : 0);
  863. # Truncate pages at 10k due to standard
  864. my $clamped = $pages > 49999 ? 49999 : $pages;
  865. $warning = "More posts than possible to represent in sitemaps & index! Old posts have been truncated." if $pages > 49999;
  866. foreach my $page ($clamped..1) {
  867. push(@to_map, "$page");
  868. }
  869. $is_index = 1;
  870. } else {
  871. $route_type = "Posts: Page $query->{map}";
  872. # Return the map of the particular range of dynamic posts
  873. $query->{limit} = 50000;
  874. $query->{page} = $query->{map};
  875. @to_map = _post_helper($query, [], ['public']);
  876. }
  877. if ( $query->{xml} ) {
  878. my $sm;
  879. my $xml_date = time();
  880. my $fmt = "xml";
  881. $fmt .= ".gz" if $query->{compressed};
  882. if ( !$query->{map}) {
  883. require WWW::SitemapIndex::XML;
  884. $sm = WWW::SitemapIndex::XML->new();
  885. foreach my $url (@to_map) {
  886. $sm->add(
  887. loc => "http://$query->{domain}/sitemap/$url.$fmt",
  888. lastmod => $xml_date,
  889. );
  890. }
  891. } else {
  892. require WWW::Sitemap::XML;
  893. $sm = WWW::Sitemap::XML->new();
  894. my $changefreq = $query->{map} eq 'static' ? 'monthly' : 'daily';
  895. foreach my $url (@to_map) {
  896. my $true_uri = "http://$query->{domain}$url";
  897. if (ref $url eq 'HASH') {
  898. my $is_user_page = grep { $_ eq 'about' } @{$url->{tags}};
  899. $true_uri = "http://$query->{domain}/posts/$url->{id}";
  900. $true_uri = "http://$query->{domain}/users/$url->{title}" if $is_user_page;
  901. }
  902. my %data = (
  903. loc => $true_uri,
  904. lastmod => $xml_date,
  905. mobile => 1,
  906. changefreq => $changefreq,
  907. priority => 1.0,
  908. );
  909. if (ref $url eq 'HASH') {
  910. #add video & preview image if applicable
  911. $data{images} = [{
  912. loc => "http://$query->{domain}$url->{href}",
  913. caption => $url->{data},
  914. title => substr($url->{title},0,100),
  915. }] if $url->{is_image};
  916. # Truncate descriptions
  917. my $desc = substr($url->{data},0,2048);
  918. $desc //= '';
  919. $data{videos} = [{
  920. content_loc => "http://$query->{domain}$url->{href}",
  921. thumbnail_loc => "http://$query->{domain}$url->{preview}",
  922. title => substr($url->{title},0,100),
  923. description => $desc,
  924. }] if $url->{is_video};
  925. }
  926. $sm->add(%data);
  927. }
  928. }
  929. my $xml = $sm->as_xml();
  930. require IO::String;
  931. my $buf = IO::String->new();
  932. my $ct = 'application/xml';
  933. $xml->toFH($buf, 0);
  934. seek $buf, 0, 0;
  935. if ($query->{compressed}) {
  936. require IO::Compress::Gzip;
  937. my $compressed = IO::String->new();
  938. IO::Compress::Gzip::gzip($buf => $compressed);
  939. $ct = 'application/gzip';
  940. $buf = $compressed;
  941. seek $compressed, 0, 0;
  942. }
  943. return [200,["Content-type" => $ct], $buf];
  944. }
  945. @to_map = sort @to_map unless $is_index;
  946. my $processor = Text::Xslate->new(
  947. path => _dir_for_resource('sitemap.tx'),
  948. );
  949. my $styles = _build_themed_styles('sitemap.css');
  950. $query->{title} = "$query->{domain} : Sitemap";
  951. my $content = $processor->render('sitemap.tx', {
  952. title => "Site Map",
  953. to_map => \@to_map,
  954. is_index => $is_index,
  955. route_type => $route_type,
  956. route => $query->{route},
  957. });
  958. return Trog::Routes::HTML::index($query, $render_cb,$content,$styles);
  959. }
  960. sub _rss ($query,$posts) {
  961. require XML::RSS;
  962. my $rss = XML::RSS->new (version => '2.0');
  963. my $now = DateTime->from_epoch(epoch => time());
  964. $rss->channel(
  965. title => "$query->{domain}",
  966. link => "http://$query->{domain}/$query->{route}?format=rss",
  967. language => 'en', #TODO localization
  968. description => "$query->{domain} : $query->{route}",
  969. pubDate => $now,
  970. lastBuildDate => $now,
  971. );
  972. #TODO configurability
  973. $rss->image(
  974. title => $query->{domain},
  975. url => "$td/img/icon/favicon.ico",
  976. link => "http://$query->{domain}",
  977. width => 88,
  978. height => 31,
  979. description => "$query->{domain} favicon",
  980. );
  981. foreach my $post (@$posts) {
  982. my $url = "http://$query->{domain}$post->{local_href}";
  983. _post2rss($rss,$url,$post);
  984. next unless ref $post->{aliases} eq 'ARRAY';
  985. foreach my $alias (@{$post->{aliases}}) {
  986. $url = "http://$query->{domain}$alias";
  987. _post2rss($rss,$url,$post);
  988. }
  989. }
  990. require Encode;
  991. return [200, ["Content-type" => "application/rss+xml"], [Encode::encode_utf8($rss->as_string)]];
  992. }
  993. sub _post2rss ($rss,$url,$post) {
  994. $rss->add_item(
  995. title => $post->{title},
  996. permaLink => $url,
  997. link => $url,
  998. enclosure => { url => $url, type=>"text/html" },
  999. description => "<![CDATA[$post->{data}]]>",
  1000. pubDate => DateTime->from_epoch(epoch => $post->{created} ), #TODO format like Thu, 23 Aug 1999 07:00:00 GMT
  1001. author => $post->{user}, #TODO translate to "email (user)" format
  1002. );
  1003. }
  1004. =head2 manual
  1005. Implements the /manual and /lib/* routes.
  1006. Basically a thin wrapper around Pod::Html.
  1007. =cut
  1008. sub manual ($query, $render_cb) {
  1009. require Pod::Html;
  1010. require Capture::Tiny;
  1011. return forbidden($query, $render_cb) unless grep { $_ eq 'admin' } @{$query->{acls}};
  1012. #Fix links from Pod::HTML
  1013. $query->{module} =~ s/\.html$//g if $query->{module};
  1014. my $infile = $query->{module} ? "$query->{module}.pm" : 'tCMS/Manual.pod';
  1015. return notfound($query,$render_cb) unless -f "lib/$infile";
  1016. my $content = capture { Pod::Html::pod2html(qw{--podpath=lib --podroot=.},"--infile=lib/$infile") };
  1017. my @series = _get_series(1);
  1018. return $render_cb->('manual.tx', {
  1019. title => 'tCMS Manual',
  1020. theme_dir => $td,
  1021. content => $content,
  1022. categories => \@series,
  1023. stylesheets => _build_themed_styles('post.css'),
  1024. });
  1025. }
  1026. # Deal with Params which may or may not be arrays
  1027. sub _coerce_array ($param) {
  1028. my $p = $param || [];
  1029. $p = [$param] if $param && (ref $param ne 'ARRAY');
  1030. return $p;
  1031. }
  1032. sub _build_themed_styles ($style) {
  1033. my @styles;
  1034. @styles = ("/styles/$style") if -f "www/styles/$style";
  1035. my $ts = _themed_style($style);
  1036. push(@styles, $ts) if $theme_dir && -f "www/$ts";
  1037. return \@styles;
  1038. }
  1039. sub _build_themed_scripts ($script) {
  1040. my @scripts = ("/scripts/$script");
  1041. my $ts = _themed_script($script);
  1042. push(@scripts, $ts) if $theme_dir && -f "www/$ts";
  1043. return \@scripts;
  1044. }
  1045. sub _build_themed_templates ($template) {
  1046. my @templates = ("/templates/$template");
  1047. my $ts = _themed_template($template);
  1048. push(@templates, $ts) if $theme_dir && -f "www/$ts";
  1049. return \@templates;
  1050. }
  1051. sub _pick_processor($file, $normal, $themed) {
  1052. return _dir_for_resource($file) eq $template_dir ? $normal : $themed;
  1053. }
  1054. # Pick appropriate dir based on whether theme override exists
  1055. sub _dir_for_resource ($resource) {
  1056. return $theme_dir && -f "www/$theme_dir/$resource" ? $theme_dir : $template_dir;
  1057. }
  1058. sub _themed_style ($resource) {
  1059. return _dir_for_resource("styles/$resource")."/styles/$resource";
  1060. }
  1061. sub _themed_script ($resource) {
  1062. return _dir_for_resource("scripts/$resource")."/scripts/$resource";
  1063. }
  1064. sub _themed_template ($resource) {
  1065. return _dir_for_resource("templates/$resource")."/templates/$resource";
  1066. }
  1067. 1;