/** * @param bool $updateNulls * @return bool */ public function store( $updateNulls = false ) { global $_CB_framework, $_PLUGINS; // TODO: Store the filename, filesize, extension, and mimetype to params // TODO: Also store height/width for image fields (add new functions for this as well) $new = ( $this->get( 'id' ) ? false : true ); if ( ! $new ) { $_PLUGINS->trigger( 'gallery_onBeforeUpdateItem', array( &$this ) ); } else { $_PLUGINS->trigger( 'gallery_onBeforeCreateItem', array( &$this ) ); } if ( isset( $_FILES['file']['tmp_name'] ) && ( ! empty( $_FILES['file']['tmp_name'] ) ) ) { $path = $_CB_framework->getCfg( 'absolute_path' ); $indexPath = $path . '/components/com_comprofiler/plugin/user/plug_cbgallery/index.html'; $filesPath = $path . '/images/comprofiler/plug_cbgallery'; $filePath = $filesPath . '/' . (int) $this->get( 'user_id' ) . '/' . $this->get( 'type' ); if ( ! is_dir( $filesPath ) ) { $oldMask = @umask( 0 ); if ( @mkdir( $filesPath, 0755, true ) ) { @umask( $oldMask ); @chmod( $filesPath, 0755 ); if ( ! file_exists( $filesPath . '/index.html' ) ) { @copy( $indexPath, $filesPath . '/index.html' ); @chmod( $filesPath . '/index.html', 0755 ); } } else { @umask( $oldMask ); } } if ( ! file_exists( $filesPath . '/.htaccess' ) ) { file_put_contents( $filesPath . '/.htaccess', 'deny from all' ); } if ( ! is_dir( $filePath ) ) { $oldMask = @umask( 0 ); if ( @mkdir( $filePath, 0755, true ) ) { @umask( $oldMask ); @chmod( $filePath, 0755 ); if ( ! file_exists( $filePath . '/index.html' ) ) { @copy( $indexPath, $filePath . '/index.html' ); @chmod( $filePath . '/index.html', 0755 ); } } else { @umask( $oldMask ); } } $fileExtension = strtolower( preg_replace( '/[^-a-zA-Z0-9_]/', '', pathinfo( $_FILES['file']['name'], PATHINFO_EXTENSION ) ) ); $fileName = Get::clean( pathinfo( $_FILES['file']['name'], PATHINFO_FILENAME ), GetterInterface::STRING ) . '.' . $fileExtension; $fileId = uniqid(); if ( $this->get( 'type' ) == 'photos' ) { $plugin = $_PLUGINS->getLoadedPlugin( 'user', 'cbgallery' ); $params = $_PLUGINS->getPluginParams( $plugin ); $resample = $params->get( 'photos_resample', 1 ); $aspectRatio = $params->get( 'photos_maintain_aspect_ratio', 1 ); $imageHeight = (int) $params->get( 'photos_image_height', 640 ); if ( ! $imageHeight ) { $imageHeight = 640; } $imageWidth = (int) $params->get( 'photos_image_width', 1280 ); if ( ! $imageWidth ) { $imageWidth = 1280; } $thumbHeight = (int) $params->get( 'photos_thumbnail_height', 320 ); if ( ! $thumbHeight ) { $thumbHeight = 320; } $thumbWidth = (int) $params->get( 'photos_thumbnail_width', 640 ); if ( ! $thumbWidth ) { $thumbWidth = 640; } $conversionType = (int) ( isset( $ueConfig['conversiontype'] ) ? $ueConfig['conversiontype'] : 0 ); $imageSoftware = ( $conversionType == 5 ? 'gmagick' : ( $conversionType == 1 ? 'imagick' : 'gd' ) ); try { $image = new \CBLib\Image\Image( $imageSoftware, $resample, $aspectRatio ); $image->setName( $fileId ); $image->setSource( $_FILES['file'] ); $image->setDestination( $filePath . '/' ); $image->processImage( $imageWidth, $imageHeight ); $newFileName = $image->getCleanFilename(); $image->setName( 'tn' . $fileId ); $image->processImage( $thumbWidth, $thumbHeight ); $this->set( 'value', $newFileName ); $this->set( 'file', $fileName ); } catch ( Exception $e ) { $this->setError( $e->getMessage() ); return false; } } else { $newFileName = $fileId . '.' . $fileExtension; if ( ! move_uploaded_file( $_FILES['file']['tmp_name'], $filePath . '/' . $newFileName ) ) { $this->setError( CBTxt::T( 'ITEM_FILE_UPLOAD_FAILED', 'The file [file] failed to upload!', array( '[file]' => $newFileName ) ) ); return false; } else { @chmod( $filePath . '/' . $newFileName, 0755 ); } $this->set( 'value', $newFileName ); $this->set( 'file', $fileName ); } } elseif ( preg_replace( '/^(?:(?:\w+\.)*)?(\w+)\..+$/', '\1', parse_url( $this->get( 'value' ), PHP_URL_HOST ) ) ) { $this->set( 'file', '' ); } elseif ( ! $this->get( 'file' ) ) { $this->set( 'file', $this->get( 'value' ) ); } $this->set( 'date', $this->get( 'date', $_CB_framework->getUTCDate() ) ); if ( ! parent::store( $updateNulls ) ) { return false; } if ( ! $new ) { $_PLUGINS->trigger( 'gallery_onAfterUpdateItem', array( $this ) ); } else { $_PLUGINS->trigger( 'gallery_onAfterCreateItem', array( $this ) ); } return true; }
/** * Mutator: * Prepares field data commit * Override * * @param FieldTable $field * @param UserTable $user RETURNED populated: touch only variables related to saving this field (also when not validating for showing re-edit) * @param array $postdata Typically $_POST (but not necessarily), filtering required. * @param string $reason 'edit' for save user edit, 'register' for save registration */ public function commitFieldDataSave(&$field, &$user, &$postdata, $reason) { global $_CB_framework, $ueConfig, $_PLUGINS, $_FILES; $col = $field->name; $colapproved = $col . 'approved'; $col_choice = $col . '__choice'; $col_file = $col . '__file'; $choice = stripslashes(cbGetParam($postdata, $col_choice)); switch ($choice) { case 'upload': $value = isset($_FILES[$col_file]) ? $_FILES[$col_file] : null; if ($this->validate($field, $user, $choice, $value, $postdata, $reason)) { $_PLUGINS->loadPluginGroup('user'); $isModerator = Application::MyUser()->isModeratorFor(Application::User((int) $user->id)); $_PLUGINS->trigger('onBeforeUserAvatarUpdate', array(&$user, &$user, $isModerator, &$value['tmp_name'])); if ($_PLUGINS->is_errors()) { $this->_setErrorMSG($_PLUGINS->getErrorMSG()); } $conversionType = (int) (isset($ueConfig['conversiontype']) ? $ueConfig['conversiontype'] : 0); $imageSoftware = $conversionType == 5 ? 'gmagick' : ($conversionType == 1 ? 'imagick' : 'gd'); $imagePath = $_CB_framework->getCfg('absolute_path') . '/images/comprofiler/'; $fileName = ($col == 'avatar' ? '' : $col . '_') . uniqid($user->id . '_'); try { $image = new \CBLib\Image\Image($imageSoftware, $this->_getImageFieldParam($field, 'avatarResizeAlways', 1), $this->_getImageFieldParam($field, 'avatarMaintainRatio', 1)); $image->setName($fileName); $image->setSource($value); $image->setDestination($imagePath); $image->processImage($this->_getImageFieldParam($field, 'avatarWidth', 200), $this->_getImageFieldParam($field, 'avatarHeight', 500)); $newFileName = $image->getCleanFilename(); $image->setName('tn' . $fileName); $image->processImage($this->_getImageFieldParam($field, 'thumbWidth', 60), $this->_getImageFieldParam($field, 'thumbHeight', 86)); } catch (Exception $e) { $this->_setValidationError($field, $user, $reason, $e->getMessage()); return; } $uploadApproval = $this->_getImageFieldParam($field, 'avatarUploadApproval', 1); if (isset($user->{$col}) && !($uploadApproval == 1 && !$isModerator)) { // if auto-approved: //TBD: else need to log update on image approval ! $this->_logFieldUpdate($field, $user, $reason, $user->{$col}, $newFileName); } if (isset($user->{$col}) && $user->{$col} != '') { deleteAvatar($user->{$col}); } if ($uploadApproval == 1 && !$isModerator) { $cbNotification = new cbNotification(); $cbNotification->sendToModerators(cbReplaceVars(CBTxt::T('UE_IMAGE_ADMIN_SUB', 'Image Pending Approval'), $user), cbReplaceVars(CBTxt::T('UE_IMAGE_ADMIN_MSG', 'A user has submitted an image for approval. Please log in and take the appropriate action.'), $user)); $user->{$col} = $newFileName; $user->{$colapproved} = 0; } else { $user->{$col} = $newFileName; $user->{$colapproved} = 1; } $_PLUGINS->trigger('onAfterUserAvatarUpdate', array(&$user, &$user, $isModerator, $newFileName)); } break; } }
/** * Uploads category or group canvas or logo * * @param string $type * @param CategoryTable|GroupTable $row * @return bool */ static public function uploadImage( $type = 'canvas', &$row ) { global $_CB_framework, $_PLUGINS; if ( ( ! $type ) || ( ! in_array( $type, array( 'canvas', 'logo' ) ) ) ) { return false; } $method = Application::Input()->get( 'post/' . $type . '_method', null, GetterInterface::INT ); if ( $method === 0 ) { return true; } static $params = null; if ( ! $params ) { $plugin = $_PLUGINS->getLoadedPlugin( 'user', 'cbgroupjive' ); $params = $_PLUGINS->getPluginParams( $plugin ); } $basePath = $_CB_framework->getCfg( 'absolute_path' ) . '/images/comprofiler/plug_cbgroupjive'; if ( $row instanceof GroupTable ) { $imagePath = $basePath . '/' . (int) $row->get( 'category' ) . '/' . (int) $row->get( 'id' ); } else { $imagePath = $basePath . '/' . (int) $row->get( 'id' ); } if ( ( ( $method === null ) || ( $method === 1 ) ) && isset( $_FILES[$type]['tmp_name'] ) && ( ! empty( $_FILES[$type]['tmp_name'] ) ) ) { if ( $row instanceof GroupTable ) { self::createDirectory( $basePath, $row->get( 'category' ), $row->get( 'id' ) ); } else { self::createDirectory( $basePath, $row->get( 'id' ) ); } $resample = $params->get( $type . '_resample', 1 ); $aspectRatio = $params->get( $type . '_maintain_aspect_ratio', 1 ); $imageHeight = (int) $params->get( $type . '_image_height', 640 ); if ( ! $imageHeight ) { $imageHeight = 640; } $imageWidth = (int) $params->get( $type . '_image_width', 1280 ); if ( ! $imageWidth ) { $imageWidth = 1280; } $thumbHeight = (int) $params->get( $type . '_thumbnail_height', 320 ); if ( ! $thumbHeight ) { $thumbHeight = 320; } $thumbWidth = (int) $params->get( $type . '_thumbnail_width', 640 ); if ( ! $thumbWidth ) { $thumbWidth = 640; } $conversionType = (int) Application::Config()->get( 'conversiontype', 0 ); $imageSoftware = ( $conversionType == 5 ? 'gmagick' : ( $conversionType == 1 ? 'imagick' : 'gd' ) ); $imageId = uniqid(); try { $image = new \CBLib\Image\Image( $imageSoftware, $resample, $aspectRatio ); $image->setName( $imageId ); $image->setSource( $_FILES[$type] ); $image->setDestination( $imagePath . '/' ); $image->processImage( $imageWidth, $imageHeight ); $newFileName = $image->getCleanFilename(); $image->setName( 'tn' . $imageId ); $image->processImage( $thumbWidth, $thumbHeight ); if ( $row->get( $type ) ) { $oldImage = $imagePath . '/' . $row->get( $type ); if ( file_exists( $oldImage ) ) { @unlink( $oldImage ); } $oldThumbnail = $imagePath . '/tn' . $row->get( $type ); if ( file_exists( $oldThumbnail ) ) { @unlink( $oldThumbnail ); } } $row->set( $type, $newFileName ); } catch ( \Exception $e ) { $row->setError( $e->getMessage() ); return false; } } elseif ( ( $method === 2 ) && $row->get( $type ) ) { $image = $imagePath . '/' . $row->get( $type ); if ( file_exists( $image ) ) { @unlink( $image ); } $thumbnail = $imagePath . '/tn' . $row->get( $type ); if ( file_exists( $thumbnail ) ) { @unlink( $thumbnail ); } $row->set( $type, '' ); } return true; }
/** * Parses profile data for an avatar and uploads it * * @param UserTable $user * @param Hybrid_User_Profile $profile */ private function avatar( &$user, $profile ) { global $_CB_framework, $ueConfig; if ( $profile->photoURL ) { try { $field = new FieldTable(); $field->load( array( 'name' => 'avatar' ) ); $field->set( 'params', new Registry( $field->get( 'params' ) ) ); $conversionType = (int) ( isset( $ueConfig['conversiontype'] ) ? $ueConfig['conversiontype'] : 0 ); $imageSoftware = ( $conversionType == 5 ? 'gmagick' : ( $conversionType == 1 ? 'imagick' : 'gd' ) ); $tmpPath = $_CB_framework->getCfg( 'absolute_path' ) . '/tmp/'; $imagePath = $_CB_framework->getCfg( 'absolute_path' ) . '/images/comprofiler/'; $fileName = uniqid( (string) $profile->identifier . '_' ); $resize = $field->params->get( 'avatarResizeAlways', '' ); if ( $resize == '' ) { if ( isset( $ueConfig['avatarResizeAlways'] ) ) { $resize = $ueConfig['avatarResizeAlways']; } else { $resize = 1; } } $aspectRatio = $field->params->get( 'avatarMaintainRatio', '' ); if ( $aspectRatio == '' ) { if ( isset( $ueConfig['avatarMaintainRatio'] ) ) { $aspectRatio = $ueConfig['avatarMaintainRatio']; } else { $aspectRatio = 1; } } $image = new \CBLib\Image\Image( $imageSoftware, $resize, $aspectRatio ); $avatar = $image->getImagine()->open( $profile->photoURL ); if ( $avatar ) { /** @var GuzzleHttp\ClientInterface $client */ $client = new GuzzleHttp\Client(); $ext = strtolower( preg_replace( '/[^-a-zA-Z0-9_]/', '', pathinfo( $profile->photoURL, PATHINFO_EXTENSION ) ) ); if ( ( ! $ext ) || ( ! in_array( $ext, array( 'jpg', 'jpeg', 'gif', 'png' ) ) ) ) { try { /** @var GuzzleHttp\Message\Response $result */ $result = $client->get( $profile->photoURL ); if ( $result->getStatusCode() == 200 ) { $mime = $result->getHeader( 'Content-Type' ); switch ( $mime ) { case 'image/jpeg': $ext = 'jpg'; break; case 'image/png': $ext = 'png'; break; case 'image/gif': $ext = 'gif'; break; } } } catch ( Exception $e ) {} } if ( ! in_array( $ext, array( 'jpg', 'jpeg', 'gif', 'png' ) ) ) { return; } $tmpAvatar = $tmpPath . $fileName . '.' . $ext; $avatar->save( $tmpAvatar ); $image->setImage( $avatar ); $image->setName( $fileName ); $image->setSource( $tmpAvatar ); $image->setDestination( $imagePath ); $width = $field->params->get( 'avatarWidth', '' ); if ( $width == '' ) { if ( isset( $ueConfig['avatarWidth'] ) ) { $width = $ueConfig['avatarWidth']; } else { $width = 200; } } $height = $field->params->get( 'avatarHeight', '' ); if ( $height == '' ) { if ( isset( $ueConfig['avatarHeight'] ) ) { $height = $ueConfig['avatarHeight']; } else { $height = 500; } } $image->processImage( $width, $height ); $user->set( 'avatar', $image->getCleanFilename() ); $image->setName( 'tn' . $fileName ); $thumbWidth = $field->params->get( 'thumbWidth', '' ); if ( $thumbWidth == '' ) { if ( isset( $ueConfig['thumbWidth'] ) ) { $thumbWidth = $ueConfig['thumbWidth']; } else { $thumbWidth = 60; } } $thumbHeight = $field->params->get( 'thumbHeight', '' ); if ( $thumbHeight == '' ) { if ( isset( $ueConfig['thumbHeight'] ) ) { $thumbHeight = $ueConfig['thumbHeight']; } else { $thumbHeight = 86; } } $image->processImage( $thumbWidth, $thumbHeight ); unlink( $tmpAvatar ); $approval = $field->params->get( 'avatarUploadApproval', '' ); if ( $approval == '' ) { if ( isset( $ueConfig['avatarUploadApproval'] ) ) { $approval = $ueConfig['avatarUploadApproval']; } else { $approval = 1; } } $user->set( 'avatarapproved', ( $approval ? 0 : 1 ) ); } } catch ( Exception $e ) {} } }