1
0

BinaryFinder.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC;
  8. use OCP\IBinaryFinder;
  9. use OCP\ICache;
  10. use OCP\ICacheFactory;
  11. use Symfony\Component\Process\ExecutableFinder;
  12. /**
  13. * Service that find the binary path for a program
  14. */
  15. class BinaryFinder implements IBinaryFinder {
  16. private ICache $cache;
  17. public function __construct(ICacheFactory $cacheFactory) {
  18. $this->cache = $cacheFactory->createLocal('findBinaryPath');
  19. }
  20. /**
  21. * Try to find a program
  22. *
  23. * @return false|string
  24. */
  25. public function findBinaryPath(string $program) {
  26. $result = $this->cache->get($program);
  27. if ($result !== null) {
  28. return $result;
  29. }
  30. $result = false;
  31. if (\OCP\Util::isFunctionEnabled('exec')) {
  32. $exeSniffer = new ExecutableFinder();
  33. // Returns null if nothing is found
  34. $result = $exeSniffer->find($program, null, [
  35. '/usr/local/sbin',
  36. '/usr/local/bin',
  37. '/usr/sbin',
  38. '/usr/bin',
  39. '/sbin',
  40. '/bin',
  41. '/opt/bin',
  42. ]);
  43. if ($result === null) {
  44. $result = false;
  45. }
  46. }
  47. // store the value for 5 minutes
  48. $this->cache->set($program, $result, 300);
  49. return $result;
  50. }
  51. }