Changeset 1181
- Timestamp:
- 07/22/08 22:31:19 (4 months ago)
- Files:
-
- bb-attachments/trunk/bb-attachments.php (modified) (10 diffs)
- bb-attachments/trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
bb-attachments/trunk/bb-attachments.php
r1128 r1181 6 6 Author: _ck_ 7 7 Author URI: http://bbShowcase.org 8 Version: 0.1. 28 Version: 0.1.5 9 9 10 10 License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ … … 14 14 15 15 $bb_attachments['role']['see']="read"; // minimum role to see list of attachments = read/participate/moderate/administrate 16 $bb_attachments['role']['inline']="read"; // minimum role to view inline images = read/participate/moderate/administrate 16 17 $bb_attachments['role']['download']="participate"; // minimum role to download = read/participate/moderate/administrate 17 18 $bb_attachments['role']['upload']="participate"; // minimum role to upload = participate/moderate/administrate (times out with post edit time) … … 40 41 $bb_attachments['max']['filename']['administrate']=80; // override 41 42 43 $bb_attachments['inline']['width']=590; // max inline image width in pixels (for display, real width unlimited) 44 $bb_attachments['inline']['height']=590; // max inline image height in pixels (for display, real height unlimited) 45 $bb_attachments['inline']['solution']="resize"; // resize|frame - images can be either resized or CSS framed to meet above requirement 46 // only resize is supported at this time 42 47 $bb_attachments['style']="#thread .post li {clear:none;}"; 43 48 … … 61 66 add_action( 'bb_init', 'bb_attachments_init'); 62 67 add_action( 'bb_post.php', 'bb_attachments_process_post'); 68 add_filter( 'bb_allowed_tags', 'bb_attachments_extra_tags' ); 63 69 bb_register_activation_hook( __FILE__, 'bb_attachments_install'); 64 70 … … 72 78 if (isset($_GET['bb_attachments'])) { 73 79 if (isset($_GET['bbat'])) { 74 bb_attachments_download(); 80 if (isset($_GET['inline'])) {bb_attachments_inline();} 81 else {bb_attachments_download();} 75 82 } else { 76 83 if (bb_attachments_location()!='edit.php') { … … 115 122 function bb_attachments_add_css() { global $bb_attachments; echo '<style type="text/css">'.$bb_attachments['style'].'</style>';} // inject css 116 123 124 function bb_attachments_extra_tags( $tags ) {if (!isset($tags['img'])) {$tags['img'] = array('src' => array(), 'title' => array(), 'alt' => array());} return $tags;} 125 117 126 function bb_attachments($post_id=0) { 118 127 global $bb_attachments_on_page; … … 149 158 if ((!is_topic() || isset($_GET['bb_attachments'])) && bb_current_user_can('moderate')) {$filter=""; $admin=bb_current_user_can('administrate');} 150 159 if (bb_current_user_can($bb_attachments['role']['delete'])) {$can_delete=true;} 160 161 $file = bb_attachments_location(); $can_inline=true; 162 if (!($bb_attachments['role']['inline']=="read" || bb_current_user_can($bb_attachments['role']['inline']))) {$can_inline=false;} 151 163 152 164 if (!isset($bb_attachments_cache[$post_id])) { 153 165 $bb_attachments_cache[$post_id]=$bbdb->get_results("SELECT * FROM bb_attachments WHERE post_id = $post_id ORDER BY time DESC LIMIT 999"); 154 } 155 166 } 156 167 157 168 if (count($bb_attachments_cache[$post_id])) { … … 191 202 $output.=' [<a onClick="return confirm('."'".__('Delete')." $attachment->filename ?"."'".')" href="'.add_query_arg('bbat_delete',$attachment->id).'">x</a>] '; 192 203 } 204 205 if ($attachment->status==0 && $file=="edit.php" && $can_inline) { 206 $output.=" [<a href='#' onclick='bbatt_inline_insert($attachment->post_id,$attachment->id); return false;'>".__("INSERT")."</a>] "; 207 } 193 208 194 209 $output.=" </span></li>"; … … 197 212 } 198 213 if ($output) {$output="<h3>".__("Attachments")."</h3><ol>".$output."</ol>";} 214 if ($file="edit.php") { 215 $output.='<scr'.'ipt type="text/javascript" defer="defer">function bbatt_inline_insert(post_id,id) { 216 myField=document.getElementsByTagName("textarea")[0]; 217 tmp="?bb_attachments="+post_id+"&bbat="+id; q="'."'".'"; 218 myValue="<a href="+q+tmp+q+"><img src="+q+tmp+"&inline"+q+"></a>"; 219 if (document.selection) {myField.focus();sel = document.selection.createRange();sel.text = myValue;} 220 else if (myField.selectionStart || myField.selectionStart == "0") {var startPos = myField.selectionStart; var endPos = myField.selectionEnd; 221 myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 222 } else {myField.value += myValue;} 223 return false;} </script>'; 224 } 199 225 } 200 226 return $output; … … 368 394 exit(); 369 395 } 370 } else {echo "<script type='text/javascript'>alert('".__('Sorry, download is restricted.')."');</script>";} 396 } else {echo "<scr"."ipt type='text/javascript'>alert('".__('Sorry, download is restricted.')."');</script>";} 397 } 398 399 function bb_attachments_inline($filenum=0) { 400 global $bbdb, $bb_attachments; 401 $filenum=intval($filenum); 402 if ($filenum==0 && isset($_GET['bbat'])) {$filenum=intval($_GET['bbat']);} 403 if ($filenum>0 && ($bb_attachments['role']['inline']=="read" || bb_current_user_can($bb_attachments['role']['inline']))) { 404 $file=$bbdb->get_results("SELECT * FROM bb_attachments WHERE id = $filenum AND status = 0 LIMIT 1"); 405 if (isset($file[0]) && $file[0]->id) { 406 $file=$file[0]; $file->filename=stripslashes($file->filename); 407 $fullpath=$bb_attachments['path'].floor($file->id/1000)."/".$file->id.".".$file->filename; 408 if (file_exists($fullpath)) { 409 if (!list($width, $height, $type) = getimagesize($fullpath)) {exit();} // not an image?! 410 $mime=image_type_to_mime_type($type); // lookup number to full string 411 $mime=trim(substr($mime,0,strpos($mime.";",";"))); // trim full string if necessary 412 413 if ($height>$bb_attachments['inline']['height'] || $width>$bb_attachments['inline']['width']) { 414 if (!file_exists($fullpath.".resize") && !bb_attachments_resize($fullpath,$type,$width,$height)) {exit();} 415 $fullpath.=".resize"; 416 $file->filename.=".resize"; 417 $file->size=filesize($fullpath); 418 if (!$file->size) {exit();} 419 } 420 421 if (ini_get('zlib.output_compression')) {ini_set('zlib.output_compression', 'Off');} // fix for IE 422 // header ("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); 423 // header("Pragma: hack"); 424 header("Content-Type: ".$mime); 425 header("Content-Length: $file->size"); 426 header('Content-Disposition: inline; filename="'.$file->filename.'"'); 427 header("Content-Transfer-Encoding: binary"); 428 ob_clean(); 429 flush(); 430 $fp = @fopen($fullpath,"rb"); 431 set_time_limit(0); 432 fpassthru($fp); // avoids file touch bug with readfile 433 fclose($fp); 434 } 435 exit(); 436 } 437 } else { 438 $im=@imagecreatetruecolor(1,1); 439 header("Content-type: image/png"); 440 header("Content-Length: ".strlen($im)); 441 imagepng($im); 442 imagedestroy($im); 443 exit(); 444 } 445 } 446 447 function bb_attachments_resize($filename, $type, $width, $height) { 448 global $bb_attachments; 449 switch($type) { 450 case IMAGETYPE_JPEG: 451 $img = imagecreatefromjpeg($filename); $save= 'imagejpeg'; break; 452 case IMAGETYPE_GIF: 453 $img = imagecreatefromgif($filename); $save='imagegif'; break; 454 case IMAGETYPE_PNG: 455 $img = imagecreatefrompng($filename); $save_img = 'imagepng'; break; 456 default: 457 return false; 458 } 459 $scale_x=1; if ($width>$bb_attachments['inline']['width']) {$scale_x = $width/$bb_attachments['inline']['width'];} 460 $scale_y=1; if ($height>$bb_attachments['inline']['height']) {$scale_y = $height/$bb_attachments['inline']['height'];} 461 $scale=$scale_x; if ($scale_y>$scale_x) {$scale=$scale_y;}; 462 if ($scale<=1) {return false;} 463 $new_width = round(1/$scale * $width); 464 $new_height = round(1/$scale * $height); 465 466 $output=$filename.".resize"; 467 if (!file_exists($output)) { 468 $new= imagecreatetruecolor($new_width, $new_height); 469 imagecopyresampled($new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 470 $save($new, $output); 471 imagedestroy($new); 472 } 473 imagedestroy($img); 474 return true; 371 475 } 372 476 bb-attachments/trunk/readme.txt
r1069 r1181 51 51 * 0.1.0 uploading now possible directly on new posts (instead of only attaching afterwards) 52 52 * 0.1.1 role check before hooks, ability to disable upload on new posts if compatibility issues 53 * 0.1.5 first attempt at inline image display ability 53 54 54 55 == To Do == … … 57 58 * check for file duplicates before saving 58 59 * thumbnails for image attachments 59 * serving images inline rather than just downloading60 60 * pre-validate upload filenames via javascript to spare user upload time with rejection 61 61 * admin menu
