Add my template project

This commit is contained in:
BitHeaven 2024-08-09 21:30:13 +05:00
parent 0e0ac4f094
commit 832bf929dc
19 changed files with 243 additions and 0 deletions

9
config/0000.root.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$config['root'] = [
'title' => 'Тестовый сайт',
'description' => 'Описание сайта для embed ссылок',
'keywords' => [
'ключевые',
'слова',
],
];

13
config/0001.db.php Normal file
View File

@ -0,0 +1,13 @@
<?php
$config['db'] = [
'main' => [
'driver' => 'mysql',
'user' => 'root',
'password' => '',
'name' => 'user-rest-api',
'host' => '127.0.0.1',
'port' => 3306,
'charset' => 'utf8',
'options' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
]
];

View File

@ -0,0 +1,19 @@
<?php
class Root {
private static $installed = false;
public static function init() {
self::$installed = floatval(file_get_contents(__RD__.'installed'));
}
public static function url($url, $force = true) {
header('Location: '.$url);
if($force) exit();
}
public static function installed() {
return self::$installed;
}
}
Root::init();

60
engine/class/0001.db.php Normal file
View File

@ -0,0 +1,60 @@
<?php
class DB {
private static $pdos = [];
public static function init($conf = []) {
try {
foreach($conf as $title => $data) {
$dsn = sprintf('%s:dbname=%s;host=%s;port=%d;charset=%s',
$data['driver'],
$data['name'],
$data['host'],
$data['port'],
$data['charset']
);
self::$pdos[$title] = new PDO($dsn, $data['user'], $data['password'], $data['options']);
}
return true;
}
catch(\Throwable $th) {
return false;
}
}
public static function getQuery($db, $sql, $args = null) {
if(!is_array($args) && !empty($args))
$args = [$args];
$stmt = self::$pdos[$db]->prepare($sql);
try {
$stmt->execute($args);
}
catch(\Throwable $th) {
Debug::print($sql);
Debug::print($args);
Debug::print($th);
}
if(stristr($sql, 'SELECT'))
return $stmt->fetch(PDO::FETCH_OBJ);
if(stristr($sql, 'INSERT'))
return self::$pdos[$db]->lastInsertId();
return true;
}
public static function getQueryAll($db, $sql, $args = null) {
if(!is_array($args) && !empty($args))
$args = [$args];
$stmt = self::$pdos[$db]->prepare($sql);
$stmt->execute($args);
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
}
if(!DB::init($config['db'])) {
die('DB not connected! Recheck configs!');
}

View File

@ -0,0 +1,37 @@
<?php
// REQUIREMENTS:
// 0001.db.php
class Debug {
public static function init() {
if(!Root::installed())
self::createTables();
}
public static function createTables() {
DB::getQuery('main',
'CREATE TABLE IF NOT EXISTS `debug_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`value` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
CHARSET=utf8
COLLATE utf8_general_ci'
);
}
public static function print($a, $b = false) {
echo '<pre>';
$b ? var_dump($a) : print_r($a);
echo '</pre>';
}
public static function log($string) {
$string = date('H:i:s d.m.Y', time()).' | '.$string;
DB::getQuery('main', 'INSERT INTO `debug_log` (`value`) VALUES (?)', $string);
return true;
}
}
// Debug::init();

13
engine/core/api.php Normal file
View File

@ -0,0 +1,13 @@
<?php
header('Content-Type: application/json');
if(file_exists(__CD__.'api/'.__URL__[1].'/'.__URL__[2].'.php'))
require __CD__.'api/'.__URL__[1].'/'.__URL__[2].'.php';
else
$data = [
'error' => true,
'message' => 'Endpoint not exists.',
];
echo json_encode($data);
exit();

View File

@ -0,0 +1,5 @@
<?php
$data = [
'error' => false,
'message' => 'test',
];

3
engine/core/index.php Normal file
View File

@ -0,0 +1,3 @@
<?php
require __CD__.'route.php';
require __TD__.'index.php';

1
engine/core/main.php Normal file
View File

@ -0,0 +1 @@
<?php

5
engine/core/route.php Normal file
View File

@ -0,0 +1,5 @@
<?php
if(__URI__ == '/' || in_array(__URL__[0], ['index', 'route']))
Root::url('/main');
elseif(file_exists(__CD__.__URL__[0].'.php'))
require __CD__.__URL__[0].'.php';

38
index.php Normal file
View File

@ -0,0 +1,38 @@
<?php
session_start([
'cookie_lifetime' => 3*60*60,
]);
try {
define('__URI__', strstr($_SERVER['REQUEST_URI'], '?', true) ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']);
define('__URL__', explode('/', substr(__URI__, 1)));
define('__RD__', dirname(__FILE__).'/');
define('__TD__', __RD__.'template/');
define('__ED__', __RD__.'engine/');
define('__CF__', __RD__.'config/');
define('__CD__', __ED__.'core/');
define('__CL__', __ED__.'class/');
define('__RS__', '/');
define('__TS__', __RS__.'template/');
define('__CS__', __TS__.'css/');
define('__JS__', __TS__.'js/');
define('__IS__', __TS__.'img/');
define('__US__', __RS__.'uploads/');
$config = [];
foreach(scandir(__CF__) as $a)
if($a != '.' && $a != '..')
require __CF__.$a;
foreach(scandir(__CL__) as $a)
if($a != '.' && $a != '..')
require __CL__.$a;
require __CD__.'index.php';
}
catch(\Throwable $th) {
echo '<pre>';
print_r($th);
}

1
installed Normal file
View File

@ -0,0 +1 @@
0

0
template/css/style.css Normal file
View File

8
template/footer.php Normal file
View File

@ -0,0 +1,8 @@
</main>
<footer>
<hr>
<p>&copy; Тестовый сайт 2024</p>
</footer>
</body>
</html>

19
template/header.php Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="<?=$config['root']['description']?>">
<meta name="keywords" content="<?=implode(', ', $config['root']['keywords'])?>">
<link rel="stylesheet" href="<?=__CS__?>style.css?v=1">
<script src="<?=__JS__?>index.js?v=1"></script>
<title><?=$config['root']['title']?></title>
</head>
<body>
<header>
<h1>Тестовый REST API сайт</h1>
<hr>
</header>
<main>

4
template/index.php Normal file
View File

@ -0,0 +1,4 @@
<?php
include __TD__.'header.php';
include __TD__.'route.php';
include __TD__.'footer.php';

0
template/js/index.js Normal file
View File

1
template/main.php Normal file
View File

@ -0,0 +1 @@
<p>Прочтите <b>README.md</b> файл для получения информации по API.</p>

7
template/route.php Normal file
View File

@ -0,0 +1,7 @@
<?php
if(__URI__ == '/' || in_array(__URL__[0], ['index', 'route', 'header', 'footer']))
header('Location: /main');
elseif(file_exists(__TD__.__URL__[0].'.php'))
require __TD__.__URL__[0].'.php';
else
header('Location: /');