Changeset 1032

Show
Ignore:
Timestamp:
05/01/08 09:12:58 (7 months ago)
Author:
_ck_
Message:

0.1.0 uploading now possible directly on new posts (instead of only attaching afterwards)

Files:

Legend:

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

    r1030 r1032  
    66Author: _ck_ 
    77Author URI: http://bbShowcase.org 
    8 Version: 0.0.9 
     8Version: 0.1.0 
    99 
    1010License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ 
     
    1515$bb_attachments['role']['see']="read";           // minimum role to see list of attachments = read/participate/moderate/administrate 
    1616$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) 
     17$bb_attachments['role']['upload']="participate";  // minimum role to upload = participate/moderate/administrate (times out with post edit time) 
    1818$bb_attachments['role']['delete']="moderate";  // minimum role to delete = read/participate/moderate/administrate 
    1919 
     
    5656 
    5757add_action( 'bb_init', 'bb_attachments_init'); 
     58add_action( 'bb_post.php', 'bb_attachments_process_post'); 
    5859bb_register_activation_hook( __FILE__,  'bb_attachments_install'); 
    5960 
    6061function bb_attachments_init() { 
    61 global $bbdb, $post_id, $bb_post, $topic, $bb_attachments; 
     62global $bbdb, $bb_attachments; 
    6263 
    6364if (isset($_GET['bb_attachments_diagnostic']) && bb_current_user_can('administrate')) {echo "<pre>"; print_r($bb_attachments); echo "</pre>"; exit();} 
     
    6869        if (isset($_GET['bbat'])) { 
    6970                bb_attachments_download(); 
    70         } else { 
     71        } else {  
    7172                if (bb_attachments_location()!='edit.php') { 
    72                         bb_repermalink();                
    73                         bb_get_header(); 
    74                         bb_attachments(); 
     73                        bb_repermalink();                                       
     74                        bb_get_header();                
     75                        bb_attachments($post_id); 
    7576                        bb_get_footer(); 
    7677                        exit();  
     
    8788} 
    8889add_action('post_edit_form','bb_attachments');  // auto-insert on post edit form 
    89  
    90 
     90add_action('post_form','bb_attachments_upload_form'); // auto-insert on new post form  
     91add_action('pre_post_form','bb_attachments_enctype'); // multipart workaround on new post form 
     92
     93 
     94// insane bbPress workaround - adds multipart enctype to the new post form via uri patch 
     95function bb_attachments_enctype() {add_filter( 'bb_get_option_uri','bb_attachments_uri',999);} 
     96function bb_attachments_uri($uri) {remove_filter( 'bb_get_option_uri','bb_attachments_uri',999);  
     97                                        return $uri. 'bb-post.php"  enctype="multipart/form-data" hack="';}  
    9198 
    9299function bb_attachments($post_id=0) { 
     
    95102 
    96103if ($post_id==0) {if (isset($_GET['bb_attachments'])) {$post_id=intval($_GET['bb_attachments']);} else {global $bb_post; $post_id=$bb_post->post_id;}} 
     104 
    97105if ($post_id) { 
    98106        $bb_post=bb_get_post($post_id); 
     
    102110        echo "<div class='indent'>"; 
    103111        if (isset($_FILES['bb_attachments'])) { 
    104         bb_attachments_process_post(intval($_GET['bb_attachments']));  
     112        bb_attachments_process_post(intval($_GET['bb_attachments']),1);  
    105113        echo "<br />"; 
    106114        }        
     
    114122 
    115123function bb_attachments_post_attachments($post_id=0) { 
    116 global $bbdb, $bb_attachments, $bb_attachments_cache;  
     124global $bbdb, $bb_attachments, $bb_attachments_cache, $bb_current_user;  
    117125 
    118126$output="";      
    119127if ($post_id && ($bb_attachments['role']['see']=="read" || bb_current_user_can($bb_attachments['role']['see']))) { 
    120128         
    121         $can_delete=false; $admin=false; $filter=" AND status = 0 ";    // speedup checks with flag      
     129        $time=time()-60; $can_delete=false; $self=false; $admin=false; $filter=true;   // " AND status = 0 ";   // speedup checks with flag      
     130        if ($bb_current_user->ID==get_post_author_id( $post_id )) {$self=true;} 
    122131        if ((!is_topic() || isset($_GET['bb_attachments'])) && bb_current_user_can('moderate')) {$filter=""; $admin=bb_current_user_can('administrate');}         
    123132        if (bb_current_user_can($bb_attachments['role']['delete'])) {$can_delete=true;} 
    124133                 
    125134        if (!isset($bb_attachments_cache[$post_id])) { 
    126                 $bb_attachments_cache[$post_id]=$bbdb->get_results("SELECT * FROM bb_attachments WHERE post_id = $post_id $filter ORDER BY time DESC LIMIT 999"); 
     135                $bb_attachments_cache[$post_id]=$bbdb->get_results("SELECT * FROM bb_attachments WHERE post_id = $post_id ORDER BY time DESC LIMIT 999"); 
    127136        }                
    128137 
     
    130139        if (count($bb_attachments_cache[$post_id])) { 
    131140                foreach ($bb_attachments_cache[$post_id] as $attachment) {  
    132                         if ($attachment->status==0 || empty($filter)) { 
     141                        $showerror=($self && $attachment->time>$time) ? true : false; 
     142                        if ($attachment->status==0 || empty($filter) || $showerror) { 
    133143                                $attachment->filename=stripslashes($attachment->filename); 
    134144                                $output.="<li>";  
     
    137147                                $output.=" <img align='absmiddle' title='".$attachment->ext."' src='".$bb_attachments['icons']['url'].$icon."' /> "; 
    138148                                 
    139                                 if ($attachment->status>0 && empty($filter)) {                                         
     149                                if ($attachment->status>0 && (empty($filter) || $showerror)) {                                         
    140150                                        $output.=" [".__($bb_attachments['status'][$attachment->status])."] $attachment->filename "; 
    141151                                } 
     
    150160                                }                                                
    151161                                 
    152                                 $output.=" <span class='num'>(".round($attachment->size/1024,1)." KB, ";                               
    153                                 $output.=bb_number_format_i18n($attachment->downloads)." ".__('downloads').")</span> "; 
    154                                  
    155                                 $bb_since=bb_since($attachment->time); 
    156                                 if (intval($bb_since)>0) {$output.=" <small>".$bb_since." ".__('old')."</small> ";} 
     162                                $output.=" <span class='num'>(".round($attachment->size/1024,1)." KB";                                 
     163                                if ($attachment->status<2) {$output.=", ".bb_number_format_i18n($attachment->downloads)." ".__('downloads');} 
     164                                $output.=")</span> "; 
     165                                 
     166                                if ($attachment->time<$time) {$output.=" <small>".bb_since($attachment->time)." ".__('old')."</small> ";} 
    157167                                                                 
    158168                                if ($admin) {                            
     
    173183} 
    174184 
    175 function bb_attachments_process_post() { 
     185function bb_attachments_process_post($post_id=0,$display=0) { 
    176186global $bbdb, $bb_attachments; 
    177187 
    178 $post_id=intval($_GET['bb_attachments']);     // only can upload if user is allowed to edit post 
     188if (!$post_id) {$post_id=intval($_GET['bb_attachments']);}    // only can upload if user is allowed to edit post 
    179189$user_id=bb_get_current_user_info( 'id' ); 
    180190if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;}   
     
    190200$maxlength=bb_attachments_lookup($bb_attachments['max']['filename']); 
    191201 
    192 echo "<h3>".__("Uploads")."</h3><ol>";        // start output 
     202$output="<h3>".__("Uploads")."</h3><ol>";     // start output 
    193203while(list($key,$value) = each($_FILES['bb_attachments']['name'])) { 
    194204        if(!empty($value)){      
     
    222232                                 
    223233                                if ($status==0 && !$failed) {$id=intval($bbdb->get_var("SELECT LAST_INSERT_ID()"));}    // fetch the assigned unique id # 
    224                                  
    225                                 // echo "status: $status - ".$_FILES['bb_attachments']['tmp_name'][$key]." - ".$file; 
    226234                                 
    227235                                if ($failed) {$status=2;}               // db failure ? 
     
    265273                if ($status>0) { 
    266274                        if ($id>0) {$bbdb->query("UPDATE  bb_attachments SET `status` = $status WHERE `id` = $id");} 
    267                         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>"; 
    268                 } else {echo "<li><span style='color:green'><strong>$filename "." <span class='num'>(".round($size/1024,1)." KB)</span> ".__('successful')."</strong></span></li>";} 
     275                        $output.="<li><span style='color:red'><strong>$filename "." <span class='num'>(".round($size/1024,1)." KB)</span> ".__('error:')." ".$bb_attachments['status'][$status]."</strong></span></li>"; 
     276                } else {$output.="<li><span style='color:green'><strong>$filename "." <span class='num'>(".round($size/1024,1)." KB)</span> ".__('successful')."</strong></span></li>";} 
    269277        } // end !$empty 
    270278} // end while 
    271 echo "</ol>"; 
     279$output.="</ol>"; 
     280if ($display) {echo $output;} 
    272281bb_update_topicmeta( $topic_id, 'bb_attachments', $topic_attachments+$offset); 
    273282} 
     
    278287if (!$post_id) {$post_id=intval($_GET['bb_attachments']);}      // only can upload if user is allowed to edit post 
    279288$user_id=bb_get_current_user_info( 'id' ); 
    280 if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;}         
     289if (!$user_id || ($post_id && !bb_current_user_can('edit_post',$post_id)) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;}        
    281290 
    282291$count=0; $allowed=__('allowed uploads:')." "; $exts=bb_attachments_lookup($bb_attachments['allowed']['extensions']); 
     
    287296$allowed=rtrim($allowed," ,"); 
    288297 
    289 echo '<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')))).'">       
    290         <h3>'.__("Upload Files from your Computer").'</h3>               
    291         <input  type="hidden" name="MAX_FILE_SIZE" value="'.$bb_attachments['max']['php_upload_limit'].'" />     
     298if ($post_id) {echo '<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')))).'">';} 
     299else {echo '<input  type="hidden" name="bb_attachments" value="0" />';} 
     300echo    '<h3>'.__("Upload Files from your Computer").'</h3>              
     301        <input  type="hidden" name="MAX_FILE_SIZE" value="'.$bb_attachments['max']['php_upload_limit'].'" />                     
    292302        <span id="bb_attachments_file_sample"> 
    293303        <input type="file" name="bb_attachments[]" size="50" /><br /> 
     
    304314        </script> 
    305315        '.$allowed.'<br /> 
    306         <div style="margin:1em 0 0 0;">  
    307          <a style="margin-right:12em;" href="'. get_post_link( $post_id ).'">'.__("&laquo; return to post").'</a> 
    308         <a href="javascript:void(0)" onClick="bb_attachment_inputs();">[+] '.__('more').'</a> &nbsp;  
     316        <div style="margin:1em 0 0 0;">';                 
     317if ($post_id) {echo '<a style="margin-right:12em;" href="'. get_post_link( $post_id ).'">'.__("&laquo; return to post").'</a>';} 
     318else {echo '<span style="margin-right:20em;">&nbsp;</span>';} 
     319echo    '<a href="javascript:void(0)" onClick="bb_attachment_inputs();">[+] '.__('more').'</a> &nbsp;  
    309320        <input style="font-weight:bold;" type="submit" class="submit" name="upload" value="'.__('Upload').'" /> 
    310         </div> 
    311         </form>'; 
     321        </div>'; 
     322if ($post_id) {echo '</form>';} 
    312323} 
    313324 
     
    380391global $bb_attachments, $bb_attachments_cache, $bb_post, $bb_current_user; 
    381392$post_id=$bb_post->post_id; 
    382         if (($bb_current_user->ID ==$bb_post->poster_id || $bb_attachments_cache[$post_id]) && bb_current_user_can($bb_attachments['role']['upload']) ) {  
     393        if (($bb_current_user->ID ==$bb_post->poster_id && $bb_attachments_cache[$post_id]) && bb_current_user_can($bb_attachments['role']['upload']) ) {  
    383394                echo " <a href='" . attribute_escape(add_query_arg('bb_attachments',$post_id,remove_query_arg(array('bb_attachments','bbat','bbat_delete')))) . "' >" . __('Attachments') ."</a> "; 
    384395        } 
  • bb-attachments/trunk/readme.txt

    r1031 r1032  
    2323* install plugin in it's own bb-attachments directory in `my-plugins` then activate plugin  
    2424* there are some optional settings you can adjust in `bb-attachments.php` 
    25 * default upload role setting is set to `moderate` in beta for security reasons, you can reduce to `participate` to allow members to test 
    2625 
    2726== Frequently Asked Questions == 
     
    5049* 0.0.7 one more mime option for windows/no-shell-access users 
    5150* 0.0.9 no longer necessary to edit `edit-post.php` template if using bbPress 0.9.0.2 or newer 
     51* 0.1.0 uploading now possible directly on new posts (instead of only attaching afterwards) 
    5252         
    5353== To Do == 
     
    5858* serving images inline rather than just downloading 
    5959* pre-validate upload filenames via javascript to spare user upload time with rejection 
    60 * deal with attachments on new, unsaved posts - tricky but possible - will take time 
    6160* admin menu