Changeset 1032
- Timestamp:
- 05/01/08 09:12:58 (7 months ago)
- Files:
-
- bb-attachments/trunk/bb-attachments.php (modified) (19 diffs)
- bb-attachments/trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
bb-attachments/trunk/bb-attachments.php
r1030 r1032 6 6 Author: _ck_ 7 7 Author URI: http://bbShowcase.org 8 Version: 0. 0.98 Version: 0.1.0 9 9 10 10 License: CC-GNU-GPL http://creativecommons.org/licenses/GPL/2.0/ … … 15 15 $bb_attachments['role']['see']="read"; // minimum role to see list of attachments = read/participate/moderate/administrate 16 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)17 $bb_attachments['role']['upload']="participate"; // minimum role to upload = participate/moderate/administrate (times out with post edit time) 18 18 $bb_attachments['role']['delete']="moderate"; // minimum role to delete = read/participate/moderate/administrate 19 19 … … 56 56 57 57 add_action( 'bb_init', 'bb_attachments_init'); 58 add_action( 'bb_post.php', 'bb_attachments_process_post'); 58 59 bb_register_activation_hook( __FILE__, 'bb_attachments_install'); 59 60 60 61 function bb_attachments_init() { 61 global $bbdb, $ post_id, $bb_post, $topic, $bb_attachments;62 global $bbdb, $bb_attachments; 62 63 63 64 if (isset($_GET['bb_attachments_diagnostic']) && bb_current_user_can('administrate')) {echo "<pre>"; print_r($bb_attachments); echo "</pre>"; exit();} … … 68 69 if (isset($_GET['bbat'])) { 69 70 bb_attachments_download(); 70 } else { 71 } else { 71 72 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); 75 76 bb_get_footer(); 76 77 exit(); … … 87 88 } 88 89 add_action('post_edit_form','bb_attachments'); // auto-insert on post edit form 89 90 } 90 add_action('post_form','bb_attachments_upload_form'); // auto-insert on new post form 91 add_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 95 function bb_attachments_enctype() {add_filter( 'bb_get_option_uri','bb_attachments_uri',999);} 96 function 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="';} 91 98 92 99 function bb_attachments($post_id=0) { … … 95 102 96 103 if ($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 97 105 if ($post_id) { 98 106 $bb_post=bb_get_post($post_id); … … 102 110 echo "<div class='indent'>"; 103 111 if (isset($_FILES['bb_attachments'])) { 104 bb_attachments_process_post(intval($_GET['bb_attachments']) );112 bb_attachments_process_post(intval($_GET['bb_attachments']),1); 105 113 echo "<br />"; 106 114 } … … 114 122 115 123 function bb_attachments_post_attachments($post_id=0) { 116 global $bbdb, $bb_attachments, $bb_attachments_cache ;124 global $bbdb, $bb_attachments, $bb_attachments_cache, $bb_current_user; 117 125 118 126 $output=""; 119 127 if ($post_id && ($bb_attachments['role']['see']=="read" || bb_current_user_can($bb_attachments['role']['see']))) { 120 128 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;} 122 131 if ((!is_topic() || isset($_GET['bb_attachments'])) && bb_current_user_can('moderate')) {$filter=""; $admin=bb_current_user_can('administrate');} 123 132 if (bb_current_user_can($bb_attachments['role']['delete'])) {$can_delete=true;} 124 133 125 134 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 $filterORDER 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"); 127 136 } 128 137 … … 130 139 if (count($bb_attachments_cache[$post_id])) { 131 140 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) { 133 143 $attachment->filename=stripslashes($attachment->filename); 134 144 $output.="<li>"; … … 137 147 $output.=" <img align='absmiddle' title='".$attachment->ext."' src='".$bb_attachments['icons']['url'].$icon."' /> "; 138 148 139 if ($attachment->status>0 && empty($filter)) {149 if ($attachment->status>0 && (empty($filter) || $showerror)) { 140 150 $output.=" [".__($bb_attachments['status'][$attachment->status])."] $attachment->filename "; 141 151 } … … 150 160 } 151 161 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> ";} 157 167 158 168 if ($admin) { … … 173 183 } 174 184 175 function bb_attachments_process_post( ) {185 function bb_attachments_process_post($post_id=0,$display=0) { 176 186 global $bbdb, $bb_attachments; 177 187 178 $post_id=intval($_GET['bb_attachments']);// only can upload if user is allowed to edit post188 if (!$post_id) {$post_id=intval($_GET['bb_attachments']);} // only can upload if user is allowed to edit post 179 189 $user_id=bb_get_current_user_info( 'id' ); 180 190 if (!$user_id || !$post_id || !bb_current_user_can('edit_post',$post_id) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;} … … 190 200 $maxlength=bb_attachments_lookup($bb_attachments['max']['filename']); 191 201 192 echo"<h3>".__("Uploads")."</h3><ol>"; // start output202 $output="<h3>".__("Uploads")."</h3><ol>"; // start output 193 203 while(list($key,$value) = each($_FILES['bb_attachments']['name'])) { 194 204 if(!empty($value)){ … … 222 232 223 233 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;226 234 227 235 if ($failed) {$status=2;} // db failure ? … … 265 273 if ($status>0) { 266 274 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>";} 269 277 } // end !$empty 270 278 } // end while 271 echo "</ol>"; 279 $output.="</ol>"; 280 if ($display) {echo $output;} 272 281 bb_update_topicmeta( $topic_id, 'bb_attachments', $topic_attachments+$offset); 273 282 } … … 278 287 if (!$post_id) {$post_id=intval($_GET['bb_attachments']);} // only can upload if user is allowed to edit post 279 288 $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;}289 if (!$user_id || ($post_id && !bb_current_user_can('edit_post',$post_id)) || !bb_current_user_can($bb_attachments['role']['upload'])) {return;} 281 290 282 291 $count=0; $allowed=__('allowed uploads:')." "; $exts=bb_attachments_lookup($bb_attachments['allowed']['extensions']); … … 287 296 $allowed=rtrim($allowed," ,"); 288 297 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'].'" /> 298 if ($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')))).'">';} 299 else {echo '<input type="hidden" name="bb_attachments" value="0" />';} 300 echo '<h3>'.__("Upload Files from your Computer").'</h3> 301 <input type="hidden" name="MAX_FILE_SIZE" value="'.$bb_attachments['max']['php_upload_limit'].'" /> 292 302 <span id="bb_attachments_file_sample"> 293 303 <input type="file" name="bb_attachments[]" size="50" /><br /> … … 304 314 </script> 305 315 '.$allowed.'<br /> 306 <div style="margin:1em 0 0 0;"> 307 <a style="margin-right:12em;" href="'. get_post_link( $post_id ).'">'.__("« return to post").'</a> 308 <a href="javascript:void(0)" onClick="bb_attachment_inputs();">[+] '.__('more').'</a> 316 <div style="margin:1em 0 0 0;">'; 317 if ($post_id) {echo '<a style="margin-right:12em;" href="'. get_post_link( $post_id ).'">'.__("« return to post").'</a>';} 318 else {echo '<span style="margin-right:20em;"> </span>';} 319 echo '<a href="javascript:void(0)" onClick="bb_attachment_inputs();">[+] '.__('more').'</a> 309 320 <input style="font-weight:bold;" type="submit" class="submit" name="upload" value="'.__('Upload').'" /> 310 </div> 311 </form>'; 321 </div>'; 322 if ($post_id) {echo '</form>';} 312 323 } 313 324 … … 380 391 global $bb_attachments, $bb_attachments_cache, $bb_post, $bb_current_user; 381 392 $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']) ) { 383 394 echo " <a href='" . attribute_escape(add_query_arg('bb_attachments',$post_id,remove_query_arg(array('bb_attachments','bbat','bbat_delete')))) . "' >" . __('Attachments') ."</a> "; 384 395 } bb-attachments/trunk/readme.txt
r1031 r1032 23 23 * install plugin in it's own bb-attachments directory in `my-plugins` then activate plugin 24 24 * 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 test26 25 27 26 == Frequently Asked Questions == … … 50 49 * 0.0.7 one more mime option for windows/no-shell-access users 51 50 * 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) 52 52 53 53 == To Do == … … 58 58 * serving images inline rather than just downloading 59 59 * 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 time61 60 * admin menu
