static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, const char *domain, int domain_len, uint32_t option, int mode, zval *idna_info) { UErrorCode status = U_ZERO_ERROR; UIDNA *uts46; int32_t len; int32_t buffer_capac = 255; /* no domain name may exceed this */ zend_string *buffer = STR_ALLOC(buffer_capac, 0); UIDNAInfo info = UIDNA_INFO_INITIALIZER; int buffer_used = 0; uts46 = uidna_openUTS46(option, &status); if (php_intl_idn_check_status(status, "failed to open UIDNA instance", mode TSRMLS_CC) == FAILURE) { STR_FREE(buffer); RETURN_FALSE; } if (mode == INTL_IDN_TO_ASCII) { len = uidna_nameToASCII_UTF8(uts46, domain, (int32_t)domain_len, buffer->val, buffer_capac, &info, &status); } else { len = uidna_nameToUnicodeUTF8(uts46, domain, (int32_t)domain_len, buffer->val, buffer_capac, &info, &status); } if (php_intl_idn_check_status(status, "failed to convert name", mode TSRMLS_CC) == FAILURE) { uidna_close(uts46); STR_FREE(buffer); RETURN_FALSE; } if (len >= 255) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "ICU returned an unexpected length"); } buffer->val[len] = '\0'; buffer->len = len; if (info.errors == 0) { RETVAL_STR(buffer); buffer_used = 1; } else { RETVAL_FALSE; } if (idna_info) { if (buffer_used) { /* used in return_value then */ zval_addref_p(return_value); add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, return_value); } else { zval zv; ZVAL_STR(&zv, buffer); buffer_used = 1; add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, &zv); } add_assoc_bool_ex(idna_info, "isTransitionalDifferent", sizeof("isTransitionalDifferent")-1, info.isTransitionalDifferent); add_assoc_long_ex(idna_info, "errors", sizeof("errors")-1, (long)info.errors); } if (!buffer_used) { STR_FREE(buffer); } uidna_close(uts46); }
static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS, const char *domain, int32_t domain_len, uint32_t option, int mode, zval *idna_info) { UErrorCode status = U_ZERO_ERROR; UIDNA *uts46; int32_t len; int32_t buffer_capac = 255; /* no domain name may exceed this */ zend_string *buffer = zend_string_alloc(buffer_capac, 0); UIDNAInfo info = UIDNA_INFO_INITIALIZER; int buffer_used = 0; uts46 = uidna_openUTS46(option, &status); if (php_intl_idn_check_status(status, "failed to open UIDNA instance", mode) == FAILURE) { zend_string_free(buffer); RETURN_FALSE; } if (mode == INTL_IDN_TO_ASCII) { len = uidna_nameToASCII_UTF8(uts46, domain, domain_len, ZSTR_VAL(buffer), buffer_capac, &info, &status); } else { len = uidna_nameToUnicodeUTF8(uts46, domain, domain_len, ZSTR_VAL(buffer), buffer_capac, &info, &status); } if (len >= 255 || php_intl_idn_check_status(status, "failed to convert name", mode) == FAILURE) { uidna_close(uts46); zend_string_free(buffer); RETURN_FALSE; } ZSTR_VAL(buffer)[len] = '\0'; ZSTR_LEN(buffer) = len; if (info.errors == 0) { RETVAL_STR(buffer); buffer_used = 1; } else { RETVAL_FALSE; } if (idna_info) { if (buffer_used) { /* used in return_value then */ zval_addref_p(return_value); add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, return_value); } else { zval zv; ZVAL_NEW_STR(&zv, buffer); buffer_used = 1; add_assoc_zval_ex(idna_info, "result", sizeof("result")-1, &zv); } add_assoc_bool_ex(idna_info, "isTransitionalDifferent", sizeof("isTransitionalDifferent")-1, info.isTransitionalDifferent); add_assoc_long_ex(idna_info, "errors", sizeof("errors")-1, (zend_long)info.errors); } if (!buffer_used) { zend_string_free(buffer); } uidna_close(uts46); }
/** * Executes the validation * * @param Phalcon\Validation $validator * @param string $attribute * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_StringLength, validate){ zval *validator, *attribute, *value = NULL, *allow_empty; zval *valid = NULL, *type, *maximum, *minimum, *label; zval *code, *pairs, *message_str = NULL, *prepared = NULL, *message; zend_class_entry *ce = Z_OBJCE_P(getThis()); PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &validator, &attribute); PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1); PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute); PHALCON_OBS_VAR(allow_empty); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &allow_empty, getThis(), ISV(allowEmpty))); if (zend_is_true(allow_empty) && phalcon_validation_validator_isempty_helper(value)) { RETURN_MM_TRUE; } PHALCON_OBS_VAR(maximum); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &maximum, getThis(), "max")); PHALCON_OBS_VAR(minimum); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &minimum, getThis(), "min")); PHALCON_CALL_SELF(&valid, "valid", value, minimum, maximum); if (PHALCON_IS_FALSE(valid)) { type = phalcon_read_property(getThis(), SL("_type"), PH_NOISY); PHALCON_OBS_VAR(label); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), ISV(label))); if (!zend_is_true(label)) { PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute); if (!zend_is_true(label)) { PHALCON_CPY_WRT(label, attribute); } } PHALCON_OBS_VAR(code); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), ISV(code))); if (Z_TYPE_P(code) == IS_NULL) { ZVAL_LONG(code, 0); } if (phalcon_compare_strict_string(type, SL("TooLong"))) { PHALCON_ALLOC_INIT_ZVAL(pairs); array_init_size(pairs, 2); Z_TRY_ADDREF_P(label); add_assoc_zval_ex(pairs, SL(":field"), label); Z_TRY_ADDREF_P(maximum); add_assoc_zval_ex(pairs, SL(":max"), maximum); PHALCON_OBS_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), "messageMaximum")); if (!zend_is_true(message_str)) { PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), message_str, validator, "TooLong")); } PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs); message = phalcon_validation_message_construct_helper(prepared, attribute, "TooLong", code); } else { PHALCON_ALLOC_INIT_ZVAL(pairs); array_init_size(pairs, 2); Z_TRY_ADDREF_P(label); add_assoc_zval_ex(pairs, SL(":field"), label); Z_TRY_ADDREF_P(minimum); add_assoc_zval_ex(pairs, SL(":min"), minimum); PHALCON_OBS_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), "messageMinimum")); if (!zend_is_true(message_str)) { PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), message_str, validator, "TooShort")); } PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs); message = phalcon_validation_message_construct_helper(prepared, attribute, "TooShort", code); } Z_TRY_DELREF_P(message); PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Executes the validation * * @param Phalcon\Validation $validator * @param string $attribute * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_Regex, validate){ zval *validator, *attribute, *value = NULL, *allow_empty, *pattern, *valid = NULL, *label, *pairs; zval *message_str, *code, *prepared = NULL, *message; zend_class_entry *ce = Z_OBJCE_P(getThis()); PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &validator, &attribute); PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1); PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute); PHALCON_OBS_VAR(allow_empty); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &allow_empty, getThis(), phalcon_interned_allowEmpty TSRMLS_CC)); if (zend_is_true(allow_empty) && phalcon_validation_validator_isempty_helper(value)) { RETURN_MM_TRUE; } /* The regular expression is set in the option 'pattern' */ PHALCON_OBS_VAR(pattern); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &pattern, getThis(), "pattern" TSRMLS_CC)); PHALCON_CALL_SELF(&valid, "valid", value, pattern); if (PHALCON_IS_FALSE(valid)) { PHALCON_OBS_VAR(label); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), phalcon_interned_label TSRMLS_CC)); if (!zend_is_true(label)) { PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute); if (!zend_is_true(label)) { PHALCON_CPY_WRT(label, attribute); } } PHALCON_ALLOC_GHOST_ZVAL(pairs); array_init_size(pairs, 1); Z_ADDREF_P(label); add_assoc_zval_ex(pairs, SS(":field"), label); PHALCON_OBS_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), phalcon_interned_message TSRMLS_CC)); if (!zend_is_true(message_str)) { PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), &message_str, validator, "Regex" TSRMLS_CC)); } PHALCON_OBS_VAR(code); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), phalcon_interned_code TSRMLS_CC)); if (Z_TYPE_P(code) == IS_NULL) { ZVAL_LONG(code, 0); } PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs); message = phalcon_validation_message_construct_helper(prepared, attribute, "Regex", code TSRMLS_CC); Z_DELREF_P(message); PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Executes the validation * * @param Phalcon\Validation $validator * @param string $attribute * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_InclusionIn, validate){ zval *validator, *attribute, *value = NULL, *allow_empty, *valid = NULL; zval *label, *domain, *joined_domain, *pairs, *message_str, *message, *code; zval *prepared = NULL; zend_class_entry *ce = Z_OBJCE_P(getThis()); PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &validator, &attribute); PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1); PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute); PHALCON_OBS_VAR(allow_empty); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &allow_empty, getThis(), ISV(allowEmpty))); if (zend_is_true(allow_empty) && phalcon_validation_validator_isempty_helper(value)) { RETURN_MM_TRUE; } /* A domain is an array with a list of valid values */ PHALCON_OBS_VAR(domain); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &domain, getThis(), ISV(domain))); if (Z_TYPE_P(domain) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_validation_exception_ce, "Option 'domain' must be an array"); return; } PHALCON_CALL_SELF(&valid, "valid", value, domain); if (PHALCON_IS_FALSE(valid)) { PHALCON_OBS_VAR(label); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), ISV(label))); if (!zend_is_true(label)) { PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute); if (!zend_is_true(label)) { PHALCON_CPY_WRT(label, attribute); } } PHALCON_ALLOC_INIT_ZVAL(joined_domain); phalcon_fast_join_str(joined_domain, SL(", "), domain); PHALCON_ALLOC_INIT_ZVAL(pairs); array_init_size(pairs, 2); Z_TRY_ADDREF_P(label); add_assoc_zval_ex(pairs, SL(":field"), label); add_assoc_zval_ex(pairs, SL(":domain"), joined_domain); PHALCON_OBS_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), ISV(message))); if (!zend_is_true(message_str)) { PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), message_str, validator, "InclusionIn")); } PHALCON_OBS_VAR(code); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), ISV(code))); if (Z_TYPE_P(code) == IS_NULL) { ZVAL_LONG(code, 0); } PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs); message = phalcon_validation_message_construct_helper(prepared, attribute, "InclusionIn", code); Z_TRY_DELREF_P(message); PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
static zval *get_trace_detail(const jsonlite_decoder *self) { HashTable *array = NULL; HashPosition *pointer = NULL; zval *traces = NULL; zval *trace = NULL; zval **val = NULL; zval **msg = NULL; zval **range_start = NULL; zval **range_end = NULL; zval *range = NULL; char *str = NULL; zval *chars = NULL; zval **detail = NULL; int start = 0; int end = 0; array = Z_ARRVAL_P(self->trace); ALLOC_INIT_ZVAL(traces); array_init(traces); zend_hash_internal_pointer_reset_ex(array, pointer); while (zend_hash_has_more_elements_ex(array, pointer) == SUCCESS) { if (zend_hash_get_current_data_ex(array, (void **) &val, pointer) == SUCCESS) { if (val && Z_TYPE_PP(val) == IS_ARRAY) { zend_hash_index_find(Z_ARRVAL_PP(val), 0, (void **) &msg); zend_hash_index_find(Z_ARRVAL_PP(val), 1, (void **) &range_start); zend_hash_index_find(Z_ARRVAL_PP(val), 2, (void **) &range_end); if (zend_hash_index_exists(Z_ARRVAL_PP(val), 3) == 1) { zend_hash_index_find(Z_ARRVAL_PP(val), 3, (void **) &detail); } ALLOC_INIT_ZVAL(trace); array_init(trace); zval_add_ref(msg); add_assoc_zval_ex(trace, ZEND_STRS("msg"), *msg); ALLOC_INIT_ZVAL(range); array_init(range); zval_add_ref(range_start); add_next_index_zval(range, *range_start); zval_add_ref(range_end); add_next_index_zval(range, *range_end); add_assoc_zval_ex(trace, ZEND_STRS("range"), range); start = Z_LVAL_PP(range_start); if (start > 0) { start--; } end = Z_LVAL_PP(range_end); if (end < self->length) { end++; } str = zend_strndup((self->jsonlite + start), end - start); add_assoc_string_ex(trace, ZEND_STRS("chars"), str, 1); free(str); if (detail != NULL) { zval_add_ref(detail); add_assoc_zval_ex(trace, ZEND_STRS("detail"), *detail); } add_next_index_zval(traces, trace); detail = NULL; trace = NULL; } } if (zend_hash_move_forward_ex(array, pointer) == FAILURE) { break; } } return traces; }
/** * Applies a format to a message before sending it to the log * * @param string $message * @param int $type * @param int $timestamp * @return string */ PHP_METHOD(Phalcon_Logger_Formatter_Firephp, format) { zval *message, *type, *type_str = NULL, *timestamp; zval *payload, *body, *backtrace, *meta, *encoded; zval *show_backtrace; smart_str result = { NULL, 0, 0 }; int i; Bucket *p; phalcon_fetch_params(0, 3, 0, &message, &type, ×tamp); /** * We intentionally do not use Phalcon's MM for better performance. * All variables allocated with PHALCON_ALLOC_ZVAL() will have * their reference count set to 1 and therefore they can be nicely * put into the result array; when that array will be destroyed, * all inserted variables will be automatically destroyed, too * and we will just save some time by not using Z_ADDREF_P and Z_DELREF_P */ PHALCON_ALLOC_ZVAL(type_str); phalcon_call_method_p1(type_str, this_ptr, "gettypestring", type); phalcon_read_property_this(&show_backtrace, getThis(), SL("_showBacktrace"), PH_NOISY TSRMLS_CC); Z_DELREF_P(show_backtrace); /** * Get the backtrace. This differs for differemt PHP versions. * 5.3.6+ allows us to skip the function arguments which will save some memory * For 5.4+ there is an extra argument. */ PHALCON_ALLOC_ZVAL(backtrace); if (zend_is_true(show_backtrace)) { #if PHP_VERSION_ID < 50306 zend_fetch_debug_backtrace(backtrace, 1, 0 TSRMLS_CC); #elif PHP_VERSION_ID < 50400 zend_fetch_debug_backtrace(backtrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS TSRMLS_CC); #else zend_fetch_debug_backtrace(backtrace, 1, DEBUG_BACKTRACE_IGNORE_ARGS, 0 TSRMLS_CC); #endif if (Z_TYPE_P(backtrace) == IS_ARRAY) { HashPosition pos; HashTable *ht = Z_ARRVAL_P(backtrace); zval **ppzval; int found = 0; ulong idx; char *key; uint key_len; /** * At this point we know that the backtrace is the array. * Again, we intentionally do not use Phalcon's API because we know * that we are working with the array / hash table and thus we can * save some time by omitting Z_TYPE_P(x) == IS_ARRAY checks */ for ( zend_hash_internal_pointer_reset_ex(ht, &pos); zend_hash_has_more_elements_ex(ht, &pos) == SUCCESS; ) { zend_hash_get_current_data_ex(ht, (void**)&ppzval, &pos); zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, &pos); zend_hash_move_forward_ex(ht, &pos); if (Z_TYPE_PP(ppzval) == IS_ARRAY) { /** * Here we need to skip the latest calls into Phalcon's core. * Calls to Zend internal functions will have "file" index not set. * We remove these entries from the array. */ if (!found && !zend_hash_exists(Z_ARRVAL_PP(ppzval), SS("file"))) { zend_hash_index_del(ht, idx); } else { /** * Remove args and object indices. They usually give * too much information; this is not suitable to send * in the HTTP headers */ zend_hash_del(Z_ARRVAL_PP(ppzval), "args", sizeof("args")); zend_hash_del(Z_ARRVAL_PP(ppzval), "object", sizeof("object")); found = 1; } } } /** * Now we need to renumber the hash table because we removed several * heading elements. If we don't do this, json_encode() will convert * this array to a JavaScript object which is an unwanted side effect */ p = ht->pListHead; i = 0; while (p != NULL) { p->nKeyLength = 0; p->h = i++; p = p->pListNext; } ht->nNextFreeElement = i; zend_hash_rehash(ht); } } /** * The result will looks like this: * * array( * array('Type' => 'message type', 'Label' => 'message'), * array('backtrace' => array(backtrace goes here) * ) */ MAKE_STD_ZVAL(payload); array_init_size(payload, 2); PHALCON_ALLOC_ZVAL(meta); array_init_size(meta, 4); add_assoc_zval_ex(meta, SS("Type"), type_str); Z_ADDREF_P(message); add_assoc_zval_ex(meta, SS("Label"), message); if (Z_TYPE_P(backtrace) == IS_ARRAY) { zval **ppzval; if (likely(SUCCESS == zend_hash_index_find(Z_ARRVAL_P(backtrace), 0, (void**)&ppzval)) && likely(Z_TYPE_PP(ppzval) == IS_ARRAY)) { zval **file = NULL, **line = NULL; zend_hash_quick_find(Z_ARRVAL_PP(ppzval), SS("file"), zend_inline_hash_func(SS("file")), (void**)&file); zend_hash_quick_find(Z_ARRVAL_PP(ppzval), SS("line"), zend_inline_hash_func(SS("line")), (void**)&line); if (likely(file != NULL)) { Z_ADDREF_PP(file); add_assoc_zval_ex(meta, SS("File"), *file); } if (likely(line != NULL)) { Z_ADDREF_PP(line); add_assoc_zval_ex(meta, SS("Line"), *line); } } } MAKE_STD_ZVAL(body); array_init_size(body, 1); if (zend_is_true(show_backtrace)) { add_assoc_zval_ex(body, SS("backtrace"), backtrace); } else { zval_ptr_dtor(&backtrace); } add_next_index_zval(payload, meta); add_next_index_zval(payload, body); /** * Convert everything to JSON */ ALLOC_INIT_ZVAL(encoded); phalcon_json_encode(encoded, payload, 0 TSRMLS_CC); /** * As promised, kill the payload and all associated elements */ zval_ptr_dtor(&payload); /** * We don't want to use Phalcon's concatenation API because it * requires the memory manager. Therefore we fall back to using smart strings. * smart_str_alloc4() will allocate all required memory amount (plus some more) * in one go and this allows us to avoid performance penalties due to * memory reallocations. */ smart_str_alloc4(&result, Z_STRLEN_P(encoded) + 2 + 5, 0, i); /** * The format is: * * <size>|[meta,body]| * * Meta and body are contained in encoded inside the array, as required * by the protocol specification * @see http://www.firephp.org/Wiki/Reference/Protocol */ smart_str_append_long(&result, Z_STRLEN_P(encoded)); smart_str_appendc(&result, '|'); smart_str_appendl(&result, Z_STRVAL_P(encoded), Z_STRLEN_P(encoded)); smart_str_appendc(&result, '|'); smart_str_0(&result); /* We don't need the JSON message anymore */ zval_ptr_dtor(&encoded); /* Do not free the samrt string because we steal its data for zval */ RETURN_STRINGL(result.c, result.len, 0); }
/** * Executes the validation * * @param Phalcon\Validation $validator * @param string $attribute * @return boolean */ PHP_METHOD(Phalcon_Validation_Validator_Confirmation, validate){ zval *validator, *attribute, *with_attribute; zval *value = NULL, *with_value = NULL, *message_str, *message, *code; zval *label, *with_label, *pairs, *prepared = NULL; zend_class_entry *ce = Z_OBJCE_P(getThis()); PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &validator, &attribute); PHALCON_VERIFY_CLASS_EX(validator, phalcon_validation_ce, phalcon_validation_exception_ce, 1); PHALCON_OBS_VAR(with_attribute); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &with_attribute, getThis(), "with" TSRMLS_CC)); PHALCON_CALL_METHOD(&value, validator, "getvalue", attribute); PHALCON_CALL_METHOD(&with_value, validator, "getvalue", with_attribute); if (!PHALCON_IS_EQUAL(value, with_value)) { PHALCON_OBS_VAR(label); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &label, getThis(), phalcon_interned_label TSRMLS_CC)); if (!zend_is_true(label)) { PHALCON_CALL_METHOD(&label, validator, "getlabel", attribute); if (!zend_is_true(label)) { PHALCON_CPY_WRT(label, attribute); } } PHALCON_OBS_VAR(with_label); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &with_label, getThis(), phalcon_interned_label TSRMLS_CC)); if (!zend_is_true(with_label)) { PHALCON_CALL_METHOD(&with_label, validator, "getlabel", with_attribute); if (!zend_is_true(with_label)) { PHALCON_CPY_WRT(with_label, with_attribute); } } PHALCON_ALLOC_GHOST_ZVAL(pairs); array_init_size(pairs, 2); Z_ADDREF_P(label); add_assoc_zval_ex(pairs, SS(":field"), label); Z_ADDREF_P(with_label); add_assoc_zval_ex(pairs, SS(":with"), with_label); PHALCON_OBS_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &message_str, getThis(), phalcon_interned_message TSRMLS_CC)); if (!zend_is_true(message_str)) { PHALCON_OBSERVE_OR_NULLIFY_VAR(message_str); RETURN_MM_ON_FAILURE(phalcon_validation_getdefaultmessage_helper(Z_OBJCE_P(validator), &message_str, validator, "Confirmation" TSRMLS_CC)); } PHALCON_OBS_VAR(code); RETURN_MM_ON_FAILURE(phalcon_validation_validator_getoption_helper(ce, &code, getThis(), phalcon_interned_code TSRMLS_CC)); if (Z_TYPE_P(code) == IS_NULL) { ZVAL_LONG(code, 0); } PHALCON_CALL_FUNCTION(&prepared, "strtr", message_str, pairs); message = phalcon_validation_message_construct_helper(prepared, attribute, "Confirmation", code TSRMLS_CC); Z_DELREF_P(message); PHALCON_CALL_METHOD(NULL, validator, "appendmessage", message); RETURN_MM_FALSE; } RETURN_MM_TRUE; }
/** * Reads annotations from the class dockblocks, its methods and/or properties * * @param string $className * @return array */ PHP_METHOD(Phalcon_Annotations_Reader, parse){ zval *class_name, *annotations; zval *class_annotations, *annotations_properties, *annotations_methods; zend_class_entry *class_ce; const char *file; zend_uint line; phalcon_fetch_params(0, 1, 0, &class_name); if (unlikely(Z_TYPE_P(class_name) != IS_STRING)) { PHALCON_THROW_EXCEPTION_STRW(phalcon_annotations_exception_ce, "The class name must be a string"); return; } 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) { zend_throw_exception_ex(phalcon_annotations_exception_ce, 0 TSRMLS_CC, "Class %s does not exist", Z_STRVAL_P(class_name)); return; } if (class_ce->type != ZEND_USER_CLASS) { array_init(return_value); return; } PHALCON_MM_GROW(); PHALCON_INIT_VAR(annotations); array_init(annotations); file = phalcon_get_class_filename(class_ce); if (!file) { file = "(unknown)"; } /* Class info */ { const char *cmt; zend_uint cmt_len; if (phalcon_get_class_doc_comment(class_ce, &cmt, &cmt_len)) { line = phalcon_get_class_startline(class_ce); PHALCON_INIT_VAR(class_annotations); RETURN_MM_ON_FAILURE(phannot_parse_annotations(class_annotations, cmt, cmt_len, file, line TSRMLS_CC)); if (Z_TYPE_P(class_annotations) == IS_ARRAY) { phalcon_array_update_string(&annotations, SL("class"), class_annotations, PH_COPY); } } } /* Get class properties */ { HashTable *props = &class_ce->properties_info; if (zend_hash_num_elements(props) > 0) { HashPosition hp; zend_property_info *property; PHALCON_INIT_VAR(annotations_properties); array_init_size(annotations_properties, zend_hash_num_elements(props)); for ( zend_hash_internal_pointer_reset_ex(props, &hp); zend_hash_get_current_data_ex(props, (void**)&property, &hp) != FAILURE; zend_hash_move_forward_ex(props, &hp) ) { const char *cmt; zend_uint cmt_len; if (phalcon_get_property_doc_comment(property, &cmt, &cmt_len)) { zval *property_annotations; MAKE_STD_ZVAL(property_annotations); if (FAILURE == phannot_parse_annotations(property_annotations, cmt, cmt_len, file, 0 TSRMLS_CC)) { zval_ptr_dtor(&property_annotations); RETURN_MM(); } if (Z_TYPE_P(property_annotations) == IS_ARRAY) { #if PHP_VERSION_ID >= 50400 { const char *prop_name, *class_name; if (zend_unmangle_property_name(property->name, property->name_length - 1, &class_name, &prop_name) == SUCCESS) { add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name) + 1, property_annotations); } } #else { char *prop_name, *class_name; if (zend_unmangle_property_name(property->name, property->name_length - 1, &class_name, &prop_name) == SUCCESS) { add_assoc_zval_ex(annotations_properties, prop_name, strlen(prop_name) + 1, property_annotations); } } #endif } else { zval_ptr_dtor(&property_annotations); } } } if (zend_hash_num_elements(Z_ARRVAL_P(annotations_properties))) { phalcon_array_update_string(&annotations, SL("properties"), annotations_properties, PH_COPY); } } } /* Get class methods */ { HashTable *methods = &class_ce->function_table; if (zend_hash_num_elements(methods) > 0) { HashPosition hp; zend_function *method; PHALCON_INIT_VAR(annotations_methods); array_init_size(annotations_methods, zend_hash_num_elements(methods)); for ( zend_hash_internal_pointer_reset_ex(methods, &hp); zend_hash_get_current_data_ex(methods, (void**)&method, &hp) != FAILURE; zend_hash_move_forward_ex(methods, &hp) ) { const char *cmt; zend_uint cmt_len; if (phalcon_get_function_doc_comment(method, &cmt, &cmt_len)) { zval *method_annotations; line = phalcon_get_function_startline(method); MAKE_STD_ZVAL(method_annotations); if (FAILURE == phannot_parse_annotations(method_annotations, cmt, cmt_len, file, line TSRMLS_CC)) { zval_ptr_dtor(&method_annotations); RETURN_MM(); } if (Z_TYPE_P(method_annotations) == IS_ARRAY) { add_assoc_zval_ex(annotations_methods, method->common.function_name, strlen(method->common.function_name) + 1, method_annotations); } else { zval_ptr_dtor(&method_annotations); } } } if (zend_hash_num_elements(Z_ARRVAL_P(annotations_methods))) { phalcon_array_update_string(&annotations, SL("methods"), annotations_methods, PH_COPY); } } } RETURN_CTOR(annotations); }