Changeset 996

Show
Ignore:
Timestamp:
04/21/08 12:59:56 (7 months ago)
Author:
_ck_
Message:

0.0.6 advanced restrictions by file type & user role, upload form displays allowed file types

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bb-attachments/trunk/bb-attachments.php

    r992 r996  
    66Author: _ck_ 
    77Author URI: http://bbShowcase.org 
    8 Version: 0.0.5 
     8Version: 0.0.6 
    99 
    1010License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ 
     
    1313*/ 
    1414 
    15 global $bb_attachments; 
     15$bb_attachments['role']['see']="read";           // minimum role to see list of attachments = read/participate/moderate/administrate 
     16$bb_attachments['role']['download']="participate";  // minimum role to download = read/participate/moderate/administrate 
     17$bb_attachments['role']['upload']="moderate";  // minimum role to upload = participate/moderate/administrate (times out with post edit time) 
     18$bb_attachments['role']['delete']="moderate";  // minimum role to delete = read/participate/moderate/administrate 
     19 
     20$bb_attachments['allowed']['extensions']['default']=array('gif','jpeg','jpg','pdf','png','txt');        // anyone who can upload can submit these 
     21$bb_attachments['allowed']['extensions']['moderate']=array('gif','gz','jpeg','jpg','pdf','png','txt','zip');    // only if they can moderate 
     22$bb_attachments['allowed']['extensions']['administrate']=array('bmp','doc','gif','gz','jpeg','jpg','pdf','png','txt','xls','zip');      // only if they can administrate 
     23 
     24$bb_attachments['allowed']['mime_types']['default']=array('text/plain', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif');  // for anyone that can upload 
     25$bb_attachments['allowed']['mime_types']['moderate']=array('text/plain', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'application/zip', 'application/x-zip' , 'application/x-gzip'); 
     26$bb_attachments['allowed']['mime_types']['administrate']=array('text/plain', 'text/x-c', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'application/zip', 'application/x-zip' , 'application/x-gzip'); 
     27 
     28$bb_attachments['max']['size']['default']=100*1024;        // general max for all type/roles, in bytes (ie. 100k) 
     29$bb_attachments['max']['size']['jpg'] =150*1024;                   // size limit override by extension, bytes (ie. 200k) 
     30$bb_attachments['max']['size']['png']=150*1024;            // size limit override by extension, bytes (ie. 200k) 
     31$bb_attachments['max']['size']['moderate']=200*1024;       // size limit override by role, bytes (ie. 250k) - note this overrides ALL extension limits 
     32$bb_attachments['max']['size']['administrate']=500*1024; // size limit override by role, bytes (ie. 500k) - note this overrides ALL extension limits 
     33 
     34$bb_attachments['max']['per_post']['default']=6;                // how many files can be attached per post 
     35$bb_attachments['max']['per_post']['moderate']=10;      // override example $bb_attachments['max']['per_post']['administrate']=20;      // you don't even need to set for every role, this is just an example  
     36$bb_attachments['max']['uploads']['default']=6;         // how many files can be uploaded at a time, in case you want to set per_post high 
     37$bb_attachments['max']['uploads']['moderate']=10;       // and again, this is optional per extra roles 
     38 
     39$bb_attachments['max']['filename']['default']=40;       // maximum length of filename before auto-trim 
     40$bb_attachments['max']['filename']['administrate']=80;  // override 
     41 
     42// stop editing here (advanced user settings below) 
    1643 
    1744$bb_attachments['path']=dirname($_SERVER['DOCUMENT_ROOT'])."/bb-attachments/";  //  make *NOT* WEB ACCESSABLE for security 
    1845 
    19 $bb_attachments['allowed_extensions']=array('bmp','doc','gif','gz','jpeg','jpg','pdf','png','txt','xls','zip'); 
    20  
    21 $bb_attachments['allowed_mime_types']=array('text/plain', 'text/x-c', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'application/zip', 'application/x-zip' , 'application/x-gzip'); 
    22  
    23 $bb_attachments['icons']=array('bmp'=>'img','doc'=>'doc','gif'=>'img','gz'=>'zip','jpeg'=>'img','jpg'=>'img','pdf'=>'pdf','png'=>'img','txt'=>'txt','xls'=>'xls','zip'=>'zip'); 
    24  
    25 $bb_attachments['max_size_default']=200*1024;   // general max for all, in bytes (ie. 200k) 
    26  
    27 $bb_attachments['max_size_overide']=array('jpg'=>200*1024,'png'=>200*1024);     // size limit override by extension, bytes (ie. 200k) 
    28  
    29 $bb_attachments['role_upload']="moderate";  // minimum role to upload = participate/moderate/administrate (times out with post edit time) 
    30  
    31 $bb_attachments['role_download']="participate";  // minimum role to download = read/participate/moderate/administrate 
    32  
    33 $bb_attachments['role_see']="read";  // minimum role to see list of attachments = read/participate/moderate/administrate 
    34  
    35 $bb_attachments['role_delete']="moderate";  // minimum role to delete = read/participate/moderate/administrate 
    36  
    37 $bb_attachments['max_per_post']=10;     // how many can be attached per post  
    38 $bb_attachments['max_uploads']=10;      // how many files can be uploaded at a time / per post 
    39  
    40 $bb_attachments['icon_url']=bb_get_option('uri').trim(str_replace(BBPATH,'',dirname(__FILE__)),' /\\').'/icons/';  
    41  
    42 $bb_attachments['title']=" <img title='attachments' border='0' align='absmiddle' src='".$bb_attachments['icon_url']."default.gif"."' />"; // text, html or image to show on topic titles if has attachments 
    43  
    44 // stop editing here 
    45  
    46 $bb_attachments['max_file_size']=2048*1024;     // internal php upload limit - only edit if you know what you are doing 
     46$bb_attachments['icons']=array('default'=>'default.gif','bmp'=>'img.gif','doc'=>'doc.gif','gif'=>'img.gif','gz'=>'zip.gif','jpeg'=>'img.gif','jpg'=>'img.gif','pdf'=>'pdf.gif','png'=>'img.gif','txt'=>'txt.gif','xls'=>'xls.gif','zip'=>'zip.gif'); 
     47$bb_attachments['icons']['url']=bb_get_option('uri').trim(str_replace(BBPATH,'',dirname(__FILE__)),' /\\').'/icons/';  
     48 
     49$bb_attachments['title']=" <img title='attachments' border='0' align='absmiddle' src='".$bb_attachments['icons']['url'].$bb_attachments['icons']['default']."' />"; // text, html or image to show on topic titles if has attachments 
     50 
     51$bb_attachments['max']['php_upload_limit']=min(bb_attachments_l2n(ini_get('post_max_size')), bb_attachments_l2n(ini_get('upload_max_filesize'))); // internal php upload limit - only edit if you know what you are doing 
    4752 
    4853$bb_attachments['status']=array("ok","deleted","failed","denied extension","denied mime","denied size","denied count","denied duplicate","denied dimensions"); 
     54 
     55// really stop editing! 
    4956 
    5057add_action( 'bb_init', 'bb_attachments_init'); 
     
    5360function bb_attachments_init() { 
    5461global $bbdb, $post_id, $bb_post, $topic, $bb_attachments; 
     62 
     63if (isset($_GET['bb_attachments_diagnostic']) && bb_current_user_can('administrate')) {echo "<pre>"; print_r($bb_attachments); echo "</pre>"; exit();} 
     64 
    5565if (isset($_GET['bbat_delete'])) {bb_attachments_delete();} 
     66 
    5667if (isset($_GET['bb_attachments'])) { 
    5768        if (isset($_GET['bbat'])) { 
    5869                bb_attachments_download(); 
    5970        } else { 
    60         if (bb_attachments_location()!='edit.php') { 
    61                 bb_repermalink();                
    62                 bb_get_header(); 
    63                 bb_attachments(); 
    64                 bb_get_footer(); 
    65                 exit();  
    66         } 
    67 
    68 
     71                if (bb_attachments_location()!='edit.php') { 
     72                        bb_repermalink();                
     73                        bb_get_header(); 
     74                        bb_attachments(); 
     75                        bb_get_footer(); 
     76                        exit();  
     77                } 
     78        } 
     79
     80 
    6981if (is_topic()) { 
    7082        add_action( 'bb_topic.php', 'bb_attachments_cache' ); 
     
    7486        if ($bb_attachments['title']) {add_filter('topic_title', 'bb_attachments_title',200);} 
    7587} 
     88 
    7689} 
    7790 
     
    100113 
    101114$output="";      
    102 if ($post_id && ($bb_attachments['role_see']=="read" || bb_current_user_can($bb_attachments['role_see']))) { 
     115if ($post_id && ($bb_attachments['role']['see']=="read" || bb_current_user_can($bb_attachments['role']['see']))) { 
    103116         
    104117        $can_delete=false; $admin=false; $filter=" AND status = 0 ";    // speedup checks with flag      
    105118        if ((!is_topic() || isset($_GET['bb_attachments'])) && bb_current_user_can('moderate')) {$filter=""; $admin=bb_current_user_can('administrate');}         
    106         if (bb_current_user_can($bb_attachments['role_delete'])) {$can_delete=true;} 
     119        if (bb_current_user_can($bb_attachments['role']['delete'])) {$can_delete=true;} 
    107120                 
    108121        if (!isset($bb_attachments_cache[$post_id])) { 
     
    117130                                $output.="<li>";  
    118131                                $output.="<span".(($attachment->status>0) ? " class='deleted' ": "")."> ";  
    119                                 if (isset($bb_attachments['icons'][$attachment->ext])) {$icon=$bb_attachments['icons'][$attachment->ext];} else {$icon="default";} 
    120                                 $output.=" <img align='absmiddle' title='".$attachment->ext."' src='".$bb_attachments['icon_url'].$icon.".gif' /> "; 
     132                                if (isset($bb_attachments['icons'][$attachment->ext])) {$icon=$bb_attachments['icons'][$attachment->ext];} else {$icon=$bb_attachments['icons']['default'];} 
     133                                $output.=" <img align='absmiddle' title='".$attachment->ext."' src='".$bb_attachments['icons']['url'].$icon."' /> "; 
    121134                                 
    122135                                if ($attachment->status>0 && empty($filter)) {                                   
     
    161174$post_id=intval($_GET['bb_attachments']);       // only can upload if user is allowed to edit post 
    162175$user_id=bb_get_current_user_info( 'id' ); 
    163 if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role_upload'])) {return;}    
     176if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;}         
    164177 
    165178$user_ip=$GLOBALS["HTTP_SERVER_VARS"]["REMOTE_ADDR"]; 
     
    171184$offset=0;      // counter for this pass 
    172185$strip = array(' ','`','"','\'','\\','/','..','__');  // filter for filenames 
     186$maxlength=bb_attachments_lookup($bb_attachments['max']['filename']); 
    173187 
    174188echo "<h3>".__("Uploads")."</h3><ol>";  // start output 
     
    182196                if (intval($_FILES['bb_attachments']['error'][$key])==0) { 
    183197                         
    184                         $ext = (strrpos($filename, '.')===false) ? "" : strtolower(substr($filename, strrpos($filename, '.')+1)); 
     198                        $ext = (strrpos($filename, '.')===false) ? "" : trim(strtolower(substr($filename, strrpos($filename, '.')+1))); 
     199                                                 
     200                        if (strlen($filename)>$maxlength) {$filename=substr($filename,0,$maxlength-strlen($ext)+1).".".$ext;}   // fix filename length 
     201                                                 
    185202                        $tmp=$_FILES['bb_attachments']['tmp_name'][$key]; 
    186203                        $size=filesize($tmp);    
     
    188205                        $status=0; $id=0; 
    189206                         
    190                         if ($status==0 && !in_array($ext,$bb_attachments['allowed_extensions'])) {$status=3;}           // disallowed extension 
    191                         if ($status==0 && !in_array($mime,$bb_attachments['allowed_mime_types'])) {$status=4;}          // disallowed mime 
    192                         if ($status==0) { 
    193                                 if (isset($bb_attachments['max_size_override'][$ext]))  { 
    194                                         if ($size>$bb_attachments['max_size_override'][$ext]) {$status=5;}                      // disallowed filesize    
    195                                 } else { 
    196                                         if ($size>$bb_attachments['max_size_default'])    {$status=5;}                          // disallowed filesize    
    197                                 } 
    198                         }                                        
    199                         if ($status==0 && ($count+1)>$bb_attachments['max_per_post']) {$status=6;}                      // disallowed attachment count 
     207                        if ($status==0 && !in_array($ext,bb_attachments_lookup($bb_attachments['allowed']['extensions']))) {$status=3;} // disallowed extension 
     208                        if ($status==0 && !in_array($mime,bb_attachments_lookup($bb_attachments['allowed']['mime_types']))) {$status=4;}        // disallowed mime 
     209                        if ($status==0 && $size>bb_attachments_lookup($bb_attachments['max']['size'],$ext)) {$status=5;}         // disallowed size                                              
     210                        if ($status==0 && ($count+1)>bb_attachments_lookup($bb_attachments['max']['per_post'])) {$status=6;}     // disallowed attachment count 
    200211    
    201212                        if ($size>0 && $filename) {     // we still save the status code if any but don't copy file until status = 0 
     
    248259                if ($status>0) { 
    249260                        if ($id>0) {$bbdb->query("UPDATE  bb_attachments SET `status` = $status WHERE `id` = $id");} 
    250                         echo "<li><span style='color:red'><strong>$filename ".__('error:')." ".$bb_attachments['status'][$status]."</strong></span></li>"; 
    251                 } else {echo "<li><span style='color:green'><strong>$filename ".__('successful')."</strong></span></li>";} 
     261                        echo "<li><span style='color:red'><strong>$filename "." <span class='num'>(".round($size/1024,1)." KB)</span> ".__('error:')." ".$bb_attachments['status'][$status]."</strong></span></li>"; 
     262                } else {echo "<li><span style='color:green'><strong>$filename "." <span class='num'>(".round($size/1024,1)." KB)</span> ".__('successful')."</strong></span></li>";} 
    252263        } // end !$empty 
    253264} // end while 
     
    261272if (!$post_id) {$post_id=intval($_GET['bb_attachments']);}      // only can upload if user is allowed to edit post 
    262273$user_id=bb_get_current_user_info( 'id' ); 
    263 if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role_upload'])) {return;}      
     274if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;}   
     275 
     276$count=0; $allowed=__('allowed uploads:')." "; $exts=bb_attachments_lookup($bb_attachments['allowed']['extensions']); 
     277$tcount=count($exts); foreach ($exts as $ext) { 
     278$allowed.=$ext.' <span class="num">('.round(bb_attachments_lookup($bb_attachments['max']['size'],$ext)/1024,1).' KB)</span>, '; 
     279$count++; if ($count==5 && $tcount>7) {$allowed.="<br />";} 
     280
     281$allowed=rtrim($allowed," ,"); 
    264282 
    265283echo '<form class="bb_attachments_upload_form" enctype="multipart/form-data" method="post" action="'.attribute_escape(add_query_arg('bb_attachments',$post_id,remove_query_arg(array('bb_attachments','bbat','bbat_delete')))).'">       
    266         <h3>'.__("Upload Files from your Computer").'</h3>       
    267         <input  type="hidden" name="MAX_FILE_SIZE" value="'.$bb_attachments['max_file_size'].'" />     
     284        <h3>'.__("Upload Files from your Computer").'</h3>              
     285        <input  type="hidden" name="MAX_FILE_SIZE" value="'.$bb_attachments['max']['php_upload_limit'].'" />   
    268286        <span id="bb_attachments_file_sample"> 
    269287        <input type="file" name="bb_attachments[]" size="50" /><br /> 
     
    274292        bb_attachment_input_count=2; 
    275293        function bb_attachment_inputs() {                
    276                 bb_attachment_input_count=bb_attachment_input_count+2; if (bb_attachment_input_count<='.$bb_attachments['max_uploads'].') {                    
     294                bb_attachment_input_count=bb_attachment_input_count+2; if (bb_attachment_input_count<='.bb_attachments_lookup($bb_attachments['max']['uploads']).') {                  
    277295                document.getElementById('."'bb_attachments_file_input_'".'+bb_attachment_input_count).innerHTML+=document.getElementById('."'bb_attachments_file_sample'".').innerHTML+"<div id=bb_attachments_file_input_"+(bb_attachment_input_count+2)+"></div>"; 
    278296                }                                        
    279297        } 
    280298        </script> 
     299        '.$allowed.'<br /> 
    281300        <div style="margin:1em 0 0 0;">  
    282301         <a style="margin-right:12em;" href="'. get_post_link( $post_id ).'">'.__("&laquo; return to post").'</a> 
     
    291310$filenum=intval($filenum); 
    292311if ($filenum==0 && isset($_GET['bbat'])) {$filenum=intval($_GET['bbat']);} 
    293 if ($filenum>0 && ($bb_attachments['role_download']=="read" || bb_current_user_can($bb_attachments['role_download']))) { 
     312if ($filenum>0 && ($bb_attachments['role']['download']=="read" || bb_current_user_can($bb_attachments['role']['download']))) { 
    294313        $file=$bbdb->get_results("SELECT * FROM bb_attachments WHERE id = $filenum AND status = 0 LIMIT 1");             
    295314        if (isset($file[0]) && $file[0]->id) { 
     
    321340$filenum=intval($filenum); 
    322341if ($filenum==0 && isset($_GET['bbat_delete'])) {$filenum=intval($_GET['bbat_delete']);} 
    323 if ($filenum>0 && bb_current_user_can($bb_attachments['role_delete'])) { 
     342if ($filenum>0 && bb_current_user_can($bb_attachments['role']['delete'])) { 
    324343        $file=$bbdb->get_results("SELECT * FROM bb_attachments WHERE id = $filenum AND status = 0 LIMIT 1");             
    325344        if (isset($file[0]) && $file[0]->id) { 
     
    355374global $bb_attachments, $bb_attachments_cache, $bb_post, $bb_current_user; 
    356375$post_id=$bb_post->post_id; 
    357         if (($bb_current_user->ID ==$bb_post->poster_id || $bb_attachments_cache[$post_id]) && bb_current_user_can($bb_attachments['role_upload']) ) {  
     376        if (($bb_current_user->ID ==$bb_post->poster_id || $bb_attachments_cache[$post_id]) && bb_current_user_can($bb_attachments['role']['upload']) ) {  
    358377                echo " <a href='" . attribute_escape(add_query_arg('bb_attachments',$post_id,remove_query_arg(array('bb_attachments','bbat','bbat_delete')))) . "' >" . __('Attachments') ."</a> "; 
    359378        } 
     
    375394        return bb_find_filename( $file ); 
    376395}        
     396 
     397function bb_attachments_lookup($array,$specific='') { 
     398$key='default'; // there is probably a faster/more dynamic way to do role checks??? 
     399if (isset($array['administrate']) && bb_current_user_can('administrate')) {$key='administrate';} 
     400else {if (isset($array['moderate']) && bb_current_user_can('moderate')) {$key='moderate';} 
     401        else {if ($specific && isset($array[$specific])) {$key=$specific;}}} 
     402if (isset($array[$key])) {return $array[$key];} else {return '';} 
     403} 
     404 
     405function bb_attachments_l2n($v){ 
     406$l = substr($v, -1); $ret = substr($v, 0, -1);  
     407switch(strtoupper($l)){   case 'P':   $ret *= 1024;  case 'T':  $ret *= 1024;  case 'G':  $ret *= 1024;  case 'M':  $ret *= 1024; case 'K':  $ret *= 1024;  break;} 
     408return $ret; 
     409} 
    377410 
    378411if ( ! function_exists ( 'mime_content_type' ) ) {      // most newer PHP doesn't have this, so try shell  ? 
  • bb-attachments/trunk/readme.txt

    r993 r996  
    3434* mime_content_type function or shell access must exist to verify mime types  
    3535* filesize max might be 2mb because of passthrough/readfile limit (supposedly fixed in newer PHP) 
     36* administrators can debug settings (ie. PHP upload limit) by adding to url `?bb_attachments_diagnostic` 
    3637 
    3738== License == 
     
    4647 
    4748* 0.0.5 first public beta release for review 
     49* 0.0.6 advanced restrictions by file type & user role, upload form displays allowed file types 
    4850         
    4951== To Do ==