500 * 1024 * 1024) { apiFormRefuse('A file is up to 500 MB'); } // Only the client's own folder, contact, asset, license, location and vendor $folder_id = folderOwnedId($_POST['folder_id'] ?? 0, $client_id); $links = []; foreach (['contact' => 'contact_id', 'location' => 'location_id', 'vendor' => 'vendor_id'] as $kind => $field) { $links[$kind] = intval(assetClientOwnedId($kind, $_POST[$field] ?? 0, $client_id)); } foreach (['asset' => 'asset_id', 'software' => 'software_id'] as $kind => $field) { $record = linkRecord($kind, $_POST[$field] ?? 0); $links[$kind] = $record && intval($record['client_id']) === $client_id ? intval($record['id']) : 0; } foreach (['folder_id' => $folder_id, 'contact_id' => $links['contact'], 'asset_id' => $links['asset'], 'software_id' => $links['software'], 'location_id' => $links['location'], 'vendor_id' => $links['vendor']] as $field => $found) { if (intval($_POST[$field] ?? 0) && !$found) { apiFormRefuse("$field isn't one of the client's"); } } $client_dir = __DIR__ . "/../../../uploads/clients/$client_id"; if (!is_dir($client_dir)) { mkdir($client_dir, 0755, true); } // An unguessable name, as an upload gets (checkFileUpload()) $file_reference_name = randomString(32) . '.' . $extension; $dest_path = "$client_dir/$file_reference_name"; if (file_put_contents($dest_path, $data) === false) { apiFormRefuse("The file couldn't be saved - check the uploads folder can be written to"); } // What the file is, read from the file $mime = function_exists('finfo_open') ? (string) finfo_file(finfo_open(FILEINFO_MIME_TYPE), $dest_path) : ''; $mime = $mime !== '' ? $mime : 'application/octet-stream'; $description = escapeSql(trim((string) (is_scalar($_POST['file_description'] ?? '') ? ($_POST['file_description'] ?? '') : ''))); $name_sql = escapeSql(mb_substr($original_name, 0, 200)); mysqli_query($mysqli, "INSERT INTO files SET file_reference_name = '$file_reference_name', file_name = '$name_sql', file_description = '$description', file_ext = '" . escapeSql($extension) . "', file_mime_type = '" . escapeSql($mime) . "', file_size = " . strlen($data) . ", file_created_by = " . intval($session_user_id) . ", file_folder_id = $folder_id, file_client_id = $client_id"); $file_id = intval(mysqli_insert_id($mysqli)); if ($file_id) { $insert_id = $file_id; foreach (['contact' => 'contact_files', 'asset' => 'asset_files', 'software' => 'software_files', 'location' => 'location_files', 'vendor' => 'vendor_files'] as $kind => $table) { if ($links[$kind]) { mysqli_query($mysqli, "INSERT IGNORE INTO $table SET {$kind}_id = {$links[$kind]}, file_id = $file_id"); } } logAudit("File", "Upload", "Uploaded file $name_sql via API ($api_key_name)", $client_id, $file_id); logAudit("API", "Success", "Uploaded file $name_sql via API ($api_key_name)", $client_id); } else { unlink($dest_path); } // Output require_once '../create_output.php';