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.

autodoc.fcgi 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #!/usr/bin/perl
  2. use strict;
  3. use FCGI;
  4. use JSON;
  5. use DBI;
  6. use Data::Dumper;
  7. use warnings;
  8. $Data::Dumper::Sortkeys = 1;
  9. my $conf = load_conf("../etc/autodoc.json");
  10. my $dbh = sqlconnect($conf->{sql});
  11. my %map = (
  12. api_v1_POST_documents => \&api_v1_POST_documents,
  13. api_v1_POST_documents_id_data => \&api_v1_POST_documents_id_data,
  14. api_v1_GET_documents_id_image => \&api_v1_GET_documents_id_image,
  15. api_v1_GET_pages_image => \&api_v1_GET_pages_image,
  16. api_v1_GET_documents => \&api_v1_GET_documents,
  17. api_v1_GET_documents_id => \&api_v1_GET_documents_id,
  18. api_v1_GET_pages_id => \&api_v1_GET_pages_id,
  19. api_v1_PATCH_documents_id => \&api_v1_PATCH_documents_id,
  20. );
  21. my $request = FCGI::Request();
  22. while($request->Accept() >= 0) {
  23. my $user = $ENV{REMOTE_USER} || 'undefined';
  24. my $qs = parse_querystring($ENV{QUERY_STRING});
  25. my $method = $ENV{REQUEST_METHOD};
  26. my $path = [ split(/\//,$ENV{SCRIPT_NAME}) ] if exists $ENV{SCRIPT_NAME};
  27. shift(@{$path});
  28. my $post = parse_post(
  29. \*STDIN,
  30. exists $ENV{CONTENT_LENGTH} ? $ENV{CONTENT_LENGTH} : 0,
  31. exists $ENV{CONTENT_TYPE} ? $ENV{CONTENT_TYPE} : 0
  32. );
  33. my($code, $type, $data) = process_query($method, $path, $qs, $post, $user);
  34. if ( defined $type ) {
  35. if ( $type eq 'application/json' ) {
  36. $data = to_json($data, { utf8 => 1, pretty => 1 });
  37. }
  38. }
  39. send_response($code, $type, $data);
  40. }
  41. sub send_response {
  42. my ($code, $type, $data) = @_;
  43. printf("Status: %s\r\n", $code);
  44. printf("Content-type: %s\r\n", $type) if defined $type;
  45. printf("Content-length: %i\r\n", length($data)) if defined $data;
  46. printf("\r\n");
  47. print $data if defined $data;
  48. }
  49. sub fatal_api_error {
  50. my($code,$type,$body)=api_error(@_);
  51. send_response($code, $type, $body);
  52. exit;
  53. }
  54. sub load_conf {
  55. my($file) = @_;
  56. my $x='';
  57. open(F,"$file") || fatal_api_error(500,"Failed to load configuration file");
  58. while(<F>) { $x.=$_; }
  59. close(F);
  60. return from_json($x);
  61. }
  62. sub process_query {
  63. my($method, $path, $qs, $post, $user) = @_;
  64. my ($api_version, $path_main, $path_id, $path_sub) = @{$path};
  65. return api_error(404, "Unknown API version") if !defined $api_version;
  66. return api_error(405, "Unknown METHOD") if !defined $method;
  67. return api_error(404, "Unknown API function") if !defined $path_main;
  68. my $func = 'api_' . $api_version . '_' . $method . '_' . $path_main;
  69. $func .= '_id' if defined $path_id;
  70. $func .= '_'.$path_sub if defined $path_sub;
  71. print STDERR "FUNC=$map{$func}\n";
  72. return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func};
  73. return api_error(404, "Invalid path/method combination");
  74. }
  75. sub db_get_document_object {
  76. my($id) = @_;
  77. my $document;
  78. my @pages;
  79. my @pageids;
  80. my @tags;
  81. my $q = sqlquery($dbh, "SELECT * FROM documents WHERE id = ?", $id);
  82. while(my $hash = $q->fetchrow_hashref()) { $document = $hash; }
  83. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  84. while(my $hash = $q->fetchrow_hashref()) { push @pages, $hash; push @pageids, $hash->{id}; }
  85. $q = sqlquery($dbh, "
  86. SELECT tags.tag AS tag
  87. FROM documents_tags LEFT JOIN tags ON documents_tags.tagId = tags.id
  88. WHERE documentId = ? ORDER BY tag", $id);
  89. while(my ($tag) = $q->fetchrow_array()) { push @tags, $tag; }
  90. my %lang;
  91. foreach my $page ( @pages ) {
  92. $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $page->{id});
  93. while(my $hash = $q->fetchrow_hashref()) {
  94. $lang{$hash->{language}}++;
  95. }
  96. }
  97. if ( defined $document ) {
  98. my $out = {
  99. id => $document->{id},
  100. pageId => [ @pageids ],
  101. name => $document->{name},
  102. created => $document->{created},
  103. owner => $document->{owner},
  104. status => $document->{status},
  105. languages => [ keys %lang ],
  106. tags => [ @tags ],
  107. };
  108. return (200, "application/json", $out);
  109. }
  110. return (404, "application/json", "");
  111. }
  112. sub db_get_page_object {
  113. my($id) = @_;
  114. my $out;
  115. my %lang;
  116. my $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $id);
  117. while(my $hash = $q->fetchrow_hashref()) {
  118. $lang{$hash->{language}}++;
  119. }
  120. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  121. while(my $hash = $q->fetchrow_hashref()) {
  122. $out = {
  123. id => $id,
  124. documentId => $hash->{documentId},
  125. name => $hash->{name},
  126. created => $hash->{created},
  127. owner => $hash->{owner},
  128. status => $hash->{status},
  129. language => [ keys %lang ],
  130. };
  131. }
  132. if ( exists $out->{id} ) {
  133. return (200, "application/json", $out);
  134. }
  135. return (404, "application/json", "");
  136. }
  137. # create an empty document object.
  138. sub api_v1_POST_documents {
  139. my($id, $qs, $post, $user) = @_;
  140. my $q = sqlquery($dbh, "
  141. INSERT INTO documents
  142. SET
  143. owner = ?,
  144. status = 'nodata'
  145. ", $user);
  146. $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  147. while(my($lastid) = $q->fetchrow_array()) {
  148. $id = $lastid;
  149. }
  150. return db_get_document_object($id);
  151. }
  152. sub api_v1_POST_documents_id_data { return api_error(501,"Not yet implemented"); }
  153. sub api_v1_GET_documents_id_image { return api_error(501,"Not yet implemented"); }
  154. sub api_v1_GET_pages_image { return api_error(501,"Not yet implemented"); }
  155. # get a list of document objects
  156. sub api_v1_GET_documents {
  157. my($id, $qs, $post, $user) = @_;
  158. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  159. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  160. my @out;
  161. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  162. $qs->{pageSize} * $qs->{pageIndex},
  163. $qs->{pageSize});
  164. while(my ($id) = $q->fetchrow_array()) {
  165. my ($code, $ct, $body) = db_get_document_object($id);
  166. push @out, $body;
  167. }
  168. return (
  169. 200,
  170. "application/json",
  171. \@out
  172. );
  173. }
  174. # get a single document object
  175. sub api_v1_GET_documents_id {
  176. my($id, $qs, $post, $user) = @_;
  177. return db_get_document_object($id);
  178. }
  179. # get a specific page
  180. sub api_v1_GET_pages_id {
  181. my($id, $qs, $post, $user) = @_;
  182. return db_get_page_object($id);
  183. }
  184. # change document properties
  185. sub api_v1_PATCH_documents_id {
  186. my($id, $qs, $post, $user) = @_;
  187. if ( exists $qs->{addTags} ) {
  188. my $tags = get_array($qs->{addTags});
  189. foreach my $tag ( @{$tags} ) {
  190. sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag);
  191. }
  192. }
  193. if ( exists $qs->{deleteTags} ) {
  194. my $tags = get_array($qs->{deleteTags});
  195. foreach my $tag ( @{$tags} ) {
  196. sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag);
  197. }
  198. }
  199. if ( exists $qs->{name} ) {
  200. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  201. $qs->{name}, $id);
  202. }
  203. return (200);
  204. }
  205. sub get_array {
  206. my($x) = @_;
  207. my @arr;
  208. if ( ref $x eq 'ARRAY' ) {
  209. @arr = @{$x};
  210. }
  211. else {
  212. @arr = [ $x ];
  213. }
  214. return \@arr;
  215. }
  216. sub api_error {
  217. my($code, $text)=@_;
  218. my %deftext = (
  219. "000" => "An error produced an internal error, cascading errors over errors",
  220. "404" => "No such API path"
  221. );
  222. $code = "000" if !defined $code;
  223. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  224. return ( $code, "text/plain", $text . "\r\n" );
  225. }
  226. sub parse_querystring {
  227. my($in) = @_;
  228. my %out;
  229. if ( defined $in && length $in ) {
  230. foreach(split(/&/,$in)) {
  231. my($name,$value) = split(/=/);
  232. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  233. # handle arrays
  234. if ( exists $out{$name} ) {
  235. if ( ! ref $out{$name} ) {
  236. my $old = $out{$name};
  237. $out{$name} = [ $old ];
  238. }
  239. push @{$out{$name}}, $value;
  240. }
  241. else {
  242. $out{$name}=$value;
  243. }
  244. }
  245. }
  246. return \%out;
  247. }
  248. sub parse_post {
  249. my($fd, $len, $ct) = @_;
  250. my $data = '';
  251. while ( $len > 0 ) {
  252. my $buf;
  253. my $read = read($fd, $buf, $len);
  254. $len -= $read;
  255. $data .= $buf;
  256. }
  257. if ( $ct eq 'application/json' ) {
  258. my $tmp = from_json($data);
  259. $data = $tmp;
  260. }
  261. return $data;
  262. }
  263. sub sqlconnect {
  264. my($sql) = @_;
  265. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  266. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  267. fatal_api_error(500,"Failed to connect to database");
  268. return $dbh;
  269. }
  270. sub sqlquery {
  271. my $dbh = shift;
  272. my $query = shift;
  273. my @args = @_;
  274. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  275. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  276. return $sth;
  277. }