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"];
27 $all = ( 'true' == $all ) ? 'true' : 'false';
28 $start = (int) $start;
39 check_admin_referer('inlineuploading');
41 if ( !current_user_can('edit_post', (int) $attachment) )
42 die(__('You are not allowed to delete this attachment.').' <a href="'.basename(__FILE__)."?post=$post&all=$all&action=upload\">".__('Go back').'</a>');
44 wp_delete_attachment($attachment);
46 wp_redirect(basename(__FILE__) ."?post=$post&all=$all&action=view&start=$start");
51 check_admin_referer('inlineuploading');
53 $overrides = array('action'=>'save');
55 $file = wp_handle_upload($_FILES['image'], $overrides);
57 if ( isset($file['error']) )
58 die($file['error'] . '<br /><a href="' . basename(__FILE__) . '?action=upload&post=' . $post . '">'.__('Back to Image Uploading').'</a>');
61 $type = $file['type'];
62 $file = $file['file'];
63 $filename = basename($file);
65 // Construct the attachment array
67 'post_title' => $imgtitle ? $imgtitle : $filename,
68 'post_content' => $descr,
69 'post_status' => 'attachment',
70 'post_parent' => $post,
71 'post_mime_type' => $type,
76 $id = wp_insert_attachment($attachment, $file, $post);
78 if ( preg_match('!^image/!', $attachment['post_mime_type']) ) {
79 // Generate the attachment's postmeta.
80 $imagesize = getimagesize($file);
81 $imagedata['width'] = $imagesize['0'];
82 $imagedata['height'] = $imagesize['1'];
83 list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']);
84 $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
85 $imagedata['file'] = $file;
87 add_post_meta($id, '_wp_attachment_metadata', $imagedata);
89 if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
90 if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
91 $thumb = wp_create_thumbnail($file, 128);
92 elseif ( $imagedata['height'] > 96 )
93 $thumb = wp_create_thumbnail($file, 96);
95 if ( @file_exists($thumb) ) {
96 $newdata = $imagedata;
97 $newdata['thumb'] = basename($thumb);
98 update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
104 add_post_meta($id, '_wp_attachment_metadata', array());
107 wp_redirect(basename(__FILE__) . "?post=$post&all=$all&action=view&start=0");
112 $current_1 = ' class="current"';
113 $back = $next = false;
118 // How many images do we show? How many do we query?
122 if ( $post && (empty($all) || $all == 'false') ) {
123 $and_post = "AND post_parent = '$post'";
124 $current_2 = ' class="current"';
126 $current_3 = ' class="current"';
129 if (! current_user_can('edit_others_posts') )
130 $and_user = "AND post_author = " . $user_ID;
133 $start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment' $and_user $and_post") - $num;
135 $start = (int) $start;
141 $sort = "post_date_gmt DESC";
143 $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);
145 if ( count($attachments) == 0 ) {
146 wp_redirect( basename(__FILE__) ."?post=$post&action=upload" );
148 } elseif ( count($attachments) > $num ) {
149 $next = $start + count($attachments) - $num;
155 $back = $start - $num;
167 if ( count($attachments) > 0 ) {
168 $attachments = array_slice( $attachments, 0, $num );
169 $__delete = __('Delete');
170 $__not_linked = __('Not Linked');
171 $__linked_to_page = __('Linked to Page');
172 $__linked_to_image = __('Linked to Image');
173 $__linked_to_file = __('Linked to File');
174 $__using_thumbnail = __('Using Thumbnail');
175 $__using_original = __('Using Original');
176 $__using_title = __('Using Title');
177 $__using_filename = __('Using Filename');
178 $__using_icon = __('Using Icon');
179 $__no_thumbnail = '<del>'.__('No Thumbnail').'</del>';
180 $__send_to_editor = __('Send to editor');
181 $__close = __('Close Options');
182 $__confirmdelete = __('Delete this file from the server?');
183 $__nothumb = __('There is no thumbnail associated with this photo.');
184 $script .= "notlinked = '$__not_linked';
185 linkedtoimage = '$__linked_to_image';
186 linkedtopage = '$__linked_to_page';
187 linkedtofile = '$__linked_to_file';
188 usingthumbnail = '$__using_thumbnail';
189 usingoriginal = '$__using_original';
190 usingtitle = '$__using_title';
191 usingfilename = '$__using_filename';
192 usingicon = '$__using_icon';
193 var aa = new Array();
194 var ab = new Array();
195 var imga = new Array();
196 var imgb = new Array();
197 var srca = new Array();
198 var srcb = new Array();
199 var title = new Array();
200 var filename = new Array();
201 var icon = new Array();
203 foreach ( $attachments as $key => $attachment ) {
204 $ID = $attachment['ID'];
205 $href = get_attachment_link($ID);
206 $meta = get_post_meta($ID, '_wp_attachment_metadata', true);
207 if (!is_array($meta)) {
208 $meta = get_post_meta($ID, 'imagedata', true); // Try 1.6 Alpha meta key
209 if (!is_array($meta)) {
212 add_post_meta($ID, '_wp_attachment_metadata', $meta);
214 $attachment = array_merge($attachment, $meta);
215 $noscript = "<noscript>
216 <div class='caption'><a href=\"".basename(__FILE__)."?action=links&attachment={$ID}&post={$post}&all={$all}&start={$start}\">Choose Links</a></div>
219 $send_delete_cancel = "<a onclick=\"sendToEditor({$ID});return false;\" href=\"javascript:void()\">$__send_to_editor</a>
220 <a onclick=\"return confirm('$__confirmdelete')\" href=\"" . wp_nonce_url( basename(__FILE__) . "?action=delete&attachment={$ID}&all=$all&start=$start&post=$post", inlineuploading) . "\">$__delete</a>
221 <a onclick=\"popup.style.display='none';return false;\" href=\"javascript:void()\">$__close</a>
224 if ( preg_match('!^image/!', $attachment['post_mime_type'] ) ) {
225 $image = & $attachment;
226 if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) {
227 $src = str_replace(basename($image['guid']), $image['thumb'], $image['guid']);
228 $script .= "srca[{$ID}] = '$src';
229 srcb[{$ID}] = '{$image['guid']}';
232 $thumbtext = $__using_thumbnail;
234 $src = $image['guid'];
236 $thumbtext = $__no_thumbnail;
238 list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']);
239 $height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"';
240 $xpadding = (128 - $image['uwidth']) / 2;
241 $ypadding = (96 - $image['uheight']) / 2;
242 $style .= "#target{$ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
243 $title = attribute_escape($image['post_title']);
244 $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" class=\"imagelink\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
245 ab[{$ID}] = '<a class=\"imagelink\" href=\"{$image['guid']}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
246 imga[{$ID}] = '<img id=\"image{$ID}\" src=\"$src\" alt=\"{$title}\" $height_width />';
247 imgb[{$ID}] = '<img id=\"image{$ID}\" src=\"{$image['guid']}\" alt=\"{$title}\" $height_width />';
249 $html .= "<div id='target{$ID}' class='attwrap left'>
250 <div id='div{$ID}' class='imagewrap' onclick=\"doPopup({$ID});\">
251 <img id=\"image{$ID}\" src=\"$src\" alt=\"{$title}\" $height_width />
256 $popups .= "<div id='popup{$ID}' class='popup'>
257 <a id=\"I{$ID}\" onclick=\"if($thumb)toggleImage({$ID});else alert('$__nothumb');return false;\" href=\"javascript:void()\">$thumbtext</a>
258 <a id=\"L{$ID}\" onclick=\"toggleLink({$ID});return false;\" href=\"javascript:void()\">$__not_linked</a>
259 {$send_delete_cancel}
263 $title = attribute_escape($attachment['post_title']);
264 $filename = basename($attachment['guid']);
265 $icon = get_attachment_icon($ID);
266 $toggle_icon = "<a id=\"I{$ID}\" onclick=\"toggleOtherIcon({$ID});return false;\" href=\"javascript:void()\">$__using_title</a>";
267 $script .= "aa[{$ID}] = '<a id=\"p{$ID}\" rel=\"attachment\" href=\"$href\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
268 ab[{$ID}] = '<a id=\"p{$ID}\" href=\"{$filename}\" onclick=\"doPopup({$ID});return false;\" title=\"{$title}\">';
269 title[{$ID}] = '{$title}';
270 filename[{$ID}] = '{$filename}';
271 icon[{$ID}] = '{$icon}';
273 $html .= "<div id='target{$ID}' class='attwrap left'>
274 <div id='div{$ID}' class='otherwrap usingtext' onmousedown=\"selectLink({$ID})\" onclick=\"doPopup({$ID});return false;\">
275 <a id=\"p{$ID}\" href=\"{$attachment['guid']}\" onmousedown=\"selectLink({$ID});\" onclick=\"return false;\">{$title}</a>
280 $popups .= "<div id='popup{$ID}' class='popup'>
281 <div class='filetype'>".__('File Type:').' '.str_replace('/',"/\n",$attachment['post_mime_type'])."</div>
282 <a id=\"L{$ID}\" onclick=\"toggleOtherLink({$ID});return false;\" href=\"javascript:void()\">$__linked_to_file</a>
284 {$send_delete_cancel}
291 $images_width = $uwidth_sum + ( count($images) * 6 ) + 35;
296 die(__('This script was not meant to be called directly.'));
300 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
301 <html xmlns="http://www.w3.org/1999/xhtml">
303 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
305 <meta http-equiv="imagetoolbar" content="no" />
306 <script type="text/javascript">
308 /* Define any variables we'll need, such as alternate URLs. */
309 <?php echo $script; ?>
310 function htmldecode(st) {
311 o = document.getElementById('htmldecode');
313 o = document.createElement("A");
320 function cancelUpload() {
321 o = document.getElementById('uploadForm');
323 o.action.value = 'view';
326 function doPopup(i) {
328 popup.style.display = 'none';
329 target = document.getElementById('target'+i);
330 popup = document.getElementById('popup'+i);
331 popup.style.left = (target.offsetLeft) + 'px';
332 popup.style.top = (target.offsetTop) + 'px';
333 popup.style.display = 'block';
336 function selectLink(n) {
337 o=document.getElementById('div'+n);
338 if ( typeof document.body.createTextRange == 'undefined' || typeof win.tinyMCE == 'undefined' || win.tinyMCE.configs.length < 1 )
340 r = document.body.createTextRange();
341 if ( typeof r != 'undefined' ) {
342 r.moveToElementText(o);
346 function toggleLink(n) {
347 ol=document.getElementById('L'+n);
348 if ( ol.innerHTML == htmldecode(notlinked) ) {
349 ol.innerHTML = linkedtoimage;
350 } else if ( ol.innerHTML == htmldecode(linkedtoimage) ) {
351 ol.innerHTML = linkedtopage;
353 ol.innerHTML = notlinked;
357 function toggleOtherLink(n) {
358 ol=document.getElementById('L'+n);
359 if ( ol.innerHTML == htmldecode(linkedtofile) ) {
360 ol.innerHTML = linkedtopage;
362 ol.innerHTML = linkedtofile;
366 function toggleImage(n) {
367 oi = document.getElementById('I'+n);
368 if ( oi.innerHTML == htmldecode(usingthumbnail) ) {
369 oi.innerHTML = usingoriginal;
371 oi.innerHTML = usingthumbnail;
375 function toggleOtherIcon(n) {
376 od = document.getElementById('div'+n);
377 oi = document.getElementById('I'+n);
378 if ( oi.innerHTML == htmldecode(usingtitle) ) {
379 oi.innerHTML = usingfilename;
380 od.className = 'otherwrap usingtext';
381 } else if ( oi.innerHTML == htmldecode(usingfilename) && icon[n] != '' ) {
382 oi.innerHTML = usingicon;
383 od.className = 'otherwrap usingicon';
385 oi.innerHTML = usingtitle;
386 od.className = 'otherwrap usingtext';
390 function updateImage(n) {
391 od=document.getElementById('div'+n);
392 ol=document.getElementById('L'+n);
393 oi=document.getElementById('I'+n);
394 if ( oi.innerHTML == htmldecode(usingthumbnail) ) {
399 if ( ol.innerHTML == htmldecode(linkedtoimage) ) {
400 od.innerHTML = ab[n]+img+'</a>';
401 } else if ( ol.innerHTML == htmldecode(linkedtopage) ) {
402 od.innerHTML = aa[n]+img+'</a>';
407 function updateOtherIcon(n) {
408 od=document.getElementById('div'+n);
409 ol=document.getElementById('L'+n);
410 oi=document.getElementById('I'+n);
411 if ( oi.innerHTML == htmldecode(usingfilename) ) {
413 } else if ( oi.innerHTML == htmldecode(usingicon) ) {
418 if ( ol.innerHTML == htmldecode(linkedtofile) ) {
419 od.innerHTML = ab[n]+txt+'</a>';
420 } else if ( ol.innerHTML == htmldecode(linkedtopage) ) {
421 od.innerHTML = aa[n]+txt+'</a>';
427 var win = window.opener ? window.opener : window.dialogArguments;
429 tinyMCE = win.tinyMCE;
430 richedit = ( typeof tinyMCE == 'object' && tinyMCE.configs.length > 0 );
431 function sendToEditor(n) {
432 o = document.getElementById('div'+n);
433 h = o.innerHTML.replace(new RegExp('^\\s*(.*?)\\s*$', ''), '$1'); // Trim
434 h = h.replace(new RegExp(' (class|title|width|height|id|onclick|onmousedown)=([^\'"][^ ]*)(?=( |/|>))', 'g'), ' $1="$2"'); // Enclose attribs in quotes
435 h = h.replace(new RegExp(' (width|height)=".*?"', 'g'), ''); // Drop size constraints
436 h = h.replace(new RegExp(' on(click|mousedown)="[^"]*"', 'g'), ''); // Drop menu events
437 h = h.replace(new RegExp('<(/?)A', 'g'), '<$1a'); // Lowercase tagnames
438 h = h.replace(new RegExp('<IMG', 'g'), '<img'); // Lowercase again
439 h = h.replace(new RegExp('(<img .+?")>', 'g'), '$1 />'); // XHTML
441 win.tinyMCE.execCommand('mceInsertContent', false, h);
443 win.edInsertContent(win.edCanvas, h);
447 <style type="text/css">
448 <?php if ( $action == 'links' ) : ?>
449 * html { overflow-x: hidden; }
451 * html { overflow-y: hidden; }
454 font: 13px "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana;
461 margin: 3px 2px 0px 6px;
473 width: <?php echo $images_width; ?>px;
476 background-color: rgb(209, 226, 239);
478 <?php echo $style; ?>
479 .attwrap, .attwrap * {
492 background-color: #f9fcfe;
497 .otherwrap a, .otherwrap a:hover, .otherwrap a:active, .otherwrap a:visited {
514 border-bottom: 3px double #89a
516 .imagewrap, .imagewrap img, .imagewrap a, .imagewrap a img, .imagewrap a:hover img, .imagewrap a:visited img, .imagewrap a:active img {
517 text-decoration: none;
525 border-bottom: 1px solid #448abd;
535 text-decoration: none;
537 border-top: 3px solid #fff;
539 #upload-menu .current a {
541 border-right: 2px solid #448abd;
543 #upload-menu a:hover {
548 color: rgb(68, 138, 189);
564 #upload-menu li.spacer {
587 background-color: rgb(240, 240, 238);
588 border-top: 2px solid #fff;
589 border-right: 2px solid #ddd;
590 border-bottom: 2px solid #ddd;
591 border-left: 2px solid #fff;
596 filter:alpha(opacity=90);
601 .popup a, .popup a:visited, .popup a:active {
602 background-color: transparent;
605 text-decoration: none;
609 background-color: #fff;
619 #submit input, #submit input:focus {
620 background: url( images/fade-butt.png );
621 border: 3px double #999;
622 border-left-color: #ccc;
623 border-top-color: #ccc;
627 #submit input:active {
629 border: 3px double #ccc;
630 border-left-color: #999;
631 border-top-color: #999;
650 <ul id="upload-menu">
651 <li<?php echo $current_1; ?>><a href="<?php echo basename(__FILE__) . "?action=upload&post=$post&all=$all&start=$start"; ?>"><?php _e('Upload'); ?></a></li>
652 <?php if ( $attachments = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post'") ) { ?>
653 <li<?php echo $current_2; ?>><a href="<?php echo basename(__FILE__) . "?action=view&post=$post&all=false"; ?>"><?php _e('Browse'); ?></a></li>
655 <?php if ($wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment'")) { ?>
656 <li<?php echo $current_3; ?>><a href="<?php echo basename(__FILE__) . "?action=view&post=$post&all=true"; ?>"><?php _e('Browse All'); ?></a></li>
659 <?php if ( $action == 'view' ) { ?>
660 <?php if ( false !== $back ) : ?>
661 <li class="spacer"><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=0"; ?>" title="<?php _e('First'); ?>">|«</a></li>
662 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=$back"; ?>">« <?php _e('Back'); ?></a></li>
664 <li class="inactive spacer">|«</li>
665 <li class="inactive">« <?php _e('Back'); ?></li>
667 <?php if ( false !== $next ) : ?>
668 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&start=$next"; ?>"><?php _e('Next »'); ?></a></li>
669 <li><a href="<?php echo basename(__FILE__) . "?action=$action&post=$post&all=$all&last=true"; ?>" title="<?php _e('Last'); ?>">»|</a></li>
671 <li class="inactive"><?php _e('Next'); ?> »</li>
672 <li class="inactive">»|</li>
674 <?php } // endif not upload?>
676 <?php if ( $action == 'view' ) : ?>
678 <!--<div class="tip"><?php _e('You can drag and drop these items into your post. Click on one for more options.'); ?></div>-->
681 <?php echo $popups; ?>
684 <?php elseif ( $action == 'upload' ) : ?>
685 <div class="tip"></div>
686 <form enctype="multipart/form-data" id="uploadForm" method="post" action="<?php echo basename(__FILE__); ?>">
687 <table style="width:99%;">
689 <th scope="row" align="right"><label for="upload"><?php _e('File:'); ?></label></th>
690 <td><input type="file" id="upload" name="image" /></td>
693 <th scope="row" align="right"><label for="title"><?php _e('Title:'); ?></label></th>
694 <td><input type="text" id="title" name="imgtitle" /></td>
697 <th scope="row" align="right"><label for="descr"><?php _e('Description:'); ?></label></th>
698 <td><input type="textarea" name="descr" id="descr" value="" /></td>
703 <input type="hidden" name="action" value="save" />
704 <input type="hidden" name="post" value="<?php echo $post; ?>" />
705 <input type="hidden" name="all" value="<?php echo $all; ?>" />
706 <input type="hidden" name="start" value="<?php echo $start; ?>" />
707 <?php wp_nonce_field( 'inlineuploading' ); ?>
709 <input type="submit" value="<?php _e('Upload'); ?>" />
710 <?php if ( !empty($all) ) : ?>
711 <input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" />
718 <?php elseif ( $action == 'links' ) : ?>
720 <?php the_attachment_links($attachment); ?>