Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-admin / includes / upload.php
1 <?php
2
3 function wp_upload_display( $dims = false, $href = '' ) {
4         global $post;
5         $id = get_the_ID();
6         $attachment_data = wp_get_attachment_metadata( $id );
7         $is_image = (int) wp_attachment_is_image();
8         $filesystem_path = get_attached_file( $id );
9         if ( !isset($attachment_data['width']) && $is_image ) {
10                 if ( $image_data = getimagesize( $filesystem_path ) ) {
11                         $attachment_data['width'] = $image_data[0];
12                         $attachment_data['height'] = $image_data[1];
13                         wp_update_attachment_metadata( $id, $attachment_data );
14                 }
15         }
16         if ( isset($attachment_data['width']) )
17                 list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
18
19         $post_title = attribute_escape( the_title( '', '', false ) );
20         $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));
21
22         $class = 'text';
23         $innerHTML = get_attachment_innerHTML( $id, false, $dims );
24         if ( $image_src = get_attachment_icon_src() ) {
25                 $image_rel = wp_make_link_relative($image_src);
26                 $innerHTML = '&nbsp;' . str_replace($image_src, $image_rel, $innerHTML);
27                 $class = 'image';
28         }
29
30         $src_base = wp_get_attachment_url();
31         $src = wp_make_link_relative( $src_base );
32         $src_base = str_replace($src, '', $src_base);
33
34         if ( !trim($post_title) )
35                 $post_title = basename($src);
36
37         $r = '';
38
39         if ( $href )
40                 $r .= "<a id='file-link-$id' href='$href' title='$post_title' class='file-link $class'>\n";
41         if ( $href || $image_src )
42                 $r .= "\t\t\t$innerHTML";
43         if ( $href )
44                 $r .= "</a>\n";
45         $size = @filesize($filesystem_path);
46         if ( !empty($size) )
47                 $r .= "\t\t\t\t<span class='upload-file-size'>".size_format($size)."</span>\n";
48         $r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n";
49         $r .= "\t\t\t\t<input type='hidden' name='attachment-url-$id' id='attachment-url-$id' value='$src' />\n";
50         $r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-$id' id='attachment-url-base-$id' value='$src_base' />\n";
51
52         if ( !$thumb_base = wp_get_attachment_thumb_url() )
53                 $thumb_base = wp_mime_type_icon();
54         if ( $thumb_base ) {
55                 $thumb_rel = wp_make_link_relative( $thumb_base );
56                 $thumb_base = str_replace( $thumb_rel, '', $thumb_base );
57                 $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
58                 $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
59         }
60
61         $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n";
62
63         if ( isset($width) ) {
64                 $r .= "\t\t\t\t<input type='hidden' name='attachment-width-$id' id='attachment-width-$id' value='$width' />\n";
65                 $r .= "\t\t\t\t<input type='hidden' name='attachment-height-$id' id='attachment-height-$id' value='$height' />\n";
66         }
67         $r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-$id' id='attachment-page-url-$id' value='" . get_attachment_link( $id ) . "' />\n";
68         $r .= "\t\t\t\t<input type='hidden' name='attachment-title-$id' id='attachment-title-$id' value='$post_title' />\n";
69         $r .= "\t\t\t\t<input type='hidden' name='attachment-description-$id' id='attachment-description-$id' value='$post_content' />\n";
70         $r .= "\t\t\t</p>\n\t\t</div>\n";
71         return $r;
72 }
73
74 function wp_upload_view() {
75         global $style, $post_id, $style;
76         $id = get_the_ID();
77         $attachment_data = wp_get_attachment_metadata( $id );
78 ?>
79         <div id="upload-file">
80                 <div id="file-title">
81                         <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
82                                         echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
83                                 the_title();
84                                 if ( !isset($attachment_data['width']) && 'inline' != $style )
85                                         echo '</a>';
86                         ?></h2>
87                         <span><?php
88                                 echo '[&nbsp;';
89                                 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
90                                 echo '&nbsp;|&nbsp;';
91                                         echo '<a href="' . clean_url(add_query_arg('action', 'edit')) . '" title="' . __('Edit this file') . '">' . __('edit') . '</a>';
92                                 echo '&nbsp;|&nbsp;';
93                                 echo '<a href="' . clean_url(remove_query_arg(array('action', 'ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
94                                 echo '&nbsp;]'; ?></span>
95                 </div>
96
97                 <div id="upload-file-view" class="alignleft">
98 <?php           if ( isset($attachment_data['width']) && 'inline' != $style )
99                         echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
100                 echo wp_upload_display( array(171, 128) );
101                 if ( isset($attachment_data['width']) && 'inline' != $style )
102                         echo '</a>'; ?>
103                 </div>
104                 <?php the_attachment_links( $id ); ?>
105         </div>
106 <?php   echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' )  . "' /></form>\n";
107 }
108
109 function wp_upload_form() {
110         $id = get_the_ID();
111         global $post_id, $tab, $style;
112         $enctype = $id ? '' : ' enctype="multipart/form-data"';
113         $post_id = (int) $post_id;
114 ?>
115         <form<?php echo $enctype; ?> id="upload-file" method="post" action="<?php echo get_option('siteurl') . '/wp-admin/upload.php?style=' . attribute_escape($style . '&amp;tab=upload&amp;post_id=' . $post_id); ?>">
116 <?php
117         if ( $id ) :
118                 $attachment = get_post_to_edit( $id );
119                 $attachment_data = wp_get_attachment_metadata( $id );
120 ?>
121                 <div id="file-title">
122                         <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
123                                         echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
124                                 the_title();
125                                 if ( !isset($attachment_data['width']) && 'inline' != $style )
126                                         echo '</a>';
127                         ?></h2>
128                         <span><?php
129                                 echo '[&nbsp;';
130                                 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
131                                 echo '&nbsp;|&nbsp;';
132                                         echo '<a href="' . clean_url(add_query_arg('action', 'view')) . '">' . __('links') . '</a>';
133                                 echo '&nbsp;|&nbsp;';
134                                 echo '<a href="' . clean_url(remove_query_arg(array('action','ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
135                                 echo '&nbsp;]'; ?></span>
136                 </div>
137
138         <div id="upload-file-view" class="alignleft">
139 <?php           if ( isset($attachment_data['width']) && 'inline' != $style )
140                         echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
141                 echo wp_upload_display( array(171, 128) );
142                 if ( isset($attachment_data['width']) && 'inline' != $style )
143                         echo '</a>'; ?>
144         </div>
145 <?php   endif; ?>
146                 <table><col /><col class="widefat" />
147 <?php   if ( $id ): ?>
148                         <tr>
149                                 <th scope="row"><label for="url"><?php _e('URL'); ?></label></th>
150                                 <td><input type="text" id="url" class="readonly" value="<?php echo wp_get_attachment_url(); ?>" readonly="readonly" /></td>
151                         </tr>
152 <?php   else : ?>
153                         <tr>
154                                 <th scope="row"><label for="upload"><?php _e('File'); ?></label></th>
155                                 <td><input type="file" id="upload" name="image" /></td>
156                         </tr>
157 <?php   endif; ?>
158                         <tr>
159                                 <th scope="row"><label for="post_title"><?php _e('Title'); ?></label></th>
160                                 <td><input type="text" id="post_title" name="post_title" value="<?php echo $attachment->post_title; ?>" /></td>
161                         </tr>
162                         <tr>
163                                 <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
164                                 <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
165                         </tr>
166                         <tr id="buttons" class="submit">
167                                 <td colspan='2'>
168 <?php   if ( $id ) : ?>
169                                         <input type="submit" name="delete" id="delete" class="delete alignleft" value="<?php _e('Delete File'); ?>" />
170 <?php   endif; ?>
171                                         <input type="hidden" name="from_tab" value="<?php echo $tab; ?>" />
172                                         <input type="hidden" name="action" value="<?php echo $id ? 'save' : 'upload'; ?>" />
173 <?php   if ( $post_id ) : ?>
174                                         <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
175 <?php   endif; if ( $id ) : ?>
176                                         <input type="hidden" name="ID" value="<?php echo $id; ?>" />
177 <?php   endif; ?>
178                                         <?php wp_nonce_field( 'inlineuploading' ); ?>
179                                         <div class="submit">
180                                                 <input type="submit" value="<?php $id ? _e('Save &raquo;') : _e('Upload &raquo;'); ?>" />
181                                         </div>
182                                 </td>
183                         </tr>
184                 </table>
185         </form>
186 <?php
187 }
188
189 function wp_upload_tab_upload() {
190         wp_upload_form();
191 }
192
193 function wp_upload_tab_upload_action() {
194         global $action;
195         if ( isset($_POST['delete']) )
196                 $action = 'delete';
197
198         switch ( $action ) :
199         case 'upload' :
200                 global $from_tab, $post_id, $style;
201                 if ( !$from_tab )
202                         $from_tab = 'upload';
203
204                 check_admin_referer( 'inlineuploading' );
205
206                 global $post_id, $post_title, $post_content;
207
208                 if ( !current_user_can( 'upload_files' ) )
209                         wp_die( __('You are not allowed to upload files.')
210                                 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=browse-all&amp;post_id=$post_id") . "'>"
211                                 . __('Browse Files') . '</a>'
212                         );
213
214                 $overrides = array('action'=>'upload');
215
216                 $file = wp_handle_upload($_FILES['image'], $overrides);
217
218                 if ( isset($file['error']) )
219                         wp_die($file['error'] . "<br /><a href='" . get_option('siteurl')
220                         . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=$from_tab&amp;post_id=$post_id") . "'>" . __('Back to Image Uploading') . '</a>'
221                 );
222
223                 $url = $file['url'];
224                 $type = $file['type'];
225                 $file = $file['file'];
226                 $filename = basename($file);
227
228                 // Construct the attachment array
229                 $attachment = array(
230                         'post_title' => $post_title,
231                         'post_content' => $post_content,
232                         'post_type' => 'attachment',
233                         'post_parent' => $post_id,
234                         'post_mime_type' => $type,
235                         'guid' => $url
236                 );
237
238                 // Save the data
239                 $id = wp_insert_attachment($attachment, $file, $post_id);
240
241                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
242
243                 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
244                 die;
245                 break;
246
247         case 'save' :
248                 global $from_tab, $post_id, $style;
249                 if ( !$from_tab )
250                         $from_tab = 'upload';
251                 check_admin_referer( 'inlineuploading' );
252
253                 wp_update_post($_POST);
254                 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id");
255                 die;
256                 break;
257
258         case 'delete' :
259                 global $ID, $post_id, $from_tab, $style;
260                 if ( !$from_tab )
261                         $from_tab = 'upload';
262
263                 check_admin_referer( 'inlineuploading' );
264
265                 if ( !current_user_can('edit_post', (int) $ID) )
266                         wp_die( __('You are not allowed to delete this attachment.')
267                                 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=$from_tab&amp;post_id=$post_id") . "'>"
268                                 . __('Go back') . '</a>'
269                         );
270
271                 wp_delete_attachment($ID);
272
273                 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id" );
274                 die;
275                 break;
276
277         endswitch;
278 }
279
280 add_action( 'upload_files_upload', 'wp_upload_tab_upload_action' );
281
282 function wp_upload_grab_attachments( $obj ) {
283         $obj->is_attachment = true;
284 }
285
286 function wp_upload_posts_where( $where ) {
287         global $post_id;
288         return $where . " AND post_parent = '" . (int) $post_id . "'";
289 }
290
291 function wp_upload_tab_browse() {
292         global $action, $paged;
293         $old_vars = compact( 'paged' );
294
295         switch ( $action ) :
296         case 'edit' :
297         case 'view' :
298                 global $ID;
299                 $attachments = query_posts("attachment_id=$ID");
300                 if ( have_posts() ) : while ( have_posts() ) : the_post();
301                         'edit' == $action ? wp_upload_form() : wp_upload_view();
302                 endwhile; endif;
303                 break;
304         default :
305                 global $tab, $post_id, $style;
306                 add_action( 'pre_get_posts', 'wp_upload_grab_attachments' );
307                 if ( 'browse' == $tab && $post_id )
308                         add_filter( 'posts_where', 'wp_upload_posts_where' );
309                 $attachments = query_posts("what_to_show=posts&post_status=any&posts_per_page=10&paged=$paged");
310
311                 echo "<ul id='upload-files'>\n";
312                 if ( have_posts() ) : while ( have_posts() ) : the_post();
313                         $href = wp_specialchars( add_query_arg( array(
314                                 'action' => 'inline' == $style ? 'view' : 'edit',
315                                 'ID' => get_the_ID())
316                          ), 1 );
317
318                         echo "\t<li id='file-";
319                         the_ID();
320                         echo "' class='alignleft'>\n";
321                         echo wp_upload_display( array(128,128), $href );
322                         echo "\t</li>\n";
323                 endwhile;
324                 else :
325                         echo "\t<li>" . __('There are no attachments to show.') . "</li>\n";
326                 endif;
327                 echo "</ul>\n\n";
328
329                 echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' )  . "' /></form>\n";
330                 break;
331         endswitch;
332
333         extract($old_vars);
334 }
335
336
337 function wp_upload_tab_browse_action() {
338         global $style;
339         if ( 'inline' == $style )
340                 wp_enqueue_script('upload');
341 }
342
343 add_action( 'upload_files_browse', 'wp_upload_tab_browse_action' );
344 add_action( 'upload_files_browse-all', 'wp_upload_tab_browse_action' );
345
346 function wp_upload_admin_head() {
347         wp_admin_css( 'css/upload' );
348         if ( 'inline' == @$_GET['style'] ) {
349                 echo "<style type='text/css' media='screen'>\n";
350                 echo "\t#upload-menu { position: absolute; z-index: 2; }\n";
351                 echo "\tbody > #upload-menu { position: fixed; }\n";
352                 echo "\t#upload-content { top: 2em; }\n";
353                 echo "\t#upload-file { position: absolute; top: 15px; }\n";
354                 echo "</style>";
355         }
356 }
357
358 ?>