OS.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Songshenzong\Support;
  3. use Stringy\Stringy;
  4. /**
  5. * Class OS
  6. *
  7. * @package Songshenzong\Support
  8. */
  9. class OS
  10. {
  11. /**
  12. * @param $filename
  13. *
  14. * @return bool
  15. */
  16. public static function inOpenBasedir($filename)
  17. {
  18. $open_basedir = ini_get('open_basedir');
  19. if (!$open_basedir) {
  20. return true;
  21. }
  22. $dirs = explode(PATH_SEPARATOR, $open_basedir);
  23. return empty($dirs) || self::inDir($filename, $dirs);
  24. }
  25. /**
  26. * @param string $filename
  27. * @param array $dirs
  28. *
  29. * @return bool
  30. */
  31. public static function inDir($filename, array $dirs)
  32. {
  33. foreach ($dirs as $dir) {
  34. if (!Stringy::create($dir)->endsWith(DIRECTORY_SEPARATOR)) {
  35. $dir .= DIRECTORY_SEPARATOR;
  36. }
  37. if (0 === strpos($filename, $dir)) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. /**
  44. * @return bool
  45. */
  46. public static function isWindows()
  47. {
  48. return PATH_SEPARATOR === ';';
  49. }
  50. /**
  51. * Gets the environment's HOME directory.
  52. *
  53. * @return null|string
  54. */
  55. public static function getHomeDirectory()
  56. {
  57. if (getenv('HOME')) {
  58. return getenv('HOME');
  59. }
  60. return (getenv('HOMEDRIVE') && getenv('HOMEPATH'))
  61. ? getenv('HOMEDRIVE') . getenv('HOMEPATH')
  62. : null;
  63. }
  64. }