An unfinished system to manage all your paper documentation in an easy way.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
vor 5 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #!/usr/bin/perl
  2. use strict;
  3. use FCGI;
  4. use JSON;
  5. use DBI;
  6. use utf8;
  7. use GD::Simple;
  8. use Data::Dumper;
  9. use Data::UUID;
  10. use warnings;
  11. $Data::Dumper::Sortkeys = 1;
  12. my $conf = load_conf("../etc/autodoc.json");
  13. my $dbh = sqlconnect($conf->{sql});
  14. my %map = (
  15. api_v1_POST_documents => \&api_v1_POST_documents,
  16. api_v1_POST_documents_id_data => \&api_v1_POST_documents_id_data,
  17. api_v1_GET_documents_id_image => \&api_v1_GET_documents_id_image,
  18. api_v1_GET_pages_id_image => \&api_v1_GET_pages_id_image,
  19. api_v1_GET_documents => \&api_v1_GET_documents,
  20. api_v1_GET_documents_id => \&api_v1_GET_documents_id,
  21. api_v1_GET_pages_id => \&api_v1_GET_pages_id,
  22. api_v1_PATCH_documents_id => \&api_v1_PATCH_documents_id,
  23. api_v1_GET_tags => \&api_v1_GET_tags,
  24. api_v1_POST_tags => \&api_v1_POST_tags,
  25. api_v1_PATCH_tags_id => \&api_v1_PATCH_tags_id,
  26. api_v1_DELETE_tags_id => \&api_v1_DELETE_tags_id
  27. );
  28. my $request = FCGI::Request();
  29. while($request->Accept() >= 0) {
  30. my $user = $ENV{REMOTE_USER} || 'undefined';
  31. my $qs = parse_querystring($ENV{QUERY_STRING});
  32. my $method = $ENV{REQUEST_METHOD};
  33. # QS sanity check
  34. if ( exists $qs->{id} && $qs->{id} !~ /^\d+$/ ) {
  35. fatal_api_error(400,"invalid id");
  36. }
  37. if ( exists $qs->{maxWidth} && $qs->{maxWidth} !~ /^\d+$/ ) {
  38. fatal_api_error(400,"invalid maxWidth");
  39. }
  40. if ( exists $qs->{maxHeight} && $qs->{maxHeight} !~ /^\d+$/ ) {
  41. fatal_api_error(400,"invalid maxHeight");
  42. }
  43. if ( exists $qs->{pageSize} && $qs->{pageSize} !~ /^\d+$/ ) {
  44. fatal_api_error(400,"invalid pageSize");
  45. }
  46. if ( exists $qs->{pageIndex} && $qs->{pageIndex} !~ /^\d+$/ ) {
  47. fatal_api_error(400,"invalid pageIndex");
  48. }
  49. my $path = [ split(/\//,$ENV{SCRIPT_NAME}) ] if exists $ENV{SCRIPT_NAME};
  50. shift(@{$path});
  51. my $post = parse_post(
  52. \*STDIN,
  53. exists $ENV{CONTENT_LENGTH} ? $ENV{CONTENT_LENGTH} : 0,
  54. exists $ENV{CONTENT_TYPE} ? $ENV{CONTENT_TYPE} : 0
  55. );
  56. my($code, $hdr, $type, $data) = process_query($method, $path, $qs, $post, $user);
  57. print STDERR Dumper("code",$code,"hdr", $hdr, "type", $type, "data", $data);
  58. $hdr = [ ] if !defined $hdr;
  59. if ( defined $type ) {
  60. if ( $type eq 'application/json' ) {
  61. $data = to_json($data, { utf8 => 1, pretty => 1 });
  62. }
  63. }
  64. send_response($code, $hdr, $type, $data);
  65. }
  66. sub gen_uuid {
  67. my $ug = Data::UUID->new;
  68. return lc($ug->create_str());
  69. }
  70. sub send_response {
  71. my ($code, $hdr, $type, $data) = @_;
  72. printf("Status: %s\r\n", $code);
  73. printf("Content-type: %s\r\n", $type) if defined $type;
  74. printf("Content-length: %i\r\n", length($data)) if defined $data;
  75. foreach ( @{$hdr} ) { printf("%s\r\n",$_); }
  76. printf("\r\n");
  77. print $data if defined $data;
  78. }
  79. sub fatal_api_error {
  80. my($code,$hdr,$type,$body)=api_error(@_);
  81. print STDERR "AUTODOC_FATAL: $body\r\n";
  82. send_response($code, $hdr, $type, $body);
  83. exit;
  84. }
  85. sub load_conf {
  86. my($file) = @_;
  87. my $x='';
  88. open(F,"$file") || fatal_api_error(500,"Failed to load configuration file");
  89. while(<F>) { $x.=$_; }
  90. close(F);
  91. return from_json($x);
  92. }
  93. sub process_query {
  94. my($method, $path, $qs, $post, $user) = @_;
  95. my ($api_version, $path_main, $path_id, $path_sub) = @{$path};
  96. return api_error(404, "Unknown API version") if !defined $api_version;
  97. return api_error(405, "Unknown METHOD") if !defined $method;
  98. return api_error(404, "Unknown API function") if !defined $path_main;
  99. my $func = 'api_' . $api_version . '_' . $method . '_' . $path_main;
  100. $func .= '_id' if defined $path_id;
  101. $func .= '_'.$path_sub if defined $path_sub;
  102. return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func};
  103. return api_error(404, "Invalid path/method combination");
  104. }
  105. sub db_get_document_object {
  106. my($id) = @_;
  107. my $document;
  108. my @pages;
  109. my @pageids;
  110. my @tags;
  111. my $q = sqlquery($dbh, "SELECT * FROM documents WHERE id = ?", $id);
  112. while(my $hash = $q->fetchrow_hashref()) { $document = $hash; }
  113. $q = sqlquery($dbh, "
  114. SELECT p.*
  115. FROM pages p
  116. JOIN documents_pages dp
  117. ON p.id = dp.pageId
  118. WHERE dp.documentId = ?", $id);
  119. while(my $hash = $q->fetchrow_hashref()) {
  120. if ( $hash->{isprimary} ) {
  121. $document->{primary} = $hash->{id};
  122. }
  123. push @pages, $hash; push @pageids, $hash->{id};
  124. }
  125. $q = sqlquery($dbh, "
  126. SELECT tags.tag AS name, tags.color AS color
  127. FROM documents_tags JOIN tags ON documents_tags.tagId = tags.id
  128. WHERE documentId = ? ORDER BY tag", $id);
  129. while(my ($tag) = $q->fetchrow_hashref()) { push @tags, $tag; }
  130. my %lang;
  131. foreach my $page ( @pages ) {
  132. $q = sqlquery($dbh, "
  133. SELECT
  134. SUM(pw.count) AS count,
  135. l.short AS short
  136. FROM pages_words pw
  137. JOIN words w ON pw.wordID = w.id
  138. JOIN lang l ON w.langId = l.id
  139. WHERE pw.pageId = ?
  140. GROUP BY l.id", $page->{id});
  141. while(my $h = $q->fetchrow_hashref()) {
  142. $lang{$h->{short}}=$h->{count};
  143. }
  144. }
  145. if ( defined $document ) {
  146. my $out = {
  147. id => $document->{id},
  148. pageId => [ @pageids ],
  149. primaryPage => $document->{primary},
  150. name => $document->{name},
  151. created => $document->{created},
  152. owner => $document->{owner},
  153. status => $document->{status},
  154. languages => [ keys %lang ],
  155. tags => [ @tags ],
  156. };
  157. return (200, undef, "application/json", $out);
  158. }
  159. return (404, undef, "application/json", "");
  160. }
  161. sub db_get_page_object {
  162. my($id) = @_;
  163. my $out;
  164. my %lang;
  165. my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id);
  166. while(my $hash = $q->fetchrow_hashref()) {
  167. $lang{$hash->{language}}++;
  168. }
  169. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  170. while(my $hash = $q->fetchrow_hashref()) {
  171. $out = {
  172. id => $id,
  173. documentId => $hash->{documentId},
  174. name => $hash->{name},
  175. created => $hash->{created},
  176. owner => $hash->{owner},
  177. status => $hash->{status},
  178. language => [ keys %lang ],
  179. };
  180. }
  181. if ( exists $out->{id} ) {
  182. return (200, "application/json", $out);
  183. }
  184. return (404, undef, "application/json", "");
  185. }
  186. sub get_page_image {
  187. my($id, $qs) = @_;
  188. my $wh;
  189. my $size;
  190. if ( exists $qs->{maxWidth} ) {
  191. $wh = 'w';
  192. $size = $qs->{maxWidth};
  193. }
  194. elsif ( exists $qs->{maxHeight} ) {
  195. $wh = 'h';
  196. $size = $qs->{maxWidth};
  197. }
  198. my $src;
  199. my $dst;
  200. if ( !defined $id ) {
  201. $src = sprintf("%s/%s",
  202. $conf->{path}{global}, $conf->{path}{error_img});
  203. }
  204. else {
  205. $src = sprintf("%s/%s/%s.jpeg",
  206. $conf->{path}{global}, $conf->{path}{original}, $id);
  207. }
  208. if ( defined $wh && defined $size ) {
  209. if ( defined $id ) {
  210. $dst = sprintf("%s/%s/%s-%s-%s.jpeg",
  211. $conf->{path}{global}, $conf->{path}{cache}, $id, $wh, $size);
  212. }
  213. else {
  214. $dst = sprintf("%s/%s/error_img-%s-%s.jpeg",
  215. $conf->{path}{global}, $conf->{path}{cache}, $wh, $size);
  216. }
  217. }
  218. else {
  219. if ( defined $id ) {
  220. $dst = sprintf("%s/%s/%s.jpeg",
  221. $conf->{path}{global}, $conf->{path}{cache}, $id);
  222. }
  223. else {
  224. $dst = sprintf("%s/%s/error_img.jpeg",
  225. $conf->{path}{global}, $conf->{path}{cache});
  226. }
  227. }
  228. my $imgfile;
  229. if ( !defined $dst ) {
  230. $imgfile = $src;
  231. }
  232. else {
  233. if ( ! -r $dst ) {
  234. my $cmd;
  235. if ( defined $wh && defined $size ) {
  236. $cmd = sprintf("convert %s -resize %s%s %s",
  237. $src,
  238. $wh eq 'h' ? 'x' : '',
  239. $size,
  240. $dst
  241. );
  242. }
  243. else {
  244. $cmd = sprintf("cp %s %s", $src, $dst);
  245. }
  246. print STDERR "CMD=$cmd\n";
  247. system($cmd);
  248. }
  249. $imgfile = $dst;
  250. }
  251. my $url = $dst;
  252. $url =~ s/$conf->{path}{global}//;
  253. return $url;
  254. }
  255. # create an empty document object.
  256. sub api_v1_POST_documents {
  257. my($id, $qs, $post, $user) = @_;
  258. my $docid;
  259. my $q = sqlquery($dbh, "CALL create_document(?)", $user);
  260. while(my($id) = $q->fetchrow_array()) {
  261. $docid = $id;
  262. }
  263. return db_get_document_object($docid);
  264. }
  265. sub api_v1_POST_documents_id_data {
  266. my($id, $qs, $post, $user) = @_;
  267. my $uuid = gen_uuid();
  268. my $ext = $post->{ctype};
  269. $ext =~ s/.*\///;
  270. my $file = sprintf("%s/%s/%s_%s.%s",
  271. $conf->{path}{global},
  272. $conf->{path}{queue},
  273. $id, $uuid, $ext);
  274. open(F,'>'.$file);
  275. print F $post->{data};
  276. close(F);
  277. return (200, undef, "application/json", { })
  278. }
  279. # get document image/thumbnail
  280. sub api_v1_GET_documents_id_image {
  281. my($id, $qs, $post, $user) = @_;
  282. my $pageid;
  283. my $q = sqlquery($dbh, "CALL get_primary_page(?)", $id);
  284. while(my($myid)=$q->fetchrow_array()) {
  285. $pageid = $myid;
  286. }
  287. return api_v1_GET_pages_id_image($pageid, $qs, $post, $user);
  288. }
  289. # get page image/thumbnail
  290. sub api_v1_GET_pages_id_image {
  291. my($id, $qs, $post, $user) = @_;
  292. #return (200, "image/jpeg", get_page_image($id, $qs));
  293. my $url = get_page_image($id, $qs);
  294. return (302, [ "Location: $url" ]);
  295. }
  296. # get a list of document objects
  297. sub api_v1_GET_documents {
  298. my($id, $qs, $post, $user) = @_;
  299. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  300. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  301. my @out;
  302. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  303. $qs->{pageSize} * $qs->{pageIndex},
  304. $qs->{pageSize});
  305. while(my ($id) = $q->fetchrow_array()) {
  306. my ($code, $hdr, $ct, $body) = db_get_document_object($id);
  307. push @out, $body;
  308. }
  309. return (
  310. 200,
  311. undef,
  312. "application/json",
  313. \@out
  314. );
  315. }
  316. # get a single document object
  317. sub api_v1_GET_documents_id {
  318. my($id, $qs, $post, $user) = @_;
  319. return db_get_document_object($id);
  320. }
  321. # get a specific page
  322. sub api_v1_GET_pages_id {
  323. my($id, $qs, $post, $user) = @_;
  324. return db_get_page_object($id);
  325. }
  326. # change document properties
  327. sub api_v1_PATCH_documents_id {
  328. my($id, $qs, $post, $user) = @_;
  329. if ( exists $qs->{addTags} ) {
  330. my $tags = get_array($qs->{addTags});
  331. foreach my $tag ( @{$tags} ) {
  332. sqlquery($dbh, "CALL add_tag(?,?)", $id, $tag);
  333. }
  334. }
  335. if ( exists $qs->{deleteTags} ) {
  336. my $tags = get_array($qs->{deleteTags});
  337. foreach my $tag ( @{$tags} ) {
  338. sqlquery($dbh, "CALL delete_tag(?,?)", $id, $tag);
  339. }
  340. }
  341. if ( exists $qs->{name} ) {
  342. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  343. $qs->{name}, $id);
  344. }
  345. if ( exists $qs->{primarypage} ) {
  346. sqlquery($dbh, "CALL set_primary_page(?)", $qs->{primarypage});
  347. }
  348. return (200);
  349. }
  350. # handle tags
  351. sub api_v1_GET_tags {
  352. my($id, $qs, $post, $user) = @_;
  353. my @tags;
  354. my $q = sqlquery($dbh, "SELECT * FROM tags ORDER by tag DESC");
  355. while(my $h = $q->fetchrow_hashref()) {
  356. push @tags, {
  357. id => $h->{id},
  358. name => $h->{tag},
  359. color => $h->{color}
  360. }
  361. }
  362. return (200, undef, "application/json", \@tags);
  363. }
  364. sub api_v1_POST_tags {
  365. my($id, $qs, $post, $user) = @_;
  366. sqlquery($dbh, "INSERT INTO tags SET id=UUID(), tag=?, color=?", $qs->{tagName}, $qs->{tagColor});
  367. return (200);
  368. }
  369. sub api_v1_PATCH_tags_id {
  370. my($id, $qs, $post, $user) = @_;
  371. sqlquery($dbh, "UPDATE tags SET tag=?, color=? WHERE id=?", $qs->{tagName}, $qs->{tagColor}, $id);
  372. return (200);
  373. }
  374. sub api_v1_DELETE_tags_id {
  375. my($id, $qs, $post, $user) = @_;
  376. sqlquery($dbh, "DELETE FROM tags WHERE id=?", $id);
  377. return (200);
  378. }
  379. sub get_array {
  380. my($x) = @_;
  381. my @arr;
  382. if ( ref $x eq 'ARRAY' ) {
  383. @arr = @{$x};
  384. }
  385. else {
  386. @arr = [ $x ];
  387. }
  388. return \@arr;
  389. }
  390. sub api_error {
  391. my($code, $text)=@_;
  392. my %deftext = (
  393. "000" => "An error produced an internal error, cascading errors over errors",
  394. "404" => "No such API path"
  395. );
  396. $code = "000" if !defined $code;
  397. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  398. return ( $code, undef, "text/plain", $text . "\r\n" );
  399. }
  400. sub parse_querystring {
  401. my($in) = @_;
  402. my %out;
  403. if ( defined $in && length $in ) {
  404. foreach(split(/&/,$in)) {
  405. my($name,$value) = split(/=/);
  406. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  407. # handle arrays
  408. if ( exists $out{$name} ) {
  409. if ( ! ref $out{$name} ) {
  410. my $old = $out{$name};
  411. $out{$name} = [ $old ];
  412. }
  413. push @{$out{$name}}, $value;
  414. }
  415. else {
  416. $out{$name}=$value;
  417. }
  418. }
  419. }
  420. return \%out;
  421. }
  422. sub parse_post {
  423. my($fd, $len, $ct) = @_;
  424. my $data = '';
  425. while ( $len > 0 ) {
  426. my $buf;
  427. my $read = read($fd, $buf, $len);
  428. $len -= $read;
  429. $data .= $buf;
  430. }
  431. if ( $ct eq 'application/json' ) {
  432. my $tmp = from_json($data);
  433. $data = $tmp;
  434. }
  435. return { ctype => $ct, len => $len, data => $data};
  436. }
  437. sub sqlconnect {
  438. my($sql) = @_;
  439. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  440. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}, { mysql_enable_utf8 => 1 }) || \
  441. fatal_api_error(500,"Failed to connect to database");
  442. return $dbh;
  443. }
  444. sub sqlquery {
  445. my $dbh = shift;
  446. my $query = shift;
  447. my @args = @_;
  448. #print STDERR "$query\n";
  449. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  450. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  451. return $sth;
  452. }