PageRenderTime 22ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/api/soap/mc_file_api.php

https://github.com/fusenigk/mantisbt-1
PHP | 222 lines | 198 code | 14 blank | 10 comment | 17 complexity | c6c7a1a5cfefbcabf48d14e58cd4e568 MD5 | raw file
  1. <?php
  2. # MantisConnect - A webservice interface to Mantis Bug Tracker
  3. # Copyright (C) 2004-2011 Victor Boctor - vboctor@users.sourceforge.net
  4. # This program is distributed under dual licensing. These include
  5. # GPL and a commercial licenses. Victor Boctor reserves the right to
  6. # change the license of future releases.
  7. # See docs/ folder for more details
  8. # Check if the current user can download attachments for the specified bug.
  9. function mci_file_can_download_bug_attachments( $p_bug_id, $p_user_id ) {
  10. $t_can_download = access_has_bug_level( config_get( 'download_attachments_threshold' ), $p_bug_id );
  11. if( $t_can_download ) {
  12. return true;
  13. }
  14. $t_reported_by_me = bug_is_user_reporter( $p_bug_id, $p_user_id );
  15. return( $t_reported_by_me && config_get( 'allow_download_own_attachments' ) );
  16. }
  17. # Read a local file and return its content.
  18. function mci_file_read_local( $p_diskfile ) {
  19. $t_handle = fopen( $p_diskfile, "r" );
  20. $t_content = fread( $t_handle, filesize( $p_diskfile ) );
  21. fclose( $t_handle );
  22. return $t_content;
  23. }
  24. # Write a local file.
  25. function mci_file_write_local( $p_diskfile, $p_content ) {
  26. $t_handle = fopen( $p_diskfile, "w" );
  27. fwrite( $t_handle, $p_content );
  28. fclose( $t_handle );
  29. }
  30. function mci_file_add( $p_id, $p_name, $p_content, $p_file_type, $p_table, $p_title = '', $p_desc = '' ) {
  31. if( !file_type_check( $p_name ) ) {
  32. return new soap_fault( 'Client', '', 'File type not allowed.' );
  33. }
  34. if( !file_is_name_unique( $p_name, $p_id ) ) {
  35. return new soap_fault( 'Client', '', 'Duplicate filename.' );
  36. }
  37. $t_file_size = strlen( $p_content );
  38. $t_max_file_size = (int) min( ini_get_number( 'upload_max_filesize' ), ini_get_number( 'post_max_size' ), config_get( 'max_file_size' ) );
  39. if( $t_file_size > $t_max_file_size ) {
  40. return new soap_fault( 'Client', '', 'File is too big.' );
  41. }
  42. if( 'bug' == $p_table ) {
  43. $t_project_id = bug_get_field( $p_id, 'project_id' );
  44. $t_issue_id = bug_format_id( $p_id );
  45. } else {
  46. $t_project_id = $p_id;
  47. $t_issue_id = 0;
  48. }
  49. # prepare variables for insertion
  50. $c_issue_id = db_prepare_int( $t_issue_id );
  51. $c_project_id = db_prepare_int( $t_project_id );
  52. $c_file_type = db_prepare_string( $p_file_type );
  53. $c_title = db_prepare_string( $p_title );
  54. $c_desc = db_prepare_string( $p_desc );
  55. if( $t_project_id == ALL_PROJECTS ) {
  56. $t_file_path = config_get( 'absolute_path_default_upload_folder' );
  57. } else {
  58. $t_file_path = project_get_field( $t_project_id, 'file_path' );
  59. if( $t_file_path == '' ) {
  60. $t_file_path = config_get( 'absolute_path_default_upload_folder' );
  61. }
  62. }
  63. $c_file_path = db_prepare_string( $t_file_path );
  64. $c_new_file_name = db_prepare_string( $p_name );
  65. $t_file_hash = $t_issue_id;
  66. $t_disk_file_name = $t_file_path . file_generate_unique_name( $t_file_hash . '-' . $p_name, $t_file_path );
  67. $c_disk_file_name = db_prepare_string( $t_disk_file_name );
  68. $t_file_size = strlen( $p_content );
  69. $c_file_size = db_prepare_int( $t_file_size );
  70. $t_method = config_get( 'file_upload_method' );
  71. switch( $t_method ) {
  72. case FTP:
  73. case DISK:
  74. if( !file_exists( $t_file_path ) || !is_dir( $t_file_path ) || !is_writable( $t_file_path ) || !is_readable( $t_file_path ) ) {
  75. return new soap_fault( 'Server', '', "Upload folder '{$t_file_path}' doesn't exist." );
  76. }
  77. file_ensure_valid_upload_path( $t_file_path );
  78. if( !file_exists( $t_disk_file_name ) ) {
  79. mci_file_write_local( $t_disk_file_name, $p_content );
  80. if( FTP == $t_method ) {
  81. $conn_id = file_ftp_connect();
  82. file_ftp_put( $conn_id, $t_disk_file_name, $t_disk_file_name );
  83. file_ftp_disconnect( $conn_id );
  84. file_delete_local( $t_disk_file_name );
  85. } else {
  86. chmod( $t_disk_file_name, config_get( 'attachments_file_permissions' ) );
  87. }
  88. $c_content = "''";
  89. }
  90. break;
  91. case DATABASE:
  92. $c_content = db_prepare_binary_string( $p_content );
  93. break;
  94. }
  95. $t_file_table = db_get_table( $p_table . '_file' );
  96. $c_id = ( 'bug' == $p_table ) ? $c_issue_id : $c_project_id;
  97. $query = "INSERT INTO $t_file_table
  98. (" . $p_table . "_id, title, description, diskfile, filename, folder, filesize, file_type, date_added, content)
  99. VALUES
  100. ($c_id, '$c_title', '$c_desc', '$c_disk_file_name', '$c_new_file_name', '$c_file_path', $c_file_size, '$c_file_type', '" . db_now() . "', $c_content)";
  101. db_query( $query );
  102. # get attachment id
  103. $t_attachment_id = db_insert_id( $t_file_table );
  104. if( 'bug' == $p_table ) {
  105. # updated the last_updated date
  106. $result = bug_update_date( $c_issue_id );
  107. # log new bug
  108. history_log_event_special( $c_issue_id, FILE_ADDED, $c_new_file_name );
  109. }
  110. return $t_attachment_id;
  111. }
  112. /**
  113. * Returns the attachment contents
  114. *
  115. * @param int $p_file_id
  116. * @param string $p_type The file type, bug or doc
  117. * @param int $p_user_id
  118. * @return string|soap_fault the string contents, or a soap_fault
  119. */
  120. function mci_file_get( $p_file_id, $p_type, $p_user_id ) {
  121. # we handle the case where the file is attached to a bug
  122. # or attached to a project as a project doc.
  123. $query = '';
  124. switch( $p_type ) {
  125. case 'bug':
  126. $t_bug_file_table = db_get_table( 'bug_file' );
  127. $query = "SELECT *
  128. FROM $t_bug_file_table
  129. WHERE id='$p_file_id'";
  130. break;
  131. case 'doc':
  132. $t_project_file_table = db_get_table( 'project_file' );
  133. $query = "SELECT *
  134. FROM $t_project_file_table
  135. WHERE id='$p_file_id'";
  136. break;
  137. default:
  138. return new soap_fault( 'Server', '', 'Invalid file type '.$p_type. ' .' );
  139. }
  140. $result = db_query( $query );
  141. if ( $result->EOF ) {
  142. return new soap_fault( 'Client', '', 'Unable to find an attachment with type ' . $p_type. ' and id ' . $p_file_id . ' .' );
  143. }
  144. $row = db_fetch_array( $result );
  145. if ( $p_type == 'doc' ) {
  146. $t_project_id = $row['project_id'];
  147. } else if ( $p_type == 'bug' ) {
  148. $t_bug_id = $row['bug_id'];
  149. $t_project_id = bug_get_field( $t_bug_id, 'project_id' );
  150. }
  151. $t_diskfile = file_normalize_attachment_path( $row['diskfile'], $t_project_id );
  152. $t_content = $row['content'];
  153. # Check access rights
  154. switch( $p_type ) {
  155. case 'bug':
  156. if( !mci_file_can_download_bug_attachments( $t_bug_id, $p_user_id ) ) {
  157. return mci_soap_fault_access_denied( $t_user_id );
  158. }
  159. break;
  160. case 'doc':
  161. # Check if project documentation feature is enabled.
  162. if( OFF == config_get( 'enable_project_documentation' ) ) {
  163. return mci_soap_fault_access_denied( $t_user_id );
  164. }
  165. if( !access_has_project_level( config_get( 'view_proj_doc_threshold' ), $t_project_id, $p_user_id ) ) {
  166. return mci_soap_fault_access_denied( $t_user_id );
  167. }
  168. break;
  169. }
  170. # dump file content to the connection.
  171. switch( config_get( 'file_upload_method' ) ) {
  172. case DISK:
  173. if( file_exists( $t_diskfile ) ) {
  174. return mci_file_read_local( $t_diskfile ) ;
  175. } else {
  176. return new soap_fault( 'Client', '', 'Unable to find an attachment with type ' . $p_type. ' and id ' . $p_file_id . ' .' );
  177. }
  178. case FTP:
  179. if( file_exists( $t_diskfile ) ) {
  180. return mci_file_read_local( $t_diskfile );
  181. } else {
  182. $ftp = file_ftp_connect();
  183. file_ftp_get( $ftp, $t_diskfile, $t_diskfile );
  184. file_ftp_disconnect( $ftp );
  185. return mci_file_read_local( $t_diskfile );
  186. }
  187. default:
  188. return $t_content;
  189. }
  190. }