Пример #1
0
void test_phalcon_cpy_wrt_ctor(void)
{
	startup_php(__func__);
	zend_first_try {
		zval* dest;
		zval* src;

		PHALCON_MM_GROW();
		/* dest is not observed by Phalcon */
			MAKE_STD_ZVAL(dest);
			ZVAL_STRING(dest, "R^itaM cha svAdhyAyapravachane cha", 1);

			PHALCON_INIT_VAR(src);
			ZVAL_STRING(src, "satyaM cha svAdhyAyapravachane cha", 1);

			PHALCON_CPY_WRT_CTOR(dest, src);

			CU_ASSERT_PTR_NOT_EQUAL(dest, src);
			CU_ASSERT_EQUAL(Z_REFCOUNT_P(src), 1);
			CU_ASSERT_EQUAL(Z_REFCOUNT_P(dest), 1);
			CU_ASSERT_EQUAL(Z_TYPE_P(src), IS_STRING);
			CU_ASSERT_EQUAL(Z_ISREF_P(src), 0);
			CU_ASSERT_EQUAL(Z_ISREF_P(dest), 0);
		PHALCON_MM_RESTORE();

		CU_ASSERT_EQUAL(_mem_block_check(dest, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC), 1);
		CU_ASSERT_PTR_NOT_EQUAL(dest, src);
		CU_ASSERT_EQUAL(Z_REFCOUNT_P(dest), 1);
		CU_ASSERT_STRING_EQUAL(Z_STRVAL_P(dest), "satyaM cha svAdhyAyapravachane cha");
		zval_ptr_dtor(&dest);

		PHALCON_MM_GROW();
		/* dest will be observed by Phalcon */
			dest = NULL;

			PHALCON_INIT_VAR(src);
			ZVAL_STRING(src, "satyaM cha svAdhyAyapravachane cha", 1);

			PHALCON_CPY_WRT_CTOR(dest, src);

			CU_ASSERT_PTR_NOT_EQUAL(dest, src);
			CU_ASSERT_EQUAL(Z_REFCOUNT_P(src), 1);
			CU_ASSERT_EQUAL(Z_REFCOUNT_P(dest), 1);
			CU_ASSERT_EQUAL(Z_TYPE_P(src), IS_STRING);
			CU_ASSERT_EQUAL(Z_ISREF_P(src), 0);
			CU_ASSERT_EQUAL(Z_ISREF_P(dest), 0);
		PHALCON_MM_RESTORE();
		/* At this point dest will be destroyed */

	}
	zend_catch {
		CU_ASSERT(0);
	}
	zend_end_try();

	shutdown_php();
	CU_ASSERT_EQUAL(leaks, 0);
}
Пример #2
0
/**
 * Execute a rotation.
 *
 * @param int $degrees
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _rotate) {

	zval *_degrees, degrees = {}, image = {}, tmp_image = {}, color = {}, alpha = {}, transparent = {}, ignore_transparent = {}, saveflag = {}, w = {}, h = {};
	int tmp_degrees;

	phalcon_fetch_params(0, 1, 0, &_degrees);
	PHALCON_CPY_WRT_CTOR(&degrees, _degrees);

	ZVAL_LONG(&color, 0);
	ZVAL_LONG(&alpha, 127);

	phalcon_return_property(&image, getThis(), SL("_image"));

	PHALCON_CALL_FUNCTIONW(&transparent, "imagecolorallocatealpha", &image, &color, &color, &color, &alpha);

	tmp_degrees = phalcon_get_intval(&degrees);

	ZVAL_LONG(&degrees, 360 - tmp_degrees);
	ZVAL_LONG(&ignore_transparent, 1);

	PHALCON_CALL_FUNCTIONW(&tmp_image, "imagerotate", &image, &degrees, &transparent, &ignore_transparent);

	ZVAL_TRUE(&saveflag);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &tmp_image, &saveflag);
	PHALCON_CALL_FUNCTIONW(&w, "imagesx", &tmp_image);
	PHALCON_CALL_FUNCTIONW(&h, "imagesy", &tmp_image);
	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	phalcon_update_property_zval(getThis(), SL("_image"), &tmp_image);
	phalcon_update_property_zval(getThis(), SL("_width"), &w);
	phalcon_update_property_zval(getThis(), SL("_height"), &h);
}
Пример #3
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Yaml, read){

	zval *file_path, *absolute_path = NULL, config_dir_path = {}, *base_path = NULL, config = {};

	phalcon_fetch_params(0, 1, 1, &file_path, &absolute_path);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (absolute_path == NULL) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path);
	} else {
		base_path = phalcon_read_static_property_ce(phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, base_path, file_path);
	}

	PHALCON_CALL_FUNCTIONW(&config, "yaml_parse_file", &config_dir_path);

	if (Z_TYPE(config) == IS_ARRAY) {
		PHALCON_CALL_METHODW(NULL, getThis(), "val", &config);
	}

	RETURN_THISW();
}
Пример #4
0
/**
 * Decrypt a text that is coded as a base64 string
 *
 * @param string $text
 * @param string $key
 * @return string
 */
PHP_METHOD(Phalcon_Crypt, decryptBase64){

	zval *text, *key = NULL, *safe = NULL, decrypt_text = {}, decrypt_value = {};

	phalcon_fetch_params(0, 1, 2, &text, &key, &safe);
	PHALCON_ENSURE_IS_STRING(text);

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

	if (!safe) {
		safe = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(safe)) {
		ZVAL_NEW_STR(&decrypt_text, zend_string_dup(Z_STR_P(text), 0));
		php_strtr(Z_STRVAL(decrypt_text), Z_STRLEN(decrypt_text), "-_", "+/", 2);
	} else {
		PHALCON_CPY_WRT_CTOR(&decrypt_text, text);
	}

	phalcon_base64_decode(&decrypt_value, &decrypt_text);

	PHALCON_RETURN_CALL_METHODW(getThis(), "decrypt", &decrypt_value, key);
}
Пример #5
0
/**
 * Add a reflection to an image. The most opaque part of the reflection
 * will be equal to the opacity setting and fade out to full transparent.
 * Alpha transparency is preserved.
 *
 * @param int $height reflection height
 * @param int $opacity reflection opacity: 0-100
 * @param boolean $fade_in TRUE to fade in, FALSE to fade out
 * @return Phalcon\Image\Adapter
 */
PHP_METHOD(Phalcon_Image_Adapter, reflection) {

    zval **h = NULL, **op = NULL, **fade_in = NULL;
    zval *image_height, *height = NULL, *opacity = NULL;
    long tmp_image_height;

    phalcon_fetch_params_ex(0, 3, &h, &op, &fade_in);

    PHALCON_MM_GROW();

    image_height     = phalcon_fetch_nproperty_this(this_ptr, SL("_height"), PH_NOISY TSRMLS_CC);
    tmp_image_height = phalcon_get_intval(image_height);

    if (!h || Z_TYPE_PP(h) != IS_LONG || Z_LVAL_PP(h) > tmp_image_height) {
        PHALCON_INIT_VAR(height);
        ZVAL_LONG(height, tmp_image_height);
    } else {
        PHALCON_CPY_WRT_CTOR(height, *h);
    }

    if (!op) {
        PHALCON_INIT_VAR(opacity);
        ZVAL_LONG(opacity, 100);
    } else {
        PHALCON_ENSURE_IS_LONG(op);

        if (Z_LVAL_PP(op) > 100) {
            PHALCON_INIT_VAR(opacity);
            ZVAL_LONG(opacity, 100);
        } else if (Z_LVAL_PP(op) < 0) {
            PHALCON_INIT_VAR(opacity);
            ZVAL_LONG(opacity, 0);
        } else {
            PHALCON_CPY_WRT_CTOR(opacity, *op);
        }
    }

    if (!fade_in) {
        fade_in = &PHALCON_GLOBAL(z_false);
    }

    PHALCON_CALL_METHOD(NULL, this_ptr, "_reflection", height, opacity, *fade_in);
    RETURN_THIS();
}
Пример #6
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_Between, validate){

	zval *validator, *attribute, value = {}, allow_empty = {}, minimum = {}, maximum = {}, label = {}, pairs = {}, valid = {}, message_str = {}, code = {}, prepared = {}, message = {};
	zend_class_entry *ce = Z_OBJCE_P(getThis());

	phalcon_fetch_params(0, 2, 0, &validator, &attribute);

	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 0);

	PHALCON_CALL_METHODW(&value, validator, "getvalue", attribute);

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&allow_empty, ce, getThis(), ISV(allowEmpty)));
	if (zend_is_true(&allow_empty) && phalcon_validation_validator_isempty_helper(&value)) {
		RETURN_TRUE;
	}

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minimum, ce, getThis(), "minimum"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maximum, ce, getThis(), "maximum"));

	PHALCON_CALL_SELFW(&valid, "valid", &value, &minimum, &maximum);

	if (PHALCON_IS_FALSE(&valid)) {
		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&label, ce, getThis(), ISV(label)));
		if (!zend_is_true(&label)) {
			PHALCON_CALL_METHODW(&label, validator, "getlabel", attribute);
			if (!zend_is_true(&label)) {
				PHALCON_CPY_WRT_CTOR(&label, attribute);
			}
		}

		array_init_size(&pairs, 3);
		phalcon_array_update_str(&pairs, SL(":field"), &label, PH_COPY);
		phalcon_array_update_str(&pairs, SL(":min"), &minimum, PH_COPY);
		phalcon_array_update_str(&pairs, SL(":max"), &maximum, PH_COPY);

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
		if (!zend_is_true(&message_str)) {
			RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "Between"));
		}

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&code, ce, getThis(), ISV(code)));
		if (Z_TYPE_P(&code) == IS_NULL) {
			ZVAL_LONG(&code, 0);
		}

		PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

		phalcon_validation_message_construct_helper(&message, &prepared, attribute, "Between", &code);

		PHALCON_CALL_METHODW(NULL, validator, "appendmessage", &message);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
Пример #7
0
/**
 * Phalcon\Mvc\Router\Route constructor
 *
 * @param string $pattern
 * @param array $paths
 * @param array|string $httpMethods
 */
PHP_METHOD(Phalcon_Mvc_Router_Route, __construct){

	zval *pattern, *paths = NULL, *http_methods = NULL, *unique_id = NULL;
	zval *route_id = NULL;
	int separate = 0;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &pattern, &paths, &http_methods);
	
	if (!paths) {
		paths = PHALCON_GLOBAL(z_null);
	}
	
	if (!http_methods) {
		http_methods = PHALCON_GLOBAL(z_null);
	}
	
	/** 
	 * Configure the route (extract parameters, paths, etc)
	 */
	PHALCON_CALL_METHOD(NULL, this_ptr, "reconfigure", pattern, paths);
	
	/** 
	 * Update the HTTP method constraints
	 */
	phalcon_update_property_this(this_ptr, SL("_methods"), http_methods TSRMLS_CC);
	
	/** 
	 * Get the unique Id from the static member _uniqueId
	 */
	unique_id = phalcon_fetch_static_property_ce(phalcon_mvc_router_route_ce, SL("_uniqueId") TSRMLS_CC);
	if (Z_REFCOUNT_P(unique_id) > 1) {
		PHALCON_INIT_VAR(unique_id);
		separate = 1;
	}

	if (Z_TYPE_P(unique_id) == IS_NULL) {
		ZVAL_LONG(unique_id, 0);
	}
	
	PHALCON_CPY_WRT_CTOR(route_id, unique_id); /* route_id is now separated from unique_id */
	phalcon_update_property_this(this_ptr, SL("_id"), route_id TSRMLS_CC);
	
	/* increment_function() will increment the value of the static property as well */
	increment_function(unique_id);
	if (separate) {
		phalcon_update_static_property_ce(phalcon_mvc_router_route_ce, SL("_uniqueId"), unique_id TSRMLS_CC);
	}
	
	PHALCON_MM_RESTORE();
}
Пример #8
0
/**
 * Internal get wrapper to filter
 *
 * @param string $name
 * @param string|array $filters
 * @param mixed $defaultValue
 * @param boolean $notAllowEmpty
 * @param boolean $noRecursive
 * @return mixed
 */
PHP_METHOD(Phalcon_Http_Request, _get)
{
	zval *data, *name, *filters, *default_value, *not_allow_empty, *norecursive;
	zval value = {}, dependency_injector = {}, service = {}, filter = {}, filter_value = {};

	phalcon_fetch_params(0, 6, 0, &data, &name, &filters, &default_value, &not_allow_empty, &norecursive);

	if (Z_TYPE_P(name) != IS_NULL) {
		if (!phalcon_array_isset_fetch(&value, data, name, 0)) {
			RETURN_CTORW(default_value);
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&value, data);
	}

	if (Z_TYPE_P(filters) != IS_NULL) {
		phalcon_return_property(&filter, getThis(), SL("_filter"));
		if (Z_TYPE(filter) != IS_OBJECT) {
			PHALCON_CALL_METHODW(&dependency_injector, getThis(), "getdi");
			if (Z_TYPE(dependency_injector) != IS_OBJECT) {
				PHALCON_THROW_EXCEPTION_STRW(phalcon_http_request_exception_ce, "A dependency injection object is required to access the 'filter' service");
				return;
			}

			PHALCON_STR(&service, ISV(filter));

			PHALCON_CALL_METHODW(&filter, &dependency_injector, "getshared", &service);
			PHALCON_VERIFY_INTERFACEW(&filter, phalcon_filterinterface_ce);

			phalcon_update_property_zval(getThis(), SL("_filter"), &filter);
		}

		PHALCON_CALL_METHODW(&filter_value, &filter, "sanitize", &value, filters, norecursive);

		if ((PHALCON_IS_EMPTY(&filter_value) && zend_is_true(not_allow_empty)) || PHALCON_IS_FALSE(&filter_value)) {
			RETURN_CTORW(default_value);
		}

		RETURN_CTORW(&filter_value);
	}

	if (PHALCON_IS_EMPTY(&value) && zend_is_true(not_allow_empty)) {
		RETURN_CTORW(default_value);
	}

	RETURN_CTORW(&value);
}
Пример #9
0
/**
 * Checks if GD is enabled
 *
 * @return  boolean
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, check){

	zval gd_version = {}, ret = {}, gd_info = {}, version = {}, exception_message = {}, pattern = {}, matches = {};

	if (phalcon_function_exists_ex(SL("gd_info")) == FAILURE) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "GD is either not installed or not enabled, check your configuration");
		return;
	}

	if (!phalcon_get_constant(&gd_version, SL("GD_VERSION"))) {
		PHALCON_CALL_FUNCTIONW(&gd_info, "gd_info");

		if (phalcon_array_isset_fetch_str(&gd_version, &gd_info, SL("GD Version"))) {
			ZVAL_STRING(&pattern, "#\\d+\\.\\d+(?:\\.\\d+)?#");
			ZVAL_NULL(&matches);
			ZVAL_MAKE_REF(&matches);
			RETURN_ON_FAILURE(phalcon_preg_match(&ret, &pattern, &gd_version, &matches));
			ZVAL_UNREF(&matches);

			if (zend_is_true(&ret)) {
				if (!phalcon_array_isset_fetch_long(&version, &matches, 0)) {
					ZVAL_EMPTY_STRING(&version);
				}
			} else {
				ZVAL_EMPTY_STRING(&version);
			}
		} else {
			PHALCON_CPY_WRT_CTOR(&version, &gd_version);
		}
	}

	if (-1 == php_version_compare(Z_STRVAL_P(&gd_version), "2.0.1")) {
		PHALCON_CONCAT_SV(&exception_message, "Phalcon\\Image\\Adapter\\GD requires GD version '2.0.1' or greater, you have '", &gd_version);
		PHALCON_THROW_EXCEPTION_ZVALW(phalcon_image_exception_ce, &exception_message);
		return;
	}

	phalcon_update_static_property_ce(phalcon_image_adapter_gd_ce, SL("_checked"), &PHALCON_GLOBAL(z_true));

	RETURN_TRUE;
}
Пример #10
0
/**
 * Set the background color of an image. This is only useful for images
 * with alpha transparency.
 *
 * @param string $color hexadecimal color value
 * @param int $opacity background opacity: 0-100
 * @return Phalcon\Image\Adapter
 */
PHP_METHOD(Phalcon_Image_Adapter, background) {

    zval *color, *opacity = NULL;
    zval *tmp_color = NULL, *r = NULL, *g = NULL, *b = NULL;
    long i;
    char *c;
    zval tmp;

    PHALCON_MM_GROW();

    phalcon_fetch_params(1, 1, 1, &color, &opacity);

    if (Z_TYPE_P(color) != IS_STRING) {
        PHALCON_SEPARATE_PARAM(color);
        convert_to_string(color);
    }

    c = Z_STRVAL_P(color);

    if (Z_STRLEN_P(color) > 0 && c[0] == '#') {
        PHALCON_INIT_NVAR(tmp_color);
        phalcon_substr(tmp_color, color, 1, 0);
    } else {
        PHALCON_CPY_WRT_CTOR(tmp_color, color);
    }

    if (Z_STRLEN_P(tmp_color) == 3) {
        /* Convert RGB to RRGGBB */
        c = Z_STRVAL_P(tmp_color);
        if (!IS_INTERNED(c)) {
            STR_REALLOC(c, 7);
        }
        else {
            char* tmp = ecalloc(7, 1);
            memcpy(tmp, c, Z_STRLEN_P(tmp_color));
            c = tmp;
        }

        c[6] = '\0';
        c[5] = c[2];
        c[4] = c[2];
        c[3] = c[1];
        c[2] = c[1];
        c[1] = c[0];
        ZVAL_STRING(tmp_color, c, 0);
    }

    if (Z_STRLEN_P(tmp_color) < 6) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_image_exception_ce, "Color is not valid");
        return;
    }

    INIT_ZVAL(tmp);

    Z_TYPE(tmp) = IS_STRING;
    ZVAL_STRINGL(&tmp, Z_STRVAL_P(tmp_color), 2, 0);

    PHALCON_INIT_NVAR(r);
    _php_math_basetozval(&tmp, 16, r);

    Z_STRVAL(tmp) += 2;
    PHALCON_INIT_NVAR(g);
    _php_math_basetozval(&tmp, 16, g);

    Z_STRVAL(tmp) += 2;
    PHALCON_INIT_NVAR(b);
    _php_math_basetozval(&tmp, 16, b);

    if (!opacity) {
        PHALCON_INIT_NVAR(opacity);
        ZVAL_LONG(opacity, 100);
    } else {
        PHALCON_SEPARATE_PARAM(opacity);

        i = phalcon_get_intval(opacity);

        if (i < 1) {
            PHALCON_INIT_NVAR(opacity);
            ZVAL_LONG(opacity, 1);
        } else if (i > 100) {
            PHALCON_INIT_NVAR(opacity);
            ZVAL_LONG(opacity, 100);
        }
    }

    PHALCON_CALL_METHOD(NULL, this_ptr, "_background", r, g, b, opacity);

    RETURN_THIS();
}
Пример #11
0
/**
 * Add a text to an image with a specified opacity.
 *
 * @param string text
 * @param int $offset_x offset from the left, If less than 0 offset from the right, If true right the x offset
 * @param int $offset_y offset from the top, If less than 0 offset from the bottom, If true bottom the Y offset
 * @param int $opacity opacity of text: 1-100
 * @param string $color hexadecimal color value
 * @param int $size font pointsize
 * @param string $fontfile font path
 * @return Phalcon\Image\Adapter
 */
PHP_METHOD(Phalcon_Image_Adapter, text) {

    zval **text, **ofs_x = NULL, **ofs_y = NULL, **op = NULL, **fontcolor = NULL, **fontsize = NULL, **fontfile = NULL;
    zval *offset_x = NULL, *offset_y = NULL, *opacity, *color, *size;
    zval *r, *g, *b;
    char *c;
    zval tmp;

    phalcon_fetch_params_ex(1, 6, &text, &ofs_x, &ofs_y, &op, &fontcolor, &fontsize, &fontfile);

    PHALCON_MM_GROW();

    if (!ofs_x || Z_TYPE_PP(ofs_x) == IS_NULL) {
        PHALCON_INIT_VAR(offset_x);
        ZVAL_FALSE(offset_x);
    }
    else {
        PHALCON_CPY_WRT_CTOR(offset_x, *ofs_x);
    }

    if (!ofs_y || Z_TYPE_PP(ofs_y) == IS_NULL) {
        PHALCON_INIT_VAR(offset_y);
        ZVAL_FALSE(offset_y);
    }
    else {
        PHALCON_CPY_WRT_CTOR(offset_y, *ofs_y);
    }

    PHALCON_INIT_VAR(opacity);
    if (!op || Z_TYPE_PP(op) == IS_NULL) {
        ZVAL_LONG(opacity, 100);
    } else {
        PHALCON_ENSURE_IS_LONG(op);
        if (Z_LVAL_PP(op) < 1) {
            ZVAL_LONG(opacity, 1);
        } else if (Z_LVAL_PP(op) > 100) {
            ZVAL_LONG(opacity, 100);
        } else {
            ZVAL_LONG(opacity, Z_LVAL_PP(op));
        }
    }

    PHALCON_INIT_VAR(color);
    if (!fontcolor || Z_TYPE_PP(fontcolor) == IS_NULL) {
        ZVAL_STRING(color, "000000", 1);
    }
    else {
        PHALCON_ENSURE_IS_STRING(fontcolor);
        if (Z_STRLEN_PP(fontcolor) > 1 && Z_STRVAL_PP(fontcolor)[0] == '#') {
            phalcon_substr(color, *fontcolor, 1, 0);
        }
        else {
            ZVAL_STRINGL(color, Z_STRVAL_PP(fontcolor), Z_STRLEN_PP(fontcolor), 1);
        }
    }

    PHALCON_INIT_VAR(size);
    if (!fontsize || Z_TYPE_PP(fontsize) == IS_NULL) {
        ZVAL_LONG(size, 12);
    }
    else {
        PHALCON_ENSURE_IS_LONG(fontsize);
        ZVAL_LONG(size, Z_LVAL_PP(fontsize));
    }

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

    if (Z_STRLEN_P(color) == 3) {
        /* Convert RGB to RRGGBB */
        c = Z_STRVAL_P(color);
        assert(!IS_INTERNED(c));
        STR_REALLOC(c, 7);
        c[6] = '\0';
        c[5] = c[2];
        c[4] = c[2];
        c[3] = c[1];
        c[2] = c[1];
        c[1] = c[0];
        ZVAL_STRING(color, c, 0);
    }

    if (Z_STRLEN_P(color) < 6) {
        PHALCON_THROW_EXCEPTION_STR(phalcon_image_exception_ce, "color is not valid");
        return;
    }

    INIT_ZVAL(tmp);

    Z_TYPE(tmp) = IS_STRING;
    ZVAL_STRINGL(&tmp, Z_STRVAL_P(color), 2, 0);

    PHALCON_INIT_VAR(r);
    _php_math_basetozval(&tmp, 16, r);

    Z_STRVAL(tmp) += 2;
    PHALCON_INIT_VAR(g);
    _php_math_basetozval(&tmp, 16, g);

    Z_STRVAL(tmp) += 2;
    PHALCON_INIT_VAR(b);
    _php_math_basetozval(&tmp, 16, b);

    PHALCON_CALL_METHOD(NULL, this_ptr, "_text", *text, offset_x, offset_y, opacity, r, g, b, size, *fontfile);

    RETURN_THIS();
}
Пример #12
0
/**
 * Detach a listener from the events manager
 *
 * @param object|callable $handler
 */
PHP_METHOD(Phalcon_Events_Manager, detach){

	zval *type, *handler, events = {}, queue = {}, priority_queue = {}, *listener;
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 2, 0, &type, &handler);

	if (Z_TYPE_P(handler) != IS_OBJECT && !phalcon_is_callable(handler)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_events_exception_ce, "Event handler must be an object or callable");
		return;
	}

	phalcon_return_property(&events, getThis(), SL("_events"));
	if (Z_TYPE(events) != IS_ARRAY) {
		RETURN_FALSE;
	}

	if (!phalcon_array_isset_fetch(&queue, &events, type)) {
		RETURN_FALSE;
	}

	if (Z_TYPE(queue) == IS_OBJECT) {
		object_init_ex(&priority_queue, spl_ce_SplPriorityQueue);
		if (phalcon_has_constructor(&priority_queue)) {
			PHALCON_CALL_METHODW(NULL, &priority_queue, "__construct");
		}

		PHALCON_CALL_METHODW(NULL, &queue, "top");

		while (1) {
			zval r0 = {}, listener0 = {}, handler_embeded = {}, priority = {};
			PHALCON_CALL_METHODW(&r0, &queue, "valid");
			if (!zend_is_true(&r0)) {
				break;
			}

			PHALCON_CALL_METHODW(&listener0, &queue, "current");
			PHALCON_CALL_METHODW(&handler_embeded, &listener0, "getlistener");

			if (!phalcon_is_equal(&handler_embeded, handler)) {
				PHALCON_CALL_METHODW(&priority, &listener0, "getpriority");
				PHALCON_CALL_METHODW(NULL, &priority_queue, "insert", &listener0, &priority);
			}
			
			PHALCON_CALL_METHODW(NULL, &queue, "next");
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&priority_queue, &queue);
		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(queue), idx, str_key, listener) {
			zval key = {}, handler_embeded = {};
			if (str_key) {
				ZVAL_STR(&key, str_key);
			} else {
				ZVAL_LONG(&key, idx);
			}

			PHALCON_CALL_METHODW(&handler_embeded, listener, "getlistener");

			if (phalcon_is_equal_object(&handler_embeded, handler)) {
				phalcon_array_unset(&priority_queue, &key, PH_COPY);
			}

		} ZEND_HASH_FOREACH_END();
	}
Пример #13
0
/**
 * Sends the cookie to the HTTP client
 * Stores the cookie definition in session
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, send){

	zval *name, *value, *expire, *domain, *path, *secure, *http_only, *dependency_injector;
	zval service = {}, has_session = {}, definition = {}, session = {}, key = {}, encryption = {}, crypt = {}, encrypt_value = {};

	name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY);
	value = phalcon_read_property(getThis(), SL("_value"), PH_NOISY);
	expire = phalcon_read_property(getThis(), SL("_expire"), PH_NOISY);
	domain = phalcon_read_property(getThis(), SL("_domain"), PH_NOISY);
	path = phalcon_read_property(getThis(), SL("_path"), PH_NOISY);
	secure = phalcon_read_property(getThis(), SL("_secure"), PH_NOISY);
	http_only = phalcon_read_property(getThis(), SL("_httpOnly"), PH_NOISY);
	dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);

	if (Z_TYPE_P(dependency_injector) == IS_OBJECT) {
		ZVAL_STRING(&service, ISV(session));

		PHALCON_CALL_METHODW(&has_session, dependency_injector, "has", &service);
		if (zend_is_true(&has_session)) {
			array_init(&definition);
			if (!PHALCON_IS_LONG(expire, 0)) {
				phalcon_array_update_str(&definition, SL("expire"), expire, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(path)) {
				phalcon_array_update_str(&definition, SL("path"), path, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(domain)) {
				phalcon_array_update_string(&definition, IS(domain), domain, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(secure)) {
				phalcon_array_update_str(&definition, SL("secure"), secure, PH_COPY);
			}

			if (PHALCON_IS_NOT_EMPTY(http_only)) {
				phalcon_array_update_str(&definition, SL("httpOnly"), http_only, PH_COPY);
			}

			/**
			 * The definition is stored in session
			 */
			if (phalcon_fast_count_ev(&definition)) {
				PHALCON_CALL_METHODW(&session, dependency_injector, "getshared", &service);

				if (Z_TYPE(session) != IS_NULL) {
					PHALCON_VERIFY_INTERFACEW(&session, phalcon_session_adapterinterface_ce);

					PHALCON_CONCAT_SV(&key, "_PHCOOKIE_", name);
					PHALCON_CALL_METHODW(NULL, &session, "set", &key, &definition);
				}
			}
		}
	}

	phalcon_return_property(&encryption, getThis(), SL("_useEncryption"));
	if (zend_is_true(&encryption) && PHALCON_IS_NOT_EMPTY(value)) {
		if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service");
			return;
		}

		ZVAL_STRING(&service, "crypt");

		PHALCON_CALL_METHODW(&crypt, dependency_injector, "getshared", &service);
		PHALCON_VERIFY_INTERFACEW(&crypt, phalcon_cryptinterface_ce);

		/**
		 * Encrypt the value also coding it with base64
		 */
		PHALCON_CALL_METHODW(&encrypt_value, &crypt, "encryptbase64", value);
	} else {
		PHALCON_CPY_WRT_CTOR(&encrypt_value, value);
	}

	/** 
	 * Sets the cookie using the standard 'setcookie' function
	 */
	convert_to_string_ex(name);
	convert_to_long_ex(expire);
	convert_to_string_ex(domain);
	convert_to_string_ex(path);
	convert_to_long_ex(secure);
	convert_to_long_ex(http_only);
	convert_to_string_ex(&encrypt_value);

	php_setcookie(Z_STR_P(name), Z_STR(encrypt_value), Z_LVAL_P(expire), Z_STR_P(path), Z_STR_P(domain), Z_LVAL_P(secure), 1, Z_LVAL_P(http_only));

	RETURN_THISW();
}
Пример #14
0
/**
 * Handles routing information received from command-line arguments
 *
 * @param array $arguments
 */
PHP_METHOD(Phalcon_CLI_Router, handle){

	zval *arguments = NULL, longopts = {}, options = {}, module_name = {}, namespace_name = {}, task_name = {}, action_name = {};

	phalcon_fetch_params(0, 0, 1, &arguments);

	if (!arguments || Z_TYPE_P(arguments) != IS_ARRAY) {
		if (unlikely(!strcmp(sapi_module.name, "cli"))) {
			array_init(&longopts);
			phalcon_array_append_string(&longopts, SL("module::"), 0);
			phalcon_array_append_string(&longopts, SL("namespace::"), 0);
			phalcon_array_append_string(&longopts, SL("task::"), 0);
			phalcon_array_append_string(&longopts, SL("action::"), 0);
			phalcon_array_append_string(&longopts, SL("params::"), 0);
			PHALCON_CALL_FUNCTIONW(&options, "getopt", &PHALCON_GLOBAL(z_null), &longopts);
		} else {
			array_init(&options);
		}
	} else {
		PHALCON_CPY_WRT_CTOR(&options, arguments);
	}

	/**
	 * Check for a module
	 */
	if (phalcon_array_isset_fetch_str(&module_name, &options, SL("module"))) {
		phalcon_array_unset_str(arguments, SL("module"), PH_COPY);
	} else {
		ZVAL_NULL(&module_name);
	}
	phalcon_update_property_zval(getThis(), SL("_module"), &module_name);

	/**
	 * Check for a namespace
	 */
	if (phalcon_array_isset_fetch_str(&namespace_name, &options, SL("namespace"))) {
		phalcon_array_unset_str(arguments, SL("namespace"), PH_COPY);
	} else {
		ZVAL_NULL(&namespace_name);
	}
	phalcon_update_property_zval(getThis(), SL("_namespace"), &namespace_name);

	/**
	 * Check for a task
	 */
	if (phalcon_array_isset_fetch_str(&task_name, &options, SL("task"))) {
		phalcon_array_unset_str(arguments, SL("task"), PH_COPY);
	} else {
		ZVAL_NULL(&task_name);
	}
	phalcon_update_property_zval(getThis(), SL("_task"), &task_name);

	/**
	 * Check for an action
	 */
	if (phalcon_array_isset_fetch_str(&action_name, &options, SL("action"))) {
		phalcon_array_unset_str(arguments, SL("action"), PH_COPY);
	} else {
		ZVAL_NULL(&action_name);
	}
	phalcon_update_property_zval(getThis(), SL("_action"), &action_name);

	phalcon_update_property_zval(getThis(), SL("_params"), &options);
}
Пример #15
0
/**
 * Executes the validation
 *
 * @param Phalcon\Validation $validator
 * @param string $attribute
 * @return boolean
 */
PHP_METHOD(Phalcon_Validation_Validator_File, validate)
{
	zval *validator, *attribute, value = {}, allow_empty = {}, mimes = {}, minsize = {}, maxsize = {}, minwidth = {}, maxwidth = {}, minheight = {}, maxheight = {};
	zval valid = {}, type = {}, code = {}, message_str = {}, message = {}, join_mimes = {}, label = {}, pairs = {}, prepared = {};
	zend_class_entry *ce = Z_OBJCE_P(getThis());

	phalcon_fetch_params(0, 2, 0, &validator, &attribute);

	PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 0);

	PHALCON_CALL_METHODW(&value, validator, "getvalue", attribute);

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&allow_empty, ce, getThis(), ISV(allowEmpty)));
	if (zend_is_true(&allow_empty) && phalcon_validation_validator_isempty_helper(&value)) {
		RETURN_TRUE;
	}

	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&mimes, ce, getThis(), "mimes"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minsize, ce, getThis(), "minsize"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxsize, ce, getThis(), "maxsize"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minwidth, ce, getThis(), "minwidth"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxwidth, ce, getThis(), "maxwidth"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&minheight, ce, getThis(), "minheight"));
	RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&maxheight, ce, getThis(), "maxheight"));

	PHALCON_CALL_SELFW(&valid, "valid", &value, &minsize, &maxsize, &mimes, &minwidth, &maxwidth, &minheight, &maxheight);

	if (PHALCON_IS_FALSE(&valid)) {
		phalcon_return_property(&type, getThis(), SL("_type"));

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&label, ce, getThis(), ISV(label)));
		if (!zend_is_true(&label)) {
			PHALCON_CALL_METHODW(&label, validator, "getlabel", attribute);
			if (!zend_is_true(&label)) {
				PHALCON_CPY_WRT_CTOR(&label, attribute);
			}
		}

		RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&code, ce, getThis(), ISV(code)));
		if (Z_TYPE(code) == IS_NULL) {
			ZVAL_LONG(&code, 0);
		}

		array_init(&pairs);
		phalcon_array_update_str(&pairs, SL(":field"), &label, PH_COPY);

		if (phalcon_compare_strict_string(&type, SL("TooLarge"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMaxSize"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLarge", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooSmall"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMinSize"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooSmall", &code);
		} else if (phalcon_compare_strict_string(&type, SL("MimeValid"))) {
			phalcon_fast_join_str(&join_mimes, SL(", "), &mimes);
			phalcon_array_update_str(&pairs, SL(":mimes"), &join_mimes, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileType"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "FileType", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooLarge"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxsize, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileMaxSize"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLarge", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooNarrow"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minwidth, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMinWidth"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooNarrow", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooWide"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxwidth, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMaxWidth"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooWide", &code);
		}  else if (phalcon_compare_strict_string(&type, SL("TooShort"))) {
			phalcon_array_update_str(&pairs, SL(":min"), &minheight, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMinHeight"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooShort", &code);
		} else if (phalcon_compare_strict_string(&type, SL("TooLong"))) {
			phalcon_array_update_str(&pairs, SL(":max"), &maxheight, PH_COPY);

			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "ImageMaxHeight"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "TooLong", &code);
		} else {
			RETURN_ON_FAILURE(phalcon_validation_validator_getoption_helper(&message_str, ce, getThis(), ISV(message)));
			if (!zend_is_true(&message_str)) {
				RETURN_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(&message_str, Z_OBJCE_P(validator), validator, "FileValid"));
			}

			PHALCON_CALL_FUNCTIONW(&prepared, "strtr", &message_str, &pairs);

			phalcon_validation_message_construct_helper(&message, &prepared, attribute, "File", &code);
		}

		PHALCON_CALL_METHODW(NULL, validator, "appendmessage", &message);
		RETURN_FALSE;
	}

	RETURN_TRUE;
}
Пример #16
0
/**
 * Shows a backtrace item
 *
 * @param int $n
 * @param array $trace
 */
PHP_METHOD(Phalcon_Debug, showTraceItem){

	zval *n, *trace, *link_format, *space, *two_spaces, *underscore;
	zval *minus, *html, *class_name;
	zval *namespace_separator, *prepare_uri_class;
	zval *lower_class_name, *prepared_function_name;
	zval *prepare_internal_class, *type, *function_name = NULL;
	zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL;
	zval *span_argument = NULL, *joined_arguments, *z_one;
	zval *file, *line, *show_files, *lines = NULL, *number_lines;
	zval *show_file_fragment, *before_context, *before_line;
	zval *first_line = NULL, *after_context, *after_line, *last_line = NULL;
	zval *comment_pattern, *charset, *tab;
	zval *comment, *i = NULL, *line_position = NULL, *current_line = NULL;
	zval *trimmed = NULL, *is_comment = NULL, *spaced_current_line = NULL;
	zval *escaped_line = NULL, *formatted_file = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &n, &trace, &link_format);

	PHALCON_INIT_VAR(space);
	ZVAL_STRING(space, " ", 1);

	PHALCON_INIT_VAR(two_spaces);
	ZVAL_STRING(two_spaces, "  ", 1);

	PHALCON_INIT_VAR(underscore);
	ZVAL_STRING(underscore, "_", 1);

	PHALCON_INIT_VAR(minus);
	ZVAL_STRING(minus, "-", 1);

	/** 
	 * Every trace in the backtrace have a unique number
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVS(html, "<tr><td align=\"right\" valign=\"top\" class=\"error-number\">#", n, "</td><td>");
	if (phalcon_array_isset_string(trace, SS("class"))) {
		zend_class_entry *class_ce;

		PHALCON_OBS_VAR(class_name);
		phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY);

		class_ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);

		if (!class_ce) {
			/* Unable to load the class, should never happen */
		}
		else if (is_phalcon_class(class_ce)) {
			PHALCON_INIT_VAR(namespace_separator);
			ZVAL_STRING(namespace_separator, "\\", 1);

			/* Prepare the class name according to the Phalcon's conventions */
			PHALCON_INIT_VAR(prepare_uri_class);
			phalcon_fast_str_replace(prepare_uri_class, namespace_separator, underscore, class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://docs.phalconphp.com/en/latest/api/", prepare_uri_class, ".html\">", class_name, "</a></span>");
		} else if (class_ce->type == ZEND_INTERNAL_CLASS) {
			PHALCON_INIT_VAR(lower_class_name);
			phalcon_fast_strtolower(lower_class_name, class_name);

			PHALCON_INIT_VAR(prepare_internal_class);
			phalcon_fast_str_replace(prepare_internal_class, underscore, minus, lower_class_name);

			/* Generate a link to the official docs */
			PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-class\"><a target=\"_new\" href=\"http://php.net/manual/en/class.", prepare_internal_class, ".php\">", class_name, "</a></span>");
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-class\">", class_name, "</span>");
		}

		/** 
		 * Object access operator: static/instance
		 */
		PHALCON_OBS_VAR(type);
		phalcon_array_fetch_string(&type, trace, SL("type"), PH_NOISY);
		phalcon_concat_self(&html, type TSRMLS_CC);
	}

	/** 
	 * Normally the backtrace contains only classes
	 */
	if (phalcon_array_isset_string(trace, SS("class"))) {
		PHALCON_OBS_VAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
	} else {
		zend_function *func;

		PHALCON_OBS_NVAR(function_name);
		phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY);
		convert_to_string(function_name);

		/** 
		 * Check if the function exists
		 */
		if (phalcon_fetch_function(&func, Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) TSRMLS_CC) == SUCCESS) {

			/** 
			 * Internal functions links to the PHP documentation
			 */
			if (func->type == ZEND_INTERNAL_FUNCTION) {
				/** 
				 * Prepare function's name according to the conventions in the docs
				 */
				PHALCON_INIT_VAR(prepared_function_name);
				phalcon_fast_str_replace(prepared_function_name, underscore, minus, function_name);
				PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-function\"><a target=\"_new\" href=\"http://php.net/manual/en/function.", prepared_function_name, ".php\">", function_name, "</a></span>");
			} else {
				PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
			}
		} else {
			PHALCON_SCONCAT_SVS(html, "<span class=\"error-function\">", function_name, "</span>");
		}
	}

	/** 
	 * Check for arguments in the function
	 */
	if (phalcon_array_isset_string(trace, SS("args"))) {

		PHALCON_OBS_VAR(trace_args);
		phalcon_array_fetch_string(&trace_args, trace, SL("args"), PH_NOISY);
		if (phalcon_fast_count_ev(trace_args TSRMLS_CC)) {

			PHALCON_INIT_VAR(arguments);
			array_init(arguments);

			phalcon_is_iterable(trace_args, &ah0, &hp0, 0, 0);

			while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

				PHALCON_GET_HVALUE(argument);

				/** 
				 * Every argument is generated using _getVarDump
				 */
				PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", argument);

				PHALCON_INIT_NVAR(span_argument);
				PHALCON_CONCAT_SVS(span_argument, "<span class=\"error-parameter\">", dumped_argument, "</span>");

				/** 
				 * Append the HTML generated to the argument's list
				 */
				phalcon_array_append(&arguments, span_argument, PH_COPY);

				zend_hash_move_forward_ex(ah0, &hp0);
			}

			/** 
			 * Join all the arguments
			 */
			PHALCON_INIT_VAR(joined_arguments);
			phalcon_fast_join_str(joined_arguments, SL(", "), arguments TSRMLS_CC);
			PHALCON_SCONCAT_SVS(html, "(", joined_arguments, ")");
		} else {
			phalcon_concat_self_str(&html, SL("()") TSRMLS_CC);
		}
	}

	/** 
	 * When 'file' is present, it usually means the function is provided by the user
	 */
	if (phalcon_array_isset_string(trace, SS("file"))) {

		z_one = PHALCON_GLOBAL(z_one);

		PHALCON_OBS_VAR(file);
		phalcon_array_fetch_string(&file, trace, SL("file"), PH_NOISY);

		PHALCON_OBS_VAR(line);
		phalcon_array_fetch_string(&line, trace, SL("line"), PH_NOISY);

		PHALCON_CALL_METHOD(&formatted_file, getThis(), "getfilelink", file, line, link_format);

		/** 
		 * Realpath to the file and its line using a special header
		 */
		PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", formatted_file, " (", line, ")</div>");

		PHALCON_OBS_VAR(show_files);
		phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY TSRMLS_CC);

		/** 
		 * The developer can change if the files must be opened or not
		 */
		if (zend_is_true(show_files)) {

			/** 
			 * Open the file to an array using 'file', this respects the openbase-dir directive
			 */
			PHALCON_CALL_FUNCTION(&lines, "file", file);

			PHALCON_INIT_VAR(number_lines);
			phalcon_fast_count(number_lines, lines TSRMLS_CC);

			PHALCON_OBS_VAR(show_file_fragment);
			phalcon_read_property_this(&show_file_fragment, this_ptr, SL("_showFileFragment"), PH_NOISY TSRMLS_CC);

			/** 
			 * File fragments just show a piece of the file where the exception is located
			 */
			if (zend_is_true(show_file_fragment)) {

				/** 
				 * Take lines back to the current exception's line
				 */
				before_context = phalcon_fetch_nproperty_this(getThis(), SL("_beforeContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(before_line);
				phalcon_sub_function(before_line, line, before_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_LT_LONG(before_line, 1)) {
					PHALCON_CPY_WRT_CTOR(first_line, z_one);
				} else {
					PHALCON_CPY_WRT(first_line, before_line);
				}

				/** 
				 * Take lines after the current exception's line
				 */
				after_context = phalcon_fetch_nproperty_this(getThis(), SL("_afterContext"), PH_NOISY TSRMLS_CC);

				PHALCON_INIT_VAR(after_line);
				phalcon_add_function(after_line, line, after_context);

				/** 
				 * Check for overflows
				 */
				if (PHALCON_GT(after_line, number_lines)) {
					PHALCON_CPY_WRT(last_line, number_lines);
				} else {
					PHALCON_CPY_WRT(last_line, after_line);
				}

				PHALCON_SCONCAT_SVSVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums:", first_line, "'>");
			} else {
				PHALCON_CPY_WRT_CTOR(first_line, z_one);
				PHALCON_CPY_WRT(last_line, number_lines);
				PHALCON_SCONCAT_SVSVS(html, "<pre class='prettyprint highlight:", first_line, ":", line, " linenums error-scroll'>");
			}

			PHALCON_INIT_VAR(comment_pattern);
			ZVAL_STRING(comment_pattern, "#\\*\\/$#", 1);

			charset = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_charset") TSRMLS_CC);

			PHALCON_INIT_VAR(tab);
			ZVAL_STRING(tab, "\t", 1);

			PHALCON_INIT_VAR(comment);
			ZVAL_STRING(comment, "* /", 1);
			PHALCON_CPY_WRT(i, first_line);

			while (PHALCON_LE(i, last_line)) {

				/** 
				 * Current line in the file
				 */
				PHALCON_INIT_NVAR(line_position);
				phalcon_sub_function(line_position, i, z_one);

				/** 
				 * Current line content in the piece of file
				 */
				PHALCON_OBS_NVAR(current_line);
				phalcon_array_fetch(&current_line, lines, line_position, PH_NOISY);

				/** 
				 * File fragments are cleaned, removing tabs and comments
				 */
				if (zend_is_true(show_file_fragment)) {
					if (PHALCON_IS_EQUAL(i, first_line)) {

						PHALCON_INIT_NVAR(trimmed);
						phalcon_fast_trim(trimmed, current_line, NULL, PHALCON_TRIM_RIGHT TSRMLS_CC);

						PHALCON_INIT_NVAR(is_comment);

						RETURN_MM_ON_FAILURE(phalcon_preg_match(is_comment, comment_pattern, current_line, NULL TSRMLS_CC));

						if (zend_is_true(is_comment)) {
							PHALCON_INIT_NVAR(spaced_current_line);
							phalcon_fast_str_replace(spaced_current_line, comment, space, current_line);
							PHALCON_CPY_WRT(current_line, spaced_current_line);
						}
					}
				}

				/** 
				 * Print a non break space if the current line is a line break, this allows to show
				 * the html zebra properly
				 */
				if (PHALCON_IS_STRING(current_line, "\n")) {
					phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
				} else {
					if (PHALCON_IS_STRING(current_line, "\r\n")) {
						phalcon_concat_self_str(&html, SL("&nbsp;\n") TSRMLS_CC);
					} else {
						PHALCON_INIT_NVAR(spaced_current_line);
						phalcon_fast_str_replace(spaced_current_line, tab, two_spaces, current_line);

						PHALCON_INIT_NVAR(escaped_line);
						phalcon_htmlentities(escaped_line, spaced_current_line, NULL, charset TSRMLS_CC);
						phalcon_concat_self(&html, escaped_line TSRMLS_CC);
					}
				}

				phalcon_increment(i);
			}
			phalcon_concat_self_str(&html, SL("</pre>") TSRMLS_CC);
		}
	}

	phalcon_concat_self_str(&html, SL("</td></tr>") TSRMLS_CC);

	RETURN_CTOR(html);
}
Пример #17
0
PHP_METHOD(Phalcon_Http_Client_Header, parse){

	zval *content, *content_parts = NULL, *key = NULL, *header = NULL, *header_parts = NULL, *name = NULL, *value = NULL, *trimmed = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &content);

	if (PHALCON_IS_EMPTY(content)) {
		RETURN_MM_FALSE;
	}
	
	if (Z_TYPE_P(content) == IS_STRING) {
		PHALCON_INIT_VAR(content_parts);
		phalcon_fast_explode_str(content_parts, SL("\r\n"), content);
	} else if (Z_TYPE_P(content) == IS_ARRAY) {
		PHALCON_CPY_WRT_CTOR(content_parts, content);
	} else {
		RETURN_MM_FALSE;
	}	

	phalcon_is_iterable(content_parts, &ah0, &hp0, 0, 0);	
	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
		PHALCON_GET_HKEY(key, ah0, hp0);
		PHALCON_GET_HVALUE(header);

		if (Z_TYPE_P(header) == IS_STRING) {
			PHALCON_INIT_NVAR(header_parts);
			if (phalcon_memnstr_str(header , SL(":"))) {
				phalcon_fast_explode_str(header_parts, SL(":"), header);
			} else {
				if (phalcon_start_with_str(header , SL("HTTP/"))) {
					phalcon_fast_explode_str(header_parts, SL(" "), header);
					if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 1) && phalcon_array_isset_long(header_parts, 2)) {
						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY);
						phalcon_update_property_this(this_ptr, SL("_status_code"), value TSRMLS_CC);

						PHALCON_OBS_NVAR(value);
						phalcon_array_fetch_long(&value, header_parts, 2, PH_NOISY);
						phalcon_update_property_this(this_ptr, SL("_status_message"), value TSRMLS_CC);
					}
				}

				zend_hash_move_forward_ex(ah0, &hp0);
				continue;
			}
		} else {
			PHALCON_CPY_WRT_CTOR(header_parts, header);
		}

		if (Z_TYPE_P(header_parts) == IS_ARRAY && phalcon_array_isset_long(header_parts, 0) && phalcon_array_isset_long(header_parts, 1)) {
				PHALCON_OBS_NVAR(name);
				phalcon_array_fetch_long(&name, header_parts, 0, PH_NOISY);

				PHALCON_OBS_NVAR(value);
				phalcon_array_fetch_long(&value, header_parts, 1, PH_NOISY);

				PHALCON_INIT_NVAR(trimmed);
				phalcon_fast_trim(trimmed, value, NULL, PHALCON_TRIM_BOTH TSRMLS_CC);

				PHALCON_CALL_METHOD(NULL, this_ptr, "set", name, trimmed);
		}

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	PHALCON_MM_RESTORE();
}
Пример #18
0
/**
 * Composite one image onto another

 *
 * @param Phalcon\Image\Adapter $mask  mask Image instance
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _mask){

	zval *mask, image = {}, mask_image = {}, blob = {}, mask_image_width = {}, mask_image_height = {}, newimage = {}, image_width = {}, image_height = {}, saveflag = {}, color = {}, c = {}, alpha = {}, temp_image = {};
	int x, y, w, h, i;

	phalcon_fetch_params(0, 1, 0, &mask);

	phalcon_return_property(&image, getThis(), SL("_image"));

	PHALCON_CALL_METHODW(&blob, mask, "render");
	PHALCON_CALL_FUNCTIONW(&mask_image, "imagecreatefromstring", &blob);

	ZVAL_TRUE(&saveflag);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &mask_image, &saveflag);

	PHALCON_CALL_FUNCTIONW(&mask_image_width, "imagesx", &mask_image);
	PHALCON_CALL_FUNCTIONW(&mask_image_height, "imagesy", &mask_image);

	phalcon_return_property(&image_width, getThis(), SL("_width"));
	phalcon_return_property(&image_height, getThis(), SL("_height"));

	PHALCON_CALL_METHODW(&newimage, getThis(), "_create", &image_width, &image_height);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &newimage, &saveflag);

	ZVAL_LONG(&c, 0);
	ZVAL_LONG(&alpha, 127);

	PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &c, &c, &c, &alpha);
	PHALCON_CALL_FUNCTIONW(NULL, "imagefill", &newimage, &c, &c, &color);

	if(!PHALCON_IS_EQUAL(&image_width, &mask_image_width) || !PHALCON_IS_EQUAL(&image_height, &mask_image_height)) {
		PHALCON_CALL_FUNCTIONW(&temp_image, "imagecreatetruecolor", &image_width, &image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagecopyresampled", &temp_image, &mask_image, &c, &c, &c, &c, &image_width, &image_height, &mask_image_width, &mask_image_height);
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

		PHALCON_CPY_WRT_CTOR(&mask_image, &temp_image);
	}

	w = phalcon_get_intval(&image_width);
	h = phalcon_get_intval(&image_height);

	for (x=0; x < w; x++) {
		zval zx = {};
		ZVAL_LONG(&zx, x);
		for (y=0; y < h; y++) {
			zval zy = {}, index = {}, red = {}, index2 = {}, color = {}, r = {}, g = {}, b = {};
			ZVAL_LONG(&zy, y);

			PHALCON_CALL_FUNCTIONW(&index, "imagecolorat", &mask_image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&alpha, "imagecolorsforindex", &mask_image, &index);

			if (phalcon_array_isset_fetch_str(&red, &alpha, SL("red"))) {
				i = (int)(127 - (phalcon_get_intval(&red) / 2));
				ZVAL_LONG(&alpha, i);
			}

			PHALCON_CALL_FUNCTIONW(&index2, "imagecolorat", &image, &zx, &zy);
			PHALCON_CALL_FUNCTIONW(&color, "imagecolorsforindex", &image, &index2);

			phalcon_array_isset_fetch_str(&r, &color, SL("red"));
			phalcon_array_isset_fetch_str(&g, &color, SL("green"));
			phalcon_array_isset_fetch_str(&b, &color, SL("blue"));

			PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &newimage, &r, &g, &b, &alpha);
			PHALCON_CALL_FUNCTIONW(NULL, "imagesetpixel", &newimage, &zx, &zy, &color);
		}
	}

	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &mask_image);

	phalcon_update_property_zval(getThis(), SL("_image"), &newimage);
}
Пример #19
0
/**
 * Execute a reflection.
 *
 * @param int $height
 * @param int $opacity
 * @param boolean $fade_in
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _reflection) {

	zval *_height, *opacity, *fade_in, height = {}, tmp = {}, reflection  = {}, line = {}, image = {}, image_width = {}, image_height = {}, dst = {}, filtertype = {};
	int h0, h1, tmp_opacity, int_opacity, offset;
	double stepping;

	phalcon_fetch_params(0, 3, 0, &_height, &opacity, &fade_in);

	PHALCON_CPY_WRT_CTOR(&height, _height);

	phalcon_return_property(&image, getThis(), SL("_image"));
	phalcon_return_property(&image_width, getThis(), SL("_width"));
	phalcon_return_property(&image_height, getThis(), SL("_height"));

	if (!phalcon_get_constant(&filtertype, SL("IMG_FILTER_COLORIZE"))) {
		return;
	}

	h0 = phalcon_get_intval(&height);
	h1 = phalcon_get_intval(&image_height);

	if (unlikely(h0 == 0)) {
		h0 = 1;
	}

	tmp_opacity = phalcon_get_intval(opacity);

	tmp_opacity = (int)((tmp_opacity * 127 / 100) - 127 + 0.5);

	if (tmp_opacity < 0) {
		tmp_opacity = -tmp_opacity;
	}

	if (tmp_opacity < 127) {
		stepping = (127 - tmp_opacity) / h0;
	} else {
		stepping = 127 / h0;
	}

	ZVAL_DOUBLE(&height, h0 + h1);

	PHALCON_CALL_METHODW(&reflection, getThis(), "_create", &image_width, &height);

	ZVAL_LONG(&dst, 0);

	PHALCON_CALL_FUNCTIONW(NULL, "imagecopy", &reflection, &image, &dst, &dst, &dst, &dst, &image_width, &image_height);

	ZVAL_LONG(&tmp, 1);

	for (offset = 0; h0 >= offset; offset++) {
		zval src_y = {}, dst_y = {}, dst_opacity = {};
		ZVAL_LONG(&src_y, h1 - offset - 1);
		ZVAL_LONG(&dst_y, h1 + offset);

		if (zend_is_true(fade_in)) {
			int_opacity = (int)(tmp_opacity + (stepping * (h0 - offset)) + 0.5);
			ZVAL_LONG(&dst_opacity, int_opacity);
		} else {
			int_opacity = (int)(tmp_opacity + (stepping * offset) + 0.5);
			ZVAL_LONG(&dst_opacity, int_opacity);
		}

		PHALCON_CALL_METHODW(&line, getThis(), "_create", &image_width, &tmp);
		PHALCON_CALL_FUNCTIONW(NULL, "imagecopy", &line, &image, &dst, &dst, &dst, &src_y, &image_width, &tmp);
		PHALCON_CALL_FUNCTIONW(NULL, "imagefilter", &line, &filtertype, &dst, &dst, &dst, &dst_opacity);
		PHALCON_CALL_FUNCTIONW(NULL, "imagecopy", &reflection, &line, &dst, &dst_y, &dst, &dst, &image_width, &tmp);
	}

	PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	phalcon_update_property_zval(getThis(), SL("_image"), &reflection);

	PHALCON_CALL_FUNCTIONW(&image_width, "imagesx", &reflection);
	PHALCON_CALL_FUNCTIONW(&image_height, "imagesy", &reflection);

	phalcon_update_property_zval(getThis(), SL("_width"), &image_width);
	phalcon_update_property_zval(getThis(), SL("_height"), &image_height);
}
Пример #20
0
/**
 * Decrypts an encrypted text
 *
 *<code>
 *	echo $crypt->decrypt($encrypted, "decrypt password");
 *</code>
 *
 * @param string $text
 * @param string $key
 * @param int $options
 * @return string
 */
PHP_METHOD(Phalcon_Crypt, decrypt){

	zval *source, *key = NULL, *options = NULL, handler = {}, arguments = {}, value = {}, text = {}, encrypt_key = {}, encrypt_options = {};
	zval method = {}, iv_size = {}, iv = {}, text_to_decipher = {};

	phalcon_fetch_params(0, 1, 2, &source, &key, &options);

	if (phalcon_function_exists_ex(SL("openssl_encrypt")) == FAILURE) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_crypt_exception_ce, "openssl extension is required");
		return;
	}

	phalcon_read_property(&handler, getThis(), SL("_beforeDecrypt"), PH_NOISY);

	if (phalcon_is_callable(&handler)) {
		PHALCON_SEPARATE_PARAM(source);

		array_init_size(&arguments, 1);
		phalcon_array_append(&arguments, source, PH_COPY);

		PHALCON_CALL_USER_FUNC_ARRAYW(&value, &handler, &arguments);

		source = &value;
	}

	/* Do not use make_printable_zval() here: we need the conversion with type juggling */
	if (Z_TYPE_P(source) != IS_STRING) {
		phalcon_cast(&text, source, IS_STRING);
	} else {
		PHALCON_CPY_WRT_CTOR(&text, source);
	}

	if (!key || Z_TYPE_P(key) == IS_NULL) {
		phalcon_return_property(&encrypt_key, getThis(), SL("_key"));
	} else {
		PHALCON_CPY_WRT_CTOR(&encrypt_key, key);
		if (Z_TYPE(encrypt_key) != IS_STRING) {
			convert_to_string(&encrypt_key);
		}
	}

	if (!options || Z_TYPE_P(options) == IS_NULL) {
		phalcon_return_property(&encrypt_options, getThis(), SL("_options"));
	} else {
		PHALCON_CPY_WRT_CTOR(&encrypt_options, options);
	}

	phalcon_read_property(&method, getThis(), SL("_method"), PH_NOISY);
	PHALCON_CALL_FUNCTIONW(&iv_size, "openssl_cipher_iv_length", &method);

	if (Z_LVAL(iv_size) <= 0) {
		ZVAL_NULL(&iv);
		PHALCON_CPY_WRT_CTOR(&text_to_decipher, &text);
	} else {
		phalcon_substr(&iv, &text, 0, Z_LVAL(iv_size));
		phalcon_substr(&text_to_decipher, &text, Z_LVAL(iv_size), 0);
	}

	PHALCON_CALL_FUNCTIONW(return_value, "openssl_decrypt", &text_to_decipher, &method, &encrypt_key, &encrypt_options, &iv);
	if (unlikely(Z_TYPE_P(return_value) != IS_STRING)) {
		convert_to_string(return_value);
	}

	phalcon_read_property(&handler, getThis(), SL("_afterDecrypt"), PH_NOISY);

	if (phalcon_is_callable(&handler)) {
		array_init_size(&arguments, 1);
		phalcon_array_append(&arguments, return_value, PH_COPY);

		PHALCON_CALL_USER_FUNC_ARRAYW(&value, &handler, &arguments);

		RETURN_CTORW(&value);
	}
}
Пример #21
0
/**
 * Load config file
 *
 * @param string $filePath
 */
PHP_METHOD(Phalcon_Config_Adapter_Ini, read){

	zval *file_path, *absolute_path = NULL, *scanner_mode = NULL, config_dir_path = {}, base_path = {}, ini_config = {}, config = {}, *directives;
	zend_string *str_key;
	ulong idx;

	phalcon_fetch_params(0, 1, 2, &file_path, &absolute_path, &scanner_mode);
	PHALCON_ENSURE_IS_STRING(file_path);

	if (!absolute_path) {
		absolute_path = &PHALCON_GLOBAL(z_false);
	}

	if (zend_is_true(absolute_path)) {
		PHALCON_CPY_WRT_CTOR(&config_dir_path, file_path);
	} else {
		phalcon_return_static_property_ce(&base_path, phalcon_config_adapter_ce, SL("_basePath"));

		PHALCON_CONCAT_VV(&config_dir_path, &base_path, file_path);
	}

	/** 
	 * Use the standard parse_ini_file
	 */
	if (scanner_mode && Z_TYPE_P(scanner_mode) == IS_LONG) {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true), scanner_mode);
	} else {
		PHALCON_CALL_FUNCTIONW(&ini_config, "parse_ini_file", &config_dir_path, &PHALCON_GLOBAL(z_true));
	}

	/** 
	 * Check if the file had errors
	 */
	if (Z_TYPE(ini_config) != IS_ARRAY) {
		zend_throw_exception_ex(phalcon_config_exception_ce, 0, "Configuration file '%s' cannot be read", Z_STRVAL(config_dir_path));
		return;
	}

	array_init(&config);

	ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(ini_config), idx, str_key, directives) {
		zval section = {}, *value;
		if (str_key) {
			ZVAL_STR(&section, str_key);
		} else {
			ZVAL_LONG(&section, idx);
		}

		if (unlikely(Z_TYPE_P(directives) != IS_ARRAY) || zend_hash_num_elements(Z_ARRVAL_P(directives)) == 0) {
			phalcon_array_update_zval(&config, &section, directives, PH_COPY);
		} else {
			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(directives), idx, str_key, value) {
				zval key = {}, directive_parts = {};
				if (str_key) {
					ZVAL_STR(&key, str_key);
				} else {
					ZVAL_LONG(&key, idx);
				}

				if (str_key && memchr(Z_STRVAL(key), '.', Z_STRLEN(key))) {
					phalcon_fast_explode_str(&directive_parts, SL("."), &key);
					phalcon_config_adapter_ini_update_zval_directive(&config, &section, &directive_parts, value);
				} else {
					phalcon_array_update_multi_2(&config, &section, &key, value, PH_COPY);
				}
			} ZEND_HASH_FOREACH_END();
		}
Пример #22
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return \stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate) {

    zval show = {}, config = {}, items = {}, page_number = {}, rowcount = {}, page = {}, last_show_page = {}, start = {}, possible_pages = {}, total_pages = {};
    zval page_items = {}, maximum_pages = {}, next = {}, additional_page = {}, before = {}, remainder = {}, pages_total = {};
    long int i, i_show;

    phalcon_return_property(&show, getThis(), SL("_limitRows"));
    phalcon_return_property(&config, getThis(), SL("_config"));
    phalcon_return_property(&page_number, getThis(), SL("_page"));

    i_show = phalcon_get_intval(&show);

    phalcon_array_fetch_str(&items, &config, SL("data"), PH_NOISY);

    if (Z_TYPE(page_number) == IS_NULL || PHALCON_LT(&show, &PHALCON_GLOBAL(z_zero))) {
        PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
    }

    phalcon_fast_count(&rowcount, &items);

    object_init(&page);

    phalcon_sub_function(&last_show_page, &page_number, &PHALCON_GLOBAL(z_one));

    mul_function(&start, &show, &last_show_page);
    phalcon_div_function(&possible_pages, &rowcount, &show);

    if (unlikely(Z_TYPE(possible_pages)) != IS_DOUBLE) {
        convert_to_double(&possible_pages);
    }

    ZVAL_LONG(&total_pages, (long int)ceil(Z_DVAL(possible_pages)));
    if (Z_TYPE(items) != IS_OBJECT) {
        PHALCON_THROW_EXCEPTION_STRW(phalcon_paginator_exception_ce, "Invalid data for paginator");
        return;
    }

    array_init(&page_items);
    if (PHALCON_GT(&rowcount, &PHALCON_GLOBAL(z_zero))) {
        /**
         * Seek to the desired position
         */
        if (PHALCON_LT(&start, &rowcount)) {
            PHALCON_CALL_METHODW(NULL, &items, "seek", &start);
        } else {
            PHALCON_CALL_METHODW(NULL, &items, "rewind");
            PHALCON_CPY_WRT_CTOR(&page_number, &PHALCON_GLOBAL(z_one));
            PHALCON_CPY_WRT_CTOR(&start, &PHALCON_GLOBAL(z_zero));
        }

        /**
         * The record must be iterable
         */
        for (i=1; ; ++i) {
            zval valid = {}, current = {};
            PHALCON_CALL_METHODW(&valid, &items, "valid");
            if (!PHALCON_IS_NOT_FALSE(&valid)) {
                break;
            }

            PHALCON_CALL_METHODW(&current, &items, "current");
            phalcon_array_append(&page_items, &current, PH_COPY);

            if (i >= i_show) {
                break;
            }
        }
    }

    phalcon_update_property_zval(&page, SL("items"), &page_items);

    phalcon_add_function(&maximum_pages, &start, &show);
    if (PHALCON_LT(&maximum_pages, &rowcount)) {
        phalcon_add_function(&next, &page_number, &PHALCON_GLOBAL(z_one));
    } else if (PHALCON_IS_EQUAL(&maximum_pages, &rowcount)) {
        PHALCON_CPY_WRT_CTOR(&next, &rowcount);
    } else {
        phalcon_div_function(&possible_pages, &rowcount, &show);

        phalcon_add_function(&additional_page, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&next, phalcon_get_intval(&additional_page));
    }

    if (PHALCON_GT(&next, &total_pages)) {
        PHALCON_CPY_WRT_CTOR(&next, &total_pages);
    }

    phalcon_update_property_zval(&page, SL("next"), &next);
    if (PHALCON_GT(&page_number, &PHALCON_GLOBAL(z_one))) {
        phalcon_sub_function(&before, &page_number, &PHALCON_GLOBAL(z_one));
    } else {
        PHALCON_CPY_WRT_CTOR(&before, &PHALCON_GLOBAL(z_one));
    }

    phalcon_update_property_zval(&page, SL("first"), &PHALCON_GLOBAL(z_one));
    phalcon_update_property_zval(&page, SL("before"), &before);
    phalcon_update_property_zval(&page, SL("current"), &page_number);

    mod_function(&remainder, &rowcount, &show);

    phalcon_div_function(&possible_pages, &rowcount, &show);
    if (!PHALCON_IS_LONG(&remainder, 0)) {
        phalcon_add_function(&next, &possible_pages, &PHALCON_GLOBAL(z_one));

        ZVAL_LONG(&pages_total, phalcon_get_intval(&next));
    } else {
        PHALCON_CPY_WRT_CTOR(&pages_total, &possible_pages);
    }

    phalcon_update_property_zval(&page, SL("last"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_pages"), &pages_total);
    phalcon_update_property_zval(&page, SL("total_items"), &rowcount);

    RETURN_CTORW(&page);
}
Пример #23
0
/**
 * Traverses a collection calling the callback to generate its HTML
 *
 * @param Phalcon\Assets\Collection $collection
 * @param callback $callback
 * @param string $type
 * @param array $args
 */
PHP_METHOD(Phalcon_Assets_Manager, output){

	zval *collection, *callback, *type = NULL, *args = NULL, *output, *use_implicit_output;
	zval *resources = NULL, *filters = NULL, *prefix = NULL, *source_base_path = NULL;
	zval *target_base_path = NULL, *options, *collection_source_path = NULL;
	zval *complete_source_path = NULL, *collection_target_path = NULL;
	zval *complete_target_path = NULL, *filtered_joined_content = NULL;
	zval *join = NULL, *exception_message = NULL, *is_directory;
	zval *resource = NULL, *filter_needed = NULL, *local = NULL, *source_path = NULL;
	zval *target_path = NULL, *path = NULL, *prefixed_path = NULL, *attributes = NULL;
	zval *parameters = NULL, *html = NULL, *content = NULL, *must_filter = NULL;
	zval *filter = NULL, *filtered_content = NULL, *target_uri = NULL;
	HashTable *ah0, *ah1;
	HashPosition hp0, hp1;
	zval **hd;
	zval *type_css;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 2, 2, &collection, &callback, &type, &args);

	if (!args) {
		args = PHALCON_GLOBAL(z_null);
	}

	PHALCON_INIT_VAR(output);

	PHALCON_OBS_VAR(use_implicit_output);
	phalcon_read_property_this(&use_implicit_output, this_ptr, SL("_implicitOutput"), PH_NOISY TSRMLS_CC);

	/** 
	 * Get the resources as an array
	 */
	PHALCON_CALL_METHOD(&resources, collection, "getresources");

	/** 
	 * Get filters in the collection
	 */
	PHALCON_CALL_METHOD(&filters, collection, "getfilters");

	/** 
	 * Get the collection's prefix
	 */
	PHALCON_CALL_METHOD(&prefix, collection, "getprefix");

	PHALCON_INIT_VAR(type_css);
	ZVAL_STRING(type_css, "css", 1);

	/** 
	 * Prepare options if the collection must be filtered
	 */
	if (Z_TYPE_P(filters) == IS_ARRAY) { 

		PHALCON_INIT_VAR(source_base_path);

		PHALCON_INIT_VAR(target_base_path);

		PHALCON_OBS_VAR(options);
		phalcon_read_property_this(&options, this_ptr, SL("_options"), PH_NOISY TSRMLS_CC);

		/** 
		 * Check for global options in the assets manager
		 */
		if (Z_TYPE_P(options) == IS_ARRAY) { 

			/** 
			 * The source base path is a global location where all resources are located
			 */
			if (phalcon_array_isset_string(options, SS("sourceBasePath"))) {
				PHALCON_OBS_NVAR(source_base_path);
				phalcon_array_fetch_string(&source_base_path, options, SL("sourceBasePath"), PH_NOISY);
			}

			/** 
			 * The target base path is a global location where all resources are written
			 */
			if (phalcon_array_isset_string(options, SS("targetBasePath"))) {
				PHALCON_OBS_NVAR(target_base_path);
				phalcon_array_fetch_string(&target_base_path, options, SL("targetBasePath"), PH_NOISY);
			}
		}

		/** 
		 * Check if the collection have its own source base path
		 */
		PHALCON_CALL_METHOD(&collection_source_path, collection, "getsourcepath");

		/** 
		 * Concatenate the global base source path with the collection one
		 */
		if (PHALCON_IS_NOT_EMPTY(collection_source_path)) {
			PHALCON_INIT_VAR(complete_source_path);
			PHALCON_CONCAT_VV(complete_source_path, source_base_path, collection_source_path);
		} else {
			PHALCON_CPY_WRT(complete_source_path, source_base_path);
		}

		/** 
		 * Check if the collection have its own target base path
		 */
		PHALCON_CALL_METHOD(&collection_target_path, collection, "gettargetpath");

		/** 
		 * Concatenate the global base source path with the collection one
		 */
		if (PHALCON_IS_NOT_EMPTY(collection_target_path)) {
			PHALCON_INIT_VAR(complete_target_path);
			PHALCON_CONCAT_VV(complete_target_path, target_base_path, collection_target_path);
		} else {
			PHALCON_CPY_WRT(complete_target_path, target_base_path);
		}

		/** 
		 * Global filtered content
		 */
		PHALCON_INIT_VAR(filtered_joined_content);

		/** 
		 * Check if all the resources in the collection must be joined
		 */
		PHALCON_CALL_METHOD(&join, collection, "getjoin");

		/** 
		 * Check for valid target paths if the collection must be joined
		 */
		if (zend_is_true(join)) {

			/** 
			 * We need a valid final target path
			 */
			if (PHALCON_IS_EMPTY(complete_target_path)) {
				PHALCON_INIT_VAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Path '", complete_target_path, "' is not a valid target path (1)");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}

			PHALCON_INIT_VAR(is_directory);
			phalcon_is_dir(is_directory, complete_target_path TSRMLS_CC);

			/** 
			 * The targetpath needs to be a valid file
			 */
			if (PHALCON_IS_TRUE(is_directory)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Path '", complete_target_path, "' is not a valid target path (2)");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}
		}
	}

	phalcon_is_iterable(resources, &ah0, &hp0, 0, 0);

	while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

		PHALCON_GET_HVALUE(resource);

		PHALCON_INIT_NVAR(filter_needed);
		ZVAL_FALSE(filter_needed);

		if (!type) {
			PHALCON_CALL_METHOD(&type, resource, "gettype");
		}

		/** 
		 * Is the resource local?
		 */
		PHALCON_CALL_METHOD(&local, resource, "getlocal");

		/** 
		 * If the collection must not be joined we must print a HTML for each one
		 */
		if (Z_TYPE_P(filters) == IS_ARRAY) { 
			if (zend_is_true(local)) {

				/** 
				 * Get the complete path
				 */
				PHALCON_CALL_METHOD(&source_path, resource, "getrealsourcepath", complete_source_path);

				/** 
				 * We need a valid source path
				 */
				if (!zend_is_true(source_path)) {
					PHALCON_CALL_METHOD(&source_path, resource, "getpath");

					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Resource '", source_path, "' does not have a valid source path");
					PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
					return;
				}
			} else {
				/** 
				 * Get the complete source path
				 */
				PHALCON_CALL_METHOD(&source_path, resource, "getpath");

				/** 
				 * resources paths are always filtered
				 */
				PHALCON_INIT_NVAR(filter_needed);
				ZVAL_TRUE(filter_needed);
			}

			/** 
			 * Get the target path, we need to write the filtered content to a file
			 */
			PHALCON_CALL_METHOD(&target_path, resource, "getrealtargetpath", complete_target_path);

			/** 
			 * We need a valid final target path
			 */
			if (PHALCON_IS_EMPTY(target_path)) {
				PHALCON_INIT_NVAR(exception_message);
				PHALCON_CONCAT_SVS(exception_message, "Resource '", source_path, "' does not have a valid target path");
				PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
				return;
			}

			if (zend_is_true(local)) {

				/** 
				 * Make sure the target path is not the same source path
				 */
				if (PHALCON_IS_EQUAL(target_path, source_path)) {
					PHALCON_INIT_NVAR(exception_message);
					PHALCON_CONCAT_SVS(exception_message, "Resource '", target_path, "' have the same source and target paths");
					PHALCON_THROW_EXCEPTION_ZVAL(phalcon_assets_exception_ce, exception_message);
					return;
				}
				if (phalcon_file_exists(target_path TSRMLS_CC) == SUCCESS) {
					if (phalcon_compare_mtime(target_path, source_path TSRMLS_CC)) {
						PHALCON_INIT_NVAR(filter_needed);
						ZVAL_TRUE(filter_needed);
					}
				} else {
					PHALCON_INIT_NVAR(filter_needed);
					ZVAL_TRUE(filter_needed);
				}
			}
		}

		/** 
		 * If there are no filters, just print/buffer the HTML
		 */
		if (Z_TYPE_P(filters) != IS_ARRAY) { 
			PHALCON_CALL_METHOD(&path, resource, "getrealtargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, path);
			} else {
				PHALCON_CPY_WRT(prefixed_path, path);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, resource, "getattributes");

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}

			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		if (zend_is_true(filter_needed)) {

			/** 
			 * Get the resource's content
			 */
			PHALCON_CALL_METHOD(&content, resource, "getcontent", complete_source_path);

			/** 
			 * Check if the resource must be filtered
			 */
			PHALCON_CALL_METHOD(&must_filter, resource, "getfilter");

			/** 
			 * Only filter the resource if it's marked as 'filterable'
			 */
			if (zend_is_true(must_filter)) {

				phalcon_is_iterable(filters, &ah1, &hp1, 0, 0);

				while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

					PHALCON_GET_HVALUE(filter);

					/** 
					 * Filters must be valid objects
					 */
					if (Z_TYPE_P(filter) != IS_OBJECT) {
						PHALCON_THROW_EXCEPTION_STR(phalcon_assets_exception_ce, "Filter is invalid");
						return;
					}

					/** 
					 * Calls the method 'filter' which must return a filtered version of the content
					 */
					PHALCON_CALL_METHOD(&filtered_content, filter, "filter", content);
					PHALCON_CPY_WRT_CTOR(content, filtered_content);

					zend_hash_move_forward_ex(ah1, &hp1);
				}

				/**
				 * Update the joined filtered content
				 */
				if (zend_is_true(join)) {
					if (PHALCON_IS_EQUAL(type, type_css)) {
						if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
							PHALCON_INIT_NVAR(filtered_joined_content);
							PHALCON_CONCAT_VS(filtered_joined_content, content, "");
						} else {
							PHALCON_SCONCAT_VS(filtered_joined_content, content, "");
						}
					} else {
						if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
							PHALCON_INIT_NVAR(filtered_joined_content);
							PHALCON_CONCAT_VS(filtered_joined_content, content, ";");
						} else {
							PHALCON_SCONCAT_VS(filtered_joined_content, content, ";");
						}
					}
				}

			} else {
				/** 
				 * Update the joined filtered content
				 */
				if (zend_is_true(join)) {
					if (Z_TYPE_P(filtered_joined_content) == IS_NULL) {
						PHALCON_CPY_WRT(filtered_joined_content, content);
					} else {
						phalcon_concat_self(&filtered_joined_content, content TSRMLS_CC);
					}
				} else {
					PHALCON_CPY_WRT(filtered_content, content);
				}
			}

			if (!zend_is_true(join)) {
				/** 
				 * Write the file using file-put-contents. This respects the openbase-dir also
				 * writes to streams
				 */
				phalcon_file_put_contents(NULL, target_path, filtered_content TSRMLS_CC);
			}
		}

		if (!zend_is_true(join)) {

			/** 
			 * Generate the HTML using the original path in the resource
			 */
			PHALCON_CALL_METHOD(&path, resource, "getrealtargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, path);
			} else {
				PHALCON_CPY_WRT(prefixed_path, path);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, resource, "getattributes");

			/** 
			 * Filtered resources are always local
			 */
			PHALCON_INIT_NVAR(local);
			ZVAL_TRUE(local);

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}
		}

		zend_hash_move_forward_ex(ah0, &hp0);
	}

	if (Z_TYPE_P(filters) == IS_ARRAY) { 
		if (zend_is_true(join)) {

			/** 
			 * Write the file using file_put_contents. This respects the openbase-dir also
			 * writes to streams
			 */
			phalcon_file_put_contents(NULL, complete_target_path, filtered_joined_content TSRMLS_CC);

			/** 
			 * Generate the HTML using the original path in the resource
			 */
			PHALCON_CALL_METHOD(&target_uri, collection, "gettargeturi");
			if (Z_TYPE_P(prefix) != IS_NULL) {
				PHALCON_INIT_NVAR(prefixed_path);
				PHALCON_CONCAT_VV(prefixed_path, prefix, target_uri);
			} else {
				PHALCON_CPY_WRT(prefixed_path, target_uri);
			}

			/** 
			 * Gets extra HTML attributes in the resource
			 */
			PHALCON_CALL_METHOD(&attributes, collection, "getattributes");
			PHALCON_CALL_METHOD(&local, collection, "gettargetlocal");

			/** 
			 * Prepare the parameters for the callback
			 */
			PHALCON_INIT_NVAR(parameters);
			array_init_size(parameters, 3);
			if (Z_TYPE_P(attributes) == IS_ARRAY) { 
				phalcon_array_update_long(&attributes, 0, prefixed_path, PH_COPY);

				phalcon_array_append(&parameters, attributes, PH_COPY);
			} else {
				phalcon_array_append(&parameters, prefixed_path, PH_COPY);
			}

			phalcon_array_append(&parameters, local, PH_COPY);
			phalcon_array_append(&parameters, args, PH_COPY);

			/** 
			 * Call the callback to generate the HTML
			 */
			PHALCON_INIT_NVAR(html);/**/
			PHALCON_CALL_USER_FUNC_ARRAY(html, callback, parameters);

			/** 
			 * Implicit output prints the content directly
			 */
			if (zend_is_true(use_implicit_output)) {
				zend_print_zval(html, 0);
			} else {
				phalcon_concat_self(&output, html TSRMLS_CC);
			}
		}
	}

	RETURN_CCTOR(output);
}
Пример #24
0
/**
 * Encrypts a text
 *
 *<code>
 *	$encrypted = $crypt->encrypt("Ultra-secret text", "encrypt password");
 *</code>
 *
 * @param string $text
 * @param string $key
 * @return string
 */
PHP_METHOD(Phalcon_Crypt, encrypt){

	zval *source, *text, *key = NULL, *encrypt_key = NULL, *cipher, *mode, *padding_type, *iv_size = NULL;
	zval *rand, *iv = NULL, *encrypt = NULL, *block_size = NULL, *padded;
	zval *handler, *arguments = NULL, *value = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 1, &source, &key);

	PHALCON_OBS_VAR(handler);
	phalcon_read_property_this(&handler, this_ptr, SL("_beforeEncrypt"), PH_NOISY TSRMLS_CC);

	if (phalcon_is_callable(handler TSRMLS_CC)) {
		PHALCON_SEPARATE_PARAM(source);

		PHALCON_INIT_NVAR(arguments);
		array_init_size(arguments, 1);
		phalcon_array_append(&arguments, source, 0);

		PHALCON_INIT_NVAR(value);
		PHALCON_CALL_USER_FUNC_ARRAY(value, handler, arguments);

		PHALCON_CPY_WRT(source, value);
	}

	/* Do not use make_printable_zval() here: we need the conversion with type juggling */
	if (Z_TYPE_P(source) != IS_STRING) {
		PHALCON_INIT_VAR(text);
		phalcon_cast(text, source, IS_STRING);
	}
	else {
		text = source;
	}

	if (phalcon_function_exists_ex(SS("mcrypt_get_iv_size") TSRMLS_CC) == FAILURE) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_crypt_exception_ce, "mcrypt extension is required");
		return;
	}

	if (!key || Z_TYPE_P(key) == IS_NULL) {
		encrypt_key = phalcon_fetch_nproperty_this(this_ptr, SL("_key"), PH_NOISY TSRMLS_CC);
	} else {
		PHALCON_CPY_WRT_CTOR(encrypt_key, key);
		if (Z_TYPE_P(encrypt_key) != IS_STRING) {
			convert_to_string(encrypt_key);
		}
	}

	if (PHALCON_IS_EMPTY(encrypt_key)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_crypt_exception_ce, "Encryption key cannot be empty");
		return;
	}

	cipher = phalcon_fetch_nproperty_this(this_ptr, SL("_cipher"), PH_NOISY TSRMLS_CC);
	mode   = phalcon_fetch_nproperty_this(this_ptr, SL("_mode"), PH_NOISY TSRMLS_CC);

	PHALCON_CALL_FUNCTION(&iv_size, "mcrypt_get_iv_size", cipher, mode);
	if (unlikely(Z_TYPE_P(iv_size) != IS_LONG)) {
		convert_to_long(iv_size);
	}

	if (Z_STRLEN_P(encrypt_key) > Z_LVAL_P(iv_size)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_crypt_exception_ce, "Size of key is too large for this algorithm");
		return;
	}

	PHALCON_INIT_VAR(rand);
	ZVAL_LONG(rand, 2);

	PHALCON_CALL_FUNCTION(&iv, "mcrypt_create_iv", iv_size, rand);
	if (unlikely(Z_TYPE_P(iv) != IS_STRING)) {
		convert_to_string(iv);
	}

	PHALCON_CALL_FUNCTION(&block_size, "mcrypt_get_block_size", cipher, mode);
	if (unlikely(Z_TYPE_P(block_size) != IS_LONG)) {
		convert_to_long(block_size);
	}

	padding_type = phalcon_fetch_nproperty_this(this_ptr, SL("_padding"), PH_NOISY TSRMLS_CC);

	assert(Z_TYPE_P(padding_type) == IS_LONG);
	assert(Z_TYPE_P(block_size) == IS_LONG);
	assert(Z_TYPE_P(mode) == IS_STRING);
	assert(Z_TYPE_P(text) == IS_STRING);

	PHALCON_INIT_VAR(padded);
	phalcon_crypt_pad_text(padded, text, mode, Z_LVAL_P(block_size), Z_LVAL_P(padding_type) TSRMLS_CC);
	assert(Z_TYPE_P(padded) == IS_STRING);

	PHALCON_CALL_FUNCTION(&encrypt, "mcrypt_encrypt", cipher, encrypt_key, padded, mode, iv);

	PHALCON_CONCAT_VV(return_value, iv, encrypt);

	PHALCON_OBS_NVAR(handler);
	phalcon_read_property_this(&handler, this_ptr, SL("_afterEncrypt"), PH_NOISY TSRMLS_CC);

	if (phalcon_is_callable(handler TSRMLS_CC)) {
		PHALCON_INIT_NVAR(arguments);
		array_init_size(arguments, 1);
		phalcon_array_append(&arguments, return_value, 0);

		PHALCON_INIT_NVAR(value);
		PHALCON_CALL_USER_FUNC_ARRAY(value, handler, arguments);

		RETURN_CTOR(value);
	}

	RETURN_MM();
}
Пример #25
0
/**
 * Returns the cookie's value
 *
 * @param string|array $filters
 * @param string $defaultValue
 * @return mixed
 */
PHP_METHOD(Phalcon_Http_Cookie, getValue)
{
	zval *filters = NULL, *default_value = NULL, *restored, *dependency_injector = NULL, *readed, *name, *_COOKIE, value = {}, *encryption;
	zval service = {}, crypt = {}, decrypted_value = {}, filter = {};

	phalcon_fetch_params(0, 0, 2, &filters, &default_value);

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

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

	restored = phalcon_read_property(getThis(), SL("_restored"), PH_NOISY);
	if (!zend_is_true(restored)) {
		PHALCON_CALL_METHODW(NULL, getThis(), "restore");
	}

	dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY);

	if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_http_cookie_exception_ce, "A dependency injection object is required to access the 'filter' service");
		return;
	}

	readed = phalcon_read_property(getThis(), SL("_readed"), PH_NOISY);
	if (PHALCON_IS_FALSE(readed)) {
		name = phalcon_read_property(getThis(), SL("_name"), PH_NOISY);

		_COOKIE = phalcon_get_global_str(SL("_COOKIE"));
		if (phalcon_array_isset_fetch(&value, _COOKIE, name)) {
			encryption = phalcon_read_property(getThis(), SL("_useEncryption"), PH_NOISY);
			if (zend_is_true(encryption) && PHALCON_IS_NOT_EMPTY(&value)) {
				ZVAL_STRING(&service, "crypt");

				PHALCON_CALL_METHODW(&crypt, dependency_injector, "getshared", &service);
				PHALCON_VERIFY_INTERFACEW(&crypt, phalcon_cryptinterface_ce);

				/** 
				 * Decrypt the value also decoding it with base64
				 */
				PHALCON_CALL_METHODW(&decrypted_value, &crypt, "decryptbase64", &value);
			} else {
				PHALCON_CPY_WRT_CTOR(&decrypted_value, &value);
			}

			/** 
			 * Update the decrypted value
			 */
			phalcon_update_property_this(getThis(), SL("_value"), &decrypted_value);
			if (Z_TYPE_P(filters) != IS_NULL) {
				phalcon_return_property(&filter, getThis(), SL("_filter"));
				if (Z_TYPE(filter) != IS_OBJECT) {
					ZVAL_STRING(&service, ISV(filter));

					PHALCON_CALL_METHODW(&filter, dependency_injector, "getshared", &service);
					PHALCON_VERIFY_INTERFACEW(&filter, phalcon_filterinterface_ce);
					phalcon_update_property_this(getThis(), SL("_filter"), &filter);
				}

				PHALCON_RETURN_CALL_METHOD(&filter, "sanitize", &decrypted_value, filters);
				return;
			}

			/** 
			 * Return the value without filtering
			 */

			RETURN_CTORW(&decrypted_value);
		}

		RETURN_CTORW(default_value);
	}

	phalcon_return_property(&value, getThis(), SL("_value"));

	RETURN_CTORW(&value);
}