/**
	 * Commit a gift wrapping type to the database (either create a new one or update an existing one)
	 *
	 * @param array An array of data about the gift wrapping type.
	 * @param int If updating an existing wrap, the ID.
	 * @return boolean True if successful, false if not.
	 */
	private function CommitWrap($data, $wrapId=0)
	{
		if(!isset($data['wrapvisible'])) {
			$data['wrapvisible'] = 0;
		}

		if(!isset($data['wrapallowcomments'])) {
			$data['wrapallowcomments'] = '';
		}

		// image validation is performed in ValidateWrap
		$files = ISC_UPLOADHANDLER::getUploadedFiles();
		foreach ($files as /** @var UploadHandlerFile */$file) {
			if ($file->fieldName == 'wrapimage') {
				if ($file->getIsMoved()) {
					// only save if file was moved by ValidateWrap
					$data['wrappreview'] = str_replace(ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/', '', $file->getMovedDestination());
				}
				break;
			}
		}

		$wrapData = array(
			'wrapname' => $data['wrapname'],
			'wrapprice' => DefaultPriceFormat($data['wrapprice']),
			'wrapvisible' => (int)$data['wrapvisible'],
			'wrapallowcomments' => (int)$data['wrapallowcomments'],
		);

		if(isset($data['wrappreview'])) {
			$wrapData['wrappreview'] = $data['wrappreview'];
		}

		if($wrapId == 0) {
			$wrapId = $GLOBALS['ISC_CLASS_DB']->InsertQuery('gift_wrapping', $wrapData);
		}
		else {
			$GLOBALS['ISC_CLASS_DB']->UpdateQuery('gift_wrapping', $wrapData, "wrapid='".(int)$wrapId."'");
		}

		$GLOBALS['ISC_CLASS_DATA_STORE']->UpdateGiftWrapping();

		// Couldn't save? return an error message
		if($GLOBALS['ISC_CLASS_DB']->GetErrorMsg()) {
			return false;
		}

		return true;
	}
Example #2
0
	/**
	* Handler for accepting a new product image via browser upload
	*
	* @param ISC_ADMIN_REMOTE $remote
	*/
	public function remoteNewImageUpload(ISC_ADMIN_REMOTE $remote)
	{
		$response = array(
			'error' => false,
			'files' => array(),
		);

		$productId = false;
		$productHash = false;

		if (isset($_REQUEST['product'])) {
			$productId = (int)@$_REQUEST['product'];
			if (!isId($productId) || !ProductExists($productId)) {
				$response['error'] = GetLang('ProductDoesntExist');
				die(isc_json_encode($response));
			}
			if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Edit_Products)) {
				$response['error'] = GetLang('Unauthorized');
				die(isc_json_encode($response));
			}
		} else if (isset($_REQUEST['hash']) && $_REQUEST['hash']) {
			$productHash = $_REQUEST['hash'];
			if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Create_Product)) {
				$response['error'] = GetLang('Unauthorized');
				die(isc_json_encode($response));
			}
			if (!$GLOBALS["ISC_CLASS_ADMIN_AUTH"]->HasPermission(AUTH_Create_Product)) {
				$response['error'] = GetLang('Unauthorized');
				die(isc_json_encode($response));
			}
		} else {
			$response['error'] = GetLang('ProductDoesntExist');
			die(isc_json_encode($response));
		}

		try {
			ISC_UPLOADHANDLER::processUploads();

			$files = ISC_UPLOADHANDLER::getAllFiles();

			foreach ($files as $file) {
				// each $file is instance of UploadHandlerFile

				$responseFile = array(
					'fieldName' => $file->fieldName,
					'name' => $file->name,
					'error' => false,
				);

				$response['files'][] = &$responseFile;

				// check if the individual image was uploaded correctly
				if (!$file->getSuccess()) {
					$responseFile['error'] = $file->getErrorMessage();
					continue;
				}

				// move the image out of php's tmp directory so functions that aren't exempt from open_basedir restrictions can access it
				while (true) {
					$temporaryPath = ISC_CACHE_DIRECTORY . 'productimage_' . ISC_PRODUCT_IMAGE::randomString(16) . '.' . $file->getExtension();

					if (!file_exists($temporaryPath)) {
						break;
					}
				}

				try {
					$file->moveAs($temporaryPath);
				} catch (UploadHandlerFileMoveNotWritableException $exception) {
					$responseFile['error'] = $exception->getMessage();
					continue;
				}

				try {
					if ($productHash) {
						$image = ISC_PRODUCT_IMAGE::importImage($temporaryPath, $file->name, $productHash, true);
					} else {
						$image = ISC_PRODUCT_IMAGE::importImage($temporaryPath, $file->name, $productId);
					}
				} catch (ISC_PRODUCT_IMAGE_IMPORT_EXCEPTION $exception) {
					// these exceptions should have language-powered messages so are safe to return to the user
					$responseFile['error'] = $exception->getMessage();
					@unlink($temporaryPath);
					continue;
				} catch (Exception $exception) {
					// other unknown error
					$responseFile['error'] = GetLang('ProductImageProcessUnknownError');
					@unlink($temporaryPath);
					continue;
				}

				try {
					$preview = $image->getResizedUrl(ISC_PRODUCT_IMAGE_SIZE_THUMBNAIL, true);
					$zoom = $image->getResizedUrl(ISC_PRODUCT_IMAGE_SIZE_ZOOM, true);
				} catch (ISC_PRODUCT_IMAGE_SOURCEFILEDOESNTEXIST_EXCEPTION $exception) {
					$preview = false;
					$zoom = false;
				}

				// these field names should match the constructor of the javascript ProductImages.Image object, see /admin/script/product.images.js, or search for "ProductImages.Image = function" if it gets moved
				// not all fields are mandatory though
				$responseFile['id'] = $image->getProductImageId();
				$responseFile['product'] = $image->getProductId();
				$responseFile['hash'] = $image->getProductHash();
				$responseFile['preview'] = $preview;
				$responseFile['zoom'] = $zoom;
				$responseFile['description'] = $image->getDescription();
				$responseFile['baseThumbnail'] = $image->getIsThumbnail();
				$responseFile['sort'] = $image->getSort();
			}

		} catch (UploadHandlerProcessNoInputException $ex) {
			$response['error'] = $ex->getMessage();

		} catch (UploadHandlerProcessPostSizeException $ex) {
			$response['error'] = $ex->getMessage();

		} catch (Exception $ex) {
			$response['error'] = 'Unhandled exception: ' . $ex->getMessage();

		}

		die(isc_json_encode($response));
	}