| 1 | <?php |
| 2 | /** |
| 3 | * Core component classes. |
| 4 | * |
| 5 | * @package BuddyPress |
| 6 | * @subpackage Core |
| 7 | */ |
| 8 | |
| 9 | // Exit if accessed directly |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * BP Attachment class to manage your component's uploads |
| 14 | * |
| 15 | * @since BuddyPress (2.3.0) |
| 16 | */ |
| 17 | class BP_Attachment { |
| 18 | |
| 19 | /** Upload properties *****************************************************/ |
| 20 | |
| 21 | /** |
| 22 | * The id of the component the upload belongs to. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | public $component = 'avatar'; |
| 27 | |
| 28 | /** |
| 29 | * Maximum file size in kilobytes |
| 30 | * |
| 31 | * @var int |
| 32 | */ |
| 33 | public $original_max_filesize = 5120000; |
| 34 | |
| 35 | /** |
| 36 | * component's upload base directory. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $base_dir = ''; |
| 41 | |
| 42 | /** |
| 43 | * The upload action. |
| 44 | * Defaults to the avatar upload action |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $action = 'bp_avatar_upload'; |
| 49 | |
| 50 | /** |
| 51 | * The file input name attribute |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | public $file_input = 'file'; |
| 56 | |
| 57 | /** |
| 58 | * List of upload errors. |
| 59 | * |
| 60 | * @var array |
| 61 | */ |
| 62 | public $upload_error_strings = array(); |
| 63 | |
| 64 | /** |
| 65 | * Name of the function that will handle errors. |
| 66 | * Defaults to the WordPress built-in handler |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | public $upload_error_handler = 'wp_handle_upload_error'; |
| 71 | |
| 72 | /** |
| 73 | * Name of the function that will contain the specific rules. |
| 74 | * |
| 75 | * @var string |
| 76 | */ |
| 77 | public $upload_rules_handler = ''; |
| 78 | |
| 79 | /** |
| 80 | * List of required core files |
| 81 | * |
| 82 | * @var array |
| 83 | */ |
| 84 | public $required_wp_files = array( 'file' ); |
| 85 | |
| 86 | /** |
| 87 | * Construct Upload parameters |
| 88 | * |
| 89 | * @since BuddyPress (2.3.0) |
| 90 | * |
| 91 | * @param array $args { |
| 92 | * @type string $component The id of the component the upload belongs to. Default: avatar. |
| 93 | * @type int $original_max_filesize Maximum file size in kilobytes. Default: 5120000. |
| 94 | * @type string $base_dir component's upload base directory. Defaults to WordPress 'uploads' |
| 95 | * @type string $action the upload action used when uploading a file, $_POST['action'] must be set |
| 96 | * and its value must equal $action {@link wp_handle_upload()} |
| 97 | * @type string $file_input the name attribute used in the file input. Default: file |
| 98 | * @type array $upload_error_strings a list of specific error messages (optional). If set, make sure |
| 99 | * to set $upload_rules_handler |
| 100 | * @type string $upload_error_handler the function that will handle errors. Default: avatar wp_handle_upload_error |
| 101 | * @type string $upload_rules_handler the function that will check upon component's specific rules. (optional) |
| 102 | * @type array $required_wp_files the list of required WordPress core files. Default: array( 'file' ); |
| 103 | * } |
| 104 | * @uses sanitize_key() |
| 105 | * @uses bp_parse_args() |
| 106 | * @uses BP_Attachment->set_upload_error_strings() |
| 107 | * @uses BP_Attachment->set_upload_dir() |
| 108 | */ |
| 109 | public function __construct( $args = '' ) { |
| 110 | /** |
| 111 | * Sanitize the component id |
| 112 | */ |
| 113 | if ( ! empty( $args['component'] ) ) { |
| 114 | $this->component = sanitize_key( $args['component'] ); |
| 115 | } |
| 116 | |
| 117 | $params = bp_parse_args( $args, get_class_vars( __CLASS__ ), $this->component . '_upload_params' ); |
| 118 | |
| 119 | foreach ( $params as $key => $param ) { |
| 120 | if ( 'upload_error_strings' == $key ) { |
| 121 | $this->{$key} = $this->set_upload_error_strings( $param ); |
| 122 | } else { |
| 123 | $this->{$key} = $param; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Set the path/url and base dir for uploads |
| 128 | $this->set_upload_dir(); |
| 129 | |
| 130 | // Finally set some specific for avatar |
| 131 | if ( 'avatar' == $this->component ) { |
| 132 | // Set the specific rules handler |
| 133 | $this->upload_rules_handler = array( $this, 'validate' ); |
| 134 | |
| 135 | //set default thumb & full values |
| 136 | if ( isset( $this->thumb, $this->full ) ) { |
| 137 | $this->thumb->default = bp_core_avatar_default_thumb() . $this->thumb->width; |
| 138 | $this->full->default = bp_core_avatar_default() . $this->full->width; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set data from the BP root blog's upload directory. |
| 145 | * |
| 146 | * Handy for multisite instances because all uploads are made on the BP root |
| 147 | * blog and we need to query the BP root blog for the upload directory data. |
| 148 | * |
| 149 | * This function ensures that we only need to use {@link switch_to_blog()} |
| 150 | * once to get what we need. |
| 151 | * |
| 152 | * @since BuddyPress (2.3.0) |
| 153 | * |
| 154 | * @uses is_multisite() |
| 155 | * @uses bp_is_root_blog() |
| 156 | * @uses switch_to_blog() |
| 157 | * @uses wp_upload_dir() |
| 158 | * @uses restore_current_blog() |
| 159 | */ |
| 160 | public function set_upload_dir() { |
| 161 | if ( 'avatar' == $this->component && defined( 'BP_AVATAR_UPLOAD_PATH' ) && defined( 'BP_AVATAR_URL' ) ) { |
| 162 | $this->upload_path = BP_AVATAR_UPLOAD_PATH; |
| 163 | $this->url = BP_AVATAR_URL; |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | $need_switch = is_multisite() && ! bp_is_root_blog(); |
| 168 | |
| 169 | // We need to switch to the root blog on multisite installs |
| 170 | if ( $need_switch ) { |
| 171 | switch_to_blog( bp_get_root_blog_id() ); |
| 172 | } |
| 173 | |
| 174 | $wp_upload_dir = wp_upload_dir(); |
| 175 | |
| 176 | if ( $need_switch ) { |
| 177 | restore_current_blog(); |
| 178 | } |
| 179 | |
| 180 | if ( ! empty( $wp_upload_dir['error'] ) ) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | $this->upload_dir = $wp_upload_dir; |
| 185 | $this->upload_path = $wp_upload_dir['basedir']; |
| 186 | $this->url = $wp_upload_dir['baseurl']; |
| 187 | |
| 188 | if ( is_ssl() ) { |
| 189 | $this->url = str_replace( 'http://', 'https://', $this->url ); |
| 190 | } |
| 191 | |
| 192 | // Custom base dir |
| 193 | if ( ! empty( $this->base_dir ) ) { |
| 194 | $this->upload_path = trailingslashit( $this->upload_path ) . $this->base_dir; |
| 195 | $this->url = trailingslashit( $this->upload_path ) . $this->base_dir; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set Upload error messages |
| 201 | * |
| 202 | * Used into the $overrides argument of BP_Attachment->upload() |
| 203 | * |
| 204 | * @since BuddyPress (2.3.0) |
| 205 | * |
| 206 | * @param array $param a list of error messages to add to BuddyPress core ones |
| 207 | */ |
| 208 | public function set_upload_error_strings( $param = array() ) { |
| 209 | /** |
| 210 | * Index of the array is the error code |
| 211 | * Custom errors will start at 9 code |
| 212 | */ |
| 213 | return array_merge( array( |
| 214 | 0 => __( 'The file was uploaded successfully', 'buddypress' ), |
| 215 | 1 => __( 'The uploaded file exceeds the maximum allowed file size on this site', 'buddypress' ), |
| 216 | 2 => sprintf( __( 'The uploaded file exceeds the maximum allowed file size of: %s', 'buddypress' ), size_format( $this->original_max_filesize ) ), |
| 217 | 3 => __( 'The uploaded file was only partially uploaded.', 'buddypress' ), |
| 218 | 4 => __( 'No file was uploaded.', 'buddypress' ), |
| 219 | 5 => '', |
| 220 | 6 => __( 'Missing a temporary folder.', 'buddypress' ), |
| 221 | 7 => __( 'Failed to write file to disk.', 'buddypress' ), |
| 222 | 8 => __( 'File upload stopped by extension.', 'buddypress' ), |
| 223 | ), (array) $param ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Include the WordPress core needed files |
| 228 | * |
| 229 | * @since BuddyPress (2.3.0) |
| 230 | */ |
| 231 | private function includes() { |
| 232 | foreach ( $this->required_wp_files as $wp_file ) { |
| 233 | if ( ! file_exists( ABSPATH . "/wp-admin/includes/{$wp_file}.php" ) ) { |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | require_once( ABSPATH . "/wp-admin/includes/{$wp_file}.php" ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Upload the attachment |
| 243 | * |
| 244 | * @since BuddyPress (2.3.0) |
| 245 | * |
| 246 | * @param array $file The appropriate entry the from $_FILES superglobal. |
| 247 | * @param string $upload_dir_filter A specific filter to be applied to 'upload_dir' (optional). |
| 248 | * @uses wp_handle_upload() |
| 249 | * @uses apply_filters() call 'bp_attachment_upload_overrides' to include specific upload overrides |
| 250 | * @return array On success, returns an associative array of file attributes. On failure, returns |
| 251 | * an array containing the error message (eg: array( 'error' => $message ) ) |
| 252 | */ |
| 253 | public function upload( $file, $upload_dir_filter = '' ) { |
| 254 | if ( ! empty( $this->upload_rules_handler ) && ! empty( $this->action ) ) { |
| 255 | /** |
| 256 | * Add custom rules before enabling the file upload |
| 257 | */ |
| 258 | add_filter( "{$this->action}_prefilter", $this->upload_rules_handler, 10, 1 ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * If you need to add some overrides we haven't thought of |
| 263 | * |
| 264 | * @var array $overrides the wp_handle_upload overrides |
| 265 | */ |
| 266 | $overrides = apply_filters( 'bp_attachment_upload_overrides', array( |
| 267 | 'action' => $this->action, |
| 268 | 'upload_error_strings' => $this->upload_error_strings, |
| 269 | ) ); |
| 270 | |
| 271 | $this->includes(); |
| 272 | |
| 273 | /** |
| 274 | * If the $base_dir was set when constructing the class, |
| 275 | * and no specific filter has been requested, use a default |
| 276 | * filter to create the specific $base dir |
| 277 | * @see BP_Attachment->upload_dir_filter() |
| 278 | */ |
| 279 | if ( empty( $upload_dir_filter ) && ! empty( $this->base_dir ) ) { |
| 280 | $upload_dir_filter = array( $this, 'upload_dir_filter' ); |
| 281 | } |
| 282 | |
| 283 | // Make sure the file will be uploaded in the attachment directory |
| 284 | add_filter( 'upload_dir', $upload_dir_filter, 10, 0 ); |
| 285 | |
| 286 | // Upload the attachment |
| 287 | $this->attachment = wp_handle_upload( $file[ $this->file_input ], $overrides ); |
| 288 | |
| 289 | // Restore WordPress Uploads data |
| 290 | remove_filter( 'upload_dir', $upload_dir_filter, 10, 0 ); |
| 291 | |
| 292 | // Finally return the uploaded file or the error |
| 293 | return $this->attachment; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Default filter to save the attachments |
| 298 | * |
| 299 | * @since BuddyPress (2.3.0) |
| 300 | * |
| 301 | * @uses wp_mkdir_p() |
| 302 | * @uses do_action() call 'bp_attachment_base_upload_dir_created' to run specific action once the |
| 303 | * $base_dir is created (eg: add an .htaccess file) |
| 304 | * @uses apply_filters() call 'bp_attachment_upload_dir' to eventually override the upload location |
| 305 | * regarding to context |
| 306 | * @return array the upload directory data |
| 307 | */ |
| 308 | public function upload_dir_filter() { |
| 309 | if ( ! file_exists( $this->upload_path ) ) { |
| 310 | @wp_mkdir_p( $this->upload_path ); |
| 311 | |
| 312 | /** |
| 313 | * Use this filter if you need to run specific actions |
| 314 | * once the directory is created |
| 315 | * |
| 316 | * @since BuddyPress (2.3.0) |
| 317 | * |
| 318 | * @var string $component the component id |
| 319 | * @var string $base_dir the specific base dir used by the component |
| 320 | */ |
| 321 | do_action( 'bp_attachment_base_upload_dir_created', $this->component, $this->base_dir ); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Filters the component's upload directory. |
| 326 | * |
| 327 | * @since BuddyPress (2.3.0) |
| 328 | * |
| 329 | * @param array $value Array containing the path, URL, and other helpful settings. |
| 330 | */ |
| 331 | return apply_filters( 'bp_attachment_upload_dir', array( |
| 332 | 'path' => $this->upload_path, |
| 333 | 'url' => $this->url, |
| 334 | 'subdir' => false, |
| 335 | 'basedir' => $this->upload_path, |
| 336 | 'baseurl' => $this->url, |
| 337 | 'error' => false |
| 338 | ) ); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Avatar specific rules |
| 343 | * |
| 344 | * Adds an error if the avatar size or type don't match BuddyPress needs |
| 345 | * The error code is the index of $upload_error_strings |
| 346 | * |
| 347 | * @since BuddyPress (2.3.0) |
| 348 | * |
| 349 | * @param array $file the temporary file attributes (before it has been moved) |
| 350 | * @uses bp_core_check_avatar_size() |
| 351 | * @uses bp_core_check_avatar_type() |
| 352 | * @return array the file with extra errors if needed |
| 353 | */ |
| 354 | public function validate( $file = array() ) { |
| 355 | if ( ! empty( $file['error'] ) ) { |
| 356 | return $file; |
| 357 | } |
| 358 | |
| 359 | if ( ! bp_core_check_avatar_size( array( 'file' => $file ) ) ) { |
| 360 | $file['error'] = 9; |
| 361 | } else if ( ! bp_core_check_avatar_type( array( 'file' => $file ) ) ) { |
| 362 | $file['error'] = 10; |
| 363 | } |
| 364 | |
| 365 | return $file; |
| 366 | } |
| 367 | } |