static-hybrid.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
  2. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. use v5.10;
  18. AddModuleDescription('static-hybrid.pl', 'Static Hybrid Module');
  19. our ($q, $Now, %Action, %Page, %IndexHash, @IndexList, $OpenPageName, $ScriptName, $FS, $RCName, $DeletedPage, $UsePathInfo, $CommentsPrefix, $Message, $KeepDays, $EmbedWiki, $ClusterMapPage, %NearLinksUsed);
  20. our ($StaticDir, $StaticAlways, %StaticMimeTypes, $StaticUrl,
  21. %StaticLinkedPages, @StaticIgnoredPages);
  22. $Action{static} = \&DoStatic;
  23. $StaticDir = '' unless defined $StaticDir;
  24. $StaticUrl = '' unless defined $StaticUrl; # change this!
  25. $StaticAlways = 0 unless defined $StaticAlways;
  26. # 1 = uploaded files only, 2 = all pages
  27. my $StaticMimeTypes = '/etc/http/mime.types'; # all-ASCII characters
  28. my %StaticFiles;
  29. my $StaticAction = 0; # Are we doing action or not?
  30. my @StaticQueue = ();
  31. my $ClusterHasChanged = 0;
  32. my $PageBeingSaved = "";
  33. sub DoStatic {
  34. $StaticAction = 1;
  35. return unless UserIsAdminOrError();
  36. my $raw = GetParam('raw', 0);
  37. if ($raw) {
  38. print GetHttpHeader('text/plain');
  39. } else {
  40. print GetHeader('', T('Static Copy'), '');
  41. }
  42. CreateDir($StaticDir);
  43. %StaticMimeTypes = StaticMimeTypes() unless %StaticMimeTypes;
  44. %StaticFiles = ();
  45. my $id = GetParam('id', '');
  46. if ($id) {
  47. local *GetDownloadLink = \&StaticGetDownloadLink;
  48. StaticWriteFile($id);
  49. } else {
  50. StaticWriteFiles();
  51. }
  52. print '</p>' unless $raw;
  53. PrintFooter() unless $raw;
  54. }
  55. sub StaticMimeTypes {
  56. my %hash;
  57. # the default mapping matches the default @UploadTypes...
  58. open(my $F, '<', $StaticMimeTypes)
  59. or return ('image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif');
  60. while (<$F>) {
  61. s/\#.*//; # remove comments
  62. my($type, $ext) = split;
  63. $hash{$type} = $ext if $ext;
  64. }
  65. close($F);
  66. return %hash;
  67. }
  68. sub StaticWriteFiles {
  69. my $raw = GetParam('raw', 0);
  70. local *GetDownloadLink = \&StaticGetDownloadLink;
  71. foreach my $id (AllPagesList()) {
  72. SetParam('rcclusteronly',0);
  73. if (! grep(/^$id$/,@StaticIgnoredPages)) {
  74. StaticWriteFile($id);
  75. }
  76. }
  77. }
  78. sub StaticGetDownloadLink {
  79. my ($name, $image, $revision, $alt) = @_; # ignore $revision
  80. $alt = $name unless $alt;
  81. my $id = FreeToNormal($name);
  82. AllPagesList();
  83. # if the page does not exist
  84. return '[' . ($image ? 'image' : 'link') . ':' . $name . ']' unless $IndexHash{$id};
  85. if ($image) {
  86. my $result = $q->img({-src=>StaticFileName($id), -alt=>$alt, -class=>'upload', -loading=>'lazy'});
  87. $result = ScriptLink($id, $result, 'image');
  88. return $result;
  89. } else {
  90. return ScriptLink($id, $alt, 'upload');
  91. }
  92. }
  93. sub StaticFileName {
  94. my $id = shift;
  95. $id =~ s/ /_/g;
  96. $id =~ s/#.*//; # remove named anchors for the filename test
  97. return $StaticFiles{$id} if $StaticFiles{$id}; # cache filenames
  98. my ($status, $data) = ReadFile(GetPageFile(StaticUrlDecode($id)));
  99. print "cannot read " . GetPageFile(StaticUrlDecode($id)) . $q->br() unless $status;
  100. my $hash = ParseData($data);
  101. my $ext = '.html';
  102. if ($hash->{text} =~ /^\#FILE ([^ \n]+)\n(.*)/s) {
  103. $ext = $StaticMimeTypes{$1};
  104. $ext = '.' . $ext if $ext;
  105. }
  106. $StaticFiles{$id} = $id . $ext;
  107. return $StaticFiles{$id};
  108. }
  109. sub StaticUrlDecode {
  110. my $str = shift;
  111. $str =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/eg;
  112. return $str;
  113. }
  114. sub StaticWriteFile {
  115. my $id = shift;
  116. my $raw = GetParam('raw', 0);
  117. my $html = GetParam('html', 1);
  118. OpenPage($id);
  119. my ($mimetype, $data) = $Page{text} =~ /^\#FILE ([^ \n]+)\n(.*)/s;
  120. return unless $html or $data;
  121. my $filename = StaticFileName($id);
  122. open(my $F, '>', encode_utf8("$StaticDir/$filename")) or ReportError(Ts('Cannot write %s', $filename));
  123. if ($data) {
  124. StaticFile($id, $mimetype, $data, $F);
  125. } elsif ($html) {
  126. StaticHtml($id, $F);
  127. }
  128. close($F);
  129. chmod 0644,"$StaticDir/$filename";
  130. if (lc(GetParam('action','')) eq "static") {
  131. print $filename, $raw ? "\n" : $q->br();
  132. }
  133. }
  134. sub StaticFile {
  135. my ($id, $type, $data, $F) = @_;
  136. require MIME::Base64;
  137. binmode($F);
  138. print $F MIME::Base64::decode($data);
  139. }
  140. sub StaticHtml {
  141. my $id = FreeToNormal(shift);
  142. my $F = shift;
  143. my $title = $id;
  144. $title =~ s/_/ /g;
  145. local *GetHttpHeader = \&StaticGetHttpHeader;
  146. local *GetCommentForm = \&StaticGetCommentForm;
  147. %NearLinksUsed = ();
  148. # Isolate our output
  149. local *STDERR;
  150. open(STDERR, '>', '/dev/null');
  151. # Process the page
  152. local $Message = "";
  153. # encoding is left off, so fix it:
  154. my $result = ToString(sub {
  155. print qq!<?xml version="1.0" encoding="UTF-8" ?>!;
  156. print GetHeader($id, QuoteHtml($id), undef, "");
  157. print $q->start_div({-class=> 'content browse'});
  158. print PageHtml($id);
  159. print $q->end_div();
  160. SetParam('rcclusteronly', $id) if (FreeToNormal(GetCluster($Page{text})) eq $id);
  161. if (($id eq $RCName) || (T($RCName) eq $id) || (T($id) eq $RCName)
  162. || GetParam('rcclusteronly', '')) {
  163. print $q->start_div({-class=>'rc'});;
  164. print $q->hr() if not GetParam('embed', $EmbedWiki);
  165. DoRc(\&GetRcHtml);
  166. print $q->end_div();
  167. }
  168. PrintFooter($id);
  169. });
  170. print $F $result;
  171. return;
  172. }
  173. *StaticFilesOldDoPost = \&DoPost;
  174. *DoPost = \&StaticFilesNewDoPost;
  175. sub StaticFilesNewDoPost {
  176. my $id = FreeToNormal(shift);
  177. OpenPage($id);
  178. my $old_cluster = FreeToNormal(GetCluster($Page{text}));
  179. StaticFilesOldDoPost($id);
  180. my $new_cluster = FreeToNormal(GetCluster($Page{text}));
  181. $ClusterHasChanged = 1 if ($old_cluster ne $new_cluster);
  182. if ($StaticAlways) {
  183. # always delete
  184. StaticDeleteFile($OpenPageName);
  185. if ($Page{text} =~ /^\#FILE / # if a file was uploaded
  186. or $StaticAlways > 1) {
  187. CreateDir($StaticDir);
  188. # If new Page added, update index
  189. if (! $IndexHash{$OpenPageName} ) {
  190. push(@IndexList, $OpenPageName);
  191. $IndexHash{$OpenPageName} = 1;
  192. }
  193. StaticWriteFile($OpenPageName);
  194. $PageBeingSaved = $OpenPageName;
  195. AddLinkedFilesToQueue($OpenPageName);
  196. StaticWriteLinkedFiles();
  197. }
  198. }
  199. }
  200. *StaticOldDeletePage = \&DeletePage;
  201. *DeletePage = \&StaticNewDeletePage;
  202. sub StaticNewDeletePage {
  203. my $id = shift;
  204. StaticDeleteFile($id) if ($StaticAlways);
  205. return StaticOldDeletePage($id);
  206. }
  207. sub StaticDeleteFile {
  208. my $id = shift;
  209. %StaticMimeTypes = StaticMimeTypes() unless %StaticMimeTypes;
  210. # we don't care if the files or $StaticDir don't exist -- just delete!
  211. for my $f (map { "$StaticDir/$id.$_" } (values %StaticMimeTypes, 'html')) {
  212. Unlink($f); # delete copies with different extensions
  213. }
  214. }
  215. # override the default!
  216. sub GetDownloadLink {
  217. my ($name, $image, $revision, $alt) = @_;
  218. $alt = $name unless $alt;
  219. my $id = FreeToNormal($name);
  220. AllPagesList();
  221. # if the page does not exist
  222. return '[' . ($image ? T('image') : T('download')) . ':' . $name
  223. . ']' . GetEditLink($id, '?', 1) unless $IndexHash{$id};
  224. my $action;
  225. if ($revision) {
  226. $action = "action=download;id=" . UrlEncode($id) . ";revision=$revision";
  227. } elsif ($UsePathInfo) {
  228. $action = "download/" . UrlEncode($id);
  229. } else {
  230. $action = "action=download;id=" . UrlEncode($id);
  231. }
  232. if ($image) {
  233. if ($UsePathInfo and not $revision) {
  234. if ($StaticAlways and $StaticUrl) {
  235. my $url = $StaticUrl;
  236. my $img = UrlEncode(StaticFileName($id));
  237. $url =~ s/\%s/$img/g or $url .= $img;
  238. $action = $url;
  239. } else {
  240. $action = $ScriptName . '/' . $action;
  241. }
  242. } else {
  243. $action = $ScriptName . '?' . $action;
  244. }
  245. my $result = $q->img({-src=>$action, -alt=>$alt, -class=>'upload', -loading=>'lazy'});
  246. $result = ScriptLink(UrlEncode($id), $result, 'image') unless $id eq $OpenPageName;
  247. return $result;
  248. } else {
  249. return ScriptLink($action, $alt, 'upload');
  250. }
  251. }
  252. # override function from Image Extension to support advanced image tags
  253. sub ImageGetInternalUrl{
  254. my $id = shift;
  255. if ($UsePathInfo) {
  256. if ($StaticAlways and $StaticUrl) {
  257. my $url = $StaticUrl;
  258. my $img = UrlEncode(StaticFileName($id));
  259. $url =~ s/\%s/$img/g or $url .= $img;
  260. return $url;
  261. } else {
  262. return $ScriptName . '/download/' . UrlEncode($id);
  263. }
  264. }
  265. return $ScriptName . '?action=download;id=' . UrlEncode($id);
  266. }
  267. sub AddLinkedFilesToQueue {
  268. my $id = shift;
  269. foreach my $pattern (keys %StaticLinkedPages) {
  270. if ($id =~ /$pattern/) {
  271. AddNewFilesToQueue(@{$StaticLinkedPages{$pattern}})
  272. }
  273. }
  274. # If you modify a comment page, then update the original
  275. # Don't check for recursive updates - the only thing that
  276. # changed was the CommentCount - no reason to waste time
  277. if ($id =~ /^$CommentsPrefix(.*)/) {
  278. my $match = $1;
  279. push(@StaticQueue,$match);
  280. }
  281. # If the page added belongs to a cluster, update the cluster's page
  282. # and the $ClusterMapPage
  283. # especially important with the clustermap module
  284. local %Page;
  285. local $OpenPageName = '';
  286. OpenPage($id);
  287. my $cluster = FreeToNormal(GetCluster($Page{text}));
  288. # Only move up the cluster hierarchy if the page we originally
  289. # edited has a cluster
  290. if ($PageBeingSaved = $id) {
  291. if ($cluster ne "" && $cluster ne $id) {
  292. AddNewFilesToQueue($cluster);
  293. # If we are using clustermaps then update
  294. # ClusterMapPage
  295. # But only if cluster has changed
  296. if ($ClusterHasChanged) {
  297. if ($ClusterMapPage ne "") {
  298. AddNewFilesToQueue($ClusterMapPage);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. sub StaticWriteLinkedFiles {
  305. my $raw = GetParam('raw', 0);
  306. my $writeRC = 0;
  307. local *GetDownloadLink = \&StaticGetDownloadLink;
  308. foreach my $id (@StaticQueue) {
  309. if (! grep(/^$id$/,@StaticIgnoredPages)) {
  310. StaticWriteFile($id);
  311. SetParam('rcclusteronly',0);
  312. }
  313. }
  314. }
  315. sub StaticGetCommentForm {
  316. my ($id, $rev, $comment) = @_;
  317. if ($CommentsPrefix ne '' and $id and $rev ne 'history' and $rev ne 'edit'
  318. and $OpenPageName =~ /^$CommentsPrefix/) {
  319. return $q->div({-class=>'comment'}, GetFormStart(undef, undef, 'comment'),
  320. $q->p(GetHiddenValue('title', $OpenPageName),
  321. GetTextArea('aftertext', $comment)),
  322. $q->p(T('Username:'), ' ',
  323. $q->textfield(-name=>'username', -default=>'',
  324. -override=>1, -size=>20, -maxlength=>50),
  325. T('Homepage URL:'), ' ',
  326. $q->textfield(-name=>'homepage', -default=>'',
  327. -override=>1, -size=>40, -maxlength=>100)),
  328. $q->p($q->submit(-name=>'Save', -accesskey=>T('s'), -value=>T('Save')), ' ',
  329. $q->submit(-name=>'Preview', -value=>T('Preview'))),
  330. $q->end_form());
  331. }
  332. return '';
  333. }
  334. sub StaticGetHttpHeader {
  335. return;
  336. }
  337. sub AddNewFilesToQueue {
  338. # Add a file to queue, but only if not already there
  339. my @ids = @_;
  340. foreach my $id (@ids) {
  341. if (! grep(/^$id$/,@StaticQueue)) {
  342. push(@StaticQueue,$id);
  343. AddLinkedFilesToQueue($id);
  344. }
  345. }
  346. }
  347. # Make rollback compatible
  348. *StaticOldDoRollback = \&DoRollback;
  349. *DoRollback = \&StaticNewDoRollback;
  350. $Action{rollback} = \&StaticNewDoRollback;
  351. # Delete the static file so that changes made during a rollback are propogated
  352. sub StaticNewDoRollback {
  353. my $page = shift;
  354. my $to = GetParam('to', 0);
  355. ReportError(T('Missing target for rollback.'), '400 BAD REQUEST') unless $to;
  356. ReportError(T('Target for rollback is too far back.'), '400 BAD REQUEST') unless $page or RollbackPossible($to);
  357. ReportError(T('A username is required for ordinary users.'), '403 FORBIDDEN') unless GetParam('username', '') or UserIsEditor();
  358. my @ids = ();
  359. if (not $page) { # cannot just use list length because of ('')
  360. return unless UserIsAdminOrError(); # only admins can do mass changes
  361. my %ids = map { my ($ts, $id) = split(/$FS/); $id => 1; } # make unique via hash
  362. GetRcLines($Now - $KeepDays * 86400, 1); # 24*60*60
  363. @ids = keys %ids;
  364. } else {
  365. @ids = ($page);
  366. }
  367. RequestLockOrError();
  368. print GetHeader('', T('Rolling back changes')), $q->start_div({-class=>'content rollback'}), $q->start_p();
  369. foreach my $id (@ids) {
  370. OpenPage($id);
  371. my ($text, $minor, $ts) = GetTextAtTime($to);
  372. if ($Page{text} eq $text) {
  373. print T("The two revisions are the same."), $q->br() if $page; # no message when doing mass revert
  374. } elsif (!UserCanEdit($id, 1)) {
  375. print Ts('Editing not allowed for %s.', $id), $q->br();
  376. } else {
  377. Save($id, $text, Ts('Rollback to %s', TimeToText($to)), $minor, ($Page{host} ne $q->remote_addr()));
  378. StaticDeleteFile($id);
  379. print Ts('%s rolled back', GetPageLink($id)), ($ts ? ' ' . Ts('to %s', TimeToText($to)) : ''), $q->br();
  380. }
  381. }
  382. WriteRcLog('[[rollback]]', '', $to) unless $page; # leave marker for DoRc() if mass rollback
  383. print $q->end_p() . $q->end_div();
  384. ReleaseLock();
  385. PrintFooter();
  386. }
  387. *StaticOldDespamPage = \&DespamPage;
  388. *DespamPage = \&StaticNewDespamPage;
  389. sub StaticNewDespamPage {
  390. my $rule = shift;
  391. # from DoHistory()
  392. my @revisions = sort {$b <=> $a} map { m|/([0-9]+).kp$|; $1; } GetKeepFiles($OpenPageName);
  393. foreach my $revision (@revisions) {
  394. my ($revisionPage, $rev) = GetTextRevision($revision, 1); # quiet
  395. if (not $rev) {
  396. print ': ' . Ts('Cannot find revision %s.', $revision);
  397. return;
  398. } elsif (not DespamBannedContent($revisionPage->{text})) {
  399. my $summary = Tss('Revert to revision %1: %2', $revision, $rule);
  400. print ': ' . $summary;
  401. Save($OpenPageName, $revisionPage->{text}, $summary) unless GetParam('debug', 0);
  402. StaticDeleteFile($OpenPageName);
  403. return;
  404. }
  405. }
  406. if (grep(/^1$/, @revisions) or not @revisions) { # if there is no kept revision, yet
  407. my $summary = Ts($rule). ' ' . Ts('Marked as %s.', $DeletedPage);
  408. print ': ' . $summary;
  409. Save($OpenPageName, $DeletedPage, $summary) unless GetParam('debug', 0);
  410. StaticDeleteFile($OpenPageName);
  411. } else {
  412. print ': ' . T('Cannot find unspammed revision.');
  413. }
  414. }