MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at script end)")
}
if ((char) nextchar == '\r') {
- if (EOF == (nextchar = php_stream_getc(fp))) {
+ /* if we have an \r we require an \n as well */
+ if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') {
MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at script end)")
}
halt_offset++;
*/
int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **error TSRMLS_DC) /* {{{ */
{
- static const char newstub[] = "<?php __HALT_COMPILER();";
+ static const char newstub[] = "<?php __HALT_COMPILER(); ?>\r\n";
phar_entry_info *entry;
int halt_offset, restore_alias_len, global_flags = 0, closeoldfile;
char *buf, *pos;
php_stream_filter *filter;
php_serialize_data_t metadata_hash;
smart_str main_metadata_str = {0};
+ int free_user_stub;
if (error) {
*error = NULL;
}
php_stream_close(newfile);
if (error) {
- spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", archive->fname);
+ spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", archive->fname);
}
return EOF;
}
} else {
len = -len;
}
- offset = php_stream_copy_to_stream(stubfile, newfile, len);
- if (len != offset && len != PHP_STREAM_COPY_ALL) {
+ user_stub = 0;
+ if (!(len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)) || !user_stub) {
if (closeoldfile) {
php_stream_close(oldfile);
}
php_stream_close(newfile);
if (error) {
- spprintf(error, 0, "unable to copy stub from resource to new phar \"%s\"", archive->fname);
+ spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", archive->fname);
}
return EOF;
}
- archive->halt_offset = offset;
+ free_user_stub = 1;
} else {
- if ((pos = strstr(user_stub, "__HALT_COMPILER();")) == NULL)
- {
- if (closeoldfile) {
- php_stream_close(oldfile);
- }
- php_stream_close(newfile);
- if (error) {
- spprintf(error, 0, "illegal stub for phar \"%s\"", archive->fname);
- }
- return EOF;
+ free_user_stub = 0;
+ }
+ if ((pos = strstr(user_stub, "__HALT_COMPILER();")) == NULL)
+ {
+ if (closeoldfile) {
+ php_stream_close(oldfile);
}
- if ((size_t)len != php_stream_write(newfile, user_stub, len)) {
- if (closeoldfile) {
- php_stream_close(oldfile);
- }
- php_stream_close(newfile);
- if (error) {
- spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", archive->fname);
- }
- return EOF;
+ php_stream_close(newfile);
+ if (error) {
+ spprintf(error, 0, "illegal stub for phar \"%s\"", archive->fname);
}
- archive->halt_offset = len;
+ if (free_user_stub) {
+ efree(user_stub);
+ }
+ return EOF;
+ }
+ len = pos - user_stub + 18;
+ if ((size_t)len != php_stream_write(newfile, user_stub, len)
+ || 5 != php_stream_write(newfile, " ?>\r\n", 5)) {
+ if (closeoldfile) {
+ php_stream_close(oldfile);
+ }
+ php_stream_close(newfile);
+ if (error) {
+ spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", archive->fname);
+ }
+ if (free_user_stub) {
+ efree(user_stub);
+ }
+ return EOF;
+ }
+ archive->halt_offset = len + 5;
+ if (free_user_stub) {
+ efree(user_stub);
}
} else {
if (archive->halt_offset && oldfile && !archive->is_brandnew) {
include 'phar://brandnewphar.phar/a.php';
var_dump($p->getStub());
$p['b.php'] = '<?php var_dump("World");';
-$p->setStub('<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>');
+$p->setStub('<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER();');
include 'phar://brandnewphar.phar/b.php';
var_dump($p->getStub());
$p->stopBuffering();
bool(true)
bool(false)
string(5) "Hello"
-string(82) "<?php var_dump("First"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
+string(84) "<?php var_dump("First"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
+"
string(5) "World"
-string(83) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
+string(85) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
+"
===COMMIT===
bool(true)
string(5) "Hello"
string(5) "World"
-string(83) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>"
+string(85) "<?php var_dump("Second"); Phar::mapPhar("brandnewphar.phar"); __HALT_COMPILER(); ?>
+"
===DONE===
var_dump($phar->getStub() == $stub);
$stub = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
+$sexp = $stub . "\r\n";
+
$phar->setStub($stub);
var_dump($phar->getStub());
var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
$phar->stopBuffering();
var_dump($phar->getStub());
var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
$phar = new Phar($fname);
var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
?>
===DONE===
string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
bool(true)
-string(49) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>"
+string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
+"
+bool(false)
bool(true)
-string(49) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>"
+string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
+"
+bool(false)
bool(true)
+bool(false)
bool(true)
===DONE===
--- /dev/null
+--TEST--
+Phar::setStub()/getStub() from file
+--SKIPIF--
+<?php if (!extension_loaded("phar")) print "skip"; ?>
+--INI--
+phar.require_hash=0
+phar.readonly=0
+--FILE--
+<?php
+$fname = dirname(__FILE__) . '/' . basename(__FILE__, '.php') . '.phar.php';
+$pname = 'phar://' . $fname;
+$stub = '<?php echo "first stub\n"; __HALT_COMPILER(); ?>';
+$file = $stub;
+
+$files = array();
+$files['a'] = 'a';
+$files['b'] = 'b';
+$files['c'] = 'c';
+
+include 'phar_test.inc';
+
+$phar = new Phar($fname);
+var_dump($stub);
+var_dump($phar->getStub());
+var_dump($phar->getStub() == $stub);
+
+$stub = '<?php echo "second stub\n"; __HALT_COMPILER(); ?>';
+$sexp = $stub . "\r\n";
+$stub = fopen('data://,'.$stub, 'r');
+$phar->setStub($stub);
+var_dump($phar->getStub());
+var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
+$phar->stopBuffering();
+var_dump($phar->getStub());
+var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
+
+$phar = new Phar($fname);
+var_dump($phar->getStub() == $stub);
+var_dump($phar->getStub() == $sexp);
+
+?>
+===DONE===
+--CLEAN--
+<?php
+unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.php');
+__HALT_COMPILER();
+?>
+--EXPECT--
+string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
+string(48) "<?php echo "first stub\n"; __HALT_COMPILER(); ?>"
+bool(true)
+string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
+"
+bool(false)
+bool(true)
+string(51) "<?php echo "second stub\n"; __HALT_COMPILER(); ?>
+"
+bool(false)
+bool(true)
+bool(false)
+bool(true)
+===DONE===