diff --git a/server/App/App.php b/server/App/App.php new file mode 100644 index 0000000..2bc8c66 --- /dev/null +++ b/server/App/App.php @@ -0,0 +1,26 @@ + db_instance; + } + + public function __construct() { + global $CNF; + $this -> router = new \Kernel\Classes\Router(); + $this -> db_instance = new \Kernel\Classes\DB( $CNF["db"] ); + + include "routes_map.php"; + } + + public function init() { + init_routes_map($this -> router); + echo $this -> router -> routing(); + } + +} \ No newline at end of file diff --git a/server/App/routes_map.php b/server/App/routes_map.php new file mode 100644 index 0000000..20870e3 --- /dev/null +++ b/server/App/routes_map.php @@ -0,0 +1,20 @@ + linking("GET", "signup", function(){ + $uname = $_GET["username"]; + $password = $_GET["password"]; + + $auth = new Auth(); + + if($auth -> user_name_is_exists($uname)) { + return error("signup", "User name already exists"); + } + + $auth -> signup($uname, $password); + + return + }); +} \ No newline at end of file diff --git a/server/Kernel/Classes/Auth.php b/server/Kernel/Classes/Auth.php new file mode 100644 index 0000000..0d16ede --- /dev/null +++ b/server/Kernel/Classes/Auth.php @@ -0,0 +1,37 @@ + get_tablename()}` WHERE `name`='{$uname}'"; + $result = app() -> db() -> query($sql); + $d = $result -> fetch_assoc(); + return $d["COUNT(*)"] ? true : false; + } + + public function signup(String $uname, String $password): bool { + $passhash = sha1($password); + $uname = normalize_input_user_name($uname); + + $user = new User(); + $user -> name = $uname; + $user -> passhash = $passhash; + $user -> create_at = get_create_at_timestamp(); + + return $user -> create_new(); + } + + public function signin() { + + } + + public function signout() { + + } +} \ No newline at end of file diff --git a/server/Kernel/Classes/DB.php b/server/Kernel/Classes/DB.php new file mode 100644 index 0000000..6a777a8 --- /dev/null +++ b/server/Kernel/Classes/DB.php @@ -0,0 +1,37 @@ + db_config = $db_config; + } + + public function connecting() { + $this -> connect_instance = new mysqli( + $this -> db_config["host"], + $this -> db_config["user"], + $this -> db_config["password"], + $this -> db_config["name"] + ); + + if ($this -> connect_instance -> connect_error) { + throw new Exception("Ошибка подключения: " . $this -> connect_instance -> connect_error); + } + } + + public function query(String $sql) { + if(!$this -> connect_instance) { + $this -> connecting(); + } + + return $this -> connect_instance -> query($sql); + } + + public function last_insert_id() { + return $this -> connect_instance -> insert_id; + } +} \ No newline at end of file diff --git a/server/Kernel/Classes/Router.php b/server/Kernel/Classes/Router.php new file mode 100644 index 0000000..d8838af --- /dev/null +++ b/server/Kernel/Classes/Router.php @@ -0,0 +1,59 @@ + [], "POST" => []]; + protected $err_404_handler; + + public function __construct() { + $this -> err_404_handler = function(){ + return "404"; + }; + } + + public function routing() :String { + if(!isset($_GET["action"]) and !isset($_POST["action"])) { + return response_prepared($this -> err_404_handler()); + } + + $actions = [$_GET["action"] ?? "", $_POST["action"] ?? ""]; + + $results = []; + foreach(["GET", "POST"] as $i => $method) { + if(isset($this -> routes[$method][$actions[$i]])) { + $results = array_merge($results, $this -> call_action_handlers($this -> routes[$method][$actions[$i]])); + } + } + + return response_prepared($results); + } + + public function linking(String $method, String $action_name, $action) :bool { + if(!in_array($method, ["GET", "POST"])) { + return throw new Exception("Method `{$method}` no exists"); + } + + if(!isset($this -> routes[$method][$action_name])) { + $this -> routes[$method][$action_name] = []; + } + + $this -> routes[$method][$action_name][] = $action; + + return true; + } + + public function set_err_404($handler) { + return $this -> err_404_handler = $handler; + } + + public function call_action_handlers(Array $handlers) :Array { + $results = []; + + foreach($handlers as $handler) { + $results[] = $handler(); + } + + return $results; + } +} \ No newline at end of file diff --git a/server/Kernel/Entity/Session.php b/server/Kernel/Entity/Session.php new file mode 100644 index 0000000..a1d2fa5 --- /dev/null +++ b/server/Kernel/Entity/Session.php @@ -0,0 +1,20 @@ + set_tablename("session"); + + $this -> set_fields([ + "id", "user_id", "token", "state", "create_at" + ]); + } + + public function force_activity() { + + } +} \ No newline at end of file diff --git a/server/Kernel/Entity/User.php b/server/Kernel/Entity/User.php new file mode 100644 index 0000000..5f0a5fa --- /dev/null +++ b/server/Kernel/Entity/User.php @@ -0,0 +1,21 @@ + set_tablename("user"); + + $this -> set_fields([ + "id", "name", "passhash", "create_at" + ]); + } + + public function init_by_id(int $id) :bool { + + } +} \ No newline at end of file diff --git a/server/Kernel/load.php b/server/Kernel/load.php new file mode 100644 index 0000000..457ea64 --- /dev/null +++ b/server/Kernel/load.php @@ -0,0 +1,18 @@ + [ + "host" => "localhost", + "name" => "flowtask", + "user" => "eugene", + "password" => "root" + ] +]; \ No newline at end of file diff --git a/server/index.php b/server/index.php new file mode 100644 index 0000000..5a4aa2a --- /dev/null +++ b/server/index.php @@ -0,0 +1,7 @@ + init(); diff --git a/server/libs/DataContain.php b/server/libs/DataContain.php new file mode 100644 index 0000000..92b7bcc --- /dev/null +++ b/server/libs/DataContain.php @@ -0,0 +1,32 @@ + data_fields = $fields; + } + + public function __get(String $field_name) { + if(!isset($this -> data[$field_name])) { + throw new Exception("Field `{$field_name}` in User object not found"); + } + + return $this -> data[$field_name]; + } + + public function __set(String $field_name, $field_value) { + if(!in_array($this -> data_fields[$field_name])) { + throw new Exception("Field `{$field_name}` not provided in User object"); + } + + $this -> data[$field_name] = $field_value; + + if(property_exists($this, "changed_fields") and is_array($this -> changed_fields)) { + $this -> changed_fields[] = $field_name; + } + } +} \ No newline at end of file diff --git a/server/libs/DataStorage.php b/server/libs/DataStorage.php new file mode 100644 index 0000000..e617a49 --- /dev/null +++ b/server/libs/DataStorage.php @@ -0,0 +1,71 @@ + id; + } + + protected function set_tablename(String $tablename) { + $this -> tablename = $tablename; + } + + public function get_tablename() :String { + return $this -> tablename; + } + + public function get_chanched_fields() :Array { + return $this -> changed_fields; + } + + public function create_new() :int { + if(count($this -> data_fields) != count($this -> data)) { + return 0; + } + + $fields = "`" . implode("`,`", $this -> data_fields) . "`"; + $values = "'" . implode("','", array_values($this -> data)) . "'"; + + $sql = "INSERT INTO `{$this -> tablename}`({$fields}) VALUES ({$values})"; + $result = app() -> db() -> query($sql); + + $this -> id = app() -> db() -> last_insert_id(); + return $result; + } + + public function init_by_field(String $field, $val) :bool { + $sql = "SELECT * FROM `{$this -> tablename}` WHERE `{$field}`='{$val}'"; + $result = app() -> db() -> query($sql); + + if(!$result or !$result -> num_rows) { + return false; + } + + $this -> data = $result -> fetch_assoc(); + $this -> id = $this -> data["id"]; + + return true; + } + + public function update() :bool { + if(!count($this -> changed_fields)) { + return true; + } + + $fields = []; + + foreach($this -> changed_fields as $field_name) { + $fields[] = "`{$field_name}`='{$this -> data[$field_name]}'"; + } + + $fields = implode(",", $fields); + + $sql = "UPDATE `{$this -> tablename}` SET {$fields} WHERE `id`='{$this -> id}' LIMIT 1"; + return app() -> db() -> query($sql); + } +} \ No newline at end of file diff --git a/server/libs/utils.php b/server/libs/utils.php new file mode 100644 index 0000000..445c587 --- /dev/null +++ b/server/libs/utils.php @@ -0,0 +1,38 @@ + $data + ]; + + @header('Content-Type: application/json'); + + return json_encode($response); +} + +function app() { + global $app; + + return $app; +}; + +function dd($var) { + die(var_dump($var)); +} + +function normalize_input_user_name(String $uname) :String { + return trim(addslashes(strip_tags($uname))); +} + +function get_create_at_timestamp() :String { + return date("Y-m-d H:i:s"); +} + +function error(String $class, String $errtext): Array { + return [ + "error" => [ + "class" => ucfirst($class), + "text" => $errtext + ] + ]; +} \ No newline at end of file