mirror of
https://github.com/php/php-src.git
synced 2025-08-18 23:18:56 +02:00
MFB: The constructor should also throw exceptions, make this semi useful now.
This commit is contained in:
parent
b15f5357fe
commit
cf8d87306f
3 changed files with 29 additions and 17 deletions
|
@ -74,36 +74,46 @@ PHP_METHOD(sqlite3, open)
|
||||||
zend_uchar filename_type;
|
zend_uchar filename_type;
|
||||||
int filename_len, encryption_key_len = 0;
|
int filename_len, encryption_key_len = 0;
|
||||||
long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
||||||
|
zend_error_handling error_handling;
|
||||||
|
|
||||||
db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
|
db_obj = (php_sqlite3_db_object *)zend_object_store_get_object(object TSRMLS_CC);
|
||||||
|
zend_replace_error_handling(EH_THROW, NULL, &error_handling TSRMLS_CC);
|
||||||
|
|
||||||
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|lS", &filename, &filename_len, &filename_type, &flags, &encryption_key, &encryption_key_len)) {
|
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|lS", &filename, &filename_len, &filename_type, &flags, &encryption_key, &encryption_key_len)) {
|
||||||
|
zend_restore_error_handling(&error_handling TSRMLS_CC);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zend_restore_error_handling(&error_handling TSRMLS_CC);
|
||||||
|
|
||||||
if (db_obj->initialised) {
|
if (db_obj->initialised) {
|
||||||
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Already initialised DB Object");
|
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Already initialised DB Object", 0 TSRMLS_CC);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename_type == IS_UNICODE) {
|
if (filename_type == IS_UNICODE) {
|
||||||
if (php_stream_path_encode(NULL, &filename, &filename_len, (UChar *)filename, filename_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
|
if (php_stream_path_encode(NULL, &filename, &filename_len, (UChar *)filename, filename_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
|
||||||
RETURN_FALSE;
|
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to decode filepath", 0 TSRMLS_CC);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strncmp(filename, ":memory:", 8) != 0) {
|
if (strncmp(filename, ":memory:", 8) != 0) {
|
||||||
if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
|
if (!(fullpath = expand_filepath(filename, NULL TSRMLS_CC))) {
|
||||||
|
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to expand filepath", 0 TSRMLS_CC);
|
||||||
if (filename_type == IS_UNICODE) {
|
if (filename_type == IS_UNICODE) {
|
||||||
efree(filename);
|
efree(filename);
|
||||||
}
|
}
|
||||||
RETURN_FALSE;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (php_check_open_basedir(fullpath TSRMLS_CC)) {
|
if (php_check_open_basedir(fullpath TSRMLS_CC)) {
|
||||||
|
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "open_basedir prohibits opening %s", fullpath);
|
||||||
if (filename_type == IS_UNICODE) {
|
if (filename_type == IS_UNICODE) {
|
||||||
efree(filename);
|
efree(filename);
|
||||||
}
|
}
|
||||||
efree(fullpath);
|
efree(fullpath);
|
||||||
RETURN_FALSE;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fullpath = estrdup(filename);
|
fullpath = estrdup(filename);
|
||||||
|
@ -119,7 +129,7 @@ PHP_METHOD(sqlite3, open)
|
||||||
/* Todo: utf-16 = sqlite3_open16 */
|
/* Todo: utf-16 = sqlite3_open16 */
|
||||||
if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
|
if (sqlite3_open(fullpath, &(db_obj->db)) != SQLITE_OK) {
|
||||||
#endif
|
#endif
|
||||||
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
|
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
|
||||||
if (fullpath) {
|
if (fullpath) {
|
||||||
efree(fullpath);
|
efree(fullpath);
|
||||||
}
|
}
|
||||||
|
@ -131,6 +141,7 @@ PHP_METHOD(sqlite3, open)
|
||||||
#if SQLITE_HAS_CODEC
|
#if SQLITE_HAS_CODEC
|
||||||
if (encryption_key_len > 0) {
|
if (encryption_key_len > 0) {
|
||||||
if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
|
if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
|
||||||
|
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,7 +154,6 @@ PHP_METHOD(sqlite3, open)
|
||||||
if (fullpath) {
|
if (fullpath) {
|
||||||
efree(fullpath);
|
efree(fullpath);
|
||||||
}
|
}
|
||||||
RETURN_TRUE;
|
|
||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
|
|
|
@ -7,14 +7,14 @@ SQLite3::open error test
|
||||||
$unreadable = dirname(__FILE__) . '/unreadable.db';
|
$unreadable = dirname(__FILE__) . '/unreadable.db';
|
||||||
touch($unreadable);
|
touch($unreadable);
|
||||||
chmod($unreadable, 0200);
|
chmod($unreadable, 0200);
|
||||||
$db = new SQLite3($unreadable);
|
try {
|
||||||
|
$db = new SQLite3($unreadable);
|
||||||
var_dump($db);
|
} catch (Exception $e) {
|
||||||
|
echo $e->getMessage() . "\n";
|
||||||
|
}
|
||||||
echo "Done\n";
|
echo "Done\n";
|
||||||
unlink($unreadable);
|
unlink($unreadable);
|
||||||
?>
|
?>
|
||||||
--EXPECTF--
|
--EXPECTF--
|
||||||
Notice: SQLite3::__construct(): Unable to open database: unable to open database file in %s/sqlite3_15_open_error.php on line %d
|
Unable to open database: unable to open database file
|
||||||
object(SQLite3)#%d (0) {
|
|
||||||
}
|
|
||||||
Done
|
Done
|
||||||
|
|
|
@ -16,8 +16,11 @@ var_dump($db->close());
|
||||||
unlink($directory . $file);
|
unlink($directory . $file);
|
||||||
|
|
||||||
echo "Above test directory\n";
|
echo "Above test directory\n";
|
||||||
$db = new SQLite3('../bad' . $file);
|
try {
|
||||||
var_dump($db);
|
$db = new SQLite3('../bad' . $file);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo $e->getMessage() . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
echo "Done\n";
|
echo "Done\n";
|
||||||
?>
|
?>
|
||||||
|
@ -28,7 +31,6 @@ object(SQLite3)#%d (0) {
|
||||||
bool(true)
|
bool(true)
|
||||||
Above test directory
|
Above test directory
|
||||||
|
|
||||||
Warning: SQLite3::__construct(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
|
Warning: SQLite3::__construct(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s/sqlite3_21_security.php on line %d
|
||||||
object(SQLite3)#%d (0) {
|
open_basedir prohibits opening %s
|
||||||
}
|
|
||||||
Done
|
Done
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue