Raketenleichtmatrose: PHP PDO:SQLITE - Falle: attempt to write a readonly database - und kein TRUNCATE table in SQLITE

Beitrag lesen

Hm. So könnte es gehen ...

<?php
class RPDO extends PDO {

	protected $arErrors = [];
	protected $pdo = false;
	protected $errorTypes = [
		0,
		E_USER_NOTICE,
		E_USER_WARNING,
		E_USER_ERROR
	];

	function __construct ( $string, $errType = E_USER_ERROR ) {
		
		if ( ! in_array( $errType, $this-> errorTypes ) ) {
			trigger_error( '$errType  has to be ' . implode (' or ', $this ->errorTypes ), E_USER_ERROR );	
		}		
		
		if ( 0 === strpos( $string, 'sqlite:') ) {
			
			$filename = trim( substr ( $string, 7, strlen( $string ) ) );
			$dirname  = dirname( $filename );
			
			if ( '' == $dirname ) {
				$dirname = './';
			}
			
			if ( ! is_writable ( $dirname ) ) {
				$eMsg = 'The directory "' . $dirname . ' has to be writable.';
				$this -> arErrors[] = $eMsg;
			} 

			if ( ! is_file( $filename ) ) {
				$eMsg = 'the file "' . $filename . '" not exits.';
				$this -> arErrors[] = $eMsg;
			} else {
					if ( ! is_readable( $filename ) ) {
					$eMsg = 'the file "' . $filename . '" is not readable';
					$this -> arErrors[] = $eMsg;	
				}
				if ( ! is_writable( $filename ) ) {
					$eMsg = 'the file "' . $filename . '" is not writable';
					$this -> arErrors[] = $eMsg;
				}
			}

            if ( count( $this -> arErrors ) ) {
				if ( 0 != $errType ) {
					trigger_error( implode( ' and ', $this -> arErrors ) , $errType );
				}
				return false;
			}
			
			$this -> pdo = new PDO ( $string );
		}
		return ( $this->pdo );
	}
	
	function hasErrors() {
		if ( count( $this -> arErrors ) ) { 
			return true;
		} else {
			return false;
		}
	}	
	
	function getErrors() {
		if ( $this -> hasErrors ) {
			return $this -> arErrors;
		} else {
			return false;
		}
	}
	
	function getPDO() {
		return ( $this -> pdo ) ;
	}	
}


/* Usage & Tests:
#$rpdo = new RPDO ( 'sqlite: values.sqlite3', 0 );
#$rpdo = new RPDO ( 'sqlite: values.sqlite3', E_USER_NOTICE );
#$rpdo = new RPDO ( 'sqlite: values.sqlite3', E_USER_WARNING );
$rpdo = new RPDO ( 'sqlite: values.sqlite3', E_USER_ERROR );
if (! $rpdo-> hasErrors() ) {
	$pdo  = $rpdo -> getPDO();
	var_dump($pdo);
}
var_dump( $rpdo );
#*/