XUpload - Call existing PHP variables

Message
Author
Nicola
Posts: 3
Joined: Mar 06, 2012 9:11 pm

Call existing PHP variables

#1 Postby Nicola » Mar 06, 2012 9:24 pm

Hi
I want to integrate xupload pro into a existing PHP code

PHP code checks this:

-Maximum file size allowed
-Store uploaded files in dynamic folders
-Assign a unique id / name to each uploaded file
-Check the file extension

But there's no official function that is used to monitor the progress of file upload

Can you help me?

here is the php code:

Code: Select all

	private function uploadConfigurableFile($fieldOptions, $file)
	{
		if (!is_array($fieldOptions) || !is_array($file) || empty($file['name'])) {
			return false;
		}

		$extension = GetFileExtension($file['name']);
		$allowedExtensions = array_map('trim', explode(',', $fieldOptions['fieldfiletype']));
		$allowedExtensions = array_map('isc_strtolower', $allowedExtensions);
		if ($fieldOptions['fieldfiletype'] && !in_array(isc_strtolower($extension), $allowedExtensions)) {
			throw new ISC_QUOTE_EXCEPTION(sprintf(GetLang('InvalidFileType'), isc_html_escape($fieldOptions['fieldfiletype'])));
		}

		// Check that the maximum size is not exceeded
		if ($fieldOptions['fieldfilesize'] > 0 && $file['size'] > $fieldOptions['fieldfilesize']*1024) {
			throw new ISC_QUOTE_EXCEPTION(sprintf(GetLang('InvalidFileSize'), $fieldOptions['fieldfilesize']));
		}

		// Store the uploaded files in our configured products directory
		$uploadDirectory = ISC_BASE_PATH.'/'.GetConfig('ImageDirectory').'/configured_products_tmp/';
		if (empty($file['existingPath'])) {
			/**
			 * @todo Implement temporary sharing/storage location with automatic pruning.
			 */
			$fileName = $fieldOptions['productfieldid'].'_'.md5(uniqid()).'.'.$extension;
			if (!move_uploaded_file($file['tmp_name'], $uploadDirectory.$fileName)) {
				throw new ISC_QUOTE_EXCEPTION(getLang('CanNotUploadFile'));
			}
		}
		else {
			$fileName = basename($file['existingPath']);
		}

		// If we've just uploaded an image, we need to perform a bit of additional validation
		// to ensure it's not someone uploading bogus images.
		$imageExtensions = array(
			'gif',
			'png',
			'jpg',
			'jpeg',
			'jpe',
			'tiff',
			'bmp'
		);
		if (in_array($extension, $imageExtensions)) {
			// Check a list of known MIME types to establish the type of image we're uploading
			switch(isc_strtolower($file['type'])) {
				case 'image/gif':
					$imageType = IMAGETYPE_GIF;
					break;
				case 'image/jpg':
				case 'image/x-jpeg':
				case 'image/x-jpg':
				case 'image/jpeg':
				case 'image/pjpeg':
				case 'image/jpg':
					$imageType = IMAGETYPE_JPEG;
					break;
				case 'image/png':
				case 'image/x-png':
					$imageType = IMAGETYPE_PNG;
					break;
				case 'image/bmp':
					$imageType = IMAGETYPE_BMP;
					break;
				case 'image/tiff':
					$imageType = IMAGETYPE_TIFF_II;
					break;
				default:
					$imageType = 0;
			}

			$imageDimensions = getimagesize($uploadDirectory.$fileName);
			if (!is_array($imageDimensions) || $imageDimensions[2] != $imageType) {
				@unlink($uploadDirectory.$fileName);
				throw new ISC_QUOTE_EXCEPTION(GetLang('InvalidImageFile'));
				return false;
			}
		}

		return $fileName;
	}
	public function applyConfiguration($configuration)
	{
		$configurableFields = $this->getConfigurableOptions();

		// Get the current configuration
		$existingConfiguration = $this->configuration;

		$newConfiguration = array();

		foreach ($configurableFields as $fieldId => $field) {
			if ($field['fieldrequired'] && empty($configuration[$fieldId]) &&
				empty($existingConfiguration[$fieldId])) {
					throw new ISC_QUOTE_EXCEPTION(getLang('EnterRequiredField'));
			}

			if ($field['fieldtype'] == 'file') {
				// Field was empty, use existing value
				if (empty($configuration[$fieldId]['name']) && !empty($existingConfiguration[$fieldId])) {
					$newConfiguration[$fieldId] = $existingConfiguration[$fieldId];
					continue;
				}
				else if(empty($configuration[$fieldId]['name'])) {
					continue;
				}

				// If we can't save the uploaded file, throw back an error
				$uploadedFile = $this->uploadConfigurableFile($field, $configuration[$fieldId]);
				if ($uploadedFile === false) {
					throw new ISC_QUOTE_EXCEPTION(getLang('CanNotUploadFile'));
				}

				// If there was an existing file, delete it
				if (isset($existingConfiguration[$fieldId]['value']) &&
					$existingConfiguration[$fieldId]['value'] != $uploadedFile) {
						$this->deleteConfigurableFile($fieldId, true);
				}

				$newConfiguration[$fieldId] = array(
					'type'				=> $field['fieldtype'],
					'name'				=> $field['fieldname'],
					'fileType'			=> $configuration[$fieldId]['type'],
					'fileOriginalName'	=> $configuration[$fieldId]['name'],
					'value'				=> $uploadedFile,
				);
				continue;
			}
			elseif ($field['fieldtype'] == 'select') {
				$newConfiguration[$fieldId] = array(
					'type'				=> $field['fieldtype'],
					'name'				=> $field['fieldname'],
					'selectOptions'		=> $field['fieldselectoptions'],
					'value'				=> $configuration[$fieldId],
				);
				continue;
			}

			if (!isset($configuration[$fieldId])) {
				continue;
			}

			$value = $configuration[$fieldId];
			$newConfiguration[$fieldId] = array(
				'type'	=> $field['fieldtype'],
				'name'	=> $field['fieldname'],
				'value'	=> $configuration[$fieldId],
			);
		}

		// Store the finalized configuration for this product
		$this->setConfiguration($newConfiguration);
		return $this;
	}

Nicola
Posts: 3
Joined: Mar 06, 2012 9:11 pm

#2 Postby Nicola » Mar 07, 2012 8:18 am

I searched in Google and found this:

In PHP file:

Code: Select all

exec('./perl_script.pl foo=bar');
Or add a space between the command and the variable. If the variable contains spaces, you also need to use the escapeshellarg function:

Code: Select all

exec('./perl_script.pl'.' '.EscapeShellArg($variable));
In Perl file (use the -s switch):

Code: Select all

#!/usr/bin/perl -s
use vars ($foo);

if $foo do_something;
Is It the way for the solution?

Another suggestion found is:

Code: Select all

#!/usr/bin/perl

print $ARGV[0];

admin
Site Admin
Posts: 1839
Joined: Mar 22, 2006 12:32 pm

#3 Postby admin » Mar 07, 2012 10:53 am

I can't imagine why you would need to call perl script from php script.

I think you're getting whole upload conception the wrong way.
XUpload uploading file by itself, then after upload it's sending POST request to your php script and sending information about uploaded files (location, size, etc).

Nicola
Posts: 3
Joined: Mar 06, 2012 9:11 pm

#4 Postby Nicola » Mar 07, 2012 11:02 am

Hi admin
Thank you for the response

I'm very confuse: the various informations are sending by post.cgi code?

Then the post.cgi output must be imported in php variables?

Is it possible to change the function "private function uploadConfigurableFile" in the php file with a function that take the post.cgi data?

Or is it possible to send the parameters from php code to XUploadConfig.pm?

admin
Site Admin
Posts: 1839
Joined: Mar 22, 2006 12:32 pm

#5 Postby admin » Mar 07, 2012 12:08 pm

post.cgi is just the sample script which print all data posted by xupload to your script.
You should put your php script url in the url_post parameter in config file, so script will redirect user to your script after upload.