Advertisement
Guest User

Untitled

a guest
Jun 12th, 2010
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.02 KB | None | 0 0
  1. <?php
  2. /*
  3.  Based on contributions from: Beau Lebens - http://www.dentedreality.com.au/
  4.  Modified for BuddyPress by: Andy Peatling - http://apeatling.wordpress.com/
  5. */
  6.  
  7. /***
  8.  * Set up the constants we need for avatar support
  9.  */
  10. function bp_core_set_avatar_constants() {
  11.     global $bp;
  12.  
  13.     if ( !defined( 'BP_AVATAR_UPLOAD_PATH' ) )
  14.         define( 'BP_AVATAR_UPLOAD_PATH', bp_core_avatar_upload_path() );
  15.  
  16.     if ( !defined( 'BP_AVATAR_URL' ) )
  17.         define( 'BP_AVATAR_URL', bp_core_avatar_url() );
  18.  
  19.     if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) )
  20.         define( 'BP_AVATAR_THUMB_WIDTH', 50 );
  21.  
  22.     //if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) )
  23.         //define( 'BP_AVATAR_THUMB_HEIGHT', 50 );
  24.  
  25.     if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) )
  26.         define( 'BP_AVATAR_FULL_WIDTH', 150 );
  27.  
  28.     //if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) )
  29.         //define( 'BP_AVATAR_FULL_HEIGHT', 150 );
  30.  
  31.     if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_WIDTH' ) )
  32.         define( 'BP_AVATAR_ORIGINAL_MAX_WIDTH', 450 );
  33.  
  34.     if ( !defined( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE' ) ) {
  35.         if ( !$bp->site_options['fileupload_maxk'] )
  36.             define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', 5120000 ); /* 5mb */
  37.         else
  38.             define( 'BP_AVATAR_ORIGINAL_MAX_FILESIZE', $bp->site_options['fileupload_maxk'] * 1024 );
  39.     }
  40.  
  41.     if ( !defined( 'BP_AVATAR_DEFAULT' ) )
  42.         define( 'BP_AVATAR_DEFAULT', BP_PLUGIN_URL . '/bp-xprofile/images/none.gif' );
  43.  
  44.     if ( !defined( 'BP_AVATAR_DEFAULT_THUMB' ) )
  45.         define( 'BP_AVATAR_DEFAULT_THUMB', BP_PLUGIN_URL . '/bp-xprofile/images/none-thumbnail.gif' );
  46. }
  47. add_action( 'bp_init', 'bp_core_set_avatar_constants' );
  48.  
  49. function bp_core_fetch_avatar( $args = '' ) {
  50.     global $bp, $current_blog;
  51.  
  52.     $defaults = array(
  53.         'item_id' => false,
  54.         'object' => 'user', // user OR group OR blog OR custom type (if you use filters)
  55.         'type' => 'thumb',
  56.         'avatar_dir' => false,
  57.         'width' => false,
  58.         'height' => false,
  59.         'class' => 'avatar',
  60.         'css_id' => false,
  61.         'alt' => __( 'Avatar Image', 'buddypress' ),
  62.         'email' => false, // Pass the user email (for gravatar) to prevent querying the DB for it
  63.         'no_grav' => false // If there is no avatar found, return false instead of a grav?
  64.     );
  65.  
  66.     $params = wp_parse_args( $args, $defaults );
  67.     extract( $params, EXTR_SKIP );
  68.  
  69.     if ( !$item_id ) {
  70.         if ( 'user' == $object )
  71.             $item_id = $bp->displayed_user->id;
  72.         else if ( 'group' == $object )
  73.             $item_id = $bp->groups->current_group->id;
  74.         else if ( 'blog' == $object )
  75.             $item_id = $current_blog->id;
  76.  
  77.         $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );
  78.  
  79.         if ( !$item_id ) return false;
  80.     }
  81.  
  82.     if ( !$avatar_dir ) {
  83.         if ( 'user' == $object )
  84.             $avatar_dir = 'avatars';
  85.         else if ( 'group' == $object )
  86.             $avatar_dir = 'group-avatars';
  87.         else if ( 'blog' == $object )
  88.             $avatar_dir = 'blog-avatars';
  89.  
  90.         $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );
  91.  
  92.         if ( !$avatar_dir ) return false;
  93.     }
  94.  
  95.     /* Add an identifying class to each item */
  96.     $class .= ' ' . $object . '-' . $item_id . '-avatar';
  97.  
  98.     if ( !empty($css_id) )
  99.         $css_id = " id='{$css_id}'";
  100.  
  101.     if ( $width )
  102.         $html_width = " width='{$width}'";
  103.     else
  104.         $html_width = ( 'thumb' == $type ) ? ' width="' . BP_AVATAR_THUMB_WIDTH . '"' : ' width="' . BP_AVATAR_FULL_WIDTH . '"';
  105.  
  106.     if ( $height )
  107.         $html_height = " height='{$height}'";
  108.     else
  109.         $html_height = ( 'thumb' == $type ) ? ' height="' . BP_AVATAR_THUMB_HEIGHT . '"' : ' height="' . BP_AVATAR_FULL_HEIGHT . '"';
  110.  
  111.     $avatar_folder_url = apply_filters( 'bp_core_avatar_folder_url', str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, BP_AVATAR_UPLOAD_PATH ) . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
  112.     $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
  113.  
  114.     /****
  115.      * Look for uploaded avatar first. Use it if it exists.
  116.      * Set the file names to search for, to select the full size
  117.      * or thumbnail image.
  118.      */
  119.     //ORIGINAL CODE $avatar_name = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
  120.     $avatar_name = ( 'full' == $type ) ? '' : '';
  121.     $legacy_user_avatar_name = ( 'full' == $type ) ? '-avatar2' : '-avatar1';
  122.     $legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb';
  123.  
  124.     if ( file_exists( $avatar_folder_dir ) ) {
  125.         if ( $av_dir = opendir( $avatar_folder_dir ) ) {
  126.             while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
  127.                 if ( preg_match( "/{$avatar_name}/", $avatar_file ) || preg_match( "/{$legacy_user_avatar_name}/", $avatar_file ) || preg_match( "/{$legacy_group_avatar_name}/", $avatar_file ) )
  128.                     $avatar_url = $avatar_folder_url . '/' . $avatar_file;
  129.             }
  130.         }
  131.         closedir($av_dir);
  132.  
  133.         if ( $avatar_url )
  134.             return apply_filters( 'bp_core_fetch_avatar', "<img src='{$avatar_url}' alt='{$alt}' class='{$class}'{$css_id}{$html_width}{$html_height} />", $params );
  135.     }
  136.  
  137.     /* If no avatars have been uploaded for this item, display a gravatar */
  138.     if ( !$no_grav ) {
  139.         if ( empty( $bp->grav_default->{$object} ) )
  140.             $default_grav = 'wavatar';
  141.         else if ( 'mystery' == $bp->grav_default->{$object} )
  142.             $default_grav = apply_filters( 'bp_core_mysteryman_src', BP_PLUGIN_URL . '/bp-core/images/mystery-man.jpg' );
  143.         else
  144.             $default_grav = $bp->grav_default->{$object};
  145.  
  146.         if ( $width ) $grav_size = $width;
  147.         else if ( 'full' == $type ) $grav_size = BP_AVATAR_FULL_WIDTH;
  148.         else if ( 'thumb' == $type ) $grav_size = BP_AVATAR_THUMB_WIDTH;
  149.  
  150.         if ( empty( $email ) ) {
  151.             if ( 'user' == $object ) {
  152.                 $email = bp_core_get_user_email( $item_id );
  153.             } else if ( 'group' == $object || 'blog' == $object ) {
  154.                 $email = "{$item_id}-{$object}@{$bp->root_domain}";
  155.             }
  156.         }
  157.  
  158.         $email = apply_filters( 'bp_core_gravatar_email', $email, $item_id, $object );
  159.         $gravatar = apply_filters( 'bp_gravatar_url', 'http://www.gravatar.com/avatar/' ) . md5( $email ) . '?d=' . $default_grav . '&amp;s=' . $grav_size;
  160.  
  161.         return apply_filters( 'bp_core_fetch_avatar', "<img src='{$gravatar}' alt='{$alt}' class='{$class}'{$css_id}{$html_width}{$html_height} />", $params );
  162.  
  163.     } else {
  164.         return false;
  165.     }
  166. }
  167.  
  168. function bp_core_delete_existing_avatar( $args = '' ) {
  169.     global $bp;
  170.  
  171.     $defaults = array(
  172.         'item_id' => false,
  173.         'object' => 'user', // user OR group OR blog OR custom type (if you use filters)
  174.         'avatar_dir' => false
  175.     );
  176.  
  177.     $args = wp_parse_args( $args, $defaults );
  178.     extract( $args, EXTR_SKIP );
  179.  
  180.     if ( !$item_id ) {
  181.         if ( 'user' == $object )
  182.             $item_id = $bp->displayed_user->id;
  183.         else if ( 'group' == $object )
  184.             $item_id = $bp->groups->current_group->id;
  185.         else if ( 'blog' == $object )
  186.             $item_id = $current_blog->id;
  187.  
  188.         $item_id = apply_filters( 'bp_core_avatar_item_id', $item_id, $object );
  189.  
  190.         if ( !$item_id ) return false;
  191.     }
  192.  
  193.     if ( !$avatar_dir ) {
  194.         if ( 'user' == $object )
  195.             $avatar_dir = 'avatars';
  196.         else if ( 'group' == $object )
  197.             $avatar_dir = 'group-avatars';
  198.         else if ( 'blog' == $object )
  199.             $avatar_dir = 'blog-avatars';
  200.  
  201.         $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );
  202.  
  203.         if ( !$avatar_dir ) return false;
  204.     }
  205.  
  206.     $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
  207.  
  208.     if ( !file_exists( $avatar_folder_dir ) )
  209.         return false;
  210.  
  211.     if ( $av_dir = opendir( $avatar_folder_dir ) ) {
  212.         while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
  213.             if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file )
  214.                 @unlink( $avatar_folder_dir . '/' . $avatar_file );
  215.         }
  216.     }
  217.     closedir($av_dir);
  218.  
  219.     @rmdir( $avatar_folder_dir );
  220.  
  221.     do_action( 'bp_core_delete_existing_avatar', $args );
  222.  
  223.     return true;
  224. }
  225.  
  226. function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) {
  227.     global $bp;
  228.  
  229.     require_once( ABSPATH . '/wp-admin/includes/image.php' );
  230.     require_once( ABSPATH . '/wp-admin/includes/file.php' );
  231.  
  232.     $uploadErrors = array(
  233.         0 => __("There is no error, the file uploaded with success", 'buddypress'),
  234.         1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE),
  235.         2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE),
  236.         3 => __("The uploaded file was only partially uploaded", 'buddypress'),
  237.         4 => __("No file was uploaded", 'buddypress'),
  238.         6 => __("Missing a temporary folder", 'buddypress')
  239.     );
  240.  
  241.     if ( !bp_core_check_avatar_upload( $file ) ) {
  242.         bp_core_add_message( sprintf( __( 'Your upload failed, please try again. Error was: %s', 'buddypress' ), $uploadErrors[$file['file']['error']] ), 'error' );
  243.         return false;
  244.     }
  245.  
  246.     if ( !bp_core_check_avatar_size( $file ) ) {
  247.         bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(BP_AVATAR_ORIGINAL_MAX_FILESIZE) ), 'error' );
  248.         return false;
  249.     }
  250.  
  251.     if ( !bp_core_check_avatar_type( $file ) ) {
  252.         bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' );
  253.         return false;
  254.     }
  255.  
  256.     /* Filter the upload location */
  257.     add_filter( 'upload_dir', $upload_dir_filter, 10, 0 );
  258.  
  259.     $bp->avatar_admin->original = wp_handle_upload( $file['file'], array( 'action'=> 'bp_avatar_upload' ) );
  260.  
  261.     /* Move the file to the correct upload location. */
  262.     if ( !empty( $bp->avatar_admin->original['error'] ) ) {
  263.         bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $bp->avatar_admin->original['error'] ), 'error' );
  264.         return false;
  265.     }
  266.  
  267.     /* Resize the image down to something manageable and then delete the original */
  268.     if ( getimagesize( $bp->avatar_admin->original['file'] ) > BP_AVATAR_ORIGINAL_MAX_WIDTH )
  269.         $bp->avatar_admin->resized = wp_create_thumbnail( $bp->avatar_admin->original['file'], BP_AVATAR_ORIGINAL_MAX_WIDTH );
  270.  
  271.     $bp->avatar_admin->image = new stdClass;
  272.  
  273.     /* We only want to handle one image after resize. */
  274.     if ( empty( $bp->avatar_admin->resized ) )
  275.         $bp->avatar_admin->image->dir = $bp->avatar_admin->original['file'];
  276.     else {
  277.         $bp->avatar_admin->image->dir = $bp->avatar_admin->resized;
  278.         @unlink( $bp->avatar_admin->original['file'] );
  279.     }
  280.  
  281.     /* Set the url value for the image */
  282.     $bp->avatar_admin->image->url = str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, $bp->avatar_admin->image->dir );
  283.  
  284.     return true;
  285. }
  286.  
  287. function bp_core_avatar_handle_crop( $args = '' ) {
  288.     global $bp;
  289.  
  290.     $defaults = array(
  291.         'object' => 'user',
  292.         'avatar_dir' => 'avatars',
  293.         'item_id' => false,
  294.         'original_file' => false,
  295.         'crop_w' => BP_AVATAR_FULL_WIDTH,
  296.         'crop_h' => BP_AVATAR_FULL_HEIGHT,
  297.         'crop_x' => 0,
  298.         'crop_y' => 0
  299.     );
  300.  
  301.     $r = wp_parse_args( $args, $defaults );
  302.     extract( $r, EXTR_SKIP );
  303.  
  304.     if ( !$original_file )
  305.         return false;
  306.  
  307.     if ( !file_exists( WP_CONTENT_DIR . '/' . $original_file ) )
  308.         return false;
  309.  
  310.     if ( !$item_id )
  311.         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', WP_CONTENT_DIR . dirname( $original_file ), $item_id, $object, $avatar_dir );
  312.     else
  313.         $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', BP_AVATAR_UPLOAD_PATH . '/' . $avatar_dir . '/' . $item_id, $item_id, $object, $avatar_dir );
  314.  
  315.     if ( !file_exists( $avatar_folder_dir ) )
  316.         return false;
  317.  
  318.     require_once( ABSPATH . '/wp-admin/includes/image.php' );
  319.     require_once( ABSPATH . '/wp-admin/includes/file.php' );
  320.  
  321.     /* Delete the existing avatar files for the object */
  322.     bp_core_delete_existing_avatar( array( 'object' => $object, 'avatar_path' => $avatar_folder_dir ) );
  323.  
  324.     /* Make sure we at least have a width and height for cropping */
  325.     if ( !(int)$crop_w )
  326.         $crop_w = BP_AVATAR_FULL_WIDTH;
  327.  
  328.     if ( !(int)$crop_h )
  329.         $crop_h = BP_AVATAR_FULL_HEIGHT;
  330.  
  331.     /* Set the full and thumb filenames */
  332.     $full_filename = wp_hash( $original_file . time() ) . '-bpfull.jpg';
  333.     $thumb_filename = wp_hash( $original_file . time() ) . '-bpthumb.jpg';
  334.  
  335.     /* Crop the image */
  336.     $full_cropped = wp_crop_image( WP_CONTENT_DIR . $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_FULL_WIDTH, BP_AVATAR_FULL_HEIGHT, false, $avatar_folder_dir . '/' . $full_filename );
  337.     $thumb_cropped = wp_crop_image( WP_CONTENT_DIR . $original_file, (int)$crop_x, (int)$crop_y, (int)$crop_w, (int)$crop_h, BP_AVATAR_THUMB_WIDTH, BP_AVATAR_THUMB_HEIGHT, false, $avatar_folder_dir . '/' . $thumb_filename );
  338.  
  339.     /* Remove the original */
  340.     @unlink( WP_CONTENT_DIR . $original_file );
  341.  
  342.     return true;
  343. }
  344.  
  345. // Override internal "get_avatar()" function to use our own where possible
  346. function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt ) {
  347.     global $authordata;
  348.  
  349.     if ( is_object( $user ) )
  350.         $id = $user->user_id;
  351.     else if ( is_numeric( $user ) )
  352.         $id = $user;
  353.     else
  354.         $id = $authordata->ID;
  355.  
  356.     if ( empty( $id ) )
  357.         return $avatar;
  358.  
  359.     $bp_avatar = bp_core_fetch_avatar( array( 'item_id' => $id, 'width' => $size, 'height' => $size, 'alt' => $alt ) );
  360.  
  361.     return ( !$bp_avatar ) ? $avatar : $bp_avatar;
  362. }
  363. add_filter( 'get_avatar', 'bp_core_fetch_avatar_filter', 10, 5 );
  364.  
  365. function bp_core_check_avatar_upload($file) {
  366.     if ( $file['error'] )
  367.         return false;
  368.  
  369.     return true;
  370. }
  371.  
  372. function bp_core_check_avatar_size($file) {
  373.     if ( $file['file']['size'] > BP_AVATAR_ORIGINAL_MAX_FILESIZE )
  374.         return false;
  375.  
  376.     return true;
  377. }
  378.  
  379. function bp_core_check_avatar_type($file) {
  380.     if ( ( strlen($file['file']['type']) && !preg_match('/(jpe?g|gif|png)$/', $file['file']['type'] ) ) && !preg_match( '/(jpe?g|gif|png)$/', $file['file']['name'] ) )
  381.         return false;
  382.  
  383.     return true;
  384. }
  385.  
  386. function bp_core_avatar_upload_path() {
  387.     if ( bp_core_is_multisite() )
  388.         $path = ABSPATH . get_blog_option( BP_ROOT_BLOG, 'upload_path' );
  389.     else {
  390.         if ( !$path = get_option( 'upload_path' ) )
  391.             $path = WP_CONTENT_DIR . '/uploads';
  392.     }
  393.  
  394.     return apply_filters( 'bp_core_avatar_upload_path', $path );
  395. }
  396.  
  397. function bp_core_avatar_url() {
  398.     if ( !bp_core_is_multisite() )
  399.         return WP_CONTENT_URL;
  400.  
  401.     return apply_filters( 'bp_core_avatar_url', get_blog_option( BP_ROOT_BLOG, 'siteurl' ) );
  402. }
  403.  
  404. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement