public/wordpress/wp-content/plugins/wp-courseware/includes/legacy/quiz/upload.php
/**
		 * Validate the files that have been uploaded, checking them against the conditions of the quiz details.
		 *
		 * @param Array $fileList The list of files to be checked for this quiz.
		 * @param Array $quizDetails The details of the quiz to check
		 *
		 * @return Array The results of the file upload (upload_errors, upload_missing, upload_valid), which contain a list of the question ID and error messages.
		 */
Validate the files that have been uploaded, checking them against the conditions of the quiz details.
@param Array $fileList The list of files to be checked for this quiz.
@param Array $quizDetails The details of the quiz to check

@return Array The results of the file upload (upload_errors, upload_missing, upload_valid), which contain a list of the question ID and error messages.
Validate the files that have been uploaded checking them against the conditions of the quiz details . @param Array $fileList The list of files to be checked for this quiz . @param Array $quizDetails The details of the quiz to check @return Array The results of the file upload ( upload_errors upload_missing upload_valid ) which contain a list of the question ID and error messages .
public static function validateFiles( $fileList, $quizDetails ) {
			$questionsWithUploads = array();
			foreach ( $quizDetails->questions as $qID => $qObj ) {
				if ( 'upload' == $qObj->question_type ) {
					$questionsWithUploads[ $qID ] = $qObj;
				}
			}
			if ( count( $questionsWithUploads ) == 0 ) {
				return false;
			}
			$userPathDetails = WPCW_files_getFileUploadDirectory_forUser( $quizDetails, get_current_user_id() );
			$results = array(
				'upload_errors'  => array(),
				'upload_missing' => array(),
				'upload_valid'   => array(),
			);
			foreach ( $questionsWithUploads as $qID => $qObj ) {
				$keyName = sprintf( 'question_%d_upload_%d', $quizDetails->quiz_id, $qID );
				if ( isset( $fileList[ $keyName ] ) ) {
					$file_name  = $fileList[ $keyName ]['name'];
					$file_tmp   = $fileList[ $keyName ]['tmp_name'];
					$file_error = $fileList[ $keyName ]['error'];
					$file_size  = $fileList[ $keyName ]['size'];
										if ( $file_error > 0 ) {
						$errMsg = __( 'Error. An unknown file upload error occurred.', 'wp-courseware' );
						switch ( $file_error ) {
							case UPLOAD_ERR_FORM_SIZE:
							case UPLOAD_ERR_INI_SIZE:
								$errMsg = sprintf( __( 'Error. The uploaded file exceeds the maximum file upload size (%s).', 'wp-courseware' ), WPCW_files_getMaxUploadSize() );
								break;
							case UPLOAD_ERR_PARTIAL:
								$errMsg = __( 'Error. The uploaded file was only partially uploaded.', 'wp-courseware' );
								break;
							case UPLOAD_ERR_NO_FILE:
								$errMsg = __( 'Error. No file was uploaded.', 'wp-courseware' );
								break;
							case UPLOAD_ERR_NO_TMP_DIR:
								$errMsg = __( 'Error. The temporary upload directory does not exist.', 'wp-courseware' );
								break;
							case UPLOAD_ERR_CANT_WRITE:
								$errMsg = __( 'Error. Could not write the uploaded file to disk.', 'wp-courseware' );
								break;
							case UPLOAD_ERR_EXTENSION:
								$errMsg = __( 'Error. An extension stopped the file upload.', 'wp-courseware' );
								break;
						}
						$results['upload_errors'][ $qID ] = $errMsg;
						continue;
					}
					$extensionTypes    = WPCW_files_cleanFileExtensionList( $qObj->question_answer_file_types );
					$thisFileExtension = pathinfo( $file_name, PATHINFO_EXTENSION );
					if ( ! in_array( strtolower( $thisFileExtension ), $extensionTypes ) ) {
						$results['upload_errors'][ $qID ] = sprintf( __( 'Error. Extension of file does not match allowed file types of %s.', 'wp-courseware' ), implode( ', ', $extensionTypes ) );
						continue;
					}
					$newFilename = $keyName . '_user_' . get_current_user_id() . '.' . $thisFileExtension;
					if ( move_uploaded_file( $file_tmp, $userPathDetails['dir_path'] . $newFilename ) !== false ) {
						$results['upload_valid'][ $qID ] = $userPathDetails['path_only'] . $newFilename;
					} 
					else {
						$results['upload_errors'][ $qID ] = __( 'Error. Could not move file to your training directory.', 'wp-courseware' );
						continue;
					}
				} 
				else {
					$results['upload_missing'][ $qID ] = true;
				}
			} 
			return $results;
		}
public static function validateFiles ( $ fileList , $ quizDetails ) { $ questionsWithUploads = array ( ) ; foreach ( $ quizDetails -> questions as $ qID => $ qObj ) { if ( 'upload' == $ qObj -> question_type ) { $ questionsWithUploads [ $ qID ] = $ qObj ; } } if ( count ( $ questionsWithUploads ) == 0 ) { return false ; } $ userPathDetails = WPCW_files_getFileUploadDirectory_forUser ( $ quizDetails , get_current_user_id ( ) ) ; $ results = array ( 'upload_errors' => array ( ) , 'upload_missing' => array ( ) , 'upload_valid' => array ( ) , ) ; foreach ( $ questionsWithUploads as $ qID => $ qObj ) { $ keyName = sprintf ( 'question_%d_upload_%d' , $ quizDetails -> quiz_id , $ qID ) ; if ( isset ( $ fileList [ $ keyName ] ) ) { $ file_name = $ fileList [ $ keyName ] [ 'name' ] ; $ file_tmp = $ fileList [ $ keyName ] [ 'tmp_name' ] ; $ file_error = $ fileList [ $ keyName ] [ 'error' ] ; $ file_size = $ fileList [ $ keyName ] [ 'size' ] ; if ( $ file_error > 0 ) { $ errMsg = __ ( 'Error. An unknown file upload error occurred.' , 'wp-courseware' ) ; switch ( $ file_error ) { case UPLOAD_ERR_FORM_SIZE : case UPLOAD_ERR_INI_SIZE : $ errMsg = sprintf ( __ ( 'Error. The uploaded file exceeds the maximum file upload size (%s).' , 'wp-courseware' ) , WPCW_files_getMaxUploadSize ( ) ) ; break ; case UPLOAD_ERR_PARTIAL : $ errMsg = __ ( 'Error. The uploaded file was only partially uploaded.' , 'wp-courseware' ) ; break ; case UPLOAD_ERR_NO_FILE : $ errMsg = __ ( 'Error. No file was uploaded.' , 'wp-courseware' ) ; break ; case UPLOAD_ERR_NO_TMP_DIR : $ errMsg = __ ( 'Error. The temporary upload directory does not exist.' , 'wp-courseware' ) ; break ; case UPLOAD_ERR_CANT_WRITE : $ errMsg = __ ( 'Error. Could not write the uploaded file to disk.' , 'wp-courseware' ) ; break ; case UPLOAD_ERR_EXTENSION : $ errMsg = __ ( 'Error. An extension stopped the file upload.' , 'wp-courseware' ) ; break ; } $ results [ 'upload_errors' ] [ $ qID ] = $ errMsg ; continue ; } $ extensionTypes = WPCW_files_cleanFileExtensionList ( $ qObj -> question_answer_file_types ) ; $ thisFileExtension = pathinfo ( $ file_name , PATHINFO_EXTENSION ) ; if ( ! in_array ( strtolower ( $ thisFileExtension ) , $ extensionTypes ) ) { $ results [ 'upload_errors' ] [ $ qID ] = sprintf ( __ ( 'Error. Extension of file does not match allowed file types of %s.' , 'wp-courseware' ) , implode ( ', ' , $ extensionTypes ) ) ; continue ; } $ newFilename = $ keyName . '_user_' . get_current_user_id ( ) . '.' . $ thisFileExtension ; if ( move_uploaded_file ( $ file_tmp , $ userPathDetails [ 'dir_path' ] . $ newFilename ) !== false ) { $ results [ 'upload_valid' ] [ $ qID ] = $ userPathDetails [ 'path_only' ] . $ newFilename ; } else { $ results [ 'upload_errors' ] [ $ qID ] = __ ( 'Error. Could not move file to your training directory.' , 'wp-courseware' ) ; continue ; } } else { $ results [ 'upload_missing' ] [ $ qID ] = true ; } } return $ results ; }
Validate the files that have been uploaded, checking them against the conditions of the quiz details.
Validate the files that have been uploaded checking them against the conditions of the quiz details .
// Assume that we have quiz details at this point. // Get a list of the questions that are expecting files. // No questions to check for. // Generate a unique path for the file uploads that uses the user's private directory. // Prepare results data // Check for each expected upload file that's in the list of questions // and do a little more validation (and handle moving the file too). // Generate the name of the file key to check e.g. question_16_upload_73 // File was found, so need to some further checks to make sure the extension is valid // and then we can move the file to the right place. // Uploaded file details // Got a PHP upload error?
 // Store error and don't process file further // Check the valid file extensions // File extension is not valid, so abort and move to next file. // Move file to the new location, which is USERPATH/question_16_upload_73_user_4.ext so that we can ensure we have // completely safe URL for the file. And the naming convention helps the admin to a certain degree. // Store relative path of file as being a valid upload. // Could not move file - might be out of space, or a write error. // Keep track of files that are missing. // end check of question in file list. // end foreach