<?php
namespace App\EventSubscriber;
use App\Entity\News;
use App\Entity\Transaction;
use App\Entity\User;
use App\Kernel;
use App\Security\AppAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use App\Controller\ProjectUtilsController;
class EasyAdminSubscriber implements EventSubscriberInterface
{
/**
* @var UserPasswordEncoderInterface
*/
private $encoder;
private $slugger;
private $authenticator;
private $storage;
private $kernel;
private $em;
public function __construct(SluggerInterface $slugger, UserPasswordEncoderInterface $encoder, AppAuthenticator $authenticator, EntityManagerInterface $em, TokenStorageInterface $storage, Kernel $kernel)
{
$this->slugger = $slugger;
$this->encoder = $encoder;
$this->authenticator = $authenticator;
$this->storage = $storage;
$this->kernel = $kernel;
$this->em = $em;
}
public static function getSubscribedEvents()
{
return [
AfterEntityBuiltEvent::class => ['afterEntityBuilt'],
AfterEntityDeletedEvent::class => ['afterEntityDeleted'],
AfterEntityPersistedEvent::class => ['afterEntityPersisted'],
AfterEntityUpdatedEvent::class => ['afterEntityUpdated'],
BeforeEntityDeletedEvent::class => ['beforeEntityDeleted'],
BeforeEntityPersistedEvent::class => ['beforeEntityPersisted'],
BeforeEntityUpdatedEvent::class => ['beforeEntityUpdated'],
];
}
public function afterEntityBuilt(AfterEntityBuiltEvent $event)
{
}
public function afterEntityDeleted(AfterEntityDeletedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
}
public function afterEntityPersisted(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
$container = $this->kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$doctrine = $container->get('doctrine');
$className = $em->getClassMetadata(get_class($entity))->getName();
if($className == 'App\Entity\Bot'){
if($entity->getToken() != '' && $entity->getToken() != null){
$host = $_SERVER['HTTP_HOST'];
$webhook_url = ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $host . '/api/bot/' . $entity->getId() . '/';
$paramsSetWebhook = array(
'url' => $webhook_url
);
if($entity->getIsActive() == '1'){
$setWebhook = ProjectUtilsController::requestToBot($entity->getToken(), 'setWebhook', $paramsSetWebhook);
if(ProjectUtilsController::isJSON($setWebhook) === true){
$r = json_decode($setWebhook, true);
if(isset($r['ok']) && isset($r['result'])){
if($r['ok'] == true && $r['result'] == true){
$entity->setWebhookUrl($webhook_url);
$em->persist($entity);
$em->flush();
}
}
}
} else {
$delWebhook = ProjectUtilsController::requestToBot($entity->getToken(), 'deleteWebhook', array());
if(ProjectUtilsController::isJSON($delWebhook) === true){
$r = json_decode($delWebhook, true);
if(isset($r['ok']) && isset($r['result'])){
if($r['ok'] == true && $r['result'] == true){
$entity->setWebhookUrl(null);
$em->persist($entity);
$em->flush();
}
}
}
}
}
}
if($className == 'App\Entity\Transaction'){
if($entity->getToGroup()){
$typeof_reverse = null;
if($entity->getTypeof() == 'expense'){
$typeof_reverse = 'income';
} elseif($entity->getTypeof() == 'income'){
$typeof_reverse = 'expense';
}
$amount_reverse = $entity->getAmount();
if($typeof_reverse == 'expense'){
$amount_reverse = '-' . $entity->getAmount();
}
$newTransactionReverse = new Transaction();
if($entity->getFromGroup()){
$newTransactionReverse->setFromGroup($entity->getFromGroup());
}
if($entity->getToGroup()){
$newTransactionReverse->setToGroup($entity->getToGroup());
}
if($entity->getOffice()){
$newTransactionReverse->setOffice($entity->getOffice());
}
$newTransactionReverse->setTypeof($typeof_reverse);
$newTransactionReverse->setAmount($amount_reverse);
$newTransactionReverse->setComment($entity->getComment());
$newTransactionReverse->setNote('Добавление транзакции через АРМ');
$em->persist($newTransactionReverse);
$em->flush();
}
$balance = false;
$from_group = $entity->getFromGroup();
if ($entity->getTypeof() == 'expense') {
$balance = $from_group->getBalance() - $entity->getAmount();
} elseif ($entity->getTypeof() == 'income') {
$balance = $from_group->getBalance() + $entity->getAmount();
}
if ($balance != $from_group->getBalance()) {
$from_group->setBalance($balance);
$em->persist($from_group);
$em->flush();
}
if ($entity->getTypeof() == 'expense' && $entity->getToGroup()) {
$to_group = $entity->getToGroup();
$to_balance = $to_group->getBalance() + $entity->getAmount();
if ($to_balance != $to_group->getBalance()) {
$to_group->setBalance($to_balance);
$em->persist($to_group);
$em->flush();
}
}
if($entity->getTypeof() == 'expense' && $entity->getAmount() > 0){
$entity->setAmount('-' . $entity->getAmount());
}
$entity->setNote('Добавление транзакции через АРМ');
$em->persist($entity);
$em->flush();
}
}
public function afterEntityUpdated(AfterEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
$container = $this->kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$doctrine = $container->get('doctrine');
$className = $em->getClassMetadata(get_class($entity))->getName();
if($className == 'App\Entity\Bot'){
if($entity->getToken() != '' && $entity->getToken() != null){
$host = $_SERVER['HTTP_HOST'];
$webhook_url = ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $host . '/api/bot/' . $entity->getId() . '/';
$paramsSetWebhook = array(
'url' => $webhook_url
);
if($entity->getIsActive() == '1'){
$setWebhook = ProjectUtilsController::requestToBot($entity->getToken(), 'setWebhook', $paramsSetWebhook);
if(ProjectUtilsController::isJSON($setWebhook) === true){
$r = json_decode($setWebhook, true);
if(isset($r['ok']) && isset($r['result'])){
if($r['ok'] == true && $r['result'] == true){
$entity->setWebhookUrl($webhook_url);
$em->persist($entity);
$em->flush();
}
}
}
} else {
$delWebhook = ProjectUtilsController::requestToBot($entity->getToken(), 'deleteWebhook', array());
if(ProjectUtilsController::isJSON($delWebhook) === true){
$r = json_decode($delWebhook, true);
if(isset($r['ok']) && isset($r['result'])){
if($r['ok'] == true && $r['result'] == true){
$entity->setWebhookUrl(null);
$em->persist($entity);
$em->flush();
}
}
}
}
}
// $infoWebhook = TelegramApi::requestToBot($bot->getToken(), 'getWebhookInfo', array());
}
}
public function beforeEntityDeleted(BeforeEntityDeletedEvent $event)
{
$entity = $event->getEntityInstance();
$container = $this->kernel->getContainer();
$request = Request::createFromGlobals();
$em = $container->get('doctrine')->getManager();
$doctrine = $container->get('doctrine');
$className = $em->getClassMetadata(get_class($entity))->getName();
}
public function beforeEntityPersisted(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
$container = $this->kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$doctrine = $container->get('doctrine');
$className = $em->getClassMetadata(get_class($entity))->getName();
if($className == 'App\Entity\User'){
if($entity->getPassword() != '' && $entity->getPassword() != null){
$encoded = $this->encoder->encodePassword($entity, $entity->getPassword());
$entity->setPassword($encoded);
}
if($entity->getTypeof() == 'admin') {
$entity->setRoles(array('ROLE_SUPER_ADMIN'));
} else {
$entity->setRoles(array('ROLE_USER'));
}
}
}
public function beforeEntityUpdated(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
$container = $this->kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$doctrine = $container->get('doctrine');
$className = $em->getClassMetadata(get_class($entity))->getName();
if($className == 'App\Entity\User'){
$curPassword = $this->curPassword($entity->getId());
if($curPassword != $entity->getPassword() && $entity->getPassword() != '' && $entity->getPassword() != null){
$encoded = $this->encoder->encodePassword($entity, $entity->getPassword());
$entity->setPassword($encoded);
}
/*
if($entity->getTypeof() == 'admin') {
$entity->setRoles(array('ROLE_SUPER_ADMIN'));
} elseif($entity->getTypeof() == 'admin_personal') {
$entity->setRoles(array('ROLE_ADMIN_LK'));
} else {
$entity->setRoles(array('ROLE_USER'));
}
*/
}
}
private function curPassword($id){
$curPassword = '';
$hostname_db = '';
$port_db = '';
$name_db = '';
$user_db = '';
$password_db = '';
$db_url = $_ENV["DATABASE_URL"];
$arr = explode('?', str_replace('mysql://', '', $db_url));
if(isset($arr[0])){
$arr1 = explode('/', $arr[0]);
if(isset($arr1[1])){
$name_db = $arr1[1];
}
if(isset($arr1[0])){
$arr2 = explode('@', $arr1[0]);
if(isset($arr2[0])){
$arr3 = explode(':', $arr2[0]);
if(isset($arr3[0])){
$user_db = $arr3[0];
}
if(isset($arr3[1])){
$password_db = $arr3[1];
}
}
if(isset($arr2[1])){
$arr4 = explode(':', $arr2[1]);
if(isset($arr4[0])){
$hostname_db = $arr4[0];
}
if(isset($arr4[1])){
$port_db = $arr4[1];
}
}
}
}
$conn = mysqli_init();
$conn->real_connect($hostname_db, $user_db, $password_db, $name_db, $port_db);
$q_find_user = mysqli_query($conn, "SELECT `id`, `password` FROM `user` WHERE `id` = '" . $id . "'");
if($q_find_user !== false && mysqli_num_rows($q_find_user) != 0){
$user_q = mysqli_fetch_assoc($q_find_user);
$curPassword = $user_q['password'];
}
mysqli_close($conn);
return $curPassword;
}
}