| 1 | <?php |
|---|
| 2 | require_once('admin.php'); |
|---|
| 3 | |
|---|
| 4 | if (!current_user_can('edit_posts')) |
|---|
| 5 | die('You do not have permission to edit posts.'); |
|---|
| 6 | |
|---|
| 7 | $wpvarstoreset = array('action', 'post', 'all', 'last', 'link', 'sort', 'start', 'imgtitle', 'descr'); |
|---|
| 8 | |
|---|
| 9 | for ($i=0; $i<count($wpvarstoreset); $i += 1) { |
|---|
| 10 | $wpvar = $wpvarstoreset[$i]; |
|---|
| 11 | if (!isset($$wpvar)) { |
|---|
| 12 | if (empty($_POST["$wpvar"])) { |
|---|
| 13 | if (empty($_GET["$wpvar"])) { |
|---|
| 14 | $$wpvar = ''; |
|---|
| 15 | } else { |
|---|
| 16 | $$wpvar = $_GET["$wpvar"]; |
|---|
| 17 | } |
|---|
| 18 | } else { |
|---|
| 19 | $$wpvar = $_POST["$wpvar"]; |
|---|
| 20 | } |
|---|
| 21 | } |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | $post = (int) $post; |
|---|
| 25 | |
|---|
| 26 | switch($action) { |
|---|
| 27 | case 'save': |
|---|
| 28 | |
|---|
| 29 | // Define acceptable image extentions/types here. Tests will apply strtolower(). |
|---|
| 30 | $exts = array('gif' => IMAGETYPE_GIF, 'jpg' => IMAGETYPE_JPEG, 'png' => IMAGETYPE_PNG); |
|---|
| 31 | |
|---|
| 32 | // Define the error messages for bad uploads. |
|---|
| 33 | $upload_err = array(false, |
|---|
| 34 | "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>.", |
|---|
| 35 | "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form.", |
|---|
| 36 | "The uploaded file was only partially uploaded.", |
|---|
| 37 | "No file was uploaded.", |
|---|
| 38 | "Missing a temporary folder.", |
|---|
| 39 | "Failed to write file to disk."); |
|---|
| 40 | |
|---|
| 41 | $iuerror = false; |
|---|
| 42 | |
|---|
| 43 | // Failing any single one of the following tests is fatal. |
|---|
| 44 | |
|---|
| 45 | // A correct form post will pass this test. |
|---|
| 46 | if ( !isset($_POST['action']) || $_POST['action'] != 'save' || count($_FILES) != 1 || ! isset($_FILES['image']) || is_array($_FILES['image']['name']) ) |
|---|
| 47 | $error = 'Invalid form submission. Only submit approved forms.'; |
|---|
| 48 | |
|---|
| 49 | // A successful upload will pass this test. |
|---|
| 50 | elseif ( $_FILES['image']['error'] > 0 ) |
|---|
| 51 | $error = $upload_err[$_FILES['image']['error']]; |
|---|
| 52 | |
|---|
| 53 | // A non-empty file will pass this test. |
|---|
| 54 | elseif ( 0 == $_FILES['image']['size'] ) |
|---|
| 55 | $error = 'File is empty. Please upload something more substantial.'; |
|---|
| 56 | |
|---|
| 57 | // A correct MIME category will pass this test. Full types are not consistent across browsers. |
|---|
| 58 | elseif ( ! 'image/' == substr($_FILES['image']['type'], 0, 6) ) |
|---|
| 59 | $error = 'Bad MIME type submitted by your browser.'; |
|---|
| 60 | |
|---|
| 61 | // An acceptable file extension will pass this test. |
|---|
| 62 | elseif ( ! ( ( 0 !== preg_match('#\.?([^\.]*)$#', $_FILES['image']['name'], $matches) ) && ( $ext = strtolower($matches[1]) ) && array_key_exists($ext, $exts) ) ) |
|---|
| 63 | $error = 'Bad file extension.'; |
|---|
| 64 | |
|---|
| 65 | // A valid uploaded file will pass this test. |
|---|
| 66 | elseif ( ! is_uploaded_file($_FILES['image']['tmp_name']) ) |
|---|
| 67 | $error = 'Bad temp file. Try renaming the file and uploading again.'; |
|---|
| 68 | |
|---|
| 69 | // A valid image file will pass this test. |
|---|
| 70 | elseif ( function_exists('exif_imagetype') && $exts[$ext] != $imagetype = exif_imagetype($_FILES['image']['tmp_name']) ) |
|---|
| 71 | $error = 'Bad image file. Try again, or try recreating it.'; |
|---|
| 72 | |
|---|
| 73 | // An image with at least one pixel will pass this test. |
|---|
| 74 | elseif ( ! ( ( $imagesize = getimagesize($_FILES['image']['tmp_name']) ) && $imagesize[0] > 1 && $imagesize[1] > 1 ) ) |
|---|
| 75 | $error = 'The image has no pixels. Isn\'t that odd?'; |
|---|
| 76 | |
|---|
| 77 | // A writable uploads dir will pass this test. |
|---|
| 78 | elseif ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) |
|---|
| 79 | $error = $uploads['error']; |
|---|
| 80 | |
|---|
| 81 | if ( $error ) |
|---|
| 82 | // Something wasn't right. Abort and never touch the temp file again. |
|---|
| 83 | die("$error <a href='".basename(__FILE__)."?action=upload&post=$post'>Back to Image Uploading</a>"); |
|---|
| 84 | |
|---|
| 85 | // Increment the file number until we have a unique file to save in $dir |
|---|
| 86 | $number = ''; |
|---|
| 87 | $filename = $_FILES['image']['name']; |
|---|
| 88 | while ( file_exists($uploads['path'] . "/$filename") ) |
|---|
| 89 | $filename = str_replace("$number.$ext", ++$number . ".$ext", $filename); |
|---|
| 90 | |
|---|
| 91 | // Move the file to the uploads dir |
|---|
| 92 | $file = $uploads['path'] . "/$filename"; |
|---|
| 93 | move_uploaded_file($_FILES['image']['tmp_name'], $file); |
|---|
| 94 | chmod($file, 0775); |
|---|
| 95 | |
|---|
| 96 | // Compute the URL |
|---|
| 97 | $url = $uploads['url'] . "/$filename"; |
|---|
| 98 | |
|---|
| 99 | // Construct the object array |
|---|
| 100 | $object = array( |
|---|
| 101 | 'post_title' => $imgtitle ? $imgtitle : $filename, |
|---|
| 102 | 'post_content' => $descr, |
|---|
| 103 | 'post_status' => 'object', |
|---|
| 104 | 'post_parent' => $post, |
|---|
| 105 | 'post_type' => $_FILES['image']['type'], |
|---|
| 106 | 'guid' => $url |
|---|
| 107 | ); |
|---|
| 108 | |
|---|
| 109 | // Save the data |
|---|
| 110 | $id = wp_attach_object($object, $post); |
|---|
| 111 | |
|---|
| 112 | // Generate the object's postmeta. |
|---|
| 113 | $imagesize = getimagesize($file); |
|---|
| 114 | $imagedata['width'] = $imagesize['0']; |
|---|
| 115 | $imagedata['height'] = $imagesize['1']; |
|---|
| 116 | if ( $imagedata['height'] < 96 && $imagedata['width'] < 128 ) { |
|---|
| 117 | $uheight = $imagedata['height']; |
|---|
| 118 | $uwidth = $imagedata['width']; |
|---|
| 119 | } elseif ( $imagedata['width'] / $imagedata['height'] > 4 / 3 ) { |
|---|
| 120 | $uwidth = 128; |
|---|
| 121 | $uheight = $imagedata['height'] / $imagedata['width'] * $uwidth; |
|---|
| 122 | } else { |
|---|
| 123 | $uheight = 96; |
|---|
| 124 | $uwidth = $imagedata['width'] / $imagedata['height'] * $uheight; |
|---|
| 125 | } |
|---|
| 126 | $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'"; |
|---|
| 127 | $imagedata['file'] = $file; |
|---|
| 128 | |
|---|
| 129 | if ( false == add_post_meta($id, 'imagedata', $imagedata) ) |
|---|
| 130 | die("failed to add_post_meta"); |
|---|
| 131 | |
|---|
| 132 | header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&last=true"); |
|---|
| 133 | die; |
|---|
| 134 | |
|---|
| 135 | case 'upload': |
|---|
| 136 | ?> |
|---|
| 137 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 138 | <html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 139 | <head> |
|---|
| 140 | <script type="text/javascript"> |
|---|
| 141 | function validateImageName() { |
|---|
| 142 | /* This is more for convenience than security. Server-side validation is very thorough.*/ |
|---|
| 143 | obj = document.getElementById('upload'); |
|---|
| 144 | r = /.jpg$|.gif$|.png$/i; |
|---|
| 145 | if ( obj.value.match(r) ) |
|---|
| 146 | return true; |
|---|
| 147 | alert('Please select a JPG, PNG or GIF file.'); |
|---|
| 148 | obj.parentNode.reset(); |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | function cancelUpload() { |
|---|
| 152 | o = document.getElementById('uploadForm'); |
|---|
| 153 | o.method = 'GET'; |
|---|
| 154 | o.action.value = 'view'; |
|---|
| 155 | o.submit(); |
|---|
| 156 | } |
|---|
| 157 | </script> |
|---|
| 158 | <style type="text/css"> |
|---|
| 159 | label { |
|---|
| 160 | float: left; |
|---|
| 161 | width: 18%; |
|---|
| 162 | } |
|---|
| 163 | #title, #descr { |
|---|
| 164 | width: 80%; |
|---|
| 165 | margin-top: 2px; |
|---|
| 166 | } |
|---|
| 167 | #descr { |
|---|
| 168 | height: 3em; |
|---|
| 169 | v-align: top; |
|---|
| 170 | } |
|---|
| 171 | #buttons { |
|---|
| 172 | width: 98%; |
|---|
| 173 | text-align: right; |
|---|
| 174 | } |
|---|
| 175 | </style> |
|---|
| 176 | </head> |
|---|
| 177 | <body> |
|---|
| 178 | <form enctype="multipart/form-data" id="uploadForm" method="POST" action="image-uploading.php" onsubmit="return validateImageName()"> |
|---|
| 179 | <label for="upload">Image:</label><input type="file" id="upload" name="image" onchange="validateImageName()" /><br /> |
|---|
| 180 | <label for="title">Title:</label><input type="text" id="title" name="imgtitle" /><br /> |
|---|
| 181 | <label for="descr">Description:</label><input type="textarea" name="descr" id="descr" value="" /><br /> |
|---|
| 182 | <input type="hidden" name="action" value="save" /> |
|---|
| 183 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 184 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 185 | <div id="buttons"> |
|---|
| 186 | <input type="submit" value="Upload" /> |
|---|
| 187 | <input type="button" value="Cancel" onclick="cancelUpload()" /> |
|---|
| 188 | </div> |
|---|
| 189 | </form> |
|---|
| 190 | </body> |
|---|
| 191 | </html> |
|---|
| 192 | <?php |
|---|
| 193 | |
|---|
| 194 | break; |
|---|
| 195 | |
|---|
| 196 | case 'view': |
|---|
| 197 | |
|---|
| 198 | if ( $post && empty($all) ) |
|---|
| 199 | $and_post = "AND post_parent = '$post'"; |
|---|
| 200 | |
|---|
| 201 | if ( $last ) |
|---|
| 202 | $start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'object' AND left(post_type, 5) = 'image' $and_post") - 5; |
|---|
| 203 | else |
|---|
| 204 | $start = (int) $start; |
|---|
| 205 | |
|---|
| 206 | if ( $start < 0 ) |
|---|
| 207 | $start = 0; |
|---|
| 208 | |
|---|
| 209 | if ( '' == $sort ) |
|---|
| 210 | $sort = "ID"; |
|---|
| 211 | |
|---|
| 212 | $images = $wpdb->get_results("SELECT ID, post_date, post_title, guid FROM $wpdb->posts WHERE post_status = 'object' AND left(post_type, 5) = 'image' $and_post ORDER BY $sort LIMIT $start, 10", ARRAY_A); |
|---|
| 213 | |
|---|
| 214 | //if ( count($images) == 0 ) |
|---|
| 215 | // header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=upload"); |
|---|
| 216 | |
|---|
| 217 | if ( count($images) > 5 ) { |
|---|
| 218 | $next = $start + count($images) - 5; |
|---|
| 219 | } else { |
|---|
| 220 | $next = false; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | if ( $start > 0 ) { |
|---|
| 224 | $back = $start - 5; |
|---|
| 225 | if ( $back < 1 ) |
|---|
| 226 | $back = '0'; |
|---|
| 227 | } else { |
|---|
| 228 | $back = false; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | ?> |
|---|
| 232 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 233 | <html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 234 | <head> |
|---|
| 235 | <style type="text/css"> |
|---|
| 236 | form { |
|---|
| 237 | display: inline; |
|---|
| 238 | } |
|---|
| 239 | #images, #buttons { |
|---|
| 240 | position: absolute; |
|---|
| 241 | left: 0px; |
|---|
| 242 | width: 98%; |
|---|
| 243 | text-align: center; |
|---|
| 244 | } |
|---|
| 245 | #images { |
|---|
| 246 | top: 0px; |
|---|
| 247 | } |
|---|
| 248 | #buttons { |
|---|
| 249 | top: 112px; |
|---|
| 250 | } |
|---|
| 251 | </style> |
|---|
| 252 | </head> |
|---|
| 253 | <body> |
|---|
| 254 | <div id="images"> |
|---|
| 255 | <?php |
|---|
| 256 | if ( count($images) > 0 ) { |
|---|
| 257 | $imagerow = ''; |
|---|
| 258 | $i = 1; |
|---|
| 259 | foreach ( $images as $image ) { |
|---|
| 260 | if ( $i++ > 5 ) break; |
|---|
| 261 | $image = array_merge($image, get_post_meta($image['ID'], 'imagedata', true) ); |
|---|
| 262 | ?> |
|---|
| 263 | <a href="<?php echo $image['guid']; ?>" disabled="true"> |
|---|
| 264 | <img src="<?php echo $image['guid']; ?>" alt="<?php echo $image['post_title']; ?>" <?php echo $image['hwstring_small']; ?> /> |
|---|
| 265 | </a> |
|---|
| 266 | <?php |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | ?> |
|---|
| 270 | <div> |
|---|
| 271 | <div id="buttons"> |
|---|
| 272 | <form action="image-uploading.php" method="GET"> |
|---|
| 273 | <input type="hidden" name="action" value="view" /> |
|---|
| 274 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 275 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 276 | <input type="hidden" name="start" value="0" /> |
|---|
| 277 | <input type="submit" value="| < <" <?php if ( false === $back ) echo 'disabled="true" ' ?>/> |
|---|
| 278 | </form> |
|---|
| 279 | <form action="image-uploading.php" method="GET"> |
|---|
| 280 | <input type="hidden" name="action" value="view" /> |
|---|
| 281 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 282 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 283 | <input type="hidden" name="start" value="<?php echo $back; ?>" /> |
|---|
| 284 | <input type="submit" value="< < < < <" <?php if ( false === $back ) echo 'disabled="true" ' ?>/> |
|---|
| 285 | </form> |
|---|
| 286 | <form action="image-uploading.php" method="GET"> |
|---|
| 287 | <input type="hidden" name="action" value="upload" /> |
|---|
| 288 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 289 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 290 | <input type="submit" value="Upload New" /> |
|---|
| 291 | </form> |
|---|
| 292 | <?php if ( $all ) : ?> |
|---|
| 293 | <form action="image-uploading.php" method="GET"> |
|---|
| 294 | <input type="hidden" name="action" value="view" /> |
|---|
| 295 | <input type="hidden" name="all" value="" /> |
|---|
| 296 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 297 | <input type="submit" value="Browse Attached" /> |
|---|
| 298 | </form> |
|---|
| 299 | <?php else : ?> |
|---|
| 300 | <form action="image-uploading.php" method="GET"> |
|---|
| 301 | <input type="hidden" name="action" value="view" /> |
|---|
| 302 | <input type="hidden" name="all" value="true" /> |
|---|
| 303 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 304 | <input type="submit" value="Browse All" /> |
|---|
| 305 | </form> |
|---|
| 306 | <?php endif; ?> |
|---|
| 307 | <form action="image-uploading.php" method="GET"> |
|---|
| 308 | <input type="hidden" name="action" value="view" /> |
|---|
| 309 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 310 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 311 | <input type="hidden" name="start" value="<?php echo $next; ?>" /> |
|---|
| 312 | <input type="submit" value="> > > > >" <?php if ( false === $next ) echo 'disabled="true" ' ?>/> |
|---|
| 313 | </form> |
|---|
| 314 | <form action="image-uploading.php" method="GET"> |
|---|
| 315 | <input type="hidden" name="action" value="view" /> |
|---|
| 316 | <input type="hidden" name="all" value="<?php echo $all; ?>" /> |
|---|
| 317 | <input type="hidden" name="post" value="<?php echo $post; ?>" /> |
|---|
| 318 | <input type="hidden" name="last" value="true" /> |
|---|
| 319 | <input type="submit" value="> > |" <?php if ( false === $next ) echo 'disabled="true" ' ?>/> |
|---|
| 320 | </form> |
|---|
| 321 | </div> |
|---|
| 322 | <?php // echo "<pre>".print_r($images,1)."</pre>"; |
|---|
| 323 | ?> |
|---|
| 324 | </body> |
|---|
| 325 | </html> |
|---|
| 326 | <?php |
|---|
| 327 | die; |
|---|
| 328 | |
|---|
| 329 | default: |
|---|
| 330 | die('This script was not meant to be called directly.'); |
|---|
| 331 | } |
|---|
| 332 | ?> |
|---|