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 @@ - - -
- - - -Нет изображений
- {% endfor %} - - \ No newline at end of file +{% extends "template.twig" %} + +{% block title %}Галерея{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{{user.login}}, вы смотрите свою галерею. На главную +Нет изображений
+{% 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 @@ - - - - - - -Drop XML file on page or enter content down here
- - +{% extends "template.twig" %} + +{% block title %}Импорт пользователей{% endblock %} +{% block head %} + {{ parent() }} + +{% endblock %} + +{% block content %} +{% if message %} +Перенесите 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 @@ - - - - - - -