From b859c9dd4bf431a799faa8e24b364b3cfe2ce9d9 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Sat, 5 Jan 2008 04:03:33 +0000 Subject: [PATCH] beautify 404 error handler, and note missing file. throw exception if webPhar() is attempted with an extracted file, as webPhar() makes no sense on-disk --- ext/phar/phar_object.c | 21 +++++++++++++++++---- ext/phar/tests/frontcontroller6.phpt | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 ext/phar/tests/frontcontroller6.phpt diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index bb248514b3..156145f84d 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -301,7 +301,7 @@ static int phar_file_action(phar_entry_data *phar, char *mime_type, int code, ch return -1; } -void phar_do_404(char *fname, int fname_len, char *f404, int f404_len TSRMLS_DC) +void phar_do_404(char *fname, int fname_len, char *f404, int f404_len, char *entry, int entry_len TSRMLS_DC) { int hi; phar_entry_data *phar; @@ -322,7 +322,9 @@ nofile: ctr.line = "HTTP/1.0 404 Not Found"; sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC); sapi_send_headers(TSRMLS_C); - PHPWRITE("File Not Found<title></head><body><h1>404 - File Not Found</h1></body></html>", sizeof("<html><head><title>File Not Found<title></head><body><h1>404 - File Not Found</h1></body></html>") - 1); + PHPWRITE("<html>\n <head>\n <title>File Not Found<title>\n </head>\n <body>\n <h1>404 - File ", sizeof("<html>\n <head>\n <title>File Not Found<title>\n </head>\n <body>\n <h1>404 - File ") - 1); + PHPWRITE(entry, entry_len); + PHPWRITE(" Not Found</h1>\n </body>\n</html>", sizeof(" Not Found</h1>\n </body>\n</html>") - 1); } } @@ -350,6 +352,17 @@ PHP_METHOD(Phar, webPhar) } phar_request_initialize(TSRMLS_C); + if (zend_hash_num_elements(&(PHAR_GLOBALS->phar_plain_map))) { + fname = zend_get_executed_filename(TSRMLS_C); + fname_len = strlen(fname); + if((alias && + zend_hash_find(&(PHAR_GLOBALS->phar_plain_map), alias, alias_len+1, (void **)&plain_map) == SUCCESS) + || (zend_hash_find(&(PHAR_GLOBALS->phar_plain_map), fname, fname_len+1, (void **)&plain_map) == SUCCESS) + ) { + zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Cannot use Phar::webPhar() from an extracted phar archive, simply use the extracted files directly"); + return; + } + } if (phar_open_compiled_file(alias, alias_len, &error TSRMLS_CC) != SUCCESS) { if (error) { zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, error); @@ -428,7 +441,7 @@ PHP_METHOD(Phar, webPhar) if (error) { efree(error); } - phar_do_404(fname, fname_len, f404, f404_len TSRMLS_CC); + phar_do_404(fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC); zend_bailout(); return; } else { @@ -474,7 +487,7 @@ PHP_METHOD(Phar, webPhar) } if (FAILURE == phar_get_entry_data(&phar, fname, fname_len, entry, entry_len, "r", &error TSRMLS_CC)) { - phar_do_404(fname, fname_len, f404, f404_len TSRMLS_CC); + phar_do_404(fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC); #ifdef PHP_WIN32 efree(fname); #endif diff --git a/ext/phar/tests/frontcontroller6.phpt b/ext/phar/tests/frontcontroller6.phpt new file mode 100644 index 0000000000..2f74c8f04d --- /dev/null +++ b/ext/phar/tests/frontcontroller6.phpt @@ -0,0 +1,20 @@ +--TEST-- +Phar front controller 404 +--SKIPIF-- +<?php if (!extension_loaded("phar")) die("skip"); ?> +--ENV-- +SCRIPT_NAME=/frontcontroller6.php/notfound.php +REQUEST_URI=/frontcontroller6.php/notfound.php +--FILE_EXTERNAL-- +frontcontroller.phar +--EXPECTHEADERS-- +Status: 404 Not Found +--EXPECT-- +<html> + <head> + <title>File Not Found<title> + </head> + <body> + <h1>404 - File /notfound.php Not Found</h1> + </body> +</html> \ No newline at end of file -- 2.50.1