/** * Executes the validation * * @param string $value * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_Regex, valid){ zval *value, *pattern; zval *matches, *match_pattern, *match_zero, *valid; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &value, &pattern); PHALCON_INIT_VAR(matches); /* Check if the value match using preg_match in the PHP userland */ PHALCON_INIT_VAR(match_pattern); RETURN_MM_ON_FAILURE(phalcon_preg_match(match_pattern, pattern, value, matches)); if (zend_is_true(match_pattern)) { PHALCON_OBS_VAR(match_zero); phalcon_array_fetch_long(&match_zero, matches, 0, PH_NOISY); PHALCON_INIT_VAR(valid); is_not_equal_function(valid, match_zero, value); if (PHALCON_IS_FALSE(valid)) { RETURN_MM_TRUE; } } RETURN_MM_FALSE; }
/** * Generates a URL for a static resource * * @param string|array $uri * @param array $args * @return string */ PHP_METHOD(Phalcon_Mvc_Url, getStatic){ zval *uri = NULL, *args = NULL, *static_base_uri, *base_uri = NULL; zval *matched, *pattern, *query_string; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 2, &uri, &args); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } else { PHALCON_ENSURE_IS_STRING(uri); if (strstr(Z_STRVAL_P(uri), "://")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^[^:\\/?#]++:/"); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, pattern, uri, NULL)); if (zend_is_true(matched)) { RETURN_CTOR(uri); } } } static_base_uri = phalcon_read_property(getThis(), SL("_staticBaseUri"), PH_NOISY); if (Z_TYPE_P(static_base_uri) != IS_NULL) { PHALCON_CONCAT_VV(return_value, static_base_uri, uri); } else { PHALCON_CALL_METHOD(&base_uri, getThis(), "getbaseuri"); PHALCON_CONCAT_VV(return_value, base_uri, uri); } if (args) { PHALCON_INIT_VAR(query_string); phalcon_http_build_query(query_string, args, "&"); if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) { if (phalcon_memnstr_str(return_value, "?", 1)) { PHALCON_SCONCAT_SV(return_value, "&", query_string); } else { PHALCON_SCONCAT_SV(return_value, "?", query_string); } } } RETURN_MM(); }
/** * 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; }
/** * Generates a URL for a static resource * * @param string|array $uri * @return string */ PHP_METHOD(Phalcon_Mvc_Url, getStatic){ zval **uri = NULL, *static_base_uri, *base_uri = NULL; zval *matched, *pattern; phalcon_fetch_params_ex(0, 1, &uri); PHALCON_MM_GROW(); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } else { PHALCON_ENSURE_IS_STRING(uri); if (strstr(Z_STRVAL_PP(uri), "://")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^[^:\\/?#]++:/", 1); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, pattern, *uri, NULL TSRMLS_CC)); if (zend_is_true(matched)) { RETURN_CTOR(*uri); } } } static_base_uri = phalcon_fetch_nproperty_this(this_ptr, SL("_staticBaseUri"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(static_base_uri) != IS_NULL) { PHALCON_CONCAT_VV(return_value, static_base_uri, *uri); RETURN_MM(); } PHALCON_CALL_METHOD(&base_uri, this_ptr, "getbaseuri"); PHALCON_CONCAT_VV(return_value, base_uri, *uri); RETURN_MM(); }
/** * Executes the validation * * @param string $value * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_Regex, valid){ zval *value, *pattern, matches = {}, match_pattern = {}, match_zero = {}, valid = {}; phalcon_fetch_params(0, 2, 0, &value, &pattern); /* Check if the value match using preg_match in the PHP userland */ ZVAL_NULL(&matches); ZVAL_MAKE_REF(&matches); RETURN_ON_FAILURE(phalcon_preg_match(&match_pattern, pattern, value, &matches)); ZVAL_UNREF(&matches); if (zend_is_true(&match_pattern)) { phalcon_array_fetch_long(&match_zero, &matches, 0, PH_NOISY); is_not_equal_function(&valid, &match_zero, value); if (PHALCON_IS_FALSE(&valid)) { RETURN_TRUE; } } RETURN_FALSE; }
/** * 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(®exp, "/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, ®exp, &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)); }
/** * Returns an array of Phalcon\Db\Column objects describing a table * * <code> * print_r($connection->describeColumns("posts")); ?> * </code> * * @param string $table * @param string $schema * @return Phalcon\Db\Column[] */ PHP_METHOD(Phalcon_Db_Adapter_Pdo_Mysql, describeColumns){ zval *table, *schema = NULL, *dialect, *ztrue, *sql, *fetch_num; zval *describe, *old_column = NULL, *size_pattern, *columns; zval *field = NULL, *definition = NULL, *column_type = NULL, *matches = NULL; zval *pos = NULL, *match_one = NULL, *match_two = NULL, *attribute = NULL, *column_name = NULL; zval *column = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &table, &schema); if (!schema) { PHALCON_INIT_VAR(schema); } PHALCON_OBS_VAR(dialect); phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); PHALCON_INIT_VAR(ztrue); ZVAL_BOOL(ztrue, 1); /** * Get the SQL to describe a table */ PHALCON_INIT_VAR(sql); phalcon_call_method_p2(sql, dialect, "describecolumns", table, schema); /** * We're using FETCH_NUM to fetch the columns */ PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, 3); /** * Get the describe */ PHALCON_INIT_VAR(describe); phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num); PHALCON_INIT_VAR(old_column); PHALCON_INIT_VAR(size_pattern); ZVAL_STRING(size_pattern, "#\\(([0-9]++)(?:,\\s*([0-9]++))?\\)#", 1); PHALCON_INIT_VAR(columns); array_init(columns); /** * Field Indexes: 0:name, 1:type, 2:not null, 3:key, 4:default, 5:extra */ phalcon_is_iterable(describe, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(field); /** * By default the bind types is two */ PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); /** * By checking every column type we convert it to a Phalcon\Db\Column */ PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY); while (1) { /** * Enum are treated as char */ if (phalcon_memnstr_str(column_type, SL("enum"))) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE); break; } /** * Smallint/Bigint/Integers/Int are int */ if (phalcon_memnstr_str(column_type, SL("int"))) { phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE); phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE); break; } /** * Varchar are varchars */ if (phalcon_memnstr_str(column_type, SL("varchar"))) { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE); break; } /** * Special type for datetime */ if (phalcon_memnstr_str(column_type, SL("datetime"))) { phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE); break; } /** * Decimals are floats */ if (phalcon_memnstr_str(column_type, SL("decimal"))) { phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE); phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE); break; } /** * Chars are chars */ if (phalcon_memnstr_str(column_type, SL("char"))) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE); break; } /** * Date/Datetime are varchars */ if (phalcon_memnstr_str(column_type, SL("date"))) { phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE); break; } /** * Text are varchars */ if (phalcon_memnstr_str(column_type, SL("text"))) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE); break; } /** * Float/Smallfloats/Decimals are float */ if (phalcon_memnstr_str(column_type, SL("float"))) { phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE); phalcon_array_update_string(&definition, SL("isNumeric"), &ztrue, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE); break; } /** * By default is string */ phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE); break; } /** * If the column type has a parentheses we try to get the column size from it */ if (phalcon_memnstr_str(column_type, SL("("))) { PHALCON_INIT_NVAR(matches); PHALCON_INIT_NVAR(pos); phalcon_preg_match(pos, size_pattern, column_type, matches TSRMLS_CC); if (zend_is_true(pos)) { if (phalcon_array_isset_long(matches, 1)) { PHALCON_OBS_NVAR(match_one); phalcon_array_fetch_long(&match_one, matches, 1, PH_NOISY); phalcon_array_update_string(&definition, SL("size"), &match_one, PH_COPY | PH_SEPARATE); } if (phalcon_array_isset_long(matches, 2)) { PHALCON_OBS_NVAR(match_two); phalcon_array_fetch_long(&match_two, matches, 2, PH_NOISY); phalcon_array_update_string(&definition, SL("scale"), &match_two, PH_COPY | PH_SEPARATE); } } } /** * Check if the column is unsigned, only MySQL support this */ if (phalcon_memnstr_str(column_type, SL("unsigned"))) { phalcon_array_update_string(&definition, SL("unsigned"), &ztrue, PH_COPY | PH_SEPARATE); } /** * Positions */ if (!zend_is_true(old_column)) { phalcon_array_update_string(&definition, SL("first"), &ztrue, PH_COPY | PH_SEPARATE); } else { phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE); } /** * Check if the field is primary key */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 3, PH_NOISY); if (PHALCON_IS_STRING(attribute, "PRI")) { phalcon_array_update_string(&definition, SL("primary"), &ztrue, PH_COPY | PH_SEPARATE); } /** * Check if the column allows null values */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 2, PH_NOISY); if (PHALCON_IS_STRING(attribute, "NO")) { phalcon_array_update_string(&definition, SL("notNull"), &ztrue, PH_COPY | PH_SEPARATE); } /** * Check if the column is auto increment */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY); if (PHALCON_IS_STRING(attribute, "auto_increment")) { phalcon_array_update_string(&definition, SL("autoIncrement"), &ztrue, PH_COPY | PH_SEPARATE); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY); /** * Every route is stored as a Phalcon\Db\Column */ PHALCON_INIT_NVAR(column); object_init_ex(column, phalcon_db_column_ce); phalcon_call_method_p2_noret(column, "__construct", column_name, definition); phalcon_array_append(&columns, column, PH_SEPARATE); PHALCON_CPY_WRT(old_column, column_name); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(columns); }
/** * Shows a backtrace item * * @param int $n * @param array $trace */ PHP_METHOD(Phalcon_Debug, showTraceItem){ zval *n, *trace, *space, *two_spaces, *underscore; zval *minus, *html, *class_name, *pattern, *is_phalcon_class; zval *namespace_separator, *prepare_uri_class; zval *class_reflection, *is_internal = NULL, *lower_class_name; zval *prepare_internal_class, *type, *function_name = NULL; zval *function_reflection, *prepared_function_name; zval *trace_args, *arguments, *argument = NULL, *dumped_argument = NULL; zval *span_argument = NULL, *joined_arguments, *one; zval *file, *line, *show_files, *lines, *number_lines; zval *show_file_fragment, *seven, *before_line; zval *first_line = NULL, *five, *after_line, *last_line = NULL; zval *comment_pattern, *utf8, *ent_compat, *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; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0, *ce1; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &n, &trace); 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"))) { PHALCON_OBS_VAR(class_name); phalcon_array_fetch_string(&class_name, trace, SL("class"), PH_NOISY); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^Phalcon/", 1); PHALCON_INIT_VAR(is_phalcon_class); phalcon_preg_match(is_phalcon_class, pattern, class_name, NULL TSRMLS_CC); /** * We assume that classes starting by Phalcon are framework's classes */ if (zend_is_true(is_phalcon_class)) { 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 { ce0 = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(class_reflection); object_init_ex(class_reflection, ce0); if (phalcon_has_constructor(class_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(class_reflection, "__construct", class_name); } /** * Check if classes are PHP's classes */ PHALCON_INIT_VAR(is_internal); phalcon_call_method(is_internal, class_reflection, "isinternal"); if (zend_is_true(is_internal)) { 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 { PHALCON_OBS_NVAR(function_name); phalcon_array_fetch_string(&function_name, trace, SL("function"), PH_NOISY); /** * Check if the function exists */ if (phalcon_function_exists(function_name TSRMLS_CC) == SUCCESS) { ce1 = zend_fetch_class(SL("ReflectionFunction"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_VAR(function_reflection); object_init_ex(function_reflection, ce1); if (phalcon_has_constructor(function_reflection TSRMLS_CC)) { phalcon_call_method_p1_noret(function_reflection, "__construct", function_name); } PHALCON_INIT_NVAR(is_internal); phalcon_call_method(is_internal, function_reflection, "isinternal"); /** * Internal functions links to the PHP documentation */ if (zend_is_true(is_internal)) { /** * 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_INIT_NVAR(dumped_argument); phalcon_call_method_p1(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_SEPARATE); 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"))) { PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); 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); /** * Realpath to the file and its line using a special header */ PHALCON_SCONCAT_SVSVS(html, "<br/><div class=\"error-file\">", file, " (", line, ")</div>"); PHALCON_OBS_VAR(show_files); phalcon_read_property_this(&show_files, this_ptr, SL("_showFiles"), PH_NOISY_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_INIT_VAR(lines); phalcon_call_func_p1(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_CC); /** * File fragments just show a piece of the file where the exception is located */ if (zend_is_true(show_file_fragment)) { /** * Take seven lines back to the current exception's line, @TODO add an option for * this */ PHALCON_INIT_VAR(seven); ZVAL_LONG(seven, 7); PHALCON_INIT_VAR(before_line); sub_function(before_line, line, seven TSRMLS_CC); /** * Check for overflows */ if (PHALCON_LT_LONG(before_line, 1)) { PHALCON_CPY_WRT(first_line, one); } else { PHALCON_CPY_WRT(first_line, before_line); } /** * Take five lines after the current exception's line, @TODO add an option for this */ PHALCON_INIT_VAR(five); ZVAL_LONG(five, 5); PHALCON_INIT_VAR(after_line); phalcon_add_function(after_line, line, five TSRMLS_CC); /** * 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(first_line, 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); /** * We assume the file is utf-8 encoded, @TODO add an option for this */ PHALCON_INIT_VAR(utf8); ZVAL_STRING(utf8, "UTF-8", 1); /** * Don't escape quotes */ PHALCON_INIT_VAR(ent_compat); ZVAL_LONG(ent_compat, 2); PHALCON_INIT_VAR(tab); ZVAL_STRING(tab, "\t", 1); PHALCON_INIT_VAR(comment); ZVAL_STRING(comment, "* /", 1); PHALCON_CPY_WRT(i, first_line); while (1) { if (PHALCON_LE(i, last_line)) { } else { break; } /** * Current line in the file */ PHALCON_INIT_NVAR(line_position); sub_function(line_position, i, one TSRMLS_CC); /** * Current line content in the piece of file */ PHALCON_OBS_NVAR(current_line); phalcon_array_fetch(¤t_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, PHALCON_TRIM_RIGHT TSRMLS_CC); PHALCON_INIT_NVAR(is_comment); 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(" \n") TSRMLS_CC); } else { if (PHALCON_IS_STRING(current_line, "\r\n")) { phalcon_concat_self_str(&html, SL(" \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_call_func_p3(escaped_line, "htmlentities", spaced_current_line, ent_compat, utf8); phalcon_concat_self(&html, escaped_line TSRMLS_CC); } } PHALCON_SEPARATE(i); 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); }
/** * Gets number of rows returned by a resulset * *<code> * $result = $connection->query("SELECT * FROM robots ORDER BY name"); * echo 'There are ', $result->numRows(), ' rows in the resulset'; *</code> * * @return int */ PHP_METHOD(Phalcon_Db_Result_Pdo, numRows){ zval *row_count = NULL, *connection, *type = NULL, *pdo_statement = NULL; zval *sql_statement, *bind_params, *bind_types; zval *matches, *pattern, *match, *else_clauses; zval *sql, *result = NULL, *row = NULL; PHALCON_MM_GROW(); PHALCON_OBS_VAR(row_count); phalcon_read_property_this(&row_count, this_ptr, SL("_rowCount"), PH_NOISY TSRMLS_CC); if (PHALCON_IS_FALSE(row_count)) { PHALCON_OBS_VAR(connection); phalcon_read_property_this(&connection, this_ptr, SL("_connection"), PH_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(&type, connection, "gettype"); /** * MySQL/PostgreSQL library property returns the number of records */ if (PHALCON_IS_STRING(type, "mysql") || PHALCON_IS_STRING(type, "pgsql")) { PHALCON_OBS_VAR(pdo_statement); phalcon_read_property_this(&pdo_statement, this_ptr, SL("_pdoStatement"), PH_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(&row_count, pdo_statement, "rowcount"); } /** * We should get the count using a new statement :( */ if (PHALCON_IS_FALSE(row_count)) { /** * SQLite/Oracle/SQLServer returns resultsets that to the client eyes (PDO) has an * arbitrary number of rows, so we need to perform an extra count to know that */ PHALCON_OBS_VAR(sql_statement); phalcon_read_property_this(&sql_statement, this_ptr, SL("_sqlStatement"), PH_NOISY TSRMLS_CC); /** * If the sql_statement starts with SELECT COUNT(*) we don't make the count */ if (!phalcon_start_with_str(sql_statement, SL("SELECT COUNT(*) "))) { PHALCON_OBS_VAR(bind_params); phalcon_read_property_this(&bind_params, this_ptr, SL("_bindParams"), PH_NOISY TSRMLS_CC); PHALCON_OBS_VAR(bind_types); phalcon_read_property_this(&bind_types, this_ptr, SL("_bindTypes"), PH_NOISY TSRMLS_CC); PHALCON_INIT_VAR(matches); PHALCON_INIT_VAR(pattern); ZVAL_STRING(pattern, "/^SELECT\\s+(.*)$/i", 1); PHALCON_INIT_VAR(match); RETURN_MM_ON_FAILURE(phalcon_preg_match(match, pattern, sql_statement, matches TSRMLS_CC)); if (zend_is_true(match)) { PHALCON_OBS_VAR(else_clauses); phalcon_array_fetch_long(&else_clauses, matches, 1, PH_NOISY); PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVS(sql, "SELECT COUNT(*) \"numrows\" FROM (SELECT ", else_clauses, ")"); PHALCON_CALL_METHOD(&result, connection, "query", sql, bind_params, bind_types); PHALCON_CALL_METHOD(&row, result, "fetch"); PHALCON_OBS_NVAR(row_count); phalcon_array_fetch_string(&row_count, row, SL("numrows"), PH_NOISY); } } else { PHALCON_INIT_NVAR(row_count); ZVAL_LONG(row_count, 1); } } /** * Update the value to avoid further calculations */ phalcon_update_property_this(this_ptr, SL("_rowCount"), row_count TSRMLS_CC); } RETURN_CCTOR(row_count); }
/** * Generates a URL * *<code> * * //Generate a URL appending the URI to the base URI * echo $url->get('products/edit/1'); * * //Generate a URL for a predefined route * echo $url->get(array('for' => 'blog-post', 'title' => 'some-cool-stuff', 'year' => '2012')); * echo $url->get(array('for' => 'blog-post', 'hostname' => true, 'title' => 'some-cool-stuff', 'year' => '2012')); * *</code> * * @param string|array $uri * @param array|object args Optional arguments to be appended to the query string * @param bool|null $local * @return string */ PHP_METHOD(Phalcon_Mvc_Url, get){ zval *uri = NULL, *args = NULL, *local = NULL, *base_uri = NULL, *router = NULL, *dependency_injector; zval *service, *route_name, *hostname, *route = NULL, *exception_message; zval *pattern = NULL, *paths = NULL, *processed_uri = NULL, *query_string; zval *matched, *regexp; zval *generator = NULL, *arguments; PHALCON_MM_GROW(); phalcon_fetch_params(1, 0, 3, &uri, &args, &local); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } if (!args) { args = &PHALCON_GLOBAL(z_null); } if (!local) { local = &PHALCON_GLOBAL(z_null); } else { PHALCON_SEPARATE_PARAM(local); } PHALCON_CALL_METHOD(&base_uri, getThis(), "getbaseuri"); if (Z_TYPE_P(uri) == IS_STRING) { if (strstr(Z_STRVAL_P(uri), ":")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(regexp); ZVAL_STRING(regexp, "/^[^:\\/?#]++:/"); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, regexp, uri, NULL)); if (zend_is_true(matched)) { PHALCON_INIT_NVAR(local); ZVAL_FALSE(local); } } if (Z_TYPE_P(local) == IS_NULL || zend_is_true(local)) { PHALCON_CONCAT_VV(return_value, base_uri, uri); } else { ZVAL_ZVAL(return_value, uri, 1, 0); } } else if (Z_TYPE_P(uri) == IS_ARRAY) { if (!phalcon_array_isset_str_fetch(&route_name, uri, SL("for"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "It's necessary to define the route name with the parameter \"for\""); return; } router = phalcon_read_property(getThis(), SL("_router"), PH_NOISY); /** * Check if the router has not previously set */ if (Z_TYPE_P(router) != IS_OBJECT) { dependency_injector = phalcon_read_property(getThis(), SL("_dependencyInjector"), PH_NOISY); if (!zend_is_true(dependency_injector)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "A dependency injector container is required to obtain the \"url\" service"); return; } PHALCON_INIT_VAR(service); ZVAL_STR(service, IS(router)); router = NULL; PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); phalcon_update_property_this(getThis(), SL("_router"), router); } /** * Every route is uniquely identified by a name */ PHALCON_CALL_METHOD(&route, router, "getroutebyname", route_name); if (Z_TYPE_P(route) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Cannot obtain a route using the name \"", route_name, "\""); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_url_exception_ce, exception_message); return; } PHALCON_CALL_METHOD(&pattern, route, "getpattern"); /** * Return the reversed paths */ PHALCON_CALL_METHOD(&paths, route, "getreversedpaths"); /** * Return the Url Generator */ PHALCON_CALL_METHOD(&generator, route, "geturlgenerator"); if (phalcon_is_callable(generator) || (Z_TYPE_P(generator) == IS_OBJECT && instanceof_function(Z_OBJCE_P(generator), zend_ce_closure))) { PHALCON_INIT_VAR(arguments); array_init_size(arguments, 3); phalcon_array_append(arguments, base_uri, PH_COPY); phalcon_array_append(arguments, paths, PH_COPY); phalcon_array_append(arguments, uri, PH_COPY); PHALCON_CALL_USER_FUNC_ARRAY(&return_value, generator, arguments); } else { /** * Replace the patterns by its variables */ PHALCON_INIT_NVAR(processed_uri); phalcon_replace_paths(processed_uri, pattern, paths, uri); PHALCON_CONCAT_VV(return_value, base_uri, processed_uri); if (phalcon_array_isset_str_fetch(&hostname, uri, SL("hostname"))) { if (zend_is_true(hostname)) { PHALCON_CALL_METHOD(&hostname, route, "gethostname"); PHALCON_INIT_NVAR(processed_uri); PHALCON_CONCAT_VV(processed_uri, hostname, return_value); ZVAL_ZVAL(return_value, processed_uri, 1, 0); } } } } if (zend_is_true(args)) { PHALCON_INIT_VAR(query_string); phalcon_http_build_query(query_string, args, "&"); if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) { if (phalcon_memnstr_str(return_value, "?", 1)) { PHALCON_SCONCAT_SV(return_value, "&", query_string); } else { PHALCON_SCONCAT_SV(return_value, "?", query_string); } } } RETURN_MM(); }
/** * 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(¤t_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(" \n") TSRMLS_CC); } else { if (PHALCON_IS_STRING(current_line, "\r\n")) { phalcon_concat_self_str(&html, SL(" \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); }
/** * Generates a URL * *<code> * * //Generate a URL appending the URI to the base URI * echo $url->get('products/edit/1'); * * //Generate a URL for a predefined route * echo $url->get(array('for' => 'blog-post', 'title' => 'some-cool-stuff', 'year' => '2012')); * *</code> * * @param string|array $uri * @param array|object args Optional arguments to be appended to the query string * @param bool|null $local * @return string */ PHP_METHOD(Phalcon_Mvc_Url, get){ zval **uri = NULL, *base_uri = NULL, *router = NULL, *dependency_injector; zval *service, *route_name, *route = NULL, *exception_message; zval *pattern = NULL, *paths = NULL, *processed_uri, **args = NULL, *query_string; zval *matched, *regexp; zval **z_local = NULL; int local = 1; phalcon_fetch_params_ex(0, 3, &uri, &args, &z_local); PHALCON_MM_GROW(); if (!uri) { uri = &PHALCON_GLOBAL(z_null); } else if (z_local && Z_TYPE_PP(z_local) != IS_NULL) { if (!zend_is_true(*z_local)) { local = 0; } } else if (Z_TYPE_PP(uri) == IS_STRING && strstr(Z_STRVAL_PP(uri), ":")) { PHALCON_INIT_VAR(matched); PHALCON_INIT_VAR(regexp); ZVAL_STRING(regexp, "/^[^:\\/?#]++:/", 1); RETURN_MM_ON_FAILURE(phalcon_preg_match(matched, regexp, *uri, NULL TSRMLS_CC)); if (zend_is_true(matched)) { local = 0; } } PHALCON_CALL_METHOD(&base_uri, this_ptr, "getbaseuri"); if (Z_TYPE_PP(uri) == IS_ARRAY) { if (!phalcon_array_isset_string_fetch(&route_name, *uri, SS("for"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "It's necessary to define the route name with the parameter \"for\""); return; } router = phalcon_fetch_nproperty_this(this_ptr, SL("_router"), PH_NOISY TSRMLS_CC); /** * Check if the router has not previously set */ if (Z_TYPE_P(router) != IS_OBJECT) { dependency_injector = phalcon_fetch_nproperty_this(this_ptr, SL("_dependencyInjector"), PH_NOISY TSRMLS_CC); if (!zend_is_true(dependency_injector)) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_url_exception_ce, "A dependency injector container is required to obtain the \"url\" service"); return; } PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_router); router = NULL; PHALCON_CALL_METHOD(&router, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(router, phalcon_mvc_routerinterface_ce); phalcon_update_property_this(this_ptr, SL("_router"), router TSRMLS_CC); } /** * Every route is uniquely identified by a name */ PHALCON_CALL_METHOD(&route, router, "getroutebyname", route_name); if (Z_TYPE_P(route) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Cannot obtain a route using the name \"", route_name, "\""); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_url_exception_ce, exception_message); return; } PHALCON_CALL_METHOD(&pattern, route, "getpattern"); /** * Return the reversed paths */ PHALCON_CALL_METHOD(&paths, route, "getreversedpaths"); /** * Replace the patterns by its variables */ PHALCON_INIT_VAR(processed_uri); phalcon_replace_paths(processed_uri, pattern, paths, *uri TSRMLS_CC); PHALCON_CONCAT_VV(return_value, base_uri, processed_uri); } else { if (local) { PHALCON_CONCAT_VV(return_value, base_uri, *uri); } else { ZVAL_ZVAL(return_value, *uri, 1, 0); } } if (args) { PHALCON_INIT_VAR(query_string); phalcon_http_build_query(query_string, *args, "&" TSRMLS_CC); if (Z_TYPE_P(query_string) == IS_STRING && Z_STRLEN_P(query_string)) { if (phalcon_memnstr_str(return_value, "?", 1)) { PHALCON_SCONCAT_SV(return_value, "&", query_string); } else { PHALCON_SCONCAT_SV(return_value, "?", query_string); } } } RETURN_MM(); }