HTML.pm 42 KB

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