Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ebakalov/hw5 #407

Open
wants to merge 7 commits into
base: EBakalov/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
/code/vendor
/Docker/dbdata
/Docker/.env
/www/vendor
7 changes: 7 additions & 0 deletions Docker/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=app
MYSQL_ROOT_PASSWORD=testpass

MEMCACHED_SERVER=127.0.0.1
MEMCACHED_PORT=11211
97 changes: 97 additions & 0 deletions Docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
version: '3'

services:
balancer:
container_name: balancer-nginx
image: email-app/nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- webserver1
- webserver2
volumes:
- ./nginx/hosts/balancer:/etc/nginx/conf.d
- phpsocket:/var/run
networks:
- email-app-network

webserver1:
container_name: nginx1
image: email-app/nginx
build:
context: ./nginx
dockerfile: Dockerfile
volumes:
- ./nginx/hosts/app:/etc/nginx/conf.d
- ../www:/var/www/email-app.local
- phpsocket:/var/run
networks:
- email-app-network

app1:
container_name: app1
build:
context: ./fpm
dockerfile: Dockerfile
image: email-app/php
volumes:
- ../www:/var/www/email-app.local
- phpsocket:/var/run
networks:
- email-app-network

webserver2:
container_name: nginx2
image: email-app/nginx
build:
context: ./nginx
dockerfile: Dockerfile
volumes:
- ./nginx/hosts/app:/etc/nginx/conf.d
- ../www:/var/www/email-app.local
- phpsocket:/var/run
networks:
- email-app-network

app2:
container_name: app2
build:
context: ./fpm
dockerfile: Dockerfile
image: email-app/php
volumes:
- ../www:/var/www/email-app.local
- phpsocket:/var/run
networks:
- email-app-network

db:
image: mysql:latest
container_name: db
ports:
- ${MYSQL_PORT}-${MYSQL_PORT}
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- ./dbdata:/var/lib/mysql
networks:
- email-app-network

memcached:
image: memcached:latest
container_name: memcache
ports:
- ${MEMCACHED_PORT}:${MEMCACHED_PORT}
networks:
- email-app-network

networks:
email-app-network:
driver: bridge

volumes:
phpsocket:
44 changes: 44 additions & 0 deletions Docker/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM php:8.1-fpm

# ставим необходимые для нормальной работы модули
RUN apt-get update && apt-get install -y \
curl \
wget \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
imagemagick \
libonig-dev \
libzip-dev \
libmcrypt-dev \
libmemcached-dev\
libmemcached-tools\
mc\
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& pecl install -n mcrypt \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-install -j$(nproc) \
iconv \
mbstring \
mysqli \
pdo_mysql \
zip \
sockets \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& pecl install memcached\
&& docker-php-ext-enable memcached\
&& rm -rf /tmp/pear

COPY ./php.ini /usr/local/etc/php/conf.d/php-custom.ini

WORKDIR /var/www

VOLUME /var/www

RUN chown www-data:www-data /var/www

CMD ["php-fpm"]
2 changes: 2 additions & 0 deletions Docker/fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler = memcache
session.save_path = "tcp://memcache:11211"
15 changes: 15 additions & 0 deletions Docker/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM nginx:latest

RUN apt-get update \
&& apt-get install -y \
nginx \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /var/www

VOLUME /var/www

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
30 changes: 30 additions & 0 deletions Docker/nginx/hosts/app/app.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
upstream php-balancer {
server app1:9000;
server app2:9000;
}

server {
listen 80;
server_name app.local;

root /var/www/email-app.local;
index index.php;

location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php-balancer;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
13 changes: 13 additions & 0 deletions Docker/nginx/hosts/balancer/balancer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
upstream balancer {
server webserver1;
server webserver2;
}

server {
listen 80;
server_name app.local;

location / {
proxy_pass http://balancer;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# PHP2021
# PHP2021. Evgeny Bakalov. Homework 5.
Binary file added images/not_valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/valid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions www/App/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App;

use App\Requests\Request;
use App\Validators\EmailValidator;

class App
{

public function run(): void
{
if (count($_POST) > 0) {
if (isset($this->request['email']) && !empty($this->request['email'])) {
$request = (new Request($_POST))->all();

(new EmailValidator($request))->run();
} else {
echo 'Email str is empty :(';
}
} else {
require_once($_SERVER['DOCUMENT_ROOT'] . '/Views/form.php');
}
}
}
36 changes: 36 additions & 0 deletions www/App/Requests/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Requests;

class Request
{
protected array $request;

public function __construct(array $request)
{
$this->request = $request;
}

public function all(): array
{
return $this->prepareRequest();
}

protected function prepareRequest(): array
{
$prepared = [];

foreach ($this->request as $key => $value) {
$prepared[$key] = $this->cleanString($value);
}

return $prepared;
}

protected function cleanString($value): string
{
return htmlspecialchars(trim($value), ENT_QUOTES);
}
}
56 changes: 56 additions & 0 deletions www/App/Validators/EmailValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace App\Validators;

class EmailValidator
{
protected array $request;
protected string $email;

/**
* @param array $request
*/
public function __construct(array $request)
{
$this->request = $request;
}

public function run()
{
$this->email = $this->request['email'];

$this->validate();
}

private function validate(): void
{
if ($this->isValidEmail() && $this->isValidEmailHostMX()) {
echo 'Email ' . $this->email . ' is valid. ';
} else {
echo 'Email ' . $this->email . ' NOT valid :( ';
}
}

public function isValidEmail(): bool
{
return (bool)preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i",
$this->email
);
}

public function isValidEmailHostMX(): bool
{
if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
[$username, $host] = explode('@', $this->email);

return getmxrr($host, $hosts);
}

return false;
}


}
23 changes: 23 additions & 0 deletions www/VIews/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!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">
<title>Email Validation</title>
</head>

<body>
<div class="container">
<form action="#" method="post">
<label for="email">Email: </label>
<input type="email" name="email" value="">
<button class="but-send">Send</button>
</form>

<div class="result-form"></div>
</div>
</body>

</html>
23 changes: 23 additions & 0 deletions www/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "taschan23/email-app",
"type": "package",
"authors": [
{
"name": "EvgenyBakalov"
}
],
"require": {
"php": ">=7.4",
"ext-json": "*",
"ext-sockets": "*"
}
,
"require-dev": {
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
"App\\": "App"
}
}
}
Loading