Changeset 395

Show
Ignore:
Timestamp:
05/02/07 14:44:53 (2 years ago)
Author:
louisedade
Message:

avatar-upload: committing version 0.3 with lots of changes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • avatar-upload/trunk/avatar-upload.php

    r388 r395  
    22/* 
    33Plugin Name: Avatar Upload 
    4 Plugin URI: http://www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html 
    5 Version: 0.2 
    6 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. 
     4Plugin URI: http://bbpress.org/plugins/topic/46 
     5Version: 0.3 
     6Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 
    77Author: Louise Dade 
    88Author URI: http://www.classical-webdesigns.co.uk/ 
     
    1111require_once('./bb-load.php'); // load bbPress config  
    1212bb_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 slash 
    19  
    20 // Define maximum values allowed 
    21 $max_width = 150; // pixels 
    22 $max_height = 150; // pixels 
    23 $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 } 
    4613 
    4714// Grab user id 
     
    5219} 
    5320 
     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 
    5427$user = bb_get_user( $user_id ); // user info 
    5528 
     29// No user found with that ID 
    5630if ( !$user ) { 
    5731        bb_die(__('User not found.')); // no user found 
    5832} 
    5933 
    60 // Do not let users upload an avatar if user is a bozo OR user id not equal to current user's i
    61 // (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 uploa
     35// but not if they are a bozo! 
     36if ( ($user->ID != $current_user->ID && !bb_current_user_can( 'moderate' )) || $current_user->is_bozo
    6337{ 
    6438        bb_die(__('You do not have permission to upload an avatar for this user.')); 
    6539} 
    66 /* --- End bbPress Stuff --- */ 
    6740 
    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 
    7045 
    7146// Some potential error messages in human readable form 
    7247$errorcodes = array( 
    7348        "- no error (this message will never be shown) -", 
    74         "The image file is too big, the maximum file size allowed is $max_kbytes KB.", 
    75         "The image file is too big, the maximum file size allowed is $max_kbytes KB.", 
     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.", 
    7651        "The file was only partially uploaded - the connection may have been interrupted.", 
    7752        "The image file does not appear to have been uploaded - did you select an image?", 
    7853        "The file does not appear to be a valid GIF, JPG/JPEG or PNG image type.", 
    7954        "The image file could not be saved to the avatars folder.", 
    80         "Image dimensions must not be greater than $max_width x $max_height pixels.", 
     55        "Image dimensions must not be greater than {$av_opts['max_width']} x {$av_opts['max_height']} pixels.", 
    8156        "The avatar filename may only contain upper/lower case letters, numbers, underscores or dashes." 
    8257); 
     
    8661if (!empty($_FILES['p_browse'])) 
    8762{ 
    88         $current_avatar = explode("|", $user->avatar_file); // for comparison later 
     63        $current_avatar = avatarupload_get_avatar($user_id, 0, 1); // for comparison later 
    8964 
    9065        $img_errs = 0; 
     
    11691        } 
    11792 
    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']) {  
    11994                // File size exceeds max_bytes 
    12095                $img_errs = 1; 
     
    12297        } 
    12398 
    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 
    132102                $img_errs = 5; 
    133103                $error++; 
     
    151121                $img_h = $dims[1]; 
    152122 
    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 
    154125                        $img_errs = 7; 
    155126                        $error++; 
     
    157128        } 
    158129 
    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)) 
    160131        { // Can save to avatars folder (does it exist?) 
    161132                $img_errs = 6; 
     
    169140                {       // compare 'new' and 'current' avatar filenames - if different, delete 'current' 
    170141                        // 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]); 
    172143                } 
    173144 
    174                 $meta_avatar = $user_filename . "|" . $img_w . "|" . $img_h
     145                $meta_avatar = $user_filename . "|" . $img_w . "|" . $img_h . "|avatar-upload"
    175146                bb_update_usermeta( $user_id, 'avatar_file', $meta_avatar ); 
    176147                $success_message = "Your avatar has been uploaded."; 
     
    178149} 
    179150 
    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') ); 
     151bb_load_template( 'avatar.php', array('success_message', 'av_opts') ); 
    191152?> 
  • avatar-upload/trunk/my-plugins/bb-avatar-upload.php

    r388 r395  
    22/* 
    33Plugin Name: Avatar Upload 
    4 Plugin URI: http://www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html 
    5 Version: 0.2 
    6 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. 
     4Plugin URI: http://bbpress.org/plugins/topic/46 
     5Version: 0.3 
     6Description: Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. 
    77Author: Louise Dade 
    88Author URI: http://www.classical-webdesigns.co.uk/ 
    99*/ 
    1010 
     11// Configuration Settings 
     12function avatarupload_config() 
     13{ 
     14        return array( 
    1115 
    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 
     64function avatarupload_display($id, $status='') 
    1365{ 
    14         if ($a = explode("|", $avatar)) 
     66        if ($a = avatarupload_get_avatar($id)) 
    1567        { 
    16                 echo '<img src="'.bb_get_option('uri').'avatars/'.$a[0]; 
     68                echo '<img src="'.$a[0]; 
    1769                echo ($status == 'new') ? '?'.time() : ''; 
    1870                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                } 
    1979        } 
    2080} 
    2181 
    22 function display_avatar($id, $status='') 
     82// Get the avatar URI 
     83function avatarupload_get_avatar($id, $fulluri=1, $force_db=0) 
    2384{ 
    24         if ($a = get_avatar($id)) 
     85        global $bbdb, $user; 
     86 
     87        if ($id == $user->ID && $force_db == 0) 
    2588        { 
    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                } 
    2994        } 
     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; 
    30113} 
    31114 
    32 function get_avatar($id) 
     115// Add an "Upload Avatar" tab to the Profile menu 
     116function add_avatar_tab() 
    33117{ 
    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'); 
    43119} 
     120add_action( 'bb_profile_menu', 'add_avatar_tab' ); 
    44121 
    45122?> 
  • avatar-upload/trunk/my-templates/avatar.php

    r388 r395  
    11<?php bb_get_header(); ?> 
    22 
    3 <h3 id="breadcrumb"><a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a> &raquo; <?php _e('Upload Avatar'); ?></h3> 
     3<h3 class="bbcrumb"><a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a> &raquo; <?php _e('Upload Avatar'); ?></h3> 
    44 
    55<?php if (bb_current_user_can('edit_user', $user->ID)) { ?> 
    66 
    77<?php 
    8         echo (!empty($success_message)) ? $success_message : ""; 
     8        echo (!empty($success_message)) ? '<div class="notice">'.__($success_message).'</div>' : ""; 
    99?> 
    1010 
     
    1212 
    1313<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> 
    1818</ul> 
    1919 
    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']; ?>" /> 
    2323<input type="file" name="p_browse" id="p_browse" size="80" /></p> 
    2424 
    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> 
    2626</form> 
    2727 
    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> 
    3030 
    3131<?php } ?> 
  • avatar-upload/trunk/readme.txt

    r388 r395  
    44Requires at least: 0.8 
    55Tested up to: 0.8.1 
    6 Stable Tag: 0.2 
     6Stable Tag: 0.3 
    77 
    88Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress. Admins can configure maximum allowed file size and image dimensions. 
     
    1010== Description == 
    1111 
    12 Plugin URI: http://www.classical-webdesigns.co.uk/articles/43_bbpress-plugin-avatar-upload.html 
     12Plugin URI: http://bbpress.org/plugins/topic/46 
     13Author: Louise Dade 
     14Author URI: http://www.classical-webdesigns.co.uk/ 
    1315 
    1416Allows users to upload an avatar (gif, jpeg/jpg or png) image to bbPress, and provides template functions to display the uploaded image. 
     
    1719* Bozos can not upload avatars. 
    1820* 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). 
    2022* Anybody with the 'moderate' capability can upload another user's avatar 
    2123  - 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 
    2327 
    2428== Installation == 
    2529 
    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)
     30UPGRADING?  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
    2731 
    28 2. Open up the 'avatar-upload.php' file and configure the "configuration Variables" (if desired). At least make sure the '$avatar_dir' path is correct.  Other configurable variables include the maximum allowed width and height of uploaded images and the maximum file size (in bytes). 
     321. 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). 
    2933 
    30 3. Open up your 'profile-edit.php' template and insert the following "Upload Avatar" link wherever you wish: 
     342. 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: 
    3135 
    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> 
    3337 
    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 
     403. To display an uploaded avatar, just insert the following template function. 
    3541 
    3642   a) On the user's profile page ('profile.php' template). 
    3743       
    38       <?php display_avatar_profile($user->avatar_file); ?> 
     44      <?php avatarupload_display($user->ID); ?> 
    3945 
    4046      This grabs the avatar info file directly from the current user's profile information. 
     
    4248   b) On each user's forum posts ('post.php' template) 
    4349 
    44       <?php display_avatar(get_post_author_id()); ?> 
     50      <?php avatarupload_display(get_post_author_id()); ?> 
    4551 
    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
    4753 
    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): 
    4955 
    50 6. Upload the plugin scripts to the following locations. 
     56     <?php avatarupload_get_avatar(ID); ?> 
    5157 
    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 
     604. 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 
     625. 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). 
    5568 
    5669That's it, the 'Avatar Upload' plugin should now be working. 
     
    7083== Change Log == 
    7184 
    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. 
     852007-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. 
     872007-04-17 Ver. 0.2 reduced DB calls, added filename checks (to stop things like 
     88                    "myavatar.exe.jpg"). 
     892007-04-07 Ver. 0.1 released.