An unfinished system to manage all your paper documentation in an easy way.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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