OSDN Git Service

335fa11550ec86448f8ab4694043b42b32bbb9c1
[libre10/libre10.git] / pdfmanager.pl
1 #!/usr/bin/perl
2 use utf8;
3 use strict;
4 use warnings;
5 use CGI;
6 use CGI::Carp qw(fatalsToBrowser);
7 use DBD::SQLite;
8 use DBI;
9 use Data::Dumper;
10 use Encode 'decode';
11 use File::Slurp;
12 use YAML::XS;
13
14 #
15 #   Libre10
16 #
17 #   Copyright 2013 yukikaze, long.inus
18 #   Licensed under the Apache License, Version 2.0 (the "License");
19 #   you may not use this file except in compliance with the License.
20 #   You may obtain a copy of the License at
21 #
22 #       http://www.apache.org/licenses/LICENSE-2.0
23 #
24 #   Unless required by applicable law or agreed to in writing, software
25 #   distributed under the License is distributed on an "AS IS" BASIS,
26 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 #   See the License for the specific language governing permissions and
28 #   limitations under the License.
29 #
30
31 my $cgi      = new CGI;
32 my $mode     = $cgi->param('mode');
33 my $confdata = YAML::XS::LoadFile(
34         -e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );
35 my $temppath = "/tmp/libre10/tmp" . generate_unique_key();
36 my $dbh      = DBI->connect( "dbi:SQLite:dbname=$confdata->{dburl}",
37         "", "", { RaiseError => 1, AutoCommit => 1 } );
38 my $pdflist = $dbh->selectall_hashref(
39 "SELECT id, path, page, title, part, genre, startpage, endpage, showpagediff, indexpage
40         FROM pdffile"
41         , 'id'
42 );
43 mkdir '/tmp/libre10' unless ( -e '/tmp/libre10' );
44
45 print $cgi->header( -charset => 'utf-8' );
46 if ( $mode eq 'partpdf' ) {
47         my $pdfid = $cgi->param('pdfid');
48         my $pdftk = $cgi->param('pdftk');
49
50         # sanitize
51         $pdftk =~ s/[^0-9\-]//;
52
53         my $pdfpath = $pdflist->{$pdfid}->{path};
54         exit unless ( -e $pdfpath );
55         system "pdftk '$pdfpath' cat '$pdftk' output '$temppath.pdf'";
56         my $length = -s "$temp:q:path.pdf";
57         print "Content-Type: application/pdf\n";
58         print "Content-Length: $length\n";
59         print "\n";
60         print read_file("$temppath.pdf");
61         exit;
62 }
63 elsif ( $mode eq 'showpagediff' ) {
64         my $dnum  = $cgi->int( param("diff") );
65         my $title = $cgi->param("title");
66         if ( $dnum > 0 ) {
67                 my $sqls =
68                   "UPDATE pdffile SET showpagediff = $dnum WHERE title_group=$title";
69                 $dbs = $dbh->prepare($sqls);
70                 $dbs->execute();
71         }
72 }
73 else {
74         list_all_pdf();
75 }
76
77 sub list_all_pdf {
78         print
79 qq {Partial PDF = ./pdfmanager.pl?mode=partpdf&pdfid=68fc6a6a1cdd79227d0f2fe2f03dd510&pdftk=765-765\n};
80         my $ary_ref = $dbh->selectall_arrayref(
81 "SELECT id, path, page, title, part, genre, startpage, endpage, showpagediff, indexpage
82                 FROM pdffile"
83                 , { Slice => {} }
84         );
85         print qq {<table>\n};
86         print qq {<tr>\n};
87         print qq {<th>id</th>\n};
88         print qq {<th>path</th>\n};
89         print qq {<th>page</th>\n};
90         print qq {<th>title</th>\n};
91         print qq {<th>part</th>\n};
92         print qq {<th>genre</th>\n};
93         print qq {<th>start</th>\n};
94         print qq {<th>end</th>\n};
95
96         #print qq {<th>showpagediff</th>\n};
97         #print qq {<th>indexpage</th>\n};
98         print qq {</tr>\n};
99         foreach my $line ( @{$ary_ref} ) {
100                 print qq {<tr>\n};
101                 print
102 qq {<td><a href="./htmlserver.pl?pdfpath=$line->{id}&amp;pagenum=1">$line->{id}</a></td>\n};
103                 print qq {<td>$line->{path}</td>\n};
104                 print qq {<td>$line->{page}</td>\n};
105                 print qq {<td>$line->{title}</td>\n};
106                 print qq {<td>$line->{part}</td>\n};
107                 print qq {<td>$line->{genre}</td>\n};
108                 print qq {<td>$line->{startpage}</td>\n};
109                 print qq {<td>$line->{endpage}</td>\n};
110
111                 #print qq {<td>$line->{showpagediff}</td>\n};
112                 #print qq {<td>$line->{indexpage}</td>\n};
113                 print qq {</tr>\n};
114         }
115         print qq {</table>\n};
116 }
117 $dbh . disconnect;
118
119 sub generate_unique_key {
120         my $ukey;
121         my @str = ( 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
122
123         foreach ( 1 .. 8 ) {
124                 $ukey .= $str[ int( rand(@str) ) ];
125         }
126         return $ukey;
127 }
128