Exemplo n.º 1
0
/**
 * Generates SQL to add an index to a table
 *
 * @param string $tableName
 * @param string $schemaName
 * @param Phalcon\Db\IndexInterface $index
 * @return string
 */
PHP_METHOD(Phalcon_Db_Dialect_Sqlite, addIndex){

	zval *table_name, *schema_name, *index, *sql = NULL, *columns = NULL;
	zval *quoted_column_list = NULL, *name = NULL;
	zval *index_type = NULL;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 3, 0, &table_name, &schema_name, &index);
	
	PHALCON_VERIFY_INTERFACE_EX(index, phalcon_db_indexinterface_ce, phalcon_db_exception_ce, 1);

	PHALCON_CALL_METHOD(&name, index, "getname");
	PHALCON_CALL_METHOD(&index_type, index, "gettype");

	PHALCON_INIT_VAR(sql);
	if (zend_is_true(schema_name)) {
		if (index_type && Z_TYPE_P(index_type) == IS_STRING && Z_STRLEN_P(index_type) > 0) {
			PHALCON_CONCAT_SVSVSVSVS(sql, "CREATE ", index_type, " INDEX \"", schema_name, "\".\"", name, "\" ON \"", table_name, "\" (");
		} else {
			PHALCON_CONCAT_SVSVSVS(sql, "CREATE INDEX \"", schema_name, "\".\"", name, "\" ON \"", table_name, "\" (");
		}
	} else if (index_type && Z_TYPE_P(index_type) == IS_STRING && Z_STRLEN_P(index_type) > 0) {
		PHALCON_CONCAT_SVSVSVS(sql, "CREATE ", index_type, " INDEX \"", name, "\" ON \"", table_name, "\" (");
	} else {
		PHALCON_CONCAT_SVSVS(sql, "CREATE INDEX \"", name, "\" ON \"", table_name, "\" (");
	}

	PHALCON_CALL_METHOD(&columns, index, "getcolumns");
	PHALCON_CALL_METHOD(&quoted_column_list, getThis(), "getcolumnlist", columns);

	PHALCON_SCONCAT_VS(sql, quoted_column_list, ")");
	RETURN_CTOR(sql);
}
Exemplo n.º 2
0
/**
 * Handles uncaught exceptions
 *
 * @param \Exception $exception
 * @return boolean
 */
PHP_METHOD(Phalcon_Debug, onUncaughtException){

	zval *exception, *is_active = NULL, *message = NULL;
	zval *class_name, *css_sources, *escaped_message = NULL;
	zval *html, *version, *file, *line, *show_back_trace;
	zval *data_vars, *trace, *trace_item = NULL, *n = NULL, *html_item = NULL;
	zval *_REQUEST, *value = NULL, *key_request = NULL, *_SERVER;
	zval *key_server = NULL, *files, *key_file = NULL;
	zval *memory, *data_var = NULL, *key_var = NULL, *variable = NULL, *dumped_argument = NULL;
	zval *js_sources;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4;
	HashPosition hp0, hp1, hp2, hp3, hp4;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &exception);
	
	/** 
	 * Cancel the output buffer if active
	 */
	if (phalcon_ob_get_level(TSRMLS_C) > 0) {
		phalcon_ob_end_clean(TSRMLS_C);
	}
	
	PHALCON_OBS_VAR(is_active);
	phalcon_read_static_property(&is_active, SL("phalcon\\debug"), SL("_isActive") TSRMLS_CC);
	
	/** 
	 * Avoid that multiple exceptions being showed
	 */
	if (zend_is_true(is_active)) {
		PHALCON_INIT_VAR(message);
		phalcon_call_method(message, exception, "getmessage");
		zend_print_zval(message, 0);
	}
	
	PHALCON_INIT_NVAR(is_active);
	ZVAL_BOOL(is_active, 1);
	
	/** 
	 * Globally block the debug component to avoid other exceptions must be shown
	 */
	phalcon_update_static_property(SL("phalcon\\debug"), SL("_isActive"), is_active TSRMLS_CC);
	
	PHALCON_INIT_VAR(class_name);
	phalcon_get_class(class_name, exception, 0 TSRMLS_CC);
	
	PHALCON_INIT_NVAR(message);
	phalcon_call_method(message, exception, "getmessage");
	
	/** 
	 * CSS static sources to style the error presentation
	 */
	PHALCON_INIT_VAR(css_sources);
	phalcon_call_method(css_sources, this_ptr, "getcsssources");
	
	/** 
	 * Escape the exception's message avoiding possible XSS injections?
	 */
	PHALCON_CPY_WRT(escaped_message, message);
	
	/** 
	 * Use the exception info as document's title
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVSVS(html, "<html><head><title>", class_name, ": ", escaped_message, "</title>");
	PHALCON_SCONCAT_VS(html, css_sources, "</head><body>");
	
	/** 
	 * Get the version link
	 */
	PHALCON_INIT_VAR(version);
	phalcon_call_method(version, this_ptr, "getversion");
	phalcon_concat_self(&html, version TSRMLS_CC);
	
	PHALCON_INIT_VAR(file);
	phalcon_call_method(file, exception, "getfile");
	
	PHALCON_INIT_VAR(line);
	phalcon_call_method(line, exception, "getline");
	
	/** 
	 * Main exception info
	 */
	phalcon_concat_self_str(&html, SL("<div align=\"center\"><div class=\"error-main\">") TSRMLS_CC);
	PHALCON_SCONCAT_SVSVS(html, "<h1>", class_name, ": ", escaped_message, "</h1>");
	PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-file\">", file, " (", line, ")</span>");
	phalcon_concat_self_str(&html, SL("</div>") TSRMLS_CC);
	
	PHALCON_OBS_VAR(show_back_trace);
	phalcon_read_property_this(&show_back_trace, this_ptr, SL("_showBackTrace"), PH_NOISY_CC);
	
	/** 
	 * Check if the developer wants to show the backtrace or not
	 */
	if (zend_is_true(show_back_trace)) {
	
		PHALCON_OBS_VAR(data_vars);
		phalcon_read_property_this(&data_vars, this_ptr, SL("_data"), PH_NOISY_CC);
	
		/** 
		 * Create the tabs in the page
		 */
		phalcon_concat_self_str(&html, SL("<div class=\"error-info\"><div id=\"tabs\"><ul>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-1\">Backtrace</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-2\">Request</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-3\">Server</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-4\">Included Files</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-5\">Memory</a></li>") TSRMLS_CC);
		if (Z_TYPE_P(data_vars) == IS_ARRAY) { 
			phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-6\">Variables</a></li>") TSRMLS_CC);
		}
	
		phalcon_concat_self_str(&html, SL("</ul>") TSRMLS_CC);
	
		/** 
		 * Print backtrace
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-1\"><table cellspacing=\"0\" align=\"center\" width=\"100%\">") TSRMLS_CC);
	
		PHALCON_INIT_VAR(trace);
		phalcon_call_method(trace, exception, "gettrace");
	
		phalcon_is_iterable(trace, &ah0, &hp0, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
	
			PHALCON_GET_HKEY(n, ah0, hp0);
			PHALCON_GET_HVALUE(trace_item);
	
			/** 
			 * Every line in the trace is rendered using 'showTraceItem'
			 */
			PHALCON_INIT_NVAR(html_item);
			phalcon_call_method_p2(html_item, this_ptr, "showtraceitem", n, trace_item);
			phalcon_concat_self(&html, html_item TSRMLS_CC);
	
			zend_hash_move_forward_ex(ah0, &hp0);
		}
	
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
	
		/** 
		 * Print _REQUEST superglobal
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-2\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);
		phalcon_get_global(&_REQUEST, SS("_REQUEST") TSRMLS_CC);
	
		phalcon_is_iterable(_REQUEST, &ah1, &hp1, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
	
			PHALCON_GET_HKEY(key_request, ah1, hp1);
			PHALCON_GET_HVALUE(value);
	
			PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_request, "</td><td>", value, "</td></tr>");
	
			zend_hash_move_forward_ex(ah1, &hp1);
		}
	
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
	
		/** 
		 * Print _SERVER superglobal
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-3\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);
		phalcon_get_global(&_SERVER, SS("_SERVER") TSRMLS_CC);
	
		phalcon_is_iterable(_SERVER, &ah2, &hp2, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {
	
			PHALCON_GET_HKEY(key_server, ah2, hp2);
			PHALCON_GET_HVALUE(value);
	
			PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_server, "</td><td>", value, "</td></tr>");
	
			zend_hash_move_forward_ex(ah2, &hp2);
		}
	
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
	
		/** 
		 * Show included files
		 */
		PHALCON_INIT_VAR(files);
		phalcon_call_func(files, "get_included_files");
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-4\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>#</th><th>Path</th></tr>") TSRMLS_CC);
	
		phalcon_is_iterable(files, &ah3, &hp3, 0, 0);
	
		while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {
	
			PHALCON_GET_HKEY(key_file, ah3, hp3);
			PHALCON_GET_HVALUE(value);
	
			PHALCON_SCONCAT_SVSVS(html, "<tr><td>", key_file, "</th><td>", value, "</td></tr>");
	
			zend_hash_move_forward_ex(ah3, &hp3);
		}
	
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
	
		/** 
		 * Memory usage
		 */
		PHALCON_INIT_VAR(memory);
		ZVAL_LONG(memory, zend_memory_usage(1 TSRMLS_CC));
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-5\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		PHALCON_SCONCAT_SVS(html, "<tr><th colspan=\"2\">Memory</th></tr><tr><td>Usage</td><td>", memory, "</td></tr>");
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
	
		/** 
		 * Print extra variables passed to the component
		 */
		if (Z_TYPE_P(data_vars) == IS_ARRAY) { 
			phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-6\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
			phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);
	
			phalcon_is_iterable(data_vars, &ah4, &hp4, 0, 0);
	
			while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {
	
				PHALCON_GET_HKEY(key_var, ah4, hp4);
				PHALCON_GET_HVALUE(data_var);
	
				PHALCON_OBS_NVAR(variable);
				phalcon_array_fetch_long(&variable, data_var, 0, PH_NOISY);
	
				PHALCON_INIT_NVAR(dumped_argument);
				phalcon_call_method_p1(dumped_argument, this_ptr, "_getvardump", variable);
				PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_var, "</td><td>", dumped_argument, "</td></tr>");
	
				zend_hash_move_forward_ex(ah4, &hp4);
			}
	
			phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
		}
	
		phalcon_concat_self_str(&html, SL("</div>") TSRMLS_CC);
	}
	
	/** 
	 * Get Javascript sources
	 */
	PHALCON_INIT_VAR(js_sources);
	phalcon_call_method(js_sources, this_ptr, "getjssources");
	PHALCON_SCONCAT_VS(html, js_sources, "</div></body></html>");
	
	/** 
	 * Print the HTML, @TODO, add an option to store the html
	 */
	zend_print_zval(html, 0);
	
	PHALCON_INIT_NVAR(is_active);
	ZVAL_BOOL(is_active, 0);
	
	/** 
	 * Unlock the exception renderer
	 */
	phalcon_update_static_property(SL("phalcon\\debug"), SL("_isActive"), is_active TSRMLS_CC);
	RETURN_MM_TRUE;
}
Exemplo n.º 3
0
/**
 * Traverses a collection calling the callback to generate its HTML
 *
 * @param Phalcon\Assets\Collection $collection
 * @param callback $callback
 * @param string $type
 * @param array $args
 */
PHP_METHOD(Phalcon_Assets_Manager, output){

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

	PHALCON_MM_GROW();

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

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

	PHALCON_INIT_VAR(output);

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

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

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

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

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

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

		PHALCON_INIT_VAR(source_base_path);

		PHALCON_INIT_VAR(target_base_path);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		PHALCON_GET_HVALUE(resource);

		PHALCON_INIT_NVAR(filter_needed);
		ZVAL_FALSE(filter_needed);

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

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

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

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

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

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

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

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

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

			if (zend_is_true(local)) {

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

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

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

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

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

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

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

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

			zend_hash_move_forward_ex(ah0, &hp0);
			continue;
		}

		if (zend_is_true(filter_needed)) {

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

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

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

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

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

					PHALCON_GET_HVALUE(filter);

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

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

					zend_hash_move_forward_ex(ah1, &hp1);
				}

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

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

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

		if (!zend_is_true(join)) {

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

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

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

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

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

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

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

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

		zend_hash_move_forward_ex(ah0, &hp0);
	}

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

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

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

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

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

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

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

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

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

	RETURN_CCTOR(output);
}
Exemplo n.º 4
0
/**
 * Prepare an HTML string of information about a single variable.
 */
PHP_METHOD(Phalcon_Debug_Dump, output){

	zval *variable, *name = NULL, *tab = NULL, *space, *tmp = NULL, *new_tab = NULL;
	zval *output = NULL, *str = NULL, *type = NULL, *style = NULL, *count = NULL, *key = NULL, *value = NULL, *replace_pairs = NULL;
	zval *class_name = NULL, *objects, *detailed = NULL, *properties = NULL, *methods = NULL, *method = NULL;
	HashTable *ah0;
	HashPosition hp0;
	zval **hd;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 2, &variable, &name, &tab);

	if (!name) {
		name = PHALCON_GLOBAL(z_null);
	} else if (!PHALCON_IS_EMPTY(name)) {
		PHALCON_CONCAT_SVS(return_value, "var ", name, " ");
	}

	if (!tab) {
		tab = PHALCON_GLOBAL(z_one);
	}

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

	if (Z_TYPE_P(variable) == IS_ARRAY) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style =':style'>Array</b> (<span style =':style'>:count</span>) (\n", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "arr", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, phalcon_fast_count_int(variable TSRMLS_CC));

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":count"), count, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		phalcon_concat_self(&return_value, output TSRMLS_CC);

		phalcon_is_iterable(variable, &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_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			phalcon_concat_self(&return_value, tmp TSRMLS_CC);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "[<span style=':style'>:key</span>] => ", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "arr", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":key"), key, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			phalcon_concat_self(&return_value, output TSRMLS_CC);

			if (PHALCON_IS_LONG(tab, 1) && !PHALCON_IS_EMPTY(name) && !phalcon_is_numeric(key) && PHALCON_IS_IDENTICAL(name, key)) {
				zend_hash_move_forward_ex(ah0, &hp0);
				continue;
			} else {
				PHALCON_INIT_NVAR(new_tab);
				ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

				PHALCON_CALL_SELF(&tmp, "output", value, PHALCON_GLOBAL(z_null), new_tab);
				PHALCON_SCONCAT_VS(return_value, tmp, "\n");
			}

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(new_tab);
		ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT(return_value, tmp);
	} else if (Z_TYPE_P(variable) == IS_OBJECT) {

		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Object</b> :class", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "obj", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_class(class_name, variable, 0 TSRMLS_CC);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":class"), class_name, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_parent_class(class_name, variable, 0 TSRMLS_CC);

		if (zend_is_true(class_name)) {
			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, " <b style=':style'>extends</b> :parent", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":parent"), class_name, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);
		}

		PHALCON_SCONCAT_STR(return_value, " (\n");

		objects  = phalcon_fetch_nproperty_this(this_ptr, SL("_objects"), PH_NOISY TSRMLS_CC);

		if (phalcon_fast_in_array(variable, objects TSRMLS_CC)) {
			
			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);
			PHALCON_SCONCAT_VS(return_value, tmp, "[already listed]\n");

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT_VS(return_value, tmp, ")");

			RETURN_MM();
		}

		phalcon_update_property_array_append(this_ptr, SL("_objects"), variable TSRMLS_CC);

		detailed  = phalcon_fetch_nproperty_this(this_ptr, SL("_detailed"), PH_NOISY TSRMLS_CC);

		PHALCON_INIT_NVAR(properties);
		phalcon_get_object_vars(properties, variable, !zend_is_true(detailed) TSRMLS_CC);

		phalcon_is_iterable(properties, &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_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT(return_value, tmp);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "-><span style=':style'>:key</span> (<span style=':style'>:type</span>) = ", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":key"), key, PH_COPY);

			if (PHALCON_PROPERTY_IS_PUBLIC_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("public"), PH_COPY);
			} else if (PHALCON_PROPERTY_IS_PRIVATE_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("private"), PH_COPY);
			} else if (PHALCON_PROPERTY_IS_PROTECTED_ZVAL(variable, key)) {
				phalcon_array_update_string_string(&replace_pairs, SL(":type"), SL("protected"), PH_COPY);
			}

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

			PHALCON_CALL_SELF(&tmp, "output", value, PHALCON_GLOBAL(z_null), new_tab);
			PHALCON_SCONCAT_VS(return_value, tmp, ")\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(methods);

		phalcon_get_class_methods(methods, variable, !zend_is_true(detailed) TSRMLS_CC);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT(return_value, tmp);

		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, ":class <b style=':style'>methods</b>: (<span style=':style'>:count</span>) (\n", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "obj", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(class_name);
		phalcon_get_class(class_name, variable, 0 TSRMLS_CC);

		PHALCON_INIT_NVAR(count);
		ZVAL_LONG(count, phalcon_fast_count_int(methods TSRMLS_CC));

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":class"), class_name, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":count"), count, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);

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

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

			PHALCON_GET_HVALUE(method);

			PHALCON_INIT_NVAR(new_tab);
			ZVAL_LONG(new_tab, Z_LVAL_P(tab) + 1);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, new_tab);

			PHALCON_SCONCAT(return_value, tmp);

			PHALCON_INIT_NVAR(str);
			ZVAL_STRING(str, "-><span style=':style'>:method</span>();\n", 1);

			PHALCON_INIT_NVAR(type);
			ZVAL_STRING(type, "obj", 1);

			PHALCON_CALL_SELF(&style, "getstyle", type);

			PHALCON_INIT_NVAR(replace_pairs);
			array_init(replace_pairs);

			phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
			phalcon_array_update_string(&replace_pairs, SL(":method"), method, PH_COPY);

			PHALCON_INIT_NVAR(output);
			phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

			PHALCON_SCONCAT(return_value, output);

			PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

			PHALCON_SCONCAT_VS(return_value, tmp, "\n");

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		PHALCON_INIT_NVAR(new_tab);
		ZVAL_LONG(new_tab, Z_LVAL_P(tab) - 1);

		PHALCON_CALL_FUNCTION(&tmp, "str_repeat", space, tab);

		PHALCON_SCONCAT_VS(return_value, tmp, ")");
	} else if (Z_TYPE_P(variable) == IS_LONG) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Integer</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "int", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_DOUBLE) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Float</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "float", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (phalcon_is_numeric_ex(variable)) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Numeric string</b> (<span style=':style'>:length</span>) \"<span style=':style'>:var</span>\"", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "num", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string_long(&replace_pairs, SL(":length"), Z_STRLEN_P(variable), PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_STRING) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>String</b> (<span style=':style'>:length</span>) \"<span style=':style'>:var</span>\"", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "str", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string_long(&replace_pairs, SL(":length"), Z_STRLEN_P(variable), PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_BOOL) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>Boolean</b> (<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "bool", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		if (zend_is_true(variable)) {
			phalcon_array_update_string_string(&replace_pairs, SL(":var"), SL("TRUE") , PH_COPY);
		} else {
			phalcon_array_update_string_string(&replace_pairs, SL(":var"), SL("FALSE") , PH_COPY);
		}

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else if (Z_TYPE_P(variable) == IS_NULL) {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "<b style=':style'>NULL</b>", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "null", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	} else {
		PHALCON_INIT_NVAR(str);
		ZVAL_STRING(str, "(<span style=':style'>:var</span>)", 1);

		PHALCON_INIT_NVAR(type);
		ZVAL_STRING(type, "other", 1);

		PHALCON_CALL_SELF(&style, "getstyle", type);

		PHALCON_INIT_NVAR(replace_pairs);
		array_init(replace_pairs);

		phalcon_array_update_string(&replace_pairs, SL(":style"), style, PH_COPY);
		phalcon_array_update_string(&replace_pairs, SL(":var"), variable, PH_COPY);

		PHALCON_INIT_NVAR(output);
		phalcon_strtr_array(output, str, replace_pairs TSRMLS_CC);

		PHALCON_SCONCAT(return_value, output);
	}

	RETURN_MM();
}
Exemplo n.º 5
0
/**
 * Handles uncaught exceptions
 *
 * @param \Exception $exception
 * @return boolean
 */
PHP_METHOD(Phalcon_Debug, onUncaughtException){

	zval *exception, *is_active = NULL, *message = NULL;
	zval *class_name, *css_sources = NULL, *escaped_message = NULL;
	zval *html, *version = NULL, *file = NULL, *line = NULL, *show_back_trace;
	zval *data_vars, *trace = NULL, *trace_item = NULL, *n = NULL, *html_item = NULL;
	zval *_REQUEST, *value = NULL, *key_request = NULL, *joined_value = NULL, *_SERVER;
	zval *key_server = NULL, *files = NULL, *key_file = NULL;
	zval *memory, *data_var = NULL, *key_var = NULL, *variable = NULL, *dumped_argument = NULL;
	zval *js_sources = NULL, *formatted_file = NULL;
	HashTable *ah0, *ah1, *ah2, *ah3, *ah4;
	HashPosition hp0, hp1, hp2, hp3, hp4;
	zval **hd;
	char* link_format;
	zend_bool ini_exists = 1;
	zval z_link_format = zval_used_for_init;
	zend_class_entry *ce;

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 1, 0, &exception);
	PHALCON_VERIFY_CLASS_EX(exception, zend_exception_get_default(TSRMLS_C), phalcon_exception_ce, 1);

	/** 
	 * Cancel the output buffer if active
	 */
	if (phalcon_ob_get_level(TSRMLS_C) > 0) {
		phalcon_ob_end_clean(TSRMLS_C);
	}

	is_active = phalcon_fetch_static_property_ce(phalcon_debug_ce, SL("_isActive") TSRMLS_CC);

	/** 
	 * Avoid that multiple exceptions being showed
	 */
	if (zend_is_true(is_active)) {
		PHALCON_CALL_METHOD(&message, exception, "getmessage");
		zend_print_zval(message, 0);
	}

	/** 
	 * Globally block the debug component to avoid other exceptions must be shown
	 */
	zend_update_static_property_bool(phalcon_debug_ce, SL("_isActive"), 1 TSRMLS_CC);

	PHALCON_INIT_VAR(class_name);
	ce = Z_OBJCE_P(exception);
	ZVAL_STRINGL(class_name, ce->name, ce->name_length, !IS_INTERNED(ce->name));

	PHALCON_CALL_METHOD(&message, exception, "getmessage");

	/** 
	 * CSS static sources to style the error presentation
	 */
	PHALCON_CALL_METHOD(&css_sources, this_ptr, "getcsssources");

	/** 
	 * Escape the exception's message avoiding possible XSS injections?
	 */
	PHALCON_CPY_WRT(escaped_message, message);

	/** 
	 * Use the exception info as document's title
	 */
	PHALCON_INIT_VAR(html);
	PHALCON_CONCAT_SVSVS(html, "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>", class_name, ": ", escaped_message, "</title>");
	PHALCON_SCONCAT_VS(html, css_sources, "</head><body>");

	/** 
	 * Get the version link
	 */
	PHALCON_CALL_METHOD(&version, this_ptr, "getversion");
	phalcon_concat_self(&html, version TSRMLS_CC);

	PHALCON_CALL_METHOD(&file, exception, "getfile");
	PHALCON_CALL_METHOD(&line, exception, "getline");

	link_format = zend_ini_string_ex(SS("xdebug.file_link_format"), 0, &ini_exists);
	if (!link_format || !ini_exists || !strlen(link_format)) {
		link_format = "file://%f#%l";
	}

	ZVAL_STRING(&z_link_format, link_format, 0);

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

	/** 
	 * Main exception info
	 */
	phalcon_concat_self_str(&html, SL("<div align=\"center\"><div class=\"error-main\">") TSRMLS_CC);
	PHALCON_SCONCAT_SVSVS(html, "<h1>", class_name, ": ", escaped_message, "</h1>");
	PHALCON_SCONCAT_SVSVS(html, "<span class=\"error-file\">", formatted_file, " (", line, ")</span>");
	phalcon_concat_self_str(&html, SL("</div>") TSRMLS_CC);

	PHALCON_OBS_VAR(show_back_trace);
	phalcon_read_property_this(&show_back_trace, this_ptr, SL("_showBackTrace"), PH_NOISY TSRMLS_CC);

	/** 
	 * Check if the developer wants to show the backtrace or not
	 */
	if (zend_is_true(show_back_trace)) {
		PHALCON_OBS_VAR(data_vars);
		phalcon_read_property_this(&data_vars, this_ptr, SL("_data"), PH_NOISY TSRMLS_CC);

		/** 
		 * Create the tabs in the page
		 */
		phalcon_concat_self_str(&html, SL("<div class=\"error-info\"><div id=\"tabs\"><ul>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-1\">Backtrace</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-2\">Request</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-3\">Server</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-4\">Included Files</a></li>") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-5\">Memory</a></li>") TSRMLS_CC);
		if (Z_TYPE_P(data_vars) == IS_ARRAY) { 
			phalcon_concat_self_str(&html, SL("<li><a href=\"#error-tabs-6\">Variables</a></li>") TSRMLS_CC);
		}

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

		/** 
		 * Print backtrace
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-1\"><table cellspacing=\"0\" align=\"center\" width=\"100%\">") TSRMLS_CC);

		PHALCON_CALL_METHOD(&trace, exception, "gettrace");

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

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

			PHALCON_GET_HKEY(n, ah0, hp0);
			PHALCON_GET_HVALUE(trace_item);

			/** 
			 * Every line in the trace is rendered using 'showTraceItem'
			 */
			PHALCON_CALL_METHOD(&html_item, this_ptr, "showtraceitem", n, trace_item, &z_link_format);
			phalcon_concat_self(&html, html_item TSRMLS_CC);

			zend_hash_move_forward_ex(ah0, &hp0);
		}

		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);

		/** 
		 * Print _REQUEST superglobal
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-2\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);
		_REQUEST = phalcon_get_global(SS("_REQUEST") TSRMLS_CC);

		if (Z_TYPE_P(_REQUEST) == IS_ARRAY) {
			phalcon_is_iterable(_REQUEST, &ah1, &hp1, 0, 0);

			while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
				PHALCON_GET_HKEY(key_request, ah1, hp1);
				PHALCON_GET_HVALUE(value);

				if (Z_TYPE_P(value) == IS_ARRAY) {
					PHALCON_CALL_METHOD(&joined_value, this_ptr, "_getvardump", value);
					PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_request, "</td><td>", joined_value, "</td></tr>");
				} else {
					PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_request, "</td><td>", value, "</td></tr>");
				}
				zend_hash_move_forward_ex(ah1, &hp1);
			}
		}

		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);

		/** 
		 * Print _SERVER superglobal
		 */
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-3\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);
		_SERVER = phalcon_get_global(SS("_SERVER") TSRMLS_CC);

		if (Z_TYPE_P(_SERVER) == IS_ARRAY) {
			phalcon_is_iterable(_SERVER, &ah2, &hp2, 0, 0);

			while (zend_hash_get_current_data_ex(ah2, (void**) &hd, &hp2) == SUCCESS) {

				PHALCON_GET_HKEY(key_server, ah2, hp2);
				PHALCON_GET_HVALUE(value);

				PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", value);
				PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_server, "</td><td>", dumped_argument, "</td></tr>");

				zend_hash_move_forward_ex(ah2, &hp2);
			}
		}

		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);

		/** 
		 * Show included files
		 */
		PHALCON_CALL_FUNCTION(&files, "get_included_files");
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-4\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		phalcon_concat_self_str(&html, SL("<tr><th>#</th><th>Path</th></tr>") TSRMLS_CC);

		phalcon_is_iterable(files, &ah3, &hp3, 0, 0);

		while (zend_hash_get_current_data_ex(ah3, (void**) &hd, &hp3) == SUCCESS) {

			PHALCON_GET_HKEY(key_file, ah3, hp3);
			PHALCON_GET_HVALUE(value);

			PHALCON_SCONCAT_SVSVS(html, "<tr><td>", key_file, "</th><td>", value, "</td></tr>");

			zend_hash_move_forward_ex(ah3, &hp3);
		}

		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);

		/** 
		 * Memory usage
		 */
		PHALCON_INIT_VAR(memory);
		ZVAL_LONG(memory, zend_memory_usage(1 TSRMLS_CC));
		phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-5\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
		PHALCON_SCONCAT_SVS(html, "<tr><th colspan=\"2\">Memory</th></tr><tr><td>Usage</td><td>", memory, "</td></tr>");
		phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);

		/** 
		 * Print extra variables passed to the component
		 */
		if (Z_TYPE_P(data_vars) == IS_ARRAY) { 
			phalcon_concat_self_str(&html, SL("<div id=\"error-tabs-6\"><table cellspacing=\"0\" align=\"center\" class=\"superglobal-detail\">") TSRMLS_CC);
			phalcon_concat_self_str(&html, SL("<tr><th>Key</th><th>Value</th></tr>") TSRMLS_CC);

			phalcon_is_iterable(data_vars, &ah4, &hp4, 0, 0);

			while (zend_hash_get_current_data_ex(ah4, (void**) &hd, &hp4) == SUCCESS) {

				PHALCON_GET_HKEY(key_var, ah4, hp4);
				PHALCON_GET_HVALUE(data_var);

				PHALCON_OBS_NVAR(variable);
				phalcon_array_fetch_long(&variable, data_var, 0, PH_NOISY);

				PHALCON_CALL_METHOD(&dumped_argument, this_ptr, "_getvardump", variable);
				PHALCON_SCONCAT_SVSVS(html, "<tr><td class=\"key\">", key_var, "</td><td>", dumped_argument, "</td></tr>");

				zend_hash_move_forward_ex(ah4, &hp4);
			}

			phalcon_concat_self_str(&html, SL("</table></div>") TSRMLS_CC);
		}

		phalcon_concat_self_str(&html, SL("</div>") TSRMLS_CC);
	}

	/** 
	 * Get Javascript sources
	 */
	PHALCON_CALL_METHOD(&js_sources, this_ptr, "getjssources");
	PHALCON_SCONCAT_VS(html, js_sources, "</div></body></html>");

	/** 
	 * Print the HTML, @TODO, add an option to store the html
	 */
	zend_print_zval(html, 0);

	/** 
	 * Unlock the exception renderer
	 */
	zend_update_static_property_bool(phalcon_debug_ce, SL("_isActive"), 0 TSRMLS_CC);
	RETURN_MM_TRUE;
}