function mci_file_add($p_id, $p_name, $p_content, $p_file_type, $p_table, $p_title = '', $p_desc = '', $p_user_id = null) { if (!file_type_check($p_name)) { return new soap_fault('Client', '', 'File type not allowed.'); } if (!file_is_name_unique($p_name, $p_id)) { return new soap_fault('Client', '', 'Duplicate filename.'); } $t_file_size = strlen($p_content); $t_max_file_size = (int) min(ini_get_number('upload_max_filesize'), ini_get_number('post_max_size'), config_get('max_file_size')); if ($t_file_size > $t_max_file_size) { return new soap_fault('Client', '', 'File is too big.'); } if ('bug' == $p_table) { $t_project_id = bug_get_field($p_id, 'project_id'); $t_issue_id = bug_format_id($p_id); } else { $t_project_id = $p_id; $t_issue_id = 0; } # prepare variables for insertion $c_issue_id = db_prepare_int($t_issue_id); $c_project_id = db_prepare_int($t_project_id); $c_file_type = db_prepare_string($p_file_type); $c_title = db_prepare_string($p_title); $c_desc = db_prepare_string($p_desc); if ($p_user_id === null) { $c_user_id = auth_get_current_user_id(); } else { $c_user_id = (int) $p_user_id; } if ($t_project_id == ALL_PROJECTS) { $t_file_path = config_get('absolute_path_default_upload_folder'); } else { $t_file_path = project_get_field($t_project_id, 'file_path'); if ($t_file_path == '') { $t_file_path = config_get('absolute_path_default_upload_folder'); } } $c_file_path = db_prepare_string($t_file_path); $c_new_file_name = db_prepare_string($p_name); $t_file_hash = $t_issue_id; $t_disk_file_name = $t_file_path . file_generate_unique_name($t_file_hash . '-' . $p_name, $t_file_path); $c_disk_file_name = db_prepare_string($t_disk_file_name); $t_file_size = strlen($p_content); $c_file_size = db_prepare_int($t_file_size); $t_method = config_get('file_upload_method'); switch ($t_method) { case FTP: case DISK: if (!file_exists($t_file_path) || !is_dir($t_file_path) || !is_writable($t_file_path) || !is_readable($t_file_path)) { return new soap_fault('Server', '', "Upload folder '{$t_file_path}' doesn't exist."); } file_ensure_valid_upload_path($t_file_path); if (!file_exists($t_disk_file_name)) { mci_file_write_local($t_disk_file_name, $p_content); if (FTP == $t_method) { $conn_id = file_ftp_connect(); file_ftp_put($conn_id, $t_disk_file_name, $t_disk_file_name); file_ftp_disconnect($conn_id); file_delete_local($t_disk_file_name); } else { chmod($t_disk_file_name, config_get('attachments_file_permissions')); } $c_content = "''"; } break; case DATABASE: $c_content = db_prepare_binary_string($p_content); break; } $t_file_table = db_get_table($p_table . '_file'); $c_id = 'bug' == $p_table ? $c_issue_id : $c_project_id; $query = "INSERT INTO {$t_file_table}\n\t\t\t(" . $p_table . "_id, title, description, diskfile, filename, folder, filesize, file_type, date_added, content, user_id)\n\t\tVALUES\n\t\t\t({$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}, {$c_user_id})"; db_query($query); # get attachment id $t_attachment_id = db_insert_id($t_file_table); if ('bug' == $p_table) { # updated the last_updated date $result = bug_update_date($c_issue_id); # log new bug history_log_event_special($c_issue_id, FILE_ADDED, $c_new_file_name); } return $t_attachment_id; }
/** * Add a file * @param integer $p_id File id. * @param string $p_name File name. * @param string $p_content File content to write. * @param string $p_file_type File type. * @param string $p_table Database table name. * @param string $p_title Title. * @param string $p_desc Description. * @param string $p_user_id User id. * @return mixed */ function mci_file_add($p_id, $p_name, $p_content, $p_file_type, $p_table, $p_title = '', $p_desc = '', $p_user_id = null) { if (!file_type_check($p_name)) { return SoapObjectsFactory::newSoapFault('Client', 'File type not allowed.'); } if (!file_is_name_unique($p_name, $p_id)) { return SoapObjectsFactory::newSoapFault('Client', 'Duplicate filename.'); } $t_file_size = strlen($p_content); $t_max_file_size = (int) min(ini_get_number('upload_max_filesize'), ini_get_number('post_max_size'), config_get('max_file_size')); if ($t_file_size > $t_max_file_size) { return SoapObjectsFactory::newSoapFault('Client', 'File is too big.'); } if ('bug' == $p_table) { $t_project_id = bug_get_field($p_id, 'project_id'); $t_id = (int) $p_id; $t_issue_id = bug_format_id($p_id); } else { $t_project_id = $p_id; $t_id = $t_project_id; $t_issue_id = 0; } if ($p_user_id === null) { $p_user_id = auth_get_current_user_id(); } if ($t_project_id == ALL_PROJECTS) { $t_file_path = config_get('absolute_path_default_upload_folder'); } else { $t_file_path = project_get_field($t_project_id, 'file_path'); if (is_blank($t_file_path)) { $t_file_path = config_get('absolute_path_default_upload_folder'); } } $t_unique_name = file_generate_unique_name($t_file_path); $t_disk_file_name = $t_file_path . $t_unique_name; $t_method = config_get('file_upload_method'); switch ($t_method) { case DISK: if (!file_exists($t_file_path) || !is_dir($t_file_path) || !is_writable($t_file_path) || !is_readable($t_file_path)) { return SoapObjectsFactory::newSoapFault('Server', 'Upload folder \'' . $t_file_path . '\' doesn\'t exist.'); } file_ensure_valid_upload_path($t_file_path); if (!file_exists($t_disk_file_name)) { mci_file_write_local($t_disk_file_name, $p_content); chmod($t_disk_file_name, config_get('attachments_file_permissions')); $c_content = "''"; } break; case DATABASE: $c_content = db_prepare_binary_string($p_content); $t_file_path = ''; break; } $t_file_table = db_get_table($p_table . '_file'); $t_id_col = $p_table . '_id'; $t_param = array($t_id_col => $t_id, 'title' => $p_title, 'description' => $p_desc, 'diskfile' => $t_unique_name, 'filename' => $p_name, 'folder' => $t_file_path, 'filesize' => $t_file_size, 'file_type' => $p_file_type, 'date_added' => db_now(), 'user_id' => (int) $p_user_id); # Oracle has to update BLOBs separately if (!db_is_oracle()) { $t_param['content'] = $c_content; } $t_query_param = db_param(); for ($i = 1; $i < count($t_param); $i++) { $t_query_param .= ', ' . db_param(); } $t_query = 'INSERT INTO ' . $t_file_table . ' ( ' . implode(', ', array_keys($t_param)) . ' ) VALUES ( ' . $t_query_param . ' )'; db_query($t_query, array_values($t_param)); # get attachment id $t_attachment_id = db_insert_id($t_file_table); if (db_is_oracle()) { db_update_blob($t_file_table, 'content', $c_content, "diskfile='{$t_unique_name}'"); } if ('bug' == $p_table) { # bump the last_updated date bug_update_date($t_issue_id); # add history entry history_log_event_special($t_issue_id, FILE_ADDED, $p_name); } return $t_attachment_id; }