diff --git a/w12/composer.json b/w12/composer.json index 29fb0ae..d409280 100644 --- a/w12/composer.json +++ b/w12/composer.json @@ -1,21 +1,15 @@ { - "name": "nuark/w12", - "description": "Work 12 app", + "name": "jenya/laba", + "description": "Laba", "type": "project", "license": "MIT", "autoload": { "psr-4": { - "Nuark\\W12\\": "src/" + "Jenya\\Laba\\": "src/" } }, - "authors": [ - { - "name": "Andrew", - "email": "me@nuark.xyz" - } - ], "require": { "pecee/simple-router": "4.3.7.2", "twig/twig": "^3.3" } -} +} \ No newline at end of file diff --git a/w12/db-init.sql b/w12/db-init.sql new file mode 100644 index 0000000..5474d37 --- /dev/null +++ b/w12/db-init.sql @@ -0,0 +1,102 @@ +-- COMMENT ON SCHEMA public IS 'standard public schema'; +--SET default_tablespace = ''; +--SET default_table_access_method = heap; +CREATE TABLE public.images ( + id integer NOT NULL, + filename character varying(255) NOT NULL, + user_id integer NOT NULL, + published boolean DEFAULT false, + description character varying(255) DEFAULT ''::character varying NOT NULL, + created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +CREATE SEQUENCE public.images_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.images_id_seq OWNED BY public.images.id; + +CREATE SEQUENCE public.images_user_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.images_user_id_seq OWNED BY public.images.user_id; + +CREATE TABLE public.menu ( + id integer NOT NULL, + user_id integer NOT NULL, + data character varying DEFAULT '[]'::character varying +); + +CREATE SEQUENCE public.menu_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.menu_id_seq OWNED BY public.menu.id; + +CREATE SEQUENCE public.menu_user_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.menu_user_id_seq OWNED BY public.menu.user_id; + +CREATE TABLE public.users ( + id integer NOT NULL, + login character varying(255) NOT NULL, + password character varying(255) NOT NULL, + created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +CREATE SEQUENCE public.users_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; + +ALTER TABLE ONLY public.images ALTER COLUMN id SET DEFAULT nextval('public.images_id_seq'::regclass); + +ALTER TABLE ONLY public.images ALTER COLUMN user_id SET DEFAULT nextval('public.images_user_id_seq'::regclass); + +ALTER TABLE ONLY public.menu ALTER COLUMN id SET DEFAULT nextval('public.menu_id_seq'::regclass); + +ALTER TABLE ONLY public.menu ALTER COLUMN user_id SET DEFAULT nextval('public.menu_user_id_seq'::regclass); + +ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); + +ALTER TABLE ONLY public.images + ADD CONSTRAINT images_pk PRIMARY KEY (id); + +ALTER TABLE ONLY public.menu + ADD CONSTRAINT menu_pk PRIMARY KEY (id); + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_pk PRIMARY KEY (id); + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_un UNIQUE (login); + +ALTER TABLE ONLY public.images + ADD CONSTRAINT images_fk FOREIGN KEY (user_id) REFERENCES public.users(id); + +ALTER TABLE ONLY public.menu + ADD CONSTRAINT menu_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON UPDATE CASCADE ON DELETE CASCADE; \ No newline at end of file diff --git a/w12/index.php b/w12/index.php index 48b2cb0..93d2ca9 100644 --- a/w12/index.php +++ b/w12/index.php @@ -53,7 +53,7 @@ SimpleRouter::post('/register', function() { // if login or password is empty, redirect to register page with error if (empty($login) || empty($password)) { - return response()->redirect('/register?error=Empty login or password given!'); + return response()->redirect('/register?error=Проверьте данные формы!'); } // try create user if ok - redirect to login page with message @@ -61,9 +61,9 @@ SimpleRouter::post('/register', function() { Database::createUser($login, $password); $user = Database::getUser($login); Database::createEmptyMenuForUser($user); - response()->redirect('/login?message=User created'); + response()->redirect('/login?message=Пользователь создан'); } catch (PDOException $e) { - response()->redirect('/register?error=User already exists!'); + response()->redirect('/register?error=Пользователь уже существует!'); } }); @@ -89,13 +89,13 @@ SimpleRouter::post('/login', function() { // if login or password is empty, redirect to register page with error if (empty($login) || empty($password)) { - return response()->redirect('/login?error=Empty login or password given!'); + return response()->redirect('/login?error=Проверьте данные формы!'); } if (!Database::userExists($login)) { - return response()->redirect('/login?error=User does not exist!'); + return response()->redirect('/login?error=Пользователь не существует!'); } if (!Database::verifyUser($login, $password)) { - return response()->redirect('/login?error=Wrong password!'); + return response()->redirect('/login?error=Неправильный пароль!'); } $user = Database::getUser($login); @@ -223,8 +223,9 @@ SimpleRouter::post('/uploadImage', function() { $source = imagecreatefromjpeg($fullpath); imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); - $color = imagecolorallocate($thumb, 255, 0, 0); - imagestring($thumb, 2, 2, 2, "Watermark text :)", $color); + $color = imagecolorallocate($thumb, 0, 255, 0); + $ulogin = getSessionVariable('user')['login']; + imagestring($thumb, 2, 2, 2, "$ulogin @ LABA", $color); imagejpeg($thumb, 'data/thumb/' . $filename); try { @@ -290,7 +291,6 @@ SimpleRouter::post('/image/{id}', function($id) { $description = input()->post('description', '')->value; $published = boolval(input()->post('published', 0)->value)?"true":"false"; - var_dump($published); try { Database::updateImage($id, $description, $published); } catch (PDOException $e) { @@ -366,12 +366,12 @@ SimpleRouter::post('/import', function() { if ($xml) { try { list($ok, $fail) = Database::importUsersXML($xml); - response()->redirect("/import?message=OK: $ok, FAIL: $fail"); + response()->redirect("/import?message=OK: $ok, КРАХ: $fail"); } catch (Exception $e) { response()->redirect("/import?error=" . $e->getMessage()); } } else { - response()->redirect('/import?error=Error happened '); + response()->redirect('/import?error=Произошла ошибка'); } }); @@ -421,15 +421,15 @@ SimpleRouter::post('/recover-password', function() use ($twig) { 'login' => $login, 'password' => $newPassword, ]); - response()->redirect('/recover-password?message=Success! We sent you an email with your new password&mail=' . urlencode($sentMail)); + response()->redirect('/recover-password?message=Сообщение с паролем отправлено&mail=' . urlencode($sentMail)); } else { - response()->redirect('/recover-password?error=User not found'); + response()->redirect('/recover-password?error=Пользователь не найден!'); } } catch (PDOException $e) { response()->redirect('/recover-password?error=' . $e->getMessage()); } } else { - response()->redirect('/recover-password?error=Error happened'); + response()->redirect('/recover-password?error=Произошла ошибка'); } }); diff --git a/w12/laba12.zip b/w12/laba12.zip new file mode 100644 index 0000000..1eac9b7 Binary files /dev/null and b/w12/laba12.zip differ diff --git a/w12/log.txt b/w12/log.txt new file mode 100644 index 0000000..181a15c --- /dev/null +++ b/w12/log.txt @@ -0,0 +1 @@ +'admin' \ No newline at end of file diff --git a/w12/src/database.php b/w12/src/database.php index 17bd92d..500b800 100644 --- a/w12/src/database.php +++ b/w12/src/database.php @@ -2,9 +2,9 @@ // simple database PDO utilizing the Postgres driver class Database { private static $db; - private static $dsn = 'pgsql:host=localhost;port=5432;dbname=w12'; - private static $username = 'postgres'; - private static $password = 'asarch6122'; + private static $dsn = 'pgsql:host=localhost;port=9001;dbname=db'; + private static $username = 'db'; + private static $password = 'postgres'; private static $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, @@ -56,11 +56,11 @@ class Database { } public static function generateUserNewPassword($login) { + $password = bin2hex(random_bytes(8)); $db = self::getDB(); $query = $db->prepare('UPDATE users SET password = :password WHERE login = :login'); $query->bindParam(':login', $login); $query->bindParam(':password', $password); - $password = bin2hex(random_bytes(8)); $query->execute(); return $password; } diff --git a/w12/views/edit-menu.twig b/w12/views/edit-menu.twig index 04e6087..cd484c4 100644 --- a/w12/views/edit-menu.twig +++ b/w12/views/edit-menu.twig @@ -1,35 +1,37 @@ - - - - - - - Document - - - {{user.login}}, вы редактируете меню. На главную -
- Меню: - +{% extends "template.twig" %} -
+{% block title %}Редактирование меню{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{{user.login}}, вы редактируете меню. На главную +
+Меню: + + +
+
+ + + +
+ + - \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/gallery.twig b/w12/views/gallery.twig index 721df04..03584ac 100644 --- a/w12/views/gallery.twig +++ b/w12/views/gallery.twig @@ -1,20 +1,21 @@ - - - - - - - Document - - - {{user.login}}, вы смотрите свою галерею. На главную -
- {% for image in images %} - - {{image.id}} - - {% else %} -

Нет изображений

- {% endfor %} - - \ No newline at end of file +{% extends "template.twig" %} + +{% block title %}Галерея{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{{user.login}}, вы смотрите свою галерею. На главную +
+{% for image in images %} + + {{image.id}} + +{% else %} +

Нет изображений

+{% endfor %} +{% endblock %} \ No newline at end of file diff --git a/w12/views/image.twig b/w12/views/image.twig index 6d36452..198009b 100644 --- a/w12/views/image.twig +++ b/w12/views/image.twig @@ -1,26 +1,27 @@ - - - - - - - Document - - - {{user.login}} осматривает картинку... На главную -
- {{image.filename}} -
- Миниатюра -
-
- -
- Опубликовано? - -
- -
- Удалить - - \ No newline at end of file +{% extends "template.twig" %} + +{% block title %}Просмотр изображения{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{{user.login}} осматривает картинку... На главную +
+{{image.filename}} +
+Миниатюра +
+
+ +
+ Опубликовано? + +
+ +
+Удалить +{% endblock %} \ No newline at end of file diff --git a/w12/views/import.twig b/w12/views/import.twig index 0263252..c92b486 100644 --- a/w12/views/import.twig +++ b/w12/views/import.twig @@ -1,26 +1,27 @@ - - - - - - - Document - - - {% if message %} -
{{message}}
- {% endif %} - {% if error %} -
{{error}}
- {% endif %} -

Import page

-

Drop XML file on page or enter content down here

-
- -
- -
- +{% extends "template.twig" %} + +{% block title %}Импорт пользователей{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{% if message %} +
{{message}}
+{% endif %} +{% if error %} +
{{error}}
+{% endif %} +

Импорт

+

Перенесите XML файл дампа на форму или вставьте содерживое в поле

+
+ +
+ +
- \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/index.twig b/w12/views/index.twig index d18661f..23d6637 100644 --- a/w12/views/index.twig +++ b/w12/views/index.twig @@ -1,11 +1,8 @@ - - - - - - - Document +{% extends "template.twig" %} +{% block title %}Главная страница{% endblock %} +{% block head %} + {{ parent() }} - - +{% endblock %} + +{% block content %} Привет, {{user.login}}! Выйти
Меню: @@ -105,4 +103,4 @@ document.addEventListener("DOMContentLoaded", function() { }); - \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/login.twig b/w12/views/login.twig index a18f6d3..0a4131d 100644 --- a/w12/views/login.twig +++ b/w12/views/login.twig @@ -1,27 +1,52 @@ - - - - - - - Document - - +{% extends "template.twig" %} + +{% block title %}Вход{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +
{% if message %}
{{message}}
{% endif %} {% if error %}
{{error}}
{% endif %} -

Login page

-
- - - -
-
- Registration -
- Recover password - - \ No newline at end of file +

Вход

+
+ + + +
+
+ Зарегистрироваться +
+ Восстановить пароль +
+{% endblock %} \ No newline at end of file diff --git a/w12/views/lookup-tables.twig b/w12/views/lookup-tables.twig index 2cc0185..e20e380 100644 --- a/w12/views/lookup-tables.twig +++ b/w12/views/lookup-tables.twig @@ -1,12 +1,14 @@ - - - - - - - Document - - +{% extends "template.twig" %} + +{% block title %}Просмотр таблиц{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} {% if table %} {{user.login}}, вы смотрите таблицу {{table}}. Отмена {% else %} @@ -46,5 +48,4 @@ {% endif %} - - \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/propose-auth.twig b/w12/views/propose-auth.twig index cf2dbd2..78cc303 100644 --- a/w12/views/propose-auth.twig +++ b/w12/views/propose-auth.twig @@ -1,12 +1,13 @@ - - - - - - - Document - - +{% extends "template.twig" %} + +{% block title %}Гейтвей{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} Вам нужно войти или зарегистрироваться для работы с системой. - - \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/recover-password-mail.twig b/w12/views/recover-password-mail.twig index 009baad..6ab05e9 100644 --- a/w12/views/recover-password-mail.twig +++ b/w12/views/recover-password-mail.twig @@ -1,4 +1,4 @@ -Hello, {{login}}! -You have requested password change, so we generated you a new one (without backtics): `{{password}}`. -
-Please be sure to delete this message after you have logged in. For now you cannot change your password by yourself. \ No newline at end of file +Привет, {{login}}! +Похоже, ты ззабыл свой пароль, вот тебе новый (без кавычек): `{{password}}`. +
+Удали это сообщение после входа, чтобы его никто не прочитал! \ No newline at end of file diff --git a/w12/views/recover-password.twig b/w12/views/recover-password.twig index 141bde8..26c74f9 100644 --- a/w12/views/recover-password.twig +++ b/w12/views/recover-password.twig @@ -1,32 +1,57 @@ - - - - - - - Document - - - {% if mail %} -
{{mail | raw}}
-
- {% endif %} - {% if message %} -
{{message}}
-
- {% endif %} - {% if error %} -
{{error}}
-
- {% endif %} -

Password recovery page

-
- - -
-
- Registration -
- Login - - \ No newline at end of file +{% extends "template.twig" %} + +{% block title %}Восстановление парля{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +
+ {% if mail %} +
{{mail | raw}}
+
+ {% endif %} + {% if message %} +
{{message}}
+
+ {% endif %} + {% if error %} +
{{error}}
+
+ {% endif %} +

Восстановление пароля

+
+ + +
+
+ Зарегистрироваться +
+ Войти +
+{% endblock %} \ No newline at end of file diff --git a/w12/views/register.twig b/w12/views/register.twig index bdfa483..f785482 100644 --- a/w12/views/register.twig +++ b/w12/views/register.twig @@ -1,24 +1,49 @@ - - - - - - - Document - - - {% if error %} -
{{error}}
- {% endif %} -

Registration page

-
- - - -
-
- Login -
- Recover password - - \ No newline at end of file +{% extends "template.twig" %} + +{% block title %}Регистрация{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +
+ {% if error %} +
{{error}}
+ {% endif %} +

Регистрация

+
+ + + +
+
+ Войти +
+ Восстановить пароль +
+{% endblock %} \ No newline at end of file diff --git a/w12/views/search.twig b/w12/views/search.twig index 83196dc..bb36bf9 100644 --- a/w12/views/search.twig +++ b/w12/views/search.twig @@ -1,11 +1,8 @@ - - - - - - - Document +{% extends "template.twig" %} +{% block title %}Импорт пользователей{% endblock %} +{% block head %} + {{ parent() }} - - +{% endblock %} + +{% block content %} {{user.login}} что-то ищет 🤔 На главную
@@ -46,5 +44,4 @@

Пусто

{% endif %} - - \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/w12/views/template.twig b/w12/views/template.twig new file mode 100644 index 0000000..2de1a8e --- /dev/null +++ b/w12/views/template.twig @@ -0,0 +1,15 @@ + + + + + + {% block head %} + + {% block title %}{% endblock %} + {% endblock %} + + +
{% block content %}{% endblock %}
+ + + \ No newline at end of file