src/Service/Utils.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\BrandDomain;
  4. use App\Entity\Check;
  5. use App\Entity\CheckHistory;
  6. use App\Entity\Customer;
  7. use App\Entity\NotificationQueue;
  8. use App\Entity\File;
  9. use App\Entity\Keyword;
  10. use App\Entity\Log;
  11. use App\Entity\NotificationTemplate;
  12. use App\Entity\Page;
  13. use App\Entity\SuspiciousDomain;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use App\Entity\Server;
  16. use App\Entity\Setting;
  17. use App\Entity\Smtp;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  22. use Symfony\Component\Routing\RouterInterface;
  23. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  24. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  25. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  27. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Twig\Environment;
  31. use Symfony\Component\HttpClient\HttpClient;
  32. use DateTime;
  33. class Utils
  34. {
  35.     private $translator;
  36.     private $em;
  37.     private $tokenStorage;
  38.     private $requestStack;
  39.     private $twig;
  40.     private $router;
  41.     private $flash;
  42.     public function __construct(TranslatorInterface $translatorTokenStorageInterface $tokenStorageRequestStack $requestStackEntityManagerInterface $emEnvironment $twigRouterInterface $routerFlashBagInterface $flash)
  43.     {
  44.         $this->translator $translator;
  45.         $this->requestStack $requestStack;
  46.         $this->tokenStorage $tokenStorage;
  47.         $this->em $em;
  48.         $this->twig $twig;
  49.         $this->router $router;
  50.         $this->flash $flash;
  51.         $this->settings $this->em->getRepositorySetting::class )->findAllGlobal();
  52.         foreach ($this->settings as $setting) {
  53.             $this->settings[$setting['name']] = $setting['value'];
  54.         }
  55.     }
  56.     public function scheduledTask_checkServerStatus($version$utils$twig$em$settings): array
  57.     {
  58.         $now = new DateTime();
  59.         $servers $em->getRepository(Server::class)->findBy(['status' => true]);
  60.         $servers_down = array();
  61.         foreach ($servers as $server) {
  62.             $difference $now->diff($server->getLastActivity());
  63.             if ($difference->5) {
  64.                 $servers_down[] = $server;
  65.             }
  66.         }
  67.         if (count($servers_down) > 0) {
  68.             $smtp $em->getRepository(Smtp::class)->findOneBy(['is_default' => true]);
  69.             foreach ($servers_down as $server_down) {
  70.                 //Utils::{"sendNotification"}($smtp->getId(), array('tech@wedoit-group.com'), array(), array(), false, 'Server: ' . $server_down->getName() . ' - Location: ' . $server_down->getLocation() . ' DOWN', 'Detected no activity for 5 minutes', null, null, $em);
  71.             }
  72.         }
  73.         $result['check'] = true;
  74.         $result['result'] = 'ok';
  75.         $result['data'] = [];
  76.         return $result;
  77.     }
  78.     /**
  79.      * @throws RedirectionExceptionInterface
  80.      * @throws ClientExceptionInterface
  81.      * @throws TransportExceptionInterface
  82.      * @throws ServerExceptionInterface
  83.      */
  84.     public function scheduledTask_importDomainsFromDnpedia($version$utils$twig$em$settings): array
  85.     {
  86.         $client HttpClient::create();
  87.         $response $client->request('GET''https://api.codepunch.com/dnfeed.php?c=auth&k=' $settings['dnpedia_key'] . '&s=' $settings['dnpedia_secret']);
  88.         $token trim(substr($response->getContent(), strpos($response->getContent(), ":") + 1));
  89.         $keywords $em->getRepository(Keyword::class)->findBy(['type' => 'include''status' => true]);
  90.         foreach ($keywords as $keyword) {
  91.             $excluded_keywords $em->getRepository(Keyword::class)->findBy(['brand' => $keyword->getBrand(), 'type' => 'exclude''status' => true]);
  92.             $excluded_keywords_string '';
  93.             foreach($excluded_keywords as $excluded_keyword) {
  94.                 $excluded_keywords_string .= $excluded_keyword->getName() . '|';
  95.             }
  96.             $response $client->request('GET''https://api.codepunch.com/dnfeed.php?t=' $token '&kw=%' trim(strtolower($keyword->getName())) . '%');
  97.             $data json_decode($response->getContent(), true);
  98.             $counter_import 0;
  99.             if ($data !== null) {
  100.                 foreach ($data['domains'] as $domain){
  101.                     $perform_check true;
  102.                     if ($excluded_keywords_string !== '') {
  103.                         if (preg_match_all('#\b(' substr($excluded_keywords_string0, -1) . ')\b#'$domain['domain'], $matches)) {
  104.                             $perform_check false;
  105.                         }
  106.                     }
  107.                     if (str_contains($domain['domain'], $keyword->getName()) && $perform_check) {
  108.                         $result_check call_user_func_array(array('App\\Service\\Utils''checkDomain'), array($version$twig$em$domain['domain'], 'dnpedia'$keyword->getBrand(), $keyword));
  109.                         if ($result_check) {
  110.                             $counter_import++;
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.             $keyword->setImportedFromDnpedia($keyword->getImportedFromDnpedia() + $counter_import);
  116.             $em->persist($keyword);
  117.             $em->flush();
  118.         }
  119.         $result['check'] = true;
  120.         $result['result'] = 'ok';
  121.         $result['data'] = [];
  122.         return $result;
  123.     }
  124.     /**
  125.      * @throws TransportExceptionInterface
  126.      * @throws ServerExceptionInterface
  127.      * @throws RedirectionExceptionInterface
  128.      * @throws ClientExceptionInterface
  129.      */
  130.     public function scheduledTask_importDomainsFromPhishTank($version$utils$twig$em$settings): array
  131.     {
  132.         $client HttpClient::create();
  133.         $response $client->request('GET''https://data.phishtank.com/data/' $settings['phishtank_key'] . '/online-valid.json');
  134.         if ($response->getStatusCode() === 200) {
  135.             $domains json_decode($response->getContent(), true);
  136.             if ($domains) {
  137.                 $keywords $em->getRepository(Keyword::class)->findBy(['type' => 'include''status' => true]);
  138.                 foreach ($keywords as $keyword) {
  139.                     $excluded_keywords $em->getRepository(Keyword::class)->findBy(['brand' => $keyword->getBrand(), 'type' => 'exclude''status' => true]);
  140.                     $excluded_keywords_string '';
  141.                     foreach ($excluded_keywords as $excluded_keyword) {
  142.                         $excluded_keywords_string .= $excluded_keyword->getName() . '|';
  143.                     }
  144.                     $counter_import 0;
  145.                     foreach ($domains as $domain) {
  146.                         $perform_check true;
  147.                         if ($excluded_keywords_string !== '') {
  148.                             if (preg_match_all('#\b(' substr($excluded_keywords_string0, -1) . ')\b#'$domain['url'], $matches)) {
  149.                                 $perform_check false;
  150.                             }
  151.                         }
  152.                         if (str_contains($domain['url'], $keyword->getName()) && $perform_check) {
  153.                             $result_check call_user_func_array(array('App\\Service\\Utils''checkDomain'), array($version$twig$em$domain['url'], 'phish_tank'$keyword->getBrand(), $keyword));
  154.                             if ($result_check) {
  155.                                 $counter_import++;
  156.                             }
  157.                         }
  158.                     }
  159.                     $keyword->setImportedFromPhishTank($keyword->getImportedFromPhishtank() + $counter_import);
  160.                     $em->persist($keyword);
  161.                     $em->flush();
  162.                 }
  163.             }
  164.             $result['result'] = 'ok';
  165.         }
  166.         else {
  167.             $result['result'] = 'nok';
  168.         }
  169.         $result['check'] = true;
  170.         $result['data'] = [];
  171.         return $result;
  172.     }
  173.     /**
  174.      * @throws RedirectionExceptionInterface
  175.      * @throws ClientExceptionInterface
  176.      * @throws TransportExceptionInterface
  177.      * @throws ServerExceptionInterface
  178.      */
  179.     public function scheduledTask_checkPhishTank($version$utils$twig$em$settings): array
  180.     {
  181.         $brand_domains $em->getRepository(BrandDomain::class)->findBy(['status' => 'monitor']);
  182.         foreach ($brand_domains as $brand_domain) {
  183.             $checks $em->getRepository(Check::class)->findBy(['brandDomain' => $brand_domain]);
  184.             foreach ($checks as $check) {
  185.                 $client HttpClient::create();
  186.                 $result_array = array();
  187.                 $url_http '';
  188.                 $url_https '';
  189.                 if (!str_starts_with($brand_domain->getUrl(), 'http') and !str_starts_with($brand_domain->getUrl(), 'https')) {
  190.                     if ($brand_domain->getDomain() === $brand_domain->getUrl()) {
  191.                         $url_http 'http://' $brand_domain->getDomain();
  192.                         $url_https 'https://' $brand_domain->getDomain();
  193.                     }
  194.                     else {
  195.                         $url_http 'http://' $brand_domain->getUrl();
  196.                         $url_https 'https://' $brand_domain->getUrl();
  197.                     }
  198.                 }
  199.                 else {
  200.                     if (!str_starts_with($brand_domain->getUrl(), 'https')) {
  201.                         $url_https $brand_domain->getUrl();
  202.                     }
  203.                     else {
  204.                         $url_http $brand_domain->getUrl();
  205.                     }
  206.                 }
  207.                 $result_array['http']['status'] = 'false';
  208.                 $result_array['http']['url'] = '';
  209.                 $result_array['http']['verified'] = '';
  210.                 if ($url_http != '') {
  211.                     $response $client->request('POST''https://checkurl.phishtank.com/checkurl/index.php', [
  212.                         'headers' => [
  213.                             'User-Agent' => 'phishtank/' $settings['phishtank_username'],
  214.                         ],
  215.                         'body' => [
  216.                             'url' => $url_http,
  217.                             'format' => 'json',
  218.                             'api_key' => $settings['phishtank_key'],
  219.                         ],
  220.                     ]);
  221.                     if ($response->getStatusCode() === 200) {
  222.                         $phishtank_json json_decode($response->getContent());
  223.                         $result_array['http']['status'] = $phishtank_json->results->in_database;
  224.                         $result_array['http']['url'] = '';
  225.                         if (property_exists($phishtank_json->results'phish_detail_page')) {
  226.                             $result_array['http']['url'] = $phishtank_json->results->phish_detail_page;
  227.                         }
  228.                         $result_array['http']['verified'] = '';
  229.                         if (property_exists($phishtank_json->results'verified')) {
  230.                             $result_array['http']['verified'] = $phishtank_json->results->verified;
  231.                         }
  232.                     }
  233.                 }
  234.                 $result_array['https']['status'] = 'false';
  235.                 $result_array['https']['url'] = '';
  236.                 $result_array['https']['verified'] = '';
  237.                 if ($url_https != '') {
  238.                     $response $client->request('POST''https://checkurl.phishtank.com/checkurl/index.php', [
  239.                         'headers' => [
  240.                             'User-Agent' => 'phishtank/' $settings['phishtank_username'],
  241.                         ],
  242.                         'body' => [
  243.                             'url' => $url_https,
  244.                             'format' => 'json',
  245.                             'api_key' => $settings['phishtank_key'],
  246.                         ],
  247.                     ]);
  248.                     if ($response->getStatusCode() === 200) {
  249.                         $phishtank_json json_decode($response->getContent());
  250.                         $result_array['https']['status'] = $phishtank_json->results->in_database;
  251.                         $result_array['https']['url'] = '';
  252.                         if (property_exists($phishtank_json->results'phish_detail_page')) {
  253.                             $result_array['https']['url'] = $phishtank_json->results->phish_detail_page;
  254.                         }
  255.                         $result_array['https']['verified'] = '';
  256.                         if (property_exists($phishtank_json->results'verified')) {
  257.                             $result_array['https']['verified'] = $phishtank_json->results->verified;
  258.                         }
  259.                     }
  260.                 }
  261.                 $phishtank_array = [
  262.                     'check_status' => 'checked',
  263.                     'last_check' => new DateTime(),
  264.                     'info' => [
  265.                         'http' => [
  266.                             'status' => $result_array['http']['status'],
  267.                             'url' => $result_array['http']['url'],
  268.                             'verified' => $result_array['http']['verified']
  269.                         ],
  270.                         'https' => [
  271.                             'status' => $result_array['https']['status'],
  272.                             'url' => $result_array['https']['url'],
  273.                             'verified' => $result_array['https']['verified']
  274.                         ]
  275.                     ]
  276.                 ];
  277.                 $check->setPhishTank($phishtank_array);
  278.                 if (count($checks) > 0) {
  279.                     $em->persist($check);
  280.                 }
  281.             }
  282.         }
  283.         $em->flush();
  284.         $result['check'] = true;
  285.         $result['result'] = 'ok';
  286.         $result['data'] = [];
  287.         return $result;
  288.     }
  289.     public function scheduledTask_cleanDB($version$utils$twig$em$settings): array
  290.     {
  291.         // Clean list of domain older then X days and in waiting or false positive status
  292.         $suspicious_domains $em->getRepository(SuspiciousDomain::class)->findToBeCleaned();
  293.         $brad_domains_array = [];
  294.         foreach ($suspicious_domains as $suspicious_domain) {
  295.             if ($utils->timeDiffMinutes(new DateTime(), $suspicious_domain->getLastActivity()) >= $settings['cleandb_domains_difference_in_minute']) {
  296.                 $total_brand_domains count($suspicious_domain->getBrandDomains());
  297.                 $total_brand_domains_counter 0;
  298.                 foreach ($suspicious_domain->getBrandDomains() as $brand_domain) {
  299.                     if ($brand_domain->getStatus() === 'waiting' or $brand_domain->getStatus() === 'false_positive' or $brand_domain->getStatus() === 'deleted') {
  300.                         $total_brand_domains_counter++;
  301.                         $brad_domains_array[] = $brand_domain;
  302.                     }
  303.                 }
  304.                 if ($total_brand_domains === $total_brand_domains_counter) {
  305.                     foreach ($brad_domains_array as $bd) {
  306.                         $em->remove($bd);
  307.                     }
  308.                     $em->remove($suspicious_domain);
  309.                 }
  310.             }
  311.         }
  312.         $filesystem = new Filesystem();
  313.         $check_histories $em->getRepository(CheckHistory::class)->findToBeCleaned();
  314.         foreach ($check_histories as $check_history) {
  315.             if ($utils->timeDiffMinutes(new DateTime(), $check_history->getCreatedAt()) >= $settings['cleandb_checkhistory_difference_in_minute']) {
  316.                 if ($check_history->getScreenshotHttp() !== null) {
  317.                     $filesystem->remove('../storage/screenshots/' $check_history->getScreenshotHttp());
  318.                 }
  319.                 if ($check_history->getScreenshotHttps() !== null) {
  320.                     $filesystem->remove('../storage/screenshots/' $check_history->getScreenshotHttps());
  321.                 }
  322.                 $em->remove($check_history);
  323.             }
  324.         }
  325.         $em->flush();
  326.         $result['check'] = true;
  327.         $result['result'] = 'ok';
  328.         $result['data'] = array('cleaned');
  329.         return $result;
  330.     }
  331.     public function checkDomain($version$twig$em$dom$source$brand$keyword$server null): bool
  332.     {
  333.         $result false;
  334.         if (!str_starts_with($dom'http') and !str_starts_with($dom'https')) {
  335.             $url parse_url('https://' $dom);
  336.         }
  337.         else {
  338.             $url parse_url($dom);
  339.         }
  340.         $entity_id null;
  341.         $suspicious_domain $em->getRepository(SuspiciousDomain::class)->findOneBy(['domain' => trim($url['host'])]);
  342.         $server_array = array();
  343.         if ($server) {
  344.             $server_array['id'] = $server->getId();
  345.             $server_array['name'] = $server->getName();
  346.             $server_array['group_number'] = $server->getGroupNumber();
  347.             $server_array['ip_address'] = $server->getIpAddress();
  348.             $server_array['country'] = $server->getCountry()->getCode();
  349.             $server_array['location'] = $server->getLocation();
  350.         }
  351.         $check_exclusion false;
  352.         $excluded_pages_array = [];
  353.         $pages $em->getRepository(Page::class)->findBy(['brand' => $brand'status' => true]);
  354.         foreach ($pages as $page) {
  355.             $excluded_pages_array[] = str_replace('www.'''parse_url($page->getUrl(), PHP_URL_HOST));
  356.         }
  357.         foreach ($excluded_pages_array as $page) {
  358.             if (str_ends_with(trim($url['host']), $page)) {
  359.                 $check_exclusion true;
  360.             }
  361.         }
  362.         if (!$check_exclusion) {
  363.             if (!$suspicious_domain) {
  364.                 $suspicious_domain = New SuspiciousDomain();
  365.                 $suspicious_domain->setDomain(rtrim($url['host'], '/'));
  366.                 $suspicious_domain->setUrl(rtrim($dom'/'));
  367.                 $suspicious_domain->setSource($source);
  368.                 $suspicious_domain->setKeyword($keyword->getName());
  369.                 $suspicious_domain->setServer($server_array);
  370.                 $suspicious_domain->setCreatedAt(new DateTime());
  371.                 $suspicious_domain->setLastActivity(new DateTime());
  372.                 $suspicious_domain->setStatus(true);
  373.                 $em->persist($suspicious_domain);
  374.                 $em->flush();
  375.                 $entity_id $suspicious_domain->getId();
  376.                 $brand_domain = New BrandDomain();
  377.                 $brand_domain->setBrand($brand);
  378.                 $brand_domain->setKeyword($keyword);
  379.                 $brand_domain->setSuspiciousDomain($suspicious_domain);
  380.                 $brand_domain->setDomain($suspicious_domain->getDomain());
  381.                 $brand_domain->setUrl($suspicious_domain->getUrl());
  382.                 $brand_domain->setStatus('waiting');
  383.                 $em->persist($brand_domain);
  384.                 $keyword->setLastImport(new DateTime());
  385.                 $em->persist($keyword);
  386.                 $em->flush();
  387.                 $result true;
  388.             }
  389.             else {
  390.                 $brand_domain_checker $em->getRepository(BrandDomain::class)->findOneBy(['brand' => $brand'suspiciousDomain' => $suspicious_domain]);
  391.                 if  (!$brand_domain_checker) {
  392.                     $brand_domain = New BrandDomain();
  393.                     $brand_domain->setBrand($brand);
  394.                     $brand_domain->setKeyword($keyword);
  395.                     $brand_domain->setSuspiciousDomain($suspicious_domain);
  396.                     $brand_domain->setDomain($suspicious_domain->getDomain());
  397.                     $brand_domain->setUrl($suspicious_domain->getUrl());
  398.                     $brand_domain->setStatus('waiting');
  399.                     $em->persist($brand_domain);
  400.                     $em->flush();
  401.                     $result true;
  402.                 }
  403.             }
  404.         }
  405.         if ($result == true) {
  406.             $settings $em->getRepositorySetting::class )->findAllGlobal();
  407.             foreach ($settings as $setting) {
  408.                 $settings[$setting['name']] = $setting['value'];
  409.             }
  410.             call_user_func_array(array('App\\Service\\Utils''sendNotification'), array(false'pushover'nullnull, array(), array(), $settings['pushover_api_key'], $settings['pushover_group_api_key'], false'''''suspicious_domain'$entity_id1null$twig$em));
  411.             $smtp $em->getRepository(Smtp::class)->findOneBy(['is_default' => true]);
  412.             if ($smtp) {
  413.                 call_user_func_array(array('App\\Service\\Utils''sendNotification'), array(false'e-mail', -1'tech@wedoit-group.com', array(), array(),  nullnullfalse'''''suspicious_domain'$entity_id1null$twig$em));
  414.             }
  415.         }
  416.         return $result;
  417.     }
  418.     public function checkSecurityToken($server_token$security_token$ip_address): bool
  419.     {
  420.         $response false;
  421.         if ($this->settings['api_disable_security_check'] === '0') {
  422.             $server $this->em->getRepository(Server::class)->findOneBy(['token' => $server_token]);
  423.             if ($server and $server->getIpAddress() == $ip_address) {
  424.                 $secret_key 'XHMGJ4ceqjtHnSNmKj7TN99YQhVXbDgsWfRCj3fTVNM6jdpR4eVbqMkG9kme3DSK';
  425.                 $secret_key_2 'c6GytFtYMpU2XpY53c2yDjSUR76WtkzB';
  426.                 $token strtoupper(substr(md5($server->getToken() . $secret_key date('Ymd') . $secret_key_2), 50));
  427.                 if ($security_token === $token) {
  428.                     $response true;
  429.                 }
  430.             }
  431.         }
  432.         else {
  433.             $response true;
  434.         }
  435.         return $response;
  436.     }
  437.     public function storeFile($file_name$path$params = array(), $object null$object_id null)
  438.     {
  439.         //$file_name = md5(uniqid()) . '.' . $file->guessExtension();
  440.         $size filesize($path);
  441.         $extension pathinfo($file_namePATHINFO_EXTENSION);
  442.         $upload_dir $path;
  443.         $file_document = new File();
  444.         $file_document->setFileName($file_name);
  445.         $file_document->setPath($path);
  446.         $file_document->setParams($params);
  447.         $file_document->setExtension($extension);
  448.         $file_document->setSize($size);
  449.         $file_document->setObject($object);
  450.         $file_document->setObjectId($object_id);
  451.         $file_document->setCreatedAt(new DateTime());
  452.         $this->em->persist($file_document);
  453.         $this->em->flush();
  454.     }
  455.     public function writeLog($type$action$message null$object null$object_id null$customer_id null)
  456.     {
  457.         if ($this->requestStack->getCurrentRequest() != null) {
  458.             $ip $this->requestStack->getCurrentRequest()->getClientIp();
  459.             if ($ip == 'unknown') {
  460.                 $ip $_SERVER['REMOTE_ADDR'];
  461.             }
  462.         } else {
  463.             $ip '000.000.000.000';
  464.         }
  465.         /*if ($customer_id != null) {
  466.             $customer = $this->em->getRepository(Customer::class)->find($customer_id);
  467.         }
  468.         else {
  469.             $customer = $this->em->getRepository(Customer::class)->find($this->requestStack->getSession()->get('customer_id'));
  470.         }*/
  471.         if ($this->tokenStorage->getToken() === null) {
  472.             $user null;
  473.         }
  474.         else {
  475.             $user $this->tokenStorage->getToken()->getUser();
  476.         }
  477.         $log = new Log();
  478.         //$log->setCustomer($customer);
  479.         //$log->setUser($user);
  480.         $log->setIpaddress(ip2long($ip));
  481.         $log->setCreatedAt(new DateTime());
  482.         $log->setType($type);
  483.         $log->setAction($action);
  484.         $log->setMessage(array('message' => $message));
  485.         $log->setObject($object);
  486.         $log->setObjectId($object_id);
  487.         //$log->setObjectCode(null);
  488.         //$log->setObjectBefore($object_before);
  489.         //$log->setObjectAfter($object_after);
  490.         $this->em->persist($log);
  491.         $this->em->flush();
  492.     }
  493.     public function sendNotification($external false$type$smtp_id null$to null$cc null$bcc null$pushover_api_key null$pushover_group_api_key null$read_receipt$subject$body$entity null$entity_id null$template_id null$customer_id null$twig$em): bool
  494.     {
  495.         if ($customer_id === null) {
  496.             $customer $em->getRepository(Customer::class)->find(1);
  497.         }
  498.         else {
  499.             $customer $em->getRepository(Customer::class)->find($customer_id);
  500.         }
  501.         $smtp null;
  502.         if ($type === 'e-mail') {
  503.             if ($smtp_id === -1) {
  504.                 $smtp $em->getRepository(Smtp::class)->findOneBy(['is_default' => true]);
  505.             }
  506.             else {
  507.                 $smtp $em->getRepository(Smtp::class)->find($smtp_id);
  508.             }
  509.             $email_from $smtp->getEmailFrom();
  510.             $email_to =  $to;
  511.             $email_cc implode','$cc);
  512.             $email_bcc implode','$bcc);
  513.         }
  514.         else if ($type === 'pushover') {
  515.             $email_from '';
  516.             $email_to '';
  517.             $email_cc '';
  518.             $email_bcc '';
  519.         }
  520.         if ($template_id !== null) {
  521.             $template $em->getRepository(NotificationTemplate::class)->find($template_id);
  522.             if ($template) {
  523.                 $entity $template->getEntity();
  524.                 if ($type === 'e-mail') {
  525.                     $subject $template->getSubject();
  526.                     $body $template->getBody();
  527.                 }
  528.                 else if ($type === 'pushover') {
  529.                     $subject $template->getSubjectPushover();
  530.                     $body $template->getBodyPushover();
  531.                 }
  532.                 if ($template->getEntity() !== null) {
  533.                     $entity_for_body_string str_replace(' '''ucwords(str_replace('_'' '$template->getEntity())));
  534.                     $entity_data $em->getRepository('App\Entity\\' $entity_for_body_string)->findOneBy(['id' => $entity_id]);
  535.                     $template_path '';
  536.                     if ($external == true) {
  537.                         $template_path 'templates/';
  538.                     }
  539.                     $template_subject_temp $twig->render($template_path 'notification_template/notification_template.html.twig', array('body' => htmlspecialchars_decode($subjectENT_QUOTES)));
  540.                     if ($type === 'e-mail') {
  541.                         $template_temp $twig->render($template_path 'notification_template/notification_template_with_layout.html.twig', array('title' => htmlspecialchars_decode($subjectENT_QUOTES), 'body' => htmlspecialchars_decode($bodyENT_QUOTES), 'background_color' => $template->getBackgroundColor(), 'font_color' => $template->getFontColor()));
  542.                     }
  543.                     else if ($type === 'pushover') {
  544.                         $template_temp $twig->render($template_path 'notification_template/notification_template.html.twig', array('body' => htmlspecialchars_decode($bodyENT_QUOTES)));
  545.                     }
  546.                     $template_subject_twig $twig->createTemplate($template_subject_temp);
  547.                     $subject $template_subject_twig->render(array('entity' => $entity_data));
  548.                     $template_twig $twig->createTemplate($template_temp);
  549.                     $body $template_twig->render(array('entity' => $entity_data));
  550.                 }
  551.             }
  552.         }
  553.         /*$object = null;
  554.         if ($entity != null and $entity_id != null) {
  555.             $entity_for_body_string = str_replace(' ', '', ucwords(str_replace('_', ' ', $entity)));
  556.             $entity_for_body = $this->em->getRepository('App\Entity\\' . $entity_for_body_string)->findOneBy(['id' => $entity_id]);
  557.             $object_lines['name'] = '';
  558.             $object_lines['content'] = array();
  559.             if ($entity === 'quote' or $entity === 'customer_order' or $entity === 'invoice' or $entity === 'credit_note' or $entity === 'shipping_note' or $entity === 'purchase_request' or $entity === 'purchase_quotation' or $entity === 'purchase_order' or $entity === 'passive_invoice' or $entity === 'passive_credit_note' or $entity === 'service_call' or $entity === 'good_receipt' or $entity === 'work_order') {
  560.                 $entity_for_line = lcfirst(str_replace(' ', '', $entity_for_body_string));
  561.                 $entity_line = $entity_for_body_string . 'Line';
  562.                 $entity_lines = $this->em->getRepository('App\Entity\\' . $entity_line)->findBy([$entity_for_line => $entity_for_body]);
  563.                 $object_lines['name'] = $entity . '_lines';
  564.                 $object_lines['content'] = $entity_lines;
  565.             }
  566.             $object['name'] = $entity;
  567.             $object['content'] = $entity_for_body;
  568.             $template_temp = $this->twig->render('email_template/email_template.html.twig', array('body' => htmlspecialchars_decode($body, ENT_QUOTES)));
  569.             $template_twig = $this->twig->createTemplate($template_temp);
  570.             $body = $template_twig->render(array($object['name'] => $object['content'], $object_lines['name'] => $object_lines['content']));
  571.         }*/
  572.         /*$notification = New NotificationQueue();
  573.         //$email->setCustomer($company);
  574.         $notification->setSmtp($smtp);
  575.         $notification->setType($type);
  576.         $notification->setEmailFrom($email_from);
  577.         $notification->setEmailTo($email_to);
  578.         $notification->setEmailCC($email_cc);
  579.         $notification->setEmailBcc($email_bcc);
  580.         $notification->setPushoverApiKey($pushover_api_key);
  581.         $notification->setPushoverGroupApiKey($pushover_group_api_key);
  582.         $notification->setReadReceipt($read_receipt);
  583.         $notification->setSubject($subject);
  584.         $notification->setBody($body);
  585.         $notification->setEntity($entity);
  586.         $notification->setEntityId($entity_id);
  587.         //$email->setTryCounter(0);
  588.         $notification->setStatus('waiting');
  589.         $notification->setCreatedAt(new DateTime());
  590.         $em->persist($notification);
  591.         $em->flush();*/
  592.         return true;
  593.     }
  594.     function getServerArray(): array
  595.     {
  596.         return array(
  597.             'cpu_percent' => '',
  598.             'memory_total' => '',
  599.             'memory_percent' => '',
  600.             'memory_used' => '',
  601.             'memory_free' => '',
  602.             'disk_total' => '',
  603.             'disk_used' => '',
  604.             'disk_free' => '',
  605.             'last_check' => '',
  606.             'data' => '',
  607.             'run_time' => ''
  608.         );
  609.     }
  610.     function getUserSettingsArray(): array
  611.     {
  612.         return array(
  613.             'datatable_number_rows' => '25',
  614.         );
  615.     }
  616.     function timeDiffMinutes($start_time$end_time) {
  617.         $interval $start_time->diff($end_time);
  618.         $days $interval->format("%d");
  619.         $hours   $interval->format('%h');
  620.         $minutes $interval->format('%i');
  621.         if ($days 0) {
  622.             return ($hours 60 $minutes) + ($days 24 60);
  623.         }
  624.         return ($hours 60 $minutes);
  625.     }
  626. }