Index: wp-admin/admin-functions.php
===================================================================
--- wp-admin/admin-functions.php	(revision 3773)
+++ wp-admin/admin-functions.php	(working copy)
@@ -1905,6 +1905,56 @@
 		return array((int) ($width / $height * $hmax), $hmax);
 }
 
+function wp_attach_upload($file_array, $post) {
+	
+	$url = $file_array['url'];
+	$type = $file_array['type'];
+	$imgtitle = $file_array['title'];
+	$descr = $file_array['description'];
+	$file = addslashes($file_array['file']);
+	$filename = basename($file);
+
+	// Construct the attachment array
+	$attachment = array(
+		'post_title' => $imgtitle ? $imgtitle : $filename,
+		'post_content' => $descr,
+		'post_type' => 'attachment',
+		'post_parent' => $post,
+		'post_mime_type' => $type,
+		'guid' => $url
+		);
+
+	//remove ABSPATH from db storage
+	$relfile = str_replace(addslashes(ABSPATH), '', $file);
+	
+	// Save the data (also inserts _wp_attached_file meta data as $relfile)
+	$id = wp_insert_attachment($attachment, $relfile, $post);
+	
+	if ( preg_match('!^image/!', $type) ) {
+
+		$imagesize = getimagesize($file);
+		$attachdata['width'] = $imagesize['0'];
+		$attachdata['height'] = $imagesize['1'];
+
+		if ( $attachdata['width'] * $attachdata['height'] < 3 * 1024 * 1024 ) {
+			if ( $attachdata['width'] > 128 && $attachdata['width'] >= $attachdata['height'] * 4 / 3 )
+				$thumb = wp_create_thumbnail($file, 128);
+			elseif ( $attachdata['height'] > 96 )
+				$thumb = wp_create_thumbnail($file, 96);
+		
+			if ( @file_exists($thumb) ) {
+				$attachdata['thumb'] = basename($thumb);
+			}
+		}
+
+		add_post_meta($id, '_wp_attachment_metadata', $attachdata);
+	}
+	
+	do_action('wp_attach_upload', $id);
+	
+	return $id;
+}
+
 function wp_import_cleanup($id) {
 	wp_delete_attachment($id);
 }
@@ -1959,8 +2009,7 @@
 }
 
 function the_attachment_links($id = false) {
-	$id = (int) $id;
-	$post = & get_post($id);
+	$post = get_attachment($id);
 
 	if ( $post->post_type != 'attachment' )
 		return false;
@@ -1981,13 +2030,9 @@
 <?php
 }
 
+// deprecated, use wp_shrink_dimensions
 function get_udims($width, $height) {
-	if ( $height <= 96 && $width <= 128 )
-		return array($width, $height);
-	elseif ( $width / $height > 4 / 3 )
-		return array(128, (int) ($height / $width * 128));
-	else
-		return array((int) ($width / $height * 96), 96);
+	return wp_shrink_dimensions($width, $height);
 }
 
 ?>
Index: wp-admin/inline-uploading.php
===================================================================
--- wp-admin/inline-uploading.php	(revision 3773)
+++ wp-admin/inline-uploading.php	(working copy)
@@ -50,58 +50,16 @@
 
 $overrides = array('action'=>'save');
 
-$file = wp_handle_upload($_FILES['image'], $overrides);
+$file_array = wp_handle_upload($_FILES['image'], $overrides);
 
-if ( isset($file['error']) )
-	die($file['error'] . '<br /><a href="' . basename(__FILE__) . '?action=upload&post=' . $post . '">'.__('Back to Image Uploading').'</a>');
+if ( isset($file_array['error']) )
+	die($file_array['error'] . '<br /><a href="' . basename(__FILE__) . '?action=upload&post=' . $post . '">'.__('Back to Image Uploading').'</a>');
 
-$url = $file['url'];
-$type = $file['type'];
-$file = $file['file'];
-$filename = basename($file);
+$file_array['title'] = $imgtitle;
+$file_array['description'] = $descr;
 
-// Construct the attachment array
-$attachment = array(
-	'post_title' => $imgtitle ? $imgtitle : $filename,
-	'post_content' => $descr,
-	'post_type' => 'attachment',
-	'post_parent' => $post,
-	'post_mime_type' => $type,
-	'guid' => $url
-	);
+$id = wp_attach_upload($file_array, $post);
 
-// Save the data
-$id = wp_insert_attachment($attachment, $file, $post);
-
-if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
-	// Generate the attachment's postmeta.
-	$imagesize = getimagesize($file);
-	$imagedata['width'] = $imagesize['0'];
-	$imagedata['height'] = $imagesize['1'];
-	list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
-	$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
-	$imagedata['file'] = $file;
-
-	add_post_meta($id, '_wp_attachment_metadata', $imagedata);
-
-	if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
-		if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
-			$thumb = wp_create_thumbnail($file, 128);
-		elseif ( $imagedata['height'] > 96 )
-			$thumb = wp_create_thumbnail($file, 96);
-
-		if ( @file_exists($thumb) ) {
-			$newdata = $imagedata;
-			$newdata['thumb'] = basename($thumb);
-			update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
-		} else {
-			$error = $thumb;
-		}
-	}
-} else {
-	add_post_meta($id, '_wp_attachment_metadata', array());
-}
-
 header("Location: " . basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
 die();
 
@@ -138,7 +96,7 @@
 if ( '' == $sort )
 	$sort = "post_date_gmt DESC";
 
-$attachments = $wpdb->get_results("SELECT ID, post_date, post_title, post_mime_type, guid FROM $wpdb->posts WHERE post_type = 'attachment' $and_type $and_post $and_user ORDER BY $sort LIMIT $start, $double", ARRAY_A);
+$attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' $and_type $and_post $and_user ORDER BY $sort LIMIT $start, $double", ARRAY_A);
 
 if ( count($attachments) == 0 ) {
 	header("Location: " . basename(__FILE__) ."?post=$post&action=upload" );
@@ -200,16 +158,12 @@
 ";
 	foreach ( $attachments as $key => $attachment ) {
 		$ID = $attachment['ID'];
-		$href = get_attachment_link($ID);
-		$meta = get_post_meta($ID, '_wp_attachment_metadata', true);
-		if (!is_array($meta)) {
-			$meta = get_post_meta($ID, 'imagedata', true); // Try 1.6 Alpha meta key
-			if (!is_array($meta)) {
-				$meta = array();
-			}
-			add_post_meta($ID, '_wp_attachment_metadata', $meta);
-		}
-		$attachment = array_merge($attachment, $meta);
+		$att_post = get_attachment($ID);
+		$title = $att_post->post_title;
+		$description = $att_post->post_content;
+		$mime = $att_post->post_mime_type;
+		$guid = $att_post->guid;
+		$attachment = $att_post->attachment_metadata;
 		$noscript = "<noscript>
 		<div class='caption'><a href=\"".basename(__FILE__)."?action=links&amp;attachment={$ID}&amp;post={$post}&amp;all={$all}&amp;start={$start}\">Choose Links</a></div>
 		</noscript>
@@ -219,30 +173,30 @@
 		<a onclick=\"popup.style.display='none';return false;\" href=\"javascript:void()\">$__close</a>
 ";
 		$uwidth_sum += 128;
-		if ( preg_match('!^image/!', $attachment['post_mime_type'] ) ) {
+		if ( preg_match('!^image/!', $mime ) ) {
 			$image = & $attachment;
 			if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) {
-				$src = str_replace(basename($image['guid']), $image['thumb'], $image['guid']);
+				$src = str_replace(basename($guid), $image['thumb'], $guid);
 				$script .= "srca[{$ID}] = '$src';
-srcb[{$ID}] = '{$image['guid']}';
+srcb[{$ID}] = '{$guid}';
 ";
 				$thumb = 'true';
 				$thumbtext = $__using_thumbnail;
 			} else {
-				$src = $image['guid'];
+				$src = $guid;
 				$thumb = 'false';
 				$thumbtext = $__no_thumbnail;
 			}
-			list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']);
+			list($image['uwidth'], $image['uheight']) = wp_shrink_dimensions($image['width'], $image['height']);
 			$height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"';
 			$xpadding = (128 - $image['uwidth']) / 2;
 			$ypadding = (96 - $image['uheight']) / 2;
 			$style .= "#target{$ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
-			$title = wp_specialchars($image['post_title'], ENT_QUOTES);
-			$script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" class=\"imagelink\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
-ab[{$ID}] = '<a class=\"imagelink\" href=\"{$image['guid']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
+			$title = wp_specialchars($title, ENT_QUOTES);
+			$script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" class=\"imagelink\" href=\"{$image['link']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
+ab[{$ID}] = '<a class=\"imagelink\" href=\"{$guid}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
 imga[{$ID}] = '<img id=\"image{$ID}\" src=\"$src\" alt=\"{$title}\" $height_width />';
-imgb[{$ID}] = '<img id=\"image{$ID}\" src=\"{$image['guid']}\" alt=\"{$title}\" $height_width />';
+imgb[{$ID}] = '<img id=\"image{$ID}\" src=\"{$guid}\" alt=\"{$title}\" $height_width />';
 ";
 			$html .= "<div id='target{$ID}' class='attwrap left'>
 	<div id='div{$ID}' class='imagewrap' onclick=\"doPopup({$ID});\">
@@ -258,25 +212,25 @@
 </div>
 ";
 		} else {
-			$title = wp_specialchars($attachment['post_title'], ENT_QUOTES);
-			$filename = basename($attachment['guid']);
+			$title = wp_specialchars($title, ENT_QUOTES);
+			$filename = basename($guid);
 			$icon = get_attachment_icon($ID);
 			$toggle_icon = "<a id=\"I{$ID}\" onclick=\"toggleOtherIcon({$ID});return false;\" href=\"javascript:void()\">$__using_title</a>";
-			$script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
-ab[{$ID}] = '<a id=\"p{$ID}\" href=\"{$filename}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
+			$script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" href=\"{$attachment['link']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
+ab[{$ID}] = '<a id=\"p{$ID}\" href=\"{$guid}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
 title[{$ID}] = '{$title}';
 filename[{$ID}] = '{$filename}';
 icon[{$ID}] = '{$icon}';
 ";
 			$html .= "<div id='target{$ID}' class='attwrap left'>
 	<div id='div{$ID}' class='otherwrap usingtext' onmousedown=\"selectLink({$ID})\" onclick=\"doPopup({$ID});return false;\">
-		<a id=\"p{$ID}\" href=\"{$attachment['guid']}\" onmousedown=\"selectLink({$ID});\" onclick=\"return false;\">{$title}</a>
+		<a id=\"p{$ID}\" href=\"{$guid}\" onmousedown=\"selectLink({$ID});\" onclick=\"return false;\">{$title}</a>
 	</div>
 	{$noscript}
 </div>
 ";
 			$popups .= "<div id='popup{$ID}' class='popup'>
-	<div class='filetype'>".__('File Type:').' '.str_replace('/',"/\n",$attachment['post_mime_type'])."</div>
+	<div class='filetype'>".__('File Type:').' '.str_replace('/',"/\n",$mime)."</div>
 	<a id=\"L{$ID}\" onclick=\"toggleOtherLink({$ID});return false;\" href=\"javascript:void()\">$__linked_to_file</a>
 	{$toggle_icon}
 	{$send_delete_cancel}
@@ -644,7 +598,7 @@
 <body>
 <ul id="upload-menu">
 <li<?php echo $current_1; ?>><a href="<?php echo basename(__FILE__) . "?action=upload&amp;post=$post&amp;all=$all&amp;start=$start"; ?>"><?php _e('Upload'); ?></a></li>
-<?php if ( $attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post'") ) { ?>
+<?php if ( $attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post' AND post_type = 'attachment'") ) { ?>
 <li<?php echo $current_2; ?>><a href="<?php echo basename(__FILE__) . "?action=view&amp;post=$post&amp;all=false"; ?>"><?php _e('Browse'); ?></a></li>
 <?php } ?>
 <?php if ($wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_type = 'attachment'")) { ?>
Index: wp-includes/functions-post.php
===================================================================
--- wp-includes/functions-post.php	(revision 3773)
+++ wp-includes/functions-post.php	(working copy)
@@ -371,8 +371,9 @@
 	if ( 'attachment' != $post->post_type )
 		return false;
 
-	$meta = get_post_meta($postid, '_wp_attachment_metadata', true);
-	$file = get_post_meta($postid, '_wp_attached_file', true);
+	$attachment = get_attachment($postid);
+	$meta = $attachment->attachment_metadata;
+	$file = $meta['file'];
 
 	$wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
 
@@ -390,7 +391,7 @@
 			@ unlink($thumbfile);
 		}
 	}
-
+	
 	$file = apply_filters('wp_delete_file', $file);
 
 	if ( ! empty($file) )
Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 3773)
+++ wp-includes/functions.php	(working copy)
@@ -1671,4 +1671,34 @@
 	echo '<input type="hidden" name="_wpnonce" value="' . wp_create_nonce($action) . '" />';
 }
 
+function get_attachment($id) {
+	$id = (int) $id;
+	$post = & get_post($id);
+
+	if ( $post->post_type != 'attachment' )
+		return false;
+		
+	$meta = get_post_meta($id, '_wp_attachment_metadata', true);
+	if (!is_array($meta)) {
+		$meta = get_post_meta($id, 'imagedata', true); // Try 1.6 Alpha meta key
+		if (!is_array($meta)) {
+			$meta = array();
+		}
+		add_post_meta($id, '_wp_attachment_metadata', $meta);
+	}
+
+	// remove file meta data from serialized array
+	if(isset($meta['file'])) {
+		unset($meta['file']);
+		update_post_meta($id, '_wp_attachment_metadata', $meta);	
+	}
+	// use _wp_attached_file instead & add ABSPATH back
+	$meta['file'] = addslashes(ABSPATH) . get_post_meta($id, '_wp_attached_file', true);
+	$meta['link'] = get_attachment_link($id);
+
+	$post->attachment_metadata = $meta;
+	
+	return apply_filters('get_attachment',$post);
+}
+
 ?>
Index: wp-includes/template-functions-post.php
===================================================================
--- wp-includes/template-functions-post.php	(revision 3773)
+++ wp-includes/template-functions-post.php	(working copy)
@@ -444,8 +444,7 @@
 }
 
 function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false) {
-	$id = (int) $id;
-	$_post = & get_post($id);
+	$_post = get_attachment($id);
 
 	if ( ('attachment' != $_post->post_type) || ('' == $_post->guid) )
 		return __('Missing Attachment');
@@ -462,32 +461,30 @@
 }
 
 function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
-	$id = (int) $id;
-	$post = & get_post($id);
-
+	$post = get_attachment($id);
+	$imagedata = $post->attachment_metadata;
+	
+	$title = $post->post_title;
 	$mime = $post->post_mime_type;
-
-	$imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
-
-	$file = get_post_meta($post->ID, '_wp_attached_file', true);
-
+	$guid = $post->guid;
+	
 	if ( !$fullsize && !empty($imagedata['thumb'])
-			&& ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
+			&& ($thumbfile = str_replace(basename($imagedata['file']), $imagedata['thumb'], $imagedata['file']))
 			&& file_exists($thumbfile) ) {
 
 		// We have a thumbnail desired, specified and existing
 
-		$src = str_replace(basename($post->guid), $imagedata['thumb'], $post->guid);
+		$src = str_replace(basename($guid), $imagedata['thumb'], $guid);
 		$src_file = $thumbfile;
 		$class = 'attachmentthumb';
 
 	} elseif ( substr($mime, 0, 6) == 'image/'
-			&& file_exists($file) ) {
+			&& file_exists($imagedata['file']) ) {
 
 		// We have an image without a thumbnail
 
-		$src = $post->guid;
-		$src_file = & $file;
+		$src = $guid;
+		$src_file = & $imagedata['file'];
 		$class = 'attachmentimage';
 	} elseif (! empty($mime) ) {
 
@@ -523,18 +520,14 @@
 			if ( $actual_aspect >= $desired_aspect ) {
 				$height = $actual_aspect * $max_dims[0];
 				$constraint = "width=\"{$max_dims[0]}\" ";
-				$post->iconsize = array($max_dims[0], $height);
 			} else {
 				$width = $max_dims[1] / $actual_aspect;
 				$constraint = "height=\"{$max_dims[1]}\" ";
-				$post->iconsize = array($width, $max_dims[1]);
 			}
-		} else {
-			$post->iconsize = array($imagesize[0], $imagesize[1]);
 		}
 	}
 
-	$icon = "<img src=\"{$src}\" title=\"{$post->post_title}\" alt=\"{$post->post_title}\" {$constraint}/>";
+	$icon = "<img src=\"{$src}\" title=\"{$title}\" alt=\"{$title}\" {$constraint}/>";
 
 	return apply_filters('attachment_icon', $icon, $post->ID);
 }
@@ -545,7 +538,7 @@
 	if ( $innerHTML = get_attachment_icon($id, $fullsize, $max_dims))
 		return $innerHTML;
 
-	$post = & get_post($id);
+	$post = get_attachment($id);
 
 	$innerHTML = $post->post_title;
 
