puli /
manager
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the puli/manager package. |
||
| 5 | * |
||
| 6 | * (c) Bernhard Schussek <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Puli\Manager\Util; |
||
| 13 | |||
| 14 | use Puli\Manager\Api\FileNotFoundException; |
||
| 15 | use Puli\Manager\Api\InvalidConfigException; |
||
| 16 | use Puli\Manager\Api\NoDirectoryException; |
||
| 17 | use Symfony\Component\Filesystem\Filesystem; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Provides system utilities. |
||
| 21 | * |
||
| 22 | * @since 1.0 |
||
| 23 | * |
||
| 24 | * @author Bernhard Schussek <[email protected]> |
||
| 25 | */ |
||
| 26 | class System |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Parses context variables for Puli's home directory. |
||
| 30 | * |
||
| 31 | * This method scans the context variables "PULI_HOME", "HOME" and |
||
| 32 | * "APPDATA" to determine Puli's home directory: |
||
| 33 | * |
||
| 34 | * * If "PULI_HOME" is found, that directory is used. |
||
| 35 | * * If "HOME" is found, a directory ".puli" is created inside. This |
||
| 36 | * variable contains the path of the user's home directory by default |
||
| 37 | * on Unix based systems. |
||
| 38 | * * If "APPDATA" is found, a directory "Puli" is created inside. This |
||
| 39 | * variable contains the path of the application data by default on |
||
| 40 | * Windows. |
||
| 41 | * |
||
| 42 | * @return string The path to Puli's home directory. |
||
| 43 | * |
||
| 44 | * @throws InvalidConfigException If no context variable can be found to |
||
| 45 | * determine the home directory. |
||
| 46 | * @throws FileNotFoundException If the home directory is not found. |
||
| 47 | * @throws NoDirectoryException If the home directory is not a directory. |
||
| 48 | */ |
||
| 49 | 67 | public static function parseHomeDirectory() |
|
| 50 | { |
||
| 51 | 67 | if ($value = getenv('PULI_HOME')) { |
|
| 52 | 56 | $homeDir = $value; |
|
| 53 | 56 | $env = 'PULI_HOME'; |
|
| 54 | 11 | } elseif ($value = getenv('HOME')) { |
|
| 55 | 4 | $homeDir = $value; |
|
| 56 | 4 | $env = 'HOME'; |
|
| 57 | 7 | } elseif ($value = getenv('APPDATA')) { |
|
| 58 | 4 | $homeDir = $value; |
|
| 59 | 4 | $env = 'APPDATA'; |
|
| 60 | } else { |
||
| 61 | 3 | throw new InvalidConfigException(sprintf( |
|
| 62 | 'Either the context variable PULI_HOME or %s must be set for '. |
||
| 63 | 3 | 'Puli to run.', |
|
| 64 | 3 | defined('PHP_WINDOWS_VERSION_MAJOR') ? 'APPDATA' : 'HOME' |
|
| 65 | )); |
||
| 66 | } |
||
| 67 | |||
| 68 | 64 | $homeDir = strtr($homeDir, array('\\' => '/')); |
|
| 69 | |||
| 70 | 64 | if (!file_exists($homeDir)) { |
|
| 71 | 3 | throw new FileNotFoundException(sprintf( |
|
| 72 | 'The home path %s defined in the context variable %s '. |
||
| 73 | 3 | 'does not exist.', |
|
| 74 | $homeDir, |
||
| 75 | $env |
||
| 76 | )); |
||
| 77 | } |
||
| 78 | |||
| 79 | 61 | if (is_file($homeDir)) { |
|
| 80 | 4 | throw new NoDirectoryException(sprintf( |
|
| 81 | 'The home path %s defined in the context variable %s '. |
||
| 82 | 4 | 'points to a file. Expected a directory path.', |
|
| 83 | $homeDir, |
||
| 84 | $env |
||
| 85 | )); |
||
| 86 | } |
||
| 87 | |||
| 88 | switch ($env) { |
||
| 89 | 57 | case 'PULI_HOME': |
|
| 90 | 53 | return $homeDir; // user defined |
|
| 91 | 2 | case 'HOME': |
|
| 92 | 2 | return $homeDir.'/.puli'; // Linux/Mac |
|
| 93 | default: |
||
| 94 | 2 | return $homeDir.'/Puli'; // Windows |
|
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Denies web access to a directory path. |
||
| 100 | * |
||
| 101 | * A .htaccess file with the contents "Deny from all" is placed in the |
||
| 102 | * directory, unless a .htaccess file exists already. |
||
| 103 | * |
||
| 104 | * @param string $directory The path to a directory. |
||
| 105 | */ |
||
| 106 | 50 | public static function denyWebAccess($directory) |
|
| 107 | { |
||
| 108 | 50 | if (!file_exists($directory.'/.htaccess')) { |
|
| 109 | 50 | if (!is_dir($directory)) { |
|
| 110 | $filesystem = new Filesystem(); |
||
| 111 | $filesystem->mkdir($directory); |
||
| 112 | } |
||
| 113 | |||
| 114 | 50 | @file_put_contents($directory.'/.htaccess', 'Deny from all'); |
|
|
0 ignored issues
–
show
|
|||
| 115 | } |
||
| 116 | 50 | } |
|
| 117 | |||
| 118 | private function __construct() |
||
| 119 | { |
||
| 120 | } |
||
| 121 | } |
||
| 122 |
If you suppress an error, we recommend checking for the error condition explicitly: