Example #1
0
void zephir_basename(zval *return_value, zval *path)
{
	if (EXPECTED(Z_TYPE_P(path) == IS_STRING)) {
		zend_string *ret;
		ret = php_basename(Z_STRVAL_P(path), Z_STRLEN_P(path), NULL, 0);
		ZVAL_STR(return_value, ret);
	} else {
		ZVAL_FALSE(return_value);
	}
}
Example #2
0
void
apd_summary_output_footer(void)
{
	apd_fcall_t *fcall;
	apd_function_entry_t *function_entry;
	apd_file_entry_t *file_entry;
	apd_coverage_t coverage;
	char *ret;
	size_t ret_len;
	TSRMLS_FETCH();

	memset(&coverage, 0, sizeof(apd_coverage_t));

	php_printf("<table border=\"1\" width=\"100%%\">\n");
	php_printf("<tr>\n");
	php_printf("<th>Function</th>\n");
	php_printf("<th>File</th>\n");
	php_printf("<th>Line</th>\n");
	php_printf("<th># of calls</th>\n");
	php_printf("<th>User</th>\n");
	php_printf("<th>System</th>\n");
	php_printf("<th>Real</th>\n");
	php_printf("</tr>\n");

	find_expensive(&coverage, 20 TSRMLS_CC);
	fcall = coverage.head;
	while (fcall) {
		function_entry = fcall->entry;
		file_entry = apd_array_get(&APD_GLOBALS(summary).files, fcall->file);
		php_basename(file_entry->filename, strlen(file_entry->filename), NULL, 0, &ret, &ret_len TSRMLS_CC);
		php_printf("<tr>\n");
		php_printf("<td>%s</td>\n", function_entry->name);
		php_printf("<td><abbr title=\"%s\">%s</abbr></td>\n", file_entry->filename, ret);
		php_printf("<td>%d</td>\n", fcall->line);
		php_printf("<td>%d</td>\n", fcall->calls);
		php_printf("<td>%4.2f</td>\n", (double) fcall->usertime / 1000000);
		php_printf("<td>%4.2f</td>\n", (double) fcall->systemtime / 1000000);
		php_printf("<td>%4.2f</td>\n", (double) fcall->realtime / 1000000);
		php_printf("</tr>\n");
		
		fcall = fcall->next;
/*		efree(ret);  */
	}
	php_printf("</table>\n");

	zend_llist_clean(&APD_GLOBALS(summary).call_list);
}
Example #3
0
/* {{{ php_mail
 */
PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd)
{
#if (defined PHP_WIN32 || defined NETWARE)
	int tsm_err;
	char *tsm_errmsg = NULL;
#endif
	FILE *sendmail;
	int ret;
	char *sendmail_path = INI_STR("sendmail_path");
	char *sendmail_cmd = NULL;
	char *mail_log = INI_STR("mail.log");
	char *hdr = headers;
#if PHP_SIGCHILD
	void (*sig_handler)() = NULL;
#endif

#define MAIL_RET(val) \
	if (hdr != headers) {	\
		efree(hdr);	\
	}	\
	return val;	\

	if (mail_log && *mail_log) {
		char *tmp;
		time_t curtime;
		size_t l;
		zend_string *date_str;

		time(&curtime);
		date_str = php_format_date("d-M-Y H:i:s e", 13, curtime, 1);

		l = spprintf(&tmp, 0, "[%s] mail() on [%s:%d]: To: %s -- Headers: %s\n", date_str->val, zend_get_executed_filename(), zend_get_executed_lineno(), to, hdr ? hdr : "");

		zend_string_free(date_str);

		if (hdr) {
			php_mail_log_crlf_to_spaces(tmp);
		}

		if (!strcmp(mail_log, "syslog")) {
			/* Drop the final space when logging to syslog. */
			tmp[l - 1] = 0;
			php_mail_log_to_syslog(tmp);
		}
		else {
			/* Convert the final space to a newline when logging to file. */
			tmp[l - 1] = '\n';
			php_mail_log_to_file(mail_log, tmp, l);
		}

		efree(tmp);
	}
	if (PG(mail_x_header)) {
		const char *tmp = zend_get_executed_filename();
		zend_string *f;

		f = php_basename(tmp, strlen(tmp), NULL, 0);

		if (headers != NULL) {
			spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s\n%s", php_getuid(), f->val, headers);
		} else {
			spprintf(&hdr, 0, "X-PHP-Originating-Script: " ZEND_LONG_FMT ":%s", php_getuid(), f->val);
		}
		zend_string_release(f);
	}

	if (!sendmail_path) {
#if (defined PHP_WIN32 || defined NETWARE)
		/* handle old style win smtp sending */
		if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, hdr, subject, to, message, NULL, NULL, NULL) == FAILURE) {
			if (tsm_errmsg) {
				php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg);
				efree(tsm_errmsg);
			} else {
				php_error_docref(NULL, E_WARNING, "%s", GetSMErrorText(tsm_err));
			}
			MAIL_RET(0);
		}
		MAIL_RET(1);
#else
		MAIL_RET(0);
#endif
	}
	if (extra_cmd != NULL) {
		spprintf(&sendmail_cmd, 0, "%s %s", sendmail_path, extra_cmd);
	} else {
		sendmail_cmd = sendmail_path;
	}

#if PHP_SIGCHILD
	/* Set signal handler of SIGCHLD to default to prevent other signal handlers
	 * from being called and reaping the return code when our child exits.
	 * The original handler needs to be restored after pclose() */
	sig_handler = (void *)signal(SIGCHLD, SIG_DFL);
	if (sig_handler == SIG_ERR) {
		sig_handler = NULL;
	}
#endif

#ifdef PHP_WIN32
	sendmail = popen_ex(sendmail_cmd, "wb", NULL, NULL);
#else
	/* Since popen() doesn't indicate if the internal fork() doesn't work
	 * (e.g. the shell can't be executed) we explicitly set it to 0 to be
	 * sure we don't catch any older errno value. */
	errno = 0;
	sendmail = popen(sendmail_cmd, "w");
#endif
	if (extra_cmd != NULL) {
		efree (sendmail_cmd);
	}

	if (sendmail) {
#ifndef PHP_WIN32
		if (EACCES == errno) {
			php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
			pclose(sendmail);
#if PHP_SIGCHILD
			/* Restore handler in case of error on Windows
			   Not sure if this applicable on Win but just in case. */
			if (sig_handler) {
				signal(SIGCHLD, sig_handler);
			}
#endif
			MAIL_RET(0);
		}
#endif
		fprintf(sendmail, "To: %s\n", to);
		fprintf(sendmail, "Subject: %s\n", subject);
		if (hdr != NULL) {
			fprintf(sendmail, "%s\n", hdr);
		}
		fprintf(sendmail, "\n%s\n", message);
		ret = pclose(sendmail);

#if PHP_SIGCHILD
		if (sig_handler) {
			signal(SIGCHLD, sig_handler);
		}
#endif

#ifdef PHP_WIN32
		if (ret == -1)
#else
#if defined(EX_TEMPFAIL)
		if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
#elif defined(EX_OK)
		if (ret != EX_OK)
#else
		if (ret != 0)
#endif
#endif
		{
			MAIL_RET(0);
		} else {
			MAIL_RET(1);
		}
	} else {
		php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
#if PHP_SIGCHILD
		if (sig_handler) {
			signal(SIGCHLD, sig_handler);						
		}
#endif
		MAIL_RET(0);
	}

	MAIL_RET(1); /* never reached */
}
Example #4
0
static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
{
	struct zip_stat sb;
	const char *path = stream->orig_path;
	size_t path_len = strlen(stream->orig_path);
	char file_dirname[MAXPATHLEN];
	struct zip *za;
	char *fragment;
	size_t fragment_len;
	int err;
	zend_string *file_basename;

	fragment = strchr(path, '#');
	if (!fragment) {
		return -1;
	}


	if (strncasecmp("zip://", path, 6) == 0) {
		path += 6;
	}

	fragment_len = strlen(fragment);

	if (fragment_len < 1) {
		return -1;
	}
	path_len = strlen(path);
	if (path_len >= MAXPATHLEN) {
		return -1;
	}

	memcpy(file_dirname, path, path_len - fragment_len);
	file_dirname[path_len - fragment_len] = '\0';

	file_basename = php_basename((char *)path, path_len - fragment_len, NULL, 0);
	fragment++;

	if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
		zend_string_release(file_basename);
		return -1;
	}

	za = zip_open(file_dirname, ZIP_CREATE, &err);
	if (za) {
		memset(ssb, 0, sizeof(php_stream_statbuf));
		if (zip_stat(za, fragment, ZIP_FL_NOCASE, &sb) != 0) {
			zip_close(za);
			zend_string_release(file_basename);
			return -1;
		}
		zip_close(za);

		if (path[path_len-1] != '/') {
			ssb->sb.st_size = sb.size;
			ssb->sb.st_mode |= S_IFREG; /* regular file */
		} else {
			ssb->sb.st_size = 0;
			ssb->sb.st_mode |= S_IFDIR; /* regular directory */
		}

		ssb->sb.st_mtime = sb.mtime;
		ssb->sb.st_atime = sb.mtime;
		ssb->sb.st_ctime = sb.mtime;
		ssb->sb.st_nlink = 1;
		ssb->sb.st_rdev = -1;
#ifndef PHP_WIN32
		ssb->sb.st_blksize = -1;
		ssb->sb.st_blocks = -1;
#endif
		ssb->sb.st_ino = -1;
	}
	zend_string_release(file_basename);
	return 0;
}
Example #5
0
void dump_dot(void)
{

    int x, print_header, key_type;
    int *block_num;
    int c = 0, len = 0, flag_ho;
    long index, key_len;
    char *block_name = NULL;
    char *time_buff = pvt_get_time();
    smart_str str_dot_func = {0};

    TSRMLS_FETCH();

    char *tmp_buff = NULL;
    tmp_buff = pvt_sprintf(tpl_dot[0], time_buff);
    fprintf(PVT_G(trace_file_f_dot), "%s", tmp_buff);
    efree(time_buff);
    efree(tmp_buff);

    if (PVT_G(pvt_graph_fold)) {
        hide_functions();
    }

    /* Iterate through all blocks/nodes */
    for (zend_hash_internal_pointer_reset(PVT_G(block_summary));
        zend_hash_has_more_elements(PVT_G(block_summary)) == SUCCESS;
        zend_hash_move_forward(PVT_G(block_summary))) {

        key_type = zend_hash_get_current_key(PVT_G(block_summary), &block_name, &index, 0);
        if (key_type == HASH_KEY_IS_STRING) {
            key_len = strlen(block_name);
        }
        zend_hash_get_current_data(PVT_G(block_summary), (void*) &block_num);

        print_header = 1;

        int flag_started = 0;
        int flag_break   = 0;
        int flag_nop     = 0;
        flag_ho = 1;

        size_t ret_len;

        /* Iterate through all functions */
        for (x = 0; x < PVT_G(funcs)->len; x++) {

            if (PVT_G(funcs)->file_id[x] == *block_num) {

                flag_started = 1;
                if (print_header) {
                    if (PVT_G(funcs)->type[x] == 2 && flag_ho) {

                        char *ret;
                        php_basename(
                            PVT_G(funcs)->file_name[x],
                            strlen(PVT_G(funcs)->file_name[x]),
                            NULL, 0, &ret, &ret_len TSRMLS_CC
                        );
                        char *escaped_str = php_escape_html_entities(
                            block_name,
                            strlen(block_name),
                            &len, 0, ENT_QUOTES, NULL TSRMLS_CC
                        );
                        /* Print the block header */
                        fprintf(PVT_G(trace_file_f_dot),
                            tpl_point_func[0],
                            *block_num,
                            PVT_G(funcs)->line[x],
                            *block_num,
                            PVT_G(funcs)->file_name[x], PVT_G(funcs)->line[x],
                            ret,
                            escaped_str,
                            PVT_G(funcs)->func_id[x]
                        );
                        flag_ho = 0;
                        efree(ret);
                        efree(escaped_str);
                    }
                }
            }

            if (PVT_G(funcs)->stack[x] <= PVT_G(dot_funcs_i)->file_id[c] && flag_started) {
                if (0 == print_header) {
                    flag_break = 1;
                } else {
                    flag_break = 0;
                }
            }

            if (0 == flag_started) {
                continue;
            }

            if ((PVT_G(funcs)->stack[x]-1) != PVT_G(dot_funcs_i)->file_id[c]) {
                if (!flag_break) {
                    continue;
                } else {
                    flag_nop = 1;
                }
            }

            if (flag_nop != 1) {
                flag_nop = 0;
                if (print_header) {
                    print_header = 0;
                }

                if (PVT_G(dot_funcs_i)->empty[c]) {
                    flag_started = 0;
                    break;
                }

                /* Check if function repeats */
                if (0 == PVT_G(funcs)->hide[x] || !PVT_G(pvt_graph_fold)) {
                    if (2 == PVT_G(funcs)->type[x]) {

                        /* This is USER function */
                        char *escaped_str = php_escape_html_entities(
                            PVT_G(funcs)->func_name[x],
                            strlen(PVT_G(funcs)->func_name[x]),
                            &len, 0, ENT_QUOTES, NULL TSRMLS_CC
                        );

                        fprintf(PVT_G(trace_file_f_dot),
                            tpl_point_func[1],
                            PVT_G(funcs)->line[x],
                            PVT_G(funcs)->line[x],
                            escaped_str,
                            PVT_G(funcs)->func_id[x]
                        );
                        efree(escaped_str);

                        char *tmp_buff = pvt_sprintf(
                            tpl_relation_func[0],
                            *block_num,
                            PVT_G(funcs)->line[x],
                            PVT_G(funcs)->file_id[x],
                            PVT_G(funcs)->file_id[x],
                            PVT_G(funcs)->func_id[x]
                        );
                        smart_str_appends(&str_dot_func, tmp_buff);
                        efree(tmp_buff);

                    } else {
                        /* This is ZEND function */
                        char *escaped_str = php_escape_html_entities(
                            PVT_G(funcs)->func_name[x],
                            strlen(PVT_G(funcs)->func_name[x]),
                            &len, 0, ENT_QUOTES, NULL TSRMLS_CC
                        );

                        fprintf(PVT_G(trace_file_f_dot),
                            tpl_point_func[2],
                            PVT_G(funcs)->line[x],
                            (1 == PVT_G(funcs)->is_evil[x] ? "d63333" : "e0ebcc"),
                            escaped_str,
                            PVT_G(funcs)->func_id[x]
                        );
                        efree(escaped_str);
                    }

                } /* end if (0 ==... */
            } /* end if (flag_nop... */

            if (flag_break) {
                if (!flag_nop) {
                    flag_break   = 0;
                    flag_started = 0;
                    break;
                }
            }
        } /* end for (x... */
        c++;
        fprintf(PVT_G(trace_file_f_dot), "</TABLE>>\n]\n");
    }

    smart_str_0(&str_dot_func);
    if (str_dot_func.c != NULL) {
        fprintf(PVT_G(trace_file_f_dot), "%s", str_dot_func.c);
    }
    smart_str_free(&str_dot_func);
    fprintf(PVT_G(trace_file_f_dot), "\n}\n");
    fclose(PVT_G(trace_file_f_dot));
}