Example #1
0
/**
 * Returns a cached content
 *
 * @param int|string $keyName
 * @param   long $lifetime
 * @return  mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, get){

	zval *key_name, *lifetime = NULL, memcache = {}, frontend = {}, prefix = {}, prefixed_key = {}, cached_content = {};

	phalcon_fetch_params(0, 1, 1, &key_name, &lifetime);

	phalcon_return_property(&memcache, getThis(), SL("_memcache"));
	if (Z_TYPE(memcache) != IS_OBJECT) {
		PHALCON_CALL_METHODW(&memcache, getThis(), "_connect");
	}

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));
	phalcon_return_property(&prefix, getThis(), SL("_prefix"));

	PHALCON_CONCAT_VV(&prefixed_key, &prefix, key_name);
	phalcon_update_property_zval(getThis(), SL("_lastKey"), &prefixed_key);

	PHALCON_CALL_METHODW(&cached_content, &memcache, "get", &prefixed_key);
	if (PHALCON_IS_FALSE(&cached_content)) {
		RETURN_NULL();
	}

	if (phalcon_is_numeric(&cached_content)) {
		RETURN_CTORW(&cached_content);
	}

	PHALCON_RETURN_CALL_METHOD(&frontend, "afterretrieve", &cached_content);
}
Example #2
0
/**
 * Applies a format to a message before sent it to the internal log
 *
 * @param string $message
 * @param int $type
 * @param int $timestamp
 * @param array $context
 * @return string
 */
PHP_METHOD(Phalcon_Logger_Formatter_Line, format){

	zval *message, *type, *timestamp, *context, format = {}, date_format = {}, date = {}, date_wildcard = {}, new_format = {}, type_string = {}, type_wildcard = {}, message_wildcard = {};

	phalcon_fetch_params(0, 4, 0, &message, &type, &timestamp, &context);

	phalcon_return_property(&format, getThis(), SL("_format"));

	/** 
	 * Check if the format has the %date% placeholder
	 */
	if (phalcon_memnstr_str(&format, SL("%date%"))) {
		phalcon_return_property(&date_format, getThis(), SL("_dateFormat"));

		phalcon_date(&date, &date_format, timestamp);

		ZVAL_STRING(&date_wildcard, "%date%");

		PHALCON_STR_REPLACE(&new_format, &date_wildcard, &date, &format);
	} else {
		PHALCON_CPY_WRT(&new_format, &format);
	}

	/** 
	 * Check if the format has the %type% placeholder
	 */
	if (phalcon_memnstr_str(&format, SL("%type%"))) {
		PHALCON_CALL_METHODW(&type_string, getThis(), "gettypestring", type);

		ZVAL_STRING(&type_wildcard, "%type%");

		PHALCON_STR_REPLACE(&format, &type_wildcard, &type_string, &new_format);
	} else {
		PHALCON_CPY_WRT(&format, &new_format);
	}

	ZVAL_STRING(&message_wildcard, "%message%");

	PHALCON_STR_REPLACE(&new_format, &message_wildcard, message, &format);

	if (Z_TYPE_P(context) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(context)) > 0) {
		PHALCON_CALL_METHODW(&format, getThis(), "interpolate", &new_format, context);
	}
	else {
		PHALCON_CPY_WRT(&format, &new_format);
	}

	PHALCON_CONCAT_VS(return_value, &format, PHP_EOL);
}
Example #3
0
/**
 * Execute a render.
 *
 * @param string $type
 * @param int $quality
 * @return string
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _render) {

	zval *extension, *quality = NULL, *interlacing = NULL, ret = {}, type = {}, mime = {}, image = {}, tmp = {};
	const char *func_name = "imagegif";
	char *ext;

	phalcon_fetch_params(0, 1, 2, &extension, &quality, &interlacing);

	phalcon_fast_strtolower(&ret, extension);

	ext = Z_STRVAL(ret);

	if (strcmp(ext, "gif") == 0) {
		ZVAL_LONG(&type, 1);
		func_name = "imagegif";
	} else if (strcmp(ext, "jpg") == 0 || strcmp(ext, "jpeg") == 0) {
		ZVAL_LONG(&type, 2);
		func_name = "imagejpeg";
	} else if (strcmp(ext, "png") == 0) {
		ZVAL_LONG(&type, 3);
		func_name = "imagepng";
	} else {
		zend_throw_exception_ex(phalcon_image_exception_ce, 0, "Installed GD does not support '%s' images", Z_STRVAL_P(extension));
		return;
	}

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

	if (interlacing && Z_TYPE_P(interlacing) >= IS_NULL && Z_LVAL(type) > 1) {
		if (zend_is_true(interlacing)) {
			PHALCON_CALL_FUNCTIONW(NULL, "imageinterlace", &image, &PHALCON_GLOBAL(z_one));
		} else {
			PHALCON_CALL_FUNCTIONW(NULL, "imageinterlace", &image, &PHALCON_GLOBAL(z_zero));
		}
	}
	phalcon_ob_start();

	if (Z_LVAL(type) == 1 || !quality || Z_TYPE_P(quality) == IS_NULL) {
		PHALCON_CALL_FUNCTIONW(&ret, func_name, &image);
	} else {
		if (Z_LVAL(type) == 3) {
			ZVAL_LONG(&tmp, ceil(Z_LVAL_P(quality)/100*9));
			PHALCON_CALL_FUNCTIONW(&ret, func_name, &image, &PHALCON_GLOBAL(z_null), &tmp);
		} else {
			PHALCON_CALL_FUNCTIONW(&ret, func_name, &image, &PHALCON_GLOBAL(z_null), quality);
		}
	}

	phalcon_ob_get_contents(return_value);
	phalcon_ob_end_clean();

	if (zend_is_true(&ret)) {
		phalcon_update_property_zval(getThis(), SL("_type"), &type);

		PHALCON_CALL_FUNCTIONW(&mime, "image_type_to_mime_type", &type);
		phalcon_update_property_zval(getThis(), SL("_mime"), &mime);
	}

	return;
}
Example #4
0
/**
 * Phalcon\Http\Uri constructor
 *
 * @param mixed $uri
 */
PHP_METHOD(Phalcon_Http_Uri, __construct)
{
	zval *uri = NULL, parts = {}, query = {}, params = {};

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

	if (!uri || PHALCON_IS_EMPTY(uri)) {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	} else if (Z_TYPE_P(uri) == IS_STRING) {
		PHALCON_CALL_FUNCTIONW(&parts, "parse_url", uri);
		if (phalcon_array_isset_fetch_str(&query, &parts, SL("query"))) {
			ZVAL_MAKE_REF(&params);
			PHALCON_CALL_FUNCTIONW(NULL, "parse_str", &query, &params);
			ZVAL_UNREF(&params);
			phalcon_array_update_str(&parts, SL("query"), &params, PH_COPY);
		}

		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else if (Z_TYPE_P(uri) == IS_ARRAY) {
		phalcon_update_property_zval(getThis(), SL("_parts"), uri);
	} else if (Z_TYPE_P(uri) == IS_OBJECT && Z_OBJCE_P(uri) == phalcon_http_uri_ce) {
		phalcon_return_property(&parts, uri, SL("_parts"));
		phalcon_update_property_zval(getThis(), SL("_parts"), &parts);
	} else {
		phalcon_update_property_empty_array(getThis(), SL("_parts"));
	}
}
Example #5
0
/**
 * Initializes the internal handler, calling functions on it
 *
 * @param string $method
 * @param array $arguments
 * @return mixed
 */
PHP_METHOD(Phalcon_Mvc_Micro_LazyLoader, __call){

	zval *method, *arguments, handler = {}, definition = {}, call_handler = {};
	zend_class_entry *ce0;

	phalcon_fetch_params(0, 2, 0, &method, &arguments);

	phalcon_return_property(&handler, getThis(), SL("_handler"));
	if (Z_TYPE(handler) != IS_OBJECT) {
		phalcon_read_property(&definition, getThis(), SL("_definition"), PH_NOISY);
		ce0 = phalcon_fetch_class(&definition, ZEND_FETCH_CLASS_DEFAULT);

		PHALCON_OBJECT_INIT(&handler, ce0);
		if (phalcon_has_constructor(&handler)) {
			PHALCON_CALL_METHODW(NULL, &handler, "__construct");
		}
		phalcon_update_property_zval(getThis(), SL("_handler"), &handler);
	}

	array_init_size(&call_handler, 2);
	phalcon_array_append(&call_handler, &handler, PH_COPY);
	phalcon_array_append(&call_handler, method, PH_COPY);

	/** 
	 * Call the handler
	 */
	PHALCON_CALL_USER_FUNC_ARRAYW(return_value, &call_handler, arguments);
}
Example #6
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);
}
Example #7
0
/**
 * Writes parsed annotations to files
 *
 * @param string $key
 * @param Phalcon\Annotations\Reflection $data
 */
PHP_METHOD(Phalcon_Annotations_Adapter_Files, write){

	zval *key, *data, annotations_dir = {}, virtual_key = {}, path = {}, php_export = {}, status = {};
	smart_str exp = { 0 };

	phalcon_fetch_params(0, 2, 0, &key, &data);
	PHALCON_ENSURE_IS_STRING(key);

	phalcon_return_property(&annotations_dir, getThis(), SL("_annotationsDir"));
	
	/** 
	 * Paths must be normalized before be used as keys
	 */
	phalcon_prepare_virtual_path_ex(&virtual_key, Z_STRVAL_P(key), Z_STRLEN_P(key), '_');

	PHALCON_CONCAT_VVS(&path, &annotations_dir, &virtual_key, ".php");
	
	smart_str_appends(&exp, "<?php return ");
	php_var_export_ex(data, 0, &exp);
	smart_str_appendc(&exp, ';');
	smart_str_0(&exp);

	ZVAL_STR(&php_export, exp.s);

	phalcon_file_put_contents(&status, &path, &php_export);
	if (PHALCON_IS_FALSE(&status)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_annotations_exception_ce, "Annotations directory cannot be written");
		return;
	}
}
Example #8
0
/**
 * Changes a parameter in the definition without resolve the service
 *
 * @param long $position
 * @param array $parameter
 * @return Phalcon\DI\Service
 */
PHP_METHOD(Phalcon_DI_Service, setParameter){

	zval *position, *parameter, definition = {}, arguments = {};

	phalcon_fetch_params(0, 2, 0, &position, &parameter);
	PHALCON_ENSURE_IS_LONG(position);

	phalcon_return_property(&definition, getThis(), SL("_definition"));

	if (unlikely(Z_TYPE(definition) != IS_ARRAY)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "Definition must be an array to update its parameters");
		return;
	}

	if (unlikely(Z_TYPE_P(parameter) != IS_ARRAY)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "The parameter must be an array");
		return;
	}

	/* Update the parameter */
	if (phalcon_array_isset_fetch_str(&arguments, &definition, SL("arguments"))) {
		phalcon_array_update_zval(&arguments, position, parameter, PH_COPY);
	} else {
		array_init_size(&arguments, 1);
		phalcon_array_update_zval(&arguments, position, parameter, PH_COPY);
	}

	phalcon_array_update_str(&definition, SL("arguments"), &arguments, PH_COPY);

	phalcon_update_property_zval(getThis(), SL("_definition"), &definition);

	RETURN_THISW();
}
Example #9
0
/**
 * Destroys the loaded image to free up resources.
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, __destruct){

	zval image = {};

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

	if (Z_TYPE(image) == IS_RESOURCE) {
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
	}
}
Example #10
0
/**
 * Execute a background.
 *
 * @param int $r
 * @param int $g
 * @param int $b
 * @param int $opacity
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _background) {

	zval *r, *g, *b, *opacity, op = {}, image = {}, background = {}, width = {}, height = {}, color = {}, tmp = {}, blendmode = {}, ret = {};
	int int_opacity;
	double num;

	phalcon_fetch_params(0, 4, 0, &r, &g, &b, &opacity);

	phalcon_return_property(&image, getThis(), SL("_image"));
	phalcon_return_property(&width, getThis(), SL("_width"));
	phalcon_return_property(&height, getThis(), SL("_height"));

	int_opacity = Z_LVAL_P(opacity);

	num = (int_opacity * 127.0 / 100) - 127;

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

	int_opacity = (int)num;

	PHALCON_CALL_METHODW(&background, getThis(), "_create", &width, &height);

	ZVAL_LONG(&op, int_opacity);

	PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &background, r, g, b, &op);

	ZVAL_LONG(&tmp, 0);

	PHALCON_CALL_FUNCTIONW(NULL, "imagefilledrectangle", &background, &tmp, &tmp, &width, &height, &color);

	ZVAL_TRUE(&blendmode);

	PHALCON_CALL_FUNCTIONW(NULL, "imagealphablending", &background, &blendmode);
	PHALCON_CALL_FUNCTIONW(&ret, "imagecopy", &background, &image, &tmp, &tmp, &tmp, &tmp, &width, &height);

	if (zend_is_true(&ret)) {
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
		phalcon_update_property_zval(getThis(), SL("_image"), &background);
	}
}
Example #11
0
/**
 * Execute a watermarking.
 *
 * @param Phalcon\Image\Adapter $watermark
 * @param int $offset_x
 * @param int $offset_y
 * @param int $opacity
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _watermark) {

	zval *watermark, *offset_x = NULL, *offset_y = NULL, *opacity = NULL, op = {}, image = {}, overlay = {}, blob = {}, saveflag = {}, width = {}, height = {}, color = {}, tmp = {}, effect = {}, blendmode = {}, ret = {};
	int int_opacity;
	double num;

	phalcon_fetch_params(0, 4, 0, &watermark, &offset_x, &offset_y, &opacity);

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

	PHALCON_CALL_METHODW(&blob, watermark, "render");
	PHALCON_CALL_FUNCTIONW(&overlay, "imagecreatefromstring", &blob);

	ZVAL_TRUE(&saveflag);

	PHALCON_CALL_FUNCTIONW(NULL, "imagesavealpha", &overlay, &saveflag);
	PHALCON_CALL_FUNCTIONW(&width, "imagesx", &overlay);
	PHALCON_CALL_FUNCTIONW(&height, "imagesy", &overlay);

	int_opacity = Z_LVAL_P(opacity);
	if (int_opacity < 100) {
		num = (int_opacity * 127.0 / 100) - 127;

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

		int_opacity = (int)num;

		ZVAL_LONG(&op, int_opacity);
		ZVAL_LONG(&tmp, 127);

		PHALCON_CALL_FUNCTIONW(&color, "imagecolorallocatealpha", &overlay, &tmp, &tmp, &tmp, &op);

		if (!phalcon_get_constant(&effect, SL("IMG_EFFECT_OVERLAY"))) {
			return;
		}

		PHALCON_CALL_FUNCTIONW(NULL, "imagelayereffect", &overlay, &effect);

		ZVAL_LONG(&tmp, 0);

		PHALCON_CALL_FUNCTIONW(NULL, "imagefilledrectangle", &overlay, &tmp, &tmp, &width, &height, &color);
	}

	ZVAL_LONG(&blendmode, 1);
	PHALCON_CALL_FUNCTIONW(NULL, "imagealphablending", &image, &blendmode);

	ZVAL_LONG(&tmp, 0);

	PHALCON_CALL_FUNCTIONW(&ret, "imagecopy", &image, &overlay, offset_x, offset_y, &tmp, &tmp, &width, &height);

	RETVAL_BOOL(zend_is_true(&ret));
}
Example #12
0
/**
 * Pixelate image
 *
 * @param int $amount amount to pixelate
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _pixelate){

	zval *amount, image = {}, width = {}, height = {}, color = {};
	int a, x, y, x1, y1, w, h;

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

	phalcon_return_property(&image, getThis(), SL("_image"));
	phalcon_return_property(&width, getThis(), SL("_width"));
	phalcon_return_property(&height, getThis(), SL("_height"));

	a = phalcon_get_intval(amount);
	w = phalcon_get_intval(&width);
	h = phalcon_get_intval(&height);

	for(x = 0; x < w; x += a) {
		for (y = 0; y < h; y += a) {
			zval tmp1 = {}, tmp2 = {}, tmp3 = {}, tmp4 = {};
			x1 = (int)(x + a/2 + 0.5);
			y1 = (int)(y + a/2 + 0.5);

			ZVAL_LONG(&tmp1, x1)
			ZVAL_LONG(&tmp2, y1)

			PHALCON_CALL_FUNCTIONW(&color, "imagecolorat", &image, &tmp1, &tmp2);

			ZVAL_LONG(&tmp1, x)
			ZVAL_LONG(&tmp2, y)

			x1 = x + a;
			y1 = y + a;

			ZVAL_LONG(&tmp3, x1)
			ZVAL_LONG(&tmp4, y1)

			PHALCON_CALL_FUNCTIONW(NULL, "imagefilledrectangle", &image, &tmp1, &tmp2, &tmp3, &tmp4, &color);
		}
	}
}
Example #13
0
/**
 * Returns cached content
 *
 * @param string $keyName
 * @param long $lifetime
 * @return mixed
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, get){

	zval *key_name, *lifetime = NULL, frontend = {}, prefix = {}, prefixed_key = {}, cached_content = {};

	phalcon_fetch_params(0, 1, 1, &key_name, &lifetime);

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));
	phalcon_return_property(&prefix, getThis(), SL("_prefix"));

	PHALCON_CONCAT_SVV(&prefixed_key, "_PHCX", &prefix, key_name);
	phalcon_update_property_zval(getThis(), SL("_lastKey"), &prefixed_key);

	PHALCON_CALL_FUNCTIONW(&cached_content, "xcache_get", &prefixed_key);
	if (Z_TYPE(cached_content) == IS_NULL) {
		RETURN_NULL();
	}

	if (phalcon_is_numeric(&cached_content)) {
		RETURN_CTORW(&cached_content);
	} else {
		PHALCON_RETURN_CALL_METHODW(&frontend, "afterretrieve", &cached_content);
	}
}
Example #14
0
/**
 * Returns the internal formatter
 *
 * @return Phalcon\Logger\Formatter\Line
 */
PHP_METHOD(Phalcon_Logger_Adapter_Stream, getFormatter)
{
	zval formatter = {};

	phalcon_return_property(&formatter, getThis(), SL("_formatter"));
	if (Z_TYPE(formatter) != IS_OBJECT) {
		object_init_ex(&formatter, phalcon_logger_formatter_line_ce);
		PHALCON_CALL_METHODW(NULL, &formatter, "__construct");

		phalcon_update_property_this(getThis(), SL("_formatter"), &formatter);
	}

	RETURN_CTORW(&formatter);
}
Example #15
0
/**
 * Draws a polygon
 *
 *<code>
 * $coordinates = array( array( 'x' => 4, 'y' => 6 ), array( 'x' => 8, 'y' => 10 ) );
 * $image->polygon($coordinates);
 *</code>
 *
 * @param array $coordinates array of x and y
 * @param string $color
 * @return Phalcon\Image\Adapter\GD
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, polygon){

	zval *coordinates, *color = NULL, image = {}, rgb = {}, r = {}, g = {}, b = {}, imagecolor = {}, *point, points = {}, num_points = {};

	phalcon_fetch_params(0, 1, 1, &coordinates, &color);

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

	if (!phalcon_fast_count_ev(coordinates)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates must be not empty");
		return;
	}
	phalcon_return_property(&image, getThis(), SL("_image"));

	if (Z_TYPE(image) == IS_RESOURCE) {
		PHALCON_CALL_METHODW(&rgb, getThis(), "getcolorrbg", color);
		phalcon_array_fetch_long(&r, &rgb, 0, PH_NOISY);
		phalcon_array_fetch_long(&g, &rgb, 1, PH_NOISY);
		phalcon_array_fetch_long(&b, &rgb, 2, PH_NOISY);

		PHALCON_CALL_FUNCTIONW(&imagecolor, "imagecolorallocate", &image, &r, &g, &b);

		array_init(&points);
		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(coordinates), point) {
			zval x = {}, y = {};
			if (Z_TYPE_P(point) == IS_ARRAY) {
				if (phalcon_fast_count_int(point) != 2) {
					PHALCON_THROW_EXCEPTION_STRW(phalcon_image_exception_ce, "Coordinates point error");
					return;
				}
				if (!phalcon_array_isset_fetch_long(&x, point, 0)) {
					phalcon_array_fetch_str(&x, point, SL("x"), PH_NOISY);
				}
				if (!phalcon_array_isset_fetch_long(&y, point, 0)) {
					phalcon_array_fetch_str(&y, point, SL("y"), PH_NOISY);
				}
				phalcon_array_append(&points, &x, PH_COPY);
				phalcon_array_append(&points, &y, PH_COPY);
			} else {
				phalcon_array_append(&points, &_p->val, PH_COPY);
				_p++;
				phalcon_array_append(&points, &_p->val, PH_COPY);
			}
			phalcon_increment(&num_points);
		} ZEND_HASH_FOREACH_END();
Example #16
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);
}
Example #17
0
/**
 * Reads the cookie-related info from the SESSION to restore the cookie as it was set
 * This method is automatically called internally so normally you don't need to call it
 *
 * @return Phalcon\Http\Cookie
 */
PHP_METHOD(Phalcon_Http_Cookie, restore)
{
	zval *restored, *dependency_injector, service = {}, session = {}, name = {}, key = {}, definition = {}, expire = {}, domain = {}, path = {}, secure = {}, http_only = {};

	restored = phalcon_read_property(getThis(), SL("_restored"), PH_NOISY);
	if (!zend_is_true(restored)) {
		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(&session, dependency_injector, "getshared", &service);
			PHALCON_VERIFY_INTERFACEW(&session, phalcon_session_adapterinterface_ce);

			phalcon_return_property(&name, getThis(), SL("_name"));

			PHALCON_CONCAT_SV(&key, "_PHCOOKIE_", &name);

			PHALCON_CALL_METHODW(&definition, &session, "get", &key);
			if (Z_TYPE(definition) == IS_ARRAY) { 
				if (phalcon_array_isset_fetch_str(&expire, &definition, SL("expire"))) {
					phalcon_update_property_this(getThis(), SL("_expire"), &expire);
				}
				if (phalcon_array_isset_fetch_str(&domain, &definition, SL("domain"))) {
					phalcon_update_property_this(getThis(), SL("_domain"), &domain);
				}

				if (phalcon_array_isset_fetch_str(&path, &definition, SL("path"))) {
					phalcon_update_property_this(getThis(), SL("_path"), &path);
				}

				if (phalcon_array_isset_fetch_str(&secure, &definition, SL("secure"))) {
					phalcon_update_property_this(getThis(), SL("_secure"), &secure);
				}

				if (phalcon_array_isset_fetch_str(&http_only, &definition, SL("httpOnly"))) {
					phalcon_update_property_this(getThis(), SL("_httpOnly"), &http_only);
				}
			}
		}

		phalcon_update_property_bool(getThis(), SL("_restored"), 1);
	}

	RETURN_THISW();
}
Example #18
0
/**
 * Execute a resize.
 *
 * @param int $width
 * @param int $height
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _resize) {

	zval *width, *height, image = {}, tmp_image = {};

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

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

	PHALCON_CALL_FUNCTIONW(&tmp_image, "imagescale", &image, width, height);

	if (Z_TYPE(tmp_image) == IS_RESOURCE) {
		PHALCON_CALL_FUNCTIONW(NULL, "imagedestroy", &image);
		phalcon_update_property_zval(getThis(), SL("_image"), &tmp_image);

		phalcon_update_property_zval(getThis(), SL("_width"), width);
		phalcon_update_property_zval(getThis(), SL("_height"), height);
	}
}
Example #19
0
/**
 * Blur image
 *
 * @param int $radius Blur radius
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _blur){

	zval *radius, image = {}, constant = {};
	int r, i;

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

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

	if (!phalcon_get_constant(&constant, SL("IMG_FILTER_GAUSSIAN_BLUR"))) {
		return;
	}

	r = phalcon_get_intval(radius);

	for (i = 0; i < r; i++) {
		PHALCON_CALL_FUNCTIONW(NULL, "imagefilter", &image, &constant);
	}
}
Example #20
0
/**
 * Execute a flip.
 *
 * @param int $direction
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, _flip) {

	zval *direction, image = {}, mode = {};

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

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

	if (Z_LVAL_P(direction) == PHALCON_IMAGE_HORIZONTAL) {
		if (!phalcon_get_constant(&mode, SL("IMG_FLIP_HORIZONTAL"))) {
			return;
		}
	} else {
		if (!phalcon_get_constant(&mode, SL("IMG_FLIP_VERTICAL"))) {
			return;
		}
	}

	PHALCON_CALL_FUNCTIONW(NULL, "imageflip", &image, &mode);
}
Example #21
0
/**
 * Returns a parameter in a specific position
 *
 * @param int $position
 * @return array
 */
PHP_METHOD(Phalcon_DI_Service, getParameter){

	zval *position, definition = {}, arguments = {};

	phalcon_fetch_params(0, 1, 0, &position);
	PHALCON_ENSURE_IS_LONG(position);

	phalcon_return_property(&definition, getThis(), SL("_definition"));
	if (Z_TYPE(definition) != IS_ARRAY) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_di_exception_ce, "Definition must be an array to obtain its parameters");
		return;
	}

	/* Update the parameter */
	if (
		!phalcon_array_isset_fetch_str(&arguments, &definition, SL("arguments")) ||
		!phalcon_array_isset_fetch(return_value, &arguments, position, 0)
	) {
		RETURN_NULL();
	}
}
Example #22
0
/**
 * Reads parsed annotations from files
 *
 * @param string $key
 * @return Phalcon\Annotations\Reflection
 */
PHP_METHOD(Phalcon_Annotations_Adapter_Files, read){

	zval *key, annotations_dir = {}, virtual_key = {}, path = {};

	phalcon_fetch_params(0, 1, 0, &key);
	PHALCON_ENSURE_IS_STRING(key);

	phalcon_return_property(&annotations_dir, getThis(), SL("_annotationsDir"));
	
	/** 
	 * Paths must be normalized before be used as keys
	 */
	phalcon_prepare_virtual_path_ex(&virtual_key, Z_STRVAL_P(key), Z_STRLEN_P(key), '_');
	
	PHALCON_CONCAT_VVS(&path, &annotations_dir, &virtual_key, ".php");
	
	if (phalcon_file_exists(&path) == SUCCESS) {
		RETURN_ON_FAILURE(phalcon_require_ret(return_value, Z_STRVAL(path)));
		return;
	}
}
Example #23
0
/**
 * Draws a line
 *
 * @param int $sx
 * @param int $sy
 * @param int $ex
 * @param int $ey
 * @param string $color
 * @return Phalcon\Image\Adapter\GD
 */
PHP_METHOD(Phalcon_Image_Adapter_GD, line){

	zval *sx, *sy, *ex, *ey, *color = NULL, image = {}, rgb = {}, r = {}, g = {}, b = {}, imagecolor = {};

	phalcon_fetch_params(0, 4, 1, &sx, &sy, &ex, &ey, &color);

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

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

	if (Z_TYPE(image) == IS_RESOURCE) {
		PHALCON_CALL_METHODW(&rgb, getThis(), "getcolorrbg", color);
		phalcon_array_fetch_long(&r, &rgb, 0, PH_NOISY);
		phalcon_array_fetch_long(&g, &rgb, 1, PH_NOISY);
		phalcon_array_fetch_long(&b, &rgb, 2, PH_NOISY);

		PHALCON_CALL_FUNCTIONW(&imagecolor, "imagecolorallocate", &image, &r, &g, &b);
		PHALCON_CALL_FUNCTIONW(NULL, "imageline", &image, sy, sx, ey, ex, &imagecolor);
	}
	RETURN_THISW();
}
Example #24
0
/**
 * Create internal connection to memcached
 */
PHP_METHOD(Phalcon_Cache_Backend_Memcache, _connect)
{
	zval options = {}, memcache = {}, host = {}, port = {}, persistent = {}, success = {};
	zend_class_entry *ce0;

	phalcon_return_property(&options, getThis(), SL("_options"));
	ce0 = phalcon_fetch_str_class(SL("Memcache"), ZEND_FETCH_CLASS_AUTO);

	object_init_ex(&memcache, ce0);
	if (phalcon_has_constructor(&memcache)) {
		PHALCON_CALL_METHODW(NULL, &memcache, "__construct");
	}

	if (
		   !phalcon_array_isset_fetch_str(&host, &options, SL("host"))
		|| !phalcon_array_isset_fetch_str(&port, &options, SL("port"))
		|| !phalcon_array_isset_fetch_str(&persistent, &options, SL("persistent"))
	) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
		return;
	}

	if (zend_is_true(&persistent)) {
		PHALCON_CALL_METHODW(&success, &memcache, "pconnect", &host, &port);
	} else {
		PHALCON_CALL_METHODW(&success, &memcache, "connect", &host, &port);
	}

	if (!zend_is_true(&success)) {
		PHALCON_THROW_EXCEPTION_STR(phalcon_cache_exception_ce, "Cannot connect to Memcached server");
		return;
	}

	phalcon_update_property_zval(getThis(), SL("_memcache"), &memcache);
	RETURN_CTORW(&memcache);
}
Example #25
0
/**
 * Stores cached content into the XCache backend and stops the frontend
 *
 * @param string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Xcache, save){

	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL, cached_content = {}, keys = {}, last_key = {}, frontend = {};
	zval prepared_content = {}, ttl = {}, success = {}, is_buffering = {}, prefix = {}, options = {}, special_key = {};

	phalcon_fetch_params(0, 0, 4, &key_name, &content, &lifetime, &stop_buffer);

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		phalcon_return_property(&last_key, getThis(), SL("_lastKey"));
	} else {
		phalcon_return_property(&prefix, getThis(), SL("_prefix"));
		PHALCON_CONCAT_SVV(&last_key, "_PHCX", &prefix, key_name);
	}

	if (!zend_is_true(&last_key)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}

	phalcon_return_property(&frontend, getThis(), SL("_frontend"));
	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHODW(&cached_content, &frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(&cached_content, content);
	}

	if (!phalcon_is_numeric(&cached_content)) {
		PHALCON_CALL_METHODW(&prepared_content, &frontend, "beforestore", &cached_content);
	}

	/** 
	 * Take the lifetime from the frontend or read it from the set in start()
	 */
	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		phalcon_return_property(&ttl, getThis(), SL("_lastLifetime"));

		if (Z_TYPE(ttl) <= IS_NULL) {
			PHALCON_CALL_METHODW(&ttl, &frontend, "getlifetime");
		}
	} else {
		PHALCON_CPY_WRT(&ttl, lifetime);
	}

	if (Z_TYPE(prepared_content) > IS_NULL) {
		PHALCON_CALL_FUNCTIONW(&success, "xcache_set", &last_key, &prepared_content, &ttl);
	} else {
		PHALCON_CALL_FUNCTIONW(&success, "xcache_set", &last_key, &cached_content, &ttl);
	}

	PHALCON_CALL_METHODW(&is_buffering, &frontend, "isbuffering");
	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHODW(NULL, &frontend, "stop");
	}

	if (PHALCON_IS_TRUE(&is_buffering)) {
		zend_print_zval(&cached_content, 0);
	}

	phalcon_update_property_bool(getThis(), SL("_started"), 0);

	if (zend_is_true(&success)) {
		phalcon_return_property(&options, getThis(), SL("_options"));

		if (unlikely(!phalcon_array_isset_fetch_str(&special_key, &options, SL("statsKey")))) {
			PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "Unexpected inconsistency in options");
			return;
		}

		/** 
		 * xcache_list() is available only to the administrator (unless XCache was
		 * patched). We have to update the list of the stored keys.
		 */
		PHALCON_CALL_FUNCTIONW(&keys, "xcache_get", &special_key);
		if (Z_TYPE(keys) != IS_ARRAY) {
			array_init(&keys);
		}

		phalcon_array_update_zval(&keys, &last_key, &ttl, PH_COPY);
		PHALCON_CALL_FUNCTIONW(NULL, "xcache_set", &special_key, &keys, &PHALCON_GLOBAL(z_zero));
	}
}
Example #26
0
/**
 * Prevernt cross-site scripting (XSS) attacks
 */
void phalcon_xss_clean(zval *return_value, zval *str, zval *allow_tags, zval *allow_attributes)
{
	zval document = {}, ret = {}, tmp = {}, elements = {}, matched = {}, regexp = {}, joined_tags = {}, clean_str = {};
	zend_class_entry *ce0;
	int i, element_length;

	ce0 = phalcon_fetch_str_class(SL("DOMDocument"), ZEND_FETCH_CLASS_AUTO);

	object_init_ex(&document, ce0);
	PHALCON_CALL_METHODW(NULL, &document, "__construct");

	phalcon_update_property_bool(&document, SL("strictErrorChecking"), 0);

	if (phalcon_function_exists_ex(SL("libxml_use_internal_errors")) == SUCCESS) {
		PHALCON_CALL_FUNCTIONW(NULL, "libxml_use_internal_errors", &PHALCON_GLOBAL(z_true));
	}

	PHALCON_CALL_METHODW(&ret, &document, "loadhtml", str);

	if (phalcon_function_exists_ex(SL("libxml_clear_errors")) == SUCCESS) {
		PHALCON_CALL_FUNCTIONW(NULL, "libxml_clear_errors");
	}

	if (!zend_is_true(&ret)) {
		return;
	}

	PHALCON_STR(&tmp, "*");

	PHALCON_CALL_METHODW(&elements, &document, "getelementsbytagname", &tmp);

	PHALCON_STR(&regexp, "/e.*x.*p.*r.*e.*s.*s.*i.*o.*n/i");

	phalcon_return_property(&tmp, &elements, SL("length"));

	element_length = Z_LVAL_P(&tmp);

	for (i = 0; i < element_length; i++) {
		zval t = {}, element = {}, element_name = {}, element_attrs = {};
		int element_attrs_length, j;

		ZVAL_LONG(&t, i);
	
		PHALCON_CALL_METHODW(&element, &elements, "item", &t);

		phalcon_return_property(&element_name, &element, SL("nodeName"));

		if (Z_TYPE_P(allow_tags) == IS_ARRAY && !phalcon_fast_in_array(&element_name, allow_tags)) {
			continue;
		}

		phalcon_return_property(&element_attrs, &element, SL("attributes"));
		phalcon_return_property(&tmp, &element_attrs, SL("length"));

		element_attrs_length = Z_LVAL_P(&tmp);

		for (j = 0; j < element_attrs_length; j++) {
			zval t2 = {}, element_attr = {}, element_attr_name = {}, element_attr_value = {};
			ZVAL_LONG(&t2, j);

			PHALCON_CALL_METHODW(&element_attr, &element_attrs, "item", &t2);

			phalcon_return_property(&element_attr_name, &element_attr, SL("nodeName"));
			if (Z_TYPE_P(allow_attributes) == IS_ARRAY && !phalcon_fast_in_array(&element_attr_name, allow_attributes)) {
				PHALCON_CALL_METHODW(NULL, &element, "removeattributenode", &element_attr);
			} else if (phalcon_memnstr_str(&element_attr_name, SL("style"))) {
				phalcon_return_property(&element_attr_value, &element_attr, SL("nodeValue"));

				RETURN_ON_FAILURE(phalcon_preg_match(&matched, &regexp, &element_attr_value, NULL));

				if (zend_is_true(&matched)) {
					PHALCON_CALL_METHODW(NULL, &element, "removeattributenode", &element_attr);
				}
			}
		}
	}

	phalcon_fast_join_str(&tmp, SL("><"), allow_tags);

	PHALCON_CONCAT_SVS(&joined_tags, "<", &tmp, ">");

	PHALCON_CALL_METHODW(&ret, &document, "savehtml");
	PHALCON_CALL_FUNCTIONW(&clean_str, "strip_tags", &ret, &joined_tags);

	ZVAL_STR(return_value, phalcon_trim(&clean_str, NULL, PHALCON_TRIM_BOTH));
}
Example #27
0
/**
 * Stores cached content into the APC backend and stops the frontend
 *
 * @param string $keyName
 * @param string $content
 * @param long $lifetime
 * @param boolean $stopBuffer
 */
PHP_METHOD(Phalcon_Cache_Backend_Apc, save)
{
	zval *key_name = NULL, *content = NULL, *lifetime = NULL, *stop_buffer = NULL, prefix = {}, frontend = {};
	zval last_key = {}, cached_content = {}, prepared_content = {}, last_lifetime = {}, ttl, is_buffering = {};

	phalcon_fetch_params(0, 0, 4, &key_name, &content, &lifetime, &stop_buffer);

	if (!key_name || Z_TYPE_P(key_name) == IS_NULL) {
		phalcon_return_property(&last_key, getThis(), SL("_lastKey"));
	} else {
		phalcon_read_property(&prefix, getThis(), SL("_prefix"), PH_NOISY);

		PHALCON_CONCAT_SVV(&last_key, "_PHCA", &prefix, key_name);
	}

	if (!zend_is_true(&last_key)) {
		PHALCON_THROW_EXCEPTION_STRW(phalcon_cache_exception_ce, "The cache must be started first");
		return;
	}

	phalcon_read_property(&frontend, getThis(), SL("_frontend"), PH_NOISY);
	if (!content || Z_TYPE_P(content) == IS_NULL) {
		PHALCON_CALL_METHODW(&cached_content, &frontend, "getcontent");
	} else {
		PHALCON_CPY_WRT(&cached_content, content);
	}

	if (!phalcon_is_numeric(&cached_content)) {
		PHALCON_CALL_METHODW(&prepared_content, &frontend, "beforestore", &cached_content);
	} else {
		PHALCON_CPY_WRT(&prepared_content, &cached_content);
	}

	/** 
	 * Take the lifetime from the frontend or read it from the set in start()
	 */
	if (!lifetime || Z_TYPE_P(lifetime) == IS_NULL) {
		phalcon_read_property(&last_lifetime, getThis(), SL("_lastLifetime"), PH_NOISY);

		if (Z_TYPE(last_lifetime) == IS_NULL) {
			PHALCON_CALL_METHODW(&ttl, &frontend, "getlifetime");
		} else {
			PHALCON_CPY_WRT(&ttl, &last_lifetime);
		}
	} else {
		PHALCON_CPY_WRT(&ttl, lifetime);
	}

	/** 
	 * Call apc_store in the PHP userland since most of the time it isn't available at
	 * compile time
	 */
	PHALCON_CALL_FUNCTIONW(NULL, "apc_store", &last_key, &prepared_content, &ttl);

	PHALCON_CALL_METHODW(&is_buffering, &frontend, "isbuffering");
	if (!stop_buffer || PHALCON_IS_TRUE(stop_buffer)) {
		PHALCON_CALL_METHODW(NULL, &frontend, "stop");
	}

	if (PHALCON_IS_TRUE(&is_buffering)) {
		zend_print_zval(&cached_content, 0);
	}

	phalcon_update_property_bool(getThis(), SL("_started"), 0);
}
Example #28
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);
}
Example #29
0
/**
 * Returns a slice of the resultset to show in the pagination
 *
 * @return stdClass
 */
PHP_METHOD(Phalcon_Paginator_Adapter_Sql, getPaginate){

	zval db = {}, sql = {}, total_sql = {}, bind = {}, limit = {}, number_page = {}, number = {}, fetch_mode = {}, items = {}, row = {}, rowcount = {};
	long int i_limit, i_number_page, i_number, i_before, i_rowcount;
	long int i_total_pages, i_next;
	ldiv_t tp;

	phalcon_return_property(&db, getThis(), SL("_db"));
	phalcon_return_property(&sql, getThis(), SL("_sql"));
	phalcon_return_property(&total_sql, getThis(), SL("_total_sql"));
	phalcon_return_property(&bind, getThis(), SL("_bind"));
	phalcon_return_property(&limit, getThis(), SL("_limitRows"));
	phalcon_return_property(&number_page, getThis(), SL("_page"));
	phalcon_return_property(&fetch_mode, getThis(), SL("_fetchMode"));

	i_limit       = phalcon_get_intval(&limit);
	i_number_page = phalcon_get_intval(&number_page);

	if (i_limit < 1) {
		/* This should never happen unless someone deliberately modified the properties of the object */
		i_limit = 10;
	}

	if (!i_number_page) {
		i_number_page = 1;
	}

	i_number = (i_number_page - 1) * i_limit;
	i_before = (i_number_page == 1) ? 1 : (i_number_page - 1);

	PHALCON_CALL_METHODW(&row, &db, "fetchone", &total_sql, &fetch_mode, &bind);

	phalcon_return_property(&rowcount, &row, SL("rowcount"));

	PHALCON_SEPARATE(&bind);
	/* Set the limit clause avoiding negative offsets */
	if (i_number < i_limit) {
		phalcon_array_update_str(&bind, SL("limit"), &limit, PH_COPY);
		phalcon_array_update_str_long(&bind, SL("offset"), 0, 0);
	} else {
		ZVAL_LONG(&number, i_number);
		phalcon_array_update_str(&bind, SL("limit"), &limit, PH_COPY);
		phalcon_array_update_str(&bind, SL("offset"), &number, PH_COPY);
	}

	PHALCON_CALL_METHODW(&items, &db, "fetchall", &sql, &fetch_mode, &bind);
	
	i_rowcount    = phalcon_get_intval(&rowcount);
	tp            = ldiv(i_rowcount, i_limit);
	i_total_pages = tp.quot + (tp.rem ? 1 : 0);
	i_next        = (i_number_page < i_total_pages) ? (i_number_page + 1) : i_total_pages;

	object_init(return_value);
	phalcon_update_property_zval(return_value, SL("items"),       &items);
	phalcon_update_property_long(return_value, SL("before"),      i_before);
	phalcon_update_property_long(return_value, SL("first"),       1);
	phalcon_update_property_long(return_value, SL("next"),        i_next);
	phalcon_update_property_long(return_value, SL("last"),        i_total_pages);
	phalcon_update_property_long(return_value, SL("current"),     i_number_page);
	phalcon_update_property_long(return_value, SL("total_pages"), i_total_pages);
	phalcon_update_property_long(return_value, SL("total_items"), i_rowcount);
}
Example #30
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();
	}