Skip to content

Commit

Permalink
Merge pull request #3 from KIUT-Projects/main
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
uzsoftic authored Apr 16, 2023
2 parents d927959 + deba401 commit 359058d
Show file tree
Hide file tree
Showing 75 changed files with 4,230 additions and 104 deletions.
8 changes: 6 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
APP_URL="http://127.0.0.1"
APP_URL="http://127.0.0.1:8000"
APP_LANGUAGE="ru"
APP_DEBUG="false"

APP_SERVE="127.0.0.1"
APP_SERVE_PORT="8000"

DB_CONN="mysql"
DB_HOST="127.0.0.1"
DB_PORT="3306"
DB_NAME="crud_database"
DB_USER="root"
DB_PASS=""
DB_PASS=""
14 changes: 12 additions & 2 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# AddDefaultCharset UTF-8
# RewriteRule ^(.+)$ index.php?q=$1 [L,QSA]

## RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.pdf)$
## RewriteRule (.*) index.php [QSA,L]

RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.pdf)$
RewriteRule (.*) index.php [QSA,L]
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]
5 changes: 4 additions & 1 deletion app/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
namespace App\Controller;

class AdminController{

public function index(){
$users = db("SELECT * FROM users");
return view('admin.index', ['users' => $users]);
}
}
26 changes: 26 additions & 0 deletions app/Controllers/DebugController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Controllers;

class DebugController
{
public function error_400(){
return view('error.400');
}
public function error_401(){
return view('error.401');
}
public function error_403(){
return view('error.403');
}
public function error_404(){
return view('error.404');
}
public function error_500(){
return view('error.500');
}
public function error_503(){
return view('error.503');
}

}
14 changes: 13 additions & 1 deletion app/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
class HomeController{
public function index(){
$users = db("SELECT * FROM users");
return view('public/index.php', ['users' => $users]);
return view('public.index', ['users' => $users]);
}

public function login(){
return view('auth.login');
}

public function register(){
return view('auth.register');
}

public function logout(){

}
}
12 changes: 0 additions & 12 deletions app/Services/DB.php

This file was deleted.

10 changes: 10 additions & 0 deletions app/Services/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Services;

class Database
{
public function query($query){
//
}
}
13 changes: 9 additions & 4 deletions app/Services/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
class Log
{

public function info(){
public function info($message){
self::write('info', $message);
}

public function warning($message){
self::write('warning', $message);
}
public function warning(){


private function write($level, $message){
//
}

}
}
139 changes: 77 additions & 62 deletions app/Services/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,118 @@

class Router{
// GET POST PUT PATCH DELETE methods
public static function get($route, $path_to_include){
//return 1;
if( $_SERVER['REQUEST_METHOD'] == 'GET' ){ self::route($route, $path_to_include); }
public static function get($route, $controller, $path_to_include = NULL){
if($_SERVER['REQUEST_METHOD'] == 'GET'){
self::route($route, $controller, $path_to_include);
}
}
public static function post($route, $controller, $path_to_include = NULL){
if($_SERVER['REQUEST_METHOD'] == 'POST'){
self::route($route, $controller, $path_to_include);
}
}
public static function post($route, $path_to_include){
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ self::route($route, $path_to_include); }
public static function put($route, $controller, $path_to_include = NULL){
if($_SERVER['REQUEST_METHOD'] == 'PUT'){
self::route($route, $controller, $path_to_include);
}
}
public static function put($route, $path_to_include){
if( $_SERVER['REQUEST_METHOD'] == 'PUT' ){ self::route($route, $path_to_include); }
public static function patch($route, $controller, $path_to_include = NULL){
if($_SERVER['REQUEST_METHOD'] == 'PATCH'){
self::route($route, $controller, $path_to_include);
}
}
public static function patch($route, $path_to_include){
if( $_SERVER['REQUEST_METHOD'] == 'PATCH' ){ self::route($route, $path_to_include); }
public static function delete($route, $controller, $path_to_include = NULL){
if($_SERVER['REQUEST_METHOD'] == 'DELETE'){
self::route($route, $controller, $path_to_include);
}
}
public static function delete($route, $path_to_include){
if( $_SERVER['REQUEST_METHOD'] == 'DELETE' ){ self::route($route, $path_to_include); }

public static function fallback($route){
self::route_fallback($route);
}

// ANY route
public static function any($route, $controller, $path_to_include){ self::route($route, $controller, $path_to_include); }
public static function any($route, $controller, $path_to_include = NULL){
self::route($route, $controller, $path_to_include);
}

// ROUTE function

/**
* @throws \Exception
*/
private static function route($route, $controller, $path_to_include){
$callback = $path_to_include;
if( !is_callable($callback) ){
if(!strpos($path_to_include, '.php')){
$path_to_include.='.php';
}
}
if($route == "/404"){
include_once __DIR__."/../$path_to_include";
exit();
}
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
$request_url = rtrim($request_url, '/');
$request_url = strtok($request_url, '?');

$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL) ?? '/';
//$request_url = rtrim($request_url, '/') ?? '/';
$request_url = strtok($request_url, '?') ?? '/';
$route_parts = explode('/', $route);
$request_url_parts = explode('/', $request_url);
array_shift($route_parts);
array_shift($request_url_parts);

if(is_array($controller)){
(new $controller[0])->{$controller[1]}();
//$controller = call_user_func(array($controller[0], $controller[1]));
if ($request_url == '/404'){
echo '404 error';
exit();
}
//dd($buildClass);

if( $route_parts[0] == '' && count($request_url_parts) == 0 ){
// Callback function
if( is_callable($callback) ){
call_user_func_array($callback, []);
exit();
try {
if($request_url == $route){
if(is_array($controller)){
if(method_exists($controller[0], $controller[1])){
(new $controller[0])->{$controller[1]}();
exit();
}else{
throw new \Exception('Method undefined. Check route Class or Function.');
}

}
}
include_once __DIR__."/../../views/".$path_to_include;
exit();
}catch (\Exception $exception){
throw new \Exception($exception->getMessage());
}
if( count($route_parts) != count($request_url_parts) ){ return; }
$parameters = [];
for( $__i__ = 0; $__i__ < count($route_parts); $__i__++ ){
$route_part = $route_parts[$__i__];
if( preg_match("/^[$]/", $route_part) ){
$route_part = ltrim($route_part, '$');
array_push($parameters, $request_url_parts[$__i__]);
$$route_part=$request_url_parts[$__i__];
}
else if( $route_parts[$__i__] != $request_url_parts[$__i__] ){
return;
}

}

private static function route_fallback($route){
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL) ?? '/';
$uri = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

// public folder
if (!$uri[1] == 'public'){
//
}
// Callback function
if( is_callable($callback) ){
call_user_func_array($callback, $parameters);

// exists file // woff woff2 ttf
if (file_exists($file = realpath(root_dir()).$request_url)){
$file_info = pathinfo($file);
switch ($file_info['extension']){
case 'css': header("Content-type: text/css; charset: UTF-8"); break;
case 'ttf': header("Content-type: font/ttf; charset: UTF-8"); break;
case 'woff': header("Content-type: font/woff; charset: UTF-8"); break;
case 'woff2': header("Content-type: font/woff2; charset: UTF-8"); break;
}

echo readfile($file);
exit();
}



//include_once __DIR__."/../$path_to_include";
echo '404 error';
exit();
}

private static function routes($route, $controller, $name){
if(is_array($controller)){
$controller = (new $controller['class'])->controller['function']();
}
dd($controller);
exit();
}

// OUT function
public static function out($text){echo htmlspecialchars($text);}
public static function out($text){
echo htmlspecialchars($text);
}

// CSRF checker
public static function set_csrf(){
if( ! isset($_SESSION["csrf"]) ){ $_SESSION["csrf"] = bin2hex(random_bytes(50)); }
echo '<input type="hidden" name="csrf" value="'.$_SESSION["csrf"].'">';
}

public static function is_csrf_valid(){
if( ! isset($_SESSION['csrf']) || ! isset($_POST['csrf'])){ return false; }
if( $_SESSION['csrf'] != $_POST['csrf']){ return false; }
Expand Down
29 changes: 29 additions & 0 deletions app/Services/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Services;

class Session
{

public function get($name){
return $_SESSION[$name] ?? null;
}

public function set($name){
return $_SESSION[$name] ?? null;
}

public function put($name, $text){
return $_SESSION[$name] = $text;
}

public function remove($name){
return $_SESSION[$name] = null;
}

public function clear(){
session_destroy();
return 1;
}

}
Loading

0 comments on commit 359058d

Please sign in to comment.