*error = NULL;
}
- if (!index_php || strlen(index_php) == 0) {
+ if (!index_php) {
index_php = "index.php";
}
*/
PHP_METHOD(Phar, setDefaultStub)
{
- char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL;
+ char *index = NULL, *webindex = NULL, *error = NULL;
+ char *stub = "dummy";
int index_len = 0, webindex_len = 0;
- size_t stub_len;
+ size_t stub_len = 0;
PHAR_ARCHIVE_OBJECT();
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
- RETURN_FALSE;
- }
-
- if (phar_obj->arc.archive->is_tar) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
- "Stub cannot be changed in a tar-based phar");
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
RETURN_FALSE;
}
- if (phar_obj->arc.archive->is_zip) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
- "Stub cannot be changed in a zip-based phar");
+ if (ZEND_NUM_ARGS() > 0 && (phar_obj->arc.archive->is_tar || phar_obj->arc.archive->is_zip)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
RETURN_FALSE;
}
RETURN_FALSE;
}
- stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
+ if (!phar_obj->arc.archive->is_tar && !phar_obj->arc.archive->is_zip) {
+ stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
- if (error) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, error);
- efree(error);
- RETURN_FALSE;
+ if (error) {
+ zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, error);
+ efree(error);
+ RETURN_FALSE;
+ }
}
phar_flush(phar_obj->arc.archive, stub, stub_len, &error TSRMLS_CC);
entry.tar_type = '0';
entry.phar = phar;
entry.fp_type = PHAR_MOD;
+
+
/* set alias */
if (!phar->is_temporary_alias && phar->alias_len) {
entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
}
/* set stub */
- if (user_stub) {
+ if (user_stub && user_stub != "dummy") {
char *pos;
if (len < 0) {
/* resource passed in */
efree(user_stub);
}
} else {
- if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
- /* this is a brand new phar */
- entry.fp = php_stream_fopen_tmpfile();
- if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
+ /* Either this is a brand new phar (add the stub), or setDefaultStub() is the caller (overwrite the stub) */
+ entry.fp = php_stream_fopen_tmpfile();
+ if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
+ if (error) {
+ spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
+ }
+ return EOF;
+ }
+ entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
+ entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
+ entry.filename_len = sizeof(".phar/stub.php")-1;
+
+ if (!user_stub) {
+ if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
+ if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
+ php_stream_close(entry.fp);
+ efree(entry.filename);
+ if (error) {
+ spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname);
+ }
+ return EOF;
+ }
+ }
+ } else {
+ if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
+ php_stream_close(entry.fp);
+ efree(entry.filename);
if (error) {
- spprintf(error, 0, "unable to create stub in new tar-based phar \"%s\"", phar->fname);
+ spprintf(error, 0, "unable to overwrite stub in zip-based phar \"%s\"", phar->fname);
}
return EOF;
}
- entry.uncompressed_filesize = sizeof(newstub) - 1;
- entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
- entry.filename_len = sizeof(".phar/stub.php")-1;
- zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL);
}
}
$phar = new Phar($fname);
$phar['a.php'] = '<php echo "this is a\n"; ?>';
$phar['b.php'] = '<php echo "this is b\n"; ?>';
+$phar->setStub('<?php echo "Hello World\n"; __HALT_COMPILER(); ?>');
+
+var_dump($phar->getStub());
+
+echo "============================================================================\n";
+echo "============================================================================\n";
try {
$phar->setDefaultStub();
<?php
unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.tar');
?>
---EXPECT--
-Stub cannot be changed in a tar-based phar
+--EXPECTF--
+string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
+"
+============================================================================
+============================================================================
string(60) "<?php // tar-based phar archive stub file
__HALT_COMPILER();"
============================================================================
============================================================================
-Stub cannot be changed in a tar-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
string(60) "<?php // tar-based phar archive stub file
__HALT_COMPILER();"
============================================================================
============================================================================
-Stub cannot be changed in a tar-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
string(60) "<?php // tar-based phar archive stub file
__HALT_COMPILER();"
===DONE===
$phar = new Phar($fname);
$phar['a.php'] = '<php echo "this is a\n"; ?>';
$phar['b.php'] = '<php echo "this is b\n"; ?>';
+$phar->setStub('<?php echo "Hello World\n"; __HALT_COMPILER(); ?>');
+
+var_dump($phar->getStub());
+
+echo "============================================================================\n";
+echo "============================================================================\n";
try {
$phar->setDefaultStub();
<?php
unlink(dirname(__FILE__) . '/' . basename(__FILE__, '.clean.php') . '.phar.zip');
?>
---EXPECT--
-Stub cannot be changed in a zip-based phar
+--EXPECTF--
+string(51) "<?php echo "Hello World\n"; __HALT_COMPILER(); ?>
+"
+============================================================================
+============================================================================
string(60) "<?php // zip-based phar archive stub file
__HALT_COMPILER();"
============================================================================
============================================================================
-Stub cannot be changed in a zip-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 1 given in %sphar_setdefaultstub.php on line %d
string(60) "<?php // zip-based phar archive stub file
__HALT_COMPILER();"
============================================================================
============================================================================
-Stub cannot be changed in a zip-based phar
+
+Warning: Phar::setDefaultStub(): method accepts no arguments for a tar- or zip-based phar stub, 2 given in %sphar_setdefaultstub.php on line %d
string(60) "<?php // zip-based phar archive stub file
__HALT_COMPILER();"
===DONE===
entry.uncompressed_filesize = entry.compressed_filesize = phar->alias_len;
entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
entry.filename_len = sizeof(".phar/alias.txt")-1;
- entry.is_modified = 1;
if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
if (error) {
spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname);
}
/* set stub */
- if (user_stub) {
+ if (user_stub && user_stub != "dummy") {
if (len < 0) {
/* resource passed in */
if (!(php_stream_from_zval_no_verify(stubfile, (zval **)user_stub))) {
}
entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
entry.filename_len = sizeof(".phar/stub.php")-1;
- entry.is_modified = 1;
if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
if (free_user_stub) {
efree(user_stub);
efree(user_stub);
}
} else {
- if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
- /* this is a brand new phar, add the stub */
- entry.fp = php_stream_fopen_tmpfile();
- if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
- if (error) {
- spprintf(error, 0, "unable to create stub in new zip-based phar \"%s\"", phar->fname);
+ /* Either this is a brand new phar (add the stub), or setDefaultStub() is the caller (overwrite the stub) */
+ entry.fp = php_stream_fopen_tmpfile();
+ if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
+ if (error) {
+ spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
+ }
+ return EOF;
+ }
+ entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
+ entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
+ entry.filename_len = sizeof(".phar/stub.php")-1;
+
+ if (!user_stub) {
+ if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
+ if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
+ php_stream_close(entry.fp);
+ efree(entry.filename);
+ if (error) {
+ spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname);
+ }
+ return EOF;
}
- return EOF;
}
- entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
- entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
- entry.filename_len = sizeof(".phar/stub.php")-1;
- entry.is_modified = 1;
- if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
+ } else {
+ if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
php_stream_close(entry.fp);
efree(entry.filename);
if (error) {
- spprintf(error, 0, "unable to create stub in new zip-based phar \"%s\"", phar->fname);
+ spprintf(error, 0, "unable to overwrite stub in zip-based phar \"%s\"", phar->fname);
}
return EOF;
}