Advertisement
Guest User

Untitled

a guest
Oct 18th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. namespace DBTech\eCommerce\Service\Product;
  6.  
  7.  
  8.  
  9. use DBTech\eCommerce\Entity\Product;
  10.  
  11. use XF\App;
  12.  
  13. use XF\Http\Upload;
  14.  
  15. use XF\Image\Imagick;
  16.  
  17. use XF\Phrase;
  18.  
  19. use XF\PrintableException;
  20.  
  21. use XF\Repository\IpRepository;
  22.  
  23. use XF\Service\AbstractService;
  24.  
  25. use XF\Util\File;
  26.  
  27.  
  28.  
  29. class IconService extends AbstractService
  30.  
  31. {
  32.  
  33. protected Product $product;
  34.  
  35. protected bool $logIp = true;
  36.  
  37. protected ?string $fileName = null;
  38.  
  39. protected string $extension;
  40.  
  41. protected int $width;
  42.  
  43. protected int $height;
  44.  
  45. protected int $type;
  46.  
  47. protected ?Phrase $error = null;
  48.  
  49. protected array $allowedTypes = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WEBP];
  50.  
  51.  
  52.  
  53.  
  54.  
  55. /**
  56.  
  57. * @param App $app
  58.  
  59. * @param Product $product
  60.  
  61. */
  62.  
  63. public function __construct(App $app, Product $product)
  64.  
  65. {
  66.  
  67. parent::__construct($app);
  68.  
  69. $this->product = $product;
  70.  
  71. }
  72.  
  73.  
  74.  
  75. /**
  76.  
  77. * @return Product
  78.  
  79. */
  80.  
  81. public function getProduct(): Product
  82.  
  83. {
  84.  
  85. return $this->product;
  86.  
  87. }
  88.  
  89.  
  90.  
  91. /**
  92.  
  93. * @param bool $logIp
  94.  
  95. */
  96.  
  97. public function logIp(bool $logIp): void
  98.  
  99. {
  100.  
  101. $this->logIp = $logIp;
  102.  
  103. }
  104.  
  105.  
  106.  
  107. /**
  108.  
  109. * @return Phrase|null
  110.  
  111. */
  112.  
  113. public function getError(): ?Phrase
  114.  
  115. {
  116.  
  117. return $this->error;
  118.  
  119. }
  120.  
  121.  
  122.  
  123. /**
  124.  
  125. * @param string $fileName
  126.  
  127. *
  128.  
  129. * @return bool
  130.  
  131. * @throws \InvalidArgumentException
  132.  
  133. */
  134.  
  135. public function setImage(string $fileName): bool
  136.  
  137. {
  138.  
  139. if (!$this->validateImageAsIcon($fileName, $error))
  140.  
  141. {
  142.  
  143. $this->error = $error;
  144.  
  145. $this->fileName = null;
  146.  
  147. return false;
  148.  
  149. }
  150.  
  151.  
  152.  
  153. $this->fileName = $fileName;
  154.  
  155. return true;
  156.  
  157. }
  158.  
  159.  
  160.  
  161. /**
  162.  
  163. * @param string $fileName
  164.  
  165. *
  166.  
  167. * @return bool
  168.  
  169. */
  170.  
  171. public function setSvgImage(string $fileName): bool
  172.  
  173. {
  174.  
  175. $this->_setImageBypass($fileName);
  176.  
  177. return true;
  178.  
  179. }
  180.  
  181.  
  182.  
  183. /**
  184.  
  185. * @param string $fileName
  186.  
  187. *
  188.  
  189. * @return bool
  190.  
  191. */
  192.  
  193. public function setWebpImage(string $fileName): bool
  194.  
  195. {
  196.  
  197. $this->_setImageBypass($fileName);
  198.  
  199. return true;
  200.  
  201. }
  202.  
  203.  
  204.  
  205. /**
  206.  
  207. * @param string $fileName
  208.  
  209. *
  210.  
  211. * @return void
  212.  
  213. */
  214.  
  215. protected function _setImageBypass(string $fileName): void
  216.  
  217. {
  218.  
  219. $this->width = \XF::app()->options()->dbtechEcommerceProductIconMaxDimensions['width'] ?:
  220.  
  221. \XF::app()->container('avatarSizeMap')['l'];
  222.  
  223.  
  224.  
  225. $this->height = \XF::app()->options()->dbtechEcommerceProductIconMaxDimensions['height'] ?:
  226.  
  227. \XF::app()->container('avatarSizeMap')['l'];
  228.  
  229.  
  230.  
  231. $this->fileName = $fileName;
  232.  
  233. }
  234.  
  235.  
  236.  
  237. /**
  238.  
  239. * @param Upload $upload
  240.  
  241. *
  242.  
  243. * @return bool
  244.  
  245. * @throws \InvalidArgumentException
  246.  
  247. */
  248.  
  249. public function setImageFromUpload(Upload $upload): bool
  250.  
  251. {
  252.  
  253. $this->extension = strtolower($upload->getExtension());
  254.  
  255. if ($this->extension === 'svg')
  256.  
  257. {
  258.  
  259. return $this->setSvgImage($upload->getTempFile());
  260.  
  261. }
  262.  
  263. else if ($this->extension === 'webp' && !\XF::isAddOnActive('DBTech/WebP'))
  264.  
  265. {
  266.  
  267. return $this->setWebpImage($upload->getTempFile());
  268.  
  269. }
  270.  
  271. else
  272.  
  273. {
  274.  
  275. $upload->requireImage();
  276.  
  277.  
  278.  
  279. if (!$upload->isValid($errors))
  280.  
  281. {
  282.  
  283. $this->error = reset($errors);
  284.  
  285. return false;
  286.  
  287. }
  288.  
  289.  
  290.  
  291. return $this->setImage($upload->getTempFile());
  292.  
  293. }
  294.  
  295. }
  296.  
  297.  
  298.  
  299. public function setImageFromExisting(): bool
  300.  
  301. {
  302.  
  303. $this->extension = $this->product->icon_extension;
  304.  
  305. $path = $this->product->getAbstractedIconPath(null, $this->extension);
  306.  
  307. if (!\XF::app()->fs()->has($path))
  308.  
  309. {
  310.  
  311. throw new \InvalidArgumentException(
  312.  
  313. "Product does not have an icon ({$path})"
  314.  
  315. );
  316.  
  317. }
  318.  
  319.  
  320.  
  321. $tempFile = File::copyAbstractedPathToTempFile($path);
  322.  
  323.  
  324.  
  325. return $this->setImage($tempFile);
  326.  
  327. }
  328.  
  329.  
  330.  
  331. /**
  332.  
  333. * @param string $fileName
  334.  
  335. * @param null $error
  336.  
  337. *
  338.  
  339. * @return bool
  340.  
  341. * @throws \InvalidArgumentException
  342.  
  343. */
  344.  
  345. public function validateImageAsIcon(string $fileName, &$error = null): bool
  346.  
  347. {
  348.  
  349. $error = null;
  350.  
  351.  
  352.  
  353. if (!file_exists($fileName))
  354.  
  355. {
  356.  
  357. throw new \InvalidArgumentException("Invalid file '$fileName' passed to icon service");
  358.  
  359. }
  360.  
  361. if (!is_readable($fileName))
  362.  
  363. {
  364.  
  365. throw new \InvalidArgumentException("'$fileName' passed to icon service is not readable");
  366.  
  367. }
  368.  
  369.  
  370.  
  371. $imageInfo = filesize($fileName) ? getimagesize($fileName) : false;
  372.  
  373. if (!$imageInfo)
  374.  
  375. {
  376.  
  377. $error = \XF::phrase('provided_file_is_not_valid_image');
  378.  
  379. return false;
  380.  
  381. }
  382.  
  383.  
  384.  
  385. $type = $imageInfo[2];
  386.  
  387. if (!in_array($type, $this->allowedTypes))
  388.  
  389. {
  390.  
  391. $error = \XF::phrase('provided_file_is_not_valid_image');
  392.  
  393. return false;
  394.  
  395. }
  396.  
  397.  
  398.  
  399. [$width, $height] = $imageInfo;
  400.  
  401.  
  402.  
  403. if (!\XF::app()->imageManager()->canResize($width, $height))
  404.  
  405. {
  406.  
  407. $error = \XF::phrase('uploaded_image_is_too_big');
  408.  
  409. return false;
  410.  
  411. }
  412.  
  413.  
  414.  
  415. $this->width = $width;
  416.  
  417. $this->height = $height;
  418.  
  419.  
  420.  
  421. return true;
  422.  
  423. }
  424.  
  425.  
  426.  
  427. // This function has been edited by MYCODE
  428.  
  429. public function updateIcon(): bool
  430.  
  431. {
  432.  
  433. if (!$this->fileName) {
  434.  
  435. throw new \LogicException('No source file for icon set');
  436.  
  437. }
  438.  
  439.  
  440.  
  441. $imageManager = \XF::app()->imageManager();
  442.  
  443.  
  444.  
  445. $targetWidth = \XF::app()->options()->dbtechEcommerceProductIconMaxDimensions['width'] ?:
  446.  
  447. \XF::app()->container('avatarSizeMap')['l'];
  448.  
  449.  
  450.  
  451. $targetHeight = \XF::app()->options()->dbtechEcommerceProductIconMaxDimensions['height'] ?:
  452.  
  453. \XF::app()->container('avatarSizeMap')['l'];
  454.  
  455.  
  456.  
  457. $outputFile = null;
  458.  
  459.  
  460.  
  461. if ($this->extension === 'svg') {
  462.  
  463. // Handle SVG specifically
  464.  
  465. $svgContent = file_get_contents($this->fileName);
  466.  
  467. if (!$svgContent) {
  468.  
  469. throw new \RuntimeException('Failed to read SVG file');
  470.  
  471. }
  472.  
  473.  
  474.  
  475. // Basic validation for SVG content
  476.  
  477. if (strpos($svgContent, '<svg') === false) {
  478.  
  479. throw new \InvalidArgumentException('File is not a valid SVG');
  480.  
  481. }
  482.  
  483.  
  484.  
  485. // Optimize SVG if necessary (you might need an SVG optimization library like SVGO)
  486.  
  487. // This is a placeholder for SVG optimization
  488.  
  489. if ($this->app->options()->imageOptimization === 'optimize') {
  490.  
  491. // Here you would implement SVG optimization. For now, it's just kept as a comment.
  492.  
  493. // $optimizedSvg = // optimize svg content here
  494.  
  495. }
  496.  
  497.  
  498.  
  499. // Assuming no changes needed for SVG content in this context, keep original
  500.  
  501. $outputFile = $this->fileName; // Use original SVG file directly
  502.  
  503.  
  504.  
  505. // Unlike raster images, vector SVGs don't need resizing in the same way
  506.  
  507. } else {
  508.  
  509. // Handle non-SVG (raster) images
  510.  
  511. $baseImage = $imageManager->imageFromFile($this->fileName);
  512.  
  513. if (!$baseImage) {
  514.  
  515. throw new \RuntimeException('Failed to load image from file');
  516.  
  517. }
  518.  
  519.  
  520.  
  521. $isOptimized = $baseImage->getType() === IMAGETYPE_WEBP;
  522.  
  523.  
  524.  
  525. unset($baseImage);
  526.  
  527.  
  528.  
  529. if ($this->width != $targetWidth || $this->height != $targetHeight) {
  530.  
  531. $image = $imageManager->imageFromFile($this->fileName);
  532.  
  533. if (!$image) {
  534.  
  535. throw new \RuntimeException('Failed to load image from file for resizing');
  536.  
  537. }
  538.  
  539.  
  540.  
  541. $image->resizeAndCrop($targetWidth, $targetHeight);
  542.  
  543.  
  544.  
  545. $newTempFile = File::getTempFile();
  546.  
  547. if ($newTempFile && $image->save($newTempFile)) {
  548.  
  549. $outputFile = $newTempFile;
  550.  
  551. }
  552.  
  553. } else {
  554.  
  555. $outputFile = $this->fileName;
  556.  
  557. }
  558.  
  559. }
  560.  
  561.  
  562.  
  563. if (!$outputFile) {
  564.  
  565. throw new \RuntimeException('Failed to save image to temporary file; check internal_data/data permissions');
  566.  
  567. }
  568.  
  569.  
  570.  
  571. $dataFile = $this->product->getAbstractedIconPath(null, $this->extension);
  572.  
  573. File::copyFileToAbstractedPath($outputFile, $dataFile);
  574.  
  575.  
  576.  
  577. $this->product->icon_date = \XF::$time;
  578.  
  579. $this->product->icon_extension = $this->extension;
  580.  
  581. $this->product->icon_optimized = $this->extension === 'svg' || $isOptimized;
  582.  
  583. $this->product->save();
  584.  
  585.  
  586.  
  587. if ($this->logIp) {
  588.  
  589. $ip = ($this->logIp === true ? \XF::app()->request()->getIp() : $this->logIp);
  590.  
  591. $this->writeIpLog('update', $ip);
  592.  
  593. }
  594.  
  595.  
  596.  
  597. return true;
  598.  
  599. }
  600.  
  601.  
  602.  
  603. /**
  604.  
  605. * @return void
  606.  
  607. * @throws PrintableException
  608.  
  609. */
  610.  
  611. public function optimizeExistingIcon(): void
  612.  
  613. {
  614.  
  615. if ($this->app->options()->imageOptimization !== 'optimize')
  616.  
  617. {
  618.  
  619. return;
  620.  
  621. }
  622.  
  623.  
  624.  
  625. $this->setImageFromExisting();
  626.  
  627.  
  628.  
  629. $imageManager = $this->app->imageManager();
  630.  
  631.  
  632.  
  633. $image = $imageManager->imageFromFile($this->fileName);
  634.  
  635. if (!$image)
  636.  
  637. {
  638.  
  639. return;
  640.  
  641. }
  642.  
  643.  
  644.  
  645. $success = $image->optimizeImage($this->fileName);
  646.  
  647. if ($success)
  648.  
  649. {
  650.  
  651. // We can change extension to webp
  652.  
  653. $this->extension = 'webp';
  654.  
  655. $this->updateIcon();
  656.  
  657. }
  658.  
  659. }
  660.  
  661.  
  662.  
  663. /**
  664.  
  665. * @return bool
  666.  
  667. * @throws \LogicException
  668.  
  669. * @throws \Exception
  670.  
  671. * @throws PrintableException
  672.  
  673. */
  674.  
  675. public function deleteIcon(): bool
  676.  
  677. {
  678.  
  679. $this->deleteIconFiles();
  680.  
  681.  
  682.  
  683. $this->product->icon_date = 0;
  684.  
  685. $this->product->icon_optimized = false;
  686.  
  687. $this->product->save();
  688.  
  689.  
  690.  
  691. if ($this->logIp)
  692.  
  693. {
  694.  
  695. $ip = ($this->logIp === true ? \XF::app()->request()->getIp() : $this->logIp);
  696.  
  697. $this->writeIpLog('delete', $ip);
  698.  
  699. }
  700.  
  701.  
  702.  
  703. return true;
  704.  
  705. }
  706.  
  707.  
  708.  
  709. /**
  710.  
  711. * @return bool
  712.  
  713. */
  714.  
  715. public function deleteIconForProductDelete(): bool
  716.  
  717. {
  718.  
  719. $this->deleteIconFiles();
  720.  
  721.  
  722.  
  723. return true;
  724.  
  725. }
  726.  
  727.  
  728.  
  729. /**
  730.  
  731. *
  732.  
  733. */
  734.  
  735. protected function deleteIconFiles(): void
  736.  
  737. {
  738.  
  739. if ($this->product->icon_date)
  740.  
  741. {
  742.  
  743. File::deleteFromAbstractedPath($this->product->getAbstractedIconPath());
  744.  
  745. }
  746.  
  747. }
  748.  
  749.  
  750.  
  751. /**
  752.  
  753. * @param string $action
  754.  
  755. * @param string $ip
  756.  
  757. */
  758.  
  759. protected function writeIpLog(string $action, string $ip): void
  760.  
  761. {
  762.  
  763. $product = $this->product;
  764.  
  765.  
  766.  
  767. $ipRepo = \XF::app()->repository(IpRepository::class);
  768.  
  769. $ipRepo->logIp(
  770.  
  771. \XF::visitor()->user_id,
  772.  
  773. $ip,
  774.  
  775. 'dbtech_ecommerce_product',
  776.  
  777. $product->product_id,
  778.  
  779. 'icon_' . $action
  780.  
  781. );
  782.  
  783. }
  784.  
  785. }
  786.  
  787.  
  788.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement