pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Diff pastebin - collaborative debugging tool View Help


Posted by Anonymous on Tue 11 Sep 00:23
report abuse | download | new post

  1. Index: templates/page.tmpl
  2. ===================================================================
  3. --- templates/page.tmpl ()
  4. +++ templates/page.tmpl ()
  5.  -58,6 +58,26 @@
  6.  <TMPL_VAR CONTENT>
  7.  </div>
  8.  
  9. +<TMPL_IF NAME="ATTACH_FORM">
  10. +<div id="attachments">
  11. +  <form action="<TMPL_VAR CGIURL>" enctype="multipart/form-data" method="post">
  12. +    <input type="hidden" name="do" value="attach" />
  13. +    <input type="hidden" name="pagename" value="<TMPL_VAR PAGENAME>" />
  14. +    <div>Attachment: <input type="file" name="datafile" size="40"></div>
  15. +    <input type="submit" value="Attach">
  16. +  </form>
  17. +</div>
  18. +</TMPL_IF>
  19. +
  20. +<TMPL_IF NAME=ATTACHMENTS>
  21. +  <h2>Attachments</h2>
  22. +  <ul>
  23. +  <TMPL_LOOP NAME=ATTACHMENTS>
  24. +    <li><a href='<TMPL_VAR NAME=ATTACHMENT>'><TMPL_VAR NAME=ATTACHMENT></a></li>
  25. +  </TMPL_LOOP>
  26. +  </ul>
  27. +</TMPL_IF>
  28. +
  29.  <div id="footer">
  30.  <div id="pageinfo">
  31.  
  32. Index: IkiWiki/Plugin/attach.pm
  33. ===================================================================
  34. --- IkiWiki/Plugin/attach.pm    ()
  35. +++ IkiWiki/Plugin/attach.pm    ()
  36.  -0,0 +1,196 @@
  37. +#!/usr/bin/perl
  38. +package IkiWiki::Plugin::attach;
  39. +
  40. +use strict;
  41. +use IkiWiki 2.00;
  42. +
  43. +our ($dir, $max_kbs, $srcdir_max_kbs, $mime_strategy, %mime_allow, %mime_deny, $DEFAULT_MAX_KBS, %want_form);
  44. +
  45. +sub import { #{{{
  46. +  hook(type => "checkconfig",  id=>"attach",   call => \&checkconfig);
  47. +        hook(type => "sessioncgi",   id => "attach", call => \&attach);
  48. +  hook(type => "pagetemplate", id => "attach", call => \&pagetemplate);
  49. +  hook(type => "preprocess",   id => "attach", call => \&preprocess);
  50. +} # }}}
  51. +
  52. +sub checkconfig {
  53. +  my $config = $config{attach};
  54. +  return unless $config{attach}{enabled} == 1;
  55. +  
  56. +  $DEFAULT_MAX_KBS = 1024; #Maximumn size in kilobytes of each upload
  57. +    
  58. +  $max_kbs = ($config{attach}{max_kbs} >= 0 && $config{attach}{max_kbs} =~ /^\d+$/) ?
  59. +                $config{attach}{max_kbs} : $DEFAULT_MAX_KBS;
  60. +  $config{attach}{dir} ||= 'attachments';
  61. +  $dir     = $config{srcdir}.'/.'.$config{attach}{dir};
  62. +  unless (-e $dir) {
  63. +    mkdir $dir or error(gettext("Can't create attachment directory: ").$!);
  64. +  }
  65. +  unless ($mime_strategy =~ /^(allow,deny)|(deny,allow)|$/) {
  66. +    error(gettext("Invalid MIME strategy specified"));
  67. +  }
  68. +  #TODO: Support regexps in MIME type names
  69. +  %mime_allow = map { $_ => 1 } split(/\s+?/, $config{attach}{mime_allow});
  70. +  %mime_deny = map  { $_ => 1 } split(/\s+?/, $config{attach}{mime_deny});
  71. +}
  72. +
  73. +
  74. +sub attach ($) {
  75. +  my ($q,$session) = @_;
  76. +  return unless $q->param('do') eq 'attach';
  77. +  if (!$config{attach}{enabled}) {
  78. +    error(gettext("Uploads are disabled"));
  79. +  }
  80. +
  81. +  my $filename = $q->param('datafile') or error(gettext("You must specify a file to attach"));
  82. +  
  83. +  unless ($pagesources{ $q->param('pagename') }) {
  84. +    error(gettext("Invalid page"));  
  85. +  }
  86. +
  87. +  #If 'every_page' isn't set, the page must contain the 'attach' directive.
  88. +  #I don't know a clean way to do this, so will just use a regex to check. :-(
  89. +  if (!$config{attach}{every_page}) {
  90. +    my $page;
  91. +    eval { $page = readfile( srcfile( $q->param('pagename') ).'.mdwn' ) };
  92. +    unless (defined($page) && $page=~ /\[\[attach \]\]/mg) {
  93. +      error(gettext("Uploads to this page are disabled"));
  94. +    }
  95. +  }
  96. +  
  97. +  #This may return a spoofed or undefined MIME type; we can check it again after the upload
  98. +  mime_ok( $q->upload_info($filename, 'mime') );
  99. +
  100. +  #This may return a spoofed value; we can check it again after the upload
  101. +  size_ok( $q->upload_info($filename, 'size') );
  102. +  
  103. +  ip_ok();
  104. +
  105. +  my $new_filename = $filename;
  106. +  $new_filename =~ s/[^[:alnum:]._:-]//g;
  107. +  my $ok = $q->upload( $filename, $dir.'/'.$new_filename );
  108. +  error(gettext("Upload failed: ").$new_filename) unless $ok;
  109. +
  110. +
  111. +  #Post upload checks here
  112. +  
  113. +  #If a file's attached to the main page of the wiki, the pagename is 'index'
  114. +  #We want these files to reside in the top-level directory; not an 'index'
  115. +  #subdirectory, so we special-case this.
  116. +  
  117. +  my $srcdir_target = $config{srcdir}.'/'.$q->param('pagename');
  118. +  $srcdir_target =~ s/\/index$//;
  119. +  my $target_filename = $q->param('pagename').'/'.$new_filename;
  120. +  $target_filename =~ s/^index\///;  
  121. +
  122. +
  123. +  unless (-d $srcdir_target) {
  124. +    mkdir $srcdir_target or error(gettext("Failed to create target directory"));
  125. +  }
  126. +  rename($dir.'/'.$new_filename, $srcdir_target.'/'.$new_filename) or error($!);
  127. +  
  128. +  #(Pilfered from IkiWiki/CGI.pm):
  129. +  if ($config{rcs}) {
  130. +    my $message="Attaching to ".$q->param('pagename');
  131. +    my $rcs_token = IkiWiki::rcs_prepedit($target_filename);
  132. +    eval { IkiWiki::rcs_add($target_filename) };
  133. +              
  134. +    # Prevent deadlock with post-commit hook by
  135. +               # signaling to it that it should not try to
  136. +               # do anything (except send commit mails).
  137. +               IkiWiki::disable_commit_hook();
  138. +               my $conflict=IkiWiki::rcs_commit($target_filename, $message,
  139. +                               $rcs_token,
  140. +                               $session->param('name'), $ENV{REMOTE_ADDR});
  141. +               IkiWiki::enable_commit_hook();
  142. +               IkiWiki::rcs_update();
  143. +    error(gettext("Attachment conflicted: %s",$conflict)) if defined($conflict);
  144. +       }
  145. +    
  146. +  #Copies file to destdir
  147. +  IkiWiki::render($target_filename) or error("render failed");
  148. +  #Makes file dependency of page
  149. +  IkiWiki::add_depends($q->param('pagename'), $target_filename);
  150. +  #Re-renders the page so it knows about the new file
  151. +  IkiWiki::refresh();
  152. +  #Write what we just did to the index so future attachments work, too
  153. +  IkiWiki::saveindex();
  154. +  
  155. +  #TODO: Inline this message into template
  156. +  print $q->header;
  157. +  print "Attached <a href='".$config{url}.'/'.$target_filename."'>$new_filename</a> to ".
  158. +        "<a href='".$config{url}.'/'.$q->param('pagename')."?updated'>".$q->param('pagename')."</a>";
  159. +  exit;
  160. +}
  161. +
  162. +sub mime_ok {
  163. +  my $mime_type = shift;
  164. +  if ($mime_strategy eq 'allow,deny') {
  165. +    if (!$mime_allow{$mime_type}) {
  166. +      error(gettext("Forbidden MIME type"));
  167. +    }
  168. +  }
  169. +  elsif ($mime_strategy eq 'deny,allow') {
  170. +    if ($mime_deny{$mime_type} && !$mime_allow{$mime_type}) {
  171. +      error(gettext("Forbidden MIME type"));
  172. +    }
  173. +  }
  174. +}
  175. +
  176. +sub size_ok {
  177. + my $size = shift;
  178. + return if $max_kbs == 0; #No size limit
  179. + if (($size / 1024) > $max_kbs) {
  180. +    error(gettext("The attachment is too big."));
  181. +  }
  182. +}
  183. +
  184. +sub ip_ok {
  185. + my @ban_ips = split /\s+?/, $config{attach}{ban_ips}; #Make global?
  186. +  for my $ip_regex (@ban_ips) {
  187. +    error(gettext("Banned IP address")) if $ENV{REMOTE_ADDR} =~ $ip_regex;
  188. +  }
  189. +}
  190. +
  191. +sub pagetemplate {
  192. +  my %params = @_;
  193. +
  194. +  my (@loop_data, %attachments); #TODO: make %attachments GLOBAL
  195. +  my $pagepath = $config{srcdir}.'/'.$params{page};
  196. +  
  197. +  if (-d $pagepath) { #Potentially has attachments
  198. +    opendir(DIR,$pagepath) or warn $!;
  199. +    while (my $f = readdir(DIR)) {
  200. +      next if $f =~ /^\.+?$/;
  201. +      next if $f =~ /\.mdwn$/;
  202. +      next unless -f $pagepath.'/'.$f;
  203. +      $attachments{ $params{page} }{ $f } = 1;
  204. +    }
  205. +
  206. +    for my $attachment (sort keys %{ $attachments{ $params{page} }}) {
  207. +      #TODO: FIX THIS
  208. +      my %useless;
  209. +      $useless{'ATTACHMENT'} = $attachment;
  210. +      #push @loop_data, \%useless;
  211. +      push @loop_data, {'ATTACHMENT' => $attachment}
  212. +    }
  213. +  }
  214. +
  215. +  my $template = $params{template};
  216. +  $template->param( 'CGIURL' => $config{cgiurl} );
  217. +  $template->param('ATTACHMENTS' =>  \@loop_data);
  218. +  
  219. +  if ($config{attach}{enabled} && ($want_form{ $params{'destpage'} } || $config{attach}{'every_page'})) {
  220. +    $template->param('ATTACH_FORM' => 1);
  221. +  }
  222. +}
  223. +
  224. +sub preprocess {
  225. +  my %params = @_;
  226. +  unless ($params{'preview'}) {
  227. +    $want_form{ $params{'destpage'} }++;
  228. +    $want_form{ $params{'page'}     }++;
  229. +  }
  230. +  return '';
  231. +}
  232. +1

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post