Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
343 righe
9.0 KiB
343 righe
9.0 KiB
<? |
|
class myPDO extends PDO |
|
{ |
|
private $sql; |
|
private $values; |
|
public $debug = false; |
|
public $result; |
|
public $pdoEnte; |
|
private $lastConnection; |
|
public $switch = true; |
|
|
|
function __destruct() { |
|
gc_collect_cycles(); |
|
} |
|
|
|
public function __construct($host,$user,$pass,$db,$socket = null) |
|
{ |
|
if (empty($host) || empty($user) || empty($pass) || empty($db)) { |
|
|
|
throw new Exception("Errore di configurazione Database!"); |
|
|
|
} else { |
|
|
|
try { |
|
|
|
$dns = "mysql:host={$host};dbname={$db};"; |
|
if(! empty($socket)) $dns = "mysql:unix_socket={$socket};dbname={$db};"; |
|
$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']; |
|
parent::__construct($dns, $user, $pass, $options); |
|
|
|
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE); |
|
|
|
} catch (PDOException $e) { |
|
|
|
if ($this->debug) { |
|
|
|
throw new Exception($e->getMessage(), 1); |
|
|
|
} else { |
|
|
|
throw new Exception("Errore di connessione al server" . (DEVELOP_ENV ? " - " . $e->getMessage() : ""), 1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private function prepareQuery($query, $options) |
|
{ |
|
return parent::prepare($query,$options); |
|
} |
|
|
|
private function runQuery($statement) |
|
{ |
|
return parent::query($statement); |
|
} |
|
|
|
private function useEntePDO($sql) |
|
{ |
|
if ($this->switch) { |
|
if (!empty($this->pdoEnte)){ |
|
$searchSql = trim($sql); |
|
$searchSql = str_replace("SHOW ", "SHOW_", $searchSql); |
|
$searchSql = str_replace("ALTER ", "ALTER_", $searchSql); |
|
$splitSQL = preg_split('/\s+/', $searchSql); |
|
$tableName = ""; |
|
|
|
switch(strtoupper($splitSQL[0])) { |
|
case "SHOW_TABLES": |
|
foreach($splitSQL as $index => $entry) { |
|
if(strtoupper($entry) === "LIKE") { |
|
$tableName = $splitSQL[$index+1]; |
|
break; |
|
} |
|
} |
|
break; |
|
case "SHOW_FIELDS": |
|
case "SHOW_COLUMNS": |
|
case "SELECT": |
|
case "DELETE": |
|
foreach($splitSQL as $index => $entry) { |
|
if(strtoupper($entry) === "FROM") { |
|
$tableName = $splitSQL[$index+1]; |
|
break; |
|
} |
|
} |
|
break; |
|
case "UPDATE": |
|
case "ALTER_TABLE": |
|
case "DESCRIBE": |
|
case "EXPLAIN": |
|
foreach($splitSQL as $index => $entry) { |
|
if($index==0) continue; |
|
if(!in_array(strtoupper($entry), ["LOW_PRIORITY", "IGNORE"])) { |
|
$tableName = $entry; |
|
break; |
|
} |
|
} |
|
break; |
|
case "INSERT": |
|
case "REPLACE": |
|
foreach($splitSQL as $index => $entry) { |
|
if(strtoupper($entry) === "INTO") { |
|
$tableName = $splitSQL[$index+1]; |
|
break; |
|
} |
|
} |
|
break; |
|
} |
|
if(empty($tableName)) { |
|
if(DEVELOP_ENV) { |
|
dd("Impossibile fare il parsing della query $sql"); |
|
} |
|
return false; |
|
} |
|
$tableName = strtolower(trim($tableName, "\"'`;")); |
|
if (stripos($tableName," b_log ") === false) { |
|
if (empty($_SESSION["tabelleEnte"])) { |
|
global $config; |
|
$tabelleEnte = jsonToArray($config["first_level"]."/enti/enteTables.json"); |
|
$_SESSION["tabelleEnte"] = $tabelleEnte; |
|
} else { |
|
$tabelleEnte = $_SESSION["tabelleEnte"]; |
|
} |
|
foreach($tabelleEnte as $tabella) { |
|
if (stripos($tableName,$tabella) !== false) { |
|
return true; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
private function lastInsertWithConnection($name) { |
|
return parent::lastInsertId($name); |
|
} |
|
|
|
/** |
|
* Undocumented function |
|
* |
|
* @param string|null $name |
|
* @return string|false |
|
*/ |
|
public function lastInsertId($name = null) |
|
{ |
|
return $this->lastConnection->lastInsertWithConnection($name); |
|
} |
|
|
|
/** |
|
* Prepare a SQL statement |
|
* |
|
* @param string $query |
|
* @param ?array $options |
|
* @return PDOStatement|false |
|
*/ |
|
public function prepare($query, $options = []) |
|
{ |
|
$this->lastConnection = $this; |
|
if ($this->useEntePDO($query)) { |
|
$this->lastConnection = $this->pdoEnte; |
|
return $this->pdoEnte->prepareQuery($query,$options); |
|
} |
|
return $this->prepareQuery($query,$options); |
|
} |
|
|
|
public function query($statement, $mode = PDO::ATTR_DEFAULT_FETCH_MODE, ...$fetch_mode_args) |
|
{ |
|
$this->lastConnection = $this; |
|
if ($this->useEntePDO($statement)) { |
|
$this->lastConnection = $this->pdoEnte; |
|
return $this->pdoEnte->runQuery($statement); |
|
} |
|
return $this->runQuery($statement); |
|
} |
|
|
|
public function setConnessioneEnte($codice_ente) |
|
{ |
|
global $config; |
|
if (!empty($config["prefix_db"])) { |
|
if (file_exists($config["first_level"]."/enti/enteTables.json")) { |
|
$this->pdoEnte = new myPDO($config["db_host"],$config["db_user"],$config["db_pass"],"{$config["prefix_db"]}_ente{$codice_ente}", $config['socket']); |
|
} else { |
|
throw new Exception("Configurazione configurazione tabelle ente non trovata", 1); |
|
} |
|
} else { |
|
throw new Exception("Configurazione prefix errata", 1); |
|
} |
|
} |
|
|
|
|
|
/** |
|
* Esegue una query sql parametrizzata |
|
* @param string $sql query sql parametrizzata |
|
* @param array $bind array associativo dei parametri della query |
|
* @return PDOStatement|false risultato della query |
|
*/ |
|
public function go($sql, $bind = array()) |
|
{ |
|
$this->sql = $sql; |
|
$this->values = $bind; |
|
try { |
|
$bind = $this->checkBind($bind); |
|
$sth = $this->prepare($this->sql); |
|
// $this->result = $sth->execute($bind); |
|
foreach ($bind as $key => $value) { |
|
if (strpos($sql, $key) === FALSE) { continue; } |
|
$type = gettype($value); |
|
$binded = false; |
|
switch ($type) { |
|
case 'boolean': |
|
$sth->bindValue($key, $value, PDO::PARAM_BOOL); |
|
$binded = true; |
|
break; |
|
case 'integer': |
|
$sth->bindValue($key, $value, PDO::PARAM_INT); |
|
$binded = true; |
|
break; |
|
case 'NULL': |
|
$sth->bindValue($key, null, PDO::PARAM_INT); |
|
$binded = true; |
|
break; |
|
default: |
|
if (empty($value)) { |
|
$sth->bindValue($key, null, PDO::PARAM_INT); |
|
$binded = true; |
|
break; |
|
} |
|
$sth->bindValue($key, $value, PDO::PARAM_STR); |
|
$binded = true; |
|
break; |
|
} |
|
$sth->bindValue($key, $value); |
|
} |
|
$this->result = $sth->execute(); |
|
} catch (PDOException $e) { |
|
$this->result = FALSE; |
|
if ($this->debug) { |
|
if(isset($sth)) $sth->debugDumpParams(); |
|
echo "Codice: " . $e->getCode() . " --- " . $e->getMessage().PHP_EOL; |
|
$err = "QUERYSTRING: ".$this->getSQL().PHP_EOL; |
|
$err .= "ERROR: ".PHP_EOL.$e->getMessage().PHP_EOL; |
|
echo nl2br($err); |
|
} |
|
} |
|
return $sth; |
|
} |
|
|
|
/** |
|
* [getSQL description] |
|
* @param string $sql |
|
* @param array $values |
|
* @return string |
|
*/ |
|
public function getSQL($sql = "", $values = array()) |
|
{ |
|
$sql = $sql != "" ? $sql : $this->sql; |
|
$values = !empty($values) ? $this->checkBind($values) : $this->checkBind($this->values); |
|
if (sizeof($values) > 0) { |
|
$underscored_key = preg_grep("/([a-zA-Z][\w]*)+_/", array_keys($values)); |
|
foreach ($underscored_key as $key) { |
|
$sql = str_replace($key, $this->paramToString($values[$key]), $sql); |
|
} |
|
foreach ($values as $key => $value) { |
|
$sql = str_replace($key, $this->paramToString($value), $sql); |
|
} |
|
} |
|
return $sql; |
|
} |
|
|
|
/** |
|
* Converte un parametro bind a stringa per essere inserito in getSQL |
|
* |
|
* @param mixed $value |
|
* @return string |
|
*/ |
|
public function paramToString($value) : string { |
|
$type = gettype($value); |
|
switch ($type) { |
|
case 'boolean': |
|
return $value ? "1" : "0"; |
|
break; |
|
case 'integer': |
|
return "$value"; |
|
break; |
|
case 'NULL': |
|
return "NULL"; |
|
break; |
|
} |
|
return $this->quote($value); |
|
} |
|
|
|
/** |
|
* [checkBind description] |
|
* @param array $bind |
|
* @return array |
|
*/ |
|
private function checkBind($bind) |
|
{ |
|
foreach ($bind as $key => $value) { |
|
if (substr($key, 0,1) != ":") { |
|
unset($bind[$key]); |
|
$bind[":".$key] = $value; |
|
} |
|
} |
|
return $bind; |
|
} |
|
|
|
/** |
|
* @return mixed |
|
*/ |
|
public function getResult() |
|
{ |
|
return $this->result; |
|
} |
|
|
|
/** |
|
* @param mixed $result |
|
* |
|
* @return self |
|
*/ |
|
public function setResult($result) |
|
{ |
|
$this->result = $result; |
|
return $this; |
|
} |
|
|
|
/** |
|
* Valida se una stringa contiene un nome di campo o tabella valido |
|
* |
|
* @param string $field |
|
* @return boolean |
|
*/ |
|
public static function validFieldName(string $field) : bool { |
|
return preg_match("/^[0-9a-zA-Z\\\$_\.]{1,64}$/", $field) === 1; |
|
} |
|
|
|
}
|
|
|