2018-10-09 21:30:00 +02:00
|
|
|
<?php
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
require_once( __DIR__ . "/class.sql.conversions.php" );
|
2019-07-02 00:22:33 +02:00
|
|
|
require_once( __DIR__ . "/../permissions/class.permissions.php" );
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
class sql {
|
|
|
|
|
2019-04-11 01:31:28 +02:00
|
|
|
const DB_TYPES = array(
|
|
|
|
|
|
|
|
"MySQL" => "mysql",
|
2019-06-24 22:55:21 +02:00
|
|
|
"PostgreSQL" => "pgsql",
|
2019-05-28 00:18:33 +02:00
|
|
|
//"SQLite" => "sqlite",
|
2019-04-11 01:31:28 +02:00
|
|
|
);
|
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
public $connection = null;
|
2019-04-04 14:18:08 +02:00
|
|
|
public $conversions = null;
|
2019-02-04 22:42:12 +01:00
|
|
|
public $identifier_character = null;
|
|
|
|
protected static $instance = null;
|
2018-10-09 21:30:00 +02:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
2019-04-04 14:18:08 +02:00
|
|
|
$this->conversions = new sql_conversions();
|
2019-02-04 22:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function close() {
|
|
|
|
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function connect() {
|
|
|
|
|
|
|
|
if( $this->connection == null ) {
|
|
|
|
|
|
|
|
$host = DBHOST;
|
|
|
|
$dbname = DBNAME;
|
|
|
|
$dbtype = DBTYPE;
|
|
|
|
$username = DBUSER;
|
|
|
|
$password = DBPASS;
|
2019-07-02 00:22:33 +02:00
|
|
|
$options = array(
|
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
|
);
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
$this->connection = new PDO( "{$dbtype}:host={$host};dbname={$dbname}", $username, $password, $options );
|
2019-02-04 22:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return( $this->connection );
|
|
|
|
}
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
public function create_table( $table_name, $fields=array(), $attributes=array() ) {
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
$query = $this->conversions->table( $table_name, $fields, $attributes );
|
|
|
|
//$this->query( $query, array(), array(), null, "rowCount" );
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:45:42 +02:00
|
|
|
public function create_default_tables() {
|
|
|
|
|
2019-04-12 18:11:27 +02:00
|
|
|
$result = $this->create_tables(
|
2019-04-12 15:45:42 +02:00
|
|
|
array(
|
|
|
|
"active" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"username" => "string",
|
|
|
|
"path" => "text",
|
|
|
|
"position" => "string",
|
|
|
|
"focused" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"path" => array( "not null", "unique" ),
|
|
|
|
"focused" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
2019-07-02 00:22:33 +02:00
|
|
|
"access" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"project" => "int",
|
|
|
|
"user" => "int",
|
|
|
|
"level" => "int",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "not null" ),
|
|
|
|
"user" => array( "not null" ),
|
|
|
|
"level" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
2019-04-12 15:45:42 +02:00
|
|
|
"options" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"value" => "text",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null", "unique" ),
|
|
|
|
"value" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"projects" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"path" => "text",
|
|
|
|
"owner" => "string",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null" ),
|
|
|
|
"path" => array( "not null", "unique" ),
|
|
|
|
"owner" => array( "not null", "unique" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"users" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"first_name" => "string",
|
|
|
|
"last_name" => "string",
|
|
|
|
"username" => "string",
|
|
|
|
"password" => "text",
|
|
|
|
"email" => "string",
|
2019-07-02 00:22:33 +02:00
|
|
|
"project" => "int",
|
2019-04-12 15:45:42 +02:00
|
|
|
"access" => "string",
|
|
|
|
"groups" => "string",
|
|
|
|
"token" => "string",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"password" => array( "not null" ),
|
|
|
|
"access" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"user_options" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"username" => "string",
|
|
|
|
"value" => "text",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null", "unique" ),
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"value" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2019-04-12 18:11:27 +02:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
if( $result === true ) {
|
|
|
|
|
|
|
|
$sql_conversions = new sql_conversions();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$access_query = "INSERT INTO access( project, user, level ) VALUES ";
|
|
|
|
$projects = $this->query( "SELECT id, access FROM projects", array(), array(), "fetchAll", "exception" );
|
|
|
|
$users = $this->query( "SELECT id, username FROM users", array(), array(), "fetchAll", "exception" );
|
|
|
|
$delete = Permissions::LEVELS["delete"];
|
|
|
|
|
|
|
|
foreach( $users as $row => $user ) {
|
|
|
|
|
|
|
|
foreach( $projects as $row => $project ) {
|
|
|
|
|
|
|
|
$access = json_decode( $project["access"], true );
|
|
|
|
if( ! is_array( $access ) || empty( $access ) ) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach( $access as $granted_user ) {
|
|
|
|
|
|
|
|
if( $granted_user == $user["username"] ) {
|
|
|
|
|
|
|
|
$access_query .= "( {$project["id"]}, {$user["id"]}, $delete ),";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $access_query !== "INSERT INTO access( project, user, level ) " ) {
|
|
|
|
|
|
|
|
$result = $this->query( substr( $access_query, 0, -1 ), array(), 0, "rowCount", "exception" );
|
|
|
|
}
|
|
|
|
$result = $this->query( "ALTER TABLE projects DROP COLUMN access", array(), 0, "rowCount" );
|
|
|
|
} catch( Exception $error ) {
|
|
|
|
|
|
|
|
//The access field is not there.
|
|
|
|
//echo var_export( $error->getMessage(), $access_query );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$update_query = "";
|
|
|
|
$projects = $this->query( "SELECT id, path FROM projects", array(), array(), "fetchAll", "exception" );
|
|
|
|
$result = $this->query( "SELECT project FROM users", array(), array(), "fetchAll", "exception" );
|
|
|
|
$convert = false;
|
|
|
|
$delete = Permissions::LEVELS["delete"];
|
|
|
|
|
|
|
|
foreach( $result as $row => $user ) {
|
|
|
|
|
|
|
|
if( ! is_numeric( $user["project"] ) ) {
|
|
|
|
|
|
|
|
$convert = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach( $projects as $row => $project ) {
|
|
|
|
|
|
|
|
if( $project["path"] == $user["project"] ) {
|
|
|
|
|
|
|
|
$update_query .= "UPDATE users SET project={$project["id"]};";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $convert ) {
|
|
|
|
|
|
|
|
//change project to users table
|
|
|
|
$result = $this->query( "ALTER TABLE users DROP COLUMN project", array(), array(), "rowCount", "exception" );
|
|
|
|
$result = $this->query( "ALTER TABLE users ADD COLUMN project " . $sql_conversions->data_types["int"][DBTYPE], array(), array(), "rowCount", "exception" );
|
|
|
|
$result = $this->query( $update_query, array(), array(), "rowCount", "exception" );
|
|
|
|
}
|
|
|
|
} catch( Exception $error ) {
|
|
|
|
|
|
|
|
//echo var_dump( $error->getMessage() );
|
|
|
|
}
|
2019-07-04 07:50:29 +02:00
|
|
|
|
|
|
|
if( DBTYPE === "mysql" || DBTYPE === "pgsql" ) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$projects = $this->query( "ALTER TABLE projects DROP CONSTRAINT path1500owner255;", array(), 0, "rowCount", "exception" );
|
|
|
|
} catch( Exception $error ) {
|
|
|
|
|
|
|
|
//echo var_dump( $error->getMessage() );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$projects = $this->query( "ALTER TABLE active DROP CONSTRAINT username255path1500;", array(), 0, "rowCount", "exception" );
|
|
|
|
} catch( Exception $error ) {
|
|
|
|
|
|
|
|
//echo var_dump( $error->getMessage() );
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 00:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:11:27 +02:00
|
|
|
return $result;
|
2019-04-12 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
public function create_tables( $table ) {
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
/**
|
|
|
|
Tables layout
|
|
|
|
array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"table_name" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"fields" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => "int",
|
|
|
|
"test_field" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => array( "id" ),
|
|
|
|
"test_field" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"table2_name" => array(
|
|
|
|
|
|
|
|
"fields" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => "int",
|
|
|
|
"test_field" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => array( "id" ),
|
|
|
|
"test_field" => array( "not null" ),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
*/
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
try {
|
2019-04-12 18:11:27 +02:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
$query = $this->conversions->tables( $table );
|
|
|
|
$connection = $this->connect();
|
|
|
|
$result = $connection->exec( $query );
|
2019-04-12 18:11:27 +02:00
|
|
|
return true;
|
2019-07-02 00:22:33 +02:00
|
|
|
} catch( exception $error ) {
|
|
|
|
|
|
|
|
return $error->getMessage();
|
2019-04-12 00:26:55 +02:00
|
|
|
}
|
2019-04-04 14:18:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function escape_identifier( $i ) {
|
|
|
|
|
|
|
|
$i = preg_replace('/[^A-Za-z0-9_]+/', '', $i );
|
2019-04-04 19:23:03 +02:00
|
|
|
return $i;
|
2018-10-09 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function is_not_error( $i ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
$return = false;
|
2019-02-04 22:42:12 +01:00
|
|
|
$result = json_decode( $i );
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if ( json_last_error() !== JSON_ERROR_NONE || ( ! $i == NULL && ! $i["status"] == "error" ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function get_instance() {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
// If the single instance hasn't been set, set it now.
|
|
|
|
if ( null == self::$instance ) {
|
|
|
|
|
|
|
|
self::$instance = new self;
|
|
|
|
}
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
return self::$instance;
|
2018-10-09 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 02:58:53 +02:00
|
|
|
public function select( $table, $fields=array(), $where=array() ) {
|
|
|
|
|
|
|
|
$array = $this->conversions->select( $table, $fields, $where );
|
|
|
|
$query = $array[0];
|
|
|
|
$bind_vars = $array[1];
|
|
|
|
$result = $this->query( $query, $bind_vars, array() );
|
|
|
|
//echo var_dump( $query, $bind_vars ) . "<br>";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update( $table, $fields=array(), $where=array() ) {
|
|
|
|
|
|
|
|
$query = $this->conversions->update( $table, $fields, $where );
|
|
|
|
//echo var_dump( $query ) . "<br>";
|
2019-07-02 00:22:33 +02:00
|
|
|
//return $query;
|
2019-04-08 02:58:53 +02:00
|
|
|
}
|
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
public function query( $query, $bind_variables, $default, $action='fetchAll', $errors="default" ) {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
/**
|
|
|
|
* Errors:
|
|
|
|
* default - this value could be anything such as true or foobar
|
|
|
|
* message
|
|
|
|
* exception
|
|
|
|
*/
|
|
|
|
|
|
|
|
try {
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
$connection = $this->connect();
|
|
|
|
$statement = $connection->prepare( $query );
|
|
|
|
$statement->execute( $bind_variables );
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
switch( $action ) {
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
case( 'rowCount' ):
|
|
|
|
|
|
|
|
$return = $statement->rowCount();
|
|
|
|
break;
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 22:46:32 +02:00
|
|
|
case( 'fetch' ):
|
|
|
|
|
|
|
|
$return = $statement->fetch( \PDO::FETCH_ASSOC );
|
|
|
|
break;
|
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
case( 'fetchAll' ):
|
|
|
|
|
|
|
|
$return = $statement->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
break;
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
case( 'fetchColumn' ):
|
|
|
|
|
|
|
|
$return = $statement->fetchColumn();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
$return = $statement->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch( exception $error ) {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $default;
|
2019-04-15 20:38:54 +02:00
|
|
|
|
2019-07-02 00:22:33 +02:00
|
|
|
if( $errors == "message" ) {
|
|
|
|
|
|
|
|
$return = json_encode( array( $error->getMessage() ) );
|
|
|
|
} elseif( $errors == "exception" ) {
|
|
|
|
|
|
|
|
throw $error;
|
|
|
|
}
|
2019-04-15 20:38:54 +02:00
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
$this->close();
|
2018-10-09 21:30:00 +02:00
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|