Changeset 395
- Timestamp:
- 05/02/07 14:44:53 (2 years ago)
- Files:
-
- avatar-upload/trunk/avatar-upload.php (modified) (10 diffs)
- avatar-upload/trunk/my-plugins/bb-avatar-upload.php (modified) (1 diff)
- avatar-upload/trunk/my-templates/avatar.php (modified) (2 diffs)
- avatar-upload/trunk/readme.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
avatar-upload/trunk/avatar-upload.php
r388 r395 2 2 /* 3 3 Plugin Name: Avatar Upload 4 Plugin URI: http:// www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html5 Version: 0. 26 Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. Admins can configure maximum allowed file size and image dimensions.4 Plugin URI: http://bbpress.org/plugins/topic/46 5 Version: 0.3 6 Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 7 7 Author: Louise Dade 8 8 Author URI: http://www.classical-webdesigns.co.uk/ … … 11 11 require_once('./bb-load.php'); // load bbPress config 12 12 bb_auth(); // logged in? 13 14 /* --- CONFIGURATION VARIABLES (you can edit these) --- */15 16 // Avatar folder location (default is 'avatars' in the bbPress root, 'BBPATH')17 // You must create the 'avatars' folder before you install this plugin.18 $avatar_dir = BBPATH . "avatars/"; // remember to include trailing slash19 20 // Define maximum values allowed21 $max_width = 150; // pixels22 $max_height = 150; // pixels23 $max_bytes = 51200; // default is 50 KB (51200 bytes)24 25 // Allowed file extensions (*.gif, *.jpeg, *.jpg, *.png)26 $allowed_extns = array("gif", "jpg", "jpeg", "png");27 28 // Content types -- more stringent check on file types.29 // Eg. a file with a '.png' extension MUST have an 'image/png' content-type!30 // You probably want to leave this alone.31 $allowed_types = array('gif'=>"image/gif", 'jpeg'=>"image/jpeg", 'jpg'=>"image/jpeg", 'png'=>"image/png");32 33 /* --- STOP EDITING --- */34 35 /* --- Start bbPress Stuff --- */36 37 // If the script filename is not correct.38 $file = '';39 foreach ( array($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME']) as $name ) {40 if ( false !== strpos($name, '.php') ) {$file = $name;}41 }42 if (bb_find_filename($file) != 'avatar-upload.php') { // not the correct form!43 $sendto = bb_get_option('uri');44 wp_redirect( $sendto );45 }46 13 47 14 // Grab user id … … 52 19 } 53 20 21 // This user may NOT be the user who's avatar is being uploaded, 22 // this allows an Admin/Moderator to update another user's avatar 23 // (in the event that the user's avatar is objectionable!) 24 $current_user = bb_get_user(bb_get_current_user_info('id')); 25 26 // User who's profile is being updated 54 27 $user = bb_get_user( $user_id ); // user info 55 28 29 // No user found with that ID 56 30 if ( !$user ) { 57 31 bb_die(__('User not found.')); // no user found 58 32 } 59 33 60 // Do not let users upload an avatar if user is a bozo OR user id not equal to current user's id61 // (AND user is not allowed to moderate).62 if ( $user->is_bozo || ($user->ID != bb_get_current_user_info( 'id' ) && !bb_current_user_can( 'moderate' )))34 // Only allow the correct User or an Admin/Moderator to upload 35 // but not if they are a bozo! 36 if ( ($user->ID != $current_user->ID && !bb_current_user_can( 'moderate' )) || $current_user->is_bozo ) 63 37 { 64 38 bb_die(__('You do not have permission to upload an avatar for this user.')); 65 39 } 66 /* --- End bbPress Stuff --- */67 40 68 69 $max_kbytes = round($max_bytes/1024, 2); // Just a pretty value for output use 41 // Get config variables 42 $av_opts = avatarupload_config(); 43 $av_opts['mime_types']['jpeg'] = $av_opts['mime_types']['jpg']; 44 $av_opts['max_kbytes'] = round($av_opts['max_bytes']/1024, 2); // Just a pretty value for output use 70 45 71 46 // Some potential error messages in human readable form 72 47 $errorcodes = array( 73 48 "- no error (this message will never be shown) -", 74 "The image file is too big, the maximum file size allowed is $max_kbytesKB.",75 "The image file is too big, the maximum file size allowed is $max_kbytesKB.",49 "The image file is too big, the maximum file size allowed is {$av_opts['max_kbytes']} KB.", 50 "The image file is too big, the maximum file size allowed is {$av_opts['max_kbytes']} KB.", 76 51 "The file was only partially uploaded - the connection may have been interrupted.", 77 52 "The image file does not appear to have been uploaded - did you select an image?", 78 53 "The file does not appear to be a valid GIF, JPG/JPEG or PNG image type.", 79 54 "The image file could not be saved to the avatars folder.", 80 "Image dimensions must not be greater than $max_width x $max_heightpixels.",55 "Image dimensions must not be greater than {$av_opts['max_width']} x {$av_opts['max_height']} pixels.", 81 56 "The avatar filename may only contain upper/lower case letters, numbers, underscores or dashes." 82 57 ); … … 86 61 if (!empty($_FILES['p_browse'])) 87 62 { 88 $current_avatar = explode("|", $user->avatar_file); // for comparison later63 $current_avatar = avatarupload_get_avatar($user_id, 0, 1); // for comparison later 89 64 90 65 $img_errs = 0; … … 116 91 } 117 92 118 if ($error == 0 && ($img_errs == 1 || $img_errs == 2) || $img_size > $ max_bytes) {93 if ($error == 0 && ($img_errs == 1 || $img_errs == 2) || $img_size > $av_opts['max_bytes']) { 119 94 // File size exceeds max_bytes 120 95 $img_errs = 1; … … 122 97 } 123 98 124 if ($error == 0 && (!in_array($img_type, $allowed_types) || !in_array($img_ext, $allowed_extns)) ) { 125 // file extension and file type check #1 - simple check to weed out bad filenames 126 $img_errs = 5; 127 $error++; 128 } 129 130 if ($error == 0 && $allowed_types[$img_ext] != $img_type) { 131 // file extension and file type check #2 - ok, they are in the list, but to do match? 99 if ($error == 0 && (!in_array($img_type, $av_opts['mime_types'][$img_ext]) || 100 !in_array($img_ext, $av_opts['file_extns'])) ) { 101 // Check for invalid and/or mismatched mime-type and file extensions 132 102 $img_errs = 5; 133 103 $error++; … … 151 121 $img_h = $dims[1]; 152 122 153 if ($img_w > $max_width || $img_h > $max_height) { // File dims greater than max_width/max_height 123 if ($img_w > $av_opts['max_width'] || $img_h > $av_opts['max_height']) { 124 // File dims greater than max_width/max_height 154 125 $img_errs = 7; 155 126 $error++; … … 157 128 } 158 129 159 if ($error == 0 && !move_uploaded_file($img_temp, $avatar_dir. $user_filename))130 if ($error == 0 && !move_uploaded_file($img_temp, BBPATH . $av_opts['avatar_dir'] . $user_filename)) 160 131 { // Can save to avatars folder (does it exist?) 161 132 $img_errs = 6; … … 169 140 { // compare 'new' and 'current' avatar filenames - if different, delete 'current' 170 141 // this will most likely only happen when the new avatar has a different extension 171 unlink(BBPATH . "avatars/". $current_avatar[0]);142 unlink(BBPATH . $av_opts['avatar_dir'] . $current_avatar[0]); 172 143 } 173 144 174 $meta_avatar = $user_filename . "|" . $img_w . "|" . $img_h ;145 $meta_avatar = $user_filename . "|" . $img_w . "|" . $img_h . "|avatar-upload"; 175 146 bb_update_usermeta( $user_id, 'avatar_file', $meta_avatar ); 176 147 $success_message = "Your avatar has been uploaded."; … … 178 149 } 179 150 180 // this array will be used in the template 181 $img_requirements = array( 182 'img_types' => $allowed_extns, 183 'max_width' => $max_width, 184 'max_height' => $max_height, 185 'max_bytes' => $max_bytes, 186 'max_kbytes' => $max_kbytes, 187 'img_ct' => $allowed_types 188 ); 189 190 bb_load_template( 'avatar.php', array('success_message', 'img_requirements') ); 151 bb_load_template( 'avatar.php', array('success_message', 'av_opts') ); 191 152 ?> avatar-upload/trunk/my-plugins/bb-avatar-upload.php
r388 r395 2 2 /* 3 3 Plugin Name: Avatar Upload 4 Plugin URI: http:// www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html5 Version: 0. 26 Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. Admins can configure maximum allowed file size and image dimensions.4 Plugin URI: http://bbpress.org/plugins/topic/46 5 Version: 0.3 6 Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 7 7 Author: Louise Dade 8 8 Author URI: http://www.classical-webdesigns.co.uk/ 9 9 */ 10 10 11 // Configuration Settings 12 function avatarupload_config() 13 { 14 return array( 11 15 12 function display_avatar_profile($avatar) 16 // Avatar folder location (default is 'avatars' in the bbPress root folder) 17 // You must create the folder before you install this plugin. 18 'avatar_dir' => "avatars/", // remember to include trailing slash 19 20 // Define maximum values allowed 21 'max_width' => 150, // (pixels) 22 'max_height' => 150, // (pixels) 23 'max_bytes' => 51200, // filesize (bytes; 1024 bytes = 1 KB) 24 25 // Default avatar - set 'use_default' to '0' to display no image instead of default 26 'default_avatar' => array( 27 'use_default' => 1, 28 'uri' => bb_get_option('uri').'avatars/default.png', // full uri of image 29 'width' => 80, 30 'height' => 80, 31 'alt' => "User has not uploaded an avatar" 32 ), 33 34 // Allowed file extensions 35 'file_extns' => array("gif", "jpg", "jpeg", "png"), 36 37 // Mime-Types (list thanks to SamBauers) - you probably want to leave this alone. 38 'mime_types' => array( 39 'gif' => array( 40 'image/gif', 41 'image/gi_' 42 ), 43 'jpg' => array( 44 'image/jpeg', 45 'image/jpg', 46 'image/jp_', 47 'image/pjpeg', 48 'image/pjpg', 49 'image/pipeg', 50 'application/jpg', 51 'application/x-jpg' 52 ), 53 'png' => array( 54 'image/png', 55 'image/x-png', 56 'application/png', 57 'application/x-png' 58 ) 59 ) 60 ); 61 } 62 63 // Display the avatar image 64 function avatarupload_display($id, $status='') 13 65 { 14 if ($a = explode("|", $avatar))66 if ($a = avatarupload_get_avatar($id)) 15 67 { 16 echo '<img src="'. bb_get_option('uri').'avatars/'.$a[0];68 echo '<img src="'.$a[0]; 17 69 echo ($status == 'new') ? '?'.time() : ''; 18 70 echo'" width="'.$a[1].'" height="'.$a[2].'" alt="Avatar" />'; 71 } else { 72 $config = avatarupload_config(); 73 $default = $config['default_avatar']; 74 if ($default['use_default'] == 1) 75 { 76 echo '<img src="'.$default['uri'].'" width="'.$default['width'].'" height="'.$default['height'] 77 .'" alt="'.$d['alt'].'" />'; 78 } 19 79 } 20 80 } 21 81 22 function display_avatar($id, $status='') 82 // Get the avatar URI 83 function avatarupload_get_avatar($id, $fulluri=1, $force_db=0) 23 84 { 24 if ($a = get_avatar($id)) 85 global $bbdb, $user; 86 87 if ($id == $user->ID && $force_db == 0) 25 88 { 26 echo '<img src="'.bb_get_option('uri').'avatars/'.$a[0]; 27 echo ($status == 'new') ? '?'.time() : ''; 28 echo'" width="'.$a[1].'" height="'.$a[2].'" alt="Avatar" />'; 89 if (!empty($user->avatar_file)) { 90 $a = explode("|", $user->avatar_file); 91 } else { 92 return false; 93 } 29 94 } 95 else 96 { 97 $bb_query = "SELECT meta_value FROM $bbdb->usermeta WHERE meta_key='avatar_file' AND user_id='$id' LIMIT 1"; 98 99 if ( $avatar = $bbdb->get_results($bb_query) ) { 100 $a = explode("|", $avatar[0]->meta_value); 101 } else { 102 return false; 103 } 104 } 105 106 // do we want the full uri? 107 if ($fulluri == 1) 108 { 109 $config = avatarupload_config(); 110 $a[0] = bb_get_option('uri') . $config['avatar_dir'] . $a[0]; 111 } 112 return $a; 30 113 } 31 114 32 function get_avatar($id) 115 // Add an "Upload Avatar" tab to the Profile menu 116 function add_avatar_tab() 33 117 { 34 global $bbdb; 35 36 $bb_query = "SELECT meta_value FROM $bbdb->usermeta WHERE meta_key='avatar_file' AND user_id='$id' LIMIT 1"; 37 38 if ( $avatar = $bbdb->get_results($bb_query) ) { 39 return explode("|", $avatar[0]->meta_value); 40 } else { 41 return false; 42 } 118 add_profile_tab(__('Avatar'), 'edit_profile', 'moderate', 'avatar-upload.php'); 43 119 } 120 add_action( 'bb_profile_menu', 'add_avatar_tab' ); 44 121 45 122 ?> avatar-upload/trunk/my-templates/avatar.php
r388 r395 1 1 <?php bb_get_header(); ?> 2 2 3 <h3 id="breadcrumb"><a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a> » <?php _e('Upload Avatar'); ?></h3>3 <h3 class="bbcrumb"><a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a> » <?php _e('Upload Avatar'); ?></h3> 4 4 5 5 <?php if (bb_current_user_can('edit_user', $user->ID)) { ?> 6 6 7 7 <?php 8 echo (!empty($success_message)) ? $success_message: "";8 echo (!empty($success_message)) ? '<div class="notice">'.__($success_message).'</div>' : ""; 9 9 ?> 10 10 … … 12 12 13 13 <ul> 14 <li> The following image formats are allowed: <strong><?php echo implode($img_requirements['img_types'], ", "); ?></strong>.</li>15 <li> Dimensions must be no greater than <strong><?php echo $img_requirements['max_width']; ?> x <?php echo $img_requirements['max_height']; ?> pixels</strong> (your image does not have to be square).</li>16 <li> File size must be no greater than <strong><?php echo $img_requirements['max_kbytes']; ?> <abbr title="kilobytes">KB</abbr></strong></li>17 <li> File names must be <strong>alpha-numeric</strong> and may contain <strong>underscores or dashes</strong> (a-z/A-Z, 0-9, _ or -).</li>14 <li><?php _e('The following image formats are allowed: <strong>' . implode($av_opts['file_extns'], ", ") . '</strong>.'); ?></li> 15 <li><?php _e('Dimensions must be no greater than <strong>' .$av_opts['max_width']. ' x ' .$av_opts['max_height']. ' pixels</strong> (your image does not have to be square).'); ?></li> 16 <li><?php _e('File size must be no greater than <strong>' . $av_opts['max_kbytes'] . '<abbr title="kilobytes">KB</abbr></strong>'); ?></li> 17 <li><?php _e('File names must be <strong>alpha-numeric</strong> and may contain <strong>underscores or dashes</strong> (a-z/A-Z, 0-9, _ or -).'); ?></li> 18 18 </ul> 19 19 20 <form enctype="multipart/form-data" method="POST" action="<?php echo bb_get_option('uri') . 'avatar-upload.php?id=' . $user->ID; ?>">21 <p><label for="p_browse"> Locate Image:</label><br />22 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $ img_requirements['max_bytes']; ?>" />20 <form enctype="multipart/form-data" method="POST" action="<?php profile_tab_link($user->ID, 'avatar'); ?>"> 21 <p><label for="p_browse"><?php _e('Locate Image'); ?>:</label><br /> 22 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $av_opts['max_bytes']; ?>" /> 23 23 <input type="file" name="p_browse" id="p_browse" size="80" /></p> 24 24 25 <p><input type="submit" name="submit" id="submit" value=" Upload Avatar" /></p>25 <p><input type="submit" name="submit" id="submit" value="<?php _e('Upload Avatar'); ?>" /></p> 26 26 </form> 27 27 28 <h3> Your Current Avatar</h3>29 <p><?php echo display_avatar($user->ID, 'new'); ?></p>28 <h3><?php _e('Your Current Avatar'); ?></h3> 29 <p><?php echo avatarupload_display($user->ID, 'new'); ?></p> 30 30 31 31 <?php } ?> avatar-upload/trunk/readme.txt
r388 r395 4 4 Requires at least: 0.8 5 5 Tested up to: 0.8.1 6 Stable Tag: 0. 26 Stable Tag: 0.3 7 7 8 8 Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. Admins can configure maximum allowed file size and image dimensions. … … 10 10 == Description == 11 11 12 Plugin URI: http://www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html 12 Plugin URI: http://bbpress.org/plugins/topic/46 13 Author: Louise Dade 14 Author URI: http://www.classical-webdesigns.co.uk/ 13 15 14 16 Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress, and provides template functions to display the uploaded image. … … 17 19 * Bozos can not upload avatars. 18 20 * Admins can configure maximum allowed file size (bytes) and dimensions (pixels) of images. 19 - Current done from within the script (no Admin page interface at this time).21 - Currently done from within the script (no Admin page interface at this time). 20 22 * Anybody with the 'moderate' capability can upload another user's avatar 21 23 - this to ensures that inappropriate images can be removed. 22 - there is no "delete avatar" function at this time, but an inappropriate image can be removed by uploading a 'safe' image (e.g. a blank 1x1 pixel image) to replace it (you could them manually set that user as a bozo to stop them re-uploading inappropriate images. 24 - there is no "delete avatar" function at this time, but an inappropriate image can be removed by uploading a 'safe' image (e.g. a blank 1x1 pixel image) to replace it (you could them manually set that user as a bozo to stop them re-uploading inappropriate images). 25 * Option to display a default avatar for users who do not upload their own. 26 * Can be extended with fel64's "Identicons" plugin to give users the option of display an identicon instead of uploading an image (becomes their 'default' avatar). http://bbpress.org/forums/topic/1027?replies=25#post-6759 23 27 24 28 == Installation == 25 29 26 1. Create a folder to store your avatars. A folder called "avatars" in the root directory of your bbPress installation is probably best (and the default).30 UPGRADING? If you are using an older version of this plugin, you need to follow these installation instructions because the template functions are incompatible with the older version. 27 31 28 2. Open up the 'avatar-upload.php' file and configure the "configuration Variables" (if desired). At least make sure the '$avatar_dir' pathis correct. Other configurable variables include the maximum allowed width and height of uploaded images and the maximum file size (in bytes).32 1. Open up the 'my-plugins/bb-avatar-upload.php' file and configure the "configuration Setting" (if desired). At least make sure the '$avatar_dir' variable is correct. Other configurable variables include the maximum allowed width and height of uploaded images and the maximum file size (in bytes). 29 33 30 3. Open up your 'profile-edit.php' template andinsert the following "Upload Avatar" link wherever you wish:34 2. The avatar upload page should appear as a tab ("Avatar") on the Profile menu on the user's profile pages. If you like the link elsewhere, then insert the following "Upload Avatar" link wherever you wish: 31 35 32 <a href="<?php echo bb_get_option('uri').'avatar-upload.php?id='.$user->ID; ?>">Upload Avatar</a>36 <a href="<?php profile_tab_link($user->ID, 'avatar'); ?>"><?php _e("Upload Avatar"); ?></a> 33 37 34 4. To display an uploaded avatar, just insert the following template functions. 38 Use the available $user->ID for the page you place the link on. 39 40 3. To display an uploaded avatar, just insert the following template function. 35 41 36 42 a) On the user's profile page ('profile.php' template). 37 43 38 <?php display_avatar_profile($user->avatar_file); ?>44 <?php avatarupload_display($user->ID); ?> 39 45 40 46 This grabs the avatar info file directly from the current user's profile information. … … 42 48 b) On each user's forum posts ('post.php' template) 43 49 44 <?php display_avatar(get_post_author_id()); ?>50 <?php avatarupload_display(get_post_author_id()); ?> 45 51 46 This function queries the database for any user's ID. In this particular case the post author's id.52 You can include the avatar anywhere else you like, just be sure to have either the current or any user's ID available. 47 53 48 5. This is optional, but you can open up 'my-templates/avatar.php' and edit the template if you wish, but be sure not to mess with the upload form. 54 c) If you just want the URI of the avatar (for your own plugins for example): 49 55 50 6. Upload the plugin scripts to the following locations. 56 <?php avatarupload_get_avatar(ID); ?> 51 57 52 'avatar-upload.php' - into your bbPress root folder. 53 'my-templates/avatar.php' - into your 'my-templates/my-template-name/' folder (or in the kakumei folder) 54 'my-plugins/bb-avatar-upload.php' - into your 'my-plugins/' folder (and activated). 58 Where ID is a user ID. Returns false if no avatar exists for that user. 59 60 4. This is optional, but you can open up 'my-templates/avatar.php' and edit the template if you wish, but be sure not to mess with the upload form. 61 62 5. Upload the plugin files to the following locations. 63 64 'avatars/default.png' - both 'avatars/' dir and image into the bbPress root dir. 65 'avatar-upload.php' - bbPress root dir. 66 'my-templates/avatar.php' - your 'my-templates/my-template-name/' (or kakumei) dir. 67 'my-plugins/bb-avatar-upload.php' - your 'my-plugins/' dir (and activated). 55 68 56 69 That's it, the 'Avatar Upload' plugin should now be working. … … 70 83 == Change Log == 71 84 72 2007-04-17 Ver. 0.2 reduced DB calls, added filename checks (to stop things like "myavatar.exe.jpg") 73 2007-04-07 Ver. 0.1 released. 85 2007-05-02 Ver. 0.3 rewritten, config vars moved to plugin script, enabled default avatar, 86 added profile tab and made it possible to use plugin with other plugins. 87 2007-04-17 Ver. 0.2 reduced DB calls, added filename checks (to stop things like 88 "myavatar.exe.jpg"). 89 2007-04-07 Ver. 0.1 released.
