3 require_once('admin.php');
5 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
7 if (!current_user_can('upload_files'))
8 die(__('You do not have permission to upload files.'));
10 $wpvarstoreset = array('action', 'post', 'all', 'last', 'link', 'sort', 'start', 'imgtitle', 'descr', 'attachment');
12 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
13 $wpvar = $wpvarstoreset[$i];
14 if (!isset($$wpvar)) {
15 if (empty($_POST["$wpvar"])) {
16 if (empty($_GET["$wpvar"])) {
19 $$wpvar = $_GET["$wpvar"];
22 $$wpvar = $_POST["$wpvar"];
37 check_admin_referer('inlineuploading');
39 if ( !current_user_can('edit_post', (int) $attachment) )
40 die(__('You are not allowed to delete this attachment.').' <a href="'.basename(__FILE__)."?post=$post&all=$all&action=upload\">".__('Go back').'</a>');
42 wp_delete_attachment($attachment);
44 wp_redirect(basename(__FILE__) ."?post=$post&all=$all&action=view&start=$start");
49 check_admin_referer('inlineuploading');
51 $overrides = array('action'=>'save');
53 $file = wp_handle_upload($_FILES['image'], $overrides);
55 if ( isset($file['error']) )
56 die($file['error'] . '<br /><a href="' . basename(__FILE__) . '?action=upload&post=' . $post . '">'.__('Back to Image Uploading').'</a>');
59 $type = $file['type'];
60 $file = $file['file'];
61 $filename = basename($file);
63 // Construct the attachment array
65 'post_title' => $imgtitle ? $imgtitle : $filename,
66 'post_content' => $descr,
67 'post_status' => 'attachment',
68 'post_parent' => $post,
69 'post_mime_type' => $type,
74 $id = wp_insert_attachment($attachment, $file, $post);
76 if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
77 // Generate the attachment's postmeta.
78 $imagesize = getimagesize($file);
79 $imagedata['width'] = $imagesize['0'];
80 $imagedata['height'] = $imagesize['1'];
81 list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
82 $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
83 $imagedata['file'] = $file;
85 add_post_meta($id, '_wp_attachment_metadata', $imagedata);
87 if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
88 if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
89 $thumb = wp_create_thumbnail($file, 128);
90 elseif ( $imagedata['height'] > 96 )
91 $thumb = wp_create_thumbnail($file, 96);
93 if ( @file_exists($thumb) ) {
94 $newdata = $imagedata;
95 $newdata['thumb'] = basename($thumb);
96 update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
102 add_post_meta($id, '_wp_attachment_metadata', array());
105 wp_redirect(basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
110 $current_1 = ' class="current"';
111 $back = $next = false;
116 // How many images do we show? How many do we query?
120 if ( $post && (empty($all) || $all == 'false') ) {
121 $and_post = "AND post_parent = '$post'";
122 $current_2 = ' class="current"';
124 $current_3 = ' class="current"';
127 if (! current_user_can('edit_others_posts') )
128 $and_user = "AND post_author = " . $user_ID;
131 $start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment' $and_user $and_post") - $num;
133 $start = (int) $start;
139 $sort = "post_date_gmt DESC";
141 $attachments = $wpdb->get_results("SELECT ID, post_date, post_title, post_mime_type, guid FROM $wpdb->posts WHERE post_status = 'attachment' $and_type $and_post $and_user ORDER BY $sort LIMIT $start, $double", ARRAY_A);
143 if ( count($attachments) == 0 ) {
144 wp_redirect( basename(__FILE__) ."?post=$post&action=upload" );
146 } elseif ( count($attachments) > $num ) {
147 $next = $start + count($attachments) - $num;
153 $back = $start - $num;
165 if ( count($attachments) > 0 ) {
166 $attachments = array_slice( $attachments, 0, $num );
167 $__delete = __('Delete');
168 $__not_linked = __('Not Linked');
169 $__linked_to_page = __('Linked to Page');
170 $__linked_to_image = __('Linked to Image');
171 $__linked_to_file = __('Linked to File');
172 $__using_thumbnail = __('Using Thumbnail');
173 $__using_original = __('Using Original');
174 $__using_title = __('Using Title');
175 $__using_filename = __('Using Filename');
176 $__using_icon = __('Using Icon');
177 $__no_thumbnail = '<del>'.__('No Thumbnail').'</del>';
178 $__send_to_editor = __('Send to editor');
179 $__close = __('Close Options');
180 $__confirmdelete = __('Delete this file from the server?');
181 $__nothumb = __('There is no thumbnail associated with this photo.');
182 $script .= "notlinked = '$__not_linked';
183 linkedtoimage = '$__linked_to_image';
184 linkedtopage = '$__linked_to_page';
185 linkedtofile = '$__linked_to_file';
186 usingthumbnail = '$__using_thumbnail';
187 usingoriginal = '$__using_original';
188 usingtitle = '$__using_title';
189 usingfilename = '$__using_filename';
190 usingicon = '$__using_icon';
191 var aa = new Array();
192 var ab = new Array();
193 var imga = new Array();
194 var imgb = new Array();
195 var srca = new Array();
196 var srcb = new Array();
197 var title = new Array();
198 var filename = new Array();
199 var icon = new Array();
201 foreach ( $attachments as $key => $attachment ) {
202 $ID = $attachment['ID'];
203 $href = get_attachment_link($ID);
204 $meta = get_post_meta($ID, '_wp_attachment_metadata', true);
205 if (!is_array($meta)) {
206 $meta = get_post_meta($ID, 'imagedata', true); // Try 1.6 Alpha meta key
207 if (!is_array($meta)) {
210 add_post_meta($ID, '_wp_attachment_metadata', $meta);
212 $attachment = array_merge($attachment, $meta);
213 $noscript = "<noscript>
214 <div class='caption'><a href=\"".basename(__FILE__)."?action=links&attachment={$ID}&post={$post}&all={$all}&start={$start}\">Choose Links</a></div>
217 $send_delete_cancel = "<a onclick=\"sendToEditor({$ID});return false;\" href=\"javascript:void()\">$__send_to_editor</a>
218 <a onclick=\"return confirm('$__confirmdelete')\" href=\"" . wp_nonce_url( basename(__FILE__) . "?action=delete&attachment={$ID}&all=$all&start=$start&post=$post", inlineuploading) . "\">$__delete</a>
219 <a onclick=\"popup.style.display='none';return false;\" href=\"javascript:void()\">$__close</a>
222 if ( preg_match('!^image/!', $attachment['post_mime_type'] ) ) {
223 $image = & $attachment;
224 if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) {
225 $src = str_replace(basename($image['guid']), $image['thumb'], $image['guid']);
226 $script .= "srca[{$ID}] = '$src';
227 srcb[{$ID}] = '{$image['guid']}';
230 $thumbtext = $__using_thumbnail;
232 $src = $image['guid'];
234 $thumbtext = $__no_thumbnail;
236 list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']);
237 $height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"';
238 $xpadding = (128 - $image['uwidth']) / 2;
239 $ypadding = (96 - $image['uheight']) / 2;
240 $style .= "#target{$ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
241 $title = wp_specialchars($image['post_title'], ENT_QUOTES);
242 $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" class=\"imagelink\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
243 ab[{$ID}] = '<a class=\"imagelink\" href=\"{$image['guid']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
244 imga[{$ID}] = '<img id=\"image{$ID}\" src=\"$src\" alt=\"{$title}\" $height_width />';
245 imgb[{$ID}] = '<img id=\"image{$ID}\" src=\"{$image['guid']}\" alt=\"{$title}\" $height_width />';
247 $html .= "<div id='target{$ID}' class='attwrap left'>
248 <div id='div{$ID}' class='imagewrap' onclick=\"doPopup({$ID});\">
249 <img id=\"image{$ID}\" src=\"$src\" alt=\"{$title}\" $height_width />
254 $popups .= "<div id='popup{$ID}' class='popup'>
255 <a id=\"I{$ID}\" onclick=\"if($thumb)toggleImage({$ID});else alert('$__nothumb');return false;\" href=\"javascript:void()\">$thumbtext</a>
256 <a id=\"L{$ID}\" onclick=\"toggleLink({$ID});return false;\" href=\"javascript:void()\">$__not_linked</a>
257 {$send_delete_cancel}
261 $title = wp_specialchars($attachment['post_title'], ENT_QUOTES);
262 $filename = basename($attachment['guid']);
263 $icon = get_attachment_icon($ID);
264 $toggle_icon = "<a id=\"I{$ID}\" onclick=\"toggleOtherIcon({$ID});return false;\" href=\"javascript:void()\">$__using_title</a>";
265 $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
266 ab[{$ID}] = '<a id=\"p{$ID}\" href=\"{$filename}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
267 title[{$ID}] = '{$title}';
268 filename[{$ID}] = '{$filename}';
269 icon[{$ID}] = '{$icon}';
271 $html .= "<div id='target{$ID}' class='attwrap left'>
272 <div id='div{$ID}' class='otherwrap usingtext' onmousedown=\"selectLink({$ID})\" onclick=\"doPopup({$ID});return false;\">
273 <a id=\"p{$ID}\" href=\"{$attachment['guid']}\" onmousedown=\"selectLink({$ID});\" onclick=\"return false;\">{$title}</a>
278 $popups .= "<div id='popup{$ID}' class='popup'>
279 <div class='filetype'>".__('File Type:').' '.str_replace('/',"/\n",$attachment['post_mime_type'])."</div>
280 <a id=\"L{$ID}\" onclick=\"toggleOtherLink({$ID});return false;\" href=\"javascript:void()\">$__linked_to_file</a>
282 {$send_delete_cancel}
289 $images_width = $uwidth_sum + ( count($images) * 6 ) + 35;
294 die(__('This script was not meant to be called directly.'));
298 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
299 <html xmlns="http://www.w3.org/1999/xhtml">
301 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
303 <meta http-equiv="imagetoolbar" content="no" />
304 <script type="text/javascript">
306 /* Define any variables we'll need, such as alternate URLs. */
307 <?php echo $script; ?>
308 function htmldecode(st) {
309 o = document.getElementById('htmldecode');
311 o = document.createElement("A");
318 function cancelUpload() {
319 o = document.getElementById('uploadForm');
321 o.action.value = 'view';
324 function doPopup(i) {
326 popup.style.display = 'none';
327 target = document.getElementById('target'+i);
328 popup = document.getElementById('popup'+i);
329 popup.style.left = (target.offsetLeft) + 'px';
330 popup.style.top = (target.offsetTop) + 'px';
331 popup.style.display = 'block';
334 function selectLink(n) {
335 o=document.getElementById('div'+n);
336 if ( typeof document.body.createTextRange == 'undefined' || typeof win.tinyMCE == 'undefined' || win.tinyMCE.configs.length < 1 )
338 r = document.body.createTextRange();
339 if ( typeof r != 'undefined' ) {
340 r.moveToElementText(o);
344 function toggleLink(n) {
345 ol=document.getElementById('L'+n);
346 if ( ol.innerHTML == htmldecode(notlinked) ) {
347 ol.innerHTML = linkedtoimage;
348 } else if ( ol.innerHTML == htmldecode(linkedtoimage) ) {
349 ol.innerHTML = linkedtopage;
351 ol.innerHTML = notlinked;
355 function toggleOtherLink(n) {
356 ol=document.getElementById('L'+n);
357 if ( ol.innerHTML == htmldecode(linkedtofile) ) {
358 ol.innerHTML = linkedtopage;
360 ol.innerHTML = linkedtofile;
364 function toggleImage(n) {
365 oi = document.getElementById('I'+n);
366 if ( oi.innerHTML == htmldecode(usingthumbnail) ) {
367 oi.innerHTML = usingoriginal;
369 oi.innerHTML = usingthumbnail;
373 function toggleOtherIcon(n) {
374 od = document.getElementById('div'+n);
375 oi = document.getElementById('I'+n);
376 if ( oi.innerHTML == htmldecode(usingtitle) ) {
377 oi.innerHTML = usingfilename;
378 od.className = 'otherwrap usingtext';
379 } else if ( oi.innerHTML == htmldecode(usingfilename) && icon[n] != '' ) {
380 oi.innerHTML = usingicon;
381 od.className = 'otherwrap usingicon';
383 oi.innerHTML = usingtitle;
384 od.className = 'otherwrap usingtext';
388 function updateImage(n) {
389 od=document.getElementById('div'+n);
390 ol=document.getElementById('L'+n);
391 oi=document.getElementById('I'+n);
392 if ( oi.innerHTML == htmldecode(usingthumbnail) ) {
397 if ( ol.innerHTML == htmldecode(linkedtoimage) ) {
398 od.innerHTML = ab[n]+img+'</a>';
399 } else if ( ol.innerHTML == htmldecode(linkedtopage) ) {
400 od.innerHTML = aa[n]+img+'</a>';
405 function updateOtherIcon(n) {
406 od=document.getElementById('div'+n);
407 ol=document.getElementById('L'+n);
408 oi=document.getElementById('I'+n);
409 if ( oi.innerHTML == htmldecode(usingfilename) ) {
411 } else if ( oi.innerHTML == htmldecode(usingicon) ) {
416 if ( ol.innerHTML == htmldecode(linkedtofile) ) {
417 od.innerHTML = ab[n]+txt+'</a>';
418 } else if ( ol.innerHTML == htmldecode(linkedtopage) ) {
419 od.innerHTML = aa[n]+txt+'</a>';
425 var win = window.opener ? window.opener : window.dialogArguments;
427 tinyMCE = win.tinyMCE;
428 richedit = ( typeof tinyMCE == 'object' && tinyMCE.configs.length > 0 );
429 function sendToEditor(n) {
430 o = document.getElementById('div'+n);
431 h = o.innerHTML.replace(new RegExp('^\\s*(.*?)\\s*$', ''), '$1'); // Trim
432 h = h.replace(new RegExp(' (class|title|width|height|id|onclick|onmousedown)=([^\'"][^ ]*)( |/|>)', 'g'), ' $1="$2"$3'); // Enclose attribs in quotes
433 h = h.replace(new RegExp(' (width|height)=".*?"', 'g'), ''); // Drop size constraints
434 h = h.replace(new RegExp(' on(click|mousedown)="[^"]*"', 'g'), ''); // Drop menu events
435 h = h.replace(new RegExp('<(/?)A', 'g'), '<$1a'); // Lowercase tagnames
436 h = h.replace(new RegExp('<IMG', 'g'), '<img'); // Lowercase again
437 h = h.replace(new RegExp('(<img .+?")>', 'g'), '$1 />'); // XHTML
439 win.tinyMCE.execCommand('mceInsertContent', false, h);
441 win.edInsertContent(win.edCanvas, h);
445 <style type="text/css">
446 <?php if ( $action == 'links' ) : ?>
447 * html { overflow-x: hidden; }
449 * html { overflow-y: hidden; }
452 font: 13px "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana;
459 margin: 3px 2px 0px 6px;
471 width: <?php echo $images_width; ?>px;
474 background-color: rgb(209, 226, 239);
476 <?php echo $style; ?>
477 .attwrap, .attwrap * {
490 background-color: #f9fcfe;
495 .otherwrap a, .otherwrap a:hover, .otherwrap a:active, .otherwrap a:visited {
512 border-bottom: 3px double #89a
514 .imagewrap, .imagewrap img, .imagewrap a, .imagewrap a img, .imagewrap a:hover img, .imagewrap a:visited img, .imagewrap a:active img {
515 text-decoration: none;
523 border-bottom: 1px solid #448abd;
533 text-decoration: none;
535 border-top: 3px solid #fff;
537 #upload-menu .current a {
539 border-right: 2px solid #448abd;
541 #upload-menu a:hover {
546 color: rgb(68, 138, 189);
562 #upload-menu li.spacer {
585 background-color: rgb(240, 240, 238);
586 border-top: 2px solid #fff;
587 border-right: 2px solid #ddd;
588 border-bottom: 2px solid #ddd;
589 border-left: 2px solid #fff;
594 filter:alpha(opacity=90);
599 .popup a, .popup a:visited, .popup a:active {
600 background-color: transparent;
603 text-decoration: none;
607 background-color: #fff;
617 #submit input, #submit input:focus {
618 background: url( images/fade-butt.png );
619 border: 3px double #999;
620 border-left-color: #ccc;
621 border-top-color: #ccc;
625 #submit input:active {
627 border: 3px double #ccc;
628 border-left-color: #999;
629 border-top-color: #999;
648 <ul id="upload-menu">
649 <li<?php echo $current_1; ?>><a href="<?php echo basename(__FILE__) . "?action=upload&post=$post&all=$all&start=$start"; ?>"><?php _e('Upload'); ?></a></li>
650 <?php if ( $attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post'") ) { ?>
651 <li<?php echo $current_2; ?>><a href="<?php echo basename(__FILE__) . "?action=view&post=$post&all=false"; ?>"><?php _e('Browse'); ?></a></li>
653 <?php if ($wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment'")) { ?>
654 <li<?php echo $current_3; ?>><a href="<?php echo basename(__FILE__) . "?action=view&post=$post&all=true"; ?>"><?php _e('Browse All'); ?></a></li>
657 <?php if ( $action == 'view' ) { ?>
658 <?php if ( false !== $back ) : ?>
659 <li class="spacer"><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=0"; ?>" title="<?php _e('First'); ?>">|«</a></li>
660 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=$back"; ?>">« <?php _e('Back'); ?></a></li>
662 <li class="inactive spacer">|«</li>
663 <li class="inactive">« <?php _e('Back'); ?></li>
665 <?php if ( false !== $next ) : ?>
666 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=$next"; ?>"><?php _e('Next »'); ?></a></li>
667 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&last=true"; ?>" title="<?php _e('Last'); ?>">»|</a></li>
669 <li class="inactive"><?php _e('Next'); ?> »</li>
670 <li class="inactive">»|</li>
672 <?php } // endif not upload?>
674 <?php if ( $action == 'view' ) : ?>
676 <!--<div class="tip"><?php _e('You can drag and drop these items into your post. Click on one for more options.'); ?></div>-->
679 <?php echo $popups; ?>
682 <?php elseif ( $action == 'upload' ) : ?>
683 <div class="tip"></div>
684 <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo basename(__FILE__); ?>">
685 <table style="width:99%;">
687 <th scope="row" align="right"><label for="upload"><?php _e('File:'); ?></label></th>
688 <td><input type="file" id="upload" name="image" /></td>
691 <th scope="row" align="right"><label for="title"><?php _e('Title:'); ?></label></th>
692 <td><input type="text" id="title" name="imgtitle" /></td>
695 <th scope="row" align="right"><label for="descr"><?php _e('Description:'); ?></label></th>
696 <td><input type="textarea" name="descr" id="descr" value="" /></td>
701 <input type="hidden" name="action" value="save" />
702 <input type="hidden" name="post" value="<?php echo $post; ?>" />
703 <input type="hidden" name="all" value="<?php echo $all; ?>" />
704 <input type="hidden" name="start" value="<?php echo $start; ?>" />
705 <?php wp_nonce_field( 'inlineuploading' ); ?>
707 <input type="submit" value="<?php _e('Upload'); ?>" />
708 <?php if ( !empty($all) ) : ?>
709 <input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
716 <?php elseif ( $action == 'links' ) : ?>
718 <?php the_attachment_links($attachment); ?>