예제 #1
0
파일: image.c 프로젝트: googlle/cphalcon7
PHP_METHOD(Phalcon_Image, factory)
{
	zval *file, *width = NULL, *height = NULL;

	phalcon_fetch_params(0, 1, 2, &file, &width, &height);

	if (!width) {
		width = &PHALCON_GLOBAL(z_null);
	}

	if (!height) {
		height = &PHALCON_GLOBAL(z_null);
	}

	if (phalcon_class_str_exists(SL("imagick"), 0) != NULL) {
		object_init_ex(return_value, phalcon_image_adapter_imagick_ce);
		PHALCON_CALL_METHODW(NULL, return_value, "__construct", file, width, height);
	} else {
		object_init_ex(return_value, phalcon_image_adapter_gd_ce);
		PHALCON_CALL_METHODW(NULL, return_value, "__construct", file, width, height);
	}
}
예제 #2
0
파일: file.c 프로젝트: dreamsxin/cphalcon7
/**
 * Executes the validation
 *
 * @param string $file
 * @param int $minsize
 * @param int $maxsize
 * @param array $mimes
 * @param int $minwidth
 * @param int $maxwidth
 * @param int $minheight
 * @param int $maxheight
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_File, valid)
{
	zval *value, *minsize = NULL, *maxsize = NULL, *mimes = NULL, *minwidth = NULL, *maxwidth = NULL, *minheight = NULL, *maxheight = NULL;
	zval file = {}, size = {}, *constant, finfo = {}, pathname = {}, mime = {}, image = {}, imageinfo = {}, width = {}, height = {}, valid = {};
	zend_class_entry *imagick_ce;

	phalcon_fetch_params(0, 1, 7, &value, &minsize, &maxsize, &mimes, &minwidth, &maxwidth, &minheight, &maxheight);

	if (Z_TYPE_P(value) == IS_STRING) {
		object_init_ex(&file, spl_ce_SplFileInfo);
		if (phalcon_has_constructor(&file)) {
			PHALCON_CALL_METHODW(NULL, &file, "__construct", value);
		}
	} else if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function_ex(Z_OBJCE_P(value), spl_ce_SplFileInfo, 0)) {
		PHALCON_CPY_WRT(&file, value);
	}

	if (Z_TYPE(file) <= IS_NULL) {
		phalcon_update_property_str(getThis(), SL("_type"), SL("TypeUnknow"));
		RETURN_FALSE;
	}

	if (!minsize) {
		minsize = &PHALCON_GLOBAL(z_null);
	}

	if (!maxsize) {
		maxsize = &PHALCON_GLOBAL(z_null);
	}

	if (!mimes) {
		mimes = &PHALCON_GLOBAL(z_null);
	}

	if (!minwidth) {
		minwidth = &PHALCON_GLOBAL(z_null);
	}

	if (!maxwidth) {
		maxwidth = &PHALCON_GLOBAL(z_null);
	}

	if (!minheight) {
		minheight = &PHALCON_GLOBAL(z_null);
	}

	if (!maxheight) {
		maxheight = &PHALCON_GLOBAL(z_null);
	}

	PHALCON_CALL_METHODW(&valid, &file, "isfile");

	if (!zend_is_true(&valid)) {
		phalcon_update_property_str(getThis(), SL("_type"), SL("FileValid"));
		RETURN_FALSE;
	}

	PHALCON_CALL_METHODW(&size, &file, "getsize");

	if (!PHALCON_IS_EMPTY(minsize)) {
		is_smaller_or_equal_function(&valid, minsize, &size);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooSmall"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxsize)) {
		is_smaller_or_equal_function(&valid, &size, maxsize);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooLarge"));
			RETURN_FALSE;
		}
	}

	PHALCON_CALL_METHODW(&pathname, &file, "getpathname");

	if (Z_TYPE_P(mimes) == IS_ARRAY) {
		if ((constant = zend_get_constant_str(SL("FILEINFO_MIME_TYPE"))) == NULL) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Undefined constant `FILEINFO_MIME_TYPE`");
			return;
		}

		PHALCON_CALL_FUNCTIONW(&finfo, "finfo_open", constant);

		if (Z_TYPE(finfo) != IS_RESOURCE) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_validation_exception_ce, "Opening fileinfo database failed");
			return;
		}		

		PHALCON_CALL_FUNCTIONW(&mime, "finfo_file", &finfo, &pathname);
		PHALCON_CALL_FUNCTIONW(NULL, "finfo_close", &finfo);
		
		if (!phalcon_fast_in_array(&mime, mimes)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("MimeValid"));
			RETURN_FALSE;
		}
	}

	if (phalcon_class_str_exists(SL("imagick"), 0) != NULL) {
		imagick_ce = phalcon_fetch_str_class(SL("Imagick"), ZEND_FETCH_CLASS_AUTO);

		object_init_ex(&image, imagick_ce);
		PHALCON_CALL_METHODW(NULL, &image, "__construct", &pathname);

		PHALCON_CALL_METHODW(&width, &image, "getImageWidth");
		PHALCON_CALL_METHODW(&height, &image, "getImageHeight");
	} else if (phalcon_function_exists_ex(SL("getimagesize")) != FAILURE) {
		PHALCON_CALL_FUNCTIONW(&imageinfo, "getimagesize", &pathname);
		if (!phalcon_array_isset_fetch_long(&width, &imageinfo, 0)) {
			ZVAL_LONG(&width, -1);
		}

		if (!phalcon_array_isset_fetch_long(&height, &imageinfo, 1)) {
			ZVAL_LONG(&height, -1);
		}
	} else {
		ZVAL_LONG(&width, -1);
		ZVAL_LONG(&height, -1);
	}

	if (!PHALCON_IS_EMPTY(minwidth)) {
		is_smaller_or_equal_function(&valid, minwidth, &width);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooNarrow"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxwidth)) {
		is_smaller_or_equal_function(&valid, &width, maxwidth);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooWide"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(minheight)) {
		is_smaller_or_equal_function(&valid, minheight, &height);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooShort"));
			RETURN_FALSE;
		}
	}

	if (!PHALCON_IS_EMPTY(maxheight)) {
		is_smaller_or_equal_function(&valid, &height, maxheight);
		if (!zend_is_true(&valid)) {
			phalcon_update_property_str(getThis(), SL("_type"), SL("TooLong"));
			RETURN_FALSE;
		}
	}

	RETURN_TRUE;
}