|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AssetManager\Resolver; |
| 4 | + |
| 5 | +use SplFileInfo; |
| 6 | +use Traversable; |
| 7 | +use Zend\Stdlib\SplStack; |
| 8 | +use Assetic\Asset\FileAsset; |
| 9 | +use AssetManager\Exception; |
| 10 | +use AssetManager\Service\MimeResolver; |
| 11 | + |
| 12 | +/** |
| 13 | + * This resolver allows you to resolve from a stack of aliases to a path. |
| 14 | + */ |
| 15 | +class AliasPathStackResolver implements ResolverInterface, MimeResolverAwareInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var Array |
| 19 | + */ |
| 20 | + protected $aliases = array(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Flag indicating whether or not LFI protection for rendering view scripts is enabled |
| 24 | + * |
| 25 | + * @var bool |
| 26 | + */ |
| 27 | + protected $lfiProtectionOn = true; |
| 28 | + |
| 29 | + /** |
| 30 | + * The mime resolver. |
| 31 | + * |
| 32 | + * @var MimeResolver |
| 33 | + */ |
| 34 | + protected $mimeResolver; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor |
| 38 | + * |
| 39 | + * Populate the array stack with a list of aliases and their corresponding paths |
| 40 | + * |
| 41 | + * @param array $aliases |
| 42 | + * @throws Exception\InvalidArgumentException |
| 43 | + */ |
| 44 | + public function __construct(Array $aliases) |
| 45 | + { |
| 46 | + foreach ($aliases as $alias => $path) { |
| 47 | + $this->addAlias($alias, $path); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Add a single alias to the stack |
| 53 | + * |
| 54 | + * @param string $alias |
| 55 | + * @param string $path |
| 56 | + * @throws Exception\InvalidArgumentException |
| 57 | + */ |
| 58 | + private function addAlias($alias, $path) |
| 59 | + { |
| 60 | + if (!is_string($path)) { |
| 61 | + throw new Exception\InvalidArgumentException(sprintf( |
| 62 | + 'Invalid path provided; must be a string, received %s', |
| 63 | + gettype($path) |
| 64 | + )); |
| 65 | + } |
| 66 | + |
| 67 | + if (!is_string($alias)) { |
| 68 | + throw new Exception\InvalidArgumentException(sprintf( |
| 69 | + 'Invalid alias provided; must be a string, received %s', |
| 70 | + gettype($alias) |
| 71 | + )); |
| 72 | + } |
| 73 | + |
| 74 | + $this->aliases[$alias] = $this->normalizePath($path); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Normalize a path for insertion in the stack |
| 79 | + * |
| 80 | + * @param string $path |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + private function normalizePath($path) |
| 84 | + { |
| 85 | + return rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the mime resolver |
| 90 | + * |
| 91 | + * @param MimeResolver $resolver |
| 92 | + */ |
| 93 | + public function setMimeResolver(MimeResolver $resolver) |
| 94 | + { |
| 95 | + $this->mimeResolver = $resolver; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the mime resolver |
| 100 | + * |
| 101 | + * @return MimeResolver |
| 102 | + */ |
| 103 | + public function getMimeResolver() |
| 104 | + { |
| 105 | + return $this->mimeResolver; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Set LFI protection flag |
| 110 | + * |
| 111 | + * @param bool $flag |
| 112 | + * @return self |
| 113 | + */ |
| 114 | + public function setLfiProtection($flag) |
| 115 | + { |
| 116 | + $this->lfiProtectionOn = (bool) $flag; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Return status of LFI protection flag |
| 121 | + * |
| 122 | + * @return bool |
| 123 | + */ |
| 124 | + public function isLfiProtectionOn() |
| 125 | + { |
| 126 | + return $this->lfiProtectionOn; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritDoc} |
| 131 | + */ |
| 132 | + public function resolve($name) |
| 133 | + { |
| 134 | + if ($this->isLfiProtectionOn() && preg_match('#\.\.[\\\/]#', $name)) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + foreach ($this->aliases as $alias => $path) { |
| 139 | + if (strpos($name, $alias) === false) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + $name = str_replace($alias, '', $name); |
| 144 | + |
| 145 | + $file = new SplFileInfo($path . $name); |
| 146 | + |
| 147 | + if ($file->isReadable() && !$file->isDir()) { |
| 148 | + $filePath = $file->getRealPath(); |
| 149 | + $mimeType = $this->getMimeResolver()->getMimeType($filePath); |
| 150 | + $asset = new FileAsset($filePath); |
| 151 | + |
| 152 | + $asset->mimetype = $mimeType; |
| 153 | + |
| 154 | + return $asset; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return null; |
| 159 | + } |
| 160 | +} |
0 commit comments