From 74bba5556764e9f77e8b56067df8080dd10fa401 Mon Sep 17 00:00:00 2001 From: xevidos Date: Mon, 27 Jul 2020 13:20:12 -0400 Subject: [PATCH] Continued work on functions needed during install process --- components/data/class.data.php | 58 ++++++++++- components/data/class.filesystemstorage.php | 11 ++- .../data/filesystemstorage/class.data.php | 2 +- .../data/filesystemstorage/class.user.php | 56 +++++++++++ components/initialize/class.initialize.php | 14 +-- components/options/class.options.php | 6 +- components/permissions/class.permissions.php | 24 +++++ components/user/class.user.php | 98 +++++++++++++++++++ install/index.php | 31 +++++- install/install.js | 88 +++++++++++++++-- 10 files changed, 362 insertions(+), 26 deletions(-) create mode 100644 components/data/filesystemstorage/class.user.php create mode 100644 components/permissions/class.permissions.php create mode 100644 components/user/class.user.php diff --git a/components/data/class.data.php b/components/data/class.data.php index 5284159..835054e 100644 --- a/components/data/class.data.php +++ b/components/data/class.data.php @@ -22,9 +22,16 @@ class Data { ); public $connection = null; + public $fss = null; protected static $instance = null; - function __construct() {} + function __construct() { + + if( DBTYPE === "filesystem" ) { + + $this->fss = FileSystemStorage::get_instance(); + } + } public function close() { @@ -140,8 +147,57 @@ class Data { if( is_callable( $query ) ) { + $return = call_user_func( $query ); + } else { + try { + + $connection = $this->connect(); + $statement = $connection->prepare( $query ); + $statement->execute( $bind_variables ); + + switch( $action ) { + + case( 'rowCount' ): + + $return = $statement->rowCount(); + break; + + case( 'fetch' ): + + $return = $statement->fetch( \PDO::FETCH_ASSOC ); + break; + + case( 'fetchAll' ): + + $return = $statement->fetchAll( \PDO::FETCH_ASSOC ); + break; + + case( 'fetchColumn' ): + + $return = $statement->fetchColumn(); + break; + + default: + + $return = $statement->fetchAll( \PDO::FETCH_ASSOC ); + break; + } + } catch( Throwable $error ) { + + $return = $default; + + if( $errors == "message" ) { + + $return = json_encode( array( $error->getMessage() ) ); + } elseif( $errors == "exception" ) { + + throw $error; + } + } + $this->close(); } + return $return; } } diff --git a/components/data/class.filesystemstorage.php b/components/data/class.filesystemstorage.php index ceffd8d..6128d42 100644 --- a/components/data/class.filesystemstorage.php +++ b/components/data/class.filesystemstorage.php @@ -2,6 +2,9 @@ require_once( "filesystemstorage/class.data.php" ); + +require_once( "filesystemstorage/class.user.php" ); + class FileSystemStorage { protected static $instance = null; @@ -83,7 +86,9 @@ class FileSystemStorage { } } else { - $return["data"] = $i->get_data( $fields ); + $return["status"] = "success"; + $return["message"] = ""; + $return["value"] = $i->get_data( $fields ); } } else { @@ -339,16 +344,16 @@ class FileSystemStorage { if( is_callable( $update ) ) { + $data = file_get_contents( $path ); $handle = fopen( $path, "w+" ); if( flock( $handle, LOCK_EX ) ) { - $data = fread( $handle, filesize( $path ) ); $c = unserialize( $data ); try { - $c->set_data( $update( $c->get_data() ) ); + $c->set_data( $update( $c->headers, $c->get_data() ) ); } catch( Throwable $e ) { $return["status"] = "error"; diff --git a/components/data/filesystemstorage/class.data.php b/components/data/filesystemstorage/class.data.php index f1b5a64..8860631 100644 --- a/components/data/filesystemstorage/class.data.php +++ b/components/data/filesystemstorage/class.data.php @@ -40,7 +40,7 @@ class Data { //Add checks to validate Data with headers //$this->data = $data; $return = \Common::get_default_return(); - + return false; } public function set_meta( $meta, $uniques = array() ) { diff --git a/components/data/filesystemstorage/class.user.php b/components/data/filesystemstorage/class.user.php new file mode 100644 index 0000000..888c6b7 --- /dev/null +++ b/components/data/filesystemstorage/class.user.php @@ -0,0 +1,56 @@ +fss->update_data( "users", self::create_user_call ); + echo var_dump( $result ); + return; + } + + public static function create_user_callback( $headers, $d ) { + + $new = array(); + foreach( $headers as $h ) { + + if( isset( $user[$h] ) ) { + + $new[$h] = $user[$h]; + } + } + $d[] = $data; + return $d; + } + + /** + * Return an instance of this class. + * + * @since ${current_version} + * @return object A single instance of this class. + */ + public static function get_instance() { + + if( null == self::$instance ) { + + self::$instance = new self; + } + + return self::$instance; + } + + public static function get_users() { + + global $data; + return $data->fss->get_data( "users" ); + } +} + +?> \ No newline at end of file diff --git a/components/initialize/class.initialize.php b/components/initialize/class.initialize.php index 4a4157d..42578a3 100644 --- a/components/initialize/class.initialize.php +++ b/components/initialize/class.initialize.php @@ -63,23 +63,19 @@ class Initialize { } } - $this->register_constants(); - $this->register_globals(); - $bases = self::BASES; + $this->register_constants(); + foreach( $bases as $base ) { $name = strtolower( $base ); $class = ucfirst( $base ); require_once( COMPONENTS . "/$name/class.$name.php" ); - - if( class_exists( $class ) ) { - - $class::get_instance(); - } } + $this->register_globals(); + if( ! $installing ) { $this->check_extensions(); @@ -219,7 +215,7 @@ class Initialize { global $data; - $data = null; + $data = Data::get_instance(); } } diff --git a/components/options/class.options.php b/components/options/class.options.php index 56a51ee..2841b01 100644 --- a/components/options/class.options.php +++ b/components/options/class.options.php @@ -25,8 +25,9 @@ class Options { public function get_option( $option ) { + global $data; $query = array( - "*" => "SELECT * FROM user_options WHERE name=? AND user=?", + "default" => "SELECT * FROM user_options WHERE name=? AND user=?", "pgsql" => 'SELECT value FROM user_options WHERE name=? AND "user"=?;', "filesystem" => array( "options", @@ -34,8 +35,7 @@ class Options { $option ), ); - $Data = Data::get_instance(); - return $Data->query( $query ); + return $data->query( $query ); } public function update_option( $option, $value ) {} diff --git a/components/permissions/class.permissions.php b/components/permissions/class.permissions.php new file mode 100644 index 0000000..e45b3f4 --- /dev/null +++ b/components/permissions/class.permissions.php @@ -0,0 +1,24 @@ + 0, + "read" => 1, + "write" => 2, + "create" => 4, + "delete" => 8, + "manager" => 16, + "owner" => 32, + "admin" => 64, + ); + + const SYSTEM_LEVELS = array( + + "user" => 32, + "admin" => 64, + ); + + function __construct() {} +} diff --git a/components/user/class.user.php b/components/user/class.user.php new file mode 100644 index 0000000..a7a478b --- /dev/null +++ b/components/user/class.user.php @@ -0,0 +1,98 @@ + "INSERT INTO users( username, password, access, project ) VALUES ( ?, ?, ?, ? );", + "filesystem" => array( "FileSystemStorage\\User", "create_user", $user ), + ); + $bind_vars = array( + $user["username"], + $user["password"], + $user["access"], + null, + ); + $return = $data->query( $query, array(), array() ); + } + return $return; + } + + /** + * Return an instance of this class. + * + * @since ${current_version} + * @return object A single instance of this class. + */ + public static function get_instance() { + + if( null == self::$instance ) { + + self::$instance = new self; + } + + return self::$instance; + } + + function get_user( $identifier ) { + + if( is_int( ) ) { + + } else { + + + } + } + + function get_user_by_id( $id ) {} + + function get_user_by_username( $username ) {} + + function get_users() { + + global $data; + + $query = array( + "default" => "SELECT * FROM users", + "filesystem" => array( "FileSystemStorage\\User", "get_users" ), + ); + return $data->query( $query, array(), array() ); + } +} + +?> \ No newline at end of file diff --git a/install/index.php b/install/index.php index a3a8e82..22a9ed6 100644 --- a/install/index.php +++ b/install/index.php @@ -1,5 +1,5 @@ install( "filesystem" ); + +$u = User::get_instance(); + +echo var_dump( $u->create_user( array( + + "username" => "test", + "password" => "test", + "password1" => "test", + "access" => 0, +) ) ); + +exit(); + $checks_html = ""; $check_paths = Initialize::PATHS; $extensions = Initialize::EXTENSIONS; @@ -110,6 +126,19 @@ if( isset( $_POST["storage"] ) ) { exit( json_encode( $return ) ); } +if( isset( $_POST["username"] ) ) { + + $pass = true; + $return = Common::get_default_return(); + $User = User::get_instance(); + + if( $pass ) { + + + } + exit( json_encode( $return ) ); +} + $components = scandir( COMPONENTS ); unset( $components["."], $components[".."] ); diff --git a/install/install.js b/install/install.js index e1c0ca9..39a4a00 100644 --- a/install/install.js +++ b/install/install.js @@ -38,7 +38,7 @@ init: function() { - let _this = this; + let _ = this; this.d = { @@ -58,35 +58,107 @@ }, dbhost: { - conditions: $.extend( true, {}, _this.dbconditions ), + conditions: $.extend( true, {}, _.dbconditions ), default: "localhost", label: "Database Host: ", type: "text", }, dbname: { - conditions: $.extend( true, {}, _this.dbconditions ), + conditions: $.extend( true, {}, _.dbconditions ), default: "", label: "Database Name: ", type: "text", }, dbuser: { - conditions: $.extend( true, {}, _this.dbconditions ), + conditions: $.extend( true, {}, _.dbconditions ), default: "", label: "Database User: ", type: "text", }, dbpass: { - conditions: $.extend( true, {}, _this.dbconditions ), + conditions: $.extend( true, {}, _.dbconditions ), default: "", label: "Database Password: ", type: "text", }, dbpass1: { - conditions: $.extend( true, {}, _this.dbconditions ), + conditions: $.extend( true, {}, _.dbconditions ), + default: "", + label: "Repeat Password: ", + type: "text", + }, + }; + this.form = new codiad.forms({ + data: _.d, + container: $( "#installation" ), + submit_label: "Check Data Storage Method", + }); + this.form.submit = async function() { + + let _this = this; + let invalid_values; + + if( _this.saving ) { + + return; + } + + _this.saving = true; + let data = await _this.m.get_values(); + let submit = _this.v.controls.find( `[type="submit"]` ); + + submit.attr( "disabled", true ); + submit.text( "Submitting ..." ); + + let response = await codiad.common.ajax( "./index.php", "POST", data ); + + console.log( response ); + + let r = JSON.parse( response ); + + if( r.status == "error" ) { + + codiad.message.error( r.message ); + $( "#data_status" ).html( "

Data Status:
" + r.value ); + } else { + + _.user_setup(); + } + + submit.text( _this.submit_label ); + submit.attr( "disabled", false ); + _this.saving = false; + } + }, + user_setup: function() { + + let _this = this; + + this.d = { + username: { + + default: "", + label: "Username: ", + type: "text", + }, + email: { + + default: "", + label: "Email: ", + type: "email", + }, + password: { + + default: "", + label: "Password: ", + type: "text", + }, + password: { + default: "", label: "Repeat Password: ", type: "text", @@ -95,7 +167,7 @@ this.form = new codiad.forms({ data: _this.d, container: $( "#installation" ), - submit_label: "Check Data Storage Method", + submit_label: "Create User", }); this.form.submit = async function() { @@ -130,6 +202,6 @@ submit.attr( "disabled", false ); _this.saving = false; } - } + }, }; })( this, jQuery ); \ No newline at end of file