An unfinished system to manage all your paper documentation in an easy way.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

autodoc.fcgi 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. printf("Status: %s\r\n", $code);
  40. printf("Content-type: %s\r\n", $type) if defined $type;
  41. printf("Content-length: %i\r\n", length($data)) if defined $data;
  42. printf("\r\n");
  43. print $data if defined $data;
  44. }
  45. sub load_conf {
  46. my($file) = @_;
  47. my $x='';
  48. open(F,"$file") || fatal_api_error(500,"Failed to load configuration file");
  49. while(<F>) { $x.=$_; }
  50. close(F);
  51. return from_json($x);
  52. }
  53. sub process_query {
  54. my($method, $path, $qs, $post, $user) = @_;
  55. my ($api_version, $path_main, $path_id, $path_sub) = @{$path};
  56. return api_error(404, "Unknown API version") if !defined $api_version;
  57. return api_error(405, "Unknown METHOD") if !defined $method;
  58. return api_error(404, "Unknown API function") if !defined $path_main;
  59. my $func = 'api_' . $api_version . '_' . $method . '_' . $path_main;
  60. $func .= '_id' if defined $path_id;
  61. $func .= '_'.$path_sub if defined $path_sub;
  62. print STDERR "FUNC=$map{$func}\n";
  63. return $map{$func}->($path_id, $qs, $post, $user) if exists $map{$func};
  64. return api_error(404, "Invalid path/method combination");
  65. }
  66. sub db_get_document_object {
  67. my($id) = @_;
  68. my $document;
  69. my @pages;
  70. my @pageids;
  71. my $q = sqlquery($dbh, "SELECT * FROM documents WHERE id = ?", $id);
  72. while(my $hash = $q->fetchrow_hashref()) { $document = $hash; }
  73. $q = sqlquery($dbh, "SELECT * FROM pages WHERE documentId = ?", $id);
  74. while(my $hash = $q->fetchrow_hashref()) { push @pages, $hash; push @pageids, $hash->{id}; }
  75. my %lang;
  76. foreach my $page ( @pages ) {
  77. $q = sqlquery($dbh, "SELECT * FROM pages_lang WHERE pageId = ?", $page->{id});
  78. while(my $hash = $q->fetchrow_hashref()) {
  79. $lang{$hash->{language}}++;
  80. }
  81. }
  82. my $out = {
  83. id => $document->{id},
  84. pageId => [ @pageids ],
  85. name => $document->{name},
  86. created => $document->{created},
  87. owner => $document->{owner},
  88. status => $document->{status},
  89. languages => [ keys %lang ],
  90. };
  91. return (200, "application/json", $out);
  92. }
  93. # create an empty document object.
  94. sub api_v1_POST_documents {
  95. my($id, $qs, $post, $user) = @_;
  96. my $q = sqlquery($dbh, "
  97. INSERT INTO documents
  98. SET
  99. owner = ?,
  100. status = 'nodata'
  101. ", $user);
  102. $q = sqlquery($dbh, "SELECT LAST_INSERT_ID()");
  103. while(my($lastid) = $q->fetchrow_array()) {
  104. $id = $lastid;
  105. }
  106. return db_get_document_object($id);
  107. }
  108. sub api_v1_POST_documents_id_data { return api_error(501,"Not yet implemented"); }
  109. sub api_v1_GET_documents_id_image { return api_error(501,"Not yet implemented"); }
  110. sub api_v1_GET_pages_image { return api_error(501,"Not yet implemented"); }
  111. # get a list of document objects
  112. sub api_v1_GET_documents {
  113. my($id, $qs, $post, $user) = @_;
  114. $qs->{pageSize} = $conf->{query}{pageSize} if !exists $qs->{pageSize};
  115. $qs->{pageIndex} = $conf->{query}{pageIndex} if !exists $qs->{pageIndex};
  116. my @out;
  117. my $q = sqlquery($dbh, "SELECT id FROM documents LIMIT ?,?",
  118. $qs->{pageSize} * $qs->{pageIndex},
  119. $qs->{pageSize});
  120. while(my ($id) = $q->fetchrow_array()) {
  121. my ($code, $ct, $body) = db_get_document_object($id);
  122. push @out, $body;
  123. }
  124. return (
  125. 200,
  126. "application/json",
  127. \@out
  128. );
  129. }
  130. # get a single document object
  131. sub api_v1_GET_documents_id {
  132. my($id, $qs, $post, $user) = @_;
  133. return db_get_document_object($id);
  134. }
  135. sub api_v1_GET_pages_id { return api_error(501,"Not yet implemented"); }
  136. # change document properties
  137. sub api_v1_PATCH_documents_id {
  138. my($id, $qs, $post, $user) = @_;
  139. if ( exists $qs->{addTags} ) {
  140. my $tags = get_array($qs->{addTags});
  141. foreach my $tag ( @{$tags} ) {
  142. sqlquery($dbh, "CALL tag_add(?,?)", $id, $tag);
  143. }
  144. }
  145. if ( exists $qs->{deleteTags} ) {
  146. my $tags = get_array($qs->{deleteTags});
  147. foreach my $tag ( @{$tags} ) {
  148. sqlquery($dbh, "CALL tag_del(?,?)", $id, $tag);
  149. }
  150. }
  151. if ( exists $qs->{name} ) {
  152. sqlquery($dbh, "UPDATE documents SET name = ? WHERE id = ?",
  153. $qs->{name}, $id);
  154. }
  155. return (200);
  156. }
  157. sub get_array {
  158. my($x) = @_;
  159. my @arr;
  160. if ( ref $x eq 'ARRAY' ) {
  161. @arr = @{$x};
  162. }
  163. else {
  164. @arr = [ $x ];
  165. }
  166. return \@arr;
  167. }
  168. sub fatal_api_error {
  169. my($code,$type,$body)=api_error(@_);
  170. printf("Status: %s\r\n", $code);
  171. printf("Content-type: %s\r\n", $type);
  172. printf("Content-length: %i\r\n", length($body));
  173. printf("\r\n");
  174. print $body;
  175. exit;
  176. }
  177. sub api_error {
  178. my($code, $text)=@_;
  179. my %deftext = (
  180. "000" => "An error produced an internal error, cascading errors over errors",
  181. "404" => "No such API path"
  182. );
  183. $code = "000" if !defined $code;
  184. $text = $deftext{$code} if ( !defined $text || $code eq "000" );
  185. return ( $code, "text/plain", $text . "\r\n" );
  186. }
  187. sub parse_querystring {
  188. my($in) = @_;
  189. my %out;
  190. if ( defined $in && length $in ) {
  191. foreach(split(/&/,$in)) {
  192. my($name,$value) = split(/=/);
  193. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  194. # handle arrays
  195. if ( exists $out{$name} ) {
  196. if ( ! ref $out{$name} ) {
  197. my $old = $out{$name};
  198. $out{$name} = [ $old ];
  199. }
  200. push @{$out{$name}}, $value;
  201. }
  202. else {
  203. $out{$name}=$value;
  204. }
  205. }
  206. }
  207. return \%out;
  208. }
  209. sub parse_post {
  210. my($fd, $len, $ct) = @_;
  211. my $data = '';
  212. while ( $len > 0 ) {
  213. my $buf;
  214. my $read = read($fd, $buf, $len);
  215. $len -= $read;
  216. $data .= $buf;
  217. }
  218. if ( $ct eq 'application/json' ) {
  219. my $tmp = from_json($data);
  220. $data = $tmp;
  221. }
  222. return $data;
  223. }
  224. sub sqlconnect {
  225. my($sql) = @_;
  226. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  227. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  228. fatal_api_error(500,"Failed to connect to database");
  229. return $dbh;
  230. }
  231. sub sqlquery {
  232. my $dbh = shift;
  233. my $query = shift;
  234. my @args = @_;
  235. my $sth = $dbh->prepare($query) || fatal_api_error(500,"Failed to execute SQL query");
  236. $sth->execute(@args) || fatal_api_error(500,"Failed to execute SQL query");
  237. return $sth;
  238. }