HTML.pm 42 KB

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