Exemple #1
0
	/**
	 * @return bool
	 */
	public function check()
	{
		global $_PLUGINS;

		$plugin								=	$_PLUGINS->getLoadedPlugin( 'user', 'cbgallery' );
		$params								=	$_PLUGINS->getPluginParams( $plugin );

		$minFileSize						=	$params->get( $this->get( 'type' ) . '_item_min_size', 0 );
		$maxFileSize						=	$params->get( $this->get( 'type' ) . '_item_max_size', 1024 );

		switch( $this->get( 'type' ) ) {
			case 'photos':
				$type						=	CBTxt::T( 'Photo' );
				break;
			case 'files':
				$type						=	CBTxt::T( 'File' );
				break;
			case 'videos':
				$type						=	CBTxt::T( 'Video' );
				break;
			case 'music':
				$type						=	CBTxt::T( 'Music' );
				break;
			default:
				$type						=	CBTxt::T( 'Item' );
				break;
		}

		$extensions							=	cbgalleryClass::getExtensions( $this->get( 'type' ) );

		if ( $this->get( 'user_id' ) == '' ) {
			$this->setError( CBTxt::T( 'Owner not specified!' ) );

			return false;
		} elseif ( $this->get( 'type' ) == '' ) {
			$this->setError( CBTxt::T( 'Type not specified!' ) );

			return false;
		} elseif ( ( ! $this->get( 'id' ) ) && ( ( ! $this->get( 'value' ) ) && ( ( ! isset( $_FILES['file']['tmp_name'] ) ) || empty( $_FILES['file']['tmp_name'] ) ) ) ) {
			$this->setError( CBTxt::T( 'ITEM_NOT_SPECIFIED', '[type] not specified!', array( '[type]' => $type ) ) );

			return false;
		} elseif ( isset( $_FILES['file']['tmp_name'] ) && ( ! empty( $_FILES['file']['tmp_name'] ) ) ) {
			$fileExtension					=	strtolower( preg_replace( '/[^-a-zA-Z0-9_]/', '', pathinfo( $_FILES['file']['name'], PATHINFO_EXTENSION ) ) );

			if ( ( ! $fileExtension ) || ( ! in_array( $fileExtension, $extensions ) ) ) {
				$this->setError( CBTxt::T( 'ITEM_UPLOAD_INVALID_EXT', 'Invalid file extension [ext]. Please upload only [exts]!', array( '[ext]' => $fileExtension, '[exts]' => implode( ', ', $extensions ) ) ) );

				return false;
			}

			$fileSize					=	$_FILES['file']['size'];

			if ( $minFileSize && ( ( $fileSize / 1024 ) < $minFileSize ) ) {
				$this->setError( CBTxt::T( 'ITEM_UPLOAD_TOO_SMALL', 'The file is too small, the minimum is [size]!', array( '[size]' => cbgalleryClass::getFormattedFileSize( $minFileSize * 1024 ) ) ) );

				return false;
			}

			if ( $maxFileSize && ( ( $fileSize / 1024 ) > $maxFileSize ) ) {
				$this->setError( CBTxt::T( 'ITEM_UPLOAD_TOO_LARGE', 'The file size exceeds the maximum of [size]!', array( '[size]' => cbgalleryClass::getFormattedFileSize( $maxFileSize * 1024 ) ) ) );

				return false;
			}
		} else {
			$linkDomain						=	preg_replace( '/^(?:(?:\w+\.)*)?(\w+)\..+$/', '\1', parse_url( $this->get( 'value' ), PHP_URL_HOST ) );

			if ( $linkDomain && ( ! ( in_array( $linkDomain, array( 'youtube', 'youtu' ) ) && ( $this->get( 'type' ) == 'videos' ) ) ) ) {
				$linkExists					=	false;

				try {
					$request				=	new GuzzleHttp\Client();

					$header					=	$request->head( $this->get( 'value' ) );

					if ( ( $header !== false ) && ( $header->getStatusCode() == 200 ) ) {
						$linkExists			=	true;
					}
				} catch( Exception $e ) {}

				if ( ! $linkExists ) {
					$this->setError( CBTxt::T( 'ITEM_LINK_INVALID_URL', 'Invalid file URL. Please ensure the URL exists!' ) );

					return false;
				}

				$linkExtension				=	strtolower( pathinfo( $this->get( 'value' ), PATHINFO_EXTENSION ) );

				if ( ( ! $linkExtension ) || ( ! in_array( $linkExtension, $extensions ) ) ) {
					if ( $this->get( 'type' ) == 'videos' ) {
						$extensions[]		=	'youtube';
					}

					$this->setError( CBTxt::T( 'ITEM_LINK_INVALID_EXT', 'Invalid file extension [ext]. Please upload only [exts]!', array( '[ext]' => $linkExtension, '[exts]' => implode( ', ', $extensions ) ) ) );

					return false;
				}
			}
		}

		return true;
	}
	/**
	 * Displays item create/edit page
	 *
	 * @param int         $id
	 * @param string      $type
	 * @param TabTable    $tab
	 * @param UserTable   $user
	 * @param UserTable   $viewer
	 * @param null|string $message
	 * @param null|string $messageType
	 */
	public function showItemEdit( $id, $type, $tab, $user, $viewer, $message = null, $messageType = 'error' )
	{
		global $_CB_framework, $_CB_database;

		/** @var Registry $params */
		$params								=	$tab->params;

		$row								=	new cbgalleryItemTable();

		$row->load( (int) $id );

		if ( ! $row->get( 'id' ) ) {
			$row->set( 'folder', $this->input( 'folder', 0, GetterInterface::INT ) );
		}

		$cbModerator						=	Application::User( (int) $viewer->get( 'id' ) )->isGlobalModerator();
		$canAccess							=	false;

		if ( ! $row->get( 'id' ) ) {
			if ( ( $user->get( 'id' ) != $viewer->get( 'id' ) ) && ( ! $cbModerator ) ) {
				$user						=	$viewer;
			}

			$canAccess						=	cbgalleryClass::canUserCreate( $viewer, $type, false );
		} elseif ( ( $row->get( 'type' ) == $type ) && ( $cbModerator || ( $viewer->get( 'id' ) == $row->get( 'user_id' ) ) ) ) {
			$canAccess						=	true;
		}

		if ( $row->get( 'folder' ) ) {
			$returnUrl						=	$_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'folders', 'func' => 'show', 'type' => $type, 'id' => (int) $row->get( 'folder' ), 'user' => (int) $user->get( 'id' ), 'tab' => (int) $tab->get( 'tabid' ) ) );
		} else {
			$returnUrl						=	$_CB_framework->userProfileUrl( (int) $row->get( 'user_id', $user->get( 'id' ) ), false, $tab->get( 'tabid' ) );
		}

		if ( ! $canAccess ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Not authorized.' ), 'error' );
		}

		$minFileSize						=	$this->params->get( $type . '_item_min_size', 0 );
		$maxFileSize						=	$this->params->get( $type . '_item_max_size', 1024 );

		switch( $type ) {
			case 'photos':
				$typeTranslated				=	CBTxt::T( 'Photo' );
				break;
			case 'files':
				$typeTranslated				=	CBTxt::T( 'File' );
				break;
			case 'videos':
				$typeTranslated				=	CBTxt::T( 'Video' );
				break;
			case 'music':
				$typeTranslated				=	CBTxt::T( 'Music' );
				break;
			default:
				$typeTranslated				=	CBTxt::T( 'Item' );
				break;
		}

		$extLimit							=	cbgalleryClass::getExtensions( $type );

		switch( $type ) {
			case 'photos':
			case 'videos':
			case 'music':
				$folderType					=	CBTxt::T( 'Album' );
				break;
			default:
				$folderType					=	CBTxt::T( 'Folder' );
				break;
		}

		cbgalleryClass::getTemplate( 'item_edit' );

		$input								=	array();

		$publishedTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_PUBLISHED_DESCRIPTION', 'Select publish status of the [type]. If unpublished the [type] will not be visible to the public.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['published']					=	moscomprofilerHTML::yesnoSelectList( 'published', 'class="form-control"' . ( $publishedTooltip ? ' ' . $publishedTooltip : null ), (int) $this->input( 'post/published', $row->get( 'published', 1 ), GetterInterface::INT ) );

		$titleTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_TITLE_DESCRIPTION', 'Optionally input a title. If no title is provided the filename will be displayed as the title.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['title']						=	'<input type="title" id="title" name="title" value="' . htmlspecialchars( $this->input( 'post/title', $row->get( 'title' ), GetterInterface::STRING ) ) . '" class="form-control" size="25"' . ( $titleTooltip ? ' ' . $titleTooltip : null ) . ' />';

		$listFolders						=	array();

		if ( cbgalleryClass::canUserCreate( $viewer, $type, true ) ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( -1, CBTxt::T( 'ITEM_NEW_FOLDER', 'New [type]', array( '[type]' => $folderType ) ) );
		}

		if ( $params->get( 'tab_' . $type . '_uncategorized', 1 ) ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( 0, CBTxt::T( 'Uncategorized' ) );
		}

		$query								=	'SELECT *'
											.	"\n FROM " . $_CB_database->NameQuote( '#__comprofiler_plugin_gallery_folders' )
											.	"\n WHERE " . $_CB_database->NameQuote( 'type' ) . " = " . $_CB_database->Quote( $type )
											.	"\n AND " . $_CB_database->NameQuote( 'user_id' ) . " = " . (int) $row->get( 'user_id', $user->get( 'id' ) )
											.	"\n ORDER BY " . $_CB_database->NameQuote( 'date' ) . " DESC";
		$_CB_database->setQuery( $query );
		$folders							=	$_CB_database->loadObjectList( null, 'cbgalleryFolderTable', array( $_CB_database ) );

		/** @var cbgalleryFolderTable[] $folders */
		foreach ( $folders as $folder ) {
			$listFolders[]					=	moscomprofilerHTML::makeOption( (int) $folder->get( 'id' ), ( $folder->get( 'title' ) ? $folder->get( 'title' ) : cbFormatDate( $folder->get( 'date' ), true, false ) ) );
		}

		$folderTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_FOLDER_DESCRIPTION', 'Select the [folder_type] for this [type].', array( '[folder_type]' => $folderType, '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['folder']					=	moscomprofilerHTML::selectList( $listFolders, 'folder', 'class="form-control"' . ( $folderTooltip ? ' ' . $folderTooltip : null ), 'value', 'text', $this->input( 'post/folder', $row->get( 'folder', 0 ), GetterInterface::INT ), 1, false, false );

		$allowUpload						=	$this->params->get( $type . '_item_upload', 1 );
		$allowLink							=	$this->params->get( $type . '_item_link', 0 );

		if ( $allowUpload && $allowLink ) {
			$uploadButton					=	CBTxt::T( 'UPLOAD_ITEM_TYPE', 'Upload [type]', array( '[type]' => $typeTranslated ) );
			$linkButton						=	CBTxt::T( 'LINK_ITEM_TYPE', 'Link [type]', array( '[type]' => $typeTranslated ) );

			$js								=	"$( '#method' ).on( 'change', function() {"
											.		"var value = $( this ).val();"
											.		"if ( value == 1 ) {"
											.			"$( '#itemUpload' ).removeClass( 'hidden' ).find( 'input' ).removeClass( 'cbValidationDisabled' );"
											.			"$( '#itemLink' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' );";

			if ( ! $row->get( 'id' ) ) {
				$js							.=			"$( '.galleryButtonSubmit' ).val( '" . addslashes( $uploadButton ) . "' );";
			}

			$js								.=		"} else if ( value == 2 ) {"
											.			"$( '#itemUpload' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' ).val( '' );"
											.			"$( '#itemLink' ).removeClass( 'hidden' ).find( 'input' ).removeClass( 'cbValidationDisabled' );";

			if ( ! $row->get( 'id' ) ) {
				$js							.=			"$( '.galleryButtonSubmit' ).val( '" . addslashes( $linkButton ) . "' );";
			}

			$js								.=		"} else {"
											.			"$( '#itemUpload' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' ).val( '' );"
											.			"$( '#itemLink' ).addClass( 'hidden' ).find( 'input' ).addClass( 'cbValidationDisabled' );"
											.		"}"
											.	"}).change();";

			$_CB_framework->outputCbJQuery( $js );

			$listMethods					=	array();

			if ( $row->get( 'id' ) ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 0, CBTxt::T( 'No Change' ) );
			}

			if ( $allowUpload ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 1, CBTxt::T( 'Upload' ) );
			}

			if ( $allowLink ) {
				$listMethods[]				=	moscomprofilerHTML::makeOption( 2, CBTxt::T( 'Link' ) );
			}

			$input['method']				=	moscomprofilerHTML::selectList( $listMethods, 'method', 'class="form-control"', 'value', 'text', $this->input( 'post/method', 0, GetterInterface::INT ), 1, false, false );
		} else {
			$input['method']				=	null;
		}

		$fileValidation						=	array();

		if ( $minFileSize || $maxFileSize ) {
			$fileValidation[]				=	cbValidator::getRuleHtmlAttributes( 'filesize', array( $minFileSize, $maxFileSize, 'KB' ) );
		}

		if ( $extLimit ) {
			$fileValidation[]				=	cbValidator::getRuleHtmlAttributes( 'extension', implode( ',', $extLimit ) );
		}

		if ( $allowUpload ) {
			$fileTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_UPLOAD_DESCRIPTION', 'Select the file to upload.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

			$input['upload']				=	'<input type="file" id="file" name="file" value="" class="form-control' . ( ! $row->get( 'id' ) ? ' required' : null ) . '"' . ( $fileTooltip ? ' ' . $fileTooltip : null ) . ( $fileValidation ? implode( ' ', $fileValidation ) : null ) . ' />';

			$input['upload_limits']			=	array();

			if ( $extLimit ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_EXT', 'Your file must be of [ext] type.', array( '[ext]' => implode( ', ', $extLimit ) ) );
			}

			if ( $minFileSize ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_MIN', 'Your file should exceed [size].', array( '[size]' => cbgalleryClass::getFormattedFileSize( $minFileSize * 1024 ) ) );
			}

			if ( $maxFileSize ) {
				$input['upload_limits'][]	=	CBTxt::T( 'ITEM_UPLOAD_LIMITS_MAX', 'Your file should not exceed [size].', array( '[size]' => cbgalleryClass::getFormattedFileSize( $maxFileSize * 1024 ) ) );
			}
		} else {
			$input['upload']				=	null;
			$input['upload_limits']			=	null;
		}

		if ( $allowLink ) {
			$linkTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_LINK_DESCRIPTION', 'Input the URL to the file to link.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

			$input['link']					=	'<input type="text" id="value" name="value" value="' . htmlspecialchars( $this->input( 'post/value', ( $row->getLinkDomain() ? $row->get( 'value' ) : null ), GetterInterface::STRING ) ) . '" size="40" class="form-control' . ( ! $row->get( 'id' ) ? ' required' : null ) . '"' . ( $linkTooltip ? ' ' . $linkTooltip : null ) . ' />';

			$input['link_limits']			=	array();

			if ( $extLimit ) {
				if ( $type == 'videos' ) {
					$extLimit[]				=	'youtube';
				}

				$input['link_limits'][]		=	CBTxt::T( 'ITEM_LINK_LIMITS_EXT', 'Your file must be of [ext] type.', array( '[ext]' => implode( ', ', $extLimit ) ) );
			}
		} else {
			$input['link']					=	null;
			$input['link_limits']			=	null;
		}

		$descriptionTooltip					=	cbTooltip( null, CBTxt::T( 'ITEM_DESCRIPTION_DESCRIPTION', 'Optionally input a description.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['description']				=	'<textarea id="description" name="description" class="form-control" cols="40" rows="5"' . ( $descriptionTooltip ? ' ' . $descriptionTooltip : null ) . '>' . htmlspecialchars( $this->input( 'post/description', $row->get( 'description' ), GetterInterface::STRING ) ) . '</textarea>';

		$ownerTooltip						=	cbTooltip( null, CBTxt::T( 'ITEM_OWNER_DESCRIPTION', 'Input owner as single integer user_id.', array( '[type]' => $typeTranslated ) ), null, null, null, null, null, 'data-hascbtooltip="true"' );

		$input['user_id']					=	'<input type="text" id="user_id" name="user_id" value="' . (int) $this->input( 'post/user_id', $row->get( 'user_id', $user->get( 'id' ) ), GetterInterface::INT ) . '" class="digits required form-control" size="6"' . ( $ownerTooltip ? ' ' . $ownerTooltip : null ) . ' />';

		if ( $message ) {
			$_CB_framework->enqueueMessage( $message, $messageType );
		}

		HTML_cbgalleryItemEdit::showItemEdit( $row, $input, $type, $tab, $user, $viewer, $this );
	}