src/Entity/VPN/Service/Service.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\VPN\Service;
  3. use App\DependencyInjection\JDF;
  4. use App\Entity\BaseEntity;
  5. use App\Entity\Country;
  6. use App\Entity\Telegram\AgentPublicBot\Bot;
  7. use App\Entity\Telegram\AgentPublicBot\BotUser;
  8. use App\Entity\Transaction;
  9. use App\Entity\Generic\User;
  10. use App\Repository\VPN\Service\ServiceRepository;
  11. use DateTimeImmutable;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\DBAL\Types\Types;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  17. #[ORM\Entity(repositoryClassServiceRepository::class)]
  18. class Service extends BaseEntity
  19. {
  20.     #[ORM\Id]
  21.     #[ORM\Column(type'guid'uniquetrue)]
  22.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  23.     #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
  24.     private ?string $id null;
  25.     #[ORM\Column(typeTypes::BIGINT)]
  26.     private ?string $clientId null;
  27.     #[ORM\ManyToOne(inversedBy'services')]
  28.     #[ORM\JoinColumn(nullablefalse)]
  29.     private ?User $owner null;
  30.     #[ORM\Column(length255)]
  31.     private ?string $username null;
  32.     #[ORM\Column(length255)]
  33.     private ?string $password null;
  34.     #[ORM\ManyToOne(inversedBy'services')]
  35.     #[ORM\JoinColumn(nullablefalse)]
  36.     private ?Plan $plan null;
  37.     #[ORM\Column(typeTypes::BIGINT)]
  38.     private ?string $total null;
  39.     #[ORM\Column(typeTypes::BIGINT)]
  40.     private ?string $used null;
  41.     #[ORM\Column(nullabletrue)]
  42.     private ?\DateTimeImmutable $expireAt null;
  43.     #[ORM\Column]
  44.     private ?bool $isEnabled null;
  45.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  46.     private ?string $note null;
  47.     #[ORM\Column(typeTypes::BIGINT)]
  48.     private ?string $totalPrice null;
  49.     #[ORM\Column(length255nullabletrue)]
  50.     private ?string $payMethod null;
  51.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  52.     private ?string $rollbackNote null;
  53.     #[ORM\Column(typeTypes::BIGINT)]
  54.     private ?string $ocServUsage null;
  55.     #[ORM\Column(typeTypes::BIGINT)]
  56.     private ?string $v2rayUsage null;
  57.     #[ORM\Column(typeTypes::BIGINT)]
  58.     private ?string $openVpnUsage null;
  59.     #[ORM\Column(length255nullabletrue)]
  60.     private ?string $serverNumber null;
  61.     #[ORM\Column(length255nullabletrue)]
  62.     private ?string $country null;
  63.     #[ORM\ManyToOne(inversedBy'services')]
  64.     private ?BotUser $botUser null;
  65.     #[ORM\ManyToOne(inversedBy'services')]
  66.     private ?Bot $creatorBot null;
  67.     #[ORM\Column]
  68.     private ?bool $sizeNotif null;
  69.     #[ORM\Column]
  70.     private ?bool $dayNotif null;
  71.     #[ORM\Column]
  72.     private ?bool $doneNotif null;
  73.     #[ORM\OneToMany(mappedBy'service'targetEntityServiceUsage::class)]
  74.     private Collection $serviceUsages;
  75.     public function __construct()
  76.     {
  77.         parent::__construct();
  78.         $this->setUsed(0);
  79.         $this->setIsEnabled(true);
  80.         $this->setOcServUsage(0);
  81.         $this->setOpenVpnUsage(0);
  82.         $this->setV2rayUsage(0);
  83.         $this->setServerNumber(random_int(50,150));
  84.         $this->setDoneNotif(0);
  85.         $this->setSizeNotif(0);
  86.         $this->setDayNotif(0);
  87.         $this->serviceUsages = new ArrayCollection();
  88.     }
  89.     public function getId(): ?string
  90.     {
  91.         return $this->id;
  92.     }
  93.     public function getClientId(): ?string
  94.     {
  95.         return $this->clientId;
  96.     }
  97.     public function setClientId(string $clientId): static
  98.     {
  99.         $this->clientId $clientId;
  100.         return $this;
  101.     }
  102.     public function getOwner(): ?User
  103.     {
  104.         return $this->owner;
  105.     }
  106.     public function setOwner(?User $owner): static
  107.     {
  108.         $this->owner $owner;
  109.         return $this;
  110.     }
  111.     public function getUsername(): ?string
  112.     {
  113.         return $this->username;
  114.     }
  115.     public function setUsername(string $username): static
  116.     {
  117.         $this->username $username;
  118.         return $this;
  119.     }
  120.     public function getPassword(): ?string
  121.     {
  122.         return $this->password;
  123.     }
  124.     public function setPassword(string $password): static
  125.     {
  126.         $this->password $password;
  127.         return $this;
  128.     }
  129.     public function getPlan(): ?Plan
  130.     {
  131.         return $this->plan;
  132.     }
  133.     public function setPlan(?Plan $plan): static
  134.     {
  135.         $this->plan $plan;
  136.         return $this;
  137.     }
  138.     public function getTotal(): ?string
  139.     {
  140.         return $this->total;
  141.     }
  142.     public function setTotal(string $total): static
  143.     {
  144.         $this->total $total;
  145.         return $this;
  146.     }
  147.     public function getUsed(): ?string
  148.     {
  149.         return $this->used;
  150.     }
  151.     public function setUsed(string $used): static
  152.     {
  153.         $this->used $used;
  154.         return $this;
  155.     }
  156.     public function getExpireAt(): ?\DateTimeImmutable
  157.     {
  158.         return $this->expireAt;
  159.     }
  160.     public function setExpireAt(?\DateTimeImmutable $expireAt): static
  161.     {
  162.         $this->expireAt $expireAt;
  163.         return $this;
  164.     }
  165.     public function isEnabled(): ?bool
  166.     {
  167.         return $this->isEnabled;
  168.     }
  169.     public function setIsEnabled(bool $isEnabled): static
  170.     {
  171.         $this->isEnabled $isEnabled;
  172.         return $this;
  173.     }
  174.     public function getNote(): ?string
  175.     {
  176.         return $this->note;
  177.     }
  178.     public function setNote(?string $note): static
  179.     {
  180.         $this->note $note;
  181.         return $this;
  182.     }
  183.     public function getTotalPrice(): ?string
  184.     {
  185.         return $this->totalPrice;
  186.     }
  187.     public function setTotalPrice(string $totalPrice): static
  188.     {
  189.         $this->totalPrice $totalPrice;
  190.         return $this;
  191.     }
  192.     public function getPayMethod(): ?string
  193.     {
  194.         return $this->payMethod;
  195.     }
  196.     public function setPayMethod(?string $payMethod): static
  197.     {
  198.         $this->payMethod $payMethod;
  199.         return $this;
  200.     }
  201.     public function v1ListService()
  202.     {
  203.         $now = new \DateTime();
  204.         if ($this->getExpireAt()){
  205.             $expireTime $this->getExpireAt();
  206.         }else{
  207.            $expireTime $now->modify('+1Year');
  208.         }
  209.         $interval $expireTime->diff($now);
  210.         if ($interval->days && $interval->24) {
  211.             $isLow true;
  212.         } else {
  213.             $isLow false;
  214.         }
  215.         if (!$this->isActive()){
  216.             $css 'text-theme-6';
  217.             $statusText 'غیر فعال - مرجوع شده';
  218.         }elseif ($this->isEnabled()){
  219.             $css =  'text-theme-9';
  220.             $statusText 'فعال';
  221.         }else{
  222.             $css 'text-theme-6';
  223.             $statusText 'غیر فعال';
  224.         }
  225.         $planTitle '';
  226.         $planTitle $planTitle $this->getPlan()->getDays() . ' روز ' '- ';
  227.         $planTitle $planTitle 'نامحدود کاربر';
  228.         $fullText '<a href="#" class="font-medium whitespace-nowrap  ' $css '">';
  229.         $fullText $fullText $planTitle .$statusText.'</a>';
  230.         $fullText $fullText '
  231.             <div class="text-gray-600 text-xs whitespace-nowrap mt-0.5" >
  232.             مالک : 
  233.                                         '.
  234.             $this->getOwner().'
  235.                                         <br>';
  236.         $fullText $fullText "
  237.                 از
  238.                 {$this->pDateFilter($this->getCreatedAt())}
  239.                 تا
  240.                 {$this->pDateFilter($this->getExpireAt())}
  241.                 ".'</div >';
  242.         $fullText $fullText "
  243.                 مصرف
  244.                 {$this->formatSizeUnits$this->getUsed())}
  245.                 از
  246.                 {$this->formatSizeUnits$this->getTotal())}
  247.                 ";
  248.         $fullText $fullText.    '</div >';
  249.         $fullText $fullText.'<br>'.'<a class="btn mt-1 btn-sm btn-primary" href="/v2/no-pass/'$this->getId() . '">' 'دریافت کانفیگ' '</a>';
  250.         return $fullText;
  251.     }
  252.     public function v2CommentService()
  253.     {
  254.         $fullText '<div class=" text-xs whitespace-nowrap mt-0.5">' $this->getMobile() . '</div>';
  255.         return $fullText;
  256.     }
  257.     public function v2InfoService()
  258.     {
  259.         $fullText '<div class=" text-xs whitespace-nowrap mt-0.5">
  260.                 نام : 
  261.                   ' $this->getUsername() . '
  262.             <br>
  263.             شناسه سرویس:
  264.             ' $this->getClientId() . '
  265.             <br>
  266.                                         کلمه عبور : 
  267.                                         ' .
  268.             $this->getPassword() . ' <br> 
  269.                                        متن آماده ارسال مشتری :‌ '  .
  270.             '
  271.                                         </div>';
  272. //        foreach ($this->getOcServServerServiceConnections() as $ocServServerServiceConnection) {
  273. //            if ($ocServServerServiceConnection->getServer() && $ocServServerServiceConnection->getServer()){
  274. //                $fullText = $fullText.'<br>'.'سرور :‌'.$ocServServerServiceConnection->getServer()->getTunnelIpOrDomain().'<br>';
  275. //            }
  276. //        }
  277.         $fullText $fullText '<button onclick="copy(';
  278.         $fullText $fullText "'";
  279.         $fullText $fullText $this->generateGuideText();
  280.         $fullText $fullText "'";
  281.         $fullText $fullText ')" class="btn mt-1 btn-sm btn-primary">کپی متن توضیحات</button>';
  282.         return $fullText;
  283.     }
  284.     public function pDateFilter($date)
  285.     {
  286.         if (!$date) return null;
  287.         date_default_timezone_set('Asia/Tehran');
  288.         $jdate = new JDF();
  289.         $gDate $date->format('Y-m-d');
  290. //        return $gDate;
  291.         $gDate explode('-'$gDate);
  292.         return $jdate->gregorianToJalali($gDate[0], $gDate[1], $gDate[2], '/');
  293. //        return $this->tr_num2($this->tr_num2($jdate->toJalali2($gDate[0], $gDate[1], $gDate[2]), 'fa'), 'fa');
  294.     }
  295.     public function getDaysLeft():int
  296.     {
  297.         $date1 = new DateTimeImmutable();
  298.         $date2 $this->getExpireAt();
  299.         if ($date2 $date1){
  300.             $diff $date2->diff($date1)->format("%a");
  301.             return intval($diff);
  302.         }else{
  303.             return  0;
  304.         }
  305.     }
  306.     public function getTrafficLeft():string
  307.     {
  308.         if ($this->getUsed() < $this->getTotal()){
  309.             $left $this->getTotal() - $this->getUsed();
  310.             return $this->readableSize($left);
  311.         }else{
  312.             return '0MB' ;
  313.         }
  314.     }
  315.     public function readableSize($bytes): string
  316.     {
  317.         if ($bytes >= 1073741824) {
  318.             $bytes number_format($bytes 10737418242) . ' GB';
  319.         } elseif ($bytes >= 1048576) {
  320.             $bytes number_format($bytes 10485762) . ' MB';
  321.         } elseif ($bytes >= 1024) {
  322.             $bytes number_format($bytes 10242) . ' KB';
  323.         } elseif ($bytes 1) {
  324.             $bytes $bytes ' bytes';
  325.         } elseif ($bytes == 1) {
  326.             $bytes $bytes ' byte';
  327.         } else {
  328.             $bytes '0 bytes';
  329.         }
  330.         return $bytes;
  331.     }
  332.     public function formatSizeUnits($bytes): string
  333.     {
  334.         if ($bytes >= 1073741824) {
  335.             $bytes number_format($bytes 10737418242) . ' GB';
  336.         } elseif ($bytes >= 1048576) {
  337.             $bytes number_format($bytes 10485762) . ' MB';
  338.         } elseif ($bytes >= 1024) {
  339.             $bytes number_format($bytes 10242) . ' KB';
  340.         } elseif ($bytes 1) {
  341.             $bytes $bytes ' bytes';
  342.         } elseif ($bytes == 1) {
  343.             $bytes $bytes ' byte';
  344.         } else {
  345.             $bytes '0 bytes';
  346.         }
  347.         return $bytes;
  348.     }
  349.     function convertToBytes(string $from): ?int
  350.     {
  351.         $units = ['B''KB''MB''GB''TB''PB'];
  352.         $number substr($from0, -2);
  353.         $suffix strtoupper(substr($from, -2));
  354.         if (is_numeric(substr($suffix01))) {
  355.             return preg_replace('/[^\d]/'''$from);
  356.         }
  357.         $exponent array_flip($units)[$suffix] ?? null;
  358.         if ($exponent === null) {
  359.             return null;
  360.         }
  361.         return $number * (1024 ** $exponent);
  362.     }
  363.     public function generateGuideText()
  364.     {
  365.         $text 'ضمن تشکر از خرید شما دوست عزیز💙'.
  366.             '\n'.
  367.             'برای دریافت آمورش - اپلیکیشن و کانفیگ خود میتوانید با ورود به لینک زیر و وارد کردن شناسه سرویس و کلمه عبور نسبت به دریافت آن اقدام فرمایید...'.
  368.             '\n'.
  369.             '\n'.
  370.             'شناسه سرویس  : '.$this->getClientId().
  371.             '\n'.
  372.             'کلمه عبور : '.$this->getPassword().
  373.             '\n'.
  374.             'حجم خریداری شده : '.$this->formatSizeUnits$this->getPlan()->getSize()).
  375.             '\n'.
  376.             'اتصال دو کاربر همزمان'.
  377.             '\n';
  378.         ;
  379.         return $text;
  380.     }
  381.     public function getRollbackNote(): ?string
  382.     {
  383.         return $this->rollbackNote;
  384.     }
  385.     public function setRollbackNote(?string $rollbackNote): static
  386.     {
  387.         $this->rollbackNote $rollbackNote;
  388.         return $this;
  389.     }
  390.     public function getOcServUsage(): ?string
  391.     {
  392.         return $this->ocServUsage;
  393.     }
  394.     public function setOcServUsage(string $ocServUsage): static
  395.     {
  396.         $this->ocServUsage $ocServUsage;
  397.         return $this;
  398.     }
  399.     public function getV2rayUsage(): ?string
  400.     {
  401.         return $this->v2rayUsage;
  402.     }
  403.     public function setV2rayUsage(string $v2rayUsage): static
  404.     {
  405.         $this->v2rayUsage $v2rayUsage;
  406.         return $this;
  407.     }
  408.     public function getOpenVpnUsage(): ?string
  409.     {
  410.         return $this->openVpnUsage;
  411.     }
  412.     public function setOpenVpnUsage(string $openVpnUsage): static
  413.     {
  414.         $this->openVpnUsage $openVpnUsage;
  415.         return $this;
  416.     }
  417.     public function getServerNumber(): ?string
  418.     {
  419.         return $this->serverNumber;
  420.     }
  421.     public function setServerNumber(?string $serverNumber): static
  422.     {
  423.         $this->serverNumber $serverNumber;
  424.         return $this;
  425.     }
  426.     public function getCountry(): ?string
  427.     {
  428.         return $this->country;
  429.     }
  430.     public function setCountry(?string $country): static
  431.     {
  432.         $this->country $country;
  433.         return $this;
  434.     }
  435.     public function getBotUser(): ?BotUser
  436.     {
  437.         return $this->botUser;
  438.     }
  439.     public function setBotUser(?BotUser $botUser): static
  440.     {
  441.         $this->botUser $botUser;
  442.         return $this;
  443.     }
  444.     public function getCreatorBot(): ?Bot
  445.     {
  446.         return $this->creatorBot;
  447.     }
  448.     public function setCreatorBot(?Bot $creatorBot): static
  449.     {
  450.         $this->creatorBot $creatorBot;
  451.         return $this;
  452.     }
  453.     public function isSizeNotif(): ?bool
  454.     {
  455.         return $this->sizeNotif;
  456.     }
  457.     public function setSizeNotif(bool $sizeNotif): static
  458.     {
  459.         $this->sizeNotif $sizeNotif;
  460.         return $this;
  461.     }
  462.     public function isDayNotif(): ?bool
  463.     {
  464.         return $this->dayNotif;
  465.     }
  466.     public function setDayNotif(bool $dayNotif): static
  467.     {
  468.         $this->dayNotif $dayNotif;
  469.         return $this;
  470.     }
  471.     public function isDoneNotif(): ?bool
  472.     {
  473.         return $this->doneNotif;
  474.     }
  475.     public function setDoneNotif(bool $doneNotif): static
  476.     {
  477.         $this->doneNotif $doneNotif;
  478.         return $this;
  479.     }
  480.     /**
  481.      * @return Collection<int, ServiceUsage>
  482.      */
  483.     public function getServiceUsages(): Collection
  484.     {
  485.         return $this->serviceUsages;
  486.     }
  487.     public function addServiceUsage(ServiceUsage $serviceUsage): static
  488.     {
  489.         if (!$this->serviceUsages->contains($serviceUsage)) {
  490.             $this->serviceUsages->add($serviceUsage);
  491.             $serviceUsage->setService($this);
  492.         }
  493.         return $this;
  494.     }
  495.     public function removeServiceUsage(ServiceUsage $serviceUsage): static
  496.     {
  497.         if ($this->serviceUsages->removeElement($serviceUsage)) {
  498.             // set the owning side to null (unless already changed)
  499.             if ($serviceUsage->getService() === $this) {
  500.                 $serviceUsage->setService(null);
  501.             }
  502.         }
  503.         return $this;
  504.     }
  505. }