PATH:
root
/
paleypartnerlib
<?php $dbServer = defined("DB_SERVER") ? DB_SERVER : "localhost"; // function connection() // { // $mysql_server = "localhost"; // $mysql_admin = "root"; // $mysql_pass = ""; // mysql_connect($mysql_server, $mysql_admin, $mysql_pass) // or die('Brak połączenia z serwerem MySQL.'); // mysql_select_db("kaczkastudio_amplex") // or die('Błąd wyboru bazy danych.'); // mysql_query('SET NAMES utf8'); // mysql_query('SET CHARACTER_SET utf8_polish_ci'); // } class db { public $execute = true; public $log = false; private $mode; private $server; private $admin; private $pass; private $db; private $options; private $lastQuery = ""; private $conn; public function __construct($mode = "mysql", &$server = null, $admin = null, $pass = null, $db = null, $options = null) { if (defined("DB_MODE")) $this->mode = DB_MODE; if ($mode != null) { $this->mode = $mode; } if (defined("DB_SERVER")) $this->server = DB_SERVER; if ($server != null) { $this->server = $server; } if (defined("DB_USER")) $this->admin = DB_USER; if ($admin != null) { $this->admin = $admin; } if (defined("DB_PASS")) $this->pass = DB_PASS; if ($pass != null) { $this->pass = $pass; } if (defined("DB_NAME")) $this->db = DB_NAME; if ($db != null) { $this->db = $db; } if ($options != null) { $this->options = $options; } switch ($mode) { case "pdo": $pdo = new PDODB($this->server, $this->admin, $this->pass, $this->db, $this->options); $this->conn = $pdo; break; case "mysqli": $this->conn = mysqli_connect($this->server, $this->admin, $this->pass, $this->db); $this->conn->set_charset("utf8"); if (!$this->conn) { die("Unable to connect to MySQL."); } break; default: mysql_connect($this->server, $this->admin, $this->pass) or die('Brak połączenia z serwerem MySQL.'); mysql_select_db($this->db) or die('Błąd wyboru bazy danych.'); mysql_query('SET NAMES utf8'); mysql_query('SET CHARACTER_SET utf8_polish_ci'); break; } return $this->conn; } public function getLastQuery() { return $this->lastQuery; } public function escape($str) { switch ($this->mode) { case "mysqli": $str = $this->conn->mysqli_real_escape_string($str); break; case "mysql": $str = mysql_real_escape_string($str); break; } //$str = htmlspecialchars($str); return $str; } public function clarify(&$fields, &$values = null) { if (gettype($fields) == "string" && $values != null) { $field = $fields; $fields = array($field => $this->escape($values)); } if ((gettype($fields) == "array" && isset($fields[0])) && (gettype($values == "array") && isset($values[0]))) { $tmp = []; foreach ($fields as $key => $value) { $tmp[$value] = $values[$key]; } unset($values); $fields = $tmp; unset($tmp); } foreach ($fields as $key => $value) { $fields[$key] = $this->escape($value); } } public function quotes($fields) { if (gettype($fields) == "array") { $quotes = function (&$val, $key) { $val = str_replace("'", "''", $val); return $val; }; array_walk($fields, $quotes); } else { $fields = str_replace("'", "''", $fields); } return $fields; } public function brackets($arr, $char = "'", $bracket = "()", $names = false) { $txt = (isset($bracket[0]) ? $bracket[0] : "") . $char . implode($char . ", " . $char, $arr) . $char . (isset($bracket[0]) ? substr($bracket, -1) : ""); return $names == true ? str_replace(".", $char . "." . $char, $txt) : $txt; } //PDO public function pdoInsert($table, $options = NULL, $fields, $values = NULL) { $fields = explode(',', $fields); $values = explode(',', $values); //Sprawdzenie czy liczba $fields == $values if (count($fields) == count($values)) { $query = "INSERT INTO $table "; $i = 0; foreach ($fields as $key => $field) { if ($i == 0) { $query .= "($field"; $i++;} else { $query .= ", "; $query .= "$field"; } } $query .= ")"; $query .= " VALUES "; $j = 0; foreach ($values as $key => $value) { if ($j == 0) { $query .= "('$value'"; $j++;} else { $query .= ", "; $query .= "'$value'"; } } $query .= ")"; $query .= " $options"; $query = $this->conn->query($query); $query = $this->conn->resultSet(); return $query; } else { echo "Niepoprawna ilość pól"; exit(); } } //PDO tested public function insert($table, $options = null, $fields, $values = null) { switch ($this->mode) { case "pdo": $this->clarify($fields, $values); $char2 = "'"; $query = "INSERT INTO " . $char . $table . $char . " " . $this->brackets(array_keys($fields), $char) . " VALUES " . $this->brackets($fields, $char2); $query .= ($options != null ? $options : ""); echo "<br> $query <br>"; if ($this->execute === true) { $query = $this->conn->query($query); $query = $this->conn->resultSet(); return $query; } else { return $query; } break; default: $this->clarify($fields, $values); $char = $this->mode == "mysql" ? "`" : ""; $char2 = $this->mode == "mysql" ? "'" : ""; $query = "INSERT INTO " . $char . $table . $char . " " . $this->brackets(array_keys($fields), $char) . " VALUES " . $this->brackets($fields, $char2); $query .= ($options != null ? $options : ""); if ($this->execute === true) { $this->query($query . ";"); } else { return $query; } } } //PDO public function pdoUpdate($table, $options = null, $fields, $values = null) { //Rodzielenie stringów na tablice if (gettype($fields) !== 'array') $fields = explode(',', $fields); if (gettype($values) !== 'array') $values = explode(',', $values); //Sprawdzenie czy liczba $fields == $values if (count($fields) == count($values)) { $options2 = []; $query = "UPDATE $table SET "; foreach ($fields as $key => $field) array_push($options2, "`{$field}` = '{$values[$key]}'"); $options2 = implode(", ", $options2); $query .= " $options2 $options"; $query = $this->conn->query($query); $query = $this->conn->resultSet(); return $query; } else { print_r($fields); print_r($values); echo "Niepoprawna ilość pól"; exit(); } } //PDO tested public function update($table, $options = null, $fields, $values = null) { switch ($this->mode) { case "pdo": $this->clarify($fields, $values); $char2 = "'"; $query = "UPDATE " . $char . $table . $char . " SET "; $i = 0; foreach ($fields as $key => $value) { $query .= ($i > 0 ? ", " : "") . $char . $key . $char . " = " . $char2 . addslashes($value) . $char2; $i++; } $query .= " " . ($options != null ? $options : ""); if ($this->execute === true) { $query = $this->query($query); // $query = $this->conn->resultSet(); return $query; } else { return $query; } default: $this->clarify($fields, $values); $char = $this->mode == "mysql" ? "`" : ""; $char2 = $this->mode == "mysql" ? "'" : ""; $query = "UPDATE " . $char . $table . $char . " SET "; $i = 0; foreach ($fields as $key => $value) { $query .= ($i > 0 ? ", " : "") . $char . $key . $char . " = " . $char2 . $value . $char2; $i++; } $query .= " " . ($options != null ? $options : ""); if ($this->execute === true) { $this->query($query . ";"); } else { return $query; } break; } } //PDO do test public function insertUpdate($table, $options = null, $fields, $values = null) { $this->clarify($fields, $values); $char = $this->mode == "mysql" ? "`" : ""; $char2 = $this->mode == "mysql" ? "'" : ""; $query = "INSERT INTO " . $char . $table . $char . " " . $this->brackets(array_keys($fields), $char) . " VALUES " . $this->brackets($fields, $char2) . " ON DUPLICATE KEY UPDATE"; $i = 0; foreach ($fields as $key => $value) { $query .= ($i > 0 ? "," : "") . " `" . $key . "` = VALUES(`" . $key . "`)"; $i++; } $query .= ($options != null ? " " . $options : ""); switch($this->mode) { case "pdo": if ($this->execute === true) { $query = $this->conn->query($query); $query = $this->conn->resultSet(); return $query; } else { return $query; } default: if ($this->execute === true) { $this->query($query . ";"); } else { return $query; } } } //PDO public function delete($table, $statement) { switch($this->mode) { case 'pdo': $query = "DELETE FROM $table $statement"; $query = $this->conn->query($query); $query = $this->conn->execute(); return $query; break; default: $query = "DELETE FROM `{$table}` "; $query .= $this->quotes($statement); if ($this->execute === true) { $this->query($query . ";"); } else { return $query; } } } //PDO public function select($table, $fields = "*", $options = null, $statement = null) { switch($this->mode) { case 'pdo': if ($fields == "" || $fields == null) {$fields = "*";} $options != null ? $mode = 1 : $mode = null; $char = $this->mode == "mysql" ? "`" : ""; $char2 = $this->mode == "mysql" ? "'" : ""; if (gettype($table) != "string") { $table = $this->brackets($table, $char, ""); $table = $char . $table . $char; } if (gettype($fields) != "string") { $fields = $this->brackets($fields, $char, ""); } $query = "SELECT $fields FROM $table $statement"; $query = $this->conn->query($query); $query = $this->conn->resultset(); return $query; break; default: if ($fields == "" || $fields == null) { $fields = "*"; } $char = $this->mode == "mysql" ? "`" : ""; $char2 = $this->mode == "mysql" ? "'" : ""; if (gettype($table) != "string") { $table = $this->brackets($table, $char, ""); $table = $char . $table . $char; } if (gettype($fields) != "string") { $fields = $this->brackets($fields, $char, ""); } $query = "SELECT " . $fields . " FROM {$table}" . ($statement != null ? " " . $statement : ""); if ($this->execute === true) { return $this->query($query . ";"); } else { return $query; } } } //PDO public function fetchAll($table, $fields = "*", $options = null, $statement = null) { if ($fields == "" || $fields == null) {$fields = "*";} $options != null ? $mode = 1 : $mode = null; $query = "SELECT $fields FROM $table $statement"; $query = $this->conn->query($query); $query = $this->conn->resultset(); return $query; } //PDO public function simpleSelect($table, $fields = "*", $options = null, $statement = null) { if ($fields == "" || $fields == null) {$fields = "*";} $options != null ? $mode = 1 : $mode = null; $query = "SELECT $fields FROM $table $statement"; $query = $this->conn->query($query); $query = $this->conn->fetchBoth(); return $query; } //PDO public function swap($table, $valFrom, $valTo, $swapCol = "id", $fields = "*") { if ($valFrom == $valTo) return false; if ($fields == "") $fields = "*"; if (gettype($fields) == "string" && $fields != "*") $fields = explode(",", $fields); if ($fields == "*") { $fields = $this->getColumnNames($table); if (array_search($swapCol, $fields)) unset($fields[$swapCol]); } foreach ($fields as $key => $val) { if ($val == $swapCol) { unset($fields[$key]); break; } } $query = "UPDATE `{$table}` `tab1` INNER JOIN `{$table}` `tab2` ON (`tab1`.`{$swapCol}`, `tab2`.`{$swapCol}`) IN (('{$valFrom}', '{$valTo}'), ('{$valFrom}', '{$valTo}')) SET "; $colon = ""; foreach ($fields as $val) { $query .= $colon . "`tab1`.`$val` = `tab2`.`$val`, `tab2`.`$val` = `tab1`.`$val`"; if ($colon == "") $colon = ", "; } $this->query($query); } //PDO public function query($query) { if ($this->log === true) { echo "<pre>" . $query . "</pre>"; } $this->lastQuery = $query; switch ($this->mode) { case "pdo": $result = $this->conn->query($query); $result = $this->conn->execute(); $result = $this->conn->resultset(); break; case "mysqli": $query = $this->db->real_escape_string($query); $result = $this->db->query(); break; case "mysql": $result = mysql_query($query); if (gettype($result) == "resource") { $tmp = []; while ($resultArray = mysql_fetch_array($result)) { array_push($tmp, $resultArray); } $result = $tmp; //if (isset($result[0][0]) && count($result[0]) == 2) $result = $result[0]; unset($tmp); unset($resultArray); } break; } return $result; } //TODO function copyTables($oldTable = "", $newTables = "", $ignore = false, $clean = false) { switch ($this->mode) { case "pdo": if (gettype($newTables) == "string") { $newTables = explode(",", str_replace(", ", ",", $newTables)); } foreach ($newTables as $item) { if ($clean == true && $this->tableExists($item)) { $this->conn->query("DROP TABLE $item"); } $this->query("CREATE TABLE IF NOT EXISTS $item LIKE $oldTable"); $this->query("INSERT " . ($ignore == true ? "IGNORE " : "") . " $item SELECT * FROM $oldTable"); } return $this; default: if (gettype($newTables) == "string") { $newTables = explode(",", str_replace(", ", ",", $newTables)); } foreach ($newTables as $item) { if ($clean == true && $this->tableExists($item)) { $this->db->query("DROP TABLE `" . $item . "`"); } $this->query("CREATE TABLE IF NOT EXISTS `" . $item . "` LIKE `" . $oldTable . "`"); $this->query("INSERT " . ($ignore == true ? "IGNORE " : "") . "`" . $item . "` SELECT * FROM `" . $oldTable . "`"); } return $this; } } public function deleteTables($tables = "") { switch($this->mode) { case "pdo": print_r ($tables); if (gettype($tables) == "string") { $tables = explode(",", str_replace(", ", ",", $tables)); } print_r ($tables); foreach ($tables as $table) { $this->query("DROP TABLE $table"); } break; default: if (gettype($tables) == "string") { $tables = explode(",", str_replace(", ", ",", $tables)); } $this->query("DROP TABLE " . $this->brackets($tables) . ";"); } } //PDO public function getColumnNames($table, $database = null) { if ($database == null) { $database = $this->db; } switch($this->mode) { case "pdo": $result = $this->select("INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME", null, "WHERE TABLE_NAME= '$table' AND TABLE_SCHEMA= '$database' "); break; default: $result = $this->query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='{$database}' AND `TABLE_NAME`='{$table}';"); break; } $tmp = []; foreach ($result as $key => $value) { array_push($tmp, $value['COLUMN_NAME']); } $result = $tmp; unset($tmp); return $result; } //PDO tested public function tableExists($table, $database = null) { if ($database == null) { $database = $this->db; } switch($this->mode) { case "pdo": $result = $this->select("INFORMATION_SCHEMA.TABLES", "*", null ,"WHERE TABLE_NAME= '$table' AND TABLE_SCHEMA = '$database' LIMIT 1"); return count($result) > 0 ? true : false; break; default: $result = $this->query("SELECT * FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`='{$database}' AND `TABLE_NAME`='{$table}' LIMIT 1;"); return count($result) > 0 ? true : false; break; } } public function importFull($sql) { // IMPORTANT! IT WILL CLEAR DATABASE, THEN IMPORT! $command = "--host={$this->server} --user={$this->admin} --password={$this->pass}"; exec("mysql {$command} {$this->db} < \"{$sql}\""); return true; } public function exportFull($file = NULL) { $command = "--host={$this->server} --user={$this->admin} --password={$this->pass}"; if (isset($this->port)) $command .= " --port={$this->port}"; if (isset($file) && $file != "") { passthru("mysqldump {$command} {$this->db} --lock-tables=false > \"{$file}\"", $output); return true; } else $output = shell_exec("mysqldump {$command} {$this->db}"); return $output; } public function exportFullCmd($file = NULL) { $command = "--host={$this->server} --user={$this->admin} --password={$this->pass}"; if (isset($this->port)) $command .= " --port={$this->port}"; if (isset($file) && $file != "") { $command = "mysqldump {$command} {$this->db} --lock-tables=false > \"{$file}\""; } else $command = "mysqldump {$command} {$this->db}"; return $command; } public function showParam() { switch ($this->mode) { case "pdo": echo "__PDO"; break; case "mysqli": echo "__Mysqli"; break; case "mysql": echo "__mysql"; break; } } } /** * Klasa podstawowa, po której dziedziczy klasa DBFunctions */ class PDODB { private $pdo; private $mode; private $server; private $admin; private $pass; private $db; private $sQuery; private $settings; private $bConnected = false; private $log; private $parameters; private $stmt; public function __construct($server = null, $admin = null, $pass = null, $db = null, $options = null) { if ($server != null) { $this->server = $server; } if ($admin != null) { $this->admin = $admin; } if ($pass != null) { $this->pass = $pass; } if ($db != null) { $this->db = $db; } $this->Connect(); $this->parameters = array(); } private function Connect() { $dsn = 'mysql:dbname=' . $this->db . ';host=' . $this->server . ''; try { # Read settings from INI file, set UTF8 $this->pdo = new PDO($dsn, $this->admin, $this->pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); # We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # Disable emulation of prepared statements, use REAL prepared statements instead. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # Connection succeeded, set the boolean to true. $this->bConnected = true; } catch (PDOException $e) { # Write into log echo $this->ExceptionLog($e->getMessage()); die(); } } public function CloseConnection() { $this->pdo = null; } private function Init($query, $parameters = "") { # Connect to database if (!$this->bConnected) {$this->Connect();} try { # Prepare query $this->sQuery = $this->pdo->prepare($query); # Add parameters to the parameter array $this->bindMore($parameters); # Bind parameters if (!empty($this->parameters)) { foreach ($this->parameters as $param) { $parameters = explode("\x7F", $param); $this->sQuery->bindParam($parameters[0], $parameters[1]); } } # Execute SQL $this->succes = $this->sQuery->execute(); } catch (PDOException $e) { # Write into log and display Exception echo $this->ExceptionLog($e->getMessage(), $query); die(); } # Reset the parameters $this->parameters = array(); } private function ExceptionLog($message, $sql = "") { $exception = 'Unhandled Exception. <br />'; $exception .= $message; $exception .= "<br /> You can find the error back in the log."; $errorcode = $message; if (!empty($sql)) { # Add the Raw SQL to the Log $message .= "\r\nRaw SQL : " . $sql; } return $errorcode; } /** * Przekazywanie bindValue przez warość */ public function bind($param, $value, $type = null) { if (is_null($type)) { switch (true) { case is_int($value): $type = PDO::PARAM_INT; break; case is_bool($value): $type = PDO::PARAM_BOOL; break; case is_null($value): $type = PDO::PARAM_NULL; break; default: $type = PDO::PARAM_STR; } } $this->stmt->bindValue($param, $value, $type); } public function single() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_ASSOC); } public function query($query) { try { $this->stmt = $this->pdo->prepare($query); } catch (PDOException $e) { echo "coś poszło nie tak Function query" . $e; } } public function execute() { return $this->stmt->execute(); } public function resultset() { $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_ASSOC); } public function fetchNum() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_NUM); } public function fetchBoth() { $this->execute(); return $this->stmt->fetch(PDO::FETCH_BOTH); } public function rowCount() { return $this->stmt->rowCount(); } public function lastInsertId() { return $this->pdo->lastInsertId(); } public function endTransaction() { return $this->pdo->commit(); } } // __construct($mode = "mysql", $server = NULL, $admin = NULL, $pass = NULL, $db = NULL, $options = NULL) $db = new db("pdo"); $db->query("DELETE FROM `users` WHERE (`id` < 0)");
[-] _includes.php
[edit]
[+]
..
[-] connection.php
[edit]
[+]
DBService_
[-] Dokumentacja
[edit]
[-] funkcje.php
[edit]
[-] includes.php
[edit]
[-] classes.php
[edit]