| 1 | package AxKit::App::TABOO::XSP::Article; | ||
| 2 | use 5.6.0; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Apache::AxKit::Language::XSP::SimpleTaglib; | ||
| 6 | use Apache::AxKit::Exception; | ||
| 7 | use AxKit; | ||
| 8 | use AxKit::App::TABOO::Data::Article; | ||
| 9 | use AxKit::App::TABOO; | ||
| 10 | use Session; | ||
| 11 | use Time::Piece ':override'; | ||
| 12 | use XML::LibXML; | ||
| 13 | use IO::File; | ||
| 14 | use MIME::Type; | ||
| 15 | |||
| 16 | use vars qw/$NS/; | ||
| 17 | |||
| 18 | our $VERSION = '0.4'; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | AxKit::App::TABOO::XSP::Article - Article management tag library for TABOO | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | Add the story: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.: | ||
| 27 | |||
| 28 | <xsp:page | ||
| 29 | language="Perl" | ||
| 30 | xmlns:xsp="http://apache.org/xsp/core/v1" | ||
| 31 | xmlns:story="http://www.kjetil.kjernsmo.net/software/TABOO/NS/Article" | ||
| 32 | > | ||
| 33 | |||
| 34 | Add this taglib to AxKit (via httpd.conf or .htaccess): | ||
| 35 | |||
| 36 | AxAddXSPTaglib AxKit::App::TABOO::XSP::Article | ||
| 37 | |||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This XSP taglib provides tags to store information related to news | ||
| 42 | stories and to fetch and return XML representations of that data, as | ||
| 43 | it communicates with TABOO Data objects, particulary | ||
| 44 | L<AxKit::App::TABOO::Data::Article>. | ||
| 45 | |||
| 46 | L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write | ||
| 47 | this taglib. | ||
| 48 | |||
| 49 | =head1 TODO | ||
| 50 | |||
| 51 | This taglib will be documented further in upcoming releases. While | ||
| 52 | there is quite a lot of working stuff here, it also has some bad | ||
| 53 | issues. | ||
| 54 | |||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | |||
| 59 | $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Article'; | ||
| 60 | |||
| 61 | # Some constants | ||
| 62 | # TODO: This stuff should go somewhere else! | ||
| 63 | |||
| 64 | use constant GUEST => 0; | ||
| 65 | use constant NEWMEMBER => 1; | ||
| 66 | use constant MEMBER => 2; | ||
| 67 | use constant OLDTIMER => 3; | ||
| 68 | use constant ASSISTANT => 4; | ||
| 69 | use constant EDITOR => 5; | ||
| 70 | use constant ADMIN => 6; | ||
| 71 | use constant DIRECTOR => 7; | ||
| 72 | use constant GURU => 8; | ||
| 73 | use constant GOD => 9; | ||
| 74 | |||
| 75 | # Internal function to get a filename we can work with. | ||
| 76 | sub _sanatize_filename { | ||
| 77 | my $file = shift; | ||
| 78 | $file =~ tr#A-Za-z0-9+#_#cd; # Replace any non-base64 with underscores | ||
| 79 | return substr($file, 0, 29); # Return the 30 first chars. | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _write_file { | ||
| 83 | my %args = @_; | ||
| 84 | my $fh = new IO::File; | ||
| 85 | my $filename = $args{filename}; | ||
| 86 | my $ext = $args{extension} || 'txt'; | ||
| 87 | if ($args{upload}) { | ||
| 88 | ($filename, $ext) = $args{upload}->filename =~ m/^(.*)\.(\w*)$/; | ||
| 89 | } | ||
| 90 | $filename = _sanatize_filename($filename); | ||
| 91 | my $i = 0; | ||
| 92 | while (-e $args{docroot} ."/articles/content/$args{primcat}/$filename/$filename.$ext") { # Append an incrementing number if the file exists | ||
| 93 | $i++; | ||
| 94 | if ($i == 1) { | ||
| 95 | $filename .= '-' . $i; | ||
| 96 | } else { | ||
| 97 | $filename =~ s/-\d+$/-$i/; | ||
| 98 | } | ||
| 99 | die "Can't really be $i identical files here, can it?" if ($i >= 100); | ||
| 100 | } | ||
| 101 | my $lookupfile = $args{docroot} ."/articles/content/$args{primcat}/$filename/"; | ||
| 102 | my $dir; | ||
| 103 | foreach my $name (split('/', $lookupfile)) { # Make sure all dirs exist | ||
| 104 | $dir .= $name . '/'; | ||
| 105 | unless (-d $dir) { | ||
| 106 | mkdir ($dir, 0775) || die "Failed to create directory " . $dir; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $lookupfile .= "$filename.$ext"; | ||
| 110 | if ($fh->open("> " . $lookupfile)) { | ||
| 111 | if ($args{upload}) { | ||
| 112 | my $uploadedfh = $args{upload}->fh; | ||
| 113 | while (<$uploadedfh>) { | ||
| 114 | print $fh $_; | ||
| 115 | } | ||
| 116 | } else { | ||
| 117 | die "Error saving string to filename $filename.$ext" unless (($filename) && ($args{text})); | ||
| 118 | print $fh $args{text}; | ||
| 119 | } | ||
| 120 | $fh->close; | ||
| 121 | } else { die "Failed to open file at $lookupfile for writing" } | ||
| 122 | return $filename; | ||
| 123 | } | ||
| 124 | |||
| 125 | package AxKit::App::TABOO::XSP::Article::Handlers; | ||
| 126 | |||
| 127 | =head1 Tag Reference | ||
| 128 | |||
| 129 | |||
| 130 | =head2 C<E<lt>store-required/E<gt>> | ||
| 131 | |||
| 132 | This tag will check and store the data from the required fields. It | ||
| 133 | will check for either uploaded files, and the MIME types of that, or | ||
| 134 | take plain text and save it to a local file. It doesn't do many | ||
| 135 | security checks yet. | ||
| 136 | |||
| 137 | If the save was successful, it will redirect to C</articles/edit>, so | ||
| 138 | that the submitter can continue editing of non-required fields. To | ||
| 139 | control where it redirects to, you should supply an attribute or child | ||
| 140 | element C<redirect> containing the URL of the page to redirect to and | ||
| 141 | C<retval>, with the HTTP return value. The latter defaults to 302. | ||
| 142 | |||
| 143 | If something went wrong, the tag will return a nodelist | ||
| 144 | C<problem>. The nodelist will contain the data store name of the | ||
| 145 | field(s) that weren't present, or C<nosave> if the save itself failed. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | |||
| 150 | sub store_required : nodelist({http://www.kjetil.kjernsmo.net/software/TABOO/NS/Article/Output}art:problem) attribOrChild(redirect,retval) { | ||
| 151 | return << 'EOC' | ||
| 152 | my @errors = (); | ||
| 153 | foreach my $required (qw(title authorid description primcat code)) { | ||
| 154 | unless ($cgi->param($required)) { | ||
| 155 | push(@errors, $required); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | unless ((($cgi->param('text')) && ($cgi->param('filename'))) || ($cgi->param('upfile'))) { | ||
| 159 | foreach my $required (qw(text filename upfile)) { | ||
| 160 | unless ($cgi->param($required)) { | ||
| 161 | push(@errors, $required); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | unless (@errors) { # OK, so far, so good | ||
| 167 | my %args = map { $_ => $cgi->param($_) } $cgi->param; | ||
| 168 | |||
| 169 | my $upload = $cgi->upload; | ||
| 170 | if ($upload->size > 0) { | ||
| 171 | $args{'filename'} = AxKit::App::TABOO::XSP::Article::_write_file(upload=>$upload, primcat => $args{'primcat'}, docroot=>$r->document_root); | ||
| 172 | $args{'mimetype'} = $upload->type; | ||
| 173 | } else { | ||
| 174 | $args{'filename'} = AxKit::App::TABOO::XSP::Article::_write_file(filename=>$args{'filename'}, text=>$args{'text'}, primcat => $args{'primcat'}, docroot=>$r->document_root); | ||
| 175 | $args{'mimetype'} = 'text/plain'; | ||
| 176 | } | ||
| 177 | # TODO: Security sanity checks. | ||
| 178 | $args{'authorok'} = 1; # Is assumed for now | ||
| 179 | unless ($args{'date'}) { | ||
| 180 | my $timestamp = localtime; | ||
| 181 | $args{'date'} = $timestamp->datetime; | ||
| 182 | } | ||
| 183 | my $article = AxKit::App::TABOO::Data::Article->new(); | ||
| 184 | $article->populate(\%args, [[$args{'primcat'}, 'primcat']], [$args{'authorid'}]); | ||
| 185 | unless($article->save) { | ||
| 186 | push(@errors, 'nosave'); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | unless (@errors) { # Actually, everything went fine here, so we redirect to continue | ||
| 191 | $cgi->status($attr_retval || 302); | ||
| 192 | $cgi->err_headers_out->add("Location" => $attr_redirect || '/'); | ||
| 193 | Apache::exit($attr_retval || 302); | ||
| 194 | } else { | ||
| 195 | AxKit::Debug(9, "Things that went wrong: " . join(' ', @errors)); | ||
| 196 | } | ||
| 197 | @errors; | ||
| 198 | EOC | ||
| 199 | } | ||
| 200 | |||
| 201 | sub this_article : struct attribOrChild(primcat) { | ||
| 202 | return << 'EOC' | ||
| 203 | my %args = map { $_ => $cgi->param($_) } $cgi->param; | ||
| 204 | my $upload = $cgi->upload; | ||
| 205 | |||
| 206 | my $lookupurl = AxKit::App::TABOO::XSP::Article::_write_file($upload, $attr_primcat, $r->document_root); | ||
| 207 | $args{'format'} = $upload->type; | ||
| 208 | # TODO: Security sanity checks. | ||
| 209 | |||
| 210 | unless ($args{'date'}) { | ||
| 211 | my $timestamp = localtime; | ||
| 212 | $args{'date'} = $timestamp->datetime; | ||
| 213 | } | ||
| 214 | |||
| 215 | $args{'authorids'} = [$args{'authorid'}]; # Has to change to support more authors | ||
| 216 | my $article = AxKit::App::TABOO::Data::Article->new(); | ||
| 217 | $article->populate(\%args); | ||
| 218 | $article->adduserinfo(); | ||
| 219 | $article->addcatinfo(); | ||
| 220 | $article->addformatinfo(); | ||
| 221 | |||
| 222 | my $doc = XML::LibXML::Document->new(); | ||
| 223 | my $addel = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Article/Output', 'art:article-submission'); | ||
| 224 | $addel->setAttribute('contenturl', $lookupurl); | ||
| 225 | $doc->setDocumentElement($addel); | ||
| 226 | $article->write_xml($doc, $addel); # Return an XML representation | ||
| 227 | EOC | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | =head2 C<E<lt>get-article filename="foo" primcat="bar"/E<gt>> | ||
| 232 | |||
| 233 | This tag will get an XML representation of the article identified with | ||
| 234 | C<filename> and C<primcat>. The parameters may be given as attributes | ||
| 235 | or child elements. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | |||
| 239 | |||
| 240 | sub get_article : struct attribOrChild(filename,primcat) { | ||
| 241 | return << 'EOC' | ||
| 242 | AxKit::Debug(8, "We look for article with filename: '" . $attr_filename . "'"); | ||
| 243 | my $article = AxKit::App::TABOO::Data::Article->new(); | ||
| 244 | unless ($article->load(limit => { filename => $attr_filename })) { | ||
| 245 | throw Apache::AxKit::Exception::Retval( | ||
| 246 | return_code => 404, | ||
| 247 | -text => "Article with identifier $attr_filename not found"); | ||
| 248 | } | ||
| 249 | unless ($article->editorok && $article->authorok) { | ||
| 250 | my $session = AxKit::App::TABOO::session($r); | ||
| 251 | my $authlevel = AxKit::App::TABOO::authlevel($session); | ||
| 252 | if (!$authlevel) { | ||
| 253 | throw Apache::AxKit::Exception::Retval( | ||
| 254 | return_code => 401, | ||
| 255 | -text => "Not authorised with an authlevel"); | ||
| 256 | } | ||
| 257 | my $editinguser = AxKit::App::TABOO::loggedin($session); | ||
| 258 | unless (grep(/$editinguser/, @{$article->authorids}) || ($authlevel >= 5)) { | ||
| 259 | throw Apache::AxKit::Exception::Retval( | ||
| 260 | return_code => 403, | ||
| 261 | -text => "Authentication and higher priviliges required to load article"); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | $article->adduserinfo; | ||
| 266 | $article->addcatinfo; | ||
| 267 | $article->addformatinfo(); | ||
| 268 | |||
| 269 | my $lookupurl; | ||
| 270 | foreach my $ext ($article->mimetype->extensions) { | ||
| 271 | my $lookupfile = "/articles/content/$attr_primcat/$attr_filename/$attr_filename.$ext"; | ||
| 272 | warn $r->document_root . $lookupfile; | ||
| 273 | if (-r $r->document_root . $lookupfile) { | ||
| 274 | $lookupurl = 'http://' . $r->get_server_name . ':' . $r->get_server_port . $lookupfile; | ||
| 275 | } | ||
| 276 | last if $lookupurl; | ||
| 277 | } | ||
| 278 | AxKit::Debug(8, "Content really at: " . $lookupurl); | ||
| 279 | |||
| 280 | unless ($lookupurl) { | ||
| 281 | throw Apache::AxKit::Exception::Retval( | ||
| 282 | return_code => 404, | ||
| 283 | -text => "Content $lookupurl not found"); | ||
| 284 | } | ||
| 285 | |||
| 286 | my $doc = XML::LibXML::Document->new(); | ||
| 287 | my $rootel = $doc->createElement('taboo'); | ||
| 288 | $rootel->setAttribute('contenturl', $lookupurl); | ||
| 289 | # $rootel->setAttribute('type', 'article'); | ||
| 290 | # $rootel->setAttribute('origin', 'Article'); | ||
| 291 | $doc->setDocumentElement($rootel); | ||
| 292 | $article->write_xml($doc, $rootel); | ||
| 293 | EOC | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | 1; | ||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | =head1 FORMALITIES | ||
| 305 | |||
| 306 | See L<AxKit::App::TABOO>. | ||
| 307 | |||
| 308 | =cut |