/** * Checks for annotations in the controller docblock * * @param string $handler * @param Phalcon\Annotations\AdapterInterface */ PHP_METHOD(Phalcon_Mvc_Router_Annotations, processControllerAnnotation){ zval *handler, *annotation, *name = NULL, *position, *value = NULL; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &handler, &annotation); PHALCON_CALL_METHOD(&name, annotation, "getname"); /** * @RoutePrefix add a prefix for all the routes defined in the model */ if (PHALCON_IS_STRING(name, "RoutePrefix")) { position = PHALCON_GLOBAL(z_zero); PHALCON_CALL_METHOD(&value, annotation, "getargument", position); phalcon_update_property_this(this_ptr, SL("_routePrefix"), value TSRMLS_CC); } PHALCON_MM_RESTORE(); }
/** * 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); }
/** * 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); }
/** * Produces a recursive representation of an array * * @param array $argument * @return string */ PHP_METHOD(Phalcon_Debug, _getArrayDump){ zval *argument, *n = NULL, *number_arguments, *one, *dump; zval *v = NULL, *k = NULL, *var_dump = NULL, *escaped_string = NULL, *next = NULL, *array_dump = NULL; zval *class_name = NULL, *joined_dump; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &argument, &n); if (!n) { PHALCON_INIT_VAR(n); ZVAL_LONG(n, 0); } PHALCON_INIT_VAR(number_arguments); phalcon_fast_count(number_arguments, argument TSRMLS_CC); if (PHALCON_LT_LONG(n, 3)) { if (PHALCON_GT_LONG(number_arguments, 0)) { if (PHALCON_LT_LONG(number_arguments, 10)) { PHALCON_INIT_VAR(one); ZVAL_LONG(one, 1); PHALCON_INIT_VAR(dump); array_init(dump); phalcon_is_iterable(argument, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(k, ah0, hp0); PHALCON_GET_HVALUE(v); if (PHALCON_IS_SCALAR(v)) { if (PHALCON_IS_STRING(v, "")) { PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVS(var_dump, "[", k, "] => (empty string)"); } else { PHALCON_INIT_NVAR(escaped_string); phalcon_call_method_p1(escaped_string, this_ptr, "_escapestring", v); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSV(var_dump, "[", k, "] => ", escaped_string); } phalcon_array_append(&dump, var_dump, PH_SEPARATE); } else { if (Z_TYPE_P(v) == IS_ARRAY) { PHALCON_INIT_NVAR(next); phalcon_add_function(next, n, one TSRMLS_CC); PHALCON_INIT_NVAR(array_dump); phalcon_call_method_p2(array_dump, this_ptr, "_getarraydump", v, next); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSVS(var_dump, "[", k, "] => Array(", array_dump, ")"); phalcon_array_append(&dump, var_dump, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); continue; } if (Z_TYPE_P(v) == IS_OBJECT) { PHALCON_INIT_NVAR(class_name); phalcon_get_class(class_name, v, 0 TSRMLS_CC); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSVS(var_dump, "[", k, "] => Object(", class_name, ")"); phalcon_array_append(&dump, var_dump, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); continue; } if (Z_TYPE_P(v) == IS_NULL) { PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVS(var_dump, "[", k, "] => null"); phalcon_array_append(&dump, var_dump, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); continue; } PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSV(var_dump, "[", k, "] => ", v); phalcon_array_append(&dump, var_dump, PH_SEPARATE); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(joined_dump); phalcon_fast_join_str(joined_dump, SL(", "), dump TSRMLS_CC); RETURN_CTOR(joined_dump); } RETURN_NCTOR(number_arguments); } } RETURN_MM_NULL(); }
/** * Checks if a role has access to a resource * * @param string $roleName * @param string $resourceName * @param string $access * @param string $action */ PHP_METHOD(Phalcon_Acl_Adapter_Memory, _allowOrDeny){ zval *role_name, *resource_name, *access, *action; zval *roles_names, *exception_message = NULL, *resources_names; zval *default_access, *access_list, *internal_access; zval *access_name = NULL, *access_key = NULL, *access_key_all = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 4, 0, &role_name, &resource_name, &access, &action); PHALCON_OBS_VAR(roles_names); phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC); if (!phalcon_array_isset(roles_names, role_name)) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Role \"", role_name, "\" does not exist in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } PHALCON_OBS_VAR(resources_names); phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC); if (!phalcon_array_isset(resources_names, resource_name)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Resource \"", resource_name, "\" does not exist in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } PHALCON_OBS_VAR(default_access); phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC); PHALCON_OBS_VAR(access_list); phalcon_read_property_this(&access_list, this_ptr, SL("_accessList"), PH_NOISY_CC); PHALCON_OBS_VAR(internal_access); phalcon_read_property_this(&internal_access, this_ptr, SL("_access"), PH_NOISY_CC); if (Z_TYPE_P(access) == IS_ARRAY) { phalcon_is_iterable(access, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(access_name); PHALCON_INIT_NVAR(access_key); PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_name); if (!phalcon_array_isset(access_list, access_key)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access_name, "' does not exist in resource '", resource_name, "'"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } zend_hash_move_forward_ex(ah0, &hp0); } phalcon_is_iterable(access, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(access_name); PHALCON_INIT_NVAR(access_key); PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access_name); phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC); if (!PHALCON_IS_STRING(access_name, "*")) { PHALCON_INIT_NVAR(access_key_all); PHALCON_CONCAT_VSVS(access_key_all, role_name, "!", resource_name, "!*"); if (!phalcon_array_isset(internal_access, access_key_all)) { phalcon_update_property_array(this_ptr, SL("_access"), access_key_all, default_access TSRMLS_CC); } } zend_hash_move_forward_ex(ah1, &hp1); } } else { if (!PHALCON_IS_STRING(access, "*")) { PHALCON_INIT_NVAR(access_key); PHALCON_CONCAT_VSV(access_key, resource_name, "!", access); if (!phalcon_array_isset(access_list, access_key)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access, "' does not exist in resource '", resource_name, "'"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } } PHALCON_INIT_NVAR(access_key); PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access); /** * Define the access action for the specified accessKey */ phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC); if (!PHALCON_IS_STRING(access, "*")) { PHALCON_INIT_NVAR(access_key); PHALCON_CONCAT_VSVS(access_key, role_name, "!", resource_name, "!*"); /** * If there is no default action for all the rest actions in the resource set the * default one */ if (!phalcon_array_isset(internal_access, access_key)) { phalcon_update_property_array(this_ptr, SL("_access"), access_key, default_access TSRMLS_CC); } } } PHALCON_MM_RESTORE(); }
/** * Builds generic INPUT tags * * @param string $type * @param array $parameters * @param boolean $asValue * @return string */ PHP_METHOD(Phalcon_Tag, _inputField){ zval *type, *parameters, *as_value = NULL, *params = NULL, *value = NULL; zval *id = NULL, *name, *code, *key = NULL, *five, *doctype, *is_xhtml; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 1, &type, ¶meters, &as_value); if (!as_value) { PHALCON_INIT_VAR(as_value); ZVAL_BOOL(as_value, 0); } if (Z_TYPE_P(parameters) != IS_ARRAY) { PHALCON_INIT_VAR(params); array_init_size(params, 1); phalcon_array_append(¶ms, parameters, PH_SEPARATE TSRMLS_CC); } else { PHALCON_CPY_WRT(params, parameters); } PHALCON_INIT_VAR(value); if (PHALCON_IS_FALSE(as_value)) { if (!phalcon_array_isset_long(params, 0)) { PHALCON_OBS_VAR(id); phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY_CC); phalcon_array_update_long(¶ms, 0, &id, PH_COPY | PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(id); phalcon_array_fetch_long(&id, params, 0, PH_NOISY_CC); if (!phalcon_array_isset_string(params, SS("name"))) { phalcon_array_update_string(¶ms, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { PHALCON_OBS_VAR(name); phalcon_array_fetch_string(&name, params, SL("name"), PH_NOISY_CC); if (!zend_is_true(name)) { phalcon_array_update_string(¶ms, SL("name"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC); } } /** * Automatically assign the id if the name is not an array */ if (!phalcon_memnstr_str(id, SL("[") TSRMLS_CC)) { if (!phalcon_array_isset_string(params, SS("id"))) { phalcon_array_update_string(¶ms, SL("id"), &id, PH_COPY | PH_SEPARATE TSRMLS_CC); } } if (!phalcon_array_isset_string(params, SS("value"))) { PHALCON_CALL_SELF_PARAMS_2(value, this_ptr, "getvalue", id, params); phalcon_array_update_string(¶ms, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC); } } else { if (phalcon_array_isset_long(params, 0)) { PHALCON_OBS_NVAR(value); phalcon_array_fetch_long(&value, params, 0, PH_NOISY_CC); phalcon_array_update_string(¶ms, SL("value"), &value, PH_COPY | PH_SEPARATE TSRMLS_CC); } } /** * Automatically check inputs */ if (PHALCON_IS_STRING(type, "checkbox")) { if (zend_is_true(value)) { phalcon_array_update_string_string(¶ms, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC); } } else { if (PHALCON_IS_STRING(type, "radio")) { if (zend_is_true(value)) { phalcon_array_update_string_string(¶ms, SL("checked"), SL("checked"), PH_SEPARATE TSRMLS_CC); } } } PHALCON_INIT_VAR(code); PHALCON_CONCAT_SVS(code, "<input type=\"", type, "\""); if (!phalcon_is_iterable(params, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(key, ah0, hp0); PHALCON_GET_FOREACH_VALUE(value); if (Z_TYPE_P(key) != IS_LONG) { PHALCON_SCONCAT_SVSVS(code, " ", key, "=\"", value, "\""); } zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(five); ZVAL_LONG(five, 5); PHALCON_OBS_VAR(doctype); phalcon_read_static_property(&doctype, SL("phalcon\\tag"), SL("_documentType") TSRMLS_CC); /** * Check if Doctype is XHTML */ PHALCON_INIT_VAR(is_xhtml); is_smaller_function(is_xhtml, five, doctype TSRMLS_CC); if (zend_is_true(is_xhtml)) { phalcon_concat_self_str(&code, SL(" />") TSRMLS_CC); } else { phalcon_concat_self_str(&code, SL(">") TSRMLS_CC); } RETURN_CTOR(code); }
/** * Transforms an intermediate representation for a expression into a database system valid expression * * @param array $expression * @param string $escapeChar * @return string */ PHP_METHOD(Phalcon_Db_Dialect, getSqlExpression){ zval *expression, *escape_char = NULL, *type, *name = NULL, *escaped_name = NULL; zval *domain, *escaped_domain = NULL, *value = NULL, *operator = NULL; zval *left = NULL, *expression_left = NULL, *right = NULL, *expression_right = NULL; zval *binary_expr, *unary_expr = NULL, *expression_group; zval *sql_arguments, *arguments, *argument = NULL, *argument_expression = NULL; zval *arguments_joined, *function_expression = NULL; zval *sql_items, *items, *item = NULL, *item_expression = NULL; zval *list_expression, *group_expression; zval *exception_message; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &expression, &escape_char) == FAILURE) { RETURN_MM_NULL(); } if (!escape_char) { PHALCON_INIT_VAR(escape_char); } else { PHALCON_SEPARATE_PARAM(escape_char); } if (PHALCON_GLOBAL(db).escape_identifiers) { if (Z_TYPE_P(escape_char) == IS_NULL) { PHALCON_OBS_NVAR(escape_char); phalcon_read_property(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY_CC); } } if (Z_TYPE_P(expression) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SQL expression"); return; } if (!phalcon_array_isset_string(expression, SS("type"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SQL expression"); return; } PHALCON_OBS_VAR(type); phalcon_array_fetch_string(&type, expression, SL("type"), PH_NOISY_CC); /** * Resolve qualified expressions */ if (PHALCON_IS_STRING(type, "qualified")) { PHALCON_OBS_VAR(name); phalcon_array_fetch_string(&name, expression, SL("name"), PH_NOISY_CC); if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_VAR(escaped_name); PHALCON_CONCAT_VVV(escaped_name, escape_char, name, escape_char); } else { PHALCON_CPY_WRT(escaped_name, name); } /** * A domain could be a table/schema */ if (phalcon_array_isset_string(expression, SS("domain"))) { PHALCON_OBS_VAR(domain); phalcon_array_fetch_string(&domain, expression, SL("domain"), PH_NOISY_CC); if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_VAR(escaped_domain); PHALCON_CONCAT_VVVSV(escaped_domain, escape_char, domain, escape_char, ".", escaped_name); } else { PHALCON_INIT_NVAR(escaped_domain); PHALCON_CONCAT_VSV(escaped_domain, domain, ".", escaped_name); } RETURN_CTOR(escaped_domain); } RETURN_CCTOR(escaped_name); } /** * Resolve literal expressions */ if (PHALCON_IS_STRING(type, "literal")) { PHALCON_OBS_VAR(value); phalcon_array_fetch_string(&value, expression, SL("value"), PH_NOISY_CC); RETURN_CCTOR(value); } /** * Resolve binary operations expressions */ if (PHALCON_IS_STRING(type, "binary-op")) { PHALCON_OBS_VAR(operator); phalcon_array_fetch_string(&operator, expression, SL("op"), PH_NOISY_CC); PHALCON_OBS_VAR(left); phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC); PHALCON_INIT_VAR(expression_left); PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char); PHALCON_OBS_VAR(right); phalcon_array_fetch_string(&right, expression, SL("right"), PH_NOISY_CC); PHALCON_INIT_VAR(expression_right); PHALCON_CALL_METHOD_PARAMS_2(expression_right, this_ptr, "getsqlexpression", right, escape_char); PHALCON_INIT_VAR(binary_expr); PHALCON_CONCAT_VSVSV(binary_expr, expression_left, " ", operator, " ", expression_right); RETURN_CTOR(binary_expr); } /** * Resolve unary operations expressions */ if (PHALCON_IS_STRING(type, "unary-op")) { PHALCON_OBS_NVAR(operator); phalcon_array_fetch_string(&operator, expression, SL("op"), PH_NOISY_CC); /** * Some unary operators uses the left operand... */ if (phalcon_array_isset_string(expression, SS("left"))) { PHALCON_OBS_NVAR(left); phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC); PHALCON_INIT_NVAR(expression_left); PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char); PHALCON_INIT_VAR(unary_expr); PHALCON_CONCAT_VV(unary_expr, expression_left, operator); RETURN_CTOR(unary_expr); } /** * ...Others uses the right operand */ if (phalcon_array_isset_string(expression, SS("right"))) { PHALCON_OBS_NVAR(right); phalcon_array_fetch_string(&right, expression, SL("right"), PH_NOISY_CC); PHALCON_INIT_NVAR(expression_right); PHALCON_CALL_METHOD_PARAMS_2(expression_right, this_ptr, "getsqlexpression", right, escape_char); PHALCON_INIT_NVAR(unary_expr); PHALCON_CONCAT_VV(unary_expr, operator, expression_right); RETURN_CTOR(unary_expr); } } /** * Resolve placeholder */ if (PHALCON_IS_STRING(type, "placeholder")) { PHALCON_OBS_NVAR(value); phalcon_array_fetch_string(&value, expression, SL("value"), PH_NOISY_CC); RETURN_CCTOR(value); } /** * Resolve parentheses */ if (PHALCON_IS_STRING(type, "parentheses")) { PHALCON_OBS_NVAR(left); phalcon_array_fetch_string(&left, expression, SL("left"), PH_NOISY_CC); PHALCON_INIT_NVAR(expression_left); PHALCON_CALL_METHOD_PARAMS_2(expression_left, this_ptr, "getsqlexpression", left, escape_char); PHALCON_INIT_VAR(expression_group); PHALCON_CONCAT_SVS(expression_group, "(", expression_left, ")"); RETURN_CTOR(expression_group); } /** * Resolve function calls */ if (PHALCON_IS_STRING(type, "functionCall")) { PHALCON_OBS_NVAR(name); phalcon_array_fetch_string(&name, expression, SL("name"), PH_NOISY_CC); PHALCON_INIT_VAR(sql_arguments); array_init(sql_arguments); if (phalcon_array_isset_string(expression, SS("arguments"))) { PHALCON_OBS_VAR(arguments); phalcon_array_fetch_string(&arguments, expression, SL("arguments"), PH_NOISY_CC); if (!phalcon_is_iterable(arguments, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(argument); PHALCON_INIT_NVAR(argument_expression); PHALCON_CALL_METHOD_PARAMS_2(argument_expression, this_ptr, "getsqlexpression", argument, escape_char); phalcon_array_append(&sql_arguments, argument_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(arguments_joined); phalcon_fast_join_str(arguments_joined, SL(", "), sql_arguments TSRMLS_CC); PHALCON_INIT_VAR(function_expression); PHALCON_CONCAT_VSVS(function_expression, name, "(", arguments_joined, ")"); } else { PHALCON_INIT_NVAR(function_expression); PHALCON_CONCAT_VS(function_expression, name, "()"); } RETURN_CTOR(function_expression); } /** * Resolve lists */ if (PHALCON_IS_STRING(type, "list")) { PHALCON_INIT_VAR(sql_items); array_init(sql_items); PHALCON_OBS_VAR(items); phalcon_array_fetch_long(&items, expression, 0, PH_NOISY_CC); if (!phalcon_is_iterable(items, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(item); PHALCON_INIT_NVAR(item_expression); PHALCON_CALL_METHOD_PARAMS_2(item_expression, this_ptr, "getsqlexpression", item, escape_char); phalcon_array_append(&sql_items, item_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } PHALCON_INIT_VAR(list_expression); phalcon_fast_join_str(list_expression, SL(", "), sql_items TSRMLS_CC); PHALCON_INIT_VAR(group_expression); PHALCON_CONCAT_SVS(group_expression, "(", list_expression, ")"); RETURN_CTOR(group_expression); } /** * Resolve * */ if (PHALCON_IS_STRING(type, "all")) { PHALCON_MM_RESTORE(); RETURN_STRING("*", 1); } /** * Expression type wasn't found */ PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Invalid SQL expression type '", type, "'"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_db_exception_ce, exception_message); return; }
/** * 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_Postgresql, describeColumns){ zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_num; zval *describe, *old_column = NULL, *field = NULL, *definition = NULL; zval *char_size = NULL, *numeric_size = NULL, *numeric_scale = NULL, *column_type = NULL; zval *attribute = NULL, *column_name = NULL, *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_INIT_VAR(columns); array_init(columns); PHALCON_OBS_VAR(dialect); phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); 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); PHALCON_INIT_VAR(describe); phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num); /** * 0:name, 1:type, 2:size, 3:numericsize, 4: null, 5: key, 6: extra, 7: position */ PHALCON_INIT_VAR(old_column); phalcon_is_iterable(describe, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(field); PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); PHALCON_OBS_NVAR(char_size); phalcon_array_fetch_long(&char_size, field, 2, PH_NOISY); PHALCON_OBS_NVAR(numeric_size); phalcon_array_fetch_long(&numeric_size, field, 3, PH_NOISY); PHALCON_OBS_NVAR(numeric_scale); phalcon_array_fetch_long(&numeric_scale, field, 4, PH_NOISY); PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY); /** * Check the column type to get the correct Phalcon type */ if (phalcon_memnstr_str(column_type, SL("int"))) { phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("varying"))) { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("date"))) { phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("numeric"))) { phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE); phalcon_array_update_string(&definition, SL("scale"), &numeric_scale, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("char"))) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("timestamp"))) { phalcon_array_update_string_long(&definition, SL("type"), 4, PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("text"))) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("float"))) { phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &numeric_size, PH_COPY | PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("bool"))) { phalcon_array_update_string_long(&definition, SL("type"), 8, PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_SEPARATE); } else { if (phalcon_memnstr_str(column_type, SL("uuid"))) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE); phalcon_array_update_string_long(&definition, SL("size"), 36, PH_SEPARATE); } else { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE); phalcon_array_update_string(&definition, SL("size"), &char_size, PH_COPY | PH_SEPARATE); } } } } } } } } } } if (phalcon_memnstr_str(column_type, SL("unsigned"))) { phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_SEPARATE); } if (Z_TYPE_P(old_column) == IS_NULL) { phalcon_array_update_string_bool(&definition, SL("first"), 1, 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, 6, PH_NOISY); if (PHALCON_IS_STRING(attribute, "PRI")) { phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE); } /** * Check if the column allows null values */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY); if (PHALCON_IS_STRING(attribute, "NO")) { phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE); } /** * Check if the column is auto increment */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY); if (PHALCON_IS_STRING(attribute, "auto_increment")) { phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_SEPARATE); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY); /** * Create a Phalcon\Db\Column to abstract the 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); }
/** * Magic method __get * * @param string $propertyName */ PHP_METHOD(Phalcon_DI_Injectable, __get){ zval *property_name, *dependency_injector = NULL; zval *has_service, *service = NULL, *class_name, *arguments; zval *persistent; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 0, &property_name); PHALCON_OBS_VAR(dependency_injector); phalcon_read_property_this(&dependency_injector, this_ptr, SL("_dependencyInjector"), PH_NOISY_CC); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_INIT_NVAR(dependency_injector); PHALCON_CALL_STATIC(dependency_injector, "phalcon\\di", "getdefault"); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_di_exception_ce, "A dependency injection object is required to access the application services"); return; } } /** * Fallback to the PHP userland if the cache is not available */ PHALCON_INIT_VAR(has_service); phalcon_call_method_p1(has_service, dependency_injector, "has", property_name); if (zend_is_true(has_service)) { PHALCON_INIT_VAR(service); phalcon_call_method_p1(service, dependency_injector, "getshared", property_name); phalcon_update_property_zval_zval(this_ptr, property_name, service TSRMLS_CC); RETURN_CCTOR(service); } if (PHALCON_IS_STRING(property_name, "di")) { phalcon_update_property_this(this_ptr, SL("di"), dependency_injector TSRMLS_CC); RETURN_CCTOR(dependency_injector); } /** * Accessing the persistent property will create a session bag in any class */ if (PHALCON_IS_STRING(property_name, "persistent")) { PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, this_ptr, 0 TSRMLS_CC); PHALCON_INIT_VAR(arguments); array_init_size(arguments, 1); phalcon_array_append(&arguments, class_name, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(service); ZVAL_STRING(service, "sessionBag", 1); PHALCON_INIT_VAR(persistent); phalcon_call_method_p2(persistent, dependency_injector, "get", service, arguments); phalcon_update_property_this(this_ptr, SL("persistent"), persistent TSRMLS_CC); RETURN_CCTOR(persistent); } /** * A notice is shown if the property is not defined and isn't a valid service */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Access to undefined property %s", Z_STRVAL_P(property_name)); RETURN_MM_NULL(); }
/** * 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); }
/** * Checks for annotations in the public methods of the controller * * @param string $controller * @param string $action * @param Phalcon\Annotations\Annotation $annotation */ PHP_METHOD(Phalcon_Mvc_Router_Annotations, processActionAnnotation){ zval *controller, *action, *annotation, *is_route = NULL; zval *methods = NULL, *name, *action_sufix, *empty_str; zval *real_action_name, *action_name, *route_prefix; zval *parameter = NULL, *paths = NULL, *position, *value, *uri = NULL, *route; zval *converts, *convert = NULL, *param = NULL, *route_name; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzz", &controller, &action, &annotation) == FAILURE) { RETURN_MM_NULL(); } PHALCON_INIT_VAR(is_route); ZVAL_BOOL(is_route, 0); PHALCON_INIT_VAR(methods); PHALCON_INIT_VAR(name); PHALCON_CALL_METHOD(name, annotation, "getname"); /** * Find if the route is for adding routes */ if (PHALCON_IS_STRING(name, "Route")) { ZVAL_BOOL(is_route, 1); } else { if (PHALCON_IS_STRING(name, "Get")) { ZVAL_BOOL(is_route, 1); ZVAL_STRING(methods, "GET", 1); } else { if (PHALCON_IS_STRING(name, "Post")) { ZVAL_BOOL(is_route, 1); PHALCON_INIT_NVAR(methods); ZVAL_STRING(methods, "POST", 1); } else { if (PHALCON_IS_STRING(name, "Put")) { ZVAL_BOOL(is_route, 1); PHALCON_INIT_NVAR(methods); ZVAL_STRING(methods, "PUT", 1); } else { if (PHALCON_IS_STRING(name, "Options")) { ZVAL_BOOL(is_route, 1); PHALCON_INIT_NVAR(methods); ZVAL_STRING(methods, "OPTIONS", 1); } } } } } if (PHALCON_IS_TRUE(is_route)) { PHALCON_OBS_VAR(action_sufix); phalcon_read_property(&action_sufix, this_ptr, SL("_actionSufix"), PH_NOISY_CC); PHALCON_INIT_VAR(empty_str); ZVAL_STRING(empty_str, "", 1); PHALCON_INIT_VAR(real_action_name); phalcon_fast_str_replace(real_action_name, action_sufix, empty_str, action TSRMLS_CC); PHALCON_INIT_VAR(action_name); phalcon_fast_strtolower(action_name, real_action_name); PHALCON_OBS_VAR(route_prefix); phalcon_read_property(&route_prefix, this_ptr, SL("_routePrefix"), PH_NOISY_CC); PHALCON_INIT_VAR(parameter); ZVAL_STRING(parameter, "paths", 1); /** * Check for existing paths in the annotation */ PHALCON_INIT_VAR(paths); PHALCON_CALL_METHOD_PARAMS_1(paths, annotation, "getnamedparameter", parameter); if (Z_TYPE_P(paths) != IS_ARRAY) { PHALCON_INIT_NVAR(paths); array_init(paths); } phalcon_array_update_string(&paths, SL("controller"), &controller, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&paths, SL("action"), &action_name, PH_COPY | PH_SEPARATE TSRMLS_CC); PHALCON_INIT_VAR(position); ZVAL_LONG(position, 0); PHALCON_INIT_VAR(value); PHALCON_CALL_METHOD_PARAMS_1(value, annotation, "getargument", position); /** * Create the route using the prefix */ if (Z_TYPE_P(value) != IS_NULL) { if (!PHALCON_IS_STRING(value, "/")) { PHALCON_INIT_VAR(uri); PHALCON_CONCAT_VV(uri, route_prefix, value); } else { PHALCON_CPY_WRT(uri, route_prefix); } } else { PHALCON_INIT_NVAR(uri); PHALCON_CONCAT_VV(uri, route_prefix, action_name); } /** * Add the route to the router */ PHALCON_INIT_VAR(route); PHALCON_CALL_METHOD_PARAMS_2(route, this_ptr, "add", uri, paths); if (Z_TYPE_P(methods) == IS_NULL) { PHALCON_INIT_NVAR(parameter); ZVAL_STRING(parameter, "methods", 1); PHALCON_INIT_NVAR(methods); PHALCON_CALL_METHOD_PARAMS_1(methods, annotation, "getnamedparameter", parameter); if (Z_TYPE_P(methods) == IS_ARRAY) { PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods); } else { if (Z_TYPE_P(methods) == IS_STRING) { PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods); } } } else { PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "via", methods); } PHALCON_INIT_NVAR(parameter); ZVAL_STRING(parameter, "converts", 1); PHALCON_INIT_VAR(converts); PHALCON_CALL_METHOD_PARAMS_1(converts, annotation, "getnamedparameter", parameter); if (Z_TYPE_P(converts) == IS_ARRAY) { if (!phalcon_is_iterable(converts, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(param, ah0, hp0); PHALCON_GET_FOREACH_VALUE(convert); PHALCON_CALL_METHOD_PARAMS_2_NORETURN(route, "convert", param, convert); zend_hash_move_forward_ex(ah0, &hp0); } } PHALCON_INIT_NVAR(parameter); ZVAL_STRING(parameter, "name", 1); PHALCON_INIT_VAR(route_name); PHALCON_CALL_METHOD_PARAMS_1(route_name, annotation, "getnamedparameter", parameter); if (Z_TYPE_P(route_name) == IS_STRING) { PHALCON_CALL_METHOD_PARAMS_1_NORETURN(route, "setname", route_name); } RETURN_MM_TRUE; } PHALCON_MM_RESTORE(); }
/** * The meta-data is obtained by reading the column descriptions from the database information schema * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){ zval *model, *dependency_injector, *service; zval *annotations = NULL, *class_name, *reflection = NULL; zval *exception_message = NULL, *properties_annotations = NULL; zval *attributes, *primary_keys, *non_primary_keys; zval *numeric_typed, *not_null, *field_types, *field_sizes, *field_bytes, *field_scales; zval *field_bind_types, *automatic_create_attributes, *automatic_update_attributes; zval *identity_field = NULL, *column_annot_name; zval *primary_annot_name, *id_annot_name; zval *column_map_name, *column_type_name, *column_size_name, *column_bytes_name, *column_scale_name, *column_nullable_name; zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL; zval *column_annotation = NULL, *real_property = NULL, *feature = NULL; zval *field_default_values, *column_default_value = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "annotations", 1); PHALCON_CALL_METHOD(&annotations, dependency_injector, "get", service); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 0 TSRMLS_CC); PHALCON_CALL_METHOD(&reflection, annotations, "get", class_name); if (Z_TYPE_P(reflection) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Get the properties defined in */ PHALCON_CALL_METHOD(&properties_annotations, reflection, "getpropertiesannotations"); if (!zend_is_true(properties_annotations) || !phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Initialize meta-data */ PHALCON_INIT_VAR(attributes); array_init(attributes); PHALCON_INIT_VAR(primary_keys); array_init(primary_keys); PHALCON_INIT_VAR(non_primary_keys); array_init(non_primary_keys); PHALCON_INIT_VAR(numeric_typed); array_init(numeric_typed); PHALCON_INIT_VAR(not_null); array_init(not_null); PHALCON_INIT_VAR(field_types); array_init(field_types); PHALCON_INIT_VAR(field_sizes); array_init(field_sizes); PHALCON_INIT_VAR(field_bytes); array_init(field_bytes); PHALCON_INIT_VAR(field_scales); array_init(field_scales); PHALCON_INIT_VAR(field_bind_types); array_init(field_bind_types); PHALCON_INIT_VAR(automatic_create_attributes); array_init(automatic_create_attributes); PHALCON_INIT_VAR(automatic_update_attributes); array_init(automatic_update_attributes); PHALCON_INIT_VAR(identity_field); ZVAL_FALSE(identity_field); PHALCON_INIT_VAR(field_default_values); array_init(field_default_values); PHALCON_INIT_VAR(column_annot_name); ZVAL_STRING(column_annot_name, "Column", 1); PHALCON_INIT_VAR(primary_annot_name); ZVAL_STRING(primary_annot_name, "Primary", 1); PHALCON_INIT_VAR(id_annot_name); ZVAL_STRING(id_annot_name, "Identity", 1); PHALCON_INIT_VAR(column_map_name); ZVAL_STRING(column_map_name, "column", 1); PHALCON_INIT_VAR(column_type_name); ZVAL_STRING(column_type_name, "type", 1); PHALCON_INIT_VAR(column_size_name); ZVAL_STRING(column_size_name, "size", 1); PHALCON_INIT_VAR(column_bytes_name); ZVAL_STRING(column_bytes_name, "bytes", 1); PHALCON_INIT_VAR(column_scale_name); ZVAL_STRING(column_scale_name, "scale", 1); PHALCON_INIT_VAR(column_default_value); ZVAL_STRING(column_default_value, "default", 1); PHALCON_INIT_VAR(column_nullable_name); ZVAL_STRING(column_nullable_name, "nullable", 1); phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(property, ah0, hp0); PHALCON_GET_HVALUE(prop_annotations); /** * All columns marked with the 'Column' annotation are considered columns */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", column_annot_name); if (!zend_is_true(has_annotation)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Fetch the 'column' annotation */ PHALCON_CALL_METHOD(&column_annotation, prop_annotations, "get", column_annot_name); /** * Check column map */ PHALCON_CALL_METHOD(&real_property, column_annotation, "getargument", column_map_name); if (PHALCON_IS_EMPTY(real_property)) { PHALCON_CPY_WRT(real_property, property); } /** * Check if annotation has the 'type' named parameter */ PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_type_name); if (PHALCON_IS_STRING(feature, "integer")) { phalcon_array_update_zval_long(&field_types, real_property, 0, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 1, PH_COPY); phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY); } else if (PHALCON_IS_STRING(feature, "decimal")) { phalcon_array_update_zval_long(&field_types, real_property, 3, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 32, PH_COPY); phalcon_array_update_zval_bool(&numeric_typed, real_property, 1, PH_COPY); } else if (PHALCON_IS_STRING(feature, "boolean")) { phalcon_array_update_zval_long(&field_types, real_property, 8, PH_COPY); phalcon_array_update_zval_long(&field_bind_types, real_property, 5, PH_COPY); } else { if (PHALCON_IS_STRING(feature, "date")) { phalcon_array_update_zval_long(&field_types, real_property, 1, PH_COPY); } else { /** * By default all columns are varchar/string */ phalcon_array_update_zval_long(&field_types, real_property, 2, PH_COPY); } phalcon_array_update_zval_long(&field_bind_types, real_property, 2, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_size_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_sizes, real_property, feature, PH_COPY); phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_bytes_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_bytes, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_scale_name); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_scales, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, real_property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY); } PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_default_value); if (!PHALCON_IS_EMPTY(feature)) { phalcon_array_update_zval(&field_default_values, property, feature, PH_COPY); } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", primary_annot_name); if (zend_is_true(has_annotation)) { phalcon_array_append(&primary_keys, real_property, PH_COPY); } else { phalcon_array_append(&non_primary_keys, real_property, PH_COPY); } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_CALL_METHOD(&has_annotation, prop_annotations, "has", id_annot_name); if (zend_is_true(has_annotation)) { PHALCON_CPY_WRT(identity_field, real_property); } /** * Check if the column */ PHALCON_CALL_METHOD(&feature, column_annotation, "getargument", column_nullable_name); if (!zend_is_true(feature)) { phalcon_array_append(¬_null, real_property, PH_COPY); } phalcon_array_append(&attributes, real_property, PH_COPY); zend_hash_move_forward_ex(ah0, &hp0); } /** * Create an array using the MODELS_* constants as indexes */ array_init_size(return_value, 14); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_ATTRIBUTES, attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_PRIMARY_KEY, primary_keys, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NON_PRIMARY_KEY, non_primary_keys, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_NOT_NULL, not_null, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES, field_types, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_NUMERIC, numeric_typed, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_IDENTITY_COLUMN, identity_field, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_TYPES_BIND, field_bind_types, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_INSERT, automatic_create_attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_AUTOMATIC_DEFAULT_UPDATE, automatic_update_attributes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_DEFAULT_VALUE, field_default_values, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SZIE, field_sizes, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_SCALE, field_scales, PH_COPY); phalcon_array_update_long(&return_value, PHALCON_MVC_MODEL_METADATA_MODELS_DATA_BYTE, field_bytes, PH_COPY); PHALCON_MM_RESTORE(); }
/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return Phalcon\Mvc\Model\Criteria */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data = NULL, *model; zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL; zval *value_pattern = NULL, *join_conditions; zval *column_map = NULL; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } object_init_ex(return_value, phalcon_mvc_model_criteria_ce); if (zend_hash_num_elements(Z_ARRVAL_P(data))) { PHALCON_INIT_VAR(service); PHALCON_ZVAL_MAYBE_INTERNED_STRING(service, phalcon_interned_modelsMetadata); PHALCON_CALL_METHOD(&meta_data, dependency_injector, "getshared", service); PHALCON_VERIFY_INTERFACE(meta_data, phalcon_mvc_model_metadatainterface_ce); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { PHALCON_CALL_METHOD(NULL, model, "__construct"); } PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1); if (PHALCON_GLOBAL(orm).column_renaming) { PHALCON_CALL_METHOD(&column_map, meta_data, "getreversecolumnmap", model); if (Z_TYPE_P(column_map) != IS_ARRAY) { PHALCON_INIT_NVAR(column_map); } } else { column_map = PHALCON_GLOBAL(z_null); } PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); PHALCON_INIT_VAR(conditions); array_init(conditions); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { zval *real_field; PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) { real_field = field; } if (phalcon_array_isset_fetch(&type, data_types, real_field)) { if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) { if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, value, PH_COPY); } phalcon_array_append(&conditions, condition, 0); } } zend_hash_move_forward_ex(ah0, &hp0); } if (zend_hash_num_elements(Z_ARRVAL_P(conditions))) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); PHALCON_CALL_METHOD(NULL, return_value, "where", join_conditions, bind); } } PHALCON_CALL_METHOD(NULL, return_value, "setmodelname", model_name); RETURN_MM(); }
/** * Produces a recursive representation of an array * * @param array $argument * @return string */ PHP_METHOD(Phalcon_Debug, _getArrayDump) { zval *argument, *n = NULL, *number_arguments, *dump; zval *v = NULL, *var_dump = NULL, *escaped_string = NULL, *next = NULL, *array_dump = NULL; zval *class_name = NULL, *joined_dump; zend_string *str_key; ulong idx; PHALCON_MM_GROW(); phalcon_fetch_params(0, 1, 1, &argument, &n); if (!n) { PHALCON_INIT_VAR(n); ZVAL_LONG(n, 0); } PHALCON_INIT_VAR(number_arguments); phalcon_fast_count(number_arguments, argument); if (PHALCON_LT_LONG(n, 3)) { if (PHALCON_GT_LONG(number_arguments, 0)) { if (PHALCON_LT_LONG(number_arguments, 10)) { PHALCON_INIT_VAR(dump); array_init(dump); ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(argument), idx, str_key, v) { zval tmp; if (str_key) { ZVAL_STR(&tmp, str_key); } else { ZVAL_LONG(&tmp, idx); } if (PHALCON_IS_SCALAR(v)) { if (PHALCON_IS_STRING(v, "")) { PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVS(var_dump, "[", &tmp, "] => (empty string)"); } else { PHALCON_CALL_METHOD(&escaped_string, getThis(), "_escapestring", v); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSV(var_dump, "[", &tmp, "] => ", escaped_string); } phalcon_array_append(dump, var_dump, PH_COPY); } else { if (Z_TYPE_P(v) == IS_ARRAY) { PHALCON_INIT_NVAR(next); phalcon_add_function(next, n, &PHALCON_GLOBAL(z_one)); PHALCON_CALL_METHOD(&array_dump, getThis(), "_getarraydump", v, next); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSVS(var_dump, "[", &tmp, "] => Array(", array_dump, ")"); phalcon_array_append(dump, var_dump, PH_COPY); continue; } if (Z_TYPE_P(v) == IS_OBJECT) { zend_class_entry *ce = Z_OBJCE_P(v); PHALCON_INIT_NVAR(class_name); ZVAL_NEW_STR(class_name, ce->name); PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSVS(var_dump, "[", &tmp, "] => Object(", class_name, ")"); phalcon_array_append(dump, var_dump, PH_COPY); continue; } if (Z_TYPE_P(v) == IS_NULL) { PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVS(var_dump, "[", &tmp, "] => null"); phalcon_array_append(dump, var_dump, PH_COPY); continue; } PHALCON_INIT_NVAR(var_dump); PHALCON_CONCAT_SVSV(var_dump, "[", &tmp, "] => ", v); phalcon_array_append(dump, var_dump, PH_COPY); } } ZEND_HASH_FOREACH_END(); PHALCON_INIT_VAR(joined_dump); phalcon_fast_join_str(joined_dump, SL(", "), dump); RETURN_CTOR(joined_dump); } RETURN_NCTOR(number_arguments); }
/** * The meta-data is obtained by reading the column descriptions from the database information schema * * @param Phalcon\Mvc\ModelInterface $model * @param Phalcon\DiInterface $dependencyInjector * @return array */ PHP_METHOD(Phalcon_Mvc_Model_MetaData_Strategy_Annotations, getMetaData){ zval *model, *dependency_injector, *service; zval *annotations, *class_name, *reflection; zval *exception_message = NULL, *properties_annotations; zval *attributes, *primary_keys, *non_primary_keys; zval *numeric_typed, *not_null, *field_types; zval *field_bind_types, *automatic_default; zval *identity_field = NULL, *column_annot_name; zval *primary_annot_name, *id_annot_name; zval *column_type_name, *column_nullable_name; zval *prop_annotations = NULL, *property = NULL, *has_annotation = NULL; zval *column_annotation = NULL, *feature = NULL, *model_metadata; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &model, &dependency_injector); if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The dependency injector is invalid"); return; } PHALCON_INIT_VAR(service); ZVAL_STRING(service, "annotations", 1); PHALCON_INIT_VAR(annotations); phalcon_call_method_p1(annotations, dependency_injector, "get", service); PHALCON_INIT_VAR(class_name); phalcon_get_class(class_name, model, 0 TSRMLS_CC); PHALCON_INIT_VAR(reflection); phalcon_call_method_p1(reflection, annotations, "get", class_name); if (Z_TYPE_P(reflection) != IS_OBJECT) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Get the properties defined in */ PHALCON_INIT_VAR(properties_annotations); phalcon_call_method(properties_annotations, reflection, "getpropertiesannotations"); if (!phalcon_fast_count_ev(properties_annotations TSRMLS_CC)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SV(exception_message, "No properties with annotations were found in class ", class_name); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message); return; } /** * Initialize meta-data */ PHALCON_INIT_VAR(attributes); array_init(attributes); PHALCON_INIT_VAR(primary_keys); array_init(primary_keys); PHALCON_INIT_VAR(non_primary_keys); array_init(non_primary_keys); PHALCON_INIT_VAR(numeric_typed); array_init(numeric_typed); PHALCON_INIT_VAR(not_null); array_init(not_null); PHALCON_INIT_VAR(field_types); array_init(field_types); PHALCON_INIT_VAR(field_bind_types); array_init(field_bind_types); PHALCON_INIT_VAR(automatic_default); array_init(automatic_default); PHALCON_INIT_VAR(identity_field); ZVAL_BOOL(identity_field, 0); PHALCON_INIT_VAR(column_annot_name); ZVAL_STRING(column_annot_name, "Column", 1); PHALCON_INIT_VAR(primary_annot_name); ZVAL_STRING(primary_annot_name, "Primary", 1); PHALCON_INIT_VAR(id_annot_name); ZVAL_STRING(id_annot_name, "Identity", 1); PHALCON_INIT_VAR(column_type_name); ZVAL_STRING(column_type_name, "type", 1); PHALCON_INIT_VAR(column_nullable_name); ZVAL_STRING(column_nullable_name, "nullable", 1); phalcon_is_iterable(properties_annotations, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(property, ah0, hp0); PHALCON_GET_HVALUE(prop_annotations); /** * All columns marked with the 'Column' annotation are considered columns */ PHALCON_INIT_NVAR(has_annotation); phalcon_call_method_p1(has_annotation, prop_annotations, "has", column_annot_name); if (!zend_is_true(has_annotation)) { zend_hash_move_forward_ex(ah0, &hp0); continue; } /** * Fetch the 'column' annotation */ PHALCON_INIT_NVAR(column_annotation); phalcon_call_method_p1(column_annotation, prop_annotations, "get", column_annot_name); /** * Check if annotation has the 'type' named parameter */ PHALCON_INIT_NVAR(feature); phalcon_call_method_p1(feature, column_annotation, "getnamedparameter", column_type_name); if (PHALCON_IS_STRING(feature, "integer")) { phalcon_array_update_zval_long(&field_types, property, 0, PH_SEPARATE); phalcon_array_update_zval_long(&field_bind_types, property, 1, PH_SEPARATE); phalcon_array_update_zval_bool(&numeric_typed, property, 1, PH_SEPARATE); } else { if (PHALCON_IS_STRING(feature, "decimal")) { phalcon_array_update_zval_long(&field_types, property, 3, PH_SEPARATE); phalcon_array_update_zval_long(&field_bind_types, property, 32, PH_SEPARATE); phalcon_array_update_zval_bool(&numeric_typed, property, 1, PH_SEPARATE); } else { if (PHALCON_IS_STRING(feature, "boolean")) { phalcon_array_update_zval_long(&field_types, property, 8, PH_SEPARATE); phalcon_array_update_zval_long(&field_bind_types, property, 5, PH_SEPARATE); } else { if (PHALCON_IS_STRING(feature, "date")) { phalcon_array_update_zval_long(&field_types, property, 1, PH_SEPARATE); } else { /** * By default all columns are varchar/string */ phalcon_array_update_zval_long(&field_types, property, 2, PH_SEPARATE); } phalcon_array_update_zval_long(&field_bind_types, property, 2, PH_SEPARATE); } } } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_INIT_NVAR(has_annotation); phalcon_call_method_p1(has_annotation, prop_annotations, "has", primary_annot_name); if (zend_is_true(has_annotation)) { phalcon_array_append(&primary_keys, property, PH_SEPARATE); } else { phalcon_array_append(&non_primary_keys, property, PH_SEPARATE); } /** * All columns marked with the 'Primary' annotation are considered primary keys */ PHALCON_INIT_NVAR(has_annotation); phalcon_call_method_p1(has_annotation, prop_annotations, "has", id_annot_name); if (zend_is_true(has_annotation)) { PHALCON_CPY_WRT(identity_field, property); } /** * Check if the column */ PHALCON_INIT_NVAR(feature); phalcon_call_method_p1(feature, column_annotation, "getnamedparameter", column_nullable_name); if (!zend_is_true(feature)) { phalcon_array_append(¬_null, property, PH_SEPARATE); } phalcon_array_append(&attributes, property, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } /** * Create an array using the MODELS_* constants as indexes */ PHALCON_INIT_VAR(model_metadata); array_init(model_metadata); phalcon_array_update_long(&model_metadata, 0, &attributes, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 1, &primary_keys, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 2, &non_primary_keys, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 3, ¬_null, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 4, &field_types, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 5, &numeric_typed, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 8, &identity_field, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 9, &field_bind_types, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 10, &automatic_default, PH_COPY | PH_SEPARATE); phalcon_array_update_long(&model_metadata, 11, &automatic_default, PH_COPY | PH_SEPARATE); RETURN_CTOR(model_metadata); }
/** * Listens for notifications from the models manager * * @param string $type * @param Phalcon\Mvc\ModelInterface $model */ PHP_METHOD(Phalcon_Mvc_Model_Behavior_SoftDelete, notify){ zval *type, *model, *options, *skip, *value, *field, *actual_value; zval *update_model, *status, *messages, *message = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 2, 0, &type, &model); if (PHALCON_IS_STRING(type, "beforeDelete")) { PHALCON_INIT_VAR(options); phalcon_call_method(options, this_ptr, "getoptions"); if (!phalcon_array_isset_string(options, SS("value"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'value' is required"); return; } if (!phalcon_array_isset_string(options, SS("field"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "The option 'field' is required"); return; } PHALCON_INIT_VAR(skip); ZVAL_BOOL(skip, 1); /** * Skip the current operation */ phalcon_call_method_p1_noret(model, "skipoperation", skip); /** * 'value' is the value to be updated instead of delete the record */ PHALCON_OBS_VAR(value); phalcon_array_fetch_string(&value, options, SL("value"), PH_NOISY_CC); /** * 'field' is the attribute to be updated instead of delete the record */ PHALCON_OBS_VAR(field); phalcon_array_fetch_string(&field, options, SL("field"), PH_NOISY_CC); PHALCON_INIT_VAR(actual_value); phalcon_call_method_p1(actual_value, model, "readattribute", field); /** * If the record is already flagged as 'deleted' we don't delete it again */ if (!PHALCON_IS_EQUAL(actual_value, value)) { /** * Clone the current model to make a clean new operation */ PHALCON_INIT_VAR(update_model); if (phalcon_clone(update_model, model TSRMLS_CC) == FAILURE) { return; } phalcon_call_method_p2_noret(update_model, "writeattribute", field, value); /** * Update the cloned model */ PHALCON_INIT_VAR(status); phalcon_call_method(status, update_model, "save"); if (!zend_is_true(status)) { /** * Transfer the messages from the cloned model to the original model */ PHALCON_INIT_VAR(messages); phalcon_call_method(messages, update_model, "getmessages"); phalcon_is_iterable(messages, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(message); phalcon_call_method_p1_noret(model, "appendmessage", message); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_MM_FALSE; } /** * Update the original model too */ phalcon_call_method_p2_noret(model, "writeattribute", field, value); } } PHALCON_MM_RESTORE(); }
PHP_METHOD(Phalcon_Http_Client_Adapter_Stream, buildBody){ zval *stream, *header, *data, *file, *username, *password, *authtype, *digest, *method, *entity_body; zval *key = NULL, *value = NULL, *realm, *qop, *nonce, *nc, *cnonce, *qoc, *ha1 = NULL, *path = NULL, *md5_entity_body = NULL, *ha2 = NULL; zval *http, *option = NULL, *body, *headers = NULL, *uniqid = NULL, *boundary; zval *path_parts = NULL, *filename, *basename, *filedata = NULL, *tmp = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); stream = phalcon_fetch_nproperty_this(this_ptr, SL("_stream"), PH_NOISY TSRMLS_CC); header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC); data = phalcon_fetch_nproperty_this(this_ptr, SL("_data"), PH_NOISY TSRMLS_CC); file = phalcon_fetch_nproperty_this(this_ptr, SL("_file"), PH_NOISY TSRMLS_CC); username = phalcon_fetch_nproperty_this(this_ptr, SL("_username"), PH_NOISY TSRMLS_CC); password = phalcon_fetch_nproperty_this(this_ptr, SL("_password"), PH_NOISY TSRMLS_CC); authtype = phalcon_fetch_nproperty_this(this_ptr, SL("_authtype"), PH_NOISY TSRMLS_CC); digest = phalcon_fetch_nproperty_this(this_ptr, SL("_digest"), PH_NOISY TSRMLS_CC); method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC); entity_body = phalcon_fetch_nproperty_this(this_ptr, SL("_entity_body"), PH_NOISY TSRMLS_CC); if (PHALCON_IS_NOT_EMPTY(username)) { if (PHALCON_IS_STRING(authtype, "basic")) { PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Authorization", 1); PHALCON_INIT_NVAR(value); PHALCON_CONCAT_SVSV(value, "Basic ", username, ":", password); PHALCON_CALL_METHOD(NULL, header, "set", key, value); } else if (PHALCON_IS_STRING(authtype, "digest") && PHALCON_IS_NOT_EMPTY(digest)) { if (phalcon_array_isset_string_fetch(&realm, digest, SS("realm"))) { PHALCON_INIT_VAR(realm); ZVAL_NULL(realm); } PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSVSV(tmp, username, ":", realm, ":", password); PHALCON_CALL_FUNCTION(&ha1, "md5", tmp); if (!phalcon_array_isset_string_fetch(&qop, digest, SS("qop"))) { PHALCON_INIT_VAR(qop); ZVAL_NULL(qop); } if (PHALCON_IS_EMPTY(qop) || phalcon_memnstr_str(qop, SL("auth"))) { PHALCON_CALL_SELF(&path, "getpath"); PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSV(tmp, method, ":", path); PHALCON_CALL_FUNCTION(&ha2, "md5", tmp); } else if (phalcon_memnstr_str(qop, SL("auth-int"))) { PHALCON_CALL_SELF(&path, "getpath"); PHALCON_CALL_FUNCTION(&md5_entity_body, "md5", entity_body); PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSVSV(tmp, method, ":", path, ":", md5_entity_body); PHALCON_CALL_FUNCTION(&ha2, "md5", tmp); } PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Authorization", 1); if (phalcon_array_isset_string_fetch(&nonce, digest, SS("nonce"))) { PHALCON_INIT_VAR(nonce); ZVAL_NULL(nonce); } if (PHALCON_IS_EMPTY(qop)) { PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSVSV(tmp, ha1, ":", nonce, ":", ha2); PHALCON_CALL_FUNCTION(&value, "md5", tmp); PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_SV(tmp, "Digest ", value); PHALCON_CALL_METHOD(NULL, header, "set", key, tmp); } else { if (phalcon_array_isset_string_fetch(&nc, digest, SS("nc"))) { PHALCON_INIT_VAR(nc); ZVAL_NULL(nc); } if (phalcon_array_isset_string_fetch(&cnonce, digest, SS("cnonce"))) { PHALCON_INIT_VAR(cnonce); ZVAL_NULL(cnonce); } if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) { PHALCON_INIT_VAR(qoc); ZVAL_NULL(qoc); } if (phalcon_array_isset_string_fetch(&qoc, digest, SS("qoc"))) { PHALCON_INIT_VAR(qoc); ZVAL_NULL(qoc); } PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSVSVS(tmp, ha1, ":", nonce, ":", nc, ":"); PHALCON_SCONCAT_VSVSV(tmp, cnonce, ":", qoc, ":", ha2); PHALCON_CALL_FUNCTION(&value, "md5", tmp); PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_SV(tmp, "Digest ", value); PHALCON_CALL_METHOD(NULL, header, "set", key, tmp); } } } PHALCON_INIT_VAR(http); ZVAL_STRING(http, "http", 1); PHALCON_CALL_FUNCTION(&uniqid, "uniqid"); PHALCON_INIT_VAR(boundary); PHALCON_CONCAT_SV(boundary, "--------------", uniqid); PHALCON_INIT_VAR(body); if (PHALCON_IS_NOT_EMPTY(data) && Z_TYPE_P(data) == IS_STRING){ PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Type", 1); PHALCON_INIT_NVAR(value); ZVAL_STRING(value, "application/x-www-form-urlencoded", 1); PHALCON_CALL_METHOD(NULL, header, "set", key, value); PHALCON_INIT_NVAR(option); ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS); PHALCON_CALL_METHOD(&headers, header, "build", option); PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "header", 1); PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, headers); PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "content", 1); PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, data); RETURN_MM(); } if (Z_TYPE_P(data) == IS_ARRAY) { phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(key, ah0, hp0); PHALCON_GET_HVALUE(value); PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n"); PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", key, "\"\r\n\r\n", value, "\r\n"); zend_hash_move_forward_ex(ah0, &hp0); } } if (PHALCON_IS_NOT_EMPTY(file)) { PHALCON_CALL_FUNCTION(&path_parts, "pathinfo", file); if (phalcon_array_isset_string_fetch(&filename, path_parts, SS("filename")) && phalcon_array_isset_string_fetch(&basename, path_parts, SS("basename"))) { PHALCON_CALL_FUNCTION(&filedata, "file_get_contents", file); PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n"); PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", filename, "\"; filename=\"", basename, "\"\r\n"); PHALCON_SCONCAT_SVS(body, "Content-Type: application/octet-stream\r\n\r\n", filedata, "\r\n"); } } if (!PHALCON_IS_EMPTY(body)) { PHALCON_SCONCAT_SVS(body, "--", boundary, "--\r\n"); PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Type", 1); PHALCON_INIT_NVAR(value); PHALCON_CONCAT_SV(value, "multipart/form-data; boundary=", boundary); PHALCON_CALL_METHOD(NULL, header, "set", key, value); PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Length", 1); PHALCON_INIT_NVAR(value); ZVAL_LONG(value, Z_STRLEN_P(body)); PHALCON_CALL_METHOD(NULL, header, "set", key, value); PHALCON_INIT_NVAR(option); ZVAL_LONG(option, PHALCON_HTTP_CLIENT_HEADER_BUILD_FIELDS); PHALCON_CALL_METHOD(&headers, header, "build", option); PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "header", 1); PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, headers); PHALCON_INIT_NVAR(option); ZVAL_STRING(option, "content", 1); PHALCON_CALL_FUNCTION(NULL, "stream_context_set_option", stream, http, option, body); } PHALCON_MM_RESTORE(); }
/** * Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST * * @param Phalcon\DiInterface $dependencyInjector * @param string $modelName * @param array $data * @return static */ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){ zval *dependency_injector, *model_name, *data; zval *conditions, *service, *meta_data, *model; zval *data_types, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL; zval *value_pattern = NULL, *criteria, *join_conditions; HashTable *ah0; HashPosition hp0; zval **hd; zend_class_entry *ce0; PHALCON_MM_GROW(); phalcon_fetch_params(1, 3, 0, &dependency_injector, &model_name, &data); if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array"); return; } if (Z_TYPE_P(dependency_injector) != IS_OBJECT) { PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services"); return; } PHALCON_INIT_VAR(conditions); array_init(conditions); if (phalcon_fast_count_ev(data TSRMLS_CC)) { PHALCON_INIT_VAR(service); ZVAL_STRING(service, "modelsMetadata", 1); PHALCON_INIT_VAR(meta_data); phalcon_call_method_p1(meta_data, dependency_injector, "getshared", service); ce0 = phalcon_fetch_class(model_name TSRMLS_CC); PHALCON_INIT_VAR(model); object_init_ex(model, ce0); if (phalcon_has_constructor(model TSRMLS_CC)) { phalcon_call_method_noret(model, "__construct"); } PHALCON_INIT_VAR(data_types); phalcon_call_method_p1(data_types, meta_data, "getdatatypes", model); PHALCON_INIT_VAR(bind); array_init(bind); /** * We look for attributes in the array passed as data */ phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(field, ah0, hp0); PHALCON_GET_HVALUE(value); if (phalcon_array_isset(data_types, field)) { if (Z_TYPE_P(value) != IS_NULL) { if (!PHALCON_IS_STRING(value, "")) { PHALCON_OBS_NVAR(type); phalcon_array_fetch(&type, data_types, field, PH_NOISY_CC); if (PHALCON_IS_LONG(type, 2)) { /** * For varchar types we use LIKE operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":"); PHALCON_INIT_NVAR(value_pattern); PHALCON_CONCAT_SVS(value_pattern, "%", value, "%"); phalcon_array_update_zval(&bind, field, &value_pattern, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { /** * For the rest of data types we use a plain = operator */ PHALCON_INIT_NVAR(condition); PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":"); phalcon_array_update_zval(&bind, field, &value, PH_COPY | PH_SEPARATE TSRMLS_CC); } phalcon_array_append(&conditions, condition, PH_SEPARATE TSRMLS_CC); } } } zend_hash_move_forward_ex(ah0, &hp0); } } /** * Create an object instance and pass the paramaters to it */ PHALCON_INIT_VAR(criteria); object_init_ex(criteria, phalcon_mvc_model_criteria_ce); if (phalcon_fast_count_ev(conditions TSRMLS_CC)) { PHALCON_INIT_VAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), conditions TSRMLS_CC); phalcon_call_method_p1_noret(criteria, "where", join_conditions); phalcon_call_method_p1_noret(criteria, "bind", bind); } RETURN_CTOR(criteria); }
PHP_METHOD(Phalcon_Http_Client_Adapter_Curl, sendInternal){ zval *uri = NULL, *url = NULL, *method, *useragent, *data, *type, *files, *timeout, *curl, *username, *password, *authtype; zval *file = NULL, *body, *uniqid = NULL, *boundary, *key = NULL, *value = NULL; zval *path_parts = NULL, *filename, *basename, *filedata = NULL; zval *constant, *header, *headers = NULL; zval *content = NULL, *errorno = NULL, *error = NULL, *headersize = NULL, *httpcode = NULL, *headerstr, *bodystr, *response, *tmp = NULL; zend_class_entry *curlfile_ce; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); PHALCON_CALL_SELF(&uri, "geturi"); PHALCON_CALL_METHOD(&url, uri, "build"); method = phalcon_fetch_nproperty_this(this_ptr, SL("_method"), PH_NOISY TSRMLS_CC); useragent = phalcon_fetch_nproperty_this(this_ptr, SL("_useragent"), PH_NOISY TSRMLS_CC); data = phalcon_fetch_nproperty_this(this_ptr, SL("_data"), PH_NOISY TSRMLS_CC); type = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC); files = phalcon_fetch_nproperty_this(this_ptr, SL("_files"), PH_NOISY TSRMLS_CC); timeout = phalcon_fetch_nproperty_this(this_ptr, SL("_timeout"), PH_NOISY TSRMLS_CC); curl = phalcon_fetch_nproperty_this(this_ptr, SL("_curl"), PH_NOISY TSRMLS_CC); username = phalcon_fetch_nproperty_this(this_ptr, SL("_username"), PH_NOISY TSRMLS_CC); password = phalcon_fetch_nproperty_this(this_ptr, SL("_password"), PH_NOISY TSRMLS_CC); authtype = phalcon_fetch_nproperty_this(this_ptr, SL("_authtype"), PH_NOISY TSRMLS_CC); PHALCON_INIT_VAR(constant); if (!zend_get_constant(SL("CURLOPT_URL"), constant TSRMLS_CC)) { RETURN_MM_FALSE; } PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, url); PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_CONNECTTIMEOUT"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, timeout); } PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_TIMEOUT"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, timeout); } PHALCON_INIT_NVAR(constant); if (PHALCON_IS_NOT_EMPTY(method) && zend_get_constant(SL("CURLOPT_CUSTOMREQUEST"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, method); } PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_USERAGENT"), constant TSRMLS_CC) && PHALCON_IS_NOT_EMPTY(useragent)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, useragent); } header = phalcon_fetch_nproperty_this(this_ptr, SL("_header"), PH_NOISY TSRMLS_CC); if (PHALCON_IS_NOT_EMPTY(username)) { if (PHALCON_IS_STRING(authtype, "any")) { PHALCON_INIT_NVAR(constant); PHALCON_INIT_NVAR(tmp); if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_ANY"), tmp TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) { PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSV(tmp, username, ":", password); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); } } } else if (PHALCON_IS_STRING(authtype, "basic")) { PHALCON_INIT_NVAR(constant); PHALCON_INIT_NVAR(tmp); if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_BASIC"), tmp TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) { PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSV(tmp, username, ":", password); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); } } } else if (PHALCON_IS_STRING(authtype, "digest")) { PHALCON_INIT_NVAR(constant); PHALCON_INIT_NVAR(tmp); if (zend_get_constant(SL("CURLOPT_HTTPAUTH"), constant TSRMLS_CC) && zend_get_constant(SL("CURLAUTH_DIGEST"), tmp TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_USERPWD"), constant TSRMLS_CC)) { PHALCON_INIT_NVAR(tmp); PHALCON_CONCAT_VSV(tmp, username, ":", password); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, tmp); } } } } PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLOPT_SAFE_UPLOAD"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, PHALCON_GLOBAL(z_true)); } if (Z_TYPE_P(data) == IS_STRING && PHALCON_IS_NOT_EMPTY(data)) { PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Type", 1); if (PHALCON_IS_EMPTY(type)) { PHALCON_INIT_NVAR(type); ZVAL_STRING(type, "application/x-www-form-urlencoded", 1); } PHALCON_CALL_METHOD(NULL, header, "set", key, type); PHALCON_INIT_NVAR(constant); zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, data); } else if (phalcon_class_exists(SL("CURLFile"), 0 TSRMLS_CC)) { if (Z_TYPE_P(data) != IS_ARRAY) { PHALCON_INIT_NVAR(data); array_init(data); } if (Z_TYPE_P(files) == IS_ARRAY) { phalcon_is_iterable(files, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(file); if (PHALCON_IS_NOT_EMPTY(file)) { curlfile_ce = zend_fetch_class(SL("CURLFile"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC); PHALCON_INIT_NVAR(tmp); object_init_ex(tmp, curlfile_ce); PHALCON_CALL_METHOD(NULL, tmp, "__construct", file); phalcon_array_append(&data, tmp, PH_COPY); } zend_hash_move_forward_ex(ah0, &hp0); } } PHALCON_INIT_NVAR(constant); zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, data); } else { PHALCON_CALL_FUNCTION(&uniqid, "uniqid"); PHALCON_INIT_VAR(boundary); PHALCON_CONCAT_SV(boundary, "--------------", uniqid); PHALCON_INIT_VAR(body); if (Z_TYPE_P(data) == IS_ARRAY) { phalcon_is_iterable(data, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(key, ah0, hp0); PHALCON_GET_HVALUE(value); PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n"); PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", key, "\"\r\n\r\n", value, "\r\n"); zend_hash_move_forward_ex(ah0, &hp0); } } if (Z_TYPE_P(files) == IS_ARRAY) { phalcon_is_iterable(files, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(file); if (PHALCON_IS_NOT_EMPTY(file)) { PHALCON_CALL_FUNCTION(&path_parts, "pathinfo", file); if (phalcon_array_isset_string_fetch(&filename, path_parts, SS("filename")) && phalcon_array_isset_string_fetch(&basename, path_parts, SS("basename"))) { PHALCON_CALL_FUNCTION(&filedata, "file_get_contents", file); PHALCON_SCONCAT_SVS(body, "--", boundary, "\r\n"); PHALCON_SCONCAT_SVSVS(body, "Content-Disposition: form-data; name=\"", filename, "\"; filename=\"", basename, "\"\r\n"); PHALCON_SCONCAT_SVS(body, "Content-Type: application/octet-stream\r\n\r\n", filedata, "\r\n"); } } zend_hash_move_forward_ex(ah1, &hp1); } } if (!PHALCON_IS_EMPTY(body)) { PHALCON_SCONCAT_SVS(body, "--", boundary, "--\r\n"); PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Type", 1); PHALCON_INIT_NVAR(value); PHALCON_CONCAT_SV(value, "multipart/form-data; boundary=", boundary); PHALCON_CALL_METHOD(NULL, header, "set", key, value); PHALCON_INIT_NVAR(key); ZVAL_STRING(key, "Content-Length", 1); PHALCON_INIT_NVAR(value); ZVAL_LONG(value, Z_STRLEN_P(body)); PHALCON_CALL_METHOD(NULL, header, "set", key, value); PHALCON_CALL_METHOD(&headers, header, "build"); PHALCON_INIT_NVAR(constant); zend_get_constant(SL("CURLOPT_POSTFIELDS"), constant TSRMLS_CC); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, body); PHALCON_INIT_NVAR(constant); zend_get_constant(SL("CURLOPT_HTTPHEADER"), constant TSRMLS_CC); PHALCON_CALL_FUNCTION(NULL, "curl_setopt", curl, constant, headers); } } PHALCON_CALL_FUNCTION(&content, "curl_exec", curl); PHALCON_CALL_FUNCTION(&errorno, "curl_errno", curl); if (PHALCON_IS_TRUE(errorno)) { PHALCON_CALL_FUNCTION(&error, "curl_error", curl); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_http_client_exception_ce, error); return; } PHALCON_INIT_VAR(response); object_init_ex(response, phalcon_http_client_response_ce); PHALCON_CALL_METHOD(NULL, response, "__construct"); PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLINFO_HTTP_CODE"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(&httpcode, "curl_getinfo", curl, constant); PHALCON_CALL_METHOD(NULL, response, "setstatuscode", httpcode); } PHALCON_INIT_NVAR(constant); if (zend_get_constant(SL("CURLINFO_HEADER_SIZE"), constant TSRMLS_CC)) { PHALCON_CALL_FUNCTION(&headersize, "curl_getinfo", curl, constant); PHALCON_INIT_VAR(headerstr); phalcon_substr(headerstr, content, 0 , Z_LVAL_P(headersize)); PHALCON_INIT_VAR(bodystr); phalcon_substr(bodystr, content, Z_LVAL_P(headersize) , Z_STRLEN_P(content)); PHALCON_CALL_METHOD(NULL, response, "setheader", headerstr); PHALCON_CALL_METHOD(NULL, response, "setbody", bodystr); } RETURN_CTOR(response); }
/** * Builds a SELECT statement * * @param array $definition * @return string */ PHP_METHOD(Phalcon_Db_Dialect, select){ zval *definition, *escape_char = NULL, *columns, *selected_columns; zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL; zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL; zval *column_alias_sql = NULL, *tables, *selected_tables; zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins; zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL; zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL; zval *join_conditions = NULL, *where_conditions; zval *where_expression, *group_items, *group_fields; zval *group_field = NULL, *group_expression = NULL, *group_sql; zval *group_clause, *having_conditions, *having_expression; zval *order_fields, *order_items, *order_item = NULL; zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL; zval *order_sql_item_type = NULL, *order_sql, *limit_value; zval *number, *offset; HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5; HashPosition hp0, hp1, hp2, hp3, hp4, hp5; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) { RETURN_MM_NULL(); } if (Z_TYPE_P(definition) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "Invalid SELECT definition"); return; } if (!phalcon_array_isset_string(definition, SS("tables"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'tables' is required in the definition array"); return; } if (!phalcon_array_isset_string(definition, SS("columns"))) { PHALCON_THROW_EXCEPTION_STR(phalcon_db_exception_ce, "The index 'columns' is required in the definition array"); return; } if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_OBS_VAR(escape_char); phalcon_read_property(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY_CC); } else { PHALCON_INIT_NVAR(escape_char); } PHALCON_OBS_VAR(columns); phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY_CC); if (Z_TYPE_P(columns) == IS_ARRAY) { PHALCON_INIT_VAR(selected_columns); array_init(selected_columns); if (!phalcon_is_iterable(columns, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(column); /** * Escape column name */ PHALCON_OBS_NVAR(column_item); phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY_CC); if (Z_TYPE_P(column_item) == IS_ARRAY) { PHALCON_INIT_NVAR(column_sql); PHALCON_CALL_METHOD_PARAMS_2(column_sql, this_ptr, "getsqlexpression", column_item, escape_char); } else { if (PHALCON_IS_STRING(column_item, "*")) { PHALCON_CPY_WRT(column_sql, column_item); } else { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char); } else { PHALCON_CPY_WRT(columns_sql, column_item); } } } /** * Escape column domain */ PHALCON_OBS_NVAR(column_domain); phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY_CC); if (zend_is_true(column_domain)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql); } else { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql); } } else { PHALCON_CPY_WRT(column_domain_sql, column_sql); } /** * Escape column alias */ if (phalcon_array_isset_long(column, 2)) { PHALCON_OBS_NVAR(column_alias); phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY_CC); if (zend_is_true(column_alias)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " AS ", escape_char, column_alias, escape_char); } else { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " AS ", column_alias); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(columns_sql); phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC); } else { PHALCON_CPY_WRT(columns_sql, columns); } /** * Check and escape tables */ PHALCON_OBS_VAR(tables); phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY_CC); if (Z_TYPE_P(tables) == IS_ARRAY) { PHALCON_INIT_VAR(selected_tables); array_init(selected_tables); if (!phalcon_is_iterable(tables, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(table); PHALCON_INIT_NVAR(sql_table); PHALCON_CALL_METHOD_PARAMS_2(sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } PHALCON_INIT_VAR(tables_sql); phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC); } else { PHALCON_CPY_WRT(tables_sql, tables); } PHALCON_INIT_VAR(sql); PHALCON_CONCAT_SVSV(sql, "SELECT ", columns_sql, " FROM ", tables_sql); /** * Check for joins */ if (phalcon_array_isset_string(definition, SS("joins"))) { PHALCON_OBS_VAR(joins); phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY_CC); if (!phalcon_is_iterable(joins, &ah2, &hp2, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(join); PHALCON_OBS_NVAR(type); phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY_CC); PHALCON_OBS_NVAR(table); phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY_CC); PHALCON_INIT_NVAR(sql_table); PHALCON_CALL_METHOD_PARAMS_2(sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(sql_join); PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table); /** * Check if the join has conditions */ if (phalcon_array_isset_string(join, SS("conditions"))) { PHALCON_OBS_NVAR(join_conditions_array); phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY_CC); if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) { PHALCON_INIT_NVAR(join_expressions); array_init(join_expressions); if (!phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(join_condition); PHALCON_INIT_NVAR(join_expression); PHALCON_CALL_METHOD_PARAMS_2(join_expression, this_ptr, "getsqlexpression", join_condition, escape_char); phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah3, &hp3); } PHALCON_INIT_NVAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC); PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions); } } phalcon_concat_self(&sql, sql_join TSRMLS_CC); zend_hash_move_forward_ex(ah2, &hp2); } } /** * Check for a WHERE clause */ if (phalcon_array_isset_string(definition, SS("where"))) { PHALCON_OBS_VAR(where_conditions); phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY_CC); PHALCON_INIT_VAR(where_expression); PHALCON_CALL_METHOD_PARAMS_2(where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression); } /** * Check for a GROUP clause */ if (phalcon_array_isset_string(definition, SS("group"))) { PHALCON_INIT_VAR(group_items); array_init(group_items); PHALCON_OBS_VAR(group_fields); phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY_CC); if (!phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(group_field); PHALCON_INIT_NVAR(group_expression); PHALCON_CALL_METHOD_PARAMS_2(group_expression, this_ptr, "getsqlexpression", group_field, escape_char); phalcon_array_append(&group_items, group_expression, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah4, &hp4); } PHALCON_INIT_VAR(group_sql); phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC); PHALCON_INIT_VAR(group_clause); PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql); phalcon_concat_self(&sql, group_clause TSRMLS_CC); /** * Check for a HAVING clause */ if (phalcon_array_isset_string(definition, SS("having"))) { PHALCON_OBS_VAR(having_conditions); phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY_CC); PHALCON_INIT_VAR(having_expression); PHALCON_CALL_METHOD_PARAMS_2(having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression); } } /** * Check for a ORDER clause */ if (phalcon_array_isset_string(definition, SS("order"))) { PHALCON_OBS_VAR(order_fields); phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY_CC); PHALCON_INIT_VAR(order_items); array_init(order_items); if (!phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(order_item); PHALCON_OBS_NVAR(order_expression); phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY_CC); PHALCON_INIT_NVAR(order_sql_item); PHALCON_CALL_METHOD_PARAMS_2(order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char); /** * In the numeric 1 position could be a ASC/DESC clause */ if (phalcon_array_isset_long(order_item, 1)) { PHALCON_OBS_NVAR(sql_order_type); phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY_CC); PHALCON_INIT_NVAR(order_sql_item_type); PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type); } else { PHALCON_CPY_WRT(order_sql_item_type, order_sql_item); } phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah5, &hp5); } PHALCON_INIT_VAR(order_sql); phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC); PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql); } /** * Check for a LIMIT condition */ if (phalcon_array_isset_string(definition, SS("limit"))) { PHALCON_OBS_VAR(limit_value); phalcon_array_fetch_string(&limit_value, definition, SL("limit"), PH_NOISY_CC); if (Z_TYPE_P(limit_value) == IS_ARRAY) { PHALCON_OBS_VAR(number); phalcon_array_fetch_string(&number, limit_value, SL("number"), PH_NOISY_CC); /** * Check for a OFFSET condition */ if (phalcon_array_isset_string(limit_value, SS("offset"))) { PHALCON_OBS_VAR(offset); phalcon_array_fetch_string(&offset, limit_value, SL("offset"), PH_NOISY_CC); PHALCON_SCONCAT_SVSV(sql, " LIMIT ", number, " OFFSET ", offset); } else { PHALCON_SCONCAT_SV(sql, " LIMIT ", number); } } else { PHALCON_SCONCAT_SV(sql, " LIMIT ", limit_value); } } RETURN_CTOR(sql); }
/** * 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_Oracle, describeColumns){ zval *table, *schema = NULL, *columns, *dialect, *sql, *fetch_num; zval *describe, *old_column = NULL, *field = NULL, *definition = NULL; zval *column_size = NULL, *column_precision = NULL, *column_scale = NULL; zval *column_type = 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_INIT_VAR(columns); array_init(columns); PHALCON_OBS_VAR(dialect); phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY_CC); 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); PHALCON_INIT_VAR(describe); phalcon_call_method_p2(describe, this_ptr, "fetchall", sql, fetch_num); /** * 0:column_name, 1:data_type, 2:data_length, 3:data_precision, 4:data_scale, * 5:nullable, 6:constraint_type, 7:default, 8:position; */ PHALCON_INIT_VAR(old_column); phalcon_is_iterable(describe, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(field); PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); PHALCON_OBS_NVAR(column_size); phalcon_array_fetch_long(&column_size, field, 2, PH_NOISY_CC); PHALCON_OBS_NVAR(column_precision); phalcon_array_fetch_long(&column_precision, field, 3, PH_NOISY_CC); PHALCON_OBS_NVAR(column_scale); phalcon_array_fetch_long(&column_scale, field, 4, PH_NOISY_CC); PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY_CC); /** * Check the column type to get the correct Phalcon type */ if (phalcon_memnstr_str(column_type, SL("NUMBER") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 3, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_precision, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("scale"), &column_scale, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("INTEGER") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 0, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_precision, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("VARCHAR2") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("FLOAT") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 7, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("scale"), &column_scale, PH_COPY | PH_SEPARATE TSRMLS_CC); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("TIMESTAMP") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 1, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("RAW") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("BLOB") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("CLOB") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("VARCHAR2") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 2, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { if (phalcon_memnstr_str(column_type, SL("CHAR") TSRMLS_CC)) { phalcon_array_update_string_long(&definition, SL("type"), 5, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&definition, SL("size"), &column_size, PH_COPY | PH_SEPARATE TSRMLS_CC); } else { phalcon_array_update_string_long(&definition, SL("type"), 6, PH_SEPARATE TSRMLS_CC); } } } } } } } } } } if (Z_TYPE_P(old_column) == IS_NULL) { phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_SEPARATE TSRMLS_CC); } else { phalcon_array_update_string(&definition, SL("after"), &old_column, PH_COPY | PH_SEPARATE TSRMLS_CC); } /** * Check if the field is primary key */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY_CC); if (PHALCON_IS_STRING(attribute, "P")) { phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_SEPARATE TSRMLS_CC); } /** * Check if the column allows null values */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY_CC); if (PHALCON_IS_STRING(attribute, "N")) { phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_SEPARATE TSRMLS_CC); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY_CC); /** * Create a Phalcon\Db\Column to abstract the 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 TSRMLS_CC); PHALCON_CPY_WRT(old_column, column_name); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(columns); }
/** * Checks if a role has access to a resource * * @param string $roleName * @param string $resourceName * @param string $access * @param string $action */ PHP_METHOD(Phalcon_Acl_Adapter_Memory, _allowOrDeny){ zval *role_name, *resource_name, *access, *action; zval *roles_names, *exception_message = NULL, *resources_names; zval *default_access, *access_list, *access_name = NULL; zval *empty_arr = NULL, *_access = NULL; zval *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *r4 = NULL, *r5 = NULL, *r6 = NULL; zval *r7 = NULL; zval *t0 = NULL, *t1 = NULL, *t2 = NULL, *t3 = NULL, *t4 = NULL, *t5 = NULL, *t6 = NULL; zval *t7 = NULL, *t8 = NULL, *t9 = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzz", &role_name, &resource_name, &access, &action) == FAILURE) { RETURN_MM_NULL(); } PHALCON_OBS_VAR(roles_names); phalcon_read_property(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC); if (!phalcon_array_isset(roles_names, role_name)) { PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Role \"", role_name, "\" does not exist in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } PHALCON_OBS_VAR(resources_names); phalcon_read_property(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC); if (!phalcon_array_isset(resources_names, resource_name)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Resource \"", resource_name, "\" does not exist in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } PHALCON_OBS_VAR(default_access); phalcon_read_property(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC); if (Z_TYPE_P(access) == IS_ARRAY) { PHALCON_OBS_VAR(access_list); phalcon_read_property(&access_list, this_ptr, SL("_accessList"), PH_NOISY_CC); if (!phalcon_is_iterable(access, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(access_name); PHALCON_OBS_NVAR(r0); phalcon_array_fetch(&r0, access_list, resource_name, PH_NOISY_CC); if (!phalcon_array_isset(r0, access_name)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access_name, "' does not exist in resource '", resource_name, "' in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } zend_hash_move_forward_ex(ah0, &hp0); } if (!phalcon_is_iterable(access, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(access_name); PHALCON_OBS_NVAR(t0); phalcon_read_property(&t0, this_ptr, SL("_access"), PH_NOISY_CC); PHALCON_OBS_NVAR(r1); phalcon_array_fetch(&r1, t0, role_name, PH_NOISY_CC); if (!phalcon_array_isset(r1, resource_name)) { PHALCON_INIT_NVAR(empty_arr); array_init(empty_arr); PHALCON_OBS_NVAR(_access); phalcon_read_property(&_access, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_multi_2(&_access, role_name, resource_name, &empty_arr, 0 TSRMLS_CC); phalcon_update_property_zval(this_ptr, SL("_access"), _access TSRMLS_CC); } PHALCON_OBS_NVAR(t1); phalcon_read_property(&t1, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_zval_zval_zval_multi_3(&t1, role_name, resource_name, access_name, &action, 0 TSRMLS_CC); PHALCON_OBS_NVAR(t2); phalcon_read_property(&t2, this_ptr, SL("_access"), PH_NOISY_CC); PHALCON_OBS_NVAR(r2); phalcon_array_fetch(&r2, t2, role_name, PH_NOISY_CC); PHALCON_OBS_NVAR(r3); phalcon_array_fetch(&r3, r2, resource_name, PH_NOISY_CC); if (!phalcon_array_isset_string(r3, SS("*"))) { PHALCON_OBS_NVAR(t3); phalcon_read_property(&t3, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_string_zval_zval_multi_3(&t3, role_name, resource_name, SL("*"), &default_access, 0 TSRMLS_CC); } zend_hash_move_forward_ex(ah1, &hp1); } } else { if (!PHALCON_IS_STRING(access, "*")) { PHALCON_OBS_VAR(t4); phalcon_read_property(&t4, this_ptr, SL("_accessList"), PH_NOISY_CC); PHALCON_OBS_VAR(r4); phalcon_array_fetch(&r4, t4, resource_name, PH_NOISY_CC); if (!phalcon_array_isset(r4, access)) { PHALCON_INIT_NVAR(exception_message); PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access, "' does not exist in resource '", resource_name, "' in ACL"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message); return; } } PHALCON_OBS_VAR(t5); phalcon_read_property(&t5, this_ptr, SL("_access"), PH_NOISY_CC); PHALCON_OBS_VAR(r5); phalcon_array_fetch(&r5, t5, role_name, PH_NOISY_CC); if (!phalcon_array_isset(r5, resource_name)) { PHALCON_INIT_NVAR(empty_arr); array_init(empty_arr); PHALCON_OBS_VAR(t6); phalcon_read_property(&t6, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_multi_2(&t6, role_name, resource_name, &empty_arr, 0 TSRMLS_CC); phalcon_update_property_zval(this_ptr, SL("_access"), t6 TSRMLS_CC); } PHALCON_OBS_VAR(t7); phalcon_read_property(&t7, this_ptr, SL("_access"), PH_NOISY_CC); PHALCON_OBS_VAR(r6); phalcon_array_fetch(&r6, t7, role_name, PH_NOISY_CC); PHALCON_OBS_VAR(r7); phalcon_array_fetch(&r7, r6, resource_name, PH_NOISY_CC); if (!phalcon_array_isset_string(r7, SS("*"))) { PHALCON_OBS_VAR(t8); phalcon_read_property(&t8, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_string_zval_zval_multi_3(&t8, role_name, resource_name, SL("*"), &default_access, 0 TSRMLS_CC); } PHALCON_OBS_VAR(t9); phalcon_read_property(&t9, this_ptr, SL("_access"), PH_NOISY_CC); phalcon_array_update_zval_zval_zval_multi_3(&t9, role_name, resource_name, access, &action, 0 TSRMLS_CC); } PHALCON_CALL_METHOD_NORETURN(this_ptr, "_rebuildaccesslist"); PHALCON_MM_RESTORE(); }
/** * 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_Oracle, describeColumns) { zval *table, *schema = NULL, *columns, *dialect, *sql = NULL, *fetch_num; zval *describe = NULL, *old_column = NULL, *field = NULL, *definition = NULL; zval *column_size = NULL, *column_precision = NULL, *column_scale = NULL; zval *column_type = 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) { schema = PHALCON_GLOBAL(z_null); } PHALCON_INIT_VAR(columns); array_init(columns); PHALCON_OBS_VAR(dialect); phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(&sql, dialect, "describecolumns", table, schema); /** * We're using FETCH_NUM to fetch the columns */ PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, PDO_FETCH_NUM); PHALCON_CALL_METHOD(&describe, this_ptr, "fetchall", sql, fetch_num); /** * 0:column_name, 1:data_type, 2:data_length, 3:data_precision, 4:data_scale, * 5:nullable, 6:constraint_type, 7:default, 8:position; */ PHALCON_INIT_VAR(old_column); phalcon_is_iterable(describe, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(field); PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); PHALCON_OBS_NVAR(column_size); phalcon_array_fetch_long(&column_size, field, 2, PH_NOISY); PHALCON_OBS_NVAR(column_precision); phalcon_array_fetch_long(&column_precision, field, 3, PH_NOISY); PHALCON_OBS_NVAR(column_scale); phalcon_array_fetch_long(&column_scale, field, 4, PH_NOISY); PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY); /** * Check the column type to get the correct Phalcon type */ while (1) { /** * Integer */ if (phalcon_memnstr_str(column_type, SL("NUMBER"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DECIMAL, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); phalcon_array_update_string(&definition, SL("size"), column_precision, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY); if (phalcon_is_numeric(column_precision)) { phalcon_array_update_string_long(&definition, SL("bytes"), Z_LVAL_P(column_precision) * 8, PH_COPY); } else { phalcon_array_update_string_long(&definition, SL("size"), 30, PH_COPY); phalcon_array_update_string_long(&definition, SL("bytes"), 80, PH_COPY); } if (phalcon_is_numeric(column_scale)) { phalcon_array_update_string(&definition, SL("scale"), column_scale, PH_COPY); } else { phalcon_array_update_string_long(&definition, SL("scale"), 6, PH_COPY); } break; } /** * Tinyint(1) is boolean */ if (phalcon_memnstr_str(column_type, SL("TINYINT(1)"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY); break; } /** * Smallint/Bigint/Integers/Int are int */ if (phalcon_memnstr_str(column_type, SL("INTEGER"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_INTEGER, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); phalcon_array_update_string(&definition, SL("size"), column_precision, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_COPY); phalcon_array_update_string_long(&definition, SL("bytes"), 32, PH_COPY); break; } /** * Float/Smallfloats/Decimals are float */ if (phalcon_memnstr_str(column_type, SL("FLOAT"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_FLOAT, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY); phalcon_array_update_string(&definition, SL("scale"), column_scale, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY); break; } /** * Date */ if (phalcon_memnstr_str(column_type, SL("TIMESTAMP"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATE, PH_COPY); break; } /** * Text */ if (phalcon_memnstr_str(column_type, SL("RAW"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY); break; } /** * Text */ if (phalcon_memnstr_str(column_type, SL("BLOB"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY); break; } /** * Text */ if (phalcon_memnstr_str(column_type, SL("CLOB"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY); break; } /** * Chars2 are string */ if (phalcon_memnstr_str(column_type, SL("VARCHAR2"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY); phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY); break; } /** * Chars are chars */ if (phalcon_memnstr_str(column_type, SL("CHAR"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY); phalcon_array_update_string(&definition, SL("size"), column_size, PH_COPY); break; } /** * Text are varchars */ if (phalcon_memnstr_str(column_type, SL("text"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY); break; } /** * By default is string */ phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY); break; } if (Z_TYPE_P(old_column) == IS_NULL) { phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_COPY); } else { phalcon_array_update_string(&definition, SL("after"), old_column, PH_COPY); } /** * Check if the field is primary key */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY); if (PHALCON_IS_STRING(attribute, "P")) { phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_COPY); } /** * Check if the column allows null values */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY); if (PHALCON_IS_STRING(attribute, "N")) { phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_COPY); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY); /** * If the column set the default values, get it */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY); if (!PHALCON_IS_EMPTY(attribute)) { phalcon_array_update_string(&definition, SL("default"), attribute, PH_COPY); } /** * Create a Phalcon\Db\Column to abstract the column */ PHALCON_INIT_NVAR(column); object_init_ex(column, phalcon_db_column_ce); PHALCON_CALL_METHOD(NULL, column, "__construct", column_name, definition); phalcon_array_append(&columns, column, PH_COPY); PHALCON_CPY_WRT(old_column, column_name); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(columns); }
/** * Check whether internal resource has rows to fetch * * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){ zval *type = NULL, *row = NULL, *underscore; zval *empty_str, *active_row = NULL; zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL; zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL; zval *column_value = NULL; zval *value = NULL, *sql_alias = NULL, *n_alias = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; int i_type, is_partial; PHALCON_MM_GROW(); type = phalcon_fetch_nproperty_this(this_ptr, SL("_type"), PH_NOISY TSRMLS_CC); i_type = (Z_TYPE_P(type) == IS_LONG) ? Z_LVAL_P(type) : phalcon_get_intval(type); is_partial = (i_type == PHALCON_MVC_MODEL_RESULTSET_TYPE_PARTIAL); type = NULL; PHALCON_INIT_VAR(row); if (is_partial) { /** * The result is bigger than 32 rows so it's retrieved one by one */ zval *result = phalcon_fetch_nproperty_this(this_ptr, SL("_result"), PH_NOISY TSRMLS_CC); if (PHALCON_IS_NOT_FALSE(result)) { PHALCON_CALL_METHOD(&row, result, "fetch", result); } else { ZVAL_FALSE(row); } } else { /** * The full rows are dumped in this_ptr->rows */ zval *rows = phalcon_fetch_nproperty_this(this_ptr, SL("_rows"), PH_NOISY TSRMLS_CC); if (Z_TYPE_P(rows) == IS_ARRAY) { phalcon_array_get_current(row, rows); if (Z_TYPE_P(row) == IS_OBJECT) { zend_hash_move_forward(Z_ARRVAL_P(rows)); } } else { ZVAL_FALSE(row); } } /** * Valid records are arrays */ if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) { /** * The result type=1 so we need to build every row */ if (is_partial) { /** * Get current hydration mode */ zval *hydrate_mode = phalcon_fetch_nproperty_this(this_ptr, SL("_hydrateMode"), PH_NOISY TSRMLS_CC); zval *columns_types = phalcon_fetch_nproperty_this(this_ptr, SL("_columnTypes"), PH_NOISY TSRMLS_CC); int i_hydrate_mode = phalcon_get_intval(hydrate_mode); PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(empty_str); ZVAL_EMPTY_STRING(empty_str); /** * Each row in a complex result is a Phalcon\Mvc\Model\Row instance */ PHALCON_INIT_VAR(active_row); switch (i_hydrate_mode) { case 0: object_init_ex(active_row, phalcon_mvc_model_row_ce); break; case 1: array_init(active_row); break; case 2: default: object_init(active_row); break; } /** * Create every record according to the column types */ /** * Set records as dirty state PERSISTENT by default */ PHALCON_INIT_VAR(dirty_state); ZVAL_LONG(dirty_state, 0); phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HKEY(alias, ah0, hp0); PHALCON_GET_HVALUE(column); PHALCON_OBS_NVAR(type); phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY); if (PHALCON_IS_STRING(type, "object")) { /** * Object columns are assigned column by column */ PHALCON_OBS_NVAR(source); phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY); PHALCON_OBS_NVAR(attributes); phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY); PHALCON_OBS_NVAR(column_map); phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY); /** * Assign the values from the _source_attribute notation to its real column name */ PHALCON_INIT_NVAR(row_model); array_init(row_model); phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(attribute); /** * Columns are supposed to be in the form _table_field */ PHALCON_INIT_NVAR(column_alias); PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute); PHALCON_OBS_NVAR(column_value); phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY); phalcon_array_update_zval(&row_model, attribute, column_value, PH_COPY | PH_SEPARATE); zend_hash_move_forward_ex(ah1, &hp1); } /** * Generate the column value according to the hydration type */ switch (phalcon_get_intval(hydrate_mode)) { case 0: { zval *keep_snapshots, *instance; /** * Check if the resultset must keep snapshots */ if (!phalcon_array_isset_string_fetch(&keep_snapshots, column, SS("keepSnapshots"))) { keep_snapshots = PHALCON_GLOBAL(z_false); } /** * Get the base instance */ if (!phalcon_array_isset_string_fetch(&instance, column, SS("instance"))) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Undefined index: instance"); instance = PHALCON_GLOBAL(z_null); } /** * Assign the values to the attributes using a column map */ PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots); break; } default: /** * Other kinds of hydrations */ PHALCON_CALL_CE_STATIC(&value, phalcon_mvc_model_ce, "cloneresultmaphydrate", row_model, column_map, hydrate_mode); break; } /** * The complete object is assigned to an attribute with the name of the alias or * the model name */ PHALCON_OBS_NVAR(attribute); if (phalcon_array_isset_string(column, SS("balias"))) { phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY); } } else { /** * Scalar columns are simply assigned to the result object */ if (phalcon_array_isset_string(column, SS("sqlAlias"))) { PHALCON_OBS_NVAR(sql_alias); phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY); PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, row, sql_alias, PH_NOISY); } else { PHALCON_OBS_NVAR(value); if (phalcon_array_isset(row, alias)) { phalcon_array_fetch(&value, row, alias, PH_NOISY); } } /** * If a 'balias' is defined is not an unnamed scalar */ if (phalcon_array_isset_string(column, SS("balias"))) { PHALCON_CPY_WRT(attribute, alias); } else { PHALCON_INIT_NVAR(n_alias); phalcon_fast_str_replace(n_alias, underscore, empty_str, alias); PHALCON_CPY_WRT(attribute, n_alias); } assert(attribute != NULL); } /** * Assign the instance according to the hydration type */ if (unlikely(!attribute)) { zend_throw_exception_ex(phalcon_mvc_model_exception_ce, 0 TSRMLS_CC, "Unexpected inconsistency: attribute is NULL"); RETURN_MM(); } switch (phalcon_get_intval(hydrate_mode)) { case 1: phalcon_array_update_zval(&active_row, attribute, value, PH_COPY | PH_SEPARATE); break; default: phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC); break; } zend_hash_move_forward_ex(ah0, &hp0); } /** * Store the generated row in this_ptr->activeRow to be retrieved by 'current' */ phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC); } else { /** * The row is already built so we just assign it to the activeRow */ phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC); } RETURN_MM_TRUE; } /** * There are no results to retrieve so we update this_ptr->activeRow as false */ phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC); RETURN_MM_FALSE; }
/** * Check whether internal resource has rows to fetch * * @return boolean */ PHP_METHOD(Phalcon_Mvc_Model_Resultset_Complex, valid){ zval *type = NULL, *result, *row = NULL, *rows, *hydrate_mode, *underscore; zval *empty_str, *active_row = NULL, *columns_types; zval *dirty_state, *column = NULL, *alias = NULL, *source = NULL, *attributes = NULL; zval *column_map = NULL, *row_model = NULL, *attribute = NULL, *column_alias = NULL; zval *column_value = NULL, *keep_snapshots = NULL, *instance = NULL; zval *value = NULL, *sql_alias = NULL, *n_alias = NULL; HashTable *ah0, *ah1; HashPosition hp0, hp1; zval **hd; PHALCON_MM_GROW(); PHALCON_OBS_VAR(type); phalcon_read_property_this(&type, this_ptr, SL("_type"), PH_NOISY_CC); if (zend_is_true(type)) { /** * The result is bigger than 32 rows so it's retrieved one by one */ PHALCON_OBS_VAR(result); phalcon_read_property_this(&result, this_ptr, SL("_result"), PH_NOISY_CC); if (PHALCON_IS_NOT_FALSE(result)) { PHALCON_INIT_VAR(row); PHALCON_CALL_METHOD_PARAMS_1(row, result, "fetch", result); } else { PHALCON_INIT_NVAR(row); ZVAL_BOOL(row, 0); } } else { /** * The full rows are dumped in this_ptr->rows */ PHALCON_OBS_VAR(rows); phalcon_read_property_this(&rows, this_ptr, SL("_rows"), PH_NOISY_CC); if (Z_TYPE_P(rows) == IS_ARRAY) { PHALCON_INIT_NVAR(row); phalcon_array_get_current(row, rows TSRMLS_CC); if (Z_TYPE_P(row) == IS_OBJECT) { phalcon_array_next(rows); } } else { PHALCON_INIT_NVAR(row); ZVAL_BOOL(row, 0); } } /** * Valid records are arrays */ if (Z_TYPE_P(row) == IS_ARRAY || Z_TYPE_P(row) == IS_OBJECT) { /** * The result type=1 so we need to build every row */ if (zend_is_true(type)) { /** * Get current hydration mode */ PHALCON_OBS_VAR(hydrate_mode); phalcon_read_property_this(&hydrate_mode, this_ptr, SL("_hydrateMode"), PH_NOISY_CC); PHALCON_INIT_VAR(underscore); ZVAL_STRING(underscore, "_", 1); PHALCON_INIT_VAR(empty_str); ZVAL_STRING(empty_str, "", 1); /** * Each row in a complex result is a Phalcon\Mvc\Model\Row instance */ switch (phalcon_get_intval(hydrate_mode)) { case 0: PHALCON_INIT_VAR(active_row); object_init_ex(active_row, phalcon_mvc_model_row_ce); break; case 1: PHALCON_INIT_NVAR(active_row); array_init(active_row); break; case 2: PHALCON_INIT_NVAR(active_row); object_init(active_row); break; } /** * Create every record according to the column types */ PHALCON_OBS_VAR(columns_types); phalcon_read_property_this(&columns_types, this_ptr, SL("_columnTypes"), PH_NOISY_CC); /** * Set records as dirty state PERSISTENT by default */ PHALCON_INIT_VAR(dirty_state); ZVAL_LONG(dirty_state, 0); if (!phalcon_is_iterable(columns_types, &ah0, &hp0, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_FOREACH_KEY(alias, ah0, hp0); PHALCON_GET_FOREACH_VALUE(column); PHALCON_OBS_NVAR(type); phalcon_array_fetch_string(&type, column, SL("type"), PH_NOISY_CC); if (PHALCON_IS_STRING(type, "object")) { /** * Object columns are assigned column by column */ PHALCON_OBS_NVAR(source); phalcon_array_fetch_string(&source, column, SL("column"), PH_NOISY_CC); PHALCON_OBS_NVAR(attributes); phalcon_array_fetch_string(&attributes, column, SL("attributes"), PH_NOISY_CC); PHALCON_OBS_NVAR(column_map); phalcon_array_fetch_string(&column_map, column, SL("columnMap"), PH_NOISY_CC); /** * Assign the values from the _source_attribute notation to its real column name */ PHALCON_INIT_NVAR(row_model); array_init(row_model); if (!phalcon_is_iterable(attributes, &ah1, &hp1, 0, 0 TSRMLS_CC)) { return; } while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_FOREACH_VALUE(attribute); /** * Columns are supposed to be in the form _table_field */ PHALCON_INIT_NVAR(column_alias); PHALCON_CONCAT_VVVV(column_alias, underscore, source, underscore, attribute); PHALCON_OBS_NVAR(column_value); phalcon_array_fetch(&column_value, row, column_alias, PH_NOISY_CC); phalcon_array_update_zval(&row_model, attribute, &column_value, PH_COPY | PH_SEPARATE TSRMLS_CC); zend_hash_move_forward_ex(ah1, &hp1); } /** * Generate the column value according to the hydration type */ switch (phalcon_get_intval(hydrate_mode)) { case 0: /** * Check if the resultset must keep snapshots */ if (phalcon_array_isset_string(column, SS("keepSnapshots"))) { PHALCON_OBS_NVAR(keep_snapshots); phalcon_array_fetch_string(&keep_snapshots, column, SL("keepSnapshots"), PH_NOISY_CC); } else { PHALCON_INIT_NVAR(keep_snapshots); ZVAL_BOOL(keep_snapshots, 0); } /** * Get the base instance */ PHALCON_OBS_NVAR(instance); phalcon_array_fetch_string(&instance, column, SL("instance"), PH_NOISY_CC); /** * Assign the values to the attributes using a column map */ PHALCON_INIT_NVAR(value); PHALCON_CALL_STATIC_PARAMS_5(value, "phalcon\\mvc\\model", "cloneresultmap", instance, row_model, column_map, dirty_state, keep_snapshots); break; default: /** * Other kinds of hydrations */ PHALCON_INIT_NVAR(value); PHALCON_CALL_STATIC_PARAMS_3(value, "phalcon\\mvc\\model", "cloneresultmaphydrate", row_model, column_map, hydrate_mode); break; } /** * The complete object is assigned to an attribute with the name of the alias or * the model name */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_string(&attribute, column, SL("balias"), PH_NOISY_CC); } else { /** * Scalar columns are simply assigned to the result object */ if (phalcon_array_isset_string(column, SS("sqlAlias"))) { PHALCON_OBS_NVAR(sql_alias); phalcon_array_fetch_string(&sql_alias, column, SL("sqlAlias"), PH_NOISY_CC); PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, row, sql_alias, PH_NOISY_CC); } else { PHALCON_OBS_NVAR(value); phalcon_array_fetch(&value, row, alias, PH_NOISY_CC); } /** * If a 'balias' is defined is not an unnamed scalar */ if (phalcon_array_isset_string(column, SS("balias"))) { PHALCON_CPY_WRT(attribute, alias); } else { PHALCON_INIT_NVAR(n_alias); phalcon_fast_str_replace(n_alias, underscore, empty_str, alias TSRMLS_CC); PHALCON_CPY_WRT(attribute, n_alias); } } /** * Assign the instance according to the hydration type */ switch (phalcon_get_intval(hydrate_mode)) { case 1: phalcon_array_update_zval(&active_row, attribute, &value, PH_COPY | PH_SEPARATE TSRMLS_CC); break; default: phalcon_update_property_zval_zval(active_row, attribute, value TSRMLS_CC); break; } zend_hash_move_forward_ex(ah0, &hp0); } /** * Store the generated row in this_ptr->activeRow to be retrieved by 'current' */ phalcon_update_property_this(this_ptr, SL("_activeRow"), active_row TSRMLS_CC); } else { /** * The row is already built so we just assign it to the activeRow */ phalcon_update_property_this(this_ptr, SL("_activeRow"), row TSRMLS_CC); } RETURN_MM_TRUE; } /** * There are no results to retrieve so we update this_ptr->activeRow as false */ phalcon_update_property_bool(this_ptr, SL("_activeRow"), 0 TSRMLS_CC); RETURN_MM_FALSE; }
/** * 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); }
/** * 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_Postgresql, describeColumns){ zval *table, *schema = NULL, *columns, *dialect, *sql = NULL, *fetch_num; zval *describe = NULL, *old_column = NULL, *field = NULL, *definition = NULL; zval *char_size = NULL, *numeric_size = NULL, *numeric_scale = NULL, *column_type = NULL; zval *attribute = NULL, *column_name = NULL, *column = NULL; HashTable *ah0; HashPosition hp0; zval **hd; PHALCON_MM_GROW(); phalcon_fetch_params(1, 1, 1, &table, &schema); if (!schema || !zend_is_true(schema)) { schema = phalcon_fetch_nproperty_this(this_ptr, SL("_schema"), PH_NOISY TSRMLS_CC); } PHALCON_INIT_VAR(columns); array_init(columns); PHALCON_OBS_VAR(dialect); phalcon_read_property_this(&dialect, this_ptr, SL("_dialect"), PH_NOISY TSRMLS_CC); PHALCON_CALL_METHOD(&sql, dialect, "describecolumns", table, schema); /** * We're using FETCH_NUM to fetch the columns */ PHALCON_INIT_VAR(fetch_num); ZVAL_LONG(fetch_num, PDO_FETCH_NUM); PHALCON_CALL_METHOD(&describe, this_ptr, "fetchall", sql, fetch_num); /** * 0:name, 1:type, 2:size, 3:numeric size, 4:numeric scale, 5: null, 6: key, 7: extra, 8: position, 9: element type */ PHALCON_INIT_VAR(old_column); phalcon_is_iterable(describe, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(field); PHALCON_INIT_NVAR(definition); array_init_size(definition, 1); add_assoc_long_ex(definition, SS("bindType"), 2); PHALCON_OBS_NVAR(char_size); phalcon_array_fetch_long(&char_size, field, 2, PH_NOISY); if (Z_TYPE_P(char_size) != IS_NULL) { convert_to_long(char_size); } PHALCON_OBS_NVAR(numeric_size); phalcon_array_fetch_long(&numeric_size, field, 3, PH_NOISY); if (phalcon_is_numeric(numeric_size)) { convert_to_long(numeric_size); } PHALCON_OBS_NVAR(numeric_scale); phalcon_array_fetch_long(&numeric_scale, field, 4, PH_NOISY); if (phalcon_is_numeric(numeric_scale)) { convert_to_long(numeric_scale); } PHALCON_OBS_NVAR(column_type); phalcon_array_fetch_long(&column_type, field, 1, PH_NOISY); /** * Check the column type to get the correct Phalcon type */ while (1) { /** * Tinyint(1) is boolean */ if (phalcon_memnstr_str(column_type, SL("smallint(1)"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY); break; } /** * Smallint/Bigint/Integers/Int are int */ if (phalcon_memnstr_str(column_type, SL("int"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_INTEGER, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 1, PH_COPY); break; } /** * Varchar */ if (phalcon_memnstr_str(column_type, SL("varying"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_VARCHAR, PH_COPY); phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY); break; } /** * Special type for datetime */ if (phalcon_memnstr_str(column_type, SL("date"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATE, PH_COPY); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY); break; } /** * Numeric */ if (phalcon_memnstr_str(column_type, SL("numeric"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DECIMAL, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); if (phalcon_is_numeric(numeric_size)) { phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY); phalcon_array_update_string_long(&definition, SL("bytes"), Z_LVAL_P(numeric_size) * 8, PH_COPY); } else { phalcon_array_update_string_long(&definition, SL("size"), 30, PH_COPY); phalcon_array_update_string_long(&definition, SL("bytes"), 80, PH_COPY); } if (phalcon_is_numeric(numeric_scale)) { phalcon_array_update_string(&definition, SL("scale"), numeric_scale, PH_COPY); } else { phalcon_array_update_string_long(&definition, SL("scale"), 6, PH_COPY); } phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY); break; } /** * Chars are chars */ if (phalcon_memnstr_str(column_type, SL("char"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY); phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY); break; } /** * Date */ if (phalcon_memnstr_str(column_type, SL("timestamp"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_DATETIME, PH_COPY); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY); break; } /** * Text are varchars */ if (phalcon_memnstr_str(column_type, SL("text"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_TEXT, PH_COPY); phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY); break; } /** * Float/Smallfloats/Decimals are float */ if (phalcon_memnstr_str(column_type, SL("float"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_FLOAT, PH_COPY); phalcon_array_update_string_bool(&definition, SL("isNumeric"), 1, PH_COPY); phalcon_array_update_string(&definition, SL("size"), numeric_size, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 32, PH_COPY); break; } /** * Boolean */ if (phalcon_memnstr_str(column_type, SL("bool"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_BOOLEAN, PH_COPY); phalcon_array_update_string_long(&definition, SL("size"), 0, PH_COPY); phalcon_array_update_string_long(&definition, SL("bindType"), 5, PH_COPY); break; } /** * UUID */ if (phalcon_memnstr_str(column_type, SL("uuid"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_CHAR, PH_COPY); phalcon_array_update_string_long(&definition, SL("size"), 36, PH_COPY); break; } /** * JSON */ if (phalcon_memnstr_str(column_type, SL("json"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_JSON, PH_COPY); phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY); break; } /** * ARRAY */ if (phalcon_memnstr_str(column_type, SL("ARRAY"))) { phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_ARRAY, PH_COPY); phalcon_array_update_string(&definition, SL("size"), char_size, PH_COPY); break; } /** * By default is string */ phalcon_array_update_string_long(&definition, SL("type"), PHALCON_DB_COLUMN_TYPE_OTHER, PH_COPY); break; } if (phalcon_memnstr_str(column_type, SL("unsigned"))) { phalcon_array_update_string_bool(&definition, SL("unsigned"), 1, PH_COPY); } if (Z_TYPE_P(old_column) == IS_NULL) { phalcon_array_update_string_bool(&definition, SL("first"), 1, PH_COPY); } else { phalcon_array_update_string(&definition, SL("after"), old_column, PH_COPY); } /** * Check if the field is primary key */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 6, PH_NOISY); if (PHALCON_IS_STRING(attribute, "PRI")) { phalcon_array_update_string_bool(&definition, SL("primary"), 1, PH_COPY); } /** * Check if the column allows null values */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 5, PH_NOISY); if (PHALCON_IS_STRING(attribute, "NO")) { phalcon_array_update_string_bool(&definition, SL("notNull"), 1, PH_COPY); } /** * Check if the column is auto increment */ PHALCON_OBS_NVAR(attribute); phalcon_array_fetch_long(&attribute, field, 7, PH_NOISY); if (PHALCON_IS_STRING(attribute, "auto_increment")) { phalcon_array_update_string_bool(&definition, SL("autoIncrement"), 1, PH_COPY); } else if (!PHALCON_IS_EMPTY(attribute)) { phalcon_array_update_string(&definition, SL("default"), attribute, PH_COPY); } PHALCON_OBS_NVAR(column_name); phalcon_array_fetch_long(&column_name, field, 0, PH_NOISY); /** * Create a Phalcon\Db\Column to abstract the column */ PHALCON_INIT_NVAR(column); object_init_ex(column, phalcon_db_column_ce); PHALCON_CALL_METHOD(NULL, column, "__construct", column_name, definition); phalcon_array_append(&columns, column, PH_COPY); PHALCON_CPY_WRT(old_column, column_name); zend_hash_move_forward_ex(ah0, &hp0); } RETURN_CTOR(columns); }
/** * Builds a SELECT statement * * @param array $definition * @return string */ PHP_METHOD(Phalcon_Db_Dialect_Oracle, select){ zval *definition, *escape_char = NULL, *columns, *selected_columns, *distinct; zval *column = NULL, *column_item = NULL, *column_sql = NULL, *columns_sql = NULL; zval *column_domain = NULL, *column_domain_sql = NULL, *column_alias = NULL; zval *column_alias_sql = NULL, *tables, *selected_tables; zval *table = NULL, *sql_table = NULL, *tables_sql = NULL, *sql, *joins; zval *join = NULL, *type = NULL, *sql_join = NULL, *join_conditions_array = NULL; zval *join_expressions = NULL, *join_condition = NULL, *join_expression = NULL; zval *join_conditions = NULL, *where_conditions; zval *where_expression = NULL, *group_items, *group_fields; zval *group_field = NULL, *group_expression = NULL, *group_sql; zval *group_clause, *having_conditions, *having_expression = NULL; zval *order_fields, *order_items, *order_item = NULL; zval *order_expression = NULL, *order_sql_item = NULL, *sql_order_type = NULL; zval *order_sql_item_type = NULL, *order_sql, *limit_value; zval *number, *offset, *tmp1 = NULL, *tmp2 = NULL; zval *z_one, *ini_range, *end_range = NULL, *sql_limit; HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5; HashPosition hp0, hp1, hp2, hp3, hp4, hp5; zval **hd; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &definition) == FAILURE) { RETURN_NULL(); } if (Z_TYPE_P(definition) != IS_ARRAY) { PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "Invalid SELECT definition"); return; } if (!phalcon_array_isset_string(definition, SS("tables"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'tables' is required in the definition array"); return; } if (!phalcon_array_isset_string(definition, SS("columns"))) { PHALCON_THROW_EXCEPTION_STRW(phalcon_db_exception_ce, "The index 'columns' is required in the definition array"); return; } PHALCON_MM_GROW(); if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_OBS_VAR(escape_char); phalcon_read_property_this(&escape_char, this_ptr, SL("_escapeChar"), PH_NOISY TSRMLS_CC); } else { PHALCON_INIT_NVAR(escape_char); } PHALCON_OBS_VAR(columns); phalcon_array_fetch_string(&columns, definition, SL("columns"), PH_NOISY); if (Z_TYPE_P(columns) == IS_ARRAY) { PHALCON_INIT_VAR(selected_columns); array_init(selected_columns); phalcon_is_iterable(columns, &ah0, &hp0, 0, 0); while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) { PHALCON_GET_HVALUE(column); /** * Escape column name */ PHALCON_OBS_NVAR(column_item); phalcon_array_fetch_long(&column_item, column, 0, PH_NOISY); if (Z_TYPE_P(column_item) == IS_ARRAY) { PHALCON_CALL_METHOD(&column_sql, this_ptr, "getsqlexpression", column_item, escape_char); } else if (PHALCON_IS_STRING(column_item, "*")) { PHALCON_CPY_WRT(column_sql, column_item); } else if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_sql); PHALCON_CONCAT_VVV(column_sql, escape_char, column_item, escape_char); } else { PHALCON_CPY_WRT(columns_sql, column_item); } /** * Escape column domain */ if (phalcon_array_isset_long(column, 1)) { PHALCON_OBS_NVAR(column_domain); phalcon_array_fetch_long(&column_domain, column, 1, PH_NOISY); if (zend_is_true(column_domain)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VVVSV(column_domain_sql, escape_char, column_domain, escape_char, ".", column_sql); } else { PHALCON_INIT_NVAR(column_domain_sql); PHALCON_CONCAT_VSV(column_domain_sql, column_domain, ".", column_sql); } } else { PHALCON_CPY_WRT(column_domain_sql, column_sql); } } else { PHALCON_CPY_WRT(column_domain_sql, column_sql); } /** * Escape column alias */ if (phalcon_array_isset_long(column, 2)) { PHALCON_OBS_NVAR(column_alias); phalcon_array_fetch_long(&column_alias, column, 2, PH_NOISY); if (zend_is_true(column_alias)) { if (PHALCON_GLOBAL(db).escape_identifiers) { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSVVV(column_alias_sql, column_domain_sql, " ", escape_char, column_alias, escape_char); } else { PHALCON_INIT_NVAR(column_alias_sql); PHALCON_CONCAT_VSV(column_alias_sql, column_domain_sql, " ", column_alias); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } } else { PHALCON_CPY_WRT(column_alias_sql, column_domain_sql); } phalcon_array_append(&selected_columns, column_alias_sql, PH_SEPARATE); zend_hash_move_forward_ex(ah0, &hp0); } PHALCON_INIT_VAR(columns_sql); phalcon_fast_join_str(columns_sql, SL(", "), selected_columns TSRMLS_CC); } else { PHALCON_CPY_WRT(columns_sql, columns); } /** * Check and escape tables */ PHALCON_OBS_VAR(tables); phalcon_array_fetch_string(&tables, definition, SL("tables"), PH_NOISY); if (Z_TYPE_P(tables) == IS_ARRAY) { PHALCON_INIT_VAR(selected_tables); array_init(selected_tables); phalcon_is_iterable(tables, &ah1, &hp1, 0, 0); while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) { PHALCON_GET_HVALUE(table); PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE); zend_hash_move_forward_ex(ah1, &hp1); } PHALCON_INIT_VAR(tables_sql); phalcon_fast_join_str(tables_sql, SL(", "), selected_tables TSRMLS_CC); } else { PHALCON_CPY_WRT(tables_sql, tables); } PHALCON_INIT_VAR(sql); if (phalcon_array_isset_string_fetch(&distinct, definition, SS("definition"))) { assert(Z_TYPE_P(distinct) == IS_LONG); if (Z_LVAL_P(distinct) == 0) { ZVAL_STRING(sql, "SELECT ALL ", 1); } else if (Z_LVAL_P(distinct) == 1) { ZVAL_STRING(sql, "SELECT DISTINCT ", 1); } else { ZVAL_STRING(sql, "SELECT ", 1); } } else { ZVAL_STRING(sql, "SELECT ", 1); } /** * Check for joins */ if (phalcon_array_isset_string(definition, SS("joins"))) { PHALCON_OBS_VAR(joins); phalcon_array_fetch_string(&joins, definition, SL("joins"), PH_NOISY); phalcon_is_iterable(joins, &ah2, &hp2, 0, 0); while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) { PHALCON_GET_HVALUE(join); PHALCON_OBS_NVAR(type); phalcon_array_fetch_string(&type, join, SL("type"), PH_NOISY); PHALCON_OBS_NVAR(table); phalcon_array_fetch_string(&table, join, SL("source"), PH_NOISY); PHALCON_CALL_METHOD(&sql_table, this_ptr, "getsqltable", table, escape_char); phalcon_array_append(&selected_tables, sql_table, PH_SEPARATE); PHALCON_INIT_NVAR(sql_join); PHALCON_CONCAT_SVSV(sql_join, " ", type, " JOIN ", sql_table); /** * Check if the join has conditions */ if (phalcon_array_isset_string(join, SS("conditions"))) { PHALCON_OBS_NVAR(join_conditions_array); phalcon_array_fetch_string(&join_conditions_array, join, SL("conditions"), PH_NOISY); if (phalcon_fast_count_ev(join_conditions_array TSRMLS_CC)) { PHALCON_INIT_NVAR(join_expressions); array_init(join_expressions); phalcon_is_iterable(join_conditions_array, &ah3, &hp3, 0, 0); while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) { PHALCON_GET_HVALUE(join_condition); PHALCON_CALL_METHOD(&join_expression, this_ptr, "getsqlexpression", join_condition, escape_char); phalcon_array_append(&join_expressions, join_expression, PH_SEPARATE); zend_hash_move_forward_ex(ah3, &hp3); } PHALCON_INIT_NVAR(join_conditions); phalcon_fast_join_str(join_conditions, SL(" AND "), join_expressions TSRMLS_CC); PHALCON_SCONCAT_SV(sql_join, " ON ", join_conditions); } } phalcon_concat_self(&sql, sql_join TSRMLS_CC); zend_hash_move_forward_ex(ah2, &hp2); } } /** * Check for a WHERE clause */ if (phalcon_array_isset_string(definition, SS("where"))) { PHALCON_OBS_VAR(where_conditions); phalcon_array_fetch_string(&where_conditions, definition, SL("where"), PH_NOISY); if (Z_TYPE_P(where_conditions) == IS_ARRAY) { PHALCON_CALL_METHOD(&where_expression, this_ptr, "getsqlexpression", where_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " WHERE ", where_expression); } else { PHALCON_SCONCAT_SV(sql, " WHERE ", where_conditions); } } /** * Check for a GROUP clause */ if (phalcon_array_isset_string(definition, SS("group"))) { PHALCON_INIT_VAR(group_items); array_init(group_items); PHALCON_OBS_VAR(group_fields); phalcon_array_fetch_string(&group_fields, definition, SL("group"), PH_NOISY); phalcon_is_iterable(group_fields, &ah4, &hp4, 0, 0); while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) { PHALCON_GET_HVALUE(group_field); PHALCON_CALL_METHOD(&group_expression, this_ptr, "getsqlexpression", group_field, escape_char); phalcon_array_append(&group_items, group_expression, PH_SEPARATE); zend_hash_move_forward_ex(ah4, &hp4); } PHALCON_INIT_VAR(group_sql); phalcon_fast_join_str(group_sql, SL(", "), group_items TSRMLS_CC); PHALCON_INIT_VAR(group_clause); PHALCON_CONCAT_SV(group_clause, " GROUP BY ", group_sql); phalcon_concat_self(&sql, group_clause TSRMLS_CC); } /** * Check for a HAVING clause */ if (phalcon_array_isset_string(definition, SS("having"))) { PHALCON_OBS_VAR(having_conditions); phalcon_array_fetch_string(&having_conditions, definition, SL("having"), PH_NOISY); PHALCON_CALL_METHOD(&having_expression, this_ptr, "getsqlexpression", having_conditions, escape_char); PHALCON_SCONCAT_SV(sql, " HAVING ", having_expression); } /** * Check for a ORDER clause */ if (phalcon_array_isset_string(definition, SS("order"))) { PHALCON_OBS_VAR(order_fields); phalcon_array_fetch_string(&order_fields, definition, SL("order"), PH_NOISY); PHALCON_INIT_VAR(order_items); array_init(order_items); phalcon_is_iterable(order_fields, &ah5, &hp5, 0, 0); while (zend_hash_get_current_data_ex(ah5, (void**) &hd, &hp5) == SUCCESS) { PHALCON_GET_HVALUE(order_item); PHALCON_OBS_NVAR(order_expression); phalcon_array_fetch_long(&order_expression, order_item, 0, PH_NOISY); PHALCON_CALL_METHOD(&order_sql_item, this_ptr, "getsqlexpression", order_expression, escape_char); /** * In the numeric 1 position could be a ASC/DESC clause */ if (phalcon_array_isset_long(order_item, 1)) { PHALCON_OBS_NVAR(sql_order_type); phalcon_array_fetch_long(&sql_order_type, order_item, 1, PH_NOISY); PHALCON_INIT_NVAR(order_sql_item_type); PHALCON_CONCAT_VSV(order_sql_item_type, order_sql_item, " ", sql_order_type); } else { PHALCON_CPY_WRT(order_sql_item_type, order_sql_item); } phalcon_array_append(&order_items, order_sql_item_type, PH_SEPARATE); zend_hash_move_forward_ex(ah5, &hp5); } PHALCON_INIT_VAR(order_sql); phalcon_fast_join_str(order_sql, SL(", "), order_items TSRMLS_CC); PHALCON_SCONCAT_SV(sql, " ORDER BY ", order_sql); } /** * Oracle does not implement the LIMIT clause as some RDBMS do. * We have to simulate it with subqueries and ROWNUM. * Unfortunately because we use the column wildcard "*", * this puts an extra column into the query result set. */ if (phalcon_array_isset_string_fetch(&limit_value, definition, SS("limit"))) { if (likely(Z_TYPE_P(limit_value) == IS_ARRAY)) { if (likely(phalcon_array_isset_string_fetch(&number, limit_value, SS("number")))) { PHALCON_OBS_NVAR(tmp1); phalcon_array_fetch_string(&tmp1, number, SL("value"), PH_NOISY); if (phalcon_array_isset_string_fetch(&offset, limit_value, SS("offset"))) { PHALCON_OBS_NVAR(tmp2); phalcon_array_fetch_string(&tmp2, offset, SL("value"), PH_NOISY); } else { PHALCON_INIT_NVAR(tmp2); ZVAL_LONG(tmp2, 0); } z_one = PHALCON_GLOBAL(z_one); PHALCON_INIT_VAR(ini_range); phalcon_add_function(ini_range, tmp2, z_one TSRMLS_CC); PHALCON_INIT_VAR(end_range); phalcon_add_function(end_range, tmp2, tmp1 TSRMLS_CC); PHALCON_INIT_VAR(sql_limit); PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ", end_range ); PHALCON_CPY_WRT(sql, sql_limit); } } else { PHALCON_INIT_VAR(ini_range); ZVAL_LONG(ini_range, 1); PHALCON_CPY_WRT(end_range, limit_value); PHALCON_INIT_VAR(sql_limit); PHALCON_SCONCAT_SVSVSV(sql_limit,"SELECT Z2.* FROM (SELECT Z1.*, ROWNUM DB_ROWNUM FROM ( ", sql, " ) Z1 ) Z2 WHERE Z2.DB_ROWNUM BETWEEN ", ini_range , " AND ", end_range ); PHALCON_CPY_WRT(sql, sql_limit); } } RETURN_CTOR(sql); }
/** * Internal sanitize wrapper to filter_var * * @param mixed $value * @param string $filter * @return mixed */ PHP_METHOD(Phalcon_Filter, _sanitize){ zval *value, *filter, *filters, *filter_object; zval *arguments, *filtered = NULL, *type = NULL, *quote, *empty_str; zval *escaped, *allow_fraction, *options, *exception_message; PHALCON_MM_GROW(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &value, &filter) == FAILURE) { RETURN_MM_NULL(); } PHALCON_OBS_VAR(filters); phalcon_read_property(&filters, this_ptr, SL("_filters"), PH_NOISY_CC); if (phalcon_array_isset(filters, filter)) { PHALCON_OBS_VAR(filter_object); phalcon_array_fetch(&filter_object, filters, filter, PH_NOISY_CC); /** * If the filter is a closure we call it in the PHP userland */ if (phalcon_is_instance_of(filter_object, SL("Closure") TSRMLS_CC)) { PHALCON_INIT_VAR(arguments); array_init_size(arguments, 1); phalcon_array_append(&arguments, value, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_VAR(filtered); PHALCON_CALL_USER_FUNC_ARRAY(filtered, filter_object, arguments); } else { PHALCON_INIT_NVAR(filtered); PHALCON_CALL_METHOD_PARAMS_1(filtered, filter_object, "filter", value); } RETURN_CCTOR(filtered); } if (PHALCON_IS_STRING(filter, "email")) { /** * The 'email' filter uses the filter extension */ PHALCON_INIT_VAR(type); ZVAL_LONG(type, 517); PHALCON_INIT_VAR(quote); ZVAL_STRING(quote, "'", 1); PHALCON_INIT_VAR(empty_str); ZVAL_STRING(empty_str, "", 1); PHALCON_INIT_VAR(escaped); phalcon_fast_str_replace(escaped, quote, empty_str, value TSRMLS_CC); PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", escaped, type); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "int")) { /** * 'int' filter sanitizes a numeric input */ PHALCON_INIT_NVAR(type); ZVAL_LONG(type, 519); PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", value, type); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "string")) { PHALCON_INIT_NVAR(type); ZVAL_LONG(type, 513); PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_2(filtered, "filter_var", value, type); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "float")) { /** * The 'float' filter uses the filter extension */ PHALCON_INIT_VAR(allow_fraction); ZVAL_LONG(allow_fraction, 4096); PHALCON_INIT_VAR(options); array_init_size(options, 1); phalcon_array_update_string(&options, SL("flags"), &allow_fraction, PH_COPY | PH_SEPARATE TSRMLS_CC); PHALCON_INIT_NVAR(type); ZVAL_LONG(type, 520); PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_3(filtered, "filter_var", value, type, options); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "alphanum")) { PHALCON_INIT_NVAR(filtered); phalcon_filter_alphanum(filtered, value); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "trim")) { PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_1(filtered, "trim", value); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "striptags")) { PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_1(filtered, "strip_tags", value); goto ph_end_0; } if (PHALCON_IS_STRING(filter, "lower")) { if (phalcon_function_exists_ex(SS("mb_strtolower") TSRMLS_CC) == SUCCESS) { /** * 'lower' checks for the mbstring extension to make a correct lowercase * transformation */ PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_1(filtered, "mb_strtolower", value); } else { PHALCON_INIT_NVAR(filtered); phalcon_fast_strtolower(filtered, value); } goto ph_end_0; } if (PHALCON_IS_STRING(filter, "upper")) { if (phalcon_function_exists_ex(SS("mb_strtoupper") TSRMLS_CC) == SUCCESS) { /** * 'upper' checks for the mbstring extension to make a correct lowercase * transformation */ PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_1(filtered, "mb_strtoupper", value); } else { PHALCON_INIT_NVAR(filtered); PHALCON_CALL_FUNC_PARAMS_1(filtered, "strtoupper", value); } goto ph_end_0; } PHALCON_INIT_VAR(exception_message); PHALCON_CONCAT_SVS(exception_message, "Sanitize filter ", filter, " is not supported"); PHALCON_THROW_EXCEPTION_ZVAL(phalcon_filter_exception_ce, exception_message); return; ph_end_0: RETURN_CCTOR(filtered); }